ShaderParser.cpp 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438
  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. for (unsigned i = 0; i < options_.Size(); ++i)
  199. {
  200. maxCombinations_ *= 2;
  201. nameToIndex_[options_[i].name_] = i;
  202. if (options_[i].isVariation_ && (i == 0 || !options_[i - 1].isVariation_))
  203. ++numVariationGroups_;
  204. }
  205. // Preprocess includes/excludes for faster combination handling
  206. for (Vector<ShaderOption>::Iterator i = options_.Begin(); i != options_.End(); ++i)
  207. {
  208. i->excludeIndices_.Resize(i->excludes_.Size());
  209. i->includeIndices_.Resize(i->includes_.Size());
  210. for (unsigned j = 0; j < i->excludes_.Size(); ++j)
  211. i->excludeIndices_[j] = nameToIndex_[i->excludes_[j]];
  212. for (unsigned j = 0; j < i->includes_.Size(); ++j)
  213. i->includeIndices_[j] = nameToIndex_[i->includes_[j]];
  214. }
  215. // Preprocess requirements
  216. for (unsigned i = 0; i < options_.Size(); ++i)
  217. {
  218. for (unsigned j = 0; j < options_[i].requires_.Size(); ++j)
  219. {
  220. unsigned bits = 0;
  221. for (unsigned l = 0; l < options_.Size(); ++l)
  222. {
  223. if (l != i)
  224. {
  225. if (options_[l].name_ == options_[i].requires_[j])
  226. bits |= 1 << l;
  227. else
  228. {
  229. for (unsigned m = 0; m < options_[l].defines_.Size(); ++m)
  230. {
  231. if (options_[l].defines_[m] == options_[i].requires_[j])
  232. {
  233. bits |= 1 << l;
  234. break;
  235. }
  236. }
  237. }
  238. }
  239. }
  240. // If requirements are not satisfied by any option, check global defines
  241. if (!bits)
  242. {
  243. for (unsigned l = 0; l < globalDefines_.Size(); ++l)
  244. {
  245. if (globalDefines_[l] == options_[i].requires_[j])
  246. {
  247. bits |= 0x80000000;
  248. break;
  249. }
  250. }
  251. }
  252. options_[i].requirementBits_.Push(bits);
  253. }
  254. }
  255. return true;
  256. }
  257. void ShaderParser::BuildCombinations()
  258. {
  259. for (unsigned i = 0; i < maxCombinations_; ++i)
  260. BuildCombination(i);
  261. builtAll_ = true;
  262. }
  263. bool ShaderParser::BuildCombination(unsigned active)
  264. {
  265. unsigned variationsActive = 0;
  266. bool skipThis = false;
  267. // Check for excludes & includes first
  268. for (unsigned j = 0; j < options_.Size(); ++j)
  269. {
  270. if ((active >> j) & 1)
  271. {
  272. for (unsigned k = 0; k < options_[j].includeIndices_.Size(); ++k)
  273. active |= 1 << options_[j].includeIndices_[k];
  274. for (unsigned k = 0; k < options_[j].excludeIndices_.Size(); ++k)
  275. active &= ~(1 << options_[j].excludeIndices_[k]);
  276. // Skip dummy separators (options without name and defines)
  277. if (options_[j].name_.Empty() && !options_[j].isVariation_ && options_[j].defines_.Empty())
  278. active &= ~(1 << j);
  279. // If it's a variation, exclude all other variations in the same group
  280. if (options_[j].isVariation_)
  281. {
  282. for (unsigned k = j - 1; k < options_.Size(); --k)
  283. {
  284. if (options_[k].isVariation_)
  285. active &= ~(1 << k);
  286. else
  287. break;
  288. }
  289. for (unsigned k = j + 1; k < options_.Size(); ++k)
  290. {
  291. if (options_[k].isVariation_)
  292. active &= ~(1 << k);
  293. else
  294. break;
  295. }
  296. ++variationsActive;
  297. }
  298. }
  299. }
  300. // Check that combination is correct: a variation chosen from all groups, and is unique
  301. if (variationsActive < numVariationGroups_ || usedCombinations_.Contains(active))
  302. return false;
  303. // Check for required defines, which may yet cause this combination to be skipped
  304. unsigned compareBits = active | 0x80000000;
  305. for (unsigned j = 0; j < options_.Size(); ++j)
  306. {
  307. if (active & (1 << j))
  308. {
  309. for (unsigned l = 0; l < options_[j].requirementBits_.Size(); ++l)
  310. {
  311. if (!(compareBits & options_[j].requirementBits_[l]))
  312. {
  313. skipThis = true;
  314. break;
  315. }
  316. }
  317. }
  318. }
  319. if (skipThis)
  320. return false;
  321. String combinationName;
  322. // Build shader combination name from active options
  323. for (unsigned j = 0; j < options_.Size(); ++j)
  324. {
  325. if (active & (1 << j) && options_[j].name_.Length())
  326. combinationName += options_[j].name_;
  327. }
  328. combinations_[combinationName] = active;
  329. usedCombinations_.Insert(active);
  330. return true;
  331. }
  332. bool ShaderParser::BuildCombination(const String& name)
  333. {
  334. // Do not attempt again if already failed
  335. if (failedCombinations_.Contains(name))
  336. return false;
  337. // Decode active bits from the name
  338. unsigned active = 0;
  339. String nameCopy = name;
  340. for (unsigned i = 0; i < options_.Size(); ++i)
  341. {
  342. // Option
  343. if (!options_[i].isVariation_)
  344. {
  345. if (!options_[i].name_.Empty() && nameCopy.StartsWith(options_[i].name_))
  346. {
  347. /// \todo Hack fix for options like Alpha & AlphaMask appearing in that order. Not a 100% general fix
  348. if (i < options_.Size() - 1 && nameCopy.StartsWith(options_[i + 1].name_))
  349. continue;
  350. active |= 1 << i;
  351. nameCopy = nameCopy.Substring(options_[i].name_.Length());
  352. }
  353. }
  354. // Variation, must choose only one from the group, furthermore there can be empty variations with defines
  355. else
  356. {
  357. bool emptyFirstVariation = options_[i].name_.Empty();
  358. bool variationFound = false;
  359. unsigned j = i;
  360. for (; j < options_.Size(); ++j)
  361. {
  362. // Reach end of group?
  363. if (!options_[j].isVariation_)
  364. break;
  365. if (!variationFound && !options_[j].name_.Empty() && nameCopy.StartsWith(options_[j].name_))
  366. {
  367. variationFound = true;
  368. active |= 1 << j;
  369. nameCopy = nameCopy.Substring(options_[j].name_.Length());
  370. }
  371. }
  372. // If no other variation was found, must choose the empty first variation, as it may have defines
  373. if (emptyFirstVariation && !variationFound)
  374. active |= 1 << i;
  375. // Skip past the group
  376. i = j - 1;
  377. }
  378. }
  379. bool success = BuildCombination(active);
  380. if (!success)
  381. failedCombinations_.Insert(name);
  382. return success;
  383. }
  384. }