ReflectionProbeComponent.cpp 4.4 KB

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