RenderPath.cpp 12 KB

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