| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241 |
- /*
- * 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
- *
- */
- #include <Atom/RHI/Factory.h>
- #include <Atom/RHI/DevicePipelineState.h>
- #include <Atom/RHI/FrameGraphInterface.h>
- #include <Atom/RHI/FrameGraphAttachmentInterface.h>
- #include <Atom/RHI/Device.h>
- #include <Atom/RPI.Public/Pass/PassUtils.h>
- #include <Atom/RPI.Public/RenderPipeline.h>
- #include <Atom/RPI.Public/RPIUtils.h>
- #include <Atom/RPI.Reflect/Pass/PassTemplate.h>
- #include <Atom/RPI.Public/View.h>
- #include <Atom/RPI.Public/Scene.h>
- #include <DiffuseProbeGrid_Traits_Platform.h>
- #include <Render/DiffuseProbeGridFeatureProcessor.h>
- #include <Render/DiffuseProbeGridRelocationPass.h>
- #include <RayTracing/RayTracingFeatureProcessor.h>
- namespace AZ
- {
- namespace Render
- {
- RPI::Ptr<DiffuseProbeGridRelocationPass> DiffuseProbeGridRelocationPass::Create(const RPI::PassDescriptor& descriptor)
- {
- RPI::Ptr<DiffuseProbeGridRelocationPass> pass = aznew DiffuseProbeGridRelocationPass(descriptor);
- return AZStd::move(pass);
- }
- DiffuseProbeGridRelocationPass::DiffuseProbeGridRelocationPass(const RPI::PassDescriptor& descriptor)
- : RPI::RenderPass(descriptor)
- {
- if (!AZ_TRAIT_DIFFUSE_GI_PASSES_SUPPORTED)
- {
- // GI is not supported on this platform
- SetEnabled(false);
- }
- else
- {
- LoadShader();
- }
- }
- void DiffuseProbeGridRelocationPass::LoadShader()
- {
- // load shader
- // Note: the shader may not be available on all platforms
- AZStd::string shaderFilePath = "Shaders/DiffuseGlobalIllumination/DiffuseProbeGridRelocation.azshader";
- m_shader = RPI::LoadCriticalShader(shaderFilePath);
- if (m_shader == nullptr)
- {
- return;
- }
- // load pipeline state
- RHI::PipelineStateDescriptorForDispatch pipelineStateDescriptor;
- const auto& shaderVariant = m_shader->GetVariant(RPI::ShaderAsset::RootShaderVariantStableId);
- shaderVariant.ConfigurePipelineState(pipelineStateDescriptor, m_shader->GetDefaultShaderOptions());
- m_pipelineState = m_shader->AcquirePipelineState(pipelineStateDescriptor);
- // load Pass Srg asset
- m_srgLayout = m_shader->FindShaderResourceGroupLayout(RPI::SrgBindingSlot::Pass);
- // retrieve the number of threads per thread group from the shader
- const auto outcome = RPI::GetComputeShaderNumThreads(m_shader->GetAsset(), m_dispatchArgs);
- if (!outcome.IsSuccess())
- {
- AZ_Error("PassSystem", false, "[DiffuseProbeRelocationPass '%s']: Shader '%s' contains invalid numthreads arguments:\n%s", GetPathName().GetCStr(), shaderFilePath.c_str(), outcome.GetError().c_str());
- }
- }
- bool DiffuseProbeGridRelocationPass::IsEnabled() const
- {
- if (!RenderPass::IsEnabled())
- {
- return false;
- }
- RPI::Scene* scene = m_pipeline->GetScene();
- if (!scene)
- {
- return false;
- }
- RayTracingFeatureProcessor* rayTracingFeatureProcessor = scene->GetFeatureProcessor<RayTracingFeatureProcessor>();
- if (!rayTracingFeatureProcessor || !rayTracingFeatureProcessor->GetSubMeshCount())
- {
- // empty scene
- return false;
- }
- DiffuseProbeGridFeatureProcessor* diffuseProbeGridFeatureProcessor = scene->GetFeatureProcessor<DiffuseProbeGridFeatureProcessor>();
- if (!diffuseProbeGridFeatureProcessor || diffuseProbeGridFeatureProcessor->GetVisibleRealTimeProbeGrids().empty())
- {
- // no diffuse probe grids
- return false;
- }
- // check TLAS version
- uint32_t rayTracingDataRevision = rayTracingFeatureProcessor->GetRevision();
- if (rayTracingDataRevision != m_rayTracingDataRevision)
- {
- return true;
- }
- // check to see if any grids need relocation
- for (auto& diffuseProbeGrid : diffuseProbeGridFeatureProcessor->GetVisibleRealTimeProbeGrids())
- {
- if (diffuseProbeGrid->GetRemainingRelocationIterations() > 0)
- {
- return true;
- }
- }
-
- return false;
- }
- void DiffuseProbeGridRelocationPass::FrameBeginInternal(FramePrepareParams params)
- {
- RPI::Scene* scene = m_pipeline->GetScene();
- DiffuseProbeGridFeatureProcessor* diffuseProbeGridFeatureProcessor = scene->GetFeatureProcessor<DiffuseProbeGridFeatureProcessor>();
- RayTracingFeatureProcessor* rayTracingFeatureProcessor = scene->GetFeatureProcessor<RayTracingFeatureProcessor>();
- AZ_Assert(rayTracingFeatureProcessor, "DiffuseProbeGridRelocationPass requires the RayTracingFeatureProcessor");
- // reset the relocation iterations on the grids if the TLAS was updated
- uint32_t rayTracingDataRevision = rayTracingFeatureProcessor->GetRevision();
- if (rayTracingDataRevision != m_rayTracingDataRevision)
- {
- for (auto& diffuseProbeGrid : diffuseProbeGridFeatureProcessor->GetVisibleRealTimeProbeGrids())
- {
- diffuseProbeGrid->ResetRemainingRelocationIterations();
- }
- }
- m_rayTracingDataRevision = rayTracingDataRevision;
- RenderPass::FrameBeginInternal(params);
- }
- void DiffuseProbeGridRelocationPass::SetupFrameGraphDependencies(RHI::FrameGraphInterface frameGraph)
- {
- RenderPass::SetupFrameGraphDependencies(frameGraph);
- RPI::Scene* scene = m_pipeline->GetScene();
- DiffuseProbeGridFeatureProcessor* diffuseProbeGridFeatureProcessor = scene->GetFeatureProcessor<DiffuseProbeGridFeatureProcessor>();
- frameGraph.SetEstimatedItemCount(aznumeric_cast<uint32_t>(diffuseProbeGridFeatureProcessor->GetVisibleRealTimeProbeGrids().size()));
- for (auto& diffuseProbeGrid : diffuseProbeGridFeatureProcessor->GetVisibleRealTimeProbeGrids())
- {
- // grid data
- {
- RHI::BufferScopeAttachmentDescriptor desc;
- desc.m_attachmentId = diffuseProbeGrid->GetGridDataBufferAttachmentId();
- desc.m_bufferViewDescriptor = diffuseProbeGrid->GetRenderData()->m_gridDataBufferViewDescriptor;
- desc.m_loadStoreAction.m_loadAction = AZ::RHI::AttachmentLoadAction::Load;
- frameGraph.UseShaderAttachment(desc, RHI::ScopeAttachmentAccess::Read, RHI::ScopeAttachmentStage::ComputeShader);
- }
- // probe raytrace image
- {
- RHI::ImageScopeAttachmentDescriptor desc;
- desc.m_attachmentId = diffuseProbeGrid->GetRayTraceImageAttachmentId();
- desc.m_imageViewDescriptor = diffuseProbeGrid->GetRenderData()->m_probeRayTraceImageViewDescriptor;
- desc.m_loadStoreAction.m_loadAction = AZ::RHI::AttachmentLoadAction::Load;
- frameGraph.UseShaderAttachment(desc, RHI::ScopeAttachmentAccess::ReadWrite, RHI::ScopeAttachmentStage::ComputeShader);
- }
- // probe data image
- {
- RHI::ImageScopeAttachmentDescriptor desc;
- desc.m_attachmentId = diffuseProbeGrid->GetProbeDataImageAttachmentId();
- desc.m_imageViewDescriptor = diffuseProbeGrid->GetRenderData()->m_probeDataImageViewDescriptor;
- desc.m_loadStoreAction.m_loadAction = AZ::RHI::AttachmentLoadAction::Load;
- frameGraph.UseShaderAttachment(desc, RHI::ScopeAttachmentAccess::ReadWrite, RHI::ScopeAttachmentStage::ComputeShader);
- }
- }
- }
- void DiffuseProbeGridRelocationPass::CompileResources([[maybe_unused]] const RHI::FrameGraphCompileContext& context)
- {
- RPI::Scene* scene = m_pipeline->GetScene();
- DiffuseProbeGridFeatureProcessor* diffuseProbeGridFeatureProcessor = scene->GetFeatureProcessor<DiffuseProbeGridFeatureProcessor>();
- for (auto& diffuseProbeGrid : diffuseProbeGridFeatureProcessor->GetVisibleRealTimeProbeGrids())
- {
- // the diffuse probe grid Srg must be updated in the Compile phase in order to successfully bind the ReadWrite shader inputs
- // (see ValidateSetImageView() in ShaderResourceGroupData.cpp)
- diffuseProbeGrid->UpdateRelocationSrg(m_shader, m_srgLayout);
- diffuseProbeGrid->GetRelocationSrg()->Compile();
- }
- }
- void DiffuseProbeGridRelocationPass::BuildCommandListInternal(const RHI::FrameGraphExecuteContext& context)
- {
- RHI::CommandList* commandList = context.GetCommandList();
- RPI::Scene* scene = m_pipeline->GetScene();
- DiffuseProbeGridFeatureProcessor* diffuseProbeGridFeatureProcessor = scene->GetFeatureProcessor<DiffuseProbeGridFeatureProcessor>();
- // submit the DispatchItems for each DiffuseProbeGrid in this range
- for (uint32_t index = context.GetSubmitRange().m_startIndex; index < context.GetSubmitRange().m_endIndex; ++index)
- {
- AZStd::shared_ptr<DiffuseProbeGrid> diffuseProbeGrid = diffuseProbeGridFeatureProcessor->GetVisibleRealTimeProbeGrids()[index];
- const RHI::ShaderResourceGroup* shaderResourceGroup = diffuseProbeGrid->GetRelocationSrg()->GetRHIShaderResourceGroup();
- commandList->SetShaderResourceGroupForDispatch(*shaderResourceGroup->GetDeviceShaderResourceGroup(context.GetDeviceIndex()));
- RHI::DeviceDispatchItem dispatchItem;
- dispatchItem.m_arguments = m_dispatchArgs;
- dispatchItem.m_pipelineState = m_pipelineState->GetDevicePipelineState(context.GetDeviceIndex()).get();
- dispatchItem.m_arguments.m_direct.m_totalNumberOfThreadsX = AZ::DivideAndRoundUp(diffuseProbeGrid->GetTotalProbeCount(), diffuseProbeGrid->GetFrameUpdateCount());
- dispatchItem.m_arguments.m_direct.m_totalNumberOfThreadsY = 1;
- dispatchItem.m_arguments.m_direct.m_totalNumberOfThreadsZ = 1;
- commandList->Submit(dispatchItem, index);
- }
- }
- void DiffuseProbeGridRelocationPass::FrameEndInternal()
- {
- RPI::Scene* scene = m_pipeline->GetScene();
- DiffuseProbeGridFeatureProcessor* diffuseProbeGridFeatureProcessor = scene->GetFeatureProcessor<DiffuseProbeGridFeatureProcessor>();
- // submit the DispatchItems for each DiffuseProbeGrid
- for (auto& diffuseProbeGrid : diffuseProbeGridFeatureProcessor->GetVisibleRealTimeProbeGrids())
- {
- // relocation stops after a limited number of iterations
- diffuseProbeGrid->DecrementRemainingRelocationIterations();
- }
- RenderPass::FrameEndInternal();
- }
- } // namespace Render
- } // namespace AZ
|