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);
        }
Beispiel #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);
     }
 }
Beispiel #3
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);
            }
        }
        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>();
            }
        }