无法绘制正方形 DirectX 12 c++
Not able to draw square DirectX 12 c++
在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;
}
你仍然需要调用 IASetPrimitiveTopology 来告诉它你想要的是三角形带还是三角形列表。
m_commandList->IASetPrimitiveTopology(D3D_PRIMITIVE_TOPOLOGY_TRIANGLELIST);
在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;
}
你仍然需要调用 IASetPrimitiveTopology 来告诉它你想要的是三角形带还是三角形列表。
m_commandList->IASetPrimitiveTopology(D3D_PRIMITIVE_TOPOLOGY_TRIANGLELIST);