PrimaryNonRenderableVisibility.cpp 5.5 KB

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