Ejemplo n.º 1
0
        public D3D11Framebuffer(Device device, ref FramebufferDescription description)
            : base(description.DepthTarget, description.ColorTargets)
        {
            if (description.DepthTarget != null)
            {
                D3D11Texture d3dDepthTarget         = Util.AssertSubtype <Texture, D3D11Texture>(description.DepthTarget);
                DepthStencilViewDescription dsvDesc = new DepthStencilViewDescription()
                {
                    Dimension = DepthStencilViewDimension.Texture2D,
                    Format    = D3D11Formats.GetDepthFormat(d3dDepthTarget.Format)
                };
                DepthStencilView = new DepthStencilView(device, d3dDepthTarget.DeviceTexture, dsvDesc);
            }

            if (description.ColorTargets != null && description.ColorTargets.Length > 0)
            {
                RenderTargetViews = new RenderTargetView[description.ColorTargets.Length];
                for (int i = 0; i < RenderTargetViews.Length; i++)
                {
                    D3D11Texture d3dColorTarget         = Util.AssertSubtype <Texture, D3D11Texture>(description.ColorTargets[i]);
                    RenderTargetViewDescription rtvDesc = new RenderTargetViewDescription
                    {
                        Format    = D3D11Formats.ToDxgiFormat(d3dColorTarget.Format, false),
                        Dimension = RenderTargetViewDimension.Texture2D,
                    };
                    RenderTargetViews[i] = new RenderTargetView(device, d3dColorTarget.DeviceTexture, rtvDesc);
                }
            }
            else
            {
                RenderTargetViews = Array.Empty <RenderTargetView>();
            }
        }
Ejemplo n.º 2
0
        public override TextureSampleCount GetSampleCountLimit(PixelFormat format, bool depthFormat)
        {
            Format dxgiFormat = D3D11Formats.ToDxgiFormat(format, depthFormat);

            if (CheckFormatMultisample(dxgiFormat, 32))
            {
                return(TextureSampleCount.Count32);
            }
            else if (CheckFormatMultisample(dxgiFormat, 16))
            {
                return(TextureSampleCount.Count16);
            }
            else if (CheckFormatMultisample(dxgiFormat, 8))
            {
                return(TextureSampleCount.Count8);
            }
            else if (CheckFormatMultisample(dxgiFormat, 4))
            {
                return(TextureSampleCount.Count4);
            }
            else if (CheckFormatMultisample(dxgiFormat, 2))
            {
                return(TextureSampleCount.Count2);
            }

            return(TextureSampleCount.Count1);
        }
Ejemplo n.º 3
0
 public override void SetIndexBuffer(IndexBuffer ib)
 {
     if (_ib != ib)
     {
         _ib = ib;
         D3D11IndexBuffer d3d11Buffer = Util.AssertSubtype <IndexBuffer, D3D11IndexBuffer>(ib);
         _context.InputAssembler.SetIndexBuffer(d3d11Buffer.Buffer, D3D11Formats.ToDxgiFormat(ib.Format), 0);
     }
 }
Ejemplo n.º 4
0
        public D3D11TextureView(Device device, ref TextureViewDescription description)
            : base(ref description)
        {
            D3D11Texture d3dTex = Util.AssertSubtype <Texture, D3D11Texture>(description.Target);

            if (BaseMipLevel == 0 && MipLevels == Target.MipLevels ||
                BaseArrayLayer == 0 && ArrayLayers == Target.ArrayLayers)
            {
                ShaderResourceView = d3dTex.GetFullShaderResourceView();
            }
            else
            {
                _srvOwned = true;
                ShaderResourceViewDescription srvDesc = D3D11Util.GetSrvDesc(
                    d3dTex,
                    description.BaseMipLevel,
                    description.MipLevels,
                    description.BaseArrayLayer,
                    description.ArrayLayers);
                ShaderResourceView = new ShaderResourceView(device, d3dTex.DeviceTexture, srvDesc);
            }

            if ((d3dTex.Usage & TextureUsage.Storage) == TextureUsage.Storage)
            {
                UnorderedAccessViewDescription uavDesc = new UnorderedAccessViewDescription();
                uavDesc.Format = D3D11Formats.GetViewFormat(d3dTex.DxgiFormat);

                if ((d3dTex.Usage & TextureUsage.Cubemap) == TextureUsage.Cubemap)
                {
                    throw new NotSupportedException();
                }
                else if (d3dTex.Depth == 1)
                {
                    if (d3dTex.ArrayLayers == 1)
                    {
                        uavDesc.Dimension          = UnorderedAccessViewDimension.Texture2D;
                        uavDesc.Texture2D.MipSlice = (int)description.BaseMipLevel;
                    }
                    else
                    {
                        uavDesc.Dimension = UnorderedAccessViewDimension.Texture2DArray;
                        uavDesc.Texture2DArray.MipSlice        = (int)description.BaseMipLevel;
                        uavDesc.Texture2DArray.FirstArraySlice = (int)description.BaseArrayLayer;
                        uavDesc.Texture2DArray.ArraySize       = (int)description.ArrayLayers;
                    }
                }
                else
                {
                    uavDesc.Dimension             = UnorderedAccessViewDimension.Texture3D;
                    uavDesc.Texture3D.MipSlice    = (int)description.BaseMipLevel;
                    uavDesc.Texture3D.FirstWSlice = (int)description.BaseArrayLayer;
                    uavDesc.Texture3D.WSize       = (int)description.ArrayLayers;
                }

                UnorderedAccessView = new UnorderedAccessView(device, d3dTex.DeviceTexture, uavDesc);
            }
        }
Ejemplo n.º 5
0
 protected override void SetIndexBufferCore(DeviceBuffer buffer, IndexFormat format)
 {
     if (_ib != buffer)
     {
         _ib = buffer;
         D3D11Buffer d3d11Buffer = Util.AssertSubtype <DeviceBuffer, D3D11Buffer>(buffer);
         _context.InputAssembler.SetIndexBuffer(d3d11Buffer.Buffer, D3D11Formats.ToDxgiFormat(format), 0);
     }
 }
