RenderPath.cpp 14 KB

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