RenderPath.cpp 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426
  1. //
  2. // Copyright (c) 2008-2013 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 "RenderPath.h"
  25. #include "StringUtils.h"
  26. #include "XMLFile.h"
  27. #include "DebugNew.h"
  28. namespace Urho3D
  29. {
  30. static const String commandTypeNames[] =
  31. {
  32. "none",
  33. "clear",
  34. "scenepass",
  35. "quad",
  36. "forwardlights",
  37. "lightvolumes",
  38. ""
  39. };
  40. static const String sortModeNames[] =
  41. {
  42. "fronttoback",
  43. "backtofront",
  44. ""
  45. };
  46. TextureUnit ParseTextureUnitName(const String& name);
  47. void RenderTargetInfo::LoadParameters(const XMLElement& element)
  48. {
  49. name_ = element.GetAttribute("name");
  50. tag_ = element.GetAttribute("tag");
  51. if (element.HasAttribute("active"))
  52. active_ = element.GetBool("active");
  53. String formatName = element.GetAttribute("format");
  54. format_ = Graphics::GetFormat(formatName);
  55. if (element.HasAttribute("filter"))
  56. filtered_ = element.GetBool("filter");
  57. if (element.HasAttribute("size"))
  58. size_ = element.GetIntVector2("size");
  59. if (element.HasAttribute("sizedivisor"))
  60. {
  61. size_ = element.GetIntVector2("sizedivisor");
  62. sizeMode_ = SIZE_VIEWPORTDIVISOR;
  63. }
  64. if (element.HasAttribute("rtsizedivisor"))
  65. {
  66. size_ = element.GetIntVector2("rtsizedivisor");
  67. sizeMode_ = SIZE_RENDERTARGETDIVISOR;
  68. }
  69. if (element.HasAttribute("width"))
  70. size_.x_ = element.GetInt("width");
  71. if (element.HasAttribute("height"))
  72. size_.y_ = element.GetInt("height");
  73. }
  74. void RenderPathCommand::LoadParameters(const XMLElement& element)
  75. {
  76. type_ = (RenderCommandType)GetStringListIndex(element.GetAttributeLower("type"), commandTypeNames, CMD_NONE);
  77. tag_ = element.GetAttribute("tag");
  78. if (element.HasAttribute("active"))
  79. active_ = element.GetBool("active");
  80. switch (type_)
  81. {
  82. case CMD_CLEAR:
  83. if (element.HasAttribute("color"))
  84. {
  85. clearFlags_ |= CLEAR_COLOR;
  86. // Mark fog color with negative values
  87. if (element.GetAttributeLower("color") == "fog")
  88. useFogColor_ = true;
  89. else
  90. clearColor_ = element.GetColor("color");
  91. }
  92. if (element.HasAttribute("depth"))
  93. {
  94. clearFlags_ |= CLEAR_DEPTH;
  95. clearDepth_ = element.GetFloat("depth");
  96. }
  97. if (element.HasAttribute("stencil"))
  98. {
  99. clearFlags_ |= CLEAR_STENCIL;
  100. clearStencil_ = element.GetInt("stencil");
  101. }
  102. break;
  103. case CMD_SCENEPASS:
  104. pass_ = StringHash(element.GetAttribute("pass"));
  105. sortMode_ = (RenderCommandSortMode)GetStringListIndex(element.GetAttributeLower("sort"), sortModeNames, SORT_FRONTTOBACK);
  106. if (element.HasAttribute("marktostencil"))
  107. markToStencil_ = element.GetBool("marktostencil");
  108. if (element.HasAttribute("vertexlights"))
  109. vertexLights_ = element.GetBool("vertexlights");
  110. if (element.HasAttribute("usescissor"))
  111. useScissor_ = element.GetBool("usescissor");
  112. break;
  113. case CMD_FORWARDLIGHTS:
  114. if (element.HasAttribute("uselitbase"))
  115. useLitBase_ = element.GetBool("uselitbase");
  116. break;
  117. case CMD_LIGHTVOLUMES:
  118. case CMD_QUAD:
  119. vertexShaderName_ = element.GetAttribute("vs");
  120. pixelShaderName_ = element.GetAttribute("ps");
  121. if (type_ == CMD_QUAD)
  122. {
  123. XMLElement parameterElem = element.GetChild("parameter");
  124. while (parameterElem)
  125. {
  126. String name = parameterElem.GetAttribute("name");
  127. Vector4 value = parameterElem.GetVector("value");
  128. shaderParameters_[StringHash(name)] = value;
  129. parameterElem = parameterElem.GetNext("parameter");
  130. }
  131. }
  132. break;
  133. default:
  134. break;
  135. }
  136. // By default use 1 output, which is the viewport
  137. outputNames_.Push("viewport");
  138. if (element.HasAttribute("output"))
  139. outputNames_[0] = element.GetAttribute("output");
  140. // Check for defining multiple outputs
  141. XMLElement outputElem = element.GetChild("output");
  142. while (outputElem)
  143. {
  144. unsigned index = outputElem.GetInt("index");
  145. if (index < MAX_RENDERTARGETS)
  146. {
  147. if (index >= outputNames_.Size())
  148. outputNames_.Resize(index + 1);
  149. outputNames_[index] = outputElem.GetAttribute("name");
  150. }
  151. outputElem = outputElem.GetNext("output");
  152. }
  153. XMLElement textureElem = element.GetChild("texture");
  154. while (textureElem)
  155. {
  156. TextureUnit unit = TU_DIFFUSE;
  157. if (textureElem.HasAttribute("unit"))
  158. {
  159. String unitName = textureElem.GetAttributeLower("unit");
  160. if (unitName.Length() > 1)
  161. unit = ParseTextureUnitName(unitName);
  162. else
  163. unit = (TextureUnit)Clamp(ToInt(unitName), 0, MAX_TEXTURE_UNITS - 1);
  164. }
  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 Vector4& value)
  179. {
  180. shaderParameters_[StringHash(name)] = value;
  181. }
  182. void RenderPathCommand::RemoveShaderParameter(const String& name)
  183. {
  184. shaderParameters_.Erase(StringHash(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 Vector4& RenderPathCommand::GetShaderParameter(const String& name) const
  203. {
  204. HashMap<StringHash, Vector4>::ConstIterator i = shaderParameters_.Find(StringHash(name));
  205. return i != shaderParameters_.End() ? i->second_ : Vector4::ZERO;
  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::LoadParameters(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.LoadParameters(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.LoadParameters(cmdElem);
  251. if (cmd.type_ != CMD_NONE)
  252. commands_.Push(cmd);
  253. cmdElem = cmdElem.GetNext("command");
  254. }
  255. return true;
  256. }
  257. void RenderPath::SetActive(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].active_ = active;
  263. }
  264. for (unsigned i = 0; i < commands_.Size(); ++i)
  265. {
  266. if (!commands_[i].tag_.Compare(tag, false))
  267. commands_[i].active_ = active;
  268. }
  269. }
  270. void RenderPath::ToggleActive(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].active_ = !renderTargets_[i].active_;
  276. }
  277. for (unsigned i = 0; i < commands_.Size(); ++i)
  278. {
  279. if (!commands_[i].tag_.Compare(tag, false))
  280. commands_[i].active_ = !commands_[i].active_;
  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 Vector4& value)
  345. {
  346. StringHash nameHash(name);
  347. for (unsigned i = 0; i < commands_.Size(); ++i)
  348. {
  349. HashMap<StringHash, Vector4>::Iterator j = commands_[i].shaderParameters_.Find(nameHash);
  350. if (j != commands_[i].shaderParameters_.End())
  351. j->second_ = value;
  352. }
  353. }
  354. const Vector4& RenderPath::GetShaderParameter(const String& name) const
  355. {
  356. StringHash nameHash(name);
  357. for (unsigned i = 0; i < commands_.Size(); ++i)
  358. {
  359. HashMap<StringHash, Vector4>::ConstIterator j = commands_[i].shaderParameters_.Find(nameHash);
  360. if (j != commands_[i].shaderParameters_.End())
  361. return j->second_;
  362. }
  363. return Vector4::ZERO;
  364. }
  365. }