RenderPath.cpp 14 KB

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