ShaderParser.cpp 12 KB

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