Skip to content

Commit

Permalink
Specify read ranges for Map calls and remove unnecessary CBV descript…
Browse files Browse the repository at this point in the history
…ors from ExecuteIndirect sample
  • Loading branch information
bobbrow committed Oct 13, 2015
1 parent 414a8a0 commit ca55151
Show file tree
Hide file tree
Showing 15 changed files with 39 additions and 36 deletions.
3 changes: 2 additions & 1 deletion Samples/D3D12Bundles/src/FrameResource.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,8 @@ FrameResource::FrameResource(ID3D12Device* pDevice, UINT cityRowCount, UINT city
// does not need to be unmapped for use by the GPU. In this sample,
// the resource stays 'permenantly' mapped to avoid overhead with
// mapping/unmapping each frame.
ThrowIfFailed(m_cbvUploadHeap->Map(0, nullptr, reinterpret_cast<void**>(&m_pConstantBuffers)));
CD3DX12_RANGE readRange(0, 0); // We do not intend to read from this resource on the CPU.
ThrowIfFailed(m_cbvUploadHeap->Map(0, &readRange, reinterpret_cast<void**>(&m_pConstantBuffers)));

// Update all of the model matrices once; our cities don't move so
// we don't need to do this ever again.
Expand Down
3 changes: 2 additions & 1 deletion Samples/D3D12DynamicIndexing/src/FrameResource.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,8 @@ FrameResource::FrameResource(ID3D12Device* pDevice, UINT cityRowCount, UINT city
// does not need to be unmapped for use by the GPU. In this sample,
// the resource stays 'permenantly' mapped to avoid overhead with
// mapping/unmapping each frame.
ThrowIfFailed(m_cbvUploadHeap->Map(0, nullptr, reinterpret_cast<void**>(&m_pConstantBuffers)));
CD3DX12_RANGE readRange(0, 0); // We do not intend to read from this resource on the CPU.
ThrowIfFailed(m_cbvUploadHeap->Map(0, &readRange, reinterpret_cast<void**>(&m_pConstantBuffers)));

// Update all of the model matrices once; our cities don't move so
// we don't need to do this ever again.
Expand Down
18 changes: 4 additions & 14 deletions Samples/D3D12ExecuteIndirect/src/D3D12ExecuteIndirect.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -349,9 +349,6 @@ void D3D12ExecuteIndirect::LoadAssets()
nullptr,
IID_PPV_ARGS(&m_constantBuffer)));

CD3DX12_CPU_DESCRIPTOR_HANDLE cpuHandle(m_cbvSrvUavHeap->GetCPUDescriptorHandleForHeapStart());
D3D12_GPU_VIRTUAL_ADDRESS gpuAddress = m_constantBuffer->GetGPUVirtualAddress();

D3D12_CONSTANT_BUFFER_VIEW_DESC cbvDesc = {};
cbvDesc.SizeInBytes = sizeof(ConstantBufferData);

Expand All @@ -362,20 +359,12 @@ void D3D12ExecuteIndirect::LoadAssets()
m_constantBufferData[n].offset = XMFLOAT4(GetRandomFloat(-5.0f, -1.5f), GetRandomFloat(-1.0f, 1.0f), GetRandomFloat(0.0f, 2.0f), 0.0f);
m_constantBufferData[n].color = XMFLOAT4(GetRandomFloat(0.5f, 1.0f), GetRandomFloat(0.5f, 1.0f), GetRandomFloat(0.5f, 1.0f), 1.0f);
XMStoreFloat4x4(&m_constantBufferData[n].projection, XMMatrixTranspose(XMMatrixPerspectiveFovLH(XM_PIDIV4, m_aspectRatio, 0.01f, 20.0f)));

for (int frame = 0; frame < FrameCount; frame++)
{
cbvDesc.BufferLocation = gpuAddress + (cbvDesc.SizeInBytes * TriangleCount * frame);
m_device->CreateConstantBufferView(&cbvDesc, CD3DX12_CPU_DESCRIPTOR_HANDLE(cpuHandle, CbvSrvUavDescriptorCountPerFrame * frame, m_cbvSrvUavDescriptorSize));
}

cpuHandle.Offset(m_cbvSrvUavDescriptorSize);
gpuAddress += cbvDesc.SizeInBytes;
}

