MaterialBuilderUtils.cpp 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105
  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 "MaterialBuilderUtils.h"
  9. #include <Atom/RPI.Edit/Common/AssetUtils.h>
  10. #include <Atom/RPI.Edit/Material/MaterialSourceData.h>
  11. #include <Atom/RPI.Edit/Material/MaterialTypeSourceData.h>
  12. #include <Atom/RPI.Reflect/Image/StreamingImageAsset.h>
  13. #include <Atom/RPI.Reflect/Material/MaterialAsset.h>
  14. #include <Atom/RPI.Reflect/Material/MaterialTypeAsset.h>
  15. #include <AzCore/Settings/SettingsRegistry.h>
  16. #include <AzToolsFramework/API/EditorAssetSystemAPI.h>
  17. namespace AZ::RPI::MaterialBuilderUtils
  18. {
  19. AssetBuilderSDK::JobDependency& AddJobDependency(
  20. AssetBuilderSDK::JobDescriptor& jobDescriptor,
  21. const AZStd::string& path,
  22. const AZStd::string& jobKey,
  23. const AZStd::string& platformId,
  24. const AZStd::vector<AZ::u32>& subIds,
  25. const bool updateFingerprint)
  26. {
  27. if (updateFingerprint)
  28. {
  29. AddFingerprintForDependency(path, jobDescriptor);
  30. }
  31. AssetBuilderSDK::JobDependency jobDependency(
  32. jobKey,
  33. platformId,
  34. AssetBuilderSDK::JobDependencyType::Order,
  35. AssetBuilderSDK::SourceFileDependency(
  36. path, AZ::Uuid{}, AssetBuilderSDK::SourceFileDependency::SourceFileDependencyType::Absolute));
  37. jobDependency.m_productSubIds = subIds;
  38. return jobDescriptor.m_jobDependencyList.emplace_back(AZStd::move(jobDependency));
  39. }
  40. void AddImageAssetDependenciesToProduct(
  41. const MaterialPropertiesLayout* propertyLayout,
  42. const AZStd::vector<MaterialPropertyValue>& propertyValues,
  43. AssetBuilderSDK::JobProduct& product)
  44. {
  45. if (!propertyLayout)
  46. {
  47. return;
  48. }
  49. for (size_t propertyIndex = 0; propertyIndex < propertyLayout->GetPropertyCount(); ++propertyIndex)
  50. {
  51. auto descriptor = propertyLayout->GetPropertyDescriptor(AZ::RPI::MaterialPropertyIndex{ propertyIndex });
  52. if (descriptor->GetDataType() == MaterialPropertyDataType::Image)
  53. {
  54. if (propertyIndex >= propertyValues.size())
  55. {
  56. continue; // invalid index, but let's not crash!
  57. }
  58. auto propertyValue = propertyValues[propertyIndex];
  59. if (propertyValue.IsValid())
  60. {
  61. AZ::Data::Asset<ImageAsset> imageAsset = propertyValue.GetValue<AZ::Data::Asset<ImageAsset>>();
  62. if (imageAsset.GetId().IsValid())
  63. {
  64. // preload images (set to NoLoad to avoid this)
  65. auto loadFlags = AZ::Data::ProductDependencyInfo::CreateFlags(AZ::Data::AssetLoadBehavior::PreLoad);
  66. product.m_dependencies.push_back(AssetBuilderSDK::ProductDependency(imageAsset.GetId(), loadFlags));
  67. }
  68. }
  69. }
  70. }
  71. }
  72. void AddImageAssetDependenciesToProduct(const AZ::RPI::MaterialAsset* materialAsset, AssetBuilderSDK::JobProduct& product)
  73. {
  74. if (!materialAsset)
  75. {
  76. return;
  77. }
  78. AddImageAssetDependenciesToProduct(materialAsset->GetMaterialPropertiesLayout(), materialAsset->GetPropertyValues(), product);
  79. }
  80. void AddImageAssetDependenciesToProduct(const AZ::RPI::MaterialTypeAsset* materialTypeAsset, AssetBuilderSDK::JobProduct& product)
  81. {
  82. if (!materialTypeAsset)
  83. {
  84. return;
  85. }
  86. AddImageAssetDependenciesToProduct(materialTypeAsset->GetMaterialPropertiesLayout(), materialTypeAsset->GetDefaultPropertyValues(), product);
  87. }
  88. void AddFingerprintForDependency(const AZStd::string& path, AssetBuilderSDK::JobDescriptor& jobDescriptor)
  89. {
  90. jobDescriptor.m_additionalFingerprintInfo +=
  91. AZStd::string::format("|%u:%llu", (AZ::u32)AZ::Crc32(path), AZ::IO::SystemFile::ModificationTime(path.c_str()));
  92. }
  93. } // namespace AZ::RPI::MaterialBuilderUtils