DecalTextureArrayFeatureProcessor.h 7.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174
  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/Utils/GpuBufferHandler.h>
  10. #include <Atom/Feature/Utils/IndexedDataVector.h>
  11. #include <Atom/Feature/Decals/DecalFeatureProcessorInterface.h>
  12. #include <Atom/Feature/Utils/MultiIndexedDataVector.h>
  13. #include <Atom/RPI.Reflect/Image/ImageAsset.h>
  14. #include <Atom/RPI.Public/Image/StreamingImage.h>
  15. #include <AtomCore/Instance/Instance.h>
  16. #include <Atom/Feature/Utils/IndexableList.h>
  17. #include <AzCore/Math/Sphere.h>
  18. #include <Decals/DecalTextureArray.h>
  19. #include <Decals/AsyncLoadTracker.h>
  20. namespace AZ
  21. {
  22. namespace RPI
  23. {
  24. class Material;
  25. class Buffer;
  26. }
  27. namespace Render
  28. {
  29. class DecalTextureArrayFeatureProcessor final
  30. : public AZ::Render::DecalFeatureProcessorInterface
  31. , public Data::AssetBus::MultiHandler
  32. {
  33. public:
  34. AZ_CLASS_ALLOCATOR(DecalTextureArrayFeatureProcessor, AZ::SystemAllocator)
  35. AZ_RTTI(AZ::Render::DecalTextureArrayFeatureProcessor, "{5E8365FA-BEA7-4D02-9A5C-67E6810D5465}", AZ::Render::DecalFeatureProcessorInterface);
  36. static void Reflect(AZ::ReflectContext* context);
  37. DecalTextureArrayFeatureProcessor() = default;
  38. virtual ~DecalTextureArrayFeatureProcessor() = default;
  39. DecalTextureArrayFeatureProcessor(const DecalTextureArrayFeatureProcessor&) = delete;
  40. DecalTextureArrayFeatureProcessor& operator=(const DecalTextureArrayFeatureProcessor&) = delete;
  41. // FeatureProcessor overrides ...
  42. void Activate() override;
  43. void Deactivate() override;
  44. void Simulate(const RPI::FeatureProcessor::SimulatePacket& packet) override;
  45. void Render(const RPI::FeatureProcessor::RenderPacket& packet) override;
  46. DecalHandle AcquireDecal() override;
  47. bool ReleaseDecal(const DecalHandle handle) override;
  48. DecalHandle CloneDecal(const DecalHandle handle) override;
  49. void SetDecalData(const DecalHandle handle, const DecalData& data) override;
  50. const Data::Instance<RPI::Buffer> GetDecalBuffer() const override;
  51. uint32_t GetDecalCount() const override;
  52. //! Sets the position of the decal
  53. void SetDecalPosition(const DecalHandle handle, const AZ::Vector3& position) override;
  54. //! Sets the color of the decal
  55. void SetDecalColor(const DecalHandle handle, const AZ::Vector3& color) override;
  56. //! Sets the factor for the decal color
  57. void SetDecalColorFactor(const DecalHandle handle, float colorFactor) override;
  58. //! Sets the orientation of the decal
  59. void SetDecalOrientation(const DecalHandle handle, const AZ::Quaternion& orientation) override;
  60. //! Sets the half size of the decal
  61. void SetDecalHalfSize(const DecalHandle handle, const Vector3& size) override;
  62. //! Sets the angle attenuation of the decal. Increasing this increases the transparency as the angle between the decal and geometry gets larger.
  63. void SetDecalAttenuationAngle(const DecalHandle handle, float angleAttenuation) override;
  64. //! Sets the opacity of the decal
  65. void SetDecalOpacity(const DecalHandle handle, float opacity) override;
  66. //! Sets the opacity of the normal map of the decal
  67. void SetDecalNormalMapOpacity(const DecalHandle handle, float opacity) override;
  68. //! Sets the decal sort key. Decals with a larger sort key appear over top of smaller sort keys.
  69. void SetDecalSortKey(const DecalHandle handle, uint8_t sortKey) override;
  70. //! Sets the transform of the decal
  71. //! Equivalent to calling SetDecalPosition() + SetDecalOrientation() + SetDecalHalfSize()
  72. //! @{
  73. void SetDecalTransform(const DecalHandle handle, const AZ::Transform& world) override;
  74. void SetDecalTransform(const DecalHandle handle, const AZ::Transform& world, const AZ::Vector3& nonUniformScale) override;
  75. //! @}
  76. //! Sets the material information for this decal
  77. void SetDecalMaterial(const DecalHandle handle, const AZ::Data::AssetId id) override;
  78. // SceneNotificationBus::Handler overrides...
  79. void OnRenderPipelinePersistentViewChanged(
  80. RPI::RenderPipeline* renderPipeline,
  81. RPI::PipelineViewTag viewTag,
  82. RPI::ViewPtr newView,
  83. RPI::ViewPtr previousView) override;
  84. private:
  85. // Number of size and format permutations
  86. // This number should match the number of texture arrays in Decals/ViewSrg.azsli
  87. static constexpr int NumTextureArrays = 5;
  88. static constexpr const char* FeatureProcessorName = "DecalTextureArrayFeatureProcessor";
  89. struct DecalLocation
  90. {
  91. int textureArrayIndex = -1;
  92. int textureIndex = -1;
  93. };
  94. struct DecalLocationAndUseCount
  95. {
  96. DecalLocation m_location;
  97. int m_useCount = 0;
  98. };
  99. void OnAssetReady(Data::Asset<Data::AssetData> asset) override;
  100. void SetPackedTexturesToSrg(const RPI::ViewPtr& view);
  101. void CacheShaderIndices();
  102. // This call could fail (returning nullopt) if we run out of texture arrays
  103. AZStd::optional<DecalLocation> AddMaterialToTextureArrays(AZ::RPI::MaterialAsset* materialAsset);
  104. int FindTextureArrayWithSize(const RHI::Size& size) const;
  105. void RemoveMaterialFromDecal(const uint16_t decalIndex);
  106. void SetDecalTextureLocation(const DecalHandle& handle, const DecalLocation location);
  107. void QueueMaterialLoadForDecal(const AZ::Data::AssetId material, const DecalHandle handle);
  108. bool RemoveDecalFromTextureArrays(const DecalLocation decalLocation);
  109. AZ::Data::AssetId GetMaterialUsedByDecal(const DecalHandle handle) const;
  110. void PackTexureArrays();
  111. void SetMaterialToDecals(RPI::MaterialAsset* materialAsset, const AZStd::vector<DecalHandle>& decalsThatUseThisMaterial);
  112. // Cull the decals for a view using the CPU.
  113. void CullDecals(const RPI::ViewPtr& view);
  114. void UpdateBounds(const DecalHandle handle);
  115. MultiIndexedDataVector<DecalData, AZ::Aabb> m_decalData;
  116. RHI::Handle<uint32_t> m_decalMeshFlag;
  117. // Texture arrays are organized one per texture size permutation.
  118. // e.g. There may be a situation where we have 3 texture arrays:
  119. // 24 textures @ 128x128
  120. // 16 textures @ 256x256
  121. // 4 textures @ 512x512
  122. IndexableList < AZStd::pair < AZ::RHI::Size, DecalTextureArray>> m_textureArrayList;
  123. AZStd::array<AZStd::array<RHI::ShaderInputImageIndex, DecalMapType_Num>, NumTextureArrays> m_decalTextureArrayIndices;
  124. GpuBufferHandler m_decalBufferHandler;
  125. AsyncLoadTracker<DecalHandle> m_materialLoadTracker;
  126. AZStd::unordered_map< AZ::Data::AssetId, DecalLocationAndUseCount> m_materialToTextureArrayLookupTable;
  127. bool m_deviceBufferNeedsUpdate = false;
  128. // Handlers to GPU buffer that are being used for CPU culling visibility.
  129. AZStd::vector<GpuBufferHandler> m_visibleDecalBufferHandlers;
  130. // Number of buffers being used for visibility in the current frame.
  131. uint32_t m_visibleDecalBufferUsedCount = 0;
  132. // Views that have a GPU culling pass per render pipeline.
  133. AZStd::unordered_map<const RPI::View*, AZStd::vector<const RPI::RenderPipeline*>> m_cpuCulledPipelinesPerView;
  134. };
  135. } // namespace Render
  136. } // namespace AZ