PostProcess.cpp 9.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304
  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 "Graphics.h"
  25. #include "Log.h"
  26. #include "PostProcess.h"
  27. #include "StringUtils.h"
  28. #include "Texture2D.h"
  29. #include "XMLFile.h"
  30. #include "DebugNew.h"
  31. static const String emptyName;
  32. TextureUnit ParseTextureUnitName(const String& name);
  33. PostProcessPass::PostProcessPass()
  34. {
  35. }
  36. PostProcessPass::~PostProcessPass()
  37. {
  38. }
  39. void PostProcessPass::SetVertexShader(const String& name)
  40. {
  41. vertexShaderName_ = name;
  42. }
  43. void PostProcessPass::SetPixelShader(const String& name)
  44. {
  45. pixelShaderName_ = name;
  46. }
  47. void PostProcessPass::SetTexture(TextureUnit unit, const String& name)
  48. {
  49. if (unit < MAX_MATERIAL_TEXTURE_UNITS)
  50. textureNames_[unit] = name;
  51. }
  52. void PostProcessPass::SetShaderParameter(const String& name, const Vector4& value)
  53. {
  54. shaderParameters_[StringHash(name)] = value;
  55. }
  56. void PostProcessPass::RemoveShaderParameter(const String& name)
  57. {
  58. shaderParameters_.Erase(StringHash(name));
  59. }
  60. void PostProcessPass::SetOutput(const String& name)
  61. {
  62. outputName_ = name;
  63. }
  64. SharedPtr<PostProcessPass> PostProcessPass::Clone()
  65. {
  66. SharedPtr<PostProcessPass> clone(new PostProcessPass());
  67. clone->vertexShaderName_ = vertexShaderName_;
  68. clone->pixelShaderName_ = pixelShaderName_;
  69. clone->shaderParameters_ = shaderParameters_;
  70. for (unsigned i = 0; i < MAX_MATERIAL_TEXTURE_UNITS; ++i)
  71. clone->textureNames_[i] = textureNames_[i];
  72. clone->outputName_ = outputName_;
  73. return clone;
  74. }
  75. const String& PostProcessPass::GetTexture(TextureUnit unit) const
  76. {
  77. return unit < MAX_MATERIAL_TEXTURE_UNITS ? textureNames_[unit] : emptyName;
  78. }
  79. const Vector4& PostProcessPass::GetShaderParameter(const String& name) const
  80. {
  81. HashMap<StringHash, Vector4>::ConstIterator i = shaderParameters_.Find(StringHash(name));
  82. return i != shaderParameters_.End() ? i->second_ : Vector4::ZERO;
  83. }
  84. OBJECTTYPESTATIC(PostProcess);
  85. PostProcess::PostProcess(Context* context) :
  86. Object(context),
  87. active_(true)
  88. {
  89. }
  90. PostProcess::~PostProcess()
  91. {
  92. }
  93. bool PostProcess::LoadParameters(XMLFile* file)
  94. {
  95. if (!file)
  96. return false;
  97. XMLElement rootElem = file->GetRoot();
  98. if (!rootElem)
  99. return false;
  100. parameterSource_ = file;
  101. renderTargets_.Clear();
  102. shaderParameters_.Clear();
  103. passes_.Clear();
  104. XMLElement rtElem = rootElem.GetChild("rendertarget");
  105. while (rtElem)
  106. {
  107. String name = rtElem.GetAttribute("name");
  108. unsigned format = Graphics::GetRGBFormat();
  109. String formatName = rtElem.GetAttributeLower("format");
  110. if (formatName == "rgba")
  111. format = Graphics::GetRGBAFormat();
  112. else if (formatName == "float")
  113. format = Graphics::GetFloatFormat();
  114. bool sizeDivisor = false;
  115. bool filtered = false;
  116. unsigned width = 0;
  117. unsigned height = 0;
  118. if (rtElem.HasAttribute("filter"))
  119. filtered = rtElem.GetBool("filter");
  120. if (rtElem.HasAttribute("size"))
  121. {
  122. IntVector2 size = rtElem.GetIntVector2("size");
  123. width = size.x_;
  124. height = size.y_;
  125. }
  126. if (rtElem.HasAttribute("width"))
  127. width = rtElem.GetInt("width");
  128. if (rtElem.HasAttribute("height"))
  129. height = rtElem.GetInt("height");
  130. if (rtElem.HasAttribute("sizedivisor"))
  131. {
  132. IntVector2 size = rtElem.GetIntVector2("sizedivisor");
  133. width = size.x_;
  134. height = size.y_;
  135. sizeDivisor = true;
  136. }
  137. CreateRenderTarget(name, width, height, format, sizeDivisor, filtered);
  138. rtElem = rtElem.GetNext("rendertarget");
  139. }
  140. XMLElement parameterElem = rootElem.GetChild("parameter");
  141. while (parameterElem)
  142. {
  143. String name = parameterElem.GetAttribute("name");
  144. Vector4 value = parameterElem.GetVector("value");
  145. SetShaderParameter(name, value);
  146. parameterElem = parameterElem.GetNext("parameter");
  147. }
  148. XMLElement passElem = rootElem.GetChild("pass");
  149. while (passElem)
  150. {
  151. SharedPtr<PostProcessPass> pass(new PostProcessPass());
  152. pass->SetVertexShader(passElem.GetAttribute("vs"));
  153. pass->SetPixelShader(passElem.GetAttribute("ps"));
  154. pass->SetOutput(passElem.GetAttribute("output"));
  155. XMLElement textureElem = passElem.GetChild("texture");
  156. while (textureElem)
  157. {
  158. TextureUnit unit = TU_DIFFUSE;
  159. if (textureElem.HasAttribute("unit"))
  160. {
  161. String unitName = textureElem.GetAttributeLower("unit");
  162. if (unitName.Length() > 1)
  163. {
  164. unit = ParseTextureUnitName(unitName);
  165. if (unit == MAX_MATERIAL_TEXTURE_UNITS)
  166. LOGERROR("Unknown texture unit " + unitName);
  167. }
  168. else
  169. unit = (TextureUnit)Clamp(ToInt(unitName), 0, MAX_MATERIAL_TEXTURE_UNITS - 1);
  170. }
  171. if (unit != MAX_MATERIAL_TEXTURE_UNITS)
  172. {
  173. String name = textureElem.GetAttribute("name");
  174. pass->SetTexture(unit, name);
  175. }
  176. textureElem = textureElem.GetNext("texture");
  177. }
  178. XMLElement parameterElem = passElem.GetChild("parameter");
  179. while (parameterElem)
  180. {
  181. String name = parameterElem.GetAttribute("name");
  182. Vector4 value = parameterElem.GetVector("value");
  183. pass->SetShaderParameter(name, value);
  184. parameterElem = parameterElem.GetNext("parameter");
  185. }
  186. passes_.Push(pass);
  187. passElem = passElem.GetNext("pass");
  188. }
  189. return true;
  190. }
  191. void PostProcess::SetNumPasses(unsigned passes)
  192. {
  193. passes_.Resize(passes);
  194. for (unsigned i = 0; i < passes_.Size(); ++i)
  195. {
  196. if (!passes_[i])
  197. passes_[i] = new PostProcessPass();
  198. }
  199. }
  200. bool PostProcess::CreateRenderTarget(const String& name, unsigned width, unsigned height, unsigned format, bool sizeDivisor, bool filtered)
  201. {
  202. if (name.Trimmed().Empty())
  203. return false;
  204. PostProcessRenderTarget target;
  205. target.name_ = name;
  206. target.format_ = format;
  207. target.size_ = IntVector2(width, height),
  208. target.sizeDivisor_ = sizeDivisor;
  209. target.filtered_ = filtered;
  210. renderTargets_[StringHash(name)] = target;
  211. return true;
  212. }
  213. void PostProcess::RemoveRenderTarget(const String& name)
  214. {
  215. renderTargets_.Erase(StringHash(name));
  216. }
  217. void PostProcess::SetShaderParameter(const String& name, const Vector4& value)
  218. {
  219. shaderParameters_[StringHash(name)] = value;
  220. }
  221. void PostProcess::RemoveShaderParameter(const String& name)
  222. {
  223. shaderParameters_.Erase(StringHash(name));
  224. }
  225. void PostProcess::SetActive(bool active)
  226. {
  227. active_ = active;
  228. }
  229. SharedPtr<PostProcess> PostProcess::Clone()
  230. {
  231. SharedPtr<PostProcess> clone(new PostProcess(context_));
  232. clone->passes_.Resize(passes_.Size());
  233. for (unsigned i = 0; i < passes_.Size(); ++i)
  234. clone->passes_[i] = passes_[i]->Clone();
  235. clone->renderTargets_ = renderTargets_;
  236. clone->active_ = active_;
  237. return clone;
  238. }
  239. PostProcessPass* PostProcess::GetPass(unsigned index) const
  240. {
  241. if (index < passes_.Size())
  242. return passes_[index];
  243. else
  244. return 0;
  245. }
  246. bool PostProcess::HasRenderTarget(const String& name) const
  247. {
  248. return renderTargets_.Contains(StringHash(name));
  249. }
  250. const Vector4& PostProcess::GetShaderParameter(const String& name) const
  251. {
  252. HashMap<StringHash, Vector4>::ConstIterator i = shaderParameters_.Find(StringHash(name));
  253. return i != shaderParameters_.End() ? i->second_ : Vector4::ZERO;
  254. }