You cannot select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
191 lines
9.1 KiB
C++
191 lines
9.1 KiB
C++
/*
|
|
* All or portions of this file Copyright (c) Amazon.com, Inc. or its affiliates or
|
|
* its licensors.
|
|
*
|
|
* For complete copyright and license terms please see the LICENSE at the root of this
|
|
* distribution (the "License"). All use of this software is governed by the License,
|
|
* or, if provided, by the license below or the license accompanying this file. Do not
|
|
* remove or modify any license notices. This file is distributed on an "AS IS" BASIS,
|
|
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
*
|
|
*/
|
|
|
|
#include <Atom/RHI/Factory.h>
|
|
#include <Atom/RHI/FrameGraphInterface.h>
|
|
#include <Atom/RHI/FrameGraphAttachmentInterface.h>
|
|
#include <Atom/RHI/Device.h>
|
|
#include <Atom/RPI.Public/Pass/PassUtils.h>
|
|
#include <Atom/RPI.Public/RenderPipeline.h>
|
|
#include <Atom/RPI.Public/RPIUtils.h>
|
|
#include <DiffuseGlobalIllumination/DiffuseProbeGridFeatureProcessor.h>
|
|
#include <DiffuseGlobalIllumination/DiffuseProbeGridBlendDistancePass.h>
|
|
#include <RayTracing/RayTracingFeatureProcessor.h>
|
|
|
|
namespace AZ
|
|
{
|
|
namespace Render
|
|
{
|
|
RPI::Ptr<DiffuseProbeGridBlendDistancePass> DiffuseProbeGridBlendDistancePass::Create(const RPI::PassDescriptor& descriptor)
|
|
{
|
|
RPI::Ptr<DiffuseProbeGridBlendDistancePass> pass = aznew DiffuseProbeGridBlendDistancePass(descriptor);
|
|
return AZStd::move(pass);
|
|
}
|
|
|
|
DiffuseProbeGridBlendDistancePass::DiffuseProbeGridBlendDistancePass(const RPI::PassDescriptor& descriptor)
|
|
: RPI::RenderPass(descriptor)
|
|
{
|
|
LoadShader();
|
|
}
|
|
|
|
void DiffuseProbeGridBlendDistancePass::LoadShader()
|
|
{
|
|
// load shader
|
|
// Note: the shader may not be available on all platforms
|
|
AZStd::string shaderFilePath = "Shaders/DiffuseGlobalIllumination/DiffuseProbeGridBlendDistance.azshader";
|
|
m_shader = RPI::LoadShader(shaderFilePath);
|
|
if (m_shader == nullptr)
|
|
{
|
|
return;
|
|
}
|
|
|
|
// load pipeline state
|
|
RHI::PipelineStateDescriptorForDispatch pipelineStateDescriptor;
|
|
const auto& shaderVariant = m_shader->GetVariant(RPI::ShaderAsset::RootShaderVariantStableId);
|
|
shaderVariant.ConfigurePipelineState(pipelineStateDescriptor);
|
|
m_pipelineState = m_shader->AcquirePipelineState(pipelineStateDescriptor);
|
|
|
|
// load Pass Srg asset
|
|
m_srgAsset = m_shader->FindShaderResourceGroupAsset(RPI::SrgBindingSlot::Pass);
|
|
|
|
// retrieve the number of threads per thread group from the shader
|
|
const auto numThreads = m_shader->GetAsset()->GetAttribute(RHI::ShaderStage::Compute, Name{ "numthreads" });
|
|
if (numThreads)
|
|
{
|
|
const RHI::ShaderStageAttributeArguments& args = *numThreads;
|
|
bool validArgs = args.size() == 3;
|
|
if (validArgs)
|
|
{
|
|
validArgs &= args[0].type() == azrtti_typeid<int>();
|
|
validArgs &= args[1].type() == azrtti_typeid<int>();
|
|
validArgs &= args[2].type() == azrtti_typeid<int>();
|
|
}
|
|
|
|
if (!validArgs)
|
|
{
|
|
AZ_Error("PassSystem", false, "[DiffuseProbeGridBlendDistancePass '%s']: Shader '%s' contains invalid numthreads arguments.", GetPathName().GetCStr(), shaderFilePath.c_str());
|
|
return;
|
|
}
|
|
|
|
m_dispatchArgs.m_threadsPerGroupX = AZStd::any_cast<int>(args[0]);
|
|
m_dispatchArgs.m_threadsPerGroupY = AZStd::any_cast<int>(args[1]);
|
|
m_dispatchArgs.m_threadsPerGroupZ = AZStd::any_cast<int>(args[2]);
|
|
}
|
|
}
|
|
|
|
void DiffuseProbeGridBlendDistancePass::FrameBeginInternal(FramePrepareParams params)
|
|
{
|
|
RPI::Scene* scene = m_pipeline->GetScene();
|
|
DiffuseProbeGridFeatureProcessor* diffuseProbeGridFeatureProcessor = scene->GetFeatureProcessor<DiffuseProbeGridFeatureProcessor>();
|
|
|
|
if (!diffuseProbeGridFeatureProcessor || diffuseProbeGridFeatureProcessor->GetRealTimeProbeGrids().empty())
|
|
{
|
|
// no diffuse probe grids
|
|
return;
|
|
}
|
|
|
|
RayTracingFeatureProcessor* rayTracingFeatureProcessor = scene->GetFeatureProcessor<RayTracingFeatureProcessor>();
|
|
AZ_Assert(rayTracingFeatureProcessor, "DiffuseProbeGridBlendDistancePass requires the RayTracingFeatureProcessor");
|
|
|
|
if (!rayTracingFeatureProcessor->GetSubMeshCount())
|
|
{
|
|
// empty scene
|
|
return;
|
|
}
|
|
|
|
RenderPass::FrameBeginInternal(params);
|
|
}
|
|
|
|
void DiffuseProbeGridBlendDistancePass::SetupFrameGraphDependencies(RHI::FrameGraphInterface frameGraph)
|
|
{
|
|
RenderPass::SetupFrameGraphDependencies(frameGraph);
|
|
|
|
RPI::Scene* scene = m_pipeline->GetScene();
|
|
DiffuseProbeGridFeatureProcessor* diffuseProbeGridFeatureProcessor = scene->GetFeatureProcessor<DiffuseProbeGridFeatureProcessor>();
|
|
for (auto& diffuseProbeGrid : diffuseProbeGridFeatureProcessor->GetRealTimeProbeGrids())
|
|
{
|
|
// probe raytrace image
|
|
{
|
|
RHI::ImageScopeAttachmentDescriptor desc;
|
|
desc.m_attachmentId = diffuseProbeGrid->GetRayTraceImageAttachmentId();
|
|
desc.m_imageViewDescriptor = diffuseProbeGrid->GetRenderData()->m_probeRayTraceImageViewDescriptor;
|
|
desc.m_loadStoreAction.m_loadAction = AZ::RHI::AttachmentLoadAction::Load;
|
|
|
|
frameGraph.UseShaderAttachment(desc, RHI::ScopeAttachmentAccess::ReadWrite);
|
|
}
|
|
|
|
// probe distance image
|
|
{
|
|
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::ReadWrite);
|
|
}
|
|
|
|
// probe classification image
|
|
{
|
|
RHI::ImageScopeAttachmentDescriptor desc;
|
|
desc.m_attachmentId = diffuseProbeGrid->GetClassificationImageAttachmentId();
|
|
desc.m_imageViewDescriptor = diffuseProbeGrid->GetRenderData()->m_probeClassificationImageViewDescriptor;
|
|
desc.m_loadStoreAction.m_loadAction = AZ::RHI::AttachmentLoadAction::Load;
|
|
|
|
frameGraph.UseShaderAttachment(desc, RHI::ScopeAttachmentAccess::ReadWrite);
|
|
}
|
|
}
|
|
}
|
|
|
|
void DiffuseProbeGridBlendDistancePass::CompileResources([[maybe_unused]] const RHI::FrameGraphCompileContext& context)
|
|
{
|
|
RPI::Scene* scene = m_pipeline->GetScene();
|
|
DiffuseProbeGridFeatureProcessor* diffuseProbeGridFeatureProcessor = scene->GetFeatureProcessor<DiffuseProbeGridFeatureProcessor>();
|
|
|
|
for (auto& diffuseProbeGrid : diffuseProbeGridFeatureProcessor->GetRealTimeProbeGrids())
|
|
{
|
|
// the diffuse probe grid Srg must be updated in the Compile phase in order to successfully bind the ReadWrite shader inputs
|
|
// (see ValidateSetImageView() in ShaderResourceGroupData.cpp)
|
|
diffuseProbeGrid->UpdateBlendDistanceSrg(m_srgAsset);
|
|
|
|
diffuseProbeGrid->GetBlendDistanceSrg()->Compile();
|
|
}
|
|
}
|
|
|
|
void DiffuseProbeGridBlendDistancePass::BuildCommandListInternal(const RHI::FrameGraphExecuteContext& context)
|
|
{
|
|
RHI::CommandList* commandList = context.GetCommandList();
|
|
RPI::Scene* scene = m_pipeline->GetScene();
|
|
DiffuseProbeGridFeatureProcessor* diffuseProbeGridFeatureProcessor = scene->GetFeatureProcessor<DiffuseProbeGridFeatureProcessor>();
|
|
|
|
// submit the DispatchItem for each DiffuseProbeGrid
|
|
for (auto& diffuseProbeGrid : diffuseProbeGridFeatureProcessor->GetRealTimeProbeGrids())
|
|
{
|
|
const RHI::ShaderResourceGroup* shaderResourceGroup = diffuseProbeGrid->GetBlendDistanceSrg()->GetRHIShaderResourceGroup();
|
|
commandList->SetShaderResourceGroupForDispatch(*shaderResourceGroup);
|
|
|
|
uint32_t probeCountX;
|
|
uint32_t probeCountY;
|
|
diffuseProbeGrid->GetTexture2DProbeCount(probeCountX, probeCountY);
|
|
|
|
RHI::DispatchItem dispatchItem;
|
|
dispatchItem.m_arguments = m_dispatchArgs;
|
|
dispatchItem.m_pipelineState = m_pipelineState;
|
|
dispatchItem.m_arguments.m_direct.m_totalNumberOfThreadsX = probeCountX * dispatchItem.m_arguments.m_direct.m_threadsPerGroupX;
|
|
dispatchItem.m_arguments.m_direct.m_totalNumberOfThreadsY = probeCountY * dispatchItem.m_arguments.m_direct.m_threadsPerGroupY;
|
|
dispatchItem.m_arguments.m_direct.m_totalNumberOfThreadsZ = 1;
|
|
|
|
commandList->Submit(dispatchItem);
|
|
}
|
|
}
|
|
} // namespace Render
|
|
} // namespace AZ
|