-
Notifications
You must be signed in to change notification settings - Fork 0
/
DirectXRaytracingHelper.h
299 lines (273 loc) · 12 KB
/
DirectXRaytracingHelper.h
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
//*********************************************************
//
// Copyright (c) Microsoft. All rights reserved.
// This code is licensed under the MIT License (MIT).
// THIS CODE IS PROVIDED *AS IS* WITHOUT WARRANTY OF
// ANY KIND, EITHER EXPRESS OR IMPLIED, INCLUDING ANY
// IMPLIED WARRANTIES OF FITNESS FOR A PARTICULAR
// PURPOSE, MERCHANTABILITY, OR NON-INFRINGEMENT.
//
//*********************************************************
#pragma once
#define SizeOfInUint32(obj) ((sizeof(obj) - 1) / sizeof(UINT32) + 1)
struct AccelerationStructureBuffers
{
ComPtr<ID3D12Resource> scratch;
ComPtr<ID3D12Resource> accelerationStructure;
ComPtr<ID3D12Resource> instanceDesc; // Used only for top-level AS
UINT64 ResultDataMaxSizeInBytes;
};
// Shader record = {{Shader ID}, {RootArguments}}
class ShaderRecord
{
public:
ShaderRecord(void* pShaderIdentifier, UINT shaderIdentifierSize) :
shaderIdentifier(pShaderIdentifier, shaderIdentifierSize)
{
}
ShaderRecord(void* pShaderIdentifier, UINT shaderIdentifierSize, void* pLocalRootArguments, UINT localRootArgumentsSize) :
shaderIdentifier(pShaderIdentifier, shaderIdentifierSize),
localRootArguments(pLocalRootArguments, localRootArgumentsSize)
{
}
void CopyTo(void* dest) const
{
uint8_t* byteDest = static_cast<uint8_t*>(dest);
memcpy(byteDest, shaderIdentifier.ptr, shaderIdentifier.size);
if (localRootArguments.ptr)
{
memcpy(byteDest + shaderIdentifier.size, localRootArguments.ptr, localRootArguments.size);
}
}
struct PointerWithSize {
void *ptr;
UINT size;
PointerWithSize() : ptr(nullptr), size(0) {}
PointerWithSize(void* _ptr, UINT _size) : ptr(_ptr), size(_size) {};
};
PointerWithSize shaderIdentifier;
PointerWithSize localRootArguments;
};
// Shader table = {{ ShaderRecord 1}, {ShaderRecord 2}, ...}
class ShaderTable : public GpuUploadBuffer
{
uint8_t* m_mappedShaderRecords;
UINT m_shaderRecordSize;
// Debug support
std::wstring m_name;
std::vector<ShaderRecord> m_shaderRecords;
ShaderTable() {}
public:
ShaderTable(ID3D12Device* device, UINT numShaderRecords, UINT shaderRecordSize, LPCWSTR resourceName = nullptr)
: m_name(resourceName)
{
m_shaderRecordSize = Align(shaderRecordSize, D3D12_RAYTRACING_SHADER_RECORD_BYTE_ALIGNMENT);
m_shaderRecords.reserve(numShaderRecords);
UINT bufferSize = numShaderRecords * m_shaderRecordSize;
Allocate(device, bufferSize, resourceName);
m_mappedShaderRecords = MapCpuWriteOnly();
}
void push_back(const ShaderRecord& shaderRecord)
{
ThrowIfFalse(m_shaderRecords.size() < m_shaderRecords.capacity());
m_shaderRecords.push_back(shaderRecord);
shaderRecord.CopyTo(m_mappedShaderRecords);
m_mappedShaderRecords += m_shaderRecordSize;
}
UINT GetShaderRecordSize() { return m_shaderRecordSize; }
// Pretty-print the shader records.
void DebugPrint(std::unordered_map<void*, std::wstring> shaderIdToStringMap)
{
std::wstringstream wstr;
wstr << L"|--------------------------------------------------------------------\n";
wstr << L"|Shader table - " << m_name.c_str() << L": "
<< m_shaderRecordSize << L" | "
<< m_shaderRecords.size() * m_shaderRecordSize << L" bytes\n";
for (UINT i = 0; i < m_shaderRecords.size(); i++)
{
wstr << L"| [" << i << L"]: ";
wstr << shaderIdToStringMap[m_shaderRecords[i].shaderIdentifier.ptr] << L", ";
wstr << m_shaderRecords[i].shaderIdentifier.size << L" + " << m_shaderRecords[i].localRootArguments.size << L" bytes \n";
}
wstr << L"|--------------------------------------------------------------------\n";
wstr << L"\n";
OutputDebugStringW(wstr.str().c_str());
}
};
inline void AllocateUAVBuffer(ID3D12Device* pDevice, UINT64 bufferSize, ID3D12Resource **ppResource, D3D12_RESOURCE_STATES initialResourceState = D3D12_RESOURCE_STATE_COMMON, const wchar_t* resourceName = nullptr)
{
auto uploadHeapProperties = CD3DX12_HEAP_PROPERTIES(D3D12_HEAP_TYPE_DEFAULT);
auto bufferDesc = CD3DX12_RESOURCE_DESC::Buffer(bufferSize, D3D12_RESOURCE_FLAG_ALLOW_UNORDERED_ACCESS);
ThrowIfFailed(pDevice->CreateCommittedResource(
&uploadHeapProperties,
D3D12_HEAP_FLAG_NONE,
&bufferDesc,
initialResourceState,
nullptr,
IID_PPV_ARGS(ppResource)));
if (resourceName)
{
(*ppResource)->SetName(resourceName);
}
}
template<class T, size_t N>
void DefineExports(T* obj, LPCWSTR(&Exports)[N])
{
for (UINT i = 0; i < N; i++)
{
obj->DefineExport(Exports[i]);
}
}
template<class T, size_t N, size_t M>
void DefineExports(T* obj, LPCWSTR(&Exports)[N][M])
{
for (UINT i = 0; i < N; i++)
for (UINT j = 0; j < M; j++)
{
obj->DefineExport(Exports[i][j]);
}
}
inline void AllocateUploadBuffer(ID3D12Device* pDevice, void *pData, UINT64 datasize, ID3D12Resource **ppResource, const wchar_t* resourceName = nullptr)
{
auto uploadHeapProperties = CD3DX12_HEAP_PROPERTIES(D3D12_HEAP_TYPE_UPLOAD);
auto bufferDesc = CD3DX12_RESOURCE_DESC::Buffer(datasize);
ThrowIfFailed(pDevice->CreateCommittedResource(
&uploadHeapProperties,
D3D12_HEAP_FLAG_NONE,
&bufferDesc,
D3D12_RESOURCE_STATE_GENERIC_READ,
nullptr,
IID_PPV_ARGS(ppResource)));
if (resourceName)
{
(*ppResource)->SetName(resourceName);
}
void *pMappedData;
(*ppResource)->Map(0, nullptr, &pMappedData);
memcpy(pMappedData, pData, datasize);
(*ppResource)->Unmap(0, nullptr);
}
// Pretty-print a state object tree.
inline void PrintStateObjectDesc(const D3D12_STATE_OBJECT_DESC* desc)
{
std::wstringstream wstr;
wstr << L"\n";
wstr << L"--------------------------------------------------------------------\n";
wstr << L"| D3D12 State Object 0x" << static_cast<const void*>(desc) << L": ";
if (desc->Type == D3D12_STATE_OBJECT_TYPE_COLLECTION) wstr << L"Collection\n";
if (desc->Type == D3D12_STATE_OBJECT_TYPE_RAYTRACING_PIPELINE) wstr << L"Raytracing Pipeline\n";
auto ExportTree = [](UINT depth, UINT numExports, const D3D12_EXPORT_DESC* exports)
{
std::wostringstream woss;
for (UINT i = 0; i < numExports; i++)
{
woss << L"|";
if (depth > 0)
{
for (UINT j = 0; j < 2 * depth - 1; j++) woss << L" ";
}
woss << L" [" << i << L"]: ";
if (exports[i].ExportToRename) woss << exports[i].ExportToRename << L" --> ";
woss << exports[i].Name << L"\n";
}
return woss.str();
};
for (UINT i = 0; i < desc->NumSubobjects; i++)
{
wstr << L"| [" << i << L"]: ";
switch (desc->pSubobjects[i].Type)
{
case D3D12_STATE_SUBOBJECT_TYPE_GLOBAL_ROOT_SIGNATURE:
wstr << L"Global Root Signature 0x" << desc->pSubobjects[i].pDesc << L"\n";
break;
case D3D12_STATE_SUBOBJECT_TYPE_LOCAL_ROOT_SIGNATURE:
wstr << L"Local Root Signature 0x" << desc->pSubobjects[i].pDesc << L"\n";
break;
case D3D12_STATE_SUBOBJECT_TYPE_NODE_MASK:
wstr << L"Node Mask: 0x" << std::hex << std::setfill(L'0') << std::setw(8) << *static_cast<const UINT*>(desc->pSubobjects[i].pDesc) << std::setw(0) << std::dec << L"\n";
break;
case D3D12_STATE_SUBOBJECT_TYPE_DXIL_LIBRARY:
{
wstr << L"DXIL Library 0x";
auto lib = static_cast<const D3D12_DXIL_LIBRARY_DESC*>(desc->pSubobjects[i].pDesc);
wstr << lib->DXILLibrary.pShaderBytecode << L", " << lib->DXILLibrary.BytecodeLength << L" bytes\n";
wstr << ExportTree(1, lib->NumExports, lib->pExports);
break;
}
case D3D12_STATE_SUBOBJECT_TYPE_EXISTING_COLLECTION:
{
wstr << L"Existing Library 0x";
auto collection = static_cast<const D3D12_EXISTING_COLLECTION_DESC*>(desc->pSubobjects[i].pDesc);
wstr << collection->pExistingCollection << L"\n";
wstr << ExportTree(1, collection->NumExports, collection->pExports);
break;
}
case D3D12_STATE_SUBOBJECT_TYPE_SUBOBJECT_TO_EXPORTS_ASSOCIATION:
{
wstr << L"Subobject to Exports Association (Subobject [";
auto association = static_cast<const D3D12_SUBOBJECT_TO_EXPORTS_ASSOCIATION*>(desc->pSubobjects[i].pDesc);
UINT index = static_cast<UINT>(association->pSubobjectToAssociate - desc->pSubobjects);
wstr << index << L"])\n";
for (UINT j = 0; j < association->NumExports; j++)
{
wstr << L"| [" << j << L"]: " << association->pExports[j] << L"\n";
}
break;
}
case D3D12_STATE_SUBOBJECT_TYPE_DXIL_SUBOBJECT_TO_EXPORTS_ASSOCIATION:
{
wstr << L"DXIL Subobjects to Exports Association (";
auto association = static_cast<const D3D12_DXIL_SUBOBJECT_TO_EXPORTS_ASSOCIATION*>(desc->pSubobjects[i].pDesc);
wstr << association->SubobjectToAssociate << L")\n";
for (UINT j = 0; j < association->NumExports; j++)
{
wstr << L"| [" << j << L"]: " << association->pExports[j] << L"\n";
}
break;
}
case D3D12_STATE_SUBOBJECT_TYPE_RAYTRACING_SHADER_CONFIG:
{
wstr << L"Raytracing Shader Config\n";
auto config = static_cast<const D3D12_RAYTRACING_SHADER_CONFIG*>(desc->pSubobjects[i].pDesc);
wstr << L"| [0]: Max Payload Size: " << config->MaxPayloadSizeInBytes << L" bytes\n";
wstr << L"| [1]: Max Attribute Size: " << config->MaxAttributeSizeInBytes << L" bytes\n";
break;
}
case D3D12_STATE_SUBOBJECT_TYPE_RAYTRACING_PIPELINE_CONFIG:
{
wstr << L"Raytracing Pipeline Config\n";
auto config = static_cast<const D3D12_RAYTRACING_PIPELINE_CONFIG*>(desc->pSubobjects[i].pDesc);
wstr << L"| [0]: Max Recursion Depth: " << config->MaxTraceRecursionDepth << L"\n";
break;
}
case D3D12_STATE_SUBOBJECT_TYPE_HIT_GROUP:
{
wstr << L"Hit Group (";
auto hitGroup = static_cast<const D3D12_HIT_GROUP_DESC*>(desc->pSubobjects[i].pDesc);
wstr << (hitGroup->HitGroupExport ? hitGroup->HitGroupExport : L"[none]") << L")\n";
wstr << L"| [0]: Any Hit Import: " << (hitGroup->AnyHitShaderImport ? hitGroup->AnyHitShaderImport : L"[none]") << L"\n";
wstr << L"| [1]: Closest Hit Import: " << (hitGroup->ClosestHitShaderImport ? hitGroup->ClosestHitShaderImport : L"[none]") << L"\n";
wstr << L"| [2]: Intersection Import: " << (hitGroup->IntersectionShaderImport ? hitGroup->IntersectionShaderImport : L"[none]") << L"\n";
break;
}
}
wstr << L"|--------------------------------------------------------------------\n";
}
wstr << L"\n";
OutputDebugStringW(wstr.str().c_str());
}
// Returns bool whether the device supports DirectX Raytracing tier.
inline bool IsDirectXRaytracingSupported(IDXGIAdapter1* adapter)
{
ComPtr<ID3D12Device> testDevice;
D3D12_FEATURE_DATA_D3D12_OPTIONS5 featureSupportData = {};
return SUCCEEDED(D3D12CreateDevice(adapter, D3D_FEATURE_LEVEL_11_0, IID_PPV_ARGS(&testDevice)))
&& SUCCEEDED(testDevice->CheckFeatureSupport(D3D12_FEATURE_D3D12_OPTIONS5, &featureSupportData, sizeof(featureSupportData)))
&& featureSupportData.RaytracingTier != D3D12_RAYTRACING_TIER_NOT_SUPPORTED;
}
inline float NumMRaysPerSecond(UINT width, UINT height, float dispatchRaysTimeMs)
{
float resolutionMRays = static_cast<float>(width * height);
float raytracingTimeInSeconds = 0.001f * dispatchRaysTimeMs;
return resolutionMRays / (raytracingTimeInSeconds * static_cast<float>(1e6));
}