ShaderVariantAssetCreator.cpp 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100
  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/RPI.Edit/Shader/ShaderVariantAssetCreator.h>
  9. #include <AzCore/Utils/TypeHash.h>
  10. #include <Atom/RPI.Reflect/Shader/ShaderOptionGroup.h>
  11. namespace AZ
  12. {
  13. namespace RPI
  14. {
  15. void ShaderVariantAssetCreator::Begin(
  16. const AZ::Data::AssetId& assetId,
  17. const ShaderVariantId& shaderVariantId,
  18. RPI::ShaderVariantStableId stableId,
  19. bool isFullyBaked)
  20. {
  21. BeginCommon(assetId);
  22. if (ValidateIsReady())
  23. {
  24. m_asset->m_stableId = stableId;
  25. m_asset->m_shaderVariantId = shaderVariantId;
  26. m_asset->m_isFullyBaked = isFullyBaked;
  27. }
  28. }
  29. bool ShaderVariantAssetCreator::End(Data::Asset<ShaderVariantAsset>& result)
  30. {
  31. if (!ValidateIsReady())
  32. {
  33. return false;
  34. }
  35. if (!m_asset->FinalizeAfterLoad())
  36. {
  37. ReportError("Failed to finalize the ShaderResourceGroupAsset.");
  38. return false;
  39. }
  40. bool foundDrawFunctions = false;
  41. bool foundDispatchFunctions = false;
  42. if (m_asset->GetShaderStageFunction(RHI::ShaderStage::Vertex) ||
  43. m_asset->GetShaderStageFunction(RHI::ShaderStage::Geometry) ||
  44. m_asset->GetShaderStageFunction(RHI::ShaderStage::Fragment))
  45. {
  46. foundDrawFunctions = true;
  47. }
  48. if (m_asset->GetShaderStageFunction(RHI::ShaderStage::Compute))
  49. {
  50. foundDispatchFunctions = true;
  51. }
  52. if (foundDrawFunctions && foundDispatchFunctions)
  53. {
  54. ReportError("ShaderVariant contains both Draw functions and Dispatch functions.");
  55. return false;
  56. }
  57. if (m_asset->GetShaderStageFunction(RHI::ShaderStage::Fragment) &&
  58. !m_asset->GetShaderStageFunction(RHI::ShaderStage::Vertex))
  59. {
  60. ReportError("Shader Variant with StableId '%u' has a fragment function but no vertex function.", m_asset->m_stableId);
  61. return false;
  62. }
  63. if (m_asset->GetShaderStageFunction(RHI::ShaderStage::Geometry) &&
  64. !m_asset->GetShaderStageFunction(RHI::ShaderStage::Vertex))
  65. {
  66. ReportError("Shader Variant with StableId '%u' has a geometry function but no vertex function.", m_asset->m_stableId);
  67. return false;
  68. }
  69. m_asset->SetReady();
  70. return EndCommon(result);
  71. }
  72. /////////////////////////////////////////////////////////////////////
  73. // Methods for all shader variant types
  74. void ShaderVariantAssetCreator::SetShaderFunction(RHI::ShaderStage shaderStage, RHI::Ptr<RHI::ShaderStageFunction> shaderStageFunction)
  75. {
  76. if (ValidateIsReady())
  77. {
  78. m_asset->m_functionsByStage[static_cast<size_t>(shaderStage)] = shaderStageFunction;
  79. }
  80. }
  81. } // namespace RPI
  82. } // namespace AZ