ShaderParser.cpp 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355
  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. #ifdef RASPI
  156. newOption.defines_.Push("RASPI");
  157. newOption.defineValues_.Push("1");
  158. #endif
  159. options_.Push(newOption);
  160. if (options_.Size() > 31)
  161. {
  162. errorMessage_ = "Maximum of 31 shader options exceeded";
  163. return false;
  164. }
  165. }
  166. else
  167. {
  168. errorMessage_ = "Unrecognized element " + value + " in shader definition";
  169. return false;
  170. }
  171. option = option.GetNext();
  172. }
  173. return true;
  174. }
  175. void ShaderParser::BuildCombinations()
  176. {
  177. unsigned combinations = 1;
  178. unsigned numVariationGroups = 0;
  179. HashSet<unsigned> usedCombinations;
  180. HashMap<String, unsigned> nameToIndex;
  181. for (unsigned i = 0; i < options_.Size(); ++i)
  182. {
  183. combinations *= 2;
  184. nameToIndex[options_[i].name_] = i;
  185. if (options_[i].isVariation_ && (i == 0 || !options_[i - 1].isVariation_))
  186. ++numVariationGroups;
  187. }
  188. // Preprocess includes/excludes for faster combination handling
  189. for (Vector<ShaderOption>::Iterator i = options_.Begin(); i != options_.End(); ++i)
  190. {
  191. i->excludeIndices_.Resize(i->excludes_.Size());
  192. i->includeIndices_.Resize(i->includes_.Size());
  193. for (unsigned j = 0; j < i->excludes_.Size(); ++j)
  194. i->excludeIndices_[j] = nameToIndex[i->excludes_[j]];
  195. for (unsigned j = 0; j < i->includes_.Size(); ++j)
  196. i->includeIndices_[j] = nameToIndex[i->includes_[j]];
  197. }
  198. // Preprocess requirements
  199. for (unsigned i = 0; i < options_.Size(); ++i)
  200. {
  201. for (unsigned j = 0; j < options_[i].requires_.Size(); ++j)
  202. {
  203. unsigned bits = 0;
  204. for (unsigned l = 0; l < options_.Size(); ++l)
  205. {
  206. if (l != i)
  207. {
  208. if (options_[l].name_ == options_[i].requires_[j])
  209. bits |= 1 << l;
  210. else
  211. {
  212. for (unsigned m = 0; m < options_[l].defines_.Size(); ++m)
  213. {
  214. if (options_[l].defines_[m] == options_[i].requires_[j])
  215. {
  216. bits |= 1 << l;
  217. break;
  218. }
  219. }
  220. }
  221. }
  222. }
  223. // If requirements are not satisfied by any option, check global defines
  224. if (!bits)
  225. {
  226. for (unsigned l = 0; l < globalDefines_.Size(); ++l)
  227. {
  228. if (globalDefines_[l] == options_[i].requires_[j])
  229. {
  230. bits |= 0x80000000;
  231. break;
  232. }
  233. }
  234. }
  235. options_[i].requirementBits_.Push(bits);
  236. }
  237. }
  238. for (unsigned i = 0; i < combinations; ++i)
  239. {
  240. // Variations/options active on this particular combination
  241. unsigned active = i;
  242. unsigned variationsActive = 0;
  243. bool skipThis = false;
  244. // Check for excludes & includes first
  245. for (unsigned j = 0; j < options_.Size(); ++j)
  246. {
  247. if ((active >> j) & 1)
  248. {
  249. for (unsigned k = 0; k < options_[j].includeIndices_.Size(); ++k)
  250. active |= 1 << options_[j].includeIndices_[k];
  251. for (unsigned k = 0; k < options_[j].excludeIndices_.Size(); ++k)
  252. active &= ~(1 << options_[j].excludeIndices_[k]);
  253. // Skip dummy separators (options without name and defines)
  254. if (options_[j].name_.Empty() && !options_[j].isVariation_ && options_[j].defines_.Empty())
  255. active &= ~(1 << j);
  256. // If it's a variation, exclude all other variations in the same group
  257. if (options_[j].isVariation_)
  258. {
  259. for (unsigned k = j - 1; k < options_.Size(); --k)
  260. {
  261. if (options_[k].isVariation_)
  262. active &= ~(1 << k);
  263. else
  264. break;
  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. ++variationsActive;
  274. }
  275. }
  276. }
  277. // Check that combination is correct: a variation chosen from all groups, and is unique
  278. if (variationsActive < numVariationGroups || usedCombinations.Contains(active))
  279. continue;
  280. // Check for required defines, which may yet cause this combination to be skipped
  281. unsigned compareBits = active | 0x80000000;
  282. for (unsigned j = 0; j < options_.Size(); ++j)
  283. {
  284. if (active & (1 << j))
  285. {
  286. for (unsigned l = 0; l < options_[j].requirementBits_.Size(); ++l)
  287. {
  288. if (!(compareBits & options_[j].requirementBits_[l]))
  289. {
  290. skipThis = true;
  291. break;
  292. }
  293. }
  294. }
  295. }
  296. if (skipThis)
  297. continue;
  298. String combinationName;
  299. // Build shader combination name from active options
  300. for (unsigned j = 0; j < options_.Size(); ++j)
  301. {
  302. if (active & (1 << j) && options_[j].name_.Length())
  303. combinationName += options_[j].name_;
  304. }
  305. combinations_[combinationName] = active;
  306. usedCombinations.Insert(active);
  307. }
  308. }
  309. }