RayTracingAmbientOcclusionPass.cpp 12 KB

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