3
0

DecalTextureArrayFeatureProcessor.h 6.5 KB

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