PostProcess.cpp 8.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303
  1. //
  2. // Urho3D Engine
  3. // Copyright (c) 2008-2012 Lasse Oorni
  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_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_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_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. String formatName = rtElem.GetAttribute("format");
  110. unsigned format = Graphics::GetFormat(formatName);
  111. bool sizeDivisor = false;
  112. bool filtered = false;
  113. unsigned width = 0;
  114. unsigned height = 0;
  115. if (rtElem.HasAttribute("filter"))
  116. filtered = rtElem.GetBool("filter");
  117. if (rtElem.HasAttribute("size"))
  118. {
  119. IntVector2 size = rtElem.GetIntVector2("size");
  120. width = size.x_;
  121. height = size.y_;
  122. }
  123. if (rtElem.HasAttribute("width"))
  124. width = rtElem.GetInt("width");
  125. if (rtElem.HasAttribute("height"))
  126. height = rtElem.GetInt("height");
  127. if (rtElem.HasAttribute("sizedivisor"))
  128. {
  129. IntVector2 size = rtElem.GetIntVector2("sizedivisor");
  130. width = size.x_;
  131. height = size.y_;
  132. sizeDivisor = true;
  133. }
  134. CreateRenderTarget(name, width, height, format, sizeDivisor, filtered);
  135. rtElem = rtElem.GetNext("rendertarget");
  136. }
  137. XMLElement parameterElem = rootElem.GetChild("parameter");
  138. while (parameterElem)
  139. {
  140. String name = parameterElem.GetAttribute("name");
  141. Vector4 value = parameterElem.GetVector("value");
  142. SetShaderParameter(name, value);
  143. parameterElem = parameterElem.GetNext("parameter");
  144. }
  145. XMLElement passElem = rootElem.GetChild("pass");
  146. while (passElem)
  147. {
  148. SharedPtr<PostProcessPass> pass(new PostProcessPass());
  149. pass->SetVertexShader(passElem.GetAttribute("vs"));
  150. pass->SetPixelShader(passElem.GetAttribute("ps"));
  151. pass->SetOutput(passElem.GetAttribute("output"));
  152. XMLElement textureElem = passElem.GetChild("texture");
  153. while (textureElem)
  154. {
  155. TextureUnit unit = TU_DIFFUSE;
  156. if (textureElem.HasAttribute("unit"))
  157. {
  158. String unitName = textureElem.GetAttributeLower("unit");
  159. if (unitName.Length() > 1)
  160. {
  161. unit = ParseTextureUnitName(unitName);
  162. if (unit >= MAX_TEXTURE_UNITS)
  163. LOGERROR("Unknown texture unit " + unitName);
  164. }
  165. else
  166. unit = (TextureUnit)Clamp(ToInt(unitName), 0, MAX_TEXTURE_UNITS - 1);
  167. }
  168. if (unit < MAX_TEXTURE_UNITS)
  169. {
  170. String name = textureElem.GetAttribute("name");
  171. pass->SetTexture(unit, name);
  172. }
  173. textureElem = textureElem.GetNext("texture");
  174. }
  175. XMLElement parameterElem = passElem.GetChild("parameter");
  176. while (parameterElem)
  177. {
  178. String name = parameterElem.GetAttribute("name");
  179. Vector4 value = parameterElem.GetVector("value");
  180. pass->SetShaderParameter(name, value);
  181. parameterElem = parameterElem.GetNext("parameter");
  182. }
  183. passes_.Push(pass);
  184. passElem = passElem.GetNext("pass");
  185. }
  186. return true;
  187. }
  188. void PostProcess::SetNumPasses(unsigned passes)
  189. {
  190. passes_.Resize(passes);
  191. for (unsigned i = 0; i < passes_.Size(); ++i)
  192. {
  193. if (!passes_[i])
  194. passes_[i] = new PostProcessPass();
  195. }
  196. }
  197. bool PostProcess::CreateRenderTarget(const String& name, unsigned width, unsigned height, unsigned format, bool sizeDivisor, bool filtered)
  198. {
  199. if (name.Trimmed().Empty())
  200. return false;
  201. RenderTargetInfo target;
  202. target.name_ = name;
  203. target.format_ = format;
  204. target.size_ = IntVector2(width, height),
  205. target.sizeDivisor_ = sizeDivisor;
  206. target.filtered_ = filtered;
  207. renderTargets_[StringHash(name)] = target;
  208. return true;
  209. }
  210. void PostProcess::RemoveRenderTarget(const String& name)
  211. {
  212. renderTargets_.Erase(StringHash(name));
  213. }
  214. void PostProcess::SetShaderParameter(const String& name, const Vector4& value)
  215. {
  216. shaderParameters_[StringHash(name)] = value;
  217. }
  218. void PostProcess::RemoveShaderParameter(const String& name)
  219. {
  220. shaderParameters_.Erase(StringHash(name));
  221. }
  222. void PostProcess::SetActive(bool active)
  223. {
  224. active_ = active;
  225. }
  226. SharedPtr<PostProcess> PostProcess::Clone()
  227. {
  228. SharedPtr<PostProcess> clone(new PostProcess(context_));
  229. clone->passes_.Resize(passes_.Size());
  230. for (unsigned i = 0; i < passes_.Size(); ++i)
  231. clone->passes_[i] = passes_[i]->Clone();
  232. clone->renderTargets_ = renderTargets_;
  233. clone->active_ = active_;
  234. return clone;
  235. }
  236. PostProcessPass* PostProcess::GetPass(unsigned index) const
  237. {
  238. if (index < passes_.Size())
  239. return passes_[index];
  240. else
  241. return 0;
  242. }
  243. bool PostProcess::HasRenderTarget(const String& name) const
  244. {
  245. return renderTargets_.Contains(StringHash(name));
  246. }
  247. const Vector4& PostProcess::GetShaderParameter(const String& name) const
  248. {
  249. HashMap<StringHash, Vector4>::ConstIterator i = shaderParameters_.Find(StringHash(name));
  250. return i != shaderParameters_.End() ? i->second_ : Vector4::ZERO;
  251. }
  252. }