Пример #1
0
        /// <inheritdoc cref="Draw(GraphicsPrimitive)" />
        public void Draw(D3D12GraphicsPrimitive primitive)
        {
            ThrowIfNull(primitive, nameof(primitive));

            var commandList = D3D12GraphicsCommandList;
            var pipeline    = primitive.Pipeline;
            ref readonly var vertexBufferView = ref primitive.VertexBufferView;
Пример #2
0
        /// <inheritdoc cref="Draw(GraphicsPrimitive)" />
        public void Draw(D3D12GraphicsPrimitive graphicsPrimitive)
        {
            ThrowIfNull(graphicsPrimitive, nameof(graphicsPrimitive));

            var graphicsCommandList = D3D12GraphicsCommandList;
            var graphicsPipeline    = graphicsPrimitive.D3D12GraphicsPipeline;
            var vertexBuffer        = graphicsPrimitive.D3D12VertexBuffer;

            graphicsCommandList->SetGraphicsRootSignature(graphicsPipeline.D3D12Signature.D3D12RootSignature);
            graphicsCommandList->SetPipelineState(graphicsPipeline.D3D12PipelineState);

            var vertexBufferView = new D3D12_VERTEX_BUFFER_VIEW {
                BufferLocation = vertexBuffer.D3D12Resource->GetGPUVirtualAddress(),
                StrideInBytes  = (uint)vertexBuffer.Stride,
                SizeInBytes    = (uint)vertexBuffer.Size,
            };

            graphicsCommandList->IASetVertexBuffers(StartSlot: 0, NumViews: 1, &vertexBufferView);

            var constantBuffers       = graphicsPrimitive.ConstantBuffers;
            var constantBuffersLength = constantBuffers.Length;

            for (var index = 0; index < constantBuffersLength; index++)
            {
                var constantBuffer = (D3D12GraphicsBuffer)constantBuffers[index];
                graphicsCommandList->SetGraphicsRootConstantBufferView(unchecked ((uint)index), constantBuffer.D3D12Resource->GetGPUVirtualAddress());
            }

            var indexBuffer = graphicsPrimitive.D3D12IndexBuffer;

            if (indexBuffer != null)
            {
                var indexBufferStride = indexBuffer.Stride;
                var indexFormat       = DXGI_FORMAT_R16_UINT;

                if (indexBufferStride != 2)
                {
                    Assert(indexBufferStride == 4, "Index Buffer has an unsupported stride.");
                    indexFormat = DXGI_FORMAT_R32_UINT;
                }

                var indexBufferView = new D3D12_INDEX_BUFFER_VIEW {
                    BufferLocation = indexBuffer.D3D12Resource->GetGPUVirtualAddress(),
                    SizeInBytes    = (uint)indexBuffer.Size,
                    Format         = indexFormat,
                };
                graphicsCommandList->IASetIndexBuffer(&indexBufferView);

                graphicsCommandList->DrawIndexedInstanced(IndexCountPerInstance: (uint)(indexBuffer.Size / indexBufferStride), InstanceCount: 1, StartIndexLocation: 0, BaseVertexLocation: 0, StartInstanceLocation: 0);
            }
            else
            {
                graphicsCommandList->DrawInstanced(VertexCountPerInstance: (uint)(vertexBuffer.Size / vertexBuffer.Stride), InstanceCount: 1, StartVertexLocation: 0, StartInstanceLocation: 0);
            }
        }