protected override int CreateTexture(int width, int height, IntPtr scan0, bool managedPool, bool mipmaps) { D3D.Texture texture = null; int levels = 1 + (mipmaps ? MipmapsLevels(width, height) : 0); if (managedPool) { texture = device.CreateTexture(width, height, levels, Usage.None, Format.A8R8G8B8, Pool.Managed); texture.SetData(0, LockFlags.None, scan0, width * height * 4); if (mipmaps) { DoMipmaps(texture, 0, 0, width, height, scan0, false); } } else { D3D.Texture sys = device.CreateTexture(width, height, levels, Usage.None, Format.A8R8G8B8, Pool.SystemMemory); sys.SetData(0, LockFlags.None, scan0, width * height * 4); if (mipmaps) { DoMipmaps(sys, 0, 0, width, height, scan0, false); } texture = device.CreateTexture(width, height, levels, Usage.None, Format.A8R8G8B8, Pool.Default); device.UpdateTexture(sys, texture); sys.Dispose(); } return(GetOrExpand(ref textures, texture, texBufferSize)); }
private void InvalidateImageSource(bool forceChangeSource) { //File.AppendAllText("DEBUG.txt", DateTime.Now.ToLongTimeString() + this.GetHashCode() + " InvalidateImageSource INSIDE" + Environment.NewLine); Debug.WriteLine("InvalidateImageSource calling ..."); if (!(CurrentRenderInfo.Image.Source is DXImageSource) || forceChangeSource) { lock (lockObject) { //File.AppendAllText("DEBUG.txt", DateTime.Now.ToLongTimeString() + this.GetHashCode() + " InvalidateImageSource DXImageSource " + CurrentRenderInfo.Image.GetHashCode() + Environment.NewLine); src = new DXImageSource(); src.OnContextRetreived += Src_OnContextRetreived; src.SetBackBuffer(tex); CurrentRenderInfo.Image.Source = src; if (oldTexA != null) { oldTexA.Dispose(); } if (oldTex != null) { oldTex.Dispose(); } Debug.WriteLine("InvalidateImageSource called ..."); } } else { src.Invalidate(); } }
public void ConstructRenderAndResource(double width, double height) { float dpiX, dpiY; this.GetDpi(out dpiX, out dpiY); D2D.RenderTargetProperties prop = new D2D.RenderTargetProperties( D2D.RenderTargetType.Default, new D2D.PixelFormat(DXGI.Format.B8G8R8A8_UNorm, D2D.AlphaMode.Premultiplied), dpiX, dpiY, D2D.RenderTargetUsage.None, D2D.FeatureLevel.Level_DEFAULT); D3D11.Texture2DDescription desc = new D3D11.Texture2DDescription(); desc.Width = (int)width; desc.Height = (int)height; desc.MipLevels = 1; desc.ArraySize = 1; desc.Format = DXGI.Format.B8G8R8A8_UNorm; desc.SampleDescription = new DXGI.SampleDescription(1, 0); desc.Usage = D3D11.ResourceUsage.Default; desc.BindFlags = D3D11.BindFlags.RenderTarget | D3D11.BindFlags.ShaderResource; desc.CpuAccessFlags = D3D11.CpuAccessFlags.None; desc.OptionFlags = D3D11.ResourceOptionFlags.Shared; this.d3d11Texture = new D3D11.Texture2D(this.device, desc); this.surface = this.d3d11Texture.QueryInterface <DXGI.Surface>(); DXGI.Resource resource = this.d3d11Texture.QueryInterface <DXGI.Resource>(); IntPtr handel = resource.SharedHandle; D3D9.Texture texture = new D3D9.Texture( this.device9, this.d3d11Texture.Description.Width, this.d3d11Texture.Description.Height, 1, D3D9.Usage.RenderTarget, D3D9.Format.A8R8G8B8, D3D9.Pool.Default, ref handel); this.surface9 = texture.GetSurfaceLevel(0); resource.Dispose(); texture.Dispose(); D2D.BitmapProperties bmpProp = new D2D.BitmapProperties(); bmpProp.DpiX = dpiX; bmpProp.DpiY = dpiY; bmpProp.PixelFormat = new D2D.PixelFormat(DXGI.Format.B8G8R8A8_UNorm, D2D.AlphaMode.Premultiplied); this.bmpd2d = new D2D.Bitmap(this.render, this.surface, bmpProp); this.cachedBitMap = new D2D.Bitmap(this.render, new Size2((int)width, (int)height), bmpProp); this.hasCache = false; this.render.Target = this.bmpd2d; this.renderSize = new Size(width, height); }
/// <summary> /// /// </summary> /// <param name="texture"></param> public void SetBackBuffer(SharpDX.Direct3D9.Texture texture) { if (IsDisposed) { throw new ObjectDisposedException(GetType().Name); } SharpDX.Direct3D9.Texture toDelete = null; try { if (texture != m_backBuffer) { // if it's from the private (SDX9ImageSource) D3D9 device, dispose of it if (m_backBuffer != null && m_backBuffer.Device.NativePointer == s_d3d9.Device.NativePointer) { toDelete = m_backBuffer; } m_backBuffer = texture; } if (texture != null) { using (Surface surface = texture.GetSurfaceLevel(0)) { Lock(); SetBackBuffer(D3DResourceType.IDirect3DSurface9, surface.NativePointer); AddDirtyRect(new Int32Rect(0, 0, base.PixelWidth, base.PixelHeight)); Unlock(); } } else { Lock(); SetBackBuffer(D3DResourceType.IDirect3DSurface9, IntPtr.Zero); AddDirtyRect(new Int32Rect(0, 0, base.PixelWidth, base.PixelHeight)); Unlock(); } } finally { if (toDelete != null) { toDelete.Dispose(); } } }
/// <summary> /// Releases the unmanaged resources used by an instance of the <see cref="D3D11Image"/> class /// and optionally releases the managed resources. /// </summary> /// <param name="disposing"> /// <see langword="true"/> to release both managed and unmanaged resources; /// <see langword="false"/> to release only unmanaged resources. /// </param> protected virtual void Dispose(bool disposing) { if (!_disposed) { if (disposing) { // Dispose managed resources. SetBackBuffer(null); if (_backBuffer != null) { _backBuffer.Dispose(); _backBuffer = null; } } // Release unmanaged resources. UninitializeD3D9(); _disposed = true; } }
public void ClearDisplay() { lock (DX.GlobalLock) { if (testCube != null) { testCube.Dispose(); testCube = null; } if (texture != null) { texture.Dispose(); texture = null; spriteTransform = Matrix.Identity; } meshes.ForEach(m => m.Dispose()); meshes.Clear(); } }
//loads the data from a stream in to a texture object. private static void InternalDDSFromStream(Stream stream, Device device, int streamOffset, bool loadMipMap, int offsetMipMaps, out SharpDX.Direct3D9.Texture texture) { stream.Position = 0; if (offsetMipMaps == 0) { texture = SharpDX.Direct3D9.Texture.FromStream(device, stream, 0, 0, 0, Usage.None, Format.Unknown, Pool.Default, Filter.None, Filter.None, 0); } else { texture = SharpDX.Direct3D9.Texture.FromStream(device, stream, 0, 0, 0, Usage.Dynamic, Format.Unknown, Pool.Default, Filter.None, Filter.None, 0); int width = MipMapSize(offsetMipMaps, texture.GetLevelDescription(0).Width); int height = MipMapSize(offsetMipMaps, texture.GetLevelDescription(0).Height); int maxLevels = Math.Min(MaxMipMapLevels(width), MaxMipMapLevels(height)); int actualLevels = Math.Min(maxLevels, texture.LevelCount - offsetMipMaps); Format format = texture.GetLevelDescription(0).Format; Texture offsetedTexture = new Texture(device, width, height, actualLevels, Usage.Dynamic, format, Pool.Default); for (int i = offsetMipMaps, j = 0; j < actualLevels; i++, j++) { int levelWidth = MipMapSize(j, width); int levelHeight = MipMapSize(j, height); SharpDX.DataStream ds; texture.LockRectangle(i, LockFlags.ReadOnly, out ds); texture.UnlockRectangle(i); SharpDX.DataStream ds2; offsetedTexture.LockRectangle(j, LockFlags.None, out ds2); ds2.Position = 0; ds2.Write(ds.DataPointer, 0, (int)MipMapSizeInBytes(levelWidth, levelHeight, format)); offsetedTexture.UnlockRectangle(j); } texture.Dispose(); texture = offsetedTexture; } }
private static void InternalDDSFromStream(Stream stream, Device device, int streamOffset, bool loadMipMap, int offsetMipMaps, out SharpDX.Direct3D9.Texture texture) { var fileStream = stream as FileStream; byte[] data = fileStream != null ? null : SharpDX.Utilities.ReadStream(stream); int maxTextureDimension = MyRender.GraphicsDevice.Capabilities.MaxTextureWidth; if (IntPtr.Size == 4 || MyRender.GraphicsDevice.AvailableTextureMemory < 1024 * 1024 * 600) { maxTextureDimension = 2048; } ImageInformation imageInfo; if (fileStream != null) { imageInfo = GetImageInfoFromFileW(fileStream.Name); } else { imageInfo = ImageInformation.FromMemory(data); } var textureWidth = imageInfo.Width; var textureHeight = imageInfo.Height; textureWidth = Math.Max(1, textureWidth >> offsetMipMaps); textureHeight = Math.Max(1, textureHeight >> offsetMipMaps); int loadMipmapOffset = offsetMipMaps; while (textureWidth > maxTextureDimension || textureHeight > maxTextureDimension) { loadMipmapOffset++; textureWidth = Math.Max(1, textureWidth >> 1); textureHeight = Math.Max(1, textureHeight >> 1); } if (offsetMipMaps == 0 && loadMipmapOffset == 0) { if (fileStream != null) { texture = SharpDX.Direct3D9.Texture.FromFile(device, fileStream.Name, 0, 0, 0, Usage.None, Format.Unknown, Pool.Default, Filter.None, Filter.None, 0); } else { texture = SharpDX.Direct3D9.Texture.FromMemory(device, data, 0, 0, 0, Usage.None, Format.Unknown, Pool.Default, Filter.None, Filter.None, 0); } } else if (loadMipmapOffset > 0) { var skipFilter = (Filter)D3DXSkipDDSMipLevels((uint)loadMipmapOffset); if (fileStream != null) { texture = SharpDX.Direct3D9.Texture.FromFile(device, fileStream.Name, 0, 0, 0, Usage.None, Format.Unknown, Pool.Default, Filter.None, skipFilter, 0); } else { texture = SharpDX.Direct3D9.Texture.FromMemory(device, data, 0, 0, 0, Usage.None, Format.Unknown, Pool.Default, Filter.None, skipFilter, 0); } } else { if (fileStream != null) { texture = SharpDX.Direct3D9.Texture.FromFile(device, fileStream.Name, 0, 0, 0, Usage.None, Format.Unknown, Pool.Default, Filter.None, Filter.None, 0); } else { texture = SharpDX.Direct3D9.Texture.FromMemory(device, data, 0, 0, 0, Usage.None, Format.Unknown, Pool.Default, Filter.None, Filter.None, 0); } int width = MipMapSize(offsetMipMaps, texture.GetLevelDescription(0).Width); int height = MipMapSize(offsetMipMaps, texture.GetLevelDescription(0).Height); int maxLevels = Math.Min(MaxMipMapLevels(width), MaxMipMapLevels(height)); int actualLevels = Math.Min(maxLevels, texture.LevelCount - offsetMipMaps); Format format = texture.GetLevelDescription(0).Format; Texture offsetedTexture = new Texture(device, width, height, actualLevels, Usage.Dynamic, format, Pool.Default); for (int i = offsetMipMaps, j = 0; j < actualLevels; i++, j++) { int levelWidth = MipMapSize(j, width); int levelHeight = MipMapSize(j, height); SharpDX.DataStream ds; texture.LockRectangle(i, LockFlags.ReadOnly, out ds); texture.UnlockRectangle(i); SharpDX.DataStream ds2; offsetedTexture.LockRectangle(j, LockFlags.None, out ds2); ds2.Position = 0; ds2.Write(ds.DataPointer, 0, (int)MipMapSizeInBytes(levelWidth, levelHeight, format)); offsetedTexture.UnlockRectangle(j); } texture.Dispose(); texture = offsetedTexture; } }
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); } } }
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); } } }
public static void SaveScreenshot(Texture texture2D, string file) { MyMwcLog.WriteLine("MyScreenshot.SaveTexture2D() - START"); MyMwcLog.IncreaseIndent(); Texture systemTex = new Texture(MinerWars.AppCode.App.MyMinerGame.Static.GraphicsDevice, texture2D.GetLevelDescription(0).Width, texture2D.GetLevelDescription(0).Height, 0, Usage.None, Format.A8R8G8B8, Pool.SystemMemory); Surface sourceSurface = texture2D.GetSurfaceLevel(0); Surface destSurface = systemTex.GetSurfaceLevel(0); MinerWars.AppCode.App.MyMinerGame.Static.GraphicsDevice.GetRenderTargetData(sourceSurface, destSurface); sourceSurface.Dispose(); destSurface.Dispose(); texture2D = systemTex; try { MyMwcLog.WriteLine("File: " + file); MyFileSystemUtils.CreateFolderForFile(file); Stack<SharpDX.Rectangle> tiles = new Stack<SharpDX.Rectangle>(); int tileWidth = texture2D.GetLevelDescription(0).Width; int tileHeight = texture2D.GetLevelDescription(0).Height; while (tileWidth > 3200) { tileWidth /= 2; tileHeight /= 2; } int widthOffset = 0; int heightOffset = 0; while (widthOffset < texture2D.GetLevelDescription(0).Width) { while (heightOffset < texture2D.GetLevelDescription(0).Height) { tiles.Push(new SharpDX.Rectangle(widthOffset, heightOffset, tileWidth, tileHeight)); heightOffset += tileHeight; } heightOffset = 0; widthOffset += tileWidth; } int sc = 0; while (tiles.Count > 0) { SharpDX.Rectangle rect = tiles.Pop(); byte[] data = new byte[rect.Width * rect.Height * 4]; SharpDX.Rectangle rect2 = new SharpDX.Rectangle(rect.X, rect.Y, rect.Width, rect.Height); //texture2D.GetData<byte>(0, rect2, data, 0, data.Length); DataStream ds; texture2D.LockRectangle(0, rect2, LockFlags.None, out ds); ds.Read(data, 0, data.Length); /* for (int i = 0; i < data.Length; i += 4) { //Swap ARGB <-> RGBA byte b = data[i + 0]; byte g = data[i + 1]; byte r = data[i + 2]; byte a = data[i + 3]; data[i + 0] = r; //Blue data[i + 1] = g; //Green data[i + 2] = b; //Red data[i + 3] = a; //Alpha } */ ds.Seek(0, SeekOrigin.Begin); ds.WriteRange(data); texture2D.UnlockRectangle(0); string filename = file.Replace(".png", "_" + sc.ToString("##00") + ".png"); using (Stream stream = File.Create(filename)) { System.Drawing.Bitmap image = new System.Drawing.Bitmap(rect.Width, rect.Height); System.Drawing.Imaging.BitmapData imageData = image.LockBits(new System.Drawing.Rectangle(0,0,rect.Width, rect.Height), System.Drawing.Imaging.ImageLockMode.WriteOnly, System.Drawing.Imaging.PixelFormat.Format32bppArgb); System.Runtime.InteropServices.Marshal.Copy(data, 0, imageData.Scan0, data.Length); image.Save(stream, System.Drawing.Imaging.ImageFormat.Png); image.UnlockBits(imageData); image.Dispose(); //texture2D.SaveAsPng(stream, texture2D.Width, texture2D.Height); //BaseTexture.ToStream(texture2D, ImageFileFormat.Png); } sc++; GC.Collect(); } } catch (Exception exc) { // Write exception to log, but continue as if nothing wrong happened MyMwcLog.WriteLine(exc); } texture2D.Dispose(); //BaseTexture.ToFile(texture2D, "c:\\test.png", ImageFileFormat.Png); MyMwcLog.DecreaseIndent(); MyMwcLog.WriteLine("MyScreenshot.SaveTexture2D() - END"); }