RenderPath.cpp 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418
  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_LIGHTVOLUMES:
  114. case CMD_QUAD:
  115. vertexShaderName_ = element.GetAttribute("vs");
  116. pixelShaderName_ = element.GetAttribute("ps");
  117. if (type_ == CMD_QUAD)
  118. {
  119. XMLElement parameterElem = element.GetChild("parameter");
  120. while (parameterElem)
  121. {
  122. String name = parameterElem.GetAttribute("name");
  123. Vector4 value = parameterElem.GetVector("value");
  124. shaderParameters_[StringHash(name)] = value;
  125. parameterElem = parameterElem.GetNext("parameter");
  126. }
  127. }
  128. break;
  129. }
  130. // By default use 1 output, which is the viewport
  131. outputNames_.Push("viewport");
  132. if (element.HasAttribute("output"))
  133. outputNames_[0] = element.GetAttribute("output");
  134. // Check for defining multiple outputs
  135. XMLElement outputElem = element.GetChild("output");
  136. while (outputElem)
  137. {
  138. unsigned index = outputElem.GetInt("index");
  139. if (index < MAX_RENDERTARGETS)
  140. {
  141. if (index >= outputNames_.Size())
  142. outputNames_.Resize(index + 1);
  143. outputNames_[index] = outputElem.GetAttribute("name");
  144. }
  145. outputElem = outputElem.GetNext("output");
  146. }
  147. XMLElement textureElem = element.GetChild("texture");
  148. while (textureElem)
  149. {
  150. TextureUnit unit = TU_DIFFUSE;
  151. if (textureElem.HasAttribute("unit"))
  152. {
  153. String unitName = textureElem.GetAttributeLower("unit");
  154. if (unitName.Length() > 1)
  155. unit = ParseTextureUnitName(unitName);
  156. else
  157. unit = (TextureUnit)Clamp(ToInt(unitName), 0, MAX_TEXTURE_UNITS - 1);
  158. }
  159. if (unit < MAX_TEXTURE_UNITS)
  160. {
  161. String name = textureElem.GetAttribute("name");
  162. textureNames_[unit] = name;
  163. }
  164. textureElem = textureElem.GetNext("texture");
  165. }
  166. }
  167. void RenderPathCommand::SetTextureName(TextureUnit unit, const String& name)
  168. {
  169. if (unit < MAX_TEXTURE_UNITS)
  170. textureNames_[unit] = name;
  171. }
  172. void RenderPathCommand::SetShaderParameter(const String& name, const Vector4& value)
  173. {
  174. shaderParameters_[StringHash(name)] = value;
  175. }
  176. void RenderPathCommand::RemoveShaderParameter(const String& name)
  177. {
  178. shaderParameters_.Erase(StringHash(name));
  179. }
  180. void RenderPathCommand::SetNumOutputs(unsigned num)
  181. {
  182. num = Clamp((int)num, 1, MAX_RENDERTARGETS);
  183. outputNames_.Resize(num);
  184. }
  185. void RenderPathCommand::SetOutputName(unsigned index, const String& name)
  186. {
  187. if (index < outputNames_.Size())
  188. outputNames_[index] = name;
  189. else if (index == outputNames_.Size() && index < MAX_RENDERTARGETS)
  190. outputNames_.Push(name);
  191. }
  192. const String& RenderPathCommand::GetTextureName(TextureUnit unit) const
  193. {
  194. return unit < MAX_TEXTURE_UNITS ? textureNames_[unit] : String::EMPTY;
  195. }
  196. const Vector4& RenderPathCommand::GetShaderParameter(const String& name) const
  197. {
  198. HashMap<StringHash, Vector4>::ConstIterator i = shaderParameters_.Find(StringHash(name));
  199. return i != shaderParameters_.End() ? i->second_ : Vector4::ZERO;
  200. }
  201. const String& RenderPathCommand::GetOutputName(unsigned index) const
  202. {
  203. return index < outputNames_.Size() ? outputNames_[index] : String::EMPTY;
  204. }
  205. RenderPath::RenderPath()
  206. {
  207. }
  208. RenderPath::~RenderPath()
  209. {
  210. }
  211. SharedPtr<RenderPath> RenderPath::Clone()
  212. {
  213. SharedPtr<RenderPath> newRenderPath(new RenderPath());
  214. newRenderPath->renderTargets_ = renderTargets_;
  215. newRenderPath->commands_ = commands_;
  216. return newRenderPath;
  217. }
  218. bool RenderPath::LoadParameters(XMLFile* file)
  219. {
  220. renderTargets_.Clear();
  221. commands_.Clear();
  222. return Append(file);
  223. }
  224. bool RenderPath::Append(XMLFile* file)
  225. {
  226. if (!file)
  227. return false;
  228. XMLElement rootElem = file->GetRoot();
  229. if (!rootElem)
  230. return false;
  231. XMLElement rtElem = rootElem.GetChild("rendertarget");
  232. while (rtElem)
  233. {
  234. RenderTargetInfo info;
  235. info.LoadParameters(rtElem);
  236. if (!info.name_.Trimmed().Empty())
  237. renderTargets_.Push(info);
  238. rtElem = rtElem.GetNext("rendertarget");
  239. }
  240. XMLElement cmdElem = rootElem.GetChild("command");
  241. while (cmdElem)
  242. {
  243. RenderPathCommand cmd;
  244. cmd.LoadParameters(cmdElem);
  245. if (cmd.type_ != CMD_NONE)
  246. commands_.Push(cmd);
  247. cmdElem = cmdElem.GetNext("command");
  248. }
  249. return true;
  250. }
  251. void RenderPath::SetActive(const String& tag, bool active)
  252. {
  253. for (unsigned i = 0; i < renderTargets_.Size(); ++i)
  254. {
  255. if (!renderTargets_[i].tag_.Compare(tag, false))
  256. renderTargets_[i].active_ = active;
  257. }
  258. for (unsigned i = 0; i < commands_.Size(); ++i)
  259. {
  260. if (!commands_[i].tag_.Compare(tag, false))
  261. commands_[i].active_ = active;
  262. }
  263. }
  264. void RenderPath::ToggleActive(const String& tag)
  265. {
  266. for (unsigned i = 0; i < renderTargets_.Size(); ++i)
  267. {
  268. if (!renderTargets_[i].tag_.Compare(tag, false))
  269. renderTargets_[i].active_ = !renderTargets_[i].active_;
  270. }
  271. for (unsigned i = 0; i < commands_.Size(); ++i)
  272. {
  273. if (!commands_[i].tag_.Compare(tag, false))
  274. commands_[i].active_ = !commands_[i].active_;
  275. }
  276. }
  277. void RenderPath::SetRenderTarget(unsigned index, const RenderTargetInfo& info)
  278. {
  279. if (index < renderTargets_.Size())
  280. renderTargets_[index] = info;
  281. else if (index == renderTargets_.Size())
  282. AddRenderTarget(info);
  283. }
  284. void RenderPath::AddRenderTarget(const RenderTargetInfo& info)
  285. {
  286. renderTargets_.Push(info);
  287. }
  288. void RenderPath::RemoveRenderTarget(unsigned index)
  289. {
  290. renderTargets_.Erase(index);
  291. }
  292. void RenderPath::RemoveRenderTarget(const String& name)
  293. {
  294. for (unsigned i = 0; i < renderTargets_.Size(); ++i)
  295. {
  296. if (!renderTargets_[i].name_.Compare(name, false))
  297. {
  298. renderTargets_.Erase(i);
  299. return;
  300. }
  301. }
  302. }
  303. void RenderPath::RemoveRenderTargets(const String& tag)
  304. {
  305. for (unsigned i = renderTargets_.Size() - 1; i < renderTargets_.Size(); --i)
  306. {
  307. if (!renderTargets_[i].tag_.Compare(tag, false))
  308. renderTargets_.Erase(i);
  309. }
  310. }
  311. void RenderPath::SetCommand(unsigned index, const RenderPathCommand& command)
  312. {
  313. if (index < commands_.Size())
  314. commands_[index] = command;
  315. else if (index == commands_.Size())
  316. AddCommand(command);
  317. }
  318. void RenderPath::AddCommand(const RenderPathCommand& command)
  319. {
  320. commands_.Push(command);
  321. }
  322. void RenderPath::InsertCommand(unsigned index, const RenderPathCommand& command)
  323. {
  324. commands_.Insert(index, command);
  325. }
  326. void RenderPath::RemoveCommand(unsigned index)
  327. {
  328. commands_.Erase(index);
  329. }
  330. void RenderPath::RemoveCommands(const String& tag)
  331. {
  332. for (unsigned i = commands_.Size() - 1; i < commands_.Size(); --i)
  333. {
  334. if (!commands_[i].tag_.Compare(tag, false))
  335. commands_.Erase(i);
  336. }
  337. }
  338. void RenderPath::SetShaderParameter(const String& name, const Vector4& value)
  339. {
  340. StringHash nameHash(name);
  341. for (unsigned i = 0; i < commands_.Size(); ++i)
  342. {
  343. HashMap<StringHash, Vector4>::Iterator j = commands_[i].shaderParameters_.Find(nameHash);
  344. if (j != commands_[i].shaderParameters_.End())
  345. j->second_ = value;
  346. }
  347. }
  348. const Vector4& RenderPath::GetShaderParameter(const String& name) const
  349. {
  350. StringHash nameHash(name);
  351. for (unsigned i = 0; i < commands_.Size(); ++i)
  352. {
  353. HashMap<StringHash, Vector4>::ConstIterator j = commands_[i].shaderParameters_.Find(nameHash);
  354. if (j != commands_[i].shaderParameters_.End())
  355. return j->second_;
  356. }
  357. return Vector4::ZERO;
  358. }
  359. }