Technique.cpp 13 KB

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