ReflectionProbeComponent.cpp 2.9 KB

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