MaterialPropertyDescriptor.cpp 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304
  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.Reflect/Material/MaterialPropertyDescriptor.h>
  9. #include <AzCore/RTTI/BehaviorContext.h>
  10. #include <AzCore/Math/Vector2.h>
  11. #include <AzCore/Math/Vector3.h>
  12. #include <AzCore/Math/Vector4.h>
  13. #include <AzCore/Math/Color.h>
  14. namespace AZ
  15. {
  16. namespace RPI
  17. {
  18. const char* ToString(MaterialPropertyOutputType materialPropertyOutputType)
  19. {
  20. switch (materialPropertyOutputType)
  21. {
  22. case MaterialPropertyOutputType::ShaderInput: return "ShaderInput";
  23. case MaterialPropertyOutputType::ShaderOption: return "ShaderOption";
  24. case MaterialPropertyOutputType::ShaderEnabled: return "ShaderEnabled";
  25. case MaterialPropertyOutputType::InternalProperty: return "InternalProperty";
  26. default:
  27. AZ_Assert(false, "Unhandled type");
  28. return "<Unknown>";
  29. }
  30. }
  31. const char* ToString(MaterialPropertyDataType materialPropertyDataType)
  32. {
  33. switch (materialPropertyDataType)
  34. {
  35. case MaterialPropertyDataType::Bool: return "Bool";
  36. case MaterialPropertyDataType::Int: return "Int";
  37. case MaterialPropertyDataType::UInt: return "UInt";
  38. case MaterialPropertyDataType::Float: return "Float";
  39. case MaterialPropertyDataType::Vector2: return "Vector2";
  40. case MaterialPropertyDataType::Vector3: return "Vector3";
  41. case MaterialPropertyDataType::Vector4: return "Vector4";
  42. case MaterialPropertyDataType::Color: return "Color";
  43. case MaterialPropertyDataType::Image: return "Image";
  44. case MaterialPropertyDataType::Enum: return "Enum";
  45. case MaterialPropertyDataType::Invalid: return "Invalid";
  46. default:
  47. AZ_Assert(false, "Unhandled type");
  48. return "<Unknown>";
  49. }
  50. }
  51. AZStd::string GetMaterialPropertyDataTypeString(AZ::TypeId typeId)
  52. {
  53. if (typeId == azrtti_typeid<bool>())
  54. {
  55. return ToString(MaterialPropertyDataType::Bool);
  56. }
  57. else if (typeId == azrtti_typeid<int32_t>())
  58. {
  59. return ToString(MaterialPropertyDataType::Int);
  60. }
  61. else if (typeId == azrtti_typeid<uint32_t>())
  62. {
  63. return ToString(MaterialPropertyDataType::UInt);
  64. }
  65. else if (typeId == azrtti_typeid<float>())
  66. {
  67. return ToString(MaterialPropertyDataType::Float);
  68. }
  69. else if (typeId == azrtti_typeid<Vector2>())
  70. {
  71. return ToString(MaterialPropertyDataType::Vector2);
  72. }
  73. else if (typeId == azrtti_typeid<Vector3>())
  74. {
  75. return ToString(MaterialPropertyDataType::Vector3);
  76. }
  77. else if (typeId == azrtti_typeid<Vector4>())
  78. {
  79. return ToString(MaterialPropertyDataType::Vector4);
  80. }
  81. else if (typeId == azrtti_typeid<Color>())
  82. {
  83. return ToString(MaterialPropertyDataType::Color);
  84. }
  85. else if (typeId == azrtti_typeid<Color>())
  86. {
  87. return ToString(MaterialPropertyDataType::Color);
  88. }
  89. else if (typeId == azrtti_typeid<Data::Instance<Image>>())
  90. {
  91. return ToString(MaterialPropertyDataType::Image);
  92. }
  93. else
  94. {
  95. return AZStd::string::format("<Unkonwn type %s>", typeId.ToString<AZStd::string>().c_str());
  96. }
  97. }
  98. bool ValidateMaterialPropertyDataType(TypeId typeId, const MaterialPropertyDescriptor* materialPropertyDescriptor, AZStd::function<void(const char*)> onError)
  99. {
  100. auto toMaterialPropertyDataType = [](TypeId typeId)
  101. {
  102. if (typeId == azrtti_typeid<bool>()) { return MaterialPropertyDataType::Bool; }
  103. if (typeId == azrtti_typeid<int32_t>()) { return MaterialPropertyDataType::Int; }
  104. if (typeId == azrtti_typeid<uint32_t>()) { return MaterialPropertyDataType::UInt; }
  105. if (typeId == azrtti_typeid<float>()) { return MaterialPropertyDataType::Float; }
  106. if (typeId == azrtti_typeid<Vector2>()) { return MaterialPropertyDataType::Vector2; }
  107. if (typeId == azrtti_typeid<Vector3>()) { return MaterialPropertyDataType::Vector3; }
  108. if (typeId == azrtti_typeid<Vector4>()) { return MaterialPropertyDataType::Vector4; }
  109. if (typeId == azrtti_typeid<Color>()) { return MaterialPropertyDataType::Color; }
  110. if (typeId == azrtti_typeid<Data::Asset<ImageAsset>>()) { return MaterialPropertyDataType::Image; }
  111. else
  112. {
  113. return MaterialPropertyDataType::Invalid;
  114. }
  115. };
  116. auto expectedDataType = materialPropertyDescriptor->GetDataType();
  117. auto actualDataType = toMaterialPropertyDataType(typeId);
  118. if (expectedDataType == MaterialPropertyDataType::Enum)
  119. {
  120. if (actualDataType != MaterialPropertyDataType::UInt)
  121. {
  122. onError(
  123. AZStd::string::format("Material property '%s' is a Enum type, can only accept UInt value, input value is %s",
  124. materialPropertyDescriptor->GetName().GetCStr(),
  125. ToString(actualDataType)
  126. ).data());
  127. return false;
  128. }
  129. }
  130. else
  131. {
  132. if (expectedDataType != actualDataType)
  133. {
  134. onError(
  135. AZStd::string::format("Material property '%s': Type mismatch. Expected %s but was %s",
  136. materialPropertyDescriptor->GetName().GetCStr(),
  137. ToString(expectedDataType),
  138. ToString(actualDataType)
  139. ).data());
  140. return false;
  141. }
  142. }
  143. return true;
  144. }
  145. void MaterialPropertyOutputId::Reflect(ReflectContext* context)
  146. {
  147. if (auto* serializeContext = azrtti_cast<SerializeContext*>(context))
  148. {
  149. serializeContext->Class<MaterialPropertyOutputId>()
  150. ->Version(2)
  151. ->Field("m_type", &MaterialPropertyOutputId::m_type)
  152. ->Field("m_materialPipelineName", &MaterialPropertyOutputId::m_materialPipelineName)
  153. ->Field("m_containerIndex", &MaterialPropertyOutputId::m_containerIndex)
  154. ->Field("m_itemIndex", &MaterialPropertyOutputId::m_itemIndex)
  155. ;
  156. }
  157. }
  158. void MaterialPropertyDescriptor::Reflect(ReflectContext* context)
  159. {
  160. if (auto* serializeContext = azrtti_cast<SerializeContext*>(context))
  161. {
  162. serializeContext->Enum<MaterialPropertyOutputType>()
  163. ->Value(ToString(MaterialPropertyOutputType::ShaderInput), MaterialPropertyOutputType::ShaderInput)
  164. ->Value(ToString(MaterialPropertyOutputType::ShaderOption), MaterialPropertyOutputType::ShaderOption)
  165. ->Value(ToString(MaterialPropertyOutputType::ShaderEnabled), MaterialPropertyOutputType::ShaderEnabled)
  166. ->Value(ToString(MaterialPropertyOutputType::InternalProperty), MaterialPropertyOutputType::InternalProperty)
  167. ;
  168. serializeContext->Enum<MaterialPropertyDataType>()
  169. ->Value(ToString(MaterialPropertyDataType::Invalid), MaterialPropertyDataType::Invalid)
  170. ->Value(ToString(MaterialPropertyDataType::Bool), MaterialPropertyDataType::Bool)
  171. ->Value(ToString(MaterialPropertyDataType::Int), MaterialPropertyDataType::Int)
  172. ->Value(ToString(MaterialPropertyDataType::UInt), MaterialPropertyDataType::UInt)
  173. ->Value(ToString(MaterialPropertyDataType::Float), MaterialPropertyDataType::Float)
  174. ->Value(ToString(MaterialPropertyDataType::Vector2), MaterialPropertyDataType::Vector2)
  175. ->Value(ToString(MaterialPropertyDataType::Vector3), MaterialPropertyDataType::Vector3)
  176. ->Value(ToString(MaterialPropertyDataType::Vector4), MaterialPropertyDataType::Vector4)
  177. ->Value(ToString(MaterialPropertyDataType::Color), MaterialPropertyDataType::Color)
  178. ->Value(ToString(MaterialPropertyDataType::Image), MaterialPropertyDataType::Image)
  179. ->Value(ToString(MaterialPropertyDataType::Enum), MaterialPropertyDataType::Enum)
  180. ;
  181. serializeContext->Class<MaterialPropertyDescriptor>()
  182. ->Version(2)
  183. ->Field("Name", &MaterialPropertyDescriptor::m_nameId)
  184. ->Field("DataType", &MaterialPropertyDescriptor::m_dataType)
  185. ->Field("OutputConnections", &MaterialPropertyDescriptor::m_outputConnections)
  186. ->Field("EnumNames", &MaterialPropertyDescriptor::m_enumNames)
  187. ;
  188. }
  189. MaterialPropertyIndex::Reflect(context);
  190. }
  191. const Name& MaterialPropertyDescriptor::GetName() const
  192. {
  193. return m_nameId;
  194. }
  195. const MaterialPropertyDescriptor::OutputList& MaterialPropertyDescriptor::GetOutputConnections() const
  196. {
  197. return m_outputConnections;
  198. }
  199. MaterialPropertyDataType MaterialPropertyDescriptor::GetDataType() const
  200. {
  201. return m_dataType;
  202. }
  203. AZ::TypeId MaterialPropertyDescriptor::GetAssetDataTypeId() const
  204. {
  205. switch (m_dataType)
  206. {
  207. case MaterialPropertyDataType::Bool:
  208. return azrtti_typeid<bool>();
  209. case MaterialPropertyDataType::Int:
  210. return azrtti_typeid<int32_t>();
  211. case MaterialPropertyDataType::UInt:
  212. return azrtti_typeid<uint32_t>();
  213. case MaterialPropertyDataType::Float:
  214. return azrtti_typeid<float>();
  215. case MaterialPropertyDataType::Vector2:
  216. return azrtti_typeid<Vector2>();
  217. case MaterialPropertyDataType::Vector3:
  218. return azrtti_typeid<Vector3>();
  219. case MaterialPropertyDataType::Vector4:
  220. return azrtti_typeid<Vector4>();
  221. case MaterialPropertyDataType::Color:
  222. return azrtti_typeid<Color>();
  223. case MaterialPropertyDataType::Enum:
  224. return azrtti_typeid<uint32_t>();
  225. case MaterialPropertyDataType::Image:
  226. return azrtti_typeid<Data::Asset<ImageAsset>>();
  227. default:
  228. AZ_Error("MaterialPropertyDescriptor", false, "Unhandle material property type %s.", ToString(m_dataType));
  229. return Uuid::CreateNull();
  230. }
  231. }
  232. AZ::TypeId MaterialPropertyDescriptor::GetStorageDataTypeId() const
  233. {
  234. switch (m_dataType)
  235. {
  236. case MaterialPropertyDataType::Bool:
  237. return azrtti_typeid<bool>();
  238. case MaterialPropertyDataType::Int:
  239. return azrtti_typeid<int32_t>();
  240. case MaterialPropertyDataType::UInt:
  241. return azrtti_typeid<uint32_t>();
  242. case MaterialPropertyDataType::Float:
  243. return azrtti_typeid<float>();
  244. case MaterialPropertyDataType::Vector2:
  245. return azrtti_typeid<Vector2>();
  246. case MaterialPropertyDataType::Vector3:
  247. return azrtti_typeid<Vector3>();
  248. case MaterialPropertyDataType::Vector4:
  249. return azrtti_typeid<Vector4>();
  250. case MaterialPropertyDataType::Color:
  251. return azrtti_typeid<Color>();
  252. case MaterialPropertyDataType::Enum:
  253. case MaterialPropertyDataType::Image:
  254. return azrtti_typeid<AZStd::string>();
  255. default:
  256. AZ_Error("MaterialPropertyDescriptor", false, "Unhandle material property type %s.", ToString(m_dataType));
  257. return Uuid::CreateNull();
  258. }
  259. }
  260. uint32_t MaterialPropertyDescriptor::GetEnumValue(const AZ::Name& enumName) const
  261. {
  262. const uint32_t total = aznumeric_cast<uint32_t>(m_enumNames.size());
  263. for (uint32_t i = 0; i < total; ++i)
  264. {
  265. if (m_enumNames[i] == enumName)
  266. {
  267. return i;
  268. }
  269. }
  270. return InvalidEnumValue;
  271. }
  272. const AZ::Name& MaterialPropertyDescriptor::GetEnumName(uint32_t enumValue) const
  273. {
  274. if (enumValue < m_enumNames.size())
  275. {
  276. return m_enumNames.at(enumValue);
  277. }
  278. static AZ::Name EmptyName = AZ::Name();
  279. return EmptyName;
  280. }
  281. } // namespace RPI
  282. } // namespace AZ