DiffuseProbeGridRelocationPass.cpp 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241
  1. /*
  2. * Copyright (c) Contributors to the Open 3D Engine Project.
  3. * For complete copyright and license terms please see the LICENSE at the root of this distribution.
  4. *
  5. * SPDX-License-Identifier: Apache-2.0 OR MIT
  6. *
  7. */
  8. #include <Atom/RHI/Factory.h>
  9. #include <Atom/RHI/DevicePipelineState.h>
  10. #include <Atom/RHI/FrameGraphInterface.h>
  11. #include <Atom/RHI/FrameGraphAttachmentInterface.h>
  12. #include <Atom/RHI/Device.h>
  13. #include <Atom/RPI.Public/Pass/PassUtils.h>
  14. #include <Atom/RPI.Public/RenderPipeline.h>
  15. #include <Atom/RPI.Public/RPIUtils.h>
  16. #include <Atom/RPI.Reflect/Pass/PassTemplate.h>
  17. #include <Atom/RPI.Public/View.h>
  18. #include <Atom/RPI.Public/Scene.h>
  19. #include <DiffuseProbeGrid_Traits_Platform.h>
  20. #include <Render/DiffuseProbeGridFeatureProcessor.h>
  21. #include <Render/DiffuseProbeGridRelocationPass.h>
  22. #include <RayTracing/RayTracingFeatureProcessor.h>
  23. namespace AZ
  24. {
  25. namespace Render
  26. {
  27. RPI::Ptr<DiffuseProbeGridRelocationPass> DiffuseProbeGridRelocationPass::Create(const RPI::PassDescriptor& descriptor)
  28. {
  29. RPI::Ptr<DiffuseProbeGridRelocationPass> pass = aznew DiffuseProbeGridRelocationPass(descriptor);
  30. return AZStd::move(pass);
  31. }
  32. DiffuseProbeGridRelocationPass::DiffuseProbeGridRelocationPass(const RPI::PassDescriptor& descriptor)
  33. : RPI::RenderPass(descriptor)
  34. {
  35. if (!AZ_TRAIT_DIFFUSE_GI_PASSES_SUPPORTED)
  36. {
  37. // GI is not supported on this platform
  38. SetEnabled(false);
  39. }
  40. else
  41. {
  42. LoadShader();
  43. }
  44. }
  45. void DiffuseProbeGridRelocationPass::LoadShader()
  46. {
  47. // load shader
  48. // Note: the shader may not be available on all platforms
  49. AZStd::string shaderFilePath = "Shaders/DiffuseGlobalIllumination/DiffuseProbeGridRelocation.azshader";
  50. m_shader = RPI::LoadCriticalShader(shaderFilePath);
  51. if (m_shader == nullptr)
  52. {
  53. return;
  54. }
  55. // load pipeline state
  56. RHI::PipelineStateDescriptorForDispatch pipelineStateDescriptor;
  57. const auto& shaderVariant = m_shader->GetVariant(RPI::ShaderAsset::RootShaderVariantStableId);
  58. shaderVariant.ConfigurePipelineState(pipelineStateDescriptor, m_shader->GetDefaultShaderOptions());
  59. m_pipelineState = m_shader->AcquirePipelineState(pipelineStateDescriptor);
  60. // load Pass Srg asset
  61. m_srgLayout = m_shader->FindShaderResourceGroupLayout(RPI::SrgBindingSlot::Pass);
  62. // retrieve the number of threads per thread group from the shader
  63. const auto outcome = RPI::GetComputeShaderNumThreads(m_shader->GetAsset(), m_dispatchArgs);
  64. if (!outcome.IsSuccess())
  65. {
  66. AZ_Error("PassSystem", false, "[DiffuseProbeRelocationPass '%s']: Shader '%s' contains invalid numthreads arguments:\n%s", GetPathName().GetCStr(), shaderFilePath.c_str(), outcome.GetError().c_str());
  67. }
  68. }
  69. bool DiffuseProbeGridRelocationPass::IsEnabled() const
  70. {
  71. if (!RenderPass::IsEnabled())
  72. {
  73. return false;
  74. }
  75. RPI::Scene* scene = m_pipeline->GetScene();
  76. if (!scene)
  77. {
  78. return false;
  79. }
  80. RayTracingFeatureProcessor* rayTracingFeatureProcessor = scene->GetFeatureProcessor<RayTracingFeatureProcessor>();
  81. if (!rayTracingFeatureProcessor || !rayTracingFeatureProcessor->GetSubMeshCount())
  82. {
  83. // empty scene
  84. return false;
  85. }
  86. DiffuseProbeGridFeatureProcessor* diffuseProbeGridFeatureProcessor = scene->GetFeatureProcessor<DiffuseProbeGridFeatureProcessor>();
  87. if (!diffuseProbeGridFeatureProcessor || diffuseProbeGridFeatureProcessor->GetVisibleRealTimeProbeGrids().empty())
  88. {
  89. // no diffuse probe grids
  90. return false;
  91. }
  92. // check TLAS version
  93. uint32_t rayTracingDataRevision = rayTracingFeatureProcessor->GetRevision();
  94. if (rayTracingDataRevision != m_rayTracingDataRevision)
  95. {
  96. return true;
  97. }
  98. // check to see if any grids need relocation
  99. for (auto& diffuseProbeGrid : diffuseProbeGridFeatureProcessor->GetVisibleRealTimeProbeGrids())
  100. {
  101. if (diffuseProbeGrid->GetRemainingRelocationIterations() > 0)
  102. {
  103. return true;
  104. }
  105. }
  106. return false;
  107. }
  108. void DiffuseProbeGridRelocationPass::FrameBeginInternal(FramePrepareParams params)
  109. {
  110. RPI::Scene* scene = m_pipeline->GetScene();
  111. DiffuseProbeGridFeatureProcessor* diffuseProbeGridFeatureProcessor = scene->GetFeatureProcessor<DiffuseProbeGridFeatureProcessor>();
  112. RayTracingFeatureProcessor* rayTracingFeatureProcessor = scene->GetFeatureProcessor<RayTracingFeatureProcessor>();
  113. AZ_Assert(rayTracingFeatureProcessor, "DiffuseProbeGridRelocationPass requires the RayTracingFeatureProcessor");
  114. // reset the relocation iterations on the grids if the TLAS was updated
  115. uint32_t rayTracingDataRevision = rayTracingFeatureProcessor->GetRevision();
  116. if (rayTracingDataRevision != m_rayTracingDataRevision)
  117. {
  118. for (auto& diffuseProbeGrid : diffuseProbeGridFeatureProcessor->GetVisibleRealTimeProbeGrids())
  119. {
  120. diffuseProbeGrid->ResetRemainingRelocationIterations();
  121. }
  122. }
  123. m_rayTracingDataRevision = rayTracingDataRevision;
  124. RenderPass::FrameBeginInternal(params);
  125. }
  126. void DiffuseProbeGridRelocationPass::SetupFrameGraphDependencies(RHI::FrameGraphInterface frameGraph)
  127. {
  128. RenderPass::SetupFrameGraphDependencies(frameGraph);
  129. RPI::Scene* scene = m_pipeline->GetScene();
  130. DiffuseProbeGridFeatureProcessor* diffuseProbeGridFeatureProcessor = scene->GetFeatureProcessor<DiffuseProbeGridFeatureProcessor>();
  131. frameGraph.SetEstimatedItemCount(aznumeric_cast<uint32_t>(diffuseProbeGridFeatureProcessor->GetVisibleRealTimeProbeGrids().size()));
  132. for (auto& diffuseProbeGrid : diffuseProbeGridFeatureProcessor->GetVisibleRealTimeProbeGrids())
  133. {
  134. // grid data
  135. {
  136. RHI::BufferScopeAttachmentDescriptor desc;
  137. desc.m_attachmentId = diffuseProbeGrid->GetGridDataBufferAttachmentId();
  138. desc.m_bufferViewDescriptor = diffuseProbeGrid->GetRenderData()->m_gridDataBufferViewDescriptor;
  139. desc.m_loadStoreAction.m_loadAction = AZ::RHI::AttachmentLoadAction::Load;
  140. frameGraph.UseShaderAttachment(desc, RHI::ScopeAttachmentAccess::Read, RHI::ScopeAttachmentStage::ComputeShader);
  141. }
  142. // probe raytrace image
  143. {
  144. RHI::ImageScopeAttachmentDescriptor desc;
  145. desc.m_attachmentId = diffuseProbeGrid->GetRayTraceImageAttachmentId();
  146. desc.m_imageViewDescriptor = diffuseProbeGrid->GetRenderData()->m_probeRayTraceImageViewDescriptor;
  147. desc.m_loadStoreAction.m_loadAction = AZ::RHI::AttachmentLoadAction::Load;
  148. frameGraph.UseShaderAttachment(desc, RHI::ScopeAttachmentAccess::ReadWrite, RHI::ScopeAttachmentStage::ComputeShader);
  149. }
  150. // probe data image
  151. {
  152. RHI::ImageScopeAttachmentDescriptor desc;
  153. desc.m_attachmentId = diffuseProbeGrid->GetProbeDataImageAttachmentId();
  154. desc.m_imageViewDescriptor = diffuseProbeGrid->GetRenderData()->m_probeDataImageViewDescriptor;
  155. desc.m_loadStoreAction.m_loadAction = AZ::RHI::AttachmentLoadAction::Load;
  156. frameGraph.UseShaderAttachment(desc, RHI::ScopeAttachmentAccess::ReadWrite, RHI::ScopeAttachmentStage::ComputeShader);
  157. }
  158. }
  159. }
  160. void DiffuseProbeGridRelocationPass::CompileResources([[maybe_unused]] const RHI::FrameGraphCompileContext& context)
  161. {
  162. RPI::Scene* scene = m_pipeline->GetScene();
  163. DiffuseProbeGridFeatureProcessor* diffuseProbeGridFeatureProcessor = scene->GetFeatureProcessor<DiffuseProbeGridFeatureProcessor>();
  164. for (auto& diffuseProbeGrid : diffuseProbeGridFeatureProcessor->GetVisibleRealTimeProbeGrids())
  165. {
  166. // the diffuse probe grid Srg must be updated in the Compile phase in order to successfully bind the ReadWrite shader inputs
  167. // (see ValidateSetImageView() in ShaderResourceGroupData.cpp)
  168. diffuseProbeGrid->UpdateRelocationSrg(m_shader, m_srgLayout);
  169. diffuseProbeGrid->GetRelocationSrg()->Compile();
  170. }
  171. }
  172. void DiffuseProbeGridRelocationPass::BuildCommandListInternal(const RHI::FrameGraphExecuteContext& context)
  173. {
  174. RHI::CommandList* commandList = context.GetCommandList();
  175. RPI::Scene* scene = m_pipeline->GetScene();
  176. DiffuseProbeGridFeatureProcessor* diffuseProbeGridFeatureProcessor = scene->GetFeatureProcessor<DiffuseProbeGridFeatureProcessor>();
  177. // submit the DispatchItems for each DiffuseProbeGrid in this range
  178. for (uint32_t index = context.GetSubmitRange().m_startIndex; index < context.GetSubmitRange().m_endIndex; ++index)
  179. {
  180. AZStd::shared_ptr<DiffuseProbeGrid> diffuseProbeGrid = diffuseProbeGridFeatureProcessor->GetVisibleRealTimeProbeGrids()[index];
  181. const RHI::ShaderResourceGroup* shaderResourceGroup = diffuseProbeGrid->GetRelocationSrg()->GetRHIShaderResourceGroup();
  182. commandList->SetShaderResourceGroupForDispatch(*shaderResourceGroup->GetDeviceShaderResourceGroup(context.GetDeviceIndex()));
  183. RHI::DeviceDispatchItem dispatchItem;
  184. dispatchItem.m_arguments = m_dispatchArgs;
  185. dispatchItem.m_pipelineState = m_pipelineState->GetDevicePipelineState(context.GetDeviceIndex()).get();
  186. dispatchItem.m_arguments.m_direct.m_totalNumberOfThreadsX = AZ::DivideAndRoundUp(diffuseProbeGrid->GetTotalProbeCount(), diffuseProbeGrid->GetFrameUpdateCount());
  187. dispatchItem.m_arguments.m_direct.m_totalNumberOfThreadsY = 1;
  188. dispatchItem.m_arguments.m_direct.m_totalNumberOfThreadsZ = 1;
  189. commandList->Submit(dispatchItem, index);
  190. }
  191. }
  192. void DiffuseProbeGridRelocationPass::FrameEndInternal()
  193. {
  194. RPI::Scene* scene = m_pipeline->GetScene();
  195. DiffuseProbeGridFeatureProcessor* diffuseProbeGridFeatureProcessor = scene->GetFeatureProcessor<DiffuseProbeGridFeatureProcessor>();
  196. // submit the DispatchItems for each DiffuseProbeGrid
  197. for (auto& diffuseProbeGrid : diffuseProbeGridFeatureProcessor->GetVisibleRealTimeProbeGrids())
  198. {
  199. // relocation stops after a limited number of iterations
  200. diffuseProbeGrid->DecrementRemainingRelocationIterations();
  201. }
  202. RenderPass::FrameEndInternal();
  203. }
  204. } // namespace Render
  205. } // namespace AZ