RenderPath.cpp 13 KB

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