-
Notifications
You must be signed in to change notification settings - Fork 25
/
test_multi_queue_d3d12.cpp
296 lines (236 loc) · 12 KB
/
test_multi_queue_d3d12.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
// Copyright (c) 2024 Advanced Micro Devices, Inc.
//
// This file is part of the AMD Render Pipeline Shaders SDK which is
// released under the MIT LICENSE.
//
// See file LICENSE.txt for full license details.
#include "test_multi_queue_shared.hpp"
#include "rps/runtime/d3d12/rps_d3d12_runtime.h"
#include "utils/rps_test_win32.hpp"
#include "utils/rps_test_d3d12_renderer.hpp"
using namespace DirectX;
class TestD3D12MultiQueue : public RpsTestD3D12Renderer, public TestRpsMultiQueue
{
public:
TestD3D12MultiQueue()
{
}
protected:
virtual void OnInit(ID3D12GraphicsCommandList* pInitCmdList,
std::vector<ComPtr<ID3D12Object>>& tempResources) override
{
LoadAssets(pInitCmdList, tempResources);
TestRpsMultiQueue::Init(rpsTestUtilCreateDevice(
[this](auto pCreateInfo, auto phDevice) { return CreateRpsRuntimeDevice(*pCreateInfo, *phDevice); }));
auto hMainEntry = rpsRenderGraphGetMainEntry(GetRpsRenderGraph());
REQUIRE_RPS_OK(
rpsProgramBindNode(hMainEntry, "UpdateInstanceData", &TestD3D12MultiQueue::UpdateInstanceData, this));
REQUIRE_RPS_OK(rpsProgramBindNode(hMainEntry, "Procedural", &TestD3D12MultiQueue::Procedural, this));
REQUIRE_RPS_OK(rpsProgramBindNode(hMainEntry, "GenMip", &TestD3D12MultiQueue::GenMip, this));
REQUIRE_RPS_OK(rpsProgramBindNode(hMainEntry, "ShadowMap", &TestD3D12MultiQueue::ShadowMap, this));
REQUIRE_RPS_OK(rpsProgramBindNode(hMainEntry, "ShadingPass", &TestD3D12MultiQueue::ShadingPass, this));
}
virtual void OnCleanUp() override
{
TestRpsMultiQueue::OnDestroy();
m_rootSigCompute = nullptr;
m_rootSigGfx = nullptr;
m_pipelineStateProcedural = nullptr;
m_pipelineStateMipGen = nullptr;
m_pipelineStateShadowMap = nullptr;
m_pipelineStateShading = nullptr;
}
virtual void OnUpdate(uint32_t frameIndex) override
{
Animate(XMUINT2(m_width, m_height));
UpdatePipeline(frameIndex, CalcGuaranteedCompletedFrameIndexForRps());
}
virtual void OnRender(uint32_t frameIndex) override
{
ExecuteRenderGraph(frameIndex, GetRpsRenderGraph());
}
private:
void UpdateInstanceData(const RpsCmdCallbackContext* pContext,
ID3D12Resource* pUploadBuffer,
ID3D12Resource* pConstantBuffer)
{
constexpr D3D12_RANGE emptyReadRange = {0, 0};
void* pData = nullptr;
if (SUCCEEDED(pUploadBuffer->Map(0, &emptyReadRange, &pData)))
{
size_t sizeToCopy =
std::min(size_t(pUploadBuffer->GetDesc().Width), m_instanceDataGpu.size() * sizeof(InstanceDataGPU));
memcpy(pData, m_instanceDataGpu.data(), sizeToCopy);
pUploadBuffer->Unmap(0, nullptr);
}
if (SUCCEEDED(pConstantBuffer->Map(0, &emptyReadRange, &pData)))
{
memcpy(pData, &m_cbufferData, sizeof(m_cbufferData));
pConstantBuffer->Unmap(0, nullptr);
}
}
void Procedural(const RpsCmdCallbackContext* pContext,
D3D12_CPU_DESCRIPTOR_HANDLE proceduralTextureUav,
ID3D12Resource* pConstantBuffer,
const XMUINT2& outputDim)
{
ID3D12GraphicsCommandList* pCmdList = rpsD3D12CommandListFromHandle(pContext->hCommandBuffer);
D3D12_CPU_DESCRIPTOR_HANDLE uavHdls[2] = {proceduralTextureUav, proceduralTextureUav};
D3D12_GPU_DESCRIPTOR_HANDLE uavTable =
AllocDynamicDescriptorsAndWrite(D3D12_DESCRIPTOR_HEAP_TYPE_CBV_SRV_UAV, uavHdls, 2);
BindDescriptorHeaps(pCmdList);
pCmdList->SetComputeRootSignature(m_rootSigCompute.Get());
pCmdList->SetPipelineState(m_pipelineStateProcedural.Get());
pCmdList->SetComputeRootConstantBufferView(0, pConstantBuffer->GetGPUVirtualAddress());
pCmdList->SetComputeRootDescriptorTable(1, uavTable);
pCmdList->Dispatch(DivRoundUp(outputDim.x, 8), DivRoundUp(outputDim.y, 8), 1);
}
void GenMip(const RpsCmdCallbackContext* pContext,
D3D12_CPU_DESCRIPTOR_HANDLE outMip,
D3D12_CPU_DESCRIPTOR_HANDLE inMip,
const XMUINT2& outputDim)
{
ID3D12GraphicsCommandList* pCmdList = rpsD3D12CommandListFromHandle(pContext->hCommandBuffer);
D3D12_CPU_DESCRIPTOR_HANDLE uavHdls[2] = {outMip, inMip};
D3D12_GPU_DESCRIPTOR_HANDLE uavTable =
AllocDynamicDescriptorsAndWrite(D3D12_DESCRIPTOR_HEAP_TYPE_CBV_SRV_UAV, uavHdls, 2);
BindDescriptorHeaps(pCmdList);
pCmdList->SetComputeRootSignature(m_rootSigCompute.Get());
pCmdList->SetPipelineState(m_pipelineStateMipGen.Get());
pCmdList->SetComputeRootDescriptorTable(1, uavTable);
pCmdList->Dispatch(DivRoundUp(outputDim.x, 8), DivRoundUp(outputDim.y, 8), 1);
}
void ShadowMap(const RpsCmdCallbackContext* pContext,
D3D12_CPU_DESCRIPTOR_HANDLE instanceBuffer,
ID3D12Resource* pConstBuffer)
{
if (!m_pipelineStateShadowMap)
{
RpsCmdRenderTargetInfo renderTargetInfo;
REQUIRE_RPS_OK(rpsCmdGetRenderTargetsInfo(pContext, &renderTargetInfo));
CreateGfxPSO(L"VSShadow", nullptr, &renderTargetInfo, &m_pipelineStateShadowMap);
}
ID3D12GraphicsCommandList* pCmdList = rpsD3D12CommandListFromHandle(pContext->hCommandBuffer);
D3D12_GPU_DESCRIPTOR_HANDLE srvTable =
AllocDynamicDescriptorsAndWrite(D3D12_DESCRIPTOR_HEAP_TYPE_CBV_SRV_UAV, &instanceBuffer, 1);
BindDescriptorHeaps(pCmdList);
pCmdList->SetGraphicsRootSignature(m_rootSigGfx.Get());
pCmdList->SetPipelineState(m_pipelineStateShadowMap.Get());
pCmdList->SetGraphicsRootConstantBufferView(0, pConstBuffer->GetGPUVirtualAddress());
pCmdList->SetGraphicsRootDescriptorTable(1, srvTable);
pCmdList->IASetPrimitiveTopology(D3D_PRIMITIVE_TOPOLOGY_TRIANGLELIST);
pCmdList->DrawInstanced(36, uint32_t(m_instanceData.size()), 0, 0);
}
void ShadingPass(const RpsCmdCallbackContext* pContext,
rps::UnusedArg colorBuffer,
rps::UnusedArg depthBuffer,
D3D12_CPU_DESCRIPTOR_HANDLE instanceBuffer,
D3D12_CPU_DESCRIPTOR_HANDLE shadowMap,
D3D12_CPU_DESCRIPTOR_HANDLE proceduralTexture,
ID3D12Resource* pConstBuffer)
{
if (!m_pipelineStateShading)
{
RpsCmdRenderTargetInfo renderTargetInfo;
REQUIRE_RPS_OK(rpsCmdGetRenderTargetsInfo(pContext, &renderTargetInfo));
CreateGfxPSO(L"VSShading", L"PSShading", &renderTargetInfo, &m_pipelineStateShading);
}
ID3D12GraphicsCommandList* pCmdList = rpsD3D12CommandListFromHandle(pContext->hCommandBuffer);
D3D12_GPU_DESCRIPTOR_HANDLE srvTable =
AllocDynamicDescriptorsAndWrite(D3D12_DESCRIPTOR_HEAP_TYPE_CBV_SRV_UAV, &instanceBuffer, 1);
D3D12_CPU_DESCRIPTOR_HANDLE srcSRVs[] = {shadowMap, proceduralTexture};
D3D12_GPU_DESCRIPTOR_HANDLE srvTablePS =
AllocDynamicDescriptorsAndWrite(D3D12_DESCRIPTOR_HEAP_TYPE_CBV_SRV_UAV, srcSRVs, 2);
BindDescriptorHeaps(pCmdList);
pCmdList->SetGraphicsRootSignature(m_rootSigGfx.Get());
pCmdList->SetPipelineState(m_pipelineStateShading.Get());
pCmdList->SetGraphicsRootConstantBufferView(0, pConstBuffer->GetGPUVirtualAddress());
pCmdList->SetGraphicsRootDescriptorTable(1, srvTable);
pCmdList->SetGraphicsRootDescriptorTable(2, srvTablePS);
pCmdList->IASetPrimitiveTopology(D3D_PRIMITIVE_TOPOLOGY_TRIANGLELIST);
pCmdList->DrawInstanced(36, uint32_t(m_instanceData.size()), 0, 0);
}
private:
void LoadAssets(ID3D12GraphicsCommandList* pInitCmdList, std::vector<ComPtr<ID3D12Object>>& tempResources)
{
OnPostResize();
CreateComputePSOs();
}
void CreateComputePSOs()
{
std::vector<uint8_t> csCode;
DxcCompile(c_Shader, L"CSProcedural", L"cs_6_0", L"", nullptr, 0, csCode);
ThrowIfFailed(m_device->CreateRootSignature(0, csCode.data(), csCode.size(), IID_PPV_ARGS(&m_rootSigCompute)));
D3D12_COMPUTE_PIPELINE_STATE_DESC compPsoDesc = {};
compPsoDesc.pRootSignature = m_rootSigCompute.Get();
compPsoDesc.CS.pShaderBytecode = csCode.data();
compPsoDesc.CS.BytecodeLength = csCode.size();
ThrowIfFailed(m_device->CreateComputePipelineState(&compPsoDesc, IID_PPV_ARGS(&m_pipelineStateProcedural)));
DxcCompile(c_Shader, L"CSMipGen", L"cs_6_0", L"", nullptr, 0, csCode);
compPsoDesc.CS.pShaderBytecode = csCode.data();
compPsoDesc.CS.BytecodeLength = csCode.size();
ThrowIfFailed(m_device->CreateComputePipelineState(&compPsoDesc, IID_PPV_ARGS(&m_pipelineStateMipGen)));
}
void CreateGfxPSO(LPCWSTR vsEntry,
LPCWSTR psEntry,
const RpsCmdRenderTargetInfo* pRtInfo,
ID3D12PipelineState** ppState)
{
std::vector<uint8_t> vsCode, psCode;
DxcCompile(c_Shader, vsEntry, L"vs_6_0", L"", nullptr, 0, vsCode);
if (!m_rootSigGfx)
{
ThrowIfFailed(m_device->CreateRootSignature(0, vsCode.data(), vsCode.size(), IID_PPV_ARGS(&m_rootSigGfx)));
}
// Describe and create the graphics pipeline state object (PSO).
D3D12_GRAPHICS_PIPELINE_STATE_DESC psoDesc = {};
psoDesc.pRootSignature = m_rootSigGfx.Get();
psoDesc.VS = CD3DX12_SHADER_BYTECODE(vsCode.data(), vsCode.size());
if (psEntry)
{
DxcCompile(c_Shader, psEntry, L"ps_6_0", L"", nullptr, 0, psCode);
psoDesc.PS = CD3DX12_SHADER_BYTECODE(psCode.data(), psCode.size());
}
psoDesc.DepthStencilState = CD3DX12_DEPTH_STENCIL_DESC(D3D12_DEFAULT);
psoDesc.RasterizerState = CD3DX12_RASTERIZER_DESC(D3D12_DEFAULT);
psoDesc.BlendState = CD3DX12_BLEND_DESC(D3D12_DEFAULT);
psoDesc.SampleMask = UINT_MAX;
psoDesc.PrimitiveTopologyType = D3D12_PRIMITIVE_TOPOLOGY_TYPE_TRIANGLE;
psoDesc.DSVFormat = rpsFormatToDXGI(pRtInfo->depthStencilFormat);
psoDesc.NumRenderTargets = pRtInfo->numRenderTargets;
for (uint32_t i = 0; i < pRtInfo->numRenderTargets; i++)
{
psoDesc.RTVFormats[i] = rpsFormatToDXGI(pRtInfo->renderTargetFormats[i]);
}
psoDesc.SampleDesc.Count = pRtInfo->numSamples;
ThrowIfFailed(m_device->CreateGraphicsPipelineState(&psoDesc, IID_PPV_ARGS(ppState)));
}
void UpdatePipeline(uint64_t frameIndex, uint64_t completedFrameIndex)
{
RpsRuntimeResource backBuffers[DXGI_MAX_SWAP_CHAIN_BUFFERS];
RpsResourceDesc backBufferDesc = {};
GetBackBuffers(backBufferDesc, backBuffers);
TestRpsMultiQueue::UpdateRpsPipeline(frameIndex, completedFrameIndex, backBufferDesc, backBuffers);
}
private:
ComPtr<ID3D12RootSignature> m_rootSigCompute;
ComPtr<ID3D12RootSignature> m_rootSigGfx;
ComPtr<ID3D12PipelineState> m_pipelineStateProcedural;
ComPtr<ID3D12PipelineState> m_pipelineStateMipGen;
ComPtr<ID3D12PipelineState> m_pipelineStateShadowMap;
ComPtr<ID3D12PipelineState> m_pipelineStateShading;
};
TEST_CASE(TEST_APP_NAME)
{
#if _DEBUG
_CrtSetDbgFlag(_CRTDBG_ALLOC_MEM_DF | _CRTDBG_LEAK_CHECK_DF);
#endif
TestD3D12MultiQueue renderer;
RpsTestRunWindowInfo runInfo = {};
runInfo.title = TEXT(TEST_APP_NAME);
runInfo.numFramesToRender = g_exitAfterFrame;
runInfo.width = 1280;
runInfo.height = 720;
runInfo.pRenderer = &renderer;
RpsTestRunWindowApp(&runInfo);
}