PostProcess.cpp 9.0 KB

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