forked from GPUOpen-LibrariesAndSDKs/RenderPipelineShaders
-
Notifications
You must be signed in to change notification settings - Fork 0
/
test_temporal_d3d12.cpp
309 lines (252 loc) · 11.3 KB
/
test_temporal_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
// Copyright (c) 2023 Advanced Micro Devices, Inc. All rights reserved.
//
// This file is part of the AMD Render Pipeline Shaders SDK which is
// released under the AMD INTERNAL EVALUATION LICENSE.
//
// See file LICENSE.txt for full license details.
#define CATCH_CONFIG_MAIN
#include <catch2/catch.hpp>
#include <stdarg.h>
#include "utils/rps_test_d3d12_renderer.hpp"
#include "utils/rps_test_common.h"
#include "utils/rps_test_win32.hpp"
RPS_DECLARE_RPSL_ENTRY(test_temporal, main);
static const char c_Shader[] = R"(
struct V2P
{
float4 Pos : SV_Position;
float4 Color : COLOR0;
};
cbuffer cb : register(b0)
{
float AspectRatio;
};
#define PI (3.14159f)
V2P VSMain(uint vId : SV_VertexID)
{
float2 pos[3] =
{
{ -0.5f, -0.5f * tan(PI / 6), },
{ 0.0f, 0.5f / cos(PI / 6), },
{ 0.5f, -0.5f * tan(PI / 6), },
};
V2P vsOut;
vsOut.Pos = float4(pos[min(vId, 2)], 0, 1);
vsOut.Pos.x *= AspectRatio;
vsOut.Color = float4(vId == 0 ? 1 : 0, vId == 1 ? 1 : 0, vId == 2 ? 1 : 0, 1);
return vsOut;
}
float4 PSMain(V2P psIn) : SV_Target0
{
return psIn.Color;
}
)";
#define TEST_APP_NAME_RAW "TestTemporal"
class TestD3D12Temporal : public RpsTestD3D12Renderer
{
protected:
virtual void OnInit(ID3D12GraphicsCommandList* pInitCmdList,
std::vector<ComPtr<ID3D12Object>>& tempResources) override
{
LoadAssets();
m_rpsDevice = rpsTestUtilCreateDevice(
[this](auto pCreateInfo, auto phDevice) { return CreateRpsRuntimeDevice(*pCreateInfo, *phDevice); });
LoadRpsPipeline();
}
virtual void OnCleanUp() override
{
rpsRenderGraphDestroy(m_rpsRenderGraph);
rpsTestUtilDestroyDevice(m_rpsDevice);
m_pipelineState = nullptr;
m_rootSignature = nullptr;
}
virtual void OnUpdate(uint32_t frameIndex) override
{
UpdateRpsPipeline(frameIndex);
}
virtual void OnRender(uint32_t frameIndex) override
{
RpsRenderGraphBatchLayout batchLayout = {};
RpsResult result = rpsRenderGraphGetBatchLayout(m_rpsRenderGraph, &batchLayout);
REQUIRE(result == RPS_OK);
m_fenceSignalInfos.resize(batchLayout.numFenceSignals);
for (uint32_t iBatch = 0; iBatch < batchLayout.numCmdBatches; iBatch++)
{
auto& batch = batchLayout.pCmdBatches[iBatch];
ActiveCommandList cmdList = AcquireCmdList(RPS_AFX_QUEUE_INDEX_GFX);
RpsRenderGraphRecordCommandInfo recordInfo = {};
recordInfo.pUserContext = this;
recordInfo.cmdBeginIndex = batch.cmdBegin;
recordInfo.numCmds = batch.numCmds;
recordInfo.hCmdBuffer = rpsD3D12CommandListToHandle(cmdList.cmdList.Get());
for (uint32_t iWaitIdx = batch.waitFencesBegin; iWaitIdx < batch.waitFencesBegin + batch.numWaitFences;
++iWaitIdx)
{
const auto& signalInfo = m_fenceSignalInfos[batchLayout.pWaitFenceIndices[iWaitIdx]];
HRESULT hr = m_queues[batch.queueIndex]->Wait(m_fences[signalInfo.queueIndex].Get(), signalInfo.value);
REQUIRE(SUCCEEDED(hr));
}
result = rpsRenderGraphRecordCommands(m_rpsRenderGraph, &recordInfo);
REQUIRE(result == RPS_OK);
CloseCmdList(cmdList);
ID3D12CommandList* pCmdLists[] = {cmdList.cmdList.Get()};
m_presentQueue->ExecuteCommandLists(1, pCmdLists);
RecycleCmdList(cmdList);
if (batch.signalFenceIndex != RPS_INDEX_NONE_U32)
{
m_fenceValue++;
auto& signalInfo = m_fenceSignalInfos[batch.signalFenceIndex];
signalInfo.queueIndex = batch.queueIndex;
signalInfo.value = m_fenceValue;
HRESULT hr =
m_queues[batch.queueIndex]->Signal(m_fences[signalInfo.queueIndex].Get(), signalInfo.value);
REQUIRE(SUCCEEDED(hr));
}
}
}
private:
void DrawTriangle(ID3D12GraphicsCommandList* pCmdList)
{
FLOAT aspectRatio = m_height / static_cast<float>(m_width);
pCmdList->SetGraphicsRootSignature(m_rootSignature.Get());
pCmdList->SetGraphicsRoot32BitConstant(0, *(const UINT*)&aspectRatio, 0);
pCmdList->SetPipelineState(m_pipelineState.Get());
pCmdList->IASetPrimitiveTopology(D3D10_PRIMITIVE_TOPOLOGY_TRIANGLELIST);
pCmdList->DrawInstanced(3, 1, 0, 0);
}
static void DrawTriangleCb(const RpsCmdCallbackContext* pContext)
{
auto pThis = static_cast<TestD3D12Temporal*>(pContext->pCmdCallbackContext);
pThis->DrawTriangle(rpsD3D12CommandListFromHandle(pContext->hCommandBuffer));
}
private:
void LoadAssets()
{
// Create a root signature consisting of a descriptor table with a single CBV.
{
CD3DX12_ROOT_PARAMETER rootParameters[1];
rootParameters[0].InitAsConstants(1, 0);
D3D12_ROOT_SIGNATURE_FLAGS rootSignatureFlags = D3D12_ROOT_SIGNATURE_FLAG_NONE;
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)));
}
// Create the pipeline state, which includes compiling and loading shaders.
{
ComPtr<ID3DBlob> vertexShader, pixelShader, err;
#if defined(_DEBUG)
// Enable better shader debugging with the graphics debugging tools.
UINT compileFlags = D3DCOMPILE_DEBUG | D3DCOMPILE_SKIP_OPTIMIZATION;
#else
UINT compileFlags = 0;
#endif
ThrowIfFailedEx(D3DCompile(c_Shader,
sizeof(c_Shader),
nullptr,
nullptr,
nullptr,
"VSMain",
"vs_5_0",
compileFlags,
0,
&vertexShader,
&err),
err);
ThrowIfFailedEx(D3DCompile(c_Shader,
sizeof(c_Shader),
nullptr,
nullptr,
nullptr,
"PSMain",
"ps_5_0",
compileFlags,
0,
&pixelShader,
&err),
err);
// Describe and create the graphics pipeline state object (PSO).
D3D12_GRAPHICS_PIPELINE_STATE_DESC psoDesc = {};
psoDesc.InputLayout = {nullptr, 0};
psoDesc.pRootSignature = m_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;
ThrowIfFailed(m_device->CreateGraphicsPipelineState(&psoDesc, IID_PPV_ARGS(&m_pipelineState)));
}
}
void LoadRpsPipeline()
{
RpsRenderGraphCreateInfo renderGraphInfo = {};
renderGraphInfo.mainEntryCreateInfo.hRpslEntryPoint = rpsTestLoadRpslEntry(test_temporal, main);
REQUIRE_RPS_OK(rpsRenderGraphCreate(m_rpsDevice, &renderGraphInfo, &m_rpsRenderGraph));
rpsProgramBindNode(rpsRenderGraphGetMainEntry(m_rpsRenderGraph), "Triangle", &DrawTriangleCb, this);
}
void UpdateRpsPipeline(uint64_t frameIndex)
{
if (m_rpsRenderGraph != RPS_NULL_HANDLE)
{
RpsRuntimeResource backBufferResources[DXGI_MAX_SWAP_CHAIN_BUFFERS] = {};
for (uint32_t i = 0; i < m_backBuffers.size(); i++)
{
backBufferResources[i] = {m_backBuffers[i].Get()};
}
const RpsRuntimeResource* argResources[] = {backBufferResources};
RpsResourceDesc backBufferDesc = {};
backBufferDesc.type = RPS_RESOURCE_TYPE_IMAGE_2D;
backBufferDesc.temporalLayers = uint32_t(m_backBuffers.size());
backBufferDesc.image.arrayLayers = 1;
backBufferDesc.image.mipLevels = 1;
backBufferDesc.image.format = RPS_FORMAT_R8G8B8A8_UNORM;
backBufferDesc.image.width = m_width;
backBufferDesc.image.height = m_height;
backBufferDesc.image.sampleCount = 1;
RpsConstant argData[] = {&backBufferDesc, &frameIndex};
const uint64_t completedFrameIndex = CalcGuaranteedCompletedFrameIndexForRps();
RpsRenderGraphUpdateInfo updateInfo = {};
updateInfo.frameIndex = frameIndex;
updateInfo.gpuCompletedFrameIndex = completedFrameIndex;
updateInfo.numArgs = 2;
updateInfo.ppArgs = argData;
updateInfo.ppArgResources = argResources;
updateInfo.diagnosticFlags = RPS_DIAGNOSTIC_ENABLE_RUNTIME_DEBUG_NAMES;
if (frameIndex < m_backBufferCount)
{
updateInfo.diagnosticFlags = RPS_DIAGNOSTIC_ENABLE_ALL;
}
REQUIRE_RPS_OK(rpsRenderGraphUpdate(m_rpsRenderGraph, &updateInfo));
}
}
private:
ComPtr<ID3D12RootSignature> m_rootSignature;
ComPtr<ID3D12PipelineState> m_pipelineState;
RpsDevice m_rpsDevice = {};
RpsRenderGraph m_rpsRenderGraph = {};
};
TEST_CASE(TEST_APP_NAME)
{
_CrtSetDbgFlag(_CRTDBG_ALLOC_MEM_DF | _CRTDBG_LEAK_CHECK_DF);
#if defined(BREAK_AT_ALLOC_ID)
_CrtSetBreakAlloc(BREAK_AT_ALLOC_ID);
#endif
TestD3D12Temporal renderer;
RpsTestRunWindowInfo runInfo = {};
runInfo.title = TEXT(TEST_APP_NAME);
runInfo.numFramesToRender = g_exitAfterFrame;
runInfo.width = 1280;
runInfo.height = 720;
runInfo.pRenderer = &renderer;
RpsTestRunWindowApp(&runInfo);
}