Technique.cpp 8.2 KB

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