llspatialpartition.cpp 102 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243224422452246224722482249225022512252225322542255225622572258225922602261226222632264226522662267226822692270227122722273227422752276227722782279228022812282228322842285228622872288228922902291229222932294229522962297229822992300230123022303230423052306230723082309231023112312231323142315231623172318231923202321232223232324232523262327232823292330233123322333233423352336233723382339234023412342234323442345234623472348234923502351235223532354235523562357235823592360236123622363236423652366236723682369237023712372237323742375237623772378237923802381238223832384238523862387238823892390239123922393239423952396239723982399240024012402240324042405240624072408240924102411241224132414241524162417241824192420242124222423242424252426242724282429243024312432243324342435243624372438243924402441244224432444244524462447244824492450245124522453245424552456245724582459246024612462246324642465246624672468246924702471247224732474247524762477247824792480248124822483248424852486248724882489249024912492249324942495249624972498249925002501250225032504250525062507250825092510251125122513251425152516251725182519252025212522252325242525252625272528252925302531253225332534253525362537253825392540254125422543254425452546254725482549255025512552255325542555255625572558255925602561256225632564256525662567256825692570257125722573257425752576257725782579258025812582258325842585258625872588258925902591259225932594259525962597259825992600260126022603260426052606260726082609261026112612261326142615261626172618261926202621262226232624262526262627262826292630263126322633263426352636263726382639264026412642264326442645264626472648264926502651265226532654265526562657265826592660266126622663266426652666266726682669267026712672267326742675267626772678267926802681268226832684268526862687268826892690269126922693269426952696269726982699270027012702270327042705270627072708270927102711271227132714271527162717271827192720272127222723272427252726272727282729273027312732273327342735273627372738273927402741274227432744274527462747274827492750275127522753275427552756275727582759276027612762276327642765276627672768276927702771277227732774277527762777277827792780278127822783278427852786278727882789279027912792279327942795279627972798279928002801280228032804280528062807280828092810281128122813281428152816281728182819282028212822282328242825282628272828282928302831283228332834283528362837283828392840284128422843284428452846284728482849285028512852285328542855285628572858285928602861286228632864286528662867286828692870287128722873287428752876287728782879288028812882288328842885288628872888288928902891289228932894289528962897289828992900290129022903290429052906290729082909291029112912291329142915291629172918291929202921292229232924292529262927292829292930293129322933293429352936293729382939294029412942294329442945294629472948294929502951295229532954295529562957295829592960296129622963296429652966296729682969297029712972297329742975297629772978297929802981298229832984298529862987298829892990299129922993299429952996299729982999300030013002300330043005300630073008300930103011301230133014301530163017301830193020302130223023302430253026302730283029303030313032303330343035303630373038303930403041304230433044304530463047304830493050305130523053305430553056305730583059306030613062306330643065306630673068306930703071307230733074307530763077307830793080308130823083308430853086308730883089309030913092309330943095309630973098309931003101310231033104310531063107310831093110311131123113311431153116311731183119312031213122312331243125312631273128312931303131313231333134313531363137313831393140314131423143314431453146314731483149315031513152315331543155315631573158315931603161316231633164316531663167316831693170317131723173317431753176317731783179318031813182318331843185318631873188318931903191319231933194319531963197319831993200320132023203320432053206320732083209321032113212321332143215321632173218321932203221322232233224322532263227322832293230323132323233323432353236323732383239324032413242324332443245324632473248324932503251325232533254325532563257325832593260326132623263326432653266326732683269327032713272327332743275327632773278327932803281328232833284328532863287328832893290329132923293329432953296329732983299330033013302330333043305330633073308330933103311331233133314331533163317331833193320332133223323332433253326332733283329333033313332333333343335333633373338333933403341334233433344334533463347334833493350335133523353335433553356335733583359336033613362336333643365336633673368336933703371337233733374337533763377337833793380338133823383338433853386338733883389339033913392339333943395339633973398339934003401340234033404340534063407340834093410341134123413341434153416341734183419342034213422342334243425342634273428342934303431343234333434343534363437343834393440344134423443344434453446344734483449345034513452345334543455345634573458345934603461346234633464346534663467346834693470347134723473347434753476347734783479348034813482348334843485348634873488348934903491349234933494349534963497349834993500350135023503350435053506350735083509351035113512351335143515351635173518351935203521352235233524352535263527352835293530353135323533353435353536353735383539354035413542354335443545354635473548354935503551355235533554355535563557355835593560356135623563356435653566356735683569357035713572357335743575357635773578357935803581358235833584358535863587358835893590359135923593359435953596359735983599360036013602360336043605360636073608360936103611361236133614361536163617361836193620362136223623362436253626362736283629363036313632363336343635363636373638363936403641364236433644364536463647364836493650365136523653365436553656365736583659366036613662366336643665366636673668366936703671367236733674367536763677367836793680368136823683368436853686368736883689369036913692369336943695369636973698369937003701370237033704370537063707370837093710371137123713371437153716371737183719372037213722372337243725372637273728372937303731373237333734373537363737373837393740374137423743374437453746374737483749375037513752375337543755375637573758375937603761376237633764376537663767376837693770377137723773377437753776377737783779378037813782378337843785378637873788378937903791379237933794379537963797379837993800380138023803380438053806380738083809381038113812381338143815381638173818381938203821382238233824382538263827382838293830383138323833383438353836383738383839384038413842384338443845384638473848384938503851385238533854385538563857385838593860386138623863386438653866386738683869387038713872387338743875387638773878387938803881388238833884388538863887388838893890389138923893389438953896389738983899390039013902390339043905390639073908390939103911391239133914391539163917391839193920392139223923392439253926392739283929393039313932393339343935393639373938393939403941394239433944394539463947394839493950395139523953395439553956395739583959396039613962396339643965396639673968396939703971397239733974397539763977397839793980398139823983398439853986398739883989399039913992399339943995399639973998399940004001400240034004400540064007400840094010401140124013401440154016401740184019402040214022402340244025402640274028402940304031403240334034403540364037403840394040404140424043404440454046404740484049405040514052405340544055405640574058405940604061406240634064406540664067406840694070407140724073407440754076407740784079408040814082408340844085408640874088408940904091409240934094409540964097409840994100410141024103410441054106410741084109411041114112411341144115411641174118411941204121412241234124412541264127412841294130413141324133413441354136413741384139414041414142414341444145414641474148414941504151415241534154415541564157415841594160416141624163416441654166416741684169417041714172417341744175417641774178417941804181418241834184418541864187418841894190419141924193419441954196419741984199420042014202420342044205420642074208420942104211421242134214421542164217421842194220422142224223422442254226422742284229423042314232423342344235423642374238423942404241424242434244424542464247424842494250425142524253425442554256425742584259426042614262426342644265426642674268426942704271427242734274427542764277427842794280428142824283428442854286428742884289429042914292429342944295429642974298429943004301430243034304430543064307430843094310431143124313431443154316431743184319432043214322432343244325432643274328432943304331433243334334433543364337433843394340434143424343434443454346434743484349435043514352435343544355435643574358435943604361436243634364436543664367436843694370437143724373437443754376
  1. /**
  2. * @file llspatialpartition.cpp
  3. * @brief LLSpatialGroup 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 "llspatialpartition.h"
  34. #include "llfasttimer.h"
  35. #include "llglslshader.h"
  36. #include "lloctree.h"
  37. #include "llphysshapebuilderutil.h"
  38. #include "llrender.h"
  39. #include "llrenderutils.h" // For gl_draw_box*()
  40. #include "llvolume.h"
  41. #include "llvolumeoctree.h"
  42. #include "hbxxh.h"
  43. #include "llappviewer.h"
  44. #include "llface.h"
  45. #include "llfloatertools.h"
  46. #include "llmeshrepository.h"
  47. #include "llpipeline.h"
  48. //MK
  49. #include "mkrlinterface.h"
  50. //mk
  51. #include "llviewercamera.h"
  52. #include "llviewercontrol.h"
  53. #include "llviewerdisplay.h" // For gCubeSnapshot
  54. #include "llviewerobjectlist.h"
  55. #include "llviewerregion.h"
  56. #include "llviewershadermgr.h"
  57. #include "llviewerwindow.h" // For gDebugRaycast*
  58. #include "llvoavatar.h"
  59. #include "llvolumemgr.h"
  60. #include "llvovolume.h"
  61. U32 LLSpatialGroup::sNodeCount = 0;
  62. bool LLSpatialGroup::sNoDelete = false;
  63. U32 gOctreeMaxCapacity;
  64. F32 gOctreeMinSize;
  65. // Must be adjusted upwards for OpenSim grids to avoid the dreaded
  66. // "Element exceeds range of spatial partition" issue on TPs and its
  67. // consequences (crashes or massive occlusion issues).
  68. LLVector4a gOctreeMaxMag(1024.f * 1024.f);
  69. spatial_groups_set_t gVisibleSelectedGroups;
  70. bool gPickingProbe = false;
  71. static F32 sLastMaxTexPriority = 1.f;
  72. static F32 sCurMaxTexPriority = 1.f;
  73. bool LLSpatialPartition::sTeleportRequested = false;
  74. // Returns:
  75. // 0 if sphere and AABB are not intersecting
  76. // 1 if they are
  77. // 2 if AABB is entirely inside sphere
  78. S32 LLSphereAABB(const LLVector3& center, const LLVector3& size,
  79. const LLVector3& pos, const F32& rad)
  80. {
  81. S32 ret = 2;
  82. LLVector3 min = center - size;
  83. LLVector3 max = center + size;
  84. for (U32 i = 0; i < 3; ++i)
  85. {
  86. if (min.mV[i] > pos.mV[i] + rad || max.mV[i] < pos.mV[i] - rad)
  87. {
  88. // Totally outside
  89. return 0;
  90. }
  91. if (min.mV[i] < pos.mV[i] - rad || max.mV[i] > pos.mV[i] + rad)
  92. {
  93. // Intersecting
  94. ret = 1;
  95. }
  96. }
  97. return ret;
  98. }
  99. ///////////////////////////////////////////////////////////////////////////////
  100. // LLOctreeIntersect class
  101. ///////////////////////////////////////////////////////////////////////////////
  102. class alignas(16) LLOctreeIntersect final
  103. : public LLOctreeTraveler<LLViewerOctreeEntry>
  104. {
  105. protected:
  106. LOG_CLASS(LLOctreeIntersect);
  107. public:
  108. LLOctreeIntersect(const LLVector4a& start, const LLVector4a& end,
  109. bool pick_transparent, bool pick_rigged, S32* face_hit,
  110. LLVector4a* intersection, LLVector2* tex_coord,
  111. LLVector4a* normal, LLVector4a* tangent)
  112. : mStart(start),
  113. mEnd(end),
  114. mFaceHit(face_hit),
  115. mIntersection(intersection),
  116. mTexCoord(tex_coord),
  117. mNormal(normal),
  118. mTangent(tangent),
  119. mHit(NULL),
  120. mPickTransparent(pick_transparent),
  121. mPickRigged(pick_rigged)
  122. {
  123. }
  124. void visit(const OctreeNode* branchp) override
  125. {
  126. for (OctreeNode::const_element_iter it = branchp->getDataBegin(),
  127. end = branchp->getDataEnd();
  128. it != end; ++it)
  129. {
  130. check(*it);
  131. }
  132. }
  133. LLDrawable* check(const OctreeNode* nodep)
  134. {
  135. if (!nodep)
  136. {
  137. llwarns << "NULL node passed to LLOctreeIntersect::check()"
  138. << llendl;
  139. return NULL;
  140. }
  141. nodep->accept(this);
  142. LLMatrix4a local_matrix4a;
  143. for (U32 i = 0; i < nodep->getChildCount(); ++i)
  144. {
  145. const OctreeNode* childp = nodep->getChild(i);
  146. if (!childp)
  147. {
  148. llwarns << "NULL spatial partition for node " << nodep
  149. << llendl;
  150. continue;
  151. }
  152. LLSpatialGroup* groupp = (LLSpatialGroup*)childp->getListener(0);
  153. if (!groupp)
  154. {
  155. llwarns << "NULL spatial group for child " << childp
  156. << " of node " << nodep << llendl;
  157. continue;
  158. }
  159. const LLVector4a* bounds = groupp->getBounds();
  160. LLVector4a size = bounds[1];
  161. LLVector4a center = bounds[0];
  162. LLVector4a local_start = mStart;
  163. LLVector4a local_end = mEnd;
  164. LLSpatialPartition* partp = groupp->getSpatialPartition();
  165. if (partp)
  166. {
  167. LLSpatialBridge* bridgep = partp->asBridge();
  168. if (bridgep)
  169. {
  170. LLDrawable* drawp = bridgep->mDrawable.get();
  171. if (drawp)
  172. {
  173. LLMatrix4 local_matrix = drawp->getRenderMatrix();
  174. local_matrix.invert();
  175. local_matrix4a.loadu(local_matrix);
  176. local_matrix4a.affineTransform(mStart, local_start);
  177. local_matrix4a.affineTransform(mEnd, local_end);
  178. }
  179. else
  180. {
  181. llwarns << "NULL drawable for spatial partition bridge of group "
  182. << groupp << " of child " << childp
  183. << " of node " << nodep << llendl;
  184. }
  185. }
  186. }
  187. else
  188. {
  189. llwarns << "NULL spatial partition for group " << groupp
  190. << " of child " << childp << " of node " << nodep
  191. << llendl;
  192. }
  193. if (LLLineSegmentBoxIntersect(local_start, local_end, center,
  194. size))
  195. {
  196. check(childp);
  197. }
  198. }
  199. return mHit;
  200. }
  201. bool check(LLViewerOctreeEntry* entryp)
  202. {
  203. LLDrawable* drawp = (LLDrawable*)entryp->getDrawable();
  204. if (!drawp || !gPipeline.hasRenderType(drawp->getRenderType()) ||
  205. !drawp->isVisible())
  206. {
  207. return false;
  208. }
  209. if (drawp->isSpatialBridge())
  210. {
  211. LLSpatialPartition* partp = drawp->asPartition();
  212. if (partp)
  213. {
  214. LLSpatialBridge* bridgep = partp->asBridge();
  215. if (bridgep && gPipeline.hasRenderType(bridgep->mDrawableType))
  216. {
  217. check(partp->mOctree);
  218. }
  219. }
  220. else
  221. {
  222. llwarns << "NULL spatial partition for drawable " << drawp
  223. << llendl;
  224. }
  225. return false;
  226. }
  227. LLViewerObject* vobjp = drawp->getVObj();
  228. if (!vobjp)
  229. {
  230. return false;
  231. }
  232. // Forbid any interaction with HUDs when they are hidden. HB
  233. if (!LLPipeline::sShowHUDAttachments && vobjp->isHUDAttachment())
  234. {
  235. return false;
  236. }
  237. // The block of code below deals with selection behaviour changes when
  238. // the build floater is visible; some interactions are forbidden when
  239. // it is not. The "PickUnselectableInEdit" setting (non-persistent and
  240. // defaulting to TRUE) determines whether we do pick non-selectable
  241. // objects when the build floater is visible). HB
  242. static LLCachedControl<bool> edit_pick(gSavedSettings,
  243. "PickUnselectableInEdit");
  244. bool not_building = !gPickingProbe && // Skip when rendering probes
  245. (!edit_pick || !LLFloaterTools::isVisible());
  246. // Forbid interaction when the build tools floater is not visible and
  247. // when this volume got an ignore click action set.
  248. if (not_building && vobjp->getClickAction() == CLICK_ACTION_IGNORE)
  249. {
  250. return false;
  251. }
  252. // Forbid interaction when this volume is a reflection probe and the
  253. // the build tools floater is not visible. I added this test here to
  254. // replace the 'pick_unselectable' hack as implemented in LL's PBR
  255. // viewer, which ruined it for us due to the differences between async
  256. // and sync picking in v1 and v2+ viewers; it is also much simpler this
  257. // way... HB
  258. if (not_building && vobjp->isReflectionProbe())
  259. {
  260. return false;
  261. }
  262. // We can interact with this volume: do check for intersection.
  263. LLVector4a intersection;
  264. bool skip_check = false;
  265. if (vobjp->isAvatar())
  266. {
  267. LLVOAvatar* avp = (LLVOAvatar*)vobjp;
  268. if (mPickRigged ||
  269. (avp->isSelf() && LLFloaterTools::isVisible()))
  270. {
  271. LLViewerObject* hitp =
  272. avp->lineSegmentIntersectRiggedAttachments(mStart, mEnd,
  273. -1,
  274. mPickTransparent,
  275. mPickRigged,
  276. mFaceHit,
  277. &intersection,
  278. mTexCoord,
  279. mNormal,
  280. mTangent);
  281. if (hitp)
  282. {
  283. mEnd = intersection;
  284. if (mIntersection)
  285. {
  286. *mIntersection = intersection;
  287. }
  288. mHit = hitp->mDrawable;
  289. skip_check = true;
  290. }
  291. }
  292. }
  293. if (!skip_check &&
  294. vobjp->lineSegmentIntersect(mStart, mEnd, -1, mPickTransparent,
  295. mPickRigged, mFaceHit, &intersection,
  296. mTexCoord, mNormal, mTangent))
  297. {
  298. // Shorten the ray so we only find CLOSER hits:
  299. mEnd = intersection;
  300. if (mIntersection)
  301. {
  302. *mIntersection = intersection;
  303. }
  304. mHit = vobjp->mDrawable;
  305. }
  306. return false;
  307. }
  308. public:
  309. LLVector4a mStart;
  310. LLVector4a mEnd;
  311. S32* mFaceHit;
  312. LLVector4a* mIntersection;
  313. LLVector2* mTexCoord;
  314. LLVector4a* mNormal;
  315. LLVector4a* mTangent;
  316. LLDrawable* mHit;
  317. bool mPickTransparent;
  318. bool mPickRigged;
  319. };
  320. ///////////////////////////////////////////////////////////////////////////////
  321. LLSpatialGroup::~LLSpatialGroup()
  322. {
  323. #if LL_DEBUG
  324. if (gDebugGL)
  325. {
  326. # if 0 // Note that this might actually "normally" happen, if to judge from
  327. // LL's latest viewer code... HB
  328. if (sNoDelete)
  329. {
  330. llerrs << "Deleted while in sNoDelete mode !" << llendl;
  331. }
  332. # endif
  333. gPipeline.checkReferences(this);
  334. }
  335. #endif
  336. --sNodeCount;
  337. clearDrawMap();
  338. }
  339. void LLSpatialGroup::clearDrawMap()
  340. {
  341. mDrawMap.clear();
  342. }
  343. bool LLSpatialGroup::isHUDGroup()
  344. {
  345. if (isDead())
  346. {
  347. return false;
  348. }
  349. LLSpatialPartition* partp = getSpatialPartition();
  350. return partp && partp->isHUDPartition();
  351. }
  352. bool LLSpatialGroup::updateInGroup(LLDrawable* drawablep, bool immediate)
  353. {
  354. if (!drawablep)
  355. {
  356. llwarns << "NULL drawable !" << llendl;
  357. return false;
  358. }
  359. drawablep->updateSpatialExtents();
  360. OctreeNode* parentp = mOctreeNode->getOctParent();
  361. if (mOctreeNode->isInside(drawablep->getPositionGroup()) &&
  362. (mOctreeNode->contains(drawablep->getEntry()) ||
  363. (drawablep->getBinRadius() > mOctreeNode->getSize()[0] &&
  364. parentp && parentp->getElementCount() >= gOctreeMaxCapacity)))
  365. {
  366. unbound();
  367. setState(OBJECT_DIRTY);
  368. #if 0
  369. setState(GEOM_DIRTY);
  370. #endif
  371. return true;
  372. }
  373. return false;
  374. }
  375. bool LLSpatialGroup::addObject(LLDrawable* drawablep)
  376. {
  377. if (!drawablep)
  378. {
  379. return false;
  380. }
  381. drawablep->setGroup(this);
  382. setState(OBJECT_DIRTY | GEOM_DIRTY);
  383. setOcclusionState(DISCARD_QUERY, STATE_MODE_ALL_CAMERAS);
  384. gPipeline.markRebuild(this);
  385. if (drawablep->isSpatialBridge())
  386. {
  387. mBridgeList.push_back((LLSpatialBridge*)drawablep);
  388. }
  389. if (drawablep->getRadius() > 1.f)
  390. {
  391. setState(IMAGE_DIRTY);
  392. }
  393. return true;
  394. }
  395. void LLSpatialGroup::rebuildGeom()
  396. {
  397. if (!isDead())
  398. {
  399. getSpatialPartition()->rebuildGeom(this);
  400. if (hasState(MESH_DIRTY))
  401. {
  402. gPipeline.markMeshDirty(this);
  403. }
  404. }
  405. }
  406. void LLSpatialGroup::rebuildMesh()
  407. {
  408. if (!isDead())
  409. {
  410. getSpatialPartition()->rebuildMesh(this);
  411. }
  412. }
  413. void LLSpatialPartition::rebuildGeom(LLSpatialGroup* groupp)
  414. {
  415. if (groupp->isDead() || !groupp->hasState(LLSpatialGroup::GEOM_DIRTY))
  416. {
  417. return;
  418. }
  419. if (groupp->changeLOD())
  420. {
  421. groupp->mLastUpdateDistance = groupp->mDistance;
  422. groupp->mLastUpdateViewAngle = groupp->mViewAngle;
  423. }
  424. LL_FAST_TIMER(FTM_REBUILD_VBO);
  425. groupp->clearDrawMap();
  426. // Get geometry count
  427. U32 index_count = 0;
  428. U32 vertex_count = 0;
  429. {
  430. LL_FAST_TIMER(FTM_ADD_GEOMETRY_COUNT);
  431. addGeometryCount(groupp, vertex_count, index_count);
  432. }
  433. LLPointer<LLVertexBuffer>& vb = groupp->mVertexBuffer;
  434. if (vertex_count > 0 && index_count > 0)
  435. {
  436. // Create vertex buffer containing volume geometry for this node
  437. {
  438. LL_FAST_TIMER(FTM_CREATE_VB);
  439. groupp->mBuilt = 1.f;
  440. if (vb.isNull() || vb->getNumVerts() != vertex_count ||
  441. vb->getNumIndices() != index_count)
  442. {
  443. vb = createVertexBuffer(mVertexDataMask);
  444. if (!vb->allocateBuffer(vertex_count, index_count))
  445. {
  446. llwarns << "Failure to allocate a vertex buffer with "
  447. << vertex_count << " vertices and "
  448. << index_count << " indices" << llendl;
  449. vb = NULL;
  450. groupp->mBufferMap.clear();
  451. groupp->mLastUpdateTime = gFrameTimeSeconds;
  452. groupp->clearState(LLSpatialGroup::GEOM_DIRTY);
  453. return;
  454. }
  455. }
  456. }
  457. {
  458. LL_FAST_TIMER(FTM_GET_GEOMETRY);
  459. getGeometry(groupp);
  460. }
  461. }
  462. else
  463. {
  464. vb = NULL;
  465. groupp->mBufferMap.clear();
  466. }
  467. groupp->mLastUpdateTime = gFrameTimeSeconds;
  468. groupp->clearState(LLSpatialGroup::GEOM_DIRTY);
  469. }
  470. LLSpatialGroup* LLSpatialGroup::getParent()
  471. {
  472. return (LLSpatialGroup*)LLViewerOctreeGroup::getParent();
  473. }
  474. bool LLSpatialGroup::removeObject(LLDrawable* drawablep, bool from_octree)
  475. {
  476. if (!drawablep)
  477. {
  478. return false;
  479. }
  480. unbound();
  481. if (mOctreeNode && !from_octree)
  482. {
  483. drawablep->setGroup(NULL);
  484. return true;
  485. }
  486. drawablep->setGroup(NULL);
  487. setState(GEOM_DIRTY);
  488. gPipeline.markRebuild(this);
  489. if (drawablep->isSpatialBridge())
  490. {
  491. for (bridge_list_t::iterator it = mBridgeList.begin(),
  492. end = mBridgeList.end();
  493. it != end; ++it)
  494. {
  495. if (*it == drawablep)
  496. {
  497. mBridgeList.erase(it);
  498. break;
  499. }
  500. }
  501. }
  502. if (isEmpty())
  503. {
  504. // Delete draw map on last element removal since a rebuild might never
  505. // happen.
  506. clearDrawMap();
  507. }
  508. return true;
  509. }
  510. void LLSpatialGroup::shift(const LLVector4a& offset)
  511. {
  512. LLVector4a t = mOctreeNode->getCenter();
  513. t.add(offset);
  514. mOctreeNode->setCenter(t);
  515. mOctreeNode->updateMinMax();
  516. mBounds[0].add(offset);
  517. mExtents[0].add(offset);
  518. mExtents[1].add(offset);
  519. mObjectBounds[0].add(offset);
  520. mObjectExtents[0].add(offset);
  521. mObjectExtents[1].add(offset);
  522. LLSpatialPartition* partition = getSpatialPartition();
  523. if (!partition)
  524. {
  525. llwarns_sparse << "NULL octree partition !" << llendl;
  526. llassert(false);
  527. return;
  528. }
  529. U32 type = partition->mPartitionType;
  530. if (!partition->mRenderByGroup &&
  531. type != LLViewerRegion::PARTITION_TREE &&
  532. type != LLViewerRegion::PARTITION_TERRAIN &&
  533. type != LLViewerRegion::PARTITION_AVATAR &&
  534. type != LLViewerRegion::PARTITION_PUPPET &&
  535. type != LLViewerRegion::PARTITION_BRIDGE)
  536. {
  537. setState(GEOM_DIRTY);
  538. gPipeline.markRebuild(this);
  539. }
  540. }
  541. class LLSpatialSetState : public OctreeTraveler
  542. {
  543. public:
  544. LLSpatialSetState(U32 state)
  545. : mState(state)
  546. {
  547. }
  548. void visit(const OctreeNode* branchp) override
  549. {
  550. LLSpatialGroup* groupp = (LLSpatialGroup*)branchp->getListener(0);
  551. if (groupp)
  552. {
  553. groupp->setState(mState);
  554. }
  555. }
  556. public:
  557. U32 mState;
  558. };
  559. class LLSpatialSetStateDiff final : public LLSpatialSetState
  560. {
  561. public:
  562. LLSpatialSetStateDiff(U32 state)
  563. : LLSpatialSetState(state)
  564. {
  565. }
  566. void traverse(const OctreeNode* nodep) override
  567. {
  568. LLSpatialGroup* groupp = (LLSpatialGroup*)nodep->getListener(0);
  569. if (groupp && !groupp->hasState(mState))
  570. {
  571. OctreeTraveler::traverse(nodep);
  572. }
  573. }
  574. };
  575. void LLSpatialGroup::setState(U32 state, S32 mode)
  576. {
  577. llassert(state <= LLSpatialGroup::STATE_MASK);
  578. if (mode <= STATE_MODE_SINGLE)
  579. {
  580. mState |= state;
  581. }
  582. else if (mode == STATE_MODE_DIFF)
  583. {
  584. LLSpatialSetStateDiff setter(state);
  585. setter.traverse(mOctreeNode);
  586. }
  587. else
  588. {
  589. LLSpatialSetState setter(state);
  590. setter.traverse(mOctreeNode);
  591. }
  592. }
  593. class LLSpatialClearState : public OctreeTraveler
  594. {
  595. public:
  596. LLSpatialClearState(U32 state)
  597. : mState(state)
  598. {
  599. }
  600. void visit(const OctreeNode* branchp) override
  601. {
  602. LLSpatialGroup* groupp = (LLSpatialGroup*)branchp->getListener(0);
  603. if (groupp)
  604. {
  605. groupp->clearState(mState);
  606. }
  607. }
  608. public:
  609. U32 mState;
  610. };
  611. class LLSpatialClearStateDiff final : public LLSpatialClearState
  612. {
  613. public:
  614. LLSpatialClearStateDiff(U32 state)
  615. : LLSpatialClearState(state)
  616. {
  617. }
  618. void traverse(const OctreeNode* nodep) override
  619. {
  620. LLSpatialGroup* groupp = (LLSpatialGroup*)nodep->getListener(0);
  621. if (groupp && groupp->hasState(mState))
  622. {
  623. OctreeTraveler::traverse(nodep);
  624. }
  625. }
  626. };
  627. void LLSpatialGroup::clearState(U32 state, S32 mode)
  628. {
  629. llassert(state <= LLSpatialGroup::STATE_MASK);
  630. if (mode > STATE_MODE_SINGLE)
  631. {
  632. if (mode == STATE_MODE_DIFF)
  633. {
  634. LLSpatialClearStateDiff clearer(state);
  635. clearer.traverse(mOctreeNode);
  636. }
  637. else
  638. {
  639. LLSpatialClearState clearer(state);
  640. clearer.traverse(mOctreeNode);
  641. }
  642. }
  643. else
  644. {
  645. mState &= ~state;
  646. }
  647. }
  648. //======================================
  649. // Octree Listener Implementation
  650. //======================================
  651. LLSpatialGroup::LLSpatialGroup(OctreeNode* node, LLSpatialPartition* part)
  652. : LLOcclusionCullingGroup(node, part),
  653. mObjectBoxSize(1.f),
  654. mGeometryBytes(0),
  655. mSurfaceArea(0.f),
  656. mAvatarp(NULL),
  657. mRenderOrder(0),
  658. mBuilt(0.f),
  659. mVertexBuffer(NULL),
  660. mDistance(0.f),
  661. mDepth(0.f),
  662. mLastUpdateDistance(-1.f),
  663. mLastUpdateTime(gFrameTimeSeconds)
  664. {
  665. ll_assert_aligned(this, 16);
  666. ++sNodeCount;
  667. mViewAngle.splat(0.f);
  668. mLastUpdateViewAngle.splat(-1.f);
  669. setState(SG_INITIAL_STATE_MASK);
  670. gPipeline.markRebuild(this);
  671. // Let the reflection map manager know about this spatial group
  672. mReflectionProbe =
  673. gPipeline.mReflectionMapManager.registerSpatialGroup(this);
  674. mRadius = 1;
  675. mPixelArea = 1024.f;
  676. }
  677. void LLSpatialGroup::updateDistance(LLCamera &camera)
  678. {
  679. if (LLViewerCamera::sCurCameraID != LLViewerCamera::CAMERA_WORLD)
  680. {
  681. llwarns << "Attempted to update distance for camera other than world camera !"
  682. << llendl;
  683. return;
  684. }
  685. if (gShiftFrame)
  686. {
  687. return;
  688. }
  689. #if LL_DEBUG
  690. if (hasState(OBJECT_DIRTY))
  691. {
  692. llerrs << "Spatial group dirty on distance update." << llendl;
  693. }
  694. #endif
  695. if (!isEmpty())
  696. {
  697. LLSpatialPartition* partition = getSpatialPartition();
  698. if (!partition)
  699. {
  700. llwarns_sparse << "NULL octree partition !" << llendl;
  701. llassert(false);
  702. return;
  703. }
  704. mRadius = partition->mRenderByGroup ? mObjectBounds[1].getLength3().getF32()
  705. : mOctreeNode->getSize().getLength3().getF32();
  706. mDistance = partition->calcDistance(this, camera);
  707. mPixelArea = partition->calcPixelArea(this, camera);
  708. }
  709. }
  710. F32 LLSpatialPartition::calcDistance(LLSpatialGroup* groupp, LLCamera& camera)
  711. {
  712. LLVector4a eye;
  713. LLVector4a origin;
  714. origin.load3(camera.getOrigin().mV);
  715. eye.setSub(groupp->mObjectBounds[0], origin);
  716. F32 dist = 0.f;
  717. if (groupp->mDrawMap.find(LLRenderPass::PASS_ALPHA) !=
  718. groupp->mDrawMap.end())
  719. {
  720. LLVector4a v = eye;
  721. dist = eye.getLength3().getF32();
  722. eye.normalize3fast();
  723. if (!groupp->hasState(LLSpatialGroup::ALPHA_DIRTY))
  724. {
  725. if (!groupp->getSpatialPartition()->isBridge())
  726. {
  727. LLVector4a view_angle = eye;
  728. LLVector4a diff;
  729. diff.setSub(view_angle, groupp->mLastUpdateViewAngle);
  730. if (diff.getLength3().getF32() > 0.64f)
  731. {
  732. groupp->mViewAngle = view_angle;
  733. groupp->mLastUpdateViewAngle = view_angle;
  734. // For occasional alpha sorting within the group.
  735. // NOTE: If there is a trivial way to detect that alpha
  736. // sorting here would not change the render order, not
  737. // setting this node to dirty would be a very good thing.
  738. groupp->setState(LLSpatialGroup::ALPHA_DIRTY);
  739. gPipeline.markRebuild(groupp);
  740. }
  741. }
  742. }
  743. // Calculate depth of node for alpha sorting
  744. LLVector3 at = camera.getAtAxis();
  745. LLVector4a ata;
  746. ata.load3(at.mV);
  747. LLVector4a t = ata;
  748. // front of bounding box
  749. t.mul(0.25f);
  750. t.mul(groupp->mObjectBounds[1]);
  751. v.sub(t);
  752. groupp->mDepth = v.dot3(ata).getF32();
  753. }
  754. else
  755. {
  756. dist = eye.getLength3().getF32();
  757. }
  758. if (dist < 16.f)
  759. {
  760. dist /= 16.f;
  761. dist *= dist;
  762. dist *= 16.f;
  763. }
  764. return dist;
  765. }
  766. F32 LLSpatialPartition::calcPixelArea(LLSpatialGroup* groupp, LLCamera& camera)
  767. {
  768. return LLPipeline::calcPixelArea(groupp->mObjectBounds[0],
  769. groupp->mObjectBounds[1], camera);
  770. }
  771. bool LLSpatialGroup::changeLOD()
  772. {
  773. if (hasState(ALPHA_DIRTY | OBJECT_DIRTY))
  774. {
  775. // A rebuild is going to happen, update distance and LoD
  776. return true;
  777. }
  778. if (getSpatialPartition()->mSlopRatio > 0.f)
  779. {
  780. F32 ratio = (mDistance - mLastUpdateDistance) /
  781. llmax(mLastUpdateDistance, mRadius);
  782. if (fabsf(ratio) >= getSpatialPartition()->mSlopRatio)
  783. {
  784. return true;
  785. }
  786. }
  787. return needsUpdate();
  788. }
  789. void LLSpatialGroup::handleInsertion(const TreeNode* nodep,
  790. LLViewerOctreeEntry* entryp)
  791. {
  792. if (!entryp)
  793. {
  794. llwarns << "Tried to insert a NULL drawable in node " << nodep
  795. << llendl;
  796. llassert(false);
  797. return;
  798. }
  799. addObject((LLDrawable*)entryp->getDrawable());
  800. unbound();
  801. setState(OBJECT_DIRTY);
  802. }
  803. void LLSpatialGroup::handleRemoval(const TreeNode* nodep,
  804. LLViewerOctreeEntry* entryp)
  805. {
  806. if (!entryp)
  807. {
  808. llwarns << "Tried to remove a NULL drawable from node " << nodep
  809. << llendl;
  810. llassert(false);
  811. return;
  812. }
  813. removeObject((LLDrawable*)entryp->getDrawable(), true);
  814. LLViewerOctreeGroup::handleRemoval(nodep, entryp);
  815. }
  816. void LLSpatialGroup::handleDestruction(const TreeNode* nodep)
  817. {
  818. if (isDead())
  819. {
  820. return;
  821. }
  822. setState(DEAD);
  823. for (element_iter it = getDataBegin(); it != getDataEnd(); ++it)
  824. {
  825. LLViewerOctreeEntry* entryp = *it;
  826. if (entryp && entryp->getGroup() == this && entryp->hasDrawable())
  827. {
  828. ((LLDrawable*)entryp->getDrawable())->setGroup(NULL);
  829. }
  830. }
  831. // Clean up avatar attachment stats
  832. LLSpatialBridge* bridgep = getSpatialPartition()->asBridge();
  833. if (bridgep && bridgep->mAvatar.notNull())
  834. {
  835. bridgep->mAvatar->subtractAttachmentBytes(mGeometryBytes);
  836. bridgep->mAvatar->subtractAttachmentArea(mSurfaceArea);
  837. }
  838. clearDrawMap();
  839. mVertexBuffer = NULL;
  840. mBufferMap.clear();
  841. mOctreeNode = NULL;
  842. }
  843. void LLSpatialGroup::handleChildAddition(const OctreeNode*,
  844. OctreeNode* childp)
  845. {
  846. if (!childp)
  847. {
  848. llwarns << "Attempted to add a NULL child node" << llendl;
  849. llassert(false);
  850. return;
  851. }
  852. if (childp->getListenerCount())
  853. {
  854. llwarns << "Group redundancy detected." << llendl;
  855. llassert(false);
  856. return;
  857. }
  858. new LLSpatialGroup(childp, getSpatialPartition());
  859. unbound();
  860. }
  861. void LLSpatialGroup::destroyGL(bool keep_occlusion)
  862. {
  863. setState(GEOM_DIRTY | IMAGE_DIRTY);
  864. // Going to need a rebuild
  865. gPipeline.markRebuild(this);
  866. mLastUpdateTime = gFrameTimeSeconds;
  867. mVertexBuffer = NULL;
  868. mBufferMap.clear();
  869. clearDrawMap();
  870. if (!keep_occlusion)
  871. {
  872. releaseOcclusionQueryObjectNames();
  873. }
  874. const element_list& element_vec = getData();
  875. for (U32 i = 0, count = element_vec.size(); i < count; ++i)
  876. {
  877. LLDrawable* drawp = (LLDrawable*)element_vec[i]->getDrawable();
  878. if (drawp)
  879. {
  880. for (U32 j = 0, count = drawp->getNumFaces(); j < count; ++j)
  881. {
  882. LLFace* facep = drawp->getFace(j);
  883. if (facep)
  884. {
  885. facep->clearVertexBuffer();
  886. }
  887. }
  888. }
  889. }
  890. }
  891. LLDrawable* LLSpatialGroup::lineSegmentIntersect(const LLVector4a& start,
  892. const LLVector4a& end,
  893. bool pick_transparent,
  894. bool pick_rigged,
  895. S32* face_hit,
  896. LLVector4a* intersection,
  897. LLVector2* tex_coord,
  898. LLVector4a* normal,
  899. LLVector4a* tangent)
  900. {
  901. LLOctreeIntersect intersect(start, end, pick_transparent, pick_rigged,
  902. face_hit, intersection, tex_coord, normal,
  903. tangent);
  904. return intersect.check(getOctreeNode());
  905. }
  906. ///////////////////////////////////////////////////////////////////////////////
  907. LLSpatialPartition::LLSpatialPartition(U32 data_mask,
  908. bool render_by_group,
  909. LLViewerRegion* regionp)
  910. : mRenderByGroup(render_by_group),
  911. mBridge(NULL)
  912. {
  913. mRegionp = regionp;
  914. mVertexDataMask = data_mask;
  915. mDepthMask = false;
  916. mSlopRatio = 0.25f;
  917. mInfiniteFarClip = false;
  918. new LLSpatialGroup(mOctree, this);
  919. }
  920. //virtual
  921. LLSpatialPartition::~LLSpatialPartition()
  922. {
  923. cleanup();
  924. }
  925. LLSpatialGroup* LLSpatialPartition::put(LLDrawable* drawablep,
  926. bool was_visible)
  927. {
  928. drawablep->updateSpatialExtents();
  929. // Keep drawable from being garbage collected
  930. LLPointer<LLDrawable> ptr = drawablep;
  931. if (!drawablep->getGroup())
  932. {
  933. mOctree->insert(drawablep->getEntry());
  934. }
  935. LLSpatialGroup* groupp = drawablep->getSpatialGroup();
  936. if (groupp && was_visible &&
  937. groupp->isOcclusionState(LLSpatialGroup::QUERY_PENDING))
  938. {
  939. groupp->setOcclusionState(LLSpatialGroup::DISCARD_QUERY,
  940. LLSpatialGroup::STATE_MODE_ALL_CAMERAS);
  941. }
  942. return groupp;
  943. }
  944. bool LLSpatialPartition::remove(LLDrawable* drawablep, LLSpatialGroup* curp)
  945. {
  946. if (curp->removeObject(drawablep))
  947. {
  948. drawablep->setGroup(NULL);
  949. return true;
  950. }
  951. llwarns << "Failed to remove drawable from octree !" << llendl;
  952. llassert(false);
  953. return false;
  954. }
  955. void LLSpatialPartition::move(LLDrawable* drawablep, LLSpatialGroup* curp,
  956. bool immediate)
  957. {
  958. // Sanity check submitted by open source user Bushing Spatula who was
  959. // seeing crashing here (see VWR-424 reported by Bunny Mayne)
  960. if (!drawablep)
  961. {
  962. llwarns << "Bad drawable !" << llendl;
  963. llassert(false);
  964. return;
  965. }
  966. bool was_visible = curp && curp->isVisible();
  967. if (curp && curp->getSpatialPartition() != this)
  968. {
  969. // Keep drawable from being garbage collected
  970. LLPointer<LLDrawable> ptr = drawablep;
  971. if (curp->getSpatialPartition()->remove(drawablep, curp))
  972. {
  973. put(drawablep, was_visible);
  974. return;
  975. }
  976. else
  977. {
  978. llwarns << "Drawable lost between spatial partitions on outbound transition."
  979. << llendl;
  980. llassert(false);
  981. }
  982. }
  983. if (curp && curp->updateInGroup(drawablep, immediate))
  984. {
  985. // Already updated, do not need to do anything
  986. return;
  987. }
  988. // Keep drawable from being garbage collected
  989. LLPointer<LLDrawable> ptr = drawablep;
  990. if (curp && !remove(drawablep, curp))
  991. {
  992. llwarns << "Move could not find existing spatial group !" << llendl;
  993. llassert(false);
  994. }
  995. put(drawablep, was_visible);
  996. }
  997. class LLSpatialShift final : public OctreeTraveler
  998. {
  999. public:
  1000. LLSpatialShift(const LLVector4a& offset)
  1001. : mOffset(offset)
  1002. {
  1003. }
  1004. void visit(const OctreeNode* branchp) override
  1005. {
  1006. LLSpatialGroup* groupp = (LLSpatialGroup*)branchp->getListener(0);
  1007. if (groupp)
  1008. {
  1009. groupp->shift(mOffset);
  1010. }
  1011. }
  1012. public:
  1013. const LLVector4a& mOffset;
  1014. };
  1015. void LLSpatialPartition::shift(const LLVector4a& offset)
  1016. {
  1017. // Shift octree node bounding boxes by offset
  1018. LLSpatialShift shifter(offset);
  1019. shifter.traverse(mOctree);
  1020. }
  1021. class LLOctreeCull : public LLViewerOctreeCull
  1022. {
  1023. protected:
  1024. LOG_CLASS(LLOctreeCull);
  1025. public:
  1026. LLOctreeCull(LLCamera* camerap)
  1027. : LLViewerOctreeCull(camerap)
  1028. {
  1029. }
  1030. bool earlyFail(LLViewerOctreeGroup* base_groupp) override
  1031. {
  1032. LLSpatialGroup* groupp = (LLSpatialGroup*)base_groupp;
  1033. if (!groupp)
  1034. {
  1035. llwarns_sparse << "NULL spatial group ! Skipping..." << llendl;
  1036. llassert(false);
  1037. return true;
  1038. }
  1039. #if 1
  1040. if (LLPipeline::sReflectionRender)
  1041. {
  1042. return false;
  1043. }
  1044. #endif
  1045. groupp->checkOcclusion();
  1046. // Never occlusion cull the root node
  1047. if (groupp->getOctreeNode()->getParent() &&
  1048. // Ignore occlusion if disabled
  1049. LLPipeline::sUseOcclusion &&
  1050. groupp->isOcclusionState(LLSpatialGroup::OCCLUDED))
  1051. {
  1052. gPipeline.markOccluder(groupp);
  1053. return true;
  1054. }
  1055. return false;
  1056. }
  1057. S32 frustumCheck(const LLViewerOctreeGroup* groupp) override
  1058. {
  1059. S32 res = AABBInFrustumNoFarClipGroupBounds(groupp);
  1060. if (res)
  1061. {
  1062. res = llmin(res, AABBSphereIntersectGroupExtents(groupp));
  1063. }
  1064. return res;
  1065. }
  1066. S32 frustumCheckObjects(const LLViewerOctreeGroup* groupp) override
  1067. {
  1068. S32 res = AABBInFrustumNoFarClipObjectBounds(groupp);
  1069. if (res)
  1070. {
  1071. res = llmin(res, AABBSphereIntersectObjectExtents(groupp));
  1072. }
  1073. return res;
  1074. }
  1075. void processGroup(LLViewerOctreeGroup* base_groupp) override
  1076. {
  1077. LLSpatialGroup* groupp = (LLSpatialGroup*)base_groupp;
  1078. if (!groupp)
  1079. {
  1080. llwarns_sparse << "NULL spatial group ! Skipping..." << llendl;
  1081. llassert(false);
  1082. return;
  1083. }
  1084. // Apparently, occlusion is still broken in the PBR renderer... HB
  1085. if (!gUsePBRShaders)
  1086. {
  1087. if (groupp->needsUpdate() ||
  1088. groupp->getVisible(LLViewerCamera::sCurCameraID) <
  1089. LLViewerOctreeEntryData::getCurrentFrame() - 1)
  1090. {
  1091. groupp->doOcclusion(mCamera);
  1092. }
  1093. }
  1094. gPipeline.markNotCulled(groupp, *mCamera);
  1095. }
  1096. };
  1097. class LLOctreeCullNoFarClip final : public LLOctreeCull
  1098. {
  1099. public:
  1100. LLOctreeCullNoFarClip(LLCamera* camerap)
  1101. : LLOctreeCull(camerap)
  1102. {
  1103. }
  1104. S32 frustumCheck(const LLViewerOctreeGroup* groupp) override
  1105. {
  1106. return AABBInFrustumNoFarClipGroupBounds(groupp);
  1107. }
  1108. S32 frustumCheckObjects(const LLViewerOctreeGroup* groupp) override
  1109. {
  1110. return AABBInFrustumNoFarClipObjectBounds(groupp);
  1111. }
  1112. };
  1113. class LLOctreeCullShadow : public LLOctreeCull
  1114. {
  1115. public:
  1116. LLOctreeCullShadow(LLCamera* camerap)
  1117. : LLOctreeCull(camerap)
  1118. {
  1119. }
  1120. S32 frustumCheck(const LLViewerOctreeGroup* groupp) override
  1121. {
  1122. return AABBInFrustumGroupBounds(groupp);
  1123. }
  1124. S32 frustumCheckObjects(const LLViewerOctreeGroup* groupp) override
  1125. {
  1126. return AABBInFrustumObjectBounds(groupp);
  1127. }
  1128. };
  1129. class LLOctreeCullVisExtents final : public LLOctreeCullShadow
  1130. {
  1131. protected:
  1132. LOG_CLASS(LLOctreeCullVisExtents);
  1133. public:
  1134. LLOctreeCullVisExtents(LLCamera* camerap, LLVector4a& min, LLVector4a& max)
  1135. : LLOctreeCullShadow(camerap),
  1136. mMin(min),
  1137. mMax(max),
  1138. mEmpty(true)
  1139. {
  1140. }
  1141. bool earlyFail(LLViewerOctreeGroup* base_groupp) override
  1142. {
  1143. LLSpatialGroup* groupp = (LLSpatialGroup*)base_groupp;
  1144. if (!groupp)
  1145. {
  1146. llwarns_sparse << "NULL spatial group ! Skipping..." << llendl;
  1147. llassert(false);
  1148. return true;
  1149. }
  1150. // Never occlusion cull the root node
  1151. if (groupp->getOctreeNode()->getParent() &&
  1152. // Ignore occlusion if disabled
  1153. LLPipeline::sUseOcclusion &&
  1154. groupp->isOcclusionState(LLSpatialGroup::OCCLUDED))
  1155. {
  1156. return true;
  1157. }
  1158. return false;
  1159. }
  1160. void traverse(const OctreeNode* nodep) override
  1161. {
  1162. LLSpatialGroup* groupp = (LLSpatialGroup*)nodep->getListener(0);
  1163. if (!groupp)
  1164. {
  1165. llwarns_once << "NULL spatial group for octree node "
  1166. << nodep << " ! Skipping..." << llendl;
  1167. llassert(false);
  1168. return;
  1169. }
  1170. if (earlyFail(groupp))
  1171. {
  1172. return;
  1173. }
  1174. if (mRes == 2 ||
  1175. (mRes && groupp->hasState(LLSpatialGroup::SKIP_FRUSTUM_CHECK)))
  1176. {
  1177. // Do not need to do frustum check
  1178. OctreeTraveler::traverse(nodep);
  1179. }
  1180. else
  1181. {
  1182. mRes = frustumCheck(groupp);
  1183. if (mRes)
  1184. {
  1185. // At least partially in, run on down
  1186. OctreeTraveler::traverse(nodep);
  1187. }
  1188. mRes = 0;
  1189. }
  1190. }
  1191. void processGroup(LLViewerOctreeGroup* base_groupp) override
  1192. {
  1193. LLSpatialGroup* groupp = (LLSpatialGroup*)base_groupp;
  1194. if (!groupp)
  1195. {
  1196. llwarns_sparse << "NULL spatial group ! Skipping..." << llendl;
  1197. llassert(false);
  1198. return;
  1199. }
  1200. llassert(!groupp->hasState(LLSpatialGroup::DIRTY) &&
  1201. !groupp->isEmpty());
  1202. if (mRes >= 2 || AABBInFrustumObjectBounds(groupp) > 0)
  1203. {
  1204. mEmpty = false;
  1205. const LLVector4a* exts = groupp->getObjectExtents();
  1206. update_min_max(mMin, mMax, exts[0]);
  1207. update_min_max(mMin, mMax, exts[1]);
  1208. }
  1209. }
  1210. public:
  1211. LLVector4a& mMin;
  1212. LLVector4a& mMax;
  1213. bool mEmpty;
  1214. };
  1215. class LLOctreeSelect final : public LLOctreeCull
  1216. {
  1217. public:
  1218. LLOctreeSelect(LLCamera* camerap, std::vector<LLDrawable*>* resultsp)
  1219. : LLOctreeCull(camerap),
  1220. mResults(resultsp)
  1221. {
  1222. }
  1223. LL_INLINE bool earlyFail(LLViewerOctreeGroup*) override
  1224. {
  1225. return false;
  1226. }
  1227. LL_INLINE void preprocess(LLViewerOctreeGroup*) override
  1228. {
  1229. }
  1230. void processGroup(LLViewerOctreeGroup* base_group) override
  1231. {
  1232. LLSpatialGroup* group = (LLSpatialGroup*)base_group;
  1233. if (!group)
  1234. {
  1235. llwarns_sparse << "NULL spatial group ! Skipping..." << llendl;
  1236. llassert(false);
  1237. return;
  1238. }
  1239. OctreeNode* branch = group->getOctreeNode();
  1240. if (!branch)
  1241. {
  1242. llwarns_sparse << "NULL octree node ! Skipping..." << llendl;
  1243. llassert(false);
  1244. return;
  1245. }
  1246. for (OctreeNode::const_element_iter i = branch->getDataBegin(),
  1247. end = branch->getDataEnd();
  1248. i != end; ++i)
  1249. {
  1250. LLDrawable* drawable = (LLDrawable*)(*i)->getDrawable();
  1251. if (drawable && !drawable->isDead())
  1252. {
  1253. if (drawable->isSpatialBridge())
  1254. {
  1255. drawable->setVisible(*mCamera, mResults, true);
  1256. }
  1257. else
  1258. {
  1259. mResults->push_back(drawable);
  1260. }
  1261. }
  1262. }
  1263. }
  1264. public:
  1265. std::vector<LLDrawable*>* mResults;
  1266. };
  1267. class LLOctreeDirty : public OctreeTraveler
  1268. {
  1269. protected:
  1270. LOG_CLASS(LLOctreeDirty);
  1271. public:
  1272. LLOctreeDirty(bool no_rebuild)
  1273. : mNoRebuild(no_rebuild)
  1274. {
  1275. }
  1276. void visit(const OctreeNode* state) override
  1277. {
  1278. LLSpatialGroup* groupp = (LLSpatialGroup*)state->getListener(0);
  1279. if (!groupp)
  1280. {
  1281. llwarns_sparse << "NULL spatial group ! Skipping..." << llendl;
  1282. llassert(false);
  1283. return;
  1284. }
  1285. groupp->destroyGL(true);
  1286. if (!mNoRebuild)
  1287. {
  1288. for (LLSpatialGroup::element_iter i = groupp->getDataBegin(),
  1289. end = groupp->getDataEnd();
  1290. i != end; ++i)
  1291. {
  1292. LLDrawable* drawablep = (LLDrawable*)(*i)->getDrawable();
  1293. if (!drawablep)
  1294. {
  1295. llwarns_once << "NULL drawable found in spatial group "
  1296. << std::hex << groupp << std::dec << llendl;
  1297. continue;
  1298. }
  1299. LLViewerObject* vobjp = drawablep->getVObj().get();
  1300. if (!vobjp)
  1301. {
  1302. continue;
  1303. }
  1304. vobjp->resetVertexBuffers();
  1305. if (!groupp->getSpatialPartition()->mRenderByGroup)
  1306. {
  1307. gPipeline.markRebuild(drawablep);
  1308. }
  1309. }
  1310. }
  1311. for (LLSpatialGroup::bridge_list_t::iterator
  1312. i = groupp->mBridgeList.begin(),
  1313. end = groupp->mBridgeList.end();
  1314. i != end; ++i)
  1315. {
  1316. LLSpatialBridge* bridgep = *i;
  1317. if (bridgep)
  1318. {
  1319. traverse(bridgep->mOctree);
  1320. }
  1321. else
  1322. {
  1323. llwarns_once << "NULL bridge found in spatial group "
  1324. << std::hex << groupp << std::dec << llendl;
  1325. }
  1326. }
  1327. }
  1328. private:
  1329. bool mNoRebuild;
  1330. };
  1331. void LLSpatialPartition::resetVertexBuffers()
  1332. {
  1333. LLOctreeDirty dirty(sTeleportRequested && !gUsePBRShaders);
  1334. dirty.traverse(mOctree);
  1335. }
  1336. bool LLSpatialPartition::getVisibleExtents(LLCamera& camera, LLVector3& vis_min,
  1337. LLVector3& vis_max)
  1338. {
  1339. LLVector4a vis_min_a, vis_max_a;
  1340. vis_min_a.load3(vis_min.mV);
  1341. vis_max_a.load3(vis_max.mV);
  1342. {
  1343. LL_FAST_TIMER(FTM_CULL_REBOUND);
  1344. LLSpatialGroup* groupp = (LLSpatialGroup*)mOctree->getListener(0);
  1345. if (groupp)
  1346. {
  1347. groupp->rebound();
  1348. }
  1349. }
  1350. LLOctreeCullVisExtents vis(&camera, vis_min_a, vis_max_a);
  1351. vis.traverse(mOctree);
  1352. vis_min.set(vis_min_a.getF32ptr());
  1353. vis_max.set(vis_max_a.getF32ptr());
  1354. return vis.mEmpty;
  1355. }
  1356. S32 LLSpatialPartition::cull(LLCamera& camera,
  1357. std::vector<LLDrawable*>* resultsp,
  1358. bool for_select)
  1359. {
  1360. {
  1361. LL_FAST_TIMER(FTM_CULL_REBOUND);
  1362. LLSpatialGroup* groupp = (LLSpatialGroup*)mOctree->getListener(0);
  1363. if (groupp)
  1364. {
  1365. groupp->rebound();
  1366. }
  1367. }
  1368. #if 0
  1369. if (for_select)
  1370. #endif
  1371. {
  1372. LLOctreeSelect selecter(&camera, resultsp);
  1373. selecter.traverse(mOctree);
  1374. }
  1375. return 0;
  1376. }
  1377. S32 LLSpatialPartition::cull(LLCamera& camera, bool do_occlusion)
  1378. {
  1379. {
  1380. LL_FAST_TIMER(FTM_CULL_REBOUND);
  1381. LLSpatialGroup* groupp = (LLSpatialGroup*)mOctree->getListener(0);
  1382. if (groupp)
  1383. {
  1384. groupp->rebound();
  1385. }
  1386. }
  1387. if (LLPipeline::sShadowRender)
  1388. {
  1389. LL_FAST_TIMER(FTM_FRUSTUM_CULL);
  1390. LLOctreeCullShadow culler(&camera);
  1391. culler.traverse(mOctree);
  1392. }
  1393. else if (mInfiniteFarClip || (!LLPipeline::sUseFarClip && !gCubeSnapshot))
  1394. {
  1395. LL_FAST_TIMER(FTM_FRUSTUM_CULL);
  1396. LLOctreeCullNoFarClip culler(&camera);
  1397. culler.traverse(mOctree);
  1398. }
  1399. else
  1400. {
  1401. LL_FAST_TIMER(FTM_FRUSTUM_CULL);
  1402. LLOctreeCull culler(&camera);
  1403. culler.traverse(mOctree);
  1404. }
  1405. return 0;
  1406. }
  1407. // Note: 'mask' is ignored for PBR rendering.
  1408. void pushVerts(LLDrawInfo* paramsp, U32 mask)
  1409. {
  1410. LLRenderPass::applyModelMatrix(*paramsp);
  1411. paramsp->mVertexBuffer->setBuffer(mask);
  1412. paramsp->mVertexBuffer->drawRange(LLRender::TRIANGLES,
  1413. paramsp->mStart, paramsp->mEnd,
  1414. paramsp->mCount, paramsp->mOffset);
  1415. }
  1416. // Note: 'mask' is ignored for PBR rendering.
  1417. void pushVerts(LLSpatialGroup* groupp, U32 mask)
  1418. {
  1419. for (LLSpatialGroup::draw_map_t::const_iterator
  1420. it = groupp->mDrawMap.begin(), end = groupp->mDrawMap.end();
  1421. it != end; ++it)
  1422. {
  1423. const LLSpatialGroup::drawmap_elem_t& draw_info_vec = it->second;
  1424. for (U32 i = 0, count = draw_info_vec.size(); i < count; ++i)
  1425. {
  1426. LLDrawInfo* paramsp = draw_info_vec[i].get();
  1427. pushVerts(paramsp, mask);
  1428. }
  1429. }
  1430. }
  1431. // Note: 'mask' is ignored for PBR rendering.
  1432. void pushVerts(LLFace* facep, U32 mask)
  1433. {
  1434. if (facep)
  1435. {
  1436. llassert(facep->verify());
  1437. facep->renderIndexed(mask);
  1438. }
  1439. }
  1440. // Note: 'mask' is ignored for PBR rendering.
  1441. void pushVerts(LLDrawable* drawablep, U32 mask)
  1442. {
  1443. for (S32 i = 0, count = drawablep->getNumFaces(); i < count; ++i)
  1444. {
  1445. pushVerts(drawablep->getFace(i), mask);
  1446. }
  1447. }
  1448. void pushVerts(LLVolume* volp)
  1449. {
  1450. if (!volp) return;
  1451. LLVertexBuffer::unbind();
  1452. for (S32 i = 0, count = volp->getNumVolumeFaces(); i < count; ++i)
  1453. {
  1454. const LLVolumeFace& face = volp->getVolumeFace(i);
  1455. LLVertexBuffer::drawElements(face.mNumVertices, face.mPositions, NULL,
  1456. face.mNumIndices, face.mIndices);
  1457. }
  1458. }
  1459. // Note: 'mask' is ignored for PBR rendering.
  1460. void pushBufferVerts(LLVertexBuffer* buffp, U32 mask)
  1461. {
  1462. if (buffp)
  1463. {
  1464. buffp->setBuffer(mask);
  1465. buffp->drawRange(LLRender::TRIANGLES, 0, buffp->getNumVerts() - 1,
  1466. buffp->getNumIndices(), 0);
  1467. }
  1468. }
  1469. // Note: 'mask' is ignored for PBR rendering.
  1470. void pushBufferVerts(LLSpatialGroup* groupp, U32 mask, bool push_alpha = true)
  1471. {
  1472. if (groupp->getSpatialPartition()->mRenderByGroup &&
  1473. !groupp->mDrawMap.empty())
  1474. {
  1475. LLDrawInfo* paramsp = *(groupp->mDrawMap.begin()->second.begin());
  1476. LLRenderPass::applyModelMatrix(*paramsp);
  1477. if (push_alpha)
  1478. {
  1479. pushBufferVerts(groupp->mVertexBuffer, mask);
  1480. }
  1481. for (LLSpatialGroup::buffer_map_t::const_iterator
  1482. i = groupp->mBufferMap.begin(), end = groupp->mBufferMap.end();
  1483. i != end; ++i)
  1484. {
  1485. for (LLSpatialGroup::buffer_texture_map_t::const_iterator
  1486. j = i->second.begin(), end2 = i->second.end();
  1487. j != end2; ++j)
  1488. {
  1489. for (LLSpatialGroup::buffer_list_t::const_iterator
  1490. k = j->second.begin(), end3 = j->second.end();
  1491. k != end3; ++k)
  1492. {
  1493. pushBufferVerts(*k, mask);
  1494. }
  1495. }
  1496. }
  1497. }
  1498. }
  1499. void pushVertsColorCoded(LLSpatialGroup* groupp, U32 mask)
  1500. {
  1501. static const LLColor4 colors[] = {
  1502. LLColor4::green,
  1503. LLColor4::green1,
  1504. LLColor4::green2,
  1505. LLColor4::green3,
  1506. LLColor4::green4,
  1507. LLColor4::green5,
  1508. LLColor4::green6
  1509. };
  1510. constexpr U32 col_count = LL_ARRAY_SIZE(colors);
  1511. U32 col = 0;
  1512. for (LLSpatialGroup::draw_map_t::const_iterator
  1513. it = groupp->mDrawMap.begin(), end = groupp->mDrawMap.end();
  1514. it != end; ++it)
  1515. {
  1516. const LLSpatialGroup::drawmap_elem_t& draw_vec = it->second;
  1517. for (U32 i = 0, count = draw_vec.size(); i < count; ++i)
  1518. {
  1519. LLDrawInfo* infop = draw_vec[i];
  1520. LLRenderPass::applyModelMatrix(*infop);
  1521. gGL.diffuseColor4f(colors[col].mV[0], colors[col].mV[1],
  1522. colors[col].mV[2], 0.5f);
  1523. // Note: mask ignored in PBR rendering mode
  1524. infop->mVertexBuffer->setBuffer(mask);
  1525. infop->mVertexBuffer->drawRange(LLRender::TRIANGLES,
  1526. infop->mStart, infop->mEnd,
  1527. infop->mCount, infop->mOffset);
  1528. col = (col + 1) % col_count;
  1529. }
  1530. }
  1531. }
  1532. // Renders solid object bounding box, color coded by buffer activity
  1533. void renderOctree(LLSpatialGroup* groupp)
  1534. {
  1535. gGL.setSceneBlendType(LLRender::BT_ADD_WITH_ALPHA);
  1536. LLVector4 col;
  1537. if (groupp->mBuilt > 0.f)
  1538. {
  1539. groupp->mBuilt -= 2.f * gFrameIntervalSeconds;
  1540. col.set(0.1f, 0.1f, 1.f, 0.1f);
  1541. LLGLDepthTest gl_depth(false, false);
  1542. glPolygonMode(GL_FRONT_AND_BACK, GL_LINE);
  1543. gGL.diffuseColor4f(1.f, 0.f, 0.f, groupp->mBuilt);
  1544. gGL.flush();
  1545. gGL.lineWidth(5.f);
  1546. const LLVector4a* bounds = groupp->getObjectBounds();
  1547. gl_draw_box_outline(bounds[0], bounds[1]);
  1548. gGL.flush();
  1549. gGL.lineWidth(1.f);
  1550. gGL.flush();
  1551. LLVOAvatar* last_avatar = NULL;
  1552. U64 last_hash = 0;
  1553. for (LLSpatialGroup::element_iter i = groupp->getDataBegin(),
  1554. end = groupp->getDataEnd();
  1555. i != end; ++i)
  1556. {
  1557. LLDrawable* drawable = (LLDrawable*)(*i)->getDrawable();
  1558. if (!drawable || drawable->isDead()) continue;
  1559. U32 count = drawable->getNumFaces();
  1560. if (!count) continue;
  1561. bool not_bridge = !groupp->getSpatialPartition()->isBridge();
  1562. if (not_bridge)
  1563. {
  1564. gGL.pushMatrix();
  1565. LLVector3 trans = drawable->getRegion()->getOriginAgent();
  1566. gGL.translatef(trans.mV[0], trans.mV[1], trans.mV[2]);
  1567. }
  1568. LLFace* facep = drawable->getFace(0);
  1569. bool rigged = facep->isState(LLFace::RIGGED);
  1570. gDebugProgram.bind(rigged);
  1571. gGL.diffuseColor4f(1.f, 0.f, 0.f, 1.f);
  1572. U32 type = LLVertexBuffer::MAP_VERTEX;
  1573. if (rigged)
  1574. {
  1575. if (facep->mAvatar != last_avatar ||
  1576. facep->mSkinInfo->mHash != last_hash)
  1577. {
  1578. if (!LLRenderPass::uploadMatrixPalette(facep->mAvatar,
  1579. facep->mSkinInfo))
  1580. {
  1581. continue;
  1582. }
  1583. last_avatar = facep->mAvatar;
  1584. last_hash = facep->mSkinInfo->mHash;
  1585. }
  1586. // Add the weights to the type for rigged faces
  1587. type |= LLVertexBuffer::MAP_WEIGHT4;
  1588. // Now that we got past the potential 'continue' above, we
  1589. // can push our render matrix (bug in LL's code that pushes
  1590. // it before the continue). HB
  1591. gGL.pushMatrix();
  1592. gGL.loadMatrix(gGLModelView);
  1593. }
  1594. for (U32 j = 0; j < count; ++j)
  1595. {
  1596. facep = drawable->getFace(j);
  1597. if (!facep) continue;
  1598. LLVertexBuffer* vb = facep->getVertexBuffer();
  1599. if (!vb) continue;
  1600. LLVOVolume* volp = drawable->getVOVolume();
  1601. if (gFrameTimeSeconds - facep->mLastUpdateTime < 0.5f)
  1602. {
  1603. if (volp && volp->isShrinkWrapped())
  1604. {
  1605. gGL.diffuseColor4f(0.f, 1.f, 1.f, groupp->mBuilt);
  1606. }
  1607. else
  1608. {
  1609. gGL.diffuseColor4f(0.f, 1.f, 0.f, groupp->mBuilt);
  1610. }
  1611. }
  1612. else if (gFrameTimeSeconds - facep->mLastMoveTime < 0.5f)
  1613. {
  1614. if (volp && volp->isShrinkWrapped())
  1615. {
  1616. gGL.diffuseColor4f(1.f, 1.f, 0.f, groupp->mBuilt);
  1617. }
  1618. else
  1619. {
  1620. gGL.diffuseColor4f(1.f, 0.f, 0.f, groupp->mBuilt);
  1621. }
  1622. }
  1623. else
  1624. {
  1625. continue;
  1626. }
  1627. // Note: mask ignored in PBR rendering mode
  1628. vb->setBuffer(type);
  1629. vb->draw(LLRender::TRIANGLES, facep->getIndicesCount(),
  1630. facep->getIndicesStart());
  1631. }
  1632. if (rigged)
  1633. {
  1634. gGL.popMatrix();
  1635. }
  1636. if (not_bridge)
  1637. {
  1638. gGL.popMatrix();
  1639. }
  1640. }
  1641. glPolygonMode(GL_FRONT_AND_BACK, GL_FILL);
  1642. gDebugProgram.bind(); // Make sure non-rigged variant is bound
  1643. gGL.diffuseColor4f(1.f, 1.f, 1.f, 1.f);
  1644. }
  1645. else
  1646. {
  1647. col.set(0.1f, 0.1f, 1.f, 0.1f);
  1648. }
  1649. gGL.diffuseColor4fv(col.mV);
  1650. LLVector4a fudge;
  1651. fudge.splat(0.001f);
  1652. gGL.setSceneBlendType(LLRender::BT_ALPHA);
  1653. gGL.diffuseColor4f(0.f, 1.f, 1.f, 1.f);
  1654. const LLVector4a* bounds = groupp->getBounds();
  1655. gl_draw_box_outline(bounds[0], bounds[1]);
  1656. stop_glerror();
  1657. }
  1658. void renderXRay(LLSpatialGroup* groupp)
  1659. {
  1660. if (!groupp->isVisible() || groupp->isEmpty() ||
  1661. (LLPipeline::sUseOcclusion &&
  1662. groupp->isOcclusionState(LLSpatialGroup::OCCLUDED)))
  1663. {
  1664. return;
  1665. }
  1666. pushBufferVerts(groupp, LLVertexBuffer::MAP_VERTEX, false);
  1667. bool selected = false;
  1668. for (LLSpatialGroup::element_iter iter = groupp->getDataBegin(),
  1669. end = groupp->getDataEnd();
  1670. iter != end; ++iter)
  1671. {
  1672. LLDrawable* drawable = (LLDrawable*)(*iter)->getDrawable();
  1673. if (drawable && drawable->getVObj().notNull() &&
  1674. drawable->getVObj()->isSelected())
  1675. {
  1676. selected = true;
  1677. break;
  1678. }
  1679. }
  1680. if (!selected)
  1681. {
  1682. return;
  1683. }
  1684. // Store for rendering occlusion volume as overlay
  1685. LLSpatialBridge* bridgep = groupp->getSpatialPartition()->asBridge();
  1686. if (bridgep)
  1687. {
  1688. gVisibleSelectedGroups.insert(bridgep->getSpatialGroup());
  1689. }
  1690. else
  1691. {
  1692. gVisibleSelectedGroups.insert(groupp);
  1693. }
  1694. }
  1695. void renderCrossHairs(LLVector3 position, F32 size, LLColor4 color)
  1696. {
  1697. gGL.color4fv(color.mV);
  1698. gGL.begin(LLRender::LINES);
  1699. {
  1700. gGL.vertex3fv((position - LLVector3(size, 0.f, 0.f)).mV);
  1701. gGL.vertex3fv((position + LLVector3(size, 0.f, 0.f)).mV);
  1702. gGL.vertex3fv((position - LLVector3(0.f, size, 0.f)).mV);
  1703. gGL.vertex3fv((position + LLVector3(0.f, size, 0.f)).mV);
  1704. gGL.vertex3fv((position - LLVector3(0.f, 0.f, size)).mV);
  1705. gGL.vertex3fv((position + LLVector3(0.f, 0.f, size)).mV);
  1706. }
  1707. gGL.end();
  1708. }
  1709. void renderUpdateType(LLDrawable* drawablep)
  1710. {
  1711. LLViewerObject* vobj = drawablep->getVObj();
  1712. if (!vobj || OUT_UNKNOWN == vobj->getLastUpdateType())
  1713. {
  1714. return;
  1715. }
  1716. LLGLEnable blend(GL_BLEND);
  1717. switch (vobj->getLastUpdateType())
  1718. {
  1719. case OUT_FULL:
  1720. gGL.diffuseColor4f(0.f, 1.f, 0.f, 0.5f);
  1721. break;
  1722. case OUT_TERSE_IMPROVED:
  1723. gGL.diffuseColor4f(0.f, 1.f, 1.f, 0.5f);
  1724. break;
  1725. case OUT_FULL_COMPRESSED:
  1726. if (vobj->getLastUpdateCached())
  1727. {
  1728. gGL.diffuseColor4f(1.f, 0.f, 0.f, 0.5f);
  1729. }
  1730. else
  1731. {
  1732. gGL.diffuseColor4f(1.f, 1.f, 0.f, 0.5f);
  1733. }
  1734. break;
  1735. case OUT_FULL_CACHED:
  1736. gGL.diffuseColor4f(0.f, 0.f, 1.f, 0.5f);
  1737. break;
  1738. default:
  1739. llwarns << "Unknown update_type " << vobj->getLastUpdateType()
  1740. << llendl;
  1741. }
  1742. S32 num_faces = drawablep->getNumFaces();
  1743. if (num_faces)
  1744. {
  1745. for (S32 i = 0; i < num_faces; ++i)
  1746. {
  1747. LLFace* facep = drawablep->getFace(i);
  1748. if (facep)
  1749. {
  1750. pushVerts(facep, LLVertexBuffer::MAP_VERTEX);
  1751. }
  1752. }
  1753. }
  1754. }
  1755. void renderBoundingBox(LLDrawable* drawable, bool set_color = true)
  1756. {
  1757. if (set_color)
  1758. {
  1759. if (drawable->isSpatialBridge())
  1760. {
  1761. gGL.diffuseColor4f(1.f, 0.5f, 0.f, 1.f);
  1762. }
  1763. else if (drawable->getVOVolume())
  1764. {
  1765. if (drawable->isRoot())
  1766. {
  1767. gGL.diffuseColor4f(1.f, 1.f, 0.f, 1.f);
  1768. }
  1769. else
  1770. {
  1771. gGL.diffuseColor4f(0.f, 1.f, 0.f, 1.f);
  1772. }
  1773. }
  1774. else if (drawable->getVObj())
  1775. {
  1776. switch (drawable->getVObj()->getPCode())
  1777. {
  1778. case LLViewerObject::LL_VO_SURFACE_PATCH:
  1779. gGL.diffuseColor4f(0.f, 1.f, 1.f, 1.f);
  1780. break;
  1781. case LLViewerObject::LL_VO_CLOUDS:
  1782. gGL.diffuseColor4f(0.5f, 0.5f, 0.5f, 1.f);
  1783. break;
  1784. case LLViewerObject::LL_VO_PART_GROUP:
  1785. case LLViewerObject::LL_VO_HUD_PART_GROUP:
  1786. gGL.diffuseColor4f(0.f, 0.f, 1.f, 1.f);
  1787. break;
  1788. case LLViewerObject::LL_VO_VOID_WATER:
  1789. case LLViewerObject::LL_VO_WATER:
  1790. gGL.diffuseColor4f(0.f, 0.5f, 1.f, 1.f);
  1791. break;
  1792. case LL_PCODE_LEGACY_TREE:
  1793. gGL.diffuseColor4f(0.f, 0.5f, 0.f, 1.f);
  1794. break;
  1795. default:
  1796. gGL.diffuseColor4f(1.f, 0.f, 1.f, 1.f);
  1797. }
  1798. }
  1799. else
  1800. {
  1801. gGL.diffuseColor4f(1.f, 0.f, 0.f, 1.f);
  1802. }
  1803. }
  1804. const LLVector4a* ext;
  1805. LLVector4a pos, size;
  1806. if (drawable->getVOVolume())
  1807. {
  1808. // Render face bounding boxes
  1809. for (S32 i = 0, count = drawable->getNumFaces(); i < count; ++i)
  1810. {
  1811. LLFace* facep = drawable->getFace(i);
  1812. if (facep)
  1813. {
  1814. ext = facep->mExtents;
  1815. pos.setAdd(ext[0], ext[1]);
  1816. pos.mul(0.5f);
  1817. size.setSub(ext[1], ext[0]);
  1818. size.mul(0.5f);
  1819. gl_draw_box_outline(pos, size);
  1820. }
  1821. }
  1822. }
  1823. // Render drawable bounding box
  1824. ext = drawable->getSpatialExtents();
  1825. pos.setAdd(ext[0], ext[1]);
  1826. pos.mul(0.5f);
  1827. size.setSub(ext[1], ext[0]);
  1828. size.mul(0.5f);
  1829. LLViewerObject* vobj = drawable->getVObj();
  1830. if (vobj && vobj->onActiveList())
  1831. {
  1832. gGL.flush();
  1833. gGL.lineWidth(llmax(4.f * sinf(gFrameTimeSeconds * 2.f) + 1.f, 1.f));
  1834. gl_draw_box_outline(pos,size);
  1835. gGL.flush();
  1836. gGL.lineWidth(1.f);
  1837. }
  1838. else
  1839. {
  1840. gl_draw_box_outline(pos, size);
  1841. }
  1842. stop_glerror();
  1843. }
  1844. void renderNormals(LLDrawable* drawablep)
  1845. {
  1846. if (!drawablep->isVisible() || !drawablep->getVObj())
  1847. {
  1848. return;
  1849. }
  1850. LLVOVolume* vovolp = drawablep->getVOVolume();
  1851. LLVolume* volp = NULL;
  1852. if (vovolp)
  1853. {
  1854. volp = vovolp->getVolume();
  1855. if (!volp)
  1856. {
  1857. return;
  1858. }
  1859. }
  1860. LLVolume::face_list_t* facesp = NULL;
  1861. LLDrawable::face_list_t* draw_facesp = NULL;
  1862. if (volp)
  1863. {
  1864. facesp = &volp->getVolumeFaces();
  1865. }
  1866. else
  1867. {
  1868. draw_facesp = &drawablep->getFaces();
  1869. }
  1870. if (!facesp)
  1871. {
  1872. if (!gUsePBRShaders)
  1873. {
  1874. // No available shader in EE mode to render draw-face normals. HB
  1875. return;
  1876. }
  1877. if (!draw_facesp)
  1878. {
  1879. // We cannot draw anything...
  1880. return;
  1881. }
  1882. }
  1883. // Drawable's normals & tangents are stored in model space, i.e. before any
  1884. // scaling is applied. SL-13490: using pos + normal to compute the second
  1885. // vertex of a normal line segment does not work when there is a non-
  1886. // uniform scale in the mix. Normals require MVP-inverse-transpose
  1887. // transform. We get that effect here by pre-applying the inverse scale
  1888. // (twice, because one forward scale will be re-applied via the MVP in the
  1889. // vertex shader)
  1890. F32 scale_len;
  1891. LLVector4a inv_scale;
  1892. if (vovolp)
  1893. {
  1894. LLVector3 scale_v3 = vovolp->getScale();
  1895. scale_len = scale_v3.length();
  1896. LLVector4a obj_scale(scale_v3.mV[VX], scale_v3.mV[VY],
  1897. scale_v3.mV[VZ]);
  1898. obj_scale.normalize3();
  1899. // Create inverse-scale vector for normals
  1900. inv_scale.set(1.f / scale_v3.mV[VX], 1.f / scale_v3.mV[VY],
  1901. 1.f / scale_v3.mV[VZ]);
  1902. inv_scale.mul(inv_scale); // Squared, to apply inverse scale twice
  1903. inv_scale.normalize3fast();
  1904. }
  1905. else
  1906. {
  1907. scale_len = 1.f;
  1908. inv_scale.set(1.f, 1.f, 1.f, 0.f);
  1909. }
  1910. // Normals & tangent line segments get scaled along with the object. Divide
  1911. // by scale length to keep the as-viewed lengths (relatively) constant with
  1912. // the debug setting length.
  1913. static LLCachedControl<F32> norm_scale(gSavedSettings,
  1914. "RenderDebugNormalScale");
  1915. F32 draw_length = norm_scale / scale_len;
  1916. LLVertexBuffer::unbind();
  1917. LLGLEnable blend(gUsePBRShaders ? 0 : GL_BLEND);
  1918. LLGLDepthTest gl_depth(GL_TRUE, GL_FALSE, GL_LEQUAL, gUsePBRShaders);
  1919. gGL.pushMatrix();
  1920. if (vovolp)
  1921. {
  1922. gGL.multMatrix(vovolp->getRelativeXform().getF32ptr());
  1923. }
  1924. gGL.getTexUnit(0)->unbind(LLTexUnit::TT_TEXTURE);
  1925. if (facesp)
  1926. {
  1927. LLVector4a p, v;
  1928. for (size_t i = 0, count = facesp->size(); i < count; ++i)
  1929. {
  1930. const LLVolumeFace& face = (*facesp)[i];
  1931. gGL.flush();
  1932. gGL.diffuseColor4f(1.f, 1.f, 0.f, 1.f);
  1933. gGL.begin(LLRender::LINES);
  1934. for (S32 j = 0; j < face.mNumVertices; ++j)
  1935. {
  1936. v.setMul(face.mNormals[j], 1.f);
  1937. // Pre-scale normal, so it is left with an inverse-transpose
  1938. // xform after MVP
  1939. v.mul(inv_scale);
  1940. v.normalize3fast();
  1941. v.mul(draw_length);
  1942. p.setAdd(face.mPositions[j], v);
  1943. gGL.vertex3fv(face.mPositions[j].getF32ptr());
  1944. gGL.vertex3fv(p.getF32ptr());
  1945. }
  1946. gGL.end();
  1947. if (!face.mTangents)
  1948. {
  1949. continue;
  1950. }
  1951. // Tangents are simple vectors and do not require reorientation via
  1952. // pre-scaling
  1953. gGL.flush();
  1954. gGL.diffuseColor4f(0.f, 1.f, 1.f, 1.f);
  1955. gGL.begin(LLRender::LINES);
  1956. for (S32 j = 0; j < face.mNumVertices; ++j)
  1957. {
  1958. v.setMul(face.mTangents[j], 1.f);
  1959. v.normalize3fast();
  1960. v.mul(draw_length);
  1961. p.setAdd(face.mPositions[j], v);
  1962. gGL.vertex3fv(face.mPositions[j].getF32ptr());
  1963. gGL.vertex3fv(p.getF32ptr());
  1964. }
  1965. gGL.end();
  1966. }
  1967. }
  1968. else if (draw_facesp && gUsePBRShaders) // Paranoia
  1969. {
  1970. // *HACK: prepare to restore previous shader as other debug code
  1971. // depends on a simpler shader being present.
  1972. LLGLSLShader* prev_shaderp = LLGLSLShader::sCurBoundShaderPtr;
  1973. constexpr U32 MASK_VN = LLVertexBuffer::TYPE_VERTEX |
  1974. LLVertexBuffer::TYPE_NORMAL;
  1975. for (size_t i = 0, count = draw_facesp->size(); i < count; ++i)
  1976. {
  1977. LLFace* facep = (*draw_facesp)[i];
  1978. if (!facep) // Paranoia
  1979. {
  1980. continue;
  1981. }
  1982. LLDrawable* drawp = facep->getDrawable();
  1983. if (!drawp) // Paranoia
  1984. {
  1985. continue;
  1986. }
  1987. LLVertexBuffer* bufp = facep->getVertexBuffer();
  1988. if (!bufp)
  1989. {
  1990. continue;
  1991. }
  1992. U32 mask = bufp->getTypeMask();
  1993. if ((mask & MASK_VN) != MASK_VN)
  1994. {
  1995. continue;
  1996. }
  1997. S32 type = (mask & LLVertexBuffer::TYPE_TANGENT) ?
  1998. NORMAL_DEBUG_SHADER_WITH_TANGENTS :
  1999. NORMAL_DEBUG_SHADER_DEFAULT;
  2000. LLGLSLShader* shaderp = &gNormalDebugProgram[type];
  2001. shaderp->bind();
  2002. shaderp->uniform1f(LLShaderMgr::DEBUG_NORMAL_DRAW_LENGTH,
  2003. draw_length);
  2004. LLRenderPass::applyModelMatrix(&drawp->getRegion()->mRenderMatrix);
  2005. bufp->setBuffer();
  2006. U32 geom_idx = facep->getGeomIndex();
  2007. bufp->drawRange(LLRender::TRIANGLES, geom_idx,
  2008. geom_idx + facep->getGeomCount() - 1,
  2009. facep->getIndicesCount(),
  2010. facep->getIndicesStart());
  2011. }
  2012. // Restore previous shader, if any.
  2013. if (prev_shaderp)
  2014. {
  2015. prev_shaderp->bind();
  2016. }
  2017. }
  2018. gGL.popMatrix();
  2019. stop_glerror();
  2020. }
  2021. void renderTexturePriority(LLDrawable* drawp)
  2022. {
  2023. // Used to be called in drawBox(), now replaced with gl_draw_box() which
  2024. // does not call this. HB
  2025. LLVertexBuffer::unbind();
  2026. for (S32 face = 0, count = drawp->getNumFaces(); face < count; ++face)
  2027. {
  2028. LLFace* facep = drawp->getFace(face);
  2029. if (!facep) continue;
  2030. LLVector4 cold(0.f, 0.f, 0.25f);
  2031. LLVector4 hot(1.f, 0.25f, 0.25f);
  2032. LLVector4 boost_cold(0.f, 0.f, 0.f, 0.f);
  2033. LLVector4 boost_hot(0.f, 1.f, 0.f, 1.f);
  2034. LLGLDisable blend(GL_BLEND);
  2035. F32 vsize = facep->getPixelArea();
  2036. if (vsize > sCurMaxTexPriority)
  2037. {
  2038. sCurMaxTexPriority = vsize;
  2039. }
  2040. F32 t = vsize / sLastMaxTexPriority;
  2041. LLVector4 col = lerp(cold, hot, t);
  2042. gGL.diffuseColor4fv(col.mV);
  2043. LLVector4a center;
  2044. center.setAdd(facep->mExtents[1], facep->mExtents[0]);
  2045. center.mul(0.5f);
  2046. LLVector4a size;
  2047. size.setSub(facep->mExtents[1], facep->mExtents[0]);
  2048. size.mul(0.5f);
  2049. size.add(LLVector4a(0.01f));
  2050. gl_draw_box(center, size);
  2051. }
  2052. stop_glerror();
  2053. }
  2054. void renderPoints(LLDrawable* drawablep)
  2055. {
  2056. LLGLDepthTest depth(GL_FALSE, GL_FALSE);
  2057. if (drawablep->getNumFaces())
  2058. {
  2059. gGL.begin(LLRender::POINTS);
  2060. gGL.diffuseColor3f(1.f, 1.f, 1.f);
  2061. for (S32 i = 0, count = drawablep->getNumFaces(); i < count; ++i)
  2062. {
  2063. LLFace* face = drawablep->getFace(i);
  2064. if (face)
  2065. {
  2066. gGL.vertex3fv(face->mCenterLocal.mV);
  2067. }
  2068. }
  2069. gGL.end();
  2070. stop_glerror();
  2071. }
  2072. }
  2073. void renderTextureAnim(LLDrawInfo* infop)
  2074. {
  2075. if (!infop->mTextureMatrix)
  2076. {
  2077. return;
  2078. }
  2079. LLGLEnable blend(GL_BLEND);
  2080. gGL.diffuseColor4f(1.f, 1.f, 0.f, 0.5f);
  2081. pushVerts(infop, LLVertexBuffer::MAP_VERTEX);
  2082. stop_glerror();
  2083. }
  2084. void renderBatchSize(LLDrawInfo* infop)
  2085. {
  2086. if (infop->mTextureList.empty())
  2087. {
  2088. return;
  2089. }
  2090. LLGLEnable offset(GL_POLYGON_OFFSET_FILL);
  2091. glPolygonOffset(-1.f, 1.f);
  2092. LLGLSLShader* old_shader = LLGLSLShader::sCurBoundShaderPtr;
  2093. // NOTE: does not impact PBR rendering (mask ignored). HB
  2094. U32 mask = LLVertexBuffer::MAP_VERTEX;
  2095. bool bind = false;
  2096. if (infop->mAvatar && old_shader->mRiggedVariant)
  2097. {
  2098. bind = true;
  2099. mask |= LLVertexBuffer::MAP_WEIGHT4;
  2100. gGL.pushMatrix();
  2101. gGL.loadMatrix(gGLModelView);
  2102. old_shader->mRiggedVariant->bind();
  2103. LLRenderPass::uploadMatrixPalette(*infop);
  2104. }
  2105. gGL.diffuseColor4ubv(infop->getDebugColor().mV);
  2106. pushVerts(infop, mask);
  2107. if (bind)
  2108. {
  2109. gGL.popMatrix();
  2110. old_shader->bind();
  2111. }
  2112. }
  2113. // Note: removed from the PBR renderer
  2114. void renderShadowFrusta(LLDrawInfo* infop)
  2115. {
  2116. LLGLEnable blend(GL_BLEND);
  2117. gGL.setSceneBlendType(LLRender::BT_ADD);
  2118. LLVector4a center;
  2119. center.setAdd(infop->mExtents[1], infop->mExtents[0]);
  2120. center.mul(0.5f);
  2121. LLVector4a size;
  2122. size.setSub(infop->mExtents[1],infop->mExtents[0]);
  2123. size.mul(0.5f);
  2124. if (gPipeline.mShadowCamera[4].AABBInFrustum(center, size))
  2125. {
  2126. gGL.diffuseColor3f(1.f, 0.f, 0.f);
  2127. pushVerts(infop, LLVertexBuffer::MAP_VERTEX);
  2128. }
  2129. if (gPipeline.mShadowCamera[5].AABBInFrustum(center, size))
  2130. {
  2131. gGL.diffuseColor3f(0.f, 1.f, 0.f);
  2132. pushVerts(infop, LLVertexBuffer::MAP_VERTEX);
  2133. }
  2134. if (gPipeline.mShadowCamera[6].AABBInFrustum(center, size))
  2135. {
  2136. gGL.diffuseColor3f(0.f, 0.f, 1.f);
  2137. pushVerts(infop, LLVertexBuffer::MAP_VERTEX);
  2138. }
  2139. if (gPipeline.mShadowCamera[7].AABBInFrustum(center, size))
  2140. {
  2141. gGL.diffuseColor3f(1.f, 0.f, 1.f);
  2142. pushVerts(infop, LLVertexBuffer::MAP_VERTEX);
  2143. }
  2144. gGL.setSceneBlendType(LLRender::BT_ALPHA);
  2145. stop_glerror();
  2146. }
  2147. void renderLights(LLDrawable* drawablep)
  2148. {
  2149. if (!drawablep->isLight())
  2150. {
  2151. return;
  2152. }
  2153. if (drawablep->getNumFaces())
  2154. {
  2155. LLGLEnable blend(GL_BLEND);
  2156. gGL.diffuseColor4f(0.f, 1.f, 1.f, 0.5f);
  2157. for (S32 i = 0, count = drawablep->getNumFaces(); i < count; ++i)
  2158. {
  2159. LLFace* face = drawablep->getFace(i);
  2160. if (face)
  2161. {
  2162. pushVerts(face, LLVertexBuffer::MAP_VERTEX);
  2163. }
  2164. }
  2165. const LLVector4a* ext = drawablep->getSpatialExtents();
  2166. LLVector4a pos;
  2167. pos.setAdd(ext[0], ext[1]);
  2168. pos.mul(0.5f);
  2169. LLVector4a size;
  2170. size.setSub(ext[1], ext[0]);
  2171. size.mul(0.5f);
  2172. {
  2173. LLGLDepthTest depth(GL_FALSE, GL_TRUE);
  2174. gGL.diffuseColor4f(1.f, 1.f, 1.f, 1.f);
  2175. gl_draw_box_outline(pos, size);
  2176. }
  2177. gGL.diffuseColor4f(1.f, 1.f, 0.f, 1.f);
  2178. F32 rad = drawablep->getVOVolume()->getLightRadius();
  2179. gl_draw_box_outline(pos, LLVector4a(rad));
  2180. stop_glerror();
  2181. }
  2182. }
  2183. class LLRenderOctreeRaycast final
  2184. : public LLOctreeTriangleRayIntersectNoOwnership
  2185. {
  2186. public:
  2187. LLRenderOctreeRaycast(const LLVector4a& start, const LLVector4a& dir,
  2188. F32* closest_t)
  2189. : LLOctreeTriangleRayIntersectNoOwnership(start, dir, NULL, closest_t,
  2190. NULL, NULL, NULL, NULL)
  2191. {
  2192. }
  2193. void visit(const LLOctreeNodeNoOwnership<LLVolumeTriangle>* branch) override
  2194. {
  2195. LLVolumeOctreeListenerNoOwnership* vl =
  2196. (LLVolumeOctreeListenerNoOwnership*)branch->getListener(0);
  2197. LLVector3 center, size;
  2198. if (branch->isEmpty())
  2199. {
  2200. gGL.diffuseColor3f(1.f, 0.2f, 0.f);
  2201. center.set(branch->getCenter().getF32ptr());
  2202. size.set(branch->getSize().getF32ptr());
  2203. }
  2204. else if (vl)
  2205. {
  2206. gGL.diffuseColor3f(0.75f, 1.f, 0.f);
  2207. center.set(vl->mBounds[0].getF32ptr());
  2208. size.set(vl->mBounds[1].getF32ptr());
  2209. }
  2210. gl_draw_box_outline(center, size);
  2211. for (U32 i = 0; i < 2; ++i)
  2212. {
  2213. LLGLDepthTest depth(GL_TRUE, GL_FALSE,
  2214. i == 1 ? GL_LEQUAL : GL_GREATER);
  2215. if (i == 1)
  2216. {
  2217. gGL.diffuseColor4f(0.f, 1.f, 1.f, 0.5f);
  2218. }
  2219. else
  2220. {
  2221. gGL.diffuseColor4f(0.f, 0.5f, 0.5f, 0.25f);
  2222. gl_draw_box_outline(center, size);
  2223. }
  2224. if (i == 1)
  2225. {
  2226. gGL.flush();
  2227. gGL.lineWidth(3.f);
  2228. }
  2229. gGL.begin(LLRender::TRIANGLES);
  2230. for (LLOctreeNodeNoOwnership<LLVolumeTriangle>::const_element_iter
  2231. iter = branch->getDataBegin(), end = branch->getDataEnd();
  2232. iter != end; ++iter)
  2233. {
  2234. const LLVolumeTriangle* tri = *iter;
  2235. gGL.vertex3fv(tri->mV[0]->getF32ptr());
  2236. gGL.vertex3fv(tri->mV[1]->getF32ptr());
  2237. gGL.vertex3fv(tri->mV[2]->getF32ptr());
  2238. }
  2239. gGL.end();
  2240. if (i == 1)
  2241. {
  2242. gGL.flush();
  2243. gGL.lineWidth(1.f);
  2244. }
  2245. }
  2246. }
  2247. };
  2248. void renderRaycast(LLDrawable* drawablep)
  2249. {
  2250. if (!drawablep->getNumFaces())
  2251. {
  2252. return;
  2253. }
  2254. LLGLEnable blend(GL_BLEND);
  2255. gGL.diffuseColor4f(0.f, 1.f, 1.f, 0.5f);
  2256. LLVOVolume* vobjp = drawablep->getVOVolume();
  2257. if (vobjp && !vobjp->isDead())
  2258. {
  2259. LLVolume* volp = vobjp->getVolume();
  2260. bool transform = true;
  2261. if (drawablep->isState(LLDrawable::RIGGED))
  2262. {
  2263. volp = vobjp->getRiggedVolume();
  2264. transform = false;
  2265. }
  2266. if (volp)
  2267. {
  2268. LLVector3 trans = drawablep->getRegion()->getOriginAgent();
  2269. for (S32 i = 0, count = volp->getNumVolumeFaces(); i < count;
  2270. ++i)
  2271. {
  2272. const LLVolumeFace& face = volp->getVolumeFace(i);
  2273. gGL.pushMatrix();
  2274. gGL.translatef(trans.mV[0], trans.mV[1], trans.mV[2]);
  2275. gGL.multMatrix(vobjp->getRelativeXform().getF32ptr());
  2276. LLVector4a start, end;
  2277. if (transform)
  2278. {
  2279. LLVector3 v_start(gDebugRaycastStart.getF32ptr());
  2280. LLVector3 v_end(gDebugRaycastEnd.getF32ptr());
  2281. v_start = vobjp->agentPositionToVolume(v_start);
  2282. v_end = vobjp->agentPositionToVolume(v_end);
  2283. start.load3(v_start.mV);
  2284. end.load3(v_end.mV);
  2285. }
  2286. else
  2287. {
  2288. start = gDebugRaycastStart;
  2289. end = gDebugRaycastEnd;
  2290. }
  2291. gGL.flush();
  2292. glPolygonMode(GL_FRONT_AND_BACK, GL_LINE);
  2293. if (!volp->isUnique())
  2294. {
  2295. if (!face.mOctree)
  2296. {
  2297. ((LLVolumeFace*)&face)->createOctree();
  2298. }
  2299. F32 t = 1.f;
  2300. LLRenderOctreeRaycast render(start, end, &t);
  2301. render.traverse(face.mOctree);
  2302. }
  2303. gGL.popMatrix();
  2304. glPolygonMode(GL_FRONT_AND_BACK, GL_FILL);
  2305. }
  2306. }
  2307. }
  2308. else if (drawablep->isAvatar())
  2309. {
  2310. if (drawablep->getVObj() == gDebugRaycastObject)
  2311. {
  2312. LLGLDepthTest depth(GL_FALSE);
  2313. LLVOAvatar* av = (LLVOAvatar*) drawablep->getVObj().get();
  2314. av->renderCollisionVolumes();
  2315. }
  2316. }
  2317. if (drawablep->getVObj() == gDebugRaycastObject)
  2318. {
  2319. // Used to be called in drawBox(), now replaced with gl_draw_box()
  2320. // which does not call this. HB
  2321. LLVertexBuffer::unbind();
  2322. // Draw intersection point
  2323. gGL.pushMatrix();
  2324. gGL.loadMatrix(gGLModelView);
  2325. LLVector3 translate(gDebugRaycastIntersection.getF32ptr());
  2326. gGL.translatef(translate.mV[0], translate.mV[1], translate.mV[2]);
  2327. LLCoordFrame orient;
  2328. LLVector4a debug_binormal;
  2329. debug_binormal.setCross3(gDebugRaycastNormal, gDebugRaycastTangent);
  2330. debug_binormal.mul(gDebugRaycastTangent.getF32ptr()[3]);
  2331. LLVector3 normal(gDebugRaycastNormal.getF32ptr());
  2332. LLVector3 binormal(debug_binormal.getF32ptr());
  2333. orient.lookDir(normal, binormal);
  2334. LLMatrix4 rotation;
  2335. orient.getRotMatrixToParent(rotation);
  2336. gGL.multMatrix(rotation.getF32ptr());
  2337. gGL.diffuseColor4f(1.f, 0.f, 0.f, 0.5f);
  2338. gl_draw_box(LLVector3::zero, LLVector3(0.1f, 0.022f, 0.022f));
  2339. gGL.diffuseColor4f(0.f, 1.f, 0.f, 0.5f);
  2340. gl_draw_box(LLVector3::zero, LLVector3(0.021f, 0.1f, 0.021f));
  2341. gGL.diffuseColor4f(0.f, 0.f, 1.f, 0.5f);
  2342. gl_draw_box(LLVector3::zero, LLVector3(0.02f, 0.02f, 0.1f));
  2343. gGL.popMatrix();
  2344. // Draw bounding box of prim
  2345. const LLVector4a* ext = drawablep->getSpatialExtents();
  2346. LLVector4a pos;
  2347. pos.setAdd(ext[0], ext[1]);
  2348. pos.mul(0.5f);
  2349. LLVector4a size;
  2350. size.setSub(ext[1], ext[0]);
  2351. size.mul(0.5f);
  2352. LLGLDepthTest depth(GL_FALSE, GL_TRUE);
  2353. gGL.diffuseColor4f(0.f, 0.5f, 0.5f, 1.f);
  2354. gl_draw_box_outline(pos, size);
  2355. }
  2356. }
  2357. void renderAgentTarget(LLVOAvatar* avatarp)
  2358. {
  2359. // Render these for self only (why, I don't know)
  2360. if (avatarp->isSelf())
  2361. {
  2362. renderCrossHairs(avatarp->getPositionAgent(), 0.2f,
  2363. LLColor4(1.f, 0.f, 0.f, 0.8f));
  2364. renderCrossHairs(avatarp->mDrawable->getPositionAgent(), 0.2f,
  2365. LLColor4(1.f, 0.f, 0.f, 0.8f));
  2366. renderCrossHairs(avatarp->mRoot->getWorldPosition(), 0.2f,
  2367. LLColor4(1.f, 1.f, 1.f, 0.8f));
  2368. renderCrossHairs(avatarp->mPelvisp->getWorldPosition(), 0.2f,
  2369. LLColor4(0.f, 0.f, 1.f, 0.8f));
  2370. }
  2371. }
  2372. class LLOctreeRenderNonOccluded final : public OctreeTraveler
  2373. {
  2374. public:
  2375. LLOctreeRenderNonOccluded(LLCamera* camera)
  2376. : mCamera(camera)
  2377. {
  2378. }
  2379. void traverse(const OctreeNode* node) override
  2380. {
  2381. LLSpatialGroup* group = (LLSpatialGroup*)node->getListener(0);
  2382. if (!group) return;
  2383. const LLVector4a* bounds = group->getBounds();
  2384. if (!mCamera || mCamera->AABBInFrustumNoFarClip(bounds[0], bounds[1]))
  2385. {
  2386. node->accept(this);
  2387. for (U32 i = 0, count = node->getChildCount(); i < count; ++i)
  2388. {
  2389. traverse(node->getChild(i));
  2390. }
  2391. // Draw tight fit bounding boxes for spatial group
  2392. if (gPipeline.hasRenderDebugMask(LLPipeline::RENDER_DEBUG_OCTREE))
  2393. {
  2394. group->rebuildGeom();
  2395. group->rebuildMesh();
  2396. renderOctree(group);
  2397. }
  2398. }
  2399. }
  2400. void visit(const OctreeNode* branch) override
  2401. {
  2402. LLSpatialGroup* group = (LLSpatialGroup*)branch->getListener(0);
  2403. if (!group) return;
  2404. const LLVector4a* bounds = group->getBounds();
  2405. if (group->hasState(LLSpatialGroup::GEOM_DIRTY) ||
  2406. (mCamera &&
  2407. !mCamera->AABBInFrustumNoFarClip(bounds[0], bounds[1])))
  2408. {
  2409. return;
  2410. }
  2411. LLGLDisable stencil(gUsePBRShaders ? 0 : GL_STENCIL_TEST);
  2412. group->rebuildGeom();
  2413. group->rebuildMesh();
  2414. if (gPipeline.hasRenderDebugMask(LLPipeline::RENDER_DEBUG_BBOXES))
  2415. {
  2416. if (!group->isEmpty())
  2417. {
  2418. gGL.diffuseColor3f(0.f, 0.f, 1.f);
  2419. const LLVector4a* obj_bounds = group->getObjectBounds();
  2420. gl_draw_box_outline(obj_bounds[0], obj_bounds[1]);
  2421. }
  2422. }
  2423. static LLCachedControl<bool> for_self_only(gSavedSettings,
  2424. "ShowAvatarDebugForSelfOnly");
  2425. for (OctreeNode::const_element_iter i = branch->getDataBegin(),
  2426. end = branch->getDataEnd();
  2427. i != end; ++i)
  2428. {
  2429. LLDrawable* drawable = (LLDrawable*)(*i)->getDrawable();
  2430. if (!drawable || drawable->isDead()) continue;
  2431. if (gPipeline.hasRenderDebugMask(LLPipeline::RENDER_DEBUG_BBOXES))
  2432. {
  2433. renderBoundingBox(drawable);
  2434. }
  2435. if (gPipeline.hasRenderDebugMask(LLPipeline::RENDER_DEBUG_NORMALS))
  2436. {
  2437. renderNormals(drawable);
  2438. }
  2439. if (drawable->getVOVolume() &&
  2440. gPipeline.hasRenderDebugMask(LLPipeline::RENDER_DEBUG_TEXTURE_PRIORITY))
  2441. {
  2442. renderTexturePriority(drawable);
  2443. }
  2444. if (gPipeline.hasRenderDebugMask(LLPipeline::RENDER_DEBUG_POINTS))
  2445. {
  2446. renderPoints(drawable);
  2447. }
  2448. if (gPipeline.hasRenderDebugMask(LLPipeline::RENDER_DEBUG_LIGHTS))
  2449. {
  2450. renderLights(drawable);
  2451. }
  2452. if (gPipeline.hasRenderDebugMask(LLPipeline::RENDER_DEBUG_RAYCAST))
  2453. {
  2454. renderRaycast(drawable);
  2455. }
  2456. if (gPipeline.hasRenderDebugMask(LLPipeline::RENDER_DEBUG_UPDATE_TYPE))
  2457. {
  2458. renderUpdateType(drawable);
  2459. }
  2460. LLViewerObject* objectp = drawable->getVObj().get();
  2461. LLVOAvatar* avatarp = objectp ? objectp->asAvatar() : NULL;
  2462. if (avatarp && (!for_self_only || avatarp->isSelf()))
  2463. {
  2464. if (gPipeline.hasRenderDebugMask(LLPipeline::RENDER_DEBUG_AVATAR_VOLUME))
  2465. {
  2466. avatarp->renderCollisionVolumes();
  2467. }
  2468. if (gPipeline.hasRenderDebugMask(LLPipeline::RENDER_DEBUG_AVATAR_JOINTS))
  2469. {
  2470. avatarp->renderJoints();
  2471. avatarp->renderBones();
  2472. }
  2473. if (gPipeline.hasRenderDebugMask(LLPipeline::RENDER_DEBUG_AGENT_TARGET))
  2474. {
  2475. renderAgentTarget(avatarp);
  2476. }
  2477. }
  2478. if (gDebugGL && !gUsePBRShaders)
  2479. {
  2480. for (U32 i = 0, count = drawable->getNumFaces();
  2481. i < count; ++i)
  2482. {
  2483. LLFace* facep = drawable->getFace(i);
  2484. if (facep && facep->mDrawInfo)
  2485. {
  2486. U8 index = facep->getTextureIndex();
  2487. if (index < FACE_DO_NOT_BATCH_TEXTURES)
  2488. {
  2489. if (facep->mDrawInfo->mTextureList.size() <= index)
  2490. {
  2491. llwarns << "Face texture index out of bounds." << llendl;
  2492. }
  2493. else if (facep->mDrawInfo->mTextureList[index] != facep->getTexture())
  2494. {
  2495. llwarns << "Face texture index incorrect." << llendl;
  2496. }
  2497. }
  2498. }
  2499. }
  2500. }
  2501. }
  2502. for (LLSpatialGroup::draw_map_t::iterator it = group->mDrawMap.begin(),
  2503. end = group->mDrawMap.end();
  2504. it != end; ++it)
  2505. {
  2506. LLSpatialGroup::drawmap_elem_t& draw_vec = it->second;
  2507. for (U32 i = 0, count = draw_vec.size(); i < count; ++i)
  2508. {
  2509. LLDrawInfo* draw_infop = draw_vec[i];
  2510. if (gPipeline.hasRenderDebugMask(LLPipeline::RENDER_DEBUG_TEXTURE_ANIM))
  2511. {
  2512. renderTextureAnim(draw_infop);
  2513. }
  2514. if (gPipeline.hasRenderDebugMask(LLPipeline::RENDER_DEBUG_BATCH_SIZE))
  2515. {
  2516. renderBatchSize(draw_infop);
  2517. }
  2518. if (!gUsePBRShaders &&
  2519. gPipeline.hasRenderDebugMask(LLPipeline::RENDER_DEBUG_SHADOW_FRUSTA))
  2520. {
  2521. renderShadowFrusta(draw_infop);
  2522. }
  2523. }
  2524. }
  2525. }
  2526. public:
  2527. LLCamera* mCamera;
  2528. };
  2529. class LLOctreeRenderXRay final : public OctreeTraveler
  2530. {
  2531. public:
  2532. LLOctreeRenderXRay(LLCamera* camerap)
  2533. : mCamera(camerap)
  2534. {
  2535. }
  2536. void traverse(const OctreeNode* nodep) override
  2537. {
  2538. LLSpatialGroup* groupp = (LLSpatialGroup*)nodep->getListener(0);
  2539. if (!groupp) return;
  2540. const LLVector4a* bounds = groupp->getBounds();
  2541. if (mCamera && !mCamera->AABBInFrustumNoFarClip(bounds[0], bounds[1]))
  2542. {
  2543. return;
  2544. }
  2545. nodep->accept(this);
  2546. for (U32 i = 0, count = nodep->getChildCount(); i < count; ++i)
  2547. {
  2548. traverse(nodep->getChild(i));
  2549. }
  2550. // Render visibility wireframe
  2551. if (gPipeline.hasRenderDebugMask(LLPipeline::RENDER_DEBUG_OCCLUSION))
  2552. {
  2553. groupp->rebuildGeom();
  2554. groupp->rebuildMesh();
  2555. gGL.flush();
  2556. gGL.pushMatrix();
  2557. gGLLastMatrix = NULL;
  2558. gGL.loadMatrix(gGLModelView);
  2559. renderXRay(groupp);
  2560. gGLLastMatrix = NULL;
  2561. gGL.popMatrix();
  2562. stop_glerror();
  2563. }
  2564. }
  2565. LL_INLINE void visit(const OctreeNode*) override
  2566. {
  2567. }
  2568. public:
  2569. LLCamera* mCamera;
  2570. };
  2571. class LLOctreeStateCheck final : public OctreeTraveler
  2572. {
  2573. protected:
  2574. LOG_CLASS(LLOctreeStateCheck);
  2575. public:
  2576. LLOctreeStateCheck()
  2577. {
  2578. for (U32 i = 0; i < LLViewerCamera::NUM_CAMERAS; ++i)
  2579. {
  2580. mInheritedMask[i] = 0;
  2581. }
  2582. }
  2583. void traverse(const OctreeNode* node) override
  2584. {
  2585. LLSpatialGroup* group = (LLSpatialGroup*)node->getListener(0);
  2586. if (!group) return;
  2587. node->accept(this);
  2588. U32 temp[LLViewerCamera::NUM_CAMERAS];
  2589. for (U32 i = 0; i < LLViewerCamera::NUM_CAMERAS; ++i)
  2590. {
  2591. temp[i] = mInheritedMask[i];
  2592. mInheritedMask[i] |= group->mOcclusionState[i] &
  2593. LLSpatialGroup::OCCLUDED;
  2594. }
  2595. for (U32 i = 0; i < node->getChildCount(); ++i)
  2596. {
  2597. traverse(node->getChild(i));
  2598. }
  2599. for (U32 i = 0; i < LLViewerCamera::NUM_CAMERAS; ++i)
  2600. {
  2601. mInheritedMask[i] = temp[i];
  2602. }
  2603. }
  2604. void visit(const OctreeNode* state) override
  2605. {
  2606. LLSpatialGroup* group = (LLSpatialGroup*)state->getListener(0);
  2607. if (!group) return;
  2608. for (U32 i = 0; i < LLViewerCamera::NUM_CAMERAS; ++i)
  2609. {
  2610. if (mInheritedMask[i] &&
  2611. !(group->mOcclusionState[i] & mInheritedMask[i]))
  2612. {
  2613. llerrs << "Spatial group failed inherited mask test."
  2614. << llendl;
  2615. }
  2616. }
  2617. if (group->hasState(LLSpatialGroup::DIRTY))
  2618. {
  2619. assert_parent_state(group, LLSpatialGroup::DIRTY);
  2620. }
  2621. }
  2622. void assert_parent_state(LLSpatialGroup* group, U32 state)
  2623. {
  2624. LLSpatialGroup* parent = group->getParent();
  2625. while (parent)
  2626. {
  2627. if (!parent->hasState(state))
  2628. {
  2629. llerrs << "Spatial group failed parent state check." << llendl;
  2630. }
  2631. parent = parent->getParent();
  2632. }
  2633. }
  2634. public:
  2635. U32 mInheritedMask[LLViewerCamera::NUM_CAMERAS];
  2636. };
  2637. S32 get_physics_detail(const LLVector3& scale)
  2638. {
  2639. constexpr S32 DEFAULT_DETAIL = 1;
  2640. constexpr F32 LARGE_THRESHOLD = 5.f;
  2641. constexpr F32 MEGA_THRESHOLD = 25.f;
  2642. S32 detail = DEFAULT_DETAIL;
  2643. F32 avg_scale = (scale[0] + scale[1] + scale[2]) / 3.f;
  2644. if (avg_scale > LARGE_THRESHOLD)
  2645. {
  2646. ++detail;
  2647. if (avg_scale > MEGA_THRESHOLD)
  2648. {
  2649. ++detail;
  2650. }
  2651. }
  2652. return detail;
  2653. }
  2654. void renderMeshBaseHull(LLVOVolume* volp, U32 data_mask, LLColor4& color,
  2655. LLColor4& line_color)
  2656. {
  2657. LLUUID mesh_id = volp->getVolume()->getParams().getSculptID();
  2658. LLModel::Decomposition* decompp = gMeshRepo.getDecomposition(mesh_id);
  2659. static const LLVector3 size(0.25f, 0.25f, 0.25f);
  2660. if (decompp)
  2661. {
  2662. if (!decompp->mBaseHullMesh.empty())
  2663. {
  2664. gGL.diffuseColor4fv(color.mV);
  2665. LLVertexBuffer::drawArrays(LLRender::TRIANGLES,
  2666. decompp->mBaseHullMesh.mPositions);
  2667. if (gUsePBRShaders)
  2668. {
  2669. return;
  2670. }
  2671. glPolygonMode(GL_FRONT_AND_BACK, GL_LINE);
  2672. gGL.diffuseColor4fv(line_color.mV);
  2673. LLVertexBuffer::drawArrays(LLRender::TRIANGLES,
  2674. decompp->mBaseHullMesh.mPositions);
  2675. glPolygonMode(GL_FRONT_AND_BACK, GL_FILL);
  2676. }
  2677. else
  2678. {
  2679. gMeshRepo.buildPhysicsMesh(*decompp);
  2680. gGL.diffuseColor4f(0.f, 1.f, 1.f, 1.f);
  2681. gl_draw_box_outline(LLVector3::zero, size);
  2682. }
  2683. }
  2684. else
  2685. {
  2686. gGL.diffuseColor3f(1.f, 0.f, 1.f);
  2687. gl_draw_box_outline(LLVector3::zero, size);
  2688. }
  2689. }
  2690. void render_hull(LLModel::PhysicsMesh& mesh, const LLColor4& color,
  2691. const LLColor4& line_color)
  2692. {
  2693. if (mesh.mPositions.empty() || mesh.mNormals.empty())
  2694. {
  2695. return;
  2696. }
  2697. gGL.diffuseColor4fv(color.mV);
  2698. LLVertexBuffer::drawArrays(LLRender::TRIANGLES, mesh.mPositions);
  2699. if (gUsePBRShaders)
  2700. {
  2701. // Outlines removed from LL's PBR viewer. *TODO: check and see if there
  2702. // is any valid reason to remove them... HB
  2703. return;
  2704. }
  2705. glPolygonMode(GL_FRONT_AND_BACK, GL_LINE);
  2706. gGL.lineWidth(3.f);
  2707. gGL.diffuseColor4fv(line_color.mV);
  2708. LLVertexBuffer::drawArrays(LLRender::TRIANGLES, mesh.mPositions);
  2709. gGL.lineWidth(1.f);
  2710. glPolygonMode(GL_FRONT_AND_BACK, GL_FILL);
  2711. }
  2712. void renderPhysicsShape(LLVOVolume* volp, bool wireframe)
  2713. {
  2714. U8 phys_type = volp->getPhysicsShapeType();
  2715. if (phys_type == LLViewerObject::PHYSICS_SHAPE_NONE || volp->isFlexible())
  2716. {
  2717. return;
  2718. }
  2719. // Not allowed to return at this point without rendering *something*
  2720. static LLCachedControl<F32> threshold(gSavedSettings,
  2721. "ObjectCostHighThreshold");
  2722. F32 cost = volp->getObjectCost();
  2723. static LLCachedControl<LLColor4> low(gSavedSettings, "ObjectCostLowColor");
  2724. static LLCachedControl<LLColor4> mid(gSavedSettings, "ObjectCostMidColor");
  2725. static LLCachedControl<LLColor4> high(gSavedSettings,
  2726. "ObjectCostHighColor");
  2727. F32 normalized_cost = 1.f - expf(-cost / threshold);
  2728. LLColor4 color;
  2729. if (normalized_cost <= 0.5f)
  2730. {
  2731. color = lerp(low, mid, 2.f * normalized_cost);
  2732. }
  2733. else
  2734. {
  2735. color = lerp(mid, high, 2.f * (normalized_cost - 0.5f));
  2736. }
  2737. if (wireframe)
  2738. {
  2739. color *= 0.5f;
  2740. }
  2741. LLColor4 line_color = color * 0.5f;
  2742. U32 data_mask = LLVertexBuffer::MAP_VERTEX;
  2743. LLVolumeParams vol_params = volp->getVolume()->getParams();
  2744. bool convex = phys_type == LLViewerObject::PHYSICS_SHAPE_CONVEX_HULL;
  2745. LLPhysicsVolumeParams phys_params(vol_params, convex);
  2746. bool has_decomp = false;
  2747. static LLCachedControl<bool> hide_convex(gSavedSettings,
  2748. "HideConvexPhysShapes");
  2749. if (hide_convex)
  2750. {
  2751. const LLUUID& mesh_id = vol_params.getSculptID();
  2752. LLModel::Decomposition* decomp = gMeshRepo.getDecomposition(mesh_id);
  2753. has_decomp = decomp && !decomp->mHull.empty();
  2754. }
  2755. LLPhysShapeBuilderUtil::ShapeSpec physics_spec;
  2756. LLPhysShapeBuilderUtil::getPhysShape(phys_params, volp->getScale(),
  2757. has_decomp, physics_spec);
  2758. U32 type = physics_spec.getType();
  2759. LLVector3 size(0.25f, 0.25f, 0.25f);
  2760. gGL.pushMatrix();
  2761. gGL.multMatrix(volp->getRelativeXform().getF32ptr());
  2762. LLGLEnable(gUsePBRShaders ? 0 : GL_POLYGON_OFFSET_LINE);
  2763. if (!gUsePBRShaders)
  2764. {
  2765. glPolygonOffset(3.f, 3.f);
  2766. }
  2767. if (type == LLPhysShapeBuilderUtil::ShapeSpec::USER_MESH)
  2768. {
  2769. const LLUUID& mesh_id = volp->getVolume()->getParams().getSculptID();
  2770. LLModel::Decomposition* decomp = gMeshRepo.getDecomposition(mesh_id);
  2771. if (decomp)
  2772. {
  2773. // Render a physics based mesh
  2774. gGL.getTexUnit(0)->unbind(LLTexUnit::TT_TEXTURE);
  2775. if (!decomp->mHull.empty())
  2776. {
  2777. // Decomposition exists, use that
  2778. if (decomp->mMesh.empty())
  2779. {
  2780. gMeshRepo.buildPhysicsMesh(*decomp);
  2781. }
  2782. for (U32 i = 0; i < decomp->mMesh.size(); ++i)
  2783. {
  2784. render_hull(decomp->mMesh[i], color, line_color);
  2785. }
  2786. }
  2787. else if (!decomp->mPhysicsShapeMesh.empty())
  2788. {
  2789. // Decomp has physics mesh, render that mesh
  2790. gGL.diffuseColor4fv(color.mV);
  2791. const auto& positions = decomp->mPhysicsShapeMesh.mPositions;
  2792. LLVertexBuffer::drawArrays(LLRender::TRIANGLES, positions);
  2793. if (!gUsePBRShaders)
  2794. {
  2795. glPolygonMode(GL_FRONT_AND_BACK, GL_LINE);
  2796. gGL.diffuseColor4fv(line_color.mV);
  2797. LLVertexBuffer::drawArrays(LLRender::TRIANGLES, positions);
  2798. glPolygonMode(GL_FRONT_AND_BACK, GL_FILL);
  2799. }
  2800. }
  2801. else
  2802. {
  2803. // No mesh or decomposition, render base hull
  2804. renderMeshBaseHull(volp, data_mask, color, line_color);
  2805. if (decomp->mPhysicsShapeMesh.empty())
  2806. {
  2807. // Attempt to fetch physics shape mesh if available
  2808. gMeshRepo.fetchPhysicsShape(mesh_id);
  2809. }
  2810. }
  2811. }
  2812. else
  2813. {
  2814. gGL.diffuseColor3f(1.f, 1.f, 0.f);
  2815. gl_draw_box_outline(LLVector3::zero, size);
  2816. }
  2817. }
  2818. else if (type == LLPhysShapeBuilderUtil::ShapeSpec::USER_CONVEX ||
  2819. type == LLPhysShapeBuilderUtil::ShapeSpec::PRIM_CONVEX)
  2820. {
  2821. if (volp->isMesh())
  2822. {
  2823. renderMeshBaseHull(volp, data_mask, color, line_color);
  2824. }
  2825. else
  2826. {
  2827. LLVolumeParams vol_params = volp->getVolume()->getParams();
  2828. S32 detail = get_physics_detail(volp->getScale());
  2829. LLVolume* phys_volp = gVolumeMgrp->refVolume(vol_params, detail);
  2830. if (!phys_volp->mHullPoints)
  2831. {
  2832. // Build convex hull
  2833. std::vector<LLVector3> pos;
  2834. std::vector<U16> index;
  2835. S32 index_offset = 0;
  2836. for (S32 i = 0, count = phys_volp->getNumVolumeFaces();
  2837. i < count; ++i)
  2838. {
  2839. const LLVolumeFace& face = phys_volp->getVolumeFace(i);
  2840. if (index_offset + face.mNumVertices > 65535)
  2841. {
  2842. continue;
  2843. }
  2844. for (S32 j = 0; j < face.mNumVertices; ++j)
  2845. {
  2846. pos.emplace_back(face.mPositions[j].getF32ptr());
  2847. }
  2848. for (S32 j = 0; j < face.mNumIndices; ++j)
  2849. {
  2850. index.push_back(face.mIndices[j] + index_offset);
  2851. }
  2852. index_offset += face.mNumVertices;
  2853. }
  2854. LLConvexDecomposition* decomp =
  2855. LLConvexDecomposition::getInstance();
  2856. if (decomp && !pos.empty() && !index.empty())
  2857. {
  2858. LLCDMeshData mesh;
  2859. mesh.mIndexBase = &index[0];
  2860. mesh.mVertexBase = pos[0].mV;
  2861. mesh.mNumVertices = pos.size();
  2862. mesh.mVertexStrideBytes = 12;
  2863. mesh.mIndexStrideBytes = 6;
  2864. mesh.mIndexType = LLCDMeshData::INT_16;
  2865. mesh.mNumTriangles = index.size() / 3;
  2866. LLCDMeshData res;
  2867. decomp->generateSingleHullMeshFromMesh(&mesh, &res);
  2868. // Copy res into phys_volp
  2869. phys_volp->mHullPoints =
  2870. (LLVector4a*)allocate_volume_mem(sizeof(LLVector4a) *
  2871. res.mNumVertices);
  2872. if (!phys_volp->mHullPoints)
  2873. {
  2874. gVolumeMgrp->unrefVolume(phys_volp);
  2875. gGL.popMatrix();
  2876. return;
  2877. }
  2878. phys_volp->mNumHullPoints = res.mNumVertices;
  2879. S32 idx_size = (res.mNumTriangles * 3 * 2 + 0xF) & ~0xF;
  2880. phys_volp->mHullIndices =
  2881. (U16*)allocate_volume_mem(idx_size);
  2882. if (!phys_volp->mHullIndices)
  2883. {
  2884. free_volume_mem(phys_volp->mHullPoints);
  2885. gVolumeMgrp->unrefVolume(phys_volp);
  2886. gGL.popMatrix();
  2887. return;
  2888. }
  2889. phys_volp->mNumHullIndices = res.mNumTriangles * 3;
  2890. const F32* v = res.mVertexBase;
  2891. for (S32 i = 0; i < res.mNumVertices; ++i)
  2892. {
  2893. F32* p = (F32*)((U8*)v+i*res.mVertexStrideBytes);
  2894. phys_volp->mHullPoints[i].load3(p);
  2895. }
  2896. if (res.mIndexType == LLCDMeshData::INT_16)
  2897. {
  2898. for (S32 i = 0; i < res.mNumTriangles; ++i)
  2899. {
  2900. U16* idx = (U16*)(((U8*)res.mIndexBase) +
  2901. i * res.mIndexStrideBytes);
  2902. phys_volp->mHullIndices[i * 3] = idx[0];
  2903. phys_volp->mHullIndices[i * 3 + 1] = idx[1];
  2904. phys_volp->mHullIndices[i * 3 + 2] = idx[2];
  2905. }
  2906. }
  2907. else
  2908. {
  2909. for (S32 i = 0; i < res.mNumTriangles; ++i)
  2910. {
  2911. U32* idx = (U32*)(((U8*)res.mIndexBase) +
  2912. i * res.mIndexStrideBytes);
  2913. phys_volp->mHullIndices[i * 3] = (U16)idx[0];
  2914. phys_volp->mHullIndices[i * 3 + 1] = (U16)idx[1];
  2915. phys_volp->mHullIndices[i * 3 + 2] = (U16)idx[2];
  2916. }
  2917. }
  2918. }
  2919. }
  2920. if (phys_volp->mHullPoints && phys_volp->mNumHullIndices &&
  2921. phys_volp->mHullIndices && phys_volp->mNumHullPoints)
  2922. {
  2923. // Render hull
  2924. if (!gUsePBRShaders)
  2925. {
  2926. glPolygonMode(GL_FRONT_AND_BACK, GL_LINE);
  2927. }
  2928. gGL.diffuseColor4fv(line_color.mV);
  2929. LLVertexBuffer::unbind();
  2930. LLVertexBuffer::drawElements(phys_volp->mNumHullPoints,
  2931. phys_volp->mHullPoints, NULL,
  2932. phys_volp->mNumHullIndices,
  2933. phys_volp->mHullIndices);
  2934. if (!gUsePBRShaders)
  2935. {
  2936. gGL.diffuseColor4fv(color.mV);
  2937. glPolygonMode(GL_FRONT_AND_BACK, GL_FILL);
  2938. LLVertexBuffer::drawElements(phys_volp->mNumHullPoints,
  2939. phys_volp->mHullPoints, NULL,
  2940. phys_volp->mNumHullIndices,
  2941. phys_volp->mHullIndices);
  2942. }
  2943. }
  2944. else
  2945. {
  2946. gGL.diffuseColor4f(1.f, 0.1f, 1.f, 1.f);
  2947. gl_draw_box_outline(LLVector3::zero, size);
  2948. }
  2949. free_volume_mem(phys_volp->mHullPoints);
  2950. free_volume_mem(phys_volp->mHullIndices);
  2951. gVolumeMgrp->unrefVolume(phys_volp);
  2952. }
  2953. }
  2954. else if (type == LLPhysShapeBuilderUtil::ShapeSpec::BOX)
  2955. {
  2956. if (!wireframe)
  2957. {
  2958. LLVector3 center = physics_spec.getCenter();
  2959. LLVector3 scale = physics_spec.getScale();
  2960. LLVector3 vscale = volp->getScale() * 2.f;
  2961. scale.set(scale[0] / vscale[0], scale[1] / vscale[1],
  2962. scale[2] / vscale[2]);
  2963. gGL.diffuseColor4fv(color.mV);
  2964. // Used to be called in drawBox(), now replaced with gl_draw_box()
  2965. // which does not call this. HB
  2966. LLVertexBuffer::unbind();
  2967. gl_draw_box(center, scale);
  2968. }
  2969. }
  2970. else if (type == LLPhysShapeBuilderUtil::ShapeSpec::SPHERE)
  2971. {
  2972. if (!wireframe)
  2973. {
  2974. LLVolumeParams vol_params;
  2975. vol_params.setType(LL_PCODE_PROFILE_CIRCLE_HALF,
  2976. LL_PCODE_PATH_CIRCLE);
  2977. vol_params.setBeginAndEndS(0.f, 1.f);
  2978. vol_params.setBeginAndEndT(0.f, 1.f);
  2979. vol_params.setRatio(1.f, 1.f);
  2980. vol_params.setShear(0.f, 0.f);
  2981. LLVolume* spherep = gVolumeMgrp->refVolume(vol_params, 3);
  2982. gGL.diffuseColor4fv(color.mV);
  2983. pushVerts(spherep);
  2984. gVolumeMgrp->unrefVolume(spherep);
  2985. }
  2986. }
  2987. else if (type == LLPhysShapeBuilderUtil::ShapeSpec::CYLINDER)
  2988. {
  2989. if (!wireframe)
  2990. {
  2991. LLVolumeParams vol_params;
  2992. vol_params.setType(LL_PCODE_PROFILE_CIRCLE, LL_PCODE_PATH_LINE);
  2993. vol_params.setBeginAndEndS(0.f, 1.f);
  2994. vol_params.setBeginAndEndT(0.f, 1.f);
  2995. vol_params.setRatio(1.f, 1.f);
  2996. vol_params.setShear(0.f, 0.f);
  2997. LLVolume* cylinderp = gVolumeMgrp->refVolume(vol_params, 3);
  2998. gGL.diffuseColor4fv(color.mV);
  2999. pushVerts(cylinderp);
  3000. gVolumeMgrp->unrefVolume(cylinderp);
  3001. }
  3002. }
  3003. else if (type == LLPhysShapeBuilderUtil::ShapeSpec::PRIM_MESH)
  3004. {
  3005. LLVolumeParams vol_params = volp->getVolume()->getParams();
  3006. S32 detail = get_physics_detail(volp->getScale());
  3007. LLVolume* phys_volp = gVolumeMgrp->refVolume(vol_params, detail);
  3008. gGL.diffuseColor4fv(line_color.mV);
  3009. pushVerts(phys_volp);
  3010. if (!gUsePBRShaders)
  3011. {
  3012. gGL.diffuseColor4fv(color.mV);
  3013. glPolygonMode(GL_FRONT_AND_BACK, GL_FILL);
  3014. pushVerts(phys_volp);
  3015. }
  3016. gVolumeMgrp->unrefVolume(phys_volp);
  3017. }
  3018. else if (type == LLPhysShapeBuilderUtil::ShapeSpec::PRIM_CONVEX)
  3019. {
  3020. LLVolumeParams vol_params = volp->getVolume()->getParams();
  3021. S32 detail = get_physics_detail(volp->getScale());
  3022. LLVolume* phys_volp = gVolumeMgrp->refVolume(vol_params, detail);
  3023. if (phys_volp->mHullPoints && phys_volp->mHullIndices)
  3024. {
  3025. if (gUsePBRShaders)
  3026. {
  3027. gGL.diffuseColor4fv(color.mV);
  3028. LLVertexBuffer::unbind();
  3029. glVertexPointer(3, GL_FLOAT, 16, phys_volp->mHullPoints);
  3030. gGL.diffuseColor4fv(line_color.mV);
  3031. gGL.syncMatrices();
  3032. glDrawElements(GL_TRIANGLES, phys_volp->mNumHullIndices,
  3033. GL_UNSIGNED_SHORT, phys_volp->mHullIndices);
  3034. }
  3035. else
  3036. {
  3037. glPolygonMode(GL_FRONT_AND_BACK, GL_LINE);
  3038. gGL.diffuseColor4fv(line_color.mV);
  3039. glPolygonMode(GL_FRONT_AND_BACK, GL_LINE);
  3040. LLVertexBuffer::drawElements(phys_volp->mNumHullPoints,
  3041. phys_volp->mHullPoints, NULL,
  3042. phys_volp->mNumHullIndices,
  3043. phys_volp->mHullIndices);
  3044. gGL.diffuseColor4fv(color.mV);
  3045. glPolygonMode(GL_FRONT_AND_BACK, GL_FILL);
  3046. LLVertexBuffer::drawElements(phys_volp->mNumHullPoints,
  3047. phys_volp->mHullPoints, NULL,
  3048. phys_volp->mNumHullIndices,
  3049. phys_volp->mHullIndices);
  3050. }
  3051. }
  3052. else
  3053. {
  3054. gGL.diffuseColor3f(1.f, 0.f, 1.f);
  3055. gl_draw_box_outline(LLVector3::zero, size);
  3056. gMeshRepo.buildHull(vol_params, detail);
  3057. }
  3058. gVolumeMgrp->unrefVolume(phys_volp);
  3059. }
  3060. else if (type == LLPhysShapeBuilderUtil::ShapeSpec::SCULPT)
  3061. {
  3062. // *TODO: implement sculpted prim physics display
  3063. }
  3064. else
  3065. {
  3066. llerrs << "Unhandled type" << llendl;
  3067. }
  3068. gGL.popMatrix();
  3069. }
  3070. void renderPhysicsShapes(LLSpatialGroup* groupp, bool wireframe)
  3071. {
  3072. const LLSpatialGroup::element_list& data_vec = groupp->getData();
  3073. for (U32 i = 0, count = data_vec.size(); i < count; ++i)
  3074. {
  3075. LLDrawable* drawp = (LLDrawable*)data_vec[i]->getDrawable();
  3076. if (!drawp || drawp->isDead()) continue;
  3077. LLSpatialPartition* partp = drawp->asPartition();
  3078. if (partp)
  3079. {
  3080. LLSpatialBridge* bridgep = drawp->asPartition()->asBridge();
  3081. if (bridgep && bridgep->mDrawable.notNull())
  3082. {
  3083. gGL.pushMatrix();
  3084. gGL.multMatrix(bridgep->mDrawable->getRenderMatrix().getF32ptr());
  3085. bridgep->renderPhysicsShapes(wireframe);
  3086. gGL.popMatrix();
  3087. }
  3088. continue;
  3089. }
  3090. LLVOVolume* volp = drawp->getVOVolume();
  3091. if (volp && !volp->isAttachment() &&
  3092. volp->getPhysicsShapeType() != LLViewerObject::PHYSICS_SHAPE_NONE)
  3093. {
  3094. if (groupp->getSpatialPartition()->isBridge())
  3095. {
  3096. renderPhysicsShape(volp, wireframe);
  3097. }
  3098. else
  3099. {
  3100. gGL.pushMatrix();
  3101. LLVector3 trans = drawp->getRegion()->getOriginAgent();
  3102. gGL.translatef(trans.mV[0], trans.mV[1], trans.mV[2]);
  3103. renderPhysicsShape(volp, wireframe);
  3104. gGL.popMatrix();
  3105. }
  3106. continue;
  3107. }
  3108. // Terrain physics shape not rendered by LL's PBR viewer
  3109. if (gUsePBRShaders)
  3110. {
  3111. return;
  3112. }
  3113. LLViewerObject* objp = drawp->getVObj();
  3114. if (objp && objp->getPCode() == LLViewerObject::LL_VO_SURFACE_PATCH)
  3115. {
  3116. gGL.pushMatrix();
  3117. gGL.multMatrix(objp->getRegion()->mRenderMatrix.getF32ptr());
  3118. // Push face vertices for terrain
  3119. for (S32 j = 0, count = drawp->getNumFaces(); j < count; ++j)
  3120. {
  3121. LLFace* facep = drawp->getFace(j);
  3122. if (!facep) continue;
  3123. LLVertexBuffer* buffp = facep->getVertexBuffer();
  3124. if (!buffp) continue;
  3125. glPolygonMode(GL_FRONT_AND_BACK, GL_LINE);
  3126. buffp->setBuffer(LLVertexBuffer::MAP_VERTEX);
  3127. gGL.diffuseColor3f(0.2f, 0.5f, 0.3f);
  3128. buffp->draw(LLRender::TRIANGLES, buffp->getNumIndices(), 0);
  3129. gGL.diffuseColor3f(0.2f, 1.f, 0.3f);
  3130. glPolygonMode(GL_FRONT_AND_BACK, GL_FILL);
  3131. buffp->draw(LLRender::TRIANGLES, buffp->getNumIndices(), 0);
  3132. gGL.popMatrix();
  3133. }
  3134. }
  3135. }
  3136. }
  3137. class LLOctreeRenderPhysicsShapes final : public OctreeTraveler
  3138. {
  3139. public:
  3140. LLOctreeRenderPhysicsShapes(LLCamera* camerap, bool wireframe)
  3141. : mCamera(camerap),
  3142. mWireframe(wireframe)
  3143. {
  3144. }
  3145. void traverse(const OctreeNode* nodep) override
  3146. {
  3147. LLSpatialGroup* groupp = (LLSpatialGroup*)nodep->getListener(0);
  3148. if (!groupp) return;
  3149. const LLVector4a* bounds = groupp->getBounds();
  3150. if (!mCamera ||
  3151. mCamera->AABBInFrustumNoFarClip(bounds[0], bounds[1]))
  3152. {
  3153. nodep->accept(this);
  3154. for (U32 i = 0; i < nodep->getChildCount(); ++i)
  3155. {
  3156. traverse(nodep->getChild(i));
  3157. }
  3158. groupp->rebuildGeom();
  3159. groupp->rebuildMesh();
  3160. renderPhysicsShapes(groupp, mWireframe);
  3161. }
  3162. }
  3163. LL_INLINE void visit(const OctreeNode*) override
  3164. {
  3165. }
  3166. public:
  3167. LLCamera* mCamera;
  3168. bool mWireframe;
  3169. };
  3170. void LLSpatialPartition::renderPhysicsShapes(bool wireframe)
  3171. {
  3172. gGL.flush();
  3173. gGL.getTexUnit(0)->unbind(LLTexUnit::TT_TEXTURE);
  3174. if (!gUsePBRShaders)
  3175. {
  3176. wireframe = false;
  3177. gGL.lineWidth(3.f);
  3178. }
  3179. LLCamera* camerap = isBridge() ? NULL : &gViewerCamera;
  3180. LLOctreeRenderPhysicsShapes render_physics(camerap, wireframe);
  3181. render_physics.traverse(mOctree);
  3182. gGL.flush();
  3183. if (!gUsePBRShaders)
  3184. {
  3185. gGL.lineWidth(1.f);
  3186. }
  3187. }
  3188. void LLSpatialPartition::renderDebug()
  3189. {
  3190. if (!gPipeline.hasRenderDebugMask(LLPipeline::RENDER_DEBUG_OCTREE |
  3191. LLPipeline::RENDER_DEBUG_OCCLUSION |
  3192. LLPipeline::RENDER_DEBUG_LIGHTS |
  3193. LLPipeline::RENDER_DEBUG_BATCH_SIZE |
  3194. LLPipeline::RENDER_DEBUG_UPDATE_TYPE |
  3195. LLPipeline::RENDER_DEBUG_BBOXES |
  3196. LLPipeline::RENDER_DEBUG_NORMALS |
  3197. LLPipeline::RENDER_DEBUG_POINTS |
  3198. LLPipeline::RENDER_DEBUG_TEXTURE_PRIORITY |
  3199. LLPipeline::RENDER_DEBUG_TEXTURE_ANIM |
  3200. LLPipeline::RENDER_DEBUG_RAYCAST |
  3201. LLPipeline::RENDER_DEBUG_AVATAR_VOLUME |
  3202. LLPipeline::RENDER_DEBUG_AVATAR_JOINTS |
  3203. LLPipeline::RENDER_DEBUG_AGENT_TARGET |
  3204. LLPipeline::RENDER_DEBUG_SHADOW_FRUSTA |
  3205. LLPipeline::RENDER_DEBUG_RENDER_COMPLEXITY))
  3206. {
  3207. return;
  3208. }
  3209. //MK
  3210. if (gRLenabled && gRLInterface.mVisionRestricted)
  3211. {
  3212. return;
  3213. }
  3214. //mk
  3215. gDebugProgram.bind();
  3216. if (gPipeline.hasRenderDebugMask(LLPipeline::RENDER_DEBUG_TEXTURE_PRIORITY))
  3217. {
  3218. sLastMaxTexPriority = (F32)gViewerCamera.getScreenPixelArea();
  3219. sCurMaxTexPriority = 0.f;
  3220. }
  3221. LLGLDisable cullface(GL_CULL_FACE);
  3222. LLGLEnable blend(GL_BLEND);
  3223. gGL.setSceneBlendType(LLRender::BT_ALPHA);
  3224. gGL.getTexUnit(0)->unbind(LLTexUnit::TT_TEXTURE);
  3225. gPipeline.disableLights();
  3226. LLCamera* camerap = isBridge() ? NULL : &gViewerCamera;
  3227. LLOctreeStateCheck checker;
  3228. checker.traverse(mOctree);
  3229. LLOctreeRenderNonOccluded render_debug(camerap);
  3230. render_debug.traverse(mOctree);
  3231. if (gPipeline.hasRenderDebugMask(LLPipeline::RENDER_DEBUG_OCCLUSION))
  3232. {
  3233. LLGLEnable cull(GL_CULL_FACE);
  3234. LLGLEnable blend(GL_BLEND);
  3235. LLGLDepthTest depth_under(GL_TRUE, GL_FALSE, GL_GREATER);
  3236. glPolygonMode(GL_FRONT_AND_BACK, GL_LINE);
  3237. gGL.diffuseColor4f(0.5f, 0.f, 0.f, 0.25f);
  3238. LLGLEnable offset(GL_POLYGON_OFFSET_LINE);
  3239. glPolygonOffset(-1.f, -1.f);
  3240. LLOctreeRenderXRay xray(camerap);
  3241. xray.traverse(mOctree);
  3242. glPolygonMode(GL_FRONT_AND_BACK, GL_FILL);
  3243. }
  3244. gDebugProgram.unbind();
  3245. stop_glerror();
  3246. }
  3247. void LLSpatialGroup::drawObjectBox(LLColor4 col)
  3248. {
  3249. gGL.diffuseColor4fv(col.mV);
  3250. LLVector4a size;
  3251. size = mObjectBounds[1];
  3252. size.mul(1.01f);
  3253. size.add(LLVector4a(0.001f));
  3254. // Used to be called in drawBox(), now replaced with gl_draw_box() which
  3255. // does not call this. HB
  3256. LLVertexBuffer::unbind();
  3257. gl_draw_box(mObjectBounds[0], size);
  3258. }
  3259. bool LLSpatialPartition::isHUDPartition()
  3260. {
  3261. return mPartitionType == LLViewerRegion::PARTITION_HUD;
  3262. }
  3263. bool LLSpatialPartition::isVisible(const LLVector3& v)
  3264. {
  3265. return gViewerCamera.sphereInFrustum(v, 4.f) != 0;
  3266. }
  3267. LLDrawable* LLSpatialPartition::lineSegmentIntersect(const LLVector4a& start,
  3268. const LLVector4a& end,
  3269. bool pick_transparent,
  3270. bool pick_rigged,
  3271. S32* face_hit,
  3272. LLVector4a* intersection,
  3273. LLVector2* tex_coord,
  3274. LLVector4a* normal,
  3275. LLVector4a* tangent)
  3276. {
  3277. LLOctreeIntersect intersect(start, end, pick_transparent, pick_rigged,
  3278. face_hit, intersection, tex_coord, normal,
  3279. tangent);
  3280. return intersect.check(mOctree);
  3281. }
  3282. ///////////////////////////////////////////////////////////////////////////////
  3283. // LLDrawInfo class
  3284. ///////////////////////////////////////////////////////////////////////////////
  3285. LLDrawInfo::LLDrawInfo(U16 start, U16 end, U32 count, U32 offset,
  3286. LLViewerTexture* texp, LLVertexBuffer* bufferp,
  3287. bool fullbright, U8 bump)
  3288. : mVertexBuffer(bufferp),
  3289. mTexture(texp),
  3290. mTextureMatrix(NULL),
  3291. mModelMatrix(NULL),
  3292. mStart(start),
  3293. mEnd(end),
  3294. mCount(count),
  3295. mOffset(offset),
  3296. mFullbright(fullbright),
  3297. mBump(bump),
  3298. mVSize(0.f),
  3299. mDistance(0.f),
  3300. mBlendFuncSrc(LLRender::BF_SOURCE_ALPHA),
  3301. mBlendFuncDst(LLRender::BF_ONE_MINUS_SOURCE_ALPHA),
  3302. mHasGlow(false),
  3303. mMaterial(NULL),
  3304. mShaderMask(0),
  3305. mSpecColor(1.f, 1.f, 1.f, 0.5f),
  3306. mEnvIntensity(0.f),
  3307. mDiffuseAlphaMode(0),
  3308. mAlphaMaskCutoff(0.5f),
  3309. mIsAgentAttachment(false),
  3310. mHasALM(false),
  3311. mHasPBR(false),
  3312. mHasLegacyFallback(false),
  3313. mHasBasecolorFallback(false)
  3314. {
  3315. if (gDebugGL)
  3316. {
  3317. mVertexBuffer->validateRange(mStart, mEnd, mCount, mOffset);
  3318. }
  3319. }
  3320. LLDrawInfo::~LLDrawInfo()
  3321. {
  3322. #if LL_DEBUG
  3323. if (gDebugGL)
  3324. {
  3325. gPipeline.checkReferences(this);
  3326. }
  3327. #endif
  3328. }
  3329. void LLDrawInfo::validate()
  3330. {
  3331. if (!mVertexBuffer->validateRange(mStart, mEnd, mCount, mOffset))
  3332. {
  3333. llwarns << "Invalid range !" << llendl;
  3334. }
  3335. }
  3336. U64 LLDrawInfo::getSkinHash()
  3337. {
  3338. return mSkinInfo.notNull() ? mSkinInfo->mHash : 0;
  3339. }
  3340. LLColor4U LLDrawInfo::getDebugColor()
  3341. {
  3342. if (mDebugColor != LLColor4U::black)
  3343. {
  3344. // When the debug color has already been computed once, we use the
  3345. // cached value to speed up rendering (even though this is just a
  3346. // debug render feature) since we do not care whether the draw info
  3347. // parameters changed or not (in the past, the debug color was picked
  3348. // up at random in the LLDrawInfo() constructor). HB
  3349. return mDebugColor;
  3350. }
  3351. constexpr F32 DEBUG_COLOR_ALPHA = 160.f; // Used to be 200. HB
  3352. // *HACK: hash the bytes of this object but do not include the ref count
  3353. constexpr size_t offset = sizeof(LLRefCount);
  3354. U64 digest = HBXXH64::digest((const void*)((const char*)this + offset),
  3355. sizeof(*this) - offset);
  3356. *((U32*)mDebugColor.mV) = digest64to32(digest);
  3357. mDebugColor.mV[3] = DEBUG_COLOR_ALPHA;
  3358. return mDebugColor;
  3359. }
  3360. ///////////////////////////////////////////////////////////////////////////////
  3361. // LLGeometryManager class
  3362. ///////////////////////////////////////////////////////////////////////////////
  3363. LLVertexBuffer* LLGeometryManager::createVertexBuffer(U32 type_mask)
  3364. {
  3365. #if LL_DEBUG_VB_ALLOC
  3366. LLVertexBuffer* vb = new LLVertexBuffer(type_mask);
  3367. vb->setOwner(llformat("LLGeometryManager type %d", type_mask).c_str());
  3368. return vb;
  3369. #else
  3370. return new LLVertexBuffer(type_mask);
  3371. #endif
  3372. }
  3373. ///////////////////////////////////////////////////////////////////////////////
  3374. // LLCullResult class
  3375. ///////////////////////////////////////////////////////////////////////////////
  3376. void LLCullResult::clear()
  3377. {
  3378. mVisibleGroups.clear();
  3379. mAlphaGroups.clear();
  3380. mRiggedAlphaGroups.clear();
  3381. mOcclusionGroups.clear();
  3382. mDrawableGroups.clear();
  3383. mVisibleList.clear();
  3384. mVisibleBridge.clear();
  3385. for (U32 i = 0; i < LLRenderPass::NUM_RENDER_TYPES; ++i)
  3386. {
  3387. mRenderMap[i].clear();
  3388. }
  3389. }
  3390. void LLCullResult::pushDrawInfo(U32 type, LLDrawInfo* infop)
  3391. {
  3392. if (infop && type < LLRenderPass::NUM_RENDER_TYPES)
  3393. {
  3394. mRenderMap[type].push_back(infop);
  3395. }
  3396. }
  3397. void LLCullResult::assertDrawMapsEmpty()
  3398. {
  3399. for (U32 i = 0; i < LLRenderPass::NUM_RENDER_TYPES; ++i)
  3400. {
  3401. if (hasRenderMap(i))
  3402. {
  3403. llerrs << "Stale LLDrawInfo's in LLCullResult !" << llendl;
  3404. }
  3405. }
  3406. }
  3407. ///////////////////////////////////////////////////////////////////////////////
  3408. // Used by LLSpatialBridge
  3409. class LLOctreeMarkNotCulled final : public OctreeTraveler
  3410. {
  3411. public:
  3412. LLOctreeMarkNotCulled(LLCamera* camerap)
  3413. : mCamera(camerap)
  3414. {
  3415. }
  3416. void traverse(const OctreeNode* nodep) override
  3417. {
  3418. if (!nodep)
  3419. {
  3420. llwarns_sparse << "NULL node ! Skipping..." << llendl;
  3421. llassert(false);
  3422. return;
  3423. }
  3424. LLSpatialGroup* groupp = (LLSpatialGroup*)nodep->getListener(0);
  3425. if (!groupp)
  3426. {
  3427. llwarns_once << "NULL satial group for node " << nodep
  3428. << " ! Skipping..." << llendl;
  3429. llassert(false);
  3430. return;
  3431. }
  3432. groupp->setVisible();
  3433. OctreeTraveler::traverse(nodep);
  3434. }
  3435. void visit(const OctreeNode* branchp) override
  3436. {
  3437. if (!branchp)
  3438. {
  3439. llwarns_sparse << "NULL branch ! Skipping..." << llendl;
  3440. llassert(false);
  3441. return;
  3442. }
  3443. gPipeline.markNotCulled((LLSpatialGroup*)branchp->getListener(0),
  3444. *mCamera);
  3445. }
  3446. public:
  3447. LLCamera* mCamera;
  3448. };
  3449. ///////////////////////////////////////////////////////////////////////////////
  3450. // LLSpatialBridge class. Spatial partition bridging drawable.
  3451. // Used to be in lldrawable.cpp, which was silly since it is declared in
  3452. // llspatialpartition.h. HB
  3453. ///////////////////////////////////////////////////////////////////////////////
  3454. #define FORCE_INVISIBLE_AREA 16.f
  3455. LLSpatialBridge::LLSpatialBridge(LLDrawable* rootp, bool render_by_group,
  3456. U32 data_mask, LLViewerRegion* regionp)
  3457. : LLDrawable(rootp->getVObj(), true),
  3458. LLSpatialPartition(data_mask, render_by_group, regionp)
  3459. {
  3460. llassert(rootp && rootp->getRegion());
  3461. mOcclusionEnabled = false;
  3462. mBridge = this;
  3463. mDrawable = rootp;
  3464. rootp->setSpatialBridge(this);
  3465. mRenderType = mDrawable->mRenderType;
  3466. mDrawableType = mDrawable->mRenderType;
  3467. mPartitionType = LLViewerRegion::PARTITION_VOLUME;
  3468. mOctree->balance();
  3469. LLSpatialPartition* part =
  3470. mDrawable->getRegion()->getSpatialPartition(mPartitionType);
  3471. // PARTITION_VOLUME cannot be NULL
  3472. llassert(part);
  3473. part->put(this);
  3474. }
  3475. LLSpatialBridge::~LLSpatialBridge()
  3476. {
  3477. if (mEntry)
  3478. {
  3479. LLSpatialGroup* groupp = getSpatialGroup();
  3480. if (groupp)
  3481. {
  3482. groupp->getSpatialPartition()->remove(this, groupp);
  3483. }
  3484. }
  3485. // Delete octree here so listeners will still be able to access bridge
  3486. // specific state
  3487. destroyTree();
  3488. }
  3489. void LLSpatialBridge::destroyTree()
  3490. {
  3491. delete mOctree;
  3492. mOctree = NULL;
  3493. }
  3494. //virtual
  3495. void LLSpatialBridge::updateBinRadius()
  3496. {
  3497. setBinRadius(llmin(mOctree->getSize()[0] * 0.5f, 256.f));
  3498. }
  3499. //virtual
  3500. void LLSpatialBridge::updateSpatialExtents()
  3501. {
  3502. LLSpatialGroup* rootp = (LLSpatialGroup*)mOctree->getListener(0);
  3503. {
  3504. LL_FAST_TIMER(FTM_CULL_REBOUND);
  3505. rootp->rebound();
  3506. }
  3507. const LLVector4a* root_bounds = rootp->getBounds();
  3508. static LLVector4a size;
  3509. size = root_bounds[1];
  3510. // VECTORIZE THIS
  3511. static LLMatrix4a mat;
  3512. mat.loadu(mDrawable->getXform()->getWorldMatrix());
  3513. static const LLVector4a t(0.f, 0.f, 0.f, 0.f);
  3514. LLVector4a center;
  3515. mat.affineTransform(t, center);
  3516. static LLVector4a offset;
  3517. mat.rotate(root_bounds[0], offset);
  3518. center.add(offset);
  3519. // Get 4 corners of bounding box
  3520. static LLVector4a v[4];
  3521. mat.rotate(size, v[0]);
  3522. static LLVector4a scale;
  3523. scale.set(-1.f, -1.f, 1.f);
  3524. scale.mul(size);
  3525. mat.rotate(scale, v[1]);
  3526. scale.set(1.f, -1.f, -1.f);
  3527. scale.mul(size);
  3528. mat.rotate(scale, v[2]);
  3529. scale.set(-1.f, 1.f, -1.f);
  3530. scale.mul(size);
  3531. mat.rotate(scale, v[3]);
  3532. static LLVector4a new_min, new_max, min, max, delta;
  3533. new_min = new_max = center;
  3534. for (U32 i = 0; i < 4; ++i)
  3535. {
  3536. delta.setAbs(v[i]);
  3537. min.setSub(center, delta);
  3538. max.setAdd(center, delta);
  3539. new_min.setMin(new_min, min);
  3540. new_max.setMax(new_max, max);
  3541. }
  3542. setSpatialExtents(new_min, new_max);
  3543. static LLVector4a diagonal;
  3544. diagonal.setSub(new_max, new_min);
  3545. mRadius = diagonal.getLength3().getF32() * 0.5f;
  3546. LLVector4a& pos = getGroupPosition();
  3547. pos.setAdd(new_min, new_max);
  3548. pos.mul(0.5f);
  3549. updateBinRadius();
  3550. }
  3551. void LLSpatialBridge::transformExtents(const LLVector4a* src, LLVector4a* dst)
  3552. {
  3553. LLMatrix4a mat;
  3554. mat.loadu(mDrawable->getXform()->getWorldMatrix());
  3555. mat.invert();
  3556. mat.matMulBoundBox(src, dst);
  3557. }
  3558. LLCamera LLSpatialBridge::transformCamera(LLCamera& camera)
  3559. {
  3560. LLXformMatrix* mat = mDrawable->getXform();
  3561. LLVector3 center = LLVector3::zero * mat->getWorldMatrix();
  3562. LLQuaternion rot = ~mat->getRotation();
  3563. LLCamera ret = camera;
  3564. LLVector3 delta = (ret.getOrigin() - center) * rot;
  3565. if (!delta.isFinite())
  3566. {
  3567. delta.clear();
  3568. }
  3569. LLVector3 look_at = ret.getAtAxis() * rot;
  3570. LLVector3 up_axis = ret.getUpAxis() * rot;
  3571. LLVector3 left_axis = ret.getLeftAxis() * rot;
  3572. ret.setOrigin(delta);
  3573. ret.setAxes(look_at, left_axis, up_axis);
  3574. return ret;
  3575. }
  3576. //virtual
  3577. void LLSpatialBridge::setVisible(LLCamera& camera_in,
  3578. std::vector<LLDrawable*>* results,
  3579. bool for_select)
  3580. {
  3581. if (!gPipeline.hasRenderType(mDrawableType))
  3582. {
  3583. return;
  3584. }
  3585. // *HACK: do not draw attachments for avatars that have not been visible in
  3586. // more than a frame
  3587. LLViewerObject* vobjp = mDrawable->getVObj();
  3588. if (vobjp && vobjp->isAttachment() && !vobjp->isHUDAttachment())
  3589. {
  3590. LLDrawable* parentp = mDrawable->getParent();
  3591. if (parentp)
  3592. {
  3593. LLViewerObject* objparentp = parentp->getVObj();
  3594. if (!objparentp || objparentp->isDead())
  3595. {
  3596. return;
  3597. }
  3598. if (objparentp->isAvatar())
  3599. {
  3600. LLVOAvatar* avatarp = (LLVOAvatar*)objparentp;
  3601. if (!avatarp->isVisible() || avatarp->isImpostor() ||
  3602. !avatarp->isFullyLoaded())
  3603. {
  3604. return;
  3605. }
  3606. }
  3607. LLDrawable* drawablep = objparentp->mDrawable;
  3608. LLSpatialGroup* groupp = drawablep->getSpatialGroup();
  3609. if (!groupp ||
  3610. LLViewerOctreeEntryData::getCurrentFrame() -
  3611. drawablep->getVisible() > 1)
  3612. {
  3613. return;
  3614. }
  3615. }
  3616. }
  3617. LLSpatialGroup* groupp = (LLSpatialGroup*)mOctree->getListener(0);
  3618. groupp->rebound();
  3619. LLVector4a center;
  3620. const LLVector4a* exts = getSpatialExtents();
  3621. center.setAdd(exts[0], exts[1]);
  3622. center.mul(0.5f);
  3623. LLVector4a size;
  3624. size.setSub(exts[1], exts[0]);
  3625. size.mul(0.5f);
  3626. if ((LLPipeline::sShadowRender && camera_in.AABBInFrustum(center, size)) ||
  3627. LLPipeline::sImpostorRender ||
  3628. (camera_in.AABBInFrustumNoFarClip(center, size) &&
  3629. AABBSphereIntersect(exts[0], exts[1], camera_in.getOrigin(),
  3630. camera_in.mFrustumCornerDist)))
  3631. {
  3632. if (!LLPipeline::sImpostorRender && !LLPipeline::sShadowRender &&
  3633. LLPipeline::calcPixelArea(center, size, camera_in) < FORCE_INVISIBLE_AREA)
  3634. {
  3635. return;
  3636. }
  3637. LLDrawable::setVisible(camera_in);
  3638. if (for_select)
  3639. {
  3640. results->push_back(mDrawable);
  3641. if (mDrawable->getVObj())
  3642. {
  3643. LLViewerObject::const_child_list_t& child_list =
  3644. mDrawable->getVObj()->getChildren();
  3645. for (LLViewerObject::child_list_t::const_iterator
  3646. iter = child_list.begin(), end = child_list.end();
  3647. iter != end; ++iter)
  3648. {
  3649. LLViewerObject* child = *iter;
  3650. LLDrawable* drawable = child->mDrawable;
  3651. if (drawable)
  3652. {
  3653. results->push_back(drawable);
  3654. }
  3655. }
  3656. }
  3657. }
  3658. else
  3659. {
  3660. LLCamera trans_camera = transformCamera(camera_in);
  3661. LLOctreeMarkNotCulled culler(&trans_camera);
  3662. culler.traverse(mOctree);
  3663. }
  3664. }
  3665. }
  3666. //virtual
  3667. void LLSpatialBridge::updateDistance(LLCamera& camera_in, bool force_update)
  3668. {
  3669. if (mDrawable.isNull())
  3670. {
  3671. markDead();
  3672. return;
  3673. }
  3674. if (gShiftFrame)
  3675. {
  3676. return;
  3677. }
  3678. if (mDrawable->getVObj())
  3679. {
  3680. if (mDrawable->getVObj()->isAttachment())
  3681. {
  3682. LLDrawable* parentp = mDrawable->getParent();
  3683. if (parentp && parentp->getVObj())
  3684. {
  3685. LLVOAvatar* av = parentp->getVObj()->asAvatar();
  3686. if (av && av->isImpostor())
  3687. {
  3688. return;
  3689. }
  3690. }
  3691. }
  3692. LLCamera camera = transformCamera(camera_in);
  3693. mDrawable->updateDistance(camera, force_update);
  3694. LLViewerObject::const_child_list_t& child_list =
  3695. mDrawable->getVObj()->getChildren();
  3696. for (LLViewerObject::child_list_t::const_iterator
  3697. iter = child_list.begin(), end = child_list.end();
  3698. iter != end; ++iter)
  3699. {
  3700. LLViewerObject* childp = *iter;
  3701. LLDrawable* drawablep = childp->mDrawable;
  3702. if (drawablep && !drawablep->isAvatar())
  3703. {
  3704. drawablep->updateDistance(camera, force_update);
  3705. }
  3706. }
  3707. }
  3708. }
  3709. //virtual
  3710. void LLSpatialBridge::makeActive()
  3711. {
  3712. // It is an error to make a spatial bridge active (it is already active)
  3713. llerrs << "makeActive called on spatial bridge" << llendl;
  3714. }
  3715. //virtual
  3716. void LLSpatialBridge::move(LLDrawable* drawablep, LLSpatialGroup* curp,
  3717. bool immediate)
  3718. {
  3719. LLSpatialPartition::move(drawablep, curp, immediate);
  3720. gPipeline.markMoved(this, false);
  3721. }
  3722. //virtual
  3723. bool LLSpatialBridge::updateMove()
  3724. {
  3725. if (mDrawable.notNull() && mDrawable->mVObjp && mDrawable->getRegion())
  3726. {
  3727. LLSpatialPartition* partp =
  3728. mDrawable->getRegion()->getSpatialPartition(mPartitionType);
  3729. mOctree->balance();
  3730. if (partp)
  3731. {
  3732. partp->move(this, getSpatialGroup(), true);
  3733. }
  3734. return true;
  3735. }
  3736. llwarns_sparse << "Bad spatial bridge (NULL drawable or mVObjp or region)."
  3737. << llendl;
  3738. return false;
  3739. }
  3740. //virtual
  3741. void LLSpatialBridge::shiftPos(const LLVector4a& vec)
  3742. {
  3743. LLDrawable::shift(vec);
  3744. }
  3745. //virtual
  3746. void LLSpatialBridge::cleanupReferences()
  3747. {
  3748. LLDrawable::cleanupReferences();
  3749. if (mDrawable.notNull())
  3750. {
  3751. LLViewerObject* vobjp = mDrawable->getVObj().get();
  3752. if (vobjp)
  3753. {
  3754. // In order to guard against modifications to the children list
  3755. // that would result from setGroup(NULL) on them, build a vector
  3756. // of LLPointer's on the drawables to operate upon, and then use
  3757. // that vector's pointers to setGroup(NULL). HB
  3758. LLViewerObject::const_child_list_t& child_list =
  3759. vobjp->getChildren();
  3760. std::vector<LLPointer<LLDrawable> > drawvec;
  3761. drawvec.reserve(child_list.size());
  3762. for (LLViewerObject::child_list_t::const_iterator
  3763. iter = child_list.begin(), end = child_list.end();
  3764. iter != end; ++iter)
  3765. {
  3766. LLViewerObject* childp = iter->get();
  3767. if (childp)
  3768. {
  3769. LLDrawable* drawp = childp->mDrawable.get();
  3770. if (drawp)
  3771. {
  3772. drawvec.emplace_back(drawp);
  3773. }
  3774. }
  3775. }
  3776. // Now we can safely operate on children's drawables. HB
  3777. for (U32 i = 0, count = drawvec.size(); i < count; ++i)
  3778. {
  3779. drawvec[i]->setGroup(NULL);
  3780. }
  3781. }
  3782. // Do this *after* it got done on children. HB
  3783. mDrawable->setGroup(NULL);
  3784. mDrawable->setSpatialBridge(NULL);
  3785. mDrawable = NULL;
  3786. }
  3787. }
  3788. LLBridgePartition::LLBridgePartition(LLViewerRegion* regionp)
  3789. : LLSpatialPartition(0, false, regionp)
  3790. {
  3791. mDrawableType = LLPipeline::RENDER_TYPE_VOLUME;
  3792. mPartitionType = LLViewerRegion::PARTITION_BRIDGE;
  3793. mLODPeriod = 16;
  3794. mSlopRatio = 0.25f;
  3795. }
  3796. LLAvatarPartition::LLAvatarPartition(LLViewerRegion* regionp)
  3797. : LLBridgePartition(regionp)
  3798. {
  3799. mDrawableType = LLPipeline::RENDER_TYPE_AVATAR;
  3800. mPartitionType = LLViewerRegion::PARTITION_AVATAR;
  3801. }
  3802. LLPuppetPartition::LLPuppetPartition(LLViewerRegion* regionp)
  3803. : LLBridgePartition(regionp)
  3804. {
  3805. mDrawableType = LLPipeline::RENDER_TYPE_PUPPET;
  3806. mPartitionType = LLViewerRegion::PARTITION_PUPPET;
  3807. }
  3808. LLHUDBridge::LLHUDBridge(LLDrawable* drawablep, LLViewerRegion* regionp)
  3809. : LLVolumeBridge(drawablep, regionp)
  3810. {
  3811. mDrawableType = LLPipeline::RENDER_TYPE_HUD;
  3812. mPartitionType = LLViewerRegion::PARTITION_HUD;
  3813. mSlopRatio = 0.f;
  3814. }