Technique.cpp 6.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261
  1. //
  2. // Urho3D Engine
  3. // Copyright (c) 2008-2011 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 "PixelShader.h"
  28. #include "Profiler.h"
  29. #include "ResourceCache.h"
  30. #include "StringUtils.h"
  31. #include "VertexShader.h"
  32. #include "XMLFile.h"
  33. static const String passNames[] =
  34. {
  35. "deferred",
  36. "prepass",
  37. "material",
  38. "base",
  39. "litbase",
  40. "light",
  41. "extra",
  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()
  68. {
  69. }
  70. Pass::Pass(PassType type) :
  71. type_(type),
  72. alphaMask_(false),
  73. alphaTest_(false),
  74. blendMode_(BLEND_REPLACE),
  75. depthTestMode_(CMP_LESSEQUAL),
  76. depthWrite_(true)
  77. {
  78. }
  79. Pass::~Pass()
  80. {
  81. }
  82. void Pass::SetAlphaMask(bool enable)
  83. {
  84. alphaMask_ = enable;
  85. }
  86. void Pass::SetAlphaTest(bool enable)
  87. {
  88. alphaTest_ = enable;
  89. }
  90. void Pass::SetBlendMode(BlendMode mode)
  91. {
  92. blendMode_ = mode;
  93. }
  94. void Pass::SetDepthTestMode(CompareMode mode)
  95. {
  96. depthTestMode_ = mode;
  97. }
  98. void Pass::SetDepthWrite(bool enable)
  99. {
  100. depthWrite_ = enable;
  101. }
  102. void Pass::SetVertexShader(const String& name)
  103. {
  104. vertexShaderName_ = name;
  105. ReleaseShaders();
  106. }
  107. void Pass::SetPixelShader(const String& name)
  108. {
  109. pixelShaderName_ = name;
  110. ReleaseShaders();
  111. }
  112. void Pass::ReleaseShaders()
  113. {
  114. vertexShaders_.Clear();
  115. pixelShaders_.Clear();
  116. }
  117. OBJECTTYPESTATIC(Technique);
  118. Technique::Technique(Context* context) :
  119. Resource(context),
  120. isSM3_(false),
  121. shadersLoadedFrameNumber_(0)
  122. {
  123. }
  124. Technique::~Technique()
  125. {
  126. }
  127. void Technique::RegisterObject(Context* context)
  128. {
  129. context->RegisterFactory<Technique>();
  130. }
  131. bool Technique::Load(Deserializer& source)
  132. {
  133. PROFILE(LoadTechnique);
  134. SharedPtr<XMLFile> xml(new XMLFile(context_));
  135. if (!xml->Load(source))
  136. return false;
  137. XMLElement rootElem = xml->GetRootElement();
  138. if (rootElem.HasAttribute("sm3"))
  139. isSM3_ = rootElem.GetBool("sm3");
  140. XMLElement passElem = rootElem.GetChildElement("pass");
  141. while (passElem)
  142. {
  143. PassType type = MAX_PASSES;
  144. if (passElem.HasAttribute("name"))
  145. {
  146. String name = passElem.GetStringLower("name");
  147. type = (PassType)GetStringListIndex(name, passNames, MAX_PASSES);
  148. if (type == MAX_PASSES)
  149. LOGERROR("Unknown pass " + name);
  150. }
  151. else
  152. LOGERROR("Missing pass name");
  153. if (type != MAX_PASSES)
  154. {
  155. Pass& newPass = *CreatePass(type);
  156. if (passElem.HasAttribute("vs"))
  157. newPass.SetVertexShader(passElem.GetString("vs"));
  158. if (passElem.HasAttribute("ps"))
  159. newPass.SetPixelShader(passElem.GetString("ps"));
  160. if (passElem.HasAttribute("alphamask"))
  161. newPass.SetAlphaMask(passElem.GetBool("alphamask"));
  162. if (passElem.HasAttribute("alphatest"))
  163. newPass.SetAlphaTest(passElem.GetBool("alphatest"));
  164. if (passElem.HasAttribute("blend"))
  165. {
  166. String blend = passElem.GetStringLower("blend");
  167. newPass.SetBlendMode((BlendMode)GetStringListIndex(blend, blendModeNames, BLEND_REPLACE));
  168. }
  169. if (passElem.HasAttribute("depthtest"))
  170. {
  171. String depthTest = passElem.GetStringLower("depthtest");
  172. if (depthTest == "false")
  173. newPass.SetDepthTestMode(CMP_ALWAYS);
  174. else
  175. newPass.SetDepthTestMode((CompareMode)GetStringListIndex(depthTest, CompareModeNames, CMP_LESSEQUAL));
  176. }
  177. if (passElem.HasAttribute("depthwrite"))
  178. newPass.SetDepthWrite(passElem.GetBool("depthwrite"));
  179. }
  180. passElem = passElem.GetNextElement("pass");
  181. }
  182. // Calculate memory use
  183. unsigned memoryUse = 0;
  184. memoryUse += sizeof(Technique);
  185. for (Map<PassType, Pass>::ConstIterator j = passes_.Begin(); j != passes_.End(); ++j)
  186. memoryUse += sizeof(Pass);
  187. SetMemoryUse(memoryUse);
  188. return true;
  189. }
  190. void Technique::SetIsSM3(bool enable)
  191. {
  192. isSM3_ = enable;
  193. }
  194. void Technique::ReleaseShaders()
  195. {
  196. for (Map<PassType, Pass>::Iterator i = passes_.Begin(); i != passes_.End(); ++i)
  197. i->second_.ReleaseShaders();
  198. }
  199. Pass* Technique::CreatePass(PassType pass)
  200. {
  201. Pass* existing = GetPass(pass);
  202. if (existing)
  203. return existing;
  204. Pass newPass(pass);
  205. passes_[pass] = newPass;
  206. return GetPass(pass);
  207. }
  208. void Technique::RemovePass(PassType pass)
  209. {
  210. passes_.Erase(pass);
  211. }
  212. void Technique::MarkShadersLoaded(unsigned frameNumber)
  213. {
  214. shadersLoadedFrameNumber_ = frameNumber;
  215. }
  216. const String& Technique::GetPassName(PassType pass)
  217. {
  218. return passNames[pass];
  219. }