Technique.cpp 6.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252
  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 "StringUtils.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. 0
  44. };
  45. static const char* compareModeNames[] =
  46. {
  47. "always",
  48. "equal",
  49. "notequal",
  50. "less",
  51. "lessequal",
  52. "greater",
  53. "greaterequal",
  54. 0
  55. };
  56. static const char* lightingModeNames[] =
  57. {
  58. "unlit",
  59. "pervertex",
  60. "perpixel",
  61. 0
  62. };
  63. Pass::Pass(StringHash type) :
  64. type_(type),
  65. blendMode_(BLEND_REPLACE),
  66. depthTestMode_(CMP_LESSEQUAL),
  67. lightingMode_(LIGHTING_UNLIT),
  68. shadersLoadedFrameNumber_(0),
  69. depthWrite_(true),
  70. alphaMask_(false)
  71. {
  72. // Guess default lighting mode from pass name
  73. if (type == PASS_BASE || type == PASS_ALPHA || type == PASS_MATERIAL || type == PASS_DEFERRED)
  74. lightingMode_ = LIGHTING_PERVERTEX;
  75. else if (type == PASS_LIGHT || type == PASS_LITBASE || type == PASS_LITALPHA)
  76. lightingMode_ = LIGHTING_PERPIXEL;
  77. }
  78. Pass::~Pass()
  79. {
  80. }
  81. void Pass::SetBlendMode(BlendMode mode)
  82. {
  83. blendMode_ = mode;
  84. }
  85. void Pass::SetDepthTestMode(CompareMode mode)
  86. {
  87. depthTestMode_ = mode;
  88. }
  89. void Pass::SetLightingMode(PassLightingMode mode)
  90. {
  91. lightingMode_ = mode;
  92. }
  93. void Pass::SetDepthWrite(bool enable)
  94. {
  95. depthWrite_ = enable;
  96. }
  97. void Pass::SetAlphaMask(bool enable)
  98. {
  99. alphaMask_ = enable;
  100. }
  101. void Pass::SetVertexShader(const String& name)
  102. {
  103. vertexShaderName_ = name;
  104. ReleaseShaders();
  105. }
  106. void Pass::SetPixelShader(const String& name)
  107. {
  108. pixelShaderName_ = name;
  109. ReleaseShaders();
  110. }
  111. void Pass::ReleaseShaders()
  112. {
  113. vertexShaders_.Clear();
  114. pixelShaders_.Clear();
  115. }
  116. void Pass::MarkShadersLoaded(unsigned frameNumber)
  117. {
  118. shadersLoadedFrameNumber_ = frameNumber;
  119. }
  120. Technique::Technique(Context* context) :
  121. Resource(context),
  122. isSM3_(false)
  123. {
  124. }
  125. Technique::~Technique()
  126. {
  127. }
  128. void Technique::RegisterObject(Context* context)
  129. {
  130. context->RegisterFactory<Technique>();
  131. }
  132. bool Technique::Load(Deserializer& source)
  133. {
  134. PROFILE(LoadTechnique);
  135. SharedPtr<XMLFile> xml(new XMLFile(context_));
  136. if (!xml->Load(source))
  137. return false;
  138. XMLElement rootElem = xml->GetRoot();
  139. if (rootElem.HasAttribute("sm3"))
  140. isSM3_ = rootElem.GetBool("sm3");
  141. unsigned numPasses = 0;
  142. XMLElement passElem = rootElem.GetChild("pass");
  143. while (passElem)
  144. {
  145. if (passElem.HasAttribute("name"))
  146. {
  147. StringHash nameHash(passElem.GetAttribute("name"));
  148. Pass* newPass = CreatePass(nameHash);
  149. ++numPasses;
  150. if (passElem.HasAttribute("vs"))
  151. newPass->SetVertexShader(passElem.GetAttribute("vs"));
  152. if (passElem.HasAttribute("ps"))
  153. newPass->SetPixelShader(passElem.GetAttribute("ps"));
  154. if (passElem.HasAttribute("lighting"))
  155. {
  156. String lighting = passElem.GetAttributeLower("lighting");
  157. newPass->SetLightingMode((PassLightingMode)GetStringListIndex(lighting.CString(), lightingModeNames,
  158. LIGHTING_UNLIT));
  159. }
  160. if (passElem.HasAttribute("blend"))
  161. {
  162. String blend = passElem.GetAttributeLower("blend");
  163. newPass->SetBlendMode((BlendMode)GetStringListIndex(blend.CString(), blendModeNames, BLEND_REPLACE));
  164. }
  165. if (passElem.HasAttribute("depthtest"))
  166. {
  167. String depthTest = passElem.GetAttributeLower("depthtest");
  168. if (depthTest == "false")
  169. newPass->SetDepthTestMode(CMP_ALWAYS);
  170. else
  171. newPass->SetDepthTestMode((CompareMode)GetStringListIndex(depthTest.CString(), compareModeNames, CMP_LESS));
  172. }
  173. if (passElem.HasAttribute("depthwrite"))
  174. newPass->SetDepthWrite(passElem.GetBool("depthwrite"));
  175. if (passElem.HasAttribute("alphamask"))
  176. newPass->SetAlphaMask(passElem.GetBool("alphamask"));
  177. }
  178. else
  179. LOGERROR("Missing pass name");
  180. passElem = passElem.GetNext("pass");
  181. }
  182. // Calculate memory use
  183. unsigned memoryUse = sizeof(Technique) + numPasses * sizeof(Pass);
  184. SetMemoryUse(memoryUse);
  185. return true;
  186. }
  187. void Technique::SetIsSM3(bool enable)
  188. {
  189. isSM3_ = enable;
  190. }
  191. void Technique::ReleaseShaders()
  192. {
  193. PODVector<SharedPtr<Pass>*> allPasses = passes_.Values();
  194. for (unsigned i = 0; i < allPasses.Size(); ++i)
  195. allPasses[i]->Get()->ReleaseShaders();
  196. }
  197. Pass* Technique::CreatePass(StringHash type)
  198. {
  199. Pass* oldPass = GetPass(type);
  200. if (oldPass)
  201. return oldPass;
  202. SharedPtr<Pass> newPass(new Pass(type));
  203. passes_.Insert(type.Value(), newPass);
  204. return newPass;
  205. }
  206. void Technique::RemovePass(StringHash type)
  207. {
  208. passes_.Erase(type.Value());
  209. }
  210. }