PostProcess.cpp 7.9 KB

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