3
0

LuaMaterialFunctor.cpp 54 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970
  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/LuaMaterialFunctor.h>
  9. #include <Atom/RPI.Reflect/Material/MaterialPropertiesLayout.h>
  10. #include <Atom/RPI.Reflect/Material/LuaScriptUtilities.h>
  11. #include <Atom/RPI.Public/Shader/ShaderResourceGroup.h>
  12. #include <AzCore/Asset/AssetSerializer.h>
  13. #include <AzCore/Script/ScriptContext.h>
  14. #include <AzCore/Script/ScriptSystemBus.h>
  15. #include <AzCore/Script/ScriptAsset.h>
  16. #include <AzCore/Math/MathReflection.h>
  17. #include <AzCore/Math/Vector2.h>
  18. #include <AzCore/Math/Vector3.h>
  19. #include <AzCore/Math/Vector4.h>
  20. #include <AzCore/Math/Color.h>
  21. namespace AZ
  22. {
  23. namespace RPI
  24. {
  25. void LuaMaterialFunctor::Reflect(ReflectContext* context)
  26. {
  27. if (auto* serializeContext = azrtti_cast<SerializeContext*>(context))
  28. {
  29. serializeContext->Class<LuaMaterialFunctor, RPI::MaterialFunctor>()
  30. ->Version(1)
  31. ->Field("scriptAsset", &LuaMaterialFunctor::m_scriptAsset)
  32. ->Field("materialNameContext", &LuaMaterialFunctor::m_materialNameContext)
  33. ;
  34. }
  35. }
  36. LuaMaterialFunctor::LuaMaterialFunctor()
  37. {
  38. // [GFX TODO][ATOM-13648] Add local system allocator to material system
  39. // ScriptContext creates a new allocator if null (default) is passed in.
  40. // Temporarily using system allocator for preventing hitting the max allocator number.
  41. m_scriptContext = AZStd::make_unique<AZ::ScriptContext>(AZ_CRC_CE("MaterialFunctor"), &AZ::AllocatorInstance<AZ::SystemAllocator>::Get());
  42. m_sriptBehaviorContext = AZStd::make_unique<AZ::BehaviorContext>();
  43. ReflectScriptContext(m_sriptBehaviorContext.get());
  44. m_scriptContext->BindTo(m_sriptBehaviorContext.get());
  45. }
  46. void LuaMaterialFunctor::ReflectScriptContext(AZ::BehaviorContext* behaviorContext)
  47. {
  48. AZ::MathReflect(behaviorContext);
  49. // We don't need any functions in Image, but just need BehaviorContext to be aware of this
  50. // type so we can pass around image pointers within lua scripts.
  51. behaviorContext->Class<Image>();
  52. MaterialPropertyDescriptor::Reflect(behaviorContext);
  53. ReflectMaterialDynamicMetadata(behaviorContext);
  54. LuaMaterialFunctorAPI::RenderStates::Reflect(behaviorContext);
  55. LuaMaterialFunctorAPI::ShaderItem::Reflect(behaviorContext);
  56. LuaScriptUtilities::Reflect(behaviorContext);
  57. LuaMaterialFunctorAPI::RuntimeContext::Reflect(behaviorContext);
  58. LuaMaterialFunctorAPI::PipelineRuntimeContext::Reflect(behaviorContext);
  59. LuaMaterialFunctorAPI::EditorContext::Reflect(behaviorContext);
  60. }
  61. const AZStd::vector<char>& LuaMaterialFunctor::GetScriptBuffer() const
  62. {
  63. if (!m_scriptBuffer.empty())
  64. {
  65. AZ_Warning("LuaMaterialFunctor", m_scriptAsset.GetId().IsValid() == false, "LuaMaterialFunctor has both a built-in script and an external script asset. The external script will be ignored.");
  66. return m_scriptBuffer;
  67. }
  68. else if (m_scriptAsset.IsReady())
  69. {
  70. return m_scriptAsset->m_data.GetScriptBuffer();
  71. }
  72. else
  73. {
  74. AZ_Error("LuaMaterialFunctor", false, "LuaMaterialFunctor has no script data.");
  75. return m_scriptBuffer;
  76. }
  77. }
  78. const char* LuaMaterialFunctor::GetScriptDescription() const
  79. {
  80. if (!m_scriptBuffer.empty())
  81. {
  82. return "<built-in functor script>";
  83. }
  84. else if (m_scriptAsset.IsReady())
  85. {
  86. return m_scriptAsset.GetHint().c_str();
  87. }
  88. else
  89. {
  90. return "<none>";
  91. }
  92. }
  93. void LuaMaterialFunctor::InitScriptContext()
  94. {
  95. if (m_scriptStatus == ScriptStatus::Uninitialized)
  96. {
  97. auto scriptBuffer = GetScriptBuffer();
  98. if (!m_scriptContext->Execute(scriptBuffer.data(), GetScriptDescription(), scriptBuffer.size()))
  99. {
  100. AZ_Error(LuaScriptUtilities::DebugName, false, "Error initializing script '%s'.", m_scriptAsset.ToString<AZStd::string>().c_str());
  101. m_scriptStatus = ScriptStatus::Error;
  102. }
  103. else
  104. {
  105. m_scriptStatus = ScriptStatus::Ready;
  106. }
  107. }
  108. }
  109. void LuaMaterialFunctor::Process(MaterialFunctorAPI::RuntimeContext& context)
  110. {
  111. AZ_PROFILE_FUNCTION(RPI);
  112. InitScriptContext();
  113. if (m_scriptStatus == ScriptStatus::Ready)
  114. {
  115. LuaMaterialFunctorAPI::RuntimeContext luaContext{&context, &GetMaterialPropertyDependencies(), &m_materialNameContext};
  116. AZ::ScriptDataContext call;
  117. if (m_scriptContext->Call("Process", call))
  118. {
  119. call.PushArg(luaContext);
  120. call.CallExecute();
  121. }
  122. }
  123. }
  124. void LuaMaterialFunctor::Process(MaterialFunctorAPI::PipelineRuntimeContext& context)
  125. {
  126. AZ_PROFILE_FUNCTION(RPI);
  127. InitScriptContext();
  128. if (m_scriptStatus == ScriptStatus::Ready)
  129. {
  130. LuaMaterialFunctorAPI::PipelineRuntimeContext luaContext{&context, &GetMaterialPropertyDependencies(), &m_materialNameContext};
  131. AZ::ScriptDataContext call;
  132. if (m_scriptContext->Call("Process", call))
  133. {
  134. call.PushArg(luaContext);
  135. call.CallExecute();
  136. }
  137. }
  138. }
  139. void LuaMaterialFunctor::Process(MaterialFunctorAPI::EditorContext& context)
  140. {
  141. AZ_PROFILE_FUNCTION(RPI);
  142. InitScriptContext();
  143. if (m_scriptStatus == ScriptStatus::Ready)
  144. {
  145. LuaMaterialFunctorAPI::EditorContext luaContext{&context, &m_materialNameContext};
  146. AZ::ScriptDataContext call;
  147. if (m_scriptContext->Call("ProcessEditor", call))
  148. {
  149. call.PushArg(luaContext);
  150. call.CallExecute();
  151. }
  152. }
  153. }
  154. LuaMaterialFunctorAPI::CommonRuntimeConfiguration::CommonRuntimeConfiguration(
  155. MaterialPropertyPsoHandling psoHandling,
  156. const MaterialPropertyFlags* materialPropertyDependencies,
  157. const MaterialPropertiesLayout* materialPropertiesLayout)
  158. : m_psoHandling(psoHandling)
  159. , m_materialPropertyDependencies(materialPropertyDependencies)
  160. , m_materialPropertiesLayout(materialPropertiesLayout)
  161. {
  162. }
  163. AZStd::string LuaMaterialFunctorAPI::CommonRuntimeConfiguration::GetMaterialPropertyDependenciesString() const
  164. {
  165. AZStd::vector<AZStd::string> propertyList;
  166. for (size_t i = 0; i < m_materialPropertyDependencies->size(); ++i)
  167. {
  168. if ((*m_materialPropertyDependencies)[i])
  169. {
  170. propertyList.push_back(m_materialPropertiesLayout->GetPropertyDescriptor(MaterialPropertyIndex{i})->GetName().GetStringView());
  171. }
  172. }
  173. AZStd::string propertyListString;
  174. AzFramework::StringFunc::Join(propertyListString, propertyList.begin(), propertyList.end(), ", ");
  175. return propertyListString;
  176. }
  177. bool LuaMaterialFunctorAPI::CommonRuntimeConfiguration::CheckPsoChangesAllowed()
  178. {
  179. if (m_psoHandling == MaterialPropertyPsoHandling::Error)
  180. {
  181. if (!m_psoChangesReported)
  182. {
  183. LuaScriptUtilities::Error(
  184. AZStd::string::format(
  185. "The following material properties must not be changed at runtime because they impact Pipeline State Objects: %s", GetMaterialPropertyDependenciesString().c_str()));
  186. m_psoChangesReported = true;
  187. }
  188. return false;
  189. }
  190. else if (m_psoHandling == MaterialPropertyPsoHandling::Warning)
  191. {
  192. if (!m_psoChangesReported)
  193. {
  194. LuaScriptUtilities::Warning(
  195. AZStd::string::format(
  196. "The following material properties should not be changed at runtime because they impact Pipeline State Objects: %s", GetMaterialPropertyDependenciesString().c_str()));
  197. m_psoChangesReported = true;
  198. }
  199. }
  200. return true;
  201. }
  202. LuaMaterialFunctorAPI::ReadMaterialPropertyValues::ReadMaterialPropertyValues(
  203. MaterialFunctorAPI::ReadMaterialPropertyValues* underlyingApi,
  204. const MaterialNameContext* materialNameContext)
  205. : m_underlyingApi(underlyingApi)
  206. , m_materialNameContext(materialNameContext)
  207. {
  208. }
  209. MaterialPropertyIndex LuaMaterialFunctorAPI::ReadMaterialPropertyValues::GetMaterialPropertyIndex(const char* name, const char* functionName) const
  210. {
  211. MaterialPropertyIndex propertyIndex;
  212. Name propertyFullName{name};
  213. m_materialNameContext->ContextualizeProperty(propertyFullName);
  214. propertyIndex = m_underlyingApi->GetMaterialPropertiesLayout()->FindPropertyIndex(propertyFullName);
  215. if (!propertyIndex.IsValid())
  216. {
  217. LuaScriptUtilities::Error(AZStd::string::format("%s() could not find property '%s'", functionName, propertyFullName.GetCStr()));
  218. }
  219. return propertyIndex;
  220. }
  221. const MaterialPropertyValue& LuaMaterialFunctorAPI::ReadMaterialPropertyValues::GetMaterialPropertyValue(MaterialPropertyIndex propertyIndex) const
  222. {
  223. return m_underlyingApi->GetMaterialPropertyValue(propertyIndex);
  224. }
  225. template<typename Type>
  226. Type LuaMaterialFunctorAPI::ReadMaterialPropertyValues::GetMaterialPropertyValue(const char* name) const
  227. {
  228. MaterialPropertyIndex index = GetMaterialPropertyIndex(name, "GetMaterialPropertyValue");
  229. if (!index.IsValid())
  230. {
  231. return {};
  232. }
  233. const MaterialPropertyValue& value = GetMaterialPropertyValue(index);
  234. if (!value.IsValid())
  235. {
  236. LuaScriptUtilities::Error(AZStd::string::format("GetMaterialPropertyValue() got invalid value for property '%s'", name));
  237. return {};
  238. }
  239. if (!value.Is<Type>())
  240. {
  241. LuaScriptUtilities::Error(AZStd::string::format("GetMaterialPropertyValue() accessed property '%s' using the wrong data type.", name));
  242. return {};
  243. }
  244. return value.GetValue<Type>();
  245. }
  246. // Specialize for type Image* because that will be more intuitive within Lua.
  247. // The script can then check the result for nil without calling "get()".
  248. // For example, "GetMaterialPropertyValue_Image(name) == nil" rather than "GetMaterialPropertyValue_Image(name):get() == nil"
  249. template<>
  250. Image* LuaMaterialFunctorAPI::ReadMaterialPropertyValues::GetMaterialPropertyValue(const char* name) const
  251. {
  252. return GetMaterialPropertyValue<Data::Instance<Image>>(name).get();
  253. }
  254. template<typename LuaApiClass>
  255. void LuaMaterialFunctorAPI::ReadMaterialPropertyValues::ReflectSubclass(BehaviorContext::ClassBuilder<LuaApiClass>* subclassBuilder)
  256. {
  257. subclassBuilder
  258. ->Method("GetMaterialPropertyValue_bool", &LuaApiClass::template GetMaterialPropertyValue<bool>)
  259. ->Method("GetMaterialPropertyValue_int", &LuaApiClass::template GetMaterialPropertyValue<int32_t>)
  260. ->Method("GetMaterialPropertyValue_uint", &LuaApiClass::template GetMaterialPropertyValue<uint32_t>)
  261. ->Method("GetMaterialPropertyValue_enum", &LuaApiClass::template GetMaterialPropertyValue<uint32_t>)
  262. ->Method("GetMaterialPropertyValue_float", &LuaApiClass::template GetMaterialPropertyValue<float>)
  263. ->Method("GetMaterialPropertyValue_Vector2", &LuaApiClass::template GetMaterialPropertyValue<Vector2>)
  264. ->Method("GetMaterialPropertyValue_Vector3", &LuaApiClass::template GetMaterialPropertyValue<Vector3>)
  265. ->Method("GetMaterialPropertyValue_Vector4", &LuaApiClass::template GetMaterialPropertyValue<Vector4>)
  266. ->Method("GetMaterialPropertyValue_Color", &LuaApiClass::template GetMaterialPropertyValue<Color>)
  267. ->Method("GetMaterialPropertyValue_Image", &LuaApiClass::template GetMaterialPropertyValue<Image*>)
  268. ->Method("HasMaterialProperty", &LuaApiClass::HasMaterialValue)
  269. ;
  270. }
  271. void LuaMaterialFunctorAPI::RuntimeContext::Reflect(BehaviorContext* behaviorContext)
  272. {
  273. auto builder = behaviorContext->Class<LuaMaterialFunctorAPI::RuntimeContext>();
  274. builder
  275. ->Method("SetShaderConstant_bool", &LuaMaterialFunctorAPI::RuntimeContext::SetShaderConstant<bool>)
  276. ->Method("SetShaderConstant_int", &LuaMaterialFunctorAPI::RuntimeContext::SetShaderConstant<int32_t>)
  277. ->Method("SetShaderConstant_uint", &LuaMaterialFunctorAPI::RuntimeContext::SetShaderConstant<uint32_t>)
  278. ->Method("SetShaderConstant_float", &LuaMaterialFunctorAPI::RuntimeContext::SetShaderConstant<float>)
  279. ->Method("SetShaderConstant_Vector2", &LuaMaterialFunctorAPI::RuntimeContext::SetShaderConstant<Vector2>)
  280. ->Method("SetShaderConstant_Vector3", &LuaMaterialFunctorAPI::RuntimeContext::SetShaderConstant<Vector3>)
  281. ->Method("SetShaderConstant_Vector4", &LuaMaterialFunctorAPI::RuntimeContext::SetShaderConstant<Vector4>)
  282. ->Method("SetShaderConstant_Color", &LuaMaterialFunctorAPI::RuntimeContext::SetShaderConstant<Color>)
  283. ->Method("SetShaderConstant_Matrix3x3", &LuaMaterialFunctorAPI::RuntimeContext::SetShaderConstant<Matrix3x3>)
  284. ->Method("SetShaderConstant_Matrix4x4", &LuaMaterialFunctorAPI::RuntimeContext::SetShaderConstant<Matrix4x4>)
  285. ->Method("SetInternalMaterialPropertyValue_bool", &LuaMaterialFunctorAPI::RuntimeContext::SetInternalMaterialPropertyValue<bool>)
  286. ->Method("SetInternalMaterialPropertyValue_int", &LuaMaterialFunctorAPI::RuntimeContext::SetInternalMaterialPropertyValue<int32_t>)
  287. ->Method("SetInternalMaterialPropertyValue_uint", &LuaMaterialFunctorAPI::RuntimeContext::SetInternalMaterialPropertyValue<uint32_t>)
  288. ->Method("SetInternalMaterialPropertyValue_enum", &LuaMaterialFunctorAPI::RuntimeContext::SetInternalMaterialPropertyValue<uint32_t>)
  289. ->Method("SetInternalMaterialPropertyValue_float", &LuaMaterialFunctorAPI::RuntimeContext::SetInternalMaterialPropertyValue<float>)
  290. // I'm not really sure what use case there might be for passing these data types to the material pipeline, but we might as well provide
  291. // them to remain consistent with the types that are supported by the GetMaterialPropertyValue function above.
  292. ->Method("SetInternalMaterialPropertyValue_Vector2", &LuaMaterialFunctorAPI::RuntimeContext::SetInternalMaterialPropertyValue<Vector2>)
  293. ->Method("SetInternalMaterialPropertyValue_Vector3", &LuaMaterialFunctorAPI::RuntimeContext::SetInternalMaterialPropertyValue<Vector3>)
  294. ->Method("SetInternalMaterialPropertyValue_Vector4", &LuaMaterialFunctorAPI::RuntimeContext::SetInternalMaterialPropertyValue<Vector4>)
  295. ->Method("SetInternalMaterialPropertyValue_Color", &LuaMaterialFunctorAPI::RuntimeContext::SetInternalMaterialPropertyValue<Color>)
  296. ->Method("SetInternalMaterialPropertyValue_Image", &LuaMaterialFunctorAPI::RuntimeContext::SetInternalMaterialPropertyValue<Image*>)
  297. ;
  298. LuaMaterialFunctorAPI::ReadMaterialPropertyValues::ReflectSubclass<LuaMaterialFunctorAPI::RuntimeContext>(&builder);
  299. LuaMaterialFunctorAPI::ConfigureShaders::ReflectSubclass<LuaMaterialFunctorAPI::RuntimeContext>(&builder);
  300. }
  301. template<typename LuaApiClass>
  302. void LuaMaterialFunctorAPI::ConfigureShaders::ReflectSubclass(BehaviorContext::ClassBuilder<LuaApiClass>* subclassBuilder)
  303. {
  304. subclassBuilder
  305. ->Method("SetShaderOptionValue_bool", &LuaApiClass::template SetShaderOptionValue<bool>)
  306. ->Method("SetShaderOptionValue_uint", &LuaApiClass::template SetShaderOptionValue<uint32_t>)
  307. ->Method("SetShaderOptionValue_enum", &LuaApiClass::template SetShaderOptionValue<const char*>)
  308. ->Method("GetShaderCount", &LuaApiClass::GetShaderCount)
  309. ->Method("GetShader", &LuaApiClass::GetShader)
  310. ->Method("GetShaderByTag", &LuaApiClass::GetShaderByTag)
  311. ->Method("HasShaderWithTag", &LuaApiClass::HasShaderWithTag)
  312. ;
  313. }
  314. LuaMaterialFunctorAPI::ConfigureShaders::ConfigureShaders(
  315. MaterialFunctorAPI::ConfigureShaders* underlyingApi,
  316. const MaterialNameContext* materialNameContext,
  317. CommonRuntimeConfiguration* commonRuntimeConfiguration)
  318. : m_underlyingApi(underlyingApi)
  319. , m_materialNameContext(materialNameContext)
  320. , m_commonRuntimeConfiguration(commonRuntimeConfiguration)
  321. {
  322. }
  323. bool LuaMaterialFunctorAPI::ReadMaterialPropertyValues::HasMaterialValue(const char* name) const
  324. {
  325. Name propertyFullName{name};
  326. m_materialNameContext->ContextualizeProperty(propertyFullName);
  327. MaterialPropertyIndex propertyIndex = m_underlyingApi->GetMaterialPropertiesLayout()->FindPropertyIndex(propertyFullName);
  328. return propertyIndex.IsValid();
  329. }
  330. template<>
  331. bool LuaMaterialFunctorAPI::ConfigureShaders::SetShaderOptionValue(const char* name, const char* value)
  332. {
  333. Name optionName{name};
  334. m_materialNameContext->ContextualizeShaderOption(optionName);
  335. return m_underlyingApi->SetShaderOptionValue(optionName, Name{value});
  336. }
  337. template<typename Type>
  338. bool LuaMaterialFunctorAPI::ConfigureShaders::SetShaderOptionValue(const char* name, Type value)
  339. {
  340. Name optionName{name};
  341. m_materialNameContext->ContextualizeShaderOption(optionName);
  342. return m_underlyingApi->SetShaderOptionValue(optionName, ShaderOptionValue{value});
  343. }
  344. RHI::ShaderInputConstantIndex LuaMaterialFunctorAPI::RuntimeContext::GetShaderInputConstantIndex(const char* name, const char* functionName) const
  345. {
  346. Name fullInputName{name};
  347. m_materialNameContext->ContextualizeSrgInput(fullInputName);
  348. RHI::ShaderInputConstantIndex index = m_runtimeContextImpl->GetShaderResourceGroup()->FindShaderInputConstantIndex(fullInputName);
  349. if (!index.IsValid())
  350. {
  351. LuaScriptUtilities::Error(AZStd::string::format("%s() could not find shader input '%s'", functionName, fullInputName.GetCStr()));
  352. }
  353. return index;
  354. }
  355. template<typename Type>
  356. bool LuaMaterialFunctorAPI::RuntimeContext::SetShaderConstant(const char* name, Type value)
  357. {
  358. RHI::ShaderInputConstantIndex index = GetShaderInputConstantIndex(name, "SetShaderConstant");
  359. if (index.IsValid())
  360. {
  361. return m_runtimeContextImpl->GetShaderResourceGroup()->SetConstant(index, value);
  362. }
  363. return false;
  364. }
  365. AZStd::size_t LuaMaterialFunctorAPI::ConfigureShaders::GetShaderCount() const
  366. {
  367. return m_underlyingApi->GetShaderCount();
  368. }
  369. LuaMaterialFunctorAPI::ShaderItem LuaMaterialFunctorAPI::ConfigureShaders::GetShader(AZStd::size_t index)
  370. {
  371. if (index < GetShaderCount())
  372. {
  373. return LuaMaterialFunctorAPI::ShaderItem{&(*m_underlyingApi->m_localShaderCollection)[index], m_commonRuntimeConfiguration};
  374. }
  375. else
  376. {
  377. LuaScriptUtilities::Error(AZStd::string::format("GetShader(%zu) is invalid.", index));
  378. return {};
  379. }
  380. }
  381. LuaMaterialFunctorAPI::ShaderItem LuaMaterialFunctorAPI::ConfigureShaders::GetShaderByTag(const char* shaderTag)
  382. {
  383. const AZ::Name tag{shaderTag};
  384. if (m_underlyingApi->m_localShaderCollection->HasShaderTag(tag))
  385. {
  386. return LuaMaterialFunctorAPI::ShaderItem{&(*m_underlyingApi->m_localShaderCollection)[tag], m_commonRuntimeConfiguration};
  387. }
  388. else
  389. {
  390. LuaScriptUtilities::Error(AZStd::string::format(
  391. "GetShaderByTag('%s') is invalid: Could not find a shader with the tag '%s'.", tag.GetCStr(), tag.GetCStr()));
  392. return {};
  393. }
  394. }
  395. bool LuaMaterialFunctorAPI::ConfigureShaders::HasShaderWithTag(const char* shaderTag)
  396. {
  397. return m_underlyingApi->m_localShaderCollection->HasShaderTag(AZ::Name{shaderTag});
  398. }
  399. LuaMaterialFunctorAPI::RuntimeContext::RuntimeContext(
  400. MaterialFunctorAPI::RuntimeContext* runtimeContextImpl,
  401. const MaterialPropertyFlags* materialPropertyDependencies,
  402. const MaterialNameContext* materialNameContext)
  403. : LuaMaterialFunctorAPI::CommonRuntimeConfiguration(runtimeContextImpl->GetMaterialPropertyPsoHandling(), materialPropertyDependencies, runtimeContextImpl->GetMaterialPropertiesLayout())
  404. , LuaMaterialFunctorAPI::ReadMaterialPropertyValues(runtimeContextImpl, materialNameContext)
  405. , LuaMaterialFunctorAPI::ConfigureShaders(runtimeContextImpl, materialNameContext, this)
  406. , m_runtimeContextImpl(runtimeContextImpl)
  407. , m_materialNameContext(materialNameContext)
  408. {
  409. }
  410. template<typename Type>
  411. Type LuaMaterialFunctorAPI::RuntimeContext::GetMaterialPropertyValue(const char* name) const
  412. {
  413. return LuaMaterialFunctorAPI::ReadMaterialPropertyValues::GetMaterialPropertyValue<Type>(name);
  414. }
  415. bool LuaMaterialFunctorAPI::RuntimeContext::HasMaterialValue(const char* name) const
  416. {
  417. return LuaMaterialFunctorAPI::ReadMaterialPropertyValues::HasMaterialValue(name);
  418. }
  419. template<typename Type>
  420. bool LuaMaterialFunctorAPI::RuntimeContext::SetShaderOptionValue(const char* name, Type value)
  421. {
  422. return LuaMaterialFunctorAPI::ConfigureShaders::SetShaderOptionValue(name, value);
  423. }
  424. AZStd::size_t LuaMaterialFunctorAPI::RuntimeContext::GetShaderCount() const
  425. {
  426. return LuaMaterialFunctorAPI::ConfigureShaders::GetShaderCount();
  427. }
  428. LuaMaterialFunctorAPI::ShaderItem LuaMaterialFunctorAPI::RuntimeContext::GetShader(AZStd::size_t index)
  429. {
  430. return LuaMaterialFunctorAPI::ConfigureShaders::GetShader(index);
  431. }
  432. LuaMaterialFunctorAPI::ShaderItem LuaMaterialFunctorAPI::RuntimeContext::GetShaderByTag(const char* shaderTag)
  433. {
  434. return LuaMaterialFunctorAPI::ConfigureShaders::GetShaderByTag(shaderTag);
  435. }
  436. bool LuaMaterialFunctorAPI::RuntimeContext::HasShaderWithTag(const char* shaderTag)
  437. {
  438. return LuaMaterialFunctorAPI::ConfigureShaders::HasShaderWithTag(shaderTag);
  439. }
  440. template<typename T>
  441. bool LuaMaterialFunctorAPI::RuntimeContext::SetInternalMaterialPropertyValue(const char* name, T value)
  442. {
  443. return m_runtimeContextImpl->SetInternalMaterialPropertyValue(AZ::Name{name}, value);
  444. }
  445. void LuaMaterialFunctorAPI::PipelineRuntimeContext::Reflect(BehaviorContext* behaviorContext)
  446. {
  447. auto builder = behaviorContext->Class<PipelineRuntimeContext>();
  448. LuaMaterialFunctorAPI::ReadMaterialPropertyValues::ReflectSubclass<LuaMaterialFunctorAPI::PipelineRuntimeContext>(&builder);
  449. LuaMaterialFunctorAPI::ConfigureShaders::ReflectSubclass<LuaMaterialFunctorAPI::PipelineRuntimeContext>(&builder);
  450. }
  451. LuaMaterialFunctorAPI::PipelineRuntimeContext::PipelineRuntimeContext(
  452. MaterialFunctorAPI::PipelineRuntimeContext* runtimeContextImpl,
  453. const MaterialPropertyFlags* materialPropertyDependencies,
  454. const MaterialNameContext* materialNameContext)
  455. : LuaMaterialFunctorAPI::CommonRuntimeConfiguration(runtimeContextImpl->GetMaterialPropertyPsoHandling(), materialPropertyDependencies, runtimeContextImpl->GetMaterialPropertiesLayout())
  456. , LuaMaterialFunctorAPI::ReadMaterialPropertyValues(runtimeContextImpl, materialNameContext)
  457. , LuaMaterialFunctorAPI::ConfigureShaders(runtimeContextImpl, materialNameContext, this)
  458. {
  459. }
  460. template<typename Type>
  461. Type LuaMaterialFunctorAPI::PipelineRuntimeContext::GetMaterialPropertyValue(const char* name) const
  462. {
  463. return LuaMaterialFunctorAPI::ReadMaterialPropertyValues::GetMaterialPropertyValue<Type>(name);
  464. }
  465. bool LuaMaterialFunctorAPI::PipelineRuntimeContext::HasMaterialValue(const char* name) const
  466. {
  467. return LuaMaterialFunctorAPI::ReadMaterialPropertyValues::HasMaterialValue(name);
  468. }
  469. template<typename Type>
  470. bool LuaMaterialFunctorAPI::PipelineRuntimeContext::SetShaderOptionValue(const char* name, Type value)
  471. {
  472. return LuaMaterialFunctorAPI::ConfigureShaders::SetShaderOptionValue(name, value);
  473. }
  474. AZStd::size_t LuaMaterialFunctorAPI::PipelineRuntimeContext::GetShaderCount() const
  475. {
  476. return LuaMaterialFunctorAPI::ConfigureShaders::GetShaderCount();
  477. }
  478. LuaMaterialFunctorAPI::ShaderItem LuaMaterialFunctorAPI::PipelineRuntimeContext::GetShader(AZStd::size_t index)
  479. {
  480. return LuaMaterialFunctorAPI::ConfigureShaders::GetShader(index);
  481. }
  482. LuaMaterialFunctorAPI::ShaderItem LuaMaterialFunctorAPI::PipelineRuntimeContext::GetShaderByTag(const char* shaderTag)
  483. {
  484. return LuaMaterialFunctorAPI::ConfigureShaders::GetShaderByTag(shaderTag);
  485. }
  486. bool LuaMaterialFunctorAPI::PipelineRuntimeContext::HasShaderWithTag(const char* shaderTag)
  487. {
  488. return LuaMaterialFunctorAPI::ConfigureShaders::HasShaderWithTag(shaderTag);
  489. }
  490. void LuaMaterialFunctorAPI::EditorContext::EditorContext::Reflect(BehaviorContext* behaviorContext)
  491. {
  492. auto builder = behaviorContext->Class<LuaMaterialFunctorAPI::EditorContext>();
  493. builder
  494. ->Method("SetMaterialPropertyVisibility", &LuaMaterialFunctorAPI::EditorContext::SetMaterialPropertyVisibility)
  495. ->Method("SetMaterialPropertyDescription", &LuaMaterialFunctorAPI::EditorContext::SetMaterialPropertyDescription)
  496. ->Method("SetMaterialPropertyMinValue_int", &LuaMaterialFunctorAPI::EditorContext::SetMaterialPropertyMinValue<int32_t>)
  497. ->Method("SetMaterialPropertyMinValue_uint", &LuaMaterialFunctorAPI::EditorContext::SetMaterialPropertyMinValue<uint32_t>)
  498. ->Method("SetMaterialPropertyMinValue_float", &LuaMaterialFunctorAPI::EditorContext::SetMaterialPropertyMinValue<float>)
  499. ->Method("SetMaterialPropertyMaxValue_int", &LuaMaterialFunctorAPI::EditorContext::SetMaterialPropertyMaxValue<int32_t>)
  500. ->Method("SetMaterialPropertyMaxValue_uint", &LuaMaterialFunctorAPI::EditorContext::SetMaterialPropertyMaxValue<uint32_t>)
  501. ->Method("SetMaterialPropertyMaxValue_float", &LuaMaterialFunctorAPI::EditorContext::SetMaterialPropertyMaxValue<float>)
  502. ->Method("SetMaterialPropertySoftMinValue_int", &LuaMaterialFunctorAPI::EditorContext::SetMaterialPropertySoftMinValue<int32_t>)
  503. ->Method("SetMaterialPropertySoftMinValue_uint", &LuaMaterialFunctorAPI::EditorContext::SetMaterialPropertySoftMinValue<uint32_t>)
  504. ->Method("SetMaterialPropertySoftMinValue_float", &LuaMaterialFunctorAPI::EditorContext::SetMaterialPropertySoftMinValue<float>)
  505. ->Method("SetMaterialPropertySoftMaxValue_int", &LuaMaterialFunctorAPI::EditorContext::SetMaterialPropertySoftMaxValue<int32_t>)
  506. ->Method("SetMaterialPropertySoftMaxValue_uint", &LuaMaterialFunctorAPI::EditorContext::SetMaterialPropertySoftMaxValue<uint32_t>)
  507. ->Method("SetMaterialPropertySoftMaxValue_float", &LuaMaterialFunctorAPI::EditorContext::SetMaterialPropertySoftMaxValue<float>)
  508. ->Method("SetMaterialPropertyGroupVisibility", &LuaMaterialFunctorAPI::EditorContext::SetMaterialPropertyGroupVisibility)
  509. ;
  510. LuaMaterialFunctorAPI::ReadMaterialPropertyValues::ReflectSubclass<LuaMaterialFunctorAPI::EditorContext>(&builder);
  511. }
  512. LuaMaterialFunctorAPI::EditorContext::EditorContext(
  513. MaterialFunctorAPI::EditorContext* editorContextImpl,
  514. const MaterialNameContext* materialNameContext)
  515. : LuaMaterialFunctorAPI::ReadMaterialPropertyValues(editorContextImpl, materialNameContext)
  516. , m_editorContextImpl(editorContextImpl)
  517. , m_materialNameContext(materialNameContext)
  518. {
  519. }
  520. template<typename Type>
  521. Type LuaMaterialFunctorAPI::EditorContext::GetMaterialPropertyValue(const char* name) const
  522. {
  523. return LuaMaterialFunctorAPI::ReadMaterialPropertyValues::GetMaterialPropertyValue<Type>(name);
  524. }
  525. bool LuaMaterialFunctorAPI::EditorContext::HasMaterialValue(const char* name) const
  526. {
  527. return LuaMaterialFunctorAPI::ReadMaterialPropertyValues::HasMaterialValue(name);
  528. }
  529. template<typename Type>
  530. bool LuaMaterialFunctorAPI::EditorContext::SetMaterialPropertyMinValue(const char* name, Type value)
  531. {
  532. const char* functionName = "SetMaterialPropertyMinValue";
  533. MaterialPropertyIndex index = GetMaterialPropertyIndex(name, functionName);
  534. if (!index.IsValid())
  535. {
  536. return false;
  537. }
  538. return m_editorContextImpl->SetMaterialPropertyMinValue(index, value);
  539. }
  540. template<typename Type>
  541. bool LuaMaterialFunctorAPI::EditorContext::SetMaterialPropertyMaxValue(const char* name, Type value)
  542. {
  543. const char* functionName = "SetMaterialPropertyMaxValue";
  544. MaterialPropertyIndex index = GetMaterialPropertyIndex(name, functionName);
  545. if (!index.IsValid())
  546. {
  547. return false;
  548. }
  549. return m_editorContextImpl->SetMaterialPropertyMaxValue(index, value);
  550. }
  551. template<typename Type>
  552. bool LuaMaterialFunctorAPI::EditorContext::SetMaterialPropertySoftMinValue(const char* name, Type value)
  553. {
  554. const char* functionName = "SetMaterialPropertySoftMinValue";
  555. MaterialPropertyIndex index = GetMaterialPropertyIndex(name, functionName);
  556. if (!index.IsValid())
  557. {
  558. return false;
  559. }
  560. return m_editorContextImpl->SetMaterialPropertySoftMinValue(index, value);
  561. }
  562. template<typename Type>
  563. bool LuaMaterialFunctorAPI::EditorContext::SetMaterialPropertySoftMaxValue(const char* name, Type value)
  564. {
  565. const char* functionName = "SetMaterialPropertySoftMaxValue";
  566. MaterialPropertyIndex index = GetMaterialPropertyIndex(name, functionName);
  567. if (!index.IsValid())
  568. {
  569. return false;
  570. }
  571. return m_editorContextImpl->SetMaterialPropertySoftMaxValue(index, value);
  572. }
  573. bool LuaMaterialFunctorAPI::EditorContext::SetMaterialPropertyGroupVisibility(const char* name, MaterialPropertyGroupVisibility visibility)
  574. {
  575. if (m_editorContextImpl)
  576. {
  577. Name fullName{name};
  578. m_materialNameContext->ContextualizeProperty(fullName);
  579. return m_editorContextImpl->SetMaterialPropertyGroupVisibility(fullName, visibility);
  580. }
  581. return false;
  582. }
  583. bool LuaMaterialFunctorAPI::EditorContext::SetMaterialPropertyVisibility(const char* name, MaterialPropertyVisibility visibility)
  584. {
  585. if (m_editorContextImpl)
  586. {
  587. Name fullName{name};
  588. m_materialNameContext->ContextualizeProperty(fullName);
  589. return m_editorContextImpl->SetMaterialPropertyVisibility(fullName, visibility);
  590. }
  591. return false;
  592. }
  593. bool LuaMaterialFunctorAPI::EditorContext::SetMaterialPropertyDescription(const char* name, const char* description)
  594. {
  595. if (m_editorContextImpl)
  596. {
  597. Name fullName{name};
  598. m_materialNameContext->ContextualizeProperty(fullName);
  599. return m_editorContextImpl->SetMaterialPropertyDescription(fullName, description);
  600. }
  601. return false;
  602. }
  603. template<>
  604. void LuaMaterialFunctorAPI::ShaderItem::SetShaderOptionValue(const char* name, const char* value);
  605. void LuaMaterialFunctorAPI::ShaderItem::Reflect(AZ::BehaviorContext* behaviorContext)
  606. {
  607. behaviorContext->Class<LuaMaterialFunctorAPI::ShaderItem>()
  608. ->Method("GetRenderStatesOverride", &LuaMaterialFunctorAPI::ShaderItem::GetRenderStatesOverride)
  609. ->Method("SetEnabled", &LuaMaterialFunctorAPI::ShaderItem::SetEnabled)
  610. ->Method("SetDrawListTagOverride", &LuaMaterialFunctorAPI::ShaderItem::SetDrawListTagOverride)
  611. ->Method("SetShaderOptionValue_bool", &LuaMaterialFunctorAPI::ShaderItem::SetShaderOptionValue<bool>)
  612. ->Method("SetShaderOptionValue_uint", &LuaMaterialFunctorAPI::ShaderItem::SetShaderOptionValue<uint32_t>)
  613. ->Method("SetShaderOptionValue_enum", &LuaMaterialFunctorAPI::ShaderItem::SetShaderOptionValue<const char*>)
  614. ;
  615. }
  616. LuaMaterialFunctorAPI::RenderStates LuaMaterialFunctorAPI::ShaderItem::GetRenderStatesOverride()
  617. {
  618. if (m_commonRuntimeConfiguration->CheckPsoChangesAllowed() && m_shaderItem)
  619. {
  620. return LuaMaterialFunctorAPI::RenderStates{m_shaderItem->GetRenderStatesOverlay()};
  621. }
  622. else
  623. {
  624. static RHI::RenderStates dummyRenderStates;
  625. return LuaMaterialFunctorAPI::RenderStates{&dummyRenderStates};
  626. }
  627. }
  628. void LuaMaterialFunctorAPI::ShaderItem::SetEnabled(bool enable)
  629. {
  630. if (m_shaderItem)
  631. {
  632. m_shaderItem->SetEnabled(enable);
  633. }
  634. }
  635. void LuaMaterialFunctorAPI::ShaderItem::SetDrawListTagOverride(const char* drawListTag)
  636. {
  637. if (m_shaderItem)
  638. {
  639. m_shaderItem->SetDrawListTagOverride(Name{drawListTag});
  640. }
  641. }
  642. void LuaMaterialFunctorAPI::ShaderItem::SetShaderOptionValue(
  643. const Name& name, AZStd::function<bool(ShaderOptionGroup*, ShaderOptionIndex)> setValueCommand)
  644. {
  645. ShaderOptionGroup* shaderOptionGroup = m_shaderItem->GetShaderOptions();
  646. const ShaderOptionGroupLayout* layout = shaderOptionGroup->GetShaderOptionLayout();
  647. ShaderOptionIndex optionIndex = layout->FindShaderOptionIndex(Name{name});
  648. if (!optionIndex.IsValid())
  649. {
  650. return;
  651. }
  652. if (!m_shaderItem->MaterialOwnsShaderOption(optionIndex))
  653. {
  654. LuaScriptUtilities::Error(
  655. AZStd::string::format(
  656. "Shader option '%s' is not owned by the shader '%s'.", name.GetCStr(), m_shaderItem->GetShaderTag().GetCStr()));
  657. return;
  658. }
  659. setValueCommand(shaderOptionGroup, optionIndex);
  660. }
  661. template<>
  662. void LuaMaterialFunctorAPI::ShaderItem::SetShaderOptionValue(const char* name, const char* value)
  663. {
  664. if (m_shaderItem)
  665. {
  666. SetShaderOptionValue(Name{name}, [value](ShaderOptionGroup* optionGroup, ShaderOptionIndex optionIndex) {
  667. return optionGroup->SetValue(optionIndex, Name{value});
  668. });
  669. }
  670. }
  671. template<typename Type>
  672. void LuaMaterialFunctorAPI::ShaderItem::SetShaderOptionValue(const char* name, Type value)
  673. {
  674. if (m_shaderItem)
  675. {
  676. SetShaderOptionValue(Name{name}, [value](ShaderOptionGroup* optionGroup, ShaderOptionIndex optionIndex) {
  677. return optionGroup->SetValue(optionIndex, ShaderOptionValue{value});
  678. });
  679. }
  680. }
  681. void LuaMaterialFunctorAPI::RenderStates::Reflect(AZ::BehaviorContext* behaviorContext)
  682. {
  683. RHI::ReflectRenderStateEnums(behaviorContext);
  684. auto classBuilder = behaviorContext->Class<LuaMaterialFunctorAPI::RenderStates>();
  685. #define TEMP_REFLECT_RENDERSTATE_METHODS(PropertyName) \
  686. classBuilder->Method("Set" AZ_STRINGIZE(PropertyName), AZ_JOIN(&LuaMaterialFunctorAPI::RenderStates::Set, PropertyName)); \
  687. classBuilder->Method("Clear" AZ_STRINGIZE(PropertyName), AZ_JOIN(&LuaMaterialFunctorAPI::RenderStates::Clear, PropertyName));
  688. TEMP_REFLECT_RENDERSTATE_METHODS(MultisampleCustomPosition)
  689. TEMP_REFLECT_RENDERSTATE_METHODS(MultisampleCustomPositionCount)
  690. TEMP_REFLECT_RENDERSTATE_METHODS(MultisampleCount)
  691. TEMP_REFLECT_RENDERSTATE_METHODS(MultisampleQuality)
  692. TEMP_REFLECT_RENDERSTATE_METHODS(FillMode)
  693. TEMP_REFLECT_RENDERSTATE_METHODS(CullMode)
  694. TEMP_REFLECT_RENDERSTATE_METHODS(DepthBias)
  695. TEMP_REFLECT_RENDERSTATE_METHODS(DepthBiasClamp)
  696. TEMP_REFLECT_RENDERSTATE_METHODS(DepthBiasSlopeScale)
  697. TEMP_REFLECT_RENDERSTATE_METHODS(MultisampleEnabled)
  698. TEMP_REFLECT_RENDERSTATE_METHODS(DepthClipEnabled)
  699. TEMP_REFLECT_RENDERSTATE_METHODS(ConservativeRasterEnabled)
  700. TEMP_REFLECT_RENDERSTATE_METHODS(ForcedSampleCount)
  701. TEMP_REFLECT_RENDERSTATE_METHODS(AlphaToCoverageEnabled)
  702. TEMP_REFLECT_RENDERSTATE_METHODS(IndependentBlendEnabled)
  703. TEMP_REFLECT_RENDERSTATE_METHODS(BlendEnabled)
  704. TEMP_REFLECT_RENDERSTATE_METHODS(BlendWriteMask)
  705. TEMP_REFLECT_RENDERSTATE_METHODS(BlendSource)
  706. TEMP_REFLECT_RENDERSTATE_METHODS(BlendDest)
  707. TEMP_REFLECT_RENDERSTATE_METHODS(BlendOp)
  708. TEMP_REFLECT_RENDERSTATE_METHODS(BlendAlphaSource)
  709. TEMP_REFLECT_RENDERSTATE_METHODS(BlendAlphaDest)
  710. TEMP_REFLECT_RENDERSTATE_METHODS(BlendAlphaOp)
  711. TEMP_REFLECT_RENDERSTATE_METHODS(DepthEnabled)
  712. TEMP_REFLECT_RENDERSTATE_METHODS(DepthWriteMask)
  713. TEMP_REFLECT_RENDERSTATE_METHODS(DepthComparisonFunc)
  714. TEMP_REFLECT_RENDERSTATE_METHODS(StencilEnabled)
  715. TEMP_REFLECT_RENDERSTATE_METHODS(StencilReadMask)
  716. TEMP_REFLECT_RENDERSTATE_METHODS(StencilWriteMask)
  717. TEMP_REFLECT_RENDERSTATE_METHODS(StencilFrontFaceFailOp)
  718. TEMP_REFLECT_RENDERSTATE_METHODS(StencilFrontFaceDepthFailOp)
  719. TEMP_REFLECT_RENDERSTATE_METHODS(StencilFrontFacePassOp)
  720. TEMP_REFLECT_RENDERSTATE_METHODS(StencilFrontFaceFunc)
  721. TEMP_REFLECT_RENDERSTATE_METHODS(StencilBackFaceFailOp)
  722. TEMP_REFLECT_RENDERSTATE_METHODS(StencilBackFaceDepthFailOp)
  723. TEMP_REFLECT_RENDERSTATE_METHODS(StencilBackFacePassOp)
  724. TEMP_REFLECT_RENDERSTATE_METHODS(StencilBackFaceFunc)
  725. #undef TEMP_REFLECT_RENDERSTATE_METHODS
  726. }
  727. #define TEMP_DEFINE_RENDERSTATE_METHODS_COMMON(PropertyName, DataType, Field, InvalidValue) \
  728. void AZ_JOIN(LuaMaterialFunctorAPI::RenderStates::Set, PropertyName)(DataType value) \
  729. { \
  730. Field = value; \
  731. } \
  732. void AZ_JOIN(LuaMaterialFunctorAPI::RenderStates::Clear, PropertyName)() \
  733. { \
  734. Field = InvalidValue; \
  735. }
  736. #define TEMP_DEFINE_RENDERSTATE_METHODS_BLENDSTATETARGET(PropertyName, DataType, Field, InvalidValue) \
  737. void AZ_JOIN(LuaMaterialFunctorAPI::RenderStates::Set, PropertyName)(AZStd::size_t targetIndex, DataType value) \
  738. { \
  739. if (targetIndex < RHI::Limits::Pipeline::AttachmentColorCountMax) \
  740. { \
  741. m_renderStates->m_blendState.m_targets[targetIndex].Field = value; \
  742. } \
  743. else \
  744. { \
  745. LuaScriptUtilities::Error(AZStd::string::format( \
  746. "Set" AZ_STRINGIZE(PropertyName) "(%zu,...) index is out of range. Must be less than %u.", \
  747. targetIndex, RHI::Limits::Pipeline::AttachmentColorCountMax)); \
  748. } \
  749. } \
  750. void AZ_JOIN(LuaMaterialFunctorAPI::RenderStates::Clear, PropertyName)(AZStd::size_t targetIndex) \
  751. { \
  752. if (targetIndex < RHI::Limits::Pipeline::AttachmentColorCountMax) \
  753. { \
  754. m_renderStates->m_blendState.m_targets[targetIndex].Field = InvalidValue; \
  755. } \
  756. else \
  757. { \
  758. LuaScriptUtilities::Error(AZStd::string::format( \
  759. "Clear" AZ_STRINGIZE(PropertyName) "(%zu,...) index is out of range. Must be less than %u.", \
  760. targetIndex, RHI::Limits::Pipeline::AttachmentColorCountMax)); \
  761. } \
  762. }
  763. void LuaMaterialFunctorAPI::RenderStates::SetMultisampleCustomPosition(AZStd::size_t multisampleCustomLocationIndex, uint8_t x, uint8_t y)
  764. {
  765. if (multisampleCustomLocationIndex < RHI::Limits::Pipeline::MultiSampleCustomLocationsCountMax)
  766. {
  767. m_renderStates->m_multisampleState.m_customPositions[multisampleCustomLocationIndex].m_x = x;
  768. m_renderStates->m_multisampleState.m_customPositions[multisampleCustomLocationIndex].m_y = y;
  769. }
  770. else
  771. {
  772. LuaScriptUtilities::Error(AZStd::string::format("SetMultisampleCustomPosition(%zu,...) index is out of range. Must be less than %u.",
  773. multisampleCustomLocationIndex, RHI::Limits::Pipeline::MultiSampleCustomLocationsCountMax));
  774. }
  775. }
  776. void LuaMaterialFunctorAPI::RenderStates::ClearMultisampleCustomPosition(AZStd::size_t multisampleCustomLocationIndex)
  777. {
  778. if (multisampleCustomLocationIndex < RHI::Limits::Pipeline::MultiSampleCustomLocationsCountMax)
  779. {
  780. m_renderStates->m_multisampleState.m_customPositions[multisampleCustomLocationIndex].m_x = RHI::Limits::Pipeline::MultiSampleCustomLocationGridSize;
  781. m_renderStates->m_multisampleState.m_customPositions[multisampleCustomLocationIndex].m_y = RHI::Limits::Pipeline::MultiSampleCustomLocationGridSize;
  782. }
  783. else
  784. {
  785. LuaScriptUtilities::Error(AZStd::string::format("ClearMultisampleCustomPosition(%zu,...) index is out of range. Must be less than %u.",
  786. multisampleCustomLocationIndex, RHI::Limits::Pipeline::MultiSampleCustomLocationsCountMax));
  787. }
  788. }
  789. void LuaMaterialFunctorAPI::RenderStates::SetMultisampleCustomPositionCount(uint32_t value)
  790. {
  791. if (value == RHI::RenderStates_InvalidUInt || value < RHI::Limits::Pipeline::MultiSampleCustomLocationsCountMax)
  792. {
  793. m_renderStates->m_multisampleState.m_customPositionsCount = value;
  794. }
  795. else
  796. {
  797. LuaScriptUtilities::Error(AZStd::string::format("SetMultisampleCustomPositionCount(%u) value is out of range. Must be less than %u.",
  798. value, RHI::Limits::Pipeline::MultiSampleCustomLocationsCountMax));
  799. }
  800. }
  801. void LuaMaterialFunctorAPI::RenderStates::ClearMultisampleCustomPositionCount()
  802. {
  803. m_renderStates->m_multisampleState.m_customPositionsCount = RHI::RenderStates_InvalidUInt;
  804. }
  805. TEMP_DEFINE_RENDERSTATE_METHODS_COMMON(MultisampleCount, uint16_t, m_renderStates->m_multisampleState.m_samples, RHI::RenderStates_InvalidUInt16)
  806. TEMP_DEFINE_RENDERSTATE_METHODS_COMMON(MultisampleQuality, uint16_t, m_renderStates->m_multisampleState.m_quality, RHI::RenderStates_InvalidUInt16)
  807. TEMP_DEFINE_RENDERSTATE_METHODS_COMMON(FillMode, RHI::FillMode, m_renderStates->m_rasterState.m_fillMode, RHI::FillMode::Invalid)
  808. TEMP_DEFINE_RENDERSTATE_METHODS_COMMON(CullMode, RHI::CullMode, m_renderStates->m_rasterState.m_cullMode, RHI::CullMode::Invalid)
  809. TEMP_DEFINE_RENDERSTATE_METHODS_COMMON(DepthBias, int32_t, m_renderStates->m_rasterState.m_depthBias, RHI::RenderStates_InvalidInt)
  810. TEMP_DEFINE_RENDERSTATE_METHODS_COMMON(DepthBiasClamp, float, m_renderStates->m_rasterState.m_depthBiasClamp, RHI::RenderStates_InvalidFloat)
  811. TEMP_DEFINE_RENDERSTATE_METHODS_COMMON(DepthBiasSlopeScale, float, m_renderStates->m_rasterState.m_depthBiasSlopeScale, RHI::RenderStates_InvalidFloat)
  812. TEMP_DEFINE_RENDERSTATE_METHODS_COMMON(MultisampleEnabled, bool, m_renderStates->m_rasterState.m_multisampleEnable, RHI::RenderStates_InvalidBool)
  813. TEMP_DEFINE_RENDERSTATE_METHODS_COMMON(DepthClipEnabled, bool, m_renderStates->m_rasterState.m_depthClipEnable, RHI::RenderStates_InvalidBool)
  814. TEMP_DEFINE_RENDERSTATE_METHODS_COMMON(ConservativeRasterEnabled, bool, m_renderStates->m_rasterState.m_conservativeRasterEnable, RHI::RenderStates_InvalidBool)
  815. TEMP_DEFINE_RENDERSTATE_METHODS_COMMON(ForcedSampleCount, uint32_t, m_renderStates->m_rasterState.m_forcedSampleCount, RHI::RenderStates_InvalidUInt)
  816. TEMP_DEFINE_RENDERSTATE_METHODS_COMMON(AlphaToCoverageEnabled, bool, m_renderStates->m_blendState.m_alphaToCoverageEnable, RHI::RenderStates_InvalidBool)
  817. TEMP_DEFINE_RENDERSTATE_METHODS_COMMON(IndependentBlendEnabled, bool, m_renderStates->m_blendState.m_independentBlendEnable, RHI::RenderStates_InvalidBool)
  818. TEMP_DEFINE_RENDERSTATE_METHODS_BLENDSTATETARGET(BlendEnabled, bool, m_enable, RHI::RenderStates_InvalidBool)
  819. TEMP_DEFINE_RENDERSTATE_METHODS_BLENDSTATETARGET(BlendWriteMask, uint32_t, m_writeMask, RHI::RenderStates_InvalidUInt)
  820. TEMP_DEFINE_RENDERSTATE_METHODS_BLENDSTATETARGET(BlendSource, RHI::BlendFactor, m_blendSource, RHI::BlendFactor::Invalid)
  821. TEMP_DEFINE_RENDERSTATE_METHODS_BLENDSTATETARGET(BlendDest, RHI::BlendFactor, m_blendDest, RHI::BlendFactor::Invalid)
  822. TEMP_DEFINE_RENDERSTATE_METHODS_BLENDSTATETARGET(BlendOp, RHI::BlendOp, m_blendOp, RHI::BlendOp::Invalid)
  823. TEMP_DEFINE_RENDERSTATE_METHODS_BLENDSTATETARGET(BlendAlphaSource, RHI::BlendFactor, m_blendAlphaSource, RHI::BlendFactor::Invalid)
  824. TEMP_DEFINE_RENDERSTATE_METHODS_BLENDSTATETARGET(BlendAlphaDest, RHI::BlendFactor, m_blendAlphaDest, RHI::BlendFactor::Invalid)
  825. TEMP_DEFINE_RENDERSTATE_METHODS_BLENDSTATETARGET(BlendAlphaOp, RHI::BlendOp, m_blendAlphaOp, RHI::BlendOp::Invalid)
  826. TEMP_DEFINE_RENDERSTATE_METHODS_COMMON(DepthEnabled, bool, m_renderStates->m_depthStencilState.m_depth.m_enable, RHI::RenderStates_InvalidBool)
  827. TEMP_DEFINE_RENDERSTATE_METHODS_COMMON(DepthWriteMask, RHI::DepthWriteMask, m_renderStates->m_depthStencilState.m_depth.m_writeMask, RHI::DepthWriteMask::Invalid)
  828. TEMP_DEFINE_RENDERSTATE_METHODS_COMMON(DepthComparisonFunc, RHI::ComparisonFunc, m_renderStates->m_depthStencilState.m_depth.m_func, RHI::ComparisonFunc::Invalid)
  829. TEMP_DEFINE_RENDERSTATE_METHODS_COMMON(StencilEnabled, bool, m_renderStates->m_depthStencilState.m_stencil.m_enable, RHI::RenderStates_InvalidBool)
  830. TEMP_DEFINE_RENDERSTATE_METHODS_COMMON(StencilReadMask, uint32_t, m_renderStates->m_depthStencilState.m_stencil.m_readMask, RHI::RenderStates_InvalidUInt)
  831. TEMP_DEFINE_RENDERSTATE_METHODS_COMMON(StencilWriteMask, uint32_t, m_renderStates->m_depthStencilState.m_stencil.m_writeMask, RHI::RenderStates_InvalidUInt)
  832. TEMP_DEFINE_RENDERSTATE_METHODS_COMMON(StencilFrontFaceFailOp, RHI::StencilOp, m_renderStates->m_depthStencilState.m_stencil.m_frontFace.m_failOp, RHI::StencilOp::Invalid)
  833. TEMP_DEFINE_RENDERSTATE_METHODS_COMMON(StencilFrontFaceDepthFailOp, RHI::StencilOp, m_renderStates->m_depthStencilState.m_stencil.m_frontFace.m_depthFailOp, RHI::StencilOp::Invalid)
  834. TEMP_DEFINE_RENDERSTATE_METHODS_COMMON(StencilFrontFacePassOp, RHI::StencilOp, m_renderStates->m_depthStencilState.m_stencil.m_frontFace.m_passOp, RHI::StencilOp::Invalid)
  835. TEMP_DEFINE_RENDERSTATE_METHODS_COMMON(StencilFrontFaceFunc, RHI::ComparisonFunc, m_renderStates->m_depthStencilState.m_stencil.m_frontFace.m_func, RHI::ComparisonFunc::Invalid)
  836. TEMP_DEFINE_RENDERSTATE_METHODS_COMMON(StencilBackFaceFailOp, RHI::StencilOp, m_renderStates->m_depthStencilState.m_stencil.m_backFace.m_failOp, RHI::StencilOp::Invalid)
  837. TEMP_DEFINE_RENDERSTATE_METHODS_COMMON(StencilBackFaceDepthFailOp, RHI::StencilOp, m_renderStates->m_depthStencilState.m_stencil.m_backFace.m_depthFailOp, RHI::StencilOp::Invalid)
  838. TEMP_DEFINE_RENDERSTATE_METHODS_COMMON(StencilBackFacePassOp, RHI::StencilOp, m_renderStates->m_depthStencilState.m_stencil.m_backFace.m_passOp, RHI::StencilOp::Invalid)
  839. TEMP_DEFINE_RENDERSTATE_METHODS_COMMON(StencilBackFaceFunc, RHI::ComparisonFunc, m_renderStates->m_depthStencilState.m_stencil.m_backFace.m_func, RHI::ComparisonFunc::Invalid)
  840. #undef TEMP_DEFINE_RENDERSTATE_METHODS_COMMON
  841. #undef TEMP_DEFINE_RENDERSTATE_METHODS_BLENDSTATETARGET
  842. } // namespace RPI
  843. } // namespace AZ