Technique.cpp 17 KB

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