ShaderParser.cpp 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440
  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. ShaderParser::ShaderParser() :
  28. maxCombinations_(0),
  29. numVariationGroups_(0),
  30. builtAll_(false)
  31. {
  32. }
  33. bool ShaderParser::Parse(ShaderType type, const XMLElement& element, bool buildAll, const Vector<String>& globalDefines, const Vector<String>& globalDefineValues)
  34. {
  35. if (globalDefines.Size() != globalDefineValues.Size())
  36. {
  37. errorMessage_ = "Amount of global define names and values does not match";
  38. return false;
  39. }
  40. globalDefines_ = globalDefines;
  41. globalDefineValues_ = globalDefineValues;
  42. errorMessage_.Clear();
  43. options_.Clear();
  44. combinations_.Clear();
  45. usedCombinations_.Clear();
  46. failedCombinations_.Clear();
  47. nameToIndex_.Clear();
  48. XMLElement shader = element.GetChild("shader");
  49. while (shader)
  50. {
  51. String typeName = String(shader.GetAttribute("type")).ToLower();
  52. if (typeName.Empty() || (type == VS && typeName == "vs") || (type == PS && typeName == "ps"))
  53. {
  54. if (!ParseOptions(shader))
  55. return false;
  56. if (buildAll)
  57. BuildCombinations();
  58. return true;
  59. }
  60. shader = shader.GetNext("shader");
  61. }
  62. return true;
  63. }
  64. bool ShaderParser::HasCombination(const String& name)
  65. {
  66. if (builtAll_)
  67. return combinations_.Contains(name);
  68. else
  69. {
  70. if (combinations_.Contains(name))
  71. return true;
  72. else
  73. return BuildCombination(name);
  74. }
  75. }
  76. ShaderCombination ShaderParser::GetCombination(const String& name)
  77. {
  78. ShaderCombination dest;
  79. if (!builtAll_ && !combinations_.Contains(name))
  80. {
  81. if (!BuildCombination(name))
  82. return dest;
  83. }
  84. HashMap<String, unsigned>::ConstIterator i = combinations_.Find(name);
  85. if (i != combinations_.End())
  86. {
  87. dest.name_ = name;
  88. for (unsigned j = 0; j < options_.Size(); ++j)
  89. {
  90. if (i->second_ & (1 << j))
  91. {
  92. for (unsigned k = 0; k < options_[j].defines_.Size(); ++k)
  93. {
  94. dest.defines_.Push(options_[j].defines_[k]);
  95. dest.defineValues_.Push(options_[j].defineValues_[k]);
  96. }
  97. }
  98. }
  99. for (unsigned j = 0; j < globalDefines_.Size(); ++j)
  100. {
  101. dest.defines_.Push(globalDefines_[j]);
  102. dest.defineValues_.Push(globalDefineValues_[j]);
  103. }
  104. }
  105. return dest;
  106. }
  107. bool ShaderParser::ParseOptions(const XMLElement& element)
  108. {
  109. XMLElement option = element.GetChild();
  110. while (option)
  111. {
  112. String value = option.GetName().ToLower();
  113. if (value == "variation" || value == "option")
  114. {
  115. String name = option.GetAttribute("name");
  116. ShaderOption newOption;
  117. newOption.name_ = name;
  118. newOption.isVariation_ = value == "variation";
  119. String simpleDefine = option.GetAttribute("define");
  120. if (!simpleDefine.Empty())
  121. {
  122. Vector<String> nameAndValue = simpleDefine.Split('=');
  123. if (nameAndValue.Size() == 2)
  124. {
  125. newOption.defines_.Push(nameAndValue[0]);
  126. newOption.defineValues_.Push(nameAndValue[1]);
  127. }
  128. else
  129. {
  130. newOption.defines_.Push(simpleDefine);
  131. newOption.defineValues_.Push("1");
  132. }
  133. }
  134. String simpleExclude = option.GetAttribute("exclude");
  135. if (!simpleExclude.Empty())
  136. newOption.excludes_.Push(simpleExclude);
  137. String simpleInclude = option.GetAttribute("include");
  138. if (!simpleInclude.Empty())
  139. newOption.includes_.Push(simpleInclude);
  140. String simpleRequire = option.GetAttribute("require");
  141. if (!simpleRequire.Empty())
  142. newOption.requires_.Push(simpleRequire);
  143. XMLElement define = option.GetChild("define");
  144. while (define)
  145. {
  146. String defineName = define.GetAttribute("name");
  147. Vector<String> nameAndValue = defineName.Split('=');
  148. if (nameAndValue.Size() == 2)
  149. {
  150. newOption.defines_.Push(nameAndValue[0]);
  151. newOption.defineValues_.Push(nameAndValue[1]);
  152. }
  153. else
  154. {
  155. newOption.defines_.Push(defineName);
  156. newOption.defineValues_.Push("1");
  157. }
  158. define = define.GetNext("define");
  159. }
  160. XMLElement exclude = option.GetChild("exclude");
  161. while (exclude)
  162. {
  163. newOption.excludes_.Push(exclude.GetAttribute("name"));
  164. exclude = exclude.GetNext("exclude");
  165. }
  166. XMLElement include = option.GetChild("include");
  167. while (include)
  168. {
  169. newOption.includes_.Push(include.GetAttribute("name"));
  170. include = include.GetNext("include");
  171. }
  172. XMLElement require = option.GetChild("require");
  173. while (require)
  174. {
  175. newOption.requires_.Push(require.GetAttribute("name"));
  176. require = require.GetNext("require");
  177. }
  178. #ifdef RASPI
  179. newOption.defines_.Push("RASPI");
  180. newOption.defineValues_.Push("1");
  181. #endif
  182. options_.Push(newOption);
  183. if (options_.Size() > 31)
  184. {
  185. errorMessage_ = "Maximum of 31 shader options exceeded";
  186. return false;
  187. }
  188. }
  189. else
  190. {
  191. errorMessage_ = "Unrecognized element " + value + " in shader definition";
  192. return false;
  193. }
  194. option = option.GetNext();
  195. }
  196. maxCombinations_ = 1;
  197. numVariationGroups_ = 0;
  198. // Build search index of option names
  199. for (unsigned i = 0; i < options_.Size(); ++i)
  200. {
  201. ShaderOption& option = options_[i];
  202. maxCombinations_ *= 2;
  203. nameToIndex_[option.name_] = i;
  204. if (option.isVariation_ && (i == 0 || !options_[i - 1].isVariation_))
  205. ++numVariationGroups_;
  206. }
  207. // Preprocess includes/excludes/requires for faster combination handling
  208. for (unsigned i = 0; i < options_.Size(); ++i)
  209. {
  210. ShaderOption& option = options_[i];
  211. option.isDummySeparator_ = option.name_.Empty() && !option.isVariation_ && option.defines_.Empty();
  212. option.excludeBits_ = 0;
  213. option.includeBits_ = 0;
  214. for (unsigned j = 0; j < option.excludes_.Size(); ++j)
  215. option.excludeBits_ |= 1 << nameToIndex_[option.excludes_[j]];
  216. for (unsigned j = 0; j < option.includes_.Size(); ++j)
  217. option.includeBits_ |= 1 << nameToIndex_[option.includes_[j]];
  218. option.variationGroupBits_ = 0;
  219. if (option.isVariation_)
  220. {
  221. for (unsigned j = i - 1; j < options_.Size(); --j)
  222. {
  223. if (options_[j].isVariation_)
  224. option.variationGroupBits_ |= 1 << j;
  225. else
  226. break;
  227. }
  228. for (unsigned j = i + 1; j < options_.Size(); ++j)
  229. {
  230. if (options_[j].isVariation_)
  231. option.variationGroupBits_ |= 1 << j;
  232. else
  233. break;
  234. }
  235. }
  236. for (unsigned j = 0; j < option.requires_.Size(); ++j)
  237. {
  238. unsigned bits = 0;
  239. for (unsigned l = 0; l < options_.Size(); ++l)
  240. {
  241. if (l != i)
  242. {
  243. if (options_[l].name_ == option.requires_[j])
  244. bits |= 1 << l;
  245. else
  246. {
  247. for (unsigned m = 0; m < options_[l].defines_.Size(); ++m)
  248. {
  249. if (options_[l].defines_[m] == option.requires_[j])
  250. {
  251. bits |= 1 << l;
  252. break;
  253. }
  254. }
  255. }
  256. }
  257. }
  258. // If requirements are not satisfied by any option, check global defines
  259. if (!bits)
  260. {
  261. for (unsigned l = 0; l < globalDefines_.Size(); ++l)
  262. {
  263. if (globalDefines_[l] == option.requires_[j])
  264. {
  265. bits |= 0x80000000;
  266. break;
  267. }
  268. }
  269. }
  270. option.requirementBits_.Push(bits);
  271. }
  272. }
  273. return true;
  274. }
  275. void ShaderParser::BuildCombinations()
  276. {
  277. for (unsigned i = 0; i < maxCombinations_; ++i)
  278. BuildCombination(i);
  279. builtAll_ = true;
  280. }
  281. bool ShaderParser::BuildCombination(unsigned active)
  282. {
  283. unsigned variationsActive = 0;
  284. // Check for excludes & includes first
  285. for (unsigned i = 0; i < options_.Size(); ++i)
  286. {
  287. if ((active >> i) & 1)
  288. {
  289. ShaderOption& option = options_[i];
  290. active |= option.includeBits_;
  291. active &= ~option.excludeBits_;
  292. // Skip dummy separators (options without name and defines)
  293. if (option.isDummySeparator_)
  294. active &= ~(1 << i);
  295. // If it's a variation, exclude all other variations in the same group
  296. if (option.isVariation_)
  297. {
  298. active &= ~options_[i].variationGroupBits_;
  299. ++variationsActive;
  300. }
  301. }
  302. }
  303. // Check that combination is correct: a variation chosen from all groups, and is unique
  304. if (variationsActive < numVariationGroups_ || usedCombinations_.Contains(active))
  305. return false;
  306. // Check for required defines, which may yet cause this combination to be skipped
  307. unsigned compareBits = active | 0x80000000;
  308. for (unsigned i = 0; i < options_.Size(); ++i)
  309. {
  310. if (active & (1 << i))
  311. {
  312. ShaderOption& option = options_[i];
  313. for (unsigned j = 0; j < option.requirementBits_.Size(); ++j)
  314. {
  315. if (!(compareBits & option.requirementBits_[j]))
  316. return false;
  317. }
  318. }
  319. }
  320. String combinationName;
  321. // Build shader combination name from active options
  322. for (unsigned i = 0; i < options_.Size(); ++i)
  323. {
  324. if (active & (1 << i))
  325. combinationName += options_[i].name_;
  326. }
  327. combinations_[combinationName] = active;
  328. usedCombinations_.Insert(active);
  329. return true;
  330. }
  331. bool ShaderParser::BuildCombination(const String& name)
  332. {
  333. // Do not attempt again if already failed
  334. if (failedCombinations_.Contains(name))
  335. return false;
  336. // Decode active bits from the name
  337. unsigned active = 0;
  338. String nameCopy = name;
  339. for (unsigned i = 0; i < options_.Size(); ++i)
  340. {
  341. // Option
  342. if (!options_[i].isVariation_)
  343. {
  344. if (!options_[i].name_.Empty() && nameCopy.StartsWith(options_[i].name_))
  345. {
  346. /// \todo Hack fix for options like Alpha & AlphaMask appearing in that order. Not a 100% general fix
  347. if (i < options_.Size() - 1 && nameCopy.StartsWith(options_[i + 1].name_))
  348. continue;
  349. active |= 1 << i;
  350. nameCopy = nameCopy.Substring(options_[i].name_.Length());
  351. }
  352. }
  353. // Variation, must choose only one from the group, furthermore there can be empty variations with defines
  354. else
  355. {
  356. bool emptyFirstVariation = options_[i].name_.Empty();
  357. bool variationFound = false;
  358. unsigned j = i;
  359. for (; j < options_.Size(); ++j)
  360. {
  361. // Reach end of group?
  362. if (!options_[j].isVariation_)
  363. break;
  364. if (!variationFound && !options_[j].name_.Empty() && nameCopy.StartsWith(options_[j].name_))
  365. {
  366. variationFound = true;
  367. active |= 1 << j;
  368. nameCopy = nameCopy.Substring(options_[j].name_.Length());
  369. }
  370. }
  371. // If no other variation was found, must choose the empty first variation, as it may have defines
  372. if (emptyFirstVariation && !variationFound)
  373. active |= 1 << i;
  374. // Skip past the group
  375. i = j - 1;
  376. }
  377. }
  378. bool success = BuildCombination(active);
  379. if (!success)
  380. failedCombinations_.Insert(name);
  381. return success;
  382. }
  383. }