ApplyShaperLookupTablePass.cpp 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133
  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 <DisplayMapper/ApplyShaperLookupTablePass.h>
  9. #include <Atom/Feature/ACES/AcesDisplayMapperFeatureProcessor.h>
  10. #include <Atom/RHI/Factory.h>
  11. #include <Atom/RHI/FrameGraphAttachmentInterface.h>
  12. #include <Atom/RPI.Public/RenderPipeline.h>
  13. #include <Atom/RPI.Public/Scene.h>
  14. namespace AZ
  15. {
  16. namespace Render
  17. {
  18. RPI::Ptr<ApplyShaperLookupTablePass> ApplyShaperLookupTablePass::Create(const RPI::PassDescriptor& descriptor)
  19. {
  20. RPI::Ptr<ApplyShaperLookupTablePass> pass = aznew ApplyShaperLookupTablePass(descriptor);
  21. return pass;
  22. }
  23. ApplyShaperLookupTablePass::ApplyShaperLookupTablePass(const RPI::PassDescriptor& descriptor)
  24. : DisplayMapperFullScreenPass(descriptor)
  25. {
  26. }
  27. ApplyShaperLookupTablePass::~ApplyShaperLookupTablePass()
  28. {
  29. ReleaseLutImage();
  30. }
  31. void ApplyShaperLookupTablePass::InitializeInternal()
  32. {
  33. DisplayMapperFullScreenPass::InitializeInternal();
  34. AZ_Assert(m_shaderResourceGroup != nullptr, "ApplyShaperLookupTablePass %s has a null shader resource group when calling Init.", GetPathName().GetCStr());
  35. if (m_shaderResourceGroup != nullptr)
  36. {
  37. m_shaderInputLutImageIndex = m_shaderResourceGroup->FindShaderInputImageIndex(Name{ "m_lut" });
  38. m_shaderShaperTypeIndex = m_shaderResourceGroup->FindShaderInputConstantIndex(Name{ "m_shaperType" });
  39. m_shaderShaperBiasIndex = m_shaderResourceGroup->FindShaderInputConstantIndex(Name{ "m_shaperBias" });
  40. m_shaderShaperScaleIndex = m_shaderResourceGroup->FindShaderInputConstantIndex(Name{ "m_shaperScale" });
  41. }
  42. UpdateShaperSrg();
  43. }
  44. void ApplyShaperLookupTablePass::SetupFrameGraphDependenciesCommon([[maybe_unused]] RHI::FrameGraphInterface frameGraph)
  45. {
  46. if (m_needToReloadLut)
  47. {
  48. ReleaseLutImage();
  49. AcquireLutImage();
  50. m_needToReloadLut = false;
  51. }
  52. AZ_Assert(m_lutResource.m_lutStreamingImage != nullptr, "ApplyShaperLookupTablePass unable to acquire LUT image");
  53. }
  54. void ApplyShaperLookupTablePass::CompileResourcesCommon([[maybe_unused]] const RHI::FrameGraphCompileContext& context)
  55. {
  56. }
  57. void ApplyShaperLookupTablePass::SetupFrameGraphDependencies(RHI::FrameGraphInterface frameGraph)
  58. {
  59. DeclareAttachmentsToFrameGraph(frameGraph);
  60. DeclarePassDependenciesToFrameGraph(frameGraph);
  61. SetupFrameGraphDependenciesCommon(frameGraph);
  62. frameGraph.SetEstimatedItemCount(1);
  63. }
  64. void ApplyShaperLookupTablePass::CompileResources(const RHI::FrameGraphCompileContext& context)
  65. {
  66. AZ_Assert(m_shaderResourceGroup != nullptr, "ApplyShaperLookupTablePass %s has a null shader resource group when calling Compile.", GetPathName().GetCStr());
  67. CompileResourcesCommon(context);
  68. BindPassSrg(context, m_shaderResourceGroup);
  69. m_shaderResourceGroup->Compile();
  70. }
  71. void ApplyShaperLookupTablePass::AcquireLutImage()
  72. {
  73. auto displayMapper = m_pipeline->GetScene()->GetFeatureProcessor<AZ::Render::AcesDisplayMapperFeatureProcessor>();
  74. displayMapper->GetLutFromAssetId(m_lutResource, m_lutAssetId);
  75. UpdateShaperSrg();
  76. }
  77. void ApplyShaperLookupTablePass::ReleaseLutImage()
  78. {
  79. m_lutResource.m_lutStreamingImage.reset();
  80. }
  81. void ApplyShaperLookupTablePass::SetShaperParameters(const ShaperParams& shaperParams)
  82. {
  83. m_shaperParams = shaperParams;
  84. UpdateShaperSrg();
  85. }
  86. void ApplyShaperLookupTablePass::SetLutAssetId(const AZ::Data::AssetId& assetId)
  87. {
  88. m_lutAssetId = assetId;
  89. }
  90. const AZ::Data::AssetId& ApplyShaperLookupTablePass::GetLutAssetId() const
  91. {
  92. return m_lutAssetId;
  93. }
  94. void ApplyShaperLookupTablePass::UpdateShaperSrg()
  95. {
  96. AZ_Assert(m_shaderResourceGroup != nullptr, "ApplyShaperLookupTablePass %s has a null shader resource group when calling UpdateShaperSrg.", GetPathName().GetCStr());
  97. if (m_shaderResourceGroup != nullptr)
  98. {
  99. if (m_lutResource.m_lutStreamingImage)
  100. {
  101. m_shaderResourceGroup->SetImageView(m_shaderInputLutImageIndex, m_lutResource.m_lutStreamingImage->GetImageView());
  102. m_shaderResourceGroup->SetConstant(m_shaderShaperTypeIndex, m_shaperParams.m_type);
  103. m_shaderResourceGroup->SetConstant(m_shaderShaperBiasIndex, m_shaperParams.m_bias);
  104. m_shaderResourceGroup->SetConstant(m_shaderShaperScaleIndex, m_shaperParams.m_scale);
  105. }
  106. }
  107. }
  108. } // namespace Render
  109. } // namespace AZ