Ejemplo n.º 6
0
        internal static ShaderResourceViewDescription GetSrvDesc(
            D3D11Texture tex,
            uint baseMipLevel,
            uint levelCount,
            uint baseArrayLayer,
            uint layerCount,
            PixelFormat format)
        {
            ShaderResourceViewDescription srvDesc = new ShaderResourceViewDescription();

            srvDesc.Format = D3D11Formats.GetViewFormat(
                D3D11Formats.ToDxgiFormat(format, (tex.Usage & TextureUsage.DepthStencil) != 0));

            if ((tex.Usage & TextureUsage.Cubemap) == TextureUsage.Cubemap)
            {
                if (tex.ArrayLayers == 1)
                {
                    srvDesc.Dimension = SharpDX.Direct3D.ShaderResourceViewDimension.TextureCube;
                    srvDesc.TextureCube.MostDetailedMip = (int)baseMipLevel;
                    srvDesc.TextureCube.MipLevels       = (int)levelCount;
                }
                else
                {
                    srvDesc.Dimension = SharpDX.Direct3D.ShaderResourceViewDimension.TextureCubeArray;
                    srvDesc.TextureCubeArray.MostDetailedMip  = (int)baseMipLevel;
                    srvDesc.TextureCubeArray.MipLevels        = (int)levelCount;
                    srvDesc.TextureCubeArray.First2DArrayFace = (int)baseArrayLayer;
                    srvDesc.TextureCubeArray.CubeCount        = (int)tex.ArrayLayers;
                }
            }
            else if (tex.Depth == 1)
            {
                if (tex.ArrayLayers == 1)
                {
                    srvDesc.Dimension = SharpDX.Direct3D.ShaderResourceViewDimension.Texture2D;
                    srvDesc.Texture2D.MostDetailedMip = (int)baseMipLevel;
                    srvDesc.Texture2D.MipLevels       = (int)levelCount;
                }
                else
                {
                    srvDesc.Dimension = SharpDX.Direct3D.ShaderResourceViewDimension.Texture2DArray;
                    srvDesc.Texture2DArray.MostDetailedMip = (int)baseMipLevel;
                    srvDesc.Texture2DArray.MipLevels       = (int)levelCount;
                    srvDesc.Texture2DArray.FirstArraySlice = (int)baseArrayLayer;
                    srvDesc.Texture2DArray.ArraySize       = (int)layerCount;
                }
            }
            else
            {
                srvDesc.Dimension = SharpDX.Direct3D.ShaderResourceViewDimension.Texture3D;
                srvDesc.Texture3D.MostDetailedMip = (int)baseMipLevel;
                srvDesc.Texture3D.MipLevels       = (int)levelCount;
            }

            return(srvDesc);
        }
Ejemplo n.º 7
0
        public D3D11Buffer(Device device, uint sizeInBytes, BufferUsage usage, uint structureByteStride)
        {
            SizeInBytes = sizeInBytes;
            Usage       = usage;
            SharpDX.Direct3D11.BufferDescription bd = new SharpDX.Direct3D11.BufferDescription(
                (int)sizeInBytes,
                D3D11Formats.VdToD3D11BindFlags(usage),
                ResourceUsage.Default);
            if ((usage & BufferUsage.StructuredBufferReadOnly) == BufferUsage.StructuredBufferReadOnly ||
                (usage & BufferUsage.StructuredBufferReadWrite) == BufferUsage.StructuredBufferReadWrite)
            {
                bd.OptionFlags         = ResourceOptionFlags.BufferStructured;
                bd.StructureByteStride = (int)structureByteStride;
            }
            if ((usage & BufferUsage.IndirectBuffer) == BufferUsage.IndirectBuffer)
            {
                bd.OptionFlags = ResourceOptionFlags.DrawIndirectArguments;
            }

            if ((usage & BufferUsage.Dynamic) == BufferUsage.Dynamic)
            {
                bd.Usage          = ResourceUsage.Dynamic;
                bd.CpuAccessFlags = CpuAccessFlags.Write;
            }
            else if ((usage & BufferUsage.Staging) == BufferUsage.Staging)
            {
                bd.Usage          = ResourceUsage.Staging;
                bd.CpuAccessFlags = CpuAccessFlags.Read | CpuAccessFlags.Write;
            }

            _buffer = new SharpDX.Direct3D11.Buffer(device, bd);

            if ((usage & BufferUsage.StructuredBufferReadWrite) == BufferUsage.StructuredBufferReadWrite ||
                (usage & BufferUsage.StructuredBufferReadOnly) == BufferUsage.StructuredBufferReadOnly)
            {
                ShaderResourceViewDescription srvDesc = new ShaderResourceViewDescription
                {
                    Dimension = SharpDX.Direct3D.ShaderResourceViewDimension.Buffer
                };
                srvDesc.Buffer.ElementCount = (int)(SizeInBytes / structureByteStride);
                ShaderResourceView          = new ShaderResourceView(device, _buffer, srvDesc);
            }

            if ((usage & BufferUsage.StructuredBufferReadWrite) == BufferUsage.StructuredBufferReadWrite)
            {
                UnorderedAccessViewDescription uavDesc = new UnorderedAccessViewDescription
                {
                    Dimension = UnorderedAccessViewDimension.Buffer
                };

                uavDesc.Buffer.ElementCount = (int)(SizeInBytes / structureByteStride);
                uavDesc.Format = SharpDX.DXGI.Format.Unknown;

                UnorderedAccessView = new UnorderedAccessView(device, _buffer, uavDesc);
            }
        }
Ejemplo n.º 8
0
 private DepthStencilOperationDescription ToD3D11StencilOpDesc(StencilBehaviorDescription sbd)
 {
     return(new DepthStencilOperationDescription
     {
         Comparison = D3D11Formats.VdToD3D11Comparison(sbd.Comparison),
         PassOperation = D3D11Formats.VdToD3D11StencilOperation(sbd.Pass),
         FailOperation = D3D11Formats.VdToD3D11StencilOperation(sbd.Fail),
         DepthFailOperation = D3D11Formats.VdToD3D11StencilOperation(sbd.DepthFail)
     });
 }
Ejemplo n.º 9
0
 public D3D11Texture(Texture2D existingTexture)
 {
     DeviceTexture = existingTexture;
     Width         = (uint)existingTexture.Description.Width;
     Height        = (uint)existingTexture.Description.Height;
     Depth         = 1;
     MipLevels     = (uint)existingTexture.Description.MipLevels;
     ArrayLayers   = (uint)existingTexture.Description.ArraySize;
     Format        = D3D11Formats.ToVdFormat(existingTexture.Description.Format);
 }
