123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325 |
- /*
- * 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.
- *
- * SPDX-License-Identifier: Apache-2.0 OR MIT
- *
- */
- #include <Atom/Feature/RayTracing/RayTracingFeatureProcessorInterface.h>
- #include <Atom/RHI/CommandList.h>
- #include <Atom/RHI/DeviceDispatchRaysItem.h>
- #include <Atom/RHI/Factory.h>
- #include <Atom/RHI/RHISystemInterface.h>
- #include <Atom/RPI.Public/RenderPipeline.h>
- #include <Atom/RPI.Public/Scene.h>
- #include <Atom/RPI.Public/RPIUtils.h>
- #include <Atom/RPI.Public/View.h>
- #include <DiffuseProbeGrid_Traits_Platform.h>
- #include <Render/DiffuseProbeGridFeatureProcessor.h>
- #include <Render/DiffuseProbeGridVisualizationRayTracingPass.h>
- namespace AZ
- {
- namespace Render
- {
- RPI::Ptr<DiffuseProbeGridVisualizationRayTracingPass> DiffuseProbeGridVisualizationRayTracingPass::Create(const RPI::PassDescriptor& descriptor)
- {
- RPI::Ptr<DiffuseProbeGridVisualizationRayTracingPass> pass = aznew DiffuseProbeGridVisualizationRayTracingPass(descriptor);
- return AZStd::move(pass);
- }
- DiffuseProbeGridVisualizationRayTracingPass::DiffuseProbeGridVisualizationRayTracingPass(const RPI::PassDescriptor& descriptor)
- : RPI::RenderPass(descriptor)
- {
- if (RHI::RHISystemInterface::Get()->GetRayTracingSupport() == RHI::MultiDevice::NoDevices || !AZ_TRAIT_DIFFUSE_GI_PASSES_SUPPORTED)
- {
- // raytracing or GI is not supported on this platform
- SetEnabled(false);
- }
- }
- void DiffuseProbeGridVisualizationRayTracingPass::CreateRayTracingPipelineState()
- {
- // load the ray tracing shader
- // Note: the shader may not be available on all platforms
- AZStd::string shaderFilePath = "Shaders/DiffuseGlobalIllumination/DiffuseProbeGridVisualizationRayTracing.azshader";
- m_rayTracingShader = RPI::LoadCriticalShader(shaderFilePath);
- if (m_rayTracingShader == nullptr)
- {
- return;
- }
- auto shaderVariant = m_rayTracingShader->GetVariant(RPI::ShaderAsset::RootShaderVariantStableId);
- RHI::PipelineStateDescriptorForRayTracing rayGenerationShaderDescriptor;
- shaderVariant.ConfigurePipelineState(rayGenerationShaderDescriptor);
- // closest hit shader
- AZStd::string closestHitShaderFilePath = "Shaders/DiffuseGlobalIllumination/DiffuseProbeGridVisualizationRayTracingClosestHit.azshader";
- m_closestHitShader = RPI::LoadCriticalShader(closestHitShaderFilePath);
- auto closestHitShaderVariant = m_closestHitShader->GetVariant(RPI::ShaderAsset::RootShaderVariantStableId);
- RHI::PipelineStateDescriptorForRayTracing closestHitShaderDescriptor;
- closestHitShaderVariant.ConfigurePipelineState(closestHitShaderDescriptor);
- // miss shader
- AZStd::string missShaderFilePath = "Shaders/DiffuseGlobalIllumination/DiffuseProbeGridVisualizationRayTracingMiss.azshader";
- m_missShader = RPI::LoadCriticalShader(missShaderFilePath);
- auto missShaderVariant = m_missShader->GetVariant(RPI::ShaderAsset::RootShaderVariantStableId);
- RHI::PipelineStateDescriptorForRayTracing missShaderDescriptor;
- missShaderVariant.ConfigurePipelineState(missShaderDescriptor);
- // global pipeline state and Srg
- m_globalPipelineState = m_rayTracingShader->AcquirePipelineState(rayGenerationShaderDescriptor);
- AZ_Assert(m_globalPipelineState, "Failed to acquire ray tracing global pipeline state");
- m_globalSrgLayout = m_rayTracingShader->FindShaderResourceGroupLayout(Name{ "RayTracingGlobalSrg" });
- AZ_Assert(m_globalSrgLayout != nullptr, "Failed to find RayTracingGlobalSrg layout for shader [%s]", shaderFilePath.c_str());
- // build the ray tracing pipeline state descriptor
- RHI::RayTracingPipelineStateDescriptor descriptor;
- descriptor.Build()
- ->PipelineState(m_globalPipelineState.get())
- ->MaxPayloadSize(64)
- ->MaxAttributeSize(32)
- ->MaxRecursionDepth(2)
- ->ShaderLibrary(rayGenerationShaderDescriptor)
- ->RayGenerationShaderName(AZ::Name("RayGen"))
- ->ShaderLibrary(missShaderDescriptor)
- ->MissShaderName(AZ::Name("Miss"))
- ->ShaderLibrary(closestHitShaderDescriptor)
- ->ClosestHitShaderName(AZ::Name("ClosestHit"))
- ->HitGroup(AZ::Name("HitGroup"))
- ->ClosestHitShaderName(AZ::Name("ClosestHit"));
- // create the ray tracing pipeline state object
- m_rayTracingPipelineState = aznew RHI::RayTracingPipelineState;
- m_rayTracingPipelineState->Init(RHI::MultiDevice::AllDevices, descriptor);
- }
- bool DiffuseProbeGridVisualizationRayTracingPass::IsEnabled() const
- {
- if (!RenderPass::IsEnabled())
- {
- return false;
- }
- RPI::Scene* scene = m_pipeline->GetScene();
- if (!scene)
- {
- return false;
- }
- DiffuseProbeGridFeatureProcessor* diffuseProbeGridFeatureProcessor = scene->GetFeatureProcessor<DiffuseProbeGridFeatureProcessor>();
- if (diffuseProbeGridFeatureProcessor)
- {
- for (auto& diffuseProbeGrid : diffuseProbeGridFeatureProcessor->GetVisibleProbeGrids())
- {
- if (diffuseProbeGrid->GetVisualizationEnabled())
- {
- return true;
- }
- }
- }
- return false;
- }
- void DiffuseProbeGridVisualizationRayTracingPass::FrameBeginInternal(FramePrepareParams params)
- {
- RPI::Scene* scene = m_pipeline->GetScene();
- DiffuseProbeGridFeatureProcessor* diffuseProbeGridFeatureProcessor = scene->GetFeatureProcessor<DiffuseProbeGridFeatureProcessor>();
- if (!m_initialized)
- {
- CreateRayTracingPipelineState();
- m_initialized = true;
- }
- if (!m_rayTracingShaderTable)
- {
- RHI::RayTracingBufferPools& rayTracingBufferPools = diffuseProbeGridFeatureProcessor->GetVisualizationBufferPools();
- m_rayTracingShaderTable = aznew RHI::RayTracingShaderTable;
- m_rayTracingShaderTable->Init(RHI::MultiDevice::AllDevices, rayTracingBufferPools);
- AZStd::shared_ptr<RHI::RayTracingShaderTableDescriptor> descriptor = AZStd::make_shared<RHI::RayTracingShaderTableDescriptor>();
- // build the ray tracing shader table descriptor
- descriptor->Build(AZ::Name("RayTracingShaderTable"), m_rayTracingPipelineState)
- ->RayGenerationRecord(AZ::Name("RayGen"))
- ->MissRecord(AZ::Name("Miss"))
- ->HitGroupRecord(AZ::Name("HitGroup"))
- ;
- m_rayTracingShaderTable->Build(descriptor);
- }
- RenderPass::FrameBeginInternal(params);
- }
- void DiffuseProbeGridVisualizationRayTracingPass::SetupFrameGraphDependencies(RHI::FrameGraphInterface frameGraph)
- {
- RenderPass::SetupFrameGraphDependencies(frameGraph);
- RPI::Scene* scene = m_pipeline->GetScene();
- DiffuseProbeGridFeatureProcessor* diffuseProbeGridFeatureProcessor = scene->GetFeatureProcessor<DiffuseProbeGridFeatureProcessor>();
- frameGraph.SetEstimatedItemCount(aznumeric_cast<uint32_t>(diffuseProbeGridFeatureProcessor->GetVisibleProbeGrids().size()));
- for (auto& diffuseProbeGrid : diffuseProbeGridFeatureProcessor->GetVisibleProbeGrids())
- {
- if (!diffuseProbeGrid->GetVisualizationEnabled())
- {
- continue;
- }
- // TLAS
- {
- AZ::RHI::AttachmentId tlasAttachmentId = diffuseProbeGrid->GetProbeVisualizationTlasAttachmentId();
- const RHI::Ptr<RHI::Buffer>& visualizationTlasBuffer = diffuseProbeGrid->GetVisualizationTlas()->GetTlasBuffer();
- if (visualizationTlasBuffer)
- {
- if (!frameGraph.GetAttachmentDatabase().IsAttachmentValid(tlasAttachmentId))
- {
- [[maybe_unused]] RHI::ResultCode result = frameGraph.GetAttachmentDatabase().ImportBuffer(tlasAttachmentId, visualizationTlasBuffer);
- AZ_Assert(result == RHI::ResultCode::Success, "Failed to import ray tracing TLAS buffer with error %d", result);
- }
- uint32_t tlasBufferByteCount = aznumeric_cast<uint32_t>(visualizationTlasBuffer->GetDescriptor().m_byteCount);
- RHI::BufferViewDescriptor tlasBufferViewDescriptor = RHI::BufferViewDescriptor::CreateRaw(0, tlasBufferByteCount);
- RHI::BufferScopeAttachmentDescriptor desc;
- desc.m_attachmentId = tlasAttachmentId;
- desc.m_bufferViewDescriptor = tlasBufferViewDescriptor;
- desc.m_loadStoreAction.m_loadAction = AZ::RHI::AttachmentLoadAction::Load;
- frameGraph.UseShaderAttachment(desc, RHI::ScopeAttachmentAccess::ReadWrite, RHI::ScopeAttachmentStage::RayTracingShader);
- }
- }
- // grid data
- {
- RHI::BufferScopeAttachmentDescriptor desc;
- desc.m_attachmentId = diffuseProbeGrid->GetGridDataBufferAttachmentId();
- desc.m_bufferViewDescriptor = diffuseProbeGrid->GetRenderData()->m_gridDataBufferViewDescriptor;
- desc.m_loadStoreAction.m_loadAction = AZ::RHI::AttachmentLoadAction::Load;
- frameGraph.UseShaderAttachment(desc, RHI::ScopeAttachmentAccess::Read, RHI::ScopeAttachmentStage::RayTracingShader);
- }
- // probe irradiance
- {
- RHI::ImageScopeAttachmentDescriptor desc;
- desc.m_attachmentId = diffuseProbeGrid->GetIrradianceImageAttachmentId();
- desc.m_imageViewDescriptor = diffuseProbeGrid->GetRenderData()->m_probeIrradianceImageViewDescriptor;
- desc.m_loadStoreAction.m_loadAction = AZ::RHI::AttachmentLoadAction::Load;
- frameGraph.UseShaderAttachment(desc, RHI::ScopeAttachmentAccess::Read, RHI::ScopeAttachmentStage::RayTracingShader);
- }
- // probe distance
- {
- RHI::ImageScopeAttachmentDescriptor desc;
- desc.m_attachmentId = diffuseProbeGrid->GetDistanceImageAttachmentId();
- desc.m_imageViewDescriptor = diffuseProbeGrid->GetRenderData()->m_probeDistanceImageViewDescriptor;
- desc.m_loadStoreAction.m_loadAction = AZ::RHI::AttachmentLoadAction::Load;
- frameGraph.UseShaderAttachment(desc, RHI::ScopeAttachmentAccess::Read, RHI::ScopeAttachmentStage::RayTracingShader);
- }
- // probe data
- {
- RHI::ImageScopeAttachmentDescriptor desc;
- desc.m_attachmentId = diffuseProbeGrid->GetProbeDataImageAttachmentId();
- desc.m_imageViewDescriptor = diffuseProbeGrid->GetRenderData()->m_probeDataImageViewDescriptor;
- desc.m_loadStoreAction.m_loadAction = AZ::RHI::AttachmentLoadAction::Load;
- frameGraph.UseShaderAttachment(desc, RHI::ScopeAttachmentAccess::Read, RHI::ScopeAttachmentStage::RayTracingShader);
- }
- }
- // retrieve the visualization image size, this will determine the number of rays to cast
- RPI::Ptr<RPI::PassAttachment> visualizationImageAttachment = m_ownedAttachments[0];
- AZ_Assert(visualizationImageAttachment.get(), "Invalid DiffuseProbeGrid Visualization image");
- m_outputAttachmentSize = visualizationImageAttachment->GetTransientImageDescriptor().m_imageDescriptor.m_size;
- }
- void DiffuseProbeGridVisualizationRayTracingPass::CompileResources([[maybe_unused]] const RHI::FrameGraphCompileContext& context)
- {
- const RHI::ImageView* outputImageView = context.GetImageView(GetOutputBinding(0).GetAttachment()->GetAttachmentId());
- AZ_Assert(outputImageView, "Failed to retrieve output ImageView");
- RPI::Scene* scene = m_pipeline->GetScene();
- DiffuseProbeGridFeatureProcessor* diffuseProbeGridFeatureProcessor = scene->GetFeatureProcessor<DiffuseProbeGridFeatureProcessor>();
- for (auto& diffuseProbeGrid : diffuseProbeGridFeatureProcessor->GetVisibleProbeGrids())
- {
- if (!diffuseProbeGrid->GetVisualizationEnabled())
- {
- continue;
- }
- // the DiffuseProbeGridVisualization Srg must be updated in the Compile phase in order to successfully bind the ReadWrite shader
- // inputs (see line ValidateSetImageView() in ShaderResourceGroupData.cpp)
- diffuseProbeGrid->UpdateVisualizationRayTraceSrg(m_rayTracingShader, m_globalSrgLayout, outputImageView);
- if (!diffuseProbeGrid->GetVisualizationRayTraceSrg()->IsQueuedForCompile())
- {
- diffuseProbeGrid->GetVisualizationRayTraceSrg()->Compile();
- }
- }
- if (auto viewSRG = diffuseProbeGridFeatureProcessor->GetViewSrg(m_pipeline, GetPipelineViewTag()))
- {
- BindSrg(viewSRG->GetRHIShaderResourceGroup());
- }
- }
-
- void DiffuseProbeGridVisualizationRayTracingPass::BuildCommandListInternal([[maybe_unused]] const RHI::FrameGraphExecuteContext& context)
- {
- RPI::Scene* scene = m_pipeline->GetScene();
- DiffuseProbeGridFeatureProcessor* diffuseProbeGridFeatureProcessor = scene->GetFeatureProcessor<DiffuseProbeGridFeatureProcessor>();
- auto* rayTracingFeatureProcessor = scene->GetFeatureProcessor<RayTracingFeatureProcessorInterface>();
- AZ_Assert(rayTracingFeatureProcessor, "DiffuseProbeGridVisualizationRayTracingPass requires the RayTracingFeatureProcessor");
- const AZStd::vector<RPI::ViewPtr>& views = m_pipeline->GetViews(RPI::PipelineViewTag{ "MainCamera" });
- if (views.empty())
- {
- return;
- }
- // submit the DispatchRaysItems for each DiffuseProbeGrid in this range
- for (uint32_t index = context.GetSubmitRange().m_startIndex; index < context.GetSubmitRange().m_endIndex; ++index)
- {
- AZStd::shared_ptr<DiffuseProbeGrid> diffuseProbeGrid = diffuseProbeGridFeatureProcessor->GetVisibleProbeGrids()[index];
- if (!diffuseProbeGrid->GetVisualizationEnabled())
- {
- continue;
- }
- const RHI::DeviceShaderResourceGroup* shaderResourceGroups[] = {
- diffuseProbeGrid->GetVisualizationRayTraceSrg()->GetRHIShaderResourceGroup()->GetDeviceShaderResourceGroup(context.GetDeviceIndex()).get(),
- rayTracingFeatureProcessor->GetRayTracingSceneSrg()->GetRHIShaderResourceGroup()->GetDeviceShaderResourceGroup(context.GetDeviceIndex()).get(),
- views[0]->GetRHIShaderResourceGroup()->GetDeviceShaderResourceGroup(context.GetDeviceIndex()).get(),
- };
- RHI::DeviceDispatchRaysItem dispatchRaysItem;
- dispatchRaysItem.m_arguments.m_direct.m_width = m_outputAttachmentSize.m_width;
- dispatchRaysItem.m_arguments.m_direct.m_height = m_outputAttachmentSize.m_height;
- dispatchRaysItem.m_arguments.m_direct.m_depth = 1;
- dispatchRaysItem.m_rayTracingPipelineState =
- m_rayTracingPipelineState->GetDeviceRayTracingPipelineState(context.GetDeviceIndex()).get();
- dispatchRaysItem.m_rayTracingShaderTable = m_rayTracingShaderTable->GetDeviceRayTracingShaderTable(context.GetDeviceIndex()).get();
- dispatchRaysItem.m_shaderResourceGroupCount = RHI::ArraySize(shaderResourceGroups);
- dispatchRaysItem.m_shaderResourceGroups = shaderResourceGroups;
- dispatchRaysItem.m_globalPipelineState = m_globalPipelineState->GetDevicePipelineState(context.GetDeviceIndex()).get();
- // submit the DispatchRays item
- context.GetCommandList()->Submit(dispatchRaysItem, index);
- }
- }
- } // namespace RPI
- } // namespace AZ
|