Technique.cpp 8.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294
  1. //
  2. // Copyright (c) 2008-2013 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. SharedPtr<XMLFile> xml(new XMLFile(context_));
  145. if (!xml->Load(source))
  146. return false;
  147. XMLElement rootElem = xml->GetRoot();
  148. if (rootElem.HasAttribute("sm3"))
  149. isSM3_ = rootElem.GetBool("sm3");
  150. String globalVS = rootElem.GetAttribute("vs");
  151. String globalPS = rootElem.GetAttribute("ps");
  152. String globalVSDefines = rootElem.GetAttribute("vsdefines");
  153. String globalPSDefines = rootElem.GetAttribute("psdefines");
  154. // End with space so that the pass-specific defines can be appended
  155. if (!globalVSDefines.Empty())
  156. globalVSDefines += ' ';
  157. if (!globalPSDefines.Empty())
  158. globalPSDefines += ' ';
  159. bool globalAlphaMask = false;
  160. if (rootElem.HasAttribute("alphamask"))
  161. globalAlphaMask = rootElem.GetBool("alphamask");
  162. unsigned numPasses = 0;
  163. XMLElement passElem = rootElem.GetChild("pass");
  164. while (passElem)
  165. {
  166. if (passElem.HasAttribute("name"))
  167. {
  168. StringHash nameHash(passElem.GetAttribute("name"));
  169. Pass* newPass = CreatePass(nameHash);
  170. ++numPasses;
  171. // Append global defines only when pass does not redefine the shader
  172. if (passElem.HasAttribute("vs"))
  173. {
  174. newPass->SetVertexShader(passElem.GetAttribute("vs"));
  175. newPass->SetVertexShaderDefines(passElem.GetAttribute("vsdefines"));
  176. }
  177. else
  178. {
  179. newPass->SetVertexShader(globalVS);
  180. newPass->SetVertexShaderDefines(globalVSDefines + passElem.GetAttribute("vsdefines"));
  181. }
  182. if (passElem.HasAttribute("ps"))
  183. {
  184. newPass->SetPixelShader(passElem.GetAttribute("ps"));
  185. newPass->SetPixelShaderDefines(passElem.GetAttribute("psdefines"));
  186. }
  187. else
  188. {
  189. newPass->SetPixelShader(globalPS);
  190. newPass->SetPixelShaderDefines(globalPSDefines + passElem.GetAttribute("psdefines"));
  191. }
  192. if (passElem.HasAttribute("lighting"))
  193. {
  194. String lighting = passElem.GetAttributeLower("lighting");
  195. newPass->SetLightingMode((PassLightingMode)GetStringListIndex(lighting.CString(), lightingModeNames,
  196. LIGHTING_UNLIT));
  197. }
  198. if (passElem.HasAttribute("blend"))
  199. {
  200. String blend = passElem.GetAttributeLower("blend");
  201. newPass->SetBlendMode((BlendMode)GetStringListIndex(blend.CString(), blendModeNames, BLEND_REPLACE));
  202. }
  203. if (passElem.HasAttribute("depthtest"))
  204. {
  205. String depthTest = passElem.GetAttributeLower("depthtest");
  206. if (depthTest == "false")
  207. newPass->SetDepthTestMode(CMP_ALWAYS);
  208. else
  209. newPass->SetDepthTestMode((CompareMode)GetStringListIndex(depthTest.CString(), compareModeNames, CMP_LESS));
  210. }
  211. if (passElem.HasAttribute("depthwrite"))
  212. newPass->SetDepthWrite(passElem.GetBool("depthwrite"));
  213. if (passElem.HasAttribute("alphamask"))
  214. newPass->SetAlphaMask(passElem.GetBool("alphamask"));
  215. else
  216. newPass->SetAlphaMask(globalAlphaMask);
  217. }
  218. else
  219. LOGERROR("Missing pass name");
  220. passElem = passElem.GetNext("pass");
  221. }
  222. // Calculate memory use
  223. unsigned memoryUse = sizeof(Technique) + numPasses * sizeof(Pass);
  224. SetMemoryUse(memoryUse);
  225. return true;
  226. }
  227. void Technique::SetIsSM3(bool enable)
  228. {
  229. isSM3_ = enable;
  230. }
  231. void Technique::ReleaseShaders()
  232. {
  233. PODVector<SharedPtr<Pass>*> allPasses = passes_.Values();
  234. for (unsigned i = 0; i < allPasses.Size(); ++i)
  235. allPasses[i]->Get()->ReleaseShaders();
  236. }
  237. Pass* Technique::CreatePass(StringHash type)
  238. {
  239. Pass* oldPass = GetPass(type);
  240. if (oldPass)
  241. return oldPass;
  242. SharedPtr<Pass> newPass(new Pass(type));
  243. passes_.Insert(type.Value(), newPass);
  244. return newPass;
  245. }
  246. void Technique::RemovePass(StringHash type)
  247. {
  248. passes_.Erase(type.Value());
  249. }
  250. }