// Map the constant buffers. We don't unmap this until the app closes.
// Keeping things mapped for the lifetime of the resource is okay.
ThrowIfFailed(m_constantBuffer->Map(0, nullptr, reinterpret_cast<void**>(&m_pCbvDataBegin)));
CD3DX12_RANGE readRange(0, 0); // We do not intend to read from this resource on the CPU.
ThrowIfFailed(m_constantBuffer->Map(0, &readRange, reinterpret_cast<void**>(&m_pCbvDataBegin)));
memcpy(m_pCbvDataBegin, &m_constantBufferData[0], TriangleCount * sizeof(ConstantBufferData));

// Create shader resource views (SRV) of the constant buffers for the
Expand Down Expand Up @@ -525,7 +514,8 @@ void D3D12ExecuteIndirect::LoadAssets()
IID_PPV_ARGS(&m_processedCommandBufferCounterReset)));

UINT8* pMappedCounterReset = nullptr;
ThrowIfFailed(m_processedCommandBufferCounterReset->Map(0, nullptr, reinterpret_cast<void**>(&pMappedCounterReset)));
CD3DX12_RANGE readRange(0, 0); // We do not intend to read from this resource on the CPU.
ThrowIfFailed(m_processedCommandBufferCounterReset->Map(0, &readRange, reinterpret_cast<void**>(&pMappedCounterReset)));
ZeroMemory(pMappedCounterReset, sizeof(UINT));
m_processedCommandBufferCounterReset->Unmap(0, nullptr);
}
Expand Down
3 changes: 1 addition & 2 deletions Samples/D3D12ExecuteIndirect/src/D3D12ExecuteIndirect.h
Original file line number Diff line number Diff line change
Expand Up @@ -90,8 +90,7 @@ class D3D12ExecuteIndirect : public DXSample
// CBV/SRV/UAV desciptor heap offsets.
enum HeapOffsets
{
CbvOffset = 0,
CbvSrvOffset = CbvOffset + TriangleCount, // SRV that points to the constant buffers used by the rendering thread.
CbvSrvOffset = 0, // SRV that points to the constant buffers used by the rendering thread.
CommandsOffset = CbvSrvOffset + 1, // SRV that points to all of the indirect commands.
ProcessedCommandsOffset = CommandsOffset + 1, // UAV that records the commands we actually want to execute.
CbvSrvUavDescriptorCountPerFrame = ProcessedCommandsOffset + 1 // 1 CBV per triangle + [2 SRVs + 1 UAV for the compute shader].
Expand Down
3 changes: 2 additions & 1 deletion Samples/D3D12Fullscreen/src/D3D12Fullscreen.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -270,7 +270,8 @@ void D3D12Fullscreen::LoadSizeDependentResources()
// Copy data to the intermediate upload heap and then schedule a copy
// from the upload heap to the vertex buffer.
UINT8* pVertexDataBegin;
ThrowIfFailed(m_vertexBufferUpload->Map(0, &CD3DX12_RANGE(0, vertexBufferSize), reinterpret_cast<void**>(&pVertexDataBegin)));
CD3DX12_RANGE readRange(0, 0); // We do not intend to read from this resource on the CPU.
ThrowIfFailed(m_vertexBufferUpload->Map(0, &readRange, reinterpret_cast<void**>(&pVertexDataBegin)));
memcpy(pVertexDataBegin, triangleVertices, sizeof(triangleVertices));
m_vertexBufferUpload->Unmap(0, nullptr);

Expand Down
12 changes: 6 additions & 6 deletions Samples/D3D12Fullscreen/src/D3D12Fullscreen.vcxproj.filters
Original file line number Diff line number Diff line change
Expand Up @@ -24,9 +24,6 @@
</Filter>
</ItemGroup>
<ItemGroup>
<ClInclude Include="stdafx.h">
<Filter>Header Files</Filter>
</ClInclude>
<ClInclude Include="d3dx12.h">
<Filter>Header Files\Util</Filter>
</ClInclude>
Expand All @@ -42,14 +39,14 @@
<ClInclude Include="Win32Application.h">
<Filter>Header Files\Util</Filter>
</ClInclude>
<ClInclude Include="stdafx.h">
<Filter>Header Files</Filter>
</ClInclude>
</ItemGroup>
<ItemGroup>
<ClCompile Include="stdafx.cpp">
<Filter>Source Files</Filter>
</ClCompile>
<ClCompile Include="Main.cpp">
<Filter>Source Files</Filter>
</ClCompile>
<ClCompile Include="D3D12Fullscreen.cpp">
<Filter>Source Files</Filter>
</ClCompile>
Expand All @@ -59,6 +56,9 @@
<ClCompile Include="Win32Application.cpp">
<Filter>Source Files\Util</Filter>
</ClCompile>
<ClCompile Include="Main.cpp">
<Filter>Source Files</Filter>
</ClCompile>
</ItemGroup>
<ItemGroup>
<CustomBuild Include="shaders.hlsl">
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -222,7 +222,8 @@ void D3D12HelloBundles::LoadAssets()

// Copy the triangle data to the vertex buffer.
UINT8* pVertexDataBegin;
ThrowIfFailed(m_vertexBuffer->Map(0, nullptr, reinterpret_cast<void**>(&pVertexDataBegin)));
CD3DX12_RANGE readRange(0, 0); // We do not intend to read from this resource on the CPU.
ThrowIfFailed(m_vertexBuffer->Map(0, &readRange, reinterpret_cast<void**>(&pVertexDataBegin)));
memcpy(pVertexDataBegin, triangleVertices, sizeof(triangleVertices));
m_vertexBuffer->Unmap(0, nullptr);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -246,7 +246,8 @@ void D3D12HelloConstBuffers::LoadAssets()

// Copy the triangle data to the vertex buffer.
UINT8* pVertexDataBegin;
ThrowIfFailed(m_vertexBuffer->Map(0, nullptr, reinterpret_cast<void**>(&pVertexDataBegin)));
CD3DX12_RANGE readRange(0, 0); // We do not intend to read from this resource on the CPU.
ThrowIfFailed(m_vertexBuffer->Map(0, &readRange, reinterpret_cast<void**>(&pVertexDataBegin)));
memcpy(pVertexDataBegin, triangleVertices, sizeof(triangleVertices));
m_vertexBuffer->Unmap(0, nullptr);

Expand Down Expand Up @@ -275,7 +276,9 @@ void D3D12HelloConstBuffers::LoadAssets()
// Initialize and map the constant buffers. We don't unmap this until the
// app closes. Keeping things mapped for the lifetime of the resource is okay.
ZeroMemory(&m_constantBufferData, sizeof(m_constantBufferData));
ThrowIfFailed(m_constantBuffer->Map(0, nullptr, reinterpret_cast<void**>(&m_pCbvDataBegin)));

CD3DX12_RANGE readRange(0, 0); // We do not intend to read from this resource on the CPU.
ThrowIfFailed(m_constantBuffer->Map(0, &readRange, reinterpret_cast<void**>(&m_pCbvDataBegin)));
memcpy(m_pCbvDataBegin, &m_constantBufferData, sizeof(m_constantBufferData));
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -245,7 +245,8 @@ void D3D12HelloTexture::LoadAssets()

// Copy the triangle data to the vertex buffer.
UINT8* pVertexDataBegin;
ThrowIfFailed(m_vertexBuffer->Map(0, nullptr, reinterpret_cast<void**>(&pVertexDataBegin)));
CD3DX12_RANGE readRange(0, 0); // We do not intend to read from this resource on the CPU.
ThrowIfFailed(m_vertexBuffer->Map(0, &readRange, reinterpret_cast<void**>(&pVertexDataBegin)));
memcpy(pVertexDataBegin, triangleVertices, sizeof(triangleVertices));
m_vertexBuffer->Unmap(0, nullptr);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -221,7 +221,8 @@ void D3D12HelloTriangle::LoadAssets()

// Copy the triangle data to the vertex buffer.
UINT8* pVertexDataBegin;
ThrowIfFailed(m_vertexBuffer->Map(0, nullptr, reinterpret_cast<void**>(&pVertexDataBegin)));
CD3DX12_RANGE readRange(0, 0); // We do not intend to read from this resource on the CPU.
ThrowIfFailed(m_vertexBuffer->Map(0, &readRange, reinterpret_cast<void**>(&pVertexDataBegin)));
memcpy(pVertexDataBegin, triangleVertices, sizeof(triangleVertices));
m_vertexBuffer->Unmap(0, nullptr);

Expand Down
5 changes: 3 additions & 2 deletions Samples/D3D12Multithreading/src/FrameResource.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -130,8 +130,9 @@ FrameResource::FrameResource(ID3D12Device* pDevice, ID3D12PipelineState* pPso, I
IID_PPV_ARGS(&m_sceneConstantBuffer)));

