RenderPath.cpp 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490
  1. //
  2. // Copyright (c) 2008-2015 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/Graphics.h"
  24. #include "../Graphics/Material.h"
  25. #include "../Graphics/RenderPath.h"
  26. #include "../IO/Log.h"
  27. #include "../Resource/XMLFile.h"
  28. #include "../DebugNew.h"
  29. #ifdef _MSC_VER
  30. #pragma warning(disable:6293)
  31. #endif
  32. namespace Atomic
  33. {
  34. static const char* commandTypeNames[] =
  35. {
  36. "none",
  37. "clear",
  38. "scenepass",
  39. "quad",
  40. "forwardlights",
  41. "lightvolumes",
  42. "renderui",
  43. "sendevent",
  44. 0
  45. };
  46. static const char* sortModeNames[] =
  47. {
  48. "fronttoback",
  49. "backtofront",
  50. 0
  51. };
  52. extern const char* blendModeNames[];
  53. TextureUnit ParseTextureUnitName(String name);
  54. void RenderTargetInfo::Load(const XMLElement& element)
  55. {
  56. name_ = element.GetAttribute("name");
  57. tag_ = element.GetAttribute("tag");
  58. if (element.HasAttribute("enabled"))
  59. enabled_ = element.GetBool("enabled");
  60. if (element.HasAttribute("cubemap"))
  61. cubemap_ = element.GetBool("cubemap");
  62. String formatName = element.GetAttribute("format");
  63. format_ = Graphics::GetFormat(formatName);
  64. if (element.HasAttribute("filter"))
  65. filtered_ = element.GetBool("filter");
  66. if (element.HasAttribute("srgb"))
  67. sRGB_ = element.GetBool("srgb");
  68. if (element.HasAttribute("persistent"))
  69. persistent_ = element.GetBool("persistent");
  70. if (element.HasAttribute("size"))
  71. size_ = element.GetVector2("size");
  72. if (element.HasAttribute("sizedivisor"))
  73. {
  74. size_ = element.GetVector2("sizedivisor");
  75. sizeMode_ = SIZE_VIEWPORTDIVISOR;
  76. }
  77. else if (element.HasAttribute("rtsizedivisor"))
  78. {
  79. // Deprecated rtsizedivisor mode, acts the same as sizedivisor mode now
  80. LOGWARNING("Deprecated rtsizedivisor mode used in rendertarget definition");
  81. size_ = element.GetVector2("rtsizedivisor");
  82. sizeMode_ = SIZE_VIEWPORTDIVISOR;
  83. }
  84. else if (element.HasAttribute("sizemultiplier"))
  85. {
  86. size_ = element.GetVector2("sizemultiplier");
  87. sizeMode_ = SIZE_VIEWPORTMULTIPLIER;
  88. }
  89. if (element.HasAttribute("width"))
  90. size_.x_ = element.GetFloat("width");
  91. if (element.HasAttribute("height"))
  92. size_.y_ = element.GetFloat("height");
  93. }
  94. void RenderPathCommand::Load(const XMLElement& element)
  95. {
  96. type_ = (RenderCommandType)GetStringListIndex(element.GetAttributeLower("type").CString(), commandTypeNames, CMD_NONE);
  97. tag_ = element.GetAttribute("tag");
  98. if (element.HasAttribute("enabled"))
  99. enabled_ = element.GetBool("enabled");
  100. if (element.HasAttribute("metadata"))
  101. metadata_ = element.GetAttribute("metadata");
  102. switch (type_)
  103. {
  104. case CMD_CLEAR:
  105. if (element.HasAttribute("color"))
  106. {
  107. clearFlags_ |= CLEAR_COLOR;
  108. if (element.GetAttributeLower("color") == "fog")
  109. useFogColor_ = true;
  110. else
  111. clearColor_ = element.GetColor("color");
  112. }
  113. if (element.HasAttribute("depth"))
  114. {
  115. clearFlags_ |= CLEAR_DEPTH;
  116. clearDepth_ = element.GetFloat("depth");
  117. }
  118. if (element.HasAttribute("stencil"))
  119. {
  120. clearFlags_ |= CLEAR_STENCIL;
  121. clearStencil_ = (unsigned)element.GetInt("stencil");
  122. }
  123. break;
  124. case CMD_SCENEPASS:
  125. pass_ = element.GetAttribute("pass");
  126. sortMode_ =
  127. (RenderCommandSortMode)GetStringListIndex(element.GetAttributeLower("sort").CString(), sortModeNames, SORT_FRONTTOBACK);
  128. if (element.HasAttribute("marktostencil"))
  129. markToStencil_ = element.GetBool("marktostencil");
  130. if (element.HasAttribute("vertexlights"))
  131. vertexLights_ = element.GetBool("vertexlights");
  132. break;
  133. case CMD_FORWARDLIGHTS:
  134. pass_ = element.GetAttribute("pass");
  135. if (element.HasAttribute("uselitbase"))
  136. useLitBase_ = element.GetBool("uselitbase");
  137. break;
  138. case CMD_LIGHTVOLUMES:
  139. case CMD_QUAD:
  140. vertexShaderName_ = element.GetAttribute("vs");
  141. pixelShaderName_ = element.GetAttribute("ps");
  142. vertexShaderDefines_ = element.GetAttribute("vsdefines");
  143. pixelShaderDefines_ = element.GetAttribute("psdefines");
  144. if (type_ == CMD_QUAD)
  145. {
  146. if (element.HasAttribute("blend"))
  147. {
  148. String blend = element.GetAttributeLower("blend");
  149. blendMode_ = ((BlendMode)GetStringListIndex(blend.CString(), blendModeNames, BLEND_REPLACE));
  150. }
  151. XMLElement parameterElem = element.GetChild("parameter");
  152. while (parameterElem)
  153. {
  154. String name = parameterElem.GetAttribute("name");
  155. shaderParameters_[name] = Material::ParseShaderParameterValue(parameterElem.GetAttribute("value"));
  156. parameterElem = parameterElem.GetNext("parameter");
  157. }
  158. }
  159. break;
  160. case CMD_SENDEVENT:
  161. eventName_ = element.GetAttribute("name");
  162. break;
  163. default:
  164. break;
  165. }
  166. // By default use 1 output, which is the viewport
  167. outputs_.Resize(1);
  168. outputs_[0] = MakePair(String("viewport"), FACE_POSITIVE_X);
  169. if (element.HasAttribute("output"))
  170. outputs_[0].first_ = element.GetAttribute("output");
  171. if (element.HasAttribute("face"))
  172. outputs_[0].second_ = (CubeMapFace)element.GetInt("face");
  173. if (element.HasAttribute("depthstencil"))
  174. depthStencilName_ = element.GetAttribute("depthstencil");
  175. // Check for defining multiple outputs
  176. XMLElement outputElem = element.GetChild("output");
  177. while (outputElem)
  178. {
  179. unsigned index = (unsigned)outputElem.GetInt("index");
  180. if (index < MAX_RENDERTARGETS)
  181. {
  182. if (index >= outputs_.Size())
  183. outputs_.Resize(index + 1);
  184. outputs_[index].first_ = outputElem.GetAttribute("name");
  185. outputs_[index].second_ = outputElem.HasAttribute("face") ? (CubeMapFace)outputElem.GetInt("face") : FACE_POSITIVE_X;
  186. }
  187. outputElem = outputElem.GetNext("output");
  188. }
  189. XMLElement textureElem = element.GetChild("texture");
  190. while (textureElem)
  191. {
  192. TextureUnit unit = TU_DIFFUSE;
  193. if (textureElem.HasAttribute("unit"))
  194. unit = ParseTextureUnitName(textureElem.GetAttribute("unit"));
  195. if (unit < MAX_TEXTURE_UNITS)
  196. {
  197. String name = textureElem.GetAttribute("name");
  198. textureNames_[unit] = name;
  199. }
  200. textureElem = textureElem.GetNext("texture");
  201. }
  202. }
  203. void RenderPathCommand::SetTextureName(TextureUnit unit, const String& name)
  204. {
  205. if (unit < MAX_TEXTURE_UNITS)
  206. textureNames_[unit] = name;
  207. }
  208. void RenderPathCommand::SetShaderParameter(const String& name, const Variant& value)
  209. {
  210. shaderParameters_[name] = value;
  211. }
  212. void RenderPathCommand::RemoveShaderParameter(const String& name)
  213. {
  214. shaderParameters_.Erase(name);
  215. }
  216. void RenderPathCommand::SetNumOutputs(unsigned num)
  217. {
  218. num = (unsigned)Clamp((int)num, 1, MAX_RENDERTARGETS);
  219. outputs_.Resize(num);
  220. }
  221. void RenderPathCommand::SetOutput(unsigned index, const String& name, CubeMapFace face)
  222. {
  223. if (index < outputs_.Size())
  224. outputs_[index] = MakePair(name, face);
  225. else if (index == outputs_.Size() && index < MAX_RENDERTARGETS)
  226. outputs_.Push(MakePair(name, face));
  227. }
  228. void RenderPathCommand::SetOutputName(unsigned index, const String& name)
  229. {
  230. if (index < outputs_.Size())
  231. outputs_[index].first_ = name;
  232. else if (index == outputs_.Size() && index < MAX_RENDERTARGETS)
  233. outputs_.Push(MakePair(name, FACE_POSITIVE_X));
  234. }
  235. void RenderPathCommand::SetOutputFace(unsigned index, CubeMapFace face)
  236. {
  237. if (index < outputs_.Size())
  238. outputs_[index].second_ = face;
  239. else if (index == outputs_.Size() && index < MAX_RENDERTARGETS)
  240. outputs_.Push(MakePair(String::EMPTY, face));
  241. }
  242. void RenderPathCommand::SetDepthStencilName(const String& name)
  243. {
  244. depthStencilName_ = name;
  245. }
  246. const String& RenderPathCommand::GetTextureName(TextureUnit unit) const
  247. {
  248. return unit < MAX_TEXTURE_UNITS ? textureNames_[unit] : String::EMPTY;
  249. }
  250. const Variant& RenderPathCommand::GetShaderParameter(const String& name) const
  251. {
  252. HashMap<StringHash, Variant>::ConstIterator i = shaderParameters_.Find(name);
  253. return i != shaderParameters_.End() ? i->second_ : Variant::EMPTY;
  254. }
  255. const String& RenderPathCommand::GetOutputName(unsigned index) const
  256. {
  257. return index < outputs_.Size() ? outputs_[index].first_ : String::EMPTY;
  258. }
  259. CubeMapFace RenderPathCommand::GetOutputFace(unsigned index) const
  260. {
  261. return index < outputs_.Size() ? outputs_[index].second_ : FACE_POSITIVE_X;
  262. }
  263. RenderPath::RenderPath()
  264. {
  265. }
  266. RenderPath::~RenderPath()
  267. {
  268. }
  269. SharedPtr<RenderPath> RenderPath::Clone()
  270. {
  271. SharedPtr<RenderPath> newRenderPath(new RenderPath());
  272. newRenderPath->renderTargets_ = renderTargets_;
  273. newRenderPath->commands_ = commands_;
  274. return newRenderPath;
  275. }
  276. bool RenderPath::Load(XMLFile* file)
  277. {
  278. renderTargets_.Clear();
  279. commands_.Clear();
  280. return Append(file);
  281. }
  282. bool RenderPath::Append(XMLFile* file)
  283. {
  284. if (!file)
  285. return false;
  286. XMLElement rootElem = file->GetRoot();
  287. if (!rootElem)
  288. return false;
  289. XMLElement rtElem = rootElem.GetChild("rendertarget");
  290. while (rtElem)
  291. {
  292. RenderTargetInfo info;
  293. info.Load(rtElem);
  294. if (!info.name_.Trimmed().Empty())
  295. renderTargets_.Push(info);
  296. rtElem = rtElem.GetNext("rendertarget");
  297. }
  298. XMLElement cmdElem = rootElem.GetChild("command");
  299. while (cmdElem)
  300. {
  301. RenderPathCommand cmd;
  302. cmd.Load(cmdElem);
  303. if (cmd.type_ != CMD_NONE)
  304. commands_.Push(cmd);
  305. cmdElem = cmdElem.GetNext("command");
  306. }
  307. return true;
  308. }
  309. void RenderPath::SetEnabled(const String& tag, bool active)
  310. {
  311. for (unsigned i = 0; i < renderTargets_.Size(); ++i)
  312. {
  313. if (!renderTargets_[i].tag_.Compare(tag, false))
  314. renderTargets_[i].enabled_ = active;
  315. }
  316. for (unsigned i = 0; i < commands_.Size(); ++i)
  317. {
  318. if (!commands_[i].tag_.Compare(tag, false))
  319. commands_[i].enabled_ = active;
  320. }
  321. }
  322. void RenderPath::ToggleEnabled(const String& tag)
  323. {
  324. for (unsigned i = 0; i < renderTargets_.Size(); ++i)
  325. {
  326. if (!renderTargets_[i].tag_.Compare(tag, false))
  327. renderTargets_[i].enabled_ = !renderTargets_[i].enabled_;
  328. }
  329. for (unsigned i = 0; i < commands_.Size(); ++i)
  330. {
  331. if (!commands_[i].tag_.Compare(tag, false))
  332. commands_[i].enabled_ = !commands_[i].enabled_;
  333. }
  334. }
  335. void RenderPath::SetRenderTarget(unsigned index, const RenderTargetInfo& info)
  336. {
  337. if (index < renderTargets_.Size())
  338. renderTargets_[index] = info;
  339. else if (index == renderTargets_.Size())
  340. AddRenderTarget(info);
  341. }
  342. void RenderPath::AddRenderTarget(const RenderTargetInfo& info)
  343. {
  344. renderTargets_.Push(info);
  345. }
  346. void RenderPath::RemoveRenderTarget(unsigned index)
  347. {
  348. renderTargets_.Erase(index);
  349. }
  350. void RenderPath::RemoveRenderTarget(const String& name)
  351. {
  352. for (unsigned i = 0; i < renderTargets_.Size(); ++i)
  353. {
  354. if (!renderTargets_[i].name_.Compare(name, false))
  355. {
  356. renderTargets_.Erase(i);
  357. return;
  358. }
  359. }
  360. }
  361. void RenderPath::RemoveRenderTargets(const String& tag)
  362. {
  363. for (unsigned i = renderTargets_.Size() - 1; i < renderTargets_.Size(); --i)
  364. {
  365. if (!renderTargets_[i].tag_.Compare(tag, false))
  366. renderTargets_.Erase(i);
  367. }
  368. }
  369. void RenderPath::SetCommand(unsigned index, const RenderPathCommand& command)
  370. {
  371. if (index < commands_.Size())
  372. commands_[index] = command;
  373. else if (index == commands_.Size())
  374. AddCommand(command);
  375. }
  376. void RenderPath::AddCommand(const RenderPathCommand& command)
  377. {
  378. commands_.Push(command);
  379. }
  380. void RenderPath::InsertCommand(unsigned index, const RenderPathCommand& command)
  381. {
  382. commands_.Insert(index, command);
  383. }
  384. void RenderPath::RemoveCommand(unsigned index)
  385. {
  386. commands_.Erase(index);
  387. }
  388. void RenderPath::RemoveCommands(const String& tag)
  389. {
  390. for (unsigned i = commands_.Size() - 1; i < commands_.Size(); --i)
  391. {
  392. if (!commands_[i].tag_.Compare(tag, false))
  393. commands_.Erase(i);
  394. }
  395. }
  396. void RenderPath::SetShaderParameter(const String& name, const Variant& value)
  397. {
  398. StringHash nameHash(name);
  399. for (unsigned i = 0; i < commands_.Size(); ++i)
  400. {
  401. HashMap<StringHash, Variant>::Iterator j = commands_[i].shaderParameters_.Find(nameHash);
  402. if (j != commands_[i].shaderParameters_.End())
  403. j->second_ = value;
  404. }
  405. }
  406. const Variant& RenderPath::GetShaderParameter(const String& name) const
  407. {
  408. StringHash nameHash(name);
  409. for (unsigned i = 0; i < commands_.Size(); ++i)
  410. {
  411. HashMap<StringHash, Variant>::ConstIterator j = commands_[i].shaderParameters_.Find(nameHash);
  412. if (j != commands_[i].shaderParameters_.End())
  413. return j->second_;
  414. }
  415. return Variant::EMPTY;
  416. }
  417. }