Ejemplo n.º 10
0
        private DepthStencilState CreateNewDepthStencilState(ref DepthStencilStateDescription description)
        {
            SharpDX.Direct3D11.DepthStencilStateDescription dssDesc = new SharpDX.Direct3D11.DepthStencilStateDescription
            {
                DepthComparison = D3D11Formats.VdToD3D11DepthComparison(description.ComparisonKind),
                IsDepthEnabled  = description.DepthTestEnabled,
                DepthWriteMask  = description.DepthWriteEnabled ? DepthWriteMask.All : DepthWriteMask.Zero
            };

            return(new DepthStencilState(_device, dssDesc));
        }
Ejemplo n.º 11
0
        public D3D11Texture(Device device, ref TextureDescription description)
        {
            Width       = description.Width;
            Height      = description.Height;
            Depth       = description.Depth;
            MipLevels   = description.MipLevels;
            ArrayLayers = description.ArrayLayers;
            Format      = description.Format;
            Usage       = description.Usage;

            SharpDX.DXGI.Format dxgiFormat = D3D11Formats.ToDxgiFormat(
                description.Format,
                (description.Usage & TextureUsage.DepthStencil) == TextureUsage.DepthStencil);

            BindFlags bindFlags = BindFlags.None;

            if ((description.Usage & TextureUsage.RenderTarget) == TextureUsage.RenderTarget)
            {
                bindFlags |= BindFlags.RenderTarget;
            }
            if ((description.Usage & TextureUsage.DepthStencil) == TextureUsage.DepthStencil)
            {
                bindFlags |= BindFlags.DepthStencil;
            }
            if ((description.Usage & TextureUsage.Sampled) == TextureUsage.Sampled)
            {
                bindFlags |= BindFlags.ShaderResource;
            }

            ResourceOptionFlags optionFlags = ResourceOptionFlags.None;
            int arraySize = (int)description.ArrayLayers;

            if ((description.Usage & TextureUsage.Cubemap) == TextureUsage.Cubemap)
            {
                optionFlags = ResourceOptionFlags.TextureCube;
                arraySize  *= 6;
            }
            Texture2DDescription deviceDescription = new Texture2DDescription()
            {
                Width             = (int)description.Width,
                Height            = (int)description.Height,
                MipLevels         = (int)description.MipLevels,
                ArraySize         = arraySize,
                Format            = dxgiFormat,
                BindFlags         = bindFlags,
                CpuAccessFlags    = CpuAccessFlags.None,
                Usage             = ResourceUsage.Default,
                SampleDescription = new SharpDX.DXGI.SampleDescription(1, 0),
                OptionFlags       = optionFlags,
            };

            DeviceTexture = new Texture2D(device, deviceDescription);
        }
Ejemplo n.º 12
0
        public D3D11Pipeline(D3D11ResourceCache cache, ref PipelineDescription description)
        {
            BlendState        = cache.GetBlendState(ref description.BlendState);
            DepthStencilState = cache.GetDepthStencilState(ref description.DepthStencilState);
            RasterizerState   = cache.GetRasterizerState(ref description.RasterizerState);
            PrimitiveTopology = D3D11Formats.VdToD3D11PrimitiveTopology(description.PrimitiveTopology);

            byte[] vsBytecode = null;
            ShaderStageDescription[] stages = description.ShaderSet.ShaderStages;
            for (int i = 0; i < description.ShaderSet.ShaderStages.Length; i++)
            {
                if (stages[i].Stage == ShaderStages.Vertex)
                {
                    D3D11Shader d3d11VertexShader = ((D3D11Shader)stages[i].Shader);
                    VertexShader = (VertexShader)d3d11VertexShader.DeviceShader;
                    vsBytecode   = d3d11VertexShader.Bytecode;
                }
                if (stages[i].Stage == ShaderStages.Geometry)
                {
                    GeometryShader = (GeometryShader)((D3D11Shader)stages[i].Shader).DeviceShader;
                }
                if (stages[i].Stage == ShaderStages.TessellationControl)
                {
                    HullShader = (HullShader)((D3D11Shader)stages[i].Shader).DeviceShader;
                }
                if (stages[i].Stage == ShaderStages.TessellationEvaluation)
                {
                    DomainShader = (DomainShader)((D3D11Shader)stages[i].Shader).DeviceShader;
                }
                if (stages[i].Stage == ShaderStages.Fragment)
                {
                    PixelShader = (PixelShader)((D3D11Shader)stages[i].Shader).DeviceShader;
                }
            }

            ResourceLayout[] genericLayouts = description.ResourceLayouts;
            ResourceLayouts = new D3D11ResourceLayout[genericLayouts.Length];
            for (int i = 0; i < ResourceLayouts.Length; i++)
            {
                ResourceLayouts[i] = Util.AssertSubtype <ResourceLayout, D3D11ResourceLayout>(genericLayouts[i]);
            }

            Debug.Assert(vsBytecode != null);
            InputLayout = cache.GetInputLayout(description.ShaderSet.VertexLayouts, vsBytecode);
            int numVertexBuffers = description.ShaderSet.VertexLayouts.Length;

            VertexStrides = new int[numVertexBuffers];
            for (int i = 0; i < numVertexBuffers; i++)
            {
                VertexStrides[i] = (int)description.ShaderSet.VertexLayouts[i].Stride;
            }
        }
Ejemplo n.º 13
0
 public D3D11Texture(Texture2D existingTexture)
 {
     DeviceTexture = existingTexture;
     Width         = (uint)existingTexture.Description.Width;
     Height        = (uint)existingTexture.Description.Height;
     Depth         = 1;
     MipLevels     = (uint)existingTexture.Description.MipLevels;
     ArrayLayers   = (uint)existingTexture.Description.ArraySize;
     Format        = D3D11Formats.ToVdFormat(existingTexture.Description.Format);
     SampleCount   = D3D11Formats.ToVdSampleCount(existingTexture.Description.SampleDescription);
     Type          = TextureType.Texture2D;
     Usage         = TextureUsage.RenderTarget;
 }
Ejemplo n.º 14
0
        private RasterizerState CreateNewRasterizerState(ref RasterizerStateDescription description)
        {
            SharpDX.Direct3D11.RasterizerStateDescription rssDesc = new SharpDX.Direct3D11.RasterizerStateDescription
            {
                CullMode                = D3D11Formats.VdToD3D11CullMode(description.CullMode),
                FillMode                = D3D11Formats.VdToD3D11FillMode(description.FillMode),
                IsDepthClipEnabled      = description.DepthClipEnabled,
                IsScissorEnabled        = description.ScissorTestEnabled,
                IsFrontCounterClockwise = description.FrontFace == FrontFace.CounterClockwise
            };

            return(new RasterizerState(_device, rssDesc));
        }
