-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathContext_ex.inl
188 lines (162 loc) · 7.42 KB
/
Context_ex.inl
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
namespace VkInline
{
BaseLevelAS::BaseLevelAS(SVBuffer* indBuf, SVBuffer* posBuf)
{
VkAccelerationStructureGeometryKHR acceleration_geometry = {};
acceleration_geometry.sType = VK_STRUCTURE_TYPE_ACCELERATION_STRUCTURE_GEOMETRY_KHR;
acceleration_geometry.flags = VK_GEOMETRY_OPAQUE_BIT_KHR;
acceleration_geometry.geometryType = VK_GEOMETRY_TYPE_TRIANGLES_KHR;
acceleration_geometry.geometry.triangles.sType = VK_STRUCTURE_TYPE_ACCELERATION_STRUCTURE_GEOMETRY_TRIANGLES_DATA_KHR;
acceleration_geometry.geometry.triangles.vertexData.deviceAddress = posBuf->internal()->get_device_address();
acceleration_geometry.geometry.triangles.maxVertex = (unsigned)posBuf->size();
acceleration_geometry.geometry.triangles.vertexStride = posBuf->elem_size();
acceleration_geometry.geometry.triangles.indexData.deviceAddress = indBuf == nullptr ? 0 : indBuf->internal()->get_device_address();
if (posBuf->name_elem_type() == "vec2")
acceleration_geometry.geometry.triangles.vertexFormat = VK_FORMAT_R32G32_SFLOAT;
else if (posBuf->name_elem_type() == "vec3")
acceleration_geometry.geometry.triangles.vertexFormat = VK_FORMAT_R32G32B32_SFLOAT;
acceleration_geometry.geometry.triangles.indexType = VK_INDEX_TYPE_NONE_KHR;
if (indBuf != nullptr)
acceleration_geometry.geometry.triangles.indexType = indBuf->elem_size() > 2 ? VK_INDEX_TYPE_UINT32 : VK_INDEX_TYPE_UINT16;
VkAccelerationStructureBuildGeometryInfoKHR geoBuildInfo{};
geoBuildInfo.sType = VK_STRUCTURE_TYPE_ACCELERATION_STRUCTURE_BUILD_GEOMETRY_INFO_KHR;
geoBuildInfo.type = VK_ACCELERATION_STRUCTURE_TYPE_BOTTOM_LEVEL_KHR;
geoBuildInfo.flags = VK_BUILD_ACCELERATION_STRUCTURE_PREFER_FAST_TRACE_BIT_KHR;
geoBuildInfo.geometryCount = 1;
geoBuildInfo.pGeometries = &acceleration_geometry;
VkAccelerationStructureBuildRangeInfoKHR range{};
range.primitiveCount = (unsigned)(indBuf == nullptr ? posBuf->size() / 3 : indBuf->size() / 3);
range.primitiveOffset = 0;
range.firstVertex = 0;
range.transformOffset = 0;
const VkAccelerationStructureBuildRangeInfoKHR* ranges = ⦥
m_blas = new Internal::BaseLevelAS(geoBuildInfo, &acceleration_geometry, &ranges);
}
BaseLevelAS::BaseLevelAS(SVBuffer* aabbBuf)
{
VkAccelerationStructureGeometryKHR acceleration_geometry = {};
acceleration_geometry.sType = VK_STRUCTURE_TYPE_ACCELERATION_STRUCTURE_GEOMETRY_KHR;
acceleration_geometry.flags = VK_GEOMETRY_OPAQUE_BIT_KHR;
acceleration_geometry.geometryType = VK_GEOMETRY_TYPE_AABBS_KHR;
acceleration_geometry.geometry.aabbs.sType = VK_STRUCTURE_TYPE_ACCELERATION_STRUCTURE_GEOMETRY_AABBS_DATA_KHR;
acceleration_geometry.geometry.aabbs.data.deviceAddress = aabbBuf->internal()->get_device_address();
acceleration_geometry.geometry.aabbs.stride = 0;
VkAccelerationStructureBuildGeometryInfoKHR geoBuildInfo{};
geoBuildInfo.sType = VK_STRUCTURE_TYPE_ACCELERATION_STRUCTURE_BUILD_GEOMETRY_INFO_KHR;
geoBuildInfo.type = VK_ACCELERATION_STRUCTURE_TYPE_BOTTOM_LEVEL_KHR;
geoBuildInfo.flags = VK_BUILD_ACCELERATION_STRUCTURE_PREFER_FAST_TRACE_BIT_KHR;
geoBuildInfo.geometryCount = 1;
geoBuildInfo.pGeometries = &acceleration_geometry;
VkAccelerationStructureBuildRangeInfoKHR range{};
range.primitiveCount = 1;
range.primitiveOffset = 0;
range.firstVertex = 0;
range.transformOffset = 0;
const VkAccelerationStructureBuildRangeInfoKHR* ranges = ⦥
m_blas = new Internal::BaseLevelAS(geoBuildInfo, &acceleration_geometry, &ranges);
}
BaseLevelAS::~BaseLevelAS()
{
delete m_blas;
}
Mat4::Mat4(const float* data)
{
memcpy(m_data, data, sizeof(float) * 16);
}
TopLevelAS::TopLevelAS(const std::vector<std::vector<BLAS_EX>>& blases)
{
size_t num_hitgroups = blases.size();
std::vector<size_t> num_instances(num_hitgroups);
std::vector<std::vector<VkAccelerationStructureKHR>> vblases(num_hitgroups);
std::vector<const VkAccelerationStructureKHR*> pblases(num_hitgroups);
std::vector<std::vector<const float*>> vtrans(num_hitgroups);
std::vector<const float**> ptrans(num_hitgroups);
for (size_t i = 0; i < num_hitgroups; i++)
{
size_t num_blases = blases[i].size();
num_instances[i] = num_blases;
vblases[i].resize(num_blases);
vtrans[i].resize(num_blases);
for (size_t j = 0; j < num_blases; j++)
{
vblases[i][j] = blases[i][j].blas->internal()->structure();
vtrans[i][j] = blases[i][j].trans->data();
}
pblases[i] = vblases[i].data();
ptrans[i] = vtrans[i].data();
}
m_tlas = new Internal::TopLevelAS(num_hitgroups, num_instances.data(), pblases.data(), ptrans.data());
}
TopLevelAS::~TopLevelAS()
{
delete m_tlas;
}
BodyHitShaders::BodyHitShaders(const char* body_closest_hit, const char* body_intersection)
{
m_body_closest_hit = body_closest_hit;
if (body_intersection!=nullptr)
m_body_intersection = body_intersection;
}
const char* BodyHitShaders::body_closest_hit() const
{
return m_body_closest_hit.c_str();
}
const char* BodyHitShaders::body_intersection() const
{
if (m_body_intersection.empty()) return nullptr;
return m_body_intersection.c_str();
}
RayTracer::RayTracer(const std::vector<const char*>& param_names, const char* body_raygen, const std::vector<const char*>& body_miss, const std::vector<const BodyHitShaders*>& body_hit, unsigned maxRecursionDepth, bool type_locked)
: m_param_names(param_names.size()), m_body_raygen(body_raygen), m_body_miss(body_miss.size()), m_body_hit(body_hit), m_maxRecursionDepth(maxRecursionDepth), m_type_locked(type_locked)
{
for (size_t i = 0; i < param_names.size(); i++)
m_param_names[i] = param_names[i];
for (size_t i = 0; i < body_miss.size(); i++)
m_body_miss[i] = body_miss[i];
m_kid = (unsigned)(-1);
}
bool RayTracer::launch(dim_type glbDim, const ShaderViewable** args,
const std::vector<TopLevelAS*>& arr_tlas, const std::vector<Texture2D*>& tex2ds, const std::vector<Texture3D*>& tex3ds, const std::vector<Cubemap*>& cubemaps, size_t times_submission)
{
Context& ctx = Context::get_context();
if (!m_type_locked)
{
std::vector<CapturedShaderViewable> arg_map(m_param_names.size());
for (size_t i = 0; i < m_param_names.size(); i++)
{
arg_map[i].obj_name = m_param_names[i].c_str();
arg_map[i].obj = args[i];
}
std::vector<const char*> p_body_miss(m_body_miss.size());
for (size_t i = 0; i < m_body_miss.size(); i++)
p_body_miss[i] = m_body_miss[i].c_str();
return ctx.launch_raytrace(glbDim, arg_map, m_maxRecursionDepth,
arr_tlas, tex2ds, tex3ds, cubemaps, m_body_raygen.c_str(), p_body_miss, m_body_hit, times_submission);
}
else
{
std::unique_lock<std::mutex> locker(m_mu_type_lock);
if (m_kid == (unsigned)(-1))
{
std::vector<CapturedShaderViewable> arg_map(m_param_names.size());
for (size_t i = 0; i < m_param_names.size(); i++)
{
arg_map[i].obj_name = m_param_names[i].c_str();
arg_map[i].obj = args[i];
}
std::vector<const char*> p_body_miss(m_body_miss.size());
for (size_t i = 0; i < m_body_miss.size(); i++)
p_body_miss[i] = m_body_miss[i].c_str();
m_offsets.resize(m_param_names.size() + 1);
return ctx.launch_raytrace(glbDim, arg_map, m_maxRecursionDepth,
arr_tlas, tex2ds, tex3ds, cubemaps, m_body_raygen.c_str(), p_body_miss, m_body_hit, m_kid, m_offsets.data(), times_submission);
}
else
{
locker.unlock();
return ctx.launch_raytrace(glbDim, m_param_names.size(), args,
arr_tlas.data(), tex2ds.data(), tex3ds.data(), cubemaps.data(), m_kid, m_offsets.data(), times_submission);
}
}
}
}