ReflectionProbeFeatureProcessor.h 7.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153
  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 <Atom/Feature/ReflectionProbe/ReflectionProbeFeatureProcessorInterface.h>
  10. #include <ReflectionProbe/ReflectionProbe.h>
  11. namespace AZ
  12. {
  13. namespace Render
  14. {
  15. class ReflectionProbeFeatureProcessor final
  16. : public ReflectionProbeFeatureProcessorInterface,
  17. private Data::AssetBus::MultiHandler
  18. {
  19. public:
  20. AZ_CLASS_ALLOCATOR(ReflectionProbeFeatureProcessor, AZ::SystemAllocator)
  21. AZ_RTTI(AZ::Render::ReflectionProbeFeatureProcessor, "{A08C591F-D2AB-4550-852A-4436533DB137}", AZ::Render::ReflectionProbeFeatureProcessorInterface);
  22. static void Reflect(AZ::ReflectContext* context);
  23. ReflectionProbeFeatureProcessor() = default;
  24. virtual ~ReflectionProbeFeatureProcessor() = default;
  25. // ReflectionProbeFeatureProcessorInterface overrides
  26. ReflectionProbeHandle AddReflectionProbe(const AZ::Transform& transform, bool useParallaxCorrection) override;
  27. void RemoveReflectionProbe(ReflectionProbeHandle& handle) override;
  28. bool IsValidHandle(const ReflectionProbeHandle& handle) const override { return (m_reflectionProbeMap.find(handle) != m_reflectionProbeMap.end()); }
  29. void SetOuterExtents(const ReflectionProbeHandle& handle, const AZ::Vector3& outerExtents) override;
  30. AZ::Vector3 GetOuterExtents(const ReflectionProbeHandle& handle) const override;
  31. void SetInnerExtents(const ReflectionProbeHandle& handle, const AZ::Vector3& innerExtents) override;
  32. AZ::Vector3 GetInnerExtents(const ReflectionProbeHandle& handle) const override;
  33. AZ::Obb GetOuterObbWs(const ReflectionProbeHandle& handle) const override;
  34. AZ::Obb GetInnerObbWs(const ReflectionProbeHandle& handle) const override;
  35. void SetTransform(const ReflectionProbeHandle& handle, const AZ::Transform& transform) override;
  36. AZ::Transform GetTransform(const ReflectionProbeHandle& handle) const override;
  37. void SetCubeMap(const ReflectionProbeHandle& handle, Data::Instance<RPI::Image>& cubeMapImage, const AZStd::string& relativePath) override;
  38. Data::Instance<RPI::Image> GetCubeMap(const ReflectionProbeHandle& handle) const override;
  39. void SetRenderExposure(const ReflectionProbeHandle& handle, float renderExposure) override;
  40. float GetRenderExposure(const ReflectionProbeHandle& handle) const override;
  41. void SetBakeExposure(const ReflectionProbeHandle& handle, float bakeExposure) override;
  42. float GetBakeExposure(const ReflectionProbeHandle& handle) const override;
  43. bool GetUseParallaxCorrection(const ReflectionProbeHandle& handle) const override;
  44. void Bake(const ReflectionProbeHandle& handle, BuildCubeMapCallback callback, const AZStd::string& relativePath) override;
  45. bool CheckCubeMapAssetNotification(const AZStd::string& relativePath, Data::Asset<RPI::StreamingImageAsset>& outCubeMapAsset, CubeMapAssetNotificationType& outNotificationType) override;
  46. bool IsCubeMapReferenced(const AZStd::string& relativePath) override;
  47. void ShowVisualization(const ReflectionProbeHandle& handle, bool showVisualization) override;
  48. void FindReflectionProbes(const AZ::Vector3& position, ReflectionProbeHandleVector& reflectionProbeHandles) override;
  49. void FindReflectionProbes(const AZ::Aabb& aabb, ReflectionProbeHandleVector& reflectionProbeHandles) override;
  50. // FeatureProcessor overrides
  51. void Activate() override;
  52. void Deactivate() override;
  53. void Simulate(const FeatureProcessor::SimulatePacket& packet) override;
  54. void OnRenderEnd() override;
  55. private:
  56. AZ_DISABLE_COPY_MOVE(ReflectionProbeFeatureProcessor);
  57. // create the box vertex and index streams, which are used to render the probe volumes
  58. void CreateBoxMesh();
  59. // load the shader and retrieve pipeline state, shader, Srg Layout, and drawListTag
  60. void LoadShader(
  61. const char* filePath, RPI::Ptr<RPI::PipelineStateForDraw>& pipelineState, Data::Instance<RPI::Shader>& shader,
  62. RHI::Ptr<RHI::ShaderResourceGroupLayout>& srgLayout, RHI::DrawListTag& drawListTag);
  63. // RPI::SceneNotificationBus::Handler overrides
  64. void OnRenderPipelineChanged(AZ::RPI::RenderPipeline* pipeline, RPI::SceneNotification::RenderPipelineChangeType changeType) override;
  65. void UpdatePipelineStates();
  66. // AssetBus::MultiHandler overrides...
  67. void OnAssetReady(Data::Asset<Data::AssetData> asset) override;
  68. void OnAssetError(Data::Asset<Data::AssetData> asset) override;
  69. // notifies and removes the notification entry
  70. void HandleAssetNotification(Data::Asset<Data::AssetData> asset, CubeMapAssetNotificationType notificationType);
  71. // internal helper for FindReflectionProbes
  72. void FindReflectionProbesInternal(const AZ::Aabb& aabb, ReflectionProbeHandleVector& reflectionProbes, AZStd::function<bool(const ReflectionProbe*)> filter = {});
  73. // checks that the ReflectionProbeHandle exists in the ReflectionProbeMap
  74. bool ValidateHandle(const ReflectionProbeHandle& handle) const;
  75. // hash table of reflection probe handles for constant-time lookup
  76. using ReflectionProbePtr = AZStd::shared_ptr<ReflectionProbe>;
  77. using ReflectionProbeMap = AZStd::unordered_map <ReflectionProbeHandle, ReflectionProbePtr>;
  78. ReflectionProbeMap m_reflectionProbeMap;
  79. // list of reflection probes, sorted by size for rendering
  80. using ReflectionProbeVector = AZStd::vector<ReflectionProbePtr>;
  81. const size_t InitialProbeAllocationSize = 64;
  82. ReflectionProbeVector m_reflectionProbes;
  83. // list of cubemap assets that we need to check during Simulate() to see if they are ready
  84. struct NotifyCubeMapAssetEntry
  85. {
  86. AZStd::string m_relativePath;
  87. AZ::Data::AssetId m_assetId;
  88. Data::Asset<RPI::StreamingImageAsset> m_asset;
  89. CubeMapAssetNotificationType m_notificationType = CubeMapAssetNotificationType::None;
  90. };
  91. typedef AZStd::vector<NotifyCubeMapAssetEntry> NotifyCubeMapAssetVector;
  92. NotifyCubeMapAssetVector m_notifyCubeMapAssets;
  93. // position structure for the box vertices
  94. struct Position
  95. {
  96. float m_x = 0.0f;
  97. float m_y = 0.0f;
  98. float m_z = 0.0f;
  99. };
  100. // buffer pool for the vertex and index buffers
  101. RHI::Ptr<RHI::BufferPool> m_bufferPool;
  102. // box mesh rendering buffers
  103. // note that the position and index views are stored in ReflectionRenderData
  104. AZStd::vector<Position> m_boxPositions;
  105. AZStd::vector<uint16_t> m_boxIndices;
  106. RHI::Ptr<RHI::Buffer> m_boxPositionBuffer;
  107. RHI::Ptr<RHI::Buffer> m_boxIndexBuffer;
  108. RHI::InputStreamLayout m_boxStreamLayout;
  109. // contains the rendering data needed by reflection probes
  110. // it is loaded by the feature processor and passed to the probes to avoid loading it in each probe
  111. ReflectionRenderData m_reflectionRenderData;
  112. // flags
  113. bool m_probeSortRequired = false;
  114. bool m_meshFeatureProcessorUpdateRequired = false;
  115. bool m_needUpdatePipelineStates = false;
  116. };
  117. } // namespace Render
  118. } // namespace AZ