ReflectionProbeComponent.cpp 3.2 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495
  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. #include <AnKi/Gr/Texture.h>
  11. #include <AnKi/Gr/TextureView.h>
  12. namespace anki {
  13. NumericCVar<U32> g_reflectionProbeResolutionCVar(CVarSubsystem::kScene, "ReflectionProbeResolution", 128, 8, 2048,
  14. "The resolution of the reflection probe's reflection");
  15. ReflectionProbeComponent::ReflectionProbeComponent(SceneNode* node)
  16. : SceneComponent(node, kClassType)
  17. {
  18. m_worldPos = node->getWorldTransform().getOrigin().xyz();
  19. m_gpuSceneProbe.allocate();
  20. TextureInitInfo texInit("ReflectionProbe");
  21. texInit.m_format =
  22. (GrManager::getSingleton().getDeviceCapabilities().m_unalignedBbpTextureFormats) ? Format::kR16G16B16_Sfloat : Format::kR16G16B16A16_Sfloat;
  23. texInit.m_width = g_reflectionProbeResolutionCVar.get();
  24. texInit.m_height = texInit.m_width;
  25. texInit.m_mipmapCount = U8(computeMaxMipmapCount2d(texInit.m_width, texInit.m_height, 8));
  26. texInit.m_type = TextureType::kCube;
  27. texInit.m_usage = TextureUsageBit::kAllSampled | TextureUsageBit::kImageComputeWrite | TextureUsageBit::kImageComputeRead
  28. | TextureUsageBit::kAllFramebuffer | TextureUsageBit::kGenerateMipmaps;
  29. m_reflectionTex = GrManager::getSingleton().newTexture(texInit);
  30. TextureViewInitInfo viewInit(m_reflectionTex.get(), "ReflectionProbe");
  31. m_reflectionView = GrManager::getSingleton().newTextureView(viewInit);
  32. m_reflectionTexBindlessIndex = m_reflectionView->getOrCreateBindlessTextureIndex();
  33. }
  34. ReflectionProbeComponent::~ReflectionProbeComponent()
  35. {
  36. }
  37. Error ReflectionProbeComponent::update(SceneComponentUpdateInfo& info, Bool& updated)
  38. {
  39. const Bool moved = info.m_node->movedThisFrame();
  40. updated = moved || m_dirty;
  41. m_dirty = false;
  42. if(moved) [[unlikely]]
  43. {
  44. m_reflectionNeedsRefresh = true;
  45. }
  46. if(updated) [[unlikely]]
  47. {
  48. m_worldPos = info.m_node->getWorldTransform().getOrigin().xyz();
  49. // Update the UUID
  50. m_uuid = (m_reflectionNeedsRefresh) ? SceneGraph::getSingleton().getNewUuid() : 0;
  51. // Upload to the GPU scene
  52. GpuSceneReflectionProbe gpuProbe;
  53. gpuProbe.m_position = m_worldPos;
  54. gpuProbe.m_cubeTexture = m_reflectionTexBindlessIndex;
  55. const Aabb aabbWorld(-m_halfSize + m_worldPos, m_halfSize + m_worldPos);
  56. gpuProbe.m_aabbMin = aabbWorld.getMin().xyz();
  57. gpuProbe.m_aabbMax = aabbWorld.getMax().xyz();
  58. gpuProbe.m_uuid = m_uuid;
  59. gpuProbe.m_arrayIndex = getArrayIndex();
  60. m_gpuSceneProbe.uploadToGpuScene(gpuProbe);
  61. }
  62. return Error::kNone;
  63. }
  64. F32 ReflectionProbeComponent::getRenderRadius() const
  65. {
  66. F32 effectiveDistance = max(m_halfSize.x(), m_halfSize.y());
  67. effectiveDistance = max(effectiveDistance, m_halfSize.z());
  68. effectiveDistance = max(effectiveDistance, g_probeEffectiveDistanceCVar.get());
  69. return effectiveDistance;
  70. }
  71. F32 ReflectionProbeComponent::getShadowsRenderRadius() const
  72. {
  73. return min(getRenderRadius(), g_probeShadowEffectiveDistanceCVar.get());
  74. }
  75. } // end namespace anki