public static void IASetInputLayout(this ID3D11DeviceContext context, ID3D11InputLayout inputLayout)
        {
            if (context == null)
            {
                throw new ArgumentNullException(nameof(context));
            }

            if (inputLayout == null)
            {
                throw new ArgumentNullException(nameof(inputLayout));
            }

            context.IASetInputLayout(inputLayout);
        }
示例#2
0
 public void GetPipelineResources(
     ref BlendStateDescription blendDesc,
     ref DepthStencilStateDescription dssDesc,
     ref RasterizerStateDescription rasterDesc,
     bool multisample,
     VertexLayoutDescription[] vertexLayouts,
     byte[] vsBytecode,
     out ID3D11BlendState blendState,
     out ID3D11DepthStencilState depthState,
     out ID3D11RasterizerState rasterState,
     out ID3D11InputLayout inputLayout)
 {
     lock (_lock)
     {
         blendState  = GetBlendState(ref blendDesc);
         depthState  = GetDepthStencilState(ref dssDesc);
         rasterState = GetRasterizerState(ref rasterDesc, multisample);
         inputLayout = GetInputLayout(vertexLayouts, vsBytecode);
     }
 }
示例#3
0
        protected override void SetPipelineStateImpl(PipelineState pipelineState)
        {
            var pipelineStateD3D11 = (PipelineStateD3D11)pipelineState;

            if (pipelineState.IsCompute)
            {
                var computeShader = pipelineStateD3D11.ComputeShader;
                D3D11Context.CSSetShader(computeShader);
            }
            else
            {
                var blendState = pipelineStateD3D11.BlendState;
                if (_boundBlendState != blendState)
                {
                    _boundBlendState = blendState;
                    D3D11Context.OMSetBlendState(blendState, _boundBlendColor);
                }

                var depthStencilState = pipelineStateD3D11.DepthStencilState;
                if (_boundDepthStencilState != depthStencilState)
                {
                    _boundDepthStencilState = depthStencilState;
                    D3D11Context.OMSetDepthStencilState(depthStencilState, _boundStencilReference);
                }

                var rasterizerState = pipelineStateD3D11.RasterizerState;
                if (_boundRasterizerState != rasterizerState)
                {
                    _boundRasterizerState = rasterizerState;
                    D3D11Context.RSSetState(rasterizerState);
                }

                var primitiveTopology = pipelineStateD3D11.PrimitiveTopology;
                if (_boundPrimitiveTopology != primitiveTopology)
                {
                    _boundPrimitiveTopology = primitiveTopology;
                    D3D11Context.IASetPrimitiveTopology(primitiveTopology);
                }

                var inputLayout = pipelineStateD3D11.InputLayout;
                if (_boundInputLayout != inputLayout)
                {
                    _boundInputLayout = inputLayout;
                    D3D11Context.IASetInputLayout(_boundInputLayout);
                }

                var vertexShader = pipelineStateD3D11.VertexShader;
                if (_boundVertexShader != vertexShader)
                {
                    _boundVertexShader = vertexShader;
                    D3D11Context.VSSetShader(vertexShader);
                }

                var geometryShader = pipelineStateD3D11.GeometryShader;
                if (_boundGeometryShader != geometryShader)
                {
                    _boundGeometryShader = geometryShader;
                    D3D11Context.GSSetShader(geometryShader);
                }

                var hullShader = pipelineStateD3D11.HullShader;
                if (_boundHullShader != hullShader)
                {
                    _boundHullShader = hullShader;
                    D3D11Context.HSSetShader(hullShader);
                }

                var domainShader = pipelineStateD3D11.DomainShader;
                if (_boundDomainShader != domainShader)
                {
                    _boundDomainShader = domainShader;
                    D3D11Context.DSSetShader(domainShader);
                }

                var pixelShader = pipelineStateD3D11.PixelShader;
                if (_boundPixelShader != pixelShader)
                {
                    _boundPixelShader = pixelShader;
                    D3D11Context.PSSetShader(pixelShader);
                }
            }
        }
