ReflectionProbeComponent.cpp 3.0 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091
  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::kAllSampled | TextureUsageBit::kStorageComputeWrite | TextureUsageBit::kStorageComputeRead
  27. | TextureUsageBit::kAllFramebuffer;
  28. m_reflectionTex = GrManager::getSingleton().newTexture(texInit);
  29. m_reflectionTexBindlessIndex = m_reflectionTex->getOrCreateBindlessTextureIndex(TextureSubresourceDesc::all());
  30. }
  31. ReflectionProbeComponent::~ReflectionProbeComponent()
  32. {
  33. }
  34. Error ReflectionProbeComponent::update(SceneComponentUpdateInfo& info, Bool& updated)
  35. {
  36. const Bool moved = info.m_node->movedThisFrame();
  37. updated = moved || m_dirty;
  38. m_dirty = false;
  39. if(moved) [[unlikely]]
  40. {
  41. m_reflectionNeedsRefresh = true;
  42. }
  43. if(updated) [[unlikely]]
  44. {
  45. m_worldPos = info.m_node->getWorldTransform().getOrigin().xyz();
  46. // Update the UUID
  47. m_uuid = (m_reflectionNeedsRefresh) ? SceneGraph::getSingleton().getNewUuid() : 0;
  48. // Upload to the GPU scene
  49. GpuSceneReflectionProbe gpuProbe;
  50. gpuProbe.m_position = m_worldPos;
  51. gpuProbe.m_cubeTexture = m_reflectionTexBindlessIndex;
  52. const Aabb aabbWorld(-m_halfSize + m_worldPos, m_halfSize + m_worldPos);
  53. gpuProbe.m_aabbMin = aabbWorld.getMin().xyz();
  54. gpuProbe.m_aabbMax = aabbWorld.getMax().xyz();
  55. gpuProbe.m_uuid = m_uuid;
  56. gpuProbe.m_componentArrayIndex = getArrayIndex();
  57. m_gpuSceneProbe.uploadToGpuScene(gpuProbe);
  58. }
  59. return Error::kNone;
  60. }
  61. F32 ReflectionProbeComponent::getRenderRadius() const
  62. {
  63. F32 effectiveDistance = max(m_halfSize.x(), m_halfSize.y());
  64. effectiveDistance = max(effectiveDistance, m_halfSize.z());
  65. effectiveDistance = max(effectiveDistance, g_probeEffectiveDistanceCVar.get());
  66. return effectiveDistance;
  67. }
  68. F32 ReflectionProbeComponent::getShadowsRenderRadius() const
  69. {
  70. return min(getRenderRadius(), g_probeShadowEffectiveDistanceCVar.get());
  71. }
  72. } // end namespace anki