PostProcessingShaderOptionBase.cpp 3.8 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182
  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 <PostProcessing/PostProcessingShaderOptionBase.h>
  9. #include <Atom/RPI.Public/RenderPipeline.h>
  10. #include <Atom/RPI.Public/View.h>
  11. namespace AZ
  12. {
  13. namespace Render
  14. {
  15. void PostProcessingShaderOptionBase::PreloadShaderVariant(
  16. const Data::Instance<AZ::RPI::Shader>& shader,
  17. const RPI::ShaderOptionGroup& shaderOption,
  18. const RHI::RenderAttachmentConfiguration& renderAttachmentConfiguration,
  19. const RHI::MultisampleState& multisampleState)
  20. {
  21. RHI::PipelineStateDescriptorForDraw pipelineStateDescriptor;
  22. AZ::u64 variationKey = shaderOption.GetShaderVariantId().m_key.to_ullong();
  23. auto shaderVariant = shader->GetVariant(shaderOption.GetShaderVariantId());
  24. shaderVariant.ConfigurePipelineState(pipelineStateDescriptor, shaderOption);
  25. pipelineStateDescriptor.m_renderAttachmentConfiguration = renderAttachmentConfiguration;
  26. pipelineStateDescriptor.m_renderStates.m_multisampleState = multisampleState;
  27. // No streams required
  28. RHI::InputStreamLayout inputStreamLayout;
  29. inputStreamLayout.SetTopology(RHI::PrimitiveTopology::TriangleList);
  30. inputStreamLayout.Finalize();
  31. pipelineStateDescriptor.m_inputStreamLayout = inputStreamLayout;
  32. m_shaderVariantTable[variationKey].m_pipelineState = shader->AcquirePipelineState(pipelineStateDescriptor);
  33. m_shaderVariantTable[variationKey].m_isFullyBaked = !shaderVariant.UseKeyFallback();
  34. }
  35. void PostProcessingShaderOptionBase::UpdateShaderVariant(const AZ::RPI::ShaderOptionGroup& shaderOption)
  36. {
  37. m_currentShaderVariantKeyValue = shaderOption.GetShaderVariantId().m_key.to_ullong();
  38. auto shaderVariant = GetShaderVariant(m_currentShaderVariantKeyValue);
  39. AZ_Assert(shaderVariant != nullptr, "Couldn't get a shader variation using the shader variant key[0x%llx].", m_currentShaderVariantKeyValue);
  40. if (!shaderVariant->m_isFullyBaked)
  41. {
  42. m_currentShaderVariantKeyFallbackValue = shaderOption.GetShaderVariantKeyFallbackValue();
  43. }
  44. }
  45. void PostProcessingShaderOptionBase::CompileShaderVariant(Data::Instance<AZ::RPI::ShaderResourceGroup>& shaderResourceGroup)
  46. {
  47. // Set the shader variant key falback value to the shader resource group if needed.
  48. auto shaderVariant = GetShaderVariant(m_currentShaderVariantKeyValue);
  49. if (shaderVariant != nullptr &&
  50. !shaderVariant->m_isFullyBaked &&
  51. shaderResourceGroup->HasShaderVariantKeyFallbackEntry())
  52. {
  53. shaderResourceGroup->SetShaderVariantKeyFallbackValue(m_currentShaderVariantKeyFallbackValue);
  54. }
  55. }
  56. const AZ::RHI::PipelineState* PostProcessingShaderOptionBase::GetPipelineStateFromShaderVariant()const
  57. {
  58. auto shaderVariant = GetShaderVariant(m_currentShaderVariantKeyValue);
  59. AZ_Assert(shaderVariant != nullptr, "Couldn't get a shader variation using the shader variant key[0x%llx].", m_currentShaderVariantKeyValue);
  60. return shaderVariant->m_pipelineState;
  61. }
  62. const PostProcessingShaderOptionBase::ShaderVariantInformation* PostProcessingShaderOptionBase::GetShaderVariant(AZ::u64 key) const
  63. {
  64. auto result = m_shaderVariantTable.find(key);
  65. return result != m_shaderVariantTable.end() ? &result->second : nullptr;
  66. }
  67. } // namespace Render
  68. } // namespace AZ