ReflectionScreenSpaceTracePass.cpp 4.8 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091
  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 "ReflectionScreenSpaceTracePass.h"
  9. #include <Atom/RPI.Public/Pass/PassSystemInterface.h>
  10. #include <Atom/RPI.Public/Pass/PassFilter.h>
  11. #include <Atom/RPI.Public/RenderPipeline.h>
  12. #include <Atom/RPI.Public/Scene.h>
  13. #include <Atom/RPI.Public/Image/ImageSystemInterface.h>
  14. #include <Atom/RPI.Public/Image/AttachmentImagePool.h>
  15. #include <SpecularReflections/SpecularReflectionsFeatureProcessor.h>
  16. namespace AZ
  17. {
  18. namespace Render
  19. {
  20. RPI::Ptr<ReflectionScreenSpaceTracePass> ReflectionScreenSpaceTracePass::Create(const RPI::PassDescriptor& descriptor)
  21. {
  22. RPI::Ptr<ReflectionScreenSpaceTracePass> pass = aznew ReflectionScreenSpaceTracePass(descriptor);
  23. return AZStd::move(pass);
  24. }
  25. ReflectionScreenSpaceTracePass::ReflectionScreenSpaceTracePass(const RPI::PassDescriptor& descriptor)
  26. : RPI::FullscreenTrianglePass(descriptor)
  27. {
  28. }
  29. void ReflectionScreenSpaceTracePass::BuildInternal()
  30. {
  31. Data::Instance<RPI::AttachmentImagePool> pool = RPI::ImageSystemInterface::Get()->GetSystemAttachmentPool();
  32. // retrieve the previous frame image attachment from the pass
  33. AZ_Assert(m_ownedAttachments.size() == 3, "ReflectionScreenSpaceTracePass must have the following attachment images defined: ReflectionImage, TraceCoordsImage, and PreviousFrameImage");
  34. RPI::Ptr<RPI::PassAttachment> previousFrameImageAttachment = m_ownedAttachments[2];
  35. // update the image attachment descriptor to sync up size and format
  36. previousFrameImageAttachment->Update();
  37. // change the lifetime since we want it to live between frames
  38. previousFrameImageAttachment->m_lifetime = RHI::AttachmentLifetimeType::Imported;
  39. // set the bind flags
  40. RHI::ImageDescriptor& imageDesc = previousFrameImageAttachment->m_descriptor.m_image;
  41. imageDesc.m_bindFlags |= RHI::ImageBindFlags::Color | RHI::ImageBindFlags::ShaderReadWrite;
  42. // create the image attachment
  43. RHI::ClearValue clearValue = RHI::ClearValue::CreateVector4Float(0, 0, 0, 0);
  44. m_previousFrameImageAttachment = RPI::AttachmentImage::Create(*pool.get(), imageDesc, Name(previousFrameImageAttachment->m_path.GetCStr()), &clearValue, nullptr);
  45. previousFrameImageAttachment->m_path = m_previousFrameImageAttachment->GetAttachmentId();
  46. previousFrameImageAttachment->m_importedResource = m_previousFrameImageAttachment;
  47. }
  48. void ReflectionScreenSpaceTracePass::CompileResources([[maybe_unused]] const RHI::FrameGraphCompileContext& context)
  49. {
  50. if (!m_shaderResourceGroup)
  51. {
  52. return;
  53. }
  54. RPI::Scene* scene = m_pipeline->GetScene();
  55. SpecularReflectionsFeatureProcessor* specularReflectionsFeatureProcessor = scene->GetFeatureProcessor<SpecularReflectionsFeatureProcessor>();
  56. AZ_Assert(specularReflectionsFeatureProcessor, "ReflectionScreenSpaceTracePass requires the SpecularReflectionsFeatureProcessor");
  57. RPI::PassAttachment* outputAttachment = GetOutputBinding(0).GetAttachment().get();
  58. AZ_Assert(outputAttachment, "ReflectionScreenSpaceTracePass: Output binding has no attachment!");
  59. RHI::Size outputImageSize = outputAttachment->m_descriptor.m_image.m_size;
  60. const SSROptions& ssrOptions = specularReflectionsFeatureProcessor->GetSSROptions();
  61. m_shaderResourceGroup->SetConstant(m_invOutputScaleNameIndex, 1.0f / ssrOptions.GetOutputScale());
  62. m_shaderResourceGroup->SetConstant(m_outputWidthNameIndex, outputImageSize.m_width);
  63. m_shaderResourceGroup->SetConstant(m_outputHeightNameIndex, outputImageSize.m_height);
  64. m_shaderResourceGroup->SetConstant(m_rayTracingEnabledNameIndex, ssrOptions.IsRayTracingEnabled());
  65. m_shaderResourceGroup->SetConstant(m_rayTraceFallbackDataNameIndex, ssrOptions.IsRayTracingFallbackEnabled());
  66. m_shaderResourceGroup->SetConstant(m_maxRayDistanceNameIndex, ssrOptions.m_maxRayDistance);
  67. m_shaderResourceGroup->SetConstant(m_maxDepthThresholdNameIndex, ssrOptions.m_maxDepthThreshold);
  68. m_shaderResourceGroup->SetConstant(m_maxRoughnessNameIndex, ssrOptions.m_maxRoughness);
  69. m_shaderResourceGroup->SetConstant(m_roughnessBiasNameIndex, ssrOptions.m_roughnessBias);
  70. FullscreenTrianglePass::CompileResources(context);
  71. }
  72. } // namespace RPI
  73. } // namespace AZ