Ejemplo n.º 15
0
        private ID3D11RasterizerState CreateNewRasterizerState(ref D3D11RasterizerStateCacheKey key)
        {
            RasterizerDescription rssDesc = new RasterizerDescription
            {
                CullMode              = D3D11Formats.VdToD3D11CullMode(key.VeldridDescription.CullMode),
                FillMode              = D3D11Formats.VdToD3D11FillMode(key.VeldridDescription.FillMode),
                DepthClipEnable       = key.VeldridDescription.DepthClipEnabled,
                ScissorEnable         = key.VeldridDescription.ScissorTestEnabled,
                FrontCounterClockwise = key.VeldridDescription.FrontFace == FrontFace.CounterClockwise,
                MultisampleEnable     = key.Multisampled
            };

            return(_device.CreateRasterizerState(rssDesc));
        }
Ejemplo n.º 16
0
        private RasterizerState CreateNewRasterizerState(ref D3D11RasterizerStateCacheKey key)
        {
            SharpDX.Direct3D11.RasterizerStateDescription rssDesc = new SharpDX.Direct3D11.RasterizerStateDescription
            {
                CullMode                = D3D11Formats.VdToD3D11CullMode(key.VeldridDescription.CullMode),
                FillMode                = D3D11Formats.VdToD3D11FillMode(key.VeldridDescription.FillMode),
                IsDepthClipEnabled      = key.VeldridDescription.DepthClipEnabled,
                IsScissorEnabled        = key.VeldridDescription.ScissorTestEnabled,
                IsFrontCounterClockwise = key.VeldridDescription.FrontFace == FrontFace.CounterClockwise,
                IsMultisampleEnabled    = key.Multisampled
            };

            return(new RasterizerState(_device, rssDesc));
        }
Ejemplo n.º 17
0
        private DepthStencilState CreateNewDepthStencilState(ref DepthStencilStateDescription description)
        {
            SharpDX.Direct3D11.DepthStencilStateDescription dssDesc = new SharpDX.Direct3D11.DepthStencilStateDescription
            {
                DepthComparison  = D3D11Formats.VdToD3D11Comparison(description.DepthComparison),
                IsDepthEnabled   = description.DepthTestEnabled,
                DepthWriteMask   = description.DepthWriteEnabled ? DepthWriteMask.All : DepthWriteMask.Zero,
                IsStencilEnabled = description.StencilTestEnabled,
                FrontFace        = ToD3D11StencilOpDesc(description.StencilFront),
                BackFace         = ToD3D11StencilOpDesc(description.StencilBack),
                StencilReadMask  = description.StencilReadMask,
                StencilWriteMask = description.StencilWriteMask
            };

            return(new DepthStencilState(_device, dssDesc));
        }
Ejemplo n.º 18
0
        private ID3D11DepthStencilState CreateNewDepthStencilState(ref DepthStencilStateDescription description)
        {
            DepthStencilDescription dssDesc = new DepthStencilDescription
            {
                DepthFunc        = D3D11Formats.VdToD3D11ComparisonFunc(description.DepthComparison),
                DepthEnable      = description.DepthTestEnabled,
                DepthWriteMask   = description.DepthWriteEnabled ? DepthWriteMask.All : DepthWriteMask.Zero,
                StencilEnable    = description.StencilTestEnabled,
                FrontFace        = ToD3D11StencilOpDesc(description.StencilFront),
                BackFace         = ToD3D11StencilOpDesc(description.StencilBack),
                StencilReadMask  = description.StencilReadMask,
                StencilWriteMask = description.StencilWriteMask
            };

            return(_device.CreateDepthStencilState(dssDesc));
        }
Ejemplo n.º 19
0
        public D3D11TextureView(Device device, ref TextureViewDescription description)
            : base(description.Target)
        {
            D3D11Texture d3dTex = Util.AssertSubtype <Texture, D3D11Texture>(description.Target);
            ShaderResourceViewDescription srvDesc = new ShaderResourceViewDescription();

            srvDesc.Format = D3D11Formats.GetViewFormat(d3dTex.DeviceTexture.Description.Format);

            if ((d3dTex.Usage & TextureUsage.Cubemap) == TextureUsage.Cubemap)
            {
                if (d3dTex.ArrayLayers == 1)
                {
                    srvDesc.Dimension             = SharpDX.Direct3D.ShaderResourceViewDimension.TextureCube;
                    srvDesc.TextureCube.MipLevels = (int)d3dTex.MipLevels;
                }
                else
                {
                    srvDesc.Dimension = SharpDX.Direct3D.ShaderResourceViewDimension.TextureCubeArray;
                    srvDesc.TextureCubeArray.MipLevels = (int)d3dTex.MipLevels;
                    srvDesc.TextureCubeArray.CubeCount = (int)d3dTex.ArrayLayers;
                }
            }
            else if (d3dTex.Depth == 1)
            {
                if (d3dTex.ArrayLayers == 1)
                {
                    srvDesc.Dimension           = SharpDX.Direct3D.ShaderResourceViewDimension.Texture2D;
                    srvDesc.Texture2D.MipLevels = (int)d3dTex.MipLevels;
                }
                else
                {
                    srvDesc.Dimension = SharpDX.Direct3D.ShaderResourceViewDimension.Texture2DArray;
                    srvDesc.Texture2DArray.MipLevels = (int)d3dTex.MipLevels;
                    srvDesc.Texture2DArray.ArraySize = (int)d3dTex.ArrayLayers;
                }
            }
            else
            {
                srvDesc.Dimension           = SharpDX.Direct3D.ShaderResourceViewDimension.Texture3D;
                srvDesc.Texture3D.MipLevels = (int)d3dTex.MipLevels;
            }

            ShaderResourceView = new ShaderResourceView(device, d3dTex.DeviceTexture, srvDesc);
        }
