3
0

ShaderOptionGroup.cpp 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330
  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/Shader/ShaderOptionGroup.h>
  9. #include <AzCore/Serialization/SerializeContext.h>
  10. namespace AZ
  11. {
  12. namespace RPI
  13. {
  14. const char* ShaderOptionGroup::DebugCategory = "ShaderOption";
  15. ShaderOptionGroup::ShaderOptionGroup(const ShaderOptionGroup& rhs)
  16. : m_layout(rhs.m_layout)
  17. , m_id{rhs.m_id}
  18. {
  19. }
  20. ShaderOptionGroup::ShaderOptionGroup(const ConstPtr<ShaderOptionGroupLayout>& shaderOptionGroupLayout)
  21. : m_layout{shaderOptionGroupLayout}
  22. {
  23. AZ_Assert(m_layout, "ShaderOptionGroup created with null layout!");
  24. Clear();
  25. }
  26. ShaderOptionGroup::ShaderOptionGroup(const ConstPtr<ShaderOptionGroupLayout>& shaderOptionGroupLayout, const ShaderVariantId& id)
  27. : m_layout{shaderOptionGroupLayout}
  28. , m_id{id}
  29. {
  30. AZ_Assert(m_layout, "ShaderOptionGroup created with null layout!");
  31. }
  32. void ShaderOptionGroup::Reflect(AZ::ReflectContext* context)
  33. {
  34. if (BehaviorContext* behaviorContext = azrtti_cast<BehaviorContext*>(context))
  35. {
  36. behaviorContext->Class<ShaderOptionGroup>()
  37. ->Attribute(AZ::Script::Attributes::Scope, AZ::Script::Attributes::ScopeFlags::Automation)
  38. ->Attribute(AZ::Script::Attributes::Category, "Shader")
  39. ->Attribute(AZ::Script::Attributes::Module, "shader")
  40. ->Method("GetValueByOptionName", static_cast<ShaderOptionValue (ShaderOptionGroup::*)(const Name&) const>(&ShaderOptionGroup::GetValue))
  41. ->Method("GetShaderOptionDescriptors", &ShaderOptionGroup::GetShaderOptionDescriptors)
  42. ->Method("GetShaderVariantId", &ShaderOptionGroup::GetShaderVariantId)
  43. ->Method("ClearValue", static_cast<bool (ShaderOptionGroup::*)(const Name&)>(&ShaderOptionGroup::ClearValue))
  44. ;
  45. }
  46. }
  47. void ShaderOptionGroup::Clear()
  48. {
  49. m_id.Reset();
  50. }
  51. ShaderOptionIndex ShaderOptionGroup::FindShaderOptionIndex(const Name& optionName) const
  52. {
  53. return m_layout->FindShaderOptionIndex(optionName);
  54. }
  55. bool ShaderOptionGroup::GetShaderOptionIndex(const Name& optionName, ShaderOptionIndex& optionIndex) const
  56. {
  57. optionIndex = FindShaderOptionIndex(optionName);
  58. if (!optionIndex.IsValid())
  59. {
  60. AZ_Error(DebugCategory, false, "ShaderOption '%s' does not exist", optionName.GetCStr());
  61. return false;
  62. }
  63. return true;
  64. }
  65. bool ShaderOptionGroup::ValidateIndex(const ShaderOptionIndex& optionIndex) const
  66. {
  67. if (!optionIndex.IsValid())
  68. {
  69. AZ_Error(DebugCategory, false, "Invalid ShaderOptionIndex");
  70. return false;
  71. }
  72. return true;
  73. }
  74. bool ShaderOptionGroup::SetValue(const Name& optionName, const Name& valueName)
  75. {
  76. ShaderOptionIndex index;
  77. if (GetShaderOptionIndex(optionName, index))
  78. {
  79. return SetValue(index, valueName);
  80. }
  81. else
  82. {
  83. return false;
  84. }
  85. }
  86. bool ShaderOptionGroup::SetValue(const Name& optionName, ShaderOptionValue valueIndex)
  87. {
  88. ShaderOptionIndex index;
  89. if (GetShaderOptionIndex(optionName, index))
  90. {
  91. return SetValue(index, valueIndex);
  92. }
  93. else
  94. {
  95. return false;
  96. }
  97. }
  98. ShaderOptionValue ShaderOptionGroup::GetValue(const Name& optionName) const
  99. {
  100. ShaderOptionIndex index;
  101. if (GetShaderOptionIndex(optionName, index))
  102. {
  103. return GetValue(index);
  104. }
  105. else
  106. {
  107. return ShaderOptionValue{};
  108. }
  109. }
  110. bool ShaderOptionGroup::SetValue(ShaderOptionIndex optionIndex, const Name& valueName)
  111. {
  112. if (ValidateIndex(optionIndex))
  113. {
  114. const ShaderOptionDescriptor& option = m_layout->GetShaderOption(optionIndex);
  115. if (!option.Set(*this, valueName))
  116. {
  117. return false;
  118. }
  119. return true;
  120. }
  121. else
  122. {
  123. return false;
  124. }
  125. }
  126. bool ShaderOptionGroup::SetValue(ShaderOptionIndex optionIndex, ShaderOptionValue valueIndex)
  127. {
  128. if (ValidateIndex(optionIndex))
  129. {
  130. const ShaderOptionDescriptor& option = m_layout->GetShaderOption(optionIndex);
  131. if (!option.Set(*this, valueIndex))
  132. {
  133. return false;
  134. }
  135. return true;
  136. }
  137. else
  138. {
  139. return false;
  140. }
  141. }
  142. ShaderOptionValue ShaderOptionGroup::GetValue(ShaderOptionIndex optionIndex) const
  143. {
  144. if (ValidateIndex(optionIndex))
  145. {
  146. const ShaderOptionDescriptor& option = m_layout->GetShaderOption(optionIndex);
  147. return option.Get(*this);
  148. }
  149. else
  150. {
  151. return ShaderOptionValue{};
  152. }
  153. }
  154. bool ShaderOptionGroup::ClearValue(const Name& optionName)
  155. {
  156. ShaderOptionIndex index;
  157. if (GetShaderOptionIndex(optionName, index))
  158. {
  159. return ClearValue(index);
  160. }
  161. else
  162. {
  163. return false;
  164. }
  165. }
  166. bool ShaderOptionGroup::ClearValue(ShaderOptionIndex optionIndex)
  167. {
  168. if (ValidateIndex(optionIndex))
  169. {
  170. const ShaderOptionDescriptor& option = m_layout->GetShaderOption(optionIndex);
  171. option.Clear(*this);
  172. return true;
  173. }
  174. return false;
  175. }
  176. void ShaderOptionGroup::SetAllToDefaultValues()
  177. {
  178. for (auto& option : m_layout->GetShaderOptions())
  179. {
  180. option.Set(*this, option.GetDefaultValue());
  181. }
  182. }
  183. void ShaderOptionGroup::SetUnspecifiedToDefaultValues()
  184. {
  185. for (auto& option : m_layout->GetShaderOptions())
  186. {
  187. if (!(m_id.m_mask & option.GetBitMask()).any())
  188. {
  189. option.Set(*this, option.GetDefaultValue());
  190. }
  191. }
  192. }
  193. bool ShaderOptionGroup::IsFullySpecified() const
  194. {
  195. for (auto& option : m_layout->GetShaderOptions())
  196. {
  197. if (!(m_id.m_mask & option.GetBitMask()).any())
  198. {
  199. return false;
  200. }
  201. }
  202. return true;
  203. }
  204. bool ShaderOptionGroup::IsEmpty() const
  205. {
  206. return m_id.IsEmpty();
  207. }
  208. ShaderVariantKey ShaderOptionGroup::GetShaderVariantKeyFallbackValue() const
  209. {
  210. // By default the fallback value is the search key
  211. auto fallbackValueKey = m_id.m_key;
  212. // However, we have to make sure that all options are set, opting for default values where missing
  213. for (auto& option : m_layout->GetShaderOptions())
  214. {
  215. if (!(m_id.m_mask & option.GetBitMask()).any())
  216. {
  217. const auto value = option.FindValue(option.GetDefaultValue());
  218. // This is an assert, not error, because the build system should have detected this situation earlier.
  219. AZ_Assert(value.IsValid(), "Default value for shader option '%s' is invalid.", option.GetName().GetCStr());
  220. option.Set(fallbackValueKey, value);
  221. }
  222. }
  223. return fallbackValueKey;
  224. }
  225. const ShaderVariantKey& ShaderOptionGroup::GetShaderVariantKey() const
  226. {
  227. return m_id.m_key;
  228. }
  229. const ShaderVariantKey& ShaderOptionGroup::GetShaderVariantMask() const
  230. {
  231. return m_id.m_mask;
  232. }
  233. const ShaderVariantId& ShaderOptionGroup::GetShaderVariantId() const
  234. {
  235. return m_id;
  236. }
  237. ShaderVariantKey& ShaderOptionGroup::GetShaderVariantKey()
  238. {
  239. return m_id.m_key;
  240. }
  241. ShaderVariantKey& ShaderOptionGroup::GetShaderVariantMask()
  242. {
  243. return m_id.m_mask;
  244. }
  245. const ShaderOptionGroupLayout* ShaderOptionGroup::GetShaderOptionLayout() const
  246. {
  247. return m_layout.get();
  248. }
  249. AZStd::string ShaderOptionGroup::ToString() const
  250. {
  251. AZStd::string s;
  252. for (int i = 0; i < GetShaderOptionLayout()->GetShaderOptionCount(); ++i)
  253. {
  254. ShaderOptionIndex index{i};
  255. const ShaderOptionDescriptor& option = GetShaderOptionLayout()->GetShaderOption(index);
  256. const ShaderOptionValue& value = GetValue(index);
  257. if (value.IsNull())
  258. {
  259. s += AZStd::string::format("%s=?, ", option.GetName().GetCStr());
  260. }
  261. else
  262. {
  263. //[GFX TODO][ATOM-3481] Report the names of enum options instead of numeric values. This depends on storing Names in NameIdReflectionMap.
  264. s += AZStd::string::format("%s=%d, ", option.GetName().GetCStr(), value.GetIndex());
  265. }
  266. }
  267. // Remove the trailing ", " from the last shader option in the string
  268. static const size_t separateLength = 2;
  269. if (s.size() >= separateLength)
  270. {
  271. s.resize(s.size() - separateLength);
  272. }
  273. return s;
  274. }
  275. const AZStd::vector<AZ::RPI::ShaderOptionDescriptor>& ShaderOptionGroup::GetShaderOptionDescriptors() const
  276. {
  277. return m_layout->GetShaderOptions();
  278. }
  279. } // namespace RPI
  280. } // namespace AZ