// Map the constant buffers and cache their heap pointers.
ThrowIfFailed(m_shadowConstantBuffer->Map(0, nullptr, reinterpret_cast<void**>(&mp_shadowConstantBufferWO)));
ThrowIfFailed(m_sceneConstantBuffer->Map(0, nullptr, reinterpret_cast<void**>(&mp_sceneConstantBufferWO)));
CD3DX12_RANGE readRange(0, 0); // We do not intend to read from this resource on the CPU.
ThrowIfFailed(m_shadowConstantBuffer->Map(0, &readRange, reinterpret_cast<void**>(&mp_shadowConstantBufferWO)));
ThrowIfFailed(m_sceneConstantBuffer->Map(0, &readRange, reinterpret_cast<void**>(&mp_sceneConstantBufferWO)));

// Create the constant buffer views: one for the shadow pass and
// another for the scene pass.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -294,7 +294,8 @@ void D3D12PipelineStateCache::LoadAssets()
IID_PPV_ARGS(&vertexIndexBufferUpload)));

UINT8* mappedUploadHeap = nullptr;
ThrowIfFailed(vertexIndexBufferUpload->Map(0, nullptr, reinterpret_cast<void**>(&mappedUploadHeap)));
CD3DX12_RANGE readRange(0, 0); // We do not intend to read from this resource on the CPU.
ThrowIfFailed(vertexIndexBufferUpload->Map(0, &readRange, reinterpret_cast<void**>(&mappedUploadHeap)));

