diff --git a/Gems/Atom/RHI/Code/Include/Atom/RHI/ShaderResourceGroupData.h b/Gems/Atom/RHI/Code/Include/Atom/RHI/ShaderResourceGroupData.h index 0ce43159f6..cbb0db53c8 100644 --- a/Gems/Atom/RHI/Code/Include/Atom/RHI/ShaderResourceGroupData.h +++ b/Gems/Atom/RHI/Code/Include/Atom/RHI/ShaderResourceGroupData.h @@ -166,6 +166,10 @@ namespace AZ AZStd::array_view> GetImageGroup() const; AZStd::array_view> GetBufferGroup() const; AZStd::array_view GetSamplerGroup() const; + + //! Reset image and buffer views setup for this ShaderResourceGroupData + //! So it won't hold references for any RHI resources + void ResetViews(); //! Returns the opaque constant data populated by calls to SetConstant and SetConstantData. //! diff --git a/Gems/Atom/RHI/Code/Source/RHI/ShaderResourceGroupData.cpp b/Gems/Atom/RHI/Code/Source/RHI/ShaderResourceGroupData.cpp index da7697d8f2..18b292ac64 100644 --- a/Gems/Atom/RHI/Code/Source/RHI/ShaderResourceGroupData.cpp +++ b/Gems/Atom/RHI/Code/Source/RHI/ShaderResourceGroupData.cpp @@ -330,6 +330,14 @@ namespace AZ return m_samplers; } + void ShaderResourceGroupData::ResetViews() + { + m_imageViews.assign(m_imageViews.size(), nullptr); + m_bufferViews.assign(m_bufferViews.size(), nullptr); + m_imageViewsUnboundedArray.assign(m_imageViewsUnboundedArray.size(), nullptr); + m_bufferViewsUnboundedArray.assign(m_bufferViewsUnboundedArray.size(), nullptr); + } + AZStd::array_view ShaderResourceGroupData::GetConstantData() const { return m_constantsData.GetConstantData(); diff --git a/Gems/Atom/RPI/Code/Include/Atom/RPI.Public/DynamicDraw/DynamicDrawContext.h b/Gems/Atom/RPI/Code/Include/Atom/RPI.Public/DynamicDraw/DynamicDrawContext.h index 93e595243e..c35d0750a5 100644 --- a/Gems/Atom/RPI/Code/Include/Atom/RPI.Public/DynamicDraw/DynamicDrawContext.h +++ b/Gems/Atom/RPI/Code/Include/Atom/RPI.Public/DynamicDraw/DynamicDrawContext.h @@ -268,6 +268,8 @@ namespace AZ AZStd::vector m_cachedStreamBufferViews; AZStd::vector m_cachedIndexBufferViews; AZStd::vector> m_cachedDrawSrg; + + uint32_t m_nextDrawSrgIdx = 0; // structure includes DrawItem and stream and index buffer index using BufferViewIndexType = uint32_t; diff --git a/Gems/Atom/RPI/Code/Include/Atom/RPI.Public/Shader/ShaderResourceGroup.h b/Gems/Atom/RPI/Code/Include/Atom/RPI.Public/Shader/ShaderResourceGroup.h index b2cea448c1..99f1f5f598 100644 --- a/Gems/Atom/RPI/Code/Include/Atom/RPI.Public/Shader/ShaderResourceGroup.h +++ b/Gems/Atom/RPI/Code/Include/Atom/RPI.Public/Shader/ShaderResourceGroup.h @@ -133,6 +133,9 @@ namespace AZ /// Returns an array of RPI buffers associated with the buffer shader input index. AZStd::array_view> GetBufferArray(RHI::ShaderInputNameIndex& inputIndex) const; AZStd::array_view> GetBufferArray(RHI::ShaderInputBufferIndex inputIndex) const; + + //! Reset image and buffer views so that it won't hold references for any RHI resources + void ResetViews(); ////////////////////////////////////////////////////////////////////////// // Methods for assignment / access of RHI Image types. diff --git a/Gems/Atom/RPI/Code/Source/RPI.Public/DynamicDraw/DynamicDrawContext.cpp b/Gems/Atom/RPI/Code/Source/RPI.Public/DynamicDraw/DynamicDrawContext.cpp index 4da85823b3..3b3473a2da 100644 --- a/Gems/Atom/RPI/Code/Source/RPI.Public/DynamicDraw/DynamicDrawContext.cpp +++ b/Gems/Atom/RPI/Code/Source/RPI.Public/DynamicDraw/DynamicDrawContext.cpp @@ -518,7 +518,6 @@ namespace AZ if (drawSrg) { drawItem.m_uniqueShaderResourceGroup = drawSrg->GetRHIShaderResourceGroup(); - m_cachedDrawSrg.push_back(drawSrg); } // Set scissor per draw if scissor is enabled. @@ -608,7 +607,6 @@ namespace AZ if (drawSrg) { drawItem.m_uniqueShaderResourceGroup = drawSrg->GetRHIShaderResourceGroup(); - m_cachedDrawSrg.push_back(drawSrg); } // Set scissor per draw if scissor is enabled. @@ -635,7 +633,22 @@ namespace AZ { return nullptr; } - auto drawSrg = AZ::RPI::ShaderResourceGroup::Create(m_shader->GetAsset(), m_shader->GetSupervariantIndex(), m_drawSrgLayout->GetName()); + + Data::Instance drawSrg; + if (m_nextDrawSrgIdx == m_cachedDrawSrg.size()) + { + drawSrg = AZ::RPI::ShaderResourceGroup::Create(m_shader->GetAsset(), m_shader->GetSupervariantIndex(), m_drawSrgLayout->GetName()); + m_cachedDrawSrg.push_back(drawSrg); + } + else if (m_nextDrawSrgIdx < m_cachedDrawSrg.size()) + { + drawSrg = m_cachedDrawSrg[m_nextDrawSrgIdx]; + } + else + { + AZ_Assert(false, "Unexpected next draw srg index"); + } + m_nextDrawSrgIdx++; // Set fallback value for shader variant if draw srg contains constant for shader variant fallback if (m_hasShaderVariantKeyFallbackEntry) @@ -727,7 +740,7 @@ namespace AZ } for (auto& drawItemProperties : m_cachedDrawList) - { + { view->AddDrawItem(m_drawListTag, drawItemProperties); } } @@ -743,9 +756,14 @@ namespace AZ m_cachedDrawItems.clear(); m_cachedStreamBufferViews.clear(); m_cachedIndexBufferViews.clear(); - m_cachedDrawSrg.clear(); m_cachedDrawList.clear(); + m_nextDrawSrgIdx = 0; m_drawFinalized = false; + + for (auto srg:m_cachedDrawSrg) + { + srg->ResetViews(); + } } const RHI::PipelineState* DynamicDrawContext::GetCurrentPipelineState() diff --git a/Gems/Atom/RPI/Code/Source/RPI.Public/Shader/ShaderResourceGroup.cpp b/Gems/Atom/RPI/Code/Source/RPI.Public/Shader/ShaderResourceGroup.cpp index 499365a9a9..f95c6abfcd 100644 --- a/Gems/Atom/RPI/Code/Source/RPI.Public/Shader/ShaderResourceGroup.cpp +++ b/Gems/Atom/RPI/Code/Source/RPI.Public/Shader/ShaderResourceGroup.cpp @@ -580,6 +580,11 @@ namespace AZ return {}; } + void ShaderResourceGroup::ResetViews() + { + m_data.ResetViews(); + } + const RHI::SamplerState& ShaderResourceGroup::GetSampler(RHI::ShaderInputNameIndex& inputIndex, uint32_t arrayIndex) const { inputIndex.ValidateOrFindSamplerIndex(GetLayout());