예제 #1
0
        public ModernCapture()
        {
#if DEBUG
            // Check memory leaks in debug config
            Configuration.EnableObjectTracking = true;
#endif

            wrtD3D11Device = Direct3D11Helper.CreateDevice();
            d3dDevice      = Direct3D11Helper.CreateSharpDXDevice(wrtD3D11Device);
            d3dContext     = d3dDevice.ImmediateContext;
            wicFactory     = new WIC.ImagingFactory2();

            InitializeShaders();
            InitializeSharedComponents();
        }
예제 #2
0
        private void ProcessFrame(Direct3D11CaptureFrame frame)
        {
            // Do proecssing
            using (frame)
            {
                using (var texture = Direct3D11Helper.CreateSharpDXTexture2D(frame.Surface))
                {
                    var hdrMetadata = currentSession.HdrMetadata;
                    var vertices    = new ShaderInputStructure[]
                    {
                        // Left-Top
                        new ShaderInputStructure {
                            Position     = new Vector3(currentSession.DestD3DVsTopLeft.X, currentSession.DestD3DVsTopLeft.Y, 0),
                            TextureCoord = new Vector2(currentSession.DestD3DPsSamplerTopLeft.X, currentSession.DestD3DPsSamplerTopLeft.Y),
                        },
                        // Right-Top
                        new ShaderInputStructure {
                            Position     = new Vector3(currentSession.DestD3DVsBottomRight.X, currentSession.DestD3DVsTopLeft.Y, 0),
                            TextureCoord = new Vector2(currentSession.DestD3DPsSamplerBottomRight.X, currentSession.DestD3DPsSamplerTopLeft.Y)
                        },
                        // Left-Bottom
                        new ShaderInputStructure {
                            Position     = new Vector3(currentSession.DestD3DVsTopLeft.X, currentSession.DestD3DVsBottomRight.Y, 0),
                            TextureCoord = new Vector2(currentSession.DestD3DPsSamplerTopLeft.X, currentSession.DestD3DPsSamplerBottomRight.Y)
                        },
                        // Right-Top
                        new ShaderInputStructure {
                            Position     = new Vector3(currentSession.DestD3DVsBottomRight.X, currentSession.DestD3DVsTopLeft.Y, 0),
                            TextureCoord = new Vector2(currentSession.DestD3DPsSamplerBottomRight.X, currentSession.DestD3DPsSamplerTopLeft.Y)
                        },
                        // Right-Bottom
                        new ShaderInputStructure {
                            Position     = new Vector3(currentSession.DestD3DVsBottomRight.X, currentSession.DestD3DVsBottomRight.Y, 0),
                            TextureCoord = new Vector2(currentSession.DestD3DPsSamplerBottomRight.X, currentSession.DestD3DPsSamplerBottomRight.Y)
                        },
                        // Left-Bottom
                        new ShaderInputStructure {
                            Position     = new Vector3(currentSession.DestD3DVsTopLeft.X, currentSession.DestD3DVsBottomRight.Y, 0),
                            TextureCoord = new Vector2(currentSession.DestD3DPsSamplerTopLeft.X, currentSession.DestD3DPsSamplerBottomRight.Y)
                        },
                    };

                    var triangleVertexBuffer = D3D11.Buffer.Create(d3dDevice, D3D11.BindFlags.VertexBuffer, vertices);
                    var hdrMetadataBuffer    = new D3D11.Buffer(d3dDevice,
                                                                Utilities.SizeOf <ShaderHdrMetadata>(),
                                                                D3D11.ResourceUsage.Default,
                                                                D3D11.BindFlags.ConstantBuffer,
                                                                D3D11.CpuAccessFlags.None,
                                                                D3D11.ResourceOptionFlags.None,
                                                                0);

                    d3dContext.UpdateSubresource(ref hdrMetadata, hdrMetadataBuffer);

                    d3dContext.InputAssembler.PrimitiveTopology = D3D.PrimitiveTopology.TriangleList;
                    d3dContext.InputAssembler.InputLayout       = inputLayout;
                    d3dContext.InputAssembler.SetVertexBuffers(0, new D3D11.VertexBufferBinding(triangleVertexBuffer, Utilities.SizeOf <ShaderInputStructure>(), 0));

                    d3dContext.VertexShader.Set(vsQuad);
                    d3dContext.PixelShader.SetConstantBuffer(0, hdrMetadataBuffer);
                    d3dContext.PixelShader.SetSampler(0, samplerState);

                    var canvasTexture = new D3D11.Texture2D(d3dDevice, new D3D11.Texture2DDescription
                    {
                        Width             = texture.Description.Width,
                        Height            = texture.Description.Height,
                        MipLevels         = 1,
                        ArraySize         = 1,
                        Format            = currentSession.HdrMetadata.EnableHdrProcessing ? DXGI.Format.R16G16B16A16_Float : DXGI.Format.B8G8R8A8_UNorm_SRgb,
                        Usage             = D3D11.ResourceUsage.Default,
                        SampleDescription = new DXGI.SampleDescription(1, 0),
                        BindFlags         = D3D11.BindFlags.ShaderResource,
                        CpuAccessFlags    = D3D11.CpuAccessFlags.None,
                        OptionFlags       = D3D11.ResourceOptionFlags.None,
                    });

                    using (canvasTexture)
                        using (var shaderResView = new D3D11.ShaderResourceView(d3dDevice, canvasTexture))
                        {
                            d3dContext.CopyResource(texture, canvasTexture);
                            d3dContext.PixelShader.SetShaderResource(0, shaderResView);
                            d3dContext.PixelShader.Set(psToneMapping);
                            d3dContext.Draw(vertices.Length, 0);
                        }

                    triangleVertexBuffer.Dispose();
                    hdrMetadataBuffer.Dispose();
                }
            }

            // Cleanup and signal event to proceed
            currentSession.Session.Dispose();
        }