SpiecsEngine
 
Loading...
Searching...
No Matches

◆ CreateTopLevelAS()

void Spices::RayTracingRenderer::CreateTopLevelAS ( FrameInfo & frameInfo,
std::shared_ptr< std::vector< uint32_t > > view,
std::shared_ptr< VulkanRayTracing > rayTracingInstance,
bool update = false )
private

Create TopLevelAS.

Parameters
[in]frameInfoFrameInfo.
[in]viewComponent View.
[in]rayTracingInstanceVulkanRayTracingInstance.
[in]updatedefault false.

Cache this frame hit groups.

Build TLAS.

Cache this frame hit groups.

Build TLAS.

Definition at line 247 of file RayTracingRenderer.cpp.

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()); // Position of the instance
267 rayInst.instanceCustomIndex = index; // gl_InstanceCustomIndexEXT
268 rayInst.accelerationStructureReference = v->GetAccel().accel->GetACDeviceAddress();
269 rayInst.flags = VK_GEOMETRY_INSTANCE_TRIANGLE_FACING_CULL_DISABLE_BIT_KHR;
270 rayInst.mask = 0xFF; // Only be hit if rayMask & instance.mask != 0
271 rayInst.instanceShaderBindingTableRecordOffset = v->GetHitShaderHandle(); // We will use the same hit group for all objects
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
289 SetHitGroupsCache(hitGroups);
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.