public override TextureSampleCount GetSampleCountLimit(PixelFormat format, bool depthFormat)
        {
            Format dxgiFormat = D3D11Formats.ToDxgiFormat(format, depthFormat);

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

            return(TextureSampleCount.Count1);
        }
Exemplo n.º 2
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);
     }
 }
Exemplo n.º 3
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.ToD3D11BindFlags(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);
            }
        }
Exemplo n.º 4
0
 private DepthStencilOperationDescription ToD3D11StencilOpDesc(StencilBehaviorDescription sbd)
 {
     return(new DepthStencilOperationDescription
     {
         Comparison = D3D11Formats.ToD3D11Comparison(sbd.Comparison),
         PassOperation = D3D11Formats.ToD3D11StencilOperation(sbd.Pass),
         FailOperation = D3D11Formats.ToD3D11StencilOperation(sbd.Fail),
         DepthFailOperation = D3D11Formats.ToD3D11StencilOperation(sbd.DepthFail)
     });
 }
 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.ToEngineFormat(existingTexture.Description.Format);
     SampleCount   = D3D11Formats.ToEngineSampleCount(existingTexture.Description.SampleDescription);
 }
Exemplo n.º 6
0
        private RasterizerState CreateNewRasterizerState(ref D3D11RasterizerStateCacheKey key)
        {
            SharpDX.Direct3D11.RasterizerStateDescription rssDesc = new SharpDX.Direct3D11.RasterizerStateDescription
            {
                CullMode                = D3D11Formats.ToD3D11CullMode(key.Description.CullMode),
                FillMode                = D3D11Formats.ToD3D11FillMode(key.Description.FillMode),
                IsDepthClipEnabled      = key.Description.DepthClipEnabled,
                IsScissorEnabled        = key.Description.ScissorTestEnabled,
                IsFrontCounterClockwise = key.Description.FrontFace == FrontFace.CounterClockwise,
                IsMultisampleEnabled    = key.Multisampled
            };

            return(new RasterizerState(_device, rssDesc));
        }
Exemplo n.º 7
0
        private DepthStencilState CreateNewDepthStencilState(ref DepthStencilStateDescription description)
        {
            SharpDX.Direct3D11.DepthStencilStateDescription dssDesc = new SharpDX.Direct3D11.DepthStencilStateDescription
            {
                DepthComparison  = D3D11Formats.ToD3D11Comparison(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));
        }
        public D3D11Sampler(Device device, ref SamplerDescription description)
        {
            Comparison comparision = description.ComparisonKind == null ? Comparison.Never : D3D11Formats.ToD3D11Comparison(description.ComparisonKind.Value);
            SamplerStateDescription samplerStateDesc = new SamplerStateDescription
            {
                AddressU           = D3D11Formats.ToD3D11AddressMode(description.AddressModeU),
                AddressV           = D3D11Formats.ToD3D11AddressMode(description.AddressModeV),
                AddressW           = D3D11Formats.ToD3D11AddressMode(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);
        }
Exemplo n.º 9
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.ToD3D11BlendOption(state.SourceColorFactor);
                d3dBlendStateDesc.RenderTarget[i].DestinationBlend      = D3D11Formats.ToD3D11BlendOption(state.DestinationColorFactor);
                d3dBlendStateDesc.RenderTarget[i].BlendOperation        = D3D11Formats.ToD3D11BlendOperation(state.ColorFunction);
                d3dBlendStateDesc.RenderTarget[i].SourceAlphaBlend      = D3D11Formats.ToD3D11BlendOption(state.SourceAlphaFactor);
                d3dBlendStateDesc.RenderTarget[i].DestinationAlphaBlend = D3D11Formats.ToD3D11BlendOption(state.DestinationAlphaFactor);
                d3dBlendStateDesc.RenderTarget[i].AlphaBlendOperation   = D3D11Formats.ToD3D11BlendOperation(state.AlphaFunction);
            }

            return(new BlendState(_device, d3dBlendStateDesc));
        }