Ejemplo n.º 20
0
        public D3D11Buffer(ID3D11Device device, uint sizeInBytes, BufferUsage usage, uint structureByteStride, bool rawBuffer)
        {
            _device              = device;
            SizeInBytes          = sizeInBytes;
            Usage                = usage;
            _structureByteStride = structureByteStride;
            _rawBuffer           = rawBuffer;

            Vortice.Direct3D11.BufferDescription bd = new Vortice.Direct3D11.BufferDescription(
                (int)sizeInBytes,
                D3D11Formats.VdToD3D11BindFlags(usage),
                ResourceUsage.Default);
            if ((usage & BufferUsage.StructuredBufferReadOnly) == BufferUsage.StructuredBufferReadOnly ||
                (usage & BufferUsage.StructuredBufferReadWrite) == BufferUsage.StructuredBufferReadWrite)
            {
                if (rawBuffer)
                {
                    bd.OptionFlags = ResourceOptionFlags.BufferAllowRawViews;
                }
                else
                {
                    bd.OptionFlags         = ResourceOptionFlags.BufferStructured;
                    bd.StructureByteStride = (int)structureByteStride;
                }
            }
            if ((usage & BufferUsage.IndirectBuffer) == BufferUsage.IndirectBuffer)
            {
                bd.OptionFlags = ResourceOptionFlags.DrawIndirectArguments;
            }

            if ((usage & BufferUsage.Dynamic) == BufferUsage.Dynamic)
            {
                bd.Usage          = ResourceUsage.Dynamic;
                bd.CpuAccessFlags = CpuAccessFlags.Write;
            }
            else if ((usage & BufferUsage.Staging) == BufferUsage.Staging)
            {
                bd.Usage          = ResourceUsage.Staging;
                bd.CpuAccessFlags = CpuAccessFlags.Read | CpuAccessFlags.Write;
            }

            _buffer = device.CreateBuffer(bd);
        }
Ejemplo n.º 21
0
        public D3D11Sampler(Device device, ref SamplerDescription description)
        {
            Comparison comparision = description.ComparisonKind == null ? Comparison.Never : D3D11Formats.VdToD3D11DepthComparison(description.ComparisonKind.Value);
            SamplerStateDescription samplerStateDesc = new SamplerStateDescription
            {
                AddressU           = D3D11Formats.VdToD3D11AddressMode(description.AddressModeU),
                AddressV           = D3D11Formats.VdToD3D11AddressMode(description.AddressModeV),
                AddressW           = D3D11Formats.VdToD3D11AddressMode(description.AddressModeW),
                Filter             = D3D11Formats.ToD3D11Filter(description.Filter, description.ComparisonKind.HasValue),
                MinimumLod         = description.MinimumLod,
                MaximumLod         = description.MaximumLod,
                MaximumAnisotropy  = (int)description.MaximumAnisotropy,
                ComparisonFunction = comparision,
                MipLodBias         = description.LodBias,
                BorderColor        = ToRawColor4(description.BorderColor)
            };

            DeviceSampler = new SamplerState(device, samplerStateDesc);
        }
Ejemplo n.º 22
0
        private BlendState CreateNewBlendState(ref BlendStateDescription description)
        {
            BlendAttachmentDescription[]             attachmentStates  = description.AttachmentStates;
            SharpDX.Direct3D11.BlendStateDescription d3dBlendStateDesc = new SharpDX.Direct3D11.BlendStateDescription();

            for (int i = 0; i < attachmentStates.Length; i++)
            {
                BlendAttachmentDescription state = attachmentStates[i];
                d3dBlendStateDesc.RenderTarget[i].IsBlendEnabled        = state.BlendEnabled;
                d3dBlendStateDesc.RenderTarget[i].RenderTargetWriteMask = ColorWriteMaskFlags.All;
                d3dBlendStateDesc.RenderTarget[i].SourceBlend           = D3D11Formats.VdToD3D11BlendOption(state.SourceColorFactor);
                d3dBlendStateDesc.RenderTarget[i].DestinationBlend      = D3D11Formats.VdToD3D11BlendOption(state.DestinationColorFactor);
                d3dBlendStateDesc.RenderTarget[i].BlendOperation        = D3D11Formats.VdToD3D11BlendOperation(state.ColorFunction);
                d3dBlendStateDesc.RenderTarget[i].SourceAlphaBlend      = D3D11Formats.VdToD3D11BlendOption(state.SourceAlphaFactor);
                d3dBlendStateDesc.RenderTarget[i].DestinationAlphaBlend = D3D11Formats.VdToD3D11BlendOption(state.DestinationAlphaFactor);
                d3dBlendStateDesc.RenderTarget[i].AlphaBlendOperation   = D3D11Formats.VdToD3D11BlendOperation(state.AlphaFunction);
            }

            return(new BlendState(_device, d3dBlendStateDesc));
        }
Ejemplo n.º 23
0
        public D3D11Texture(Texture2D existingTexture, TextureType type, PixelFormat format)
        {
            _device       = existingTexture.Device;
            DeviceTexture = existingTexture;
            Width         = (uint)existingTexture.Description.Width;
            Height        = (uint)existingTexture.Description.Height;
            Depth         = 1;
            MipLevels     = (uint)existingTexture.Description.MipLevels;
            ArrayLayers   = (uint)existingTexture.Description.ArraySize;
            Format        = format;
            SampleCount   = FormatHelpers.GetSampleCount((uint)existingTexture.Description.SampleDescription.Count);
            Type          = type;
            Usage         = D3D11Formats.GetVdUsage(
                existingTexture.Description.BindFlags,
                existingTexture.Description.CpuAccessFlags,
                existingTexture.Description.OptionFlags);

            DxgiFormat = D3D11Formats.ToDxgiFormat(
                format,
                (Usage & TextureUsage.DepthStencil) == TextureUsage.DepthStencil);
        }
