В D3D12, как я могу нарисовать квадрат? Следующий код может нарисовать только первый треугольник.
Почему у нас нет D3D12_PRIMITIVE_TOPOLOGY_TYPE_TRIANGLELIST в D3D12? Есть ли возможность для 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;
}
Вам все еще нужно вызвать IASetPrimitiveTopology, чтобы сообщить, хотите ли вы треугольную полосу или список треугольников.
m_commandList-> IASetPrimitiveTopology (D3D_PRIMITIVE_TOPOLOGY_TRIANGLELIST);
Увидеть MSDN. ID3D12GraphicsCommandList :: IASetPrimitiveTopology
Других решений пока нет …