ShaderParser.cpp 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361
  1. //
  2. // Copyright (c) 2008-2013 the Urho3D project.
  3. //
  4. // Permission is hereby granted, free of charge, to any person obtaining a copy
  5. // of this software and associated documentation files (the "Software"), to deal
  6. // in the Software without restriction, including without limitation the rights
  7. // to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
  8. // copies of the Software, and to permit persons to whom the Software is
  9. // furnished to do so, subject to the following conditions:
  10. //
  11. // The above copyright notice and this permission notice shall be included in
  12. // all copies or substantial portions of the Software.
  13. //
  14. // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  15. // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  16. // FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  17. // AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  18. // LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
  19. // OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
  20. // THE SOFTWARE.
  21. //
  22. #include "Precompiled.h"
  23. #include "ShaderParser.h"
  24. #include "XMLElement.h"
  25. namespace Urho3D
  26. {
  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. ShaderCombination ShaderParser::GetCombination(const String& name) const
  69. {
  70. ShaderCombination dest;
  71. HashMap<String, unsigned>::ConstIterator i = combinations_.Find(name);
  72. if (i != combinations_.End())
  73. {
  74. dest.name_ = name;
  75. for (unsigned j = 0; j < options_.Size(); ++j)
  76. {
  77. if (i->second_ & (1 << j))
  78. {
  79. for (unsigned k = 0; k < options_[j].defines_.Size(); ++k)
  80. {
  81. dest.defines_.Push(options_[j].defines_[k]);
  82. dest.defineValues_.Push(options_[j].defineValues_[k]);
  83. }
  84. for (unsigned k = 0; k < globalDefines_.Size(); ++k)
  85. {
  86. dest.defines_.Push(globalDefines_[k]);
  87. dest.defineValues_.Push(globalDefineValues_[k]);
  88. }
  89. }
  90. }
  91. }
  92. return dest;
  93. }
  94. bool ShaderParser::ParseOptions(const XMLElement& element)
  95. {
  96. XMLElement option = element.GetChild();
  97. while (option)
  98. {
  99. String value = option.GetName().ToLower();
  100. if (value == "variation" || value == "option")
  101. {
  102. String name = option.GetAttribute("name");
  103. ShaderOption newOption;
  104. newOption.name_ = name;
  105. newOption.isVariation_ = value == "variation";
  106. String simpleDefine = option.GetAttribute("define");
  107. if (!simpleDefine.Empty())
  108. {
  109. Vector<String> nameAndValue = simpleDefine.Split('=');
  110. if (nameAndValue.Size() == 2)
  111. {
  112. newOption.defines_.Push(nameAndValue[0]);
  113. newOption.defineValues_.Push(nameAndValue[1]);
  114. }
  115. else
  116. {
  117. newOption.defines_.Push(simpleDefine);
  118. newOption.defineValues_.Push("1");
  119. }
  120. }
  121. String simpleExclude = option.GetAttribute("exclude");
  122. if (!simpleExclude.Empty())
  123. newOption.excludes_.Push(simpleExclude);
  124. String simpleInclude = option.GetAttribute("include");
  125. if (!simpleInclude.Empty())
  126. newOption.includes_.Push(simpleInclude);
  127. String simpleRequire = option.GetAttribute("require");
  128. if (!simpleRequire.Empty())
  129. newOption.requires_.Push(simpleRequire);
  130. XMLElement define = option.GetChild("define");
  131. while (define)
  132. {
  133. String defineName = define.GetAttribute("name");
  134. Vector<String> nameAndValue = defineName.Split('=');
  135. if (nameAndValue.Size() == 2)
  136. {
  137. newOption.defines_.Push(nameAndValue[0]);
  138. newOption.defineValues_.Push(nameAndValue[1]);
  139. }
  140. else
  141. {
  142. newOption.defines_.Push(defineName);
  143. newOption.defineValues_.Push("1");
  144. }
  145. define = define.GetNext("define");
  146. }
  147. XMLElement exclude = option.GetChild("exclude");
  148. while (exclude)
  149. {
  150. newOption.excludes_.Push(exclude.GetAttribute("name"));
  151. exclude = exclude.GetNext("exclude");
  152. }
  153. XMLElement include = option.GetChild("include");
  154. while (include)
  155. {
  156. newOption.includes_.Push(include.GetAttribute("name"));
  157. include = include.GetNext("include");
  158. }
  159. XMLElement require = option.GetChild("require");
  160. while (require)
  161. {
  162. newOption.requires_.Push(require.GetAttribute("name"));
  163. require = require.GetNext("require");
  164. }
  165. options_.Push(newOption);
  166. if (options_.Size() > 31)
  167. {
  168. errorMessage_ = "Maximum of 31 shader options exceeded";
  169. return false;
  170. }
  171. }
  172. else
  173. {
  174. errorMessage_ = "Unrecognized element " + value + " in shader definition";
  175. return false;
  176. }
  177. option = option.GetNext();
  178. }
  179. return true;
  180. }
  181. void ShaderParser::BuildCombinations()
  182. {
  183. unsigned combinations = 1;
  184. unsigned numVariationGroups = 0;
  185. HashSet<unsigned> usedCombinations;
  186. HashMap<String, unsigned> nameToIndex;
  187. for (unsigned i = 0; i < options_.Size(); ++i)
  188. {
  189. combinations *= 2;
  190. nameToIndex[options_[i].name_] = i;
  191. if (options_[i].isVariation_ && (i == 0 || !options_[i - 1].isVariation_))
  192. ++numVariationGroups;
  193. }
  194. // Preprocess includes/excludes for faster combination handling
  195. for (Vector<ShaderOption>::Iterator i = options_.Begin(); i != options_.End(); ++i)
  196. {
  197. i->excludeIndices_.Resize(i->excludes_.Size());
  198. i->includeIndices_.Resize(i->includes_.Size());
  199. for (unsigned j = 0; j < i->excludes_.Size(); ++j)
  200. i->excludeIndices_[j] = nameToIndex[i->excludes_[j]];
  201. for (unsigned j = 0; j < i->includes_.Size(); ++j)
  202. i->includeIndices_[j] = nameToIndex[i->includes_[j]];
  203. }
  204. // Preprocess requirements
  205. for (unsigned i = 0; i < options_.Size(); ++i)
  206. {
  207. for (unsigned j = 0; j < options_[i].requires_.Size(); ++j)
  208. {
  209. unsigned bits = 0;
  210. for (unsigned l = 0; l < options_.Size(); ++l)
  211. {
  212. if (l != i)
  213. {
  214. if (options_[l].name_ == options_[i].requires_[j])
  215. bits |= 1 << l;
  216. else
  217. {
  218. for (unsigned m = 0; m < options_[l].defines_.Size(); ++m)
  219. {
  220. if (options_[l].defines_[m] == options_[i].requires_[j])
  221. {
  222. bits |= 1 << l;
  223. break;
  224. }
  225. }
  226. }
  227. }
  228. }
  229. // If requirements are not satisfied by any option, check global defines
  230. if (!bits)
  231. {
  232. for (unsigned l = 0; l < globalDefines_.Size(); ++l)
  233. {
  234. if (globalDefines_[l] == options_[i].requires_[j])
  235. {
  236. bits |= 0x80000000;
  237. break;
  238. }
  239. }
  240. }
  241. options_[i].requirementBits_.Push(bits);
  242. }
  243. }
  244. for (unsigned i = 0; i < combinations; ++i)
  245. {
  246. // Variations/options active on this particular combination
  247. unsigned active = i;
  248. unsigned variationsActive = 0;
  249. bool skipThis = false;
  250. // Check for excludes & includes first
  251. for (unsigned j = 0; j < options_.Size(); ++j)
  252. {
  253. if ((active >> j) & 1)
  254. {
  255. for (unsigned k = 0; k < options_[j].includeIndices_.Size(); ++k)
  256. active |= 1 << options_[j].includeIndices_[k];
  257. for (unsigned k = 0; k < options_[j].excludeIndices_.Size(); ++k)
  258. active &= ~(1 << options_[j].excludeIndices_[k]);
  259. // Skip dummy separators (options without name and defines)
  260. if (options_[j].name_.Empty() && !options_[j].isVariation_ && options_[j].defines_.Empty())
  261. active &= ~(1 << j);
  262. // If it's a variation, exclude all other variations in the same group
  263. if (options_[j].isVariation_)
  264. {
  265. for (unsigned k = j - 1; k < options_.Size(); --k)
  266. {
  267. if (options_[k].isVariation_)
  268. active &= ~(1 << k);
  269. else
  270. break;
  271. }
  272. for (unsigned k = j + 1; k < options_.Size(); ++k)
  273. {
  274. if (options_[k].isVariation_)
  275. active &= ~(1 << k);
  276. else
  277. break;
  278. }
  279. ++variationsActive;
  280. }
  281. }
  282. }
  283. // Check that combination is correct: a variation chosen from all groups, and is unique
  284. if (variationsActive < numVariationGroups || usedCombinations.Contains(active))
  285. continue;
  286. // Check for required defines, which may yet cause this combination to be skipped
  287. unsigned compareBits = active | 0x80000000;
  288. for (unsigned j = 0; j < options_.Size(); ++j)
  289. {
  290. if (active & (1 << j))
  291. {
  292. for (unsigned l = 0; l < options_[j].requirementBits_.Size(); ++l)
  293. {
  294. if (!(compareBits & options_[j].requirementBits_[l]))
  295. {
  296. skipThis = true;
  297. break;
  298. }
  299. }
  300. }
  301. }
  302. if (skipThis)
  303. continue;
  304. String combinationName;
  305. // Build shader combination name from active options
  306. for (unsigned j = 0; j < options_.Size(); ++j)
  307. {
  308. if (active & (1 << j) && options_[j].name_.Length())
  309. combinationName += options_[j].name_;
  310. }
  311. combinations_[combinationName] = active;
  312. usedCombinations.Insert(active);
  313. }
  314. }
  315. }