forked from microsoft/Xbox-ATG-Samples
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathRayTracingHelper.h
162 lines (134 loc) · 6.25 KB
/
RayTracingHelper.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
//*********************************************************
//
// 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)
// Align a constant buffer.
inline constexpr unsigned int CalculateRaytracingRecordByteSize(const unsigned int size)
{
return Align(size, D3D12_RAYTRACING_SHADER_RECORD_BYTE_ALIGNMENT);
}
// Enable experimental features required for compute-based raytracing fallback.
// This will set active D3D12 devices to DEVICE_REMOVED state.
// Returns bool whether the call succeeded and the device supports the feature.
inline bool EnableComputeRaytracingFallback(IDXGIAdapter1* adapter)
{
Microsoft::WRL::ComPtr<ID3D12Device> testDevice;
UUID experimentalFeatures[] = { D3D12ExperimentalShaderModels };
return SUCCEEDED(D3D12EnableExperimentalFeatures(1, experimentalFeatures, nullptr, nullptr))
&& SUCCEEDED(D3D12CreateDevice(adapter, D3D_FEATURE_LEVEL_11_0, IID_PPV_ARGS(&testDevice)));
}
inline bool IsDirectXRaytracingSupported(IDXGIAdapter1* adapter)
{
Microsoft::WRL::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 void SerializeAndCreateRaytracingRootSignature(
ID3D12RaytracingFallbackDevice* fallbackDevice,
D3D12_ROOT_SIGNATURE_DESC& desc,
Microsoft::WRL::ComPtr<ID3D12RootSignature>* rootSig)
{
Microsoft::WRL::ComPtr<ID3DBlob> blob;
Microsoft::WRL::ComPtr<ID3DBlob> error;
DX::ThrowIfFailed(fallbackDevice->D3D12SerializeRootSignature(&desc, D3D_ROOT_SIGNATURE_VERSION_1, &blob, &error), error ? static_cast<wchar_t*>(error->GetBufferPointer()) : nullptr);
DX::ThrowIfFailed(fallbackDevice->CreateRootSignature(1, blob->GetBufferPointer(), blob->GetBufferSize(), IID_PPV_ARGS(&(*rootSig))));
}
// Shader record = {{Shader ID}, {RootArguments}}
class ShaderRecord
{
public:
ShaderRecord(void* pShaderIdentifier, unsigned int shaderIdentifierSize) :
shaderIdentifier(pShaderIdentifier, shaderIdentifierSize)
{
}
ShaderRecord(void* pShaderIdentifier, unsigned int shaderIdentifierSize, void* pLocalRootArguments, unsigned int 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;
unsigned int size;
PointerWithSize() : ptr(nullptr), size(0) {}
PointerWithSize(void* _ptr, unsigned int _size) : ptr(_ptr), size(_size) {};
};
PointerWithSize shaderIdentifier;
PointerWithSize localRootArguments;
};
// Shader table = {{ ShaderRecord 1}, {ShaderRecord 2}, ...}
class ShaderTable
{
Microsoft::WRL::ComPtr<ID3D12Resource> m_bufferResource;
uint8_t* m_mappedShaderRecords;
unsigned int m_shaderRecordSize;
bool closed;
// Debug support
std::wstring m_name;
std::vector<ShaderRecord> m_shaderRecords;
ShaderTable() : closed(FALSE) {}
public:
ShaderTable(ID3D12Device* device, unsigned int numShaderRecords, unsigned int shaderRecordSize, LPCWSTR resourceName = nullptr)
: m_name(resourceName), closed(FALSE)
{
m_shaderRecordSize = CalculateRaytracingRecordByteSize(shaderRecordSize);
m_shaderRecords.reserve(numShaderRecords);
unsigned int bufferSize = numShaderRecords * m_shaderRecordSize;
AllocateUploadBuffer(device, nullptr, bufferSize, &m_bufferResource, resourceName);
// Map the data.
CD3DX12_RANGE readRange(0, 0); // We do not intend to read from this resource on the CPU.
DX::ThrowIfFailed(m_bufferResource->Map(0, &readRange, reinterpret_cast<void**>(&m_mappedShaderRecords)));
}
void Add(const ShaderRecord& shaderRecord)
{
DX::ThrowIfFalse(!closed, L"Cannot add to a closed ShaderTable.");
DX::ThrowIfFalse(m_shaderRecords.size() < m_shaderRecords.capacity());
m_shaderRecords.push_back(shaderRecord);
shaderRecord.CopyTo(m_mappedShaderRecords);
m_mappedShaderRecords += m_shaderRecordSize;
}
void Close()
{
DX::ThrowIfFalse(!closed, L"Cannot close an already closed ShaderTable.");
closed = TRUE;
m_bufferResource->Unmap(0, nullptr);
}
Microsoft::WRL::ComPtr<ID3D12Resource> GetResource(){ return m_bufferResource; };
unsigned int 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 (unsigned int 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());
}
};