RenderPath.cpp 15 KB

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