ReflectionProbeComponent.cpp 2.9 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/Util/CVarSet.h>
  10. #include <AnKi/Gr/Texture.h>
  11. namespace anki {
  12. ReflectionProbeComponent::ReflectionProbeComponent(SceneNode* node)
  13. : SceneComponent(node, kClassType)
  14. {
  15. m_worldPos = node->getWorldTransform().getOrigin().xyz;
  16. m_gpuSceneProbe.allocate();
  17. TextureInitInfo texInit("ReflectionProbe");
  18. texInit.m_format =
  19. (GrManager::getSingleton().getDeviceCapabilities().m_unalignedBbpTextureFormats) ? Format::kR16G16B16_Sfloat : Format::kR16G16B16A16_Sfloat;
  20. texInit.m_width = g_cvarRenderProbeReflectionsResolution;
  21. texInit.m_height = texInit.m_width;
  22. texInit.m_mipmapCount = computeMaxMipmapCount2d(texInit.m_width, texInit.m_height, 8);
  23. texInit.m_type = TextureType::kCube;
  24. texInit.m_usage = TextureUsageBit::kAllSrv | TextureUsageBit::kUavCompute | TextureUsageBit::kAllRtvDsv;
  25. m_reflectionTex = GrManager::getSingleton().newTexture(texInit);
  26. m_reflectionTexBindlessIndex = m_reflectionTex->getOrCreateBindlessTextureIndex(TextureSubresourceDesc::all());
  27. }
  28. ReflectionProbeComponent::~ReflectionProbeComponent()
  29. {
  30. }
  31. void ReflectionProbeComponent::update(SceneComponentUpdateInfo& info, Bool& updated)
  32. {
  33. const Bool moved = info.m_node->movedThisFrame();
  34. updated = moved || m_dirty;
  35. m_dirty = false;
  36. if(moved) [[unlikely]]
  37. {
  38. m_reflectionNeedsRefresh = true;
  39. }
  40. if(updated) [[unlikely]]
  41. {
  42. m_worldPos = info.m_node->getWorldTransform().getOrigin().xyz;
  43. m_halfSize = info.m_node->getWorldTransform().getScale().xyz;
  44. // Upload to the GPU scene
  45. GpuSceneReflectionProbe gpuProbe;
  46. gpuProbe.m_position = m_worldPos;
  47. gpuProbe.m_cubeTexture = m_reflectionTexBindlessIndex;
  48. const Aabb aabbWorld(-m_halfSize + m_worldPos, m_halfSize + m_worldPos);
  49. gpuProbe.m_aabbMin = aabbWorld.getMin().xyz;
  50. gpuProbe.m_aabbMax = aabbWorld.getMax().xyz;
  51. gpuProbe.m_componentUuid = getUuid();
  52. gpuProbe.m_componentArrayIndex = getArrayIndex();
  53. gpuProbe.m_cpuFeedback = m_reflectionNeedsRefresh;
  54. gpuProbe.m_sceneNodeUuid = info.m_node->getUuid();
  55. m_gpuSceneProbe.uploadToGpuScene(gpuProbe);
  56. }
  57. }
  58. F32 ReflectionProbeComponent::getRenderRadius() const
  59. {
  60. F32 effectiveDistance = max(m_halfSize.x, m_halfSize.y);
  61. effectiveDistance = max(effectiveDistance, m_halfSize.z);
  62. effectiveDistance = max<F32>(effectiveDistance, g_cvarSceneProbeEffectiveDistance);
  63. return effectiveDistance;
  64. }
  65. F32 ReflectionProbeComponent::getShadowsRenderRadius() const
  66. {
  67. return min<F32>(getRenderRadius(), g_cvarSceneProbeShadowEffectiveDistance);
  68. }
  69. Error ReflectionProbeComponent::serialize(SceneSerializer& serializer)
  70. {
  71. ANKI_SERIALIZE(m_halfSize, 1);
  72. return Error::kNone;
  73. }
  74. } // end namespace anki