DiffuseProbeGridVisualizationRayTracingPass.cpp 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325
  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/Feature/RayTracing/RayTracingFeatureProcessorInterface.h>
  9. #include <Atom/RHI/CommandList.h>
  10. #include <Atom/RHI/DeviceDispatchRaysItem.h>
  11. #include <Atom/RHI/Factory.h>
  12. #include <Atom/RHI/RHISystemInterface.h>
  13. #include <Atom/RPI.Public/RenderPipeline.h>
  14. #include <Atom/RPI.Public/Scene.h>
  15. #include <Atom/RPI.Public/RPIUtils.h>
  16. #include <Atom/RPI.Public/View.h>
  17. #include <DiffuseProbeGrid_Traits_Platform.h>
  18. #include <Render/DiffuseProbeGridFeatureProcessor.h>
  19. #include <Render/DiffuseProbeGridVisualizationRayTracingPass.h>
  20. namespace AZ
  21. {
  22. namespace Render
  23. {
  24. RPI::Ptr<DiffuseProbeGridVisualizationRayTracingPass> DiffuseProbeGridVisualizationRayTracingPass::Create(const RPI::PassDescriptor& descriptor)
  25. {
  26. RPI::Ptr<DiffuseProbeGridVisualizationRayTracingPass> pass = aznew DiffuseProbeGridVisualizationRayTracingPass(descriptor);
  27. return AZStd::move(pass);
  28. }
  29. DiffuseProbeGridVisualizationRayTracingPass::DiffuseProbeGridVisualizationRayTracingPass(const RPI::PassDescriptor& descriptor)
  30. : RPI::RenderPass(descriptor)
  31. {
  32. if (RHI::RHISystemInterface::Get()->GetRayTracingSupport() == RHI::MultiDevice::NoDevices || !AZ_TRAIT_DIFFUSE_GI_PASSES_SUPPORTED)
  33. {
  34. // raytracing or GI is not supported on this platform
  35. SetEnabled(false);
  36. }
  37. }
  38. void DiffuseProbeGridVisualizationRayTracingPass::CreateRayTracingPipelineState()
  39. {
  40. // load the ray tracing shader
  41. // Note: the shader may not be available on all platforms
  42. AZStd::string shaderFilePath = "Shaders/DiffuseGlobalIllumination/DiffuseProbeGridVisualizationRayTracing.azshader";
  43. m_rayTracingShader = RPI::LoadCriticalShader(shaderFilePath);
  44. if (m_rayTracingShader == nullptr)
  45. {
  46. return;
  47. }
  48. auto shaderVariant = m_rayTracingShader->GetVariant(RPI::ShaderAsset::RootShaderVariantStableId);
  49. RHI::PipelineStateDescriptorForRayTracing rayGenerationShaderDescriptor;
  50. shaderVariant.ConfigurePipelineState(rayGenerationShaderDescriptor);
  51. // closest hit shader
  52. AZStd::string closestHitShaderFilePath = "Shaders/DiffuseGlobalIllumination/DiffuseProbeGridVisualizationRayTracingClosestHit.azshader";
  53. m_closestHitShader = RPI::LoadCriticalShader(closestHitShaderFilePath);
  54. auto closestHitShaderVariant = m_closestHitShader->GetVariant(RPI::ShaderAsset::RootShaderVariantStableId);
  55. RHI::PipelineStateDescriptorForRayTracing closestHitShaderDescriptor;
  56. closestHitShaderVariant.ConfigurePipelineState(closestHitShaderDescriptor);
  57. // miss shader
  58. AZStd::string missShaderFilePath = "Shaders/DiffuseGlobalIllumination/DiffuseProbeGridVisualizationRayTracingMiss.azshader";
  59. m_missShader = RPI::LoadCriticalShader(missShaderFilePath);
  60. auto missShaderVariant = m_missShader->GetVariant(RPI::ShaderAsset::RootShaderVariantStableId);
  61. RHI::PipelineStateDescriptorForRayTracing missShaderDescriptor;
  62. missShaderVariant.ConfigurePipelineState(missShaderDescriptor);
  63. // global pipeline state and Srg
  64. m_globalPipelineState = m_rayTracingShader->AcquirePipelineState(rayGenerationShaderDescriptor);
  65. AZ_Assert(m_globalPipelineState, "Failed to acquire ray tracing global pipeline state");
  66. m_globalSrgLayout = m_rayTracingShader->FindShaderResourceGroupLayout(Name{ "RayTracingGlobalSrg" });
  67. AZ_Assert(m_globalSrgLayout != nullptr, "Failed to find RayTracingGlobalSrg layout for shader [%s]", shaderFilePath.c_str());
  68. // build the ray tracing pipeline state descriptor
  69. RHI::RayTracingPipelineStateDescriptor descriptor;
  70. descriptor.Build()
  71. ->PipelineState(m_globalPipelineState.get())
  72. ->MaxPayloadSize(64)
  73. ->MaxAttributeSize(32)
  74. ->MaxRecursionDepth(2)
  75. ->ShaderLibrary(rayGenerationShaderDescriptor)
  76. ->RayGenerationShaderName(AZ::Name("RayGen"))
  77. ->ShaderLibrary(missShaderDescriptor)
  78. ->MissShaderName(AZ::Name("Miss"))
  79. ->ShaderLibrary(closestHitShaderDescriptor)
  80. ->ClosestHitShaderName(AZ::Name("ClosestHit"))
  81. ->HitGroup(AZ::Name("HitGroup"))
  82. ->ClosestHitShaderName(AZ::Name("ClosestHit"));
  83. // create the ray tracing pipeline state object
  84. m_rayTracingPipelineState = aznew RHI::RayTracingPipelineState;
  85. m_rayTracingPipelineState->Init(RHI::MultiDevice::AllDevices, descriptor);
  86. }
  87. bool DiffuseProbeGridVisualizationRayTracingPass::IsEnabled() const
  88. {
  89. if (!RenderPass::IsEnabled())
  90. {
  91. return false;
  92. }
  93. RPI::Scene* scene = m_pipeline->GetScene();
  94. if (!scene)
  95. {
  96. return false;
  97. }
  98. DiffuseProbeGridFeatureProcessor* diffuseProbeGridFeatureProcessor = scene->GetFeatureProcessor<DiffuseProbeGridFeatureProcessor>();
  99. if (diffuseProbeGridFeatureProcessor)
  100. {
  101. for (auto& diffuseProbeGrid : diffuseProbeGridFeatureProcessor->GetVisibleProbeGrids())
  102. {
  103. if (diffuseProbeGrid->GetVisualizationEnabled())
  104. {
  105. return true;
  106. }
  107. }
  108. }
  109. return false;
  110. }
  111. void DiffuseProbeGridVisualizationRayTracingPass::FrameBeginInternal(FramePrepareParams params)
  112. {
  113. RPI::Scene* scene = m_pipeline->GetScene();
  114. DiffuseProbeGridFeatureProcessor* diffuseProbeGridFeatureProcessor = scene->GetFeatureProcessor<DiffuseProbeGridFeatureProcessor>();
  115. if (!m_initialized)
  116. {
  117. CreateRayTracingPipelineState();
  118. m_initialized = true;
  119. }
  120. if (!m_rayTracingShaderTable)
  121. {
  122. RHI::RayTracingBufferPools& rayTracingBufferPools = diffuseProbeGridFeatureProcessor->GetVisualizationBufferPools();
  123. m_rayTracingShaderTable = aznew RHI::RayTracingShaderTable;
  124. m_rayTracingShaderTable->Init(RHI::MultiDevice::AllDevices, rayTracingBufferPools);
  125. AZStd::shared_ptr<RHI::RayTracingShaderTableDescriptor> descriptor = AZStd::make_shared<RHI::RayTracingShaderTableDescriptor>();
  126. // build the ray tracing shader table descriptor
  127. descriptor->Build(AZ::Name("RayTracingShaderTable"), m_rayTracingPipelineState)
  128. ->RayGenerationRecord(AZ::Name("RayGen"))
  129. ->MissRecord(AZ::Name("Miss"))
  130. ->HitGroupRecord(AZ::Name("HitGroup"))
  131. ;
  132. m_rayTracingShaderTable->Build(descriptor);
  133. }
  134. RenderPass::FrameBeginInternal(params);
  135. }
  136. void DiffuseProbeGridVisualizationRayTracingPass::SetupFrameGraphDependencies(RHI::FrameGraphInterface frameGraph)
  137. {
  138. RenderPass::SetupFrameGraphDependencies(frameGraph);
  139. RPI::Scene* scene = m_pipeline->GetScene();
  140. DiffuseProbeGridFeatureProcessor* diffuseProbeGridFeatureProcessor = scene->GetFeatureProcessor<DiffuseProbeGridFeatureProcessor>();
  141. frameGraph.SetEstimatedItemCount(aznumeric_cast<uint32_t>(diffuseProbeGridFeatureProcessor->GetVisibleProbeGrids().size()));
  142. for (auto& diffuseProbeGrid : diffuseProbeGridFeatureProcessor->GetVisibleProbeGrids())
  143. {
  144. if (!diffuseProbeGrid->GetVisualizationEnabled())
  145. {
  146. continue;
  147. }
  148. // TLAS
  149. {
  150. AZ::RHI::AttachmentId tlasAttachmentId = diffuseProbeGrid->GetProbeVisualizationTlasAttachmentId();
  151. const RHI::Ptr<RHI::Buffer>& visualizationTlasBuffer = diffuseProbeGrid->GetVisualizationTlas()->GetTlasBuffer();
  152. if (visualizationTlasBuffer)
  153. {
  154. if (!frameGraph.GetAttachmentDatabase().IsAttachmentValid(tlasAttachmentId))
  155. {
  156. [[maybe_unused]] RHI::ResultCode result = frameGraph.GetAttachmentDatabase().ImportBuffer(tlasAttachmentId, visualizationTlasBuffer);
  157. AZ_Assert(result == RHI::ResultCode::Success, "Failed to import ray tracing TLAS buffer with error %d", result);
  158. }
  159. uint32_t tlasBufferByteCount = aznumeric_cast<uint32_t>(visualizationTlasBuffer->GetDescriptor().m_byteCount);
  160. RHI::BufferViewDescriptor tlasBufferViewDescriptor = RHI::BufferViewDescriptor::CreateRaw(0, tlasBufferByteCount);
  161. RHI::BufferScopeAttachmentDescriptor desc;
  162. desc.m_attachmentId = tlasAttachmentId;
  163. desc.m_bufferViewDescriptor = tlasBufferViewDescriptor;
  164. desc.m_loadStoreAction.m_loadAction = AZ::RHI::AttachmentLoadAction::Load;
  165. frameGraph.UseShaderAttachment(desc, RHI::ScopeAttachmentAccess::ReadWrite, RHI::ScopeAttachmentStage::RayTracingShader);
  166. }
  167. }
  168. // grid data
  169. {
  170. RHI::BufferScopeAttachmentDescriptor desc;
  171. desc.m_attachmentId = diffuseProbeGrid->GetGridDataBufferAttachmentId();
  172. desc.m_bufferViewDescriptor = diffuseProbeGrid->GetRenderData()->m_gridDataBufferViewDescriptor;
  173. desc.m_loadStoreAction.m_loadAction = AZ::RHI::AttachmentLoadAction::Load;
  174. frameGraph.UseShaderAttachment(desc, RHI::ScopeAttachmentAccess::Read, RHI::ScopeAttachmentStage::RayTracingShader);
  175. }
  176. // probe irradiance
  177. {
  178. RHI::ImageScopeAttachmentDescriptor desc;
  179. desc.m_attachmentId = diffuseProbeGrid->GetIrradianceImageAttachmentId();
  180. desc.m_imageViewDescriptor = diffuseProbeGrid->GetRenderData()->m_probeIrradianceImageViewDescriptor;
  181. desc.m_loadStoreAction.m_loadAction = AZ::RHI::AttachmentLoadAction::Load;
  182. frameGraph.UseShaderAttachment(desc, RHI::ScopeAttachmentAccess::Read, RHI::ScopeAttachmentStage::RayTracingShader);
  183. }
  184. // probe distance
  185. {
  186. RHI::ImageScopeAttachmentDescriptor desc;
  187. desc.m_attachmentId = diffuseProbeGrid->GetDistanceImageAttachmentId();
  188. desc.m_imageViewDescriptor = diffuseProbeGrid->GetRenderData()->m_probeDistanceImageViewDescriptor;
  189. desc.m_loadStoreAction.m_loadAction = AZ::RHI::AttachmentLoadAction::Load;
  190. frameGraph.UseShaderAttachment(desc, RHI::ScopeAttachmentAccess::Read, RHI::ScopeAttachmentStage::RayTracingShader);
  191. }
  192. // probe data
  193. {
  194. RHI::ImageScopeAttachmentDescriptor desc;
  195. desc.m_attachmentId = diffuseProbeGrid->GetProbeDataImageAttachmentId();
  196. desc.m_imageViewDescriptor = diffuseProbeGrid->GetRenderData()->m_probeDataImageViewDescriptor;
  197. desc.m_loadStoreAction.m_loadAction = AZ::RHI::AttachmentLoadAction::Load;
  198. frameGraph.UseShaderAttachment(desc, RHI::ScopeAttachmentAccess::Read, RHI::ScopeAttachmentStage::RayTracingShader);
  199. }
  200. }
  201. // retrieve the visualization image size, this will determine the number of rays to cast
  202. RPI::Ptr<RPI::PassAttachment> visualizationImageAttachment = m_ownedAttachments[0];
  203. AZ_Assert(visualizationImageAttachment.get(), "Invalid DiffuseProbeGrid Visualization image");
  204. m_outputAttachmentSize = visualizationImageAttachment->GetTransientImageDescriptor().m_imageDescriptor.m_size;
  205. }
  206. void DiffuseProbeGridVisualizationRayTracingPass::CompileResources([[maybe_unused]] const RHI::FrameGraphCompileContext& context)
  207. {
  208. const RHI::ImageView* outputImageView = context.GetImageView(GetOutputBinding(0).GetAttachment()->GetAttachmentId());
  209. AZ_Assert(outputImageView, "Failed to retrieve output ImageView");
  210. RPI::Scene* scene = m_pipeline->GetScene();
  211. DiffuseProbeGridFeatureProcessor* diffuseProbeGridFeatureProcessor = scene->GetFeatureProcessor<DiffuseProbeGridFeatureProcessor>();
  212. for (auto& diffuseProbeGrid : diffuseProbeGridFeatureProcessor->GetVisibleProbeGrids())
  213. {
  214. if (!diffuseProbeGrid->GetVisualizationEnabled())
  215. {
  216. continue;
  217. }
  218. // the DiffuseProbeGridVisualization Srg must be updated in the Compile phase in order to successfully bind the ReadWrite shader
  219. // inputs (see line ValidateSetImageView() in ShaderResourceGroupData.cpp)
  220. diffuseProbeGrid->UpdateVisualizationRayTraceSrg(m_rayTracingShader, m_globalSrgLayout, outputImageView);
  221. if (!diffuseProbeGrid->GetVisualizationRayTraceSrg()->IsQueuedForCompile())
  222. {
  223. diffuseProbeGrid->GetVisualizationRayTraceSrg()->Compile();
  224. }
  225. }
  226. if (auto viewSRG = diffuseProbeGridFeatureProcessor->GetViewSrg(m_pipeline, GetPipelineViewTag()))
  227. {
  228. BindSrg(viewSRG->GetRHIShaderResourceGroup());
  229. }
  230. }
  231. void DiffuseProbeGridVisualizationRayTracingPass::BuildCommandListInternal([[maybe_unused]] const RHI::FrameGraphExecuteContext& context)
  232. {
  233. RPI::Scene* scene = m_pipeline->GetScene();
  234. DiffuseProbeGridFeatureProcessor* diffuseProbeGridFeatureProcessor = scene->GetFeatureProcessor<DiffuseProbeGridFeatureProcessor>();
  235. auto* rayTracingFeatureProcessor = scene->GetFeatureProcessor<RayTracingFeatureProcessorInterface>();
  236. AZ_Assert(rayTracingFeatureProcessor, "DiffuseProbeGridVisualizationRayTracingPass requires the RayTracingFeatureProcessor");
  237. const AZStd::vector<RPI::ViewPtr>& views = m_pipeline->GetViews(RPI::PipelineViewTag{ "MainCamera" });
  238. if (views.empty())
  239. {
  240. return;
  241. }
  242. // submit the DispatchRaysItems for each DiffuseProbeGrid in this range
  243. for (uint32_t index = context.GetSubmitRange().m_startIndex; index < context.GetSubmitRange().m_endIndex; ++index)
  244. {
  245. AZStd::shared_ptr<DiffuseProbeGrid> diffuseProbeGrid = diffuseProbeGridFeatureProcessor->GetVisibleProbeGrids()[index];
  246. if (!diffuseProbeGrid->GetVisualizationEnabled())
  247. {
  248. continue;
  249. }
  250. const RHI::DeviceShaderResourceGroup* shaderResourceGroups[] = {
  251. diffuseProbeGrid->GetVisualizationRayTraceSrg()->GetRHIShaderResourceGroup()->GetDeviceShaderResourceGroup(context.GetDeviceIndex()).get(),
  252. rayTracingFeatureProcessor->GetRayTracingSceneSrg()->GetRHIShaderResourceGroup()->GetDeviceShaderResourceGroup(context.GetDeviceIndex()).get(),
  253. views[0]->GetRHIShaderResourceGroup()->GetDeviceShaderResourceGroup(context.GetDeviceIndex()).get(),
  254. };
  255. RHI::DeviceDispatchRaysItem dispatchRaysItem;
  256. dispatchRaysItem.m_arguments.m_direct.m_width = m_outputAttachmentSize.m_width;
  257. dispatchRaysItem.m_arguments.m_direct.m_height = m_outputAttachmentSize.m_height;
  258. dispatchRaysItem.m_arguments.m_direct.m_depth = 1;
  259. dispatchRaysItem.m_rayTracingPipelineState =
  260. m_rayTracingPipelineState->GetDeviceRayTracingPipelineState(context.GetDeviceIndex()).get();
  261. dispatchRaysItem.m_rayTracingShaderTable = m_rayTracingShaderTable->GetDeviceRayTracingShaderTable(context.GetDeviceIndex()).get();
  262. dispatchRaysItem.m_shaderResourceGroupCount = RHI::ArraySize(shaderResourceGroups);
  263. dispatchRaysItem.m_shaderResourceGroups = shaderResourceGroups;
  264. dispatchRaysItem.m_globalPipelineState = m_globalPipelineState->GetDevicePipelineState(context.GetDeviceIndex()).get();
  265. // submit the DispatchRays item
  266. context.GetCommandList()->Submit(dispatchRaysItem, index);
  267. }
  268. }
  269. } // namespace RPI
  270. } // namespace AZ