ShaderParser.cpp 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358
  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 "HashSet.h"
  25. #include "ShaderParser.h"
  26. #include "XMLElement.h"
  27. bool ShaderParser::Parse(ShaderType type, const XMLElement& element, const Vector<String>& globalDefines, const Vector<String>& globalDefineValues)
  28. {
  29. if (globalDefines.Size() != globalDefineValues.Size())
  30. {
  31. errorMessage_ = "Amount of global define names and values does not match";
  32. return false;
  33. }
  34. globalDefines_ = globalDefines;
  35. globalDefineValues_ = globalDefineValues;
  36. errorMessage_.Clear();
  37. options_.Clear();
  38. combinations_.Clear();
  39. XMLElement shader = element.GetChild("shader");
  40. while (shader)
  41. {
  42. String typeName = String(shader.GetAttribute("type")).ToLower();
  43. if (typeName.Empty() || (type == VS && typeName == "vs") || (type == PS && typeName == "ps"))
  44. {
  45. if (!ParseOptions(shader))
  46. return false;
  47. BuildCombinations();
  48. return true;
  49. }
  50. shader = shader.GetNext("shader");
  51. }
  52. // If no shader sub-elements, check also the element itself
  53. String typeName = String(element.GetAttribute("type")).ToLower();
  54. if (typeName.Empty() || (type == VS && typeName == "vs") || (type == PS && typeName == "ps"))
  55. {
  56. if (!ParseOptions(element))
  57. return false;
  58. BuildCombinations();
  59. return true;
  60. }
  61. errorMessage_ = "No shader definitions";
  62. return false;
  63. }
  64. bool ShaderParser::HasCombination(const String& name) const
  65. {
  66. return combinations_.Contains(name);
  67. }
  68. bool ShaderParser::GetCombination(ShaderCombination& dest, const String& name) const
  69. {
  70. HashMap<String, unsigned>::ConstIterator i = combinations_.Find(name);
  71. if (i != combinations_.End())
  72. {
  73. dest.name_ = name;
  74. for (unsigned j = 0; j < options_.Size(); ++j)
  75. {
  76. if (i->second_ & (1 << j))
  77. {
  78. for (unsigned k = 0; k < options_[j].defines_.Size(); ++k)
  79. {
  80. dest.defines_.Push(options_[j].defines_[k]);
  81. dest.defineValues_.Push(options_[j].defineValues_[k]);
  82. }
  83. for (unsigned k = 0; k < globalDefines_.Size(); ++k)
  84. {
  85. dest.defines_.Push(globalDefines_[k]);
  86. dest.defineValues_.Push(globalDefineValues_[k]);
  87. }
  88. }
  89. }
  90. return true;
  91. }
  92. else
  93. return false;
  94. }
  95. bool ShaderParser::ParseOptions(const XMLElement& element)
  96. {
  97. XMLElement option = element.GetChild();
  98. while (option)
  99. {
  100. String value = option.GetName().ToLower();
  101. if (value == "variation" || value == "option")
  102. {
  103. String name = option.GetAttribute("name");
  104. ShaderOption newOption;
  105. newOption.name_ = name;
  106. newOption.isVariation_ = value == "variation";
  107. String simpleDefine = option.GetAttribute("define");
  108. if (!simpleDefine.Empty())
  109. {
  110. Vector<String> nameAndValue = simpleDefine.Split('=');
  111. if (nameAndValue.Size() == 2)
  112. {
  113. newOption.defines_.Push(nameAndValue[0]);
  114. newOption.defineValues_.Push(nameAndValue[1]);
  115. }
  116. else
  117. {
  118. newOption.defines_.Push(simpleDefine);
  119. newOption.defineValues_.Push("1");
  120. }
  121. }
  122. String simpleExclude = option.GetAttribute("exclude");
  123. if (!simpleExclude.Empty())
  124. newOption.excludes_.Push(simpleExclude);
  125. String simpleInclude = option.GetAttribute("include");
  126. if (!simpleInclude.Empty())
  127. newOption.includes_.Push(simpleInclude);
  128. String simpleRequire = option.GetAttribute("require");
  129. if (!simpleRequire.Empty())
  130. newOption.requires_.Push(simpleRequire);
  131. XMLElement define = option.GetChild("define");
  132. while (define)
  133. {
  134. String defineName = define.GetAttribute("name");
  135. Vector<String> nameAndValue = defineName.Split('=');
  136. if (nameAndValue.Size() == 2)
  137. {
  138. newOption.defines_.Push(nameAndValue[0]);
  139. newOption.defineValues_.Push(nameAndValue[1]);
  140. }
  141. else
  142. {
  143. newOption.defines_.Push(defineName);
  144. newOption.defineValues_.Push("1");
  145. }
  146. define = define.GetNext("define");
  147. }
  148. XMLElement exclude = option.GetChild("exclude");
  149. while (exclude)
  150. {
  151. newOption.excludes_.Push(exclude.GetAttribute("name"));
  152. exclude = exclude.GetNext("exclude");
  153. }
  154. XMLElement include = option.GetChild("include");
  155. while (include)
  156. {
  157. newOption.includes_.Push(include.GetAttribute("name"));
  158. include = include.GetNext("include");
  159. }
  160. XMLElement require = option.GetChild("require");
  161. while (require)
  162. {
  163. newOption.requires_.Push(require.GetAttribute("name"));
  164. require = require.GetNext("require");
  165. }
  166. options_.Push(newOption);
  167. if (options_.Size() > 31)
  168. {
  169. errorMessage_ = "Maximum of 31 shader options exceeded";
  170. return false;
  171. }
  172. }
  173. else
  174. {
  175. errorMessage_ = "Unrecognized element " + value + " in shader definition";
  176. return false;
  177. }
  178. option = option.GetNext();
  179. }
  180. return true;
  181. }
  182. void ShaderParser::BuildCombinations()
  183. {
  184. unsigned combinations = 1;
  185. unsigned numVariationGroups = 0;
  186. HashSet<unsigned> usedCombinations;
  187. HashMap<String, unsigned> nameToIndex;
  188. for (unsigned i = 0; i < options_.Size(); ++i)
  189. {
  190. combinations *= 2;
  191. nameToIndex[options_[i].name_] = i;
  192. if (options_[i].isVariation_ && (i == 0 || !options_[i - 1].isVariation_))
  193. ++numVariationGroups;
  194. }
  195. // Preprocess includes/excludes for faster combination handling
  196. for (Vector<ShaderOption>::Iterator i = options_.Begin(); i != options_.End(); ++i)
  197. {
  198. i->excludeIndices_.Resize(i->excludes_.Size());
  199. i->includeIndices_.Resize(i->includes_.Size());
  200. for (unsigned j = 0; j < i->excludes_.Size(); ++j)
  201. i->excludeIndices_[j] = nameToIndex[i->excludes_[j]];
  202. for (unsigned j = 0; j < i->includes_.Size(); ++j)
  203. i->includeIndices_[j] = nameToIndex[i->includes_[j]];
  204. }
  205. // Preprocess requirements
  206. for (unsigned i = 0; i < options_.Size(); ++i)
  207. {
  208. for (unsigned j = 0; j < options_[i].requires_.Size(); ++j)
  209. {
  210. unsigned bits = 0;
  211. for (unsigned l = 0; l < options_.Size(); ++l)
  212. {
  213. if (l != i)
  214. {
  215. if (options_[l].name_ == options_[i].requires_[j])
  216. bits |= 1 << l;
  217. else
  218. {
  219. for (unsigned m = 0; m < options_[l].defines_.Size(); ++m)
  220. {
  221. if (options_[l].defines_[m] == options_[i].requires_[j])
  222. {
  223. bits |= 1 << l;
  224. break;
  225. }
  226. }
  227. }
  228. }
  229. }
  230. // If requirements are not satisfied by any option, check global defines
  231. if (!bits)
  232. {
  233. for (unsigned l = 0; l < globalDefines_.Size(); ++l)
  234. {
  235. if (globalDefines_[l] == options_[i].requires_[j])
  236. {
  237. bits |= 0x80000000;
  238. break;
  239. }
  240. }
  241. }
  242. options_[i].requirementBits_.Push(bits);
  243. }
  244. }
  245. for (unsigned i = 0; i < combinations; ++i)
  246. {
  247. // Variations/options active on this particular combination
  248. unsigned active = i;
  249. unsigned variationsActive = 0;
  250. bool skipThis = false;
  251. // Check for excludes & includes first
  252. for (unsigned j = 0; j < options_.Size(); ++j)
  253. {
  254. if ((active >> j) & 1)
  255. {
  256. for (unsigned k = 0; k < options_[j].includeIndices_.Size(); ++k)
  257. active |= 1 << options_[j].includeIndices_[k];
  258. for (unsigned k = 0; k < options_[j].excludeIndices_.Size(); ++k)
  259. active &= ~(1 << options_[j].excludeIndices_[k]);
  260. // Skip dummy separators (options without name and defines)
  261. if (options_[j].name_.Empty() && !options_[j].isVariation_ && options_[j].defines_.Empty())
  262. active &= ~(1 << j);
  263. // If it's a variation, exclude all other variations in the same group
  264. if (options_[j].isVariation_)
  265. {
  266. for (unsigned k = j - 1; k < options_.Size(); --k)
  267. {
  268. if (options_[k].isVariation_)
  269. active &= ~(1 << k);
  270. else
  271. break;
  272. }
  273. for (unsigned k = j + 1; k < options_.Size(); ++k)
  274. {
  275. if (options_[k].isVariation_)
  276. active &= ~(1 << k);
  277. else
  278. break;
  279. }
  280. ++variationsActive;
  281. }
  282. }
  283. }
  284. // Check that combination is correct: a variation chosen from all groups, and is unique
  285. if (variationsActive < numVariationGroups || usedCombinations.Contains(active))
  286. continue;
  287. // Check for required defines, which may yet cause this combination to be skipped
  288. unsigned compareBits = active | 0x80000000;
  289. for (unsigned j = 0; j < options_.Size(); ++j)
  290. {
  291. if (active & (1 << j))
  292. {
  293. for (unsigned l = 0; l < options_[j].requirementBits_.Size(); ++l)
  294. {
  295. if (!(compareBits & options_[j].requirementBits_[l]))
  296. {
  297. skipThis = true;
  298. break;
  299. }
  300. }
  301. }
  302. }
  303. if (skipThis)
  304. continue;
  305. String combinationName;
  306. // Build shader combination name from active options
  307. for (unsigned j = 0; j < options_.Size(); ++j)
  308. {
  309. if (active & (1 << j) && options_[j].name_.Length())
  310. combinationName += options_[j].name_;
  311. }
  312. combinations_[combinationName] = active;
  313. usedCombinations.Insert(active);
  314. }
  315. }