RenderPath.cpp 12 KB

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