Technique.cpp 6.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259
  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. static const String blendModeNames[] =
  45. {
  46. "replace",
  47. "add",
  48. "multiply",
  49. "alpha",
  50. "addalpha",
  51. "premulalpha",
  52. "invdestalpha"
  53. };
  54. static const String CompareModeNames[] =
  55. {
  56. "always",
  57. "equal",
  58. "notequal",
  59. "less",
  60. "lessequal",
  61. "greater",
  62. "greaterequal"
  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->GetRootElement();
  135. if (rootElem.HasAttribute("sm3"))
  136. isSM3_ = rootElem.GetBool("sm3");
  137. XMLElement passElem = rootElem.GetChildElement("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, 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, MAX_BLENDMODES, 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, MAX_COMPAREMODES,
  173. CMP_LESSEQUAL));
  174. }
  175. if (passElem.HasAttribute("depthwrite"))
  176. newPass.SetDepthWrite(passElem.GetBool("depthwrite"));
  177. }
  178. passElem = passElem.GetNextElement("pass");
  179. }
  180. // Calculate memory use
  181. unsigned memoryUse = 0;
  182. memoryUse += sizeof(Technique);
  183. for (Map<PassType, Pass>::ConstIterator j = passes_.Begin(); j != passes_.End(); ++j)
  184. memoryUse += sizeof(Pass);
  185. SetMemoryUse(memoryUse);
  186. return true;
  187. }
  188. void Technique::SetIsSM3(bool enable)
  189. {
  190. isSM3_ = enable;
  191. }
  192. void Technique::ReleaseShaders()
  193. {
  194. for (Map<PassType, Pass>::Iterator i = passes_.Begin(); i != passes_.End(); ++i)
  195. i->second_.ReleaseShaders();
  196. }
  197. Pass* Technique::CreatePass(PassType pass)
  198. {
  199. Pass* existing = GetPass(pass);
  200. if (existing)
  201. return existing;
  202. Pass newPass(pass);
  203. passes_[pass] = newPass;
  204. return GetPass(pass);
  205. }
  206. void Technique::RemovePass(PassType pass)
  207. {
  208. passes_.Erase(pass);
  209. }
  210. void Technique::MarkShadersLoaded(unsigned frameNumber)
  211. {
  212. shadersLoadedFrameNumber_ = frameNumber;
  213. }
  214. const String& Technique::GetPassName(PassType pass)
  215. {
  216. return passNames[pass];
  217. }