123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174 |
- /*
- * Copyright (c) Contributors to the Open 3D Engine Project.
- * For complete copyright and license terms please see the LICENSE at the root of this distribution.
- *
- * SPDX-License-Identifier: Apache-2.0 OR MIT
- *
- */
- #pragma once
- #include <Atom/Feature/Utils/GpuBufferHandler.h>
- #include <Atom/Feature/Utils/IndexedDataVector.h>
- #include <Atom/Feature/Decals/DecalFeatureProcessorInterface.h>
- #include <Atom/Feature/Utils/MultiIndexedDataVector.h>
- #include <Atom/RPI.Reflect/Image/ImageAsset.h>
- #include <Atom/RPI.Public/Image/StreamingImage.h>
- #include <AtomCore/Instance/Instance.h>
- #include <Atom/Feature/Utils/IndexableList.h>
- #include <AzCore/Math/Sphere.h>
- #include <Decals/DecalTextureArray.h>
- #include <Decals/AsyncLoadTracker.h>
- namespace AZ
- {
- namespace RPI
- {
- class Material;
- class Buffer;
- }
- namespace Render
- {
- class DecalTextureArrayFeatureProcessor final
- : public AZ::Render::DecalFeatureProcessorInterface
- , public Data::AssetBus::MultiHandler
- {
- public:
- AZ_CLASS_ALLOCATOR(DecalTextureArrayFeatureProcessor, AZ::SystemAllocator)
- AZ_RTTI(AZ::Render::DecalTextureArrayFeatureProcessor, "{5E8365FA-BEA7-4D02-9A5C-67E6810D5465}", AZ::Render::DecalFeatureProcessorInterface);
- static void Reflect(AZ::ReflectContext* context);
- DecalTextureArrayFeatureProcessor() = default;
- virtual ~DecalTextureArrayFeatureProcessor() = default;
- DecalTextureArrayFeatureProcessor(const DecalTextureArrayFeatureProcessor&) = delete;
- DecalTextureArrayFeatureProcessor& operator=(const DecalTextureArrayFeatureProcessor&) = delete;
- // FeatureProcessor overrides ...
- void Activate() override;
- void Deactivate() override;
- void Simulate(const RPI::FeatureProcessor::SimulatePacket& packet) override;
- void Render(const RPI::FeatureProcessor::RenderPacket& packet) override;
- DecalHandle AcquireDecal() override;
- bool ReleaseDecal(const DecalHandle handle) override;
- DecalHandle CloneDecal(const DecalHandle handle) override;
- void SetDecalData(const DecalHandle handle, const DecalData& data) override;
- const Data::Instance<RPI::Buffer> GetDecalBuffer() const override;
- uint32_t GetDecalCount() const override;
- //! Sets the position of the decal
- void SetDecalPosition(const DecalHandle handle, const AZ::Vector3& position) override;
- //! Sets the color of the decal
- void SetDecalColor(const DecalHandle handle, const AZ::Vector3& color) override;
- //! Sets the factor for the decal color
- void SetDecalColorFactor(const DecalHandle handle, float colorFactor) override;
- //! Sets the orientation of the decal
- void SetDecalOrientation(const DecalHandle handle, const AZ::Quaternion& orientation) override;
- //! Sets the half size of the decal
- void SetDecalHalfSize(const DecalHandle handle, const Vector3& size) override;
- //! Sets the angle attenuation of the decal. Increasing this increases the transparency as the angle between the decal and geometry gets larger.
- void SetDecalAttenuationAngle(const DecalHandle handle, float angleAttenuation) override;
- //! Sets the opacity of the decal
- void SetDecalOpacity(const DecalHandle handle, float opacity) override;
- //! Sets the opacity of the normal map of the decal
- void SetDecalNormalMapOpacity(const DecalHandle handle, float opacity) override;
- //! Sets the decal sort key. Decals with a larger sort key appear over top of smaller sort keys.
- void SetDecalSortKey(const DecalHandle handle, uint8_t sortKey) override;
- //! Sets the transform of the decal
- //! Equivalent to calling SetDecalPosition() + SetDecalOrientation() + SetDecalHalfSize()
- //! @{
- void SetDecalTransform(const DecalHandle handle, const AZ::Transform& world) override;
- void SetDecalTransform(const DecalHandle handle, const AZ::Transform& world, const AZ::Vector3& nonUniformScale) override;
- //! @}
- //! Sets the material information for this decal
- void SetDecalMaterial(const DecalHandle handle, const AZ::Data::AssetId id) override;
- // SceneNotificationBus::Handler overrides...
- void OnRenderPipelinePersistentViewChanged(
- RPI::RenderPipeline* renderPipeline,
- RPI::PipelineViewTag viewTag,
- RPI::ViewPtr newView,
- RPI::ViewPtr previousView) override;
- private:
- // Number of size and format permutations
- // This number should match the number of texture arrays in Decals/ViewSrg.azsli
- static constexpr int NumTextureArrays = 5;
- static constexpr const char* FeatureProcessorName = "DecalTextureArrayFeatureProcessor";
- struct DecalLocation
- {
- int textureArrayIndex = -1;
- int textureIndex = -1;
- };
- struct DecalLocationAndUseCount
- {
- DecalLocation m_location;
- int m_useCount = 0;
- };
- void OnAssetReady(Data::Asset<Data::AssetData> asset) override;
- void SetPackedTexturesToSrg(const RPI::ViewPtr& view);
- void CacheShaderIndices();
- // This call could fail (returning nullopt) if we run out of texture arrays
- AZStd::optional<DecalLocation> AddMaterialToTextureArrays(AZ::RPI::MaterialAsset* materialAsset);
- int FindTextureArrayWithSize(const RHI::Size& size) const;
- void RemoveMaterialFromDecal(const uint16_t decalIndex);
- void SetDecalTextureLocation(const DecalHandle& handle, const DecalLocation location);
- void QueueMaterialLoadForDecal(const AZ::Data::AssetId material, const DecalHandle handle);
- bool RemoveDecalFromTextureArrays(const DecalLocation decalLocation);
- AZ::Data::AssetId GetMaterialUsedByDecal(const DecalHandle handle) const;
- void PackTexureArrays();
- void SetMaterialToDecals(RPI::MaterialAsset* materialAsset, const AZStd::vector<DecalHandle>& decalsThatUseThisMaterial);
- // Cull the decals for a view using the CPU.
- void CullDecals(const RPI::ViewPtr& view);
- void UpdateBounds(const DecalHandle handle);
- MultiIndexedDataVector<DecalData, AZ::Aabb> m_decalData;
- RHI::Handle<uint32_t> m_decalMeshFlag;
- // Texture arrays are organized one per texture size permutation.
- // e.g. There may be a situation where we have 3 texture arrays:
- // 24 textures @ 128x128
- // 16 textures @ 256x256
- // 4 textures @ 512x512
- IndexableList < AZStd::pair < AZ::RHI::Size, DecalTextureArray>> m_textureArrayList;
- AZStd::array<AZStd::array<RHI::ShaderInputImageIndex, DecalMapType_Num>, NumTextureArrays> m_decalTextureArrayIndices;
- GpuBufferHandler m_decalBufferHandler;
- AsyncLoadTracker<DecalHandle> m_materialLoadTracker;
- AZStd::unordered_map< AZ::Data::AssetId, DecalLocationAndUseCount> m_materialToTextureArrayLookupTable;
- bool m_deviceBufferNeedsUpdate = false;
- // Handlers to GPU buffer that are being used for CPU culling visibility.
- AZStd::vector<GpuBufferHandler> m_visibleDecalBufferHandlers;
- // Number of buffers being used for visibility in the current frame.
- uint32_t m_visibleDecalBufferUsedCount = 0;
- // Views that have a GPU culling pass per render pipeline.
- AZStd::unordered_map<const RPI::View*, AZStd::vector<const RPI::RenderPipeline*>> m_cpuCulledPipelinesPerView;
- };
- } // namespace Render
- } // namespace AZ
|