2
0

ShaderParser.cpp 13 KB

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