llviewerdisplay.cpp 45 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748
  1. /**
  2. * @file llviewerdisplay.cpp
  3. * @brief LLViewerDisplay class implementation
  4. *
  5. * $LicenseInfo:firstyear=2004&license=viewergpl$
  6. *
  7. * Copyright (c) 2004-2009, Linden Research, Inc.
  8. *
  9. * Second Life Viewer Source Code
  10. * The source code in this file ("Source Code") is provided by Linden Lab
  11. * to you under the terms of the GNU General Public License, version 2.0
  12. * ("GPL"), unless you have obtained a separate licensing agreement
  13. * ("Other License"), formally executed by you and Linden Lab. Terms of
  14. * the GPL can be found in doc/GPL-license.txt in this distribution, or
  15. * online at http://secondlifegrid.net/programs/open_source/licensing/gplv2
  16. *
  17. * There are special exceptions to the terms and conditions of the GPL as
  18. * it is applied to this Source Code. View the full text of the exception
  19. * in the file doc/FLOSS-exception.txt in this software distribution, or
  20. * online at
  21. * http://secondlifegrid.net/programs/open_source/licensing/flossexception
  22. *
  23. * By copying, modifying or distributing this software, you acknowledge
  24. * that you have read and understood your obligations described above,
  25. * and agree to abide by those obligations.
  26. *
  27. * ALL LINDEN LAB SOURCE CODE IS PROVIDED "AS IS." LINDEN LAB MAKES NO
  28. * WARRANTIES, EXPRESS, IMPLIED OR OTHERWISE, REGARDING ITS ACCURACY,
  29. * COMPLETENESS OR PERFORMANCE.
  30. * $/LicenseInfo$
  31. */
  32. #include "llviewerprecompiledheaders.h"
  33. #include "llviewerdisplay.h"
  34. #include "llapp.h"
  35. #include "llcubemap.h"
  36. #include "lldir.h"
  37. #include "llfasttimer.h"
  38. #include "llimagebmp.h"
  39. #include "llimagedecodethread.h"
  40. #include "llimagegl.h"
  41. #include "llrender.h"
  42. #include "llagent.h"
  43. #include "llappviewer.h"
  44. #include "lldynamictexture.h"
  45. #include "lldrawpoolalpha.h"
  46. #include "lldrawpoolbump.h"
  47. #include "lldrawpoolwater.h"
  48. #include "llenvironment.h"
  49. #include "llfeaturemanager.h"
  50. #include "llfloatertools.h"
  51. #include "llgltfmateriallist.h"
  52. #include "llgridmanager.h" // For gIsInProductionGrid
  53. #include "llhudmanager.h"
  54. #include "llpipeline.h"
  55. //MK
  56. #include "mkrlinterface.h"
  57. //mk
  58. #include "llselectmgr.h"
  59. #include "llsky.h"
  60. #include "llspatialpartition.h"
  61. #include "llstartup.h"
  62. #include "llstatusbar.h"
  63. #include "lltooldraganddrop.h"
  64. #include "lltoolfocus.h"
  65. #include "lltoolmgr.h"
  66. #include "lltoolpie.h"
  67. #include "lltracker.h"
  68. #include "llviewercamera.h"
  69. #include "llviewercontrol.h"
  70. #include "llviewerobjectlist.h"
  71. #include "llviewerparcelmgr.h"
  72. #include "llviewerregion.h"
  73. #include "llviewershadermgr.h"
  74. #include "llviewertexturelist.h"
  75. #include "llviewerwindow.h"
  76. #include "llvoavatarself.h"
  77. #include "llvograss.h"
  78. #include "llworld.h"
  79. extern LLPointer<LLViewerTexture> gStartTexture;
  80. LLPointer<LLViewerTexture> gDisconnectedImagep = NULL;
  81. // This is how long the sim will try to teleport you before giving up.
  82. constexpr F32 TELEPORT_EXPIRY = 15.f;
  83. // Additional time (in seconds) to wait per attachment
  84. constexpr F32 TELEPORT_EXPIRY_PER_ATTACHMENT = 3.f;
  85. // Constants used to toggle renderer back on after teleport
  86. // Time to preload the world before raising the curtain after we've actually
  87. // already arrived.
  88. constexpr F32 TELEPORT_ARRIVAL_DELAY = 2.f;
  89. // Delay to prevent teleports after starting an in-sim teleport.
  90. constexpr F32 TELEPORT_LOCAL_DELAY = 1.f;
  91. // Wait this long while reloading textures before we raise the curtain
  92. constexpr F32 RESTORE_GL_TIME = 5.f;
  93. // Globals
  94. LLFrameTimer gTeleportDisplayTimer;
  95. LLFrameTimer gTeleportArrivalTimer;
  96. bool gTeleportDisplay = false;
  97. bool gUpdateDrawDistance = false;
  98. F32 gSavedDrawDistance = 0.f;
  99. bool gForceRenderLandFence = false;
  100. bool gDisplaySwapBuffers = false;
  101. bool gDepthDirty = false;
  102. bool gResizeScreenTexture = false;
  103. bool gResizeShadowTexture = false;
  104. bool gSnapshot = false;
  105. bool gCubeSnapshot = false;
  106. bool gShaderProfileFrame = false;
  107. bool gScreenIsDirty = false;
  108. U32 gRecentFrameCount = 0; // Number of 'recent' frames
  109. U32 gLastFPSAverage = 0;
  110. LLFrameTimer gRecentFPSTime;
  111. LLFrameTimer gRecentMemoryTime;
  112. // Rendering stuff
  113. void render_hud_attachments();
  114. void render_ui_3d();
  115. void render_ui_2d();
  116. void render_disconnected_background();
  117. void display_startup()
  118. {
  119. if (!gViewerWindowp || !gViewerWindowp->getActive() ||
  120. !gWindowp->getVisible() || gWindowp->getMinimized())
  121. {
  122. return;
  123. }
  124. gPipeline.updateGL();
  125. if (LLViewerFetchedTexture::sWhiteImagep.notNull())
  126. {
  127. LLTexUnit::sWhiteTexture =
  128. LLViewerFetchedTexture::sWhiteImagep->getTexName();
  129. }
  130. LLGLSDefault gls_default;
  131. // Required for HTML update in login screen
  132. static S32 frame_count = 0;
  133. LL_GL_CHECK_STATES;
  134. if (frame_count++ > 1) // Make sure we have rendered a frame first
  135. {
  136. LLViewerDynamicTexture::updateAllInstances();
  137. }
  138. LL_GL_CHECK_STATES;
  139. #if 0 // Only enable this should the EE renderer be gutted out. HB
  140. glClear(GL_DEPTH_BUFFER_BIT | GL_COLOR_BUFFER_BIT);
  141. #else
  142. glClear(GL_DEPTH_BUFFER_BIT | GL_COLOR_BUFFER_BIT | GL_STENCIL_BUFFER_BIT);
  143. #endif
  144. LLGLSUIDefault gls_ui;
  145. gPipeline.disableLights();
  146. gViewerWindowp->setup2DRender();
  147. gViewerWindowp->draw();
  148. gGL.flush();
  149. LLVertexBuffer::unbind();
  150. LL_GL_CHECK_STATES;
  151. gWindowp->swapBuffers();
  152. glClear(GL_DEPTH_BUFFER_BIT);
  153. }
  154. void display_update_camera()
  155. {
  156. F32 farclip;
  157. if (gCubeSnapshot)
  158. {
  159. static LLCachedControl<F32> dist(gSavedSettings,
  160. "RenderReflectionProbeDrawDistance");
  161. farclip = llclamp(F32(dist), 32.f, 1024.f);
  162. }
  163. else
  164. {
  165. farclip = gAgent.mDrawDistance;
  166. }
  167. gViewerCamera.setFar(farclip);
  168. gViewerWindowp->setup3DRender();
  169. if (!gCubeSnapshot)
  170. {
  171. // Update land visibility
  172. gWorld.setLandFarClip(farclip);
  173. }
  174. }
  175. // Write some stats to llinfos
  176. void display_stats()
  177. {
  178. static LLCachedControl<S32> background_yield_time(gSavedSettings,
  179. "BackgroundYieldTime");
  180. if (!gWindowp->getVisible() ||
  181. (background_yield_time > 0 && !gFocusMgr.getAppHasFocus()))
  182. {
  183. // Do not keep FPS statistics while yielding cooperatively or not
  184. // visible.
  185. gRecentFrameCount = 0;
  186. gRecentFPSTime.reset();
  187. }
  188. static LLCachedControl<F32> fps_log_freq(gSavedSettings,
  189. "FPSLogFrequency");
  190. if (fps_log_freq > 0.f)
  191. {
  192. F32 ellapsed = gRecentFPSTime.getElapsedTimeF32();
  193. if (ellapsed >= fps_log_freq)
  194. {
  195. F32 fps = gRecentFrameCount / ellapsed;
  196. llinfos << llformat("FPS: %.02f", fps) << llendl;
  197. gLastFPSAverage = (U32)fps;
  198. gRecentFrameCount = 0;
  199. gRecentFPSTime.reset();
  200. }
  201. else if (ellapsed >= 10.f)
  202. {
  203. gLastFPSAverage = (U32)gRecentFrameCount / ellapsed;
  204. }
  205. }
  206. static LLCachedControl<F32> mem_log_freq(gSavedSettings,
  207. "MemoryLogFrequency");
  208. if (mem_log_freq > 0.f &&
  209. gRecentMemoryTime.getElapsedTimeF32() >= mem_log_freq)
  210. {
  211. gMemoryAllocated = LLMemory::getCurrentRSS();
  212. U32 memory = (U32)(gMemoryAllocated / 1048576UL);
  213. llinfos << llformat("MEMORY: %d MB", memory) << llendl;
  214. gRecentMemoryTime.reset();
  215. }
  216. }
  217. static void update_tp_display(bool minimized)
  218. {
  219. S32 attach_count = 0;
  220. if (isAgentAvatarValid())
  221. {
  222. attach_count = gAgentAvatarp->getNumAttachments();
  223. }
  224. F32 teleport_save_time = TELEPORT_EXPIRY +
  225. TELEPORT_EXPIRY_PER_ATTACHMENT * attach_count;
  226. F32 teleport_elapsed = gTeleportDisplayTimer.getElapsedTimeF32();
  227. F32 teleport_percent = teleport_elapsed * 100.f / teleport_save_time;
  228. LLAgent::ETeleportState tp_state = gAgent.getTeleportState();
  229. if (teleport_percent > 100.f && tp_state != LLAgent::TELEPORT_START &&
  230. tp_state != LLAgent::TELEPORT_QUEUED)
  231. {
  232. // Give up. Do not keep the UI locked forever.
  233. LL_DEBUGS("Teleport") << "TP timeout ?... Resetting to TELEPORT_NONE"
  234. << LL_ENDL;
  235. gAgent.setTeleportState(LLAgent::TELEPORT_NONE);
  236. gAgent.setTeleportMessage("");
  237. }
  238. if (minimized)
  239. {
  240. gViewerWindowp->setShowProgress(false);
  241. }
  242. static LLCachedControl<bool> hide_tp_progress(gSavedSettings,
  243. "HideTeleportProgress");
  244. bool show_tp_progress = !hide_tp_progress && !minimized;
  245. const std::string& message = gAgent.getTeleportMessage();
  246. switch (gAgent.getTeleportState())
  247. {
  248. case LLAgent::TELEPORT_NONE:
  249. // No teleport in progress
  250. gViewerWindowp->setShowProgress(false);
  251. gTeleportDisplay = false;
  252. gTeleportArrivalTimer.reset();
  253. break;
  254. case LLAgent::TELEPORT_START:
  255. // Transition to REQUESTED. Viewer has sent some kind of
  256. // TeleportRequest to the source simulator.
  257. gTeleportDisplayTimer.reset();
  258. if (show_tp_progress)
  259. {
  260. gViewerWindowp->setShowProgress(true);
  261. gViewerWindowp->setProgressPercent(0);
  262. gAgent.setTeleportMessage(LLAgent::sTeleportProgressMessages["requesting"]);
  263. }
  264. // Release geometry from old location
  265. gPipeline.resetVertexBuffers();
  266. LLSpatialPartition::sTeleportRequested = true;
  267. gAgent.setTeleportState(LLAgent::TELEPORT_REQUESTED);
  268. break;
  269. case LLAgent::TELEPORT_REQUESTED:
  270. // Waiting for source simulator to respond
  271. if (show_tp_progress)
  272. {
  273. gViewerWindowp->setProgressPercent(llmin(teleport_percent,
  274. 37.5f));
  275. gViewerWindowp->setProgressString(message);
  276. }
  277. break;
  278. case LLAgent::TELEPORT_MOVING:
  279. // Viewer has received destination location from source simulator
  280. if (show_tp_progress)
  281. {
  282. gViewerWindowp->setProgressPercent(llmin(teleport_percent,
  283. 75.f));
  284. gViewerWindowp->setProgressString(message);
  285. }
  286. break;
  287. case LLAgent::TELEPORT_START_ARRIVAL:
  288. // Transition to ARRIVING. Viewer has received avatar update, etc,
  289. // from destination simulator
  290. gTeleportArrivalTimer.reset();
  291. if (show_tp_progress)
  292. {
  293. gViewerWindowp->setProgressCancelButtonVisible(false);
  294. gViewerWindowp->setProgressPercent(75.f);
  295. gAgent.setTeleportMessage(LLAgent::sTeleportProgressMessages["arriving"]);
  296. }
  297. gAgent.setTeleportState(LLAgent::TELEPORT_ARRIVING);
  298. if (gSavedSettings.getBool("DisablePrecacheDelayAfterTP"))
  299. {
  300. LL_DEBUGS("Teleport") << "No pre-caching, switching to TELEPORT_NONE"
  301. << LL_ENDL;
  302. gAgent.setTeleportState(LLAgent::TELEPORT_NONE);
  303. }
  304. break;
  305. case LLAgent::TELEPORT_ARRIVING:
  306. {
  307. // Make the user wait while content "pre-caches"
  308. F32 percent = gTeleportArrivalTimer.getElapsedTimeF32() /
  309. TELEPORT_ARRIVAL_DELAY;
  310. if (!show_tp_progress || percent > 1.f)
  311. {
  312. percent = 1.f;
  313. LL_DEBUGS("Teleport") << "Arrived. Switching to TELEPORT_NONE"
  314. << LL_ENDL;
  315. gAgent.setTeleportState(LLAgent::TELEPORT_NONE);
  316. }
  317. if (show_tp_progress)
  318. {
  319. gViewerWindowp->setProgressCancelButtonVisible(false);
  320. gViewerWindowp->setProgressPercent(percent * 25.f + 75.f);
  321. gViewerWindowp->setProgressString(message);
  322. }
  323. break;
  324. }
  325. case LLAgent::TELEPORT_LOCAL:
  326. // Short delay when teleporting in the same sim (progress screen
  327. // active but not shown; did not fall-through from TELEPORT_START)
  328. if (gTeleportDisplayTimer.getElapsedTimeF32() > TELEPORT_LOCAL_DELAY)
  329. {
  330. LL_DEBUGS("Teleport") << "Local TP done, switching to TELEPORT_NONE"
  331. << LL_ENDL;
  332. gAgent.setTeleportState(LLAgent::TELEPORT_NONE);
  333. }
  334. break;
  335. case LLAgent::TELEPORT_QUEUED:
  336. gTeleportDisplayTimer.reset();
  337. if (show_tp_progress)
  338. {
  339. gViewerWindowp->setShowProgress(true);
  340. gViewerWindowp->setProgressPercent(0);
  341. gAgent.setTeleportMessage(LLAgent::sTeleportProgressMessages["requesting"]);
  342. }
  343. gAgent.fireQueuedTeleport();
  344. default:
  345. break;
  346. }
  347. }
  348. // Paint the display
  349. void display(bool rebuild, F32 zoom_factor, S32 subfield, bool for_snapshot)
  350. {
  351. LL_FAST_TIMER(FTM_RENDER);
  352. if (!gViewerWindowp) return;
  353. static LLCachedControl<bool> use_pbr(gSavedSettings, "RenderUsePBR");
  354. if (gUsePBRShaders != use_pbr)
  355. {
  356. gPipeline.toggleRenderer();
  357. }
  358. stop_glerror();
  359. if (gResizeScreenTexture)
  360. {
  361. // Skip render on frames where window has been resized
  362. gGL.flush();
  363. glClear(GL_COLOR_BUFFER_BIT);
  364. gWindowp->swapBuffers();
  365. gPipeline.resizeScreenTexture();
  366. return;
  367. }
  368. if (gResizeShadowTexture)
  369. {
  370. gPipeline.resizeShadowTexture();
  371. }
  372. bool report_materials_stats = false;
  373. // Reset materials usage stats for this scene.
  374. if (!for_snapshot)
  375. {
  376. LLPipeline::sUsingALM = LLPipeline::sUsingPBR =
  377. LLPipeline::sLacksLegacyFallback = false;
  378. report_materials_stats = true;
  379. }
  380. if (LLPipeline::sRenderDeferred)
  381. {
  382. // *HACK: to make sky show up in deferred snapshots
  383. for_snapshot = false;
  384. }
  385. if (LLPipeline::sRenderFrameTest)
  386. {
  387. LLWorld::sendAgentPause();
  388. }
  389. gSnapshot = for_snapshot;
  390. LLGLSDefault gls_default;
  391. LLGLDepthTest gls_depth(GL_TRUE, GL_TRUE, GL_LEQUAL);
  392. LLVertexBuffer::unbind();
  393. LL_GL_CHECK_STATES;
  394. gPipeline.disableLights();
  395. // Reset vertex buffers if needed
  396. gPipeline.doResetVertexBuffers();
  397. stop_glerror();
  398. // Do not draw if the window is hidden or minimized. In fact, we must
  399. // explicitly check the minimized state before drawing. Attempting to draw
  400. // into a minimized window causes a GL error. JC
  401. if (!gViewerWindowp->getActive() || !gWindowp->getVisible() ||
  402. gWindowp->getMinimized())
  403. {
  404. // Clean up memory the pools may have allocated
  405. if (rebuild)
  406. {
  407. gPipeline.rebuildPools();
  408. }
  409. // Avoid accumulating HUD objects while minimized. HB
  410. LLHUDObject::removeExpired();
  411. gViewerWindowp->returnEmptyPicks();
  412. // We still need to update the teleport progress (to get changes done
  413. // in TP states, else the sim does not get the messages signaling the
  414. // agent's arrival). Of course, we do not show/update the TP screen.
  415. // This fixes BUG-230616. HB
  416. if (gTeleportDisplay)
  417. {
  418. update_tp_display(true);
  419. }
  420. return;
  421. }
  422. gViewerWindowp->checkSettings();
  423. {
  424. LL_FAST_TIMER(FTM_PICK);
  425. gViewerWindowp->performPick();
  426. }
  427. LL_GL_CHECK_STATES;
  428. //////////////////////////////////////////////////////////
  429. // Logic for forcing window updates if we are in drone mode.
  430. // Bail out if we are in the startup state and do not want to try to render
  431. // the world.
  432. if (!LLStartUp::isLoggedIn())
  433. {
  434. display_startup();
  435. gScreenIsDirty = false;
  436. return;
  437. }
  438. if (gShaderProfileFrame)
  439. {
  440. LLGLSLShader::initProfile();
  441. }
  442. #if 0
  443. LLGLState::verify(false);
  444. #endif
  445. /////////////////////////////////////////////////
  446. // Update GL Texture statistics (used for discard logic)
  447. stop_glerror();
  448. LLImageGL::updateStats(gFrameTimeSeconds);
  449. static LLCachedControl<S32> render_name(gSavedSettings, "RenderName");
  450. static LLCachedControl<bool> hide_all_titles(gSavedSettings,
  451. "RenderHideGroupTitleAll");
  452. LLVOAvatar::sRenderName = render_name;
  453. LLVOAvatar::sRenderGroupTitles = !hide_all_titles;
  454. ++gRecentFrameCount;
  455. LLImageGL::freeDeletedTextures();
  456. gGL.cleanupVertexBufferCache(++gFrameCount);
  457. LLImageGL::updateStats(gFrameTimeSeconds);
  458. //////////////////////////////////////////////////////////
  459. // Display start screen if we are teleporting, and skip render
  460. if (gTeleportDisplay)
  461. {
  462. update_tp_display(false);
  463. }
  464. else if (gAppViewerp->logoutRequestSent())
  465. {
  466. F32 percent_done = gLogoutTimer.getElapsedTimeF32() * 100.f /
  467. gLogoutMaxTime;
  468. if (percent_done > 100.f)
  469. {
  470. percent_done = 100.f;
  471. }
  472. if (LLApp::isExiting())
  473. {
  474. percent_done = 100.f;
  475. }
  476. gViewerWindowp->setProgressPercent(percent_done);
  477. }
  478. else if (gRestoreGL)
  479. {
  480. F32 percent_done = gRestoreGLTimer.getElapsedTimeF32() * 100.f /
  481. RESTORE_GL_TIME;
  482. if (percent_done > 100.f || LLApp::isExiting())
  483. {
  484. gViewerWindowp->setShowProgress(false);
  485. gRestoreGL = false;
  486. }
  487. else
  488. {
  489. gViewerWindowp->setProgressPercent(percent_done);
  490. }
  491. }
  492. // Progressively increase draw distance after TP when required and when
  493. // possible (enough available memory). HB
  494. else if (gSavedDrawDistance > 0.f && !gAgent.teleportInProgress() &&
  495. LLViewerTexture::sDesiredDiscardBias <= 2.5f)
  496. {
  497. static LLCachedControl<U32> speed_rez_interval(gSavedSettings,
  498. "SpeedRezInterval");
  499. if (gTeleportArrivalTimer.getElapsedTimeF32() >= (F32)speed_rez_interval)
  500. {
  501. gTeleportArrivalTimer.reset();
  502. F32 current = gSavedSettings.getF32("RenderFarClip");
  503. if (gSavedDrawDistance > current)
  504. {
  505. current *= 2.f;
  506. if (current > gSavedDrawDistance)
  507. {
  508. current = gSavedDrawDistance;
  509. }
  510. gSavedSettings.setF32("RenderFarClip", current);
  511. }
  512. if (current >= gSavedDrawDistance)
  513. {
  514. gSavedDrawDistance = 0.f;
  515. gSavedSettings.setF32("SavedRenderFarClip", 0.f);
  516. }
  517. }
  518. }
  519. // We do this here instead of inside of handleRenderFarClipChanged() in
  520. // llviewercontrol.cpp to ensure this is not done during rendering, which
  521. // would cause drawables to get destroyed while LLSpatialGroup::sNoDelete
  522. // is true and would therefore cause a mess. HB
  523. if (gUpdateDrawDistance)
  524. {
  525. gUpdateDrawDistance = false;
  526. F32 draw_distance = gSavedSettings.getF32("RenderFarClip");
  527. gAgent.mDrawDistance = draw_distance;
  528. gWorld.setLandFarClip(draw_distance);
  529. LLVOCacheEntry::updateSettings();
  530. }
  531. //////////////////////////
  532. // Prepare for the next frame
  533. // Update the camera
  534. gViewerCamera.setZoomParameters(zoom_factor, subfield);
  535. gViewerCamera.setNear(MIN_NEAR_PLANE);
  536. if (gDisconnected)
  537. {
  538. render_ui();
  539. }
  540. //////////////////////////
  541. // Set rendering options
  542. stop_glerror();
  543. ///////////////////////////////////////
  544. // Slam lighting parameters back to our defaults.
  545. // Note that these are not the same as GL defaults...
  546. gGL.setAmbientLightColor(LLColor4::white);
  547. /////////////////////////////////////
  548. // Render
  549. //
  550. // Actually push all of our triangles to the screen.
  551. // Do render-to-texture stuff here
  552. if (gPipeline.hasRenderDebugFeatureMask(LLPipeline::RENDER_DEBUG_FEATURE_DYNAMIC_TEXTURES))
  553. {
  554. LL_FAST_TIMER(FTM_UPDATE_TEXTURES);
  555. if (LLViewerDynamicTexture::updateAllInstances())
  556. {
  557. gGL.setColorMask(true, true);
  558. glClear(GL_DEPTH_BUFFER_BIT);
  559. }
  560. }
  561. gViewerWindowp->setupViewport();
  562. // Reset per-frame statistics.
  563. gPipeline.resetFrameStats();
  564. LLViewerTextureList::resetFrameStats();
  565. if (!gDisconnected && !LLApp::isExiting())
  566. {
  567. if (!gSnapshot && !gTeleportDisplay && LLPipeline::sRenderMirrors)
  568. {
  569. gPipeline.mHeroProbeManager.update();
  570. gPipeline.mHeroProbeManager.renderProbes();
  571. }
  572. if (gPipeline.hasRenderType(LLPipeline::RENDER_TYPE_HUD))
  573. {
  574. // Do not draw hud objects in this frame
  575. gPipeline.toggleRenderType(LLPipeline::RENDER_TYPE_HUD);
  576. }
  577. if (gPipeline.hasRenderType(LLPipeline::RENDER_TYPE_HUD_PARTICLES))
  578. {
  579. // Do not draw hud particles in this frame
  580. gPipeline.toggleRenderType(LLPipeline::RENDER_TYPE_HUD_PARTICLES);
  581. }
  582. display_update_camera();
  583. // Update all the sky/atmospheric/water settings
  584. gEnvironment.update();
  585. stop_glerror();
  586. {
  587. LL_FAST_TIMER(FTM_HUD_UPDATE);
  588. LLHUDManager::updateEffects();
  589. LLHUDObject::updateAll();
  590. stop_glerror();
  591. }
  592. {
  593. LL_FAST_TIMER(FTM_DISPLAY_UPDATE_GEOM);
  594. // 50 ms/second update time:
  595. const F32 max_geom_update_time = 0.05f * gFrameIntervalSeconds;
  596. gPipeline.createObjects(max_geom_update_time);
  597. gPipeline.processPartitionQ();
  598. gPipeline.updateGeom(max_geom_update_time);
  599. stop_glerror();
  600. }
  601. gPipeline.updateGL();
  602. stop_glerror();
  603. // Increment drawable frame counter
  604. LLDrawable::incrementVisible();
  605. LLSpatialGroup::sNoDelete = true;
  606. if (LLViewerFetchedTexture::sWhiteImagep.notNull())
  607. {
  608. LLTexUnit::sWhiteTexture =
  609. LLViewerFetchedTexture::sWhiteImagep->getTexName();
  610. }
  611. S32 occlusion = LLPipeline::sUseOcclusion;
  612. if (gDepthDirty)
  613. {
  614. // Depth buffer is invalid, do not overwrite occlusion state
  615. LLPipeline::sUseOcclusion = llmin(occlusion, 1);
  616. gDepthDirty = false;
  617. }
  618. LL_GL_CHECK_STATES;
  619. static LLCullResult result;
  620. LLViewerCamera::sCurCameraID = LLViewerCamera::CAMERA_WORLD;
  621. LLPipeline::sUnderWaterRender = gViewerCamera.cameraUnderWater();
  622. gPipeline.updateCull(gViewerCamera, result);
  623. LL_GL_CHECK_STATES;
  624. {
  625. if (gResizeScreenTexture)
  626. {
  627. gPipeline.resizeScreenTexture();
  628. }
  629. gGL.setColorMask(true, true);
  630. glClearColor(0.f, 0.f, 0.f, 0.f);
  631. LL_GL_CHECK_STATES;
  632. if (!for_snapshot)
  633. {
  634. if (gFrameCount > 1)
  635. {
  636. // For some reason, ATI 4800 series will error out if you
  637. // try to generate a shadow before the first frame is
  638. // through
  639. gPipeline.generateSunShadow();
  640. }
  641. LLVertexBuffer::unbind();
  642. LL_GL_CHECK_STATES;
  643. const LLMatrix4a proj = gGLProjection;
  644. const LLMatrix4a mod = gGLModelView;
  645. glViewport(0, 0, 512, 512);
  646. {
  647. LL_FAST_TIMER(FTM_IMPOSTORS_UPDATE);
  648. LLViewerCamera::sCurCameraID =
  649. LLViewerCamera::CAMERA_WORLD;
  650. LLVOAvatar::updateImpostors();
  651. }
  652. gGLProjection = proj;
  653. gGLModelView = mod;
  654. gGL.matrixMode(LLRender::MM_PROJECTION);
  655. gGL.loadMatrix(proj);
  656. gGL.matrixMode(LLRender::MM_MODELVIEW);
  657. gGL.loadMatrix(mod);
  658. gViewerWindowp->setupViewport();
  659. LL_GL_CHECK_STATES;
  660. }
  661. if (gUsePBRShaders)
  662. {
  663. glClear(GL_DEPTH_BUFFER_BIT);
  664. }
  665. else
  666. {
  667. glClear(GL_DEPTH_BUFFER_BIT | GL_STENCIL_BUFFER_BIT);
  668. }
  669. }
  670. if (!gUsePBRShaders)
  671. {
  672. gPipeline.generateWaterReflection();
  673. if (gPipeline.hasRenderDebugMask(LLPipeline::RENDER_DEBUG_PHYSICS_SHAPES))
  674. {
  675. gPipeline.renderPhysicsDisplay();
  676. }
  677. LL_GL_CHECK_STATES;
  678. }
  679. //////////////////////////////////////
  680. // Update images, using the image stats generated during object update
  681. // and culling. This may put objects onto the retextured list. Doing
  682. // this here gives hardware occlusion queries extra time to complete...
  683. {
  684. LL_FAST_TIMER(FTM_IMAGE_UPDATE);
  685. {
  686. LL_FAST_TIMER(FTM_IMAGE_UPDATE_CLASS);
  687. LLViewerTexture::updateClass();
  688. }
  689. {
  690. LL_FAST_TIMER(FTM_IMAGE_UPDATE_BUMP);
  691. // Must be called before gTextureList version so that its
  692. // textures are thrown out first.
  693. gBumpImageList.updateImages();
  694. }
  695. {
  696. LL_FAST_TIMER(FTM_IMAGE_UPDATE_LIST);
  697. F32 max_image_decode_time = 0.2f * gFrameIntervalSeconds;
  698. // Min 2ms/frame, max 20ms/frame)
  699. max_image_decode_time = llclamp(max_image_decode_time, 0.002f,
  700. 0.02f);
  701. gTextureList.updateImages(max_image_decode_time);
  702. }
  703. {
  704. // Remove dead gltf materials
  705. gGLTFMaterialList.flushMaterials();
  706. }
  707. }
  708. ///////////////////////////////////
  709. // StateSort
  710. //
  711. // Responsible for taking visible objects, and adding them to the
  712. // appropriate draw orders. In the case of alpha objects, z-sorts them
  713. // first. Also creates special lists for outlines and selected face
  714. // rendering.
  715. {
  716. LLViewerCamera::sCurCameraID = LLViewerCamera::CAMERA_WORLD;
  717. gPipeline.stateSort(gViewerCamera, result);
  718. if (rebuild)
  719. {
  720. // Rebuild pools
  721. gPipeline.rebuildPools();
  722. }
  723. }
  724. LL_GL_CHECK_STATES;
  725. LLPipeline::sUseOcclusion = occlusion;
  726. {
  727. LL_FAST_TIMER(FTM_UPDATE_SKY);
  728. gSky.updateSky();
  729. }
  730. if (gUseWireframe)
  731. {
  732. glClearColor(0.5f, 0.5f, 0.5f, 0.f);
  733. glClear(GL_COLOR_BUFFER_BIT);
  734. if (!gUsePBRShaders)
  735. {
  736. glPolygonMode(GL_FRONT_AND_BACK, GL_LINE);
  737. }
  738. }
  739. LLPipeline::sUnderWaterRender = gViewerCamera.cameraUnderWater();
  740. LL_GL_CHECK_STATES;
  741. gGL.setColorMask(true, true);
  742. if (LLPipeline::sRenderDeferred)
  743. {
  744. gPipeline.mRT->mDeferredScreen.bindTarget();
  745. if (gUsePBRShaders && gUseWireframe)
  746. {
  747. glClearColor(0.5f, 0.5f, 0.5f, 1.f);
  748. }
  749. else
  750. {
  751. glClearColor(1.f, 0.f, 1.f, 1.f);
  752. }
  753. gPipeline.mRT->mDeferredScreen.clear();
  754. }
  755. else
  756. {
  757. gPipeline.mRT->mScreen.bindTarget();
  758. if (LLPipeline::sUnderWaterRender &&
  759. !gPipeline.canUseWindLightShaders())
  760. {
  761. const LLColor4& col = LLDrawPoolWater::sWaterFogColor;
  762. glClearColor(col.mV[0], col.mV[1], col.mV[2], 0.f);
  763. }
  764. gPipeline.mRT->mScreen.clear();
  765. }
  766. gGL.setColorMask(true, false);
  767. if (!gRestoreGL &&
  768. !(gAppViewerp->logoutRequestSent() &&
  769. gAppViewerp->hasSavedFinalSnapshot()))
  770. {
  771. LLViewerCamera::sCurCameraID = LLViewerCamera::CAMERA_WORLD;
  772. if (gUsePBRShaders)
  773. {
  774. gGL.setColorMask(true, true);
  775. gPipeline.renderGeomDeferred(gViewerCamera, true);
  776. }
  777. else
  778. {
  779. gGL.setColorMask(true, false);
  780. if (LLPipeline::sRenderDeferred)
  781. {
  782. gPipeline.renderGeomDeferred(gViewerCamera);
  783. }
  784. else
  785. {
  786. gPipeline.renderGeom(gViewerCamera);
  787. }
  788. gGL.setColorMask(true, true);
  789. // Store this frame's modelview matrix for use when rendering
  790. // next frame's occlusion queries
  791. gGLLastModelView = gGLModelView;
  792. gGLLastProjection = gGLProjection;
  793. }
  794. stop_glerror();
  795. }
  796. {
  797. LL_FAST_TIMER(FTM_TEXTURE_UNBIND);
  798. for (S32 i = 0; i < gGLManager.mNumTextureImageUnits; ++i)
  799. {
  800. // Dummy cleanup of any currently bound textures
  801. LLTexUnit* unitp = gGL.getTexUnit(i);
  802. if (!unitp) continue; // Paranoia
  803. LLTexUnit::eTextureType type = unitp->getCurrType();
  804. if (type != LLTexUnit::TT_NONE)
  805. {
  806. unitp->unbind(type);
  807. unitp->disable();
  808. }
  809. }
  810. }
  811. LLRenderTarget& rt =
  812. LLPipeline::sRenderDeferred ? gPipeline.mRT->mDeferredScreen
  813. : gPipeline.mRT->mScreen;
  814. rt.flush();
  815. if (!gUsePBRShaders && rt.getFBO() && LLRenderTarget::sUseFBO)
  816. {
  817. LLRenderTarget::copyContentsToFramebuffer(rt, 0, 0, rt.getWidth(),
  818. rt.getHeight(), 0, 0,
  819. rt.getWidth(),
  820. rt.getHeight(),
  821. GL_DEPTH_BUFFER_BIT,
  822. GL_NEAREST);
  823. }
  824. if (LLPipeline::sRenderDeferred)
  825. {
  826. gPipeline.renderDeferredLighting();
  827. }
  828. LLPipeline::sUnderWaterRender = false;
  829. if (!for_snapshot)
  830. {
  831. render_ui();
  832. }
  833. LLSpatialGroup::sNoDelete = false;
  834. gPipeline.clearReferences();
  835. }
  836. stop_glerror();
  837. if (report_materials_stats && gStatusBarp)
  838. {
  839. gStatusBarp->materialsUsage(LLPipeline::sUsingALM,
  840. LLPipeline::sUsingPBR,
  841. LLPipeline::sLacksLegacyFallback,
  842. LLPipeline::sLacksBaseColorFallback);
  843. }
  844. if (LLPipeline::sRenderFrameTest)
  845. {
  846. LLWorld::sendAgentResume();
  847. LLPipeline::sRenderFrameTest = false;
  848. }
  849. display_stats();
  850. gShiftFrame = gScreenIsDirty = false;
  851. if (gShaderProfileFrame)
  852. {
  853. gShaderProfileFrame = false;
  854. LLGLSLShader::finishProfile();
  855. }
  856. }
  857. // For use by the PBR renderer only.
  858. void display_cube_face()
  859. {
  860. if (gRestoreGL || gSnapshot || gTeleportDisplay || !gPipeline.isInit() ||
  861. gAppViewerp->logoutRequestSent())
  862. {
  863. return;
  864. }
  865. LLGLSDefault gls_default;
  866. LLGLDepthTest gls_depth(GL_TRUE, GL_TRUE, GL_LEQUAL);
  867. LLVertexBuffer::unbind();
  868. gPipeline.disableLights();
  869. gViewerWindowp->setupViewport();
  870. // Do not render HUDs in this frame
  871. if (gPipeline.hasRenderType(LLPipeline::RENDER_TYPE_HUD))
  872. {
  873. gPipeline.toggleRenderType(LLPipeline::RENDER_TYPE_HUD);
  874. }
  875. if (gPipeline.hasRenderType(LLPipeline::RENDER_TYPE_HUD_PARTICLES))
  876. {
  877. gPipeline.toggleRenderType(LLPipeline::RENDER_TYPE_HUD_PARTICLES);
  878. }
  879. display_update_camera();
  880. // We need to update environment related uniforms and mark them dirty in
  881. // shaders used during the cube snapshot.
  882. gEnvironment.updateSettingsUniforms();
  883. gEnvironment.dirtyUniforms();
  884. LLSpatialGroup::sNoDelete = true;
  885. {
  886. // Occlusion data is from main camera point of view, do not read or
  887. // write it during cube snapshots.
  888. LLDisableOcclusionCulling no_occlusion;
  889. static LLCullResult result;
  890. LLViewerCamera::sCurCameraID = LLViewerCamera::CAMERA_WORLD;
  891. LLPipeline::sUnderWaterRender = gViewerCamera.cameraUnderWater();
  892. gPipeline.updateCull(gViewerCamera, result);
  893. gGL.setColorMask(true, true);
  894. glClearColor(0.f, 0.f, 0.f, 0.f);
  895. gPipeline.generateSunShadow();
  896. glClear(GL_DEPTH_BUFFER_BIT);
  897. LLViewerCamera::sCurCameraID = LLViewerCamera::CAMERA_WORLD;
  898. gPipeline.stateSort(gViewerCamera, result);
  899. }
  900. LLPipeline::sUnderWaterRender = gViewerCamera.cameraUnderWater();
  901. gGL.setColorMask(true, true);
  902. gPipeline.mRT->mDeferredScreen.bindTarget();
  903. if (gUseWireframe)
  904. {
  905. glClearColor(0.5f, 0.5f, 0.5f, 1.f);
  906. }
  907. else
  908. {
  909. glClearColor(1.f, 0.f, 1.f, 1.f);
  910. }
  911. gPipeline.mRT->mDeferredScreen.clear();
  912. LLViewerCamera::sCurCameraID = LLViewerCamera::CAMERA_WORLD;
  913. gPipeline.renderGeomDeferred(gViewerCamera, false);
  914. gPipeline.mRT->mDeferredScreen.flush();
  915. gPipeline.renderDeferredLighting();
  916. LLPipeline::sUnderWaterRender = false;
  917. LLSpatialGroup::sNoDelete = false;
  918. gPipeline.clearReferences();
  919. }
  920. void render_hud_attachments()
  921. {
  922. gGL.matrixMode(LLRender::MM_PROJECTION);
  923. gGL.pushMatrix();
  924. gGL.matrixMode(LLRender::MM_MODELVIEW);
  925. gGL.pushMatrix();
  926. const LLMatrix4a current_proj = gGLProjection;
  927. const LLMatrix4a current_mod = gGLModelView;
  928. F32 current_zoom = gAgent.mHUDCurZoom;
  929. F32 target_zoom = gAgent.getHUDTargetZoom();
  930. if (current_zoom != target_zoom)
  931. {
  932. // Smoothly interpolate current zoom level
  933. gAgent.mHUDCurZoom = lerp(current_zoom, target_zoom,
  934. LLCriticalDamp::getInterpolant(0.03f));
  935. }
  936. if (LLPipeline::sShowHUDAttachments && !gDisconnected &&
  937. setup_hud_matrices())
  938. {
  939. LLPipeline::sRenderingHUDs = true;
  940. LLCamera hud_cam = gViewerCamera;
  941. hud_cam.setOrigin(-1.f, 0.f, 0.f);
  942. hud_cam.setAxes(LLVector3::x_axis, LLVector3::y_axis,
  943. LLVector3::z_axis);
  944. LLViewerCamera::updateFrustumPlanes(hud_cam, true);
  945. static LLCachedControl<bool> render_hud_particles(gSavedSettings,
  946. "RenderHUDParticles");
  947. bool render_particles =
  948. render_hud_particles &&
  949. gPipeline.hasRenderType(LLPipeline::RENDER_TYPE_PARTICLES);
  950. // Only render hud objects
  951. gPipeline.pushRenderTypeMask();
  952. // Turn off everything
  953. gPipeline.andRenderTypeMask(LLPipeline::END_RENDER_TYPES);
  954. // Turn on HUD
  955. gPipeline.toggleRenderType(LLPipeline::RENDER_TYPE_HUD);
  956. // Turn on HUD particles
  957. gPipeline.toggleRenderType(LLPipeline::RENDER_TYPE_HUD_PARTICLES);
  958. // If particles are off, turn off hud-particles as well
  959. if (!render_particles)
  960. {
  961. // Turn back off HUD particles
  962. gPipeline.toggleRenderType(LLPipeline::RENDER_TYPE_HUD_PARTICLES);
  963. }
  964. bool has_ui =
  965. gPipeline.hasRenderDebugFeatureMask(LLPipeline::RENDER_DEBUG_FEATURE_UI);
  966. if (has_ui)
  967. {
  968. gPipeline.toggleRenderDebugFeature((void*)LLPipeline::RENDER_DEBUG_FEATURE_UI);
  969. }
  970. // Disable occlusion from now on and until end of context
  971. LLDisableOcclusionCulling no_occlusion;
  972. // Cull, sort, and render hud objects
  973. static LLCullResult result;
  974. LLSpatialGroup::sNoDelete = true;
  975. LLViewerCamera::sCurCameraID = LLViewerCamera::CAMERA_WORLD;
  976. gPipeline.updateCull(hud_cam, result, NULL, true);
  977. gPipeline.toggleRenderType(LLPipeline::RENDER_TYPE_BUMP);
  978. gPipeline.toggleRenderType(LLPipeline::RENDER_TYPE_SIMPLE);
  979. gPipeline.toggleRenderType(LLPipeline::RENDER_TYPE_VOLUME);
  980. gPipeline.toggleRenderType(LLPipeline::RENDER_TYPE_ALPHA);
  981. gPipeline.toggleRenderType(LLPipeline::RENDER_TYPE_ALPHA_MASK);
  982. gPipeline.toggleRenderType(LLPipeline::RENDER_TYPE_FULLBRIGHT_ALPHA_MASK);
  983. gPipeline.toggleRenderType(LLPipeline::RENDER_TYPE_FULLBRIGHT);
  984. gPipeline.toggleRenderType(LLPipeline::RENDER_TYPE_PASS_ALPHA);
  985. gPipeline.toggleRenderType(LLPipeline::RENDER_TYPE_PASS_ALPHA_MASK);
  986. gPipeline.toggleRenderType(LLPipeline::RENDER_TYPE_PASS_BUMP);
  987. gPipeline.toggleRenderType(LLPipeline::RENDER_TYPE_PASS_MATERIAL);
  988. gPipeline.toggleRenderType(LLPipeline::RENDER_TYPE_PASS_FULLBRIGHT);
  989. gPipeline.toggleRenderType(LLPipeline::RENDER_TYPE_PASS_FULLBRIGHT_ALPHA_MASK);
  990. gPipeline.toggleRenderType(LLPipeline::RENDER_TYPE_PASS_FULLBRIGHT_SHINY);
  991. gPipeline.toggleRenderType(LLPipeline::RENDER_TYPE_PASS_SHINY);
  992. gPipeline.toggleRenderType(LLPipeline::RENDER_TYPE_PASS_INVISIBLE);
  993. gPipeline.toggleRenderType(LLPipeline::RENDER_TYPE_PASS_INVISI_SHINY);
  994. if (gUsePBRShaders)
  995. {
  996. gPipeline.toggleRenderType(LLPipeline::RENDER_TYPE_ALPHA_PRE_WATER);
  997. gPipeline.toggleRenderType(LLPipeline::RENDER_TYPE_MAT_PBR);
  998. gPipeline.toggleRenderType(LLPipeline::RENDER_TYPE_MAT_PBR_ALPHA_MASK);
  999. gPipeline.toggleRenderType(LLPipeline::RENDER_TYPE_PASS_MAT_PBR);
  1000. gPipeline.toggleRenderType(LLPipeline::RENDER_TYPE_PASS_MAT_ALPHA_MASK_PBR);
  1001. }
  1002. gPipeline.stateSort(hud_cam, result);
  1003. if (gUsePBRShaders)
  1004. {
  1005. gPipeline.renderGeomPostDeferred(hud_cam);
  1006. }
  1007. else
  1008. {
  1009. gPipeline.renderGeom(hud_cam);
  1010. }
  1011. LLSpatialGroup::sNoDelete = false;
  1012. #if 0
  1013. gPipeline.clearReferences();
  1014. #endif
  1015. render_hud_elements();
  1016. // Restore type mask
  1017. gPipeline.popRenderTypeMask();
  1018. if (has_ui)
  1019. {
  1020. gPipeline.toggleRenderDebugFeature((void*)LLPipeline::RENDER_DEBUG_FEATURE_UI);
  1021. }
  1022. LLPipeline::sRenderingHUDs = false;
  1023. }
  1024. gGL.matrixMode(LLRender::MM_PROJECTION);
  1025. gGL.popMatrix();
  1026. gGL.matrixMode(LLRender::MM_MODELVIEW);
  1027. gGL.popMatrix();
  1028. gGLProjection = current_proj;
  1029. gGLModelView = current_mod;
  1030. }
  1031. bool setup_hud_matrices()
  1032. {
  1033. LLRect whole_screen = gViewerWindowp->getVirtualWindowRect();
  1034. // Apply camera zoom transform (for high res screenshots)
  1035. F32 zoom_factor = gViewerCamera.getZoomFactor();
  1036. S16 sub_region = gViewerCamera.getZoomSubRegion();
  1037. if (zoom_factor > 1.f)
  1038. {
  1039. S32 num_horizontal_tiles = llceil(zoom_factor);
  1040. S32 tile_width = ll_roundp((F32)gViewerWindowp->getWindowWidth() /
  1041. zoom_factor);
  1042. S32 tile_height = ll_roundp((F32)gViewerWindowp->getWindowHeight() /
  1043. zoom_factor);
  1044. S32 tile_y = sub_region / num_horizontal_tiles;
  1045. S32 tile_x = sub_region - (tile_y * num_horizontal_tiles);
  1046. whole_screen.setLeftTopAndSize(tile_x * tile_width,
  1047. gViewerWindowp->getWindowHeight() -
  1048. (tile_y * tile_height),
  1049. tile_width, tile_height);
  1050. }
  1051. return setup_hud_matrices(whole_screen);
  1052. }
  1053. bool setup_hud_matrices(const LLRect& screen_region)
  1054. {
  1055. LLMatrix4a proj, model;
  1056. if (!get_hud_matrices(screen_region, proj, model))
  1057. {
  1058. return false;
  1059. }
  1060. // Set up transform to keep HUD objects in front of camera
  1061. gGL.matrixMode(LLRender::MM_PROJECTION);
  1062. gGL.loadMatrix(proj);
  1063. gGLProjection = proj;
  1064. gGL.matrixMode(LLRender::MM_MODELVIEW);
  1065. gGL.loadMatrix(model);
  1066. gGLModelView = model;
  1067. return true;
  1068. }
  1069. bool get_hud_matrices(LLMatrix4a& proj, LLMatrix4a& model)
  1070. {
  1071. LLRect whole_screen = gViewerWindowp->getVirtualWindowRect();
  1072. return get_hud_matrices(whole_screen, proj, model);
  1073. }
  1074. bool get_hud_matrices(const LLRect& screen_region,
  1075. LLMatrix4a& proj, LLMatrix4a& model)
  1076. {
  1077. if (!isAgentAvatarValid() || !gAgentAvatarp->hasHUDAttachment())
  1078. {
  1079. return false;
  1080. }
  1081. LLBBox hud_bbox = gAgentAvatarp->getHUDBBox();
  1082. F32 aspect_ratio = gViewerCamera.getAspect();
  1083. F32 hud_depth = llmax(1.f, hud_bbox.getExtentLocal().mV[VX] * 1.1f);
  1084. proj = gl_ortho(-0.5f * aspect_ratio, 0.5f * aspect_ratio, -0.5f, 0.5f,
  1085. 0.f, hud_depth);
  1086. proj.getRow<2>().copyComponent<2>(LLVector4a(-0.01f));
  1087. F32 wwidth = (F32)gViewerWindowp->getWindowWidth();
  1088. F32 wheight = (F32)gViewerWindowp->getWindowHeight();
  1089. F32 scale_x = wwidth / (F32)screen_region.getWidth();
  1090. F32 scale_y = wheight /(F32)screen_region.getHeight();
  1091. F32 delta_x = screen_region.getCenterX() - screen_region.mLeft;
  1092. F32 delta_y = screen_region.getCenterY() - screen_region.mBottom;
  1093. proj.applyTranslationAffine(clamp_rescale(delta_x, 0.f, wwidth,
  1094. 0.5f * scale_x * aspect_ratio,
  1095. -0.5f * scale_x * aspect_ratio),
  1096. clamp_rescale(delta_y, 0.f, wheight,
  1097. 0.5f * scale_y,
  1098. -0.5f * scale_y), 0.f);
  1099. proj.applyScaleAffine(scale_x, scale_y, 1.f);
  1100. model = OGL_TO_CFR_ROT4A;
  1101. model.applyTranslationAffine(LLVector3(hud_depth * 0.5f -
  1102. hud_bbox.getCenterLocal().mV[VX],
  1103. 0.f, 0.f));
  1104. model.applyScaleAffine(gAgent.mHUDCurZoom);
  1105. return true;
  1106. }
  1107. void render_ui(F32 zoom_factor)
  1108. {
  1109. gGL.flush();
  1110. {
  1111. LL_FAST_TIMER(FTM_RENDER_UI);
  1112. LL_GL_CHECK_STATES;
  1113. const LLMatrix4a saved_view = gGLModelView;
  1114. bool not_snaphot = !gSnapshot;
  1115. if (not_snaphot)
  1116. {
  1117. gGL.pushMatrix();
  1118. gGL.loadMatrix(gGLLastModelView);
  1119. gGLModelView = gGLLastModelView;
  1120. }
  1121. // Finalize scene
  1122. gPipeline.renderFinalize();
  1123. //MK
  1124. {
  1125. LL_TRACY_TIMER(TRC_RLV_RENDER_LIMITS);
  1126. // Possibly draw a big black sphere around our avatar if the camera
  1127. // render is limited
  1128. if (gRLenabled && !gRLInterface.mRenderLimitRenderedThisFrame &&
  1129. !(isAgentAvatarValid() && gAgentAvatarp->isFullyLoaded()))
  1130. {
  1131. gRLInterface.drawRenderLimit(true);
  1132. }
  1133. }
  1134. //mk
  1135. render_hud_elements();
  1136. render_hud_attachments();
  1137. LLGLSDefault gls_default;
  1138. LLGLSUIDefault gls_ui;
  1139. gPipeline.disableLights();
  1140. gGL.color4f(1.f, 1.f, 1.f, 1.f);
  1141. if (gPipeline.hasRenderDebugFeatureMask(LLPipeline::RENDER_DEBUG_FEATURE_UI))
  1142. {
  1143. if (!gDisconnected)
  1144. {
  1145. render_ui_3d();
  1146. LL_GL_CHECK_STATES;
  1147. }
  1148. else
  1149. {
  1150. render_disconnected_background();
  1151. }
  1152. if (gUsePBRShaders)
  1153. {
  1154. LLHUDObject::renderAll();
  1155. }
  1156. render_ui_2d();
  1157. LL_GL_CHECK_STATES;
  1158. }
  1159. else
  1160. {
  1161. // Make sure particle effects disappear
  1162. LLHUDObject::removeExpired();
  1163. }
  1164. if (!gUsePBRShaders)
  1165. {
  1166. gGL.flush();
  1167. }
  1168. gViewerWindowp->setup2DRender();
  1169. gViewerWindowp->updateDebugText();
  1170. gViewerWindowp->drawDebugText();
  1171. if (!gUsePBRShaders)
  1172. {
  1173. LLVertexBuffer::unbind();
  1174. }
  1175. if (not_snaphot)
  1176. {
  1177. gGLModelView = saved_view;
  1178. gGL.popMatrix();
  1179. }
  1180. gGL.flush();
  1181. }
  1182. // Do not include this in FTM_RENDER_UI, since during the swap all sorts
  1183. // non-UI stuff will be drawn... HB
  1184. if (gDisplaySwapBuffers)
  1185. {
  1186. gWindowp->swapBuffers();
  1187. }
  1188. gDisplaySwapBuffers = true;
  1189. }
  1190. void renderCoordinateAxes()
  1191. {
  1192. gGL.getTexUnit(0)->unbind(LLTexUnit::TT_TEXTURE);
  1193. gGL.begin(LLRender::LINES);
  1194. gGL.color3f(1.f, 0.f, 0.f); // i direction = X-Axis = red
  1195. gGL.vertex3f(0.f, 0.f, 0.f);
  1196. gGL.vertex3f(2.f, 0.f, 0.f);
  1197. gGL.vertex3f(3.f, 0.f, 0.f);
  1198. gGL.vertex3f(5.f, 0.f, 0.f);
  1199. gGL.vertex3f(6.f, 0.f, 0.f);
  1200. gGL.vertex3f(8.f, 0.f, 0.f);
  1201. // Make an X
  1202. gGL.vertex3f(11.f, 1.f, 1.f);
  1203. gGL.vertex3f(11.f, -1.f, -1.f);
  1204. gGL.vertex3f(11.f, 1.f, -1.f);
  1205. gGL.vertex3f(11.f, -1.f, 1.f);
  1206. gGL.color3f(0.f, 1.f, 0.f); // j direction = Y-Axis = green
  1207. gGL.vertex3f(0.f, 0.f, 0.f);
  1208. gGL.vertex3f(0.f, 2.f, 0.f);
  1209. gGL.vertex3f(0.f, 3.f, 0.f);
  1210. gGL.vertex3f(0.f, 5.f, 0.f);
  1211. gGL.vertex3f(0.f, 6.f, 0.f);
  1212. gGL.vertex3f(0.f, 8.f, 0.f);
  1213. // Make a Y
  1214. gGL.vertex3f(1.f, 11.f, 1.f);
  1215. gGL.vertex3f(0.f, 11.f, 0.f);
  1216. gGL.vertex3f(-1.f, 11.f, 1.f);
  1217. gGL.vertex3f(0.f, 11.f, 0.f);
  1218. gGL.vertex3f(0.f, 11.f, 0.f);
  1219. gGL.vertex3f(0.f, 11.f, -1.f);
  1220. gGL.color3f(0.f, 0.f, 1.f); // Z-Axis = blue
  1221. gGL.vertex3f(0.f, 0.f, 0.f);
  1222. gGL.vertex3f(0.f, 0.f, 2.f);
  1223. gGL.vertex3f(0.f, 0.f, 3.f);
  1224. gGL.vertex3f(0.f, 0.f, 5.f);
  1225. gGL.vertex3f(0.f, 0.f, 6.f);
  1226. gGL.vertex3f(0.f, 0.f, 8.f);
  1227. // Make a Z
  1228. gGL.vertex3f(-1.f, 1.f, 11.f);
  1229. gGL.vertex3f(1.f, 1.f, 11.f);
  1230. gGL.vertex3f(1.f, 1.f, 11.f);
  1231. gGL.vertex3f(-1.f, -1.f, 11.f);
  1232. gGL.vertex3f(-1.f, -1.f, 11.f);
  1233. gGL.vertex3f(1.f, -1.f, 11.f);
  1234. gGL.end();
  1235. }
  1236. void draw_axes()
  1237. {
  1238. LLGLSUIDefault gls_ui;
  1239. gGL.getTexUnit(0)->unbind(LLTexUnit::TT_TEXTURE);
  1240. // A vertical white line at origin
  1241. LLVector3 v = gAgent.getPositionAgent();
  1242. gGL.begin(LLRender::LINES);
  1243. gGL.color3f(1.f, 1.f, 1.f);
  1244. gGL.vertex3f(0.f, 0.f, 0.f);
  1245. gGL.vertex3f(0.f, 0.f, 40.f);
  1246. gGL.end();
  1247. // Some coordinate axes
  1248. gGL.pushMatrix();
  1249. gGL.translatef(v.mV[VX], v.mV[VY], v.mV[VZ]);
  1250. renderCoordinateAxes();
  1251. gGL.popMatrix();
  1252. }
  1253. void render_ui_3d()
  1254. {
  1255. LLGLSPipeline gls_pipeline;
  1256. //////////////////////////////////////
  1257. // Render 3D UI elements
  1258. // NOTE: zbuffer is cleared before we get here by LLDrawPoolHUD,
  1259. // so 3d elements requiring Z buffer are moved to LLDrawPoolHUD
  1260. /////////////////////////////////////////////////////////////
  1261. // Render 2.5D elements (2D elements in the world)
  1262. // Stuff without z writes
  1263. // Debugging stuff goes before the UI.
  1264. gUIProgram.bind();
  1265. if (gUsePBRShaders)
  1266. {
  1267. gGL.color4f(1.f, 1.f, 1.f, 1.f);
  1268. }
  1269. // Coordinate axes
  1270. static LLCachedControl<bool> show_axes(gSavedSettings, "ShowAxes");
  1271. if (show_axes)
  1272. {
  1273. draw_axes();
  1274. }
  1275. // Non HUD call in render_hud_elements
  1276. gViewerWindowp->renderSelections(false, false, true);
  1277. if (gPipeline.hasRenderDebugFeatureMask(LLPipeline::RENDER_DEBUG_FEATURE_UI))
  1278. {
  1279. gObjectList.renderObjectBeacons();
  1280. gObjectList.resetObjectBeacons();
  1281. gSky.addSunMoonBeacons();
  1282. }
  1283. // Make sure particle effects disappear
  1284. LLHUDObject::removeExpired();
  1285. stop_glerror();
  1286. }
  1287. // Renders 2D UI elements that overlay the world (no z compare)
  1288. void render_ui_2d()
  1289. {
  1290. LLGLSUIDefault gls_ui;
  1291. // Disable wireframe mode below here, as this is HUD/menus
  1292. glPolygonMode(GL_FRONT_AND_BACK, GL_FILL);
  1293. // Menu overlays, HUD, etc
  1294. gViewerWindowp->setup2DRender();
  1295. F32 zoom_factor = gViewerCamera.getZoomFactor();
  1296. S16 sub_region = gViewerCamera.getZoomSubRegion();
  1297. if (zoom_factor > 1.f)
  1298. {
  1299. // Decompose subregion number to x and y values
  1300. S32 pos_y = sub_region / llceil(zoom_factor);
  1301. S32 pos_x = sub_region - pos_y * llceil(zoom_factor);
  1302. // offset for this tile
  1303. LLFontGL::sCurOrigin.mX -=
  1304. ll_round((F32)gViewerWindowp->getWindowWidth() * (F32)pos_x /
  1305. zoom_factor);
  1306. LLFontGL::sCurOrigin.mY -=
  1307. ll_round((F32)gViewerWindowp->getWindowHeight() * (F32)pos_y /
  1308. zoom_factor);
  1309. }
  1310. // Render outline for HUD
  1311. static LLCachedControl<F32> hud_scale(gSavedSettings, "HUDScaleFactor");
  1312. F32 zoom = gAgent.mHUDCurZoom / llmax(0.5f, hud_scale);
  1313. if (isAgentAvatarValid() && zoom <= 0.98f)
  1314. {
  1315. if (gUsePBRShaders)
  1316. {
  1317. gUIProgram.bind();
  1318. }
  1319. gGL.pushMatrix();
  1320. S32 half_width = gViewerWindowp->getWindowWidth() / 2;
  1321. S32 half_height = gViewerWindowp->getWindowHeight() / 2;
  1322. gGL.scalef(LLUI::sGLScaleFactor.mV[0],
  1323. LLUI::sGLScaleFactor.mV[1], 1.f);
  1324. gGL.translatef((F32)half_width, (F32)half_height, 0.f);
  1325. gGL.scalef(zoom, zoom, 1.f);
  1326. gGL.color4fv(LLColor4::white.mV);
  1327. gl_rect_2d(-half_width, half_height, half_width, -half_height, false);
  1328. gGL.popMatrix();
  1329. if (gUsePBRShaders)
  1330. {
  1331. gUIProgram.unbind();
  1332. }
  1333. }
  1334. gViewerWindowp->draw();
  1335. // Reset current origin for font rendering, in case of tiling render
  1336. LLFontGL::sCurOrigin.set(0, 0);
  1337. stop_glerror();
  1338. }
  1339. void render_disconnected_background()
  1340. {
  1341. gUIProgram.bind();
  1342. LLTexUnit* unit0 = gGL.getTexUnit(0);
  1343. gGL.color4f(1.f, 1.f, 1.f, 1.f);
  1344. if (!gDisconnectedImagep && gDisconnected)
  1345. {
  1346. std::string temp = gDirUtil.getLindenUserDir() + LL_DIR_DELIM_STR;
  1347. if (gIsInProductionGrid)
  1348. {
  1349. temp += SCREEN_LAST_FILENAME;
  1350. }
  1351. else
  1352. {
  1353. temp += SCREEN_LAST_BETA_FILENAME;
  1354. }
  1355. LLPointer<LLImageBMP> image_bmp = new LLImageBMP;
  1356. if (!image_bmp->load(temp))
  1357. {
  1358. return;
  1359. }
  1360. llinfos << "Loaded last bitmap: " << temp << llendl;
  1361. LLPointer<LLImageRaw> raw = new LLImageRaw;
  1362. if (!image_bmp->decode(raw))
  1363. {
  1364. llwarns << "Bitmap decode failed" << llendl;
  1365. gDisconnectedImagep = NULL;
  1366. return;
  1367. }
  1368. U8* rawp = raw->getData();
  1369. S32 npixels = (S32)image_bmp->getWidth() * (S32)image_bmp->getHeight();
  1370. for (S32 i = 0; i < npixels; ++i)
  1371. {
  1372. S32 sum = 0;
  1373. sum = *rawp + *(rawp + 1) + *(rawp + 2);
  1374. sum /= 3;
  1375. *rawp = ((S32)sum * 6 + *rawp) / 7;
  1376. rawp++;
  1377. *rawp = ((S32)sum * 6 + *rawp) / 7;
  1378. rawp++;
  1379. *rawp = ((S32)sum * 6 + *rawp) / 7;
  1380. rawp++;
  1381. }
  1382. raw->expandToPowerOfTwo();
  1383. gDisconnectedImagep = LLViewerTextureManager::getLocalTexture(raw.get(),
  1384. false);
  1385. gStartTexture = gDisconnectedImagep;
  1386. unit0->unbind(LLTexUnit::TT_TEXTURE);
  1387. }
  1388. // Make sure the progress view always fills the entire window.
  1389. S32 width = gViewerWindowp->getWindowWidth();
  1390. S32 height = gViewerWindowp->getWindowHeight();
  1391. if (gDisconnectedImagep)
  1392. {
  1393. LLGLSUIDefault gls_ui;
  1394. gViewerWindowp->setup2DRender();
  1395. gGL.pushMatrix();
  1396. {
  1397. // scale ui to reflect UIScaleFactor
  1398. // this can't be done in setup2DRender because it requires a
  1399. // pushMatrix/popMatrix pair
  1400. const LLVector2& display_scale = gViewerWindowp->getDisplayScale();
  1401. gGL.scalef(display_scale.mV[VX], display_scale.mV[VY], 1.f);
  1402. unit0->bind(gDisconnectedImagep);
  1403. gGL.color4f(1.f, 1.f, 1.f, 1.f);
  1404. gl_rect_2d_simple_tex(width, height);
  1405. unit0->unbind(LLTexUnit::TT_TEXTURE);
  1406. }
  1407. gGL.popMatrix();
  1408. }
  1409. gGL.flush();
  1410. gUIProgram.unbind();
  1411. }
  1412. void display_cleanup()
  1413. {
  1414. gDisconnectedImagep = NULL;
  1415. }
  1416. void hud_render_text(const LLWString& wstr, const LLVector3& pos_agent,
  1417. const LLFontGL& font, U8 style, F32 x_offset,
  1418. F32 y_offset, const LLColor4& color, bool orthographic)
  1419. {
  1420. // Do cheap plane culling
  1421. LLVector3 dir_vec = pos_agent - gViewerCamera.getOrigin();
  1422. dir_vec /= dir_vec.length();
  1423. if (wstr.empty() ||
  1424. (!orthographic && dir_vec * gViewerCamera.getAtAxis() <= 0.f))
  1425. {
  1426. return;
  1427. }
  1428. LLVector3 right_axis;
  1429. LLVector3 up_axis;
  1430. if (orthographic)
  1431. {
  1432. F32 height_inv = 1.f / (F32)gViewerWindowp->getWindowHeight();
  1433. right_axis.set(0.f, -height_inv, 0.f);
  1434. up_axis.set(0.f, 0.f, height_inv);
  1435. }
  1436. else
  1437. {
  1438. gViewerCamera.getPixelVectors(pos_agent, up_axis, right_axis);
  1439. }
  1440. LLQuaternion rot;
  1441. if (!orthographic)
  1442. {
  1443. rot = gViewerCamera.getQuaternion();
  1444. rot = rot * LLQuaternion(-F_PI_BY_TWO, gViewerCamera.getYAxis());
  1445. rot = rot * LLQuaternion(F_PI_BY_TWO, gViewerCamera.getXAxis());
  1446. }
  1447. else
  1448. {
  1449. rot = LLQuaternion(-F_PI_BY_TWO, LLVector3::z_axis);
  1450. rot = rot * LLQuaternion(-F_PI_BY_TWO, LLVector3::y_axis);
  1451. }
  1452. F32 angle;
  1453. LLVector3 axis;
  1454. rot.getAngleAxis(&angle, axis);
  1455. LLVector3 render_pos = pos_agent + floorf(x_offset) * right_axis +
  1456. floorf(y_offset) * up_axis;
  1457. // Get the render_pos in screen space
  1458. LLVector3 win_coord;
  1459. LLRect viewport(gGLViewport[0], gGLViewport[1] + gGLViewport[3],
  1460. gGLViewport[0] + gGLViewport[2], gGLViewport[1]);
  1461. gGL.projectf(render_pos, gGLModelView, gGLProjection, viewport, win_coord);
  1462. // Fonts all render orthographically, set up projection
  1463. gGL.matrixMode(LLRender::MM_PROJECTION);
  1464. gGL.pushMatrix();
  1465. gGL.matrixMode(LLRender::MM_MODELVIEW);
  1466. gGL.pushMatrix();
  1467. LLUI::pushMatrix();
  1468. gl_state_for_2d(gViewerWindowp->getWindowDisplayWidth(),
  1469. gViewerWindowp->getWindowDisplayHeight());
  1470. gViewerWindowp->setupViewport();
  1471. LLUI::loadIdentity();
  1472. gGL.loadIdentity();
  1473. LLUI::translate(win_coord.mV[VX] / LLFontGL::sScaleX,
  1474. win_coord.mV[VY] / LLFontGL::sScaleY,
  1475. -2.f * win_coord.mV[VZ] + 1.f);
  1476. F32 right_x;
  1477. font.render(wstr, 0, 0, 0, color, LLFontGL::LEFT, LLFontGL::BASELINE,
  1478. style, wstr.length(), 1000, &right_x);
  1479. LLUI::popMatrix();
  1480. gGL.popMatrix();
  1481. gGL.matrixMode(LLRender::MM_PROJECTION);
  1482. gGL.popMatrix();
  1483. gGL.matrixMode(LLRender::MM_MODELVIEW);
  1484. }