Technique.cpp 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556
  1. //
  2. // Copyright (c) 2008-2020 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. nullptr
  48. };
  49. static const char* compareModeNames[] =
  50. {
  51. "always",
  52. "equal",
  53. "notequal",
  54. "less",
  55. "lessequal",
  56. "greater",
  57. "greaterequal",
  58. nullptr
  59. };
  60. static const char* lightingModeNames[] =
  61. {
  62. "unlit",
  63. "pervertex",
  64. "perpixel",
  65. nullptr
  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() = default;
  87. void Pass::SetBlendMode(BlendMode mode)
  88. {
  89. blendMode_ = mode;
  90. }
  91. void Pass::SetCullMode(CullMode mode)
  92. {
  93. cullMode_ = mode;
  94. }
  95. void Pass::SetDepthTestMode(CompareMode mode)
  96. {
  97. depthTestMode_ = mode;
  98. }
  99. void Pass::SetLightingMode(PassLightingMode mode)
  100. {
  101. lightingMode_ = mode;
  102. }
  103. void Pass::SetDepthWrite(bool enable)
  104. {
  105. depthWrite_ = enable;
  106. }
  107. void Pass::SetAlphaToCoverage(bool enable)
  108. {
  109. alphaToCoverage_ = enable;
  110. }
  111. void Pass::SetIsDesktop(bool enable)
  112. {
  113. isDesktop_ = enable;
  114. }
  115. void Pass::SetVertexShader(const String& name)
  116. {
  117. vertexShaderName_ = name;
  118. ReleaseShaders();
  119. }
  120. void Pass::SetPixelShader(const String& name)
  121. {
  122. pixelShaderName_ = name;
  123. ReleaseShaders();
  124. }
  125. void Pass::SetVertexShaderDefines(const String& defines)
  126. {
  127. vertexShaderDefines_ = defines;
  128. ReleaseShaders();
  129. }
  130. void Pass::SetPixelShaderDefines(const String& defines)
  131. {
  132. pixelShaderDefines_ = defines;
  133. ReleaseShaders();
  134. }
  135. void Pass::SetVertexShaderDefineExcludes(const String& excludes)
  136. {
  137. vertexShaderDefineExcludes_ = excludes;
  138. ReleaseShaders();
  139. }
  140. void Pass::SetPixelShaderDefineExcludes(const String& excludes)
  141. {
  142. pixelShaderDefineExcludes_ = excludes;
  143. ReleaseShaders();
  144. }
  145. void Pass::ReleaseShaders()
  146. {
  147. vertexShaders_.Clear();
  148. pixelShaders_.Clear();
  149. extraVertexShaders_.Clear();
  150. extraPixelShaders_.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. Vector<SharedPtr<ShaderVariation> >& Pass::GetVertexShaders(const StringHash& extraDefinesHash)
  179. {
  180. // If empty hash, return the base shaders
  181. if (!extraDefinesHash.Value())
  182. return vertexShaders_;
  183. else
  184. return extraVertexShaders_[extraDefinesHash];
  185. }
  186. Vector<SharedPtr<ShaderVariation> >& Pass::GetPixelShaders(const StringHash& extraDefinesHash)
  187. {
  188. if (!extraDefinesHash.Value())
  189. return pixelShaders_;
  190. else
  191. return extraPixelShaders_[extraDefinesHash];
  192. }
  193. unsigned Technique::basePassIndex = 0;
  194. unsigned Technique::alphaPassIndex = 0;
  195. unsigned Technique::materialPassIndex = 0;
  196. unsigned Technique::deferredPassIndex = 0;
  197. unsigned Technique::lightPassIndex = 0;
  198. unsigned Technique::litBasePassIndex = 0;
  199. unsigned Technique::litAlphaPassIndex = 0;
  200. unsigned Technique::shadowPassIndex = 0;
  201. HashMap<String, unsigned> Technique::passIndices;
  202. Technique::Technique(Context* context) :
  203. Resource(context),
  204. isDesktop_(false)
  205. {
  206. #ifdef DESKTOP_GRAPHICS
  207. desktopSupport_ = true;
  208. #else
  209. desktopSupport_ = false;
  210. #endif
  211. }
  212. Technique::~Technique() = default;
  213. void Technique::RegisterObject(Context* context)
  214. {
  215. context->RegisterFactory<Technique>();
  216. }
  217. bool Technique::BeginLoad(Deserializer& source)
  218. {
  219. passes_.Clear();
  220. cloneTechniques_.Clear();
  221. SetMemoryUse(sizeof(Technique));
  222. SharedPtr<XMLFile> xml(new XMLFile(context_));
  223. if (!xml->Load(source))
  224. return false;
  225. XMLElement rootElem = xml->GetRoot();
  226. if (rootElem.HasAttribute("desktop"))
  227. isDesktop_ = rootElem.GetBool("desktop");
  228. String globalVS = rootElem.GetAttribute("vs");
  229. String globalPS = rootElem.GetAttribute("ps");
  230. String globalVSDefines = rootElem.GetAttribute("vsdefines");
  231. String globalPSDefines = rootElem.GetAttribute("psdefines");
  232. // End with space so that the pass-specific defines can be appended
  233. if (!globalVSDefines.Empty())
  234. globalVSDefines += ' ';
  235. if (!globalPSDefines.Empty())
  236. globalPSDefines += ' ';
  237. XMLElement passElem = rootElem.GetChild("pass");
  238. while (passElem)
  239. {
  240. if (passElem.HasAttribute("name"))
  241. {
  242. Pass* newPass = CreatePass(passElem.GetAttribute("name"));
  243. if (passElem.HasAttribute("desktop"))
  244. newPass->SetIsDesktop(passElem.GetBool("desktop"));
  245. // Append global defines only when pass does not redefine the shader
  246. if (passElem.HasAttribute("vs"))
  247. {
  248. newPass->SetVertexShader(passElem.GetAttribute("vs"));
  249. newPass->SetVertexShaderDefines(passElem.GetAttribute("vsdefines"));
  250. }
  251. else
  252. {
  253. newPass->SetVertexShader(globalVS);
  254. newPass->SetVertexShaderDefines(globalVSDefines + passElem.GetAttribute("vsdefines"));
  255. }
  256. if (passElem.HasAttribute("ps"))
  257. {
  258. newPass->SetPixelShader(passElem.GetAttribute("ps"));
  259. newPass->SetPixelShaderDefines(passElem.GetAttribute("psdefines"));
  260. }
  261. else
  262. {
  263. newPass->SetPixelShader(globalPS);
  264. newPass->SetPixelShaderDefines(globalPSDefines + passElem.GetAttribute("psdefines"));
  265. }
  266. newPass->SetVertexShaderDefineExcludes(passElem.GetAttribute("vsexcludes"));
  267. newPass->SetPixelShaderDefineExcludes(passElem.GetAttribute("psexcludes"));
  268. if (passElem.HasAttribute("lighting"))
  269. {
  270. String lighting = passElem.GetAttributeLower("lighting");
  271. newPass->SetLightingMode((PassLightingMode)GetStringListIndex(lighting.CString(), lightingModeNames,
  272. LIGHTING_UNLIT));
  273. }
  274. if (passElem.HasAttribute("blend"))
  275. {
  276. String blend = passElem.GetAttributeLower("blend");
  277. newPass->SetBlendMode((BlendMode)GetStringListIndex(blend.CString(), blendModeNames, BLEND_REPLACE));
  278. }
  279. if (passElem.HasAttribute("cull"))
  280. {
  281. String cull = passElem.GetAttributeLower("cull");
  282. newPass->SetCullMode((CullMode)GetStringListIndex(cull.CString(), cullModeNames, MAX_CULLMODES));
  283. }
  284. if (passElem.HasAttribute("depthtest"))
  285. {
  286. String depthTest = passElem.GetAttributeLower("depthtest");
  287. if (depthTest == "false")
  288. newPass->SetDepthTestMode(CMP_ALWAYS);
  289. else
  290. newPass->SetDepthTestMode((CompareMode)GetStringListIndex(depthTest.CString(), compareModeNames, CMP_LESS));
  291. }
  292. if (passElem.HasAttribute("depthwrite"))
  293. newPass->SetDepthWrite(passElem.GetBool("depthwrite"));
  294. if (passElem.HasAttribute("alphatocoverage"))
  295. newPass->SetAlphaToCoverage(passElem.GetBool("alphatocoverage"));
  296. }
  297. else
  298. URHO3D_LOGERROR("Missing pass name");
  299. passElem = passElem.GetNext("pass");
  300. }
  301. return true;
  302. }
  303. void Technique::SetIsDesktop(bool enable)
  304. {
  305. isDesktop_ = enable;
  306. }
  307. void Technique::ReleaseShaders()
  308. {
  309. for (Vector<SharedPtr<Pass> >::ConstIterator i = passes_.Begin(); i != passes_.End(); ++i)
  310. {
  311. Pass* pass = i->Get();
  312. if (pass)
  313. pass->ReleaseShaders();
  314. }
  315. }
  316. SharedPtr<Technique> Technique::Clone(const String& cloneName) const
  317. {
  318. SharedPtr<Technique> ret(new Technique(context_));
  319. ret->SetIsDesktop(isDesktop_);
  320. ret->SetName(cloneName);
  321. // Deep copy passes
  322. for (Vector<SharedPtr<Pass> >::ConstIterator i = passes_.Begin(); i != passes_.End(); ++i)
  323. {
  324. Pass* srcPass = i->Get();
  325. if (!srcPass)
  326. continue;
  327. Pass* newPass = ret->CreatePass(srcPass->GetName());
  328. newPass->SetCullMode(srcPass->GetCullMode());
  329. newPass->SetBlendMode(srcPass->GetBlendMode());
  330. newPass->SetDepthTestMode(srcPass->GetDepthTestMode());
  331. newPass->SetLightingMode(srcPass->GetLightingMode());
  332. newPass->SetDepthWrite(srcPass->GetDepthWrite());
  333. newPass->SetAlphaToCoverage(srcPass->GetAlphaToCoverage());
  334. newPass->SetIsDesktop(srcPass->IsDesktop());
  335. newPass->SetVertexShader(srcPass->GetVertexShader());
  336. newPass->SetPixelShader(srcPass->GetPixelShader());
  337. newPass->SetVertexShaderDefines(srcPass->GetVertexShaderDefines());
  338. newPass->SetPixelShaderDefines(srcPass->GetPixelShaderDefines());
  339. newPass->SetVertexShaderDefineExcludes(srcPass->GetVertexShaderDefineExcludes());
  340. newPass->SetPixelShaderDefineExcludes(srcPass->GetPixelShaderDefineExcludes());
  341. }
  342. return ret;
  343. }
  344. Pass* Technique::CreatePass(const String& name)
  345. {
  346. Pass* oldPass = GetPass(name);
  347. if (oldPass)
  348. return oldPass;
  349. SharedPtr<Pass> newPass(new Pass(name));
  350. unsigned passIndex = newPass->GetIndex();
  351. if (passIndex >= passes_.Size())
  352. passes_.Resize(passIndex + 1);
  353. passes_[passIndex] = newPass;
  354. // Calculate memory use now
  355. SetMemoryUse((unsigned)(sizeof(Technique) + GetNumPasses() * sizeof(Pass)));
  356. return newPass;
  357. }
  358. void Technique::RemovePass(const String& name)
  359. {
  360. HashMap<String, unsigned>::ConstIterator i = passIndices.Find(name.ToLower());
  361. if (i == passIndices.End())
  362. return;
  363. else if (i->second_ < passes_.Size() && passes_[i->second_].Get())
  364. {
  365. passes_[i->second_].Reset();
  366. SetMemoryUse((unsigned)(sizeof(Technique) + GetNumPasses() * sizeof(Pass)));
  367. }
  368. }
  369. bool Technique::HasPass(const String& name) const
  370. {
  371. HashMap<String, unsigned>::ConstIterator i = passIndices.Find(name.ToLower());
  372. return i != passIndices.End() ? HasPass(i->second_) : false;
  373. }
  374. Pass* Technique::GetPass(const String& name) const
  375. {
  376. HashMap<String, unsigned>::ConstIterator i = passIndices.Find(name.ToLower());
  377. return i != passIndices.End() ? GetPass(i->second_) : nullptr;
  378. }
  379. Pass* Technique::GetSupportedPass(const String& name) const
  380. {
  381. HashMap<String, unsigned>::ConstIterator i = passIndices.Find(name.ToLower());
  382. return i != passIndices.End() ? GetSupportedPass(i->second_) : nullptr;
  383. }
  384. unsigned Technique::GetNumPasses() const
  385. {
  386. unsigned ret = 0;
  387. for (Vector<SharedPtr<Pass> >::ConstIterator i = passes_.Begin(); i != passes_.End(); ++i)
  388. {
  389. if (i->Get())
  390. ++ret;
  391. }
  392. return ret;
  393. }
  394. Vector<String> Technique::GetPassNames() const
  395. {
  396. Vector<String> ret;
  397. for (Vector<SharedPtr<Pass> >::ConstIterator i = passes_.Begin(); i != passes_.End(); ++i)
  398. {
  399. Pass* pass = i->Get();
  400. if (pass)
  401. ret.Push(pass->GetName());
  402. }
  403. return ret;
  404. }
  405. PODVector<Pass*> Technique::GetPasses() const
  406. {
  407. PODVector<Pass*> ret;
  408. for (Vector<SharedPtr<Pass> >::ConstIterator i = passes_.Begin(); i != passes_.End(); ++i)
  409. {
  410. Pass* pass = i->Get();
  411. if (pass)
  412. ret.Push(pass);
  413. }
  414. return ret;
  415. }
  416. SharedPtr<Technique> Technique::CloneWithDefines(const String& vsDefines, const String& psDefines)
  417. {
  418. // Return self if no actual defines
  419. if (vsDefines.Empty() && psDefines.Empty())
  420. return SharedPtr<Technique>(this);
  421. Pair<StringHash, StringHash> key = MakePair(StringHash(vsDefines), StringHash(psDefines));
  422. // Return existing if possible
  423. HashMap<Pair<StringHash, StringHash>, SharedPtr<Technique> >::Iterator i = cloneTechniques_.Find(key);
  424. if (i != cloneTechniques_.End())
  425. return i->second_;
  426. // Set same name as the original for the clones to ensure proper serialization of the material. This should not be a problem
  427. // since the clones are never stored to the resource cache
  428. i = cloneTechniques_.Insert(MakePair(key, Clone(GetName())));
  429. for (Vector<SharedPtr<Pass> >::ConstIterator j = i->second_->passes_.Begin(); j != i->second_->passes_.End(); ++j)
  430. {
  431. Pass* pass = (*j);
  432. if (!pass)
  433. continue;
  434. if (!vsDefines.Empty())
  435. pass->SetVertexShaderDefines(pass->GetVertexShaderDefines() + " " + vsDefines);
  436. if (!psDefines.Empty())
  437. pass->SetPixelShaderDefines(pass->GetPixelShaderDefines() + " " + psDefines);
  438. }
  439. return i->second_;
  440. }
  441. unsigned Technique::GetPassIndex(const String& passName)
  442. {
  443. // Initialize built-in pass indices on first call
  444. if (passIndices.Empty())
  445. {
  446. basePassIndex = passIndices["base"] = 0;
  447. alphaPassIndex = passIndices["alpha"] = 1;
  448. materialPassIndex = passIndices["material"] = 2;
  449. deferredPassIndex = passIndices["deferred"] = 3;
  450. lightPassIndex = passIndices["light"] = 4;
  451. litBasePassIndex = passIndices["litbase"] = 5;
  452. litAlphaPassIndex = passIndices["litalpha"] = 6;
  453. shadowPassIndex = passIndices["shadow"] = 7;
  454. }
  455. String nameLower = passName.ToLower();
  456. HashMap<String, unsigned>::Iterator i = passIndices.Find(nameLower);
  457. if (i != passIndices.End())
  458. return i->second_;
  459. else
  460. {
  461. unsigned newPassIndex = passIndices.Size();
  462. passIndices[nameLower] = newPassIndex;
  463. return newPassIndex;
  464. }
  465. }
  466. }