RenderPath.cpp 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444
  1. //
  2. // Copyright (c) 2008-2014 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/Graphics.h"
  24. #include "../IO/Log.h"
  25. #include "../Graphics/Material.h"
  26. #include "../Graphics/RenderPath.h"
  27. #include "../Core/StringUtils.h"
  28. #include "../Resource/XMLFile.h"
  29. #include "../DebugNew.h"
  30. namespace Atomic
  31. {
  32. static const char* commandTypeNames[] =
  33. {
  34. "none",
  35. "clear",
  36. "scenepass",
  37. "quad",
  38. "forwardlights",
  39. "lightvolumes",
  40. "renderui",
  41. 0
  42. };
  43. static const char* sortModeNames[] =
  44. {
  45. "fronttoback",
  46. "backtofront",
  47. 0
  48. };
  49. TextureUnit ParseTextureUnitName(String name);
  50. void RenderTargetInfo::Load(const XMLElement& element)
  51. {
  52. name_ = element.GetAttribute("name");
  53. tag_ = element.GetAttribute("tag");
  54. if (element.HasAttribute("enabled"))
  55. enabled_ = element.GetBool("enabled");
  56. String formatName = element.GetAttribute("format");
  57. format_ = Graphics::GetFormat(formatName);
  58. if (element.HasAttribute("filter"))
  59. filtered_ = element.GetBool("filter");
  60. if (element.HasAttribute("srgb"))
  61. sRGB_ = element.GetBool("srgb");
  62. if (element.HasAttribute("persistent"))
  63. persistent_ = element.GetBool("persistent");
  64. if (element.HasAttribute("size"))
  65. size_ = element.GetVector2("size");
  66. if (element.HasAttribute("sizedivisor"))
  67. {
  68. size_ = element.GetVector2("sizedivisor");
  69. sizeMode_ = SIZE_VIEWPORTDIVISOR;
  70. }
  71. else if (element.HasAttribute("rtsizedivisor"))
  72. {
  73. // Deprecated rtsizedivisor mode, acts the same as sizedivisor mode now
  74. LOGWARNING("Deprecated rtsizedivisor mode used in rendertarget definition");
  75. size_ = element.GetVector2("rtsizedivisor");
  76. sizeMode_ = SIZE_VIEWPORTDIVISOR;
  77. }
  78. else if (element.HasAttribute("sizemultiplier"))
  79. {
  80. size_ = element.GetVector2("sizemultiplier");
  81. sizeMode_ = SIZE_VIEWPORTMULTIPLIER;
  82. }
  83. if (element.HasAttribute("width"))
  84. size_.x_ = element.GetFloat("width");
  85. if (element.HasAttribute("height"))
  86. size_.y_ = element.GetFloat("height");
  87. }
  88. void RenderPathCommand::Load(const XMLElement& element)
  89. {
  90. type_ = (RenderCommandType)GetStringListIndex(element.GetAttributeLower("type").CString(), commandTypeNames, CMD_NONE);
  91. tag_ = element.GetAttribute("tag");
  92. if (element.HasAttribute("enabled"))
  93. enabled_ = element.GetBool("enabled");
  94. if (element.HasAttribute("metadata"))
  95. metadata_ = element.GetAttribute("metadata");
  96. switch (type_)
  97. {
  98. case CMD_CLEAR:
  99. if (element.HasAttribute("color"))
  100. {
  101. clearFlags_ |= CLEAR_COLOR;
  102. if (element.GetAttributeLower("color") == "fog")
  103. useFogColor_ = true;
  104. else
  105. clearColor_ = element.GetColor("color");
  106. }
  107. if (element.HasAttribute("depth"))
  108. {
  109. clearFlags_ |= CLEAR_DEPTH;
  110. clearDepth_ = element.GetFloat("depth");
  111. }
  112. if (element.HasAttribute("stencil"))
  113. {
  114. clearFlags_ |= CLEAR_STENCIL;
  115. clearStencil_ = element.GetInt("stencil");
  116. }
  117. break;
  118. case CMD_SCENEPASS:
  119. pass_ = element.GetAttribute("pass");
  120. sortMode_ = (RenderCommandSortMode)GetStringListIndex(element.GetAttributeLower("sort").CString(), sortModeNames, SORT_FRONTTOBACK);
  121. if (element.HasAttribute("marktostencil"))
  122. markToStencil_ = element.GetBool("marktostencil");
  123. if (element.HasAttribute("vertexlights"))
  124. vertexLights_ = element.GetBool("vertexlights");
  125. break;
  126. case CMD_FORWARDLIGHTS:
  127. pass_ = element.GetAttribute("pass");
  128. if (element.HasAttribute("uselitbase"))
  129. useLitBase_ = element.GetBool("uselitbase");
  130. break;
  131. case CMD_LIGHTVOLUMES:
  132. case CMD_QUAD:
  133. vertexShaderName_ = element.GetAttribute("vs");
  134. pixelShaderName_ = element.GetAttribute("ps");
  135. vertexShaderDefines_ = element.GetAttribute("vsdefines");
  136. pixelShaderDefines_ = element.GetAttribute("psdefines");
  137. if (type_ == CMD_QUAD)
  138. {
  139. XMLElement parameterElem = element.GetChild("parameter");
  140. while (parameterElem)
  141. {
  142. String name = parameterElem.GetAttribute("name");
  143. shaderParameters_[name] = Material::ParseShaderParameterValue(parameterElem.GetAttribute("value"));
  144. parameterElem = parameterElem.GetNext("parameter");
  145. }
  146. }
  147. break;
  148. default:
  149. break;
  150. }
  151. // By default use 1 output, which is the viewport
  152. outputNames_.Push("viewport");
  153. if (element.HasAttribute("output"))
  154. outputNames_[0] = element.GetAttribute("output");
  155. if (element.HasAttribute("depthstencil"))
  156. depthStencilName_ = element.GetAttribute("depthstencil");
  157. // Check for defining multiple outputs
  158. XMLElement outputElem = element.GetChild("output");
  159. while (outputElem)
  160. {
  161. unsigned index = outputElem.GetInt("index");
  162. if (index < MAX_RENDERTARGETS)
  163. {
  164. if (index >= outputNames_.Size())
  165. outputNames_.Resize(index + 1);
  166. outputNames_[index] = outputElem.GetAttribute("name");
  167. }
  168. outputElem = outputElem.GetNext("output");
  169. }
  170. XMLElement textureElem = element.GetChild("texture");
  171. while (textureElem)
  172. {
  173. TextureUnit unit = TU_DIFFUSE;
  174. if (textureElem.HasAttribute("unit"))
  175. unit = ParseTextureUnitName(textureElem.GetAttribute("unit"));
  176. if (unit < MAX_TEXTURE_UNITS)
  177. {
  178. String name = textureElem.GetAttribute("name");
  179. textureNames_[unit] = name;
  180. }
  181. textureElem = textureElem.GetNext("texture");
  182. }
  183. }
  184. void RenderPathCommand::SetTextureName(TextureUnit unit, const String& name)
  185. {
  186. if (unit < MAX_TEXTURE_UNITS)
  187. textureNames_[unit] = name;
  188. }
  189. void RenderPathCommand::SetShaderParameter(const String& name, const Variant& value)
  190. {
  191. shaderParameters_[name] = value;
  192. }
  193. void RenderPathCommand::RemoveShaderParameter(const String& name)
  194. {
  195. shaderParameters_.Erase(name);
  196. }
  197. void RenderPathCommand::SetNumOutputs(unsigned num)
  198. {
  199. num = Clamp((int)num, 1, MAX_RENDERTARGETS);
  200. outputNames_.Resize(num);
  201. }
  202. void RenderPathCommand::SetOutputName(unsigned index, const String& name)
  203. {
  204. if (index < outputNames_.Size())
  205. outputNames_[index] = name;
  206. else if (index == outputNames_.Size() && index < MAX_RENDERTARGETS)
  207. outputNames_.Push(name);
  208. }
  209. void RenderPathCommand::SetDepthStencilName(const String& name)
  210. {
  211. depthStencilName_ = name;
  212. }
  213. const String& RenderPathCommand::GetTextureName(TextureUnit unit) const
  214. {
  215. return unit < MAX_TEXTURE_UNITS ? textureNames_[unit] : String::EMPTY;
  216. }
  217. const Variant& RenderPathCommand::GetShaderParameter(const String& name) const
  218. {
  219. HashMap<StringHash, Variant>::ConstIterator i = shaderParameters_.Find(name);
  220. return i != shaderParameters_.End() ? i->second_ : Variant::EMPTY;
  221. }
  222. const String& RenderPathCommand::GetOutputName(unsigned index) const
  223. {
  224. return index < outputNames_.Size() ? outputNames_[index] : String::EMPTY;
  225. }
  226. RenderPath::RenderPath()
  227. {
  228. }
  229. RenderPath::~RenderPath()
  230. {
  231. }
  232. RenderPath *RenderPath::Clone()
  233. {
  234. RenderPath* newRenderPath = new RenderPath();
  235. newRenderPath->renderTargets_ = renderTargets_;
  236. newRenderPath->commands_ = commands_;
  237. return newRenderPath;
  238. }
  239. bool RenderPath::Load(XMLFile* file)
  240. {
  241. renderTargets_.Clear();
  242. commands_.Clear();
  243. return Append(file);
  244. }
  245. bool RenderPath::Append(XMLFile* file)
  246. {
  247. if (!file)
  248. return false;
  249. XMLElement rootElem = file->GetRoot();
  250. if (!rootElem)
  251. return false;
  252. XMLElement rtElem = rootElem.GetChild("rendertarget");
  253. while (rtElem)
  254. {
  255. RenderTargetInfo info;
  256. info.Load(rtElem);
  257. if (!info.name_.Trimmed().Empty())
  258. renderTargets_.Push(info);
  259. rtElem = rtElem.GetNext("rendertarget");
  260. }
  261. XMLElement cmdElem = rootElem.GetChild("command");
  262. while (cmdElem)
  263. {
  264. RenderPathCommand cmd;
  265. cmd.Load(cmdElem);
  266. if (cmd.type_ != CMD_NONE)
  267. commands_.Push(cmd);
  268. cmdElem = cmdElem.GetNext("command");
  269. }
  270. return true;
  271. }
  272. void RenderPath::SetEnabled(const String& tag, bool active)
  273. {
  274. for (unsigned i = 0; i < renderTargets_.Size(); ++i)
  275. {
  276. if (!renderTargets_[i].tag_.Compare(tag, false))
  277. renderTargets_[i].enabled_ = active;
  278. }
  279. for (unsigned i = 0; i < commands_.Size(); ++i)
  280. {
  281. if (!commands_[i].tag_.Compare(tag, false))
  282. commands_[i].enabled_ = active;
  283. }
  284. }
  285. void RenderPath::ToggleEnabled(const String& tag)
  286. {
  287. for (unsigned i = 0; i < renderTargets_.Size(); ++i)
  288. {
  289. if (!renderTargets_[i].tag_.Compare(tag, false))
  290. renderTargets_[i].enabled_ = !renderTargets_[i].enabled_;
  291. }
  292. for (unsigned i = 0; i < commands_.Size(); ++i)
  293. {
  294. if (!commands_[i].tag_.Compare(tag, false))
  295. commands_[i].enabled_ = !commands_[i].enabled_;
  296. }
  297. }
  298. void RenderPath::SetRenderTarget(unsigned index, const RenderTargetInfo& info)
  299. {
  300. if (index < renderTargets_.Size())
  301. renderTargets_[index] = info;
  302. else if (index == renderTargets_.Size())
  303. AddRenderTarget(info);
  304. }
  305. void RenderPath::AddRenderTarget(const RenderTargetInfo& info)
  306. {
  307. renderTargets_.Push(info);
  308. }
  309. void RenderPath::RemoveRenderTarget(unsigned index)
  310. {
  311. renderTargets_.Erase(index);
  312. }
  313. void RenderPath::RemoveRenderTarget(const String& name)
  314. {
  315. for (unsigned i = 0; i < renderTargets_.Size(); ++i)
  316. {
  317. if (!renderTargets_[i].name_.Compare(name, false))
  318. {
  319. renderTargets_.Erase(i);
  320. return;
  321. }
  322. }
  323. }
  324. void RenderPath::RemoveRenderTargets(const String& tag)
  325. {
  326. for (unsigned i = renderTargets_.Size() - 1; i < renderTargets_.Size(); --i)
  327. {
  328. if (!renderTargets_[i].tag_.Compare(tag, false))
  329. renderTargets_.Erase(i);
  330. }
  331. }
  332. void RenderPath::SetCommand(unsigned index, const RenderPathCommand& command)
  333. {
  334. if (index < commands_.Size())
  335. commands_[index] = command;
  336. else if (index == commands_.Size())
  337. AddCommand(command);
  338. }
  339. void RenderPath::AddCommand(const RenderPathCommand& command)
  340. {
  341. commands_.Push(command);
  342. }
  343. void RenderPath::InsertCommand(unsigned index, const RenderPathCommand& command)
  344. {
  345. commands_.Insert(index, command);
  346. }
  347. void RenderPath::RemoveCommand(unsigned index)
  348. {
  349. commands_.Erase(index);
  350. }
  351. void RenderPath::RemoveCommands(const String& tag)
  352. {
  353. for (unsigned i = commands_.Size() - 1; i < commands_.Size(); --i)
  354. {
  355. if (!commands_[i].tag_.Compare(tag, false))
  356. commands_.Erase(i);
  357. }
  358. }
  359. void RenderPath::SetShaderParameter(const String& name, const Variant& value)
  360. {
  361. StringHash nameHash(name);
  362. for (unsigned i = 0; i < commands_.Size(); ++i)
  363. {
  364. HashMap<StringHash, Variant>::Iterator j = commands_[i].shaderParameters_.Find(nameHash);
  365. if (j != commands_[i].shaderParameters_.End())
  366. j->second_ = value;
  367. }
  368. }
  369. const Variant& RenderPath::GetShaderParameter(const String& name) const
  370. {
  371. StringHash nameHash(name);
  372. for (unsigned i = 0; i < commands_.Size(); ++i)
  373. {
  374. HashMap<StringHash, Variant>::ConstIterator j = commands_[i].shaderParameters_.Find(nameHash);
  375. if (j != commands_[i].shaderParameters_.End())
  376. return j->second_;
  377. }
  378. return Variant::EMPTY;
  379. }
  380. }