PrimaryNonRenderableVisibility.cpp 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150
  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/PrimaryNonRenderableVisibility.h>
  6. #include <AnKi/Renderer/Renderer.h>
  7. #include <AnKi/Renderer/GBuffer.h>
  8. #include <AnKi/Renderer/Utils/GpuVisibility.h>
  9. #include <AnKi/Shaders/Include/GpuSceneFunctions.h>
  10. #include <AnKi/Scene/GpuSceneArray.h>
  11. #include <AnKi/Scene/Components/LightComponent.h>
  12. #include <AnKi/Scene/Components/ReflectionProbeComponent.h>
  13. #include <AnKi/Scene/Components/GlobalIlluminationProbeComponent.h>
  14. #include <AnKi/Util/Tracer.h>
  15. namespace anki {
  16. template<typename TComponent, typename TArray, typename TPool>
  17. static WeakArray<TComponent*> gatherComponents(ConstWeakArray<UVec2> pairs, TArray& array, TPool& pool)
  18. {
  19. DynamicArray<TComponent*, MemoryPoolPtrWrapper<StackMemoryPool>> components(&pool);
  20. for(UVec2 pair : pairs)
  21. {
  22. if(!array.indexExists(pair.y()))
  23. {
  24. continue;
  25. }
  26. TComponent* comp = &array[pair.y()];
  27. if(comp->getUuid() == pair.x())
  28. {
  29. components.emplaceBack(comp);
  30. }
  31. }
  32. WeakArray<TComponent*> out;
  33. components.moveAndReset(out);
  34. return out;
  35. }
  36. void PrimaryNonRenderableVisibility::populateRenderGraph(RenderingContext& ctx)
  37. {
  38. ANKI_TRACE_SCOPED_EVENT(PrimaryNonRenderableVisibility);
  39. RenderGraphBuilder& rgraph = ctx.m_renderGraphDescr;
  40. m_runCtx = {};
  41. for(GpuSceneNonRenderableObjectType type : EnumIterable<GpuSceneNonRenderableObjectType>())
  42. {
  43. U32 objCount = 0;
  44. CString passName;
  45. switch(type)
  46. {
  47. case GpuSceneNonRenderableObjectType::kLight:
  48. objCount = GpuSceneArrays::Light::getSingleton().getElementCount();
  49. passName = "Primary non-renderable visibility: Lights";
  50. break;
  51. case GpuSceneNonRenderableObjectType::kDecal:
  52. objCount = GpuSceneArrays::Decal::getSingleton().getElementCount();
  53. passName = "Primary non-renderable visibility: Decals";
  54. break;
  55. case GpuSceneNonRenderableObjectType::kFogDensityVolume:
  56. objCount = GpuSceneArrays::FogDensityVolume::getSingleton().getElementCount();
  57. passName = "Primary non-renderable visibility: Fog volumes";
  58. break;
  59. case GpuSceneNonRenderableObjectType::kReflectionProbe:
  60. objCount = GpuSceneArrays::ReflectionProbe::getSingleton().getElementCount();
  61. passName = "Primary non-renderable visibility: Refl probes";
  62. break;
  63. case GpuSceneNonRenderableObjectType::kGlobalIlluminationProbe:
  64. objCount = GpuSceneArrays::GlobalIlluminationProbe::getSingleton().getElementCount();
  65. passName = "Primary non-renderable visibility: GI probes";
  66. break;
  67. default:
  68. ANKI_ASSERT(0);
  69. }
  70. if(objCount == 0)
  71. {
  72. // No objects, point to a buffer with zeros
  73. void* mem;
  74. RebarAllocation alloc = RebarTransientMemoryPool::getSingleton().allocateFrame(sizeof(U32), mem);
  75. memset(mem, 0, sizeof(U32));
  76. m_runCtx.m_visibleIndicesBuffers[type] = alloc;
  77. m_runCtx.m_visibleIndicesHandles[type] = rgraph.importBuffer(m_runCtx.m_visibleIndicesBuffers[type], BufferUsageBit::kNone);
  78. }
  79. else
  80. {
  81. // Some objects, perform visibility testing
  82. GpuVisibilityNonRenderablesInput in;
  83. in.m_passesName = passName;
  84. in.m_objectType = type;
  85. in.m_viewProjectionMat = ctx.m_matrices.m_viewProjection;
  86. in.m_hzbRt = &getRenderer().getGBuffer().getHzbRt();
  87. in.m_rgraph = &rgraph;
  88. const GpuSceneNonRenderableObjectTypeWithFeedback feedbackType = toGpuSceneNonRenderableObjectTypeWithFeedback(type);
  89. if(feedbackType != GpuSceneNonRenderableObjectTypeWithFeedback::kCount)
  90. {
  91. // Read feedback from the GPU
  92. DynamicArray<U32, MemoryPoolPtrWrapper<StackMemoryPool>> readbackData(&getRenderer().getFrameMemoryPool());
  93. getRenderer().getReadbackManager().readMostRecentData(m_readbacks[feedbackType], readbackData);
  94. if(readbackData.getSize())
  95. {
  96. ANKI_ASSERT(readbackData.getSize() > 1);
  97. const U32 pairCount = readbackData[0];
  98. if(pairCount)
  99. {
  100. WeakArray<UVec2> pairs(reinterpret_cast<UVec2*>(&readbackData[1]), pairCount);
  101. if(feedbackType == GpuSceneNonRenderableObjectTypeWithFeedback::kLight)
  102. {
  103. m_runCtx.m_interestingComponents.m_shadowLights = gatherComponents<LightComponent>(
  104. pairs, SceneGraph::getSingleton().getComponentArrays().getLights(), getRenderer().getFrameMemoryPool());
  105. }
  106. else if(feedbackType == GpuSceneNonRenderableObjectTypeWithFeedback::kReflectionProbe)
  107. {
  108. m_runCtx.m_interestingComponents.m_reflectionProbes = gatherComponents<ReflectionProbeComponent>(
  109. pairs, SceneGraph::getSingleton().getComponentArrays().getReflectionProbes(), getRenderer().getFrameMemoryPool());
  110. }
  111. else
  112. {
  113. ANKI_ASSERT(feedbackType == GpuSceneNonRenderableObjectTypeWithFeedback::kGlobalIlluminationProbe);
  114. m_runCtx.m_interestingComponents.m_globalIlluminationProbes = gatherComponents<GlobalIlluminationProbeComponent>(
  115. pairs, SceneGraph::getSingleton().getComponentArrays().getGlobalIlluminationProbes(),
  116. getRenderer().getFrameMemoryPool());
  117. }
  118. }
  119. }
  120. // Allocate feedback buffer for this frame
  121. getRenderer().getReadbackManager().allocateData(m_readbacks[feedbackType], (objCount * 2 + 1) * sizeof(U32), in.m_cpuFeedbackBuffer);
  122. }
  123. GpuVisibilityNonRenderablesOutput out;
  124. getRenderer().getGpuVisibilityNonRenderables().populateRenderGraph(in, out);
  125. m_runCtx.m_visibleIndicesHandles[type] = out.m_visiblesBufferHandle;
  126. m_runCtx.m_visibleIndicesBuffers[type] = out.m_visiblesBuffer;
  127. }
  128. }
  129. }
  130. } // end namespace anki