PostProcess.cpp 7.0 KB

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