Technique.cpp 6.0 KB

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