RenderPath.cpp 14 KB

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