Technique.cpp 6.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262
  1. //
  2. // Copyright (c) 2008-2013 the Urho3D project.
  3. //
  4. // Permission is hereby granted, free of charge, to any person obtaining a copy
  5. // of this software and associated documentation files (the "Software"), to deal
  6. // in the Software without restriction, including without limitation the rights
  7. // to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
  8. // copies of the Software, and to permit persons to whom the Software is
  9. // furnished to do so, subject to the following conditions:
  10. //
  11. // The above copyright notice and this permission notice shall be included in
  12. // all copies or substantial portions of the Software.
  13. //
  14. // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  15. // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  16. // FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  17. // AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  18. // LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
  19. // OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
  20. // THE SOFTWARE.
  21. //
  22. #include "Precompiled.h"
  23. #include "Context.h"
  24. #include "Log.h"
  25. #include "Technique.h"
  26. #include "Profiler.h"
  27. #include "ResourceCache.h"
  28. #include "ShaderVariation.h"
  29. #include "StringUtils.h"
  30. #include "XMLFile.h"
  31. #include "DebugNew.h"
  32. namespace Urho3D
  33. {
  34. static const String blendModeNames[] =
  35. {
  36. "replace",
  37. "add",
  38. "multiply",
  39. "alpha",
  40. "addalpha",
  41. "premulalpha",
  42. "invdestalpha",
  43. ""
  44. };
  45. static const String compareModeNames[] =
  46. {
  47. "always",
  48. "equal",
  49. "notequal",
  50. "less",
  51. "lessequal",
  52. "greater",
  53. "greaterequal",
  54. ""
  55. };
  56. static const String lightingModeNames[] =
  57. {
  58. "unlit",
  59. "pervertex",
  60. "perpixel",
  61. ""
  62. };
  63. Pass::Pass(StringHash type) :
  64. type_(type),
  65. blendMode_(BLEND_REPLACE),
  66. depthTestMode_(CMP_LESSEQUAL),
  67. lightingMode_(LIGHTING_UNLIT),
  68. depthWrite_(true),
  69. alphaMask_(false)
  70. {
  71. // Guess default lighting mode from pass name
  72. if (type == PASS_BASE || type == PASS_ALPHA || type == PASS_MATERIAL || type == PASS_DEFERRED)
  73. lightingMode_ = LIGHTING_PERVERTEX;
  74. if (type == PASS_LIGHT || type == PASS_LITBASE || type == PASS_LITALPHA)
  75. lightingMode_ = LIGHTING_PERPIXEL;
  76. }
  77. Pass::~Pass()
  78. {
  79. }
  80. void Pass::SetBlendMode(BlendMode mode)
  81. {
  82. blendMode_ = mode;
  83. }
  84. void Pass::SetDepthTestMode(CompareMode mode)
  85. {
  86. depthTestMode_ = mode;
  87. }
  88. void Pass::SetLightingMode(PassLightingMode mode)
  89. {
  90. lightingMode_ = mode;
  91. }
  92. void Pass::SetDepthWrite(bool enable)
  93. {
  94. depthWrite_ = enable;
  95. }
  96. void Pass::SetAlphaMask(bool enable)
  97. {
  98. alphaMask_ = enable;
  99. }
  100. void Pass::SetVertexShader(const String& name)
  101. {
  102. vertexShaderName_ = name;
  103. ReleaseShaders();
  104. }
  105. void Pass::SetPixelShader(const String& name)
  106. {
  107. pixelShaderName_ = name;
  108. ReleaseShaders();
  109. }
  110. void Pass::ReleaseShaders()
  111. {
  112. vertexShaders_.Clear();
  113. pixelShaders_.Clear();
  114. }
  115. OBJECTTYPESTATIC(Technique);
  116. Technique::Technique(Context* context) :
  117. Resource(context),
  118. isSM3_(false),
  119. shadersLoadedFrameNumber_(0)
  120. {
  121. }
  122. Technique::~Technique()
  123. {
  124. }
  125. void Technique::RegisterObject(Context* context)
  126. {
  127. context->RegisterFactory<Technique>();
  128. }
  129. bool Technique::Load(Deserializer& source)
  130. {
  131. PROFILE(LoadTechnique);
  132. SharedPtr<XMLFile> xml(new XMLFile(context_));
  133. if (!xml->Load(source))
  134. return false;
  135. XMLElement rootElem = xml->GetRoot();
  136. if (rootElem.HasAttribute("sm3"))
  137. isSM3_ = rootElem.GetBool("sm3");
  138. XMLElement passElem = rootElem.GetChild("pass");
  139. while (passElem)
  140. {
  141. if (passElem.HasAttribute("name"))
  142. {
  143. StringHash nameHash(passElem.GetAttribute("name"));
  144. Pass* newPass = CreatePass(nameHash);
  145. if (passElem.HasAttribute("vs"))
  146. newPass->SetVertexShader(passElem.GetAttribute("vs"));
  147. if (passElem.HasAttribute("ps"))
  148. newPass->SetPixelShader(passElem.GetAttribute("ps"));
  149. if (passElem.HasAttribute("lighting"))
  150. {
  151. String lighting = passElem.GetAttributeLower("lighting");
  152. newPass->SetLightingMode((PassLightingMode)GetStringListIndex(lighting, lightingModeNames, LIGHTING_UNLIT));
  153. }
  154. if (passElem.HasAttribute("blend"))
  155. {
  156. String blend = passElem.GetAttributeLower("blend");
  157. newPass->SetBlendMode((BlendMode)GetStringListIndex(blend, blendModeNames, BLEND_REPLACE));
  158. }
  159. if (passElem.HasAttribute("depthtest"))
  160. {
  161. String depthTest = passElem.GetAttributeLower("depthtest");
  162. if (depthTest == "false")
  163. newPass->SetDepthTestMode(CMP_ALWAYS);
  164. else
  165. newPass->SetDepthTestMode((CompareMode)GetStringListIndex(depthTest, compareModeNames, CMP_LESS));
  166. }
  167. if (passElem.HasAttribute("depthwrite"))
  168. newPass->SetDepthWrite(passElem.GetBool("depthwrite"));
  169. if (passElem.HasAttribute("alphamask"))
  170. newPass->SetAlphaMask(passElem.GetBool("alphamask"));
  171. }
  172. else
  173. LOGERROR("Missing pass name");
  174. passElem = passElem.GetNext("pass");
  175. }
  176. // Rehash the pass map to ensure minimum load factor and fast queries
  177. passes_.Rehash(NextPowerOfTwo(passes_.Size()));
  178. // Calculate memory use
  179. unsigned memoryUse = sizeof(Technique);
  180. memoryUse += sizeof(HashMap<StringHash, SharedPtr<Pass> >) + passes_.Size() * sizeof(Pass);
  181. SetMemoryUse(memoryUse);
  182. return true;
  183. }
  184. void Technique::SetIsSM3(bool enable)
  185. {
  186. isSM3_ = enable;
  187. }
  188. void Technique::ReleaseShaders()
  189. {
  190. for (HashMap<StringHash, SharedPtr<Pass> >::Iterator i = passes_.Begin(); i != passes_.End(); ++i)
  191. i->second_->ReleaseShaders();
  192. }
  193. Pass* Technique::CreatePass(StringHash type)
  194. {
  195. Pass* oldPass = GetPass(type);
  196. if (oldPass)
  197. return oldPass;
  198. SharedPtr<Pass> newPass(new Pass(type));
  199. passes_[type] = newPass;
  200. // Rehash the pass map to ensure minimum load factor and fast queries
  201. passes_.Rehash(NextPowerOfTwo(passes_.Size()));
  202. return newPass;
  203. }
  204. void Technique::RemovePass(StringHash type)
  205. {
  206. passes_.Erase(type);
  207. }
  208. Pass* Technique::GetPass(StringHash type) const
  209. {
  210. HashMap<StringHash, SharedPtr<Pass> >::ConstIterator i = passes_.Find(type);
  211. return i != passes_.End() ? i->second_ : (Pass*)0;
  212. }
  213. void Technique::MarkShadersLoaded(unsigned frameNumber)
  214. {
  215. shadersLoadedFrameNumber_ = frameNumber;
  216. }
  217. }