Technique.cpp 6.1 KB

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