DepthOfFieldCompositePass.cpp 7.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159
  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 <PostProcess/PostProcessFeatureProcessor.h>
  9. #include <PostProcess/DepthOfField/DepthOfFieldSettings.h>
  10. #include <PostProcessing/DepthOfFieldCompositePass.h>
  11. #include <Atom/RPI.Public/RenderPipeline.h>
  12. #include <Atom/RPI.Public/Scene.h>
  13. #include <Atom/RPI.Public/View.h>
  14. namespace AZ
  15. {
  16. namespace Render
  17. {
  18. RPI::Ptr<DepthOfFieldCompositePass> DepthOfFieldCompositePass::Create(const RPI::PassDescriptor& descriptor)
  19. {
  20. RPI::Ptr<DepthOfFieldCompositePass> pass = aznew DepthOfFieldCompositePass(descriptor);
  21. return pass;
  22. }
  23. DepthOfFieldCompositePass::DepthOfFieldCompositePass(const RPI::PassDescriptor& descriptor)
  24. : RPI::FullscreenTrianglePass(descriptor)
  25. // option names from the azsl file
  26. , m_optionName("o_dofMode")
  27. , m_optionValues
  28. {
  29. AZ::Name("DepthOfFieldDebugColoring::Disabled"),
  30. AZ::Name("DepthOfFieldDebugColoring::Enabled")
  31. }
  32. {
  33. }
  34. void DepthOfFieldCompositePass::InitializeInternal()
  35. {
  36. FullscreenTrianglePass::InitializeInternal();
  37. m_backBlendFactorDivision2Index.Reset();
  38. m_backBlendFactorDivision4Index.Reset();
  39. m_backBlendFactorDivision8Index.Reset();
  40. m_frontBlendFactorDivision2Index.Reset();
  41. m_frontBlendFactorDivision4Index.Reset();
  42. m_frontBlendFactorDivision8Index.Reset();
  43. InitializeShaderVariant();
  44. }
  45. void DepthOfFieldCompositePass::InitializeShaderVariant()
  46. {
  47. AZ_Assert(m_shader != nullptr, "DepthOfFieldCompositePass %s has a null shader when calling InitializeShaderVariant.", GetPathName().GetCStr());
  48. // Caching all pipeline state for each shader variation for performance reason.
  49. auto optionValueCount = m_optionValues.size();
  50. for (auto shaderVariantIndex = 0; shaderVariantIndex < optionValueCount; ++shaderVariantIndex)
  51. {
  52. auto shaderOption = m_shader->CreateShaderOptionGroup();
  53. shaderOption.SetValue(m_optionName, m_optionValues[shaderVariantIndex]);
  54. PreloadShaderVariant(m_shader, shaderOption, GetRenderAttachmentConfiguration(), GetMultisampleState());
  55. }
  56. m_needToUpdateShaderVariant = true;
  57. }
  58. void DepthOfFieldCompositePass::UpdateCurrentShaderVariant()
  59. {
  60. AZ_Assert(m_shader != nullptr, "DepthOfFieldCompositePass %s has a null shader when calling UpdateCurrentShaderVariant.", GetPathName().GetCStr());
  61. RPI::ShaderOptionGroup shaderOption = m_shader->CreateShaderOptionGroup();
  62. // DebugColoring::Disabled == 0
  63. // DebugColoring::Enabled == 1
  64. int32_t index = m_enabledDebugColoring ? 1 : 0;
  65. shaderOption.SetValue(m_optionName, m_optionValues[index]);
  66. UpdateShaderVariant(shaderOption);
  67. m_needToUpdateShaderVariant = false;
  68. }
  69. void DepthOfFieldCompositePass::FrameBeginInternal(FramePrepareParams params)
  70. {
  71. FullscreenTrianglePass::FrameBeginInternal(params);
  72. RPI::Scene* scene = GetScene();
  73. PostProcessFeatureProcessor* fp = scene->GetFeatureProcessor<PostProcessFeatureProcessor>();
  74. AZ::RPI::ViewPtr view = GetRenderPipeline()->GetFirstView(GetPipelineViewTag());
  75. if (fp)
  76. {
  77. PostProcessSettings* postProcessSettings = fp->GetLevelSettingsFromView(view);
  78. if (postProcessSettings)
  79. {
  80. DepthOfFieldSettings* dofSettings = postProcessSettings->GetDepthOfFieldSettings();
  81. if (dofSettings)
  82. {
  83. SetEnabledDebugColoring(dofSettings->GetEnableDebugColoring());
  84. dofSettings->SetValuesToViewSrg(view->GetShaderResourceGroup());
  85. m_backBlendFactorDivision2 = dofSettings->m_configurationToViewSRG.m_backBlendFactorDivision2;
  86. m_backBlendFactorDivision4 = dofSettings->m_configurationToViewSRG.m_backBlendFactorDivision4;
  87. m_backBlendFactorDivision8 = dofSettings->m_configurationToViewSRG.m_backBlendFactorDivision8;
  88. m_frontBlendFactorDivision2 = dofSettings->m_configurationToViewSRG.m_frontBlendFactorDivision2;
  89. m_frontBlendFactorDivision4 = dofSettings->m_configurationToViewSRG.m_frontBlendFactorDivision4;
  90. m_frontBlendFactorDivision8 = dofSettings->m_configurationToViewSRG.m_frontBlendFactorDivision8;
  91. }
  92. }
  93. }
  94. }
  95. void DepthOfFieldCompositePass::CompileResources(const RHI::FrameGraphCompileContext& context)
  96. {
  97. AZ_Assert(m_shaderResourceGroup, "DepthOfFieldCompositePass %s has a null shader resource group when calling FrameBeginInternal.", GetPathName().GetCStr());
  98. if (m_needToUpdateShaderVariant)
  99. {
  100. UpdateCurrentShaderVariant();
  101. }
  102. CompileShaderVariant(m_shaderResourceGroup);
  103. m_shaderResourceGroup->SetConstant(m_backBlendFactorDivision2Index, m_backBlendFactorDivision2);
  104. m_shaderResourceGroup->SetConstant(m_backBlendFactorDivision4Index, m_backBlendFactorDivision4);
  105. m_shaderResourceGroup->SetConstant(m_backBlendFactorDivision8Index, m_backBlendFactorDivision8);
  106. m_shaderResourceGroup->SetConstant(m_frontBlendFactorDivision2Index, m_frontBlendFactorDivision2);
  107. m_shaderResourceGroup->SetConstant(m_frontBlendFactorDivision4Index, m_frontBlendFactorDivision4);
  108. m_shaderResourceGroup->SetConstant(m_frontBlendFactorDivision8Index, m_frontBlendFactorDivision8);
  109. BindPassSrg(context, m_shaderResourceGroup);
  110. m_shaderResourceGroup->Compile();
  111. }
  112. void DepthOfFieldCompositePass::BuildCommandListInternal(const RHI::FrameGraphExecuteContext& context)
  113. {
  114. AZ_Assert(m_shaderResourceGroup != nullptr, "DepthOfFieldCompositePass %s has a null shader resource group when calling Execute.", GetPathName().GetCStr());
  115. RHI::CommandList* commandList = context.GetCommandList();
  116. commandList->SetViewport(m_viewportState);
  117. commandList->SetScissor(m_scissorState);
  118. SetSrgsForDraw(context);
  119. m_item.SetPipelineState(GetPipelineStateFromShaderVariant());
  120. commandList->Submit(m_item.GetDeviceDrawItem(context.GetDeviceIndex()));
  121. }
  122. void DepthOfFieldCompositePass::SetEnabledDebugColoring(bool enabled)
  123. {
  124. m_needToUpdateShaderVariant = m_needToUpdateShaderVariant || (m_enabledDebugColoring != enabled);
  125. m_enabledDebugColoring = enabled;
  126. }
  127. } // namespace Render
  128. } // namespace AZ