Ejemplo n.º 24
0
        private ID3D11BlendState CreateNewBlendState(ref BlendStateDescription description)
        {
            BlendAttachmentDescription[]        attachmentStates  = description.AttachmentStates;
            Vortice.Direct3D11.BlendDescription d3dBlendStateDesc = new Vortice.Direct3D11.BlendDescription();

            for (int i = 0; i < attachmentStates.Length; i++)
            {
                BlendAttachmentDescription state = attachmentStates[i];
                d3dBlendStateDesc.RenderTarget[i].IsBlendEnabled        = state.BlendEnabled;
                d3dBlendStateDesc.RenderTarget[i].RenderTargetWriteMask = ColorWriteEnable.All;
                d3dBlendStateDesc.RenderTarget[i].SourceBlend           = D3D11Formats.VdToD3D11Blend(state.SourceColorFactor);
                d3dBlendStateDesc.RenderTarget[i].DestinationBlend      = D3D11Formats.VdToD3D11Blend(state.DestinationColorFactor);
                d3dBlendStateDesc.RenderTarget[i].BlendOperation        = D3D11Formats.VdToD3D11BlendOperation(state.ColorFunction);
                d3dBlendStateDesc.RenderTarget[i].SourceBlendAlpha      = D3D11Formats.VdToD3D11Blend(state.SourceAlphaFactor);
                d3dBlendStateDesc.RenderTarget[i].DestinationBlendAlpha = D3D11Formats.VdToD3D11Blend(state.DestinationAlphaFactor);
                d3dBlendStateDesc.RenderTarget[i].BlendOperationAlpha   = D3D11Formats.VdToD3D11BlendOperation(state.AlphaFunction);
            }

            d3dBlendStateDesc.AlphaToCoverageEnable = description.AlphaToCoverageEnabled;

            return(_device.CreateBlendState(d3dBlendStateDesc));
        }
Ejemplo n.º 25
0
        private InputLayout CreateNewInputLayout(VertexLayoutDescription[] vertexLayouts, byte[] vsBytecode)
        {
            int totalCount = 0;

            for (int i = 0; i < vertexLayouts.Length; i++)
            {
                totalCount += vertexLayouts[i].Elements.Length;
            }

            int element = 0; // Total element index across slots.

            InputElement[]  elements = new InputElement[totalCount];
            SemanticIndices si       = new SemanticIndices();

            for (int slot = 0; slot < vertexLayouts.Length; slot++)
            {
                VertexElementDescription[] elementDescs = vertexLayouts[slot].Elements;
                uint stepRate      = vertexLayouts[slot].InstanceStepRate;
                int  currentOffset = 0;
                for (int i = 0; i < elementDescs.Length; i++)
                {
                    VertexElementDescription desc = elementDescs[i];
                    elements[element] = new InputElement(
                        GetSemanticString(desc.Semantic),
                        SemanticIndices.GetAndIncrement(ref si, desc.Semantic),
                        D3D11Formats.ToDxgiFormat(desc.Format),
                        currentOffset,
                        slot,
                        stepRate == 0 ? InputClassification.PerVertexData : InputClassification.PerInstanceData,
                        (int)stepRate);

                    currentOffset += (int)FormatHelpers.GetSizeInBytes(desc.Format);
                    element       += 1;
                }
            }

            return(new InputLayout(_device, vsBytecode, elements));
        }
Ejemplo n.º 26
0
        protected override MappedResource MapCore(MappableResource resource, MapMode mode, uint subresource)
        {
            MappedResourceCacheKey key = new MappedResourceCacheKey(resource, subresource);

            lock (_mappedResourceLock)
            {
                if (_mappedResources.TryGetValue(key, out MappedResourceInfo info))
                {
                    if (info.Mode != mode)
                    {
                        throw new VeldridException("The given resource was already mapped with a different MapMode.");
                    }

                    info.RefCount        += 1;
                    _mappedResources[key] = info;
                }
                else
                {
                    // No current mapping exists -- create one.

                    if (resource is D3D11Buffer buffer)
                    {
                        lock (_immediateContextLock)
                        {
                            DataBox db = _immediateContext.MapSubresource(
                                buffer.Buffer,
                                0,
                                D3D11Formats.VdToD3D11MapMode((buffer.Usage & BufferUsage.Dynamic) == BufferUsage.Dynamic, mode),
                                SharpDX.Direct3D11.MapFlags.None);

                            info.MappedResource = new MappedResource(resource, mode, db.DataPointer, buffer.SizeInBytes);
                            info.RefCount       = 1;
                            info.Mode           = mode;
                            _mappedResources.Add(key, info);
                        }
                    }
                    else
                    {
                        D3D11Texture texture = Util.AssertSubtype <MappableResource, D3D11Texture>(resource);
                        lock (_immediateContextLock)
                        {
                            DataBox db = _immediateContext.MapSubresource(
                                texture.DeviceTexture,
                                (int)subresource,
                                D3D11Formats.VdToD3D11MapMode(false, mode),
                                SharpDX.Direct3D11.MapFlags.None,
                                out DataStream ds);

                            info.MappedResource = new MappedResource(
                                resource,
                                mode,
                                db.DataPointer,
                                (uint)ds.Length,
                                subresource,
                                (uint)db.RowPitch,
                                (uint)db.SlicePitch);
                            info.RefCount = 1;
                            info.Mode     = mode;
                            _mappedResources.Add(key, info);
                        }
                    }
                }

                return(info.MappedResource);
            }
        }
Ejemplo n.º 27
0
        protected override bool GetPixelFormatSupportCore(
            PixelFormat format,
            TextureType type,
            TextureUsage usage,
            out PixelFormatProperties properties)
        {
            if (D3D11Formats.IsUnsupportedFormat(format))
            {
                properties = default(PixelFormatProperties);
                return(false);
            }

            Format        dxgiFormat = D3D11Formats.ToDxgiFormat(format, (usage & TextureUsage.DepthStencil) != 0);
            FormatSupport fs         = _device.CheckFormatSupport(dxgiFormat);

            if ((usage & TextureUsage.RenderTarget) != 0 && (fs & FormatSupport.RenderTarget) == 0 ||
                (usage & TextureUsage.DepthStencil) != 0 && (fs & FormatSupport.DepthStencil) == 0 ||
                (usage & TextureUsage.Sampled) != 0 && (fs & FormatSupport.ShaderSample) == 0 ||
                (usage & TextureUsage.Cubemap) != 0 && (fs & FormatSupport.TextureCube) == 0 ||
                (usage & TextureUsage.Storage) != 0 && (fs & FormatSupport.TypedUnorderedAccessView) == 0)
            {
                properties = default(PixelFormatProperties);
                return(false);
            }

            const uint MaxTextureDimension = 16384;
            const uint MaxVolumeExtent     = 2048;

            uint sampleCounts = 0;

            if (CheckFormatMultisample(dxgiFormat, 1))
            {
                sampleCounts |= (1 << 0);
            }
            if (CheckFormatMultisample(dxgiFormat, 2))
            {
                sampleCounts |= (1 << 1);
            }
            if (CheckFormatMultisample(dxgiFormat, 4))
            {
                sampleCounts |= (1 << 2);
            }
            if (CheckFormatMultisample(dxgiFormat, 8))
            {
                sampleCounts |= (1 << 3);
            }
            if (CheckFormatMultisample(dxgiFormat, 16))
            {
                sampleCounts |= (1 << 4);
            }
            if (CheckFormatMultisample(dxgiFormat, 32))
            {
                sampleCounts |= (1 << 5);
            }

            properties = new PixelFormatProperties(
                MaxTextureDimension,
                type == TextureType.Texture1D ? 1 : MaxTextureDimension,
                type != TextureType.Texture3D ? 1 : MaxVolumeExtent,
                uint.MaxValue,
                type == TextureType.Texture3D ? 1 : MaxVolumeExtent,
                sampleCounts);
            return(true);
        }
