RenderPath.cpp 15 KB

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