llvieweroctree.cpp 38 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714
  1. /**
  2. * @file llvieweroctree.cpp
  3. * @brief LLViewerObjectOctree class implementation and supporting functions
  4. *
  5. * $LicenseInfo:firstyear=2003&license=viewergpl$
  6. *
  7. * Copyright (c) 2003-2009, Linden Research, Inc.
  8. *
  9. * Second Life Viewer Source Code
  10. * The source code in this file ("Source Code") is provided by Linden Lab
  11. * to you under the terms of the GNU General Public License, version 2.0
  12. * ("GPL"), unless you have obtained a separate licensing agreement
  13. * ("Other License"), formally executed by you and Linden Lab. Terms of
  14. * the GPL can be found in doc/GPL-license.txt in this distribution, or
  15. * online at http://secondlifegrid.net/programs/open_source/licensing/gplv2
  16. *
  17. * There are special exceptions to the terms and conditions of the GPL as
  18. * it is applied to this Source Code. View the full text of the exception
  19. * in the file doc/FLOSS-exception.txt in this software distribution, or
  20. * online at
  21. * http://secondlifegrid.net/programs/open_source/licensing/flossexception
  22. *
  23. * By copying, modifying or distributing this software, you acknowledge
  24. * that you have read and understood your obligations described above,
  25. * and agree to abide by those obligations.
  26. *
  27. * ALL LINDEN LAB SOURCE CODE IS PROVIDED "AS IS." LINDEN LAB MAKES NO
  28. * WARRANTIES, EXPRESS, IMPLIED OR OTHERWISE, REGARDING ITS ACCURACY,
  29. * COMPLETENESS OR PERFORMANCE.
  30. * $/LicenseInfo$
  31. */
  32. #include "llviewerprecompiledheaders.h"
  33. #include <queue>
  34. #include "llvieweroctree.h"
  35. #include "llfasttimer.h"
  36. #include "llimagedecodethread.h"
  37. #include "llappviewer.h" // For gFrameCount
  38. #include "lldrawpoolwater.h"
  39. #include "llpipeline.h"
  40. #include "lltexturecache.h"
  41. #include "lltexturefetch.h"
  42. #include "llviewercontrol.h"
  43. #include "llviewerregion.h"
  44. #include "llviewershadermgr.h"
  45. //-----------------------------------------------------------------------------
  46. // Static & global variables and definitions
  47. //-----------------------------------------------------------------------------
  48. constexpr F32 SG_OCCLUSION_FUDGE = 0.25f;
  49. #define SG_DISCARD_TOLERANCE 0.01f
  50. // reserve the low numbers for special use.
  51. U32 LLViewerOctreeEntryData::sCurVisible = 10;
  52. bool LLViewerOctreeDebug::sInDebug = false;
  53. //-----------------------------------------------------------------------------
  54. // Some global functions definitions
  55. //-----------------------------------------------------------------------------
  56. typedef enum
  57. {
  58. b000 = 0x00,
  59. b001 = 0x01,
  60. b010 = 0x02,
  61. b011 = 0x03,
  62. b100 = 0x04,
  63. b101 = 0x05,
  64. b110 = 0x06,
  65. b111 = 0x07,
  66. } eLoveTheBits;
  67. // Contact Runitai Linden for a copy of the SL object used to write this table
  68. // basically, you give the table a bitmask of the look-at vector to a node and
  69. // it gives you a triangle fan index array
  70. static U16 sOcclusionIndices[] =
  71. {
  72. //000
  73. b111, b110, b010, b011, b001, b101, b100, b110,
  74. //001
  75. b011, b010, b000, b001, b101, b111, b110, b010,
  76. //010
  77. b101, b100, b110, b111, b011, b001, b000, b100,
  78. //011
  79. b001, b000, b100, b101, b111, b011, b010, b000,
  80. //100
  81. b110, b000, b010, b011, b111, b101, b100, b000,
  82. //101
  83. b010, b100, b000, b001, b011, b111, b110, b100,
  84. //110
  85. b100, b010, b110, b111, b101, b001, b000, b010,
  86. //111
  87. b000, b110, b100, b101, b001, b011, b010, b110,
  88. };
  89. U32 get_box_fan_indices(LLCamera* camera, const LLVector4a& center)
  90. {
  91. LLVector4a origin;
  92. origin.load3(camera->getOrigin().mV);
  93. S32 cypher = center.greaterThan(origin).getGatheredBits() & 0x7;
  94. return cypher * 8;
  95. }
  96. U8* get_box_fan_indices_ptr(LLCamera* camera, const LLVector4a& center)
  97. {
  98. LLVector4a origin;
  99. origin.load3(camera->getOrigin().mV);
  100. S32 cypher = center.greaterThan(origin).getGatheredBits() & 0x7;
  101. return (U8*)(sOcclusionIndices + cypher * 8);
  102. }
  103. bool ll_setup_cube_vb(LLVertexBuffer* vb)
  104. {
  105. if (!vb->allocateBuffer(8, 64))
  106. {
  107. return false;
  108. }
  109. LLStrider<LLVector3> pos;
  110. LLStrider<U16> idx;
  111. if (!vb->getVertexStrider(pos) || !vb->getIndexStrider(idx))
  112. {
  113. return false;
  114. }
  115. pos[0] = LLVector3(-1.f, -1.f, -1.f);
  116. pos[1] = LLVector3(-1.f, -1.f, 1.f);
  117. pos[2] = LLVector3(-1.f, 1.f, -1.f);
  118. pos[3] = LLVector3(-1.f, 1.f, 1.f);
  119. pos[4] = LLVector3(1.f, -1.f, -1.f);
  120. pos[5] = LLVector3(1.f, -1.f, 1.f);
  121. pos[6] = LLVector3(1.f, 1.f, -1.f);
  122. pos[7] = LLVector3(1.f, 1.f, 1.f);
  123. for (U32 i = 0; i < 64; ++i)
  124. {
  125. idx[i] = sOcclusionIndices[i];
  126. }
  127. vb->unmapBuffer();
  128. return true;
  129. }
  130. S32 AABBSphereIntersect(const LLVector3& min, const LLVector3& max,
  131. const LLVector3& origin, const F32& rad)
  132. {
  133. return AABBSphereIntersectR2(min, max, origin, rad * rad);
  134. }
  135. S32 AABBSphereIntersectR2(const LLVector3& min, const LLVector3& max,
  136. const LLVector3& origin, const F32& r)
  137. {
  138. F32 d = 0.f;
  139. F32 t;
  140. if ((min - origin).lengthSquared() < r &&
  141. (max - origin).lengthSquared() < r)
  142. {
  143. return 2;
  144. }
  145. for (U32 i = 0; i < 3; ++i)
  146. {
  147. if (origin.mV[i] < min.mV[i])
  148. {
  149. t = min.mV[i] - origin.mV[i];
  150. d += t * t;
  151. }
  152. else if (origin.mV[i] > max.mV[i])
  153. {
  154. t = origin.mV[i] - max.mV[i];
  155. d += t * t;
  156. }
  157. if (d > r)
  158. {
  159. return 0;
  160. }
  161. }
  162. return 1;
  163. }
  164. S32 AABBSphereIntersect(const LLVector4a& min, const LLVector4a& max,
  165. const LLVector3& origin, const F32& rad)
  166. {
  167. return AABBSphereIntersectR2(min, max, origin, rad * rad);
  168. }
  169. S32 AABBSphereIntersectR2(const LLVector4a& min, const LLVector4a& max,
  170. const LLVector3& origin, const F32& r)
  171. {
  172. F32 d = 0.f;
  173. F32 t;
  174. LLVector4a origina;
  175. origina.load3(origin.mV);
  176. LLVector4a v;
  177. v.setSub(min, origina);
  178. if (v.dot3(v) < r)
  179. {
  180. v.setSub(max, origina);
  181. if (v.dot3(v) < r)
  182. {
  183. return 2;
  184. }
  185. }
  186. for (U32 i = 0; i < 3; ++i)
  187. {
  188. if (origin.mV[i] < min[i])
  189. {
  190. t = min[i] - origin.mV[i];
  191. d += t * t;
  192. }
  193. else if (origin.mV[i] > max[i])
  194. {
  195. t = origin.mV[i] - max[i];
  196. d += t * t;
  197. }
  198. if (d > r)
  199. {
  200. return 0;
  201. }
  202. }
  203. return 1;
  204. }
  205. //-----------------------------------------------------------------------------
  206. // LLViewerOctreeEntry class
  207. //-----------------------------------------------------------------------------
  208. LLViewerOctreeEntry::LLViewerOctreeEntry()
  209. : mGroup(NULL),
  210. mBinRadius(0.f),
  211. mBinIndex(-1),
  212. mVisible(0)
  213. {
  214. mPositionGroup.clear();
  215. mExtents[0].clear();
  216. mExtents[1].clear();
  217. for (S32 i = 0; i < NUM_DATA_TYPE; ++i)
  218. {
  219. mData[i] = NULL;
  220. }
  221. }
  222. LLViewerOctreeEntry::~LLViewerOctreeEntry()
  223. {
  224. llassert(!mGroup);
  225. }
  226. void LLViewerOctreeEntry::addData(LLViewerOctreeEntryData* data)
  227. {
  228. #if 0
  229. llassert(mData[data->getDataType()] == NULL);
  230. #endif
  231. llassert(data != NULL);
  232. mData[data->getDataType()] = data;
  233. }
  234. void LLViewerOctreeEntry::removeData(LLViewerOctreeEntryData* data)
  235. {
  236. #if 0
  237. // Cannot remove LLVOCache entry
  238. llassert(data->getDataType() != LLVOCACHEENTRY);
  239. #endif
  240. if (!mData[data->getDataType()] || mData[data->getDataType()] != data)
  241. {
  242. return;
  243. }
  244. mData[data->getDataType()] = NULL;
  245. if (mGroup != NULL && !mData[LLDRAWABLE])
  246. {
  247. LLViewerOctreeGroup* group = mGroup;
  248. mGroup = NULL;
  249. group->removeFromGroup(data);
  250. llassert(mBinIndex == -1);
  251. }
  252. }
  253. void LLViewerOctreeEntry::setGroup(LLViewerOctreeGroup* group)
  254. {
  255. if (mGroup == group)
  256. {
  257. return;
  258. }
  259. if (mGroup)
  260. {
  261. LLViewerOctreeGroup* group = mGroup;
  262. mGroup = NULL;
  263. group->removeFromGroup(this);
  264. llassert(mBinIndex == -1);
  265. }
  266. mGroup = group;
  267. }
  268. //-----------------------------------------------------------------------------
  269. // LLViewerOctreeEntryData class
  270. //-----------------------------------------------------------------------------
  271. LLViewerOctreeEntryData::~LLViewerOctreeEntryData()
  272. {
  273. if (mEntry)
  274. {
  275. mEntry->removeData(this);
  276. mEntry = NULL;
  277. }
  278. }
  279. LLViewerOctreeEntryData::LLViewerOctreeEntryData(LLViewerOctreeEntry::eEntryDataType_t data_type)
  280. : mDataType(data_type),
  281. mEntry(NULL)
  282. {
  283. }
  284. //virtual
  285. void LLViewerOctreeEntryData::setOctreeEntry(LLViewerOctreeEntry* entry)
  286. {
  287. if (mEntry.notNull())
  288. {
  289. llwarns << "This should not be called when mEntry is not NULL !"
  290. << llendl;
  291. llassert(false);
  292. return;
  293. }
  294. if (!entry)
  295. {
  296. mEntry = new LLViewerOctreeEntry();
  297. }
  298. else
  299. {
  300. mEntry = entry;
  301. }
  302. mEntry->addData(this);
  303. }
  304. void LLViewerOctreeEntryData::removeOctreeEntry()
  305. {
  306. if (mEntry)
  307. {
  308. mEntry->removeData(this);
  309. mEntry = NULL;
  310. }
  311. }
  312. void LLViewerOctreeEntryData::setSpatialExtents(const LLVector3& min,
  313. const LLVector3& max)
  314. {
  315. mEntry->mExtents[0].load3(min.mV);
  316. mEntry->mExtents[1].load3(max.mV);
  317. }
  318. void LLViewerOctreeEntryData::setSpatialExtents(const LLVector4a& min,
  319. const LLVector4a& max)
  320. {
  321. mEntry->mExtents[0] = min;
  322. mEntry->mExtents[1] = max;
  323. }
  324. void LLViewerOctreeEntryData::setPositionGroup(const LLVector4a& pos)
  325. {
  326. mEntry->mPositionGroup = pos;
  327. }
  328. const LLVector4a* LLViewerOctreeEntryData::getSpatialExtents() const
  329. {
  330. return mEntry->getSpatialExtents();
  331. }
  332. //virtual
  333. void LLViewerOctreeEntryData::setGroup(LLViewerOctreeGroup* group)
  334. {
  335. mEntry->setGroup(group);
  336. }
  337. void LLViewerOctreeEntryData::shift(const LLVector4a& shift_vector)
  338. {
  339. mEntry->mExtents[0].add(shift_vector);
  340. mEntry->mExtents[1].add(shift_vector);
  341. mEntry->mPositionGroup.add(shift_vector);
  342. }
  343. LLViewerOctreeGroup* LLViewerOctreeEntryData::getGroup()const
  344. {
  345. return mEntry.notNull() ? mEntry->mGroup : NULL;
  346. }
  347. const LLVector4a& LLViewerOctreeEntryData::getPositionGroup() const
  348. {
  349. return mEntry->getPositionGroup();
  350. }
  351. //virtual
  352. bool LLViewerOctreeEntryData::isVisible() const
  353. {
  354. if (mEntry)
  355. {
  356. return mEntry->mVisible == sCurVisible;
  357. }
  358. return false;
  359. }
  360. //virtual
  361. bool LLViewerOctreeEntryData::isRecentlyVisible() const
  362. {
  363. if (!mEntry)
  364. {
  365. return false;
  366. }
  367. if (isVisible())
  368. {
  369. return true;
  370. }
  371. if (getGroup() && getGroup()->isRecentlyVisible())
  372. {
  373. setVisible();
  374. return true;
  375. }
  376. return false;
  377. }
  378. void LLViewerOctreeEntryData::setVisible() const
  379. {
  380. if (mEntry)
  381. {
  382. mEntry->mVisible = sCurVisible;
  383. }
  384. }
  385. void LLViewerOctreeEntryData::resetVisible() const
  386. {
  387. if (mEntry)
  388. {
  389. mEntry->mVisible = 0;
  390. }
  391. }
  392. //-----------------------------------------------------------------------------
  393. // LLViewerOctreeGroup class
  394. //-----------------------------------------------------------------------------
  395. LLViewerOctreeGroup::LLViewerOctreeGroup(OctreeNode* node)
  396. : mOctreeNode(node),
  397. mAnyVisible(0),
  398. mState(CLEAN)
  399. {
  400. LLVector4a tmp;
  401. tmp.splat(0.f);
  402. mExtents[0] = mExtents[1] = mObjectBounds[0] = mObjectBounds[1] =
  403. mObjectExtents[0] = mObjectExtents[1] = tmp;
  404. mBounds[0] = node->getCenter();
  405. mBounds[1] = node->getSize();
  406. mOctreeNode->addListener(this);
  407. }
  408. bool LLViewerOctreeGroup::hasElement(LLViewerOctreeEntryData* data)
  409. {
  410. if (!data->getEntry())
  411. {
  412. return false;
  413. }
  414. return std::find(getDataBegin(), getDataEnd(),
  415. data->getEntry()) != getDataEnd();
  416. }
  417. bool LLViewerOctreeGroup::removeFromGroup(LLViewerOctreeEntryData* data)
  418. {
  419. return removeFromGroup(data->getEntry());
  420. }
  421. bool LLViewerOctreeGroup::removeFromGroup(LLViewerOctreeEntry* entry)
  422. {
  423. llassert(entry && !entry->getGroup());
  424. unbound();
  425. setState(OBJECT_DIRTY);
  426. if (isDead())
  427. {
  428. // Group is about to be destroyed: don't double delete the entry.
  429. entry->setBinIndex(-1);
  430. return true;
  431. }
  432. if (mOctreeNode)
  433. {
  434. // This could cause *this* pointer to be destroyed, so no more function
  435. // calls after this.
  436. if (!mOctreeNode->remove(entry))
  437. {
  438. llwarns_sparse << "Could not remove LLVOCacheEntry from LLVOCacheOctreeGroup"
  439. << llendl;
  440. llassert(false);
  441. return false;
  442. }
  443. }
  444. return true;
  445. }
  446. //virtual
  447. void LLViewerOctreeGroup::unbound()
  448. {
  449. if (isDirty())
  450. {
  451. return;
  452. }
  453. setState(DIRTY);
  454. // All the parent nodes need to rebound this child
  455. if (mOctreeNode)
  456. {
  457. OctreeNode* parent = (OctreeNode*)mOctreeNode->getParent();
  458. while (parent)
  459. {
  460. LLViewerOctreeGroup* group =
  461. (LLViewerOctreeGroup*)parent->getListener(0);
  462. if (!group || group->isDirty())
  463. {
  464. return;
  465. }
  466. group->setState(DIRTY);
  467. parent = (OctreeNode*)parent->getParent();
  468. }
  469. }
  470. }
  471. //virtual
  472. void LLViewerOctreeGroup::rebound()
  473. {
  474. if (!isDirty())
  475. {
  476. return;
  477. }
  478. if (mOctreeNode->getChildCount() == 1 &&
  479. mOctreeNode->getElementCount() == 0)
  480. {
  481. LLViewerOctreeGroup* group =
  482. (LLViewerOctreeGroup*)mOctreeNode->getChild(0)->getListener(0);
  483. if (!group)
  484. {
  485. llwarns_sparse << "NULL group found ! Cannot rebound." << llendl;
  486. llassert(false);
  487. return;
  488. }
  489. group->rebound();
  490. // Copy single child's bounding box
  491. mBounds[0] = group->mBounds[0];
  492. mBounds[1] = group->mBounds[1];
  493. mExtents[0] = group->mExtents[0];
  494. mExtents[1] = group->mExtents[1];
  495. group->setState(SKIP_FRUSTUM_CHECK);
  496. }
  497. else if (!mOctreeNode->getChildCount())
  498. {
  499. // Copy object bounding box if this is a leaf
  500. boundObjects(true, mExtents[0], mExtents[1]);
  501. mBounds[0] = mObjectBounds[0];
  502. mBounds[1] = mObjectBounds[1];
  503. }
  504. else
  505. {
  506. LLVector4a& new_min = mExtents[0];
  507. LLVector4a& new_max = mExtents[1];
  508. LLViewerOctreeGroup* group =
  509. (LLViewerOctreeGroup*)mOctreeNode->getChild(0)->getListener(0);
  510. if (!group)
  511. {
  512. llwarns_sparse << "NULL group found ! Cannot rebound." << llendl;
  513. llassert(false);
  514. return;
  515. }
  516. group->clearState(SKIP_FRUSTUM_CHECK);
  517. group->rebound();
  518. // Initialize to first child
  519. new_min = group->mExtents[0];
  520. new_max = group->mExtents[1];
  521. // First, rebound children
  522. for (U32 i = 1; i < mOctreeNode->getChildCount(); ++i)
  523. {
  524. group =
  525. (LLViewerOctreeGroup*)mOctreeNode->getChild(i)->getListener(0);
  526. if (!group) continue;
  527. group->clearState(SKIP_FRUSTUM_CHECK);
  528. group->rebound();
  529. const LLVector4a& max = group->mExtents[1];
  530. const LLVector4a& min = group->mExtents[0];
  531. new_max.setMax(new_max, max);
  532. new_min.setMin(new_min, min);
  533. }
  534. boundObjects(false, new_min, new_max);
  535. mBounds[0].setAdd(new_min, new_max);
  536. mBounds[0].mul(0.5f);
  537. mBounds[1].setSub(new_max, new_min);
  538. mBounds[1].mul(0.5f);
  539. }
  540. clearState(DIRTY);
  541. return;
  542. }
  543. //virtual
  544. void LLViewerOctreeGroup::handleInsertion(const TreeNode* node,
  545. LLViewerOctreeEntry* obj)
  546. {
  547. obj->setGroup(this);
  548. unbound();
  549. setState(OBJECT_DIRTY);
  550. }
  551. //virtual
  552. void LLViewerOctreeGroup::handleRemoval(const TreeNode* node,
  553. LLViewerOctreeEntry* obj)
  554. {
  555. unbound();
  556. setState(OBJECT_DIRTY);
  557. // This could cause *this* pointer to be destroyed. So no more function
  558. // calls after this.
  559. obj->setGroup(NULL);
  560. }
  561. //virtual
  562. void LLViewerOctreeGroup::handleDestruction(const TreeNode* node)
  563. {
  564. if (isDead())
  565. {
  566. return;
  567. }
  568. setState(DEAD);
  569. for (OctreeNode::element_iter i = mOctreeNode->getDataBegin(),
  570. end = mOctreeNode->getDataEnd();
  571. i != end; ++i)
  572. {
  573. LLViewerOctreeEntry* obj = *i;
  574. if (obj && obj->getGroup() == this)
  575. {
  576. obj->nullGroup();
  577. }
  578. }
  579. mOctreeNode = NULL;
  580. }
  581. //virtual
  582. void LLViewerOctreeGroup::handleStateChange(const TreeNode* node)
  583. {
  584. // Drop bounding box upon state change
  585. if (mOctreeNode != node)
  586. {
  587. mOctreeNode = (OctreeNode*)node;
  588. }
  589. unbound();
  590. }
  591. //virtual
  592. void LLViewerOctreeGroup::handleChildAddition(const OctreeNode* parent,
  593. OctreeNode* child)
  594. {
  595. if (child->getListenerCount() == 0)
  596. {
  597. new LLViewerOctreeGroup(child);
  598. }
  599. else
  600. {
  601. llwarns_sparse << "LLViewerOctreeGroup redundancy detected." << llendl;
  602. llassert(false);
  603. }
  604. unbound();
  605. LLViewerOctreeGroup* group = (LLViewerOctreeGroup*)child->getListener(0);
  606. if (group)
  607. {
  608. group->unbound();
  609. }
  610. }
  611. //virtual
  612. void LLViewerOctreeGroup::handleChildRemoval(const OctreeNode* parent,
  613. const OctreeNode* child)
  614. {
  615. unbound();
  616. }
  617. LLViewerOctreeGroup* LLViewerOctreeGroup::getParent()
  618. {
  619. if (isDead() || !mOctreeNode)
  620. {
  621. return NULL;
  622. }
  623. OctreeNode* parent = mOctreeNode->getOctParent();
  624. if (parent)
  625. {
  626. return (LLViewerOctreeGroup*)parent->getListener(0);
  627. }
  628. return NULL;
  629. }
  630. //virtual
  631. bool LLViewerOctreeGroup::boundObjects(bool empty, LLVector4a& min_out,
  632. LLVector4a& max_out)
  633. {
  634. const OctreeNode* node = mOctreeNode;
  635. if (node->isEmpty())
  636. {
  637. // Do not do anything if there is no object
  638. if (empty && mOctreeNode->getParent())
  639. {
  640. // Only root is allowed to be empty
  641. llwarns_sparse << "Empty leaf found in octree." << llendl;
  642. llassert(false);
  643. }
  644. return false;
  645. }
  646. LLVector4a& new_min = mObjectExtents[0];
  647. LLVector4a& new_max = mObjectExtents[1];
  648. if (hasState(OBJECT_DIRTY))
  649. {
  650. // Calculate new bounding box
  651. clearState(OBJECT_DIRTY);
  652. // Initialize bounding box to first element
  653. OctreeNode::const_element_iter i = node->getDataBegin();
  654. LLViewerOctreeEntry* entry = *i;
  655. const LLVector4a* minMax = entry->getSpatialExtents();
  656. new_min = minMax[0];
  657. new_max = minMax[1];
  658. for (++i; i != node->getDataEnd(); ++i)
  659. {
  660. entry = *i;
  661. minMax = entry->getSpatialExtents();
  662. update_min_max(new_min, new_max, minMax[0]);
  663. update_min_max(new_min, new_max, minMax[1]);
  664. }
  665. mObjectBounds[0].setAdd(new_min, new_max);
  666. mObjectBounds[0].mul(0.5f);
  667. mObjectBounds[1].setSub(new_max, new_min);
  668. mObjectBounds[1].mul(0.5f);
  669. }
  670. if (empty)
  671. {
  672. min_out = new_min;
  673. max_out = new_max;
  674. }
  675. else
  676. {
  677. min_out.setMin(min_out, new_min);
  678. max_out.setMax(max_out, new_max);
  679. }
  680. return true;
  681. }
  682. //virtual
  683. bool LLViewerOctreeGroup::isVisible() const
  684. {
  685. return mVisible[LLViewerCamera::sCurCameraID] >=
  686. LLViewerOctreeEntryData::getCurrentFrame();
  687. }
  688. void LLViewerOctreeGroup::setVisible()
  689. {
  690. mVisible[LLViewerCamera::sCurCameraID] =
  691. LLViewerOctreeEntryData::getCurrentFrame();
  692. if (LLViewerCamera::sCurCameraID < LLViewerCamera::CAMERA_WATER0)
  693. {
  694. mAnyVisible = LLViewerOctreeEntryData::getCurrentFrame();
  695. }
  696. }
  697. //-----------------------------------------------------------------------------
  698. // Occulsion culling functions and classes
  699. //-----------------------------------------------------------------------------
  700. //static
  701. U32 LLOcclusionCullingGroup::sOcclusionTimeouts = 0;
  702. #define QUERY_POOL_SIZE 1024
  703. static std::queue<GLuint> sFreeQueries;
  704. U32 LLOcclusionCullingGroup::getNewOcclusionQueryObjectName()
  705. {
  706. if (sFreeQueries.empty())
  707. {
  708. GLuint queries[QUERY_POOL_SIZE];
  709. glGenQueries(1024, queries);
  710. for (S32 i = 0; i < 1024; ++i)
  711. {
  712. sFreeQueries.push(queries[i]);
  713. }
  714. }
  715. // Pull from pool
  716. GLuint ret = sFreeQueries.front();
  717. sFreeQueries.pop();
  718. return ret;
  719. }
  720. void LLOcclusionCullingGroup::releaseOcclusionQueryObjectName(GLuint name)
  721. {
  722. if (name)
  723. {
  724. sFreeQueries.push(name);
  725. }
  726. }
  727. //=====================================
  728. // Occlusion State Set/Clear
  729. //=====================================
  730. class LLSpatialSetOcclusionState : public OctreeTraveler
  731. {
  732. public:
  733. LLSpatialSetOcclusionState(U32 state) : mState(state) {}
  734. void visit(const OctreeNode* branch) override
  735. {
  736. LLOcclusionCullingGroup* group =
  737. (LLOcclusionCullingGroup*)branch->getListener(0);
  738. if (group)
  739. {
  740. group->setOcclusionState(mState);
  741. }
  742. }
  743. public:
  744. U32 mState;
  745. };
  746. class LLSpatialSetOcclusionStateDiff final : public LLSpatialSetOcclusionState
  747. {
  748. public:
  749. LLSpatialSetOcclusionStateDiff(U32 state)
  750. : LLSpatialSetOcclusionState(state)
  751. {
  752. }
  753. void traverse(const OctreeNode* n) override
  754. {
  755. LLOcclusionCullingGroup* group =
  756. (LLOcclusionCullingGroup*)n->getListener(0);
  757. if (group && !group->isOcclusionState(mState))
  758. {
  759. OctreeTraveler::traverse(n);
  760. }
  761. }
  762. };
  763. LLOcclusionCullingGroup::LLOcclusionCullingGroup(OctreeNode* node,
  764. LLViewerOctreePartition* part)
  765. : LLViewerOctreeGroup(node),
  766. mSpatialPartition(part)
  767. {
  768. part->mLODSeed = (part->mLODSeed + 1) % part->mLODPeriod;
  769. mLODHash = part->mLODSeed;
  770. LLOcclusionCullingGroup* parent = NULL;
  771. OctreeNode* oct_parent = node->getOctParent();
  772. if (oct_parent)
  773. {
  774. parent = (LLOcclusionCullingGroup*)oct_parent->getListener(0);
  775. }
  776. for (U32 i = 0; i < LLViewerCamera::NUM_CAMERAS; ++i)
  777. {
  778. mOcclusionQuery[i] = 0;
  779. mOcclusionChecks[i] = 0;
  780. mOcclusionIssued[i] = 0;
  781. mOcclusionState[i] = parent ? SG_STATE_INHERIT_MASK &
  782. parent->mOcclusionState[i]
  783. : 0;
  784. mVisible[i] = 0;
  785. }
  786. }
  787. LLOcclusionCullingGroup::~LLOcclusionCullingGroup()
  788. {
  789. releaseOcclusionQueryObjectNames();
  790. }
  791. bool LLOcclusionCullingGroup::needsUpdate()
  792. {
  793. return mSpatialPartition &&
  794. (S32)(LLViewerOctreeEntryData::getCurrentFrame() %
  795. mSpatialPartition->mLODPeriod) == mLODHash;
  796. }
  797. bool LLOcclusionCullingGroup::isRecentlyVisible() const
  798. {
  799. constexpr S32 MIN_VIS_FRAME_RANGE = 2;
  800. return LLViewerOctreeEntryData::getCurrentFrame() -
  801. mVisible[LLViewerCamera::sCurCameraID] < MIN_VIS_FRAME_RANGE;
  802. }
  803. bool LLOcclusionCullingGroup::isAnyRecentlyVisible() const
  804. {
  805. constexpr S32 MIN_VIS_FRAME_RANGE = 2;
  806. return LLViewerOctreeEntryData::getCurrentFrame() -
  807. mAnyVisible < MIN_VIS_FRAME_RANGE;
  808. }
  809. //virtual
  810. void LLOcclusionCullingGroup::handleChildAddition(const OctreeNode* parent,
  811. OctreeNode* child)
  812. {
  813. if (child->getListenerCount() == 0)
  814. {
  815. new LLOcclusionCullingGroup(child, mSpatialPartition);
  816. }
  817. else
  818. {
  819. llwarns_sparse << "LLOcclusionCullingGroup redundancy detected."
  820. << llendl;
  821. llassert(false);
  822. }
  823. unbound();
  824. LLViewerOctreeGroup* group = (LLViewerOctreeGroup*)child->getListener(0);
  825. if (group)
  826. {
  827. group->unbound();
  828. }
  829. }
  830. void LLOcclusionCullingGroup::releaseOcclusionQueryObjectNames()
  831. {
  832. for (U32 i = 0; i < LLViewerCamera::NUM_CAMERAS; ++i)
  833. {
  834. if (mOcclusionQuery[i])
  835. {
  836. releaseOcclusionQueryObjectName(mOcclusionQuery[i]);
  837. mOcclusionQuery[i] = 0;
  838. }
  839. }
  840. }
  841. void LLOcclusionCullingGroup::setOcclusionState(U32 state, S32 mode)
  842. {
  843. switch (mode)
  844. {
  845. case STATE_MODE_SINGLE:
  846. {
  847. mOcclusionState[LLViewerCamera::sCurCameraID] |= state;
  848. GLuint query = mOcclusionQuery[LLViewerCamera::sCurCameraID];
  849. if (query && (state & DISCARD_QUERY))
  850. {
  851. releaseOcclusionQueryObjectName(query);
  852. mOcclusionQuery[LLViewerCamera::sCurCameraID] = 0;
  853. }
  854. break;
  855. }
  856. case STATE_MODE_BRANCH:
  857. {
  858. if (mOctreeNode)
  859. {
  860. LLSpatialSetOcclusionState setter(state);
  861. setter.traverse(mOctreeNode);
  862. }
  863. break;
  864. }
  865. case STATE_MODE_DIFF:
  866. {
  867. if (mOctreeNode)
  868. {
  869. LLSpatialSetOcclusionStateDiff setter(state);
  870. setter.traverse(mOctreeNode);
  871. }
  872. break;
  873. }
  874. default: // STATE_MODE_ALL_CAMERAS
  875. {
  876. for (U32 i = 0; i < LLViewerCamera::NUM_CAMERAS; ++i)
  877. {
  878. mOcclusionState[i] |= state;
  879. if ((state & DISCARD_QUERY) != 0 && mOcclusionQuery[i])
  880. {
  881. releaseOcclusionQueryObjectName(mOcclusionQuery[i]);
  882. mOcclusionQuery[i] = 0;
  883. }
  884. }
  885. }
  886. }
  887. }
  888. class LLSpatialClearOcclusionState : public OctreeTraveler
  889. {
  890. public:
  891. LLSpatialClearOcclusionState(U32 state)
  892. : mState(state)
  893. {
  894. }
  895. void visit(const OctreeNode* branch) override
  896. {
  897. LLOcclusionCullingGroup* group =
  898. (LLOcclusionCullingGroup*)branch->getListener(0);
  899. if (group)
  900. {
  901. group->clearOcclusionState(mState);
  902. }
  903. }
  904. public:
  905. U32 mState;
  906. };
  907. class LLSpatialClearOcclusionStateDiff final
  908. : public LLSpatialClearOcclusionState
  909. {
  910. public:
  911. LLSpatialClearOcclusionStateDiff(U32 state)
  912. : LLSpatialClearOcclusionState(state)
  913. {
  914. }
  915. void traverse(const OctreeNode* n) override
  916. {
  917. LLOcclusionCullingGroup* group =
  918. (LLOcclusionCullingGroup*)n->getListener(0);
  919. if (group && group->isOcclusionState(mState))
  920. {
  921. OctreeTraveler::traverse(n);
  922. }
  923. }
  924. };
  925. void LLOcclusionCullingGroup::clearOcclusionState(U32 state, S32 mode)
  926. {
  927. switch (mode)
  928. {
  929. case STATE_MODE_SINGLE:
  930. {
  931. mOcclusionState[LLViewerCamera::sCurCameraID] &= ~state;
  932. break;
  933. }
  934. case STATE_MODE_BRANCH:
  935. {
  936. if (mOctreeNode)
  937. {
  938. LLSpatialClearOcclusionState clearer(state);
  939. clearer.traverse(mOctreeNode);
  940. }
  941. break;
  942. }
  943. case STATE_MODE_DIFF:
  944. {
  945. if (mOctreeNode)
  946. {
  947. LLSpatialClearOcclusionStateDiff clearer(state);
  948. clearer.traverse(mOctreeNode);
  949. }
  950. break;
  951. }
  952. default: // STATE_MODE_ALL_CAMERAS
  953. {
  954. for (U32 i = 0; i < LLViewerCamera::NUM_CAMERAS; ++i)
  955. {
  956. mOcclusionState[i] &= ~state;
  957. }
  958. }
  959. }
  960. }
  961. bool LLOcclusionCullingGroup::earlyFail(LLCamera* camera,
  962. const LLVector4a* bounds)
  963. {
  964. if (camera->getOrigin().isExactlyZero())
  965. {
  966. return false;
  967. }
  968. constexpr F32 vel = SG_OCCLUSION_FUDGE * 2.f;
  969. LLVector4a fudge;
  970. fudge.splat(vel);
  971. const LLVector4a& c = bounds[0];
  972. LLVector4a r;
  973. r.setAdd(bounds[1], fudge);
  974. #if 0
  975. if (r.lengthSquared() > 1024.0 * 1024.0)
  976. {
  977. return true;
  978. }
  979. #endif
  980. LLVector4a e;
  981. e.load3(camera->getOrigin().mV);
  982. LLVector4a min;
  983. min.setSub(c, r);
  984. LLVector4a max;
  985. max.setAdd(c, r);
  986. S32 lt = e.lessThan(min).getGatheredBits() & 0x7;
  987. if (lt)
  988. {
  989. return false;
  990. }
  991. S32 gt = e.greaterThan(max).getGatheredBits() & 0x7;
  992. if (gt)
  993. {
  994. return false;
  995. }
  996. return true;
  997. }
  998. U32 LLOcclusionCullingGroup::getLastOcclusionIssuedTime()
  999. {
  1000. return mOcclusionIssued[LLViewerCamera::sCurCameraID];
  1001. }
  1002. void LLOcclusionCullingGroup::checkOcclusion()
  1003. {
  1004. if (!mSpatialPartition || LLPipeline::sUseOcclusion <= 1)
  1005. {
  1006. return;
  1007. }
  1008. LL_FAST_TIMER(FTM_OCCLUSION_READBACK);
  1009. LLOcclusionCullingGroup* parent = (LLOcclusionCullingGroup*)getParent();
  1010. if (parent && parent->isOcclusionState(OCCLUDED))
  1011. {
  1012. // If the parent has been marked as occluded, the child is implicitly
  1013. // occluded
  1014. clearOcclusionState(QUERY_PENDING | DISCARD_QUERY);
  1015. return;
  1016. }
  1017. GLuint query = mOcclusionQuery[LLViewerCamera::sCurCameraID];
  1018. if (query && isOcclusionState(QUERY_PENDING))
  1019. {
  1020. if (isOcclusionState(DISCARD_QUERY))
  1021. {
  1022. // Delete the query to avoid holding onto hundreds of pending
  1023. // queries
  1024. releaseOcclusionQueryObjectName(query);
  1025. mOcclusionQuery[LLViewerCamera::sCurCameraID] = 0;
  1026. // Mark as not occluded
  1027. clearOcclusionState(OCCLUDED, STATE_MODE_DIFF);
  1028. clearOcclusionState(QUERY_PENDING | DISCARD_QUERY);
  1029. return;
  1030. }
  1031. // Otherwise a query is pending; read it back
  1032. GLuint available = 0;
  1033. glGetQueryObjectuiv(query, GL_QUERY_RESULT_AVAILABLE, &available);
  1034. // If the result is not available, wait until next frame, but count the
  1035. // number of frames we wait and timeout when above the configured
  1036. // limit...
  1037. static LLCachedControl<U32> max_count(gSavedSettings,
  1038. "RenderOcclusionTimeout");
  1039. if (!available &&
  1040. mOcclusionChecks[LLViewerCamera::sCurCameraID] < (U32)max_count)
  1041. {
  1042. ++mOcclusionChecks[LLViewerCamera::sCurCameraID];
  1043. return;
  1044. }
  1045. if (!available)
  1046. {
  1047. ++sOcclusionTimeouts;
  1048. }
  1049. mOcclusionChecks[LLViewerCamera::sCurCameraID] = 0;
  1050. // Read back the result
  1051. GLuint res = 0;
  1052. glGetQueryObjectuiv(query, GL_QUERY_RESULT, &res);
  1053. if (res > 0)
  1054. {
  1055. clearOcclusionState(OCCLUDED, STATE_MODE_DIFF);
  1056. }
  1057. else
  1058. {
  1059. setOcclusionState(OCCLUDED, STATE_MODE_DIFF);
  1060. }
  1061. clearOcclusionState(QUERY_PENDING);
  1062. }
  1063. else if (!gUsePBRShaders && mSpatialPartition->isOcclusionEnabled() &&
  1064. isOcclusionState(OCCLUDED))
  1065. {
  1066. // Check occlusion has been issued for occluded node that has not had a
  1067. // query issued
  1068. clearOcclusionState(OCCLUDED, STATE_MODE_DIFF);
  1069. }
  1070. }
  1071. void LLOcclusionCullingGroup::doOcclusion(LLCamera* camera,
  1072. const LLVector4a* shift)
  1073. {
  1074. if (!mSpatialPartition || !mSpatialPartition->isOcclusionEnabled() ||
  1075. LLPipeline::sUseOcclusion <= 1)
  1076. {
  1077. return;
  1078. }
  1079. // Move mBounds to the agent space if necessary
  1080. LLVector4a bounds[2];
  1081. bounds[0] = mBounds[0];
  1082. bounds[1] = mBounds[1];
  1083. if (shift)
  1084. {
  1085. bounds[0].add(*shift);
  1086. }
  1087. // Do not cull hole/edge water, unless we have the GL_ARB_depth_clamp
  1088. // extension
  1089. if (earlyFail(camera, bounds))
  1090. {
  1091. LL_FAST_TIMER(FTM_OCCLUSION_EARLY_FAIL);
  1092. setOcclusionState(DISCARD_QUERY);
  1093. clearOcclusionState(OCCLUDED, STATE_MODE_DIFF);
  1094. return;
  1095. }
  1096. if (isOcclusionState(QUERY_PENDING) && !isOcclusionState(DISCARD_QUERY))
  1097. {
  1098. return;
  1099. }
  1100. // No query pending, or previous query to be discarded
  1101. {
  1102. LL_FAST_TIMER(FTM_RENDER_OCCLUSION);
  1103. GLuint query = mOcclusionQuery[LLViewerCamera::sCurCameraID];
  1104. if (!query)
  1105. {
  1106. LL_FAST_TIMER(FTM_OCCLUSION_ALLOCATE);
  1107. query = getNewOcclusionQueryObjectName();
  1108. mOcclusionQuery[LLViewerCamera::sCurCameraID] = query;
  1109. }
  1110. U32 type = mSpatialPartition->mDrawableType;
  1111. // Depth clamp all water to avoid it being culled as a result of being
  1112. // behind the far clip plane, and in the case of edge water to avoid it
  1113. // being culled while still visible.
  1114. bool use_depth_clamp = gGLManager.mHasDepthClamp &&
  1115. (type == LLPipeline::RENDER_TYPE_WATER ||
  1116. type == LLPipeline::RENDER_TYPE_VOIDWATER);
  1117. LLGLEnable clamp(use_depth_clamp ? GL_DEPTH_CLAMP : 0);
  1118. U32 mode = gGLManager.mHasOcclusionQuery2 ? GL_ANY_SAMPLES_PASSED
  1119. : GL_SAMPLES_PASSED;
  1120. {
  1121. LL_FAST_TIMER(FTM_PUSH_OCCLUSION_VERTS);
  1122. // Store which frame this query was issued on
  1123. mOcclusionIssued[LLViewerCamera::sCurCameraID] = gFrameCount;
  1124. {
  1125. LL_FAST_TIMER(FTM_OCCLUSION_BEGIN_QUERY);
  1126. // Get an occlusion query that has not been used in a while
  1127. releaseOcclusionQueryObjectName(query);
  1128. query = getNewOcclusionQueryObjectName();
  1129. mOcclusionQuery[LLViewerCamera::sCurCameraID] = query;
  1130. glBeginQuery(mode, query);
  1131. }
  1132. LLGLSLShader* shader = LLGLSLShader::sCurBoundShaderPtr;
  1133. if (shader) // Paranoia
  1134. {
  1135. shader->uniform3fv(LLShaderMgr::BOX_CENTER, 1,
  1136. bounds[0].getF32ptr());
  1137. F32 fudge_z;
  1138. if (type == LLPipeline::RENDER_TYPE_VOIDWATER)
  1139. {
  1140. fudge_z = 1.f;
  1141. }
  1142. else
  1143. {
  1144. fudge_z = SG_OCCLUSION_FUDGE;
  1145. }
  1146. shader->uniform3f(LLShaderMgr::BOX_SIZE,
  1147. bounds[1][0] + SG_OCCLUSION_FUDGE,
  1148. bounds[1][1] + SG_OCCLUSION_FUDGE,
  1149. bounds[1][2] + fudge_z);
  1150. }
  1151. if (!use_depth_clamp && type == LLPipeline::RENDER_TYPE_VOIDWATER)
  1152. {
  1153. LL_FAST_TIMER(FTM_OCCLUSION_DRAW_WATER);
  1154. LLGLSquashToFarClip squash;
  1155. if (camera->getOrigin().isExactlyZero())
  1156. {
  1157. // Origin is invalid, draw entire box
  1158. gPipeline.mCubeVB->drawRange(LLRender::TRIANGLE_FAN,
  1159. 0, 7, 8, 0);
  1160. gPipeline.mCubeVB->drawRange(LLRender::TRIANGLE_FAN,
  1161. 0, 7, 8, b111 * 8);
  1162. }
  1163. else
  1164. {
  1165. gPipeline.mCubeVB->drawRange(LLRender::TRIANGLE_FAN,
  1166. 0, 7, 8,
  1167. get_box_fan_indices(camera,
  1168. bounds[0]));
  1169. }
  1170. }
  1171. else
  1172. {
  1173. LL_FAST_TIMER(FTM_OCCLUSION_DRAW);
  1174. if (camera->getOrigin().isExactlyZero())
  1175. {
  1176. // Origin is invalid, draw entire box
  1177. gPipeline.mCubeVB->drawRange(LLRender::TRIANGLE_FAN,
  1178. 0, 7, 8, 0);
  1179. gPipeline.mCubeVB->drawRange(LLRender::TRIANGLE_FAN,
  1180. 0, 7, 8, b111 * 8);
  1181. }
  1182. else
  1183. {
  1184. gPipeline.mCubeVB->drawRange(LLRender::TRIANGLE_FAN,
  1185. 0, 7, 8,
  1186. get_box_fan_indices(camera,
  1187. bounds[0]));
  1188. }
  1189. }
  1190. {
  1191. LL_FAST_TIMER(FTM_OCCLUSION_END_QUERY);
  1192. glEndQuery(mode);
  1193. }
  1194. }
  1195. }
  1196. {
  1197. LL_FAST_TIMER(FTM_SET_OCCLUSION_STATE);
  1198. setOcclusionState(QUERY_PENDING);
  1199. clearOcclusionState(DISCARD_QUERY);
  1200. }
  1201. }
  1202. //-----------------------------------------------------------------------------
  1203. // LLViewerOctreePartition class
  1204. //-----------------------------------------------------------------------------
  1205. LLViewerOctreePartition::LLViewerOctreePartition()
  1206. : mRegionp(NULL),
  1207. mPartitionType(LLViewerRegion::PARTITION_NONE),
  1208. mDrawableType(0),
  1209. mOcclusionEnabled(true),
  1210. mLODSeed(0),
  1211. mLODPeriod(1)
  1212. {
  1213. LLVector4a center, size;
  1214. center.splat(0.f);
  1215. size.splat(1.f);
  1216. mOctree = new OctreeRoot(center, size, NULL);
  1217. }
  1218. LLViewerOctreePartition::~LLViewerOctreePartition()
  1219. {
  1220. cleanup();
  1221. }
  1222. void LLViewerOctreePartition::cleanup()
  1223. {
  1224. if (mOctree)
  1225. {
  1226. delete mOctree;
  1227. mOctree = NULL;
  1228. }
  1229. }
  1230. bool LLViewerOctreePartition::isOcclusionEnabled()
  1231. {
  1232. return mOcclusionEnabled || LLPipeline::sUseOcclusion > 2;
  1233. }
  1234. //-----------------------------------------------------------------------------
  1235. // LLViewerOctreeCull class
  1236. //-----------------------------------------------------------------------------
  1237. //virtual
  1238. bool LLViewerOctreeCull::earlyFail(LLViewerOctreeGroup* group)
  1239. {
  1240. return false;
  1241. }
  1242. //virtual
  1243. void LLViewerOctreeCull::traverse(const OctreeNode* n)
  1244. {
  1245. if (!n)
  1246. {
  1247. llwarns_sparse << "NULL node was passed ! Skipping..." << llendl;
  1248. llassert(false);
  1249. return;
  1250. }
  1251. LLViewerOctreeGroup* group = (LLViewerOctreeGroup*)n->getListener(0);
  1252. if (!group)
  1253. {
  1254. llwarns_once << "NULL spatial group for octree node "
  1255. << n << " ! Skipping..." << llendl;
  1256. llassert(false);
  1257. return;
  1258. }
  1259. if (earlyFail(group))
  1260. {
  1261. return;
  1262. }
  1263. if (mRes == 2 ||
  1264. (mRes && group->hasState(LLViewerOctreeGroup::SKIP_FRUSTUM_CHECK)))
  1265. {
  1266. // Fully in, just add everything
  1267. OctreeTraveler::traverse(n);
  1268. }
  1269. else
  1270. {
  1271. mRes = frustumCheck(group);
  1272. if (mRes)
  1273. {
  1274. // At least partially in, run on down
  1275. OctreeTraveler::traverse(n);
  1276. }
  1277. mRes = 0;
  1278. }
  1279. }
  1280. //------------------------------------------
  1281. // Agent space group culling
  1282. //------------------------------------------
  1283. S32 LLViewerOctreeCull::AABBInFrustumNoFarClipGroupBounds(const LLViewerOctreeGroup* group)
  1284. {
  1285. return mCamera->AABBInFrustumNoFarClip(group->mBounds[0],
  1286. group->mBounds[1]);
  1287. }
  1288. S32 LLViewerOctreeCull::AABBSphereIntersectGroupExtents(const LLViewerOctreeGroup* group)
  1289. {
  1290. return AABBSphereIntersect(group->mExtents[0], group->mExtents[1],
  1291. mCamera->getOrigin(),
  1292. mCamera->mFrustumCornerDist);
  1293. }
  1294. S32 LLViewerOctreeCull::AABBInFrustumGroupBounds(const LLViewerOctreeGroup* group)
  1295. {
  1296. return mCamera->AABBInFrustum(group->mBounds[0], group->mBounds[1]);
  1297. }
  1298. //------------------------------------------
  1299. // Agent space object set culling
  1300. //------------------------------------------
  1301. S32 LLViewerOctreeCull::AABBInFrustumNoFarClipObjectBounds(const LLViewerOctreeGroup* group)
  1302. {
  1303. return mCamera->AABBInFrustumNoFarClip(group->mObjectBounds[0],
  1304. group->mObjectBounds[1]);
  1305. }
  1306. S32 LLViewerOctreeCull::AABBSphereIntersectObjectExtents(const LLViewerOctreeGroup* group)
  1307. {
  1308. return AABBSphereIntersect(group->mObjectExtents[0],
  1309. group->mObjectExtents[1],
  1310. mCamera->getOrigin(),
  1311. mCamera->mFrustumCornerDist);
  1312. }
  1313. S32 LLViewerOctreeCull::AABBInFrustumObjectBounds(const LLViewerOctreeGroup* group)
  1314. {
  1315. return mCamera->AABBInFrustum(group->mObjectBounds[0],
  1316. group->mObjectBounds[1]);
  1317. }
  1318. //------------------------------------------
  1319. // Local regional space group culling
  1320. //------------------------------------------
  1321. S32 LLViewerOctreeCull::AABBInRegionFrustumNoFarClipGroupBounds(const LLViewerOctreeGroup* group)
  1322. {
  1323. return mCamera->AABBInRegionFrustumNoFarClip(group->mBounds[0],
  1324. group->mBounds[1]);
  1325. }
  1326. S32 LLViewerOctreeCull::AABBInRegionFrustumGroupBounds(const LLViewerOctreeGroup* group)
  1327. {
  1328. return mCamera->AABBInRegionFrustum(group->mBounds[0], group->mBounds[1]);
  1329. }
  1330. S32 LLViewerOctreeCull::AABBRegionSphereIntersectGroupExtents(const LLViewerOctreeGroup* group,
  1331. const LLVector3& shift)
  1332. {
  1333. return AABBSphereIntersect(group->mExtents[0], group->mExtents[1],
  1334. mCamera->getOrigin() - shift,
  1335. mCamera->mFrustumCornerDist);
  1336. }
  1337. //------------------------------------------
  1338. // Local regional space object culling
  1339. //------------------------------------------
  1340. S32 LLViewerOctreeCull::AABBInRegionFrustumObjectBounds(const LLViewerOctreeGroup* group)
  1341. {
  1342. return mCamera->AABBInRegionFrustum(group->mObjectBounds[0],
  1343. group->mObjectBounds[1]);
  1344. }
  1345. S32 LLViewerOctreeCull::AABBInRegionFrustumNoFarClipObjectBounds(const LLViewerOctreeGroup* group)
  1346. {
  1347. return mCamera->AABBInRegionFrustumNoFarClip(group->mObjectBounds[0],
  1348. group->mObjectBounds[1]);
  1349. }
  1350. S32 LLViewerOctreeCull::AABBRegionSphereIntersectObjectExtents(const LLViewerOctreeGroup* group,
  1351. const LLVector3& shift)
  1352. {
  1353. return AABBSphereIntersect(group->mObjectExtents[0],
  1354. group->mObjectExtents[1],
  1355. mCamera->getOrigin() - shift,
  1356. mCamera->mFrustumCornerDist);
  1357. }
  1358. //------------------------------------------
  1359. // Check if the objects projection large enough
  1360. bool LLViewerOctreeCull::checkProjectionArea(const LLVector4a& center,
  1361. const LLVector4a& size,
  1362. const LLVector3& shift,
  1363. F32 pixel_threshold,
  1364. F32 near_radius)
  1365. {
  1366. LLVector3 local_orig = mCamera->getOrigin() - shift;
  1367. LLVector4a origin;
  1368. origin.load3(local_orig.mV);
  1369. LLVector4a lookAt;
  1370. lookAt.setSub(center, origin);
  1371. F32 distance = lookAt.getLength3().getF32();
  1372. if (distance <= near_radius)
  1373. {
  1374. return true; // always load close-by objects
  1375. }
  1376. // Treat object as if it were near_radius meters closer than it actually
  1377. // is. This allows us to get some temporal coherence on visibility...
  1378. // Objects that can be reached quickly will tend to be visible.
  1379. distance -= near_radius;
  1380. F32 squared_rad = size.dot3(size).getF32();
  1381. return squared_rad / distance > pixel_threshold;
  1382. }
  1383. //virtual
  1384. bool LLViewerOctreeCull::checkObjects(const OctreeNode* branch,
  1385. const LLViewerOctreeGroup* group)
  1386. {
  1387. if (branch->getElementCount() == 0)
  1388. {
  1389. // No element
  1390. return false;
  1391. }
  1392. else if (branch->getChildCount() == 0)
  1393. {
  1394. // Leaf state, already checked tightest bounding box
  1395. return true;
  1396. }
  1397. else if (mRes == 1 && !frustumCheckObjects(group))
  1398. {
  1399. // No object in frustum
  1400. return false;
  1401. }
  1402. return true;
  1403. }
  1404. //virtual
  1405. void LLViewerOctreeCull::preprocess(LLViewerOctreeGroup* group)
  1406. {
  1407. }
  1408. //virtual
  1409. void LLViewerOctreeCull::processGroup(LLViewerOctreeGroup* group)
  1410. {
  1411. }
  1412. //virtual
  1413. void LLViewerOctreeCull::visit(const OctreeNode* branch)
  1414. {
  1415. LLViewerOctreeGroup* group = (LLViewerOctreeGroup*)branch->getListener(0);
  1416. if (group)
  1417. {
  1418. preprocess(group);
  1419. if (checkObjects(branch, group))
  1420. {
  1421. processGroup(group);
  1422. }
  1423. }
  1424. }
  1425. //--------------------------------------------------------------
  1426. // LLViewerOctreeDebug class
  1427. //--------------------------------------------------------------
  1428. //virtual
  1429. void LLViewerOctreeDebug::visit(const OctreeNode* branch)
  1430. {
  1431. #if 0
  1432. llinfos << "Node: " << (U32)branch << " - Elements: "
  1433. << branch->getElementCount() << " - Children: "
  1434. << branch->getChildCount();
  1435. for (U32 i = 0, count = branch->getChildCount(); i < count; ++i)
  1436. {
  1437. llcont << "\n - Child " << i << " : " << (U32)branch->getChild(i);
  1438. }
  1439. llcont << llendl;
  1440. #endif
  1441. LLViewerOctreeGroup* group = (LLViewerOctreeGroup*)branch->getListener(0);
  1442. if (group)
  1443. {
  1444. processGroup(group);
  1445. }
  1446. }
  1447. //virtual
  1448. void LLViewerOctreeDebug::processGroup(LLViewerOctreeGroup* group)
  1449. {
  1450. #if 0
  1451. const LLVector4a* vec4 = group->getBounds();
  1452. LLVector3 vec[2];
  1453. vec[0].set(vec4[0].getF32ptr());
  1454. vec[1].set(vec4[1].getF32ptr());
  1455. llinfos << "Bounds: " << vec[0] << " : " << vec[1] << llendl;
  1456. vec4 = group->getExtents();
  1457. vec[0].set(vec4[0].getF32ptr());
  1458. vec[1].set(vec4[1].getF32ptr());
  1459. llinfos << "Extents: " << vec[0] << " : " << vec[1] << llendl;
  1460. vec4 = group->getObjectBounds();
  1461. vec[0].set(vec4[0].getF32ptr());
  1462. vec[1].set(vec4[1].getF32ptr());
  1463. llinfos << "ObjectBounds: " << vec[0] << " : " << vec[1] << llendl;
  1464. vec4 = group->getObjectExtents();
  1465. vec[0].set(vec4[0].getF32ptr());
  1466. vec[1].set(vec4[1].getF32ptr());
  1467. llinfos << "ObjectExtents: " << vec[0] << " : " << vec[1] << llendl;
  1468. #endif
  1469. }