PostProcess.cpp 7.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237
  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_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. const String& PostProcessPass::GetTexture(TextureUnit unit) const
  64. {
  65. return unit < MAX_TEXTURE_UNITS ? textureNames_[unit] : emptyName;
  66. }
  67. const Vector4& PostProcessPass::GetShaderParameter(const String& name) const
  68. {
  69. HashMap<StringHash, Vector4>::ConstIterator i = shaderParameters_.Find(StringHash(name));
  70. return i != shaderParameters_.End() ? i->second_ : Vector4::ZERO;
  71. }
  72. OBJECTTYPESTATIC(PostProcess);
  73. PostProcess::PostProcess(Context* context) :
  74. Object(context)
  75. {
  76. }
  77. PostProcess::~PostProcess()
  78. {
  79. }
  80. bool PostProcess::LoadParameters(XMLFile* file)
  81. {
  82. if (!file)
  83. return false;
  84. XMLElement rootElem = parameterSource_->GetRoot();
  85. if (!rootElem)
  86. return false;
  87. parameterSource_ = file;
  88. renderTargets_.Clear();
  89. passes_.Clear();
  90. XMLElement rtElem = rootElem.GetChild("rendertarget");
  91. while (rtElem)
  92. {
  93. String name = rtElem.GetString("name");
  94. unsigned format = Graphics::GetRGBFormat();
  95. String formatName = rtElem.GetString("format").ToLower();
  96. if (formatName == "rgba")
  97. format = Graphics::GetRGBAFormat();
  98. else if (formatName == "float")
  99. format = Graphics::GetFloatFormat();
  100. bool sizeDivisor = false;
  101. bool filtered = false;
  102. unsigned width = 0;
  103. unsigned height = 0;
  104. if (rtElem.HasAttribute("filter"))
  105. filtered = rtElem.GetBool("filter");
  106. if (rtElem.HasAttribute("size"))
  107. {
  108. IntVector2 size = rtElem.GetIntVector2("size");
  109. width = size.x_;
  110. height = size.y_;
  111. }
  112. if (rtElem.HasAttribute("width"))
  113. width = rtElem.GetInt("width");
  114. if (rtElem.HasAttribute("height"))
  115. height = rtElem.GetInt("height");
  116. if (rtElem.HasAttribute("sizedivisor"))
  117. {
  118. IntVector2 size = rtElem.GetIntVector2("sizedivisor");
  119. width = size.x_;
  120. height = size.y_;
  121. sizeDivisor = true;
  122. }
  123. CreateRenderTarget(name, width, height, format, sizeDivisor, filtered);
  124. rtElem = rtElem.GetNext("rendertarget");
  125. }
  126. XMLElement passElem = rootElem.GetChild("pass");
  127. while (passElem)
  128. {
  129. SharedPtr<PostProcessPass> pass(new PostProcessPass());
  130. pass->SetVertexShader(passElem.GetString("vs"));
  131. pass->SetPixelShader(passElem.GetString("ps"));
  132. pass->SetOutput(passElem.GetString("output"));
  133. XMLElement textureElem = passElem.GetChild("texture");
  134. while (textureElem)
  135. {
  136. TextureUnit unit = TU_DIFFUSE;
  137. if (textureElem.HasAttribute("unit"))
  138. {
  139. String unitName = textureElem.GetStringLower("unit");
  140. unit = ParseTextureUnitName(unitName);
  141. if (unit == MAX_MATERIAL_TEXTURE_UNITS)
  142. LOGERROR("Unknown texture unit " + unitName);
  143. }
  144. if (unit != MAX_MATERIAL_TEXTURE_UNITS)
  145. {
  146. String name = textureElem.GetString("name");
  147. pass->SetTexture(unit, name);
  148. }
  149. textureElem = textureElem.GetNext("texture");
  150. }
  151. XMLElement parameterElem = passElem.GetChild("parameter");
  152. while (parameterElem)
  153. {
  154. String name = parameterElem.GetString("name");
  155. Vector4 value = parameterElem.GetVector("value");
  156. pass->SetShaderParameter(name, value);
  157. parameterElem = parameterElem.GetNext("parameter");
  158. }
  159. passes_.Push(pass);
  160. passElem = passElem.GetNext("pass");
  161. }
  162. return true;
  163. }
  164. void PostProcess::SetNumPasses(unsigned passes)
  165. {
  166. passes_.Resize(passes);
  167. for (unsigned i = 0; i < passes_.Size(); ++i)
  168. {
  169. if (!passes_[i])
  170. passes_[i] = new PostProcessPass();
  171. }
  172. }
  173. bool PostProcess::CreateRenderTarget(const String& name, unsigned width, unsigned height, unsigned format, bool sizeDivisor, bool filtered)
  174. {
  175. if (name.Trimmed().Empty())
  176. return false;
  177. PostProcessRenderTarget target;
  178. target.format_ = format;
  179. target.size_ = IntVector2(width, height),
  180. target.sizeDivisor_ = sizeDivisor;
  181. target.filtered_ = filtered;
  182. renderTargets_[StringHash(name)] = target;
  183. return true;
  184. }
  185. void PostProcess::RemoveRenderTarget(const String& name)
  186. {
  187. renderTargets_.Erase(StringHash(name));
  188. }
  189. PostProcessPass* PostProcess::GetPass(unsigned index) const
  190. {
  191. if (index < passes_.Size())
  192. return passes_[index];
  193. else
  194. return 0;
  195. }
  196. bool PostProcess::HasRenderTarget(const String& name) const
  197. {
  198. return renderTargets_.Contains(StringHash(name));
  199. }