Esempio n. 1
0
        private ShaderResourceView ApplyCS(ShaderResourceView srcTextureSRV, float fIntencity, string Shader)
        {
            SharpDX.Direct3D.ShaderMacro[] defines = new[] {
                new SharpDX.Direct3D.ShaderMacro("THREADSX", 32),
                new SharpDX.Direct3D.ShaderMacro("THREADSY", 32),
            };
            using (var srcTexture = srcTextureSRV.ResourceAs <Texture2D>())
            {
                var desc = srcTexture.Description;
                desc.BindFlags = BindFlags.UnorderedAccess;
                using (var target = new Texture2D(_game.DeviceContext.Device, desc))
                    using (var targetUAV = new UnorderedAccessView(_game.DeviceContext.Device, target))
                        using (var computeBuffer = new Buffer(_game.DeviceContext.Device,
                                                              Utilities.SizeOf <ComputeConstants>(),
                                                              ResourceUsage.Default,
                                                              BindFlags.ConstantBuffer,
                                                              CpuAccessFlags.None,
                                                              ResourceOptionFlags.None,
                                                              0))
                            using (var cs = StaticMetods.GetComputeShader(_game.DeviceContext.Device, Shader, defines))
                            {
                                var constants = new ComputeConstants {
                                    Intensity = fIntencity
                                };
                                _game.DeviceContext.UpdateSubresource(ref constants, computeBuffer);

                                _game.DeviceContext.ComputeShader.Set(cs);
                                _game.DeviceContext.ComputeShader.SetShaderResource(0, srcTextureSRV);
                                _game.DeviceContext.ComputeShader.SetUnorderedAccessView(0, targetUAV);
                                _game.DeviceContext.ComputeShader.SetConstantBuffer(0, computeBuffer);

                                _game.DeviceContext.Dispatch((int)Math.Ceiling(desc.Width / 32.0),
                                                             (int)Math.Ceiling(desc.Height / 32.0), 1);

                                _game.DeviceContext.ComputeShader.SetShaderResource(0, null);
                                _game.DeviceContext.ComputeShader.SetUnorderedAccessView(0, null);
                                _game.DeviceContext.ComputeShader.SetConstantBuffer(0, null);

                                StaticMetods.CopyUAVToSRV(_game.DeviceContext.Device, ref srcTextureSRV, targetUAV);
                            }
            }

            return(srcTextureSRV);
        }
Esempio n. 2
0
        public void Blur(ref ShaderResourceView srcTextureSRV, float fIntencity)
        {
            var device  = _game.DeviceContext.Device;
            var context = _game.DeviceContext;

            using (var srcTexture = srcTextureSRV.ResourceAs <Texture2D>())
            {
                var desc = srcTexture.Description;
                desc.BindFlags = BindFlags.ShaderResource | BindFlags.UnorderedAccess;
                using (var target = new Texture2D(_game.DeviceContext.Device, desc))
                    using (var targetUAV = new UnorderedAccessView(_game.DeviceContext.Device, target))
                        using (var targetSRV = new ShaderResourceView(_game.DeviceContext.Device, target))
                            using (var target2 = new Texture2D(_game.DeviceContext.Device, desc))
                                using (var target2UAV = new UnorderedAccessView(_game.DeviceContext.Device, target2))
                                    using (var target2SRV = new ShaderResourceView(_game.DeviceContext.Device, target2))

                                        using (var computeBuffer = new Buffer(_game.DeviceContext.Device,
                                                                              Utilities.SizeOf <ComputeConstants>(),
                                                                              ResourceUsage.Default,
                                                                              BindFlags.ConstantBuffer,
                                                                              CpuAccessFlags.None,
                                                                              ResourceOptionFlags.None, 0))
                                            using (var horizCS = StaticMetods.GetComputeShader(device, "Shaders\\Filters\\HBlurCS.hlsl", new[] {
                                                new SharpDX.Direct3D.ShaderMacro("THREADSX", 1024),
                                                new SharpDX.Direct3D.ShaderMacro("THREADSY", 1),
                                            }))
                                                using (var vertCS = StaticMetods.GetComputeShader(device, "Shaders\\Filters\\VBlurCS.hlsl", new[] {
                                                    new SharpDX.Direct3D.ShaderMacro("THREADSX", 1),
                                                    new SharpDX.Direct3D.ShaderMacro("THREADSY", 1024),
                                                }))
                                                {
                                                    var constants = new ComputeConstants {
                                                        Intensity = fIntencity
                                                    };

                                                    _game.DeviceContext.UpdateSubresource(ref constants, computeBuffer);

                                                    // The first source resource is the original image
                                                    context.ComputeShader.SetShaderResource(0, srcTextureSRV);
                                                    // The first destination resource is target
                                                    context.ComputeShader.SetUnorderedAccessView(0, targetUAV);
                                                    // Run the horizontal blur first (order doesn't matter)
                                                    context.ComputeShader.Set(horizCS);
                                                    _game.DeviceContext.ComputeShader.SetConstantBuffer(0, computeBuffer);
                                                    context.Dispatch((int)Math.Ceiling(desc.Width / 1024.0), (int)Math.Ceiling(desc.Height / 1.0), 1);

                                                    // We must set the compute shader stage SRV and UAV to
                                                    // null between calls to the compute shader
                                                    context.ComputeShader.SetShaderResource(0, null);
                                                    context.ComputeShader.SetUnorderedAccessView(0, null);

                                                    // The second source resource is the first target
                                                    context.ComputeShader.SetShaderResource(0, targetSRV);
                                                    // The second destination resource is target2
                                                    context.ComputeShader.SetUnorderedAccessView(0, target2UAV);
                                                    // Run the vertical blur
                                                    context.ComputeShader.Set(vertCS);
                                                    _game.DeviceContext.ComputeShader.SetConstantBuffer(0, computeBuffer);
                                                    context.Dispatch((int)Math.Ceiling(desc.Width / 1.0), (int)Math.Ceiling(desc.Height / 1024.0), 1);

                                                    // Set the compute shader stage SRV and UAV to null
                                                    context.ComputeShader.SetShaderResource(0, null);
                                                    context.ComputeShader.SetUnorderedAccessView(0, null);
                                                    _game.DeviceContext.ComputeShader.SetConstantBuffer(0, null);
                                                    StaticMetods.CopyUAVToSRV(_game.DeviceContext.Device, ref srcTextureSRV, target2UAV);
                                                }
            }
        }