Create TopLevelAS.
Cache this frame hit groups.
Cache this frame hit groups.
248 {
250
251 std::vector<VkAccelerationStructureInstanceKHR> tlas;
252 std::shared_ptr<std::unordered_map<std::string, uint32_t>> hitGroups = std::make_shared<std::unordered_map<std::string, uint32_t>>();
253
254 int index = 0;
255 auto& desc = rayTracingInstance->GetMeshDesc().attributes;
256 desc->resize(SpicesShader::MESH_BUFFER_MAXNUM, 0);
257
258 frameInfo.m_World->ViewComponent<MeshComponent>(*view, [&](
auto e,
auto& meshComp){
259
260 auto& tranComp = frameInfo.m_World->GetComponent<TransformComponent>(
static_cast<entt::entity
>(
e));
261
262 meshComp.GetMesh()->AddMaterialToHitGroup(*hitGroups);
263 meshComp.GetMesh()->GetPacks().for_each([&](const uint32_t& k, const std::shared_ptr<MeshPack>& v) {
264
265 VkAccelerationStructureInstanceKHR rayInst{};
266 rayInst.transform = ToVkTransformMatrixKHR(tranComp.GetModelMatrix());
267 rayInst.instanceCustomIndex = index;
268 rayInst.accelerationStructureReference = v->GetAccel().accel->GetACDeviceAddress();
269 rayInst.flags = VK_GEOMETRY_INSTANCE_TRIANGLE_FACING_CULL_DISABLE_BIT_KHR;
270 rayInst.mask = 0xFF;
271 rayInst.instanceShaderBindingTableRecordOffset = v->GetHitShaderHandle();
272
273 tlas.push_back(rayInst);
274
275 (*desc)[index] = v->GetMeshDesc().GetBufferAddress();
276
277 index += 1;
278 return false;
279 });
280
281 return false;
282 });
283
284 rayTracingInstance->GetMeshDesc().CreateBuffer("MeshDescBuffer", VK_BUFFER_USAGE_STORAGE_BUFFER_BIT);
285
290
291 rayTracingInstance->SetHitGroups(hitGroups);
292
296 rayTracingInstance->BuildTLAS(
297 tlas,
298 VK_BUILD_ACCELERATION_STRUCTURE_PREFER_FAST_TRACE_BIT_KHR |
299 VK_BUILD_ACCELERATION_STRUCTURE_ALLOW_UPDATE_BIT_KHR |
300 VK_BUILD_ACCELERATION_STRUCTURE_ALLOW_COMPACTION_BIT_KHR ,
301 update
302 );
303 }
#define SPICES_PROFILE_ZONE
void SetHitGroupsCache(std::shared_ptr< std::unordered_map< std::string, uint32_t > > cache)
Set HitGroupsCache.