Renderer.cpp 56 KB

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