Technique.cpp 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421
  1. //
  2. // Copyright (c) 2008-2015 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 "../Core/Context.h"
  24. #include "../Graphics/Graphics.h"
  25. #include "../IO/Log.h"
  26. #include "../Graphics/Technique.h"
  27. #include "../Core/ProcessUtils.h"
  28. #include "../Core/Profiler.h"
  29. #include "../Resource/ResourceCache.h"
  30. #include "../Graphics/ShaderVariation.h"
  31. #include "../Resource/XMLFile.h"
  32. #include "../DebugNew.h"
  33. namespace Atomic
  34. {
  35. const char* blendModeNames[] =
  36. {
  37. "replace",
  38. "add",
  39. "multiply",
  40. "alpha",
  41. "addalpha",
  42. "premulalpha",
  43. "invdestalpha",
  44. "subtract",
  45. "subtractalpha",
  46. 0
  47. };
  48. static const char* compareModeNames[] =
  49. {
  50. "always",
  51. "equal",
  52. "notequal",
  53. "less",
  54. "lessequal",
  55. "greater",
  56. "greaterequal",
  57. 0
  58. };
  59. static const char* lightingModeNames[] =
  60. {
  61. "unlit",
  62. "pervertex",
  63. "perpixel",
  64. 0
  65. };
  66. Pass::Pass(const String& name) :
  67. blendMode_(BLEND_REPLACE),
  68. depthTestMode_(CMP_LESSEQUAL),
  69. lightingMode_(LIGHTING_UNLIT),
  70. shadersLoadedFrameNumber_(0),
  71. depthWrite_(true),
  72. alphaMask_(false),
  73. isDesktop_(false)
  74. {
  75. name_ = name.ToLower();
  76. index_ = Technique::GetPassIndex(name_);
  77. // Guess default lighting mode from pass name
  78. if (index_ == Technique::basePassIndex || index_ == Technique::alphaPassIndex || index_ == Technique::materialPassIndex ||
  79. index_ == Technique::deferredPassIndex)
  80. lightingMode_ = LIGHTING_PERVERTEX;
  81. else if (index_ == Technique::lightPassIndex || index_ == Technique::litBasePassIndex || index_ == Technique::litAlphaPassIndex)
  82. lightingMode_ = LIGHTING_PERPIXEL;
  83. }
  84. Pass::~Pass()
  85. {
  86. }
  87. void Pass::SetBlendMode(BlendMode mode)
  88. {
  89. blendMode_ = mode;
  90. }
  91. void Pass::SetDepthTestMode(CompareMode mode)
  92. {
  93. depthTestMode_ = mode;
  94. }
  95. void Pass::SetLightingMode(PassLightingMode mode)
  96. {
  97. lightingMode_ = mode;
  98. }
  99. void Pass::SetDepthWrite(bool enable)
  100. {
  101. depthWrite_ = enable;
  102. }
  103. void Pass::SetAlphaMask(bool enable)
  104. {
  105. alphaMask_ = enable;
  106. }
  107. void Pass::SetIsDesktop(bool enable)
  108. {
  109. isDesktop_ = enable;
  110. }
  111. void Pass::SetVertexShader(const String& name)
  112. {
  113. vertexShaderName_ = name;
  114. ReleaseShaders();
  115. }
  116. void Pass::SetPixelShader(const String& name)
  117. {
  118. pixelShaderName_ = name;
  119. ReleaseShaders();
  120. }
  121. void Pass::SetVertexShaderDefines(const String& defines)
  122. {
  123. vertexShaderDefines_ = defines;
  124. ReleaseShaders();
  125. }
  126. void Pass::SetPixelShaderDefines(const String& defines)
  127. {
  128. pixelShaderDefines_ = defines;
  129. ReleaseShaders();
  130. }
  131. void Pass::ReleaseShaders()
  132. {
  133. vertexShaders_.Clear();
  134. pixelShaders_.Clear();
  135. }
  136. void Pass::MarkShadersLoaded(unsigned frameNumber)
  137. {
  138. shadersLoadedFrameNumber_ = frameNumber;
  139. }
  140. unsigned Technique::basePassIndex = 0;
  141. unsigned Technique::alphaPassIndex = 0;
  142. unsigned Technique::materialPassIndex = 0;
  143. unsigned Technique::deferredPassIndex = 0;
  144. unsigned Technique::lightPassIndex = 0;
  145. unsigned Technique::litBasePassIndex = 0;
  146. unsigned Technique::litAlphaPassIndex = 0;
  147. unsigned Technique::shadowPassIndex = 0;
  148. HashMap<String, unsigned> Technique::passIndices;
  149. Technique::Technique(Context* context) :
  150. Resource(context),
  151. isDesktop_(false)
  152. {
  153. #ifdef DESKTOP_GRAPHICS
  154. desktopSupport_ = true;
  155. #else
  156. desktopSupport_ = false;
  157. #endif
  158. }
  159. Technique::~Technique()
  160. {
  161. }
  162. void Technique::RegisterObject(Context* context)
  163. {
  164. context->RegisterFactory<Technique>();
  165. }
  166. bool Technique::BeginLoad(Deserializer& source)
  167. {
  168. passes_.Clear();
  169. SetMemoryUse(sizeof(Technique));
  170. SharedPtr<XMLFile> xml(new XMLFile(context_));
  171. if (!xml->Load(source))
  172. return false;
  173. XMLElement rootElem = xml->GetRoot();
  174. if (rootElem.HasAttribute("desktop"))
  175. isDesktop_ = rootElem.GetBool("desktop");
  176. String globalVS = rootElem.GetAttribute("vs");
  177. String globalPS = rootElem.GetAttribute("ps");
  178. String globalVSDefines = rootElem.GetAttribute("vsdefines");
  179. String globalPSDefines = rootElem.GetAttribute("psdefines");
  180. // End with space so that the pass-specific defines can be appended
  181. if (!globalVSDefines.Empty())
  182. globalVSDefines += ' ';
  183. if (!globalPSDefines.Empty())
  184. globalPSDefines += ' ';
  185. bool globalAlphaMask = false;
  186. if (rootElem.HasAttribute("alphamask"))
  187. globalAlphaMask = rootElem.GetBool("alphamask");
  188. XMLElement passElem = rootElem.GetChild("pass");
  189. while (passElem)
  190. {
  191. if (passElem.HasAttribute("name"))
  192. {
  193. Pass* newPass = CreatePass(passElem.GetAttribute("name"));
  194. if (passElem.HasAttribute("desktop"))
  195. newPass->SetIsDesktop(passElem.GetBool("desktop"));
  196. // Append global defines only when pass does not redefine the shader
  197. if (passElem.HasAttribute("vs"))
  198. {
  199. newPass->SetVertexShader(passElem.GetAttribute("vs"));
  200. newPass->SetVertexShaderDefines(passElem.GetAttribute("vsdefines"));
  201. }
  202. else
  203. {
  204. newPass->SetVertexShader(globalVS);
  205. newPass->SetVertexShaderDefines(globalVSDefines + passElem.GetAttribute("vsdefines"));
  206. }
  207. if (passElem.HasAttribute("ps"))
  208. {
  209. newPass->SetPixelShader(passElem.GetAttribute("ps"));
  210. newPass->SetPixelShaderDefines(passElem.GetAttribute("psdefines"));
  211. }
  212. else
  213. {
  214. newPass->SetPixelShader(globalPS);
  215. newPass->SetPixelShaderDefines(globalPSDefines + passElem.GetAttribute("psdefines"));
  216. }
  217. if (passElem.HasAttribute("lighting"))
  218. {
  219. String lighting = passElem.GetAttributeLower("lighting");
  220. newPass->SetLightingMode((PassLightingMode)GetStringListIndex(lighting.CString(), lightingModeNames,
  221. LIGHTING_UNLIT));
  222. }
  223. if (passElem.HasAttribute("blend"))
  224. {
  225. String blend = passElem.GetAttributeLower("blend");
  226. newPass->SetBlendMode((BlendMode)GetStringListIndex(blend.CString(), blendModeNames, BLEND_REPLACE));
  227. }
  228. if (passElem.HasAttribute("depthtest"))
  229. {
  230. String depthTest = passElem.GetAttributeLower("depthtest");
  231. if (depthTest == "false")
  232. newPass->SetDepthTestMode(CMP_ALWAYS);
  233. else
  234. newPass->SetDepthTestMode((CompareMode)GetStringListIndex(depthTest.CString(), compareModeNames, CMP_LESS));
  235. }
  236. if (passElem.HasAttribute("depthwrite"))
  237. newPass->SetDepthWrite(passElem.GetBool("depthwrite"));
  238. if (passElem.HasAttribute("alphamask"))
  239. newPass->SetAlphaMask(passElem.GetBool("alphamask"));
  240. else
  241. newPass->SetAlphaMask(globalAlphaMask);
  242. }
  243. else
  244. LOGERROR("Missing pass name");
  245. passElem = passElem.GetNext("pass");
  246. }
  247. return true;
  248. }
  249. void Technique::SetIsDesktop(bool enable)
  250. {
  251. isDesktop_ = enable;
  252. }
  253. void Technique::ReleaseShaders()
  254. {
  255. for (Vector<SharedPtr<Pass> >::ConstIterator i = passes_.Begin(); i != passes_.End(); ++i)
  256. {
  257. Pass* pass = i->Get();
  258. if (pass)
  259. pass->ReleaseShaders();
  260. }
  261. }
  262. Pass* Technique::CreatePass(const String& name)
  263. {
  264. Pass* oldPass = GetPass(name);
  265. if (oldPass)
  266. return oldPass;
  267. SharedPtr<Pass> newPass(new Pass(name));
  268. unsigned passIndex = newPass->GetIndex();
  269. if (passIndex >= passes_.Size())
  270. passes_.Resize(passIndex + 1);
  271. passes_[passIndex] = newPass;
  272. // Calculate memory use now
  273. SetMemoryUse(sizeof(Technique) + GetNumPasses() * sizeof(Pass));
  274. return newPass;
  275. }
  276. void Technique::RemovePass(const String& name)
  277. {
  278. HashMap<String, unsigned>::ConstIterator i = passIndices.Find(name.ToLower());
  279. if (i == passIndices.End())
  280. return;
  281. else if (i->second_ < passes_.Size() && passes_[i->second_].Get())
  282. {
  283. passes_[i->second_].Reset();
  284. SetMemoryUse(sizeof(Technique) + GetNumPasses() * sizeof(Pass));
  285. }
  286. }
  287. bool Technique::HasPass(const String& name) const
  288. {
  289. HashMap<String, unsigned>::ConstIterator i = passIndices.Find(name.ToLower());
  290. return i != passIndices.End() ? HasPass(i->second_) : false;
  291. }
  292. Pass* Technique::GetPass(const String& name) const
  293. {
  294. HashMap<String, unsigned>::ConstIterator i = passIndices.Find(name.ToLower());
  295. return i != passIndices.End() ? GetPass(i->second_) : 0;
  296. }
  297. Pass* Technique::GetSupportedPass(const String& name) const
  298. {
  299. HashMap<String, unsigned>::ConstIterator i = passIndices.Find(name.ToLower());
  300. return i != passIndices.End() ? GetSupportedPass(i->second_) : 0;
  301. }
  302. unsigned Technique::GetNumPasses() const
  303. {
  304. unsigned ret = 0;
  305. for (Vector<SharedPtr<Pass> >::ConstIterator i = passes_.Begin(); i != passes_.End(); ++i)
  306. {
  307. if (i->Get())
  308. ++ret;
  309. }
  310. return ret;
  311. }
  312. Vector<String> Technique::GetPassNames() const
  313. {
  314. Vector<String> ret;
  315. for (Vector<SharedPtr<Pass> >::ConstIterator i = passes_.Begin(); i != passes_.End(); ++i)
  316. {
  317. Pass* pass = i->Get();
  318. if (pass)
  319. ret.Push(pass->GetName());
  320. }
  321. return ret;
  322. }
  323. PODVector<Pass*> Technique::GetPasses() const
  324. {
  325. PODVector<Pass*> ret;
  326. for (Vector<SharedPtr<Pass> >::ConstIterator i = passes_.Begin(); i != passes_.End(); ++i)
  327. {
  328. Pass* pass = i->Get();
  329. if (pass)
  330. ret.Push(pass);
  331. }
  332. return ret;
  333. }
  334. unsigned Technique::GetPassIndex(const String& passName)
  335. {
  336. // Initialize built-in pass indices on first call
  337. if (passIndices.Empty())
  338. {
  339. basePassIndex = passIndices["base"] = 0;
  340. alphaPassIndex = passIndices["alpha"] = 1;
  341. materialPassIndex = passIndices["material"] = 2;
  342. deferredPassIndex = passIndices["deferred"] = 3;
  343. lightPassIndex = passIndices["light"] = 4;
  344. litBasePassIndex = passIndices["litbase"] = 5;
  345. litAlphaPassIndex = passIndices["litalpha"] = 6;
  346. shadowPassIndex = passIndices["shadow"] = 7;
  347. }
  348. String nameLower = passName.ToLower();
  349. HashMap<String, unsigned>::Iterator i = passIndices.Find(nameLower);
  350. if (i != passIndices.End())
  351. return i->second_;
  352. else
  353. {
  354. unsigned newPassIndex = passIndices.Size();
  355. passIndices[nameLower] = newPassIndex;
  356. return newPassIndex;
  357. }
  358. }
  359. }