/// <summary> /// Unloads this instance. /// </summary> internal void Unload() { Debug.Assert(this.loadState == LoadState.Loaded); OnUnloading(); this.texture.Dispose(); this.texture = null; this.LoadState = LoadState.Unloaded; #if DETECT_LEAKS var o = SharpDX.Diagnostics.ObjectTracker.FindActiveObjects(); foreach (var ob in o) { if (ob.IsAlive) { Texture t = ob.Object.Target as Texture; if (t != null) { System.Diagnostics.Debug.Assert(t.DebugName != Name); } } } #endif //slowdown //Debug.WriteLine(string.Format("Texture {0} unloaded.", this.Name)); }
/// <summary> /// Save a texture from memory to a file. /// (Supported formats : Dxt1,Dxt3,Dxt5,A8R8G8B8/Color,A4R4G4B4,A1R5G5B5,R5G6B5,A8, /// FP32/Single,FP16/HalfSingle,FP32x4/Vector4,FP16x4/HalfVector4,CxV8U8/NormalizedByte2/CxVU,Q8VW8V8U8/NormalizedByte4/8888QWVU /// ,HalfVector2/G16R16F/16.16fGR,Vector2/G32R32F,G16R16/RG32/1616GB,A8B8G8R8,A2B10G10R10/Rgba1010102,A16B16G16R16/Rgba64) /// </summary> /// <param name="fileName">The name of the file where you want to save the texture.</param> /// <param name="saveMipMaps">Save the complete mip-map chain ?</param> /// <param name="texture">The texture that you want to save.</param> /// <param name="throwExceptionIfFileExist">Throw an exception if the file exists ?</param> public static void DDSToFile(string fileName, bool saveMipMaps, BaseTexture texture, bool throwExceptionIfFileExist) { if (throwExceptionIfFileExist && File.Exists(fileName)) { throw new Exception("The file allready exists and \"throwExceptionIfFileExist\" is true"); } Stream fileStream = null; try { fileStream = File.Create(fileName); BaseTexture.ToStream(texture, ImageFileFormat.Dds); //DDSToStream(fileStream, 0, saveMipMaps, texture); // sometimes needed because of out of memory and this helps //GC.Collect(2); } catch (Exception x) { throw x; } finally { if (fileStream != null) { fileStream.Close(); fileStream = null; } } }
public void Bind(D3D9.Device dev, D3D9.Volume volume, D3D9.BaseTexture mipTex) { //Entering critical section LockDeviceAccess(); var bufferResources = GetBufferResources(dev); var isNewBuffer = false; if (bufferResources == null) { bufferResources = new BufferResources(); this.mapDeviceToBufferResources.Add(dev, bufferResources); isNewBuffer = true; } bufferResources.MipTex = mipTex; bufferResources.Volume = volume; var desc = volume.Description; width = desc.Width; height = desc.Height; depth = desc.Depth; format = D3D9Helper.ConvertEnum(desc.Format); // Default rowPitch = Width; slicePitch = Height * Width; sizeInBytes = PixelUtil.GetMemorySize(Width, Height, Depth, Format); if (isNewBuffer && this.ownerTexture.IsManuallyLoaded) { foreach (var it in this.mapDeviceToBufferResources) { if (it.Value != bufferResources && it.Value.Volume != null && it.Key.TestCooperativeLevel().Success&& dev.TestCooperativeLevel().Success) { var fullBufferBox = new BasicBox(0, 0, 0, Width, Height, Depth); var dstBox = new PixelBox(fullBufferBox, Format); var data = new byte[sizeInBytes]; using (var d = BufferBase.Wrap(data)) { dstBox.Data = d; BlitToMemory(fullBufferBox, dstBox, it.Value, it.Key); BlitFromMemory(dstBox, fullBufferBox, bufferResources); Array.Clear(data, 0, sizeInBytes); } break; } } } //Leaving critical section UnlockDeviceAccess(); }
/// <summary> /// Reloads this instance. /// </summary> internal bool Load(TextureQuality quality = 0) { Debug.Assert(this.LoadState != LoadState.Loaded); MyMwcLog.WriteLine(string.Format("Loading texture {0} ...", this.Name), SysUtils.LoggingOptions.LOADING_TEXTURES); var ddsTexture = new FileInfo(MyMinerGame.Static.RootDirectory + "\\" + this.Name + ".dds"); // System.Diagnostics.Debug.Assert(ddsTexture.Exists); if (ddsTexture.Exists) { this.texture = LoadDDSTexture(ddsTexture.FullName, quality);// : LoadXNATexture(this.Name); } else { var pngTexture = new FileInfo(MyMinerGame.Static.RootDirectory + "\\" + this.Name + ".png"); if (pngTexture.Exists) { this.texture = LoadPNGTexture(pngTexture.FullName);// : LoadXNATexture(this.Name); } else { this.LoadState = LoadState.Error; string s = "Texture " + this.Name + " is missing."; System.Diagnostics.Debug.Assert(pngTexture.Exists, s); return(false); } } if (this.texture == null) { this.LoadState = LoadState.Error; return(false); } this.texture.DebugName = this.Name; this.LoadState = LoadState.Loaded; // if (Name.Contains("Textures\\GUI\\Loading")) // { //} UpdateProperties(texture); MyMwcLog.WriteLine(string.Format("Texture {0} loaded", this.Name), SysUtils.LoggingOptions.LOADING_TEXTURES); OnLoaded(); return(true); }
/// <summary> /// Unloads this instance. /// </summary> internal void Unload() { Debug.Assert(this.loadState == LoadState.Loaded); OnUnloading(); this.texture.Dispose(); this.texture = null; this.LoadState = LoadState.Unloaded; //slowdown //Debug.WriteLine(string.Format("Texture {0} unloaded.", this.Name)); }
bool Load(string contentDir, TextureQuality quality, bool canBeMissing) { string ext = Path.GetExtension(Name); if (String.IsNullOrEmpty(ext)) { Debug.Fail("Texture without extension: " + Name); Name += ".dds"; ext = ".dds"; } string path = Path.Combine(contentDir, Name); if (MyFileSystem.FileExists(path)) { try { if (ext.Equals(".dds", StringComparison.InvariantCultureIgnoreCase)) { this.texture = LoadDDSTexture(path, quality); } else if (ext.Equals(".png", StringComparison.InvariantCultureIgnoreCase)) { this.texture = LoadPNGTexture(path); } else { Debug.Fail("Unsupported texture format: " + path); MyRender.Log.WriteLine(String.Format("Unsupported texture format: {0}", path)); } } catch (SharpDXException e) { MyRender.Log.WriteLine(String.Format("Error decoding texture, file might be corrupt, quality {1}: {0}", path, quality)); } catch (Exception e) { MyRender.Log.WriteLine(String.Format("Error loading texture, quality {1}: {0}", path, quality)); throw new ApplicationException("Error loading texture: " + path, e); } } if (!canBeMissing && this.texture == null) { this.LoadState = LoadState.Error; return(false); } return(true); }
internal void GenMipmaps(D3D9.BaseTexture mipTex) { Debug.Assert(mipTex != null); // Mipmapping if (this.HWMipmaps) { // Hardware mipmaps mipTex.GenerateMipSubLevels(); } else { // Software mipmaps mipTex.FilterTexture((int)D3D9.Filter.Default, D3D9.Filter.Default); } }
/// <summary> /// Unloads this instance. /// </summary> internal void Unload() { Debug.Assert(this.loadState == LoadState.Loaded); OnUnloading(); this.texture.Dispose(); this.texture = null; this.LoadState = LoadState.Unloaded; #if DETECT_LEAKS var o = SharpDX.Diagnostics.ObjectTracker.FindActiveObjects(); foreach (var ob in o) { if (ob.IsAlive) { Texture t = ob.Object.Target as Texture; if (t != null) { System.Diagnostics.Debug.Assert(t.DebugName != Name); } } } #endif //slowdown //Debug.WriteLine(string.Format("Texture {0} unloaded.", this.Name)); }
protected void UpdateProperties(BaseTexture texture) { SurfaceDescription desc; Texture tex = texture as Texture; if (tex != null) desc = tex.GetLevelDescription(0); else { CubeTexture ctex = texture as CubeTexture; desc = ctex.GetLevelDescription(0); } Width = desc.Width; Height = desc.Height; Format = desc.Format; }
/// <summary> /// Reloads this instance. /// </summary> internal bool Load(TextureQuality quality = 0) { Debug.Assert(this.LoadState != LoadState.Loaded); MyMwcLog.WriteLine(string.Format("Loading texture {0} ...", this.Name), SysUtils.LoggingOptions.LOADING_TEXTURES); var ddsTexture = new FileInfo(MyMinerGame.Static.RootDirectory + "\\" + this.Name + ".dds"); // System.Diagnostics.Debug.Assert(ddsTexture.Exists); if (ddsTexture.Exists) { this.texture = LoadDDSTexture(ddsTexture.FullName, quality);// : LoadXNATexture(this.Name); } else { var pngTexture = new FileInfo(MyMinerGame.Static.RootDirectory + "\\" + this.Name + ".png"); if (pngTexture.Exists) { this.texture = LoadPNGTexture(pngTexture.FullName);// : LoadXNATexture(this.Name); } else { this.LoadState = LoadState.Error; string s = "Texture " + this.Name + " is missing."; System.Diagnostics.Debug.Assert(pngTexture.Exists, s); return false; } } if (this.texture == null) { this.LoadState = LoadState.Error; return false; } this.texture.DebugName = this.Name; this.LoadState = LoadState.Loaded; // if (Name.Contains("Textures\\GUI\\Loading")) // { //} UpdateProperties(texture); MyMwcLog.WriteLine(string.Format("Texture {0} loaded", this.Name), SysUtils.LoggingOptions.LOADING_TEXTURES); OnLoaded(); return true; }
/// <summary> /// Unloads this instance. /// </summary> internal void Unload() { Debug.Assert(this.loadState == LoadState.Loaded); OnUnloading(); this.texture.Dispose(); this.texture = null; this.LoadState = LoadState.Unloaded; //slowdown //Debug.WriteLine(string.Format("Texture {0} unloaded.", this.Name)); }
/// <summary> /// Initializes a new instance of the <see cref="Sprite"/> class. /// </summary> /// <param name="texture">The texture.</param> /// <param name="position">The position.</param> public Sprite(BaseTexture texture, Vector2 position) : this() { UpdateTextureBitmap( (Bitmap) Image.FromStream(BaseTexture.ToStream(texture, ImageFileFormat.Bmp)), position); }
internal static void DrawSprite(BaseTexture texture, CubeMapFace? face, ref RectangleF destination, bool scaleDestination, ref Rectangle? sourceRectangle, Color color, Vector2 rightVector, ref Vector2 origin, VRageRender.Graphics.SpriteEffects effects, float depth) { DrawSpriteMain(texture, face, ref destination, scaleDestination, sourceRectangle, color, rightVector, ref origin, effects, depth); }
bool Load(string contentDir, TextureQuality quality, bool canBeMissing) { string ext = Path.GetExtension(Name); if (String.IsNullOrEmpty(ext)) { Debug.Fail("Texture without extension: " + Name); Name += ".dds"; ext = ".dds"; } string path = Path.Combine(contentDir, Name); if (MyFileSystem.FileExists(path)) { try { if (ext.Equals(".dds", StringComparison.InvariantCultureIgnoreCase)) { this.texture = LoadDDSTexture(path, quality); } else if (ext.Equals(".png", StringComparison.InvariantCultureIgnoreCase)) { this.texture = LoadPNGTexture(path); } else { Debug.Fail("Unsupported texture format: " + path); MyRender.Log.WriteLine(String.Format("Unsupported texture format: {0}", path)); } } catch (SharpDXException ) { MyRender.Log.WriteLine(String.Format("Error decoding texture, file might be corrupt, quality {1}: {0}", path, quality)); } catch (Exception e) { MyRender.Log.WriteLine(String.Format("Error loading texture, quality {1}: {0}", path, quality)); throw new ApplicationException("Error loading texture: " + path, e); } } if (!canBeMissing && this.texture == null) { this.LoadState = LoadState.Error; return false; } return true; }
void Hook_OnCreateTexture(ref IntPtr devicePointer, ref int width, ref int height, ref int levels, ref Usage usage, ref Format format, ref Pool pool, ref IntPtr texture, ref IntPtr sharedHandle) { Log.LogMethodSignatureTypesAndValues(devicePointer, width, height, levels, usage, format, pool, "out", sharedHandle); IntPtr _devicePointer = devicePointer; int _width = width; int _height = height; int _levels = levels; Usage _usage = usage; Format _format = format; Pool _pool = pool; IntPtr _sharedHandle = sharedHandle; IntPtr _texturePointer = IntPtr.Zero; try { { lock (lock1) { Task.Factory.StartNew(() => { try { var baseTexture = new BaseTexture(_texturePointer); if (baseTexture.TypeInfo == ResourceType.Texture && numSaves > 0) { var dataStream = Texture.ToStream(baseTexture, ImageFileFormat.Png); var hash = dataStream.GetMD5HashString(); this.Log.Info("Got hash: {0}.", hash); if (!this.TextureDictionary.ContainsKey(hash)) { TextureDictionary.Add(hash, new Texture(_texturePointer)); this.Log.Info("Saving file with hash {0}.", hash); Texture.ToFile(baseTexture, String.Format( @"C:\Sc2Ai\Textures\{0}.png", hash.Substring(0, hash .Length - 2)), ImageFileFormat.Png); numSaves--; } } } catch (Exception ex) { Log.Warn(ex); } }, TaskCreationOptions.LongRunning); } } } catch (Exception ex) { Log.Warn(ex); } }
private static void DrawSpriteMain(BaseTexture texture, CubeMapFace? face, ref RectangleF destination, bool scaleDestination, Rectangle? sourceRectangle, Color color, Vector2 rightVector, ref Vector2 origin, SpriteEffects effects, float depth) { if (m_screenshot != null && m_screenshot.IgnoreSprites) return; m_spriteBatch.DrawSprite( texture, face, ref destination, scaleDestination, sourceRectangle, color, rightVector, ref origin, effects, depth); }
private void Hook_OnSetTexture(ref IntPtr devicePointer, ref int stage, ref IntPtr texturePointer) { Log.LogMethodSignatureTypesAndValues(devicePointer, stage, texturePointer); IntPtr _devicePointer = devicePointer; int _stage = stage; IntPtr _texturePointer = texturePointer; try { { lock (lock1) { Task.Factory.StartNew(() => { try { var baseTexture = new BaseTexture(_texturePointer); if (baseTexture.TypeInfo == ResourceType.Texture && numSaves > 0) { var dataStream = Texture.ToStream(baseTexture, ImageFileFormat.Png); var hash = dataStream.GetMD5HashString(); this.Log.Info("Got hash: {0}.", hash); if (!this.TextureDictionary.ContainsKey(hash)) { TextureDictionary.Add(hash, new Texture(_texturePointer)); this.Log.Info("Saving file with hash {0}.", hash); Texture.ToFile(baseTexture, String.Format( @"C:\Sc2Ai\Textures\{0}.png", hash.Substring(0, hash .Length - 2)), ImageFileFormat.Png); numSaves--; } } } catch (Exception ex) { Log.Warn(ex); } }, TaskCreationOptions.LongRunning); } } } catch (Exception ex) { Log.Warn(ex); } }
public void NativeSetTexture(string name, BaseTexture tex) { handle.SetTexture(name, tex); }
internal static void TakeScreenshot(string name, BaseTexture target, MyEffectScreenshot.ScreenshotTechniqueEnum technique) { if (ScreenshotOnlyFinal && name != "FinalScreen") return; // Screenshot object survives only one DRAW after created. We delete it immediatelly. So if 'm_screenshot' // is not null we know we have to take screenshot and set it to null. if (MyGuiManager.GetScreenshot() != null) { if (target is Texture) { Texture renderTarget = target as Texture; Texture rt = new Texture(m_device, renderTarget.GetLevelDescription(0).Width, renderTarget.GetLevelDescription(0).Height, 0, Usage.RenderTarget, Format.A8R8G8B8, Pool.Default); MyMinerGame.SetRenderTarget(rt, null); BlendState.NonPremultiplied.Apply(); MyEffectScreenshot ssEffect = GetEffect(MyEffects.Screenshot) as MyEffectScreenshot; ssEffect.SetSourceTexture(renderTarget); ssEffect.SetTechnique(technique); MyGuiManager.GetFullscreenQuad().Draw(ssEffect); MyMinerGame.SetRenderTarget(null, null); MyGuiManager.GetScreenshot().SaveTexture2D(rt, name); rt.Dispose(); } else if (target is CubeTexture) { string filename = MyGuiManager.GetScreenshot().GetFilename(name + ".dds"); CubeTexture.ToFile(target, filename, ImageFileFormat.Dds); //MyDDSFile.DDSToFile(filename, true, target, false); } } }
internal static void TakeScreenshot(string name, BaseTexture target, MyEffectScreenshot.ScreenshotTechniqueEnum technique) { if (ScreenshotOnlyFinal && name != "FinalScreen" && name != "test") return; // Screenshot object survives only one DRAW after created. We delete it immediatelly. So if 'm_screenshot' // is not null we know we have to take screenshot and set it to null. if (m_screenshot != null) { try { if (target is Texture) { Texture renderTarget = target as Texture; Texture rt = new Texture(MyRender.GraphicsDevice, renderTarget.GetLevelDescription(0).Width, renderTarget.GetLevelDescription(0).Height, 0, Usage.RenderTarget, Format.A8R8G8B8, Pool.Default); MyRender.SetRenderTarget(rt, null); BlendState.NonPremultiplied.Apply(); DepthStencilState.None.Apply(); RasterizerState.CullNone.Apply(); MyEffectScreenshot ssEffect = GetEffect(MyEffects.Screenshot) as MyEffectScreenshot; ssEffect.SetSourceTexture(renderTarget); ssEffect.SetTechnique(technique); ssEffect.SetScale(Vector2.One); MyRender.GetFullscreenQuad().Draw(ssEffect); MyRender.SetRenderTarget(null, null); string filename = m_screenshot.SaveTexture2D(rt, name); rt.Dispose(); MyRenderProxy.ScreenshotTaken(filename != null, filename, m_screenshot.ShowNotification); } else if (target is CubeTexture) { string filename = m_screenshot.GetFilename(name + ".dds"); CubeTexture.ToFile(target, filename, ImageFileFormat.Dds); MyRenderProxy.ScreenshotTaken(true, filename, m_screenshot.ShowNotification); } } catch (Exception e) { Log.WriteLine("Error while taking screenshot."); Log.WriteLine(e.ToString()); MyRenderProxy.ScreenshotTaken(false, null, m_screenshot.ShowNotification); } } }
protected override void dispose( bool disposeManagedResources ) { if ( !IsDisposed ) { if ( disposeManagedResources ) { this.NormalTexture.SafeDispose(); this.NormalTexture = null; this.CubeTexture.SafeDispose(); this.CubeTexture = null; this.VolumeTexture.SafeDispose(); this.VolumeTexture = null; this.BaseTexture.SafeDispose(); this.BaseTexture = null; this.FSAASurface.SafeDispose(); this.FSAASurface = null; } } base.dispose( disposeManagedResources ); }