无法绘制方形DirectX 12 c ++

时间:2016-01-04 07:13:36

标签: c++ visual-c++ directx directx-12

在D3D12中,我怎样画方形?以下代码只能绘制第一个三角形。

为什么D3D12中没有D3D12_PRIMITIVE_TOPOLOGY_TYPE_TRIANGLELIST? TRIANGLELIST有什么选择吗?或者有没有办法直接画广场?

// Describe and create the graphics pipeline state object (PSO).
            D3D12_GRAPHICS_PIPELINE_STATE_DESC psoDesc = {};
            psoDesc.InputLayout = { inputElementDescs, _countof(inputElementDescs) };
            psoDesc.pRootSignature = g_rootSignature.Get();
            psoDesc.VS = CD3DX12_SHADER_BYTECODE(vertexShader.Get());
            psoDesc.PS = CD3DX12_SHADER_BYTECODE(pixelShader.Get());
            psoDesc.RasterizerState = CD3DX12_RASTERIZER_DESC(D3D12_DEFAULT);
            psoDesc.BlendState = CD3DX12_BLEND_DESC(D3D12_DEFAULT);
            psoDesc.DepthStencilState.DepthEnable = FALSE;
            psoDesc.DepthStencilState.StencilEnable = FALSE;
            psoDesc.SampleMask = UINT_MAX;
            psoDesc.PrimitiveTopologyType = D3D12_PRIMITIVE_TOPOLOGY_TYPE_TRIANGLE;
            psoDesc.NumRenderTargets = 1;
            psoDesc.RTVFormats[0] = DXGI_FORMAT_R8G8B8A8_UNORM;
            psoDesc.SampleDesc.Count = 1;
            if (SUCCEEDED(g_pd3dDevice->CreateGraphicsPipelineState(&psoDesc, IID_PPV_ARGS(&g_pipelineState))))
            {
                cout << "CreateGraphicsPipelineState passed";
            }
            else
            {
                cout << "CreateGraphicsPipelineState failed";
                return E_FAIL;
            }
        }

        // Create the command list.
        if (SUCCEEDED(g_pd3dDevice->CreateCommandList(0, D3D12_COMMAND_LIST_TYPE_DIRECT, g_commandAllocator.Get(), g_pipelineState.Get(), IID_PPV_ARGS(&g_commandList))))
        {
            cout << "CreateCommandList passed";
        }
        else
        {
            cout << "CreateCommandList failed";
            return E_FAIL;
        }

        // Create the vertex buffer.
        {
            // Define the geometry for a triangle.
            Vertex triangleVertices[] =
            {

                { { -1.0f, 1.0f, 1.0f },{ 0.0f, 0.0f } },
                { { 1.0f, 1.0f , 1.0f },{ 1.0f, 0.0f } },
                { { 1.0f, -1.0f, 1.0f },{ 1.0f, 1.0f } },

                { { -1.0f, 1.0f, 1.0f },{ 0.0f, 0.0f } },
                { { 1.0f, -1.0f, 1.0f },{ 1.0f, 1.0f } },
                { { -1.0f, -1.0f , 1.0f },{ 0.0f, 1.0f } }

            };

            const UINT vertexBufferSize = sizeof(triangleVertices);



            if (SUCCEEDED(g_pd3dDevice->CreateCommittedResource(
                &CD3DX12_HEAP_PROPERTIES(D3D12_HEAP_TYPE_UPLOAD),
                D3D12_HEAP_FLAG_ALLOW_ALL_BUFFERS_AND_TEXTURES,
                &CD3DX12_RESOURCE_DESC::Buffer(vertexBufferSize),
                D3D12_RESOURCE_STATE_GENERIC_READ,
                nullptr,
                IID_PPV_ARGS(&g_vertexBuffer))))
            {
                cout << "CreateCommittedResource passed";
            }
            else
            {
                cout << "CreateCommittedResource failed";
                return E_FAIL;
            }

            // Copy the triangle data to the vertex buffer.
            UINT8* pVertexDataBegin;
            CD3DX12_RANGE readRange(0, 0);      // We do not intend to read from this resource on the CPU.
            if (SUCCEEDED(g_vertexBuffer->Map(0, &readRange, reinterpret_cast<void**>(&pVertexDataBegin))))
            {
                cout << "Copy the triangle data to the vertex buffer passed";
            }
            else
            {
                cout << "Copy the triangle data to the vertex buffer failed";
                return E_FAIL;
            }

            memcpy(pVertexDataBegin, triangleVertices, sizeof(triangleVertices));
            g_vertexBuffer->Unmap(0, nullptr);
            // Initialize the vertex buffer view.
            g_vertexBufferView.BufferLocation = g_vertexBuffer->GetGPUVirtualAddress();
            g_vertexBufferView.StrideInBytes = sizeof(Vertex);
            g_vertexBufferView.SizeInBytes = vertexBufferSize;

        }

1 个答案:

答案 0 :(得分:6)

您仍然需要调用IASetPrimitiveTopology来告诉它您是否需要三角形条或三角形列表。

m_commandList-&GT; IASetPrimitiveTopology(D3D_PRIMITIVE_TOPOLOGY_TRIANGLELIST);

请参阅MSDN. ID3D12GraphicsCommandList::IASetPrimitiveTopology