Пример #1
0
        public int[] Histogram(ShaderResourceView srcTextureSRV)
        {
            SharpDX.Direct3D.ShaderMacro[] defines = new[] {
                new SharpDX.Direct3D.ShaderMacro("THREADSX", 32),
                new SharpDX.Direct3D.ShaderMacro("THREADSY", 32),
            };
            using (var histogramResult = new SharpDX.Direct3D11.Buffer(_game.DeviceContext.Device,
                                                                       new BufferDescription
            {
                BindFlags = BindFlags.UnorderedAccess,
                CpuAccessFlags = CpuAccessFlags.None,
                OptionFlags = ResourceOptionFlags.BufferAllowRawViews,
                Usage = ResourceUsage.Default,
                SizeInBytes = 256 * 4,
                StructureByteStride = 4
            }))
                using (var histogramUAV = StaticMetods.CreateBufferUAV(_game.DeviceContext.Device, histogramResult))
                {
                    _game.DeviceContext.ClearUnorderedAccessView(histogramUAV, Int4.Zero);

                    var cpuReadDesc = histogramResult.Description;
                    cpuReadDesc.OptionFlags    = ResourceOptionFlags.None;
                    cpuReadDesc.BindFlags      = BindFlags.None;
                    cpuReadDesc.CpuAccessFlags = CpuAccessFlags.Read;
                    cpuReadDesc.Usage          = ResourceUsage.Staging;

                    using (var histogramCPU = new Buffer(_game.DeviceContext.Device, cpuReadDesc))
                        using (var srcTexture = srcTextureSRV.ResourceAs <Texture2D>())
                            using (var cs = StaticMetods.GetComputeShader(_game.DeviceContext.Device, "Shaders\\Filters\\LumHistorgam.hlsl", defines))
                            {
                                var desc = srcTexture.Description;

                                _game.DeviceContext.ComputeShader.SetShaderResource(0, srcTextureSRV);
                                _game.DeviceContext.ComputeShader.SetUnorderedAccessView(0, histogramUAV);
                                _game.DeviceContext.ComputeShader.Set(cs);
                                _game.DeviceContext.Dispatch((int)Math.Ceiling(desc.Width / 1024.0), (int)Math.Ceiling(desc.Height / 1.0), 1);

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

                                // Копировать результат в буффер из которого наш Процессор может читать данные.
                                _game.DeviceContext.CopyResource(histogramResult, histogramCPU);

                                return(StaticMetods.GetIntArrayFromByfferData(_game.DeviceContext, histogramCPU));
                            }
                }
        }
Пример #2
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);
        }
Пример #3
0
 public Mesh3D(SharpDX.Direct3D11.Device device, AssimpMesh mesh, string texturFolder)
 {
     Center        = mesh.Center;
     primitiveType = mesh.primitiveType;
     Diff          = mesh.Dif;
     this._indeces = mesh.Indeces;
     this._veteces = mesh.Veteces;
     InitBuffers(device);
     if (!string.IsNullOrEmpty(mesh.Texture))
     {
         _textures = StaticMetods.LoadTextureFromFile(device.ImmediateContext, Path.GetFullPath(Path.Combine(Environment.CurrentDirectory, texturFolder + mesh.Texture)));
     }
     //if (!string.IsNullOrEmpty(mesh.NormalMap))
     //    _normalMap = StaticMetods.LoadTextureFromFile(device.ImmediateContext, Path.GetFullPath(Path.Combine(Environment.CurrentDirectory, texturFolder + mesh.NormalMap)));
     //if (!string.IsNullOrEmpty(mesh.SpecularMap))
     //    _specularMap = StaticMetods.LoadTextureFromFile(device.ImmediateContext, Path.GetFullPath(Path.Combine(Environment.CurrentDirectory, texturFolder + mesh.SpecularMap)));
     //if (!string.IsNullOrEmpty(mesh.DiplacementMap))
     //    _displacementMap = StaticMetods.LoadTextureFromFile(device.ImmediateContext, Path.GetFullPath(Path.Combine(Environment.CurrentDirectory, texturFolder + mesh.DiplacementMap)));
 }
Пример #4
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);
                                                }
            }
        }