Technique.cpp 6.0 KB

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