From 7e4efa47f575b7ae4ac5056750dfacb74c29813c Mon Sep 17 00:00:00 2001 From: galibzon <66021303+galibzon@users.noreply.github.com> Date: Wed, 17 Apr 2024 14:52:25 -0500 Subject: [PATCH 1/2] Updated the "RHI/Subpass" example according to the new changes introduced by Subpass support in the RPI Signed-off-by: galibzon <66021303+galibzon@users.noreply.github.com> --- .../Source/RHI/SubpassExampleComponent.cpp | 18 ++++++++++++++++++ Gem/Code/Source/RHI/SubpassExampleComponent.h | 6 ++++++ 2 files changed, 24 insertions(+) diff --git a/Gem/Code/Source/RHI/SubpassExampleComponent.cpp b/Gem/Code/Source/RHI/SubpassExampleComponent.cpp index acd2b43f..de595130 100644 --- a/Gem/Code/Source/RHI/SubpassExampleComponent.cpp +++ b/Gem/Code/Source/RHI/SubpassExampleComponent.cpp @@ -241,6 +241,10 @@ namespace AtomSampleViewer [[maybe_unused]] RHI::ResultCode result = attachmentsBuilder.End(renderAttachmentLayout); AZ_Assert(result == RHI::ResultCode::Success, "Failed to create render attachment layout"); { + // Hold a reference to the subpass dependencies so we can give it to the FrameGraph later. + m_subpassDependencies = attachmentsBuilder.GetSubpassDependencies(); + AZ_Assert(m_subpassDependencies != nullptr, "Need subpass dependencies data"); + // GBuffer Scope Pipelines const auto& shader = m_shaders[GBufferScope]; auto& variant = shader->GetVariant(AZ::RPI::ShaderAsset::RootShaderVariantStableId); @@ -335,12 +339,15 @@ namespace AtomSampleViewer const auto prepareFunction = [this](RHI::FrameGraphInterface frameGraph, [[maybe_unused]] ScopeData& scopeData) { + frameGraph.UseSubpassDependencies(m_subpassDependencies); + // Bind the position GBuffer { RHI::ImageScopeAttachmentDescriptor descriptor; descriptor.m_attachmentId = m_positionAttachmentId; descriptor.m_loadStoreAction.m_loadAction = RHI::AttachmentLoadAction::Clear; descriptor.m_loadStoreAction.m_clearValue = RHI::ClearValue::CreateVector4Float(0.f, 0.f, 0.f, 0.f); + descriptor.m_imageViewDescriptor.m_aspectFlags = RHI::ImageAspectFlags::Color; frameGraph.UseColorAttachment(descriptor); } @@ -350,6 +357,7 @@ namespace AtomSampleViewer descriptor.m_attachmentId = m_normalAttachmentId; descriptor.m_loadStoreAction.m_loadAction = RHI::AttachmentLoadAction::Clear; descriptor.m_loadStoreAction.m_clearValue = RHI::ClearValue::CreateVector4Float(0.f, 0.f, 0.f, 0.f); + descriptor.m_imageViewDescriptor.m_aspectFlags = RHI::ImageAspectFlags::Color; frameGraph.UseColorAttachment(descriptor); } @@ -359,6 +367,7 @@ namespace AtomSampleViewer descriptor.m_attachmentId = m_albedoAttachmentId; descriptor.m_loadStoreAction.m_loadAction = RHI::AttachmentLoadAction::Clear; descriptor.m_loadStoreAction.m_clearValue = RHI::ClearValue::CreateVector4Float(0.f, 0.f, 0.f, 0.f); + descriptor.m_imageViewDescriptor.m_aspectFlags = RHI::ImageAspectFlags::Color; frameGraph.UseColorAttachment(descriptor); } @@ -367,6 +376,7 @@ namespace AtomSampleViewer RHI::ImageScopeAttachmentDescriptor descriptor; descriptor.m_attachmentId = m_outputAttachmentId; descriptor.m_loadStoreAction.m_loadAction = RHI::AttachmentLoadAction::Load; + descriptor.m_imageViewDescriptor.m_aspectFlags = RHI::ImageAspectFlags::Color; frameGraph.UseColorAttachment(descriptor); } @@ -376,6 +386,7 @@ namespace AtomSampleViewer dsDesc.m_attachmentId = m_depthStencilAttachmentId; dsDesc.m_loadStoreAction.m_clearValue = RHI::ClearValue::CreateDepthStencil(0, 0); dsDesc.m_loadStoreAction.m_loadAction = RHI::AttachmentLoadAction::Clear; + dsDesc.m_imageViewDescriptor.m_aspectFlags = RHI::ImageAspectFlags::Depth; frameGraph.UseDepthStencilAttachment(dsDesc, RHI::ScopeAttachmentAccess::Write); } @@ -442,11 +453,14 @@ namespace AtomSampleViewer const auto prepareFunction = [this](RHI::FrameGraphInterface frameGraph, [[maybe_unused]] ScopeData& scopeData) { + frameGraph.UseSubpassDependencies(m_subpassDependencies); + // Bind the position GBuffer { RHI::ImageScopeAttachmentDescriptor descriptor; descriptor.m_attachmentId = m_positionAttachmentId; descriptor.m_loadStoreAction.m_loadAction = RHI::AttachmentLoadAction::Load; + descriptor.m_imageViewDescriptor.m_aspectFlags = RHI::ImageAspectFlags::Color; frameGraph.UseSubpassInputAttachment(descriptor); } @@ -455,6 +469,7 @@ namespace AtomSampleViewer RHI::ImageScopeAttachmentDescriptor descriptor; descriptor.m_attachmentId = m_normalAttachmentId; descriptor.m_loadStoreAction.m_loadAction = RHI::AttachmentLoadAction::Load; + descriptor.m_imageViewDescriptor.m_aspectFlags = RHI::ImageAspectFlags::Color; frameGraph.UseSubpassInputAttachment(descriptor); } @@ -463,6 +478,7 @@ namespace AtomSampleViewer RHI::ImageScopeAttachmentDescriptor descriptor; descriptor.m_attachmentId = m_albedoAttachmentId; descriptor.m_loadStoreAction.m_loadAction = RHI::AttachmentLoadAction::Load; + descriptor.m_imageViewDescriptor.m_aspectFlags = RHI::ImageAspectFlags::Color; frameGraph.UseSubpassInputAttachment(descriptor); } @@ -471,6 +487,7 @@ namespace AtomSampleViewer RHI::ImageScopeAttachmentDescriptor descriptor; descriptor.m_attachmentId = m_outputAttachmentId; descriptor.m_loadStoreAction.m_loadAction = RHI::AttachmentLoadAction::Load; + descriptor.m_imageViewDescriptor.m_aspectFlags = RHI::ImageAspectFlags::Color; frameGraph.UseColorAttachment(descriptor); } @@ -479,6 +496,7 @@ namespace AtomSampleViewer RHI::ImageScopeAttachmentDescriptor dsDesc; dsDesc.m_attachmentId = m_depthStencilAttachmentId; dsDesc.m_loadStoreAction.m_loadAction = RHI::AttachmentLoadAction::Load; + dsDesc.m_imageViewDescriptor.m_aspectFlags = RHI::ImageAspectFlags::Depth; frameGraph.UseDepthStencilAttachment(dsDesc, RHI::ScopeAttachmentAccess::Read); } diff --git a/Gem/Code/Source/RHI/SubpassExampleComponent.h b/Gem/Code/Source/RHI/SubpassExampleComponent.h index 46ae037b..72505c83 100644 --- a/Gem/Code/Source/RHI/SubpassExampleComponent.h +++ b/Gem/Code/Source/RHI/SubpassExampleComponent.h @@ -126,5 +126,11 @@ namespace AtomSampleViewer AZ::Data::Instance m_compositionSubpassInputsSRG; AZ::Data::Instance m_viewShaderResourceGroup; AZ::RHI::ConstPtr m_compositionPipeline; + + //! We need to hold a reference to the Subpass Dependencies, so the dummy VkRenderPass + //! created during PSO initialization, uses the same Subpass Dependencies as the VkRenderPass + //! created by the FrameGraph. By forcing both VkRenderPasses to have the exact same subpass + //! dependencies we guarantee that they are truly compatible and this avoids validation errors. + AZStd::shared_ptr m_subpassDependencies; }; } // namespace AtomSampleViewer From 611e3fe650ead8f17dc4f0f149958c4e2eaa6d90 Mon Sep 17 00:00:00 2001 From: galibzon <66021303+galibzon@users.noreply.github.com> Date: Mon, 22 Apr 2024 15:42:51 -0500 Subject: [PATCH 2/2] Updated the RHI/Subpass example according to the new Subpass related API. Signed-off-by: galibzon <66021303+galibzon@users.noreply.github.com> --- .../Source/RHI/SubpassExampleComponent.cpp | 20 ++++++------------- Gem/Code/Source/RHI/SubpassExampleComponent.h | 7 ++----- 2 files changed, 8 insertions(+), 19 deletions(-) diff --git a/Gem/Code/Source/RHI/SubpassExampleComponent.cpp b/Gem/Code/Source/RHI/SubpassExampleComponent.cpp index de595130..9699dd0e 100644 --- a/Gem/Code/Source/RHI/SubpassExampleComponent.cpp +++ b/Gem/Code/Source/RHI/SubpassExampleComponent.cpp @@ -41,6 +41,8 @@ namespace AtomSampleViewer , m_depthStencilAttachmentId("depthAttachmentId") { m_supportRHISamplePipeline = true; + m_gbufferScopeId = AZ::RHI::ScopeId("GBufferScope"); + m_compositionScopeId = AZ::RHI::ScopeId("CompositionScope"); } void SubpassExampleComponent::Reflect(AZ::ReflectContext* context) @@ -223,14 +225,14 @@ namespace AtomSampleViewer // Build the render attachment layout with the 2 subpasses. RHI::RenderAttachmentLayoutBuilder attachmentsBuilder; // GBuffer Subpass - attachmentsBuilder.AddSubpass() + attachmentsBuilder.AddSubpass(&m_gbufferScopeId) ->RenderTargetAttachment(RHI::Format::R16G16B16A16_FLOAT, m_positionAttachmentId) ->RenderTargetAttachment(RHI::Format::R16G16B16A16_FLOAT, m_normalAttachmentId) ->RenderTargetAttachment(RHI::Format::R8G8B8A8_UNORM, m_albedoAttachmentId) ->RenderTargetAttachment(m_outputFormat, m_outputAttachmentId) ->DepthStencilAttachment(AZ::RHI::Format::D32_FLOAT, m_depthStencilAttachmentId); // Composition Subpass - attachmentsBuilder.AddSubpass() + attachmentsBuilder.AddSubpass(&m_compositionScopeId) ->SubpassInputAttachment(m_positionAttachmentId, RHI::ImageAspectFlags::Color) ->SubpassInputAttachment(m_normalAttachmentId, RHI::ImageAspectFlags::Color) ->SubpassInputAttachment(m_albedoAttachmentId, RHI::ImageAspectFlags::Color) @@ -241,10 +243,6 @@ namespace AtomSampleViewer [[maybe_unused]] RHI::ResultCode result = attachmentsBuilder.End(renderAttachmentLayout); AZ_Assert(result == RHI::ResultCode::Success, "Failed to create render attachment layout"); { - // Hold a reference to the subpass dependencies so we can give it to the FrameGraph later. - m_subpassDependencies = attachmentsBuilder.GetSubpassDependencies(); - AZ_Assert(m_subpassDependencies != nullptr, "Need subpass dependencies data"); - // GBuffer Scope Pipelines const auto& shader = m_shaders[GBufferScope]; auto& variant = shader->GetVariant(AZ::RPI::ShaderAsset::RootShaderVariantStableId); @@ -339,8 +337,6 @@ namespace AtomSampleViewer const auto prepareFunction = [this](RHI::FrameGraphInterface frameGraph, [[maybe_unused]] ScopeData& scopeData) { - frameGraph.UseSubpassDependencies(m_subpassDependencies); - // Bind the position GBuffer { RHI::ImageScopeAttachmentDescriptor descriptor; @@ -431,14 +427,13 @@ namespace AtomSampleViewer } }; - const RHI::ScopeId forwardScope("GBufferScope"); m_scopeProducers.emplace_back( aznew RHI::ScopeProducerFunction< ScopeData, decltype(prepareFunction), decltype(compileFunction), decltype(executeFunction)>( - forwardScope, + m_gbufferScopeId, ScopeData{}, prepareFunction, compileFunction, @@ -453,8 +448,6 @@ namespace AtomSampleViewer const auto prepareFunction = [this](RHI::FrameGraphInterface frameGraph, [[maybe_unused]] ScopeData& scopeData) { - frameGraph.UseSubpassDependencies(m_subpassDependencies); - // Bind the position GBuffer { RHI::ImageScopeAttachmentDescriptor descriptor; @@ -549,14 +542,13 @@ namespace AtomSampleViewer commandList->Submit(drawItem); }; - const RHI::ScopeId forwardScope("CompositionScope"); m_scopeProducers.emplace_back( aznew RHI::ScopeProducerFunction< ScopeData, decltype(prepareFunction), decltype(compileFunction), decltype(executeFunction)>( - forwardScope, + m_compositionScopeId, ScopeData{}, prepareFunction, compileFunction, diff --git a/Gem/Code/Source/RHI/SubpassExampleComponent.h b/Gem/Code/Source/RHI/SubpassExampleComponent.h index 72505c83..5d9085cf 100644 --- a/Gem/Code/Source/RHI/SubpassExampleComponent.h +++ b/Gem/Code/Source/RHI/SubpassExampleComponent.h @@ -127,10 +127,7 @@ namespace AtomSampleViewer AZ::Data::Instance m_viewShaderResourceGroup; AZ::RHI::ConstPtr m_compositionPipeline; - //! We need to hold a reference to the Subpass Dependencies, so the dummy VkRenderPass - //! created during PSO initialization, uses the same Subpass Dependencies as the VkRenderPass - //! created by the FrameGraph. By forcing both VkRenderPasses to have the exact same subpass - //! dependencies we guarantee that they are truly compatible and this avoids validation errors. - AZStd::shared_ptr m_subpassDependencies; + AZ::RHI::ScopeId m_gbufferScopeId; + AZ::RHI::ScopeId m_compositionScopeId; }; } // namespace AtomSampleViewer