LuaMaterialFunctor.cpp 54 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977
  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->Method("SetShaderParameterValue_bool", &LuaMaterialFunctorAPI::RuntimeContext::SetShaderParameterValue<bool>)
  286. ->Method("SetShaderParameterValue_int", &LuaMaterialFunctorAPI::RuntimeContext::SetShaderParameterValue<int32_t>)
  287. ->Method("SetShaderParameterValue_uint", &LuaMaterialFunctorAPI::RuntimeContext::SetShaderParameterValue<uint32_t>)
  288. ->Method("SetShaderParameterValue_float", &LuaMaterialFunctorAPI::RuntimeContext::SetShaderParameterValue<float>)
  289. ->Method("SetShaderParameterValue_Vector2", &LuaMaterialFunctorAPI::RuntimeContext::SetShaderParameterValue<Vector2>)
  290. ->Method("SetShaderParameterValue_Vector3", &LuaMaterialFunctorAPI::RuntimeContext::SetShaderParameterValue<Vector3>)
  291. ->Method("SetShaderParameterValue_Vector4", &LuaMaterialFunctorAPI::RuntimeContext::SetShaderParameterValue<Vector4>)
  292. ->Method("SetShaderParameterValue_Color", &LuaMaterialFunctorAPI::RuntimeContext::SetShaderParameterValue<Color>)
  293. ->Method("SetShaderParameterValue_Matrix3x3", &LuaMaterialFunctorAPI::RuntimeContext::SetShaderParameterValue<Matrix3x3>)
  294. ->Method("SetShaderParameterValue_Matrix4x4", &LuaMaterialFunctorAPI::RuntimeContext::SetShaderParameterValue<Matrix4x4>)
  295. ->Method(
  296. "SetInternalMaterialPropertyValue_bool", &LuaMaterialFunctorAPI::RuntimeContext::SetInternalMaterialPropertyValue<bool>)
  297. ->Method(
  298. "SetInternalMaterialPropertyValue_int",
  299. &LuaMaterialFunctorAPI::RuntimeContext::SetInternalMaterialPropertyValue<int32_t>)
  300. ->Method(
  301. "SetInternalMaterialPropertyValue_uint",
  302. &LuaMaterialFunctorAPI::RuntimeContext::SetInternalMaterialPropertyValue<uint32_t>)
  303. ->Method(
  304. "SetInternalMaterialPropertyValue_enum",
  305. &LuaMaterialFunctorAPI::RuntimeContext::SetInternalMaterialPropertyValue<uint32_t>)
  306. ->Method(
  307. "SetInternalMaterialPropertyValue_float",
  308. &LuaMaterialFunctorAPI::RuntimeContext::SetInternalMaterialPropertyValue<float>)
  309. // I'm not really sure what use case there might be for passing these data types to the material pipeline, but we might as
  310. // well provide them to remain consistent with the types that are supported by the GetMaterialPropertyValue function above.
  311. ->Method(
  312. "SetInternalMaterialPropertyValue_Vector2",
  313. &LuaMaterialFunctorAPI::RuntimeContext::SetInternalMaterialPropertyValue<Vector2>)
  314. ->Method(
  315. "SetInternalMaterialPropertyValue_Vector3",
  316. &LuaMaterialFunctorAPI::RuntimeContext::SetInternalMaterialPropertyValue<Vector3>)
  317. ->Method(
  318. "SetInternalMaterialPropertyValue_Vector4",
  319. &LuaMaterialFunctorAPI::RuntimeContext::SetInternalMaterialPropertyValue<Vector4>)
  320. ->Method(
  321. "SetInternalMaterialPropertyValue_Color",
  322. &LuaMaterialFunctorAPI::RuntimeContext::SetInternalMaterialPropertyValue<Color>)
  323. ->Method(
  324. "SetInternalMaterialPropertyValue_Image",
  325. &LuaMaterialFunctorAPI::RuntimeContext::SetInternalMaterialPropertyValue<Image*>);
  326. LuaMaterialFunctorAPI::ReadMaterialPropertyValues::ReflectSubclass<LuaMaterialFunctorAPI::RuntimeContext>(&builder);
  327. LuaMaterialFunctorAPI::ConfigureShaders::ReflectSubclass<LuaMaterialFunctorAPI::RuntimeContext>(&builder);
  328. }
  329. template<typename LuaApiClass>
  330. void LuaMaterialFunctorAPI::ConfigureShaders::ReflectSubclass(BehaviorContext::ClassBuilder<LuaApiClass>* subclassBuilder)
  331. {
  332. subclassBuilder
  333. ->Method("SetShaderOptionValue_bool", &LuaApiClass::template SetShaderOptionValue<bool>)
  334. ->Method("SetShaderOptionValue_uint", &LuaApiClass::template SetShaderOptionValue<uint32_t>)
  335. ->Method("SetShaderOptionValue_enum", &LuaApiClass::template SetShaderOptionValue<const char*>)
  336. ->Method("GetShaderCount", &LuaApiClass::GetShaderCount)
  337. ->Method("GetShader", &LuaApiClass::GetShader)
  338. ->Method("GetShaderByTag", &LuaApiClass::GetShaderByTag)
  339. ->Method("HasShaderWithTag", &LuaApiClass::HasShaderWithTag)
  340. ;
  341. }
  342. LuaMaterialFunctorAPI::ConfigureShaders::ConfigureShaders(
  343. MaterialFunctorAPI::ConfigureShaders* underlyingApi,
  344. const MaterialNameContext* materialNameContext,
  345. CommonRuntimeConfiguration* commonRuntimeConfiguration)
  346. : m_underlyingApi(underlyingApi)
  347. , m_materialNameContext(materialNameContext)
  348. , m_commonRuntimeConfiguration(commonRuntimeConfiguration)
  349. {
  350. }
  351. bool LuaMaterialFunctorAPI::ReadMaterialPropertyValues::HasMaterialValue(const char* name) const
  352. {
  353. Name propertyFullName{name};
  354. m_materialNameContext->ContextualizeProperty(propertyFullName);
  355. MaterialPropertyIndex propertyIndex = m_underlyingApi->GetMaterialPropertiesLayout()->FindPropertyIndex(propertyFullName);
  356. return propertyIndex.IsValid();
  357. }
  358. template<>
  359. bool LuaMaterialFunctorAPI::ConfigureShaders::SetShaderOptionValue(const char* name, const char* value)
  360. {
  361. Name optionName{name};
  362. m_materialNameContext->ContextualizeShaderOption(optionName);
  363. return m_underlyingApi->SetShaderOptionValue(optionName, Name{value});
  364. }
  365. template<typename Type>
  366. bool LuaMaterialFunctorAPI::ConfigureShaders::SetShaderOptionValue(const char* name, Type value)
  367. {
  368. Name optionName{name};
  369. m_materialNameContext->ContextualizeShaderOption(optionName);
  370. return m_underlyingApi->SetShaderOptionValue(optionName, ShaderOptionValue{value});
  371. }
  372. template<typename Type>
  373. bool LuaMaterialFunctorAPI::RuntimeContext::SetShaderParameterValue(const char* name, Type value)
  374. {
  375. AZ::Name optionName{ name };
  376. m_materialNameContext->ContextualizeSrgInput(optionName);
  377. return m_runtimeContextImpl->GetMaterialShaderParameter()->SetParameter(optionName, value);
  378. }
  379. AZStd::size_t LuaMaterialFunctorAPI::ConfigureShaders::GetShaderCount() const
  380. {
  381. return m_underlyingApi->GetShaderCount();
  382. }
  383. LuaMaterialFunctorAPI::ShaderItem LuaMaterialFunctorAPI::ConfigureShaders::GetShader(AZStd::size_t index)
  384. {
  385. if (index < GetShaderCount())
  386. {
  387. return LuaMaterialFunctorAPI::ShaderItem{&(*m_underlyingApi->m_localShaderCollection)[index], m_commonRuntimeConfiguration};
  388. }
  389. else
  390. {
  391. LuaScriptUtilities::Error(AZStd::string::format("GetShader(%zu) is invalid.", index));
  392. return {};
  393. }
  394. }
  395. LuaMaterialFunctorAPI::ShaderItem LuaMaterialFunctorAPI::ConfigureShaders::GetShaderByTag(const char* shaderTag)
  396. {
  397. const AZ::Name tag{shaderTag};
  398. if (m_underlyingApi->m_localShaderCollection->HasShaderTag(tag))
  399. {
  400. return LuaMaterialFunctorAPI::ShaderItem{&(*m_underlyingApi->m_localShaderCollection)[tag], m_commonRuntimeConfiguration};
  401. }
  402. else
  403. {
  404. LuaScriptUtilities::Error(AZStd::string::format(
  405. "GetShaderByTag('%s') is invalid: Could not find a shader with the tag '%s'.", tag.GetCStr(), tag.GetCStr()));
  406. return {};
  407. }
  408. }
  409. bool LuaMaterialFunctorAPI::ConfigureShaders::HasShaderWithTag(const char* shaderTag)
  410. {
  411. return m_underlyingApi->m_localShaderCollection->HasShaderTag(AZ::Name{shaderTag});
  412. }
  413. LuaMaterialFunctorAPI::RuntimeContext::RuntimeContext(
  414. MaterialFunctorAPI::RuntimeContext* runtimeContextImpl,
  415. const MaterialPropertyFlags* materialPropertyDependencies,
  416. const MaterialNameContext* materialNameContext)
  417. : LuaMaterialFunctorAPI::CommonRuntimeConfiguration(runtimeContextImpl->GetMaterialPropertyPsoHandling(), materialPropertyDependencies, runtimeContextImpl->GetMaterialPropertiesLayout())
  418. , LuaMaterialFunctorAPI::ReadMaterialPropertyValues(runtimeContextImpl, materialNameContext)
  419. , LuaMaterialFunctorAPI::ConfigureShaders(runtimeContextImpl, materialNameContext, this)
  420. , m_runtimeContextImpl(runtimeContextImpl)
  421. , m_materialNameContext(materialNameContext)
  422. {
  423. }
  424. template<typename Type>
  425. Type LuaMaterialFunctorAPI::RuntimeContext::GetMaterialPropertyValue(const char* name) const
  426. {
  427. return LuaMaterialFunctorAPI::ReadMaterialPropertyValues::GetMaterialPropertyValue<Type>(name);
  428. }
  429. bool LuaMaterialFunctorAPI::RuntimeContext::HasMaterialValue(const char* name) const
  430. {
  431. return LuaMaterialFunctorAPI::ReadMaterialPropertyValues::HasMaterialValue(name);
  432. }
  433. template<typename Type>
  434. bool LuaMaterialFunctorAPI::RuntimeContext::SetShaderOptionValue(const char* name, Type value)
  435. {
  436. return LuaMaterialFunctorAPI::ConfigureShaders::SetShaderOptionValue(name, value);
  437. }
  438. AZStd::size_t LuaMaterialFunctorAPI::RuntimeContext::GetShaderCount() const
  439. {
  440. return LuaMaterialFunctorAPI::ConfigureShaders::GetShaderCount();
  441. }
  442. LuaMaterialFunctorAPI::ShaderItem LuaMaterialFunctorAPI::RuntimeContext::GetShader(AZStd::size_t index)
  443. {
  444. return LuaMaterialFunctorAPI::ConfigureShaders::GetShader(index);
  445. }
  446. LuaMaterialFunctorAPI::ShaderItem LuaMaterialFunctorAPI::RuntimeContext::GetShaderByTag(const char* shaderTag)
  447. {
  448. return LuaMaterialFunctorAPI::ConfigureShaders::GetShaderByTag(shaderTag);
  449. }
  450. bool LuaMaterialFunctorAPI::RuntimeContext::HasShaderWithTag(const char* shaderTag)
  451. {
  452. return LuaMaterialFunctorAPI::ConfigureShaders::HasShaderWithTag(shaderTag);
  453. }
  454. template<typename T>
  455. bool LuaMaterialFunctorAPI::RuntimeContext::SetInternalMaterialPropertyValue(const char* name, T value)
  456. {
  457. return m_runtimeContextImpl->SetInternalMaterialPropertyValue(AZ::Name{name}, value);
  458. }
  459. void LuaMaterialFunctorAPI::PipelineRuntimeContext::Reflect(BehaviorContext* behaviorContext)
  460. {
  461. auto builder = behaviorContext->Class<PipelineRuntimeContext>();
  462. LuaMaterialFunctorAPI::ReadMaterialPropertyValues::ReflectSubclass<LuaMaterialFunctorAPI::PipelineRuntimeContext>(&builder);
  463. LuaMaterialFunctorAPI::ConfigureShaders::ReflectSubclass<LuaMaterialFunctorAPI::PipelineRuntimeContext>(&builder);
  464. }
  465. LuaMaterialFunctorAPI::PipelineRuntimeContext::PipelineRuntimeContext(
  466. MaterialFunctorAPI::PipelineRuntimeContext* runtimeContextImpl,
  467. const MaterialPropertyFlags* materialPropertyDependencies,
  468. const MaterialNameContext* materialNameContext)
  469. : LuaMaterialFunctorAPI::CommonRuntimeConfiguration(runtimeContextImpl->GetMaterialPropertyPsoHandling(), materialPropertyDependencies, runtimeContextImpl->GetMaterialPropertiesLayout())
  470. , LuaMaterialFunctorAPI::ReadMaterialPropertyValues(runtimeContextImpl, materialNameContext)
  471. , LuaMaterialFunctorAPI::ConfigureShaders(runtimeContextImpl, materialNameContext, this)
  472. {
  473. }
  474. template<typename Type>
  475. Type LuaMaterialFunctorAPI::PipelineRuntimeContext::GetMaterialPropertyValue(const char* name) const
  476. {
  477. return LuaMaterialFunctorAPI::ReadMaterialPropertyValues::GetMaterialPropertyValue<Type>(name);
  478. }
  479. bool LuaMaterialFunctorAPI::PipelineRuntimeContext::HasMaterialValue(const char* name) const
  480. {
  481. return LuaMaterialFunctorAPI::ReadMaterialPropertyValues::HasMaterialValue(name);
  482. }
  483. template<typename Type>
  484. bool LuaMaterialFunctorAPI::PipelineRuntimeContext::SetShaderOptionValue(const char* name, Type value)
  485. {
  486. return LuaMaterialFunctorAPI::ConfigureShaders::SetShaderOptionValue(name, value);
  487. }
  488. AZStd::size_t LuaMaterialFunctorAPI::PipelineRuntimeContext::GetShaderCount() const
  489. {
  490. return LuaMaterialFunctorAPI::ConfigureShaders::GetShaderCount();
  491. }
  492. LuaMaterialFunctorAPI::ShaderItem LuaMaterialFunctorAPI::PipelineRuntimeContext::GetShader(AZStd::size_t index)
  493. {
  494. return LuaMaterialFunctorAPI::ConfigureShaders::GetShader(index);
  495. }
  496. LuaMaterialFunctorAPI::ShaderItem LuaMaterialFunctorAPI::PipelineRuntimeContext::GetShaderByTag(const char* shaderTag)
  497. {
  498. return LuaMaterialFunctorAPI::ConfigureShaders::GetShaderByTag(shaderTag);
  499. }
  500. bool LuaMaterialFunctorAPI::PipelineRuntimeContext::HasShaderWithTag(const char* shaderTag)
  501. {
  502. return LuaMaterialFunctorAPI::ConfigureShaders::HasShaderWithTag(shaderTag);
  503. }
  504. void LuaMaterialFunctorAPI::EditorContext::EditorContext::Reflect(BehaviorContext* behaviorContext)
  505. {
  506. auto builder = behaviorContext->Class<LuaMaterialFunctorAPI::EditorContext>();
  507. builder
  508. ->Method("SetMaterialPropertyVisibility", &LuaMaterialFunctorAPI::EditorContext::SetMaterialPropertyVisibility)
  509. ->Method("SetMaterialPropertyDescription", &LuaMaterialFunctorAPI::EditorContext::SetMaterialPropertyDescription)
  510. ->Method("SetMaterialPropertyMinValue_int", &LuaMaterialFunctorAPI::EditorContext::SetMaterialPropertyMinValue<int32_t>)
  511. ->Method("SetMaterialPropertyMinValue_uint", &LuaMaterialFunctorAPI::EditorContext::SetMaterialPropertyMinValue<uint32_t>)
  512. ->Method("SetMaterialPropertyMinValue_float", &LuaMaterialFunctorAPI::EditorContext::SetMaterialPropertyMinValue<float>)
  513. ->Method("SetMaterialPropertyMaxValue_int", &LuaMaterialFunctorAPI::EditorContext::SetMaterialPropertyMaxValue<int32_t>)
  514. ->Method("SetMaterialPropertyMaxValue_uint", &LuaMaterialFunctorAPI::EditorContext::SetMaterialPropertyMaxValue<uint32_t>)
  515. ->Method("SetMaterialPropertyMaxValue_float", &LuaMaterialFunctorAPI::EditorContext::SetMaterialPropertyMaxValue<float>)
  516. ->Method("SetMaterialPropertySoftMinValue_int", &LuaMaterialFunctorAPI::EditorContext::SetMaterialPropertySoftMinValue<int32_t>)
  517. ->Method("SetMaterialPropertySoftMinValue_uint", &LuaMaterialFunctorAPI::EditorContext::SetMaterialPropertySoftMinValue<uint32_t>)
  518. ->Method("SetMaterialPropertySoftMinValue_float", &LuaMaterialFunctorAPI::EditorContext::SetMaterialPropertySoftMinValue<float>)
  519. ->Method("SetMaterialPropertySoftMaxValue_int", &LuaMaterialFunctorAPI::EditorContext::SetMaterialPropertySoftMaxValue<int32_t>)
  520. ->Method("SetMaterialPropertySoftMaxValue_uint", &LuaMaterialFunctorAPI::EditorContext::SetMaterialPropertySoftMaxValue<uint32_t>)
  521. ->Method("SetMaterialPropertySoftMaxValue_float", &LuaMaterialFunctorAPI::EditorContext::SetMaterialPropertySoftMaxValue<float>)
  522. ->Method("SetMaterialPropertyGroupVisibility", &LuaMaterialFunctorAPI::EditorContext::SetMaterialPropertyGroupVisibility)
  523. ;
  524. LuaMaterialFunctorAPI::ReadMaterialPropertyValues::ReflectSubclass<LuaMaterialFunctorAPI::EditorContext>(&builder);
  525. }
  526. LuaMaterialFunctorAPI::EditorContext::EditorContext(
  527. MaterialFunctorAPI::EditorContext* editorContextImpl,
  528. const MaterialNameContext* materialNameContext)
  529. : LuaMaterialFunctorAPI::ReadMaterialPropertyValues(editorContextImpl, materialNameContext)
  530. , m_editorContextImpl(editorContextImpl)
  531. , m_materialNameContext(materialNameContext)
  532. {
  533. }
  534. template<typename Type>
  535. Type LuaMaterialFunctorAPI::EditorContext::GetMaterialPropertyValue(const char* name) const
  536. {
  537. return LuaMaterialFunctorAPI::ReadMaterialPropertyValues::GetMaterialPropertyValue<Type>(name);
  538. }
  539. bool LuaMaterialFunctorAPI::EditorContext::HasMaterialValue(const char* name) const
  540. {
  541. return LuaMaterialFunctorAPI::ReadMaterialPropertyValues::HasMaterialValue(name);
  542. }
  543. template<typename Type>
  544. bool LuaMaterialFunctorAPI::EditorContext::SetMaterialPropertyMinValue(const char* name, Type value)
  545. {
  546. const char* functionName = "SetMaterialPropertyMinValue";
  547. MaterialPropertyIndex index = GetMaterialPropertyIndex(name, functionName);
  548. if (!index.IsValid())
  549. {
  550. return false;
  551. }
  552. return m_editorContextImpl->SetMaterialPropertyMinValue(index, value);
  553. }
  554. template<typename Type>
  555. bool LuaMaterialFunctorAPI::EditorContext::SetMaterialPropertyMaxValue(const char* name, Type value)
  556. {
  557. const char* functionName = "SetMaterialPropertyMaxValue";
  558. MaterialPropertyIndex index = GetMaterialPropertyIndex(name, functionName);
  559. if (!index.IsValid())
  560. {
  561. return false;
  562. }
  563. return m_editorContextImpl->SetMaterialPropertyMaxValue(index, value);
  564. }
  565. template<typename Type>
  566. bool LuaMaterialFunctorAPI::EditorContext::SetMaterialPropertySoftMinValue(const char* name, Type value)
  567. {
  568. const char* functionName = "SetMaterialPropertySoftMinValue";
  569. MaterialPropertyIndex index = GetMaterialPropertyIndex(name, functionName);
  570. if (!index.IsValid())
  571. {
  572. return false;
  573. }
  574. return m_editorContextImpl->SetMaterialPropertySoftMinValue(index, value);
  575. }
  576. template<typename Type>
  577. bool LuaMaterialFunctorAPI::EditorContext::SetMaterialPropertySoftMaxValue(const char* name, Type value)
  578. {
  579. const char* functionName = "SetMaterialPropertySoftMaxValue";
  580. MaterialPropertyIndex index = GetMaterialPropertyIndex(name, functionName);
  581. if (!index.IsValid())
  582. {
  583. return false;
  584. }
  585. return m_editorContextImpl->SetMaterialPropertySoftMaxValue(index, value);
  586. }
  587. bool LuaMaterialFunctorAPI::EditorContext::SetMaterialPropertyGroupVisibility(const char* name, MaterialPropertyGroupVisibility visibility)
  588. {
  589. if (m_editorContextImpl)
  590. {
  591. Name fullName{name};
  592. m_materialNameContext->ContextualizeProperty(fullName);
  593. return m_editorContextImpl->SetMaterialPropertyGroupVisibility(fullName, visibility);
  594. }
  595. return false;
  596. }
  597. bool LuaMaterialFunctorAPI::EditorContext::SetMaterialPropertyVisibility(const char* name, MaterialPropertyVisibility visibility)
  598. {
  599. if (m_editorContextImpl)
  600. {
  601. Name fullName{name};
  602. m_materialNameContext->ContextualizeProperty(fullName);
  603. return m_editorContextImpl->SetMaterialPropertyVisibility(fullName, visibility);
  604. }
  605. return false;
  606. }
  607. bool LuaMaterialFunctorAPI::EditorContext::SetMaterialPropertyDescription(const char* name, const char* description)
  608. {
  609. if (m_editorContextImpl)
  610. {
  611. Name fullName{name};
  612. m_materialNameContext->ContextualizeProperty(fullName);
  613. return m_editorContextImpl->SetMaterialPropertyDescription(fullName, description);
  614. }
  615. return false;
  616. }
  617. template<>
  618. void LuaMaterialFunctorAPI::ShaderItem::SetShaderOptionValue(const char* name, const char* value);
  619. void LuaMaterialFunctorAPI::ShaderItem::Reflect(AZ::BehaviorContext* behaviorContext)
  620. {
  621. behaviorContext->Class<LuaMaterialFunctorAPI::ShaderItem>()
  622. ->Method("GetRenderStatesOverride", &LuaMaterialFunctorAPI::ShaderItem::GetRenderStatesOverride)
  623. ->Method("SetEnabled", &LuaMaterialFunctorAPI::ShaderItem::SetEnabled)
  624. ->Method("SetDrawListTagOverride", &LuaMaterialFunctorAPI::ShaderItem::SetDrawListTagOverride)
  625. ->Method("SetShaderOptionValue_bool", &LuaMaterialFunctorAPI::ShaderItem::SetShaderOptionValue<bool>)
  626. ->Method("SetShaderOptionValue_uint", &LuaMaterialFunctorAPI::ShaderItem::SetShaderOptionValue<uint32_t>)
  627. ->Method("SetShaderOptionValue_enum", &LuaMaterialFunctorAPI::ShaderItem::SetShaderOptionValue<const char*>)
  628. ;
  629. }
  630. LuaMaterialFunctorAPI::RenderStates LuaMaterialFunctorAPI::ShaderItem::GetRenderStatesOverride()
  631. {
  632. if (m_commonRuntimeConfiguration->CheckPsoChangesAllowed() && m_shaderItem)
  633. {
  634. return LuaMaterialFunctorAPI::RenderStates{m_shaderItem->GetRenderStatesOverlay()};
  635. }
  636. else
  637. {
  638. static RHI::RenderStates dummyRenderStates;
  639. return LuaMaterialFunctorAPI::RenderStates{&dummyRenderStates};
  640. }
  641. }
  642. void LuaMaterialFunctorAPI::ShaderItem::SetEnabled(bool enable)
  643. {
  644. if (m_shaderItem)
  645. {
  646. m_shaderItem->SetEnabled(enable);
  647. }
  648. }
  649. void LuaMaterialFunctorAPI::ShaderItem::SetDrawListTagOverride(const char* drawListTag)
  650. {
  651. if (m_shaderItem)
  652. {
  653. m_shaderItem->SetDrawListTagOverride(Name{drawListTag});
  654. }
  655. }
  656. void LuaMaterialFunctorAPI::ShaderItem::SetShaderOptionValue(
  657. const Name& name, AZStd::function<bool(ShaderOptionGroup*, ShaderOptionIndex)> setValueCommand)
  658. {
  659. ShaderOptionGroup* shaderOptionGroup = m_shaderItem->GetShaderOptions();
  660. const ShaderOptionGroupLayout* layout = shaderOptionGroup->GetShaderOptionLayout();
  661. ShaderOptionIndex optionIndex = layout->FindShaderOptionIndex(Name{name});
  662. if (!optionIndex.IsValid())
  663. {
  664. return;
  665. }
  666. if (!m_shaderItem->MaterialOwnsShaderOption(optionIndex))
  667. {
  668. LuaScriptUtilities::Error(
  669. AZStd::string::format(
  670. "Shader option '%s' is not owned by the shader '%s'.", name.GetCStr(), m_shaderItem->GetShaderTag().GetCStr()));
  671. return;
  672. }
  673. setValueCommand(shaderOptionGroup, optionIndex);
  674. }
  675. template<>
  676. void LuaMaterialFunctorAPI::ShaderItem::SetShaderOptionValue(const char* name, const char* value)
  677. {
  678. if (m_shaderItem)
  679. {
  680. SetShaderOptionValue(Name{name}, [value](ShaderOptionGroup* optionGroup, ShaderOptionIndex optionIndex) {
  681. return optionGroup->SetValue(optionIndex, Name{value});
  682. });
  683. }
  684. }
  685. template<typename Type>
  686. void LuaMaterialFunctorAPI::ShaderItem::SetShaderOptionValue(const char* name, Type value)
  687. {
  688. if (m_shaderItem)
  689. {
  690. SetShaderOptionValue(Name{name}, [value](ShaderOptionGroup* optionGroup, ShaderOptionIndex optionIndex) {
  691. return optionGroup->SetValue(optionIndex, ShaderOptionValue{value});
  692. });
  693. }
  694. }
  695. void LuaMaterialFunctorAPI::RenderStates::Reflect(AZ::BehaviorContext* behaviorContext)
  696. {
  697. auto classBuilder = behaviorContext->Class<LuaMaterialFunctorAPI::RenderStates>();
  698. #define TEMP_REFLECT_RENDERSTATE_METHODS(PropertyName) \
  699. classBuilder->Method("Set" AZ_STRINGIZE(PropertyName), AZ_JOIN(&LuaMaterialFunctorAPI::RenderStates::Set, PropertyName)); \
  700. classBuilder->Method("Clear" AZ_STRINGIZE(PropertyName), AZ_JOIN(&LuaMaterialFunctorAPI::RenderStates::Clear, PropertyName));
  701. TEMP_REFLECT_RENDERSTATE_METHODS(MultisampleCustomPosition)
  702. TEMP_REFLECT_RENDERSTATE_METHODS(MultisampleCustomPositionCount)
  703. TEMP_REFLECT_RENDERSTATE_METHODS(MultisampleCount)
  704. TEMP_REFLECT_RENDERSTATE_METHODS(MultisampleQuality)
  705. TEMP_REFLECT_RENDERSTATE_METHODS(FillMode)
  706. TEMP_REFLECT_RENDERSTATE_METHODS(CullMode)
  707. TEMP_REFLECT_RENDERSTATE_METHODS(DepthBias)
  708. TEMP_REFLECT_RENDERSTATE_METHODS(DepthBiasClamp)
  709. TEMP_REFLECT_RENDERSTATE_METHODS(DepthBiasSlopeScale)
  710. TEMP_REFLECT_RENDERSTATE_METHODS(MultisampleEnabled)
  711. TEMP_REFLECT_RENDERSTATE_METHODS(DepthClipEnabled)
  712. TEMP_REFLECT_RENDERSTATE_METHODS(ConservativeRasterEnabled)
  713. TEMP_REFLECT_RENDERSTATE_METHODS(ForcedSampleCount)
  714. TEMP_REFLECT_RENDERSTATE_METHODS(AlphaToCoverageEnabled)
  715. TEMP_REFLECT_RENDERSTATE_METHODS(IndependentBlendEnabled)
  716. TEMP_REFLECT_RENDERSTATE_METHODS(BlendEnabled)
  717. TEMP_REFLECT_RENDERSTATE_METHODS(BlendWriteMask)
  718. TEMP_REFLECT_RENDERSTATE_METHODS(BlendSource)
  719. TEMP_REFLECT_RENDERSTATE_METHODS(BlendDest)
  720. TEMP_REFLECT_RENDERSTATE_METHODS(BlendOp)
  721. TEMP_REFLECT_RENDERSTATE_METHODS(BlendAlphaSource)
  722. TEMP_REFLECT_RENDERSTATE_METHODS(BlendAlphaDest)
  723. TEMP_REFLECT_RENDERSTATE_METHODS(BlendAlphaOp)
  724. TEMP_REFLECT_RENDERSTATE_METHODS(DepthEnabled)
  725. TEMP_REFLECT_RENDERSTATE_METHODS(DepthWriteMask)
  726. TEMP_REFLECT_RENDERSTATE_METHODS(DepthComparisonFunc)
  727. TEMP_REFLECT_RENDERSTATE_METHODS(StencilEnabled)
  728. TEMP_REFLECT_RENDERSTATE_METHODS(StencilReadMask)
  729. TEMP_REFLECT_RENDERSTATE_METHODS(StencilWriteMask)
  730. TEMP_REFLECT_RENDERSTATE_METHODS(StencilFrontFaceFailOp)
  731. TEMP_REFLECT_RENDERSTATE_METHODS(StencilFrontFaceDepthFailOp)
  732. TEMP_REFLECT_RENDERSTATE_METHODS(StencilFrontFacePassOp)
  733. TEMP_REFLECT_RENDERSTATE_METHODS(StencilFrontFaceFunc)
  734. TEMP_REFLECT_RENDERSTATE_METHODS(StencilBackFaceFailOp)
  735. TEMP_REFLECT_RENDERSTATE_METHODS(StencilBackFaceDepthFailOp)
  736. TEMP_REFLECT_RENDERSTATE_METHODS(StencilBackFacePassOp)
  737. TEMP_REFLECT_RENDERSTATE_METHODS(StencilBackFaceFunc)
  738. #undef TEMP_REFLECT_RENDERSTATE_METHODS
  739. }
  740. #define TEMP_DEFINE_RENDERSTATE_METHODS_COMMON(PropertyName, DataType, Field, InvalidValue) \
  741. void AZ_JOIN(LuaMaterialFunctorAPI::RenderStates::Set, PropertyName)(DataType value) \
  742. { \
  743. Field = value; \
  744. } \
  745. void AZ_JOIN(LuaMaterialFunctorAPI::RenderStates::Clear, PropertyName)() \
  746. { \
  747. Field = InvalidValue; \
  748. }
  749. #define TEMP_DEFINE_RENDERSTATE_METHODS_BLENDSTATETARGET(PropertyName, DataType, Field, InvalidValue) \
  750. void AZ_JOIN(LuaMaterialFunctorAPI::RenderStates::Set, PropertyName)(AZStd::size_t targetIndex, DataType value) \
  751. { \
  752. if (targetIndex < RHI::Limits::Pipeline::AttachmentColorCountMax) \
  753. { \
  754. m_renderStates->m_blendState.m_targets[targetIndex].Field = value; \
  755. } \
  756. else \
  757. { \
  758. LuaScriptUtilities::Error(AZStd::string::format( \
  759. "Set" AZ_STRINGIZE(PropertyName) "(%zu,...) index is out of range. Must be less than %u.", \
  760. targetIndex, RHI::Limits::Pipeline::AttachmentColorCountMax)); \
  761. } \
  762. } \
  763. void AZ_JOIN(LuaMaterialFunctorAPI::RenderStates::Clear, PropertyName)(AZStd::size_t targetIndex) \
  764. { \
  765. if (targetIndex < RHI::Limits::Pipeline::AttachmentColorCountMax) \
  766. { \
  767. m_renderStates->m_blendState.m_targets[targetIndex].Field = InvalidValue; \
  768. } \
  769. else \
  770. { \
  771. LuaScriptUtilities::Error(AZStd::string::format( \
  772. "Clear" AZ_STRINGIZE(PropertyName) "(%zu,...) index is out of range. Must be less than %u.", \
  773. targetIndex, RHI::Limits::Pipeline::AttachmentColorCountMax)); \
  774. } \
  775. }
  776. void LuaMaterialFunctorAPI::RenderStates::SetMultisampleCustomPosition(AZStd::size_t multisampleCustomLocationIndex, uint8_t x, uint8_t y)
  777. {
  778. if (multisampleCustomLocationIndex < RHI::Limits::Pipeline::MultiSampleCustomLocationsCountMax)
  779. {
  780. m_renderStates->m_multisampleState.m_customPositions[multisampleCustomLocationIndex].m_x = x;
  781. m_renderStates->m_multisampleState.m_customPositions[multisampleCustomLocationIndex].m_y = y;
  782. }
  783. else
  784. {
  785. LuaScriptUtilities::Error(AZStd::string::format("SetMultisampleCustomPosition(%zu,...) index is out of range. Must be less than %u.",
  786. multisampleCustomLocationIndex, RHI::Limits::Pipeline::MultiSampleCustomLocationsCountMax));
  787. }
  788. }
  789. void LuaMaterialFunctorAPI::RenderStates::ClearMultisampleCustomPosition(AZStd::size_t multisampleCustomLocationIndex)
  790. {
  791. if (multisampleCustomLocationIndex < RHI::Limits::Pipeline::MultiSampleCustomLocationsCountMax)
  792. {
  793. m_renderStates->m_multisampleState.m_customPositions[multisampleCustomLocationIndex].m_x = RHI::Limits::Pipeline::MultiSampleCustomLocationGridSize;
  794. m_renderStates->m_multisampleState.m_customPositions[multisampleCustomLocationIndex].m_y = RHI::Limits::Pipeline::MultiSampleCustomLocationGridSize;
  795. }
  796. else
  797. {
  798. LuaScriptUtilities::Error(AZStd::string::format("ClearMultisampleCustomPosition(%zu,...) index is out of range. Must be less than %u.",
  799. multisampleCustomLocationIndex, RHI::Limits::Pipeline::MultiSampleCustomLocationsCountMax));
  800. }
  801. }
  802. void LuaMaterialFunctorAPI::RenderStates::SetMultisampleCustomPositionCount(uint32_t value)
  803. {
  804. if (value == RHI::RenderStates_InvalidUInt || value < RHI::Limits::Pipeline::MultiSampleCustomLocationsCountMax)
  805. {
  806. m_renderStates->m_multisampleState.m_customPositionsCount = value;
  807. }
  808. else
  809. {
  810. LuaScriptUtilities::Error(AZStd::string::format("SetMultisampleCustomPositionCount(%u) value is out of range. Must be less than %u.",
  811. value, RHI::Limits::Pipeline::MultiSampleCustomLocationsCountMax));
  812. }
  813. }
  814. void LuaMaterialFunctorAPI::RenderStates::ClearMultisampleCustomPositionCount()
  815. {
  816. m_renderStates->m_multisampleState.m_customPositionsCount = RHI::RenderStates_InvalidUInt;
  817. }
  818. TEMP_DEFINE_RENDERSTATE_METHODS_COMMON(MultisampleCount, uint16_t, m_renderStates->m_multisampleState.m_samples, RHI::RenderStates_InvalidUInt16)
  819. TEMP_DEFINE_RENDERSTATE_METHODS_COMMON(MultisampleQuality, uint16_t, m_renderStates->m_multisampleState.m_quality, RHI::RenderStates_InvalidUInt16)
  820. TEMP_DEFINE_RENDERSTATE_METHODS_COMMON(FillMode, RHI::FillMode, m_renderStates->m_rasterState.m_fillMode, RHI::FillMode::Invalid)
  821. TEMP_DEFINE_RENDERSTATE_METHODS_COMMON(CullMode, RHI::CullMode, m_renderStates->m_rasterState.m_cullMode, RHI::CullMode::Invalid)
  822. TEMP_DEFINE_RENDERSTATE_METHODS_COMMON(DepthBias, int32_t, m_renderStates->m_rasterState.m_depthBias, RHI::RenderStates_InvalidInt)
  823. TEMP_DEFINE_RENDERSTATE_METHODS_COMMON(DepthBiasClamp, float, m_renderStates->m_rasterState.m_depthBiasClamp, RHI::RenderStates_InvalidFloat)
  824. TEMP_DEFINE_RENDERSTATE_METHODS_COMMON(DepthBiasSlopeScale, float, m_renderStates->m_rasterState.m_depthBiasSlopeScale, RHI::RenderStates_InvalidFloat)
  825. TEMP_DEFINE_RENDERSTATE_METHODS_COMMON(MultisampleEnabled, bool, m_renderStates->m_rasterState.m_multisampleEnable, RHI::RenderStates_InvalidBool)
  826. TEMP_DEFINE_RENDERSTATE_METHODS_COMMON(DepthClipEnabled, bool, m_renderStates->m_rasterState.m_depthClipEnable, RHI::RenderStates_InvalidBool)
  827. TEMP_DEFINE_RENDERSTATE_METHODS_COMMON(ConservativeRasterEnabled, bool, m_renderStates->m_rasterState.m_conservativeRasterEnable, RHI::RenderStates_InvalidBool)
  828. TEMP_DEFINE_RENDERSTATE_METHODS_COMMON(ForcedSampleCount, uint32_t, m_renderStates->m_rasterState.m_forcedSampleCount, RHI::RenderStates_InvalidUInt)
  829. TEMP_DEFINE_RENDERSTATE_METHODS_COMMON(AlphaToCoverageEnabled, bool, m_renderStates->m_blendState.m_alphaToCoverageEnable, RHI::RenderStates_InvalidBool)
  830. TEMP_DEFINE_RENDERSTATE_METHODS_COMMON(IndependentBlendEnabled, bool, m_renderStates->m_blendState.m_independentBlendEnable, RHI::RenderStates_InvalidBool)
  831. TEMP_DEFINE_RENDERSTATE_METHODS_BLENDSTATETARGET(BlendEnabled, bool, m_enable, RHI::RenderStates_InvalidBool)
  832. TEMP_DEFINE_RENDERSTATE_METHODS_BLENDSTATETARGET(BlendWriteMask, uint32_t, m_writeMask, RHI::RenderStates_InvalidUInt)
  833. TEMP_DEFINE_RENDERSTATE_METHODS_BLENDSTATETARGET(BlendSource, RHI::BlendFactor, m_blendSource, RHI::BlendFactor::Invalid)
  834. TEMP_DEFINE_RENDERSTATE_METHODS_BLENDSTATETARGET(BlendDest, RHI::BlendFactor, m_blendDest, RHI::BlendFactor::Invalid)
  835. TEMP_DEFINE_RENDERSTATE_METHODS_BLENDSTATETARGET(BlendOp, RHI::BlendOp, m_blendOp, RHI::BlendOp::Invalid)
  836. TEMP_DEFINE_RENDERSTATE_METHODS_BLENDSTATETARGET(BlendAlphaSource, RHI::BlendFactor, m_blendAlphaSource, RHI::BlendFactor::Invalid)
  837. TEMP_DEFINE_RENDERSTATE_METHODS_BLENDSTATETARGET(BlendAlphaDest, RHI::BlendFactor, m_blendAlphaDest, RHI::BlendFactor::Invalid)
  838. TEMP_DEFINE_RENDERSTATE_METHODS_BLENDSTATETARGET(BlendAlphaOp, RHI::BlendOp, m_blendAlphaOp, RHI::BlendOp::Invalid)
  839. TEMP_DEFINE_RENDERSTATE_METHODS_COMMON(DepthEnabled, bool, m_renderStates->m_depthStencilState.m_depth.m_enable, RHI::RenderStates_InvalidBool)
  840. TEMP_DEFINE_RENDERSTATE_METHODS_COMMON(DepthWriteMask, RHI::DepthWriteMask, m_renderStates->m_depthStencilState.m_depth.m_writeMask, RHI::DepthWriteMask::Invalid)
  841. TEMP_DEFINE_RENDERSTATE_METHODS_COMMON(DepthComparisonFunc, RHI::ComparisonFunc, m_renderStates->m_depthStencilState.m_depth.m_func, RHI::ComparisonFunc::Invalid)
  842. TEMP_DEFINE_RENDERSTATE_METHODS_COMMON(StencilEnabled, bool, m_renderStates->m_depthStencilState.m_stencil.m_enable, RHI::RenderStates_InvalidBool)
  843. TEMP_DEFINE_RENDERSTATE_METHODS_COMMON(StencilReadMask, uint32_t, m_renderStates->m_depthStencilState.m_stencil.m_readMask, RHI::RenderStates_InvalidUInt)
  844. TEMP_DEFINE_RENDERSTATE_METHODS_COMMON(StencilWriteMask, uint32_t, m_renderStates->m_depthStencilState.m_stencil.m_writeMask, RHI::RenderStates_InvalidUInt)
  845. TEMP_DEFINE_RENDERSTATE_METHODS_COMMON(StencilFrontFaceFailOp, RHI::StencilOp, m_renderStates->m_depthStencilState.m_stencil.m_frontFace.m_failOp, RHI::StencilOp::Invalid)
  846. TEMP_DEFINE_RENDERSTATE_METHODS_COMMON(StencilFrontFaceDepthFailOp, RHI::StencilOp, m_renderStates->m_depthStencilState.m_stencil.m_frontFace.m_depthFailOp, RHI::StencilOp::Invalid)
  847. TEMP_DEFINE_RENDERSTATE_METHODS_COMMON(StencilFrontFacePassOp, RHI::StencilOp, m_renderStates->m_depthStencilState.m_stencil.m_frontFace.m_passOp, RHI::StencilOp::Invalid)
  848. TEMP_DEFINE_RENDERSTATE_METHODS_COMMON(StencilFrontFaceFunc, RHI::ComparisonFunc, m_renderStates->m_depthStencilState.m_stencil.m_frontFace.m_func, RHI::ComparisonFunc::Invalid)
  849. TEMP_DEFINE_RENDERSTATE_METHODS_COMMON(StencilBackFaceFailOp, RHI::StencilOp, m_renderStates->m_depthStencilState.m_stencil.m_backFace.m_failOp, RHI::StencilOp::Invalid)
  850. TEMP_DEFINE_RENDERSTATE_METHODS_COMMON(StencilBackFaceDepthFailOp, RHI::StencilOp, m_renderStates->m_depthStencilState.m_stencil.m_backFace.m_depthFailOp, RHI::StencilOp::Invalid)
  851. TEMP_DEFINE_RENDERSTATE_METHODS_COMMON(StencilBackFacePassOp, RHI::StencilOp, m_renderStates->m_depthStencilState.m_stencil.m_backFace.m_passOp, RHI::StencilOp::Invalid)
  852. TEMP_DEFINE_RENDERSTATE_METHODS_COMMON(StencilBackFaceFunc, RHI::ComparisonFunc, m_renderStates->m_depthStencilState.m_stencil.m_backFace.m_func, RHI::ComparisonFunc::Invalid)
  853. #undef TEMP_DEFINE_RENDERSTATE_METHODS_COMMON
  854. #undef TEMP_DEFINE_RENDERSTATE_METHODS_BLENDSTATETARGET
  855. } // namespace RPI
  856. } // namespace AZ