Exemple #1
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);
            }
        }
Exemple #2
0
    /// <summary>Binds an index buffer view to the context.</summary>
    /// <param name="indexBufferView">The index buffer view to set.</param>
    /// <exception cref="ArgumentNullException"><paramref name="indexBufferView" /> is <c>null</c>.</exception>
    public void BindIndexBufferView(GraphicsBufferView indexBufferView)
    {
        ThrowIfNull(indexBufferView);

        var d3d12IndexBufferView = new D3D12_INDEX_BUFFER_VIEW {
            BufferLocation = indexBufferView.D3D12GpuVirtualAddress,
            SizeInBytes    = checked ((uint)indexBufferView.ByteLength),
            Format         = indexBufferView.BytesPerElement == 2 ? DXGI_FORMAT_R16_UINT : DXGI_FORMAT_R32_UINT,
        };

        D3D12GraphicsCommandList->IASetIndexBuffer(&d3d12IndexBufferView);
    }
        public virtual void IASetIndexBuffer(
            ref D3D12_INDEX_BUFFER_VIEW pView
            )
        {
            var fp = GetFunctionPointer(43);

            if (m_IASetIndexBufferFunc == null)
            {
                m_IASetIndexBufferFunc = (IASetIndexBufferFunc)Marshal.GetDelegateForFunctionPointer(fp, typeof(IASetIndexBufferFunc));
            }

            m_IASetIndexBufferFunc(m_ptr, ref pView);
        }
        public unsafe bool Render()
        {
            if (!_loadingComplete)
            {
                return(false);
            }

            ThrowIfFailed(_deviceResources.CommandAllocator->Reset());

            ThrowIfFailed(_commandList.Ptr->Reset(_deviceResources.CommandAllocator, _pipelineState.Ptr));

            {
                _commandList.Ptr->SetGraphicsRootSignature(_rootSignature.Ptr);
                const uint             ppHeapsCount = 1;
                ID3D12DescriptorHeap **ppHeaps      = stackalloc ID3D12DescriptorHeap *[(int)ppHeapsCount] {
                    _cbvHeap.Ptr
                };

                _commandList.Ptr->SetDescriptorHeaps(ppHeapsCount, ppHeaps);

                D3D12_GPU_DESCRIPTOR_HANDLE gpuHandle;
                _cbvHeap.Ptr->GetGPUDescriptorHandleForHeapStart(&gpuHandle);
                gpuHandle.ptr += _deviceResources.CurrentFrameIndex * _cbvDescriptorSize;
                _commandList.Ptr->SetGraphicsRootDescriptorTable(0, gpuHandle);

                D3D12_VIEWPORT viewport = _deviceResources.ScreenViewport;
                _commandList.Ptr->RSSetViewports(1, &viewport);
                D3D12_RECT rect = _scissorRect;
                _commandList.Ptr->RSSetScissorRects(1, &rect);

                D3D12_RESOURCE_BARRIER renderTargetResourceBarrier =
                    CD3DX12_RESOURCE_BARRIER.Transition(
                        _deviceResources.RenderTarget,
                        D3D12_RESOURCE_STATE_PRESENT,
                        D3D12_RESOURCE_STATE_RENDER_TARGET
                        );
                _commandList.Ptr->ResourceBarrier(1, &renderTargetResourceBarrier);

                D3D12_CPU_DESCRIPTOR_HANDLE renderTargetView = _deviceResources.RenderTargetView;
                D3D12_CPU_DESCRIPTOR_HANDLE depthStencilView = _deviceResources.DepthStencilView;


                _commandList.Ptr->ClearRenderTargetView(renderTargetView, CornflowerBlue, 0, null);
                _commandList.Ptr->ClearDepthStencilView(depthStencilView, D3D12_CLEAR_FLAG_DEPTH,
                                                        1, 0, 0, null);

                _commandList.Ptr->OMSetRenderTargets(1, &renderTargetView, FALSE, &depthStencilView);

                _commandList.Ptr->IASetPrimitiveTopology(D3D10_PRIMITIVE_TOPOLOGY_TRIANGLELIST);

                D3D12_VERTEX_BUFFER_VIEW vertexBufferView = _vertexBufferView;
                D3D12_INDEX_BUFFER_VIEW  indexBufferView  = _indexBufferView;
                _commandList.Ptr->IASetVertexBuffers(0, 1, &vertexBufferView);
                _commandList.Ptr->IASetIndexBuffer(&indexBufferView);

                _commandList.Ptr->DrawIndexedInstanced(36, 1, 0, 0, 0);

                D3D12_RESOURCE_BARRIER presentResourceBarrier =
                    CD3DX12_RESOURCE_BARRIER.Transition(
                        _deviceResources.RenderTarget,
                        D3D12_RESOURCE_STATE_RENDER_TARGET,
                        D3D12_RESOURCE_STATE_PRESENT);

                _commandList.Ptr->ResourceBarrier(1, &presentResourceBarrier);
            }

            ThrowIfFailed(_commandList.Ptr->Close());

            const uint          ppCommandListsCount = 1;
            ID3D12CommandList **ppCommandLists      = stackalloc ID3D12CommandList *[(int)ppCommandListsCount]
            {
                (ID3D12CommandList *)_commandList.Ptr
            };

            _deviceResources.CommandQueue->ExecuteCommandLists(ppCommandListsCount, ppCommandLists);

            return(true);
        }