Technique.cpp 6.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251
  1. //
  2. // Urho3D Engine
  3. // Copyright (c) 2008-2012 Lasse Öörni
  4. //
  5. // Permission is hereby granted, free of charge, to any person obtaining a copy
  6. // of this software and associated documentation files (the "Software"), to deal
  7. // in the Software without restriction, including without limitation the rights
  8. // to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
  9. // copies of the Software, and to permit persons to whom the Software is
  10. // furnished to do so, subject to the following conditions:
  11. //
  12. // The above copyright notice and this permission notice shall be included in
  13. // all copies or substantial portions of the Software.
  14. //
  15. // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  16. // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  17. // FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  18. // AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  19. // LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
  20. // OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
  21. // THE SOFTWARE.
  22. //
  23. #include "Precompiled.h"
  24. #include "Context.h"
  25. #include "Log.h"
  26. #include "Technique.h"
  27. #include "Profiler.h"
  28. #include "ResourceCache.h"
  29. #include "ShaderVariation.h"
  30. #include "StringUtils.h"
  31. #include "XMLFile.h"
  32. #include "DebugNew.h"
  33. static const String passNames[] =
  34. {
  35. "base",
  36. "litbase",
  37. "light",
  38. "prealpha",
  39. "postalpha",
  40. "prepass",
  41. "material",
  42. "deferred",
  43. "shadow",
  44. ""
  45. };
  46. static const String blendModeNames[] =
  47. {
  48. "replace",
  49. "add",
  50. "multiply",
  51. "alpha",
  52. "addalpha",
  53. "premulalpha",
  54. "invdestalpha",
  55. ""
  56. };
  57. static const String CompareModeNames[] =
  58. {
  59. "always",
  60. "equal",
  61. "notequal",
  62. "less",
  63. "lessequal",
  64. "greater",
  65. "greaterequal",
  66. ""
  67. };
  68. Pass::Pass(PassType type) :
  69. type_(type),
  70. alphaTest_(false),
  71. blendMode_(BLEND_REPLACE),
  72. depthTestMode_(CMP_LESSEQUAL),
  73. depthWrite_(true)
  74. {
  75. }
  76. Pass::~Pass()
  77. {
  78. }
  79. void Pass::SetAlphaTest(bool enable)
  80. {
  81. alphaTest_ = enable;
  82. }
  83. void Pass::SetBlendMode(BlendMode mode)
  84. {
  85. blendMode_ = mode;
  86. }
  87. void Pass::SetDepthTestMode(CompareMode mode)
  88. {
  89. depthTestMode_ = mode;
  90. }
  91. void Pass::SetDepthWrite(bool enable)
  92. {
  93. depthWrite_ = enable;
  94. }
  95. void Pass::SetVertexShader(const String& name)
  96. {
  97. vertexShaderName_ = name;
  98. ReleaseShaders();
  99. }
  100. void Pass::SetPixelShader(const String& name)
  101. {
  102. pixelShaderName_ = name;
  103. ReleaseShaders();
  104. }
  105. void Pass::ReleaseShaders()
  106. {
  107. vertexShaders_.Clear();
  108. pixelShaders_.Clear();
  109. }
  110. OBJECTTYPESTATIC(Technique);
  111. Technique::Technique(Context* context) :
  112. Resource(context),
  113. isSM3_(false),
  114. shadersLoadedFrameNumber_(0)
  115. {
  116. }
  117. Technique::~Technique()
  118. {
  119. }
  120. void Technique::RegisterObject(Context* context)
  121. {
  122. context->RegisterFactory<Technique>();
  123. }
  124. bool Technique::Load(Deserializer& source)
  125. {
  126. PROFILE(LoadTechnique);
  127. SharedPtr<XMLFile> xml(new XMLFile(context_));
  128. if (!xml->Load(source))
  129. return false;
  130. XMLElement rootElem = xml->GetRoot();
  131. if (rootElem.HasAttribute("sm3"))
  132. isSM3_ = rootElem.GetBool("sm3");
  133. XMLElement passElem = rootElem.GetChild("pass");
  134. while (passElem)
  135. {
  136. PassType type = MAX_PASSES;
  137. if (passElem.HasAttribute("name"))
  138. {
  139. String name = passElem.GetAttributeLower("name");
  140. type = (PassType)GetStringListIndex(name, passNames, MAX_PASSES);
  141. if (type == MAX_PASSES)
  142. LOGERROR("Unknown pass " + name);
  143. }
  144. else
  145. LOGERROR("Missing pass name");
  146. if (type != MAX_PASSES)
  147. {
  148. Pass* newPass = CreatePass(type);
  149. if (passElem.HasAttribute("vs"))
  150. newPass->SetVertexShader(passElem.GetAttribute("vs"));
  151. if (passElem.HasAttribute("ps"))
  152. newPass->SetPixelShader(passElem.GetAttribute("ps"));
  153. if (passElem.HasAttribute("alphatest"))
  154. newPass->SetAlphaTest(passElem.GetBool("alphatest"));
  155. if (passElem.HasAttribute("blend"))
  156. {
  157. String blend = passElem.GetAttributeLower("blend");
  158. newPass->SetBlendMode((BlendMode)GetStringListIndex(blend, blendModeNames, BLEND_REPLACE));
  159. }
  160. if (passElem.HasAttribute("depthtest"))
  161. {
  162. String depthTest = passElem.GetAttributeLower("depthtest");
  163. if (depthTest == "false")
  164. newPass->SetDepthTestMode(CMP_ALWAYS);
  165. else
  166. newPass->SetDepthTestMode((CompareMode)GetStringListIndex(depthTest, CompareModeNames, CMP_LESS));
  167. }
  168. if (passElem.HasAttribute("depthwrite"))
  169. newPass->SetDepthWrite(passElem.GetBool("depthwrite"));
  170. }
  171. passElem = passElem.GetNext("pass");
  172. }
  173. // Calculate memory use
  174. unsigned memoryUse = sizeof(Technique);
  175. for (unsigned i = 0; i < MAX_PASSES; ++i)
  176. {
  177. if (passes_[i])
  178. memoryUse += sizeof(Pass);
  179. }
  180. SetMemoryUse(memoryUse);
  181. return true;
  182. }
  183. void Technique::SetIsSM3(bool enable)
  184. {
  185. isSM3_ = enable;
  186. }
  187. void Technique::ReleaseShaders()
  188. {
  189. for (unsigned i = 0; i < MAX_PASSES; ++i)
  190. {
  191. if (passes_[i])
  192. passes_[i]->ReleaseShaders();
  193. }
  194. }
  195. Pass* Technique::CreatePass(PassType pass)
  196. {
  197. if (!passes_[pass])
  198. passes_[pass] = new Pass(pass);
  199. return passes_[pass];
  200. }
  201. void Technique::RemovePass(PassType pass)
  202. {
  203. passes_[pass].Reset();
  204. }
  205. void Technique::MarkShadersLoaded(unsigned frameNumber)
  206. {
  207. shadersLoadedFrameNumber_ = frameNumber;
  208. }
  209. const String& Technique::GetPassName(PassType pass)
  210. {
  211. return passNames[pass];
  212. }