RayTracingAmbientOcclusionPass.cpp 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272
  1. /*
  2. * 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.
  3. *
  4. * SPDX-License-Identifier: Apache-2.0 OR MIT
  5. *
  6. */
  7. #include <Passes/RayTracingAmbientOcclusionPass.h>
  8. #include <Atom/RHI/CommandList.h>
  9. #include <Atom/RHI/DispatchRaysItem.h>
  10. #include <Atom/RHI/Factory.h>
  11. #include <Atom/RHI/FrameScheduler.h>
  12. #include <Atom/RHI/RHISystemInterface.h>
  13. #include <Atom/RHI/ScopeProducerFunction.h>
  14. #include <Atom/RPI.Public/Buffer/BufferSystemInterface.h>
  15. #include <Atom/RPI.Public/Buffer/Buffer.h>
  16. #include <Atom/RPI.Public/RenderPipeline.h>
  17. #include <Atom/RPI.Public/Scene.h>
  18. #include <Atom/RPI.Public/Pass/PassUtils.h>
  19. #include <Atom/RPI.Public/RPIUtils.h>
  20. #include <Atom/RPI.Public/View.h>
  21. #include <Atom/Feature/TransformService/TransformServiceFeatureProcessor.h>
  22. namespace AZ
  23. {
  24. namespace Render
  25. {
  26. RPI::Ptr<RayTracingAmbientOcclusionPass> RayTracingAmbientOcclusionPass::Create(const RPI::PassDescriptor& descriptor)
  27. {
  28. RPI::Ptr<RayTracingAmbientOcclusionPass> pass = aznew RayTracingAmbientOcclusionPass(descriptor);
  29. return AZStd::move(pass);
  30. }
  31. RayTracingAmbientOcclusionPass::RayTracingAmbientOcclusionPass(const RPI::PassDescriptor& descriptor)
  32. : RPI::RenderPass(descriptor)
  33. {
  34. RHI::Ptr<RHI::Device> device = RHI::RHISystemInterface::Get()->GetDevice();
  35. if (device->GetFeatures().m_rayTracing == false)
  36. {
  37. // ray tracing is not supported on this platform
  38. SetEnabled(false);
  39. }
  40. }
  41. RayTracingAmbientOcclusionPass::~RayTracingAmbientOcclusionPass()
  42. {
  43. }
  44. void RayTracingAmbientOcclusionPass::CreateRayTracingPipelineState()
  45. {
  46. RHI::Ptr<RHI::Device> device = RHI::RHISystemInterface::Get()->GetDevice();
  47. // load ray generation shader
  48. const char* rayGenerationShaderFilePath = "Shaders/RayTracing/RTAOGeneration.azshader";
  49. m_rayGenerationShader = RPI::LoadShader(rayGenerationShaderFilePath);
  50. AZ_Assert(m_rayGenerationShader, "Failed to load ray generation shader");
  51. auto rayGenerationShaderVariant = m_rayGenerationShader->GetVariant(RPI::ShaderAsset::RootShaderVariantStableId);
  52. RHI::PipelineStateDescriptorForRayTracing rayGenerationShaderDescriptor;
  53. rayGenerationShaderVariant.ConfigurePipelineState(rayGenerationShaderDescriptor);
  54. // load miss shader
  55. const char* missShaderFilePath = "Shaders/RayTracing/RTAOMiss.azshader";
  56. m_missShader = RPI::LoadShader(missShaderFilePath);
  57. AZ_Assert(m_missShader, "Failed to load miss shader");
  58. auto missShaderVariant = m_missShader->GetVariant(RPI::ShaderAsset::RootShaderVariantStableId);
  59. RHI::PipelineStateDescriptorForRayTracing missShaderDescriptor;
  60. missShaderVariant.ConfigurePipelineState(missShaderDescriptor);
  61. // Load closest hit shader
  62. // This can be removed when the following issue is fixed.
  63. // [ATOM-15087] RayTracingShaderTable shouldn't report an error if there is no hit group in the descriptor
  64. const char* hitShaderFilePath = "Shaders/RayTracing/RTAOClosestHit.azshader";
  65. m_hitShader = RPI::LoadShader(hitShaderFilePath);
  66. AZ_Assert(m_hitShader, "Failed to load closest hit shader");
  67. auto hitShaderVariant = m_hitShader->GetVariant(RPI::ShaderAsset::RootShaderVariantStableId);
  68. RHI::PipelineStateDescriptorForRayTracing hitShaderDescriptor;
  69. hitShaderVariant.ConfigurePipelineState(hitShaderDescriptor);
  70. // global pipeline state
  71. m_globalPipelineState = m_rayGenerationShader->AcquirePipelineState(rayGenerationShaderDescriptor);
  72. AZ_Assert(m_globalPipelineState, "Failed to acquire ray tracing global pipeline state");
  73. //Get pass srg
  74. m_shaderResourceGroup = RPI::ShaderResourceGroup::Create(m_rayGenerationShader->GetAsset(), Name { "RayTracingGlobalSrg" });
  75. AZ_Assert(m_shaderResourceGroup, "[RayTracingAmbientOcclusionPass '%s']: Failed to create SRG from shader asset '%s'",
  76. GetPathName().GetCStr(), rayGenerationShaderFilePath);
  77. RHI::RayTracingPipelineStateDescriptor descriptor;
  78. descriptor.Build()
  79. ->PipelineState(m_globalPipelineState.get())
  80. ->ShaderLibrary(rayGenerationShaderDescriptor)
  81. ->RayGenerationShaderName(AZ::Name("AoRayGen"))
  82. ->ShaderLibrary(missShaderDescriptor)
  83. ->MissShaderName(AZ::Name("AoMiss"))
  84. ->ShaderLibrary(hitShaderDescriptor)
  85. ->ClosestHitShaderName(AZ::Name("AoClosestHit"))
  86. ->HitGroup(AZ::Name("ClosestHitGroup"))
  87. ->ClosestHitShaderName(AZ::Name("AoClosestHit"))
  88. ;
  89. // create the ray tracing pipeline state object
  90. m_rayTracingPipelineState = RHI::Factory::Get().CreateRayTracingPipelineState();
  91. m_rayTracingPipelineState->Init(*device.get(), &descriptor);
  92. }
  93. void RayTracingAmbientOcclusionPass::FrameBeginInternal(FramePrepareParams params)
  94. {
  95. if (m_createRayTracingPipelineState)
  96. {
  97. RPI::Scene* scene = m_pipeline->GetScene();
  98. m_rayTracingFeatureProcessor = scene->GetFeatureProcessor<RayTracingFeatureProcessor>();
  99. CreateRayTracingPipelineState();
  100. m_createRayTracingPipelineState = false;
  101. }
  102. if (!m_rayTracingShaderTable)
  103. {
  104. RHI::Ptr<RHI::Device> device = RHI::RHISystemInterface::Get()->GetDevice();
  105. RHI::RayTracingBufferPools& rayTracingBufferPools = m_rayTracingFeatureProcessor->GetBufferPools();
  106. // Build shader table once. Since we are not using local srg so we don't need to rebuild it even when scene changed
  107. m_rayTracingShaderTable = RHI::Factory::Get().CreateRayTracingShaderTable();
  108. m_rayTracingShaderTable->Init(*device.get(), rayTracingBufferPools);
  109. AZStd::shared_ptr<RHI::RayTracingShaderTableDescriptor> descriptor = AZStd::make_shared<RHI::RayTracingShaderTableDescriptor>();
  110. descriptor->Build(AZ::Name("RayTracingAOShaderTable"), m_rayTracingPipelineState)
  111. ->RayGenerationRecord(AZ::Name("AoRayGen"))
  112. ->MissRecord(AZ::Name("AoMiss"))
  113. ->HitGroupRecord(AZ::Name("ClosestHitGroup"))
  114. ;
  115. m_rayTracingShaderTable->Build(descriptor);
  116. }
  117. RenderPass::FrameBeginInternal(params);
  118. }
  119. void RayTracingAmbientOcclusionPass::SetupFrameGraphDependencies(RHI::FrameGraphInterface frameGraph)
  120. {
  121. RenderPass::SetupFrameGraphDependencies(frameGraph);
  122. frameGraph.SetEstimatedItemCount(1);
  123. }
  124. void RayTracingAmbientOcclusionPass::CompileResources(const RHI::FrameGraphCompileContext& context)
  125. {
  126. if (!m_shaderResourceGroup)
  127. {
  128. return;
  129. }
  130. // Bind pass attachments to global srg
  131. BindPassSrg(context, m_shaderResourceGroup);
  132. // Bind others for global srg
  133. const RHI::ShaderResourceGroupLayout* srgLayout = m_shaderResourceGroup->GetLayout();
  134. RHI::ShaderInputImageIndex imageIndex;
  135. RHI::ShaderInputBufferIndex bufferIndex;
  136. RHI::ShaderInputConstantIndex constantIndex;
  137. // Bind scene TLAS buffer
  138. const RHI::Ptr<RHI::Buffer> tlasBuffer = m_rayTracingFeatureProcessor->GetTlas()->GetTlasBuffer();
  139. if (tlasBuffer)
  140. {
  141. // TLAS
  142. uint32_t tlasBufferByteCount = aznumeric_cast<uint32_t>(tlasBuffer->GetDescriptor().m_byteCount);
  143. RHI::BufferViewDescriptor bufferViewDescriptor = RHI::BufferViewDescriptor::CreateRayTracingTLAS(tlasBufferByteCount);
  144. bufferIndex = srgLayout->FindShaderInputBufferIndex(AZ::Name("m_scene"));
  145. m_shaderResourceGroup->SetBufferView(bufferIndex, tlasBuffer->GetBufferView(bufferViewDescriptor).get());
  146. }
  147. // Bind constants
  148. // float m_aoRadius
  149. constantIndex = srgLayout->FindShaderInputConstantIndex(AZ::Name("m_aoRadius"));
  150. m_shaderResourceGroup->SetConstant(constantIndex, m_rayMaxT);
  151. // uint m_frameCount
  152. constantIndex = srgLayout->FindShaderInputConstantIndex(AZ::Name("m_frameCount"));
  153. m_shaderResourceGroup->SetConstant(constantIndex, m_frameCount++);
  154. // float m_rayMinT
  155. constantIndex = srgLayout->FindShaderInputConstantIndex(AZ::Name("m_rayMinT"));
  156. m_shaderResourceGroup->SetConstant(constantIndex, m_rayMinT);
  157. // uint m_numRays
  158. constantIndex = srgLayout->FindShaderInputConstantIndex(AZ::Name("m_numRays"));
  159. m_shaderResourceGroup->SetConstant(constantIndex, m_rayNumber);
  160. // Matrix4x4 m_viewProjectionInverseMatrix. This is the copy of same constant from ViewSrg.
  161. // Although we don't have access to ViewSrg in ray tracing shader at this moment
  162. constantIndex = srgLayout->FindShaderInputConstantIndex(AZ::Name("m_viewProjectionInverseMatrix"));
  163. const AZStd::vector<RPI::ViewPtr>& views = m_pipeline->GetViews(RPI::PipelineViewTag{"MainCamera"});
  164. Matrix4x4 clipToWorld = views[0]->GetWorldToClipMatrix();
  165. clipToWorld.InvertFull();
  166. m_shaderResourceGroup->SetConstant(constantIndex, clipToWorld);
  167. m_shaderResourceGroup->Compile();
  168. }
  169. void RayTracingAmbientOcclusionPass::BuildCommandListInternal([[maybe_unused]] const RHI::FrameGraphExecuteContext& context)
  170. {
  171. RPI::Scene* scene = m_pipeline->GetScene();
  172. RayTracingFeatureProcessor* rayTracingFeatureProcessor = scene->GetFeatureProcessor<RayTracingFeatureProcessor>();
  173. AZ_Assert(rayTracingFeatureProcessor, "RayTracingAmbientOcclusionPass requires the RayTracingFeatureProcessor");
  174. if (!rayTracingFeatureProcessor->GetSubMeshCount())
  175. {
  176. return;
  177. }
  178. if (!m_rayTracingShaderTable)
  179. {
  180. return;
  181. }
  182. RPI::PassAttachment* outputAttachment = GetOutputBinding(0).m_attachment.get();
  183. RHI::Size targetImageSize = outputAttachment->m_descriptor.m_image.m_size;
  184. const RHI::ShaderResourceGroup* shaderResourceGroups[] =
  185. {
  186. m_shaderResourceGroup->GetRHIShaderResourceGroup()
  187. };
  188. RHI::DispatchRaysItem dispatchRaysItem;
  189. dispatchRaysItem.m_width = targetImageSize.m_width;
  190. dispatchRaysItem.m_height = targetImageSize.m_height;
  191. dispatchRaysItem.m_depth = 1;
  192. dispatchRaysItem.m_rayTracingPipelineState = m_rayTracingPipelineState.get();
  193. dispatchRaysItem.m_rayTracingShaderTable = m_rayTracingShaderTable.get();
  194. dispatchRaysItem.m_shaderResourceGroupCount = 1;
  195. dispatchRaysItem.m_shaderResourceGroups = shaderResourceGroups;
  196. dispatchRaysItem.m_globalPipelineState = m_globalPipelineState.get();
  197. // submit the DispatchRays item
  198. context.GetCommandList()->Submit(dispatchRaysItem);
  199. }
  200. uint32_t RayTracingAmbientOcclusionPass::GetRayNumberPerPixel()
  201. {
  202. return m_rayNumber;
  203. }
  204. void RayTracingAmbientOcclusionPass::SetRayNumberPerPixel(uint32_t rayNumber)
  205. {
  206. m_rayNumber = rayNumber;
  207. }
  208. float RayTracingAmbientOcclusionPass::GetRayExtentMin()
  209. {
  210. return m_rayMinT;
  211. }
  212. void RayTracingAmbientOcclusionPass::SetRayExtentMin(float minT)
  213. {
  214. m_rayMinT = minT;
  215. }
  216. float RayTracingAmbientOcclusionPass::GetRayExtentMax()
  217. {
  218. return m_rayMaxT;
  219. }
  220. void RayTracingAmbientOcclusionPass::SetRayExtentMax(float maxT)
  221. {
  222. m_rayMaxT = maxT;
  223. }
  224. } // namespace RPI
  225. } // namespace AZ