Renderer.cpp 55 KB

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