Technique.cpp 8.2 KB

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