Technique.cpp 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364
  1. //
  2. // Copyright (c) 2008-2014 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 "Context.h"
  24. #include "Graphics.h"
  25. #include "Log.h"
  26. #include "Technique.h"
  27. #include "ProcessUtils.h"
  28. #include "Profiler.h"
  29. #include "ResourceCache.h"
  30. #include "ShaderVariation.h"
  31. #include "XMLFile.h"
  32. #include "DebugNew.h"
  33. namespace Urho3D
  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. static bool desktopSupportChecked = false;
  67. static bool desktopSupportResult = false;
  68. Pass::Pass(StringHash type) :
  69. type_(type),
  70. blendMode_(BLEND_REPLACE),
  71. depthTestMode_(CMP_LESSEQUAL),
  72. lightingMode_(LIGHTING_UNLIT),
  73. shadersLoadedFrameNumber_(0),
  74. depthWrite_(true),
  75. alphaMask_(false),
  76. isSM3_(false),
  77. isDesktop_(false)
  78. {
  79. // Guess default lighting mode from pass name
  80. if (type == PASS_BASE || type == PASS_ALPHA || type == PASS_MATERIAL || type == PASS_DEFERRED)
  81. lightingMode_ = LIGHTING_PERVERTEX;
  82. else if (type == PASS_LIGHT || type == PASS_LITBASE || type == PASS_LITALPHA)
  83. lightingMode_ = LIGHTING_PERPIXEL;
  84. }
  85. Pass::~Pass()
  86. {
  87. }
  88. void Pass::SetBlendMode(BlendMode mode)
  89. {
  90. blendMode_ = mode;
  91. }
  92. void Pass::SetDepthTestMode(CompareMode mode)
  93. {
  94. depthTestMode_ = mode;
  95. }
  96. void Pass::SetLightingMode(PassLightingMode mode)
  97. {
  98. lightingMode_ = mode;
  99. }
  100. void Pass::SetDepthWrite(bool enable)
  101. {
  102. depthWrite_ = enable;
  103. }
  104. void Pass::SetAlphaMask(bool enable)
  105. {
  106. alphaMask_ = enable;
  107. }
  108. void Pass::SetIsSM3(bool enable)
  109. {
  110. isSM3_ = enable;
  111. }
  112. void Pass::SetIsDesktop(bool enable)
  113. {
  114. isDesktop_ = enable;
  115. }
  116. void Pass::SetVertexShader(const String& name)
  117. {
  118. vertexShaderName_ = name;
  119. ReleaseShaders();
  120. }
  121. void Pass::SetPixelShader(const String& name)
  122. {
  123. pixelShaderName_ = name;
  124. ReleaseShaders();
  125. }
  126. void Pass::SetVertexShaderDefines(const String& defines)
  127. {
  128. vertexShaderDefines_ = defines;
  129. ReleaseShaders();
  130. }
  131. void Pass::SetPixelShaderDefines(const String& defines)
  132. {
  133. pixelShaderDefines_ = defines;
  134. ReleaseShaders();
  135. }
  136. void Pass::ReleaseShaders()
  137. {
  138. vertexShaders_.Clear();
  139. pixelShaders_.Clear();
  140. }
  141. void Pass::MarkShadersLoaded(unsigned frameNumber)
  142. {
  143. shadersLoadedFrameNumber_ = frameNumber;
  144. }
  145. Technique::Technique(Context* context) :
  146. Resource(context),
  147. isSM3_(false),
  148. isDesktop_(false),
  149. numPasses_(0)
  150. {
  151. Graphics* graphics = GetSubsystem<Graphics>();
  152. sm3Support_ = graphics ? graphics->GetSM3Support() : true;
  153. if (!desktopSupportChecked)
  154. {
  155. String platformString = GetPlatform();
  156. desktopSupportResult = (platformString == "Windows" || platformString == "Mac OS X" || platformString == "Linux");
  157. desktopSupportChecked = true;
  158. }
  159. desktopSupport_ = desktopSupportResult;
  160. }
  161. Technique::~Technique()
  162. {
  163. }
  164. void Technique::RegisterObject(Context* context)
  165. {
  166. context->RegisterFactory<Technique>();
  167. }
  168. bool Technique::BeginLoad(Deserializer& source)
  169. {
  170. passes_.Clear();
  171. numPasses_ = 0;
  172. SetMemoryUse(sizeof(Technique));
  173. SharedPtr<XMLFile> xml(new XMLFile(context_));
  174. if (!xml->Load(source))
  175. return false;
  176. XMLElement rootElem = xml->GetRoot();
  177. if (rootElem.HasAttribute("sm3"))
  178. isSM3_ = rootElem.GetBool("sm3");
  179. if (rootElem.HasAttribute("desktop"))
  180. isDesktop_ = rootElem.GetBool("desktop");
  181. String globalVS = rootElem.GetAttribute("vs");
  182. String globalPS = rootElem.GetAttribute("ps");
  183. String globalVSDefines = rootElem.GetAttribute("vsdefines");
  184. String globalPSDefines = rootElem.GetAttribute("psdefines");
  185. // End with space so that the pass-specific defines can be appended
  186. if (!globalVSDefines.Empty())
  187. globalVSDefines += ' ';
  188. if (!globalPSDefines.Empty())
  189. globalPSDefines += ' ';
  190. bool globalAlphaMask = false;
  191. if (rootElem.HasAttribute("alphamask"))
  192. globalAlphaMask = rootElem.GetBool("alphamask");
  193. XMLElement passElem = rootElem.GetChild("pass");
  194. while (passElem)
  195. {
  196. if (passElem.HasAttribute("name"))
  197. {
  198. StringHash nameHash(passElem.GetAttribute("name"));
  199. Pass* newPass = CreatePass(nameHash);
  200. if (passElem.HasAttribute("sm3"))
  201. newPass->SetIsSM3(passElem.GetBool("sm3"));
  202. if (passElem.HasAttribute("desktop"))
  203. newPass->SetIsDesktop(passElem.GetBool("desktop"));
  204. // Append global defines only when pass does not redefine the shader
  205. if (passElem.HasAttribute("vs"))
  206. {
  207. newPass->SetVertexShader(passElem.GetAttribute("vs"));
  208. newPass->SetVertexShaderDefines(passElem.GetAttribute("vsdefines"));
  209. }
  210. else
  211. {
  212. newPass->SetVertexShader(globalVS);
  213. newPass->SetVertexShaderDefines(globalVSDefines + passElem.GetAttribute("vsdefines"));
  214. }
  215. if (passElem.HasAttribute("ps"))
  216. {
  217. newPass->SetPixelShader(passElem.GetAttribute("ps"));
  218. newPass->SetPixelShaderDefines(passElem.GetAttribute("psdefines"));
  219. }
  220. else
  221. {
  222. newPass->SetPixelShader(globalPS);
  223. newPass->SetPixelShaderDefines(globalPSDefines + passElem.GetAttribute("psdefines"));
  224. }
  225. if (passElem.HasAttribute("lighting"))
  226. {
  227. String lighting = passElem.GetAttributeLower("lighting");
  228. newPass->SetLightingMode((PassLightingMode)GetStringListIndex(lighting.CString(), lightingModeNames,
  229. LIGHTING_UNLIT));
  230. }
  231. if (passElem.HasAttribute("blend"))
  232. {
  233. String blend = passElem.GetAttributeLower("blend");
  234. newPass->SetBlendMode((BlendMode)GetStringListIndex(blend.CString(), blendModeNames, BLEND_REPLACE));
  235. }
  236. if (passElem.HasAttribute("depthtest"))
  237. {
  238. String depthTest = passElem.GetAttributeLower("depthtest");
  239. if (depthTest == "false")
  240. newPass->SetDepthTestMode(CMP_ALWAYS);
  241. else
  242. newPass->SetDepthTestMode((CompareMode)GetStringListIndex(depthTest.CString(), compareModeNames, CMP_LESS));
  243. }
  244. if (passElem.HasAttribute("depthwrite"))
  245. newPass->SetDepthWrite(passElem.GetBool("depthwrite"));
  246. if (passElem.HasAttribute("alphamask"))
  247. newPass->SetAlphaMask(passElem.GetBool("alphamask"));
  248. else
  249. newPass->SetAlphaMask(globalAlphaMask);
  250. }
  251. else
  252. LOGERROR("Missing pass name");
  253. passElem = passElem.GetNext("pass");
  254. }
  255. return true;
  256. }
  257. void Technique::SetIsSM3(bool enable)
  258. {
  259. isSM3_ = enable;
  260. }
  261. void Technique::SetIsDesktop(bool enable)
  262. {
  263. isDesktop_ = enable;
  264. }
  265. void Technique::ReleaseShaders()
  266. {
  267. PODVector<SharedPtr<Pass>*> allPasses = passes_.Values();
  268. for (unsigned i = 0; i < allPasses.Size(); ++i)
  269. allPasses[i]->Get()->ReleaseShaders();
  270. }
  271. Pass* Technique::CreatePass(StringHash type)
  272. {
  273. Pass* oldPass = GetPass(type);
  274. if (oldPass)
  275. return oldPass;
  276. SharedPtr<Pass> newPass(new Pass(type));
  277. passes_.Insert(type.Value(), newPass);
  278. // Calculate memory use now
  279. SetMemoryUse(sizeof(Technique) + ++numPasses_ * sizeof(Pass));
  280. return newPass;
  281. }
  282. void Technique::RemovePass(StringHash type)
  283. {
  284. if (passes_.Erase(type.Value()))
  285. SetMemoryUse(sizeof(Technique) + --numPasses_ * sizeof(Pass));
  286. }
  287. Vector<StringHash> Technique::GetPassTypes() const
  288. {
  289. // Convert PODVector<unsigned> to Vector<StringHash>
  290. PODVector<unsigned> vectorIn = passes_.Keys();
  291. Vector<StringHash> vectorOut;
  292. vectorOut.Reserve(vectorIn.Size());
  293. for (unsigned i = 0; i < vectorIn.Size(); ++i)
  294. vectorOut.Push(StringHash(vectorIn[i]));
  295. return vectorOut;
  296. }
  297. PODVector<Pass*> Technique::GetPasses() const
  298. {
  299. // Convert PODVector<SharedPtr<Pass>*> to PODVector<Pass*>
  300. PODVector<SharedPtr<Pass>*> vectorIn = passes_.Values();
  301. PODVector<Pass*> vectorOut;
  302. vectorOut.Reserve(vectorIn.Size());
  303. for (unsigned i = 0; i < vectorIn.Size(); ++i)
  304. vectorOut.Push(vectorIn[i]->Get());
  305. return vectorOut;
  306. }
  307. }