Technique.cpp 6.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249
  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. static const String passNames[] =
  33. {
  34. "base",
  35. "litbase",
  36. "light",
  37. "prealpha",
  38. "postalpha",
  39. "prepass",
  40. "material",
  41. "deferred",
  42. "shadow",
  43. ""
  44. };
  45. static const String blendModeNames[] =
  46. {
  47. "replace",
  48. "add",
  49. "multiply",
  50. "alpha",
  51. "addalpha",
  52. "premulalpha",
  53. "invdestalpha",
  54. ""
  55. };
  56. static const String CompareModeNames[] =
  57. {
  58. "always",
  59. "equal",
  60. "notequal",
  61. "less",
  62. "lessequal",
  63. "greater",
  64. "greaterequal",
  65. ""
  66. };
  67. Pass::Pass(PassType type) :
  68. type_(type),
  69. alphaTest_(false),
  70. blendMode_(BLEND_REPLACE),
  71. depthTestMode_(CMP_LESSEQUAL),
  72. depthWrite_(true)
  73. {
  74. }
  75. Pass::~Pass()
  76. {
  77. }
  78. void Pass::SetAlphaTest(bool enable)
  79. {
  80. alphaTest_ = enable;
  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::SetDepthWrite(bool enable)
  91. {
  92. depthWrite_ = enable;
  93. }
  94. void Pass::SetVertexShader(const String& name)
  95. {
  96. vertexShaderName_ = name;
  97. ReleaseShaders();
  98. }
  99. void Pass::SetPixelShader(const String& name)
  100. {
  101. pixelShaderName_ = name;
  102. ReleaseShaders();
  103. }
  104. void Pass::ReleaseShaders()
  105. {
  106. vertexShaders_.Clear();
  107. pixelShaders_.Clear();
  108. }
  109. OBJECTTYPESTATIC(Technique);
  110. Technique::Technique(Context* context) :
  111. Resource(context),
  112. isSM3_(false),
  113. shadersLoadedFrameNumber_(0)
  114. {
  115. }
  116. Technique::~Technique()
  117. {
  118. }
  119. void Technique::RegisterObject(Context* context)
  120. {
  121. context->RegisterFactory<Technique>();
  122. }
  123. bool Technique::Load(Deserializer& source)
  124. {
  125. PROFILE(LoadTechnique);
  126. SharedPtr<XMLFile> xml(new XMLFile(context_));
  127. if (!xml->Load(source))
  128. return false;
  129. XMLElement rootElem = xml->GetRoot();
  130. if (rootElem.HasAttribute("sm3"))
  131. isSM3_ = rootElem.GetBool("sm3");
  132. XMLElement passElem = rootElem.GetChild("pass");
  133. while (passElem)
  134. {
  135. PassType type = MAX_PASSES;
  136. if (passElem.HasAttribute("name"))
  137. {
  138. String name = passElem.GetAttributeLower("name");
  139. type = (PassType)GetStringListIndex(name, passNames, MAX_PASSES);
  140. if (type == MAX_PASSES)
  141. LOGERROR("Unknown pass " + name);
  142. }
  143. else
  144. LOGERROR("Missing pass name");
  145. if (type != MAX_PASSES)
  146. {
  147. Pass* newPass = CreatePass(type);
  148. if (passElem.HasAttribute("vs"))
  149. newPass->SetVertexShader(passElem.GetAttribute("vs"));
  150. if (passElem.HasAttribute("ps"))
  151. newPass->SetPixelShader(passElem.GetAttribute("ps"));
  152. if (passElem.HasAttribute("alphatest"))
  153. newPass->SetAlphaTest(passElem.GetBool("alphatest"));
  154. if (passElem.HasAttribute("blend"))
  155. {
  156. String blend = passElem.GetAttributeLower("blend");
  157. newPass->SetBlendMode((BlendMode)GetStringListIndex(blend, blendModeNames, BLEND_REPLACE));
  158. }
  159. if (passElem.HasAttribute("depthtest"))
  160. {
  161. String depthTest = passElem.GetAttributeLower("depthtest");
  162. if (depthTest == "false")
  163. newPass->SetDepthTestMode(CMP_ALWAYS);
  164. else
  165. newPass->SetDepthTestMode((CompareMode)GetStringListIndex(depthTest, CompareModeNames, CMP_LESS));
  166. }
  167. if (passElem.HasAttribute("depthwrite"))
  168. newPass->SetDepthWrite(passElem.GetBool("depthwrite"));
  169. }
  170. passElem = passElem.GetNext("pass");
  171. }
  172. // Calculate memory use
  173. unsigned memoryUse = sizeof(Technique);
  174. for (unsigned i = 0; i < MAX_PASSES; ++i)
  175. {
  176. if (passes_[i])
  177. memoryUse += sizeof(Pass);
  178. }
  179. SetMemoryUse(memoryUse);
  180. return true;
  181. }
  182. void Technique::SetIsSM3(bool enable)
  183. {
  184. isSM3_ = enable;
  185. }
  186. void Technique::ReleaseShaders()
  187. {
  188. for (unsigned i = 0; i < MAX_PASSES; ++i)
  189. {
  190. if (passes_[i])
  191. passes_[i]->ReleaseShaders();
  192. }
  193. }
  194. Pass* Technique::CreatePass(PassType pass)
  195. {
  196. if (!passes_[pass])
  197. passes_[pass] = new Pass(pass);
  198. return passes_[pass];
  199. }
  200. void Technique::RemovePass(PassType pass)
  201. {
  202. passes_[pass].Reset();
  203. }
  204. void Technique::MarkShadersLoaded(unsigned frameNumber)
  205. {
  206. shadersLoadedFrameNumber_ = frameNumber;
  207. }
  208. const String& Technique::GetPassName(PassType pass)
  209. {
  210. return passNames[pass];
  211. }