Ejemplo n.º 28
0
        public D3D11Texture(Device device, ref TextureDescription description)
        {
            _device     = device;
            Width       = description.Width;
            Height      = description.Height;
            Depth       = description.Depth;
            MipLevels   = description.MipLevels;
            ArrayLayers = description.ArrayLayers;
            Format      = description.Format;
            Usage       = description.Usage;
            Type        = description.Type;
            SampleCount = description.SampleCount;

            DxgiFormat = D3D11Formats.ToDxgiFormat(
                description.Format,
                (description.Usage & TextureUsage.DepthStencil) == TextureUsage.DepthStencil);

            CpuAccessFlags      cpuFlags      = CpuAccessFlags.None;
            ResourceUsage       resourceUsage = ResourceUsage.Default;
            BindFlags           bindFlags     = BindFlags.None;
            ResourceOptionFlags optionFlags   = ResourceOptionFlags.None;

            if ((description.Usage & TextureUsage.RenderTarget) == TextureUsage.RenderTarget)
            {
                bindFlags |= BindFlags.RenderTarget;
            }
            if ((description.Usage & TextureUsage.DepthStencil) == TextureUsage.DepthStencil)
            {
                bindFlags |= BindFlags.DepthStencil;
            }
            if ((description.Usage & TextureUsage.Sampled) == TextureUsage.Sampled)
            {
                bindFlags |= BindFlags.ShaderResource;
            }
            if ((description.Usage & TextureUsage.Storage) == TextureUsage.Storage)
            {
                bindFlags |= BindFlags.UnorderedAccess;
            }
            if ((description.Usage & TextureUsage.Staging) == TextureUsage.Staging)
            {
                cpuFlags      = CpuAccessFlags.Read | CpuAccessFlags.Write;
                resourceUsage = ResourceUsage.Staging;
            }

            if ((description.Usage & TextureUsage.GenerateMipmaps) != 0)
            {
                bindFlags   |= BindFlags.RenderTarget | BindFlags.ShaderResource;
                optionFlags |= ResourceOptionFlags.GenerateMipMaps;
            }

            int arraySize = (int)description.ArrayLayers;

            if ((description.Usage & TextureUsage.Cubemap) == TextureUsage.Cubemap)
            {
                optionFlags = ResourceOptionFlags.TextureCube;
                arraySize  *= 6;
            }

            if (Type == TextureType.Texture1D)
            {
                Texture1DDescription desc1D = new Texture1DDescription()
                {
                    Width          = (int)description.Width,
                    MipLevels      = (int)description.MipLevels,
                    ArraySize      = arraySize,
                    Format         = DxgiFormat,
                    BindFlags      = bindFlags,
                    CpuAccessFlags = cpuFlags,
                    Usage          = resourceUsage,
                    OptionFlags    = optionFlags,
                };

                DeviceTexture = new Texture1D(device, desc1D);
            }
            else if (Type == TextureType.Texture2D)
            {
                Texture2DDescription deviceDescription = new Texture2DDescription()
                {
                    Width             = (int)description.Width,
                    Height            = (int)description.Height,
                    MipLevels         = (int)description.MipLevels,
                    ArraySize         = arraySize,
                    Format            = DxgiFormat,
                    BindFlags         = bindFlags,
                    CpuAccessFlags    = cpuFlags,
                    Usage             = resourceUsage,
                    SampleDescription = new SharpDX.DXGI.SampleDescription((int)FormatHelpers.GetSampleCountUInt32(SampleCount), 0),
                    OptionFlags       = optionFlags,
                };

                DeviceTexture = new Texture2D(device, deviceDescription);
            }
            else
            {
                Debug.Assert(Type == TextureType.Texture3D);
                Texture3DDescription desc3D = new Texture3DDescription()
                {
                    Width          = (int)description.Width,
                    Height         = (int)description.Height,
                    Depth          = (int)description.Depth,
                    MipLevels      = (int)description.MipLevels,
                    Format         = DxgiFormat,
                    BindFlags      = bindFlags,
                    CpuAccessFlags = cpuFlags,
                    Usage          = resourceUsage,
                    OptionFlags    = optionFlags,
                };

                DeviceTexture = new Texture3D(device, desc3D);
            }
        }
