RenderPath.cpp 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524
  1. //
  2. // Copyright (c) 2008-2020 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() = default;
  267. RenderPath::~RenderPath() = default;
  268. SharedPtr<RenderPath> RenderPath::Clone()
  269. {
  270. SharedPtr<RenderPath> newRenderPath(new RenderPath());
  271. newRenderPath->renderTargets_ = renderTargets_;
  272. newRenderPath->commands_ = commands_;
  273. return newRenderPath;
  274. }
  275. bool RenderPath::Load(XMLFile* file)
  276. {
  277. renderTargets_.Clear();
  278. commands_.Clear();
  279. return Append(file);
  280. }
  281. bool RenderPath::Append(XMLFile* file)
  282. {
  283. if (!file)
  284. return false;
  285. XMLElement rootElem = file->GetRoot();
  286. if (!rootElem)
  287. return false;
  288. XMLElement rtElem = rootElem.GetChild("rendertarget");
  289. while (rtElem)
  290. {
  291. RenderTargetInfo info;
  292. info.Load(rtElem);
  293. if (!info.name_.Trimmed().Empty())
  294. renderTargets_.Push(info);
  295. rtElem = rtElem.GetNext("rendertarget");
  296. }
  297. XMLElement cmdElem = rootElem.GetChild("command");
  298. while (cmdElem)
  299. {
  300. RenderPathCommand cmd;
  301. cmd.Load(cmdElem);
  302. if (cmd.type_ != CMD_NONE)
  303. commands_.Push(cmd);
  304. cmdElem = cmdElem.GetNext("command");
  305. }
  306. return true;
  307. }
  308. void RenderPath::SetEnabled(const String& tag, bool active)
  309. {
  310. for (unsigned i = 0; i < renderTargets_.Size(); ++i)
  311. {
  312. if (!renderTargets_[i].tag_.Compare(tag, false))
  313. renderTargets_[i].enabled_ = active;
  314. }
  315. for (unsigned i = 0; i < commands_.Size(); ++i)
  316. {
  317. if (!commands_[i].tag_.Compare(tag, false))
  318. commands_[i].enabled_ = active;
  319. }
  320. }
  321. bool RenderPath::IsEnabled(const String& tag) const
  322. {
  323. for (unsigned i = 0; i < renderTargets_.Size(); ++i)
  324. {
  325. if (!renderTargets_[i].tag_.Compare(tag, false) && renderTargets_[i].enabled_)
  326. return true;
  327. }
  328. for (unsigned i = 0; i < commands_.Size(); ++i)
  329. {
  330. if (!commands_[i].tag_.Compare(tag, false) && commands_[i].enabled_)
  331. return true;
  332. }
  333. return false;
  334. }
  335. bool RenderPath::IsAdded(const String& tag) const
  336. {
  337. for (unsigned i = 0; i < renderTargets_.Size(); ++i)
  338. {
  339. if (!renderTargets_[i].tag_.Compare(tag, false))
  340. return true;
  341. }
  342. for (unsigned i = 0; i < commands_.Size(); ++i)
  343. {
  344. if (!commands_[i].tag_.Compare(tag, false))
  345. return true;
  346. }
  347. return false;
  348. }
  349. void RenderPath::ToggleEnabled(const String& tag)
  350. {
  351. for (unsigned i = 0; i < renderTargets_.Size(); ++i)
  352. {
  353. if (!renderTargets_[i].tag_.Compare(tag, false))
  354. renderTargets_[i].enabled_ = !renderTargets_[i].enabled_;
  355. }
  356. for (unsigned i = 0; i < commands_.Size(); ++i)
  357. {
  358. if (!commands_[i].tag_.Compare(tag, false))
  359. commands_[i].enabled_ = !commands_[i].enabled_;
  360. }
  361. }
  362. void RenderPath::SetRenderTarget(unsigned index, const RenderTargetInfo& info)
  363. {
  364. if (index < renderTargets_.Size())
  365. renderTargets_[index] = info;
  366. else if (index == renderTargets_.Size())
  367. AddRenderTarget(info);
  368. }
  369. void RenderPath::AddRenderTarget(const RenderTargetInfo& info)
  370. {
  371. renderTargets_.Push(info);
  372. }
  373. void RenderPath::RemoveRenderTarget(unsigned index)
  374. {
  375. renderTargets_.Erase(index);
  376. }
  377. void RenderPath::RemoveRenderTarget(const String& name)
  378. {
  379. for (unsigned i = 0; i < renderTargets_.Size(); ++i)
  380. {
  381. if (!renderTargets_[i].name_.Compare(name, false))
  382. {
  383. renderTargets_.Erase(i);
  384. return;
  385. }
  386. }
  387. }
  388. void RenderPath::RemoveRenderTargets(const String& tag)
  389. {
  390. for (unsigned i = renderTargets_.Size() - 1; i < renderTargets_.Size(); --i)
  391. {
  392. if (!renderTargets_[i].tag_.Compare(tag, false))
  393. renderTargets_.Erase(i);
  394. }
  395. }
  396. void RenderPath::SetCommand(unsigned index, const RenderPathCommand& command)
  397. {
  398. if (index < commands_.Size())
  399. commands_[index] = command;
  400. else if (index == commands_.Size())
  401. AddCommand(command);
  402. }
  403. void RenderPath::AddCommand(const RenderPathCommand& command)
  404. {
  405. commands_.Push(command);
  406. }
  407. void RenderPath::InsertCommand(unsigned index, const RenderPathCommand& command)
  408. {
  409. commands_.Insert(index, command);
  410. }
  411. void RenderPath::RemoveCommand(unsigned index)
  412. {
  413. commands_.Erase(index);
  414. }
  415. void RenderPath::RemoveCommands(const String& tag)
  416. {
  417. for (unsigned i = commands_.Size() - 1; i < commands_.Size(); --i)
  418. {
  419. if (!commands_[i].tag_.Compare(tag, false))
  420. commands_.Erase(i);
  421. }
  422. }
  423. void RenderPath::SetShaderParameter(const String& name, const Variant& value)
  424. {
  425. StringHash nameHash(name);
  426. for (unsigned i = 0; i < commands_.Size(); ++i)
  427. {
  428. HashMap<StringHash, Variant>::Iterator j = commands_[i].shaderParameters_.Find(nameHash);
  429. if (j != commands_[i].shaderParameters_.End())
  430. j->second_ = value;
  431. }
  432. }
  433. const Variant& RenderPath::GetShaderParameter(const String& name) const
  434. {
  435. StringHash nameHash(name);
  436. for (unsigned i = 0; i < commands_.Size(); ++i)
  437. {
  438. HashMap<StringHash, Variant>::ConstIterator j = commands_[i].shaderParameters_.Find(nameHash);
  439. if (j != commands_[i].shaderParameters_.End())
  440. return j->second_;
  441. }
  442. return Variant::EMPTY;
  443. }
  444. }