RayTracingAmbientOcclusionPass.cpp 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264
  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/TransformService/TransformServiceFeatureProcessorInterface.h>
  9. #include <Atom/RHI/CommandList.h>
  10. #include <Atom/RHI/DeviceDispatchRaysItem.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/Buffer.h>
  16. #include <Atom/RPI.Public/Buffer/BufferSystemInterface.h>
  17. #include <Atom/RPI.Public/Pass/PassUtils.h>
  18. #include <Atom/RPI.Public/RPIUtils.h>
  19. #include <Atom/RPI.Public/RenderPipeline.h>
  20. #include <Atom/RPI.Public/Scene.h>
  21. #include <Atom/RPI.Public/View.h>
  22. #include <Passes/RayTracingAmbientOcclusionPass.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. // 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.m_pipelineState = m_globalPipelineState.get();
  79. descriptor.AddRayGenerationShaderLibrary(rayGenerationShaderDescriptor, Name("AoRayGen"));
  80. descriptor.AddMissShaderLibrary(missShaderDescriptor, Name("AoMiss"));
  81. descriptor.AddClosestHitShaderLibrary(hitShaderDescriptor, Name("AoClosestHit"));
  82. descriptor.AddHitGroup(Name("ClosestHitGroup"), Name("AoClosestHit"));
  83. // create the ray tracing pipeline state object
  84. m_rayTracingPipelineState = aznew RHI::RayTracingPipelineState;
  85. m_rayTracingPipelineState->Init(RHI::MultiDevice::AllDevices, descriptor);
  86. }
  87. void RayTracingAmbientOcclusionPass::FrameBeginInternal(FramePrepareParams params)
  88. {
  89. if (m_createRayTracingPipelineState)
  90. {
  91. RPI::Scene* scene = m_pipeline->GetScene();
  92. m_rayTracingFeatureProcessor = scene->GetFeatureProcessor<RayTracingFeatureProcessorInterface>();
  93. CreateRayTracingPipelineState();
  94. m_createRayTracingPipelineState = false;
  95. }
  96. if (!m_rayTracingShaderTable)
  97. {
  98. RHI::RayTracingBufferPools& rayTracingBufferPools = m_rayTracingFeatureProcessor->GetBufferPools();
  99. // Build shader table once. Since we are not using local srg so we don't need to rebuild it even when scene changed
  100. m_rayTracingShaderTable = aznew RHI::RayTracingShaderTable;
  101. m_rayTracingShaderTable->Init(RHI::MultiDevice::AllDevices, rayTracingBufferPools);
  102. AZStd::shared_ptr<RHI::RayTracingShaderTableDescriptor> descriptor = AZStd::make_shared<RHI::RayTracingShaderTableDescriptor>();
  103. descriptor->m_name = Name("RayTracingAOShaderTable");
  104. descriptor->m_rayTracingPipelineState = m_rayTracingPipelineState;
  105. descriptor->m_rayGenerationRecord.emplace_back(Name("AoRayGen"));
  106. descriptor->m_missRecords.emplace_back(Name("AoMiss"));
  107. descriptor->m_hitGroupRecords.emplace_back(Name("ClosestHitGroup"));
  108. m_rayTracingShaderTable->Build(descriptor);
  109. }
  110. RenderPass::FrameBeginInternal(params);
  111. }
  112. void RayTracingAmbientOcclusionPass::SetupFrameGraphDependencies(RHI::FrameGraphInterface frameGraph)
  113. {
  114. RenderPass::SetupFrameGraphDependencies(frameGraph);
  115. frameGraph.SetEstimatedItemCount(1);
  116. }
  117. void RayTracingAmbientOcclusionPass::CompileResources(const RHI::FrameGraphCompileContext& context)
  118. {
  119. if (!m_shaderResourceGroup)
  120. {
  121. return;
  122. }
  123. // Bind pass attachments to global srg
  124. BindPassSrg(context, m_shaderResourceGroup);
  125. // Bind others for global srg
  126. const RHI::ShaderResourceGroupLayout* srgLayout = m_shaderResourceGroup->GetLayout();
  127. RHI::ShaderInputBufferIndex bufferIndex;
  128. RHI::ShaderInputConstantIndex constantIndex;
  129. // Bind scene TLAS buffer
  130. auto tlasBuffer = m_rayTracingFeatureProcessor->GetTlas()->GetTlasBuffer();
  131. if (tlasBuffer)
  132. {
  133. // TLAS
  134. uint32_t tlasBufferByteCount = aznumeric_cast<uint32_t>(tlasBuffer->GetDescriptor().m_byteCount);
  135. RHI::BufferViewDescriptor bufferViewDescriptor = RHI::BufferViewDescriptor::CreateRayTracingTLAS(tlasBufferByteCount);
  136. bufferIndex = srgLayout->FindShaderInputBufferIndex(AZ::Name("m_scene"));
  137. m_shaderResourceGroup->SetBufferView(bufferIndex, tlasBuffer->GetBufferView(bufferViewDescriptor).get());
  138. }
  139. // Bind constants
  140. // float m_aoRadius
  141. constantIndex = srgLayout->FindShaderInputConstantIndex(AZ::Name("m_aoRadius"));
  142. m_shaderResourceGroup->SetConstant(constantIndex, m_rayMaxT);
  143. // uint m_frameCount
  144. constantIndex = srgLayout->FindShaderInputConstantIndex(AZ::Name("m_frameCount"));
  145. m_shaderResourceGroup->SetConstant(constantIndex, m_frameCount++);
  146. // float m_rayMinT
  147. constantIndex = srgLayout->FindShaderInputConstantIndex(AZ::Name("m_rayMinT"));
  148. m_shaderResourceGroup->SetConstant(constantIndex, m_rayMinT);
  149. // uint m_numRays
  150. constantIndex = srgLayout->FindShaderInputConstantIndex(AZ::Name("m_numRays"));
  151. m_shaderResourceGroup->SetConstant(constantIndex, m_rayNumber);
  152. // Matrix4x4 m_viewProjectionInverseMatrix. This is the copy of same constant from ViewSrg.
  153. // Although we don't have access to ViewSrg in ray tracing shader at this moment
  154. constantIndex = srgLayout->FindShaderInputConstantIndex(AZ::Name("m_viewProjectionInverseMatrix"));
  155. const AZStd::vector<RPI::ViewPtr>& views = m_pipeline->GetViews(RPI::PipelineViewTag{"MainCamera"});
  156. Matrix4x4 clipToWorld = views[0]->GetWorldToClipMatrix();
  157. clipToWorld.InvertFull();
  158. m_shaderResourceGroup->SetConstant(constantIndex, clipToWorld);
  159. m_shaderResourceGroup->Compile();
  160. }
  161. void RayTracingAmbientOcclusionPass::BuildCommandListInternal([[maybe_unused]] const RHI::FrameGraphExecuteContext& context)
  162. {
  163. RPI::Scene* scene = m_pipeline->GetScene();
  164. RayTracingFeatureProcessorInterface* rayTracingFeatureProcessor = scene->GetFeatureProcessor<RayTracingFeatureProcessorInterface>();
  165. AZ_Assert(rayTracingFeatureProcessor, "RayTracingAmbientOcclusionPass requires the RayTracingFeatureProcessor");
  166. if (!rayTracingFeatureProcessor->GetSubMeshCount())
  167. {
  168. return;
  169. }
  170. if (!m_rayTracingShaderTable)
  171. {
  172. return;
  173. }
  174. RPI::PassAttachment* outputAttachment = GetOutputBinding(0).GetAttachment().get();
  175. RHI::Size targetImageSize = outputAttachment->m_descriptor.m_image.m_size;
  176. const RHI::DeviceShaderResourceGroup* shaderResourceGroups[] = {
  177. m_shaderResourceGroup->GetRHIShaderResourceGroup()->GetDeviceShaderResourceGroup(context.GetDeviceIndex()).get()
  178. };
  179. RHI::DeviceDispatchRaysItem dispatchRaysItem;
  180. dispatchRaysItem.m_arguments.m_direct.m_width = targetImageSize.m_width;
  181. dispatchRaysItem.m_arguments.m_direct.m_height = targetImageSize.m_height;
  182. dispatchRaysItem.m_arguments.m_direct.m_depth = 1;
  183. dispatchRaysItem.m_rayTracingPipelineState = m_rayTracingPipelineState->GetDeviceRayTracingPipelineState(context.GetDeviceIndex()).get();
  184. dispatchRaysItem.m_rayTracingShaderTable = m_rayTracingShaderTable->GetDeviceRayTracingShaderTable(context.GetDeviceIndex()).get();
  185. dispatchRaysItem.m_shaderResourceGroupCount = 1;
  186. dispatchRaysItem.m_shaderResourceGroups = shaderResourceGroups;
  187. dispatchRaysItem.m_globalPipelineState = m_globalPipelineState->GetDevicePipelineState(context.GetDeviceIndex()).get();
  188. // submit the DispatchRays item
  189. context.GetCommandList()->Submit(dispatchRaysItem);
  190. }
  191. uint32_t RayTracingAmbientOcclusionPass::GetRayNumberPerPixel()
  192. {
  193. return m_rayNumber;
  194. }
  195. void RayTracingAmbientOcclusionPass::SetRayNumberPerPixel(uint32_t rayNumber)
  196. {
  197. m_rayNumber = rayNumber;
  198. }
  199. float RayTracingAmbientOcclusionPass::GetRayExtentMin()
  200. {
  201. return m_rayMinT;
  202. }
  203. void RayTracingAmbientOcclusionPass::SetRayExtentMin(float minT)
  204. {
  205. m_rayMinT = minT;
  206. }
  207. float RayTracingAmbientOcclusionPass::GetRayExtentMax()
  208. {
  209. return m_rayMaxT;
  210. }
  211. void RayTracingAmbientOcclusionPass::SetRayExtentMax(float maxT)
  212. {
  213. m_rayMaxT = maxT;
  214. }
  215. } // namespace RPI
  216. } // namespace AZ