Technique.cpp 8.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310
  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 "Profiler.h"
  28. #include "ResourceCache.h"
  29. #include "ShaderVariation.h"
  30. #include "XMLFile.h"
  31. #include "DebugNew.h"
  32. namespace Urho3D
  33. {
  34. const char* blendModeNames[] =
  35. {
  36. "replace",
  37. "add",
  38. "multiply",
  39. "alpha",
  40. "addalpha",
  41. "premulalpha",
  42. "invdestalpha",
  43. "subtract",
  44. "subtractalpha",
  45. 0
  46. };
  47. static const char* compareModeNames[] =
  48. {
  49. "always",
  50. "equal",
  51. "notequal",
  52. "less",
  53. "lessequal",
  54. "greater",
  55. "greaterequal",
  56. 0
  57. };
  58. static const char* lightingModeNames[] =
  59. {
  60. "unlit",
  61. "pervertex",
  62. "perpixel",
  63. 0
  64. };
  65. Pass::Pass(StringHash type) :
  66. type_(type),
  67. blendMode_(BLEND_REPLACE),
  68. depthTestMode_(CMP_LESSEQUAL),
  69. lightingMode_(LIGHTING_UNLIT),
  70. shadersLoadedFrameNumber_(0),
  71. depthWrite_(true),
  72. alphaMask_(false),
  73. isSM3_(false)
  74. {
  75. // Guess default lighting mode from pass name
  76. if (type == PASS_BASE || type == PASS_ALPHA || type == PASS_MATERIAL || type == PASS_DEFERRED)
  77. lightingMode_ = LIGHTING_PERVERTEX;
  78. else if (type == PASS_LIGHT || type == PASS_LITBASE || type == PASS_LITALPHA)
  79. lightingMode_ = LIGHTING_PERPIXEL;
  80. }
  81. Pass::~Pass()
  82. {
  83. }
  84. void Pass::SetBlendMode(BlendMode mode)
  85. {
  86. blendMode_ = mode;
  87. }
  88. void Pass::SetDepthTestMode(CompareMode mode)
  89. {
  90. depthTestMode_ = mode;
  91. }
  92. void Pass::SetLightingMode(PassLightingMode mode)
  93. {
  94. lightingMode_ = mode;
  95. }
  96. void Pass::SetDepthWrite(bool enable)
  97. {
  98. depthWrite_ = enable;
  99. }
  100. void Pass::SetAlphaMask(bool enable)
  101. {
  102. alphaMask_ = enable;
  103. }
  104. void Pass::SetIsSM3(bool enable)
  105. {
  106. isSM3_ = enable;
  107. }
  108. void Pass::SetVertexShader(const String& name)
  109. {
  110. vertexShaderName_ = name;
  111. ReleaseShaders();
  112. }
  113. void Pass::SetPixelShader(const String& name)
  114. {
  115. pixelShaderName_ = name;
  116. ReleaseShaders();
  117. }
  118. void Pass::SetVertexShaderDefines(const String& defines)
  119. {
  120. vertexShaderDefines_ = defines;
  121. ReleaseShaders();
  122. }
  123. void Pass::SetPixelShaderDefines(const String& defines)
  124. {
  125. pixelShaderDefines_ = defines;
  126. ReleaseShaders();
  127. }
  128. void Pass::ReleaseShaders()
  129. {
  130. vertexShaders_.Clear();
  131. pixelShaders_.Clear();
  132. }
  133. void Pass::MarkShadersLoaded(unsigned frameNumber)
  134. {
  135. shadersLoadedFrameNumber_ = frameNumber;
  136. }
  137. Technique::Technique(Context* context) :
  138. Resource(context),
  139. isSM3_(false)
  140. {
  141. Graphics* graphics = GetSubsystem<Graphics>();
  142. sm3Support_ = graphics ? graphics->GetSM3Support() : true;
  143. }
  144. Technique::~Technique()
  145. {
  146. }
  147. void Technique::RegisterObject(Context* context)
  148. {
  149. context->RegisterFactory<Technique>();
  150. }
  151. bool Technique::BeginLoad(Deserializer& source)
  152. {
  153. passes_.Clear();
  154. SetMemoryUse(sizeof(Technique));
  155. SharedPtr<XMLFile> xml(new XMLFile(context_));
  156. if (!xml->Load(source))
  157. return false;
  158. XMLElement rootElem = xml->GetRoot();
  159. if (rootElem.HasAttribute("sm3"))
  160. isSM3_ = rootElem.GetBool("sm3");
  161. String globalVS = rootElem.GetAttribute("vs");
  162. String globalPS = rootElem.GetAttribute("ps");
  163. String globalVSDefines = rootElem.GetAttribute("vsdefines");
  164. String globalPSDefines = rootElem.GetAttribute("psdefines");
  165. // End with space so that the pass-specific defines can be appended
  166. if (!globalVSDefines.Empty())
  167. globalVSDefines += ' ';
  168. if (!globalPSDefines.Empty())
  169. globalPSDefines += ' ';
  170. bool globalAlphaMask = false;
  171. if (rootElem.HasAttribute("alphamask"))
  172. globalAlphaMask = rootElem.GetBool("alphamask");
  173. unsigned numPasses = 0;
  174. XMLElement passElem = rootElem.GetChild("pass");
  175. while (passElem)
  176. {
  177. if (passElem.HasAttribute("name"))
  178. {
  179. StringHash nameHash(passElem.GetAttribute("name"));
  180. Pass* newPass = CreatePass(nameHash);
  181. ++numPasses;
  182. if (passElem.HasAttribute("sm3"))
  183. newPass->SetIsSM3(passElem.GetBool("sm3"));
  184. // Append global defines only when pass does not redefine the shader
  185. if (passElem.HasAttribute("vs"))
  186. {
  187. newPass->SetVertexShader(passElem.GetAttribute("vs"));
  188. newPass->SetVertexShaderDefines(passElem.GetAttribute("vsdefines"));
  189. }
  190. else
  191. {
  192. newPass->SetVertexShader(globalVS);
  193. newPass->SetVertexShaderDefines(globalVSDefines + passElem.GetAttribute("vsdefines"));
  194. }
  195. if (passElem.HasAttribute("ps"))
  196. {
  197. newPass->SetPixelShader(passElem.GetAttribute("ps"));
  198. newPass->SetPixelShaderDefines(passElem.GetAttribute("psdefines"));
  199. }
  200. else
  201. {
  202. newPass->SetPixelShader(globalPS);
  203. newPass->SetPixelShaderDefines(globalPSDefines + passElem.GetAttribute("psdefines"));
  204. }
  205. if (passElem.HasAttribute("lighting"))
  206. {
  207. String lighting = passElem.GetAttributeLower("lighting");
  208. newPass->SetLightingMode((PassLightingMode)GetStringListIndex(lighting.CString(), lightingModeNames,
  209. LIGHTING_UNLIT));
  210. }
  211. if (passElem.HasAttribute("blend"))
  212. {
  213. String blend = passElem.GetAttributeLower("blend");
  214. newPass->SetBlendMode((BlendMode)GetStringListIndex(blend.CString(), blendModeNames, BLEND_REPLACE));
  215. }
  216. if (passElem.HasAttribute("depthtest"))
  217. {
  218. String depthTest = passElem.GetAttributeLower("depthtest");
  219. if (depthTest == "false")
  220. newPass->SetDepthTestMode(CMP_ALWAYS);
  221. else
  222. newPass->SetDepthTestMode((CompareMode)GetStringListIndex(depthTest.CString(), compareModeNames, CMP_LESS));
  223. }
  224. if (passElem.HasAttribute("depthwrite"))
  225. newPass->SetDepthWrite(passElem.GetBool("depthwrite"));
  226. if (passElem.HasAttribute("alphamask"))
  227. newPass->SetAlphaMask(passElem.GetBool("alphamask"));
  228. else
  229. newPass->SetAlphaMask(globalAlphaMask);
  230. }
  231. else
  232. LOGERROR("Missing pass name");
  233. passElem = passElem.GetNext("pass");
  234. }
  235. // Calculate memory use now
  236. SetMemoryUse(sizeof(Technique) + numPasses * sizeof(Pass));
  237. return true;
  238. }
  239. void Technique::SetIsSM3(bool enable)
  240. {
  241. isSM3_ = enable;
  242. }
  243. void Technique::ReleaseShaders()
  244. {
  245. PODVector<SharedPtr<Pass>*> allPasses = passes_.Values();
  246. for (unsigned i = 0; i < allPasses.Size(); ++i)
  247. allPasses[i]->Get()->ReleaseShaders();
  248. }
  249. Pass* Technique::CreatePass(StringHash type)
  250. {
  251. /// \todo Memory use is not tracked when creating passes programmatically due to HashTable not returning the element count
  252. Pass* oldPass = GetPass(type);
  253. if (oldPass)
  254. return oldPass;
  255. SharedPtr<Pass> newPass(new Pass(type));
  256. passes_.Insert(type.Value(), newPass);
  257. return newPass;
  258. }
  259. void Technique::RemovePass(StringHash type)
  260. {
  261. passes_.Erase(type.Value());
  262. }
  263. }