Technique.cpp 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540
  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 Atomic
  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. alphaToCoverage_(false),
  74. depthWrite_(true),
  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::SetAlphaToCoverage(bool enable)
  110. {
  111. alphaToCoverage_ = 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::SetVertexShaderDefineExcludes(const String& excludes)
  138. {
  139. vertexShaderDefineExcludes_ = excludes;
  140. ReleaseShaders();
  141. }
  142. void Pass::SetPixelShaderDefineExcludes(const String& excludes)
  143. {
  144. pixelShaderDefineExcludes_ = excludes;
  145. ReleaseShaders();
  146. }
  147. void Pass::ReleaseShaders()
  148. {
  149. vertexShaders_.Clear();
  150. pixelShaders_.Clear();
  151. }
  152. void Pass::MarkShadersLoaded(unsigned frameNumber)
  153. {
  154. shadersLoadedFrameNumber_ = frameNumber;
  155. }
  156. String Pass::GetEffectiveVertexShaderDefines() const
  157. {
  158. // Prefer to return just the original defines if possible
  159. if (vertexShaderDefineExcludes_.Empty())
  160. return vertexShaderDefines_;
  161. Vector<String> vsDefines = vertexShaderDefines_.Split(' ');
  162. Vector<String> vsExcludes = vertexShaderDefineExcludes_.Split(' ');
  163. for (unsigned i = 0; i < vsExcludes.Size(); ++i)
  164. vsDefines.Remove(vsExcludes[i]);
  165. return String::Joined(vsDefines, " ");
  166. }
  167. String Pass::GetEffectivePixelShaderDefines() const
  168. {
  169. // Prefer to return just the original defines if possible
  170. if (pixelShaderDefineExcludes_.Empty())
  171. return pixelShaderDefines_;
  172. Vector<String> psDefines = pixelShaderDefines_.Split(' ');
  173. Vector<String> psExcludes = pixelShaderDefineExcludes_.Split(' ');
  174. for (unsigned i = 0; i < psExcludes.Size(); ++i)
  175. psDefines.Remove(psExcludes[i]);
  176. return String::Joined(psDefines, " ");
  177. }
  178. unsigned Technique::basePassIndex = 0;
  179. unsigned Technique::alphaPassIndex = 0;
  180. unsigned Technique::materialPassIndex = 0;
  181. unsigned Technique::deferredPassIndex = 0;
  182. unsigned Technique::lightPassIndex = 0;
  183. unsigned Technique::litBasePassIndex = 0;
  184. unsigned Technique::litAlphaPassIndex = 0;
  185. unsigned Technique::shadowPassIndex = 0;
  186. HashMap<String, unsigned> Technique::passIndices;
  187. Technique::Technique(Context* context) :
  188. Resource(context),
  189. isDesktop_(false)
  190. {
  191. #ifdef DESKTOP_GRAPHICS
  192. desktopSupport_ = true;
  193. #else
  194. desktopSupport_ = false;
  195. #endif
  196. }
  197. Technique::~Technique()
  198. {
  199. }
  200. void Technique::RegisterObject(Context* context)
  201. {
  202. context->RegisterFactory<Technique>();
  203. }
  204. bool Technique::BeginLoad(Deserializer& source)
  205. {
  206. passes_.Clear();
  207. cloneTechniques_.Clear();
  208. SetMemoryUse(sizeof(Technique));
  209. SharedPtr<XMLFile> xml(new XMLFile(context_));
  210. if (!xml->Load(source))
  211. return false;
  212. XMLElement rootElem = xml->GetRoot();
  213. if (rootElem.HasAttribute("desktop"))
  214. isDesktop_ = rootElem.GetBool("desktop");
  215. String globalVS = rootElem.GetAttribute("vs");
  216. String globalPS = rootElem.GetAttribute("ps");
  217. String globalVSDefines = rootElem.GetAttribute("vsdefines");
  218. String globalPSDefines = rootElem.GetAttribute("psdefines");
  219. // End with space so that the pass-specific defines can be appended
  220. if (!globalVSDefines.Empty())
  221. globalVSDefines += ' ';
  222. if (!globalPSDefines.Empty())
  223. globalPSDefines += ' ';
  224. XMLElement passElem = rootElem.GetChild("pass");
  225. while (passElem)
  226. {
  227. if (passElem.HasAttribute("name"))
  228. {
  229. Pass* newPass = CreatePass(passElem.GetAttribute("name"));
  230. if (passElem.HasAttribute("desktop"))
  231. newPass->SetIsDesktop(passElem.GetBool("desktop"));
  232. // Append global defines only when pass does not redefine the shader
  233. if (passElem.HasAttribute("vs"))
  234. {
  235. newPass->SetVertexShader(passElem.GetAttribute("vs"));
  236. newPass->SetVertexShaderDefines(passElem.GetAttribute("vsdefines"));
  237. }
  238. else
  239. {
  240. newPass->SetVertexShader(globalVS);
  241. newPass->SetVertexShaderDefines(globalVSDefines + passElem.GetAttribute("vsdefines"));
  242. }
  243. if (passElem.HasAttribute("ps"))
  244. {
  245. newPass->SetPixelShader(passElem.GetAttribute("ps"));
  246. newPass->SetPixelShaderDefines(passElem.GetAttribute("psdefines"));
  247. }
  248. else
  249. {
  250. newPass->SetPixelShader(globalPS);
  251. newPass->SetPixelShaderDefines(globalPSDefines + passElem.GetAttribute("psdefines"));
  252. }
  253. newPass->SetVertexShaderDefineExcludes(passElem.GetAttribute("vsexcludes"));
  254. newPass->SetPixelShaderDefineExcludes(passElem.GetAttribute("psexcludes"));
  255. if (passElem.HasAttribute("lighting"))
  256. {
  257. String lighting = passElem.GetAttributeLower("lighting");
  258. newPass->SetLightingMode((PassLightingMode)GetStringListIndex(lighting.CString(), lightingModeNames,
  259. LIGHTING_UNLIT));
  260. }
  261. if (passElem.HasAttribute("blend"))
  262. {
  263. String blend = passElem.GetAttributeLower("blend");
  264. newPass->SetBlendMode((BlendMode)GetStringListIndex(blend.CString(), blendModeNames, BLEND_REPLACE));
  265. }
  266. if (passElem.HasAttribute("cull"))
  267. {
  268. String cull = passElem.GetAttributeLower("cull");
  269. newPass->SetCullMode((CullMode)GetStringListIndex(cull.CString(), cullModeNames, MAX_CULLMODES));
  270. }
  271. if (passElem.HasAttribute("depthtest"))
  272. {
  273. String depthTest = passElem.GetAttributeLower("depthtest");
  274. if (depthTest == "false")
  275. newPass->SetDepthTestMode(CMP_ALWAYS);
  276. else
  277. newPass->SetDepthTestMode((CompareMode)GetStringListIndex(depthTest.CString(), compareModeNames, CMP_LESS));
  278. }
  279. if (passElem.HasAttribute("depthwrite"))
  280. newPass->SetDepthWrite(passElem.GetBool("depthwrite"));
  281. if (passElem.HasAttribute("alphatocoverage"))
  282. newPass->SetAlphaToCoverage(passElem.GetBool("alphatocoverage"));
  283. }
  284. else
  285. ATOMIC_LOGERROR("Missing pass name");
  286. passElem = passElem.GetNext("pass");
  287. }
  288. return true;
  289. }
  290. void Technique::SetIsDesktop(bool enable)
  291. {
  292. isDesktop_ = enable;
  293. }
  294. void Technique::ReleaseShaders()
  295. {
  296. for (Vector<SharedPtr<Pass> >::ConstIterator i = passes_.Begin(); i != passes_.End(); ++i)
  297. {
  298. Pass* pass = i->Get();
  299. if (pass)
  300. pass->ReleaseShaders();
  301. }
  302. }
  303. SharedPtr<Technique> Technique::Clone(const String& cloneName) const
  304. {
  305. SharedPtr<Technique> ret(new Technique(context_));
  306. ret->SetIsDesktop(isDesktop_);
  307. ret->SetName(cloneName);
  308. // Deep copy passes
  309. for (Vector<SharedPtr<Pass> >::ConstIterator i = passes_.Begin(); i != passes_.End(); ++i)
  310. {
  311. Pass* srcPass = i->Get();
  312. if (!srcPass)
  313. continue;
  314. Pass* newPass = ret->CreatePass(srcPass->GetName());
  315. newPass->SetBlendMode(srcPass->GetBlendMode());
  316. newPass->SetDepthTestMode(srcPass->GetDepthTestMode());
  317. newPass->SetLightingMode(srcPass->GetLightingMode());
  318. newPass->SetDepthWrite(srcPass->GetDepthWrite());
  319. newPass->SetAlphaToCoverage(srcPass->GetAlphaToCoverage());
  320. newPass->SetIsDesktop(srcPass->IsDesktop());
  321. newPass->SetVertexShader(srcPass->GetVertexShader());
  322. newPass->SetPixelShader(srcPass->GetPixelShader());
  323. newPass->SetVertexShaderDefines(srcPass->GetVertexShaderDefines());
  324. newPass->SetPixelShaderDefines(srcPass->GetPixelShaderDefines());
  325. newPass->SetVertexShaderDefineExcludes(srcPass->GetVertexShaderDefineExcludes());
  326. newPass->SetPixelShaderDefineExcludes(srcPass->GetPixelShaderDefineExcludes());
  327. }
  328. return ret;
  329. }
  330. Pass* Technique::CreatePass(const String& name)
  331. {
  332. Pass* oldPass = GetPass(name);
  333. if (oldPass)
  334. return oldPass;
  335. SharedPtr<Pass> newPass(new Pass(name));
  336. unsigned passIndex = newPass->GetIndex();
  337. if (passIndex >= passes_.Size())
  338. passes_.Resize(passIndex + 1);
  339. passes_[passIndex] = newPass;
  340. // Calculate memory use now
  341. SetMemoryUse((unsigned)(sizeof(Technique) + GetNumPasses() * sizeof(Pass)));
  342. return newPass;
  343. }
  344. void Technique::RemovePass(const String& name)
  345. {
  346. HashMap<String, unsigned>::ConstIterator i = passIndices.Find(name.ToLower());
  347. if (i == passIndices.End())
  348. return;
  349. else if (i->second_ < passes_.Size() && passes_[i->second_].Get())
  350. {
  351. passes_[i->second_].Reset();
  352. SetMemoryUse((unsigned)(sizeof(Technique) + GetNumPasses() * sizeof(Pass)));
  353. }
  354. }
  355. bool Technique::HasPass(const String& name) const
  356. {
  357. HashMap<String, unsigned>::ConstIterator i = passIndices.Find(name.ToLower());
  358. return i != passIndices.End() ? HasPass(i->second_) : false;
  359. }
  360. Pass* Technique::GetPass(const String& name) const
  361. {
  362. HashMap<String, unsigned>::ConstIterator i = passIndices.Find(name.ToLower());
  363. return i != passIndices.End() ? GetPass(i->second_) : 0;
  364. }
  365. Pass* Technique::GetSupportedPass(const String& name) const
  366. {
  367. HashMap<String, unsigned>::ConstIterator i = passIndices.Find(name.ToLower());
  368. return i != passIndices.End() ? GetSupportedPass(i->second_) : 0;
  369. }
  370. unsigned Technique::GetNumPasses() const
  371. {
  372. unsigned ret = 0;
  373. for (Vector<SharedPtr<Pass> >::ConstIterator i = passes_.Begin(); i != passes_.End(); ++i)
  374. {
  375. if (i->Get())
  376. ++ret;
  377. }
  378. return ret;
  379. }
  380. Vector<String> Technique::GetPassNames() const
  381. {
  382. Vector<String> ret;
  383. for (Vector<SharedPtr<Pass> >::ConstIterator i = passes_.Begin(); i != passes_.End(); ++i)
  384. {
  385. Pass* pass = i->Get();
  386. if (pass)
  387. ret.Push(pass->GetName());
  388. }
  389. return ret;
  390. }
  391. PODVector<Pass*> Technique::GetPasses() const
  392. {
  393. PODVector<Pass*> ret;
  394. for (Vector<SharedPtr<Pass> >::ConstIterator i = passes_.Begin(); i != passes_.End(); ++i)
  395. {
  396. Pass* pass = i->Get();
  397. if (pass)
  398. ret.Push(pass);
  399. }
  400. return ret;
  401. }
  402. SharedPtr<Technique> Technique::CloneWithDefines(const String& vsDefines, const String& psDefines)
  403. {
  404. // Return self if no actual defines
  405. if (vsDefines.Empty() && psDefines.Empty())
  406. return SharedPtr<Technique>(this);
  407. Pair<StringHash, StringHash> key = MakePair(StringHash(vsDefines), StringHash(psDefines));
  408. // Return existing if possible
  409. HashMap<Pair<StringHash, StringHash>, SharedPtr<Technique> >::Iterator i = cloneTechniques_.Find(key);
  410. if (i != cloneTechniques_.End())
  411. return i->second_;
  412. // Set same name as the original for the clones to ensure proper serialization of the material. This should not be a problem
  413. // since the clones are never stored to the resource cache
  414. i = cloneTechniques_.Insert(MakePair(key, Clone(GetName())));
  415. for (Vector<SharedPtr<Pass> >::ConstIterator j = i->second_->passes_.Begin(); j != i->second_->passes_.End(); ++j)
  416. {
  417. Pass* pass = (*j);
  418. if (!pass)
  419. continue;
  420. if (!vsDefines.Empty())
  421. pass->SetVertexShaderDefines(pass->GetVertexShaderDefines() + " " + vsDefines);
  422. if (!psDefines.Empty())
  423. pass->SetPixelShaderDefines(pass->GetPixelShaderDefines() + " " + psDefines);
  424. }
  425. return i->second_;
  426. }
  427. unsigned Technique::GetPassIndex(const String& passName)
  428. {
  429. // Initialize built-in pass indices on first call
  430. if (passIndices.Empty())
  431. {
  432. basePassIndex = passIndices["base"] = 0;
  433. alphaPassIndex = passIndices["alpha"] = 1;
  434. materialPassIndex = passIndices["material"] = 2;
  435. deferredPassIndex = passIndices["deferred"] = 3;
  436. lightPassIndex = passIndices["light"] = 4;
  437. litBasePassIndex = passIndices["litbase"] = 5;
  438. litAlphaPassIndex = passIndices["litalpha"] = 6;
  439. shadowPassIndex = passIndices["shadow"] = 7;
  440. }
  441. String nameLower = passName.ToLower();
  442. HashMap<String, unsigned>::Iterator i = passIndices.Find(nameLower);
  443. if (i != passIndices.End())
  444. return i->second_;
  445. else
  446. {
  447. unsigned newPassIndex = passIndices.Size();
  448. passIndices[nameLower] = newPassIndex;
  449. return newPassIndex;
  450. }
  451. }
  452. }