PostProcess.cpp 8.8 KB

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