Technique.cpp 5.8 KB

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