RenderPath.cpp 12 KB

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