示例#4
0
    private D3D11GraphicsDevice(Window?window, SizeI size, Format depthStencilFormat = Format.D32_Float)
    {
        Window = window;
        Size   = size;

        Factory = CreateDXGIFactory1 <IDXGIFactory2>();

        using (IDXGIAdapter1 adapter = GetHardwareAdapter())
        {
            DeviceCreationFlags creationFlags = DeviceCreationFlags.BgraSupport;
#if DEBUG
            if (SdkLayersAvailable())
            {
                creationFlags |= DeviceCreationFlags.Debug;
            }
#endif

            if (D3D11CreateDevice(
                    adapter,
                    DriverType.Unknown,
                    creationFlags,
                    s_featureLevels,
                    out ID3D11Device tempDevice, out FeatureLevel, out ID3D11DeviceContext tempContext).Failure)
            {
                // If the initialization fails, fall back to the WARP device.
                // For more information on WARP, see:
                // http://go.microsoft.com/fwlink/?LinkId=286690
                D3D11CreateDevice(
                    IntPtr.Zero,
                    DriverType.Warp,
                    creationFlags,
                    s_featureLevels,
                    out tempDevice, out FeatureLevel, out tempContext).CheckError();
            }

            Device        = tempDevice.QueryInterface <ID3D11Device1>();
            DeviceContext = tempContext.QueryInterface <ID3D11DeviceContext1>();
            tempContext.Dispose();
            tempDevice.Dispose();
        }

        if (window != null)
        {
            IntPtr hwnd = window.Handle;

            SwapChainDescription1 swapChainDescription = new()
            {
                Width             = window.ClientSize.Width,
                Height            = window.ClientSize.Height,
                Format            = Format.R8G8B8A8_UNorm,
                BufferCount       = FrameCount,
                BufferUsage       = Usage.RenderTargetOutput,
                SampleDescription = SampleDescription.Default,
                Scaling           = Scaling.Stretch,
                SwapEffect        = SwapEffect.FlipDiscard,
                AlphaMode         = AlphaMode.Ignore
            };

            SwapChainFullscreenDescription fullscreenDescription = new SwapChainFullscreenDescription
            {
                Windowed = true
            };

            SwapChain = Factory.CreateSwapChainForHwnd(Device, hwnd, swapChainDescription, fullscreenDescription);
            Factory.MakeWindowAssociation(hwnd, WindowAssociationFlags.IgnoreAltEnter);

            BackBufferTexture = SwapChain.GetBuffer <ID3D11Texture2D>(0);
            RenderTargetView  = Device.CreateRenderTargetView(BackBufferTexture);
        }
        else
        {
            // Create offscreen texture
            OffscreenTexture = Device.CreateTexture2D(Format.R8G8B8A8_UNorm, Size.Width, Size.Height, 1, 1, null, BindFlags.ShaderResource | BindFlags.RenderTarget);
            RenderTargetView = Device.CreateRenderTargetView(OffscreenTexture);
        }

        if (depthStencilFormat != Format.Unknown)
        {
            DepthStencilTexture = Device.CreateTexture2D(depthStencilFormat, Size.Width, Size.Height, 1, 1, null, BindFlags.DepthStencil);
            DepthStencilView    = Device.CreateDepthStencilView(DepthStencilTexture !, new DepthStencilViewDescription(DepthStencilTexture, DepthStencilViewDimension.Texture2D));
        }

        ReadOnlySpan <VertexPositionColor> triangleVertices = stackalloc VertexPositionColor[]
        {
            new VertexPositionColor(new Vector3(0f, 0.5f, 0.0f), new Color4(1.0f, 0.0f, 0.0f, 1.0f)),
            new VertexPositionColor(new Vector3(0.5f, -0.5f, 0.0f), new Color4(0.0f, 1.0f, 0.0f, 1.0f)),
            new VertexPositionColor(new Vector3(-0.5f, -0.5f, 0.0f), new Color4(0.0f, 0.0f, 1.0f, 1.0f))
        };

        bool dynamic = false;
        if (dynamic)
        {
            _vertexBuffer = Device.CreateBuffer(VertexPositionColor.SizeInBytes * 3, BindFlags.VertexBuffer, ResourceUsage.Dynamic, CpuAccessFlags.Write);
            MappedSubresource mappedSubresource = DeviceContext.Map(_vertexBuffer, 0, MapMode.WriteDiscard);
            triangleVertices.CopyTo(mappedSubresource.AsSpan <VertexPositionColor>(3));
            DeviceContext.Unmap(_vertexBuffer, 0);
        }
        else
        {
            _vertexBuffer = Device.CreateBuffer(triangleVertices, BindFlags.VertexBuffer);
        }

        InputElementDescription[] inputElementDescs = new[]
        {
            new InputElementDescription("POSITION", 0, Format.R32G32B32_Float, 0, 0),
            new InputElementDescription("COLOR", 0, Format.R32G32B32A32_Float, 12, 0)
        };

        Span <byte> vertexShaderByteCode = CompileBytecode("Triangle.hlsl", "VSMain", "vs_4_0");
        Span <byte> pixelShaderByteCode  = CompileBytecode("Triangle.hlsl", "PSMain", "ps_4_0");

        _vertexShader = Device.CreateVertexShader(vertexShaderByteCode);
        _pixelShader  = Device.CreatePixelShader(pixelShaderByteCode);
        _inputLayout  = Device.CreateInputLayout(inputElementDescs, vertexShaderByteCode);
    }
 internal D3D11InputLayout(ID3D11InputLayout inputLayout)
 {
     this.inputLayout = inputLayout;
 }
