3
0

ApplyShaperLookupTablePass.cpp 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119
  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/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::SetupFrameGraphDependencies(RHI::FrameGraphInterface frameGraph)
  45. {
  46. DeclareAttachmentsToFrameGraph(frameGraph);
  47. DeclarePassDependenciesToFrameGraph(frameGraph);
  48. if (m_needToReloadLut)
  49. {
  50. ReleaseLutImage();
  51. AcquireLutImage();
  52. m_needToReloadLut = false;
  53. }
  54. AZ_Assert(m_lutResource.m_lutStreamingImage != nullptr, "ApplyShaperLookupTablePass unable to acquire LUT image");
  55. frameGraph.SetEstimatedItemCount(1);
  56. }
  57. void ApplyShaperLookupTablePass::CompileResources(const RHI::FrameGraphCompileContext& context)
  58. {
  59. AZ_Assert(m_shaderResourceGroup != nullptr, "ApplyShaperLookupTablePass %s has a null shader resource group when calling Compile.", GetPathName().GetCStr());
  60. BindPassSrg(context, m_shaderResourceGroup);
  61. m_shaderResourceGroup->Compile();
  62. }
  63. void ApplyShaperLookupTablePass::AcquireLutImage()
  64. {
  65. auto displayMapper = m_pipeline->GetScene()->GetFeatureProcessor<AZ::Render::AcesDisplayMapperFeatureProcessor>();
  66. displayMapper->GetLutFromAssetId(m_lutResource, m_lutAssetId);
  67. UpdateShaperSrg();
  68. }
  69. void ApplyShaperLookupTablePass::ReleaseLutImage()
  70. {
  71. m_lutResource.m_lutStreamingImage.reset();
  72. }
  73. void ApplyShaperLookupTablePass::SetShaperParameters(const ShaperParams& shaperParams)
  74. {
  75. m_shaperParams = shaperParams;
  76. UpdateShaperSrg();
  77. }
  78. void ApplyShaperLookupTablePass::SetLutAssetId(const AZ::Data::AssetId& assetId)
  79. {
  80. m_lutAssetId = assetId;
  81. }
  82. void ApplyShaperLookupTablePass::UpdateShaperSrg()
  83. {
  84. AZ_Assert(m_shaderResourceGroup != nullptr, "ApplyShaperLookupTablePass %s has a null shader resource group when calling UpdateShaperSrg.", GetPathName().GetCStr());
  85. if (m_shaderResourceGroup != nullptr)
  86. {
  87. if (m_lutResource.m_lutStreamingImage)
  88. {
  89. m_shaderResourceGroup->SetImageView(m_shaderInputLutImageIndex, m_lutResource.m_lutStreamingImage->GetImageView());
  90. m_shaderResourceGroup->SetConstant(m_shaderShaperTypeIndex, m_shaperParams.m_type);
  91. m_shaderResourceGroup->SetConstant(m_shaderShaperBiasIndex, m_shaperParams.m_bias);
  92. m_shaderResourceGroup->SetConstant(m_shaderShaperScaleIndex, m_shaperParams.m_scale);
  93. }
  94. }
  95. }
  96. } // namespace Render
  97. } // namespace AZ