-
Notifications
You must be signed in to change notification settings - Fork 25
/
test_multithreading_d3d12.cpp
315 lines (241 loc) · 11.6 KB
/
test_multithreading_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
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
// 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_multithreading_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 TestD3D12Multithreading : public RpsTestD3D12Renderer, public TestRpsMultithreading
{
protected:
virtual void OnInit(ID3D12GraphicsCommandList* pInitCmdList, std::vector<ComPtr<ID3D12Object>>& tempResources) override
{
LoadAssets(pInitCmdList, tempResources);
TestRpsMultithreading::Init(rpsTestUtilCreateDevice(
[this](auto pCreateInfo, auto phDevice) { return CreateRpsRuntimeDevice(*pCreateInfo, *phDevice); }));
}
virtual void OnCleanUp() override
{
TestRpsMultithreading::OnDestroy();
m_rootSignature = nullptr;
m_pipelineState = nullptr;
}
virtual void OnUpdate(uint32_t frameIndex) override
{
UpdatePipeline(frameIndex, CalcGuaranteedCompletedFrameIndexForRps());
if (frameIndex < 50 * MAX_THREADS)
{
SetRenderJobCount(frameIndex / 50 + 1);
}
}
virtual RpsRuntimeCommandBuffer AcquireNewCommandBuffer(uint32_t* pInsertAfter) override final
{
std::lock_guard<std::mutex> lock(m_cmdListsMutex);
uint32_t newIdx = uint32_t(m_activeCmdLists.size());
m_activeCmdLists.push_back({
AcquireCmdList(RPS_AFX_QUEUE_INDEX_GFX),
UINT32_MAX,
});
if (pInsertAfter)
{
if (*pInsertAfter != UINT32_MAX)
{
uint32_t next = m_activeCmdLists[*pInsertAfter].Next;
m_activeCmdLists[*pInsertAfter].Next = newIdx;
m_activeCmdLists.back().Next = next;
}
*pInsertAfter = newIdx;
}
return rpsD3D12CommandListToHandle(m_activeCmdLists.back().CmdList.cmdList.Get());
}
virtual void OnRender(uint32_t frameIndex) override
{
m_activeCmdLists.clear();
TestRpsMultithreading::OnRender(frameIndex, m_numPasses);
for (auto& cl : m_activeCmdLists)
{
CloseCmdList(cl.CmdList);
}
m_cmdListsToSubmit.clear();
m_cmdListsToSubmit.reserve(m_activeCmdLists.size());
for (uint32_t i = 0; i < m_activeCmdLists.size(); i = m_activeCmdLists[i].Next)
{
m_cmdListsToSubmit.push_back(m_activeCmdLists[i].CmdList.cmdList.Get());
}
REQUIRE(m_cmdListsToSubmit.size() == m_activeCmdLists.size());
if (m_cmdListsToSubmit.size() > 0)
{
m_presentQueue->ExecuteCommandLists(uint32_t(m_cmdListsToSubmit.size()), m_cmdListsToSubmit.data());
}
for (uint32_t i = 0; i < m_activeCmdLists.size(); i++)
{
RecycleCmdList(m_activeCmdLists[i].CmdList);
}
}
virtual void OnKeyUp(char key) override
{
if (key >= '1' && key <= '8')
SetRenderJobCount(key - '1');
}
void SetRenderJobCount(uint32_t count)
{
m_renderJobs = std::max(1u, count);
char buf[256];
sprintf_s(buf, "TestD3D12Multithreading - %d workers on %d threads", m_renderJobs, m_threadPool.GetNumThreads());
SetWindowText(m_hWnd, buf);
}
protected:
virtual bool IsSoftwareAdapter() const override final
{
return m_useWarpDevice;
}
virtual void DrawGeometryPass(const RpsCmdCallbackContext* pContext) override final
{
const uint32_t numThreads = std::max(1u, std::min(MAX_THREADS, m_renderJobs));
CmdRangeContext* pRangeContext = static_cast<CmdRangeContext*>(pContext->pUserRecordContext);
if (m_pipelineState == nullptr)
{
std::lock_guard<std::mutex> lock(m_cmdListsMutex);
if (m_pipelineState == nullptr)
{
RpsCmdRenderTargetInfo rtInfo;
RpsResult result = rpsCmdGetRenderTargetsInfo(pContext, &rtInfo);
THREAD_SAFE_REQUIRE(result == RPS_OK);
CreatePSO(c_Shader, &rtInfo, &m_pipelineState);
}
}
for (uint32_t i = 0; i < numThreads; i++)
{
auto hNewCmdBuf = AcquireNewCommandBuffer(&pRangeContext->LastCmdListIndex);
const RpsCmdCallbackContext* pLocalContext = {};
{
std::lock_guard<std::mutex> lock(m_cmdListsMutex);
THREAD_SAFE_REQUIRE(rpsCmdCloneContext(pContext, hNewCmdBuf, &pLocalContext) == RPS_OK);
}
RpsAfxThreadPool::WaitHandle waitHdl = m_threadPool.EnqueueJob(
[this, pLocalContext, i, numThreads, hNewCmdBuf, batchId = pRangeContext->BatchIndex]() {
uint32_t numTrianglesPerThread = uint32_t(m_triangleData.size() + numThreads - 1) / numThreads;
uint32_t beginIndex = numTrianglesPerThread * i;
uint32_t endIndex = std::min(uint32_t(m_triangleData.size()), beginIndex + numTrianglesPerThread);
auto pCmdList = rpsD3D12CommandListFromHandle(pLocalContext->hCommandBuffer);
if (hNewCmdBuf != pLocalContext->hCommandBuffer)
{
m_failCount++;
}
RpsCmdRenderPassBeginInfo rpBeginInfo = {};
if (i != 0)
rpBeginInfo.flags |= RPS_RUNTIME_RENDER_PASS_RESUMING;
if (i != (numThreads - 1))
rpBeginInfo.flags |= RPS_RUNTIME_RENDER_PASS_SUSPENDING;
RpsResult threadResult = rpsCmdBeginRenderPass(pLocalContext, &rpBeginInfo);
if (threadResult != RPS_OK)
m_failCount++;
const float aspectRatio = m_height / static_cast<float>(m_width);
pCmdList->SetGraphicsRootSignature(m_rootSignature.Get());
pCmdList->SetPipelineState(m_pipelineState.Get());
pCmdList->IASetPrimitiveTopology(D3D_PRIMITIVE_TOPOLOGY_TRIANGLELIST);
auto tid = batchId * numThreads + i;
const XMVECTOR threadColorTint = XMVectorSet(float((tid / 7) & 1), float((tid / 13) & 1), float((tid / 25) & 1), 1.0f);
for (uint32_t triangleIdx = beginIndex; triangleIdx < endIndex; triangleIdx++)
{
TriangleDataGPU cbData = {};
TriangleDataCPU* pTriangle = &m_triangleData[triangleIdx];
pTriangle->Offset.x = fmod(pTriangle->Offset.x + pTriangle->Speed + m_runwayLength * 0.5f, m_runwayLength) - m_runwayLength * 0.5f;
cbData.Pos = pTriangle->Offset;
cbData.AspectRatio = aspectRatio;
cbData.Scale = pTriangle->Scale;
XMStoreFloat3(&cbData.Color, XMVectorLerp(XMLoadFloat3(&pTriangle->Color), threadColorTint, 0.7f));
pCmdList->SetGraphicsRoot32BitConstants(0, sizeof(TriangleDataGPU) / sizeof(uint32_t), &cbData, 0);
pCmdList->DrawInstanced(3, 1, 0, 0);
}
threadResult = rpsCmdEndRenderPass(pLocalContext);
if (threadResult != RPS_OK)
m_failCount++;
m_executeCount++;
});
{
std::lock_guard<std::mutex> lock(m_cmdListsMutex);
m_waitHandles.emplace_back(std::move(waitHdl));
}
}
RpsRuntimeCommandBuffer newBuffer = AcquireNewCommandBuffer(&pRangeContext->LastCmdListIndex);
RpsResult result = rpsCmdSetCommandBuffer(pContext, newBuffer);
THREAD_SAFE_REQUIRE(result == RPS_OK);
}
private:
void LoadAssets(ID3D12GraphicsCommandList* pInitCmdList, std::vector<ComPtr<ID3D12Object>>& tempResources)
{
OnPostResize();
{
CD3DX12_ROOT_PARAMETER rootParameters[1] = {};
rootParameters[0].InitAsConstants(sizeof(TriangleDataGPU) / sizeof(uint32_t), 0, 0, D3D12_SHADER_VISIBILITY_VERTEX);
D3D12_ROOT_SIGNATURE_FLAGS rootSignatureFlags = D3D12_ROOT_SIGNATURE_FLAG_ALLOW_INPUT_ASSEMBLER_INPUT_LAYOUT;
CD3DX12_VERSIONED_ROOT_SIGNATURE_DESC rootSignatureDesc;
rootSignatureDesc.Init_1_0(_countof(rootParameters), rootParameters, 0, nullptr, rootSignatureFlags);
ComPtr<ID3DBlob> signature;
ComPtr<ID3DBlob> error;
ThrowIfFailed(D3DX12SerializeVersionedRootSignature(&rootSignatureDesc, D3D_ROOT_SIGNATURE_VERSION_1_0, &signature, &error));
ThrowIfFailed(m_device->CreateRootSignature(0, signature->GetBufferPointer(), signature->GetBufferSize(), IID_PPV_ARGS(&m_rootSignature)));
}
}
void CreatePSO(const char* shader, const RpsCmdRenderTargetInfo* pRtInfo, ID3D12PipelineState** ppState)
{
std::vector<uint8_t> vsCode, psCode;
DxcCompile(c_Shader, L"VSMain", L"vs_6_0", L"", nullptr, 0, vsCode);
DxcCompile(c_Shader, L"PSMain", L"ps_6_0", L"", nullptr, 0, psCode);
// Describe and create the graphics pipeline state object (PSO).
D3D12_GRAPHICS_PIPELINE_STATE_DESC psoDesc = {};
psoDesc.pRootSignature = m_rootSignature.Get();
psoDesc.VS = CD3DX12_SHADER_BYTECODE(vsCode.data(), vsCode.size());
psoDesc.PS = CD3DX12_SHADER_BYTECODE(psCode.data(), psCode.size());
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.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(&m_pipelineState)));
}
void UpdatePipeline(uint64_t frameIndex, uint64_t completedFrameIndex)
{
RpsRuntimeResource backBuffers[DXGI_MAX_SWAP_CHAIN_BUFFERS];
RpsResourceDesc backBufferDesc = {};
GetBackBuffers(backBufferDesc, backBuffers);
TestRpsMultithreading::UpdateRpsPipeline(frameIndex, completedFrameIndex, backBufferDesc, backBuffers);
}
private:
ComPtr<ID3D12RootSignature> m_rootSignature;
ComPtr<ID3D12PipelineState> m_pipelineState;
struct ActiveCommandListEx
{
ActiveCommandList CmdList;
uint32_t Next;
};
std::vector<ActiveCommandListEx> m_activeCmdLists;
std::vector<ID3D12CommandList*> m_cmdListsToSubmit;
};
TEST_CASE(TEST_APP_NAME)
{
#if _DEBUG
_CrtSetDbgFlag(_CRTDBG_ALLOC_MEM_DF | _CRTDBG_LEAK_CHECK_DF);
#endif
TestD3D12Multithreading renderer;
RpsTestRunWindowInfo runInfo = {};
runInfo.title = TEXT(TEST_APP_NAME);
runInfo.numFramesToRender = g_exitAfterFrame;
runInfo.width = 1280;
runInfo.height = 720;
runInfo.pRenderer = &renderer;
RpsTestRunWindowApp(&runInfo);
}