ReflectionProbeComponent.cpp 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118
  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/ReflectionProbeComponent.h>
  6. #include <AnKi/Scene/Components/MoveComponent.h>
  7. #include <AnKi/Scene/SceneGraph.h>
  8. #include <AnKi/Scene/SceneNode.h>
  9. #include <AnKi/Core/ConfigSet.h>
  10. namespace anki {
  11. ReflectionProbeComponent::ReflectionProbeComponent(SceneNode* node)
  12. : QueryableSceneComponent<ReflectionProbeComponent>(node, getStaticClassId())
  13. , m_spatial(this)
  14. {
  15. m_worldPos = node->getWorldTransform().getOrigin().xyz();
  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(m_worldPos.xyz0(), Frustum::getOmnidirectionalFrustumRotations()[i], 1.0f));
  21. m_frustums[i].setShadowCascadeCount(1);
  22. m_frustums[i].update();
  23. }
  24. m_gpuSceneIndex = GpuSceneContiguousArrays::getSingleton().allocate(GpuSceneContiguousArrayType::kReflectionProbes);
  25. }
  26. ReflectionProbeComponent::~ReflectionProbeComponent()
  27. {
  28. m_spatial.removeFromOctree(SceneGraph::getSingleton().getOctree());
  29. }
  30. Error ReflectionProbeComponent::update(SceneComponentUpdateInfo& info, Bool& updated)
  31. {
  32. const Bool moved = info.m_node->movedThisFrame();
  33. const Bool shapeUpdated = m_dirty;
  34. m_dirty = false;
  35. updated = moved || shapeUpdated;
  36. if(shapeUpdated && !m_reflectionTex) [[unlikely]]
  37. {
  38. TextureInitInfo texInit("ReflectionProbe");
  39. texInit.m_format = (GrManager::getSingleton().getDeviceCapabilities().m_unalignedBbpTextureFormats) ? Format::kR16G16B16_Sfloat
  40. : Format::kR16G16B16A16_Sfloat;
  41. texInit.m_width = ConfigSet::getSingleton().getSceneReflectionProbeResolution();
  42. texInit.m_height = texInit.m_width;
  43. texInit.m_mipmapCount = U8(computeMaxMipmapCount2d(texInit.m_width, texInit.m_height, 8));
  44. texInit.m_type = TextureType::kCube;
  45. texInit.m_usage = TextureUsageBit::kAllSampled | TextureUsageBit::kImageComputeWrite | TextureUsageBit::kImageComputeRead
  46. | TextureUsageBit::kAllFramebuffer | TextureUsageBit::kGenerateMipmaps;
  47. m_reflectionTex = GrManager::getSingleton().newTexture(texInit);
  48. TextureViewInitInfo viewInit(m_reflectionTex.get(), "ReflectionPRobe");
  49. m_reflectionView = GrManager::getSingleton().newTextureView(viewInit);
  50. m_reflectionTexBindlessIndex = m_reflectionView->getOrCreateBindlessTextureIndex();
  51. }
  52. if(updated) [[unlikely]]
  53. {
  54. m_reflectionNeedsRefresh = true;
  55. m_worldPos = info.m_node->getWorldTransform().getOrigin().xyz();
  56. F32 effectiveDistance = max(m_halfSize.x(), m_halfSize.y());
  57. effectiveDistance = max(effectiveDistance, m_halfSize.z());
  58. effectiveDistance = max(effectiveDistance, ConfigSet::getSingleton().getSceneProbeEffectiveDistance());
  59. const F32 shadowCascadeDistance = min(effectiveDistance, ConfigSet::getSingleton().getSceneProbeShadowEffectiveDistance());
  60. for(U32 i = 0; i < 6; ++i)
  61. {
  62. m_frustums[i].setWorldTransform(Transform(m_worldPos.xyz0(), Frustum::getOmnidirectionalFrustumRotations()[i], 1.0f));
  63. m_frustums[i].setFar(effectiveDistance);
  64. m_frustums[i].setShadowCascadeDistance(0, shadowCascadeDistance);
  65. // Add something really far to force LOD 0 to be used. The importing tools create LODs with holes some times
  66. // and that causes the sky to bleed to GI rendering
  67. m_frustums[i].setLodDistances(
  68. {effectiveDistance - 3.0f * kEpsilonf, effectiveDistance - 2.0f * kEpsilonf, effectiveDistance - 1.0f * kEpsilonf});
  69. }
  70. const Aabb aabbWorld(-m_halfSize + m_worldPos, m_halfSize + m_worldPos);
  71. m_spatial.setBoundingShape(aabbWorld);
  72. // New UUID
  73. refreshUuid();
  74. // Upload to the GPU scene
  75. GpuSceneReflectionProbe gpuProbe;
  76. gpuProbe.m_position = m_worldPos;
  77. gpuProbe.m_cubeTexture = m_reflectionTexBindlessIndex;
  78. gpuProbe.m_aabbMin = aabbWorld.getMin().xyz();
  79. gpuProbe.m_aabbMax = aabbWorld.getMax().xyz();
  80. gpuProbe.m_uuid = getUuid();
  81. GpuSceneMicroPatcher::getSingleton().newCopy(*info.m_framePool, m_gpuSceneIndex.getOffsetInGpuScene(), gpuProbe);
  82. }
  83. // Update spatial and frustums
  84. const Bool spatialUpdated = m_spatial.update(SceneGraph::getSingleton().getOctree());
  85. updated = updated || spatialUpdated;
  86. for(U32 i = 0; i < 6; ++i)
  87. {
  88. const Bool frustumUpdated = m_frustums[i].update();
  89. updated = updated || frustumUpdated;
  90. }
  91. return Error::kNone;
  92. }
  93. } // end namespace anki