Ejemplo n.º 29
0
        public D3D11Pipeline(D3D11ResourceCache cache, ref GraphicsPipelineDescription description)
            : base(ref description)
        {
            byte[] vsBytecode = null;
            Shader[] stages = description.ShaderSet.Shaders;
            for (int i = 0; i < description.ShaderSet.Shaders.Length; i++)
            {
                if (stages[i].Stage == ShaderStages.Vertex)
                {
                    D3D11Shader d3d11VertexShader = ((D3D11Shader)stages[i]);
                    VertexShader = (VertexShader)d3d11VertexShader.DeviceShader;
                    vsBytecode = d3d11VertexShader.Bytecode;
                }
                if (stages[i].Stage == ShaderStages.Geometry)
                {
                    GeometryShader = (GeometryShader)((D3D11Shader)stages[i]).DeviceShader;
                }
                if (stages[i].Stage == ShaderStages.TessellationControl)
                {
                    HullShader = (HullShader)((D3D11Shader)stages[i]).DeviceShader;
                }
                if (stages[i].Stage == ShaderStages.TessellationEvaluation)
                {
                    DomainShader = (DomainShader)((D3D11Shader)stages[i]).DeviceShader;
                }
                if (stages[i].Stage == ShaderStages.Fragment)
                {
                    PixelShader = (PixelShader)((D3D11Shader)stages[i]).DeviceShader;
                }
                if (stages[i].Stage == ShaderStages.Compute)
                {
                    ComputeShader = (ComputeShader)((D3D11Shader)stages[i]).DeviceShader;
                }
            }

            cache.GetPipelineResources(
                ref description.BlendState,
                ref description.DepthStencilState,
                ref description.RasterizerState,
                description.Outputs.SampleCount != TextureSampleCount.Count1,
                description.ShaderSet.VertexLayouts,
                vsBytecode,
                out BlendState blendState,
                out DepthStencilState depthStencilState,
                out RasterizerState rasterizerState,
                out InputLayout inputLayout);

            BlendState = blendState;
            DepthStencilState = depthStencilState;
            StencilReference = description.DepthStencilState.StencilReference;
            RasterizerState = rasterizerState;
            PrimitiveTopology = D3D11Formats.VdToD3D11PrimitiveTopology(description.PrimitiveTopology);

            ResourceLayout[] genericLayouts = description.ResourceLayouts;
            ResourceLayouts = new D3D11ResourceLayout[genericLayouts.Length];
            for (int i = 0; i < ResourceLayouts.Length; i++)
            {
                ResourceLayouts[i] = Util.AssertSubtype<ResourceLayout, D3D11ResourceLayout>(genericLayouts[i]);
            }

            Debug.Assert(vsBytecode != null || ComputeShader != null);
            if (vsBytecode != null && description.ShaderSet.VertexLayouts.Length > 0)
            {
                InputLayout = inputLayout;
                int numVertexBuffers = description.ShaderSet.VertexLayouts.Length;
                VertexStrides = new int[numVertexBuffers];
                for (int i = 0; i < numVertexBuffers; i++)
                {
                    VertexStrides[i] = (int)description.ShaderSet.VertexLayouts[i].Stride;
                }
            }
            else
            {
                VertexStrides = Array.Empty<int>();
            }
        }
Ejemplo n.º 30
0
        public D3D11TextureView(Device device, ref TextureViewDescription description)
            : base(ref description)
        {
            D3D11Texture d3dTex = Util.AssertSubtype <Texture, D3D11Texture>(description.Target);
            ShaderResourceViewDescription srvDesc = new ShaderResourceViewDescription();

            srvDesc.Format = D3D11Formats.GetViewFormat(d3dTex.DxgiFormat);

            if ((d3dTex.Usage & TextureUsage.Cubemap) == TextureUsage.Cubemap)
            {
                if (d3dTex.ArrayLayers == 1)
                {
                    srvDesc.Dimension = SharpDX.Direct3D.ShaderResourceViewDimension.TextureCube;
                    srvDesc.TextureCube.MostDetailedMip = (int)description.BaseMipLevel;
                    srvDesc.TextureCube.MipLevels       = (int)description.MipLevels;
                }
                else
                {
                    srvDesc.Dimension = SharpDX.Direct3D.ShaderResourceViewDimension.TextureCubeArray;
                    srvDesc.TextureCubeArray.MostDetailedMip  = (int)description.BaseMipLevel;
                    srvDesc.TextureCubeArray.MipLevels        = (int)description.MipLevels;
                    srvDesc.TextureCubeArray.First2DArrayFace = (int)description.BaseArrayLayer;
                    srvDesc.TextureCubeArray.CubeCount        = (int)d3dTex.ArrayLayers;
                }
            }
            else if (d3dTex.Depth == 1)
            {
                if (d3dTex.ArrayLayers == 1)
                {
                    srvDesc.Dimension = SharpDX.Direct3D.ShaderResourceViewDimension.Texture2D;
                    srvDesc.Texture2D.MostDetailedMip = (int)description.BaseMipLevel;
                    srvDesc.Texture2D.MipLevels       = (int)description.MipLevels;
                }
                else
                {
                    srvDesc.Dimension = SharpDX.Direct3D.ShaderResourceViewDimension.Texture2DArray;
                    srvDesc.Texture2DArray.MostDetailedMip = (int)description.BaseMipLevel;
                    srvDesc.Texture2DArray.MipLevels       = (int)description.MipLevels;
                    srvDesc.Texture2DArray.FirstArraySlice = (int)description.BaseArrayLayer;
                    srvDesc.Texture2DArray.ArraySize       = (int)description.ArrayLayers;
                }
            }
            else
            {
                srvDesc.Dimension = SharpDX.Direct3D.ShaderResourceViewDimension.Texture3D;
                srvDesc.Texture3D.MostDetailedMip = (int)description.BaseMipLevel;
                srvDesc.Texture3D.MipLevels       = (int)description.MipLevels;
            }

            ShaderResourceView = new ShaderResourceView(device, d3dTex.DeviceTexture, srvDesc);

            if ((d3dTex.Usage & TextureUsage.Storage) == TextureUsage.Storage)
            {
                UnorderedAccessViewDescription uavDesc = new UnorderedAccessViewDescription();
                uavDesc.Format = D3D11Formats.GetViewFormat(d3dTex.DxgiFormat);

                if ((d3dTex.Usage & TextureUsage.Cubemap) == TextureUsage.Cubemap)
                {
                    throw new NotSupportedException();
                }
                else if (d3dTex.Depth == 1)
                {
                    if (d3dTex.ArrayLayers == 1)
                    {
                        uavDesc.Dimension          = UnorderedAccessViewDimension.Texture2D;
                        uavDesc.Texture2D.MipSlice = (int)description.BaseMipLevel;
                    }
                    else
                    {
                        uavDesc.Dimension = UnorderedAccessViewDimension.Texture2DArray;
                        uavDesc.Texture2DArray.MipSlice        = (int)description.BaseMipLevel;
                        uavDesc.Texture2DArray.FirstArraySlice = (int)description.BaseArrayLayer;
                        uavDesc.Texture2DArray.ArraySize       = (int)description.ArrayLayers;
                    }
                }
                else
                {
                    uavDesc.Dimension             = UnorderedAccessViewDimension.Texture3D;
                    uavDesc.Texture3D.MipSlice    = (int)description.BaseMipLevel;
                    uavDesc.Texture3D.FirstWSlice = (int)description.BaseArrayLayer;
                    uavDesc.Texture3D.WSize       = (int)description.ArrayLayers;
                }

                UnorderedAccessView = new UnorderedAccessView(device, d3dTex.DeviceTexture, uavDesc);
            }
        }