Technique.cpp 6.3 KB

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