This repository has been archived by the owner on Dec 2, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 225
/
RayTracingHelper.h
135 lines (108 loc) · 4.5 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
// Copyright (c) Microsoft Corporation. All rights reserved.
// Licensed under the MIT License.
#pragma once
#include <unordered_map>
#include "DirectXHelpers.h"
// 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 = DirectX::AlignUp(shaderRecordSize, D3D12_RAYTRACING_SHADER_RECORD_BYTE_ALIGNMENT);
m_shaderRecords.reserve(numShaderRecords);
unsigned int bufferSize = numShaderRecords * m_shaderRecordSize;
auto uploadHeapProperties = CD3DX12_HEAP_PROPERTIES(D3D12_HEAP_TYPE_UPLOAD);
auto bufferDesc = CD3DX12_RESOURCE_DESC::Buffer(bufferSize);
DX::ThrowIfFailed(device->CreateCommittedResource(
&uploadHeapProperties,
D3D12_HEAP_FLAG_NONE,
&bufferDesc,
D3D12_RESOURCE_STATE_GENERIC_READ,
nullptr,
IID_PPV_ARGS(m_bufferResource.GetAddressOf())));
if (resourceName)
{
m_bufferResource->SetName(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)
{
if (closed)
throw std::exception("Cannot add to a closed ShaderTable.");
assert(m_shaderRecords.size() < m_shaderRecords.capacity());
m_shaderRecords.push_back(shaderRecord);
shaderRecord.CopyTo(m_mappedShaderRecords);
m_mappedShaderRecords += m_shaderRecordSize;
}
void Close()
{
if (closed)
throw std::exception("Cannot close an already closed ShaderTable.");
closed = closed;
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());
}
};