RenderPath.cpp 8.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275
  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 "RenderPath.h"
  25. #include "StringUtils.h"
  26. #include "XMLFile.h"
  27. #include "DebugNew.h"
  28. namespace Urho3D
  29. {
  30. static const String commandTypeNames[] =
  31. {
  32. "none",
  33. "clear",
  34. "scenepass",
  35. "quad",
  36. "forwardlights",
  37. "lightvolumes",
  38. ""
  39. };
  40. static const String sortModeNames[] =
  41. {
  42. "fronttoback"
  43. "backtofront",
  44. ""
  45. };
  46. TextureUnit ParseTextureUnitName(const String& name);
  47. void RenderTargetInfo::LoadParameters(const XMLElement& element)
  48. {
  49. name_ = element.GetAttribute("name");
  50. tag_ = element.GetAttribute("tag");
  51. if (element.HasAttribute("active"))
  52. active_ = element.GetBool("active");
  53. String formatName = element.GetAttribute("format");
  54. format_ = Graphics::GetFormat(formatName);
  55. if (element.HasAttribute("filter"))
  56. filtered_ = element.GetBool("filter");
  57. if (element.HasAttribute("size"))
  58. size_ = element.GetIntVector2("size");
  59. if (element.HasAttribute("sizedivisor"))
  60. {
  61. size_ = element.GetIntVector2("sizedivisor");
  62. sizeMode_ = SIZE_VIEWPORTDIVISOR;
  63. }
  64. if (element.HasAttribute("rtsizedivisor"))
  65. {
  66. size_ = element.GetIntVector2("rtsizedivisor");
  67. sizeMode_ = SIZE_RENDERTARGETDIVISOR;
  68. }
  69. if (element.HasAttribute("width"))
  70. size_.x_ = element.GetInt("width");
  71. if (element.HasAttribute("height"))
  72. size_.y_ = element.GetInt("height");
  73. }
  74. void RenderPathCommand::LoadParameters(const XMLElement& element)
  75. {
  76. type_ = (RenderCommandType)GetStringListIndex(element.GetAttributeLower("type"), commandTypeNames, CMD_NONE);
  77. tag_ = element.GetAttribute("tag");
  78. if (element.HasAttribute("active"))
  79. active_ = element.GetBool("active");
  80. switch (type_)
  81. {
  82. case CMD_CLEAR:
  83. if (element.HasAttribute("color"))
  84. {
  85. clearFlags_ |= CLEAR_COLOR;
  86. // Mark fog color with negative values
  87. if (element.GetAttributeLower("color") == "fog")
  88. useFogColor_ = true;
  89. else
  90. clearColor_ = element.GetColor("color");
  91. }
  92. if (element.HasAttribute("depth"))
  93. {
  94. clearFlags_ |= CLEAR_DEPTH;
  95. clearDepth_ = element.GetFloat("depth");
  96. }
  97. if (element.HasAttribute("stencil"))
  98. {
  99. clearFlags_ |= CLEAR_STENCIL;
  100. clearStencil_ = element.GetInt("stencil");
  101. }
  102. break;
  103. case CMD_SCENEPASS:
  104. pass_ = StringHash(element.GetAttribute("pass"));
  105. sortMode_ = (RenderCommandSortMode)GetStringListIndex(element.GetAttributeLower("sort"), sortModeNames, SORT_FRONTTOBACK);
  106. if (element.HasAttribute("marktostencil"))
  107. markToStencil_ = element.GetBool("marktostencil");
  108. if (element.HasAttribute("vertexlights"))
  109. vertexLights_ = element.GetBool("vertexlights");
  110. if (element.HasAttribute("usescissor"))
  111. useScissor_ = element.GetBool("usescissor");
  112. break;
  113. case CMD_LIGHTVOLUMES:
  114. case CMD_QUAD:
  115. vertexShaderName_ = element.GetAttribute("vs");
  116. pixelShaderName_ = element.GetAttribute("ps");
  117. if (type_ == CMD_QUAD)
  118. {
  119. XMLElement parameterElem = element.GetChild("parameter");
  120. while (parameterElem)
  121. {
  122. String name = parameterElem.GetAttribute("name");
  123. Vector4 value = parameterElem.GetVector("value");
  124. shaderParameters_[StringHash(name)] = value;
  125. parameterElem = parameterElem.GetNext("parameter");
  126. }
  127. }
  128. break;
  129. }
  130. // By default use 1 output, which is the viewport
  131. outputs_.Push("viewport");
  132. if (element.HasAttribute("output"))
  133. outputs_[0] = element.GetAttribute("output");
  134. // Check for defining multiple outputs
  135. XMLElement outputElem = element.GetChild("output");
  136. while (outputElem)
  137. {
  138. unsigned index = outputElem.GetInt("index");
  139. if (index < MAX_RENDERTARGETS)
  140. {
  141. if (index >= outputs_.Size())
  142. outputs_.Resize(index + 1);
  143. outputs_[index] = outputElem.GetAttribute("name");
  144. }
  145. outputElem = outputElem.GetNext("output");
  146. }
  147. XMLElement textureElem = element.GetChild("texture");
  148. while (textureElem)
  149. {
  150. TextureUnit unit = TU_DIFFUSE;
  151. if (textureElem.HasAttribute("unit"))
  152. {
  153. String unitName = textureElem.GetAttributeLower("unit");
  154. if (unitName.Length() > 1)
  155. unit = ParseTextureUnitName(unitName);
  156. else
  157. unit = (TextureUnit)Clamp(ToInt(unitName), 0, MAX_TEXTURE_UNITS - 1);
  158. }
  159. if (unit < MAX_TEXTURE_UNITS)
  160. {
  161. String name = textureElem.GetAttribute("name");
  162. textureNames_[unit] = name;
  163. }
  164. textureElem = textureElem.GetNext("texture");
  165. }
  166. }
  167. RenderPath::RenderPath()
  168. {
  169. }
  170. RenderPath::~RenderPath()
  171. {
  172. }
  173. SharedPtr<RenderPath> RenderPath::Clone()
  174. {
  175. SharedPtr<RenderPath> newRenderPath(new RenderPath());
  176. newRenderPath->renderTargets_ = renderTargets_;
  177. newRenderPath->commands_ = commands_;
  178. return newRenderPath;
  179. }
  180. bool RenderPath::LoadParameters(XMLFile* file)
  181. {
  182. renderTargets_.Clear();
  183. commands_.Clear();
  184. return Append(file);
  185. }
  186. bool RenderPath::Append(XMLFile* file)
  187. {
  188. if (!file)
  189. return false;
  190. XMLElement rootElem = file->GetRoot();
  191. if (!rootElem)
  192. return false;
  193. XMLElement rtElem = rootElem.GetChild("rendertarget");
  194. while (rtElem)
  195. {
  196. RenderTargetInfo info;
  197. info.LoadParameters(rtElem);
  198. if (!info.name_.Trimmed().Empty())
  199. renderTargets_.Push(info);
  200. rtElem = rtElem.GetNext("rendertarget");
  201. }
  202. XMLElement cmdElem = rootElem.GetChild("command");
  203. while (cmdElem)
  204. {
  205. RenderPathCommand cmd;
  206. cmd.LoadParameters(cmdElem);
  207. if (cmd.type_ != CMD_NONE)
  208. commands_.Push(cmd);
  209. cmdElem = cmdElem.GetNext("command");
  210. }
  211. return true;
  212. }
  213. void RenderPath::SetActive(const String& tag, bool active)
  214. {
  215. for (unsigned i = 0; i < renderTargets_.Size(); ++i)
  216. {
  217. if (!renderTargets_[i].tag_.Compare(tag, false))
  218. renderTargets_[i].active_ = active;
  219. }
  220. for (unsigned i = 0; i < commands_.Size(); ++i)
  221. {
  222. if (!commands_[i].tag_.Compare(tag, false))
  223. commands_[i].active_ = active;
  224. }
  225. }
  226. void RenderPath::ToggleActive(const String& tag)
  227. {
  228. for (unsigned i = 0; i < renderTargets_.Size(); ++i)
  229. {
  230. if (!renderTargets_[i].tag_.Compare(tag, false))
  231. renderTargets_[i].active_ = !renderTargets_[i].active_;
  232. }
  233. for (unsigned i = 0; i < commands_.Size(); ++i)
  234. {
  235. if (!commands_[i].tag_.Compare(tag, false))
  236. commands_[i].active_ = !commands_[i].active_;
  237. }
  238. }
  239. }