Renderer.cpp 56 KB

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