コード例 #1
0
        public override ImageCapture CaptureRenderTarget(RenderTarget surface)
        {
            IntPtr             texNativePtr = RenderDeviceD3D11.GetTextureNativePointer(surface.Texture);
            ShaderResourceView view         = new ShaderResourceView(texNativePtr);

            using (Texture2D source = view.Resource.QueryInterface <Texture2D>())
            {
                Texture2DDescription desc = source.Description;
                desc.CpuAccessFlags = CpuAccessFlags.Read;
                desc.Usage          = ResourceUsage.Staging;
                desc.BindFlags      = BindFlags.None;

                using (Texture2D dest = new Texture2D(_dev, desc))
                {
                    _dev.ImmediateContext.CopyResource(source, dest);

                    SharpDX.DataBox data = _dev.ImmediateContext.MapSubresource(dest, 0,
                                                                                MapMode.Read, SharpDX.Direct3D11.MapFlags.None);

                    if (_imageCapture == null || _imageCapture.Width != desc.Width || _imageCapture.Height != desc.Height)
                    {
                        _imageCapture = new ImageCapture((uint)desc.Width, (uint)desc.Height);
                    }

                    byte[] dst = _imageCapture.Pixels;
                    IntPtr src = data.DataPointer;

                    for (int i = 0; i < desc.Height; ++i)
                    {
                        int dstRow = i * (int)_imageCapture.Stride;
                        int srcRow = i * data.RowPitch;

                        for (int j = 0; j < desc.Width; j++)
                        {
                            // RGBA -> BGRA
                            dst[dstRow + 4 * j + 2] = Noesis.Marshal.ReadByte(src, srcRow + 4 * j + 0);
                            dst[dstRow + 4 * j + 1] = Noesis.Marshal.ReadByte(src, srcRow + 4 * j + 1);
                            dst[dstRow + 4 * j + 0] = Noesis.Marshal.ReadByte(src, srcRow + 4 * j + 2);
                            dst[dstRow + 4 * j + 3] = Noesis.Marshal.ReadByte(src, srcRow + 4 * j + 3);
                        }
                    }

                    _dev.ImmediateContext.UnmapSubresource(dest, 0);
                }
            }

            return(_imageCapture);
        }
コード例 #2
0
ファイル: RenderContextD3D12.cs プロジェクト: Noesis/Managed
        public override ImageCapture CaptureRenderTarget(RenderTarget surface)
        {
            IntPtr texNativePtr = RenderDeviceD3D11.GetTextureNativePointer(surface.Texture);

            SharpDX.Direct3D12.Resource            texture = new SharpDX.Direct3D12.Resource(texNativePtr);
            SharpDX.Direct3D12.ResourceDescription texDesc = texture.Description;

            // Transition PIXEL_SHADER_RESOURCE -> COPY_SOURCE
            SharpDX.Direct3D12.ResourceBarrier barrier = new SharpDX.Direct3D12.ResourceBarrier();
            barrier.Type       = SharpDX.Direct3D12.ResourceBarrierType.Transition;
            barrier.Flags      = SharpDX.Direct3D12.ResourceBarrierFlags.None;
            barrier.Transition = new SharpDX.Direct3D12.ResourceTransitionBarrier(texture,
                                                                                  SharpDX.Direct3D12.ResourceStates.PixelShaderResource,
                                                                                  SharpDX.Direct3D12.ResourceStates.CopySource)
            {
                Subresource = 0
            };

            _commands.ResourceBarrier(barrier);

            long totalBytes;

            long[] rowSize = { 0 };
            _dev.GetCopyableFootprints(ref texDesc, 0, 1, 0, null, null, rowSize, out totalBytes);
            long pitch = (rowSize[0] + 255) & ~255;

            // Create staging texture
            SharpDX.Direct3D12.HeapProperties heap;
            heap.Type                 = SharpDX.Direct3D12.HeapType.Readback;
            heap.CPUPageProperty      = SharpDX.Direct3D12.CpuPageProperty.Unknown;
            heap.MemoryPoolPreference = SharpDX.Direct3D12.MemoryPool.Unknown;
            heap.CreationNodeMask     = 0;
            heap.VisibleNodeMask      = 0;

            SharpDX.Direct3D12.ResourceDescription desc;
            desc.Dimension         = SharpDX.Direct3D12.ResourceDimension.Buffer;
            desc.Alignment         = 0;
            desc.Width             = pitch * texDesc.Height * 4;
            desc.Height            = 1;
            desc.DepthOrArraySize  = 1;
            desc.MipLevels         = 1;
            desc.Format            = SharpDX.DXGI.Format.Unknown;
            desc.SampleDescription = new SampleDescription {
                Count = 1, Quality = 0
            };
            desc.Layout = SharpDX.Direct3D12.TextureLayout.RowMajor;
            desc.Flags  = SharpDX.Direct3D12.ResourceFlags.None;

            SharpDX.Direct3D12.Resource stagingTex = _dev.CreateCommittedResource(heap, SharpDX.Direct3D12.HeapFlags.None,
                                                                                  desc, SharpDX.Direct3D12.ResourceStates.CopyDestination, null);

            SharpDX.Direct3D12.TextureCopyLocation copySrc = new SharpDX.Direct3D12.TextureCopyLocation(texture, 0);
            SharpDX.Direct3D12.TextureCopyLocation copyDst = new SharpDX.Direct3D12.TextureCopyLocation(texture,
                                                                                                        new SharpDX.Direct3D12.PlacedSubResourceFootprint
            {
                Offset    = 0,
                Footprint = new SharpDX.Direct3D12.SubResourceFootprint
                {
                    Width    = (int)texDesc.Width,
                    Height   = (int)texDesc.Height,
                    Depth    = 1,
                    RowPitch = (int)pitch,
                    Format   = texDesc.Format
                }
            });

            // Wait GPU completion
            _commands.CopyTextureRegion(copyDst, 0, 0, 0, copySrc, null);
            _commands.Close();
            _queue.ExecuteCommandList(_commands);
            WaitForGpu();

            // Ensure image capture object is created with the appropriate size
            if (_imageCapture == null || _imageCapture.Width != desc.Width || _imageCapture.Height != desc.Height)
            {
                _imageCapture = new ImageCapture((uint)desc.Width, (uint)desc.Height);
            }

            // Map and copy to image
            byte[] dst = _imageCapture.Pixels;
            IntPtr src = stagingTex.Map(0);

            for (int i = 0; i < desc.Height; ++i)
            {
                int dstRow = i * (int)_imageCapture.Stride;
                int srcRow = i * (int)pitch;

                for (int j = 0; j < desc.Width; j++)
                {
                    // RGBA -> BGRA
                    dst[dstRow + 4 * j + 2] = Noesis.Marshal.ReadByte(src, srcRow + 4 * j + 0);
                    dst[dstRow + 4 * j + 1] = Noesis.Marshal.ReadByte(src, srcRow + 4 * j + 1);
                    dst[dstRow + 4 * j + 0] = Noesis.Marshal.ReadByte(src, srcRow + 4 * j + 2);
                    dst[dstRow + 4 * j + 3] = Noesis.Marshal.ReadByte(src, srcRow + 4 * j + 3);
                }
            }

            stagingTex.Unmap(0);

            return(_imageCapture);
        }