pipeline.cpp
上传用户:king477883
上传日期:2021-03-01
资源大小:9553k
文件大小:221k
源码类别:

游戏引擎

开发平台:

C++ Builder

  1. /** 
  2.  * @file pipeline.cpp
  3.  * @brief Rendering pipeline.
  4.  *
  5.  * $LicenseInfo:firstyear=2005&license=viewergpl$
  6.  * 
  7.  * Copyright (c) 2005-2010, 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 "pipeline.h"
  34. // library includes
  35. #include "llaudioengine.h" // For debugging.
  36. #include "imageids.h"
  37. #include "llerror.h"
  38. #include "llviewercontrol.h"
  39. #include "llfasttimer.h"
  40. #include "llfontgl.h"
  41. #include "llmemtype.h"
  42. #include "llnamevalue.h"
  43. #include "llpointer.h"
  44. #include "llprimitive.h"
  45. #include "llvolume.h"
  46. #include "material_codes.h"
  47. #include "timing.h"
  48. #include "v3color.h"
  49. #include "llui.h" 
  50. #include "llglheaders.h"
  51. #include "llrender.h"
  52. #include "llwindow.h" // swapBuffers()
  53. // newview includes
  54. #include "llagent.h"
  55. #include "lldrawable.h"
  56. #include "lldrawpoolalpha.h"
  57. #include "lldrawpoolavatar.h"
  58. #include "lldrawpoolground.h"
  59. #include "lldrawpoolbump.h"
  60. #include "lldrawpooltree.h"
  61. #include "lldrawpoolwater.h"
  62. #include "llface.h"
  63. #include "llfeaturemanager.h"
  64. #include "llfloatertelehub.h"
  65. #include "llfloaterreg.h"
  66. #include "llgldbg.h"
  67. #include "llhudmanager.h"
  68. #include "lllightconstants.h"
  69. #include "llresmgr.h"
  70. #include "llselectmgr.h"
  71. #include "llsky.h"
  72. #include "lltracker.h"
  73. #include "lltool.h"
  74. #include "lltoolmgr.h"
  75. #include "llviewercamera.h"
  76. #include "llviewertexturelist.h"
  77. #include "llviewerobject.h"
  78. #include "llviewerobjectlist.h"
  79. #include "llviewerparcelmgr.h"
  80. #include "llviewerregion.h" // for audio debugging.
  81. #include "llviewerwindow.h" // For getSpinAxis
  82. #include "llvoavatarself.h"
  83. #include "llvoground.h"
  84. #include "llvosky.h"
  85. #include "llvotree.h"
  86. #include "llvovolume.h"
  87. #include "llvosurfacepatch.h"
  88. #include "llvowater.h"
  89. #include "llvotree.h"
  90. #include "llvopartgroup.h"
  91. #include "llworld.h"
  92. #include "llcubemap.h"
  93. #include "llviewershadermgr.h"
  94. #include "llviewerstats.h"
  95. #include "llviewerjoystick.h"
  96. #include "llviewerdisplay.h"
  97. #include "llwlparammanager.h"
  98. #include "llwaterparammanager.h"
  99. #include "llspatialpartition.h"
  100. #include "llmutelist.h"
  101. #include "lltoolpie.h"
  102. #ifdef _DEBUG
  103. // Debug indices is disabled for now for debug performance - djs 4/24/02
  104. //#define DEBUG_INDICES
  105. #else
  106. //#define DEBUG_INDICES
  107. #endif
  108. const F32 BACKLIGHT_DAY_MAGNITUDE_AVATAR = 0.2f;
  109. const F32 BACKLIGHT_NIGHT_MAGNITUDE_AVATAR = 0.1f;
  110. const F32 BACKLIGHT_DAY_MAGNITUDE_OBJECT = 0.1f;
  111. const F32 BACKLIGHT_NIGHT_MAGNITUDE_OBJECT = 0.08f;
  112. const S32 MAX_ACTIVE_OBJECT_QUIET_FRAMES = 40;
  113. const S32 MAX_OFFSCREEN_GEOMETRY_CHANGES_PER_FRAME = 10;
  114. const U32 REFLECTION_MAP_RES = 128;
  115. // Max number of occluders to search for. JC
  116. const S32 MAX_OCCLUDER_COUNT = 2;
  117. extern S32 gBoxFrame;
  118. //extern BOOL gHideSelectedObjects;
  119. extern BOOL gDisplaySwapBuffers;
  120. extern BOOL gDebugGL;
  121. // hack counter for rendering a fixed number of frames after toggling
  122. // fullscreen to work around DEV-5361
  123. static S32 sDelayedVBOEnable = 0;
  124. BOOL gAvatarBacklight = FALSE;
  125. BOOL gRenderForSelect = FALSE;
  126. BOOL gDebugPipeline = FALSE;
  127. LLPipeline gPipeline;
  128. const LLMatrix4* gGLLastMatrix = NULL;
  129. LLFastTimer::DeclareTimer FTM_RENDER_GEOMETRY("Geometry");
  130. LLFastTimer::DeclareTimer FTM_RENDER_GRASS("Grass");
  131. LLFastTimer::DeclareTimer FTM_RENDER_INVISIBLE("Invisible");
  132. LLFastTimer::DeclareTimer FTM_RENDER_OCCLUSION("Occlusion");
  133. LLFastTimer::DeclareTimer FTM_RENDER_SHINY("Shiny");
  134. LLFastTimer::DeclareTimer FTM_RENDER_SIMPLE("Simple");
  135. LLFastTimer::DeclareTimer FTM_RENDER_TERRAIN("Terrain");
  136. LLFastTimer::DeclareTimer FTM_RENDER_TREES("Trees");
  137. LLFastTimer::DeclareTimer FTM_RENDER_UI("UI");
  138. LLFastTimer::DeclareTimer FTM_RENDER_WATER("Water");
  139. LLFastTimer::DeclareTimer FTM_RENDER_WL_SKY("Windlight Sky");
  140. LLFastTimer::DeclareTimer FTM_RENDER_ALPHA("Alpha Objects");
  141. LLFastTimer::DeclareTimer FTM_RENDER_CHARACTERS("Avatars");
  142. LLFastTimer::DeclareTimer FTM_RENDER_BUMP("Bump");
  143. LLFastTimer::DeclareTimer FTM_RENDER_FULLBRIGHT("Fullbright");
  144. LLFastTimer::DeclareTimer FTM_RENDER_GLOW("Glow");
  145. LLFastTimer::DeclareTimer FTM_GEO_UPDATE("Geo Update");
  146. LLFastTimer::DeclareTimer FTM_POOLRENDER("RenderPool");
  147. LLFastTimer::DeclareTimer FTM_POOLS("Pools");
  148. LLFastTimer::DeclareTimer FTM_RENDER_BLOOM_FBO("First FBO");
  149. LLFastTimer::DeclareTimer FTM_STATESORT("Sort Draw State");
  150. LLFastTimer::DeclareTimer FTM_PIPELINE("Pipeline");
  151. LLFastTimer::DeclareTimer FTM_CLIENT_COPY("Client Copy");
  152. LLFastTimer::DeclareTimer FTM_RENDER_DEFERRED("Deferred Shading");
  153. static LLFastTimer::DeclareTimer FTM_STATESORT_DRAWABLE("Sort Drawables");
  154. static LLFastTimer::DeclareTimer FTM_STATESORT_POSTSORT("Post Sort");
  155. //----------------------------------------
  156. std::string gPoolNames[] = 
  157. {
  158. // Correspond to LLDrawpool enum render type
  159. "NONE",
  160. "POOL_SIMPLE",
  161. "POOL_TERRAIN",
  162. "POOL_BUMP",
  163. "POOL_TREE",
  164. "POOL_SKY",
  165. "POOL_WL_SKY",
  166. "POOL_GROUND",
  167. "POOL_INVISIBLE",
  168. "POOL_AVATAR",
  169. "POOL_WATER",
  170. "POOL_GRASS",
  171. "POOL_FULLBRIGHT",
  172. "POOL_GLOW",
  173. "POOL_ALPHA",
  174. };
  175. void drawBox(const LLVector3& c, const LLVector3& r);
  176. U32 nhpo2(U32 v) 
  177. {
  178. U32 r = 1;
  179. while (r < v) {
  180. r *= 2;
  181. }
  182. return r;
  183. }
  184. glh::matrix4f glh_copy_matrix(GLdouble* src)
  185. {
  186. glh::matrix4f ret;
  187. for (U32 i = 0; i < 16; i++)
  188. {
  189. ret.m[i] = (F32) src[i];
  190. }
  191. return ret;
  192. }
  193. glh::matrix4f glh_get_current_modelview()
  194. {
  195. return glh_copy_matrix(gGLModelView);
  196. }
  197. glh::matrix4f glh_get_current_projection()
  198. {
  199. return glh_copy_matrix(gGLProjection);
  200. }
  201. void glh_copy_matrix(const glh::matrix4f& src, GLdouble* dst)
  202. {
  203. for (U32 i = 0; i < 16; i++)
  204. {
  205. dst[i] = src.m[i];
  206. }
  207. }
  208. void glh_set_current_modelview(const glh::matrix4f& mat)
  209. {
  210. glh_copy_matrix(mat, gGLModelView);
  211. }
  212. void glh_set_current_projection(glh::matrix4f& mat)
  213. {
  214. glh_copy_matrix(mat, gGLProjection);
  215. }
  216. glh::matrix4f gl_ortho(GLfloat left, GLfloat right, GLfloat bottom, GLfloat top, GLfloat znear, GLfloat zfar)
  217. {
  218. glh::matrix4f ret(
  219. 2.f/(right-left), 0.f, 0.f, -(right+left)/(right-left),
  220. 0.f, 2.f/(top-bottom), 0.f, -(top+bottom)/(top-bottom),
  221. 0.f, 0.f, -2.f/(zfar-znear),  -(zfar+znear)/(zfar-znear),
  222. 0.f, 0.f, 0.f, 1.f);
  223. return ret;
  224. }
  225. void display_update_camera();
  226. //----------------------------------------
  227. S32 LLPipeline::sCompiles = 0;
  228. BOOL LLPipeline::sPickAvatar = TRUE;
  229. BOOL LLPipeline::sDynamicLOD = TRUE;
  230. BOOL LLPipeline::sShowHUDAttachments = TRUE;
  231. BOOL LLPipeline::sRenderPhysicalBeacons = TRUE;
  232. BOOL LLPipeline::sRenderScriptedBeacons = FALSE;
  233. BOOL LLPipeline::sRenderScriptedTouchBeacons = TRUE;
  234. BOOL LLPipeline::sRenderParticleBeacons = FALSE;
  235. BOOL LLPipeline::sRenderSoundBeacons = FALSE;
  236. BOOL LLPipeline::sRenderBeacons = FALSE;
  237. BOOL LLPipeline::sRenderHighlight = TRUE;
  238. BOOL LLPipeline::sForceOldBakedUpload = FALSE;
  239. S32 LLPipeline::sUseOcclusion = 0;
  240. BOOL LLPipeline::sDelayVBUpdate = TRUE;
  241. BOOL LLPipeline::sFastAlpha = TRUE;
  242. BOOL LLPipeline::sDisableShaders = FALSE;
  243. BOOL LLPipeline::sRenderBump = TRUE;
  244. BOOL LLPipeline::sUseFarClip = TRUE;
  245. BOOL LLPipeline::sShadowRender = FALSE;
  246. BOOL LLPipeline::sWaterReflections = FALSE;
  247. BOOL LLPipeline::sRenderGlow = FALSE;
  248. BOOL LLPipeline::sReflectionRender = FALSE;
  249. BOOL LLPipeline::sImpostorRender = FALSE;
  250. BOOL LLPipeline::sUnderWaterRender = FALSE;
  251. BOOL LLPipeline::sTextureBindTest = FALSE;
  252. BOOL LLPipeline::sRenderFrameTest = FALSE;
  253. BOOL LLPipeline::sRenderAttachedLights = TRUE;
  254. BOOL LLPipeline::sRenderAttachedParticles = TRUE;
  255. BOOL LLPipeline::sRenderDeferred = FALSE;
  256. S32 LLPipeline::sVisibleLightCount = 0;
  257. F32 LLPipeline::sMinRenderSize = 0.f;
  258. static LLCullResult* sCull = NULL;
  259. static const U32 gl_cube_face[] = 
  260. {
  261. GL_TEXTURE_CUBE_MAP_POSITIVE_X_ARB,
  262. GL_TEXTURE_CUBE_MAP_NEGATIVE_X_ARB,
  263. GL_TEXTURE_CUBE_MAP_POSITIVE_Y_ARB,
  264. GL_TEXTURE_CUBE_MAP_NEGATIVE_Y_ARB,
  265. GL_TEXTURE_CUBE_MAP_POSITIVE_Z_ARB,
  266. GL_TEXTURE_CUBE_MAP_NEGATIVE_Z_ARB,
  267. };
  268. void validate_framebuffer_object();
  269. void addDeferredAttachments(LLRenderTarget& target)
  270. {
  271. target.addColorAttachment(GL_RGBA); //specular
  272. target.addColorAttachment(GL_RGBA); //normal+z
  273. }
  274. LLPipeline::LLPipeline() :
  275. mBackfaceCull(FALSE),
  276. mBatchCount(0),
  277. mMatrixOpCount(0),
  278. mTextureMatrixOps(0),
  279. mMaxBatchSize(0),
  280. mMinBatchSize(0),
  281. mMeanBatchSize(0),
  282. mTrianglesDrawn(0),
  283. mNumVisibleNodes(0),
  284. mVerticesRelit(0),
  285. mLightingChanges(0),
  286. mGeometryChanges(0),
  287. mNumVisibleFaces(0),
  288. mInitialized(FALSE),
  289. mVertexShadersEnabled(FALSE),
  290. mVertexShadersLoaded(0),
  291. mRenderTypeMask(0),
  292. mRenderDebugFeatureMask(0),
  293. mRenderDebugMask(0),
  294. mOldRenderDebugMask(0),
  295. mLastRebuildPool(NULL),
  296. mAlphaPool(NULL),
  297. mSkyPool(NULL),
  298. mTerrainPool(NULL),
  299. mWaterPool(NULL),
  300. mGroundPool(NULL),
  301. mSimplePool(NULL),
  302. mFullbrightPool(NULL),
  303. mInvisiblePool(NULL),
  304. mGlowPool(NULL),
  305. mBumpPool(NULL),
  306. mWLSkyPool(NULL),
  307. mLightMask(0),
  308. mLightMovingMask(0),
  309. mLightingDetail(0),
  310. mScreenWidth(0),
  311. mScreenHeight(0)
  312. {
  313. mNoiseMap = 0;
  314. mTrueNoiseMap = 0;
  315. mLightFunc = 0;
  316. }
  317. void LLPipeline::init()
  318. {
  319. LLMemType mt(LLMemType::MTYPE_PIPELINE_INIT);
  320. sDynamicLOD = gSavedSettings.getBOOL("RenderDynamicLOD");
  321. sRenderBump = gSavedSettings.getBOOL("RenderObjectBump");
  322. sRenderAttachedLights = gSavedSettings.getBOOL("RenderAttachedLights");
  323. sRenderAttachedParticles = gSavedSettings.getBOOL("RenderAttachedParticles");
  324. mInitialized = TRUE;
  325. stop_glerror();
  326. //create render pass pools
  327. getPool(LLDrawPool::POOL_ALPHA);
  328. getPool(LLDrawPool::POOL_SIMPLE);
  329. getPool(LLDrawPool::POOL_GRASS);
  330. getPool(LLDrawPool::POOL_FULLBRIGHT);
  331. getPool(LLDrawPool::POOL_INVISIBLE);
  332. getPool(LLDrawPool::POOL_BUMP);
  333. getPool(LLDrawPool::POOL_GLOW);
  334. LLViewerStats::getInstance()->mTrianglesDrawnStat.reset();
  335. resetFrameStats();
  336. mRenderTypeMask = 0xffffffff; // All render types start on
  337. mRenderDebugFeatureMask = 0xffffffff; // All debugging features on
  338. mRenderDebugMask = 0; // All debug starts off
  339. // Don't turn on ground when this is set
  340. // Mac Books with intel 950s need this
  341. if(!gSavedSettings.getBOOL("RenderGround"))
  342. {
  343. toggleRenderType(RENDER_TYPE_GROUND);
  344. }
  345. mOldRenderDebugMask = mRenderDebugMask;
  346. mBackfaceCull = TRUE;
  347. stop_glerror();
  348. // Enable features
  349. LLViewerShaderMgr::instance()->setShaders();
  350. stop_glerror();
  351. for (U32 i = 0; i < 2; ++i)
  352. {
  353. mSpotLightFade[i] = 1.f;
  354. }
  355. }
  356. LLPipeline::~LLPipeline()
  357. {
  358. }
  359. void LLPipeline::cleanup()
  360. {
  361. assertInitialized();
  362. mGroupQ1.clear() ;
  363. mGroupQ2.clear() ;
  364. for(pool_set_t::iterator iter = mPools.begin();
  365. iter != mPools.end(); )
  366. {
  367. pool_set_t::iterator curiter = iter++;
  368. LLDrawPool* poolp = *curiter;
  369. if (poolp->isFacePool())
  370. {
  371. LLFacePool* face_pool = (LLFacePool*) poolp;
  372. if (face_pool->mReferences.empty())
  373. {
  374. mPools.erase(curiter);
  375. removeFromQuickLookup( poolp );
  376. delete poolp;
  377. }
  378. }
  379. else
  380. {
  381. mPools.erase(curiter);
  382. removeFromQuickLookup( poolp );
  383. delete poolp;
  384. }
  385. }
  386. if (!mTerrainPools.empty())
  387. {
  388. llwarns << "Terrain Pools not cleaned up" << llendl;
  389. }
  390. if (!mTreePools.empty())
  391. {
  392. llwarns << "Tree Pools not cleaned up" << llendl;
  393. }
  394. delete mAlphaPool;
  395. mAlphaPool = NULL;
  396. delete mSkyPool;
  397. mSkyPool = NULL;
  398. delete mTerrainPool;
  399. mTerrainPool = NULL;
  400. delete mWaterPool;
  401. mWaterPool = NULL;
  402. delete mGroundPool;
  403. mGroundPool = NULL;
  404. delete mSimplePool;
  405. mSimplePool = NULL;
  406. delete mFullbrightPool;
  407. mFullbrightPool = NULL;
  408. delete mInvisiblePool;
  409. mInvisiblePool = NULL;
  410. delete mGlowPool;
  411. mGlowPool = NULL;
  412. delete mBumpPool;
  413. mBumpPool = NULL;
  414. // don't delete wl sky pool it was handled above in the for loop
  415. //delete mWLSkyPool;
  416. mWLSkyPool = NULL;
  417. releaseGLBuffers();
  418. mFaceSelectImagep = NULL;
  419. mMovedBridge.clear();
  420. mInitialized = FALSE;
  421. }
  422. //============================================================================
  423. void LLPipeline::destroyGL() 
  424. {
  425. stop_glerror();
  426. unloadShaders();
  427. mHighlightFaces.clear();
  428. resetDrawOrders();
  429. resetVertexBuffers();
  430. releaseGLBuffers();
  431. if (LLVertexBuffer::sEnableVBOs)
  432. {
  433. // render 30 frames after switching to work around DEV-5361
  434. sDelayedVBOEnable = 30;
  435. LLVertexBuffer::sEnableVBOs = FALSE;
  436. }
  437. }
  438. static LLFastTimer::DeclareTimer FTM_RESIZE_SCREEN_TEXTURE("Resize Screen Texture");
  439. void LLPipeline::resizeScreenTexture()
  440. {
  441. LLFastTimer ft(FTM_RESIZE_SCREEN_TEXTURE);
  442. if (gPipeline.canUseVertexShaders() && assertInitialized())
  443. {
  444. GLuint resX = gViewerWindow->getWorldViewWidthRaw();
  445. GLuint resY = gViewerWindow->getWorldViewHeightRaw();
  446. allocateScreenBuffer(resX,resY);
  447. }
  448. }
  449. void LLPipeline::allocateScreenBuffer(U32 resX, U32 resY)
  450. {
  451. // remember these dimensions
  452. mScreenWidth = resX;
  453. mScreenHeight = resY;
  454. U32 samples = gSavedSettings.getU32("RenderFSAASamples");
  455. U32 res_mod = gSavedSettings.getU32("RenderResolutionDivisor");
  456. if (res_mod > 1 && res_mod < resX && res_mod < resY)
  457. {
  458. resX /= res_mod;
  459. resY /= res_mod;
  460. }
  461. if (gSavedSettings.getBOOL("RenderUIBuffer"))
  462. {
  463. mUIScreen.allocate(resX,resY, GL_RGBA, FALSE, FALSE, LLTexUnit::TT_RECT_TEXTURE, FALSE);
  464. }
  465. if (LLPipeline::sRenderDeferred)
  466. {
  467. //allocate deferred rendering color buffers
  468. mDeferredScreen.allocate(resX, resY, GL_RGBA, TRUE, TRUE, LLTexUnit::TT_RECT_TEXTURE, FALSE);
  469. mDeferredDepth.allocate(resX, resY, 0, TRUE, FALSE, LLTexUnit::TT_RECT_TEXTURE, FALSE);
  470. addDeferredAttachments(mDeferredScreen);
  471. // always set viewport to desired size, since allocate resets the viewport
  472. mScreen.allocate(resX, resY, GL_RGBA, FALSE, FALSE, LLTexUnit::TT_RECT_TEXTURE, FALSE);
  473. mEdgeMap.allocate(resX, resY, GL_ALPHA, FALSE, FALSE, LLTexUnit::TT_RECT_TEXTURE, FALSE);
  474. for (U32 i = 0; i < 3; i++)
  475. {
  476. mDeferredLight[i].allocate(resX, resY, GL_RGBA, FALSE, FALSE, LLTexUnit::TT_RECT_TEXTURE);
  477. }
  478. for (U32 i = 0; i < 2; i++)
  479. {
  480. mGIMapPost[i].allocate(resX,resY, GL_RGB, FALSE, FALSE, LLTexUnit::TT_RECT_TEXTURE);
  481. }
  482. F32 scale = gSavedSettings.getF32("RenderShadowResolutionScale");
  483. for (U32 i = 0; i < 4; i++)
  484. {
  485. mShadow[i].allocate(U32(resX*scale),U32(resY*scale), 0, TRUE, FALSE, LLTexUnit::TT_RECT_TEXTURE);
  486. }
  487. U32 width = nhpo2(U32(resX*scale))/2;
  488. U32 height = width;
  489. for (U32 i = 4; i < 6; i++)
  490. {
  491. mShadow[i].allocate(width, height, 0, TRUE, FALSE);
  492. }
  493. width = nhpo2(resX)/2;
  494. height = nhpo2(resY)/2;
  495. mLuminanceMap.allocate(width,height, GL_RGBA, FALSE, FALSE);
  496. }
  497. else
  498. {
  499. mScreen.allocate(resX, resY, GL_RGBA, TRUE, TRUE, LLTexUnit::TT_RECT_TEXTURE, FALSE);
  500. }
  501. if (gGLManager.mHasFramebufferMultisample && samples > 1)
  502. {
  503. mSampleBuffer.allocate(resX,resY,GL_RGBA,TRUE,TRUE,LLTexUnit::TT_RECT_TEXTURE,FALSE,samples);
  504. if (LLPipeline::sRenderDeferred)
  505. {
  506. addDeferredAttachments(mSampleBuffer);
  507. mDeferredScreen.setSampleBuffer(&mSampleBuffer);
  508. }
  509. mScreen.setSampleBuffer(&mSampleBuffer);
  510. stop_glerror();
  511. }
  512. if (LLPipeline::sRenderDeferred)
  513. { //share depth buffer between deferred targets
  514. mDeferredScreen.shareDepthBuffer(mScreen);
  515. for (U32 i = 0; i < 3; i++)
  516. { //share stencil buffer with screen space lightmap to stencil out sky
  517. mDeferredScreen.shareDepthBuffer(mDeferredLight[i]);
  518. }
  519. }
  520. gGL.getTexUnit(0)->disable();
  521. stop_glerror();
  522. }
  523. //static
  524. void LLPipeline::updateRenderDeferred()
  525. {
  526. BOOL deferred = (gSavedSettings.getBOOL("RenderDeferred") && 
  527. LLRenderTarget::sUseFBO &&
  528. gSavedSettings.getBOOL("VertexShaderEnable") && 
  529. gSavedSettings.getBOOL("RenderAvatarVP") &&
  530. gSavedSettings.getBOOL("WindLightUseAtmosShaders")) ? TRUE : FALSE;
  531. sRenderDeferred = deferred;
  532. }
  533. void LLPipeline::releaseGLBuffers()
  534. {
  535. assertInitialized();
  536. if (mNoiseMap)
  537. {
  538. LLImageGL::deleteTextures(1, &mNoiseMap);
  539. mNoiseMap = 0;
  540. }
  541. if (mTrueNoiseMap)
  542. {
  543. LLImageGL::deleteTextures(1, &mTrueNoiseMap);
  544. mTrueNoiseMap = 0;
  545. }
  546. if (mLightFunc)
  547. {
  548. LLImageGL::deleteTextures(1, &mLightFunc);
  549. mLightFunc = 0;
  550. }
  551. mWaterRef.release();
  552. mWaterDis.release();
  553. mScreen.release();
  554. mUIScreen.release();
  555. mSampleBuffer.releaseSampleBuffer();
  556. mDeferredScreen.release();
  557. mDeferredDepth.release();
  558. for (U32 i = 0; i < 3; i++)
  559. {
  560. mDeferredLight[i].release();
  561. }
  562. mEdgeMap.release();
  563. mGIMap.release();
  564. mGIMapPost[0].release();
  565. mGIMapPost[1].release();
  566. mHighlight.release();
  567. mLuminanceMap.release();
  568. for (U32 i = 0; i < 6; i++)
  569. {
  570. mShadow[i].release();
  571. }
  572. for (U32 i = 0; i < 3; i++)
  573. {
  574. mGlow[i].release();
  575. }
  576. LLVOAvatar::resetImpostors();
  577. }
  578. void LLPipeline::createGLBuffers()
  579. {
  580. LLMemType mt_cb(LLMemType::MTYPE_PIPELINE_CREATE_BUFFERS);
  581. assertInitialized();
  582. updateRenderDeferred();
  583. if (LLPipeline::sWaterReflections)
  584. { //water reflection texture
  585. U32 res = (U32) gSavedSettings.getS32("RenderWaterRefResolution");
  586. mWaterRef.allocate(res,res,GL_RGBA,TRUE,FALSE);
  587. mWaterDis.allocate(res,res,GL_RGBA,TRUE,FALSE);
  588. }
  589. mHighlight.allocate(256,256,GL_RGBA, FALSE, FALSE);
  590. stop_glerror();
  591. GLuint resX = gViewerWindow->getWorldViewWidthRaw();
  592. GLuint resY = gViewerWindow->getWorldViewHeightRaw();
  593. if (LLPipeline::sRenderGlow)
  594. { //screen space glow buffers
  595. const U32 glow_res = llmax(1, 
  596. llmin(512, 1 << gSavedSettings.getS32("RenderGlowResolutionPow")));
  597. for (U32 i = 0; i < 3; i++)
  598. {
  599. mGlow[i].allocate(512,glow_res,GL_RGBA,FALSE,FALSE);
  600. }
  601. allocateScreenBuffer(resX,resY);
  602. mScreenWidth = 0;
  603. mScreenHeight = 0;
  604. }
  605. if (sRenderDeferred)
  606. {
  607. if (!mNoiseMap)
  608. {
  609. const U32 noiseRes = 128;
  610. LLVector3 noise[noiseRes*noiseRes];
  611. F32 scaler = gSavedSettings.getF32("RenderDeferredNoise")/100.f;
  612. for (U32 i = 0; i < noiseRes*noiseRes; ++i)
  613. {
  614. noise[i] = LLVector3(ll_frand()-0.5f, ll_frand()-0.5f, 0.f);
  615. noise[i].normVec();
  616. noise[i].mV[2] = ll_frand()*scaler+1.f-scaler/2.f;
  617. }
  618. LLImageGL::generateTextures(1, &mNoiseMap);
  619. gGL.getTexUnit(0)->bindManual(LLTexUnit::TT_TEXTURE, mNoiseMap);
  620. LLImageGL::setManualImage(LLTexUnit::getInternalType(LLTexUnit::TT_TEXTURE), 0, GL_RGB16F_ARB, noiseRes, noiseRes, GL_RGB, GL_FLOAT, noise);
  621. gGL.getTexUnit(0)->setTextureFilteringOption(LLTexUnit::TFO_POINT);
  622. }
  623. if (!mTrueNoiseMap)
  624. {
  625. const U32 noiseRes = 128;
  626. F32 noise[noiseRes*noiseRes*3];
  627. for (U32 i = 0; i < noiseRes*noiseRes*3; i++)
  628. {
  629. noise[i] = ll_frand()*2.0-1.0;
  630. }
  631. LLImageGL::generateTextures(1, &mTrueNoiseMap);
  632. gGL.getTexUnit(0)->bindManual(LLTexUnit::TT_TEXTURE, mTrueNoiseMap);
  633. LLImageGL::setManualImage(LLTexUnit::getInternalType(LLTexUnit::TT_TEXTURE), 0, GL_RGB16F_ARB, noiseRes, noiseRes, GL_RGB,GL_FLOAT, noise);
  634. gGL.getTexUnit(0)->setTextureFilteringOption(LLTexUnit::TFO_POINT);
  635. }
  636. if (!mLightFunc)
  637. {
  638. U32 lightResX = gSavedSettings.getU32("RenderSpecularResX");
  639. U32 lightResY = gSavedSettings.getU32("RenderSpecularResY");
  640. U8* lg = new U8[lightResX*lightResY];
  641. for (U32 y = 0; y < lightResY; ++y)
  642. {
  643. for (U32 x = 0; x < lightResX; ++x)
  644. {
  645. //spec func
  646. F32 sa = (F32) x/(lightResX-1);
  647. F32 spec = (F32) y/(lightResY-1);
  648. //lg[y*lightResX+x] = (U8) (powf(sa, 128.f*spec*spec)*255);
  649. //F32 sp = acosf(sa)/(1.f-spec);
  650. sa = powf(sa, gSavedSettings.getF32("RenderSpecularExponent"));
  651. F32 a = acosf(sa*0.25f+0.75f);
  652. F32 m = llmax(0.5f-spec*0.5f, 0.001f);
  653. F32 t2 = tanf(a)/m;
  654. t2 *= t2;
  655. F32 c4a = (3.f+4.f*cosf(2.f*a)+cosf(4.f*a))/8.f;
  656. F32 bd = 1.f/(4.f*m*m*c4a)*powf(F_E, -t2);
  657. lg[y*lightResX+x] = (U8) (llclamp(bd, 0.f, 1.f)*255);
  658. }
  659. }
  660. LLImageGL::generateTextures(1, &mLightFunc);
  661. gGL.getTexUnit(0)->bindManual(LLTexUnit::TT_TEXTURE, mLightFunc);
  662. LLImageGL::setManualImage(LLTexUnit::getInternalType(LLTexUnit::TT_TEXTURE), 0, GL_ALPHA, lightResX, lightResY, GL_ALPHA, GL_UNSIGNED_BYTE, lg);
  663. gGL.getTexUnit(0)->setTextureAddressMode(LLTexUnit::TAM_CLAMP);
  664. gGL.getTexUnit(0)->setTextureFilteringOption(LLTexUnit::TFO_TRILINEAR);
  665. delete [] lg;
  666. }
  667. if (gSavedSettings.getBOOL("RenderDeferredGI"))
  668. {
  669. mGIMap.allocate(512,512,GL_RGBA, TRUE, FALSE);
  670. addDeferredAttachments(mGIMap);
  671. }
  672. }
  673. }
  674. void LLPipeline::restoreGL() 
  675. {
  676. LLMemType mt_cb(LLMemType::MTYPE_PIPELINE_RESTORE_GL);
  677. assertInitialized();
  678. if (mVertexShadersEnabled)
  679. {
  680. LLViewerShaderMgr::instance()->setShaders();
  681. }
  682. for (LLWorld::region_list_t::const_iterator iter = LLWorld::getInstance()->getRegionList().begin(); 
  683. iter != LLWorld::getInstance()->getRegionList().end(); ++iter)
  684. {
  685. LLViewerRegion* region = *iter;
  686. for (U32 i = 0; i < LLViewerRegion::NUM_PARTITIONS; i++)
  687. {
  688. LLSpatialPartition* part = region->getSpatialPartition(i);
  689. if (part)
  690. {
  691. part->restoreGL();
  692. }
  693. }
  694. }
  695. }
  696. BOOL LLPipeline::canUseVertexShaders()
  697. {
  698. if (sDisableShaders ||
  699. !gGLManager.mHasVertexShader ||
  700. !gGLManager.mHasFragmentShader ||
  701. !LLFeatureManager::getInstance()->isFeatureAvailable("VertexShaderEnable") ||
  702. (assertInitialized() && mVertexShadersLoaded != 1) )
  703. {
  704. return FALSE;
  705. }
  706. else
  707. {
  708. return TRUE;
  709. }
  710. }
  711. BOOL LLPipeline::canUseWindLightShaders() const
  712. {
  713. return (!LLPipeline::sDisableShaders &&
  714. gWLSkyProgram.mProgramObject != 0 &&
  715. LLViewerShaderMgr::instance()->getVertexShaderLevel(LLViewerShaderMgr::SHADER_WINDLIGHT) > 1);
  716. }
  717. BOOL LLPipeline::canUseWindLightShadersOnObjects() const
  718. {
  719. return (canUseWindLightShaders() 
  720. && LLViewerShaderMgr::instance()->getVertexShaderLevel(LLViewerShaderMgr::SHADER_OBJECT) > 0);
  721. }
  722. void LLPipeline::unloadShaders()
  723. {
  724. LLMemType mt_us(LLMemType::MTYPE_PIPELINE_UNLOAD_SHADERS);
  725. LLViewerShaderMgr::instance()->unloadShaders();
  726. mVertexShadersLoaded = 0;
  727. }
  728. void LLPipeline::assertInitializedDoError()
  729. {
  730. llerrs << "LLPipeline used when uninitialized." << llendl;
  731. }
  732. //============================================================================
  733. void LLPipeline::enableShadows(const BOOL enable_shadows)
  734. {
  735. //should probably do something here to wrangle shadows....
  736. }
  737. S32 LLPipeline::getMaxLightingDetail() const
  738. {
  739. /*if (mVertexShaderLevel[SHADER_OBJECT] >= LLDrawPoolSimple::SHADER_LEVEL_LOCAL_LIGHTS)
  740. {
  741. return 3;
  742. }
  743. else*/
  744. {
  745. return 1;
  746. }
  747. }
  748. S32 LLPipeline::setLightingDetail(S32 level)
  749. {
  750. LLMemType mt_ld(LLMemType::MTYPE_PIPELINE_LIGHTING_DETAIL);
  751. assertInitialized();
  752. if (level < 0)
  753. {
  754. level = gSavedSettings.getS32("RenderLightingDetail");
  755. }
  756. level = llclamp(level, 0, getMaxLightingDetail());
  757. if (level != mLightingDetail)
  758. {
  759. gSavedSettings.setS32("RenderLightingDetail", level);
  760. mLightingDetail = level;
  761. if (mVertexShadersLoaded == 1)
  762. {
  763. LLViewerShaderMgr::instance()->setShaders();
  764. }
  765. }
  766. return mLightingDetail;
  767. }
  768. class LLOctreeDirtyTexture : public LLOctreeTraveler<LLDrawable>
  769. {
  770. public:
  771. const std::set<LLViewerFetchedTexture*>& mTextures;
  772. LLOctreeDirtyTexture(const std::set<LLViewerFetchedTexture*>& textures) : mTextures(textures) { }
  773. virtual void visit(const LLOctreeNode<LLDrawable>* node)
  774. {
  775. LLSpatialGroup* group = (LLSpatialGroup*) node->getListener(0);
  776. if (!group->isState(LLSpatialGroup::GEOM_DIRTY) && !group->getData().empty())
  777. {
  778. for (LLSpatialGroup::draw_map_t::iterator i = group->mDrawMap.begin(); i != group->mDrawMap.end(); ++i)
  779. {
  780. for (LLSpatialGroup::drawmap_elem_t::iterator j = i->second.begin(); j != i->second.end(); ++j) 
  781. {
  782. LLDrawInfo* params = *j;
  783. LLViewerFetchedTexture* tex = LLViewerTextureManager::staticCastToFetchedTexture(params->mTexture);
  784. if (tex && mTextures.find(tex) != mTextures.end())
  785. group->setState(LLSpatialGroup::GEOM_DIRTY);
  786. }
  787. }
  788. }
  789. }
  790. for (LLSpatialGroup::bridge_list_t::iterator i = group->mBridgeList.begin(); i != group->mBridgeList.end(); ++i)
  791. {
  792. LLSpatialBridge* bridge = *i;
  793. traverse(bridge->mOctree);
  794. }
  795. }
  796. };
  797. // Called when a texture changes # of channels (causes faces to move to alpha pool)
  798. void LLPipeline::dirtyPoolObjectTextures(const std::set<LLViewerFetchedTexture*>& textures)
  799. {
  800. assertInitialized();
  801. // *TODO: This is inefficient and causes frame spikes; need a better way to do this
  802. //        Most of the time is spent in dirty.traverse.
  803. for (pool_set_t::iterator iter = mPools.begin(); iter != mPools.end(); ++iter)
  804. {
  805. LLDrawPool *poolp = *iter;
  806. if (poolp->isFacePool())
  807. {
  808. ((LLFacePool*) poolp)->dirtyTextures(textures);
  809. }
  810. }
  811. LLOctreeDirtyTexture dirty(textures);
  812. for (LLWorld::region_list_t::const_iterator iter = LLWorld::getInstance()->getRegionList().begin(); 
  813. iter != LLWorld::getInstance()->getRegionList().end(); ++iter)
  814. {
  815. LLViewerRegion* region = *iter;
  816. for (U32 i = 0; i < LLViewerRegion::NUM_PARTITIONS; i++)
  817. {
  818. LLSpatialPartition* part = region->getSpatialPartition(i);
  819. if (part)
  820. {
  821. dirty.traverse(part->mOctree);
  822. }
  823. }
  824. }
  825. }
  826. LLDrawPool *LLPipeline::findPool(const U32 type, LLViewerTexture *tex0)
  827. {
  828. assertInitialized();
  829. LLDrawPool *poolp = NULL;
  830. switch( type )
  831. {
  832. case LLDrawPool::POOL_SIMPLE:
  833. poolp = mSimplePool;
  834. break;
  835. case LLDrawPool::POOL_GRASS:
  836. poolp = mGrassPool;
  837. break;
  838. case LLDrawPool::POOL_FULLBRIGHT:
  839. poolp = mFullbrightPool;
  840. break;
  841. case LLDrawPool::POOL_INVISIBLE:
  842. poolp = mInvisiblePool;
  843. break;
  844. case LLDrawPool::POOL_GLOW:
  845. poolp = mGlowPool;
  846. break;
  847. case LLDrawPool::POOL_TREE:
  848. poolp = get_if_there(mTreePools, (uintptr_t)tex0, (LLDrawPool*)0 );
  849. break;
  850. case LLDrawPool::POOL_TERRAIN:
  851. poolp = get_if_there(mTerrainPools, (uintptr_t)tex0, (LLDrawPool*)0 );
  852. break;
  853. case LLDrawPool::POOL_BUMP:
  854. poolp = mBumpPool;
  855. break;
  856. case LLDrawPool::POOL_ALPHA:
  857. poolp = mAlphaPool;
  858. break;
  859. case LLDrawPool::POOL_AVATAR:
  860. break; // Do nothing
  861. case LLDrawPool::POOL_SKY:
  862. poolp = mSkyPool;
  863. break;
  864. case LLDrawPool::POOL_WATER:
  865. poolp = mWaterPool;
  866. break;
  867. case LLDrawPool::POOL_GROUND:
  868. poolp = mGroundPool;
  869. break;
  870. case LLDrawPool::POOL_WL_SKY:
  871. poolp = mWLSkyPool;
  872. break;
  873. default:
  874. llassert(0);
  875. llerrs << "Invalid Pool Type in  LLPipeline::findPool() type=" << type << llendl;
  876. break;
  877. }
  878. return poolp;
  879. }
  880. LLDrawPool *LLPipeline::getPool(const U32 type, LLViewerTexture *tex0)
  881. {
  882. LLMemType mt(LLMemType::MTYPE_PIPELINE);
  883. LLDrawPool *poolp = findPool(type, tex0);
  884. if (poolp)
  885. {
  886. return poolp;
  887. }
  888. LLDrawPool *new_poolp = LLDrawPool::createPool(type, tex0);
  889. addPool( new_poolp );
  890. return new_poolp;
  891. }
  892. // static
  893. LLDrawPool* LLPipeline::getPoolFromTE(const LLTextureEntry* te, LLViewerTexture* imagep)
  894. {
  895. LLMemType mt(LLMemType::MTYPE_PIPELINE);
  896. U32 type = getPoolTypeFromTE(te, imagep);
  897. return gPipeline.getPool(type, imagep);
  898. }
  899. //static 
  900. U32 LLPipeline::getPoolTypeFromTE(const LLTextureEntry* te, LLViewerTexture* imagep)
  901. {
  902. LLMemType mt_gpt(LLMemType::MTYPE_PIPELINE_GET_POOL_TYPE);
  903. if (!te || !imagep)
  904. {
  905. return 0;
  906. }
  907. bool alpha = te->getColor().mV[3] < 0.999f;
  908. if (imagep)
  909. {
  910. alpha = alpha || (imagep->getComponents() == 4 && imagep->getType() != LLViewerTexture::MEDIA_TEXTURE) || (imagep->getComponents() == 2);
  911. }
  912. if (alpha)
  913. {
  914. return LLDrawPool::POOL_ALPHA;
  915. }
  916. else if ((te->getBumpmap() || te->getShiny()))
  917. {
  918. return LLDrawPool::POOL_BUMP;
  919. }
  920. else
  921. {
  922. return LLDrawPool::POOL_SIMPLE;
  923. }
  924. }
  925. void LLPipeline::addPool(LLDrawPool *new_poolp)
  926. {
  927. LLMemType mt_a(LLMemType::MTYPE_PIPELINE_ADD_POOL);
  928. assertInitialized();
  929. mPools.insert(new_poolp);
  930. addToQuickLookup( new_poolp );
  931. }
  932. void LLPipeline::allocDrawable(LLViewerObject *vobj)
  933. {
  934. LLMemType mt_ad(LLMemType::MTYPE_PIPELINE_ALLOCATE_DRAWABLE);
  935. LLDrawable *drawable = new LLDrawable();
  936. vobj->mDrawable = drawable;
  937. drawable->mVObjp     = vobj;
  938. //encompass completely sheared objects by taking 
  939. //the most extreme point possible (<1,1,0.5>)
  940. drawable->setRadius(LLVector3(1,1,0.5f).scaleVec(vobj->getScale()).length());
  941. if (vobj->isOrphaned())
  942. {
  943. drawable->setState(LLDrawable::FORCE_INVISIBLE);
  944. }
  945. drawable->updateXform(TRUE);
  946. }
  947. void LLPipeline::unlinkDrawable(LLDrawable *drawable)
  948. {
  949. LLFastTimer t(FTM_PIPELINE);
  950. assertInitialized();
  951. LLPointer<LLDrawable> drawablep = drawable; // make sure this doesn't get deleted before we are done
  952. // Based on flags, remove the drawable from the queues that it's on.
  953. if (drawablep->isState(LLDrawable::ON_MOVE_LIST))
  954. {
  955. LLDrawable::drawable_vector_t::iterator iter = std::find(mMovedList.begin(), mMovedList.end(), drawablep);
  956. if (iter != mMovedList.end())
  957. {
  958. mMovedList.erase(iter);
  959. }
  960. }
  961. if (drawablep->getSpatialGroup())
  962. {
  963. if (!drawablep->getSpatialGroup()->mSpatialPartition->remove(drawablep, drawablep->getSpatialGroup()))
  964. {
  965. #ifdef LL_RELEASE_FOR_DOWNLOAD
  966. llwarns << "Couldn't remove object from spatial group!" << llendl;
  967. #else
  968. llerrs << "Couldn't remove object from spatial group!" << llendl;
  969. #endif
  970. }
  971. }
  972. mLights.erase(drawablep);
  973. for (light_set_t::iterator iter = mNearbyLights.begin();
  974. iter != mNearbyLights.end(); iter++)
  975. {
  976. if (iter->drawable == drawablep)
  977. {
  978. mNearbyLights.erase(iter);
  979. break;
  980. }
  981. }
  982. {
  983. HighlightItem item(drawablep);
  984. mHighlightSet.erase(item);
  985. if (mHighlightObject == drawablep)
  986. {
  987. mHighlightObject = NULL;
  988. }
  989. }
  990. for (U32 i = 0; i < 2; ++i)
  991. {
  992. if (mShadowSpotLight[i] == drawablep)
  993. {
  994. mShadowSpotLight[i] = NULL;
  995. }
  996. if (mTargetShadowSpotLight[i] == drawablep)
  997. {
  998. mTargetShadowSpotLight[i] = NULL;
  999. }
  1000. }
  1001. }
  1002. U32 LLPipeline::addObject(LLViewerObject *vobj)
  1003. {
  1004. LLMemType mt_ao(LLMemType::MTYPE_PIPELINE_ADD_OBJECT);
  1005. if (gNoRender)
  1006. {
  1007. return 0;
  1008. }
  1009. if (gSavedSettings.getBOOL("RenderDelayCreation"))
  1010. {
  1011. mCreateQ.push_back(vobj);
  1012. }
  1013. else
  1014. {
  1015. createObject(vobj);
  1016. }
  1017. return 1;
  1018. }
  1019. void LLPipeline::createObjects(F32 max_dtime)
  1020. {
  1021. LLFastTimer ftm(FTM_GEO_UPDATE);
  1022. LLMemType mt(LLMemType::MTYPE_PIPELINE_CREATE_OBJECTS);
  1023. LLTimer update_timer;
  1024. while (!mCreateQ.empty() && update_timer.getElapsedTimeF32() < max_dtime)
  1025. {
  1026. LLViewerObject* vobj = mCreateQ.front();
  1027. if (!vobj->isDead())
  1028. {
  1029. createObject(vobj);
  1030. }
  1031. mCreateQ.pop_front();
  1032. }
  1033. //for (LLViewerObject::vobj_list_t::iterator iter = mCreateQ.begin(); iter != mCreateQ.end(); ++iter)
  1034. //{
  1035. // createObject(*iter);
  1036. //}
  1037. //mCreateQ.clear();
  1038. }
  1039. void LLPipeline::createObject(LLViewerObject* vobj)
  1040. {
  1041. LLDrawable* drawablep = vobj->mDrawable;
  1042. if (!drawablep)
  1043. {
  1044. drawablep = vobj->createDrawable(this);
  1045. }
  1046. else
  1047. {
  1048. llerrs << "Redundant drawable creation!" << llendl;
  1049. }
  1050. llassert(drawablep);
  1051. if (vobj->getParent())
  1052. {
  1053. vobj->setDrawableParent(((LLViewerObject*)vobj->getParent())->mDrawable); // LLPipeline::addObject 1
  1054. }
  1055. else
  1056. {
  1057. vobj->setDrawableParent(NULL); // LLPipeline::addObject 2
  1058. }
  1059. markRebuild(drawablep, LLDrawable::REBUILD_ALL, TRUE);
  1060. if (drawablep->getVOVolume() && gSavedSettings.getBOOL("RenderAnimateRes"))
  1061. {
  1062. // fun animated res
  1063. drawablep->updateXform(TRUE);
  1064. drawablep->clearState(LLDrawable::MOVE_UNDAMPED);
  1065. drawablep->setScale(LLVector3(0,0,0));
  1066. drawablep->makeActive();
  1067. }
  1068. }
  1069. void LLPipeline::resetFrameStats()
  1070. {
  1071. assertInitialized();
  1072. LLViewerStats::getInstance()->mTrianglesDrawnStat.addValue(mTrianglesDrawn/1000.f);
  1073. if (mBatchCount > 0)
  1074. {
  1075. mMeanBatchSize = gPipeline.mTrianglesDrawn/gPipeline.mBatchCount;
  1076. }
  1077. mTrianglesDrawn = 0;
  1078. sCompiles        = 0;
  1079. mVerticesRelit   = 0;
  1080. mLightingChanges = 0;
  1081. mGeometryChanges = 0;
  1082. mNumVisibleFaces = 0;
  1083. if (mOldRenderDebugMask != mRenderDebugMask)
  1084. {
  1085. gObjectList.clearDebugText();
  1086. mOldRenderDebugMask = mRenderDebugMask;
  1087. }
  1088. }
  1089. //external functions for asynchronous updating
  1090. void LLPipeline::updateMoveDampedAsync(LLDrawable* drawablep)
  1091. {
  1092. if (gSavedSettings.getBOOL("FreezeTime"))
  1093. {
  1094. return;
  1095. }
  1096. if (!drawablep)
  1097. {
  1098. llerrs << "updateMove called with NULL drawablep" << llendl;
  1099. return;
  1100. }
  1101. if (drawablep->isState(LLDrawable::EARLY_MOVE))
  1102. {
  1103. return;
  1104. }
  1105. assertInitialized();
  1106. // update drawable now
  1107. drawablep->clearState(LLDrawable::MOVE_UNDAMPED); // force to DAMPED
  1108. drawablep->updateMove(); // returns done
  1109. drawablep->setState(LLDrawable::EARLY_MOVE); // flag says we already did an undamped move this frame
  1110. // Put on move list so that EARLY_MOVE gets cleared
  1111. if (!drawablep->isState(LLDrawable::ON_MOVE_LIST))
  1112. {
  1113. mMovedList.push_back(drawablep);
  1114. drawablep->setState(LLDrawable::ON_MOVE_LIST);
  1115. }
  1116. }
  1117. void LLPipeline::updateMoveNormalAsync(LLDrawable* drawablep)
  1118. {
  1119. if (gSavedSettings.getBOOL("FreezeTime"))
  1120. {
  1121. return;
  1122. }
  1123. if (!drawablep)
  1124. {
  1125. llerrs << "updateMove called with NULL drawablep" << llendl;
  1126. return;
  1127. }
  1128. if (drawablep->isState(LLDrawable::EARLY_MOVE))
  1129. {
  1130. return;
  1131. }
  1132. assertInitialized();
  1133. // update drawable now
  1134. drawablep->setState(LLDrawable::MOVE_UNDAMPED); // force to UNDAMPED
  1135. drawablep->updateMove();
  1136. drawablep->setState(LLDrawable::EARLY_MOVE); // flag says we already did an undamped move this frame
  1137. // Put on move list so that EARLY_MOVE gets cleared
  1138. if (!drawablep->isState(LLDrawable::ON_MOVE_LIST))
  1139. {
  1140. mMovedList.push_back(drawablep);
  1141. drawablep->setState(LLDrawable::ON_MOVE_LIST);
  1142. }
  1143. }
  1144. void LLPipeline::updateMovedList(LLDrawable::drawable_vector_t& moved_list)
  1145. {
  1146. for (LLDrawable::drawable_vector_t::iterator iter = moved_list.begin();
  1147.  iter != moved_list.end(); )
  1148. {
  1149. LLDrawable::drawable_vector_t::iterator curiter = iter++;
  1150. LLDrawable *drawablep = *curiter;
  1151. BOOL done = TRUE;
  1152. if (!drawablep->isDead() && (!drawablep->isState(LLDrawable::EARLY_MOVE)))
  1153. {
  1154. done = drawablep->updateMove();
  1155. }
  1156. drawablep->clearState(LLDrawable::EARLY_MOVE | LLDrawable::MOVE_UNDAMPED);
  1157. if (done)
  1158. {
  1159. drawablep->clearState(LLDrawable::ON_MOVE_LIST);
  1160. iter = moved_list.erase(curiter);
  1161. }
  1162. }
  1163. }
  1164. static LLFastTimer::DeclareTimer FTM_OCTREE_BALANCE("Balance Octree");
  1165. static LLFastTimer::DeclareTimer FTM_UPDATE_MOVE("Update Move");
  1166. void LLPipeline::updateMove()
  1167. {
  1168. LLFastTimer t(FTM_UPDATE_MOVE);
  1169. LLMemType mt_um(LLMemType::MTYPE_PIPELINE_UPDATE_MOVE);
  1170. if (gSavedSettings.getBOOL("FreezeTime"))
  1171. {
  1172. return;
  1173. }
  1174. assertInitialized();
  1175. for (LLDrawable::drawable_set_t::iterator iter = mRetexturedList.begin();
  1176.  iter != mRetexturedList.end(); ++iter)
  1177. {
  1178. LLDrawable* drawablep = *iter;
  1179. if (drawablep && !drawablep->isDead())
  1180. {
  1181. drawablep->updateTexture();
  1182. }
  1183. }
  1184. mRetexturedList.clear();
  1185. updateMovedList(mMovedList);
  1186. for (LLDrawable::drawable_set_t::iterator iter = mActiveQ.begin();
  1187.  iter != mActiveQ.end(); )
  1188. {
  1189. LLDrawable::drawable_set_t::iterator curiter = iter++;
  1190. LLDrawable* drawablep = *curiter;
  1191. if (drawablep && !drawablep->isDead()) 
  1192. {
  1193. if (drawablep->isRoot() && 
  1194. drawablep->mQuietCount++ > MAX_ACTIVE_OBJECT_QUIET_FRAMES && 
  1195. (!drawablep->getParent() || !drawablep->getParent()->isActive()))
  1196. {
  1197. drawablep->makeStatic(); // removes drawable and its children from mActiveQ
  1198. iter = mActiveQ.upper_bound(drawablep); // next valid entry
  1199. }
  1200. }
  1201. else
  1202. {
  1203. mActiveQ.erase(curiter);
  1204. }
  1205. }
  1206. //balance octrees
  1207. {
  1208.   LLFastTimer ot(FTM_OCTREE_BALANCE);
  1209. for (LLWorld::region_list_t::const_iterator iter = LLWorld::getInstance()->getRegionList().begin(); 
  1210. iter != LLWorld::getInstance()->getRegionList().end(); ++iter)
  1211. {
  1212. LLViewerRegion* region = *iter;
  1213. for (U32 i = 0; i < LLViewerRegion::NUM_PARTITIONS; i++)
  1214. {
  1215. LLSpatialPartition* part = region->getSpatialPartition(i);
  1216. if (part)
  1217. {
  1218. part->mOctree->balance();
  1219. }
  1220. }
  1221. }
  1222. }
  1223. }
  1224. /////////////////////////////////////////////////////////////////////////////
  1225. // Culling and occlusion testing
  1226. /////////////////////////////////////////////////////////////////////////////
  1227. //static
  1228. F32 LLPipeline::calcPixelArea(LLVector3 center, LLVector3 size, LLCamera &camera)
  1229. {
  1230. LLVector3 lookAt = center - camera.getOrigin();
  1231. F32 dist = lookAt.length();
  1232. //ramp down distance for nearby objects
  1233. //shrink dist by dist/16.
  1234. if (dist < 16.f)
  1235. {
  1236. dist /= 16.f;
  1237. dist *= dist;
  1238. dist *= 16.f;
  1239. }
  1240. //get area of circle around node
  1241. F32 app_angle = atanf(size.length()/dist);
  1242. F32 radius = app_angle*LLDrawable::sCurPixelAngle;
  1243. return radius*radius * F_PI;
  1244. }
  1245. void LLPipeline::grabReferences(LLCullResult& result)
  1246. {
  1247. sCull = &result;
  1248. }
  1249. BOOL LLPipeline::visibleObjectsInFrustum(LLCamera& camera)
  1250. {
  1251. for (LLWorld::region_list_t::const_iterator iter = LLWorld::getInstance()->getRegionList().begin(); 
  1252. iter != LLWorld::getInstance()->getRegionList().end(); ++iter)
  1253. {
  1254. LLViewerRegion* region = *iter;
  1255. for (U32 i = 0; i < LLViewerRegion::NUM_PARTITIONS; i++)
  1256. {
  1257. LLSpatialPartition* part = region->getSpatialPartition(i);
  1258. if (part)
  1259. {
  1260. if (hasRenderType(part->mDrawableType))
  1261. {
  1262. if (part->visibleObjectsInFrustum(camera))
  1263. {
  1264. return TRUE;
  1265. }
  1266. }
  1267. }
  1268. }
  1269. }
  1270. return FALSE;
  1271. }
  1272. BOOL LLPipeline::getVisibleExtents(LLCamera& camera, LLVector3& min, LLVector3& max)
  1273. {
  1274. min = LLVector3(F32_MAX, F32_MAX, F32_MAX);
  1275. max = LLVector3(-F32_MAX, -F32_MAX, -F32_MAX);
  1276. U32 saved_camera_id = LLViewerCamera::sCurCameraID;
  1277. LLViewerCamera::sCurCameraID = LLViewerCamera::CAMERA_WORLD;
  1278. BOOL res = TRUE;
  1279. for (LLWorld::region_list_t::const_iterator iter = LLWorld::getInstance()->getRegionList().begin(); 
  1280. iter != LLWorld::getInstance()->getRegionList().end(); ++iter)
  1281. {
  1282. LLViewerRegion* region = *iter;
  1283. for (U32 i = 0; i < LLViewerRegion::NUM_PARTITIONS; i++)
  1284. {
  1285. LLSpatialPartition* part = region->getSpatialPartition(i);
  1286. if (part)
  1287. {
  1288. if (hasRenderType(part->mDrawableType))
  1289. {
  1290. if (!part->getVisibleExtents(camera, min, max))
  1291. {
  1292. res = FALSE;
  1293. }
  1294. }
  1295. }
  1296. }
  1297. }
  1298. LLViewerCamera::sCurCameraID = saved_camera_id;
  1299. return res;
  1300. }
  1301. static LLFastTimer::DeclareTimer FTM_CULL("Object Culling");
  1302. void LLPipeline::updateCull(LLCamera& camera, LLCullResult& result, S32 water_clip)
  1303. {
  1304. LLFastTimer t(FTM_CULL);
  1305. LLMemType mt_uc(LLMemType::MTYPE_PIPELINE_UPDATE_CULL);
  1306. grabReferences(result);
  1307. sCull->clear();
  1308. BOOL to_texture = LLPipeline::sUseOcclusion > 1 &&
  1309. !hasRenderType(LLPipeline::RENDER_TYPE_HUD) && 
  1310. LLViewerCamera::sCurCameraID == LLViewerCamera::CAMERA_WORLD &&
  1311. gPipeline.canUseVertexShaders() &&
  1312. sRenderGlow;
  1313. if (to_texture)
  1314. {
  1315. mScreen.bindTarget();
  1316. }
  1317. glMatrixMode(GL_PROJECTION);
  1318. glPushMatrix();
  1319. glLoadMatrixd(gGLLastProjection);
  1320. glMatrixMode(GL_MODELVIEW);
  1321. glPushMatrix();
  1322. gGLLastMatrix = NULL;
  1323. glLoadMatrixd(gGLLastModelView);
  1324. LLVertexBuffer::unbind();
  1325. LLGLDisable blend(GL_BLEND);
  1326. LLGLDisable test(GL_ALPHA_TEST);
  1327. gGL.getTexUnit(0)->unbind(LLTexUnit::TT_TEXTURE);
  1328. if (sUseOcclusion > 1)
  1329. {
  1330. gGL.setColorMask(false, false);
  1331. }
  1332. LLGLDepthTest depth(GL_TRUE, GL_FALSE);
  1333. for (LLWorld::region_list_t::const_iterator iter = LLWorld::getInstance()->getRegionList().begin(); 
  1334. iter != LLWorld::getInstance()->getRegionList().end(); ++iter)
  1335. {
  1336. LLViewerRegion* region = *iter;
  1337. if (water_clip != 0)
  1338. {
  1339. LLPlane plane(LLVector3(0,0, (F32) -water_clip), (F32) water_clip*region->getWaterHeight());
  1340. camera.setUserClipPlane(plane);
  1341. }
  1342. else
  1343. {
  1344. camera.disableUserClipPlane();
  1345. }
  1346. for (U32 i = 0; i < LLViewerRegion::NUM_PARTITIONS; i++)
  1347. {
  1348. LLSpatialPartition* part = region->getSpatialPartition(i);
  1349. if (part)
  1350. {
  1351. if (hasRenderType(part->mDrawableType))
  1352. {
  1353. part->cull(camera);
  1354. }
  1355. }
  1356. }
  1357. }
  1358. camera.disableUserClipPlane();
  1359. if (gSky.mVOSkyp.notNull() && gSky.mVOSkyp->mDrawable.notNull())
  1360. {
  1361. // Hack for sky - always visible.
  1362. if (hasRenderType(LLPipeline::RENDER_TYPE_SKY)) 
  1363. {
  1364. gSky.mVOSkyp->mDrawable->setVisible(camera);
  1365. sCull->pushDrawable(gSky.mVOSkyp->mDrawable);
  1366. gSky.updateCull();
  1367. stop_glerror();
  1368. }
  1369. }
  1370. else
  1371. {
  1372. llinfos << "No sky drawable!" << llendl;
  1373. }
  1374. if (hasRenderType(LLPipeline::RENDER_TYPE_GROUND) && 
  1375. !gPipeline.canUseWindLightShaders() &&
  1376. gSky.mVOGroundp.notNull() && 
  1377. gSky.mVOGroundp->mDrawable.notNull() &&
  1378. !LLPipeline::sWaterReflections)
  1379. {
  1380. gSky.mVOGroundp->mDrawable->setVisible(camera);
  1381. sCull->pushDrawable(gSky.mVOGroundp->mDrawable);
  1382. }
  1383. glMatrixMode(GL_PROJECTION);
  1384. glPopMatrix();
  1385. glMatrixMode(GL_MODELVIEW);
  1386. glPopMatrix();
  1387. if (sUseOcclusion > 1)
  1388. {
  1389. gGL.setColorMask(true, false);
  1390. }
  1391. if (to_texture)
  1392. {
  1393. mScreen.flush();
  1394. }
  1395. }
  1396. void LLPipeline::markNotCulled(LLSpatialGroup* group, LLCamera& camera)
  1397. {
  1398. if (group->getData().empty())
  1399. return;
  1400. }
  1401. group->setVisible();
  1402. if (LLViewerCamera::sCurCameraID == LLViewerCamera::CAMERA_WORLD)
  1403. {
  1404. group->updateDistance(camera);
  1405. }
  1406. const F32 MINIMUM_PIXEL_AREA = 16.f;
  1407. if (group->mPixelArea < MINIMUM_PIXEL_AREA)
  1408. {
  1409. return;
  1410. }
  1411. if (sMinRenderSize > 0.f && 
  1412. llmax(llmax(group->mBounds[1].mV[0], group->mBounds[1].mV[1]), group->mBounds[1].mV[2]) < sMinRenderSize)
  1413. {
  1414. return;
  1415. }
  1416. assertInitialized();
  1417. if (!group->mSpatialPartition->mRenderByGroup)
  1418. { //render by drawable
  1419. sCull->pushDrawableGroup(group);
  1420. }
  1421. else
  1422. {   //render by group
  1423. sCull->pushVisibleGroup(group);
  1424. }
  1425. mNumVisibleNodes++;
  1426. }
  1427. void LLPipeline::markOccluder(LLSpatialGroup* group)
  1428. {
  1429. if (sUseOcclusion > 1 && group && !group->isOcclusionState(LLSpatialGroup::ACTIVE_OCCLUSION))
  1430. {
  1431. LLSpatialGroup* parent = group->getParent();
  1432. if (!parent || !parent->isOcclusionState(LLSpatialGroup::OCCLUDED))
  1433. { //only mark top most occluders as active occlusion
  1434. sCull->pushOcclusionGroup(group);
  1435. group->setOcclusionState(LLSpatialGroup::ACTIVE_OCCLUSION);
  1436. if (parent && 
  1437. !parent->isOcclusionState(LLSpatialGroup::ACTIVE_OCCLUSION) &&
  1438. parent->getElementCount() == 0 &&
  1439. parent->needsUpdate())
  1440. {
  1441. sCull->pushOcclusionGroup(group);
  1442. parent->setOcclusionState(LLSpatialGroup::ACTIVE_OCCLUSION);
  1443. }
  1444. }
  1445. }
  1446. }
  1447. void LLPipeline::doOcclusion(LLCamera& camera)
  1448. {
  1449. LLVertexBuffer::unbind();
  1450. if (hasRenderDebugMask(LLPipeline::RENDER_DEBUG_OCCLUSION))
  1451. {
  1452. gGL.setColorMask(true, false, false, false);
  1453. }
  1454. else
  1455. {
  1456. gGL.setColorMask(false, false);
  1457. }
  1458. LLGLDisable blend(GL_BLEND);
  1459. LLGLDisable test(GL_ALPHA_TEST);
  1460. gGL.getTexUnit(0)->unbind(LLTexUnit::TT_TEXTURE);
  1461. LLGLDepthTest depth(GL_TRUE, GL_FALSE);
  1462. LLGLDisable cull(GL_CULL_FACE);
  1463. if (LLPipeline::sUseOcclusion > 1)
  1464. {
  1465. for (LLCullResult::sg_list_t::iterator iter = sCull->beginOcclusionGroups(); iter != sCull->endOcclusionGroups(); ++iter)
  1466. {
  1467. LLSpatialGroup* group = *iter;
  1468. group->doOcclusion(&camera);
  1469. group->clearOcclusionState(LLSpatialGroup::ACTIVE_OCCLUSION);
  1470. }
  1471. }
  1472. gGL.setColorMask(true, false);
  1473. }
  1474. BOOL LLPipeline::updateDrawableGeom(LLDrawable* drawablep, BOOL priority)
  1475. {
  1476. BOOL update_complete = drawablep->updateGeometry(priority);
  1477. if (update_complete && assertInitialized())
  1478. {
  1479. drawablep->setState(LLDrawable::BUILT);
  1480. mGeometryChanges++;
  1481. }
  1482. return update_complete;
  1483. }
  1484. void LLPipeline::updateGL()
  1485. {
  1486. while (!LLGLUpdate::sGLQ.empty())
  1487. {
  1488. LLGLUpdate* glu = LLGLUpdate::sGLQ.front();
  1489. glu->updateGL();
  1490. glu->mInQ = FALSE;
  1491. LLGLUpdate::sGLQ.pop_front();
  1492. }
  1493. }
  1494. void LLPipeline::rebuildPriorityGroups()
  1495. {
  1496. LLTimer update_timer;
  1497. LLMemType mt(LLMemType::MTYPE_PIPELINE);
  1498. assertInitialized();
  1499. // Iterate through all drawables on the priority build queue,
  1500. for (LLSpatialGroup::sg_list_t::iterator iter = mGroupQ1.begin();
  1501.  iter != mGroupQ1.end(); ++iter)
  1502. {
  1503. LLSpatialGroup* group = *iter;
  1504. group->rebuildGeom();
  1505. group->clearState(LLSpatialGroup::IN_BUILD_Q1);
  1506. }
  1507. mGroupQ1.clear();
  1508. }
  1509. void LLPipeline::rebuildGroups()
  1510. {
  1511. llpushcallstacks ;
  1512. // Iterate through some drawables on the non-priority build queue
  1513. S32 size = (S32) mGroupQ2.size();
  1514. S32 min_count = llclamp((S32) ((F32) (size * size)/4096*0.25f), 1, size);
  1515. S32 count = 0;
  1516. std::sort(mGroupQ2.begin(), mGroupQ2.end(), LLSpatialGroup::CompareUpdateUrgency());
  1517. LLSpatialGroup::sg_vector_t::iterator iter;
  1518. for (iter = mGroupQ2.begin();
  1519.  iter != mGroupQ2.end(); ++iter)
  1520. {
  1521. LLSpatialGroup* group = *iter;
  1522. if (group->isDead())
  1523. {
  1524. continue;
  1525. }
  1526. group->rebuildGeom();
  1527. if (group->mSpatialPartition->mRenderByGroup)
  1528. {
  1529. count++;
  1530. }
  1531. group->clearState(LLSpatialGroup::IN_BUILD_Q2);
  1532. if (count > min_count)
  1533. {
  1534. ++iter;
  1535. break;
  1536. }
  1537. }
  1538. mGroupQ2.erase(mGroupQ2.begin(), iter);
  1539. updateMovedList(mMovedBridge);
  1540. }
  1541. void LLPipeline::updateGeom(F32 max_dtime)
  1542. {
  1543. LLTimer update_timer;
  1544. LLMemType mt(LLMemType::MTYPE_PIPELINE_UPDATE_GEOM);
  1545. LLPointer<LLDrawable> drawablep;
  1546. LLFastTimer t(FTM_GEO_UPDATE);
  1547. assertInitialized();
  1548. if (sDelayedVBOEnable > 0)
  1549. {
  1550. if (--sDelayedVBOEnable <= 0)
  1551. {
  1552. resetVertexBuffers();
  1553. LLVertexBuffer::sEnableVBOs = TRUE;
  1554. }
  1555. }
  1556. // notify various object types to reset internal cost metrics, etc.
  1557. // for now, only LLVOVolume does this to throttle LOD changes
  1558. LLVOVolume::preUpdateGeom();
  1559. // Iterate through all drawables on the priority build queue,
  1560. for (LLDrawable::drawable_list_t::iterator iter = mBuildQ1.begin();
  1561.  iter != mBuildQ1.end();)
  1562. {
  1563. LLDrawable::drawable_list_t::iterator curiter = iter++;
  1564. LLDrawable* drawablep = *curiter;
  1565. if (drawablep && !drawablep->isDead())
  1566. {
  1567. if (drawablep->isState(LLDrawable::IN_REBUILD_Q2))
  1568. {
  1569. drawablep->clearState(LLDrawable::IN_REBUILD_Q2);
  1570. LLDrawable::drawable_list_t::iterator find = std::find(mBuildQ2.begin(), mBuildQ2.end(), drawablep);
  1571. if (find != mBuildQ2.end())
  1572. {
  1573. mBuildQ2.erase(find);
  1574. }
  1575. }
  1576. if (updateDrawableGeom(drawablep, TRUE))
  1577. {
  1578. drawablep->clearState(LLDrawable::IN_REBUILD_Q1);
  1579. mBuildQ1.erase(curiter);
  1580. }
  1581. }
  1582. else
  1583. {
  1584. mBuildQ1.erase(curiter);
  1585. }
  1586. }
  1587. // Iterate through some drawables on the non-priority build queue
  1588. S32 min_count = 16;
  1589. S32 size = (S32) mBuildQ2.size();
  1590. if (size > 1024)
  1591. {
  1592. min_count = llclamp((S32) (size * (F32) size/4096), 16, size);
  1593. }
  1594. S32 count = 0;
  1595. max_dtime = llmax(update_timer.getElapsedTimeF32()+0.001f, max_dtime);
  1596. LLSpatialGroup* last_group = NULL;
  1597. LLSpatialBridge* last_bridge = NULL;
  1598. for (LLDrawable::drawable_list_t::iterator iter = mBuildQ2.begin();
  1599.  iter != mBuildQ2.end(); )
  1600. {
  1601. LLDrawable::drawable_list_t::iterator curiter = iter++;
  1602. LLDrawable* drawablep = *curiter;
  1603. LLSpatialBridge* bridge = drawablep->isRoot() ? drawablep->getSpatialBridge() :
  1604. drawablep->getParent()->getSpatialBridge();
  1605. if (drawablep->getSpatialGroup() != last_group && 
  1606. (!last_bridge || bridge != last_bridge) &&
  1607. (update_timer.getElapsedTimeF32() >= max_dtime) && count > min_count)
  1608. {
  1609. break;
  1610. }
  1611. //make sure updates don't stop in the middle of a spatial group
  1612. //to avoid thrashing (objects are enqueued by group)
  1613. last_group = drawablep->getSpatialGroup();
  1614. last_bridge = bridge;
  1615. BOOL update_complete = TRUE;
  1616. if (!drawablep->isDead())
  1617. {
  1618. update_complete = updateDrawableGeom(drawablep, FALSE);
  1619. count++;
  1620. }
  1621. if (update_complete)
  1622. {
  1623. drawablep->clearState(LLDrawable::IN_REBUILD_Q2);
  1624. mBuildQ2.erase(curiter);
  1625. }
  1626. }
  1627. updateMovedList(mMovedBridge);
  1628. }
  1629. void LLPipeline::markVisible(LLDrawable *drawablep, LLCamera& camera)
  1630. {
  1631. LLMemType mt(LLMemType::MTYPE_PIPELINE_MARK_VISIBLE);
  1632. if(!drawablep || drawablep->isDead())
  1633. {
  1634. return;
  1635. }
  1636. if (drawablep->isSpatialBridge())
  1637. {
  1638. LLDrawable* root = ((LLSpatialBridge*) drawablep)->mDrawable;
  1639. if (root && root->getParent() && root->getVObj() && root->getVObj()->isAttachment())
  1640. {
  1641. LLVOAvatar* av = root->getParent()->getVObj()->asAvatar();
  1642. if (av && av->isImpostor())
  1643. {
  1644. return;
  1645. }
  1646. }
  1647. sCull->pushBridge((LLSpatialBridge*) drawablep);
  1648. }
  1649. else
  1650. {
  1651. sCull->pushDrawable(drawablep);
  1652. }
  1653. drawablep->setVisible(camera);
  1654. }
  1655. void LLPipeline::markMoved(LLDrawable *drawablep, BOOL damped_motion)
  1656. {
  1657. LLMemType mt_mm(LLMemType::MTYPE_PIPELINE_MARK_MOVED);
  1658. if (!drawablep)
  1659. {
  1660. //llerrs << "Sending null drawable to moved list!" << llendl;
  1661. return;
  1662. }
  1663. if (drawablep->isDead())
  1664. {
  1665. llwarns << "Marking NULL or dead drawable moved!" << llendl;
  1666. return;
  1667. }
  1668. if (drawablep->getParent()) 
  1669. {
  1670. //ensure that parent drawables are moved first
  1671. markMoved(drawablep->getParent(), damped_motion);
  1672. }
  1673. assertInitialized();
  1674. if (!drawablep->isState(LLDrawable::ON_MOVE_LIST))
  1675. {
  1676. if (drawablep->isSpatialBridge())
  1677. {
  1678. mMovedBridge.push_back(drawablep);
  1679. }
  1680. else
  1681. {
  1682. mMovedList.push_back(drawablep);
  1683. }
  1684. drawablep->setState(LLDrawable::ON_MOVE_LIST);
  1685. }
  1686. if (damped_motion == FALSE)
  1687. {
  1688. drawablep->setState(LLDrawable::MOVE_UNDAMPED); // UNDAMPED trumps DAMPED
  1689. }
  1690. else if (drawablep->isState(LLDrawable::MOVE_UNDAMPED))
  1691. {
  1692. drawablep->clearState(LLDrawable::MOVE_UNDAMPED);
  1693. }
  1694. }
  1695. void LLPipeline::markShift(LLDrawable *drawablep)
  1696. {
  1697. LLMemType mt(LLMemType::MTYPE_PIPELINE_MARK_SHIFT);
  1698. if (!drawablep || drawablep->isDead())
  1699. {
  1700. return;
  1701. }
  1702. assertInitialized();
  1703. if (!drawablep->isState(LLDrawable::ON_SHIFT_LIST))
  1704. {
  1705. drawablep->getVObj()->setChanged(LLXform::SHIFTED | LLXform::SILHOUETTE);
  1706. if (drawablep->getParent()) 
  1707. {
  1708. markShift(drawablep->getParent());
  1709. }
  1710. mShiftList.push_back(drawablep);
  1711. drawablep->setState(LLDrawable::ON_SHIFT_LIST);
  1712. }
  1713. }
  1714. void LLPipeline::shiftObjects(const LLVector3 &offset)
  1715. {
  1716. LLMemType mt(LLMemType::MTYPE_PIPELINE_SHIFT_OBJECTS);
  1717. assertInitialized();
  1718. glClear(GL_DEPTH_BUFFER_BIT);
  1719. gDepthDirty = TRUE;
  1720. for (LLDrawable::drawable_vector_t::iterator iter = mShiftList.begin();
  1721.  iter != mShiftList.end(); iter++)
  1722. {
  1723. LLDrawable *drawablep = *iter;
  1724. if (drawablep->isDead())
  1725. {
  1726. continue;
  1727. }
  1728. drawablep->shiftPos(offset);
  1729. drawablep->clearState(LLDrawable::ON_SHIFT_LIST);
  1730. }
  1731. mShiftList.resize(0);
  1732. for (LLWorld::region_list_t::const_iterator iter = LLWorld::getInstance()->getRegionList().begin(); 
  1733. iter != LLWorld::getInstance()->getRegionList().end(); ++iter)
  1734. {
  1735. LLViewerRegion* region = *iter;
  1736. for (U32 i = 0; i < LLViewerRegion::NUM_PARTITIONS; i++)
  1737. {
  1738. LLSpatialPartition* part = region->getSpatialPartition(i);
  1739. if (part)
  1740. {
  1741. part->shift(offset);
  1742. }
  1743. }
  1744. }
  1745. LLHUDText::shiftAll(offset);
  1746. display_update_camera();
  1747. }
  1748. void LLPipeline::markTextured(LLDrawable *drawablep)
  1749. {
  1750. LLMemType mt(LLMemType::MTYPE_PIPELINE_MARK_TEXTURED);
  1751. if (drawablep && !drawablep->isDead() && assertInitialized())
  1752. {
  1753. mRetexturedList.insert(drawablep);
  1754. }
  1755. }
  1756. void LLPipeline::markGLRebuild(LLGLUpdate* glu)
  1757. {
  1758. if (glu && !glu->mInQ)
  1759. {
  1760. LLGLUpdate::sGLQ.push_back(glu);
  1761. glu->mInQ = TRUE;
  1762. }
  1763. }
  1764. void LLPipeline::markRebuild(LLSpatialGroup* group, BOOL priority)
  1765. {
  1766. LLMemType mt(LLMemType::MTYPE_PIPELINE);
  1767. //assert_main_thread();
  1768. if (group && !group->isDead() && group->mSpatialPartition)
  1769. {
  1770. if (group->mSpatialPartition->mPartitionType == LLViewerRegion::PARTITION_HUD)
  1771. {
  1772. priority = TRUE;
  1773. }
  1774. if (priority)
  1775. {
  1776. if (!group->isState(LLSpatialGroup::IN_BUILD_Q1))
  1777. {
  1778. mGroupQ1.push_back(group);
  1779. group->setState(LLSpatialGroup::IN_BUILD_Q1);
  1780. if (group->isState(LLSpatialGroup::IN_BUILD_Q2))
  1781. {
  1782. LLSpatialGroup::sg_vector_t::iterator iter = std::find(mGroupQ2.begin(), mGroupQ2.end(), group);
  1783. if (iter != mGroupQ2.end())
  1784. {
  1785. mGroupQ2.erase(iter);
  1786. }
  1787. group->clearState(LLSpatialGroup::IN_BUILD_Q2);
  1788. }
  1789. }
  1790. }
  1791. else if (!group->isState(LLSpatialGroup::IN_BUILD_Q2 | LLSpatialGroup::IN_BUILD_Q1))
  1792. {
  1793. //llerrs << "Non-priority updates not yet supported!" << llendl;
  1794. if (std::find(mGroupQ2.begin(), mGroupQ2.end(), group) != mGroupQ2.end())
  1795. {
  1796. llerrs << "WTF?" << llendl;
  1797. }
  1798. mGroupQ2.push_back(group);
  1799. group->setState(LLSpatialGroup::IN_BUILD_Q2);
  1800. }
  1801. }
  1802. }
  1803. void LLPipeline::markRebuild(LLDrawable *drawablep, LLDrawable::EDrawableFlags flag, BOOL priority)
  1804. {
  1805. LLMemType mt(LLMemType::MTYPE_PIPELINE_MARK_REBUILD);
  1806. if (drawablep && !drawablep->isDead() && assertInitialized())
  1807. {
  1808. if (!drawablep->isState(LLDrawable::BUILT))
  1809. {
  1810. priority = TRUE;
  1811. }
  1812. if (priority)
  1813. {
  1814. if (!drawablep->isState(LLDrawable::IN_REBUILD_Q1))
  1815. {
  1816. mBuildQ1.push_back(drawablep);
  1817. drawablep->setState(LLDrawable::IN_REBUILD_Q1); // mark drawable as being in priority queue
  1818. }
  1819. }
  1820. else if (!drawablep->isState(LLDrawable::IN_REBUILD_Q2))
  1821. {
  1822. mBuildQ2.push_back(drawablep);
  1823. drawablep->setState(LLDrawable::IN_REBUILD_Q2); // need flag here because it is just a list
  1824. }
  1825. if (flag & (LLDrawable::REBUILD_VOLUME | LLDrawable::REBUILD_POSITION))
  1826. {
  1827. drawablep->getVObj()->setChanged(LLXform::SILHOUETTE);
  1828. }
  1829. drawablep->setState(flag);
  1830. }
  1831. }
  1832. static LLFastTimer::DeclareTimer FTM_RESET_DRAWORDER("Reset Draw Order");
  1833. void LLPipeline::stateSort(LLCamera& camera, LLCullResult &result)
  1834. {
  1835. const U32 face_mask = (1 << LLPipeline::RENDER_TYPE_AVATAR) |
  1836.   (1 << LLPipeline::RENDER_TYPE_GROUND) |
  1837.   (1 << LLPipeline::RENDER_TYPE_TERRAIN) |
  1838.   (1 << LLPipeline::RENDER_TYPE_TREE) |
  1839.   (1 << LLPipeline::RENDER_TYPE_SKY) |
  1840.   (1 << LLPipeline::RENDER_TYPE_WATER);
  1841. if (mRenderTypeMask & face_mask)
  1842. {
  1843. //clear faces from face pools
  1844. LLFastTimer t(FTM_RESET_DRAWORDER);
  1845. gPipeline.resetDrawOrders();
  1846. }
  1847. LLFastTimer ftm(FTM_STATESORT);
  1848. LLMemType mt(LLMemType::MTYPE_PIPELINE_STATE_SORT);
  1849. //LLVertexBuffer::unbind();
  1850. grabReferences(result);
  1851. for (LLCullResult::sg_list_t::iterator iter = sCull->beginDrawableGroups(); iter != sCull->endDrawableGroups(); ++iter)
  1852. {
  1853. LLSpatialGroup* group = *iter;
  1854. group->checkOcclusion();
  1855. if (sUseOcclusion > 1 && group->isOcclusionState(LLSpatialGroup::OCCLUDED))
  1856. {
  1857. markOccluder(group);
  1858. }
  1859. else
  1860. {
  1861. group->setVisible();
  1862. for (LLSpatialGroup::element_iter i = group->getData().begin(); i != group->getData().end(); ++i)
  1863. {
  1864. markVisible(*i, camera);
  1865. }
  1866. }
  1867. }
  1868. for (LLCullResult::sg_list_t::iterator iter = sCull->beginVisibleGroups(); iter != sCull->endVisibleGroups(); ++iter)
  1869. {
  1870. LLSpatialGroup* group = *iter;
  1871. group->checkOcclusion();
  1872. if (sUseOcclusion > 1 && group->isOcclusionState(LLSpatialGroup::OCCLUDED))
  1873. {
  1874. markOccluder(group);
  1875. }
  1876. else
  1877. {
  1878. group->setVisible();
  1879. stateSort(group, camera);
  1880. }
  1881. }
  1882. if (LLViewerCamera::sCurCameraID == LLViewerCamera::CAMERA_WORLD)
  1883. {
  1884. for (LLCullResult::bridge_list_t::iterator i = sCull->beginVisibleBridge(); i != sCull->endVisibleBridge(); ++i)
  1885. {
  1886. LLCullResult::bridge_list_t::iterator cur_iter = i;
  1887. LLSpatialBridge* bridge = *cur_iter;
  1888. LLSpatialGroup* group = bridge->getSpatialGroup();
  1889. if (!bridge->isDead() && group && !group->isOcclusionState(LLSpatialGroup::OCCLUDED))
  1890. {
  1891. stateSort(bridge, camera);
  1892. }
  1893. }
  1894. }
  1895. {
  1896. LLFastTimer ftm(FTM_STATESORT_DRAWABLE);
  1897. for (LLCullResult::drawable_list_t::iterator iter = sCull->beginVisibleList();
  1898.  iter != sCull->endVisibleList(); ++iter)
  1899. {
  1900. LLDrawable *drawablep = *iter;
  1901. if (!drawablep->isDead())
  1902. {
  1903. stateSort(drawablep, camera);
  1904. }
  1905. }
  1906. }
  1907. {
  1908. LLFastTimer ftm(FTM_CLIENT_COPY);
  1909. LLVertexBuffer::clientCopy();
  1910. }
  1911. postSort(camera);
  1912. }
  1913. void LLPipeline::stateSort(LLSpatialGroup* group, LLCamera& camera)
  1914. {
  1915. LLMemType mt(LLMemType::MTYPE_PIPELINE_STATE_SORT);
  1916. if (group->changeLOD())
  1917. {
  1918. for (LLSpatialGroup::element_iter i = group->getData().begin(); i != group->getData().end(); ++i)
  1919. {
  1920. LLDrawable* drawablep = *i;
  1921. stateSort(drawablep, camera);
  1922. }
  1923. }
  1924. }
  1925. void LLPipeline::stateSort(LLSpatialBridge* bridge, LLCamera& camera)
  1926. {
  1927. LLMemType mt(LLMemType::MTYPE_PIPELINE_STATE_SORT);
  1928. if (!sShadowRender && bridge->getSpatialGroup()->changeLOD())
  1929. {
  1930. bool force_update = false;
  1931. bridge->updateDistance(camera, force_update);
  1932. }
  1933. }
  1934. void LLPipeline::stateSort(LLDrawable* drawablep, LLCamera& camera)
  1935. {
  1936. LLMemType mt(LLMemType::MTYPE_PIPELINE_STATE_SORT);
  1937. if (!drawablep
  1938. || drawablep->isDead() 
  1939. || !hasRenderType(drawablep->getRenderType()))
  1940. {
  1941. return;
  1942. }
  1943. if (LLSelectMgr::getInstance()->mHideSelectedObjects)
  1944. {
  1945. if (drawablep->getVObj().notNull() &&
  1946. drawablep->getVObj()->isSelected())
  1947. {
  1948. return;
  1949. }
  1950. }
  1951. if (drawablep->isAvatar())
  1952. { //don't draw avatars beyond render distance or if we don't have a spatial group.
  1953. if ((drawablep->getSpatialGroup() == NULL) || 
  1954. (drawablep->getSpatialGroup()->mDistance > LLVOAvatar::sRenderDistance))
  1955. {
  1956. return;
  1957. }
  1958. LLVOAvatar* avatarp = (LLVOAvatar*) drawablep->getVObj().get();
  1959. if (!avatarp->isVisible())
  1960. {
  1961. return;
  1962. }
  1963. }
  1964. assertInitialized();
  1965. if (hasRenderType(drawablep->mRenderType))
  1966. {
  1967. if (!drawablep->isState(LLDrawable::INVISIBLE|LLDrawable::FORCE_INVISIBLE))
  1968. {
  1969. drawablep->setVisible(camera, NULL, FALSE);
  1970. }
  1971. else if (drawablep->isState(LLDrawable::CLEAR_INVISIBLE))
  1972. {
  1973. // clear invisible flag here to avoid single frame glitch
  1974. drawablep->clearState(LLDrawable::FORCE_INVISIBLE|LLDrawable::CLEAR_INVISIBLE);
  1975. }
  1976. }
  1977. if (LLViewerCamera::sCurCameraID == LLViewerCamera::CAMERA_WORLD)
  1978. {
  1979. LLSpatialGroup* group = drawablep->getSpatialGroup();
  1980. if (!group || group->changeLOD())
  1981. {
  1982. if (drawablep->isVisible())
  1983. {
  1984. if (!drawablep->isActive())
  1985. {
  1986. bool force_update = false;
  1987. drawablep->updateDistance(camera, force_update);
  1988. }
  1989. else if (drawablep->isAvatar())
  1990. {
  1991. bool force_update = false;
  1992. drawablep->updateDistance(camera, force_update); // calls vobj->updateLOD() which calls LLVOAvatar::updateVisibility()
  1993. }
  1994. }
  1995. }
  1996. }
  1997. if (!drawablep->getVOVolume())
  1998. {
  1999. for (LLDrawable::face_list_t::iterator iter = drawablep->mFaces.begin();
  2000. iter != drawablep->mFaces.end(); iter++)
  2001. {
  2002. LLFace* facep = *iter;
  2003. if (facep->hasGeometry())
  2004. {
  2005. if (facep->getPool())
  2006. {
  2007. facep->getPool()->enqueue(facep);
  2008. }
  2009. else
  2010. {
  2011. break;
  2012. }
  2013. }
  2014. }
  2015. }
  2016. mNumVisibleFaces += drawablep->getNumFaces();
  2017. }
  2018. void forAllDrawables(LLCullResult::sg_list_t::iterator begin, 
  2019.  LLCullResult::sg_list_t::iterator end,
  2020.  void (*func)(LLDrawable*))
  2021. {
  2022. for (LLCullResult::sg_list_t::iterator i = begin; i != end; ++i)
  2023. {
  2024. for (LLSpatialGroup::element_iter j = (*i)->getData().begin(); j != (*i)->getData().end(); ++j)
  2025. {
  2026. func(*j);
  2027. }
  2028. }
  2029. }
  2030. void LLPipeline::forAllVisibleDrawables(void (*func)(LLDrawable*))
  2031. {
  2032. forAllDrawables(sCull->beginDrawableGroups(), sCull->endDrawableGroups(), func);
  2033. forAllDrawables(sCull->beginVisibleGroups(), sCull->endVisibleGroups(), func);
  2034. }
  2035. //function for creating scripted beacons
  2036. void renderScriptedBeacons(LLDrawable* drawablep)
  2037. {
  2038. LLViewerObject *vobj = drawablep->getVObj();
  2039. if (vobj 
  2040. && !vobj->isAvatar() 
  2041. && !vobj->getParent()
  2042. && vobj->flagScripted())
  2043. {
  2044. if (gPipeline.sRenderBeacons)
  2045. {
  2046. gObjectList.addDebugBeacon(vobj->getPositionAgent(), "", LLColor4(1.f, 0.f, 0.f, 0.5f), LLColor4(1.f, 1.f, 1.f, 0.5f), gSavedSettings.getS32("DebugBeaconLineWidth"));
  2047. }
  2048. if (gPipeline.sRenderHighlight)
  2049. {
  2050. S32 face_id;
  2051. S32 count = drawablep->getNumFaces();
  2052. for (face_id = 0; face_id < count; face_id++)
  2053. {
  2054. gPipeline.mHighlightFaces.push_back(drawablep->getFace(face_id) );
  2055. }
  2056. }
  2057. }
  2058. }
  2059. void renderScriptedTouchBeacons(LLDrawable* drawablep)
  2060. {
  2061. LLViewerObject *vobj = drawablep->getVObj();
  2062. if (vobj 
  2063. && !vobj->isAvatar() 
  2064. && !vobj->getParent()
  2065. && vobj->flagScripted()
  2066. && vobj->flagHandleTouch())
  2067. {
  2068. if (gPipeline.sRenderBeacons)
  2069. {
  2070. gObjectList.addDebugBeacon(vobj->getPositionAgent(), "", LLColor4(1.f, 0.f, 0.f, 0.5f), LLColor4(1.f, 1.f, 1.f, 0.5f), gSavedSettings.getS32("DebugBeaconLineWidth"));
  2071. }
  2072. if (gPipeline.sRenderHighlight)
  2073. {
  2074. S32 face_id;
  2075. S32 count = drawablep->getNumFaces();
  2076. for (face_id = 0; face_id < count; face_id++)
  2077. {
  2078. gPipeline.mHighlightFaces.push_back(drawablep->getFace(face_id) );
  2079. }
  2080. }
  2081. }
  2082. }
  2083. void renderPhysicalBeacons(LLDrawable* drawablep)
  2084. {
  2085. LLViewerObject *vobj = drawablep->getVObj();
  2086. if (vobj 
  2087. && !vobj->isAvatar() 
  2088. //&& !vobj->getParent()
  2089. && vobj->usePhysics())
  2090. {
  2091. if (gPipeline.sRenderBeacons)
  2092. {
  2093. gObjectList.addDebugBeacon(vobj->getPositionAgent(), "", LLColor4(0.f, 1.f, 0.f, 0.5f), LLColor4(1.f, 1.f, 1.f, 0.5f), gSavedSettings.getS32("DebugBeaconLineWidth"));
  2094. }
  2095. if (gPipeline.sRenderHighlight)
  2096. {
  2097. S32 face_id;
  2098. S32 count = drawablep->getNumFaces();
  2099. for (face_id = 0; face_id < count; face_id++)
  2100. {
  2101. gPipeline.mHighlightFaces.push_back(drawablep->getFace(face_id) );
  2102. }
  2103. }
  2104. }
  2105. }
  2106. void renderParticleBeacons(LLDrawable* drawablep)
  2107. {
  2108. // Look for attachments, objects, etc.
  2109. LLViewerObject *vobj = drawablep->getVObj();
  2110. if (vobj 
  2111. && vobj->isParticleSource())
  2112. {
  2113. if (gPipeline.sRenderBeacons)
  2114. {
  2115. LLColor4 light_blue(0.5f, 0.5f, 1.f, 0.5f);
  2116. gObjectList.addDebugBeacon(vobj->getPositionAgent(), "", light_blue, LLColor4(1.f, 1.f, 1.f, 0.5f), gSavedSettings.getS32("DebugBeaconLineWidth"));
  2117. }
  2118. if (gPipeline.sRenderHighlight)
  2119. {
  2120. S32 face_id;
  2121. S32 count = drawablep->getNumFaces();
  2122. for (face_id = 0; face_id < count; face_id++)
  2123. {
  2124. gPipeline.mHighlightFaces.push_back(drawablep->getFace(face_id) );
  2125. }
  2126. }
  2127. }
  2128. }
  2129. void renderSoundHighlights(LLDrawable* drawablep)
  2130. {
  2131. // Look for attachments, objects, etc.
  2132. LLViewerObject *vobj = drawablep->getVObj();
  2133. if (vobj && vobj->isAudioSource())
  2134. {
  2135. if (gPipeline.sRenderHighlight)
  2136. {
  2137. S32 face_id;
  2138. S32 count = drawablep->getNumFaces();
  2139. for (face_id = 0; face_id < count; face_id++)
  2140. {
  2141. gPipeline.mHighlightFaces.push_back(drawablep->getFace(face_id) );
  2142. }
  2143. }
  2144. }
  2145. }
  2146. void LLPipeline::postSort(LLCamera& camera)
  2147. {
  2148. LLMemType mt(LLMemType::MTYPE_PIPELINE_POST_SORT);
  2149. LLFastTimer ftm(FTM_STATESORT_POSTSORT);
  2150. assertInitialized();
  2151. //rebuild drawable geometry
  2152. for (LLCullResult::sg_list_t::iterator i = sCull->beginDrawableGroups(); i != sCull->endDrawableGroups(); ++i)
  2153. {
  2154. LLSpatialGroup* group = *i;
  2155. if (!sUseOcclusion || 
  2156. !group->isOcclusionState(LLSpatialGroup::OCCLUDED))
  2157. {
  2158. group->rebuildGeom();
  2159. }
  2160. }
  2161. //rebuild groups
  2162. sCull->assertDrawMapsEmpty();
  2163. /*LLSpatialGroup::sNoDelete = FALSE;
  2164. for (LLCullResult::sg_list_t::iterator i = sCull->beginVisibleGroups(); i != sCull->endVisibleGroups(); ++i)
  2165. {
  2166. LLSpatialGroup* group = *i;
  2167. if (sUseOcclusion && 
  2168. group->isState(LLSpatialGroup::OCCLUDED))
  2169. {
  2170. continue;
  2171. }
  2172. group->rebuildGeom();
  2173. }
  2174. LLSpatialGroup::sNoDelete = TRUE;*/
  2175. rebuildPriorityGroups();
  2176. const S32 bin_count = 1024*8;
  2177. static LLCullResult::drawinfo_list_t alpha_bins[bin_count];
  2178. static U32 bin_size[bin_count];
  2179. //clear one bin per frame to avoid memory bloat
  2180. static S32 clear_idx = 0;
  2181. clear_idx = (1+clear_idx)%bin_count;
  2182. alpha_bins[clear_idx].clear();
  2183. for (U32 j = 0; j < bin_count; j++)
  2184. {
  2185. bin_size[j] = 0;
  2186. }
  2187. //build render map
  2188. for (LLCullResult::sg_list_t::iterator i = sCull->beginVisibleGroups(); i != sCull->endVisibleGroups(); ++i)
  2189. {
  2190. LLSpatialGroup* group = *i;
  2191. if (sUseOcclusion && 
  2192. group->isOcclusionState(LLSpatialGroup::OCCLUDED))
  2193. {
  2194. continue;
  2195. }
  2196. if (group->isState(LLSpatialGroup::NEW_DRAWINFO) && group->isState(LLSpatialGroup::GEOM_DIRTY))
  2197. { //no way this group is going to be drawable without a rebuild
  2198. group->rebuildGeom();
  2199. }
  2200. for (LLSpatialGroup::draw_map_t::iterator j = group->mDrawMap.begin(); j != group->mDrawMap.end(); ++j)
  2201. {
  2202. LLSpatialGroup::drawmap_elem_t& src_vec = j->second;
  2203. if (!hasRenderType(j->first))
  2204. {
  2205. continue;
  2206. }
  2207. for (LLSpatialGroup::drawmap_elem_t::iterator k = src_vec.begin(); k != src_vec.end(); ++k)
  2208. {
  2209. if (sMinRenderSize > 0.f)
  2210. {
  2211. LLVector3 bounds = (*k)->mExtents[1]-(*k)->mExtents[0];
  2212. if (llmax(llmax(bounds.mV[0], bounds.mV[1]), bounds.mV[2]) > sMinRenderSize)
  2213. {
  2214. sCull->pushDrawInfo(j->first, *k);
  2215. }
  2216. }
  2217. else
  2218. {
  2219. sCull->pushDrawInfo(j->first, *k);
  2220. }
  2221. }
  2222. }
  2223. if (hasRenderType(LLPipeline::RENDER_TYPE_PASS_ALPHA))
  2224. {
  2225. LLSpatialGroup::draw_map_t::iterator alpha = group->mDrawMap.find(LLRenderPass::PASS_ALPHA);
  2226. if (alpha != group->mDrawMap.end())
  2227. { //store alpha groups for sorting
  2228. LLSpatialBridge* bridge = group->mSpatialPartition->asBridge();
  2229. if (LLViewerCamera::sCurCameraID == LLViewerCamera::CAMERA_WORLD)
  2230. {
  2231. if (bridge)
  2232. {
  2233. LLCamera trans_camera = bridge->transformCamera(camera);
  2234. group->updateDistance(trans_camera);
  2235. }
  2236. else
  2237. {
  2238. group->updateDistance(camera);
  2239. }
  2240. }
  2241. if (hasRenderType(LLDrawPool::POOL_ALPHA))
  2242. {
  2243. sCull->pushAlphaGroup(group);
  2244. }
  2245. }
  2246. }
  2247. }
  2248. if (!sShadowRender)
  2249. {
  2250. //sort by texture or bump map
  2251. for (U32 i = 0; i < LLRenderPass::NUM_RENDER_TYPES; ++i)
  2252. {
  2253. if (i == LLRenderPass::PASS_BUMP)
  2254. {
  2255. std::sort(sCull->beginRenderMap(i), sCull->endRenderMap(i), LLDrawInfo::CompareBump());
  2256. }
  2257. else 
  2258. {
  2259. std::sort(sCull->beginRenderMap(i), sCull->endRenderMap(i), LLDrawInfo::CompareTexturePtrMatrix());
  2260. }
  2261. }
  2262. std::sort(sCull->beginAlphaGroups(), sCull->endAlphaGroups(), LLSpatialGroup::CompareDepthGreater());
  2263. }
  2264. // only render if the flag is set. The flag is only set if we are in edit mode or the toggle is set in the menus
  2265. if (LLFloaterReg::instanceVisible("beacons") && !sShadowRender)
  2266. {
  2267. if (sRenderScriptedTouchBeacons)
  2268. {
  2269. // Only show the beacon on the root object.
  2270. forAllVisibleDrawables(renderScriptedTouchBeacons);
  2271. }
  2272. else
  2273. if (sRenderScriptedBeacons)
  2274. {
  2275. // Only show the beacon on the root object.
  2276. forAllVisibleDrawables(renderScriptedBeacons);
  2277. }
  2278. if (sRenderPhysicalBeacons)
  2279. {
  2280. // Only show the beacon on the root object.
  2281. forAllVisibleDrawables(renderPhysicalBeacons);
  2282. }
  2283. if (sRenderParticleBeacons)
  2284. {
  2285. forAllVisibleDrawables(renderParticleBeacons);
  2286. }
  2287. // If god mode, also show audio cues
  2288. if (sRenderSoundBeacons && gAudiop)
  2289. {
  2290. // Walk all sound sources and render out beacons for them. Note, this isn't done in the ForAllVisibleDrawables function, because some are not visible.
  2291. LLAudioEngine::source_map::iterator iter;
  2292. for (iter = gAudiop->mAllSources.begin(); iter != gAudiop->mAllSources.end(); ++iter)
  2293. {
  2294. LLAudioSource *sourcep = iter->second;
  2295. LLVector3d pos_global = sourcep->getPositionGlobal();
  2296. LLVector3 pos = gAgent.getPosAgentFromGlobal(pos_global);
  2297. if (gPipeline.sRenderBeacons)
  2298. {
  2299. //pos += LLVector3(0.f, 0.f, 0.2f);
  2300. gObjectList.addDebugBeacon(pos, "", LLColor4(1.f, 1.f, 0.f, 0.5f), LLColor4(1.f, 1.f, 1.f, 0.5f), gSavedSettings.getS32("DebugBeaconLineWidth"));
  2301. }
  2302. }
  2303. // now deal with highlights for all those seeable sound sources
  2304. forAllVisibleDrawables(renderSoundHighlights);
  2305. }
  2306. }
  2307. // If managing your telehub, draw beacons at telehub and currently selected spawnpoint.
  2308. if (LLFloaterTelehub::renderBeacons())
  2309. {
  2310. LLFloaterTelehub::addBeacons();
  2311. }
  2312. if (!sShadowRender)
  2313. {
  2314. mSelectedFaces.clear();
  2315. // Draw face highlights for selected faces.
  2316. if (LLSelectMgr::getInstance()->getTEMode())
  2317. {
  2318. struct f : public LLSelectedTEFunctor
  2319. {
  2320. virtual bool apply(LLViewerObject* object, S32 te)
  2321. {
  2322. if (object->mDrawable)
  2323. {
  2324. gPipeline.mSelectedFaces.push_back(object->mDrawable->getFace(te));
  2325. }
  2326. return true;
  2327. }
  2328. } func;
  2329. LLSelectMgr::getInstance()->getSelection()->applyToTEs(&func);
  2330. }
  2331. }
  2332. //LLSpatialGroup::sNoDelete = FALSE;
  2333. }
  2334. void render_hud_elements()
  2335. {
  2336. LLMemType mt_rhe(LLMemType::MTYPE_PIPELINE_RENDER_HUD_ELS);
  2337. LLFastTimer t(FTM_RENDER_UI);
  2338. gPipeline.disableLights();
  2339. LLGLDisable fog(GL_FOG);
  2340. LLGLSUIDefault gls_ui;
  2341. LLGLEnable stencil(GL_STENCIL_TEST);
  2342. glStencilFunc(GL_ALWAYS, 255, 0xFFFFFFFF);
  2343. glStencilMask(0xFFFFFFFF);
  2344. glStencilOp(GL_KEEP, GL_KEEP, GL_REPLACE);
  2345. gGL.color4f(1,1,1,1);
  2346. if (!LLPipeline::sReflectionRender && gPipeline.hasRenderDebugFeatureMask(LLPipeline::RENDER_DEBUG_FEATURE_UI))
  2347. {
  2348. LLGLEnable multisample(GL_MULTISAMPLE_ARB);
  2349. gViewerWindow->renderSelections(FALSE, FALSE, FALSE); // For HUD version in render_ui_3d()
  2350. // Draw the tracking overlays
  2351. LLTracker::render3D();
  2352. // Show the property lines
  2353. LLWorld::getInstance()->renderPropertyLines();
  2354. LLViewerParcelMgr::getInstance()->render();
  2355. LLViewerParcelMgr::getInstance()->renderParcelCollision();
  2356. // Render name tags.
  2357. LLHUDObject::renderAll();
  2358. }
  2359. else if (gForceRenderLandFence)
  2360. {
  2361. // This is only set when not rendering the UI, for parcel snapshots
  2362. LLViewerParcelMgr::getInstance()->render();
  2363. }
  2364. else if (gPipeline.hasRenderType(LLPipeline::RENDER_TYPE_HUD))
  2365. {
  2366. LLHUDText::renderAllHUD();
  2367. }
  2368. gGL.flush();
  2369. }
  2370. void LLPipeline::renderHighlights()
  2371. {
  2372. LLMemType mt(LLMemType::MTYPE_PIPELINE_RENDER_HL);
  2373. assertInitialized();
  2374. // Draw 3D UI elements here (before we clear the Z buffer in POOL_HUD)
  2375. // Render highlighted faces.
  2376. LLGLSPipelineAlpha gls_pipeline_alpha;
  2377. LLColor4 color(1.f, 1.f, 1.f, 0.5f);
  2378. LLGLEnable color_mat(GL_COLOR_MATERIAL);
  2379. disableLights();
  2380. if (!hasRenderType(LLPipeline::RENDER_TYPE_HUD) && !mHighlightSet.empty())
  2381. { //draw blurry highlight image over screen
  2382. LLGLEnable blend(GL_BLEND);
  2383. LLGLDepthTest depth(GL_TRUE, GL_FALSE, GL_ALWAYS);
  2384. LLGLDisable test(GL_ALPHA_TEST);
  2385. LLGLEnable stencil(GL_STENCIL_TEST);
  2386. gGL.flush();
  2387. glStencilMask(0xFFFFFFFF);
  2388. glClearStencil(1);
  2389. glClear(GL_STENCIL_BUFFER_BIT);
  2390. glStencilFunc(GL_ALWAYS, 0, 0xFFFFFFFF);
  2391. glStencilOp(GL_REPLACE, GL_REPLACE, GL_REPLACE);
  2392. gGL.setColorMask(false, false);
  2393. for (std::set<HighlightItem>::iterator iter = mHighlightSet.begin(); iter != mHighlightSet.end(); ++iter)
  2394. {
  2395. renderHighlight(iter->mItem->getVObj(), 1.f);
  2396. }
  2397. gGL.setColorMask(true, false);
  2398. glStencilOp(GL_KEEP, GL_KEEP, GL_KEEP);
  2399. glStencilFunc(GL_NOTEQUAL, 0, 0xFFFFFFFF);
  2400. //gGL.setSceneBlendType(LLRender::BT_ADD_WITH_ALPHA);
  2401. gGL.pushMatrix();
  2402. glLoadIdentity();
  2403. glMatrixMode(GL_PROJECTION);
  2404. gGL.pushMatrix();
  2405. glLoadIdentity();
  2406. gGL.getTexUnit(0)->bind(&mHighlight);
  2407. LLVector2 tc1;
  2408. LLVector2 tc2;
  2409. tc1.setVec(0,0);
  2410. tc2.setVec(2,2);
  2411. gGL.begin(LLRender::TRIANGLES);
  2412. F32 scale = gSavedSettings.getF32("RenderHighlightBrightness");
  2413. LLColor4 color = gSavedSettings.getColor4("RenderHighlightColor");
  2414. F32 thickness = gSavedSettings.getF32("RenderHighlightThickness");
  2415. for (S32 pass = 0; pass < 2; ++pass)
  2416. {
  2417. if (pass == 0)
  2418. {
  2419. gGL.setSceneBlendType(LLRender::BT_ADD_WITH_ALPHA);
  2420. }
  2421. else
  2422. {
  2423. gGL.setSceneBlendType(LLRender::BT_ALPHA);
  2424. }
  2425. for (S32 i = 0; i < 8; ++i)
  2426. {
  2427. for (S32 j = 0; j < 8; ++j)
  2428. {
  2429. LLVector2 tc(i-4+0.5f, j-4+0.5f);
  2430. F32 dist = 1.f-(tc.length()/sqrtf(32.f));
  2431. dist *= scale/64.f;
  2432. tc *= thickness;
  2433. tc.mV[0] = (tc.mV[0])/mHighlight.getWidth();
  2434. tc.mV[1] = (tc.mV[1])/mHighlight.getHeight();
  2435. gGL.color4f(color.mV[0],
  2436. color.mV[1],
  2437. color.mV[2],
  2438. color.mV[3]*dist);
  2439. gGL.texCoord2f(tc.mV[0]+tc1.mV[0], tc.mV[1]+tc2.mV[1]);
  2440. gGL.vertex2f(-1,3);
  2441. gGL.texCoord2f(tc.mV[0]+tc1.mV[0], tc.mV[1]+tc1.mV[1]);
  2442. gGL.vertex2f(-1,-1);
  2443. gGL.texCoord2f(tc.mV[0]+tc2.mV[0], tc.mV[1]+tc1.mV[1]);
  2444. gGL.vertex2f(3,-1);
  2445. }
  2446. }
  2447. }
  2448. gGL.end();
  2449. gGL.popMatrix();
  2450. glMatrixMode(GL_MODELVIEW);
  2451. gGL.popMatrix();
  2452. //gGL.setSceneBlendType(LLRender::BT_ALPHA);
  2453. }
  2454. if ((LLViewerShaderMgr::instance()->getVertexShaderLevel(LLViewerShaderMgr::SHADER_INTERFACE) > 0))
  2455. {
  2456. gHighlightProgram.bind();
  2457. gHighlightProgram.vertexAttrib4f(LLViewerShaderMgr::MATERIAL_COLOR,1,1,1,0.5f);
  2458. }
  2459. if (hasRenderDebugFeatureMask(RENDER_DEBUG_FEATURE_SELECTED))
  2460. {
  2461. // Make sure the selection image gets downloaded and decoded
  2462. if (!mFaceSelectImagep)
  2463. {
  2464. mFaceSelectImagep = LLViewerTextureManager::getFetchedTexture(IMG_FACE_SELECT);
  2465. }
  2466. mFaceSelectImagep->addTextureStats((F32)MAX_IMAGE_AREA);
  2467. U32 count = mSelectedFaces.size();
  2468. for (U32 i = 0; i < count; i++)
  2469. {
  2470. LLFace *facep = mSelectedFaces[i];
  2471. if (!facep || facep->getDrawable()->isDead())
  2472. {
  2473. llerrs << "Bad face on selection" << llendl;
  2474. return;
  2475. }
  2476. facep->renderSelected(mFaceSelectImagep, color);
  2477. }
  2478. }
  2479. if (hasRenderDebugFeatureMask(RENDER_DEBUG_FEATURE_SELECTED))
  2480. {
  2481. // Paint 'em red!
  2482. color.setVec(1.f, 0.f, 0.f, 0.5f);
  2483. if ((LLViewerShaderMgr::instance()->getVertexShaderLevel(LLViewerShaderMgr::SHADER_INTERFACE) > 0))
  2484. {
  2485. gHighlightProgram.vertexAttrib4f(LLViewerShaderMgr::MATERIAL_COLOR,1,0,0,0.5f);
  2486. }
  2487. int count = mHighlightFaces.size();
  2488. for (S32 i = 0; i < count; i++)
  2489. {
  2490. LLFace* facep = mHighlightFaces[i];
  2491. facep->renderSelected(LLViewerTexture::sNullImagep, color);
  2492. }
  2493. }
  2494. // Contains a list of the faces of objects that are physical or
  2495. // have touch-handlers.
  2496. mHighlightFaces.clear();
  2497. if (LLViewerShaderMgr::instance()->getVertexShaderLevel(LLViewerShaderMgr::SHADER_INTERFACE) > 0)
  2498. {
  2499. gHighlightProgram.unbind();
  2500. }
  2501. }
  2502. void LLPipeline::renderGeom(LLCamera& camera, BOOL forceVBOUpdate)
  2503. {
  2504. LLMemType mt(LLMemType::MTYPE_PIPELINE_RENDER_GEOM);
  2505. LLFastTimer t(FTM_RENDER_GEOMETRY);
  2506. assertInitialized();
  2507. F64 saved_modelview[16];
  2508. F64 saved_projection[16];
  2509. //HACK: preserve/restore matrices around HUD render
  2510. if (gPipeline.hasRenderType(LLPipeline::RENDER_TYPE_HUD))
  2511. {
  2512. for (U32 i = 0; i < 16; i++)
  2513. {
  2514. saved_modelview[i] = gGLModelView[i];
  2515. saved_projection[i] = gGLProjection[i];
  2516. }
  2517. }
  2518. ///////////////////////////////////////////
  2519. //
  2520. // Sync and verify GL state
  2521. //
  2522. //
  2523. stop_glerror();
  2524. LLVertexBuffer::unbind();
  2525. // Do verification of GL state
  2526. LLGLState::checkStates();
  2527. LLGLState::checkTextureChannels();
  2528. LLGLState::checkClientArrays();
  2529. if (mRenderDebugMask & RENDER_DEBUG_VERIFY)
  2530. {
  2531. if (!verify())
  2532. {
  2533. llerrs << "Pipeline verification failed!" << llendl;
  2534. }
  2535. }
  2536. LLAppViewer::instance()->pingMainloopTimeout("Pipeline:ForceVBO");
  2537. // Initialize lots of GL state to "safe" values
  2538. glMatrixMode(GL_TEXTURE);
  2539. glLoadIdentity();
  2540. glMatrixMode(GL_MODELVIEW);
  2541. LLGLSPipeline gls_pipeline;
  2542. LLGLEnable multisample(GL_MULTISAMPLE_ARB);
  2543. LLGLState gls_color_material(GL_COLOR_MATERIAL, mLightingDetail < 2);
  2544. // Toggle backface culling for debugging
  2545. LLGLEnable cull_face(mBackfaceCull ? GL_CULL_FACE : 0);
  2546. // Set fog
  2547. BOOL use_fog = hasRenderDebugFeatureMask(LLPipeline::RENDER_DEBUG_FEATURE_FOG);
  2548. LLGLEnable fog_enable(use_fog &&
  2549.   !gPipeline.canUseWindLightShadersOnObjects() ? GL_FOG : 0);
  2550. gSky.updateFog(camera.getFar());
  2551. if (!use_fog)
  2552. {
  2553. sUnderWaterRender = FALSE;
  2554. }
  2555. gGL.getTexUnit(0)->bind(LLViewerFetchedTexture::sDefaultImagep);
  2556. LLViewerFetchedTexture::sDefaultImagep->setAddressMode(LLTexUnit::TAM_WRAP);
  2557. //////////////////////////////////////////////
  2558. //
  2559. // Actually render all of the geometry
  2560. //
  2561. //
  2562. stop_glerror();
  2563. LLAppViewer::instance()->pingMainloopTimeout("Pipeline:RenderDrawPools");
  2564. for (pool_set_t::iterator iter = mPools.begin(); iter != mPools.end(); ++iter)
  2565. {
  2566. LLDrawPool *poolp = *iter;
  2567. if (hasRenderType(poolp->getType()))
  2568. {
  2569. poolp->prerender();
  2570. }
  2571. }
  2572. if (gPipeline.hasRenderDebugMask(LLPipeline::RENDER_DEBUG_PICKING))
  2573. {
  2574. LLAppViewer::instance()->pingMainloopTimeout("Pipeline:RenderForSelect");
  2575. gObjectList.renderObjectsForSelect(camera, gViewerWindow->getWindowRectScaled());
  2576. }
  2577. else
  2578. {
  2579. LLFastTimer t(FTM_POOLS);
  2580. // HACK: don't calculate local lights if we're rendering the HUD!
  2581. //    Removing this check will cause bad flickering when there are 
  2582. //    HUD elements being rendered AND the user is in flycam mode  -nyx
  2583. if (!gPipeline.hasRenderType(LLPipeline::RENDER_TYPE_HUD))
  2584. {
  2585. calcNearbyLights(camera);
  2586. setupHWLights(NULL);
  2587. }
  2588. BOOL occlude = sUseOcclusion > 1;
  2589. U32 cur_type = 0;
  2590. pool_set_t::iterator iter1 = mPools.begin();
  2591. while ( iter1 != mPools.end() )
  2592. {
  2593. LLDrawPool *poolp = *iter1;
  2594. cur_type = poolp->getType();
  2595. if (occlude && cur_type >= LLDrawPool::POOL_GRASS)
  2596. {
  2597. occlude = FALSE;
  2598. gGLLastMatrix = NULL;
  2599. glLoadMatrixd(gGLModelView);
  2600. doOcclusion(camera);
  2601. }
  2602. pool_set_t::iterator iter2 = iter1;
  2603. if (hasRenderType(poolp->getType()) && poolp->getNumPasses() > 0)
  2604. {
  2605. LLFastTimer t(FTM_POOLRENDER);
  2606. gGLLastMatrix = NULL;
  2607. glLoadMatrixd(gGLModelView);
  2608. for( S32 i = 0; i < poolp->getNumPasses(); i++ )
  2609. {
  2610. LLVertexBuffer::unbind();
  2611. poolp->beginRenderPass(i);
  2612. for (iter2 = iter1; iter2 != mPools.end(); iter2++)
  2613. {
  2614. LLDrawPool *p = *iter2;
  2615. if (p->getType() != cur_type)
  2616. {
  2617. break;
  2618. }
  2619. p->render(i);
  2620. }
  2621. poolp->endRenderPass(i);
  2622. LLVertexBuffer::unbind();
  2623. if (gDebugGL || gDebugPipeline)
  2624. {
  2625. GLint depth;
  2626. glGetIntegerv(GL_MODELVIEW_STACK_DEPTH, &depth);
  2627. if (depth > 3)
  2628. {
  2629. if (gDebugSession)
  2630. {
  2631. ll_fail("GL matrix stack corrupted.");
  2632. }
  2633. llerrs << "GL matrix stack corrupted!" << llendl;
  2634. }
  2635. std::string msg = llformat("%s pass %d", gPoolNames[cur_type].c_str(), i);
  2636. LLGLState::checkStates(msg);
  2637. LLGLState::checkTextureChannels(msg);
  2638. LLGLState::checkClientArrays(msg);
  2639. }
  2640. }
  2641. }
  2642. else
  2643. {
  2644. // Skip all pools of this type
  2645. for (iter2 = iter1; iter2 != mPools.end(); iter2++)
  2646. {
  2647. LLDrawPool *p = *iter2;
  2648. if (p->getType() != cur_type)
  2649. {
  2650. break;
  2651. }
  2652. }
  2653. }
  2654. iter1 = iter2;
  2655. stop_glerror();
  2656. }
  2657. LLAppViewer::instance()->pingMainloopTimeout("Pipeline:RenderDrawPoolsEnd");
  2658. LLVertexBuffer::unbind();
  2659. gGLLastMatrix = NULL;
  2660. glLoadMatrixd(gGLModelView);
  2661. if (occlude)
  2662. {
  2663. occlude = FALSE;
  2664. gGLLastMatrix = NULL;
  2665. glLoadMatrixd(gGLModelView);
  2666. doOcclusion(camera);
  2667. }
  2668. }
  2669. LLVertexBuffer::unbind();
  2670. LLGLState::checkStates();
  2671. LLGLState::checkTextureChannels();
  2672. LLGLState::checkClientArrays();
  2673. stop_glerror();
  2674. LLGLState::checkStates();
  2675. LLGLState::checkTextureChannels();
  2676. LLGLState::checkClientArrays();
  2677. LLAppViewer::instance()->pingMainloopTimeout("Pipeline:RenderHighlights");
  2678. if (!sReflectionRender)
  2679. {
  2680. renderHighlights();
  2681. }
  2682. // Contains a list of the faces of objects that are physical or
  2683. // have touch-handlers.
  2684. mHighlightFaces.clear();
  2685. LLAppViewer::instance()->pingMainloopTimeout("Pipeline:RenderDebug");
  2686. renderDebug();
  2687. LLVertexBuffer::unbind();
  2688. if (!LLPipeline::sReflectionRender && !LLPipeline::sRenderDeferred && gPipeline.hasRenderDebugFeatureMask(LLPipeline::RENDER_DEBUG_FEATURE_UI))
  2689. {
  2690. // Render debugging beacons.
  2691. gObjectList.renderObjectBeacons();
  2692. gObjectList.resetObjectBeacons();
  2693. }
  2694. LLAppViewer::instance()->pingMainloopTimeout("Pipeline:RenderGeomEnd");
  2695. //HACK: preserve/restore matrices around HUD render
  2696. if (gPipeline.hasRenderType(LLPipeline::RENDER_TYPE_HUD))
  2697. {
  2698. for (U32 i = 0; i < 16; i++)
  2699. {
  2700. gGLModelView[i] = saved_modelview[i];
  2701. gGLProjection[i] = saved_projection[i];
  2702. }
  2703. }
  2704. LLVertexBuffer::unbind();
  2705. LLGLState::checkStates();
  2706. LLGLState::checkTextureChannels();
  2707. LLGLState::checkClientArrays();
  2708. }
  2709. void LLPipeline::renderGeomDeferred(LLCamera& camera)
  2710. {
  2711. LLAppViewer::instance()->pingMainloopTimeout("Pipeline:RenderGeomDeferred");
  2712. LLMemType mt_rgd(LLMemType::MTYPE_PIPELINE_RENDER_GEOM_DEFFERRED);
  2713. LLFastTimer t(FTM_RENDER_GEOMETRY);
  2714. LLFastTimer t2(FTM_POOLS);
  2715. LLGLEnable cull(GL_CULL_FACE);
  2716. LLGLEnable stencil(GL_STENCIL_TEST);
  2717. glStencilFunc(GL_ALWAYS, 1, 0xFFFFFFFF);
  2718. stop_glerror();
  2719. glStencilOp(GL_KEEP, GL_KEEP, GL_REPLACE);
  2720. stop_glerror();
  2721. for (pool_set_t::iterator iter = mPools.begin(); iter != mPools.end(); ++iter)
  2722. {
  2723. LLDrawPool *poolp = *iter;
  2724. if (hasRenderType(poolp->getType()))
  2725. {
  2726. poolp->prerender();
  2727. }
  2728. }
  2729. LLGLEnable multisample(GL_MULTISAMPLE_ARB);
  2730. LLVertexBuffer::unbind();
  2731. LLGLState::checkStates();
  2732. LLGLState::checkTextureChannels();
  2733. LLGLState::checkClientArrays();
  2734. U32 cur_type = 0;
  2735. gGL.setColorMask(true, true);
  2736. pool_set_t::iterator iter1 = mPools.begin();
  2737. while ( iter1 != mPools.end() )
  2738. {
  2739. LLDrawPool *poolp = *iter1;
  2740. cur_type = poolp->getType();
  2741. pool_set_t::iterator iter2 = iter1;
  2742. if (hasRenderType(poolp->getType()) && poolp->getNumDeferredPasses() > 0)
  2743. {
  2744. LLFastTimer t(FTM_POOLRENDER);
  2745. gGLLastMatrix = NULL;
  2746. glLoadMatrixd(gGLModelView);
  2747. for( S32 i = 0; i < poolp->getNumDeferredPasses(); i++ )
  2748. {
  2749. LLVertexBuffer::unbind();
  2750. poolp->beginDeferredPass(i);
  2751. for (iter2 = iter1; iter2 != mPools.end(); iter2++)
  2752. {
  2753. LLDrawPool *p = *iter2;
  2754. if (p->getType() != cur_type)
  2755. {
  2756. break;
  2757. }
  2758. p->renderDeferred(i);
  2759. }
  2760. poolp->endDeferredPass(i);
  2761. LLVertexBuffer::unbind();
  2762. if (gDebugGL || gDebugPipeline)
  2763. {
  2764. GLint depth;
  2765. glGetIntegerv(GL_MODELVIEW_STACK_DEPTH, &depth);
  2766. if (depth > 3)
  2767. {
  2768. llerrs << "GL matrix stack corrupted!" << llendl;
  2769. }
  2770. LLGLState::checkStates();
  2771. LLGLState::checkTextureChannels();
  2772. LLGLState::checkClientArrays();
  2773. }
  2774. }
  2775. }
  2776. else
  2777. {
  2778. // Skip all pools of this type
  2779. for (iter2 = iter1; iter2 != mPools.end(); iter2++)
  2780. {
  2781. LLDrawPool *p = *iter2;
  2782. if (p->getType() != cur_type)
  2783. {
  2784. break;
  2785. }
  2786. }
  2787. }
  2788. iter1 = iter2;
  2789. stop_glerror();
  2790. }
  2791. gGLLastMatrix = NULL;
  2792. glLoadMatrixd(gGLModelView);
  2793. gGL.setColorMask(true, false);
  2794. }
  2795. void LLPipeline::renderGeomPostDeferred(LLCamera& camera)
  2796. {
  2797. LLMemType mt_rgpd(LLMemType::MTYPE_PIPELINE_RENDER_GEOM_POST_DEF);
  2798. LLFastTimer t(FTM_POOLS);
  2799. U32 cur_type = 0;
  2800. LLGLEnable cull(GL_CULL_FACE);
  2801. LLGLEnable multisample(GL_MULTISAMPLE_ARB);
  2802. calcNearbyLights(camera);
  2803. setupHWLights(NULL);
  2804. gGL.setColorMask(true, false);
  2805. pool_set_t::iterator iter1 = mPools.begin();
  2806. BOOL occlude = LLPipeline::sUseOcclusion > 1;
  2807. while ( iter1 != mPools.end() )
  2808. {
  2809. LLDrawPool *poolp = *iter1;
  2810. cur_type = poolp->getType();
  2811. if (occlude && cur_type >= LLDrawPool::POOL_GRASS)
  2812. {
  2813. occlude = FALSE;
  2814. gGLLastMatrix = NULL;
  2815. glLoadMatrixd(gGLModelView);
  2816. doOcclusion(camera);
  2817. gGL.setColorMask(true, false);
  2818. }
  2819. pool_set_t::iterator iter2 = iter1;
  2820. if (hasRenderType(poolp->getType()) && poolp->getNumPostDeferredPasses() > 0)
  2821. {
  2822. LLFastTimer t(FTM_POOLRENDER);
  2823. gGLLastMatrix = NULL;
  2824. glLoadMatrixd(gGLModelView);
  2825. for( S32 i = 0; i < poolp->getNumPostDeferredPasses(); i++ )
  2826. {
  2827. LLVertexBuffer::unbind();
  2828. poolp->beginPostDeferredPass(i);
  2829. for (iter2 = iter1; iter2 != mPools.end(); iter2++)
  2830. {
  2831. LLDrawPool *p = *iter2;
  2832. if (p->getType() != cur_type)
  2833. {
  2834. break;
  2835. }
  2836. p->renderPostDeferred(i);
  2837. }
  2838. poolp->endPostDeferredPass(i);
  2839. LLVertexBuffer::unbind();
  2840. if (gDebugGL || gDebugPipeline)
  2841. {
  2842. GLint depth;
  2843. glGetIntegerv(GL_MODELVIEW_STACK_DEPTH, &depth);
  2844. if (depth > 3)
  2845. {
  2846. llerrs << "GL matrix stack corrupted!" << llendl;
  2847. }
  2848. LLGLState::checkStates();
  2849. LLGLState::checkTextureChannels();
  2850. LLGLState::checkClientArrays();
  2851. }
  2852. }
  2853. }
  2854. else
  2855. {
  2856. // Skip all pools of this type
  2857. for (iter2 = iter1; iter2 != mPools.end(); iter2++)
  2858. {
  2859. LLDrawPool *p = *iter2;
  2860. if (p->getType() != cur_type)
  2861. {
  2862. break;
  2863. }
  2864. }
  2865. }
  2866. iter1 = iter2;
  2867. stop_glerror();
  2868. }
  2869. gGLLastMatrix = NULL;
  2870. glLoadMatrixd(gGLModelView);
  2871. renderHighlights();
  2872. mHighlightFaces.clear();
  2873. renderDebug();
  2874. LLVertexBuffer::unbind();
  2875. if (gPipeline.hasRenderDebugFeatureMask(LLPipeline::RENDER_DEBUG_FEATURE_UI))
  2876. {
  2877. // Render debugging beacons.
  2878. gObjectList.renderObjectBeacons();
  2879. gObjectList.resetObjectBeacons();
  2880. }
  2881. if (occlude)
  2882. {
  2883. occlude = FALSE;
  2884. gGLLastMatrix = NULL;
  2885. glLoadMatrixd(gGLModelView);
  2886. doOcclusion(camera);
  2887. }
  2888. }
  2889. void LLPipeline::renderGeomShadow(LLCamera& camera)
  2890. {
  2891. LLMemType mt_rgs(LLMemType::MTYPE_PIPELINE_RENDER_GEOM_SHADOW);
  2892. U32 cur_type = 0;
  2893. LLGLEnable cull(GL_CULL_FACE);
  2894. LLVertexBuffer::unbind();
  2895. pool_set_t::iterator iter1 = mPools.begin();
  2896. while ( iter1 != mPools.end() )
  2897. {
  2898. LLDrawPool *poolp = *iter1;
  2899. cur_type = poolp->getType();
  2900. pool_set_t::iterator iter2 = iter1;
  2901. if (hasRenderType(poolp->getType()) && poolp->getNumShadowPasses() > 0)
  2902. {
  2903. gGLLastMatrix = NULL;
  2904. glLoadMatrixd(gGLModelView);
  2905. for( S32 i = 0; i < poolp->getNumShadowPasses(); i++ )
  2906. {
  2907. LLVertexBuffer::unbind();
  2908. poolp->beginShadowPass(i);
  2909. for (iter2 = iter1; iter2 != mPools.end(); iter2++)
  2910. {
  2911. LLDrawPool *p = *iter2;
  2912. if (p->getType() != cur_type)
  2913. {
  2914. break;
  2915. }
  2916. p->renderShadow(i);
  2917. }
  2918. poolp->endShadowPass(i);
  2919. LLVertexBuffer::unbind();
  2920. LLGLState::checkStates();
  2921. LLGLState::checkTextureChannels();
  2922. LLGLState::checkClientArrays();
  2923. }
  2924. }
  2925. else
  2926. {
  2927. // Skip all pools of this type
  2928. for (iter2 = iter1; iter2 != mPools.end(); iter2++)
  2929. {
  2930. LLDrawPool *p = *iter2;
  2931. if (p->getType() != cur_type)
  2932. {
  2933. break;
  2934. }
  2935. }
  2936. }
  2937. iter1 = iter2;
  2938. stop_glerror();
  2939. }
  2940. gGLLastMatrix = NULL;
  2941. glLoadMatrixd(gGLModelView);
  2942. }
  2943. void LLPipeline::addTrianglesDrawn(S32 count)
  2944. {
  2945. assertInitialized();
  2946. mTrianglesDrawn += count;
  2947. mBatchCount++;
  2948. mMaxBatchSize = llmax(mMaxBatchSize, count);
  2949. mMinBatchSize = llmin(mMinBatchSize, count);
  2950. if (LLPipeline::sRenderFrameTest)
  2951. {
  2952. gViewerWindow->getWindow()->swapBuffers();
  2953. ms_sleep(16);
  2954. }
  2955. }
  2956. void LLPipeline::renderDebug()
  2957. {
  2958. LLMemType mt(LLMemType::MTYPE_PIPELINE);
  2959. assertInitialized();
  2960. gGL.color4f(1,1,1,1);
  2961. gGLLastMatrix = NULL;
  2962. glLoadMatrixd(gGLModelView);
  2963. gGL.setColorMask(true, false);
  2964. // Debug stuff.
  2965. for (LLWorld::region_list_t::const_iterator iter = LLWorld::getInstance()->getRegionList().begin(); 
  2966. iter != LLWorld::getInstance()->getRegionList().end(); ++iter)
  2967. {
  2968. LLViewerRegion* region = *iter;
  2969. for (U32 i = 0; i < LLViewerRegion::NUM_PARTITIONS; i++)
  2970. {
  2971. LLSpatialPartition* part = region->getSpatialPartition(i);
  2972. if (part)
  2973. {
  2974. if (hasRenderType(part->mDrawableType))
  2975. {
  2976. part->renderDebug();
  2977. }
  2978. }
  2979. }
  2980. }
  2981. for (LLCullResult::bridge_list_t::const_iterator i = sCull->beginVisibleBridge(); i != sCull->endVisibleBridge(); ++i)
  2982. {
  2983. LLSpatialBridge* bridge = *i;
  2984. if (!bridge->isDead() && hasRenderType(bridge->mDrawableType))
  2985. {
  2986. glPushMatrix();
  2987. glMultMatrixf((F32*)bridge->mDrawable->getRenderMatrix().mMatrix);
  2988. bridge->renderDebug();
  2989. glPopMatrix();
  2990. }
  2991. }
  2992. if (hasRenderDebugMask(LLPipeline::RENDER_DEBUG_SHADOW_FRUSTA))
  2993. {
  2994. LLGLEnable blend(GL_BLEND);
  2995. LLGLDepthTest depth(TRUE, FALSE);
  2996. LLGLDisable cull(GL_CULL_FACE);
  2997. gGL.color4f(1,1,1,1);
  2998. gGL.getTexUnit(0)->unbind(LLTexUnit::TT_TEXTURE);
  2999. F32 a = 0.1f;
  3000. F32 col[] =
  3001. {
  3002. 1,0,0,a,
  3003. 0,1,0,a,
  3004. 0,0,1,a,
  3005. 1,0,1,a,
  3006. 1,1,0,a,
  3007. 0,1,1,a,
  3008. 1,1,1,a,
  3009. 1,0,1,a,
  3010. };
  3011. for (U32 i = 0; i < 8; i++)
  3012. {
  3013. if (i > 3)
  3014. {
  3015. gGL.color4fv(col+(i-4)*4);
  3016. LLVector3* frust = mShadowCamera[i].mAgentFrustum;
  3017. gGL.begin(LLRender::TRIANGLE_STRIP);
  3018. gGL.vertex3fv(frust[0].mV); gGL.vertex3fv(frust[4].mV);
  3019. gGL.vertex3fv(frust[1].mV); gGL.vertex3fv(frust[5].mV);
  3020. gGL.vertex3fv(frust[2].mV); gGL.vertex3fv(frust[6].mV);
  3021. gGL.vertex3fv(frust[3].mV); gGL.vertex3fv(frust[7].mV);
  3022. gGL.vertex3fv(frust[0].mV); gGL.vertex3fv(frust[4].mV);
  3023. gGL.end();
  3024. gGL.begin(LLRender::TRIANGLE_STRIP);
  3025. gGL.vertex3fv(frust[0].mV);
  3026. gGL.vertex3fv(frust[1].mV);
  3027. gGL.vertex3fv(frust[3].mV);
  3028. gGL.vertex3fv(frust[2].mV);
  3029. gGL.end();
  3030. gGL.begin(LLRender::TRIANGLE_STRIP);
  3031. gGL.vertex3fv(frust[4].mV);
  3032. gGL.vertex3fv(frust[5].mV);
  3033. gGL.vertex3fv(frust[7].mV);
  3034. gGL.vertex3fv(frust[6].mV);
  3035. gGL.end();
  3036. }
  3037. if (i < 4)
  3038. {
  3039. gGL.begin(LLRender::LINES);
  3040. F32* c = col+i*4;
  3041. for (U32 j = 0; j < mShadowFrustPoints[i].size(); ++j)
  3042. {
  3043. gGL.color3fv(c);
  3044. for (U32 k = 0; k < mShadowFrustPoints[i].size(); ++k)
  3045. {
  3046. if (j != k)
  3047. {
  3048. gGL.vertex3fv(mShadowFrustPoints[i][j].mV);
  3049. gGL.vertex3fv(mShadowFrustPoints[i][k].mV);
  3050. }
  3051. }
  3052. if (!mShadowFrustOrigin[i].isExactlyZero())
  3053. {
  3054. gGL.vertex3fv(mShadowFrustPoints[i][j].mV);
  3055. gGL.color4f(1,1,1,1);
  3056. gGL.vertex3fv(mShadowFrustOrigin[i].mV);
  3057. }
  3058. }
  3059. gGL.end();
  3060. }
  3061. /*for (LLWorld::region_list_t::const_iterator iter = LLWorld::getInstance()->getRegionList().begin(); 
  3062. iter != LLWorld::getInstance()->getRegionList().end(); ++iter)
  3063. {
  3064. LLViewerRegion* region = *iter;
  3065. for (U32 j = 0; j < LLViewerRegion::NUM_PARTITIONS; j++)
  3066. {
  3067. LLSpatialPartition* part = region->getSpatialPartition(j);
  3068. if (part)
  3069. {
  3070. if (hasRenderType(part->mDrawableType))
  3071. {
  3072. part->renderIntersectingBBoxes(&mShadowCamera[i]);
  3073. }
  3074. }
  3075. }
  3076. }*/
  3077. }
  3078. }
  3079. if (mRenderDebugMask & RENDER_DEBUG_COMPOSITION)
  3080. {
  3081. // Debug composition layers
  3082. F32 x, y;
  3083. gGL.getTexUnit(0)->unbind(LLTexUnit::TT_TEXTURE);
  3084. if (gAgent.getRegion())
  3085. {
  3086. gGL.begin(LLRender::POINTS);
  3087. // Draw the composition layer for the region that I'm in.
  3088. for (x = 0; x <= 260; x++)
  3089. {
  3090. for (y = 0; y <= 260; y++)
  3091. {
  3092. if ((x > 255) || (y > 255))
  3093. {
  3094. gGL.color4f(1.f, 0.f, 0.f, 1.f);
  3095. }
  3096. else
  3097. {
  3098. gGL.color4f(0.f, 0.f, 1.f, 1.f);
  3099. }
  3100. F32 z = gAgent.getRegion()->getCompositionXY((S32)x, (S32)y);
  3101. z *= 5.f;
  3102. z += 50.f;
  3103. gGL.vertex3f(x, y, z);
  3104. }
  3105. }
  3106. gGL.end();
  3107. }
  3108. }
  3109. if (mRenderDebugMask & LLPipeline::RENDER_DEBUG_BUILD_QUEUE)
  3110. {
  3111. U32 count = 0;
  3112. U32 size = mBuildQ2.size();
  3113. LLColor4 col;
  3114. LLGLEnable blend(GL_BLEND);
  3115. LLGLDepthTest depth(GL_TRUE, GL_FALSE);
  3116. gGL.getTexUnit(0)->bind(LLViewerFetchedTexture::sWhiteImagep);
  3117. for (LLSpatialGroup::sg_vector_t::iterator iter = mGroupQ2.begin(); iter != mGroupQ2.end(); ++iter)
  3118. {
  3119. LLSpatialGroup* group = *iter;
  3120. if (group->isDead())
  3121. {
  3122. continue;
  3123. }
  3124. LLSpatialBridge* bridge = group->mSpatialPartition->asBridge();
  3125. if (bridge && (!bridge->mDrawable || bridge->mDrawable->isDead()))
  3126. {
  3127. continue;
  3128. }
  3129. if (bridge)
  3130. {
  3131. gGL.pushMatrix();
  3132. glMultMatrixf((F32*)bridge->mDrawable->getRenderMatrix().mMatrix);
  3133. }
  3134. F32 alpha = (F32) (size-count)/size;
  3135. LLVector2 c(1.f-alpha, alpha);
  3136. c.normVec();
  3137. ++count;
  3138. col.set(c.mV[0], c.mV[1], 0, alpha*0.5f+0.1f);
  3139. group->drawObjectBox(col);
  3140. if (bridge)
  3141. {
  3142. gGL.popMatrix();
  3143. }
  3144. }
  3145. }
  3146. gGL.flush();
  3147. }
  3148. void LLPipeline::renderForSelect(std::set<LLViewerObject*>& objects, BOOL render_transparent, const LLRect& screen_rect)
  3149. {
  3150. assertInitialized();
  3151. gGL.setColorMask(true, false);
  3152. gPipeline.resetDrawOrders();
  3153. LLViewerCamera* camera = LLViewerCamera::getInstance();
  3154. for (std::set<LLViewerObject*>::iterator iter = objects.begin(); iter != objects.end(); ++iter)
  3155. {
  3156. stateSort((*iter)->mDrawable, *camera);
  3157. }
  3158. LLMemType mt(LLMemType::MTYPE_PIPELINE_RENDER_SELECT);
  3159. glMatrixMode(GL_MODELVIEW);
  3160. LLGLSDefault gls_default;
  3161. LLGLSObjectSelect gls_object_select;
  3162. gGL.getTexUnit(0)->unbind(LLTexUnit::TT_TEXTURE);
  3163. LLGLDepthTest gls_depth(GL_TRUE,GL_TRUE);
  3164. disableLights();
  3165. LLVertexBuffer::unbind();
  3166. //for each drawpool
  3167. LLGLState::checkStates();
  3168. LLGLState::checkTextureChannels();
  3169. LLGLState::checkClientArrays();
  3170. U32 last_type = 0;
  3171. // If we don't do this, we crash something on changing graphics settings
  3172. // from Medium -> Low, because we unload all the shaders and the 
  3173. // draw pools aren't aware.  I don't know if this has to be a separate
  3174. // loop before actual rendering. JC
  3175. for (pool_set_t::iterator iter = mPools.begin(); iter != mPools.end(); ++iter)
  3176. {
  3177. LLDrawPool *poolp = *iter;
  3178. if (poolp->isFacePool() && hasRenderType(poolp->getType()))
  3179. {
  3180. poolp->prerender();
  3181. }
  3182. }
  3183. for (pool_set_t::iterator iter = mPools.begin(); iter != mPools.end(); ++iter)
  3184. {
  3185. LLDrawPool *poolp = *iter;
  3186. if (poolp->isFacePool() && hasRenderType(poolp->getType()))
  3187. {
  3188. LLFacePool* face_pool = (LLFacePool*) poolp;
  3189. face_pool->renderForSelect();
  3190. LLVertexBuffer::unbind();
  3191. gGLLastMatrix = NULL;
  3192. glLoadMatrixd(gGLModelView);
  3193. if (poolp->getType() != last_type)
  3194. {
  3195. last_type = poolp->getType();
  3196. LLGLState::checkStates();
  3197. LLGLState::checkTextureChannels();
  3198. LLGLState::checkClientArrays();
  3199. }
  3200. }
  3201. }
  3202. LLGLEnable alpha_test(GL_ALPHA_TEST);
  3203. if (render_transparent)
  3204. {
  3205. gGL.setAlphaRejectSettings(LLRender::CF_GREATER_EQUAL, 0.f);
  3206. }
  3207. else
  3208. {
  3209. gGL.setAlphaRejectSettings(LLRender::CF_GREATER, 0.2f);
  3210. }
  3211. gGL.getTexUnit(0)->setTextureColorBlend(LLTexUnit::TBO_REPLACE, LLTexUnit::TBS_VERT_COLOR);
  3212. gGL.getTexUnit(0)->setTextureAlphaBlend(LLTexUnit::TBO_MULT, LLTexUnit::TBS_TEX_ALPHA, LLTexUnit::TBS_VERT_ALPHA);
  3213. U32 prim_mask = LLVertexBuffer::MAP_VERTEX | 
  3214. LLVertexBuffer::MAP_TEXCOORD0;
  3215. for (std::set<LLViewerObject*>::iterator i = objects.begin(); i != objects.end(); ++i)
  3216. {
  3217. LLViewerObject* vobj = *i;
  3218. LLDrawable* drawable = vobj->mDrawable;
  3219. if (vobj->isDead() || 
  3220. vobj->isHUDAttachment() ||
  3221. (LLSelectMgr::getInstance()->mHideSelectedObjects && vobj->isSelected()) ||
  3222. drawable->isDead() || 
  3223. !hasRenderType(drawable->getRenderType()))
  3224. {
  3225. continue;
  3226. }
  3227. for (S32 j = 0; j < drawable->getNumFaces(); ++j)
  3228. {
  3229. LLFace* facep = drawable->getFace(j);
  3230. if (!facep->getPool())
  3231. {
  3232. facep->renderForSelect(prim_mask);
  3233. }
  3234. }
  3235. }
  3236. // pick HUD objects
  3237. LLVOAvatar* avatarp = gAgent.getAvatarObject();
  3238. if (avatarp && sShowHUDAttachments)
  3239. {
  3240. glh::matrix4f save_proj(glh_get_current_projection());
  3241. glh::matrix4f save_model(glh_get_current_modelview());
  3242. setup_hud_matrices(screen_rect);
  3243. for (LLVOAvatar::attachment_map_t::iterator iter = avatarp->mAttachmentPoints.begin(); 
  3244.  iter != avatarp->mAttachmentPoints.end(); )
  3245. {
  3246. LLVOAvatar::attachment_map_t::iterator curiter = iter++;
  3247. LLViewerJointAttachment* attachment = curiter->second;
  3248. if (attachment->getIsHUDAttachment())
  3249. {
  3250. for (LLViewerJointAttachment::attachedobjs_vec_t::iterator attachment_iter = attachment->mAttachedObjects.begin();
  3251.  attachment_iter != attachment->mAttachedObjects.end();
  3252.  ++attachment_iter)
  3253. {
  3254. if (LLViewerObject* attached_object = (*attachment_iter))
  3255. {
  3256. LLDrawable* drawable = attached_object->mDrawable;
  3257. if (drawable->isDead())
  3258. {
  3259. continue;
  3260. }
  3261. for (S32 j = 0; j < drawable->getNumFaces(); ++j)
  3262. {
  3263. LLFace* facep = drawable->getFace(j);
  3264. if (!facep->getPool())
  3265. {
  3266. facep->renderForSelect(prim_mask);
  3267. }
  3268. }
  3269. //render child faces
  3270. LLViewerObject::const_child_list_t& child_list = attached_object->getChildren();
  3271. for (LLViewerObject::child_list_t::const_iterator iter = child_list.begin();
  3272.  iter != child_list.end(); iter++)
  3273. {
  3274. LLViewerObject* child = *iter;
  3275. LLDrawable* child_drawable = child->mDrawable;
  3276. for (S32 l = 0; l < child_drawable->getNumFaces(); ++l)
  3277. {
  3278. LLFace* facep = child_drawable->getFace(l);
  3279. if (!facep->getPool())
  3280. {
  3281. facep->renderForSelect(prim_mask);
  3282. }
  3283. }
  3284. }
  3285. }
  3286. }
  3287. }
  3288. }
  3289. glMatrixMode(GL_PROJECTION);
  3290. glLoadMatrixf(save_proj.m);
  3291. glh_set_current_projection(save_proj);
  3292. glMatrixMode(GL_MODELVIEW);
  3293. glLoadMatrixf(save_model.m);
  3294. glh_set_current_modelview(save_model);
  3295. }
  3296. gGL.getTexUnit(0)->setTextureBlendType(LLTexUnit::TB_MULT);
  3297. LLVertexBuffer::unbind();
  3298. gGL.setColorMask(true, true);
  3299. }
  3300. void LLPipeline::rebuildPools()
  3301. {
  3302. LLMemType mt(LLMemType::MTYPE_PIPELINE_REBUILD_POOLS);
  3303. assertInitialized();
  3304. S32 max_count = mPools.size();
  3305. pool_set_t::iterator iter1 = mPools.upper_bound(mLastRebuildPool);
  3306. while(max_count > 0 && mPools.size() > 0) // && num_rebuilds < MAX_REBUILDS)
  3307. {
  3308. if (iter1 == mPools.end())
  3309. {
  3310. iter1 = mPools.begin();
  3311. }
  3312. LLDrawPool* poolp = *iter1;
  3313. if (poolp->isDead())
  3314. {
  3315. mPools.erase(iter1++);
  3316. removeFromQuickLookup( poolp );
  3317. if (poolp == mLastRebuildPool)
  3318. {
  3319. mLastRebuildPool = NULL;
  3320. }
  3321. delete poolp;
  3322. }
  3323. else
  3324. {
  3325. mLastRebuildPool = poolp;
  3326. iter1++;
  3327. }
  3328. max_count--;
  3329. }
  3330. if (gAgent.getAvatarObject())
  3331. {
  3332. gAgent.getAvatarObject()->rebuildHUD();
  3333. }
  3334. }
  3335. void LLPipeline::addToQuickLookup( LLDrawPool* new_poolp )
  3336. {
  3337. LLMemType mt(LLMemType::MTYPE_PIPELINE_QUICK_LOOKUP);
  3338. assertInitialized();
  3339. switch( new_poolp->getType() )
  3340. {
  3341. case LLDrawPool::POOL_SIMPLE:
  3342. if (mSimplePool)
  3343. {
  3344. llassert(0);
  3345. llwarns << "Ignoring duplicate simple pool." << llendl;
  3346. }
  3347. else
  3348. {
  3349. mSimplePool = (LLRenderPass*) new_poolp;
  3350. }
  3351. break;
  3352. case LLDrawPool::POOL_GRASS:
  3353. if (mGrassPool)
  3354. {
  3355. llassert(0);
  3356. llwarns << "Ignoring duplicate grass pool." << llendl;
  3357. }
  3358. else
  3359. {
  3360. mGrassPool = (LLRenderPass*) new_poolp;
  3361. }
  3362. break;
  3363. case LLDrawPool::POOL_FULLBRIGHT:
  3364. if (mFullbrightPool)
  3365. {
  3366. llassert(0);
  3367. llwarns << "Ignoring duplicate simple pool." << llendl;
  3368. }
  3369. else
  3370. {
  3371. mFullbrightPool = (LLRenderPass*) new_poolp;
  3372. }
  3373. break;
  3374. case LLDrawPool::POOL_INVISIBLE:
  3375. if (mInvisiblePool)
  3376. {
  3377. llassert(0);
  3378. llwarns << "Ignoring duplicate simple pool." << llendl;
  3379. }
  3380. else
  3381. {
  3382. mInvisiblePool = (LLRenderPass*) new_poolp;
  3383. }
  3384. break;
  3385. case LLDrawPool::POOL_GLOW:
  3386. if (mGlowPool)
  3387. {
  3388. llassert(0);
  3389. llwarns << "Ignoring duplicate glow pool." << llendl;
  3390. }
  3391. else
  3392. {
  3393. mGlowPool = (LLRenderPass*) new_poolp;
  3394. }
  3395. break;
  3396. case LLDrawPool::POOL_TREE:
  3397. mTreePools[ uintptr_t(new_poolp->getTexture()) ] = new_poolp ;
  3398. break;
  3399.  
  3400. case LLDrawPool::POOL_TERRAIN:
  3401. mTerrainPools[ uintptr_t(new_poolp->getTexture()) ] = new_poolp ;
  3402. break;
  3403. case LLDrawPool::POOL_BUMP:
  3404. if (mBumpPool)
  3405. {
  3406. llassert(0);
  3407. llwarns << "Ignoring duplicate bump pool." << llendl;
  3408. }
  3409. else
  3410. {
  3411. mBumpPool = new_poolp;
  3412. }
  3413. break;
  3414. case LLDrawPool::POOL_ALPHA:
  3415. if( mAlphaPool )
  3416. {
  3417. llassert(0);
  3418. llwarns << "LLPipeline::addPool(): Ignoring duplicate Alpha pool" << llendl;
  3419. }
  3420. else
  3421. {
  3422. mAlphaPool = new_poolp;
  3423. }
  3424. break;
  3425. case LLDrawPool::POOL_AVATAR:
  3426. break; // Do nothing
  3427. case LLDrawPool::POOL_SKY:
  3428. if( mSkyPool )
  3429. {
  3430. llassert(0);
  3431. llwarns << "LLPipeline::addPool(): Ignoring duplicate Sky pool" << llendl;
  3432. }
  3433. else
  3434. {
  3435. mSkyPool = new_poolp;
  3436. }
  3437. break;
  3438. case LLDrawPool::POOL_WATER:
  3439. if( mWaterPool )
  3440. {
  3441. llassert(0);
  3442. llwarns << "LLPipeline::addPool(): Ignoring duplicate Water pool" << llendl;
  3443. }
  3444. else
  3445. {
  3446. mWaterPool = new_poolp;
  3447. }
  3448. break;
  3449. case LLDrawPool::POOL_GROUND:
  3450. if( mGroundPool )
  3451. {
  3452. llassert(0);
  3453. llwarns << "LLPipeline::addPool(): Ignoring duplicate Ground Pool" << llendl;
  3454. }
  3455. else
  3456. mGroundPool = new_poolp;
  3457. }
  3458. break;
  3459. case LLDrawPool::POOL_WL_SKY:
  3460. if( mWLSkyPool )
  3461. {
  3462. llassert(0);
  3463. llwarns << "LLPipeline::addPool(): Ignoring duplicate WLSky Pool" << llendl;
  3464. }
  3465. else
  3466. mWLSkyPool = new_poolp;
  3467. }
  3468. break;
  3469. default:
  3470. llassert(0);
  3471. llwarns << "Invalid Pool Type in  LLPipeline::addPool()" << llendl;
  3472. break;
  3473. }
  3474. }
  3475. void LLPipeline::removePool( LLDrawPool* poolp )
  3476. {
  3477. assertInitialized();
  3478. removeFromQuickLookup(poolp);
  3479. mPools.erase(poolp);
  3480. delete poolp;
  3481. }
  3482. void LLPipeline::removeFromQuickLookup( LLDrawPool* poolp )
  3483. {
  3484. assertInitialized();
  3485. LLMemType mt(LLMemType::MTYPE_PIPELINE);
  3486. switch( poolp->getType() )
  3487. {
  3488. case LLDrawPool::POOL_SIMPLE:
  3489. llassert(mSimplePool == poolp);
  3490. mSimplePool = NULL;
  3491. break;
  3492. case LLDrawPool::POOL_GRASS:
  3493. llassert(mGrassPool == poolp);
  3494. mGrassPool = NULL;
  3495. break;
  3496. case LLDrawPool::POOL_FULLBRIGHT:
  3497. llassert(mFullbrightPool == poolp);
  3498. mFullbrightPool = NULL;
  3499. break;
  3500. case LLDrawPool::POOL_INVISIBLE:
  3501. llassert(mInvisiblePool == poolp);
  3502. mInvisiblePool = NULL;
  3503. break;
  3504. case LLDrawPool::POOL_WL_SKY:
  3505. llassert(mWLSkyPool == poolp);
  3506. mWLSkyPool = NULL;
  3507. break;
  3508. case LLDrawPool::POOL_GLOW:
  3509. llassert(mGlowPool == poolp);
  3510. mGlowPool = NULL;
  3511. break;
  3512. case LLDrawPool::POOL_TREE:
  3513. #ifdef _DEBUG
  3514. {
  3515. BOOL found = mTreePools.erase( (uintptr_t)poolp->getTexture() );
  3516. llassert( found );
  3517. }
  3518. #else
  3519. mTreePools.erase( (uintptr_t)poolp->getTexture() );
  3520. #endif
  3521. break;
  3522. case LLDrawPool::POOL_TERRAIN:
  3523. #ifdef _DEBUG
  3524. {
  3525. BOOL found = mTerrainPools.erase( (uintptr_t)poolp->getTexture() );
  3526. llassert( found );
  3527. }
  3528. #else
  3529. mTerrainPools.erase( (uintptr_t)poolp->getTexture() );
  3530. #endif
  3531. break;
  3532. case LLDrawPool::POOL_BUMP:
  3533. llassert( poolp == mBumpPool );
  3534. mBumpPool = NULL;
  3535. break;
  3536. case LLDrawPool::POOL_ALPHA:
  3537. llassert( poolp == mAlphaPool );
  3538. mAlphaPool = NULL;
  3539. break;
  3540. case LLDrawPool::POOL_AVATAR:
  3541. break; // Do nothing
  3542. case LLDrawPool::POOL_SKY:
  3543. llassert( poolp == mSkyPool );
  3544. mSkyPool = NULL;
  3545. break;
  3546. case LLDrawPool::POOL_WATER:
  3547. llassert( poolp == mWaterPool );
  3548. mWaterPool = NULL;
  3549. break;
  3550. case LLDrawPool::POOL_GROUND:
  3551. llassert( poolp == mGroundPool );
  3552. mGroundPool = NULL;
  3553. break;
  3554. default:
  3555. llassert(0);
  3556. llwarns << "Invalid Pool Type in  LLPipeline::removeFromQuickLookup() type=" << poolp->getType() << llendl;
  3557. break;
  3558. }
  3559. }
  3560. void LLPipeline::resetDrawOrders()
  3561. {
  3562. assertInitialized();
  3563. // Iterate through all of the draw pools and rebuild them.
  3564. for (pool_set_t::iterator iter = mPools.begin(); iter != mPools.end(); ++iter)
  3565. {
  3566. LLDrawPool *poolp = *iter;
  3567. poolp->resetDrawOrders();
  3568. }
  3569. }
  3570. //============================================================================
  3571. // Once-per-frame setup of hardware lights,
  3572. // including sun/moon, avatar backlight, and up to 6 local lights
  3573. void LLPipeline::setupAvatarLights(BOOL for_edit)
  3574. {
  3575. assertInitialized();
  3576. if (for_edit)
  3577. {
  3578. LLColor4 diffuse(1.f, 1.f, 1.f, 0.f);
  3579. LLVector4 light_pos_cam(-8.f, 0.25f, 10.f, 0.f);  // w==0 => directional light
  3580. LLMatrix4 camera_mat = LLViewerCamera::getInstance()->getModelview();
  3581. LLMatrix4 camera_rot(camera_mat.getMat3());
  3582. camera_rot.invert();
  3583. LLVector4 light_pos = light_pos_cam * camera_rot;
  3584. light_pos.normalize();
  3585. mHWLightColors[1] = diffuse;
  3586. glLightfv(GL_LIGHT1, GL_DIFFUSE,  diffuse.mV);
  3587. glLightfv(GL_LIGHT1, GL_AMBIENT,  LLColor4::black.mV);
  3588. glLightfv(GL_LIGHT1, GL_SPECULAR, LLColor4::black.mV);
  3589. glLightfv(GL_LIGHT1, GL_POSITION, light_pos.mV); 
  3590. glLightf (GL_LIGHT1, GL_CONSTANT_ATTENUATION,  1.0f);
  3591. glLightf (GL_LIGHT1, GL_LINEAR_ATTENUATION,   0.0f);
  3592. glLightf (GL_LIGHT1, GL_QUADRATIC_ATTENUATION, 0.0f);
  3593. glLightf (GL_LIGHT1, GL_SPOT_EXPONENT,   0.0f);
  3594. glLightf (GL_LIGHT1, GL_SPOT_CUTOFF,   180.0f);
  3595. }
  3596. else if (gAvatarBacklight) // Always true (unless overridden in a devs .ini)
  3597. {
  3598. LLVector3 opposite_pos = -1.f * mSunDir;
  3599. LLVector3 orthog_light_pos = mSunDir % LLVector3::z_axis;
  3600. LLVector4 backlight_pos = LLVector4(lerp(opposite_pos, orthog_light_pos, 0.3f), 0.0f);
  3601. backlight_pos.normalize();
  3602. LLColor4 light_diffuse = mSunDiffuse;
  3603. LLColor4 backlight_diffuse(1.f - light_diffuse.mV[VRED], 1.f - light_diffuse.mV[VGREEN], 1.f - light_diffuse.mV[VBLUE], 1.f);
  3604. F32 max_component = 0.001f;
  3605. for (S32 i = 0; i < 3; i++)
  3606. {
  3607. if (backlight_diffuse.mV[i] > max_component)
  3608. {
  3609. max_component = backlight_diffuse.mV[i];
  3610. }
  3611. }
  3612. F32 backlight_mag;
  3613. if (gSky.getSunDirection().mV[2] >= LLSky::NIGHTTIME_ELEVATION_COS)
  3614. {
  3615. backlight_mag = BACKLIGHT_DAY_MAGNITUDE_OBJECT;
  3616. }
  3617. else
  3618. {
  3619. backlight_mag = BACKLIGHT_NIGHT_MAGNITUDE_OBJECT;
  3620. }
  3621. backlight_diffuse *= backlight_mag / max_component;
  3622. mHWLightColors[1] = backlight_diffuse;
  3623. glLightfv(GL_LIGHT1, GL_POSITION, backlight_pos.mV); // this is just sun/moon direction
  3624. glLightfv(GL_LIGHT1, GL_DIFFUSE,  backlight_diffuse.mV);
  3625. glLightfv(GL_LIGHT1, GL_AMBIENT,  LLColor4::black.mV);
  3626. glLightfv(GL_LIGHT1, GL_SPECULAR, LLColor4::black.mV);
  3627. glLightf (GL_LIGHT1, GL_CONSTANT_ATTENUATION,  1.0f);
  3628. glLightf (GL_LIGHT1, GL_LINEAR_ATTENUATION,    0.0f);
  3629. glLightf (GL_LIGHT1, GL_QUADRATIC_ATTENUATION, 0.0f);
  3630. glLightf (GL_LIGHT1, GL_SPOT_EXPONENT,         0.0f);
  3631. glLightf (GL_LIGHT1, GL_SPOT_CUTOFF,           180.0f);
  3632. }
  3633. else
  3634. {
  3635. mHWLightColors[1] = LLColor4::black;
  3636. glLightfv(GL_LIGHT1, GL_DIFFUSE,  LLColor4::black.mV);
  3637. glLightfv(GL_LIGHT1, GL_AMBIENT,  LLColor4::black.mV);
  3638. glLightfv(GL_LIGHT1, GL_SPECULAR, LLColor4::black.mV);
  3639. }
  3640. }
  3641. static F32 calc_light_dist(LLVOVolume* light, const LLVector3& cam_pos, F32 max_dist)
  3642. {
  3643. F32 inten = light->getLightIntensity();
  3644. if (inten < .001f)
  3645. {
  3646. return max_dist;
  3647. }
  3648. F32 radius = light->getLightRadius();
  3649. BOOL selected = light->isSelected();
  3650. LLVector3 dpos = light->getRenderPosition() - cam_pos;
  3651. F32 dist2 = dpos.lengthSquared();
  3652. if (!selected && dist2 > (max_dist + radius)*(max_dist + radius))
  3653. {
  3654. return max_dist;
  3655. }
  3656. F32 dist = fsqrtf(dist2);
  3657. dist *= 1.f / inten;
  3658. dist -= radius;
  3659. if (selected)
  3660. {
  3661. dist -= 10000.f; // selected lights get highest priority
  3662. }
  3663. if (light->mDrawable.notNull() && light->mDrawable->isState(LLDrawable::ACTIVE))
  3664. {
  3665. // moving lights get a little higher priority (too much causes artifacts)
  3666. dist -= light->getLightRadius()*0.25f;
  3667. }
  3668. return dist;
  3669. }
  3670. void LLPipeline::calcNearbyLights(LLCamera& camera)
  3671. {
  3672. assertInitialized();
  3673. if (LLPipeline::sReflectionRender)
  3674. {
  3675. return;
  3676. }
  3677. if (mLightingDetail >= 1)
  3678. {
  3679. // mNearbyLight (and all light_set_t's) are sorted such that
  3680. // begin() == the closest light and rbegin() == the farthest light
  3681. const S32 MAX_LOCAL_LIGHTS = 6;
  3682. //  LLVector3 cam_pos = gAgent.getCameraPositionAgent();
  3683. LLVector3 cam_pos = LLViewerJoystick::getInstance()->getOverrideCamera() ?
  3684. camera.getOrigin() : 
  3685. gAgent.getPositionAgent();
  3686. F32 max_dist = LIGHT_MAX_RADIUS * 4.f; // ignore enitrely lights > 4 * max light rad
  3687. // UPDATE THE EXISTING NEARBY LIGHTS
  3688. light_set_t cur_nearby_lights;
  3689. for (light_set_t::iterator iter = mNearbyLights.begin();
  3690. iter != mNearbyLights.end(); iter++)
  3691. {
  3692. const Light* light = &(*iter);
  3693. LLDrawable* drawable = light->drawable;
  3694. LLVOVolume* volight = drawable->getVOVolume();
  3695. if (!volight || !drawable->isState(LLDrawable::LIGHT))
  3696. {
  3697. drawable->clearState(LLDrawable::NEARBY_LIGHT);
  3698. continue;
  3699. }
  3700. if (light->fade <= -LIGHT_FADE_TIME)
  3701. {
  3702. drawable->clearState(LLDrawable::NEARBY_LIGHT);
  3703. continue;
  3704. }
  3705. if (!sRenderAttachedLights && volight && volight->isAttachment())
  3706. {
  3707. drawable->clearState(LLDrawable::NEARBY_LIGHT);
  3708. continue;
  3709. }
  3710. F32 dist = calc_light_dist(volight, cam_pos, max_dist);
  3711. cur_nearby_lights.insert(Light(drawable, dist, light->fade));
  3712. }
  3713. mNearbyLights = cur_nearby_lights;
  3714. // FIND NEW LIGHTS THAT ARE IN RANGE
  3715. light_set_t new_nearby_lights;
  3716. for (LLDrawable::drawable_set_t::iterator iter = mLights.begin();
  3717.  iter != mLights.end(); ++iter)
  3718. {
  3719. LLDrawable* drawable = *iter;
  3720. LLVOVolume* light = drawable->getVOVolume();
  3721. if (!light || drawable->isState(LLDrawable::NEARBY_LIGHT))
  3722. {
  3723. continue;
  3724. }
  3725. if (light->isHUDAttachment())
  3726. {
  3727. continue; // no lighting from HUD objects
  3728. }
  3729. F32 dist = calc_light_dist(light, cam_pos, max_dist);
  3730. if (dist >= max_dist)
  3731. {
  3732. continue;
  3733. }
  3734. if (!sRenderAttachedLights && light && light->isAttachment())
  3735. {
  3736. continue;
  3737. }
  3738. new_nearby_lights.insert(Light(drawable, dist, 0.f));
  3739. if (new_nearby_lights.size() > (U32)MAX_LOCAL_LIGHTS)
  3740. {
  3741. new_nearby_lights.erase(--new_nearby_lights.end());
  3742. const Light& last = *new_nearby_lights.rbegin();
  3743. max_dist = last.dist;
  3744. }
  3745. }
  3746. // INSERT ANY NEW LIGHTS
  3747. for (light_set_t::iterator iter = new_nearby_lights.begin();
  3748.  iter != new_nearby_lights.end(); iter++)
  3749. {
  3750. const Light* light = &(*iter);