Technique.cpp 12 KB

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