GlobalIlluminationProbeComponent.cpp 6.2 KB

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