RenderPath.cpp 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423
  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_FORWARDLIGHTS:
  114. if (element.HasAttribute("uselitbase"))
  115. useLitBase_ = element.GetBool("uselitbase");
  116. break;
  117. case CMD_LIGHTVOLUMES:
  118. case CMD_QUAD:
  119. vertexShaderName_ = element.GetAttribute("vs");
  120. pixelShaderName_ = element.GetAttribute("ps");
  121. if (type_ == CMD_QUAD)
  122. {
  123. XMLElement parameterElem = element.GetChild("parameter");
  124. while (parameterElem)
  125. {
  126. String name = parameterElem.GetAttribute("name");
  127. Vector4 value = parameterElem.GetVector("value");
  128. shaderParameters_[StringHash(name)] = value;
  129. parameterElem = parameterElem.GetNext("parameter");
  130. }
  131. }
  132. break;
  133. }
  134. // By default use 1 output, which is the viewport
  135. outputNames_.Push("viewport");
  136. if (element.HasAttribute("output"))
  137. outputNames_[0] = element.GetAttribute("output");
  138. // Check for defining multiple outputs
  139. XMLElement outputElem = element.GetChild("output");
  140. while (outputElem)
  141. {
  142. unsigned index = outputElem.GetInt("index");
  143. if (index < MAX_RENDERTARGETS)
  144. {
  145. if (index >= outputNames_.Size())
  146. outputNames_.Resize(index + 1);
  147. outputNames_[index] = outputElem.GetAttribute("name");
  148. }
  149. outputElem = outputElem.GetNext("output");
  150. }
  151. XMLElement textureElem = element.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. unit = ParseTextureUnitName(unitName);
  160. else
  161. unit = (TextureUnit)Clamp(ToInt(unitName), 0, MAX_TEXTURE_UNITS - 1);
  162. }
  163. if (unit < MAX_TEXTURE_UNITS)
  164. {
  165. String name = textureElem.GetAttribute("name");
  166. textureNames_[unit] = name;
  167. }
  168. textureElem = textureElem.GetNext("texture");
  169. }
  170. }
  171. void RenderPathCommand::SetTextureName(TextureUnit unit, const String& name)
  172. {
  173. if (unit < MAX_TEXTURE_UNITS)
  174. textureNames_[unit] = name;
  175. }
  176. void RenderPathCommand::SetShaderParameter(const String& name, const Vector4& value)
  177. {
  178. shaderParameters_[StringHash(name)] = value;
  179. }
  180. void RenderPathCommand::RemoveShaderParameter(const String& name)
  181. {
  182. shaderParameters_.Erase(StringHash(name));
  183. }
  184. void RenderPathCommand::SetNumOutputs(unsigned num)
  185. {
  186. num = Clamp((int)num, 1, MAX_RENDERTARGETS);
  187. outputNames_.Resize(num);
  188. }
  189. void RenderPathCommand::SetOutputName(unsigned index, const String& name)
  190. {
  191. if (index < outputNames_.Size())
  192. outputNames_[index] = name;
  193. else if (index == outputNames_.Size() && index < MAX_RENDERTARGETS)
  194. outputNames_.Push(name);
  195. }
  196. const String& RenderPathCommand::GetTextureName(TextureUnit unit) const
  197. {
  198. return unit < MAX_TEXTURE_UNITS ? textureNames_[unit] : String::EMPTY;
  199. }
  200. const Vector4& RenderPathCommand::GetShaderParameter(const String& name) const
  201. {
  202. HashMap<StringHash, Vector4>::ConstIterator i = shaderParameters_.Find(StringHash(name));
  203. return i != shaderParameters_.End() ? i->second_ : Vector4::ZERO;
  204. }
  205. const String& RenderPathCommand::GetOutputName(unsigned index) const
  206. {
  207. return index < outputNames_.Size() ? outputNames_[index] : String::EMPTY;
  208. }
  209. RenderPath::RenderPath()
  210. {
  211. }
  212. RenderPath::~RenderPath()
  213. {
  214. }
  215. SharedPtr<RenderPath> RenderPath::Clone()
  216. {
  217. SharedPtr<RenderPath> newRenderPath(new RenderPath());
  218. newRenderPath->renderTargets_ = renderTargets_;
  219. newRenderPath->commands_ = commands_;
  220. return newRenderPath;
  221. }
  222. bool RenderPath::LoadParameters(XMLFile* file)
  223. {
  224. renderTargets_.Clear();
  225. commands_.Clear();
  226. return Append(file);
  227. }
  228. bool RenderPath::Append(XMLFile* file)
  229. {
  230. if (!file)
  231. return false;
  232. XMLElement rootElem = file->GetRoot();
  233. if (!rootElem)
  234. return false;
  235. XMLElement rtElem = rootElem.GetChild("rendertarget");
  236. while (rtElem)
  237. {
  238. RenderTargetInfo info;
  239. info.LoadParameters(rtElem);
  240. if (!info.name_.Trimmed().Empty())
  241. renderTargets_.Push(info);
  242. rtElem = rtElem.GetNext("rendertarget");
  243. }
  244. XMLElement cmdElem = rootElem.GetChild("command");
  245. while (cmdElem)
  246. {
  247. RenderPathCommand cmd;
  248. cmd.LoadParameters(cmdElem);
  249. if (cmd.type_ != CMD_NONE)
  250. commands_.Push(cmd);
  251. cmdElem = cmdElem.GetNext("command");
  252. }
  253. return true;
  254. }
  255. void RenderPath::SetActive(const String& tag, bool active)
  256. {
  257. for (unsigned i = 0; i < renderTargets_.Size(); ++i)
  258. {
  259. if (!renderTargets_[i].tag_.Compare(tag, false))
  260. renderTargets_[i].active_ = active;
  261. }
  262. for (unsigned i = 0; i < commands_.Size(); ++i)
  263. {
  264. if (!commands_[i].tag_.Compare(tag, false))
  265. commands_[i].active_ = active;
  266. }
  267. }
  268. void RenderPath::ToggleActive(const String& tag)
  269. {
  270. for (unsigned i = 0; i < renderTargets_.Size(); ++i)
  271. {
  272. if (!renderTargets_[i].tag_.Compare(tag, false))
  273. renderTargets_[i].active_ = !renderTargets_[i].active_;
  274. }
  275. for (unsigned i = 0; i < commands_.Size(); ++i)
  276. {
  277. if (!commands_[i].tag_.Compare(tag, false))
  278. commands_[i].active_ = !commands_[i].active_;
  279. }
  280. }
  281. void RenderPath::SetRenderTarget(unsigned index, const RenderTargetInfo& info)
  282. {
  283. if (index < renderTargets_.Size())
  284. renderTargets_[index] = info;
  285. else if (index == renderTargets_.Size())
  286. AddRenderTarget(info);
  287. }
  288. void RenderPath::AddRenderTarget(const RenderTargetInfo& info)
  289. {
  290. renderTargets_.Push(info);
  291. }
  292. void RenderPath::RemoveRenderTarget(unsigned index)
  293. {
  294. renderTargets_.Erase(index);
  295. }
  296. void RenderPath::RemoveRenderTarget(const String& name)
  297. {
  298. for (unsigned i = 0; i < renderTargets_.Size(); ++i)
  299. {
  300. if (!renderTargets_[i].name_.Compare(name, false))
  301. {
  302. renderTargets_.Erase(i);
  303. return;
  304. }
  305. }
  306. }
  307. void RenderPath::RemoveRenderTargets(const String& tag)
  308. {
  309. for (unsigned i = renderTargets_.Size() - 1; i < renderTargets_.Size(); --i)
  310. {
  311. if (!renderTargets_[i].tag_.Compare(tag, false))
  312. renderTargets_.Erase(i);
  313. }
  314. }
  315. void RenderPath::SetCommand(unsigned index, const RenderPathCommand& command)
  316. {
  317. if (index < commands_.Size())
  318. commands_[index] = command;
  319. else if (index == commands_.Size())
  320. AddCommand(command);
  321. }
  322. void RenderPath::AddCommand(const RenderPathCommand& command)
  323. {
  324. commands_.Push(command);
  325. }
  326. void RenderPath::InsertCommand(unsigned index, const RenderPathCommand& command)
  327. {
  328. commands_.Insert(index, command);
  329. }
  330. void RenderPath::RemoveCommand(unsigned index)
  331. {
  332. commands_.Erase(index);
  333. }
  334. void RenderPath::RemoveCommands(const String& tag)
  335. {
  336. for (unsigned i = commands_.Size() - 1; i < commands_.Size(); --i)
  337. {
  338. if (!commands_[i].tag_.Compare(tag, false))
  339. commands_.Erase(i);
  340. }
  341. }
  342. void RenderPath::SetShaderParameter(const String& name, const Vector4& value)
  343. {
  344. StringHash nameHash(name);
  345. for (unsigned i = 0; i < commands_.Size(); ++i)
  346. {
  347. HashMap<StringHash, Vector4>::Iterator j = commands_[i].shaderParameters_.Find(nameHash);
  348. if (j != commands_[i].shaderParameters_.End())
  349. j->second_ = value;
  350. }
  351. }
  352. const Vector4& RenderPath::GetShaderParameter(const String& name) const
  353. {
  354. StringHash nameHash(name);
  355. for (unsigned i = 0; i < commands_.Size(); ++i)
  356. {
  357. HashMap<StringHash, Vector4>::ConstIterator j = commands_[i].shaderParameters_.Find(nameHash);
  358. if (j != commands_[i].shaderParameters_.End())
  359. return j->second_;
  360. }
  361. return Vector4::ZERO;
  362. }
  363. }