Esempio n. 1
0
 protected DX11IndexBuffer(DxDevice device, int indicescount, SharpDX.Direct3D11.Buffer buffer)
 {
     this.format       = SharpDX.DXGI.Format.R32_UInt;
     this.device       = device;
     this.IndicesCount = indicescount;
     this.Buffer       = buffer;
 }
Esempio n. 2
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));
                            }
                }
        }
Esempio n. 3
0
 public ViewBuffer(SharpDX.Direct3D11.Buffer buffer, SharpDX.Direct3D11.UnorderedAccessView view)
 {
     Buffer = buffer;
     View   = view;
 }
Esempio n. 4
0
        private bool InitialiseShader(Device device, IntPtr windowsHandler, string vsFileName, string psFileName)
        {
            try
            {
                vsFileName = SystemConfiguration.ShaderFilePath + vsFileName;
                psFileName = SystemConfiguration.ShaderFilePath + psFileName;

                var vertexShaderByteCode = ShaderBytecode.CompileFromFile(vsFileName, "TextureVertexShader", "vs_4_0", ShaderFlags.None, EffectFlags.None);
                var pixelShaderByteCode  = ShaderBytecode.CompileFromFile(psFileName, "TexturePixelShader", "ps_4_0", ShaderFlags.None, EffectFlags.None);

                VertexShader = new VertexShader(device, vertexShaderByteCode);
                PixelShader  = new PixelShader(device, pixelShaderByteCode);

                var inputElements = new InputElement[]
                {
                    new InputElement()
                    {
                        SemanticName         = "POSITION",
                        SemanticIndex        = 0,
                        Format               = SharpDX.DXGI.Format.R32G32B32_Float,
                        Slot                 = 0,
                        AlignedByteOffset    = 0,
                        Classification       = InputClassification.PerVertexData,
                        InstanceDataStepRate = 0
                    },
                    new InputElement()
                    {
                        SemanticName         = "TEXCOORD",
                        SemanticIndex        = 0,
                        Format               = SharpDX.DXGI.Format.R32G32_Float,
                        Slot                 = 0,
                        AlignedByteOffset    = InputElement.AppendAligned,
                        Classification       = InputClassification.PerVertexData,
                        InstanceDataStepRate = 0
                    }
                };

                Layout = new InputLayout(device, ShaderSignature.GetInputSignature(vertexShaderByteCode), inputElements);

                vertexShaderByteCode.Dispose();
                pixelShaderByteCode.Dispose();

                BufferDescription matrixBufferDescription = new BufferDescription()
                {
                    Usage               = ResourceUsage.Dynamic,
                    SizeInBytes         = SharpDX.Utilities.SizeOf <WorldViewProjectionMatrixBuffer>(),
                    BindFlags           = BindFlags.ConstantBuffer,
                    CpuAccessFlags      = CpuAccessFlags.Write,
                    OptionFlags         = ResourceOptionFlags.None,
                    StructureByteStride = 0
                };

                ConstantMatrixBuffer = new SharpDX.Direct3D11.Buffer(device, matrixBufferDescription);

                var samplerDesc = new SamplerStateDescription()
                {
                    Filter             = Filter.MinMagMipLinear,
                    AddressU           = TextureAddressMode.Wrap,
                    AddressV           = TextureAddressMode.Wrap,
                    AddressW           = TextureAddressMode.Wrap,
                    MipLodBias         = 0,
                    MaximumAnisotropy  = 1,
                    ComparisonFunction = Comparison.Always,
                    BorderColor        = new Color4(0, 0, 0, 0),               // Black Border.
                    MinimumLod         = 0,
                    MaximumLod         = float.MaxValue
                };

                SamplerState = new SamplerState(device, samplerDesc);

                return(true);
            }
            catch (Exception ex)
            {
                return(false);
            }
        }