Renderer.cpp 56 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803
  1. //
  2. // Copyright (c) 2008-2014 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 "Camera.h"
  24. #include "CoreEvents.h"
  25. #include "DebugRenderer.h"
  26. #include "Geometry.h"
  27. #include "Graphics.h"
  28. #include "GraphicsEvents.h"
  29. #include "GraphicsImpl.h"
  30. #include "IndexBuffer.h"
  31. #include "Log.h"
  32. #include "Material.h"
  33. #include "OcclusionBuffer.h"
  34. #include "Octree.h"
  35. #include "Profiler.h"
  36. #include "Renderer.h"
  37. #include "RenderPath.h"
  38. #include "ResourceCache.h"
  39. #include "Scene.h"
  40. #include "Shader.h"
  41. #include "ShaderVariation.h"
  42. #include "Technique.h"
  43. #include "Texture2D.h"
  44. #include "TextureCube.h"
  45. #include "VertexBuffer.h"
  46. #include "View.h"
  47. #include "XMLFile.h"
  48. #include "Zone.h"
  49. #include "DebugNew.h"
  50. namespace Urho3D
  51. {
  52. static const float dirLightVertexData[] =
  53. {
  54. -1, 1, 0,
  55. 1, 1, 0,
  56. 1, -1, 0,
  57. -1, -1, 0,
  58. };
  59. static const unsigned short dirLightIndexData[] =
  60. {
  61. 0, 1, 2,
  62. 2, 3, 0,
  63. };
  64. static const float pointLightVertexData[] =
  65. {
  66. -0.423169f, -1.000000f, 0.423169f,
  67. -0.423169f, -1.000000f, -0.423169f,
  68. 0.423169f, -1.000000f, -0.423169f,
  69. 0.423169f, -1.000000f, 0.423169f,
  70. 0.423169f, 1.000000f, -0.423169f,
  71. -0.423169f, 1.000000f, -0.423169f,
  72. -0.423169f, 1.000000f, 0.423169f,
  73. 0.423169f, 1.000000f, 0.423169f,
  74. -1.000000f, 0.423169f, -0.423169f,
  75. -1.000000f, -0.423169f, -0.423169f,
  76. -1.000000f, -0.423169f, 0.423169f,
  77. -1.000000f, 0.423169f, 0.423169f,
  78. 0.423169f, 0.423169f, -1.000000f,
  79. 0.423169f, -0.423169f, -1.000000f,
  80. -0.423169f, -0.423169f, -1.000000f,
  81. -0.423169f, 0.423169f, -1.000000f,
  82. 1.000000f, 0.423169f, 0.423169f,
  83. 1.000000f, -0.423169f, 0.423169f,
  84. 1.000000f, -0.423169f, -0.423169f,
  85. 1.000000f, 0.423169f, -0.423169f,
  86. 0.423169f, -0.423169f, 1.000000f,
  87. 0.423169f, 0.423169f, 1.000000f,
  88. -0.423169f, 0.423169f, 1.000000f,
  89. -0.423169f, -0.423169f, 1.000000f
  90. };
  91. static const unsigned short pointLightIndexData[] =
  92. {
  93. 0, 1, 2,
  94. 0, 2, 3,
  95. 4, 5, 6,
  96. 4, 6, 7,
  97. 8, 9, 10,
  98. 8, 10, 11,
  99. 12, 13, 14,
  100. 12, 14, 15,
  101. 16, 17, 18,
  102. 16, 18, 19,
  103. 20, 21, 22,
  104. 20, 22, 23,
  105. 0, 10, 9,
  106. 0, 9, 1,
  107. 13, 2, 1,
  108. 13, 1, 14,
  109. 23, 0, 3,
  110. 23, 3, 20,
  111. 17, 3, 2,
  112. 17, 2, 18,
  113. 21, 7, 6,
  114. 21, 6, 22,
  115. 7, 16, 19,
  116. 7, 19, 4,
  117. 5, 8, 11,
  118. 5, 11, 6,
  119. 4, 12, 15,
  120. 4, 15, 5,
  121. 22, 11, 10,
  122. 22, 10, 23,
  123. 8, 15, 14,
  124. 8, 14, 9,
  125. 12, 19, 18,
  126. 12, 18, 13,
  127. 16, 21, 20,
  128. 16, 20, 17,
  129. 0, 23, 10,
  130. 1, 9, 14,
  131. 2, 13, 18,
  132. 3, 17, 20,
  133. 6, 11, 22,
  134. 5, 15, 8,
  135. 4, 19, 12,
  136. 7, 21, 16
  137. };
  138. static const float spotLightVertexData[] =
  139. {
  140. 0.00001f, 0.00001f, 0.00001f,
  141. 0.00001f, -0.00001f, 0.00001f,
  142. -0.00001f, -0.00001f, 0.00001f,
  143. -0.00001f, 0.00001f, 0.00001f,
  144. 1.00000f, 1.00000f, 0.99999f,
  145. 1.00000f, -1.00000f, 0.99999f,
  146. -1.00000f, -1.00000f, 0.99999f,
  147. -1.00000f, 1.00000f, 0.99999f,
  148. };
  149. static const unsigned short spotLightIndexData[] =
  150. {
  151. 3, 0, 1,
  152. 3, 1, 2,
  153. 0, 4, 5,
  154. 0, 5, 1,
  155. 3, 7, 4,
  156. 3, 4, 0,
  157. 7, 3, 2,
  158. 7, 2, 6,
  159. 6, 2, 1,
  160. 6, 1, 5,
  161. 7, 5, 4,
  162. 7, 6, 5
  163. };
  164. static const char* shadowVariations[] =
  165. {
  166. // No specific hardware shadow compare variation on OpenGL, it is always supported
  167. #ifdef USE_OPENGL
  168. "LQ",
  169. "LQ",
  170. "",
  171. ""
  172. #else
  173. "",
  174. "LQHW",
  175. "",
  176. "HW"
  177. #endif
  178. };
  179. static const char* geometryVSVariations[] =
  180. {
  181. "",
  182. "Skinned",
  183. "Instanced",
  184. "Billboard"
  185. };
  186. static const char* lightVSVariations[] =
  187. {
  188. "Dir",
  189. "Spot",
  190. "Point",
  191. "DirSpec",
  192. "SpotSpec",
  193. "PointSpec",
  194. "DirShadow",
  195. "SpotShadow",
  196. "PointShadow",
  197. "DirSpecShadow",
  198. "SpotSpecShadow",
  199. "PointSpecShadow"
  200. };
  201. static const char* vertexLightVSVariations[] =
  202. {
  203. "",
  204. "1VL",
  205. "2VL",
  206. "3VL",
  207. "4VL",
  208. "5VL",
  209. "6VL"
  210. };
  211. static const char* deferredLightVSVariations[] =
  212. {
  213. "",
  214. "Dir",
  215. "Ortho",
  216. "OrthoDir"
  217. };
  218. static const char* lightPSVariations[] =
  219. {
  220. "Dir",
  221. "Spot",
  222. "Point",
  223. "PointMask",
  224. "DirSpec",
  225. "SpotSpec",
  226. "PointSpec",
  227. "PointMaskSpec",
  228. "DirShadow",
  229. "SpotShadow",
  230. "PointShadow",
  231. "PointMaskShadow",
  232. "DirSpecShadow",
  233. "SpotSpecShadow",
  234. "PointSpecShadow",
  235. "PointMaskSpecShadow"
  236. };
  237. static const unsigned INSTANCING_BUFFER_MASK = MASK_INSTANCEMATRIX1 | MASK_INSTANCEMATRIX2 | MASK_INSTANCEMATRIX3;
  238. static const unsigned MAX_BUFFER_AGE = 1000;
  239. Renderer::Renderer(Context* context) :
  240. Object(context),
  241. defaultZone_(new Zone(context)),
  242. quadDirLight_(new Light(context)),
  243. textureAnisotropy_(4),
  244. textureFilterMode_(FILTER_TRILINEAR),
  245. textureQuality_(QUALITY_HIGH),
  246. materialQuality_(QUALITY_HIGH),
  247. shadowMapSize_(1024),
  248. shadowQuality_(SHADOWQUALITY_HIGH_16BIT),
  249. maxShadowMaps_(1),
  250. maxShadowCascades_(MAX_CASCADE_SPLITS),
  251. minInstances_(2),
  252. maxInstanceTriangles_(500),
  253. maxSortedInstances_(1000),
  254. maxOccluderTriangles_(5000),
  255. occlusionBufferSize_(256),
  256. occluderSizeThreshold_(0.025f),
  257. numViews_(0),
  258. numOcclusionBuffers_(0),
  259. numShadowCameras_(0),
  260. shadersChangedFrameNumber_(M_MAX_UNSIGNED),
  261. hdrRendering_(false),
  262. specularLighting_(true),
  263. drawShadows_(true),
  264. reuseShadowMaps_(true),
  265. dynamicInstancing_(true),
  266. shadersDirty_(true),
  267. initialized_(false)
  268. {
  269. #ifndef USE_OPENGL
  270. shaderPath_ = "Shaders/HLSL/";
  271. #else
  272. shaderPath_ = "Shaders/GLSL/";
  273. #endif
  274. SubscribeToEvent(E_SCREENMODE, HANDLER(Renderer, HandleScreenMode));
  275. SubscribeToEvent(E_GRAPHICSFEATURES, HANDLER(Renderer, HandleGraphicsFeatures));
  276. quadDirLight_->SetLightType(LIGHT_DIRECTIONAL);
  277. // Try to initialize right now, but skip if screen mode is not yet set
  278. Initialize();
  279. }
  280. Renderer::~Renderer()
  281. {
  282. }
  283. void Renderer::SetNumViewports(unsigned num)
  284. {
  285. viewports_.Resize(num);
  286. }
  287. void Renderer::SetViewport(unsigned index, Viewport* viewport)
  288. {
  289. if (index >= viewports_.Size())
  290. viewports_.Resize(index + 1);
  291. viewports_[index] = viewport;
  292. }
  293. void Renderer::SetDefaultRenderPath(RenderPath* renderPath)
  294. {
  295. if (renderPath)
  296. defaultRenderPath_ = renderPath;
  297. }
  298. void Renderer::SetDefaultRenderPath(XMLFile* xmlFile)
  299. {
  300. SharedPtr<RenderPath> newRenderPath(new RenderPath());
  301. if (newRenderPath->Load(xmlFile))
  302. defaultRenderPath_ = newRenderPath;
  303. }
  304. void Renderer::SetHDRRendering(bool enable)
  305. {
  306. hdrRendering_ = enable;
  307. }
  308. void Renderer::SetSpecularLighting(bool enable)
  309. {
  310. specularLighting_ = enable;
  311. }
  312. void Renderer::SetTextureAnisotropy(int level)
  313. {
  314. textureAnisotropy_ = Max(level, 1);
  315. }
  316. void Renderer::SetTextureFilterMode(TextureFilterMode mode)
  317. {
  318. textureFilterMode_ = mode;
  319. }
  320. void Renderer::SetTextureQuality(int quality)
  321. {
  322. quality = Clamp(quality, QUALITY_LOW, QUALITY_HIGH);
  323. if (quality != textureQuality_)
  324. {
  325. textureQuality_ = quality;
  326. ReloadTextures();
  327. }
  328. }
  329. void Renderer::SetMaterialQuality(int quality)
  330. {
  331. materialQuality_ = Clamp(quality, QUALITY_LOW, QUALITY_MAX);
  332. shadersDirty_ = true;
  333. ResetViews();
  334. }
  335. void Renderer::SetDrawShadows(bool enable)
  336. {
  337. if (!graphics_ || !graphics_->GetShadowMapFormat())
  338. return;
  339. drawShadows_ = enable;
  340. if (!drawShadows_)
  341. ResetShadowMaps();
  342. }
  343. void Renderer::SetShadowMapSize(int size)
  344. {
  345. if (!graphics_)
  346. return;
  347. size = NextPowerOfTwo(Max(size, SHADOW_MIN_PIXELS));
  348. if (size != shadowMapSize_)
  349. {
  350. shadowMapSize_ = size;
  351. ResetShadowMaps();
  352. }
  353. }
  354. void Renderer::SetShadowQuality(int quality)
  355. {
  356. if (!graphics_)
  357. return;
  358. quality &= SHADOWQUALITY_HIGH_24BIT;
  359. // If no hardware PCF, do not allow to select one-sample quality
  360. if (!graphics_->GetHardwareShadowSupport())
  361. quality |= SHADOWQUALITY_HIGH_16BIT;
  362. if (!graphics_->GetHiresShadowMapFormat())
  363. quality &= SHADOWQUALITY_HIGH_16BIT;
  364. if (quality != shadowQuality_)
  365. {
  366. shadowQuality_ = quality;
  367. shadersDirty_ = true;
  368. ResetShadowMaps();
  369. }
  370. }
  371. void Renderer::SetReuseShadowMaps(bool enable)
  372. {
  373. if (enable == reuseShadowMaps_)
  374. return;
  375. reuseShadowMaps_ = enable;
  376. }
  377. void Renderer::SetMaxShadowMaps(int shadowMaps)
  378. {
  379. if (shadowMaps < 1)
  380. return;
  381. maxShadowMaps_ = shadowMaps;
  382. for (HashMap<int, Vector<SharedPtr<Texture2D> > >::Iterator i = shadowMaps_.Begin(); i != shadowMaps_.End(); ++i)
  383. {
  384. if ((int)i->second_.Size() > maxShadowMaps_)
  385. i->second_.Resize(maxShadowMaps_);
  386. }
  387. }
  388. void Renderer::SetMaxShadowCascades(int cascades)
  389. {
  390. cascades = Clamp(cascades, 1, MAX_CASCADE_SPLITS);
  391. if (cascades != maxShadowCascades_)
  392. {
  393. maxShadowCascades_ = cascades;
  394. ResetShadowMaps();
  395. }
  396. }
  397. void Renderer::SetDynamicInstancing(bool enable)
  398. {
  399. if (!instancingBuffer_)
  400. enable = false;
  401. dynamicInstancing_ = enable;
  402. }
  403. void Renderer::SetMinInstances(int instances)
  404. {
  405. minInstances_ = Max(instances, 2);
  406. }
  407. void Renderer::SetMaxInstanceTriangles(int triangles)
  408. {
  409. maxInstanceTriangles_ = Max(triangles, 0);
  410. }
  411. void Renderer::SetMaxSortedInstances(int instances)
  412. {
  413. maxSortedInstances_ = Max(instances, 0);
  414. }
  415. void Renderer::SetMaxOccluderTriangles(int triangles)
  416. {
  417. maxOccluderTriangles_ = Max(triangles, 0);
  418. }
  419. void Renderer::SetOcclusionBufferSize(int size)
  420. {
  421. occlusionBufferSize_ = Max(size, 1);
  422. occlusionBuffers_.Clear();
  423. }
  424. void Renderer::SetOccluderSizeThreshold(float screenSize)
  425. {
  426. occluderSizeThreshold_ = Max(screenSize, 0.0f);
  427. }
  428. void Renderer::ReloadShaders()
  429. {
  430. shadersDirty_ = true;
  431. }
  432. Viewport* Renderer::GetViewport(unsigned index) const
  433. {
  434. return index < viewports_.Size() ? viewports_[index] : (Viewport*)0;
  435. }
  436. RenderPath* Renderer::GetDefaultRenderPath() const
  437. {
  438. return defaultRenderPath_;
  439. }
  440. ShaderVariation* Renderer::GetVertexShader(const String& name, bool checkExists) const
  441. {
  442. return GetShader(VS, name, checkExists);
  443. }
  444. ShaderVariation* Renderer::GetPixelShader(const String& name, bool checkExists) const
  445. {
  446. return GetShader(PS, name, checkExists);
  447. }
  448. unsigned Renderer::GetNumGeometries(bool allViews) const
  449. {
  450. unsigned numGeometries = 0;
  451. unsigned lastView = allViews ? numViews_ : 1;
  452. for (unsigned i = 0; i < lastView; ++i)
  453. numGeometries += views_[i]->GetGeometries().Size();
  454. return numGeometries;
  455. }
  456. unsigned Renderer::GetNumLights(bool allViews) const
  457. {
  458. unsigned numLights = 0;
  459. unsigned lastView = allViews ? numViews_ : 1;
  460. for (unsigned i = 0; i < lastView; ++i)
  461. numLights += views_[i]->GetLights().Size();
  462. return numLights;
  463. }
  464. unsigned Renderer::GetNumShadowMaps(bool allViews) const
  465. {
  466. unsigned numShadowMaps = 0;
  467. unsigned lastView = allViews ? numViews_ : 1;
  468. for (unsigned i = 0; i < lastView; ++i)
  469. {
  470. const Vector<LightBatchQueue>& lightQueues = views_[i]->GetLightQueues();
  471. for (Vector<LightBatchQueue>::ConstIterator i = lightQueues.Begin(); i != lightQueues.End(); ++i)
  472. {
  473. if (i->shadowMap_)
  474. ++numShadowMaps;
  475. }
  476. }
  477. return numShadowMaps;
  478. }
  479. unsigned Renderer::GetNumOccluders(bool allViews) const
  480. {
  481. unsigned numOccluders = 0;
  482. unsigned lastView = allViews ? numViews_ : 1;
  483. for (unsigned i = 0; i < lastView; ++i)
  484. numOccluders += views_[i]->GetOccluders().Size();
  485. return numOccluders;
  486. }
  487. void Renderer::Update(float timeStep)
  488. {
  489. PROFILE(UpdateViews);
  490. numViews_ = 0;
  491. // If device lost, do not perform update. This is because any dynamic vertex/index buffer updates happen already here,
  492. // and if the device is lost, the updates queue up, causing memory use to rise constantly
  493. if (!graphics_ || !graphics_->IsInitialized() || graphics_->IsDeviceLost())
  494. return;
  495. // Set up the frameinfo structure for this frame
  496. frame_.frameNumber_ = GetSubsystem<Time>()->GetFrameNumber();
  497. frame_.timeStep_ = timeStep;
  498. frame_.camera_ = 0;
  499. numShadowCameras_ = 0;
  500. numOcclusionBuffers_ = 0;
  501. updatedOctrees_.Clear();
  502. // Reload shaders now if needed
  503. if (shadersDirty_)
  504. LoadShaders();
  505. // Queue update of the main viewports. Use reverse order, as rendering order is also reverse
  506. // to render auxiliary views before dependant main views
  507. for (unsigned i = viewports_.Size() - 1; i < viewports_.Size(); --i)
  508. QueueViewport(0, viewports_[i]);
  509. // Gather other render surfaces that are autoupdated
  510. SendEvent(E_RENDERSURFACEUPDATE);
  511. // Process gathered views. This may queue further views (render surfaces that are only updated when visible)
  512. for (unsigned i = 0; i < queuedViews_.Size(); ++i)
  513. {
  514. WeakPtr<RenderSurface>& renderTarget = queuedViews_[i].first_;
  515. WeakPtr<Viewport>& viewport = queuedViews_[i].second_;
  516. // Null pointer means backbuffer view. Differentiate between that and an expired rendersurface
  517. if ((renderTarget.NotNull() && renderTarget.Expired()) || viewport.Expired())
  518. continue;
  519. // Allocate new view if necessary
  520. if (numViews_ == views_.Size())
  521. views_.Push(SharedPtr<View>(new View(context_)));
  522. // Check if view can be defined successfully (has valid scene, camera and octree)
  523. assert(numViews_ < views_.Size());
  524. View* view = views_[numViews_];
  525. if (!view->Define(renderTarget, viewport))
  526. continue;
  527. ++numViews_;
  528. const IntRect& viewRect = viewport->GetRect();
  529. Scene* scene = viewport->GetScene();
  530. Octree* octree = scene->GetComponent<Octree>();
  531. // Update octree (perform early update for drawables which need that, and reinsert moved drawables.)
  532. // However, if the same scene is viewed from multiple cameras, update the octree only once
  533. if (!updatedOctrees_.Contains(octree))
  534. {
  535. frame_.camera_ = viewport->GetCamera();
  536. frame_.viewSize_ = viewRect.Size();
  537. if (frame_.viewSize_ == IntVector2::ZERO)
  538. frame_.viewSize_ = IntVector2(graphics_->GetWidth(), graphics_->GetHeight());
  539. octree->Update(frame_);
  540. updatedOctrees_.Insert(octree);
  541. // Set also the view for the debug renderer already here, so that it can use culling
  542. /// \todo May result in incorrect debug geometry culling if the same scene is drawn from multiple viewports
  543. DebugRenderer* debug = scene->GetComponent<DebugRenderer>();
  544. if (debug)
  545. debug->SetView(viewport->GetCamera());
  546. }
  547. // Update view. This may queue further views
  548. using namespace BeginViewUpdate;
  549. VariantMap& eventData = GetEventDataMap();
  550. eventData[P_SURFACE] = (void*)renderTarget.Get();
  551. eventData[P_TEXTURE] = (void*)(renderTarget ? renderTarget->GetParentTexture() : 0);
  552. eventData[P_SCENE] = (void*)scene;
  553. eventData[P_CAMERA] = (void*)viewport->GetCamera();
  554. SendEvent(E_BEGINVIEWUPDATE, eventData);
  555. ResetShadowMapAllocations(); // Each view can reuse the same shadow maps
  556. view->Update(frame_);
  557. SendEvent(E_ENDVIEWUPDATE, eventData);
  558. }
  559. // Reset update flag from queued render surfaces. At this point no new views can be added on this frame
  560. for (unsigned i = 0; i < queuedViews_.Size(); ++i)
  561. {
  562. WeakPtr<RenderSurface>& renderTarget = queuedViews_[i].first_;
  563. if (renderTarget)
  564. renderTarget->WasUpdated();
  565. }
  566. queuedViews_.Clear();
  567. }
  568. void Renderer::Render()
  569. {
  570. // Engine does not render when window is closed or device is lost
  571. assert(graphics_ && graphics_->IsInitialized() && !graphics_->IsDeviceLost());
  572. PROFILE(RenderViews);
  573. // If the indirection textures have lost content (OpenGL mode only), restore them now
  574. if (faceSelectCubeMap_ && faceSelectCubeMap_->IsDataLost())
  575. SetIndirectionTextureData();
  576. graphics_->SetDefaultTextureFilterMode(textureFilterMode_);
  577. graphics_->SetTextureAnisotropy(textureAnisotropy_);
  578. graphics_->ClearParameterSources();
  579. // If no views, just clear the screen
  580. if (!numViews_)
  581. {
  582. graphics_->SetBlendMode(BLEND_REPLACE);
  583. graphics_->SetColorWrite(true);
  584. graphics_->SetDepthWrite(true);
  585. graphics_->SetScissorTest(false);
  586. graphics_->SetStencilTest(false);
  587. graphics_->ResetRenderTargets();
  588. graphics_->Clear(CLEAR_COLOR | CLEAR_DEPTH | CLEAR_STENCIL, defaultZone_->GetFogColor());
  589. numPrimitives_ = 0;
  590. numBatches_ = 0;
  591. }
  592. else
  593. {
  594. // Render views from last to first (each main view is rendered after the auxiliary views it depends on)
  595. for (unsigned i = numViews_ - 1; i < numViews_; --i)
  596. {
  597. using namespace BeginViewRender;
  598. RenderSurface* renderTarget = views_[i]->GetRenderTarget();
  599. VariantMap& eventData = GetEventDataMap();
  600. eventData[P_SURFACE] = (void*)renderTarget;
  601. eventData[P_TEXTURE] = (void*)(renderTarget ? renderTarget->GetParentTexture() : 0);
  602. eventData[P_SCENE] = (void*)views_[i]->GetScene();
  603. eventData[P_CAMERA] = (void*)views_[i]->GetCamera();
  604. SendEvent(E_BEGINVIEWRENDER, eventData);
  605. // Screen buffers can be reused between views, as each is rendered completely
  606. PrepareViewRender();
  607. views_[i]->Render();
  608. SendEvent(E_ENDVIEWRENDER, eventData);
  609. }
  610. // Copy the number of batches & primitives from Graphics so that we can account for 3D geometry only
  611. numPrimitives_ = graphics_->GetNumPrimitives();
  612. numBatches_ = graphics_->GetNumBatches();
  613. }
  614. // Remove unused occlusion buffers and renderbuffers
  615. RemoveUnusedBuffers();
  616. }
  617. void Renderer::DrawDebugGeometry(bool depthTest)
  618. {
  619. PROFILE(RendererDrawDebug);
  620. /// \todo Because debug geometry is per-scene, if two cameras show views of the same area, occlusion is not shown correctly
  621. HashSet<Drawable*> processedGeometries;
  622. HashSet<Light*> processedLights;
  623. for (unsigned i = 0; i < numViews_; ++i)
  624. {
  625. // Make sure it's a main view, and process each node only once
  626. View* view = views_[i];
  627. if (view->GetRenderTarget())
  628. continue;
  629. Octree* octree = view->GetOctree();
  630. if (!octree)
  631. continue;
  632. DebugRenderer* debug = octree->GetComponent<DebugRenderer>();
  633. if (!debug)
  634. continue;
  635. const PODVector<Drawable*>& geometries = view->GetGeometries();
  636. const PODVector<Light*>& lights = view->GetLights();
  637. for (unsigned i = 0; i < geometries.Size(); ++i)
  638. {
  639. if (!processedGeometries.Contains(geometries[i]))
  640. {
  641. geometries[i]->DrawDebugGeometry(debug, depthTest);
  642. processedGeometries.Insert(geometries[i]);
  643. }
  644. }
  645. for (unsigned i = 0; i < lights.Size(); ++i)
  646. {
  647. if (!processedLights.Contains(lights[i]))
  648. {
  649. lights[i]->DrawDebugGeometry(debug, depthTest);
  650. processedLights.Insert(lights[i]);
  651. }
  652. }
  653. }
  654. }
  655. void Renderer::QueueRenderSurface(RenderSurface* renderTarget)
  656. {
  657. if (renderTarget)
  658. {
  659. unsigned numViewports = renderTarget->GetNumViewports();
  660. for (unsigned i = 0; i < numViewports; ++i)
  661. QueueViewport(renderTarget, renderTarget->GetViewport(i));
  662. }
  663. }
  664. void Renderer::QueueViewport(RenderSurface* renderTarget, Viewport* viewport)
  665. {
  666. if (viewport)
  667. {
  668. queuedViews_.Push(Pair<WeakPtr<RenderSurface>, WeakPtr<Viewport> >(WeakPtr<RenderSurface>(renderTarget),
  669. WeakPtr<Viewport>(viewport)));
  670. }
  671. }
  672. void Renderer::GetLightVolumeShaders(PODVector<ShaderVariation*>& lightVS, PODVector<ShaderVariation*>& lightPS, const String& vsName, const String& psName)
  673. {
  674. lightVS.Resize(MAX_DEFERRED_LIGHT_VS_VARIATIONS);
  675. lightPS.Resize(MAX_DEFERRED_LIGHT_PS_VARIATIONS);
  676. unsigned shadows = (graphics_->GetHardwareShadowSupport() ? 1 : 0) | (shadowQuality_ & SHADOWQUALITY_HIGH_16BIT);
  677. for (unsigned i = 0; i < MAX_DEFERRED_LIGHT_VS_VARIATIONS; ++i)
  678. lightVS[i] = GetVertexShader(vsName + "_" + deferredLightVSVariations[i]);
  679. for (unsigned i = 0; i < lightPS.Size(); ++i)
  680. {
  681. String ortho;
  682. if (i >= DLPS_ORTHO)
  683. ortho = "Ortho";
  684. if (i & DLPS_SHADOW)
  685. {
  686. lightPS[i] = GetPixelShader(psName + "_" + ortho + lightPSVariations[i % DLPS_ORTHO] +
  687. shadowVariations[shadows]);
  688. }
  689. else
  690. lightPS[i] = GetPixelShader(psName + "_" + ortho + lightPSVariations[i % DLPS_ORTHO]);
  691. }
  692. }
  693. Geometry* Renderer::GetLightGeometry(Light* light)
  694. {
  695. switch (light->GetLightType())
  696. {
  697. case LIGHT_DIRECTIONAL:
  698. return dirLightGeometry_;
  699. case LIGHT_SPOT:
  700. return spotLightGeometry_;
  701. case LIGHT_POINT:
  702. return pointLightGeometry_;
  703. }
  704. return 0;
  705. }
  706. Texture2D* Renderer::GetShadowMap(Light* light, Camera* camera, unsigned viewWidth, unsigned viewHeight)
  707. {
  708. LightType type = light->GetLightType();
  709. const FocusParameters& parameters = light->GetShadowFocus();
  710. float size = (float)shadowMapSize_ * light->GetShadowResolution();
  711. // Automatically reduce shadow map size when far away
  712. if (parameters.autoSize_ && type != LIGHT_DIRECTIONAL)
  713. {
  714. const Matrix3x4& view = camera->GetView();
  715. const Matrix4& projection = camera->GetProjection();
  716. BoundingBox lightBox;
  717. float lightPixels;
  718. if (type == LIGHT_POINT)
  719. {
  720. // Calculate point light pixel size from the projection of its diagonal
  721. Vector3 center = view * light->GetNode()->GetWorldPosition();
  722. float extent = 0.58f * light->GetRange();
  723. lightBox.Define(center + Vector3(extent, extent, extent), center - Vector3(extent, extent, extent));
  724. }
  725. else
  726. {
  727. // Calculate spot light pixel size from the projection of its frustum far vertices
  728. Frustum lightFrustum = light->GetFrustum().Transformed(view);
  729. lightBox.Define(&lightFrustum.vertices_[4], 4);
  730. }
  731. Vector2 projectionSize = lightBox.Projected(projection).Size();
  732. lightPixels = Max(0.5f * (float)viewWidth * projectionSize.x_, 0.5f * (float)viewHeight * projectionSize.y_);
  733. // Clamp pixel amount to a sufficient minimum to avoid self-shadowing artifacts due to loss of precision
  734. if (lightPixels < SHADOW_MIN_PIXELS)
  735. lightPixels = SHADOW_MIN_PIXELS;
  736. size = Min(size, lightPixels);
  737. }
  738. /// \todo Allow to specify maximum shadow maps per resolution, as smaller shadow maps take less memory
  739. int width = NextPowerOfTwo((unsigned)size);
  740. int height = width;
  741. // Adjust the size for directional or point light shadow map atlases
  742. if (type == LIGHT_DIRECTIONAL)
  743. {
  744. if (maxShadowCascades_ > 1)
  745. width *= 2;
  746. if (maxShadowCascades_ > 2)
  747. height *= 2;
  748. }
  749. else if (type == LIGHT_POINT)
  750. {
  751. width *= 2;
  752. height *= 3;
  753. }
  754. int searchKey = (width << 16) | height;
  755. if (shadowMaps_.Contains(searchKey))
  756. {
  757. // If shadow maps are reused, always return the first
  758. if (reuseShadowMaps_)
  759. return shadowMaps_[searchKey][0];
  760. else
  761. {
  762. // If not reused, check allocation count and return existing shadow map if possible
  763. unsigned allocated = shadowMapAllocations_[searchKey].Size();
  764. if (allocated < shadowMaps_[searchKey].Size())
  765. {
  766. shadowMapAllocations_[searchKey].Push(light);
  767. return shadowMaps_[searchKey][allocated];
  768. }
  769. else if ((int)allocated >= maxShadowMaps_)
  770. return 0;
  771. }
  772. }
  773. unsigned shadowMapFormat = (shadowQuality_ & SHADOWQUALITY_LOW_24BIT) ? graphics_->GetHiresShadowMapFormat() :
  774. graphics_->GetShadowMapFormat();
  775. if (!shadowMapFormat)
  776. return 0;
  777. SharedPtr<Texture2D> newShadowMap(new Texture2D(context_));
  778. int retries = 3;
  779. // OpenGL: create shadow map only. Color rendertarget is not needed
  780. #ifdef USE_OPENGL
  781. while (retries)
  782. {
  783. if (!newShadowMap->SetSize(width, height, shadowMapFormat, TEXTURE_DEPTHSTENCIL))
  784. {
  785. width >>= 1;
  786. height >>= 1;
  787. --retries;
  788. }
  789. else
  790. {
  791. #ifndef GL_ES_VERSION_2_0
  792. newShadowMap->SetFilterMode(FILTER_BILINEAR);
  793. newShadowMap->SetShadowCompare(true);
  794. #endif
  795. break;
  796. }
  797. }
  798. #else
  799. // Direct3D9: create shadow map and dummy color rendertarget
  800. unsigned dummyColorFormat = graphics_->GetDummyColorFormat();
  801. while (retries)
  802. {
  803. if (!newShadowMap->SetSize(width, height, shadowMapFormat, TEXTURE_DEPTHSTENCIL))
  804. {
  805. width >>= 1;
  806. height >>= 1;
  807. --retries;
  808. }
  809. else
  810. {
  811. newShadowMap->SetFilterMode(FILTER_BILINEAR);
  812. // If no dummy color rendertarget for this size exists yet, create one now
  813. if (!colorShadowMaps_.Contains(searchKey))
  814. {
  815. colorShadowMaps_[searchKey] = new Texture2D(context_);
  816. colorShadowMaps_[searchKey]->SetSize(width, height, dummyColorFormat, TEXTURE_RENDERTARGET);
  817. }
  818. // Link the color rendertarget to the shadow map
  819. newShadowMap->GetRenderSurface()->SetLinkedRenderTarget(colorShadowMaps_[searchKey]->GetRenderSurface());
  820. break;
  821. }
  822. }
  823. #endif
  824. // If failed to set size, store a null pointer so that we will not retry
  825. if (!retries)
  826. newShadowMap.Reset();
  827. shadowMaps_[searchKey].Push(newShadowMap);
  828. if (!reuseShadowMaps_)
  829. shadowMapAllocations_[searchKey].Push(light);
  830. return newShadowMap;
  831. }
  832. Texture2D* Renderer::GetScreenBuffer(int width, int height, unsigned format, bool filtered, bool srgb)
  833. {
  834. bool depthStencil = (format == Graphics::GetDepthStencilFormat());
  835. if (depthStencil)
  836. {
  837. filtered = false;
  838. srgb = false;
  839. }
  840. long long searchKey = ((long long)format << 32) | (width << 16) | height;
  841. if (filtered)
  842. searchKey |= 0x8000000000000000LL;
  843. if (srgb)
  844. searchKey |= 0x4000000000000000LL;
  845. // If new size or format, initialize the allocation stats
  846. if (screenBuffers_.Find(searchKey) == screenBuffers_.End())
  847. screenBufferAllocations_[searchKey] = 0;
  848. // Reuse depth-stencil buffers whenever the size matches, instead of allocating new
  849. unsigned allocations = screenBufferAllocations_[searchKey];
  850. if(!depthStencil)
  851. ++screenBufferAllocations_[searchKey];
  852. if (allocations >= screenBuffers_[searchKey].Size())
  853. {
  854. SharedPtr<Texture2D> newBuffer(new Texture2D(context_));
  855. newBuffer->SetSRGB(srgb);
  856. newBuffer->SetSize(width, height, format, depthStencil ? TEXTURE_DEPTHSTENCIL : TEXTURE_RENDERTARGET);
  857. newBuffer->SetFilterMode(filtered ? FILTER_BILINEAR : FILTER_NEAREST);
  858. newBuffer->ResetUseTimer();
  859. screenBuffers_[searchKey].Push(newBuffer);
  860. LOGDEBUG("Allocated new screen buffer size " + String(width) + "x" + String(height) + " format " + String(format));
  861. return newBuffer;
  862. }
  863. else
  864. {
  865. Texture2D* buffer = screenBuffers_[searchKey][allocations];
  866. buffer->ResetUseTimer();
  867. return buffer;
  868. }
  869. }
  870. RenderSurface* Renderer::GetDepthStencil(int width, int height)
  871. {
  872. // Return the default depth-stencil surface if applicable
  873. // (when using OpenGL Graphics will allocate right size surfaces on demand to emulate Direct3D9)
  874. if (width == graphics_->GetWidth() && height == graphics_->GetHeight() && graphics_->GetMultiSample() <= 1)
  875. return 0;
  876. else
  877. return GetScreenBuffer(width, height, Graphics::GetDepthStencilFormat(), false, false)->GetRenderSurface();
  878. }
  879. OcclusionBuffer* Renderer::GetOcclusionBuffer(Camera* camera)
  880. {
  881. assert(numOcclusionBuffers_ <= occlusionBuffers_.Size());
  882. if (numOcclusionBuffers_ == occlusionBuffers_.Size())
  883. {
  884. SharedPtr<OcclusionBuffer> newBuffer(new OcclusionBuffer(context_));
  885. occlusionBuffers_.Push(newBuffer);
  886. }
  887. int width = occlusionBufferSize_;
  888. int height = (int)((float)occlusionBufferSize_ / camera->GetAspectRatio() + 0.5f);
  889. OcclusionBuffer* buffer = occlusionBuffers_[numOcclusionBuffers_++];
  890. buffer->SetSize(width, height);
  891. buffer->SetView(camera);
  892. buffer->ResetUseTimer();
  893. return buffer;
  894. }
  895. Camera* Renderer::GetShadowCamera()
  896. {
  897. MutexLock lock(rendererMutex_);
  898. assert(numShadowCameras_ <= shadowCameraNodes_.Size());
  899. if (numShadowCameras_ == shadowCameraNodes_.Size())
  900. {
  901. SharedPtr<Node> newNode(new Node(context_));
  902. newNode->CreateComponent<Camera>();
  903. shadowCameraNodes_.Push(newNode);
  904. }
  905. Camera* camera = shadowCameraNodes_[numShadowCameras_++]->GetComponent<Camera>();
  906. camera->SetOrthographic(false);
  907. camera->SetZoom(1.0f);
  908. return camera;
  909. }
  910. ShaderVariation* Renderer::GetShader(ShaderType type, const String& name, bool checkExists) const
  911. {
  912. if (name.Trimmed().Empty())
  913. return 0;
  914. ResourceCache* cache = GetSubsystem<ResourceCache>();
  915. String shaderName = shaderPath_;
  916. String variationName;
  917. unsigned split = name.Find('_');
  918. if (split != String::NPOS)
  919. {
  920. shaderName += name.Substring(0, split) + ".xml";
  921. variationName = name.Substring(split + 1);
  922. }
  923. else
  924. shaderName += name + ".xml";
  925. if (checkExists)
  926. {
  927. if (!cache->Exists(shaderName))
  928. return 0;
  929. }
  930. Shader* shader = cache->GetResource<Shader>(shaderName);
  931. if (shader)
  932. return shader->GetVariation(type, variationName);
  933. else
  934. return 0;
  935. }
  936. void Renderer::SetBatchShaders(Batch& batch, Technique* tech, bool allowShadows)
  937. {
  938. // Check if shaders are unloaded or need reloading
  939. Pass* pass = batch.pass_;
  940. Vector<SharedPtr<ShaderVariation> >& vertexShaders = pass->GetVertexShaders();
  941. Vector<SharedPtr<ShaderVariation> >& pixelShaders = pass->GetPixelShaders();
  942. if (!vertexShaders.Size() || !pixelShaders.Size() || pass->GetShadersLoadedFrameNumber() !=
  943. shadersChangedFrameNumber_)
  944. {
  945. // First release all previous shaders, then load
  946. pass->ReleaseShaders();
  947. LoadPassShaders(tech, pass->GetType());
  948. }
  949. // Make sure shaders are loaded now
  950. if (vertexShaders.Size() && pixelShaders.Size())
  951. {
  952. // If instancing is not supported, but was requested, or the object is too large to be instanced,
  953. // choose static geometry vertex shader instead
  954. if (batch.geometryType_ == GEOM_INSTANCED && (!GetDynamicInstancing() || batch.geometry_->GetIndexCount() >
  955. (unsigned)maxInstanceTriangles_ * 3))
  956. batch.geometryType_ = GEOM_STATIC;
  957. if (batch.geometryType_ == GEOM_STATIC_NOINSTANCING)
  958. batch.geometryType_ = GEOM_STATIC;
  959. // Check whether is a pixel lit forward pass. If not, there is only one pixel shader
  960. if (pass->GetLightingMode() == LIGHTING_PERPIXEL)
  961. {
  962. LightBatchQueue* lightQueue = batch.lightQueue_;
  963. if (!lightQueue)
  964. {
  965. // Do not log error, as it would result in a lot of spam
  966. batch.vertexShader_ = 0;
  967. batch.pixelShader_ = 0;
  968. return;
  969. }
  970. Light* light = lightQueue->light_;
  971. unsigned vsi = 0;
  972. unsigned psi = 0;
  973. vsi = batch.geometryType_ * MAX_LIGHT_VS_VARIATIONS;
  974. bool materialHasSpecular = batch.material_ ? batch.material_->GetSpecular() : true;
  975. if (specularLighting_ && light->GetSpecularIntensity() > 0.0f && materialHasSpecular)
  976. {
  977. vsi += LVS_SPEC;
  978. psi += LPS_SPEC;
  979. }
  980. if (allowShadows && lightQueue->shadowMap_)
  981. {
  982. vsi += LVS_SHADOW;
  983. psi += LPS_SHADOW;
  984. }
  985. switch (light->GetLightType())
  986. {
  987. case LIGHT_DIRECTIONAL:
  988. vsi += LVS_DIR;
  989. break;
  990. case LIGHT_SPOT:
  991. psi += LPS_SPOT;
  992. vsi += LVS_SPOT;
  993. break;
  994. case LIGHT_POINT:
  995. if (light->GetShapeTexture())
  996. psi += LPS_POINTMASK;
  997. else
  998. psi += LPS_POINT;
  999. vsi += LVS_POINT;
  1000. break;
  1001. }
  1002. batch.vertexShader_ = vertexShaders[vsi];
  1003. batch.pixelShader_ = pixelShaders[psi];
  1004. // If shadow or specular variations do not exist, try without them
  1005. if ((!batch.vertexShader_ || !batch.pixelShader_) && ((vsi % MAX_LIGHT_VS_VARIATIONS) >= LVS_SHADOW))
  1006. {
  1007. vsi -= LVS_SHADOW;
  1008. psi -= LPS_SHADOW;
  1009. batch.vertexShader_ = vertexShaders[vsi];
  1010. batch.pixelShader_ = pixelShaders[psi];
  1011. }
  1012. if ((!batch.vertexShader_ || !batch.pixelShader_) && ((vsi % MAX_LIGHT_VS_VARIATIONS) >= LVS_SPEC))
  1013. {
  1014. vsi -= LVS_SPEC;
  1015. psi -= LPS_SPEC;
  1016. batch.vertexShader_ = vertexShaders[vsi];
  1017. batch.pixelShader_ = pixelShaders[psi];
  1018. }
  1019. }
  1020. else
  1021. {
  1022. // Check if pass has vertex lighting support
  1023. if (pass->GetLightingMode() == LIGHTING_PERVERTEX)
  1024. {
  1025. unsigned numVertexLights = 0;
  1026. if (batch.lightQueue_)
  1027. numVertexLights = batch.lightQueue_->vertexLights_.Size();
  1028. unsigned vsi = batch.geometryType_ * MAX_VERTEXLIGHT_VS_VARIATIONS + numVertexLights;
  1029. batch.vertexShader_ = vertexShaders[vsi];
  1030. // If vertex lights variations do not exist, try without them
  1031. if (!batch.vertexShader_)
  1032. {
  1033. unsigned vsi = batch.geometryType_ * MAX_VERTEXLIGHT_VS_VARIATIONS;
  1034. batch.vertexShader_ = vertexShaders[vsi];
  1035. }
  1036. }
  1037. else
  1038. {
  1039. unsigned vsi = batch.geometryType_;
  1040. batch.vertexShader_ = vertexShaders[vsi];
  1041. }
  1042. batch.pixelShader_ = pixelShaders[0];
  1043. }
  1044. }
  1045. // Log error if shaders could not be assigned, but only once per technique
  1046. if (!batch.vertexShader_ || !batch.pixelShader_)
  1047. {
  1048. if (!shaderErrorDisplayed_.Contains(tech))
  1049. {
  1050. shaderErrorDisplayed_.Insert(tech);
  1051. LOGERROR("Technique " + tech->GetName() + " has missing shaders");
  1052. }
  1053. }
  1054. }
  1055. void Renderer::SetLightVolumeBatchShaders(Batch& batch, PODVector<ShaderVariation*>& lightVS, PODVector<ShaderVariation*>& lightPS)
  1056. {
  1057. unsigned vsi = DLVS_NONE;
  1058. unsigned psi = DLPS_NONE;
  1059. Light* light = batch.lightQueue_->light_;
  1060. switch (light->GetLightType())
  1061. {
  1062. case LIGHT_DIRECTIONAL:
  1063. vsi += DLVS_DIR;
  1064. break;
  1065. case LIGHT_SPOT:
  1066. psi += DLPS_SPOT;
  1067. break;
  1068. case LIGHT_POINT:
  1069. if (light->GetShapeTexture())
  1070. psi += DLPS_POINTMASK;
  1071. else
  1072. psi += DLPS_POINT;
  1073. break;
  1074. }
  1075. if (batch.lightQueue_->shadowMap_)
  1076. psi += DLPS_SHADOW;
  1077. if (specularLighting_ && light->GetSpecularIntensity() > 0.0f)
  1078. psi += DLPS_SPEC;
  1079. if (batch.camera_->IsOrthographic())
  1080. {
  1081. vsi += DLVS_ORTHO;
  1082. psi += DLPS_ORTHO;
  1083. }
  1084. batch.vertexShader_ = lightVS[vsi];
  1085. batch.pixelShader_ = lightPS[psi];
  1086. }
  1087. void Renderer::SetCullMode(CullMode mode, Camera* camera)
  1088. {
  1089. // If a camera is specified, check whether it reverses culling due to vertical flipping or reflection
  1090. if (camera && camera->GetReverseCulling())
  1091. {
  1092. if (mode == CULL_CW)
  1093. mode = CULL_CCW;
  1094. else if (mode == CULL_CCW)
  1095. mode = CULL_CW;
  1096. }
  1097. graphics_->SetCullMode(mode);
  1098. }
  1099. bool Renderer::ResizeInstancingBuffer(unsigned numInstances)
  1100. {
  1101. if (!instancingBuffer_ || !dynamicInstancing_)
  1102. return false;
  1103. unsigned oldSize = instancingBuffer_->GetVertexCount();
  1104. if (numInstances <= oldSize)
  1105. return true;
  1106. unsigned newSize = INSTANCING_BUFFER_DEFAULT_SIZE;
  1107. while (newSize < numInstances)
  1108. newSize <<= 1;
  1109. if (!instancingBuffer_->SetSize(newSize, INSTANCING_BUFFER_MASK, true))
  1110. {
  1111. LOGERROR("Failed to resize instancing buffer to " + String(newSize));
  1112. // If failed, try to restore the old size
  1113. instancingBuffer_->SetSize(oldSize, INSTANCING_BUFFER_MASK, true);
  1114. return false;
  1115. }
  1116. LOGDEBUG("Resized instancing buffer to " + String(newSize));
  1117. return true;
  1118. }
  1119. void Renderer::SaveScreenBufferAllocations()
  1120. {
  1121. savedScreenBufferAllocations_ = screenBufferAllocations_;
  1122. }
  1123. void Renderer::RestoreScreenBufferAllocations()
  1124. {
  1125. screenBufferAllocations_ = savedScreenBufferAllocations_;
  1126. }
  1127. void Renderer::OptimizeLightByScissor(Light* light, Camera* camera)
  1128. {
  1129. if (light && light->GetLightType() != LIGHT_DIRECTIONAL)
  1130. graphics_->SetScissorTest(true, GetLightScissor(light, camera));
  1131. else
  1132. graphics_->SetScissorTest(false);
  1133. }
  1134. void Renderer::OptimizeLightByStencil(Light* light, Camera* camera)
  1135. {
  1136. #ifndef GL_ES_VERSION_2_0
  1137. if (light)
  1138. {
  1139. LightType type = light->GetLightType();
  1140. if (type == LIGHT_DIRECTIONAL)
  1141. {
  1142. graphics_->SetStencilTest(false);
  1143. return;
  1144. }
  1145. Geometry* geometry = GetLightGeometry(light);
  1146. const Matrix3x4& view = camera->GetView();
  1147. const Matrix4& projection = camera->GetProjection();
  1148. Vector3 cameraPos = camera->GetNode()->GetWorldPosition();
  1149. float lightDist;
  1150. if (type == LIGHT_POINT)
  1151. lightDist = Sphere(light->GetNode()->GetWorldPosition(), light->GetRange() * 1.25f).Distance(cameraPos);
  1152. else
  1153. lightDist = light->GetFrustum().Distance(cameraPos);
  1154. // If the camera is actually inside the light volume, do not draw to stencil as it would waste fillrate
  1155. if (lightDist < M_EPSILON)
  1156. {
  1157. graphics_->SetStencilTest(false);
  1158. return;
  1159. }
  1160. // If the stencil value has wrapped, clear the whole stencil first
  1161. if (!lightStencilValue_)
  1162. {
  1163. graphics_->Clear(CLEAR_STENCIL);
  1164. lightStencilValue_ = 1;
  1165. }
  1166. // If possible, render the stencil volume front faces. However, close to the near clip plane render back faces instead
  1167. // to avoid clipping.
  1168. if (lightDist < camera->GetNearClip() * 2.0f)
  1169. {
  1170. SetCullMode(CULL_CW, camera);
  1171. graphics_->SetDepthTest(CMP_GREATER);
  1172. }
  1173. else
  1174. {
  1175. SetCullMode(CULL_CCW, camera);
  1176. graphics_->SetDepthTest(CMP_LESSEQUAL);
  1177. }
  1178. graphics_->SetColorWrite(false);
  1179. graphics_->SetDepthWrite(false);
  1180. graphics_->SetStencilTest(true, CMP_ALWAYS, OP_REF, OP_KEEP, OP_KEEP, lightStencilValue_);
  1181. graphics_->SetShaders(stencilVS_, stencilPS_);
  1182. graphics_->SetShaderParameter(VSP_VIEWPROJ, projection * view);
  1183. graphics_->SetShaderParameter(VSP_MODEL, light->GetVolumeTransform(camera));
  1184. geometry->Draw(graphics_);
  1185. graphics_->ClearTransformSources();
  1186. graphics_->SetColorWrite(true);
  1187. graphics_->SetStencilTest(true, CMP_EQUAL, OP_KEEP, OP_KEEP, OP_KEEP, lightStencilValue_);
  1188. // Increase stencil value for next light
  1189. ++lightStencilValue_;
  1190. }
  1191. else
  1192. graphics_->SetStencilTest(false);
  1193. #endif
  1194. }
  1195. const Rect& Renderer::GetLightScissor(Light* light, Camera* camera)
  1196. {
  1197. Pair<Light*, Camera*> combination(light, camera);
  1198. HashMap<Pair<Light*, Camera*>, Rect>::Iterator i = lightScissorCache_.Find(combination);
  1199. if (i != lightScissorCache_.End())
  1200. return i->second_;
  1201. const Matrix3x4& view = camera->GetView();
  1202. const Matrix4& projection = camera->GetProjection();
  1203. assert(light->GetLightType() != LIGHT_DIRECTIONAL);
  1204. if (light->GetLightType() == LIGHT_SPOT)
  1205. {
  1206. Frustum viewFrustum(light->GetFrustum().Transformed(view));
  1207. return lightScissorCache_[combination] = viewFrustum.Projected(projection);
  1208. }
  1209. else // LIGHT_POINT
  1210. {
  1211. BoundingBox viewBox(light->GetWorldBoundingBox().Transformed(view));
  1212. return lightScissorCache_[combination] = viewBox.Projected(projection);
  1213. }
  1214. }
  1215. void Renderer::PrepareViewRender()
  1216. {
  1217. ResetScreenBufferAllocations();
  1218. lightScissorCache_.Clear();
  1219. lightStencilValue_ = 1;
  1220. }
  1221. void Renderer::RemoveUnusedBuffers()
  1222. {
  1223. for (unsigned i = occlusionBuffers_.Size() - 1; i < occlusionBuffers_.Size(); --i)
  1224. {
  1225. if (occlusionBuffers_[i]->GetUseTimer() > MAX_BUFFER_AGE)
  1226. {
  1227. LOGDEBUG("Removed unused occlusion buffer");
  1228. occlusionBuffers_.Erase(i);
  1229. }
  1230. }
  1231. for (HashMap<long long, Vector<SharedPtr<Texture2D> > >::Iterator i = screenBuffers_.Begin(); i != screenBuffers_.End();)
  1232. {
  1233. HashMap<long long, Vector<SharedPtr<Texture2D> > >::Iterator current = i++;
  1234. Vector<SharedPtr<Texture2D> >& buffers = current->second_;
  1235. for (unsigned j = buffers.Size() - 1; j < buffers.Size(); --j)
  1236. {
  1237. Texture2D* buffer = buffers[j];
  1238. if (buffer->GetUseTimer() > MAX_BUFFER_AGE)
  1239. {
  1240. LOGDEBUG("Removed unused screen buffer size " + String(buffer->GetWidth()) + "x" + String(buffer->GetHeight()) + " format " + String(buffer->GetFormat()));
  1241. buffers.Erase(j);
  1242. }
  1243. }
  1244. if (buffers.Empty())
  1245. {
  1246. screenBufferAllocations_.Erase(current->first_);
  1247. screenBuffers_.Erase(current);
  1248. }
  1249. }
  1250. }
  1251. void Renderer::ResetShadowMapAllocations()
  1252. {
  1253. for (HashMap<int, PODVector<Light*> >::Iterator i = shadowMapAllocations_.Begin(); i != shadowMapAllocations_.End(); ++i)
  1254. i->second_.Clear();
  1255. }
  1256. void Renderer::ResetScreenBufferAllocations()
  1257. {
  1258. for (HashMap<long long, unsigned>::Iterator i = screenBufferAllocations_.Begin(); i != screenBufferAllocations_.End(); ++i)
  1259. i->second_ = 0;
  1260. }
  1261. void Renderer::Initialize()
  1262. {
  1263. Graphics* graphics = GetSubsystem<Graphics>();
  1264. ResourceCache* cache = GetSubsystem<ResourceCache>();
  1265. if (!graphics || !graphics->IsInitialized() || !cache)
  1266. return;
  1267. PROFILE(InitRenderer);
  1268. graphics_ = graphics;
  1269. if (!graphics_->GetShadowMapFormat())
  1270. drawShadows_ = false;
  1271. defaultLightRamp_ = cache->GetResource<Texture2D>("Textures/Ramp.png");
  1272. defaultLightSpot_ = cache->GetResource<Texture2D>("Textures/Spot.png");
  1273. defaultMaterial_ = cache->GetResource<Material>("Materials/Default.xml");
  1274. // If default material not found, create one. This will actually not render properly, but prevents crashing
  1275. if (!defaultMaterial_)
  1276. defaultMaterial_ = new Material(context_);
  1277. defaultRenderPath_ = new RenderPath();
  1278. defaultRenderPath_->Load(cache->GetResource<XMLFile>("RenderPaths/Forward.xml"));
  1279. CreateGeometries();
  1280. CreateInstancingBuffer();
  1281. viewports_.Resize(1);
  1282. ResetViews();
  1283. ResetShadowMaps();
  1284. ResetBuffers();
  1285. shadersDirty_ = true;
  1286. initialized_ = true;
  1287. SubscribeToEvent(E_RENDERUPDATE, HANDLER(Renderer, HandleRenderUpdate));
  1288. LOGINFO("Initialized renderer");
  1289. }
  1290. void Renderer::ResetViews()
  1291. {
  1292. views_.Clear();
  1293. numViews_ = 0;
  1294. }
  1295. void Renderer::LoadShaders()
  1296. {
  1297. LOGDEBUG("Reloading shaders");
  1298. // Release old material shaders, mark them for reload
  1299. ReleaseMaterialShaders();
  1300. shadersChangedFrameNumber_ = GetSubsystem<Time>()->GetFrameNumber();
  1301. // Load inbuilt shaders
  1302. stencilVS_ = GetVertexShader("Stencil");
  1303. stencilPS_ = GetPixelShader("Stencil");
  1304. shadersDirty_ = false;
  1305. }
  1306. void Renderer::LoadPassShaders(Technique* tech, StringHash type)
  1307. {
  1308. Pass* pass = tech->GetPass(type);
  1309. if (!pass)
  1310. return;
  1311. PROFILE(LoadPassShaders);
  1312. unsigned shadows = (graphics_->GetHardwareShadowSupport() ? 1 : 0) | (shadowQuality_ & SHADOWQUALITY_HIGH_16BIT);
  1313. String vertexShaderName = pass->GetVertexShader();
  1314. String pixelShaderName = pass->GetPixelShader();
  1315. // Check if the shader name is already a variation in itself
  1316. if (!vertexShaderName.Contains('_'))
  1317. vertexShaderName += "_";
  1318. if (!pixelShaderName.Contains('_'))
  1319. pixelShaderName += "_";
  1320. Vector<SharedPtr<ShaderVariation> >& vertexShaders = pass->GetVertexShaders();
  1321. Vector<SharedPtr<ShaderVariation> >& pixelShaders = pass->GetPixelShaders();
  1322. // Forget all the old shaders
  1323. vertexShaders.Clear();
  1324. pixelShaders.Clear();
  1325. if (pass->GetLightingMode() == LIGHTING_PERPIXEL)
  1326. {
  1327. // Load forward pixel lit variations
  1328. vertexShaders.Resize(MAX_GEOMETRYTYPES * MAX_LIGHT_VS_VARIATIONS);
  1329. pixelShaders.Resize(MAX_LIGHT_PS_VARIATIONS);
  1330. for (unsigned j = 0; j < MAX_GEOMETRYTYPES * MAX_LIGHT_VS_VARIATIONS; ++j)
  1331. {
  1332. unsigned g = j / MAX_LIGHT_VS_VARIATIONS;
  1333. unsigned l = j % MAX_LIGHT_VS_VARIATIONS;
  1334. vertexShaders[j] = GetVertexShader(vertexShaderName + lightVSVariations[l] + geometryVSVariations[g], g != 0);
  1335. }
  1336. for (unsigned j = 0; j < MAX_LIGHT_PS_VARIATIONS; ++j)
  1337. {
  1338. if (j & LPS_SHADOW)
  1339. pixelShaders[j] = GetPixelShader(pixelShaderName + lightPSVariations[j] + shadowVariations[shadows]);
  1340. else
  1341. pixelShaders[j] = GetPixelShader(pixelShaderName + lightPSVariations[j]);
  1342. }
  1343. }
  1344. else
  1345. {
  1346. // Load vertex light variations
  1347. if (pass->GetLightingMode() == LIGHTING_PERVERTEX)
  1348. {
  1349. vertexShaders.Resize(MAX_VERTEXLIGHT_VS_VARIATIONS * MAX_GEOMETRYTYPES);
  1350. for (unsigned j = 0; j < MAX_GEOMETRYTYPES * MAX_VERTEXLIGHT_VS_VARIATIONS; ++j)
  1351. {
  1352. unsigned g = j / MAX_VERTEXLIGHT_VS_VARIATIONS;
  1353. unsigned l = j % MAX_VERTEXLIGHT_VS_VARIATIONS;
  1354. vertexShaders[j] = GetVertexShader(vertexShaderName + vertexLightVSVariations[l] + geometryVSVariations[g],
  1355. g != 0 || l != 0);
  1356. }
  1357. }
  1358. else
  1359. {
  1360. vertexShaders.Resize(MAX_GEOMETRYTYPES);
  1361. for (unsigned j = 0; j < MAX_GEOMETRYTYPES; ++j)
  1362. vertexShaders[j] = GetVertexShader(vertexShaderName + geometryVSVariations[j], j != 0);
  1363. }
  1364. pixelShaders.Resize(1);
  1365. pixelShaders[0] = GetPixelShader(pixelShaderName);
  1366. }
  1367. pass->MarkShadersLoaded(shadersChangedFrameNumber_);
  1368. }
  1369. void Renderer::ReleaseMaterialShaders()
  1370. {
  1371. ResourceCache* cache = GetSubsystem<ResourceCache>();
  1372. PODVector<Material*> materials;
  1373. cache->GetResources<Material>(materials);
  1374. for (unsigned i = 0; i < materials.Size(); ++i)
  1375. materials[i]->ReleaseShaders();
  1376. }
  1377. void Renderer::ReloadTextures()
  1378. {
  1379. ResourceCache* cache = GetSubsystem<ResourceCache>();
  1380. PODVector<Resource*> textures;
  1381. cache->GetResources(textures, Texture2D::GetTypeStatic());
  1382. for (unsigned i = 0; i < textures.Size(); ++i)
  1383. cache->ReloadResource(textures[i]);
  1384. cache->GetResources(textures, TextureCube::GetTypeStatic());
  1385. for (unsigned i = 0; i < textures.Size(); ++i)
  1386. cache->ReloadResource(textures[i]);
  1387. }
  1388. void Renderer::CreateGeometries()
  1389. {
  1390. SharedPtr<VertexBuffer> dlvb(new VertexBuffer(context_));
  1391. dlvb->SetShadowed(true);
  1392. dlvb->SetSize(4, MASK_POSITION);
  1393. dlvb->SetData(dirLightVertexData);
  1394. SharedPtr<IndexBuffer> dlib(new IndexBuffer(context_));
  1395. dlib->SetShadowed(true);
  1396. dlib->SetSize(6, false);
  1397. dlib->SetData(dirLightIndexData);
  1398. dirLightGeometry_ = new Geometry(context_);
  1399. dirLightGeometry_->SetVertexBuffer(0, dlvb);
  1400. dirLightGeometry_->SetIndexBuffer(dlib);
  1401. dirLightGeometry_->SetDrawRange(TRIANGLE_LIST, 0, dlib->GetIndexCount());
  1402. SharedPtr<VertexBuffer> slvb(new VertexBuffer(context_));
  1403. slvb->SetShadowed(true);
  1404. slvb->SetSize(8, MASK_POSITION);
  1405. slvb->SetData(spotLightVertexData);
  1406. SharedPtr<IndexBuffer> slib(new IndexBuffer(context_));
  1407. slib->SetShadowed(true);
  1408. slib->SetSize(36, false);
  1409. slib->SetData(spotLightIndexData);
  1410. spotLightGeometry_ = new Geometry(context_);
  1411. spotLightGeometry_->SetVertexBuffer(0, slvb);
  1412. spotLightGeometry_->SetIndexBuffer(slib);
  1413. spotLightGeometry_->SetDrawRange(TRIANGLE_LIST, 0, slib->GetIndexCount());
  1414. SharedPtr<VertexBuffer> plvb(new VertexBuffer(context_));
  1415. plvb->SetShadowed(true);
  1416. plvb->SetSize(24, MASK_POSITION);
  1417. plvb->SetData(pointLightVertexData);
  1418. SharedPtr<IndexBuffer> plib(new IndexBuffer(context_));
  1419. plib->SetShadowed(true);
  1420. plib->SetSize(132, false);
  1421. plib->SetData(pointLightIndexData);
  1422. pointLightGeometry_ = new Geometry(context_);
  1423. pointLightGeometry_->SetVertexBuffer(0, plvb);
  1424. pointLightGeometry_->SetIndexBuffer(plib);
  1425. pointLightGeometry_->SetDrawRange(TRIANGLE_LIST, 0, plib->GetIndexCount());
  1426. #if !defined(USE_OPENGL) || !defined(GL_ES_VERSION_2_0)
  1427. if (graphics_->GetShadowMapFormat())
  1428. {
  1429. faceSelectCubeMap_ = new TextureCube(context_);
  1430. faceSelectCubeMap_->SetNumLevels(1);
  1431. faceSelectCubeMap_->SetSize(1, graphics_->GetRGBAFormat());
  1432. faceSelectCubeMap_->SetFilterMode(FILTER_NEAREST);
  1433. indirectionCubeMap_ = new TextureCube(context_);
  1434. indirectionCubeMap_->SetNumLevels(1);
  1435. indirectionCubeMap_->SetSize(256, graphics_->GetRGBAFormat());
  1436. indirectionCubeMap_->SetFilterMode(FILTER_BILINEAR);
  1437. indirectionCubeMap_->SetAddressMode(COORD_U, ADDRESS_CLAMP);
  1438. indirectionCubeMap_->SetAddressMode(COORD_V, ADDRESS_CLAMP);
  1439. indirectionCubeMap_->SetAddressMode(COORD_W, ADDRESS_CLAMP);
  1440. SetIndirectionTextureData();
  1441. }
  1442. #endif
  1443. }
  1444. void Renderer::SetIndirectionTextureData()
  1445. {
  1446. unsigned char data[256 * 256 * 4];
  1447. for (unsigned i = 0; i < MAX_CUBEMAP_FACES; ++i)
  1448. {
  1449. unsigned axis = i / 2;
  1450. data[0] = (axis == 0) ? 255 : 0;
  1451. data[1] = (axis == 1) ? 255 : 0;
  1452. data[2] = (axis == 2) ? 255 : 0;
  1453. data[3] = 0;
  1454. faceSelectCubeMap_->SetData((CubeMapFace)i, 0, 0, 0, 1, 1, data);
  1455. }
  1456. for (unsigned i = 0; i < MAX_CUBEMAP_FACES; ++i)
  1457. {
  1458. unsigned char faceX = (i & 1) * 255;
  1459. unsigned char faceY = (i / 2) * 255 / 3;
  1460. unsigned char* dest = data;
  1461. for (unsigned y = 0; y < 256; ++y)
  1462. {
  1463. for (unsigned x = 0; x < 256; ++x)
  1464. {
  1465. #ifdef USE_OPENGL
  1466. dest[0] = x;
  1467. dest[1] = 255 - y;
  1468. dest[2] = faceX;
  1469. dest[3] = 255 * 2 / 3 - faceY;
  1470. #else
  1471. dest[0] = x;
  1472. dest[1] = y;
  1473. dest[2] = faceX;
  1474. dest[3] = faceY;
  1475. #endif
  1476. dest += 4;
  1477. }
  1478. }
  1479. indirectionCubeMap_->SetData((CubeMapFace)i, 0, 0, 0, 256, 256, data);
  1480. }
  1481. faceSelectCubeMap_->ClearDataLost();
  1482. indirectionCubeMap_->ClearDataLost();
  1483. }
  1484. void Renderer::CreateInstancingBuffer()
  1485. {
  1486. // Do not create buffer if instancing not supported
  1487. if (!graphics_->GetInstancingSupport())
  1488. {
  1489. instancingBuffer_.Reset();
  1490. dynamicInstancing_ = false;
  1491. return;
  1492. }
  1493. // If must lock the buffer for each batch group, set a smaller size
  1494. unsigned defaultSize = graphics_->GetStreamOffsetSupport() ? INSTANCING_BUFFER_DEFAULT_SIZE : INSTANCING_BUFFER_DEFAULT_SIZE / 4;
  1495. instancingBuffer_ = new VertexBuffer(context_);
  1496. if (!instancingBuffer_->SetSize(defaultSize, INSTANCING_BUFFER_MASK, true))
  1497. {
  1498. instancingBuffer_.Reset();
  1499. dynamicInstancing_ = false;
  1500. }
  1501. }
  1502. void Renderer::ResetShadowMaps()
  1503. {
  1504. shadowMaps_.Clear();
  1505. shadowMapAllocations_.Clear();
  1506. colorShadowMaps_.Clear();
  1507. }
  1508. void Renderer::ResetBuffers()
  1509. {
  1510. occlusionBuffers_.Clear();
  1511. screenBuffers_.Clear();
  1512. screenBufferAllocations_.Clear();
  1513. }
  1514. void Renderer::HandleScreenMode(StringHash eventType, VariantMap& eventData)
  1515. {
  1516. if (!initialized_)
  1517. Initialize();
  1518. else
  1519. {
  1520. // When screen mode changes, purge old views
  1521. ResetViews();
  1522. }
  1523. }
  1524. void Renderer::HandleGraphicsFeatures(StringHash eventType, VariantMap& eventData)
  1525. {
  1526. // Reinitialize if already initialized
  1527. if (initialized_)
  1528. Initialize();
  1529. }
  1530. void Renderer::HandleRenderUpdate(StringHash eventType, VariantMap& eventData)
  1531. {
  1532. using namespace RenderUpdate;
  1533. Update(eventData[P_TIMESTEP].GetFloat());
  1534. }
  1535. }