AccelerationStructureBuilder.cpp 3.8 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788
  1. // Copyright (C) 2009-present, Panagiotis Christopoulos Charitos and contributors.
  2. // All rights reserved.
  3. // Code licensed under the BSD License.
  4. // http://www.anki3d.org/LICENSE
  5. #include <AnKi/Renderer/AccelerationStructureBuilder.h>
  6. #include <AnKi/Renderer/Renderer.h>
  7. #include <AnKi/Renderer/Utils/GpuVisibility.h>
  8. #include <AnKi/Util/Tracer.h>
  9. #include <AnKi/Core/App.h>
  10. #include <AnKi/GpuMemory/GpuVisibleTransientMemoryPool.h>
  11. namespace anki {
  12. void AccelerationStructureBuilder::populateRenderGraph(RenderingContext& ctx)
  13. {
  14. ANKI_TRACE_SCOPED_EVENT(ASBuilder);
  15. // Do visibility
  16. GpuVisibilityAccelerationStructuresOutput visOut;
  17. {
  18. const Array<F32, kMaxLodCount - 1> lodDistances = {g_cvarRenderLod0MaxDistance, g_cvarRenderLod1MaxDistance};
  19. GpuVisibilityAccelerationStructuresInput in;
  20. in.m_passesName = "Main TLAS visiblity";
  21. in.m_lodReferencePoint = ctx.m_matrices.m_cameraTransform.getTranslationPart().xyz;
  22. in.m_lodDistances = lodDistances;
  23. in.m_pointOfTest = in.m_lodReferencePoint;
  24. in.m_testRadius = g_cvarRenderRtExtendedFrustumDistance;
  25. in.m_viewProjectionMatrix = ctx.m_matrices.m_viewProjection;
  26. in.m_rgraph = &ctx.m_renderGraphDescr;
  27. getRenderer().getGpuVisibilityAccelerationStructures().pupulateRenderGraph(in, visOut);
  28. m_runCtx.m_dependency = visOut.m_dependency;
  29. m_runCtx.m_visibleRenderablesBuff = visOut.m_renderablesBuffer;
  30. m_runCtx.m_buildSbtIndirectArgsBuff = visOut.m_buildSbtIndirectArgsBuffer;
  31. }
  32. // Create the TLAS
  33. AccelerationStructureInitInfo initInf("Main TLAS");
  34. initInf.m_type = AccelerationStructureType::kTopLevel;
  35. initInf.m_topLevel.m_instanceCount = GpuSceneArrays::RenderableBoundingVolumeRt::getSingleton().getElementCount();
  36. initInf.m_topLevel.m_instancesBuffer = visOut.m_instancesBuffer;
  37. const PtrSize memoryReq = GrManager::getSingleton().getAccelerationStructureMemoryRequirement(initInf);
  38. initInf.m_accelerationStructureBuffer = GpuVisibleTransientMemoryPool::getSingleton().allocate(memoryReq, 1);
  39. m_runCtx.m_tlas = GrManager::getSingleton().newAccelerationStructure(initInf);
  40. // Build the AS
  41. {
  42. RenderGraphBuilder& rgraph = ctx.m_renderGraphDescr;
  43. const BufferView scratchBuff = GpuVisibleTransientMemoryPool::getSingleton().allocate(m_runCtx.m_tlas->getBuildScratchBufferSize(), 1);
  44. m_runCtx.m_tlasHandle = rgraph.importAccelerationStructure(m_runCtx.m_tlas.get(), AccelerationStructureUsageBit::kNone);
  45. NonGraphicsRenderPass& rpass = rgraph.newNonGraphicsRenderPass("Build TLAS");
  46. rpass.newAccelerationStructureDependency(m_runCtx.m_tlasHandle, AccelerationStructureUsageBit::kBuild);
  47. rpass.newBufferDependency(visOut.m_dependency, BufferUsageBit::kAccelerationStructureBuild);
  48. rpass.setWork([this, scratchBuff](RenderPassWorkContext& rgraphCtx) {
  49. ANKI_TRACE_SCOPED_EVENT(ASBuilder);
  50. rgraphCtx.m_commandBuffer->buildAccelerationStructure(m_runCtx.m_tlas.get(), scratchBuff);
  51. });
  52. }
  53. // Light visibility
  54. {
  55. GpuVisibilityLocalLightsInput in;
  56. in.m_cellCounts = UVec3(g_cvarRenderRtLightGridCellCountXZ, g_cvarRenderRtLightGridCellCountY, g_cvarRenderRtLightGridCellCountXZ);
  57. in.m_cellSize = Vec3(g_cvarRenderRtLightGridSizeXZ, g_cvarRenderRtLightGridSizeY, g_cvarRenderRtLightGridSizeXZ) / Vec3(in.m_cellCounts);
  58. in.m_cameraPosition = ctx.m_matrices.m_cameraTransform.getTranslationPart().xyz;
  59. in.m_lookDirection = -ctx.m_matrices.m_cameraTransform.getRotationPart().getZAxis();
  60. in.m_lightIndexListSize = g_cvarRenderRtLightIndexListSize;
  61. in.m_rgraph = &ctx.m_renderGraphDescr;
  62. getGpuVisibilityLocalLights().populateRenderGraph(in, m_runCtx.m_lightVisInfo);
  63. m_runCtx.m_lightGridConsts.m_volumeMin = m_runCtx.m_lightVisInfo.m_lightGridMin;
  64. m_runCtx.m_lightGridConsts.m_volumeMax = m_runCtx.m_lightVisInfo.m_lightGridMax;
  65. m_runCtx.m_lightGridConsts.m_cellCounts = in.m_cellCounts;
  66. m_runCtx.m_lightGridConsts.m_cellSize = in.m_cellSize;
  67. }
  68. }
  69. } // end namespace anki