LuaMaterialFunctor.cpp 55 KB

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