ShaderParser.cpp 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309
  1. //
  2. // Urho3D Engine
  3. // Copyright (c) 2008-2012 Lasse Öörni
  4. //
  5. // Permission is hereby granted, free of charge, to any person obtaining a copy
  6. // of this software and associated documentation files (the "Software"), to deal
  7. // in the Software without restriction, including without limitation the rights
  8. // to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
  9. // copies of the Software, and to permit persons to whom the Software is
  10. // furnished to do so, subject to the following conditions:
  11. //
  12. // The above copyright notice and this permission notice shall be included in
  13. // all copies or substantial portions of the Software.
  14. //
  15. // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  16. // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  17. // FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  18. // AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  19. // LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
  20. // OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
  21. // THE SOFTWARE.
  22. //
  23. #include "Precompiled.h"
  24. #include "Map.h"
  25. #include "Set.h"
  26. #include "ShaderParser.h"
  27. #include "XMLElement.h"
  28. bool ShaderParser::Parse(ShaderType type, const XMLElement& element, const Vector<String>& globalDefines, const Vector<String>& globalDefineValues)
  29. {
  30. if (globalDefines.Size() != globalDefineValues.Size())
  31. {
  32. errorMessage_ = "Amount of global define names and values does not match";
  33. return false;
  34. }
  35. errorMessage_.Clear();
  36. combinations_.Clear();
  37. Vector<ShaderOption> options;
  38. XMLElement shader = element.GetChild("shader");
  39. while (shader)
  40. {
  41. String typeName = String(shader.GetAttribute("type")).ToLower();
  42. if (typeName.Empty() || (type == VS && typeName == "vs") || (type == PS && typeName == "ps"))
  43. {
  44. if (!ParseOptions(options, shader))
  45. return false;
  46. BuildCombinations(options, globalDefines, globalDefineValues);
  47. return true;
  48. }
  49. shader = shader.GetNext("shader");
  50. }
  51. // If no shader sub-elements, check also the element itself
  52. String typeName = String(element.GetAttribute("type")).ToLower();
  53. if (typeName.Empty() || (type == VS && typeName == "vs") || (type == PS && typeName == "ps"))
  54. {
  55. if (!ParseOptions(options, element))
  56. return false;
  57. BuildCombinations(options, globalDefines, globalDefineValues);
  58. return true;
  59. }
  60. errorMessage_ = "No shader definitions";
  61. return false;
  62. }
  63. bool ShaderParser::ParseOptions(Vector<ShaderOption>& dest, const XMLElement& element)
  64. {
  65. XMLElement option = element.GetChild();
  66. while (option)
  67. {
  68. String value = option.GetName().ToLower();
  69. if (value == "variation" || value == "option")
  70. {
  71. String name = option.GetAttribute("name");
  72. ShaderOption newOption;
  73. newOption.name_ = name;
  74. newOption.isVariation_ = value == "variation";
  75. String simpleDefine = option.GetAttribute("define");
  76. if (!simpleDefine.Empty())
  77. {
  78. Vector<String> nameAndValue = simpleDefine.Split('=');
  79. if (nameAndValue.Size() == 2)
  80. {
  81. newOption.defines_.Push(nameAndValue[0]);
  82. newOption.defineValues_.Push(nameAndValue[1]);
  83. }
  84. else
  85. {
  86. newOption.defines_.Push(simpleDefine);
  87. newOption.defineValues_.Push("1");
  88. }
  89. }
  90. String simpleExclude = option.GetAttribute("exclude");
  91. if (!simpleExclude.Empty())
  92. newOption.excludes_.Push(simpleExclude);
  93. String simpleInclude = option.GetAttribute("include");
  94. if (!simpleInclude.Empty())
  95. newOption.includes_.Push(simpleInclude);
  96. String simpleRequire = option.GetAttribute("require");
  97. if (!simpleRequire.Empty())
  98. newOption.requires_.Push(simpleRequire);
  99. XMLElement define = option.GetChild("define");
  100. while (define)
  101. {
  102. String defineName = define.GetAttribute("name");
  103. Vector<String> nameAndValue = defineName.Split('=');
  104. if (nameAndValue.Size() == 2)
  105. {
  106. newOption.defines_.Push(nameAndValue[0]);
  107. newOption.defineValues_.Push(nameAndValue[1]);
  108. }
  109. else
  110. {
  111. newOption.defines_.Push(defineName);
  112. newOption.defineValues_.Push("1");
  113. }
  114. define = define.GetNext("define");
  115. }
  116. XMLElement exclude = option.GetChild("exclude");
  117. while (exclude)
  118. {
  119. newOption.excludes_.Push(exclude.GetAttribute("name"));
  120. exclude = exclude.GetNext("exclude");
  121. }
  122. XMLElement include = option.GetChild("include");
  123. while (include)
  124. {
  125. newOption.includes_.Push(include.GetAttribute("name"));
  126. include = include.GetNext("include");
  127. }
  128. XMLElement require = option.GetChild("require");
  129. while (require)
  130. {
  131. newOption.requires_.Push(require.GetAttribute("name"));
  132. require = require.GetNext("require");
  133. }
  134. dest.Push(newOption);
  135. if (dest.Size() > 32)
  136. {
  137. errorMessage_ = "Maximum of 32 shader options exceeded";
  138. return false;
  139. }
  140. }
  141. else
  142. {
  143. errorMessage_ = "Unrecognized element " + value + " in shader definition";
  144. return false;
  145. }
  146. option = option.GetNext();
  147. }
  148. return true;
  149. }
  150. void ShaderParser::BuildCombinations(Vector<ShaderOption>& options, const Vector<String>& globalDefines, const Vector<String>& globalDefineValues)
  151. {
  152. unsigned combinations = 1;
  153. unsigned numVariationGroups = 0;
  154. Set<unsigned> usedCombinations;
  155. Map<String, unsigned> nameToIndex;
  156. for (unsigned i = 0; i < options.Size(); ++i)
  157. {
  158. combinations *= 2;
  159. nameToIndex[options[i].name_] = i;
  160. if (options[i].isVariation_ && (i == 0 || !options[i - 1].isVariation_))
  161. ++numVariationGroups;
  162. }
  163. for (unsigned i = 0; i < combinations; ++i)
  164. {
  165. // Variations/options active on this particular combination
  166. unsigned active = i;
  167. unsigned variationsActive = 0;
  168. bool skipThis = false;
  169. // Check for excludes/includes/requires
  170. for (unsigned j = 0; j < options.Size(); ++j)
  171. {
  172. if ((active >> j) & 1)
  173. {
  174. for (unsigned k = 0; k < options[j].includes_.Size(); ++k)
  175. {
  176. if (nameToIndex.Contains(options[j].includes_[k]))
  177. active |= (1 << nameToIndex[options[j].includes_[k]]);
  178. }
  179. for (unsigned k = 0; k < options[j].excludes_.Size(); ++k)
  180. {
  181. if (nameToIndex.Contains(options[j].excludes_[k]))
  182. active &= ~(1 << nameToIndex[options[j].excludes_[k]]);
  183. }
  184. // Skip dummy separators (options without name and defines)
  185. if (options[j].name_.Empty() && !options[j].isVariation_ && options[j].defines_.Empty())
  186. active &= ~(1 << j);
  187. // If it's a variation, exclude all other variations in the same group
  188. if (options[j].isVariation_)
  189. {
  190. for (unsigned k = j - 1; k < options.Size(); --k)
  191. {
  192. if (options[k].isVariation_)
  193. active &= ~(1 << k);
  194. else
  195. break;
  196. }
  197. for (unsigned k = j + 1; k < options.Size(); ++k)
  198. {
  199. if (options[k].isVariation_)
  200. active &= ~(1 << k);
  201. else
  202. break;
  203. }
  204. ++variationsActive;
  205. }
  206. for (unsigned k = 0; k < options[j].requires_.Size(); ++k)
  207. {
  208. bool requireFound = false;
  209. for (unsigned l = 0; l < globalDefines.Size(); ++l)
  210. {
  211. if (globalDefines[l] == options[j].requires_[k])
  212. {
  213. requireFound = true;
  214. break;
  215. }
  216. }
  217. if (!requireFound)
  218. {
  219. for (unsigned l = 0; l < options.Size(); ++l)
  220. {
  221. if ((active >> l) & 1 && l != j)
  222. {
  223. if (options[l].name_ == options[j].requires_[k])
  224. {
  225. requireFound = true;
  226. break;
  227. }
  228. for (unsigned m = 0; m < options[l].defines_.Size(); ++m)
  229. {
  230. if (options[l].defines_[m] == options[j].requires_[k])
  231. {
  232. requireFound = true;
  233. break;
  234. }
  235. }
  236. }
  237. if (requireFound)
  238. break;
  239. }
  240. }
  241. if (!requireFound)
  242. skipThis = true;
  243. }
  244. }
  245. }
  246. // Check that combination is correct: a variation chosen from all groups, and is unique
  247. if (variationsActive < numVariationGroups || skipThis || usedCombinations.Contains(active))
  248. continue;
  249. ShaderCombination newCombination;
  250. // Build shader combination name & defines from active options
  251. for (unsigned j = 0; j < options.Size(); ++j)
  252. {
  253. if (active & (1 << j))
  254. {
  255. if (options[j].name_.Length())
  256. newCombination.name_ += options[j].name_;
  257. for (unsigned k = 0; k < options[j].defines_.Size(); ++k)
  258. {
  259. newCombination.defines_.Push(options[j].defines_[k]);
  260. newCombination.defineValues_.Push(options[j].defineValues_[k]);
  261. }
  262. for (unsigned k = 0; k < globalDefines.Size(); ++k)
  263. {
  264. newCombination.defines_.Push(globalDefines[k]);
  265. newCombination.defineValues_.Push(globalDefineValues[k]);
  266. }
  267. }
  268. }
  269. combinations_.Push(newCombination);
  270. usedCombinations.Insert(active);
  271. }
  272. }