GlobalIlluminationProbeComponent.cpp 6.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183
  1. // Copyright (C) 2009-2023, 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/Scene/Components/GlobalIlluminationProbeComponent.h>
  6. #include <AnKi/Scene/Components/MoveComponent.h>
  7. #include <AnKi/Scene/SceneNode.h>
  8. #include <AnKi/Scene/SceneGraph.h>
  9. #include <AnKi/Core/ConfigSet.h>
  10. #include <AnKi/Resource/ResourceManager.h>
  11. namespace anki {
  12. GlobalIlluminationProbeComponent::GlobalIlluminationProbeComponent(SceneNode* node)
  13. : SceneComponent(node, getStaticClassId())
  14. , m_spatial(this)
  15. {
  16. for(U32 i = 0; i < 6; ++i)
  17. {
  18. m_frustums[i].init(FrustumType::kPerspective, &node->getMemoryPool());
  19. m_frustums[i].setPerspective(kClusterObjectFrustumNearPlane, 100.0f, kPi / 2.0f, kPi / 2.0f);
  20. m_frustums[i].setWorldTransform(
  21. Transform(node->getWorldTransform().getOrigin(), Frustum::getOmnidirectionalFrustumRotations()[i], 1.0f));
  22. m_frustums[i].setShadowCascadeCount(1);
  23. m_frustums[i].update();
  24. }
  25. m_gpuSceneIndex = node->getSceneGraph().getAllGpuSceneContiguousArrays().allocate(
  26. GpuSceneContiguousArrayType::kGlobalIlluminationProbes);
  27. const Error err = getExternalSubsystems(*node).m_resourceManager->loadResource(
  28. "ShaderBinaries/ClearTextureCompute.ankiprogbin", m_clearTextureProg);
  29. if(err)
  30. {
  31. ANKI_LOGF("Failed to load shader");
  32. }
  33. }
  34. GlobalIlluminationProbeComponent::~GlobalIlluminationProbeComponent()
  35. {
  36. }
  37. void GlobalIlluminationProbeComponent::onDestroy(SceneNode& node)
  38. {
  39. m_spatial.removeFromOctree(node.getSceneGraph().getOctree());
  40. node.getSceneGraph().getAllGpuSceneContiguousArrays().deferredFree(
  41. GpuSceneContiguousArrayType::kGlobalIlluminationProbes, m_gpuSceneIndex);
  42. }
  43. Error GlobalIlluminationProbeComponent::update(SceneComponentUpdateInfo& info, Bool& updated)
  44. {
  45. updated = info.m_node->movedThisFrame() || m_shapeDirty;
  46. if(m_shapeDirty) [[unlikely]]
  47. {
  48. TextureInitInfo texInit("GiProbe");
  49. texInit.m_format =
  50. (getExternalSubsystems(*info.m_node).m_grManager->getDeviceCapabilities().m_unalignedBbpTextureFormats)
  51. ? Format::kR16G16B16_Sfloat
  52. : Format::kR16G16B16A16_Sfloat;
  53. texInit.m_width = m_cellCounts.x() * 6;
  54. texInit.m_height = m_cellCounts.y();
  55. texInit.m_depth = m_cellCounts.z();
  56. texInit.m_type = TextureType::k3D;
  57. texInit.m_usage =
  58. TextureUsageBit::kAllSampled | TextureUsageBit::kImageComputeWrite | TextureUsageBit::kImageComputeRead;
  59. m_volTex = getExternalSubsystems(*info.m_node).m_grManager->newTexture(texInit);
  60. TextureViewInitInfo viewInit(m_volTex, "GiProbe");
  61. m_volView = getExternalSubsystems(*info.m_node).m_grManager->newTextureView(viewInit);
  62. m_volTexBindlessIdx = m_volView->getOrCreateBindlessTextureIndex();
  63. // Zero the texture
  64. const ShaderProgramResourceVariant* variant;
  65. ShaderProgramResourceVariantInitInfo variantInit(m_clearTextureProg);
  66. variantInit.addMutation("TEXTURE_DIMENSIONS", 3);
  67. variantInit.addMutation("COMPONENT_TYPE", 0);
  68. m_clearTextureProg->getOrCreateVariant(variantInit, variant);
  69. CommandBufferInitInfo cmdbInit("ClearGIVol");
  70. cmdbInit.m_flags = CommandBufferFlag::kSmallBatch | CommandBufferFlag::kGeneralWork;
  71. CommandBufferPtr cmdb = getExternalSubsystems(*info.m_node).m_grManager->newCommandBuffer(cmdbInit);
  72. TextureBarrierInfo texBarrier;
  73. texBarrier.m_previousUsage = TextureUsageBit::kNone;
  74. texBarrier.m_nextUsage = TextureUsageBit::kImageComputeWrite;
  75. texBarrier.m_texture = m_volTex.get();
  76. cmdb->setPipelineBarrier({&texBarrier, 1}, {}, {});
  77. cmdb->bindShaderProgram(variant->getProgram());
  78. cmdb->bindImage(0, 0, m_volView);
  79. const Vec4 clearColor(0.0f);
  80. cmdb->setPushConstants(&clearColor, sizeof(clearColor));
  81. UVec3 wgSize;
  82. wgSize.x() = (8 - 1 + m_volTex->getWidth()) / 8;
  83. wgSize.y() = (8 - 1 + m_volTex->getHeight()) / 8;
  84. wgSize.z() = (8 - 1 + m_volTex->getDepth()) / 8;
  85. cmdb->dispatchCompute(wgSize.x(), wgSize.y(), wgSize.z());
  86. texBarrier.m_previousUsage = TextureUsageBit::kImageComputeWrite;
  87. texBarrier.m_nextUsage = m_volTex->getTextureUsage();
  88. cmdb->setPipelineBarrier({&texBarrier, 1}, {}, {});
  89. cmdb->flush();
  90. }
  91. if(updated) [[unlikely]]
  92. {
  93. m_shapeDirty = false;
  94. m_cellIdxToRefresh = 0;
  95. m_worldPos = info.m_node->getWorldTransform().getOrigin().xyz();
  96. const Aabb aabb(-m_halfSize + m_worldPos, m_halfSize + m_worldPos);
  97. m_spatial.setBoundingShape(aabb);
  98. // Upload to the GPU scene
  99. GpuSceneGlobalIlluminationProbe gpuProbe;
  100. gpuProbe.m_aabbMin = aabb.getMin().xyz();
  101. gpuProbe.m_aabbMax = aabb.getMax().xyz();
  102. gpuProbe.m_volumeTexture = m_volTexBindlessIdx;
  103. gpuProbe.m_halfTexelSizeU = 1.0f / (F32(m_cellCounts.y()) * 6.0f) / 2.0f;
  104. gpuProbe.m_fadeDistance = m_fadeDistance;
  105. const PtrSize offset = m_gpuSceneIndex * sizeof(GpuSceneGlobalIlluminationProbe)
  106. + info.m_node->getSceneGraph().getAllGpuSceneContiguousArrays().getArrayBase(
  107. GpuSceneContiguousArrayType::kGlobalIlluminationProbes);
  108. GpuSceneMicroPatcher::getSingleton().newCopy(*info.m_framePool, offset, sizeof(gpuProbe), &gpuProbe);
  109. }
  110. if(needsRefresh()) [[unlikely]]
  111. {
  112. updated = true;
  113. // Compute the position of the cell
  114. const Aabb aabb(-m_halfSize + m_worldPos, m_halfSize + m_worldPos);
  115. U32 x, y, z;
  116. unflatten3dArrayIndex(m_cellCounts.x(), m_cellCounts.y(), m_cellCounts.z(), m_cellIdxToRefresh, x, y, z);
  117. const Vec3 cellSize = ((m_halfSize * 2.0f) / Vec3(m_cellCounts));
  118. const Vec3 halfCellSize = cellSize / 2.0f;
  119. const Vec3 cellCenter = aabb.getMin().xyz() + halfCellSize + cellSize * Vec3(UVec3(x, y, z));
  120. F32 effectiveDistance = max(m_halfSize.x(), m_halfSize.y());
  121. effectiveDistance = max(effectiveDistance, m_halfSize.z());
  122. effectiveDistance = max(effectiveDistance, ConfigSet::getSingleton().getSceneProbeEffectiveDistance());
  123. const F32 shadowCascadeDistance =
  124. min(effectiveDistance, ConfigSet::getSingleton().getSceneProbeShadowEffectiveDistance());
  125. for(U32 i = 0; i < 6; ++i)
  126. {
  127. m_frustums[i].setWorldTransform(
  128. Transform(cellCenter.xyz0(), Frustum::getOmnidirectionalFrustumRotations()[i], 1.0f));
  129. m_frustums[i].setFar(effectiveDistance);
  130. m_frustums[i].setShadowCascadeDistance(0, shadowCascadeDistance);
  131. // Add something really far to force LOD 0 to be used. The importing tools create LODs with holes some times
  132. // and that causes the sky to bleed to GI rendering
  133. m_frustums[i].setLodDistances({effectiveDistance - 3.0f * kEpsilonf, effectiveDistance - 2.0f * kEpsilonf,
  134. effectiveDistance - 1.0f * kEpsilonf});
  135. }
  136. }
  137. for(U32 i = 0; i < 6; ++i)
  138. {
  139. const Bool frustumUpdated = m_frustums[i].update();
  140. updated = updated || frustumUpdated;
  141. }
  142. const Bool spatialUpdated = m_spatial.update(info.m_node->getSceneGraph().getOctree());
  143. updated = updated || spatialUpdated;
  144. return Error::kNone;
  145. }
  146. } // end namespace anki