// Fill in part of the upload heap with our index and vertex data.
UINT8* heapLocation = static_cast<UINT8*>(mappedUploadHeap);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,8 @@ void DynamicConstantBuffer::Init(ID3D12Device* pDevice)
IID_PPV_ARGS(&m_constantBuffer)
));

ThrowIfFailed(m_constantBuffer->Map(0, nullptr, reinterpret_cast<void**>(&m_pMappedConstantBuffer)));
CD3DX12_RANGE readRange(0, 0); // We do not intend to read from this resource on the CPU.
ThrowIfFailed(m_constantBuffer->Map(0, &readRange, reinterpret_cast<void**>(&m_pMappedConstantBuffer)));
}

void* DynamicConstantBuffer::GetMappedMemory(UINT drawIndex, UINT frameIndex)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -321,7 +321,8 @@ void D3D12PredicationQueries::LoadAssets()
// Initialize and map the constant buffers. We don't unmap this until the
// app closes. Keeping things mapped for the lifetime of the resource is okay.
ZeroMemory(&m_constantBufferData, sizeof(m_constantBufferData));
ThrowIfFailed(m_constantBuffer->Map(0, nullptr, reinterpret_cast<void**>(&m_pCbvDataBegin)));
CD3DX12_RANGE readRange(0, 0); // We do not intend to read from this resource on the CPU.
ThrowIfFailed(m_constantBuffer->Map(0, &readRange, reinterpret_cast<void**>(&m_pCbvDataBegin)));
ZeroMemory(m_pCbvDataBegin, FrameCount * sizeof(m_constantBufferData));

// Create constant buffer views to access the upload buffer.
Expand Down
3 changes: 2 additions & 1 deletion Samples/D3D12nBodyGravity/src/D3D12nBodyGravity.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -331,7 +331,8 @@ void D3D12nBodyGravity::LoadAssets()
IID_PPV_ARGS(&m_constantBufferGS)
));

ThrowIfFailed(m_constantBufferGS->Map(0, nullptr, reinterpret_cast<void**>(&m_pConstantBufferGSData)));
CD3DX12_RANGE readRange(0, 0); // We do not intend to read from this resource on the CPU.
ThrowIfFailed(m_constantBufferGS->Map(0, &readRange, reinterpret_cast<void**>(&m_pConstantBufferGSData)));
ZeroMemory(m_pConstantBufferGSData, constantBufferGSSize);
}

Expand Down

0 comments on commit ca55151

Please sign in to comment.