gpt4 book ai didi

c++ - 在D3D12中绘制多个对象

转载 作者:行者123 更新时间:2023-11-30 02:34:33 26 4
gpt4 key购买 nike

我正在修补 DirectX 12,但在尝试绘制“棋盘”时碰壁了。我在网上搜索了很多,所以任何帮助/指点都将不胜感激。

在D3D11中,工作代码如下。

auto context = m_deviceResources->GetD3DDeviceContext();

for (int i = -10; i < 10; i++)
{
for (int j = -10; j < 10; j++)
{
// perform translation
XMStoreFloat4x4(&m_constantBufferData.model, XMMatrixTranspose(XMMatrixTranslation(i, j, 0.0f)));
context->UpdateSubresource(
m_constantBuffer.Get(),
0,
NULL,
&m_constantBufferData,
0,
0
);

// shaders, etc...

// draw the square
context->DrawIndexed(
m_indexCount,
0,
0
);
}
}

在 D3D12 中,我尝试过做同样的事情,但它似乎是在全局执行翻译,因为所有方 block 都在同​​一位置。

bool Sample3DSceneRenderer::Render()
{
if (!m_loadingComplete)
{
return false;
}

DX::ThrowIfFailed(m_deviceResources->GetCommandAllocator()->Reset());

DX::ThrowIfFailed(m_commandList->Reset(m_deviceResources->GetCommandAllocator(), m_pipelineState.Get()));


PIXBeginEvent(m_commandList.Get(), 0, L"Draw the objects");
{
m_commandList->SetGraphicsRootSignature(m_rootSignature.Get());
ID3D12DescriptorHeap* ppHeaps[] = { m_cbvHeap.Get() };
m_commandList->SetDescriptorHeaps(_countof(ppHeaps), ppHeaps);

CD3DX12_GPU_DESCRIPTOR_HANDLE gpuHandle(m_cbvHeap->GetGPUDescriptorHandleForHeapStart(), m_deviceResources->GetCurrentFrameIndex(), m_cbvDescriptorSize);
m_commandList->SetGraphicsRootDescriptorTable(0, gpuHandle);

D3D12_VIEWPORT viewport = m_deviceResources->GetScreenViewport();
m_commandList->RSSetViewports(1, &viewport);
m_commandList->RSSetScissorRects(1, &m_scissorRect);

CD3DX12_RESOURCE_BARRIER renderTargetResourceBarrier =
CD3DX12_RESOURCE_BARRIER::Transition(m_deviceResources->GetRenderTarget(), D3D12_RESOURCE_STATE_PRESENT, D3D12_RESOURCE_STATE_RENDER_TARGET);
m_commandList->ResourceBarrier(1, &renderTargetResourceBarrier);

D3D12_CPU_DESCRIPTOR_HANDLE renderTargetView = m_deviceResources->GetRenderTargetView();
D3D12_CPU_DESCRIPTOR_HANDLE depthStencilView = m_deviceResources->GetDepthStencilView();
m_commandList->ClearRenderTargetView(renderTargetView, m_colors.Get_background(), 0, nullptr);
m_commandList->ClearDepthStencilView(depthStencilView, D3D12_CLEAR_FLAG_DEPTH, 1.0f, 0, 0, nullptr);


m_commandList->OMSetRenderTargets(1, &renderTargetView, false, &depthStencilView);

m_commandList->IASetPrimitiveTopology(D3D_PRIMITIVE_TOPOLOGY_TRIANGLELIST);
m_commandList->IASetVertexBuffers(0, 1, &m_vertexBufferView);
m_commandList->IASetIndexBuffer(&m_indexBufferView);


for (float i = -10.0f; i < 10.0f; i++)
{
for (float j = -10.0f; j < 10.0f; j++)
{
// as far as I know, this is how I should perform the translation
XMStoreFloat4x4(&m_constantBufferData.model, XMMatrixTranspose(XMMatrixTranslation(i, j, 0.0f)));

UINT8* destination = m_mappedConstantBuffer + (m_deviceResources->GetCurrentFrameIndex() * c_alignedConstantBufferSize);
memcpy(destination, &m_constantBufferData, sizeof(m_constantBufferData));

m_commandList->DrawIndexedInstanced(6, 1, 0, 0, 0);
}
}

CD3DX12_RESOURCE_BARRIER presentResourceBarrier =
CD3DX12_RESOURCE_BARRIER::Transition(m_deviceResources->GetRenderTarget(), D3D12_RESOURCE_STATE_RENDER_TARGET, D3D12_RESOURCE_STATE_PRESENT);
m_commandList->ResourceBarrier(1, &presentResourceBarrier);
}
PIXEndEvent(m_commandList.Get());

DX::ThrowIfFailed(m_commandList->Close());

ID3D12CommandList* ppCommandLists[] = { m_commandList.Get() };
m_deviceResources->GetCommandQueue()->ExecuteCommandLists(_countof(ppCommandLists), ppCommandLists);

return true;
}

谢谢, 切尔西

最佳答案

您只是将翻译矩阵写入模型的每个拷贝的同一 block 内存中。由于在您为最后一个模型编写完翻译矩阵时 GPU 甚至还没有开始绘制第一个模型,因此这些模型中唯一要绘制的地方就是最后一个编写的翻译矩阵的位置。

您需要将每个矩阵写入内存中一个单独的、不同的位置,并确保在 GPU 完成模型绘制之前它们不会被任何其他内容覆盖。

调用 DrawIndexedInstanced 的行为不会立即指示 GPU 绘制任何东西,它只是在命令列表中添加一个命令以在将来的某个时间绘制对象。如果您不熟悉图形 API 和 GPU 执行的异步特性,您可能应该阅读更多有关其工作原理的内容。

关于c++ - 在D3D12中绘制多个对象,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34483778/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com