Browse Source

Merge pull request #18969 from galibzon/DrawPacket_GetDrawSrg_Improvement

Improved the Mesh FP GetDrawSrg API.

GetDrawSrg() now returns a const reference and the function itself is const.

This update prevents the possibility of modifying
the content of the MeshDrawPacket::m_perDrawSrgs array.

The inspiration for this update occurred because
inadvertently I was getting crashes when using the non-const references in a way that was modifying the array of DrawSrgs.
galibzon 2 months ago
parent
commit
124e569c84

+ 2 - 2
Gems/Atom/Feature/Common/Code/Include/Atom/Feature/Mesh/MeshFeatureProcessorInterface.h

@@ -322,8 +322,8 @@ namespace AZ
             //! Returns the DrawSrg from the first DrawItem that matches @drawListTag
             //! and its material pipeline matches @materialPipelineMask.
             //! The search is done only within the DrawItems of the subMesh identifiable by the tuple (@meshHandle, @lodIndex, @subMeshIndex). 
-            virtual Data::Instance<RPI::ShaderResourceGroup>& GetDrawSrg(const MeshHandle& meshHandle, uint32_t lodIndex, uint32_t subMeshIndex,
-                RHI::DrawListTag drawListTag, RHI::DrawFilterMask materialPipelineMask) = 0;
+            virtual const Data::Instance<RPI::ShaderResourceGroup>& GetDrawSrg(const MeshHandle& meshHandle, uint32_t lodIndex, uint32_t subMeshIndex,
+                RHI::DrawListTag drawListTag, RHI::DrawFilterMask materialPipelineMask) const = 0;
 
         };
     } // namespace Render

+ 2 - 2
Gems/Atom/Feature/Common/Code/Source/Mesh/MeshFeatureProcessor.cpp

@@ -1683,9 +1683,9 @@ namespace AZ
             }
         }
 
-        Data::Instance<RPI::ShaderResourceGroup>& MeshFeatureProcessor::GetDrawSrg(const MeshHandle& meshHandle,
+        const Data::Instance<RPI::ShaderResourceGroup>& MeshFeatureProcessor::GetDrawSrg(const MeshHandle& meshHandle,
             uint32_t lodIndex, uint32_t subMeshIndex,
-            RHI::DrawListTag drawListTag, RHI::DrawFilterMask materialPipelineMask)
+            RHI::DrawListTag drawListTag, RHI::DrawFilterMask materialPipelineMask) const
         {
             if (!meshHandle.IsValid())
             {

+ 2 - 2
Gems/Atom/Feature/Common/Code/Source/Mesh/MeshFeatureProcessor.h

@@ -290,8 +290,8 @@ namespace AZ
             MeshInstanceManager& GetMeshInstanceManager();
             bool IsMeshInstancingEnabled() const;
 
-            Data::Instance<RPI::ShaderResourceGroup>& GetDrawSrg(const MeshHandle& meshHandle, uint32_t lodIndex, uint32_t subMeshIndex,
-                RHI::DrawListTag drawListTag, RHI::DrawFilterMask materialPipelineMask) override;
+            const Data::Instance<RPI::ShaderResourceGroup>& GetDrawSrg(const MeshHandle& meshHandle, uint32_t lodIndex, uint32_t subMeshIndex,
+                RHI::DrawListTag drawListTag, RHI::DrawFilterMask materialPipelineMask) const override;
 
         private:
             MeshFeatureProcessor(const MeshFeatureProcessor&) = delete;

+ 1 - 1
Gems/Atom/RPI/Code/Include/Atom/RPI.Public/MeshDrawPacket.h

@@ -88,7 +88,7 @@ namespace AZ
             void DebugOutputShaderVariants();
 
             //! Returns the DrawSrg from the DrawItem that corresponds to @drawItemIndex.
-            Data::Instance<RPI::ShaderResourceGroup>& GetDrawSrg(uint32_t drawItemIndex);
+            const Data::Instance<RPI::ShaderResourceGroup>& GetDrawSrg(uint32_t drawItemIndex) const;
 
         private:
             bool DoUpdate(const Scene& parentScene);

+ 1 - 1
Gems/Atom/RPI/Code/Source/RPI.Public/MeshDrawPacket.cpp

@@ -249,7 +249,7 @@ namespace AZ
 #endif
         }
 
-        Data::Instance<RPI::ShaderResourceGroup>& MeshDrawPacket::GetDrawSrg(uint32_t drawItemIndex)
+        const Data::Instance<RPI::ShaderResourceGroup>& MeshDrawPacket::GetDrawSrg(uint32_t drawItemIndex) const
         {
             if (drawItemIndex >= aznumeric_cast<uint32_t>(m_perDrawSrgs.size()))
             {