Technique.cpp 9.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358
  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 "Precompiled.h"
  23. #include "../Core/Context.h"
  24. #include "../Graphics/Graphics.h"
  25. #include "../IO/Log.h"
  26. #include "../Graphics/Technique.h"
  27. #include "../Core/ProcessUtils.h"
  28. #include "../Core/Profiler.h"
  29. #include "../Resource/ResourceCache.h"
  30. #include "../Graphics/ShaderVariation.h"
  31. #include "../Resource/XMLFile.h"
  32. #include "../DebugNew.h"
  33. namespace Atomic
  34. {
  35. const char* blendModeNames[] =
  36. {
  37. "replace",
  38. "add",
  39. "multiply",
  40. "alpha",
  41. "addalpha",
  42. "premulalpha",
  43. "invdestalpha",
  44. "subtract",
  45. "subtractalpha",
  46. 0
  47. };
  48. static const char* compareModeNames[] =
  49. {
  50. "always",
  51. "equal",
  52. "notequal",
  53. "less",
  54. "lessequal",
  55. "greater",
  56. "greaterequal",
  57. 0
  58. };
  59. static const char* lightingModeNames[] =
  60. {
  61. "unlit",
  62. "pervertex",
  63. "perpixel",
  64. 0
  65. };
  66. Pass::Pass(StringHash type) :
  67. type_(type),
  68. blendMode_(BLEND_REPLACE),
  69. depthTestMode_(CMP_LESSEQUAL),
  70. lightingMode_(LIGHTING_UNLIT),
  71. shadersLoadedFrameNumber_(0),
  72. depthWrite_(true),
  73. alphaMask_(false),
  74. isSM3_(false),
  75. isDesktop_(false)
  76. {
  77. // Guess default lighting mode from pass name
  78. if (type == PASS_BASE || type == PASS_ALPHA || type == PASS_MATERIAL || type == PASS_DEFERRED || type == PASS_LIGHT2D)
  79. lightingMode_ = LIGHTING_PERVERTEX;
  80. else if (type == PASS_LIGHT || type == PASS_LITBASE || type == PASS_LITALPHA)
  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::SetIsSM3(bool enable)
  107. {
  108. isSM3_ = enable;
  109. }
  110. void Pass::SetIsDesktop(bool enable)
  111. {
  112. isDesktop_ = enable;
  113. }
  114. void Pass::SetVertexShader(const String& name)
  115. {
  116. vertexShaderName_ = name;
  117. ReleaseShaders();
  118. }
  119. void Pass::SetPixelShader(const String& name)
  120. {
  121. pixelShaderName_ = name;
  122. ReleaseShaders();
  123. }
  124. void Pass::SetVertexShaderDefines(const String& defines)
  125. {
  126. vertexShaderDefines_ = defines;
  127. ReleaseShaders();
  128. }
  129. void Pass::SetPixelShaderDefines(const String& defines)
  130. {
  131. pixelShaderDefines_ = defines;
  132. ReleaseShaders();
  133. }
  134. void Pass::ReleaseShaders()
  135. {
  136. vertexShaders_.Clear();
  137. pixelShaders_.Clear();
  138. }
  139. void Pass::MarkShadersLoaded(unsigned frameNumber)
  140. {
  141. shadersLoadedFrameNumber_ = frameNumber;
  142. }
  143. Technique::Technique(Context* context) :
  144. Resource(context),
  145. isSM3_(false),
  146. isDesktop_(false),
  147. numPasses_(0)
  148. {
  149. Graphics* graphics = GetSubsystem<Graphics>();
  150. sm3Support_ = graphics ? graphics->GetSM3Support() : true;
  151. #ifdef DESKTOP_GRAPHICS
  152. desktopSupport_ = true;
  153. #else
  154. desktopSupport_ = false;
  155. #endif
  156. }
  157. Technique::~Technique()
  158. {
  159. }
  160. void Technique::RegisterObject(Context* context)
  161. {
  162. context->RegisterFactory<Technique>();
  163. }
  164. bool Technique::BeginLoad(Deserializer& source)
  165. {
  166. passes_.Clear();
  167. numPasses_ = 0;
  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("sm3"))
  174. isSM3_ = rootElem.GetBool("sm3");
  175. if (rootElem.HasAttribute("desktop"))
  176. isDesktop_ = rootElem.GetBool("desktop");
  177. String globalVS = rootElem.GetAttribute("vs");
  178. String globalPS = rootElem.GetAttribute("ps");
  179. String globalVSDefines = rootElem.GetAttribute("vsdefines");
  180. String globalPSDefines = rootElem.GetAttribute("psdefines");
  181. // End with space so that the pass-specific defines can be appended
  182. if (!globalVSDefines.Empty())
  183. globalVSDefines += ' ';
  184. if (!globalPSDefines.Empty())
  185. globalPSDefines += ' ';
  186. bool globalAlphaMask = false;
  187. if (rootElem.HasAttribute("alphamask"))
  188. globalAlphaMask = rootElem.GetBool("alphamask");
  189. XMLElement passElem = rootElem.GetChild("pass");
  190. while (passElem)
  191. {
  192. if (passElem.HasAttribute("name"))
  193. {
  194. StringHash nameHash(passElem.GetAttribute("name"));
  195. Pass* newPass = CreatePass(nameHash);
  196. if (passElem.HasAttribute("sm3"))
  197. newPass->SetIsSM3(passElem.GetBool("sm3"));
  198. if (passElem.HasAttribute("desktop"))
  199. newPass->SetIsDesktop(passElem.GetBool("desktop"));
  200. // Append global defines only when pass does not redefine the shader
  201. if (passElem.HasAttribute("vs"))
  202. {
  203. newPass->SetVertexShader(passElem.GetAttribute("vs"));
  204. newPass->SetVertexShaderDefines(passElem.GetAttribute("vsdefines"));
  205. }
  206. else
  207. {
  208. newPass->SetVertexShader(globalVS);
  209. newPass->SetVertexShaderDefines(globalVSDefines + passElem.GetAttribute("vsdefines"));
  210. }
  211. if (passElem.HasAttribute("ps"))
  212. {
  213. newPass->SetPixelShader(passElem.GetAttribute("ps"));
  214. newPass->SetPixelShaderDefines(passElem.GetAttribute("psdefines"));
  215. }
  216. else
  217. {
  218. newPass->SetPixelShader(globalPS);
  219. newPass->SetPixelShaderDefines(globalPSDefines + passElem.GetAttribute("psdefines"));
  220. }
  221. if (passElem.HasAttribute("lighting"))
  222. {
  223. String lighting = passElem.GetAttributeLower("lighting");
  224. newPass->SetLightingMode((PassLightingMode)GetStringListIndex(lighting.CString(), lightingModeNames,
  225. LIGHTING_UNLIT));
  226. }
  227. if (passElem.HasAttribute("blend"))
  228. {
  229. String blend = passElem.GetAttributeLower("blend");
  230. newPass->SetBlendMode((BlendMode)GetStringListIndex(blend.CString(), blendModeNames, BLEND_REPLACE));
  231. }
  232. if (passElem.HasAttribute("depthtest"))
  233. {
  234. String depthTest = passElem.GetAttributeLower("depthtest");
  235. if (depthTest == "false")
  236. newPass->SetDepthTestMode(CMP_ALWAYS);
  237. else
  238. newPass->SetDepthTestMode((CompareMode)GetStringListIndex(depthTest.CString(), compareModeNames, CMP_LESS));
  239. }
  240. if (passElem.HasAttribute("depthwrite"))
  241. newPass->SetDepthWrite(passElem.GetBool("depthwrite"));
  242. if (passElem.HasAttribute("alphamask"))
  243. newPass->SetAlphaMask(passElem.GetBool("alphamask"));
  244. else
  245. newPass->SetAlphaMask(globalAlphaMask);
  246. }
  247. else
  248. LOGERROR("Missing pass name");
  249. passElem = passElem.GetNext("pass");
  250. }
  251. return true;
  252. }
  253. void Technique::SetIsSM3(bool enable)
  254. {
  255. isSM3_ = enable;
  256. }
  257. void Technique::SetIsDesktop(bool enable)
  258. {
  259. isDesktop_ = enable;
  260. }
  261. void Technique::ReleaseShaders()
  262. {
  263. PODVector<SharedPtr<Pass>*> allPasses = passes_.Values();
  264. for (unsigned i = 0; i < allPasses.Size(); ++i)
  265. allPasses[i]->Get()->ReleaseShaders();
  266. }
  267. Pass* Technique::CreatePass(StringHash type)
  268. {
  269. Pass* oldPass = GetPass(type);
  270. if (oldPass)
  271. return oldPass;
  272. SharedPtr<Pass> newPass(new Pass(type));
  273. passes_.Insert(type.Value(), newPass);
  274. // Calculate memory use now
  275. SetMemoryUse(sizeof(Technique) + ++numPasses_ * sizeof(Pass));
  276. return newPass;
  277. }
  278. void Technique::RemovePass(StringHash type)
  279. {
  280. if (passes_.Erase(type.Value()))
  281. SetMemoryUse(sizeof(Technique) + --numPasses_ * sizeof(Pass));
  282. }
  283. Vector<StringHash> Technique::GetPassTypes() const
  284. {
  285. // Convert PODVector<unsigned> to Vector<StringHash>
  286. PODVector<unsigned> vectorIn = passes_.Keys();
  287. Vector<StringHash> vectorOut;
  288. vectorOut.Reserve(vectorIn.Size());
  289. for (unsigned i = 0; i < vectorIn.Size(); ++i)
  290. vectorOut.Push(StringHash(vectorIn[i]));
  291. return vectorOut;
  292. }
  293. PODVector<Pass*> Technique::GetPasses() const
  294. {
  295. // Convert PODVector<SharedPtr<Pass>*> to PODVector<Pass*>
  296. PODVector<SharedPtr<Pass>*> vectorIn = passes_.Values();
  297. PODVector<Pass*> vectorOut;
  298. vectorOut.Reserve(vectorIn.Size());
  299. for (unsigned i = 0; i < vectorIn.Size(); ++i)
  300. vectorOut.Push(vectorIn[i]->Get());
  301. return vectorOut;
  302. }
  303. }