RenderPath.cpp 13 KB

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