ReflectionProbe.h 8.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183
  1. /*
  2. * Copyright (c) Contributors to the Open 3D Engine Project.
  3. * For complete copyright and license terms please see the LICENSE at the root of this distribution.
  4. *
  5. * SPDX-License-Identifier: Apache-2.0 OR MIT
  6. *
  7. */
  8. #pragma once
  9. #include <AzCore/Math/Transform.h>
  10. #include <Atom/Feature/RenderCommon.h>
  11. #include <Atom/Feature/Mesh/MeshFeatureProcessorInterface.h>
  12. #include <Atom/RHI/GeometryView.h>
  13. #include <Atom/RPI.Public/Base.h>
  14. #include <Atom/RPI.Public/Model/Model.h>
  15. #include <Atom/RPI.Public/MeshDrawPacket.h>
  16. #include <Atom/RPI.Public/Pass/Specific/EnvironmentCubeMapPass.h>
  17. #include <Atom/RPI.Public/PipelineState.h>
  18. #include <Atom/RPI.Public/Shader/ShaderResourceGroup.h>
  19. #include <Atom/RPI.Public/Scene.h>
  20. #include <CubeMapCapture/CubeMapRenderer.h>
  21. namespace AZ
  22. {
  23. namespace Render
  24. {
  25. // shared data for rendering reflections, loaded and stored by the ReflectionProbeFeatureProcessor and passed to all probes
  26. struct ReflectionRenderData
  27. {
  28. RHI::GeometryView m_geometryView;
  29. RPI::Ptr<RPI::PipelineStateForDraw> m_stencilPipelineState;
  30. RPI::Ptr<RPI::PipelineStateForDraw> m_blendWeightPipelineState;
  31. RPI::Ptr<RPI::PipelineStateForDraw> m_renderOuterPipelineState;
  32. RPI::Ptr<RPI::PipelineStateForDraw> m_renderInnerPipelineState;
  33. Data::Instance<RPI::Shader> m_stencilShader;
  34. RHI::Ptr<RHI::ShaderResourceGroupLayout> m_stencilSrgLayout;
  35. Data::Instance<RPI::Shader> m_blendWeightShader;
  36. RHI::Ptr<RHI::ShaderResourceGroupLayout> m_blendWeightSrgLayout;
  37. Data::Instance<RPI::Shader> m_renderOuterShader;
  38. RHI::Ptr<RHI::ShaderResourceGroupLayout> m_renderOuterSrgLayout;
  39. Data::Instance<RPI::Shader> m_renderInnerShader;
  40. RHI::Ptr<RHI::ShaderResourceGroupLayout> m_renderInnerSrgLayout;
  41. RHI::DrawListTag m_stencilDrawListTag;
  42. RHI::DrawListTag m_blendWeightDrawListTag;
  43. RHI::DrawListTag m_renderOuterDrawListTag;
  44. RHI::DrawListTag m_renderInnerDrawListTag;
  45. RHI::ShaderInputNameIndex m_modelToWorldStencilConstantIndex = "m_modelToWorld";
  46. RHI::ShaderInputNameIndex m_modelToWorldRenderConstantIndex = "m_modelToWorld";
  47. RHI::ShaderInputNameIndex m_modelToWorldInverseRenderConstantIndex = "m_modelToWorldInverse";
  48. RHI::ShaderInputNameIndex m_outerObbHalfLengthsRenderConstantIndex = "m_outerObbHalfLengths";
  49. RHI::ShaderInputNameIndex m_innerObbHalfLengthsRenderConstantIndex = "m_innerObbHalfLengths";
  50. RHI::ShaderInputNameIndex m_useParallaxCorrectionRenderConstantIndex = "m_useParallaxCorrection";
  51. RHI::ShaderInputNameIndex m_exposureConstantIndex = "m_exposure";
  52. RHI::ShaderInputNameIndex m_reflectionCubeMapRenderImageIndex = "m_reflectionCubeMap";
  53. };
  54. // ReflectionProbe manages all aspects of a single probe, including rendering, visualization, and cubemap generation
  55. class ReflectionProbe final
  56. : public AZ::Data::AssetBus::Handler
  57. , private CubeMapRenderer
  58. {
  59. public:
  60. ReflectionProbe() = default;
  61. ~ReflectionProbe();
  62. void Init(RPI::Scene* scene, ReflectionRenderData* reflectionRenderData);
  63. void Simulate(uint32_t probeIndex);
  64. void OnRenderEnd();
  65. const Vector3& GetPosition() const { return m_transform.GetTranslation(); }
  66. const AZ::Transform& GetTransform() const { return m_transform; }
  67. void SetTransform(const AZ::Transform& transform);
  68. const AZ::Vector3& GetOuterExtents() const { return m_outerExtents; }
  69. void SetOuterExtents(const AZ::Vector3& outerExtents);
  70. const AZ::Vector3& GetInnerExtents() const { return m_innerExtents; }
  71. void SetInnerExtents(const AZ::Vector3& innerExtents);
  72. const Obb& GetOuterObbWs() const { return m_outerObbWs; }
  73. const Obb& GetInnerObbWs() const { return m_innerObbWs; }
  74. const Data::Instance<RPI::Image>& GetCubeMapImage() const { return m_cubeMapImage; }
  75. void SetCubeMapImage(const Data::Instance<RPI::Image>& cubeMapImage, const AZStd::string& relativePath);
  76. const AZStd::string& GetCubeMapRelativePath() const { return m_cubeMapRelativePath; }
  77. bool GetUseParallaxCorrection() const { return m_useParallaxCorrection; }
  78. void SetUseParallaxCorrection(bool useParallaxCorrection) { m_useParallaxCorrection = useParallaxCorrection; }
  79. const AZ::Uuid& GetUuid() const { return m_uuid; }
  80. // initiates the reflection probe bake and invokes the callback when the cubemap is finished rendering
  81. void Bake(RenderCubeMapCallback callback);
  82. // called by the feature processor so the probe can set the default view for the pipeline
  83. void OnRenderPipelinePassesChanged(RPI::RenderPipeline* renderPipeline);
  84. // enables or disables rendering of the visualization sphere
  85. void ShowVisualization(bool showVisualization);
  86. // the exposure to use when rendering meshes with this probe's cubemap
  87. void SetRenderExposure(float renderExposure);
  88. float GetRenderExposure() const { return m_renderExposure; }
  89. // the exposure to use when baking the probe cubemap
  90. void SetBakeExposure(float bakeExposure);
  91. float GetBakeExposure() const { return m_bakeExposure; }
  92. private:
  93. AZ_DISABLE_COPY_MOVE(ReflectionProbe);
  94. RHI::ConstPtr<RHI::DrawPacket> BuildDrawPacket(
  95. const Data::Instance<RPI::ShaderResourceGroup>& srg,
  96. const RPI::Ptr<RPI::PipelineStateForDraw>& pipelineState,
  97. const RHI::DrawListTag& drawListTag,
  98. uint32_t stencilRef);
  99. void UpdateCulling();
  100. // AZ::Data::AssetBus::Handler overrides...
  101. void OnAssetReady(Data::Asset<Data::AssetData> asset) override;
  102. void OnAssetError(Data::Asset<Data::AssetData> asset) override;
  103. // scene
  104. RPI::Scene* m_scene = nullptr;
  105. // probe volume transform
  106. AZ::Transform m_transform = AZ::Transform::CreateIdentity();
  107. // extents of the probe volume
  108. AZ::Vector3 m_outerExtents = AZ::Vector3(0.0f, 0.0f, 0.0f);
  109. AZ::Vector3 m_innerExtents = AZ::Vector3(0.0f, 0.0f, 0.0f);
  110. // probe volume OBBs (world space), built from position and extents
  111. Obb m_outerObbWs;
  112. Obb m_innerObbWs;
  113. // cubemap
  114. Data::Instance<RPI::Image> m_cubeMapImage;
  115. AZStd::string m_cubeMapRelativePath;
  116. bool m_useParallaxCorrection = false;
  117. // probe visualization
  118. AZ::Render::MeshFeatureProcessorInterface* m_meshFeatureProcessor = nullptr;
  119. Data::Asset<RPI::ModelAsset> m_visualizationModelAsset;
  120. Data::Asset<RPI::MaterialAsset> m_visualizationMaterialAsset;
  121. AZ::Render::MeshFeatureProcessorInterface::MeshHandle m_visualizationMeshHandle;
  122. // reflection rendering
  123. ReflectionRenderData* m_reflectionRenderData = nullptr;
  124. Data::Instance<RPI::ShaderResourceGroup> m_stencilSrg;
  125. Data::Instance<RPI::ShaderResourceGroup> m_blendWeightSrg;
  126. Data::Instance<RPI::ShaderResourceGroup> m_renderOuterSrg;
  127. Data::Instance<RPI::ShaderResourceGroup> m_renderInnerSrg;
  128. RHI::ConstPtr<RHI::DrawPacket> m_stencilDrawPacket;
  129. RHI::ConstPtr<RHI::DrawPacket> m_blendWeightDrawPacket;
  130. RHI::ConstPtr<RHI::DrawPacket> m_renderOuterDrawPacket;
  131. RHI::ConstPtr<RHI::DrawPacket> m_renderInnerDrawPacket;
  132. float m_renderExposure = 0.0f;
  133. float m_bakeExposure = 0.0f;
  134. bool m_updateSrg = false;
  135. const RHI::DrawItemSortKey InvalidSortKey = static_cast<RHI::DrawItemSortKey>(-1);
  136. RHI::DrawItemSortKey m_sortKey = InvalidSortKey;
  137. // culling
  138. RPI::Cullable m_cullable;
  139. AZ::Uuid m_uuid = AZ::Uuid::Create();
  140. };
  141. } // namespace Render
  142. } // namespace AZ