RenderPath.cpp 14 KB

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