示例#6
0
 public InputLayout(ID3D11InputLayout inputLayout)
 {
     _inputLayout = inputLayout ?? throw new ArgumentNullException(nameof(inputLayout));
 }
 internal D3D11InputLayout(ID3D11InputLayout inputLayout)
 {
     this.inputLayout = inputLayout;
 }
示例#8
0
        void CreateDeviceObjects()
        {
            var vertexShaderCode =
                @"
                    cbuffer vertexBuffer : register(b0) 
                    {
                        float4x4 ProjectionMatrix; 
                    };

                    struct VS_INPUT
                    {
                        float2 pos : POSITION;
                        float4 col : COLOR0;
                        float2 uv  : TEXCOORD0;
                    };
            
                    struct PS_INPUT
                    {
                        float4 pos : SV_POSITION;
                        float4 col : COLOR0;
                        float2 uv  : TEXCOORD0;
                    };
            
                    PS_INPUT main(VS_INPUT input)
                    {
                        PS_INPUT output;
                        output.pos = mul(ProjectionMatrix, float4(input.pos.xy, 0.f, 1.f));
                        output.col = input.col;
                        output.uv  = input.uv;
                        return output;
                    }";

            Compiler.Compile(vertexShaderCode, "main", "vs", "vs_4_0", out vertexShaderBlob, out var errorBlob);
            if (vertexShaderBlob == null)
            {
                throw new Exception("error compiling vertex shader");
            }

            vertexShader = device.CreateVertexShader(vertexShaderBlob.GetBytes());

            var inputElements = new[]
            {
                new InputElementDescription("POSITION", 0, Format.R32G32_Float, 0, 0, InputClassification.PerVertexData, 0),
                new InputElementDescription("TEXCOORD", 0, Format.R32G32_Float, 8, 0, InputClassification.PerVertexData, 0),
                new InputElementDescription("COLOR", 0, Format.R8G8B8A8_UNorm, 16, 0, InputClassification.PerVertexData, 0),
            };

            inputLayout = device.CreateInputLayout(inputElements, vertexShaderBlob);

            var constBufferDesc = new BufferDescription
            {
                SizeInBytes    = VertexConstantBufferSize,
                Usage          = Vortice.Direct3D11.Usage.Dynamic,
                BindFlags      = BindFlags.ConstantBuffer,
                CpuAccessFlags = CpuAccessFlags.Write
            };

            constantBuffer = device.CreateBuffer(constBufferDesc);

            var pixelShaderCode =
                @"struct PS_INPUT
                    {
                        float4 pos : SV_POSITION;
                        float4 col : COLOR0;
                        float2 uv  : TEXCOORD0;
                    };

                    sampler sampler0;
                    Texture2D texture0;
            
                    float4 main(PS_INPUT input) : SV_Target
                    {
                        float4 out_col = input.col * texture0.Sample(sampler0, input.uv); 
                        return out_col; 
                    }";

            Compiler.Compile(pixelShaderCode, "main", "ps", "ps_4_0", out pixelShaderBlob, out errorBlob);
            if (pixelShaderBlob == null)
            {
                throw new Exception("error compiling pixel shader");
            }

            pixelShader = device.CreatePixelShader(pixelShaderBlob.GetBytes());

            var blendDesc = new BlendDescription
            {
                AlphaToCoverageEnable = false
            };

            blendDesc.RenderTarget[0] = new RenderTargetBlendDescription
            {
                IsBlendEnabled        = true,
                SourceBlend           = Blend.SourceAlpha,
                DestinationBlend      = Blend.InverseSourceAlpha,
                BlendOperation        = BlendOperation.Add,
                SourceBlendAlpha      = Blend.InverseSourceAlpha,
                DestinationBlendAlpha = Blend.Zero,
                BlendOperationAlpha   = BlendOperation.Add,
                RenderTargetWriteMask = ColorWriteEnable.All
            };

            blendState = device.CreateBlendState(blendDesc);

            var rasterDesc = new RasterizerDescription
            {
                FillMode        = FillMode.Solid,
                CullMode        = CullMode.None,
                ScissorEnable   = true,
                DepthClipEnable = true
            };

            rasterizerState = device.CreateRasterizerState(rasterDesc);

            var stencilOpDesc = new DepthStencilOperationDescription(StencilOperation.Keep, StencilOperation.Keep, StencilOperation.Keep, ComparisonFunction.Always);
            var depthDesc     = new DepthStencilDescription
            {
                DepthEnable    = false,
                DepthWriteMask = DepthWriteMask.All,
                DepthFunc      = ComparisonFunction.Always,
                StencilEnable  = false,
                FrontFace      = stencilOpDesc,
                BackFace       = stencilOpDesc
            };

            depthStencilState = device.CreateDepthStencilState(depthDesc);

            CreateFontsTexture();
        }