public override void Draw(SharpDX.Direct3D11.DeviceContext context, int index) { if (_engine.CurrentMSAASampling.Count <= 1) { if (OffSreenRenderTarget == null) { context.CopyResource(_engine.BackBufferTex, _renderTexture); } else { context.CopyResource(OffSreenRenderTarget, _renderTexture); } } else { if (OffSreenRenderTarget == null) { context.ResolveSubresource(_engine.BackBufferTex, 0, _renderTexture, 0, Format.R8G8B8A8_UNorm); } else { context.ResolveSubresource(OffSreenRenderTarget, 0, _renderTexture, 0, Format.R8G8B8A8_UNorm); } } }
private Stream DumpAndSaveImage() { var stream = new MemoryStream(); var textureSDRCpuCopy = new D3D11.Texture2D(d3dDevice, new D3D11.Texture2DDescription { Width = description.CanvasRect.Width, Height = description.CanvasRect.Height, MipLevels = 1, ArraySize = 1, Format = DXGI.Format.B8G8R8A8_UNorm_SRgb, Usage = D3D11.ResourceUsage.Staging, SampleDescription = new DXGI.SampleDescription(1, 0), BindFlags = D3D11.BindFlags.None, CpuAccessFlags = D3D11.CpuAccessFlags.Read, OptionFlags = D3D11.ResourceOptionFlags.None, }); DataStream rawSdrImageDataStream; d3dContext.CopyResource(textureSDRImage, textureSDRCpuCopy); var dataBox = d3dContext.MapSubresource(textureSDRCpuCopy, 0, 0, D3D11.MapMode.Read, D3D11.MapFlags.None, out rawSdrImageDataStream); var dataRectangle = new DataRectangle { DataPointer = rawSdrImageDataStream.DataPointer, Pitch = dataBox.RowPitch, }; using (var bitmap = new WIC.Bitmap(wicFactory, description.CanvasRect.Width, description.CanvasRect.Height, WIC.PixelFormat.Format32bppBGRA, dataRectangle)) using (var imageEncoder = new WIC.BmpBitmapEncoder(wicFactory, stream)) using (var encodeInstance = new WIC.BitmapFrameEncode(imageEncoder)) { encodeInstance.Initialize(); encodeInstance.SetSize(bitmap.Size.Width, bitmap.Size.Height); var pixelFormat = WIC.PixelFormat.Format24bppBGR; encodeInstance.SetPixelFormat(ref pixelFormat); encodeInstance.WriteSource(bitmap); encodeInstance.Commit(); imageEncoder.Commit(); stream.Flush(); } d3dContext.UnmapSubresource(textureSDRCpuCopy, 0); rawSdrImageDataStream.Dispose(); textureSDRCpuCopy.Dispose(); return(stream); }
public void TakeSnapshot(string fileName) { Texture2D snapshotTexture; lock (device) { Utilities.Dispose(ref rtv); Utilities.Dispose(ref backBuffer); swapChain.ResizeBuffers(0, decoder.vDecoder.info.Width, decoder.vDecoder.info.Height, Format.Unknown, SwapChainFlags.None); backBuffer = Texture2D.FromSwapChain <Texture2D>(swapChain, 0); rtv = new RenderTargetView(device, backBuffer); context.Rasterizer.SetViewport(0, 0, backBuffer.Description.Width, backBuffer.Description.Height); for (int i = 0; i < swapChain.Description.BufferCount; i++) { context.OutputMerger.SetRenderTargets(rtv); context.ClearRenderTargetView(rtv, cfg.video._ClearColor); context.Draw(6, 0); swapChain.Present(cfg.video.VSync, PresentFlags.None); } snapshotTexture = new Texture2D(device, new Texture2DDescription() { Usage = ResourceUsage.Staging, ArraySize = 1, MipLevels = 1, Width = backBuffer.Description.Width, Height = backBuffer.Description.Height, Format = Format.B8G8R8A8_UNorm, BindFlags = BindFlags.None, CpuAccessFlags = CpuAccessFlags.Read, OptionFlags = ResourceOptionFlags.None, SampleDescription = new SampleDescription(1, 0) }); context.CopyResource(backBuffer, snapshotTexture); ResizeBuffers(null, null); } System.Drawing.Bitmap snapshotBitmap = new System.Drawing.Bitmap(snapshotTexture.Description.Width, snapshotTexture.Description.Height); DataBox db = context.MapSubresource(snapshotTexture, 0, MapMode.Read, SharpDX.Direct3D11.MapFlags.None); var bitmapData = snapshotBitmap.LockBits(new System.Drawing.Rectangle(0, 0, snapshotBitmap.Width, snapshotBitmap.Height), System.Drawing.Imaging.ImageLockMode.WriteOnly, System.Drawing.Imaging.PixelFormat.Format32bppArgb); var sourcePtr = db.DataPointer; var destPtr = bitmapData.Scan0; for (int y = 0; y < snapshotBitmap.Height; y++) { Utilities.CopyMemory(destPtr, sourcePtr, snapshotBitmap.Width * 4); sourcePtr = IntPtr.Add(sourcePtr, db.RowPitch); destPtr = IntPtr.Add(destPtr, bitmapData.Stride); } snapshotBitmap.UnlockBits(bitmapData); context.UnmapSubresource(snapshotTexture, 0); snapshotTexture.Dispose(); try { snapshotBitmap.Save(fileName); } catch (Exception) { } snapshotBitmap.Dispose(); }
public T[] GetData <T>(int count) where T : struct { _deviceContext.CopyResource(_buffer, _resultBuffer); SharpDX.DataStream stream; SharpDX.DataBox box = _deviceContext.MapSubresource(_resultBuffer, 0, MapMode.Read, MapFlags.None, out stream); T[] result = stream.ReadRange <T>(count); _deviceContext.UnmapSubresource(_buffer, 0); return(result); }
/// <summary> /// Called when RenderLoop wants to present its results. /// </summary> private void OnRenderLoopPresent(EngineDevice engineDevice) { D3D11.DeviceContext deviceContext = engineDevice.DeviceImmediateContextD3D11; // Copy all contents of the render target to the texture that is used for synchronization deviceContext.Flush(); deviceContext.CopyResource( m_backBuffer, m_backBufferSynchronizing); // Request next frame m_drawingInterop.RequestNextFrame(); }
/// <summary> /// Takes a screenshot and returns it as a gdi bitmap. /// </summary> public GDI.Bitmap TakeScreenshotGdi(GDI.Size size) { //Get and read data from the gpu (create copy helper texture on demand) if (_copyHelperTextureStaging == null) { _copyHelperTextureStaging = CreateStagingTexture(_d3d11Device, size); } _d3d11Context.CopyResource(_renderTexture, _copyHelperTextureStaging); //Prepare target bitmap var result = new GDI.Bitmap(size.Width, size.Height); var dataBox = _d3d11Context.MapSubresource(_copyHelperTextureStaging, 0, D3D11.MapMode.Read, D3D11.MapFlags.None); try { //Lock bitmap so it can be accessed for texture loading System.Drawing.Imaging.BitmapData bitmapData = result.LockBits( new System.Drawing.Rectangle(0, 0, result.Width, result.Height), System.Drawing.Imaging.ImageLockMode.WriteOnly, System.Drawing.Imaging.PixelFormat.Format32bppArgb); try { //Copy bitmap data memcpy(bitmapData.Scan0, dataBox.DataPointer, new UIntPtr((uint)(size.Width * size.Height * 4))); } finally { result.UnlockBits(bitmapData); } } finally { _d3d11Context.UnmapSubresource(_copyHelperTextureStaging, 0); } return(result); }
// TODO: Code that uses temporary resources (that calls this method) should be changed to use Managers and their interfaces internal void CopyResource(IResource source, Resource destination) { m_deviceContext.CopyResource(source.Resource, destination); CheckErrors(); }
public void CopyResource(Resource src, Resource dst) { context.CopyResource(src, dst); }