Technique.cpp 6.3 KB

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