Exemplo n.º 10
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));
        }
        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;
            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;

            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;
            }

            ResourceOptionFlags optionFlags = ResourceOptionFlags.None;
            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);
            }
        }
        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 RendererException("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.ToD3D11MapMode((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.ToD3D11MapMode(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);
            }
        }
        public D3D11Framebuffer(Device device, ref FramebufferDescription description)
            : base(description.DepthTarget, description.ColorTargets)
        {
            if (description.DepthTarget != null)
            {
                D3D11Texture d3dDepthTarget         = Util.AssertSubtype <Texture, D3D11Texture>(description.DepthTarget.Value.Target);
                DepthStencilViewDescription dsvDesc = new DepthStencilViewDescription()
                {
                    Format = D3D11Formats.GetDepthFormat(d3dDepthTarget.Format),
                };
                if (d3dDepthTarget.ArrayLayers == 1)
                {
                    if (d3dDepthTarget.SampleCount == TextureSampleCount.Count1)
                    {
                        dsvDesc.Dimension = DepthStencilViewDimension.Texture2D;
                    }
                    else
                    {
                        dsvDesc.Dimension = DepthStencilViewDimension.Texture2DMultisampled;
                    }
                }
                else
                {
                    if (d3dDepthTarget.SampleCount == TextureSampleCount.Count1)
                    {
                        dsvDesc.Dimension = DepthStencilViewDimension.Texture2DArray;
                        dsvDesc.Texture2DArray.FirstArraySlice = (int)description.DepthTarget.Value.ArrayLayer;
                        dsvDesc.Texture2DArray.ArraySize       = 1;
                    }
                    else
                    {
                        dsvDesc.Dimension = DepthStencilViewDimension.Texture2DMultisampledArray;
                        dsvDesc.Texture2DMSArray.FirstArraySlice = (int)description.DepthTarget.Value.ArrayLayer;
                        dsvDesc.Texture2DMSArray.ArraySize       = 1;
                    }
                }

                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].Target);
                    RenderTargetViewDescription rtvDesc = new RenderTargetViewDescription
                    {
                        Format = D3D11Formats.ToDxgiFormat(d3dColorTarget.Format, false),
                    };
                    if (d3dColorTarget.ArrayLayers == 1)
                    {
                        if (d3dColorTarget.SampleCount == TextureSampleCount.Count1)
                        {
                            rtvDesc.Dimension = RenderTargetViewDimension.Texture2D;
                        }
                        else
                        {
                            rtvDesc.Dimension = RenderTargetViewDimension.Texture2DMultisampled;
                        }
                    }
                    else
                    {
                        if (d3dColorTarget.SampleCount == TextureSampleCount.Count1)
                        {
                            rtvDesc.Dimension      = RenderTargetViewDimension.Texture2DArray;
                            rtvDesc.Texture2DArray = new RenderTargetViewDescription.Texture2DArrayResource
                            {
                                ArraySize       = 1,
                                FirstArraySlice = (int)description.ColorTargets[i].ArrayLayer
                            };
                        }
                        else
                        {
                            rtvDesc.Dimension        = RenderTargetViewDimension.Texture2DMultisampledArray;
                            rtvDesc.Texture2DMSArray = new RenderTargetViewDescription.Texture2DMultisampledArrayResource
                            {
                                ArraySize       = 1,
                                FirstArraySlice = (int)description.ColorTargets[i].ArrayLayer
                            };
                        }
                    }
                    RenderTargetViews[i] = new RenderTargetView(device, d3dColorTarget.DeviceTexture, rtvDesc);
                }
            }
            else
            {
                RenderTargetViews = Array.Empty <RenderTargetView>();
            }
        }
        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);
            }
        }
Exemplo n.º 15
0
        public D3D11Pipeline(D3D11ResourceCache cache, ref GraphicsPipelineDescription description)
            : base(ref description)
        {
            BlendState        = cache.GetBlendState(ref description.BlendState);
            DepthStencilState = cache.GetDepthStencilState(ref description.DepthStencilState);
            StencilReference  = description.DepthStencilState.StencilReference;
            RasterizerState   = cache.GetRasterizerState(
                ref description.RasterizerState,
                description.Outputs.SampleCount != TextureSampleCount.Count1);
            PrimitiveTopology = D3D11Formats.ToD3D11PrimitiveTopology(description.PrimitiveTopology);

            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;
                }
            }

            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 = 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;
                }
            }
            else
            {
                VertexStrides = Array.Empty <int>();
            }
        }