llpipeline.cpp 385 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191219221932194219521962197219821992200220122022203220422052206220722082209221022112212221322142215221622172218221922202221222222232224222522262227222822292230223122322233223422352236223722382239224022412242224322442245224622472248224922502251225222532254225522562257225822592260226122622263226422652266226722682269227022712272227322742275227622772278227922802281228222832284228522862287228822892290229122922293229422952296229722982299230023012302230323042305230623072308230923102311231223132314231523162317231823192320232123222323232423252326232723282329233023312332233323342335233623372338233923402341234223432344234523462347234823492350235123522353235423552356235723582359236023612362236323642365236623672368236923702371237223732374237523762377237823792380238123822383238423852386238723882389239023912392239323942395239623972398239924002401240224032404240524062407240824092410241124122413241424152416241724182419242024212422242324242425242624272428242924302431243224332434243524362437243824392440244124422443244424452446244724482449245024512452245324542455245624572458245924602461246224632464246524662467246824692470247124722473247424752476247724782479248024812482248324842485248624872488248924902491249224932494249524962497249824992500250125022503250425052506250725082509251025112512251325142515251625172518251925202521252225232524252525262527252825292530253125322533253425352536253725382539254025412542254325442545254625472548254925502551255225532554255525562557255825592560256125622563256425652566256725682569257025712572257325742575257625772578257925802581258225832584258525862587258825892590259125922593259425952596259725982599260026012602260326042605260626072608260926102611261226132614261526162617261826192620262126222623262426252626262726282629263026312632263326342635263626372638263926402641264226432644264526462647264826492650265126522653265426552656265726582659266026612662266326642665266626672668266926702671267226732674267526762677267826792680268126822683268426852686268726882689269026912692269326942695269626972698269927002701270227032704270527062707270827092710271127122713271427152716271727182719272027212722272327242725272627272728272927302731273227332734273527362737273827392740274127422743274427452746274727482749275027512752275327542755275627572758275927602761276227632764276527662767276827692770277127722773277427752776277727782779278027812782278327842785278627872788278927902791279227932794279527962797279827992800280128022803280428052806280728082809281028112812281328142815281628172818281928202821282228232824282528262827282828292830283128322833283428352836283728382839284028412842284328442845284628472848284928502851285228532854285528562857285828592860286128622863286428652866286728682869287028712872287328742875287628772878287928802881288228832884288528862887288828892890289128922893289428952896289728982899290029012902290329042905290629072908290929102911291229132914291529162917291829192920292129222923292429252926292729282929293029312932293329342935293629372938293929402941294229432944294529462947294829492950295129522953295429552956295729582959296029612962296329642965296629672968296929702971297229732974297529762977297829792980298129822983298429852986298729882989299029912992299329942995299629972998299930003001300230033004300530063007300830093010301130123013301430153016301730183019302030213022302330243025302630273028302930303031303230333034303530363037303830393040304130423043304430453046304730483049305030513052305330543055305630573058305930603061306230633064306530663067306830693070307130723073307430753076307730783079308030813082308330843085308630873088308930903091309230933094309530963097309830993100310131023103310431053106310731083109311031113112311331143115311631173118311931203121312231233124312531263127312831293130313131323133313431353136313731383139314031413142314331443145314631473148314931503151315231533154315531563157315831593160316131623163316431653166316731683169317031713172317331743175317631773178317931803181318231833184318531863187318831893190319131923193319431953196319731983199320032013202320332043205320632073208320932103211321232133214321532163217321832193220322132223223322432253226322732283229323032313232323332343235323632373238323932403241324232433244324532463247324832493250325132523253325432553256325732583259326032613262326332643265326632673268326932703271327232733274327532763277327832793280328132823283328432853286328732883289329032913292329332943295329632973298329933003301330233033304330533063307330833093310331133123313331433153316331733183319332033213322332333243325332633273328332933303331333233333334333533363337333833393340334133423343334433453346334733483349335033513352335333543355335633573358335933603361336233633364336533663367336833693370337133723373337433753376337733783379338033813382338333843385338633873388338933903391339233933394339533963397339833993400340134023403340434053406340734083409341034113412341334143415341634173418341934203421342234233424342534263427342834293430343134323433343434353436343734383439344034413442344334443445344634473448344934503451345234533454345534563457345834593460346134623463346434653466346734683469347034713472347334743475347634773478347934803481348234833484348534863487348834893490349134923493349434953496349734983499350035013502350335043505350635073508350935103511351235133514351535163517351835193520352135223523352435253526352735283529353035313532353335343535353635373538353935403541354235433544354535463547354835493550355135523553355435553556355735583559356035613562356335643565356635673568356935703571357235733574357535763577357835793580358135823583358435853586358735883589359035913592359335943595359635973598359936003601360236033604360536063607360836093610361136123613361436153616361736183619362036213622362336243625362636273628362936303631363236333634363536363637363836393640364136423643364436453646364736483649365036513652365336543655365636573658365936603661366236633664366536663667366836693670367136723673367436753676367736783679368036813682368336843685368636873688368936903691369236933694369536963697369836993700370137023703370437053706370737083709371037113712371337143715371637173718371937203721372237233724372537263727372837293730373137323733373437353736373737383739374037413742374337443745374637473748374937503751375237533754375537563757375837593760376137623763376437653766376737683769377037713772377337743775377637773778377937803781378237833784378537863787378837893790379137923793379437953796379737983799380038013802380338043805380638073808380938103811381238133814381538163817381838193820382138223823382438253826382738283829383038313832383338343835383638373838383938403841384238433844384538463847384838493850385138523853385438553856385738583859386038613862386338643865386638673868386938703871387238733874387538763877387838793880388138823883388438853886388738883889389038913892389338943895389638973898389939003901390239033904390539063907390839093910391139123913391439153916391739183919392039213922392339243925392639273928392939303931393239333934393539363937393839393940394139423943394439453946394739483949395039513952395339543955395639573958395939603961396239633964396539663967396839693970397139723973397439753976397739783979398039813982398339843985398639873988398939903991399239933994399539963997399839994000400140024003400440054006400740084009401040114012401340144015401640174018401940204021402240234024402540264027402840294030403140324033403440354036403740384039404040414042404340444045404640474048404940504051405240534054405540564057405840594060406140624063406440654066406740684069407040714072407340744075407640774078407940804081408240834084408540864087408840894090409140924093409440954096409740984099410041014102410341044105410641074108410941104111411241134114411541164117411841194120412141224123412441254126412741284129413041314132413341344135413641374138413941404141414241434144414541464147414841494150415141524153415441554156415741584159416041614162416341644165416641674168416941704171417241734174417541764177417841794180418141824183418441854186418741884189419041914192419341944195419641974198419942004201420242034204420542064207420842094210421142124213421442154216421742184219422042214222422342244225422642274228422942304231423242334234423542364237423842394240424142424243424442454246424742484249425042514252425342544255425642574258425942604261426242634264426542664267426842694270427142724273427442754276427742784279428042814282428342844285428642874288428942904291429242934294429542964297429842994300430143024303430443054306430743084309431043114312431343144315431643174318431943204321432243234324432543264327432843294330433143324333433443354336433743384339434043414342434343444345434643474348434943504351435243534354435543564357435843594360436143624363436443654366436743684369437043714372437343744375437643774378437943804381438243834384438543864387438843894390439143924393439443954396439743984399440044014402440344044405440644074408440944104411441244134414441544164417441844194420442144224423442444254426442744284429443044314432443344344435443644374438443944404441444244434444444544464447444844494450445144524453445444554456445744584459446044614462446344644465446644674468446944704471447244734474447544764477447844794480448144824483448444854486448744884489449044914492449344944495449644974498449945004501450245034504450545064507450845094510451145124513451445154516451745184519452045214522452345244525452645274528452945304531453245334534453545364537453845394540454145424543454445454546454745484549455045514552455345544555455645574558455945604561456245634564456545664567456845694570457145724573457445754576457745784579458045814582458345844585458645874588458945904591459245934594459545964597459845994600460146024603460446054606460746084609461046114612461346144615461646174618461946204621462246234624462546264627462846294630463146324633463446354636463746384639464046414642464346444645464646474648464946504651465246534654465546564657465846594660466146624663466446654666466746684669467046714672467346744675467646774678467946804681468246834684468546864687468846894690469146924693469446954696469746984699470047014702470347044705470647074708470947104711471247134714471547164717471847194720472147224723472447254726472747284729473047314732473347344735473647374738473947404741474247434744474547464747474847494750475147524753475447554756475747584759476047614762476347644765476647674768476947704771477247734774477547764777477847794780478147824783478447854786478747884789479047914792479347944795479647974798479948004801480248034804480548064807480848094810481148124813481448154816481748184819482048214822482348244825482648274828482948304831483248334834483548364837483848394840484148424843484448454846484748484849485048514852485348544855485648574858485948604861486248634864486548664867486848694870487148724873487448754876487748784879488048814882488348844885488648874888488948904891489248934894489548964897489848994900490149024903490449054906490749084909491049114912491349144915491649174918491949204921492249234924492549264927492849294930493149324933493449354936493749384939494049414942494349444945494649474948494949504951495249534954495549564957495849594960496149624963496449654966496749684969497049714972497349744975497649774978497949804981498249834984498549864987498849894990499149924993499449954996499749984999500050015002500350045005500650075008500950105011501250135014501550165017501850195020502150225023502450255026502750285029503050315032503350345035503650375038503950405041504250435044504550465047504850495050505150525053505450555056505750585059506050615062506350645065506650675068506950705071507250735074507550765077507850795080508150825083508450855086508750885089509050915092509350945095509650975098509951005101510251035104510551065107510851095110511151125113511451155116511751185119512051215122512351245125512651275128512951305131513251335134513551365137513851395140514151425143514451455146514751485149515051515152515351545155515651575158515951605161516251635164516551665167516851695170517151725173517451755176517751785179518051815182518351845185518651875188518951905191519251935194519551965197519851995200520152025203520452055206520752085209521052115212521352145215521652175218521952205221522252235224522552265227522852295230523152325233523452355236523752385239524052415242524352445245524652475248524952505251525252535254525552565257525852595260526152625263526452655266526752685269527052715272527352745275527652775278527952805281528252835284528552865287528852895290529152925293529452955296529752985299530053015302530353045305530653075308530953105311531253135314531553165317531853195320532153225323532453255326532753285329533053315332533353345335533653375338533953405341534253435344534553465347534853495350535153525353535453555356535753585359536053615362536353645365536653675368536953705371537253735374537553765377537853795380538153825383538453855386538753885389539053915392539353945395539653975398539954005401540254035404540554065407540854095410541154125413541454155416541754185419542054215422542354245425542654275428542954305431543254335434543554365437543854395440544154425443544454455446544754485449545054515452545354545455545654575458545954605461546254635464546554665467546854695470547154725473547454755476547754785479548054815482548354845485548654875488548954905491549254935494549554965497549854995500550155025503550455055506550755085509551055115512551355145515551655175518551955205521552255235524552555265527552855295530553155325533553455355536553755385539554055415542554355445545554655475548554955505551555255535554555555565557555855595560556155625563556455655566556755685569557055715572557355745575557655775578557955805581558255835584558555865587558855895590559155925593559455955596559755985599560056015602560356045605560656075608560956105611561256135614561556165617561856195620562156225623562456255626562756285629563056315632563356345635563656375638563956405641564256435644564556465647564856495650565156525653565456555656565756585659566056615662566356645665566656675668566956705671567256735674567556765677567856795680568156825683568456855686568756885689569056915692569356945695569656975698569957005701570257035704570557065707570857095710571157125713571457155716571757185719572057215722572357245725572657275728572957305731573257335734573557365737573857395740574157425743574457455746574757485749575057515752575357545755575657575758575957605761576257635764576557665767576857695770577157725773577457755776577757785779578057815782578357845785578657875788578957905791579257935794579557965797579857995800580158025803580458055806580758085809581058115812581358145815581658175818581958205821582258235824582558265827582858295830583158325833583458355836583758385839584058415842584358445845584658475848584958505851585258535854585558565857585858595860586158625863586458655866586758685869587058715872587358745875587658775878587958805881588258835884588558865887588858895890589158925893589458955896589758985899590059015902590359045905590659075908590959105911591259135914591559165917591859195920592159225923592459255926592759285929593059315932593359345935593659375938593959405941594259435944594559465947594859495950595159525953595459555956595759585959596059615962596359645965596659675968596959705971597259735974597559765977597859795980598159825983598459855986598759885989599059915992599359945995599659975998599960006001600260036004600560066007600860096010601160126013601460156016601760186019602060216022602360246025602660276028602960306031603260336034603560366037603860396040604160426043604460456046604760486049605060516052605360546055605660576058605960606061606260636064606560666067606860696070607160726073607460756076607760786079608060816082608360846085608660876088608960906091609260936094609560966097609860996100610161026103610461056106610761086109611061116112611361146115611661176118611961206121612261236124612561266127612861296130613161326133613461356136613761386139614061416142614361446145614661476148614961506151615261536154615561566157615861596160616161626163616461656166616761686169617061716172617361746175617661776178617961806181618261836184618561866187618861896190619161926193619461956196619761986199620062016202620362046205620662076208620962106211621262136214621562166217621862196220622162226223622462256226622762286229623062316232623362346235623662376238623962406241624262436244624562466247624862496250625162526253625462556256625762586259626062616262626362646265626662676268626962706271627262736274627562766277627862796280628162826283628462856286628762886289629062916292629362946295629662976298629963006301630263036304630563066307630863096310631163126313631463156316631763186319632063216322632363246325632663276328632963306331633263336334633563366337633863396340634163426343634463456346634763486349635063516352635363546355635663576358635963606361636263636364636563666367636863696370637163726373637463756376637763786379638063816382638363846385638663876388638963906391639263936394639563966397639863996400640164026403640464056406640764086409641064116412641364146415641664176418641964206421642264236424642564266427642864296430643164326433643464356436643764386439644064416442644364446445644664476448644964506451645264536454645564566457645864596460646164626463646464656466646764686469647064716472647364746475647664776478647964806481648264836484648564866487648864896490649164926493649464956496649764986499650065016502650365046505650665076508650965106511651265136514651565166517651865196520652165226523652465256526652765286529653065316532653365346535653665376538653965406541654265436544654565466547654865496550655165526553655465556556655765586559656065616562656365646565656665676568656965706571657265736574657565766577657865796580658165826583658465856586658765886589659065916592659365946595659665976598659966006601660266036604660566066607660866096610661166126613661466156616661766186619662066216622662366246625662666276628662966306631663266336634663566366637663866396640664166426643664466456646664766486649665066516652665366546655665666576658665966606661666266636664666566666667666866696670667166726673667466756676667766786679668066816682668366846685668666876688668966906691669266936694669566966697669866996700670167026703670467056706670767086709671067116712671367146715671667176718671967206721672267236724672567266727672867296730673167326733673467356736673767386739674067416742674367446745674667476748674967506751675267536754675567566757675867596760676167626763676467656766676767686769677067716772677367746775677667776778677967806781678267836784678567866787678867896790679167926793679467956796679767986799680068016802680368046805680668076808680968106811681268136814681568166817681868196820682168226823682468256826682768286829683068316832683368346835683668376838683968406841684268436844684568466847684868496850685168526853685468556856685768586859686068616862686368646865686668676868686968706871687268736874687568766877687868796880688168826883688468856886688768886889689068916892689368946895689668976898689969006901690269036904690569066907690869096910691169126913691469156916691769186919692069216922692369246925692669276928692969306931693269336934693569366937693869396940694169426943694469456946694769486949695069516952695369546955695669576958695969606961696269636964696569666967696869696970697169726973697469756976697769786979698069816982698369846985698669876988698969906991699269936994699569966997699869997000700170027003700470057006700770087009701070117012701370147015701670177018701970207021702270237024702570267027702870297030703170327033703470357036703770387039704070417042704370447045704670477048704970507051705270537054705570567057705870597060706170627063706470657066706770687069707070717072707370747075707670777078707970807081708270837084708570867087708870897090709170927093709470957096709770987099710071017102710371047105710671077108710971107111711271137114711571167117711871197120712171227123712471257126712771287129713071317132713371347135713671377138713971407141714271437144714571467147714871497150715171527153715471557156715771587159716071617162716371647165716671677168716971707171717271737174717571767177717871797180718171827183718471857186718771887189719071917192719371947195719671977198719972007201720272037204720572067207720872097210721172127213721472157216721772187219722072217222722372247225722672277228722972307231723272337234723572367237723872397240724172427243724472457246724772487249725072517252725372547255725672577258725972607261726272637264726572667267726872697270727172727273727472757276727772787279728072817282728372847285728672877288728972907291729272937294729572967297729872997300730173027303730473057306730773087309731073117312731373147315731673177318731973207321732273237324732573267327732873297330733173327333733473357336733773387339734073417342734373447345734673477348734973507351735273537354735573567357735873597360736173627363736473657366736773687369737073717372737373747375737673777378737973807381738273837384738573867387738873897390739173927393739473957396739773987399740074017402740374047405740674077408740974107411741274137414741574167417741874197420742174227423742474257426742774287429743074317432743374347435743674377438743974407441744274437444744574467447744874497450745174527453745474557456745774587459746074617462746374647465746674677468746974707471747274737474747574767477747874797480748174827483748474857486748774887489749074917492749374947495749674977498749975007501750275037504750575067507750875097510751175127513751475157516751775187519752075217522752375247525752675277528752975307531753275337534753575367537753875397540754175427543754475457546754775487549755075517552755375547555755675577558755975607561756275637564756575667567756875697570757175727573757475757576757775787579758075817582758375847585758675877588758975907591759275937594759575967597759875997600760176027603760476057606760776087609761076117612761376147615761676177618761976207621762276237624762576267627762876297630763176327633763476357636763776387639764076417642764376447645764676477648764976507651765276537654765576567657765876597660766176627663766476657666766776687669767076717672767376747675767676777678767976807681768276837684768576867687768876897690769176927693769476957696769776987699770077017702770377047705770677077708770977107711771277137714771577167717771877197720772177227723772477257726772777287729773077317732773377347735773677377738773977407741774277437744774577467747774877497750775177527753775477557756775777587759776077617762776377647765776677677768776977707771777277737774777577767777777877797780778177827783778477857786778777887789779077917792779377947795779677977798779978007801780278037804780578067807780878097810781178127813781478157816781778187819782078217822782378247825782678277828782978307831783278337834783578367837783878397840784178427843784478457846784778487849785078517852785378547855785678577858785978607861786278637864786578667867786878697870787178727873787478757876787778787879788078817882788378847885788678877888788978907891789278937894789578967897789878997900790179027903790479057906790779087909791079117912791379147915791679177918791979207921792279237924792579267927792879297930793179327933793479357936793779387939794079417942794379447945794679477948794979507951795279537954795579567957795879597960796179627963796479657966796779687969797079717972797379747975797679777978797979807981798279837984798579867987798879897990799179927993799479957996799779987999800080018002800380048005800680078008800980108011801280138014801580168017801880198020802180228023802480258026802780288029803080318032803380348035803680378038803980408041804280438044804580468047804880498050805180528053805480558056805780588059806080618062806380648065806680678068806980708071807280738074807580768077807880798080808180828083808480858086808780888089809080918092809380948095809680978098809981008101810281038104810581068107810881098110811181128113811481158116811781188119812081218122812381248125812681278128812981308131813281338134813581368137813881398140814181428143814481458146814781488149815081518152815381548155815681578158815981608161816281638164816581668167816881698170817181728173817481758176817781788179818081818182818381848185818681878188818981908191819281938194819581968197819881998200820182028203820482058206820782088209821082118212821382148215821682178218821982208221822282238224822582268227822882298230823182328233823482358236823782388239824082418242824382448245824682478248824982508251825282538254825582568257825882598260826182628263826482658266826782688269827082718272827382748275827682778278827982808281828282838284828582868287828882898290829182928293829482958296829782988299830083018302830383048305830683078308830983108311831283138314831583168317831883198320832183228323832483258326832783288329833083318332833383348335833683378338833983408341834283438344834583468347834883498350835183528353835483558356835783588359836083618362836383648365836683678368836983708371837283738374837583768377837883798380838183828383838483858386838783888389839083918392839383948395839683978398839984008401840284038404840584068407840884098410841184128413841484158416841784188419842084218422842384248425842684278428842984308431843284338434843584368437843884398440844184428443844484458446844784488449845084518452845384548455845684578458845984608461846284638464846584668467846884698470847184728473847484758476847784788479848084818482848384848485848684878488848984908491849284938494849584968497849884998500850185028503850485058506850785088509851085118512851385148515851685178518851985208521852285238524852585268527852885298530853185328533853485358536853785388539854085418542854385448545854685478548854985508551855285538554855585568557855885598560856185628563856485658566856785688569857085718572857385748575857685778578857985808581858285838584858585868587858885898590859185928593859485958596859785988599860086018602860386048605860686078608860986108611861286138614861586168617861886198620862186228623862486258626862786288629863086318632863386348635863686378638863986408641864286438644864586468647864886498650865186528653865486558656865786588659866086618662866386648665866686678668866986708671867286738674867586768677867886798680868186828683868486858686868786888689869086918692869386948695869686978698869987008701870287038704870587068707870887098710871187128713871487158716871787188719872087218722872387248725872687278728872987308731873287338734873587368737873887398740874187428743874487458746874787488749875087518752875387548755875687578758875987608761876287638764876587668767876887698770877187728773877487758776877787788779878087818782878387848785878687878788878987908791879287938794879587968797879887998800880188028803880488058806880788088809881088118812881388148815881688178818881988208821882288238824882588268827882888298830883188328833883488358836883788388839884088418842884388448845884688478848884988508851885288538854885588568857885888598860886188628863886488658866886788688869887088718872887388748875887688778878887988808881888288838884888588868887888888898890889188928893889488958896889788988899890089018902890389048905890689078908890989108911891289138914891589168917891889198920892189228923892489258926892789288929893089318932893389348935893689378938893989408941894289438944894589468947894889498950895189528953895489558956895789588959896089618962896389648965896689678968896989708971897289738974897589768977897889798980898189828983898489858986898789888989899089918992899389948995899689978998899990009001900290039004900590069007900890099010901190129013901490159016901790189019902090219022902390249025902690279028902990309031903290339034903590369037903890399040904190429043904490459046904790489049905090519052905390549055905690579058905990609061906290639064906590669067906890699070907190729073907490759076907790789079908090819082908390849085908690879088908990909091909290939094909590969097909890999100910191029103910491059106910791089109911091119112911391149115911691179118911991209121912291239124912591269127912891299130913191329133913491359136913791389139914091419142914391449145914691479148914991509151915291539154915591569157915891599160916191629163916491659166916791689169917091719172917391749175917691779178917991809181918291839184918591869187918891899190919191929193919491959196919791989199920092019202920392049205920692079208920992109211921292139214921592169217921892199220922192229223922492259226922792289229923092319232923392349235923692379238923992409241924292439244924592469247924892499250925192529253925492559256925792589259926092619262926392649265926692679268926992709271927292739274927592769277927892799280928192829283928492859286928792889289929092919292929392949295929692979298929993009301930293039304930593069307930893099310931193129313931493159316931793189319932093219322932393249325932693279328932993309331933293339334933593369337933893399340934193429343934493459346934793489349935093519352935393549355935693579358935993609361936293639364936593669367936893699370937193729373937493759376937793789379938093819382938393849385938693879388938993909391939293939394939593969397939893999400940194029403940494059406940794089409941094119412941394149415941694179418941994209421942294239424942594269427942894299430943194329433943494359436943794389439944094419442944394449445944694479448944994509451945294539454945594569457945894599460946194629463946494659466946794689469947094719472947394749475947694779478947994809481948294839484948594869487948894899490949194929493949494959496949794989499950095019502950395049505950695079508950995109511951295139514951595169517951895199520952195229523952495259526952795289529953095319532953395349535953695379538953995409541954295439544954595469547954895499550955195529553955495559556955795589559956095619562956395649565956695679568956995709571957295739574957595769577957895799580958195829583958495859586958795889589959095919592959395949595959695979598959996009601960296039604960596069607960896099610961196129613961496159616961796189619962096219622962396249625962696279628962996309631963296339634963596369637963896399640964196429643964496459646964796489649965096519652965396549655965696579658965996609661966296639664966596669667966896699670967196729673967496759676967796789679968096819682968396849685968696879688968996909691969296939694969596969697969896999700970197029703970497059706970797089709971097119712971397149715971697179718971997209721972297239724972597269727972897299730973197329733973497359736973797389739974097419742974397449745974697479748974997509751975297539754975597569757975897599760976197629763976497659766976797689769977097719772977397749775977697779778977997809781978297839784978597869787978897899790979197929793979497959796979797989799980098019802980398049805980698079808980998109811981298139814981598169817981898199820982198229823982498259826982798289829983098319832983398349835983698379838983998409841984298439844984598469847984898499850985198529853985498559856985798589859986098619862986398649865986698679868986998709871987298739874987598769877987898799880988198829883988498859886988798889889989098919892989398949895989698979898989999009901990299039904990599069907990899099910991199129913991499159916991799189919992099219922992399249925992699279928992999309931993299339934993599369937993899399940994199429943994499459946994799489949995099519952995399549955995699579958995999609961996299639964996599669967996899699970997199729973997499759976997799789979998099819982998399849985998699879988998999909991999299939994999599969997999899991000010001100021000310004100051000610007100081000910010100111001210013100141001510016100171001810019100201002110022100231002410025100261002710028100291003010031100321003310034100351003610037100381003910040100411004210043100441004510046100471004810049100501005110052100531005410055100561005710058100591006010061100621006310064100651006610067100681006910070100711007210073100741007510076100771007810079100801008110082100831008410085100861008710088100891009010091100921009310094100951009610097100981009910100101011010210103101041010510106101071010810109101101011110112101131011410115101161011710118101191012010121101221012310124101251012610127101281012910130101311013210133101341013510136101371013810139101401014110142101431014410145101461014710148101491015010151101521015310154101551015610157101581015910160101611016210163101641016510166101671016810169101701017110172101731017410175101761017710178101791018010181101821018310184101851018610187101881018910190101911019210193101941019510196101971019810199102001020110202102031020410205102061020710208102091021010211102121021310214102151021610217102181021910220102211022210223102241022510226102271022810229102301023110232102331023410235102361023710238102391024010241102421024310244102451024610247102481024910250102511025210253102541025510256102571025810259102601026110262102631026410265102661026710268102691027010271102721027310274102751027610277102781027910280102811028210283102841028510286102871028810289102901029110292102931029410295102961029710298102991030010301103021030310304103051030610307103081030910310103111031210313103141031510316103171031810319103201032110322103231032410325103261032710328103291033010331103321033310334103351033610337103381033910340103411034210343103441034510346103471034810349103501035110352103531035410355103561035710358103591036010361103621036310364103651036610367103681036910370103711037210373103741037510376103771037810379103801038110382103831038410385103861038710388103891039010391103921039310394103951039610397103981039910400104011040210403104041040510406104071040810409104101041110412104131041410415104161041710418104191042010421104221042310424104251042610427104281042910430104311043210433104341043510436104371043810439104401044110442104431044410445104461044710448104491045010451104521045310454104551045610457104581045910460104611046210463104641046510466104671046810469104701047110472104731047410475104761047710478104791048010481104821048310484104851048610487104881048910490104911049210493104941049510496104971049810499105001050110502105031050410505105061050710508105091051010511105121051310514105151051610517105181051910520105211052210523105241052510526105271052810529105301053110532105331053410535105361053710538105391054010541105421054310544105451054610547105481054910550105511055210553105541055510556105571055810559105601056110562105631056410565105661056710568105691057010571105721057310574105751057610577105781057910580105811058210583105841058510586105871058810589105901059110592105931059410595105961059710598105991060010601106021060310604106051060610607106081060910610106111061210613106141061510616106171061810619106201062110622106231062410625106261062710628106291063010631106321063310634106351063610637106381063910640106411064210643106441064510646106471064810649106501065110652106531065410655106561065710658106591066010661106621066310664106651066610667106681066910670106711067210673106741067510676106771067810679106801068110682106831068410685106861068710688106891069010691106921069310694106951069610697106981069910700107011070210703107041070510706107071070810709107101071110712107131071410715107161071710718107191072010721107221072310724107251072610727107281072910730107311073210733107341073510736107371073810739107401074110742107431074410745107461074710748107491075010751107521075310754107551075610757107581075910760107611076210763107641076510766107671076810769107701077110772107731077410775107761077710778107791078010781107821078310784107851078610787107881078910790107911079210793107941079510796107971079810799108001080110802108031080410805108061080710808108091081010811108121081310814108151081610817108181081910820108211082210823108241082510826108271082810829108301083110832108331083410835108361083710838108391084010841108421084310844108451084610847108481084910850108511085210853108541085510856108571085810859108601086110862108631086410865108661086710868108691087010871108721087310874108751087610877108781087910880108811088210883108841088510886108871088810889108901089110892108931089410895108961089710898108991090010901109021090310904109051090610907109081090910910109111091210913109141091510916109171091810919109201092110922109231092410925109261092710928109291093010931109321093310934109351093610937109381093910940109411094210943109441094510946109471094810949109501095110952109531095410955109561095710958109591096010961109621096310964109651096610967109681096910970109711097210973109741097510976109771097810979109801098110982109831098410985109861098710988109891099010991109921099310994109951099610997109981099911000110011100211003110041100511006110071100811009110101101111012110131101411015110161101711018110191102011021110221102311024110251102611027110281102911030110311103211033110341103511036110371103811039110401104111042110431104411045110461104711048110491105011051110521105311054110551105611057110581105911060110611106211063110641106511066110671106811069110701107111072110731107411075110761107711078110791108011081110821108311084110851108611087110881108911090110911109211093110941109511096110971109811099111001110111102111031110411105111061110711108111091111011111111121111311114111151111611117111181111911120111211112211123111241112511126111271112811129111301113111132111331113411135111361113711138111391114011141111421114311144111451114611147111481114911150111511115211153111541115511156111571115811159111601116111162111631116411165111661116711168111691117011171111721117311174111751117611177111781117911180111811118211183111841118511186111871118811189111901119111192111931119411195111961119711198111991120011201112021120311204112051120611207112081120911210112111121211213112141121511216112171121811219112201122111222112231122411225112261122711228112291123011231112321123311234112351123611237112381123911240112411124211243112441124511246112471124811249112501125111252112531125411255112561125711258112591126011261112621126311264112651126611267112681126911270112711127211273112741127511276112771127811279112801128111282112831128411285112861128711288112891129011291112921129311294112951129611297112981129911300113011130211303113041130511306113071130811309113101131111312113131131411315113161131711318113191132011321113221132311324113251132611327113281132911330113311133211333113341133511336113371133811339113401134111342113431134411345113461134711348113491135011351113521135311354113551135611357113581135911360113611136211363113641136511366113671136811369113701137111372113731137411375113761137711378113791138011381113821138311384113851138611387113881138911390113911139211393113941139511396113971139811399114001140111402114031140411405114061140711408114091141011411114121141311414114151141611417114181141911420114211142211423114241142511426114271142811429114301143111432114331143411435114361143711438114391144011441114421144311444114451144611447114481144911450114511145211453114541145511456114571145811459114601146111462114631146411465114661146711468114691147011471114721147311474114751147611477114781147911480114811148211483114841148511486114871148811489114901149111492114931149411495114961149711498114991150011501115021150311504115051150611507115081150911510115111151211513115141151511516115171151811519115201152111522115231152411525115261152711528115291153011531115321153311534115351153611537115381153911540115411154211543115441154511546115471154811549115501155111552115531155411555115561155711558115591156011561115621156311564115651156611567115681156911570115711157211573115741157511576115771157811579115801158111582115831158411585115861158711588115891159011591115921159311594115951159611597115981159911600116011160211603116041160511606116071160811609116101161111612116131161411615116161161711618116191162011621116221162311624116251162611627116281162911630116311163211633116341163511636116371163811639116401164111642116431164411645116461164711648116491165011651116521165311654116551165611657116581165911660116611166211663116641166511666116671166811669116701167111672116731167411675116761167711678116791168011681116821168311684116851168611687116881168911690116911169211693116941169511696116971169811699117001170111702117031170411705117061170711708117091171011711117121171311714117151171611717117181171911720117211172211723117241172511726117271172811729117301173111732117331173411735117361173711738117391174011741117421174311744117451174611747117481174911750117511175211753117541175511756117571175811759117601176111762117631176411765117661176711768117691177011771117721177311774117751177611777117781177911780117811178211783117841178511786117871178811789117901179111792117931179411795117961179711798117991180011801118021180311804118051180611807118081180911810118111181211813118141181511816118171181811819118201182111822118231182411825118261182711828118291183011831118321183311834118351183611837118381183911840118411184211843118441184511846118471184811849118501185111852118531185411855118561185711858118591186011861118621186311864118651186611867118681186911870118711187211873118741187511876118771187811879118801188111882118831188411885118861188711888118891189011891118921189311894118951189611897118981189911900119011190211903119041190511906119071190811909119101191111912119131191411915119161191711918119191192011921119221192311924119251192611927119281192911930119311193211933119341193511936119371193811939119401194111942119431194411945119461194711948119491195011951119521195311954119551195611957119581195911960119611196211963119641196511966119671196811969119701197111972119731197411975119761197711978119791198011981119821198311984119851198611987119881198911990119911199211993119941199511996119971199811999120001200112002120031200412005120061200712008120091201012011120121201312014120151201612017120181201912020120211202212023120241202512026120271202812029120301203112032120331203412035120361203712038120391204012041120421204312044120451204612047120481204912050120511205212053120541205512056120571205812059120601206112062120631206412065120661206712068120691207012071120721207312074120751207612077120781207912080120811208212083120841208512086120871208812089120901209112092120931209412095120961209712098120991210012101121021210312104121051210612107121081210912110121111211212113121141211512116121171211812119121201212112122121231212412125121261212712128121291213012131121321213312134121351213612137121381213912140121411214212143121441214512146121471214812149121501215112152121531215412155121561215712158121591216012161121621216312164121651216612167121681216912170121711217212173121741217512176121771217812179121801218112182121831218412185121861218712188121891219012191121921219312194121951219612197121981219912200122011220212203122041220512206122071220812209122101221112212122131221412215122161221712218122191222012221122221222312224122251222612227122281222912230122311223212233122341223512236122371223812239122401224112242122431224412245122461224712248122491225012251122521225312254122551225612257122581225912260122611226212263122641226512266122671226812269122701227112272122731227412275122761227712278122791228012281122821228312284122851228612287122881228912290122911229212293122941229512296122971229812299123001230112302123031230412305123061230712308123091231012311123121231312314123151231612317123181231912320123211232212323123241232512326123271232812329123301233112332123331233412335123361233712338123391234012341123421234312344123451234612347123481234912350123511235212353123541235512356123571235812359123601236112362123631236412365123661236712368123691237012371123721237312374123751237612377123781237912380123811238212383123841238512386123871238812389123901239112392123931239412395123961239712398123991240012401124021240312404124051240612407124081240912410124111241212413124141241512416124171241812419124201242112422124231242412425124261242712428124291243012431124321243312434124351243612437124381243912440124411244212443124441244512446124471244812449124501245112452124531245412455124561245712458124591246012461124621246312464124651246612467124681246912470124711247212473124741247512476124771247812479124801248112482124831248412485124861248712488124891249012491124921249312494124951249612497124981249912500125011250212503125041250512506125071250812509125101251112512125131251412515125161251712518125191252012521125221252312524125251252612527125281252912530125311253212533125341253512536125371253812539125401254112542125431254412545125461254712548125491255012551125521255312554125551255612557125581255912560125611256212563125641256512566125671256812569125701257112572125731257412575125761257712578125791258012581125821258312584125851258612587125881258912590125911259212593125941259512596125971259812599126001260112602126031260412605126061260712608126091261012611126121261312614126151261612617126181261912620126211262212623126241262512626126271262812629126301263112632126331263412635126361263712638126391264012641126421264312644126451264612647126481264912650126511265212653126541265512656126571265812659126601266112662126631266412665126661266712668126691267012671126721267312674126751267612677126781267912680126811268212683126841268512686126871268812689126901269112692126931269412695126961269712698126991270012701127021270312704127051270612707127081270912710127111271212713127141271512716127171271812719127201272112722127231272412725127261272712728127291273012731127321273312734127351273612737127381273912740127411274212743127441274512746127471274812749127501275112752127531275412755127561275712758127591276012761127621276312764127651276612767127681276912770127711277212773127741277512776127771277812779127801278112782127831278412785127861278712788127891279012791127921279312794127951279612797127981279912800128011280212803128041280512806128071280812809128101281112812128131281412815128161281712818128191282012821128221282312824128251282612827128281282912830128311283212833128341283512836128371283812839128401284112842128431284412845128461284712848128491285012851128521285312854128551285612857128581285912860128611286212863128641286512866128671286812869128701287112872128731287412875128761287712878128791288012881128821288312884128851288612887128881288912890128911289212893128941289512896128971289812899129001290112902129031290412905129061290712908129091291012911129121291312914129151291612917129181291912920129211292212923129241292512926129271292812929129301293112932129331293412935129361293712938129391294012941129421294312944129451294612947129481294912950129511295212953129541295512956129571295812959129601296112962129631296412965129661296712968129691297012971129721297312974129751297612977129781297912980129811298212983129841298512986129871298812989129901299112992129931299412995129961299712998129991300013001130021300313004130051300613007130081300913010130111301213013130141301513016130171301813019130201302113022130231302413025130261302713028130291303013031130321303313034130351303613037130381303913040130411304213043130441304513046130471304813049130501305113052130531305413055130561305713058130591306013061130621306313064130651306613067130681306913070130711307213073130741307513076130771307813079130801308113082130831308413085130861308713088130891309013091130921309313094130951309613097130981309913100131011310213103131041310513106131071310813109131101311113112131131311413115131161311713118131191312013121131221312313124131251312613127131281312913130131311313213133131341313513136131371313813139131401314113142131431314413145131461314713148131491315013151131521315313154131551315613157131581315913160131611316213163131641316513166131671316813169131701317113172131731317413175131761317713178131791318013181131821318313184131851318613187131881318913190131911319213193131941319513196131971319813199132001320113202132031320413205132061320713208132091321013211132121321313214132151321613217132181321913220132211322213223132241322513226132271322813229132301323113232132331323413235132361323713238132391324013241132421324313244132451324613247132481324913250132511325213253132541325513256132571325813259132601326113262132631326413265132661326713268132691327013271132721327313274132751327613277132781327913280132811328213283132841328513286132871328813289132901329113292132931329413295132961329713298132991330013301133021330313304133051330613307133081330913310133111331213313133141331513316133171331813319133201332113322133231332413325133261332713328133291333013331133321333313334133351333613337133381333913340133411334213343133441334513346133471334813349133501335113352133531335413355133561335713358133591336013361133621336313364133651336613367133681336913370133711337213373133741337513376133771337813379133801338113382133831338413385133861338713388133891339013391133921339313394133951339613397133981339913400134011340213403134041340513406134071340813409134101341113412134131341413415134161341713418134191342013421134221342313424134251342613427134281342913430134311343213433134341343513436134371343813439134401344113442134431344413445134461344713448134491345013451134521345313454134551345613457134581345913460134611346213463134641346513466134671346813469134701347113472134731347413475134761347713478134791348013481134821348313484134851348613487134881348913490134911349213493134941349513496134971349813499135001350113502135031350413505135061350713508135091351013511135121351313514135151351613517135181351913520135211352213523135241352513526135271352813529135301353113532135331353413535135361353713538135391354013541135421354313544135451354613547135481354913550135511355213553135541355513556135571355813559135601356113562135631356413565135661356713568135691357013571135721357313574135751357613577135781357913580135811358213583135841358513586135871358813589135901359113592135931359413595135961359713598135991360013601136021360313604136051360613607136081360913610136111361213613136141361513616136171361813619136201362113622136231362413625136261362713628136291363013631136321363313634136351363613637136381363913640136411364213643136441364513646136471364813649136501365113652136531365413655136561365713658136591366013661136621366313664136651366613667136681366913670136711367213673136741367513676136771367813679136801368113682136831368413685136861368713688136891369013691136921369313694136951369613697136981369913700137011370213703137041370513706137071370813709137101371113712137131371413715137161371713718137191372013721137221372313724137251372613727137281372913730137311373213733137341373513736137371373813739137401374113742137431374413745137461374713748137491375013751137521375313754137551375613757137581375913760137611376213763137641376513766137671376813769137701377113772137731377413775137761377713778137791378013781137821378313784137851378613787137881378913790137911379213793137941379513796137971379813799138001380113802138031380413805138061380713808138091381013811138121381313814138151381613817138181381913820138211382213823138241382513826138271382813829138301383113832138331383413835138361383713838138391384013841138421384313844138451384613847138481384913850138511385213853138541385513856138571385813859138601386113862138631386413865138661386713868138691387013871138721387313874138751387613877138781387913880138811388213883138841388513886138871388813889138901389113892138931389413895138961389713898138991390013901139021390313904139051390613907139081390913910139111391213913139141391513916139171391813919139201392113922139231392413925139261392713928139291393013931139321393313934139351393613937139381393913940139411394213943139441394513946139471394813949139501395113952139531395413955139561395713958139591396013961139621396313964139651396613967139681396913970139711397213973139741397513976139771397813979139801398113982139831398413985139861398713988139891399013991139921399313994139951399613997139981399914000140011400214003140041400514006140071400814009140101401114012140131401414015140161401714018140191402014021140221402314024140251402614027140281402914030140311403214033140341403514036140371403814039140401404114042140431404414045140461404714048140491405014051140521405314054140551405614057140581405914060140611406214063140641406514066140671406814069140701407114072140731407414075140761407714078140791408014081140821408314084140851408614087140881408914090140911409214093140941409514096140971409814099141001410114102141031410414105141061410714108141091411014111141121411314114141151411614117141181411914120141211412214123141241412514126141271412814129141301413114132141331413414135141361413714138141391414014141141421414314144141451414614147141481414914150141511415214153141541415514156141571415814159141601416114162141631416414165141661416714168141691417014171141721417314174141751417614177141781417914180141811418214183141841418514186141871418814189141901419114192141931419414195141961419714198141991420014201142021420314204142051420614207142081420914210142111421214213142141421514216142171421814219142201422114222142231422414225142261422714228142291423014231142321423314234142351423614237142381423914240142411424214243142441424514246142471424814249142501425114252142531425414255142561425714258142591426014261142621426314264142651426614267142681426914270142711427214273142741427514276142771427814279142801428114282142831428414285142861428714288142891429014291142921429314294142951429614297142981429914300143011430214303143041430514306143071430814309143101431114312143131431414315143161431714318143191432014321143221432314324143251432614327143281432914330143311433214333143341433514336143371433814339143401434114342143431434414345143461434714348143491435014351143521435314354143551435614357143581435914360143611436214363143641436514366143671436814369143701437114372143731437414375143761437714378143791438014381143821438314384143851438614387143881438914390143911439214393143941439514396143971439814399144001440114402144031440414405144061440714408144091441014411144121441314414144151441614417144181441914420144211442214423144241442514426144271442814429144301443114432144331443414435144361443714438144391444014441144421444314444144451444614447144481444914450144511445214453144541445514456144571445814459144601446114462144631446414465144661446714468144691447014471144721447314474144751447614477144781447914480144811448214483144841448514486144871448814489144901449114492144931449414495144961449714498144991450014501145021450314504145051450614507145081450914510145111451214513145141451514516145171451814519145201452114522145231452414525145261452714528145291453014531145321453314534145351453614537145381453914540145411454214543145441454514546145471454814549145501455114552145531455414555145561455714558145591456014561145621456314564145651456614567145681456914570145711457214573145741457514576145771457814579145801458114582145831458414585145861458714588145891459014591145921459314594145951459614597145981459914600146011460214603146041460514606146071460814609146101461114612146131461414615146161461714618146191462014621146221462314624146251462614627146281462914630146311463214633146341463514636146371463814639146401464114642146431464414645146461464714648146491465014651146521465314654146551465614657146581465914660146611466214663146641466514666146671466814669146701467114672146731467414675146761467714678146791468014681146821468314684146851468614687146881468914690146911469214693146941469514696146971469814699147001470114702147031470414705147061470714708147091471014711147121471314714147151471614717147181471914720147211472214723147241472514726147271472814729147301473114732147331473414735147361473714738147391474014741147421474314744147451474614747147481474914750147511475214753147541475514756147571475814759147601476114762147631476414765147661476714768147691477014771147721477314774147751477614777147781477914780147811478214783147841478514786147871478814789147901479114792147931479414795147961479714798147991480014801148021480314804148051480614807148081480914810148111481214813148141481514816148171481814819148201482114822148231482414825148261482714828148291483014831148321483314834148351483614837148381483914840148411484214843148441484514846148471484814849148501485114852148531485414855148561485714858148591486014861148621486314864148651486614867148681486914870148711487214873148741487514876148771487814879148801488114882148831488414885148861488714888148891489014891148921489314894148951489614897148981489914900149011490214903149041490514906149071490814909149101491114912149131491414915149161491714918149191492014921149221492314924149251492614927149281492914930149311493214933149341493514936149371493814939149401494114942149431494414945149461494714948149491495014951149521495314954149551495614957149581495914960149611496214963149641496514966149671496814969149701497114972149731497414975149761497714978149791498014981149821498314984149851498614987149881498914990149911499214993149941499514996149971499814999150001500115002150031500415005150061500715008150091501015011150121501315014150151501615017150181501915020150211502215023150241502515026150271502815029150301503115032150331503415035150361503715038150391504015041150421504315044150451504615047150481504915050150511505215053150541505515056150571505815059150601506115062150631506415065150661506715068150691507015071150721507315074150751507615077150781507915080150811508215083150841508515086150871508815089150901509115092150931509415095150961509715098150991510015101151021510315104151051510615107151081510915110151111511215113151141511515116151171511815119151201512115122151231512415125151261512715128151291513015131151321513315134151351513615137151381513915140151411514215143151441514515146151471514815149151501515115152151531515415155151561515715158151591516015161151621516315164151651516615167151681516915170151711517215173151741517515176151771517815179151801518115182151831518415185151861518715188151891519015191151921519315194151951519615197151981519915200152011520215203152041520515206152071520815209152101521115212152131521415215152161521715218152191522015221152221522315224152251522615227152281522915230152311523215233152341523515236152371523815239152401524115242152431524415245152461524715248152491525015251152521525315254152551525615257152581525915260152611526215263152641526515266152671526815269152701527115272152731527415275152761527715278152791528015281152821528315284152851528615287152881528915290152911529215293152941529515296152971529815299153001530115302153031530415305153061530715308153091531015311153121531315314153151531615317153181531915320153211532215323153241532515326153271532815329153301533115332153331533415335153361533715338153391534015341153421534315344153451534615347153481534915350153511535215353153541535515356153571535815359153601536115362153631536415365153661536715368153691537015371153721537315374153751537615377
  1. /**
  2. * @file llpipeline.cpp
  3. * @brief Rendering pipeline.
  4. *
  5. * $LicenseInfo:firstyear=2005&license=viewergpl$
  6. *
  7. * Copyright (c) 2005-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 <utility>
  34. #include "GL/smaa.h"
  35. #include "llpipeline.h"
  36. #include "imageids.h"
  37. #include "llaudioengine.h" // For sound beacons
  38. #include "llcubemap.h"
  39. #include "llfasttimer.h"
  40. #include "llrenderutils.h" // For gl_draw_box*()
  41. #include "llagent.h"
  42. #include "llappviewer.h"
  43. #include "lldrawpoolalpha.h"
  44. #include "lldrawpoolavatar.h"
  45. #include "lldrawpoolterrain.h"
  46. #include "lldrawpooltree.h"
  47. #include "lldrawpoolwater.h"
  48. #include "lldrawpoolwlsky.h"
  49. #include "llenvironment.h"
  50. #include "llenvsettings.h"
  51. #include "llface.h"
  52. #include "llfeaturemanager.h"
  53. #include "hbfloatersoundslist.h"
  54. #include "llfloaterstats.h"
  55. #include "llfloatertelehub.h"
  56. #include "llgltfscenemanager.h"
  57. #include "llhudmanager.h"
  58. #include "llhudtext.h"
  59. #include "llmeshrepository.h"
  60. #include "llpanelface.h"
  61. #include "llprefsgraphics.h"
  62. //MK
  63. #include "mkrlinterface.h"
  64. //mk
  65. #include "llselectmgr.h"
  66. #include "llsky.h"
  67. #include "llspatialpartition.h"
  68. #include "llstartup.h"
  69. #include "lltexturecache.h"
  70. #include "lltexturefetch.h"
  71. #include "lltracker.h"
  72. #include "lltool.h"
  73. #include "lltoolmgr.h"
  74. #include "lltoolpie.h"
  75. #include "llviewercamera.h"
  76. #include "llviewercontrol.h"
  77. #include "llviewerdisplay.h"
  78. #include "llviewerjoystick.h"
  79. #include "llviewermediafocus.h"
  80. #include "llviewermenu.h" // For handle_refresh_probes()
  81. #include "llviewerobjectlist.h"
  82. #include "llvieweroctree.h" // For ll_setup_cube_vb()
  83. #include "llviewerparcelmgr.h"
  84. #include "llviewerregion.h"
  85. #include "llviewershadermgr.h"
  86. #include "llviewerwindow.h"
  87. #include "llvoavatarself.h"
  88. #include "llvocache.h"
  89. #include "llvopartgroup.h"
  90. #include "llvosurfacepatch.h"
  91. #include "llvotree.h"
  92. #include "llvovolume.h"
  93. #include "llvowater.h"
  94. #include "llvowlsky.h"
  95. #include "llworld.h"
  96. // Set to 0 to disable optimized deferred shaders uniforms setting
  97. #define OPTIMIZED_UNIFORMS 1
  98. // Set to 1 to debug deferred shaders uniforms setting
  99. #define DEBUG_OPTIMIZED_UNIFORMS 0
  100. LLPipeline gPipeline;
  101. const LLMatrix4* gGLLastMatrix = NULL;
  102. bool gShiftFrame = false;
  103. // Cached settings
  104. bool LLPipeline::RenderDeferred;
  105. F32 LLPipeline::RenderDeferredSunWash;
  106. F32 LLPipeline::RenderDeferredDisplayGamma;
  107. U32 LLPipeline::RenderFSAASamples;
  108. S32 LLPipeline::RenderDeferredAAQuality;
  109. bool LLPipeline::RenderDeferredAASharpen;
  110. U32 LLPipeline::RenderResolutionDivisor;
  111. U32 LLPipeline::RenderShadowDetail;
  112. bool LLPipeline::RenderDeferredSSAO;
  113. bool LLPipeline::RenderShadowSoften;
  114. F32 LLPipeline::RenderShadowResolutionScale;
  115. U32 LLPipeline::RenderLocalLightCount;
  116. bool LLPipeline::RenderDelayCreation;
  117. bool LLPipeline::RenderAnimateRes;
  118. bool LLPipeline::RenderSpotLightsInNondeferred;
  119. LLColor4 LLPipeline::PreviewAmbientColor;
  120. LLColor4 LLPipeline::PreviewDiffuse0;
  121. LLColor4 LLPipeline::PreviewSpecular0;
  122. LLColor4 LLPipeline::PreviewDiffuse1;
  123. LLColor4 LLPipeline::PreviewSpecular1;
  124. LLColor4 LLPipeline::PreviewDiffuse2;
  125. LLColor4 LLPipeline::PreviewSpecular2;
  126. LLVector3 LLPipeline::PreviewDirection0;
  127. LLVector3 LLPipeline::PreviewDirection1;
  128. LLVector3 LLPipeline::PreviewDirection2;
  129. bool LLPipeline::RenderGlow;
  130. F32 LLPipeline::RenderGlowMinLuminance;
  131. F32 LLPipeline::RenderGlowMaxExtractAlpha;
  132. F32 LLPipeline::RenderGlowWarmthAmount;
  133. LLVector3 LLPipeline::RenderGlowLumWeights;
  134. LLVector3 LLPipeline::RenderGlowWarmthWeights;
  135. U32 LLPipeline::RenderGlowResolutionPow;
  136. U32 LLPipeline::RenderGlowIterations;
  137. F32 LLPipeline::RenderGlowWidth;
  138. F32 LLPipeline::RenderGlowStrength;
  139. bool LLPipeline::RenderDepthOfField;
  140. bool LLPipeline::RenderDepthOfFieldInEditMode;
  141. F32 LLPipeline::RenderShadowNoise;
  142. F32 LLPipeline::RenderShadowBlurSize;
  143. F32 LLPipeline::RenderSSAOScale;
  144. U32 LLPipeline::RenderSSAOMaxScale;
  145. F32 LLPipeline::RenderSSAOFactor;
  146. LLVector3 LLPipeline::RenderSSAOEffect;
  147. F32 LLPipeline::RenderShadowBiasError;
  148. F32 LLPipeline::RenderShadowOffset;
  149. F32 LLPipeline::RenderShadowOffsetNoSSAO;
  150. F32 LLPipeline::RenderShadowBias;
  151. F32 LLPipeline::RenderSpotShadowOffset;
  152. F32 LLPipeline::RenderSpotShadowBias;
  153. LLVector3 LLPipeline::RenderShadowGaussian;
  154. F32 LLPipeline::RenderShadowBlurDistFactor;
  155. bool LLPipeline::RenderDeferredAtmospheric;
  156. U32 LLPipeline::RenderWaterReflectionType;
  157. bool LLPipeline::RenderTransparentWater = true;
  158. LLVector3 LLPipeline::RenderShadowClipPlanes;
  159. LLVector3 LLPipeline::RenderShadowOrthoClipPlanes;
  160. F32 LLPipeline::RenderFarClip;
  161. LLVector3 LLPipeline::RenderShadowSplitExponent;
  162. F32 LLPipeline::RenderShadowErrorCutoff;
  163. F32 LLPipeline::RenderShadowFOVCutoff;
  164. bool LLPipeline::CameraOffset;
  165. F32 LLPipeline::CameraMaxCoF;
  166. F32 LLPipeline::CameraDoFResScale;
  167. U32 LLPipeline::RenderAutoHideGeometryMemoryLimit;
  168. F32 LLPipeline::RenderAutoHideSurfaceAreaLimit;
  169. S32 LLPipeline::RenderBufferVisualization;
  170. U32 LLPipeline::RenderScreenSpaceReflectionIterations;
  171. F32 LLPipeline::RenderScreenSpaceReflectionRayStep;
  172. F32 LLPipeline::RenderScreenSpaceReflectionDistanceBias;
  173. F32 LLPipeline::RenderScreenSpaceReflectionDepthRejectBias;
  174. F32 LLPipeline::RenderScreenSpaceReflectionAdaptiveStepMultiplier;
  175. U32 LLPipeline::RenderScreenSpaceReflectionGlossySamples;
  176. S32 LLPipeline::sRenderPBRTerrainDetail = -4;
  177. bool LLPipeline::sRenderPBRTerrain = false;
  178. bool LLPipeline::RenderScreenSpaceReflections = false;
  179. bool LLPipeline::sRenderMirrors = false;
  180. bool LLPipeline::sRenderScriptedBeacons = false;
  181. bool LLPipeline::sRenderScriptedTouchBeacons = false;
  182. bool LLPipeline::sRenderPhysicalBeacons = false;
  183. bool LLPipeline::sRenderPermanentBeacons = false;
  184. bool LLPipeline::sRenderCharacterBeacons = false;
  185. bool LLPipeline::sRenderSoundBeacons = false;
  186. bool LLPipeline::sRenderInvisibleSoundBeacons = false;
  187. bool LLPipeline::sRenderParticleBeacons = false;
  188. bool LLPipeline::sRenderMOAPBeacons = false;
  189. bool LLPipeline::sRenderHighlight = true;
  190. bool LLPipeline::sRenderBeacons = false;
  191. bool LLPipeline::sRenderAttachments = false;
  192. bool LLPipeline::sRenderingHUDs = false;
  193. U32 LLPipeline::sRenderByOwner = 0;
  194. U32 LLPipeline::DebugBeaconLineWidth;
  195. LLRender::eTexIndex LLPipeline::sRenderHighlightTextureChannel = LLRender::DIFFUSE_MAP;
  196. LLVector4a LLPipeline::sWaterPlane;
  197. constexpr F32 LIGHT_FADE_TIME = 0.2f;
  198. constexpr F32 ALPHA_BLEND_CUTOFF = 0.598f;
  199. constexpr U32 AUX_VB_MASK = LLVertexBuffer::MAP_VERTEX |
  200. LLVertexBuffer::MAP_TEXCOORD0 |
  201. LLVertexBuffer::MAP_TEXCOORD1;
  202. static LLDrawable* sRenderSpotLight = NULL;
  203. static LLStaticHashedString sDelta("delta");
  204. static LLStaticHashedString sDistFactor("dist_factor");
  205. static LLStaticHashedString sKern("kern");
  206. static LLStaticHashedString sKernScale("kern_scale");
  207. static LLStaticHashedString sSmaaRTMetrics("SMAA_RT_METRICS");
  208. static LLStaticHashedString sSharpness("sharpen_params");
  209. static LLStaticHashedString sMipLevel("mipLevel");
  210. static LLStaticHashedString sDT("dt");
  211. static LLStaticHashedString sNoiseVec("noiseVec");
  212. static LLStaticHashedString sExpParams("dynamic_exposure_params");
  213. static LLStaticHashedString sExposure("exposure");
  214. static LLStaticHashedString sAcesMix("aces_mix");
  215. static LLStaticHashedString sIrradianceScale("ssao_irradiance_scale");
  216. static LLStaticHashedString sIrradianceMax("ssao_irradiance_max");
  217. static LLStaticHashedString sAboveWater("above_water");
  218. static LLStaticHashedString sLuminanceScale("diffuse_luminance_scale");
  219. std::string gPoolNames[LLDrawPool::NUM_POOL_TYPES] =
  220. {
  221. // Correspond to LLDrawpool enum render type
  222. "NONE",
  223. "POOL_SKY",
  224. "POOL_WL_SKY",
  225. "POOL_SIMPLE",
  226. "POOL_FULLBRIGHT",
  227. "POOL_BUMP",
  228. "POOL_TERRAIN,"
  229. "POOL_MATERIALS",
  230. "POOL_MAT_PBR",
  231. "POOL_GRASS",
  232. "POOL_MAT_PBR_ALPHA_MASK",
  233. "POOL_TREE",
  234. "POOL_ALPHA_MASK",
  235. "POOL_FULLBRIGHT_ALPHA_MASK",
  236. "POOL_INVISIBLE",
  237. "POOL_AVATAR",
  238. "POOL_PUPPET",
  239. "POOL_GLOW",
  240. "POOL_ALPHA_PRE_WATER",
  241. "POOL_VOIDWATER",
  242. "POOL_WATER",
  243. "POOL_ALPHA_POST_WATER",
  244. "POOL_ALPHA"
  245. };
  246. bool LLPipeline::sFreezeTime = false;
  247. bool LLPipeline::sPickAvatar = true;
  248. bool LLPipeline::sDynamicLOD = true;
  249. bool LLPipeline::sShowHUDAttachments = true;
  250. bool LLPipeline::sRenderBeaconsFloaterOpen = false;
  251. bool LLPipeline::sAutoMaskAlphaDeferred = true;
  252. bool LLPipeline::sAutoMaskAlphaNonDeferred = false;
  253. bool LLPipeline::sUseFarClip = true;
  254. bool LLPipeline::sShadowRender = false;
  255. bool LLPipeline::sCanRenderGlow = false;
  256. bool LLPipeline::sReflectionRender = false;
  257. bool LLPipeline::sImpostorRender = false;
  258. bool LLPipeline::sImpostorRenderAlphaDepthPass = false;
  259. bool LLPipeline::sAvatarPreviewRender = false;
  260. bool LLPipeline::sUnderWaterRender = false;
  261. bool LLPipeline::sRenderFrameTest = false;
  262. bool LLPipeline::sRenderAttachedLights = true;
  263. bool LLPipeline::sRenderAttachedParticles = true;
  264. bool LLPipeline::sRenderDeferred = false;
  265. bool LLPipeline::sRenderWater = true;
  266. bool LLPipeline::sReflectionProbesEnabled = false;
  267. S32 LLPipeline::sUseOcclusion = 0;
  268. S32 LLPipeline::sVisibleLightCount = 0;
  269. // Per-frame materials usage flags. HB
  270. bool LLPipeline::sUsingALM = false;
  271. bool LLPipeline::sUsingPBR = false;
  272. bool LLPipeline::sLacksLegacyFallback = false;
  273. bool LLPipeline::sLacksBaseColorFallback = false;
  274. LLCullResult* LLPipeline::sCull = NULL;
  275. static const LLMatrix4a TRANS_MAT(LLVector4a(0.5f, 0.f, 0.f, 0.f),
  276. LLVector4a(0.f, 0.5f, 0.f, 0.f),
  277. LLVector4a(0.f, 0.f, 0.5f, 0.f),
  278. LLVector4a(0.5f, 0.5f, 0.5f, 1.f));
  279. // Utility functions only used here
  280. static LLMatrix4a look_proj(const LLVector3& pos_in, const LLVector3& dir_in,
  281. const LLVector3& up_in)
  282. {
  283. const LLVector4a pos(pos_in.mV[VX], pos_in.mV[VY], pos_in.mV[VZ], 1.f);
  284. LLVector4a dir(dir_in.mV[VX], dir_in.mV[VY], dir_in.mV[VZ]);
  285. const LLVector4a up(up_in.mV[VX], up_in.mV[VY], up_in.mV[VZ]);
  286. LLVector4a left_norm;
  287. left_norm.setCross3(dir, up);
  288. left_norm.normalize3fast();
  289. LLVector4a up_norm;
  290. up_norm.setCross3(left_norm, dir);
  291. up_norm.normalize3fast();
  292. LLVector4a& dir_norm = dir;
  293. dir.normalize3fast();
  294. LLVector4a left_dot;
  295. left_dot.setAllDot3(left_norm, pos);
  296. left_dot.negate();
  297. LLVector4a up_dot;
  298. up_dot.setAllDot3(up_norm, pos);
  299. up_dot.negate();
  300. LLVector4a dir_dot;
  301. dir_dot.setAllDot3(dir_norm, pos);
  302. dir_norm.negate();
  303. LLMatrix4a ret;
  304. ret.setRow<0>(left_norm);
  305. ret.setRow<1>(up_norm);
  306. ret.setRow<2>(dir_norm);
  307. ret.setRow<3>(LLVector4a(0.f, 0.f, 0.f, 1.f));
  308. ret.getRow<0>().copyComponent<3>(left_dot);
  309. ret.getRow<1>().copyComponent<3>(up_dot);
  310. ret.getRow<2>().copyComponent<3>(dir_dot);
  311. ret.transpose();
  312. return ret;
  313. }
  314. static bool addDeferredAttachments(LLRenderTarget& target)
  315. {
  316. if (gUsePBRShaders)
  317. {
  318. // frag-data[1] specular OR PBR ORM
  319. return target.addColorAttachment(GL_RGBA) &&
  320. // frag_data[2] normal+z+fogmask,
  321. // See: class1/deferred/materialF.glsl & softenlight
  322. target.addColorAttachment(GL_RGBA16F) &&
  323. // frag_data[3] PBR emissive
  324. target.addColorAttachment(GL_RGB16F);
  325. }
  326. return target.addColorAttachment(GL_SRGB8_ALPHA8) && // Specular
  327. target.addColorAttachment(GL_RGBA12); // Normal + z
  328. }
  329. LLPipeline::LLPipeline()
  330. : mNeedsDrawStats(false),
  331. mPoissonOffset(0),
  332. mBatchCount(0),
  333. mMatrixOpCount(0),
  334. mTextureMatrixOps(0),
  335. mMaxBatchSize(0),
  336. mMinBatchSize(0),
  337. mTrianglesDrawn(0),
  338. mNumVisibleNodes(0),
  339. mInitialized(false),
  340. mVertexShadersLoaded(-1),
  341. mRenderDebugFeatureMask(0),
  342. mRenderDebugMask(0),
  343. mOldRenderDebugMask(0),
  344. mMeshDirtyQueryObject(0),
  345. mGroupQLocked(false),
  346. mResetVertexBuffers(false),
  347. mLastRebuildPool(NULL),
  348. mAlphaPool(NULL),
  349. mAlphaPoolPreWater(NULL),
  350. mAlphaPoolPostWater(NULL),
  351. mSkyPool(NULL),
  352. mTerrainPool(NULL),
  353. mWaterPool(NULL),
  354. mSimplePool(NULL),
  355. mGrassPool(NULL),
  356. mAlphaMaskPool(NULL),
  357. mFullbrightAlphaMaskPool(NULL),
  358. mFullbrightPool(NULL),
  359. mInvisiblePool(NULL),
  360. mGlowPool(NULL),
  361. mBumpPool(NULL),
  362. mMaterialsPool(NULL),
  363. mWLSkyPool(NULL),
  364. mPBROpaquePool(NULL),
  365. mPBRAlphaMaskPool(NULL),
  366. mLightMask(0),
  367. mNoiseMap(0),
  368. mTrueNoiseMap(0),
  369. mAreaMap(0),
  370. mSearchMap(0),
  371. mLightFunc(0),
  372. mProbeAmbiance(0.f),
  373. mSkyGamma(1.f),
  374. mEyeAboveWater(0.f),
  375. mWaterHeight(0.f),
  376. mIsSunUp(true),
  377. mIsMoonUp(false)
  378. {
  379. }
  380. void LLPipeline::connectRefreshCachedSettingsSafe(const char* name)
  381. {
  382. LLPointer<LLControlVariable> cvp = gSavedSettings.getControl(name);
  383. if (cvp.isNull())
  384. {
  385. llwarns << "Global setting name not found: " << name << llendl;
  386. return;
  387. }
  388. cvp->getSignal()->connect(boost::bind(&LLPipeline::refreshCachedSettings));
  389. }
  390. void LLPipeline::createAuxVBs()
  391. {
  392. mCubeVB = new LLVertexBuffer(LLVertexBuffer::MAP_VERTEX);
  393. if (!ll_setup_cube_vb(mCubeVB))
  394. {
  395. llwarns << "Could not setup a cube VB !" << llendl;
  396. mCubeVB = NULL;
  397. }
  398. mDeferredVB = new LLVertexBuffer(AUX_VB_MASK);
  399. mDeferredVB->allocateBuffer(8, 0);
  400. mScreenTriangleVB = new LLVertexBuffer(LLVertexBuffer::MAP_VERTEX);
  401. mScreenTriangleVB->allocateBuffer(3, 0);
  402. LLStrider<LLVector3> v;
  403. if (mScreenTriangleVB->getVertexStrider(v))
  404. {
  405. v[0].set(-1.f, 1.f, 0.f);
  406. v[1].set(-1.f, -3.f, 0.f);
  407. v[2].set(3.f, 1.f, 0.f);
  408. }
  409. else
  410. {
  411. llwarns << "Could not initialize mScreenTriangleVB strider !"
  412. << llendl;
  413. }
  414. mScreenTriangleVB->unmapBuffer();
  415. if (!gUsePBRShaders)
  416. {
  417. mGlowCombineVB = new LLVertexBuffer(AUX_VB_MASK);
  418. mGlowCombineVB->allocateBuffer(3, 0);
  419. LLStrider<LLVector3> v;
  420. LLStrider<LLVector2> uv1;
  421. if (mGlowCombineVB->getVertexStrider(v) &&
  422. mGlowCombineVB->getTexCoord0Strider(uv1))
  423. {
  424. uv1[0].clear();
  425. uv1[1].set(0.f, 2.f);
  426. uv1[2].set(2.f, 0.f);
  427. v[0].set(-1.f, -1.f, 0.f);
  428. v[1].set(-1.f, 3.f, 0.f);
  429. v[2].set(3.f, -1.f, 0.f);
  430. }
  431. else
  432. {
  433. llwarns << "Could not initialize mGlowCombineVB striders !"
  434. << llendl;
  435. }
  436. mGlowCombineVB->unmapBuffer();
  437. }
  438. #if LL_DEBUG_VB_ALLOC
  439. if (mCubeVB)
  440. {
  441. mCubeVB->setOwner("LLPipeline cube VB");
  442. }
  443. mDeferredVB->setOwner("LLPipeline deferred VB");
  444. if (mGlowCombineVB)
  445. {
  446. mGlowCombineVB->setOwner("LLPipeline glow combine VB");
  447. }
  448. if (mScreenTriangleVB)
  449. {
  450. mScreenTriangleVB->setOwner("LLPipeline screen triangle VB");
  451. }
  452. #endif
  453. }
  454. void LLPipeline::init()
  455. {
  456. mRT = &mMainRT;
  457. // The following three lines used to be in llappviewer.cpp, in
  458. // settings_to_globals(). HB
  459. sRenderDeferred = gUsePBRShaders ||
  460. gSavedSettings.getBool("RenderDeferred");
  461. LLRenderTarget::sUseFBO = sRenderDeferred;
  462. #if 1 // This should only taken into account after a restart, thus why it
  463. // is set here. HB
  464. RenderFSAASamples = llmin((U32)gSavedSettings.getU32("RenderFSAASamples"),
  465. 16U);
  466. #endif
  467. refreshCachedSettings();
  468. gOctreeMaxCapacity = gSavedSettings.getU32("OctreeMaxNodeCapacity");
  469. gOctreeMinSize = gSavedSettings.getF32("OctreeMinimumNodeSize");
  470. sDynamicLOD = gSavedSettings.getBool("RenderDynamicLOD");
  471. sRenderAttachedLights = gSavedSettings.getBool("RenderAttachedLights");
  472. sRenderAttachedParticles =
  473. gSavedSettings.getBool("RenderAttachedParticles");
  474. sAutoMaskAlphaDeferred =
  475. gSavedSettings.getBool("RenderAutoMaskAlphaDeferred");
  476. sAutoMaskAlphaNonDeferred =
  477. gSavedSettings.getBool("RenderAutoMaskAlphaNonDeferred");
  478. if (gFeatureManager.isFeatureAvailable("RenderCompressTextures"))
  479. {
  480. LLImageGL::sCompressTextures =
  481. gGLManager.mGLVersion >= 2.1f &&
  482. gSavedSettings.getBool("RenderCompressTextures");
  483. LLImageGL::sCompressThreshold =
  484. gSavedSettings.getU32("RenderCompressThreshold");
  485. }
  486. // Create render pass pools
  487. if (gUsePBRShaders)
  488. {
  489. getPool(LLDrawPool::POOL_ALPHA_PRE_WATER);
  490. getPool(LLDrawPool::POOL_ALPHA_POST_WATER);
  491. }
  492. else
  493. {
  494. getPool(LLDrawPool::POOL_ALPHA);
  495. }
  496. getPool(LLDrawPool::POOL_SIMPLE);
  497. getPool(LLDrawPool::POOL_ALPHA_MASK);
  498. getPool(LLDrawPool::POOL_FULLBRIGHT_ALPHA_MASK);
  499. getPool(LLDrawPool::POOL_GRASS);
  500. getPool(LLDrawPool::POOL_FULLBRIGHT);
  501. if (!gUsePBRShaders)
  502. {
  503. getPool(LLDrawPool::POOL_INVISIBLE);
  504. }
  505. getPool(LLDrawPool::POOL_BUMP);
  506. getPool(LLDrawPool::POOL_MATERIALS);
  507. getPool(LLDrawPool::POOL_GLOW);
  508. if (gUsePBRShaders)
  509. {
  510. getPool(LLDrawPool::POOL_MAT_PBR);
  511. getPool(LLDrawPool::POOL_MAT_PBR_ALPHA_MASK);
  512. }
  513. mTrianglesDrawnStat.reset();
  514. resetFrameStats();
  515. setAllRenderTypes(); // All rendering types start enabled
  516. mRenderDebugFeatureMask = 0xffffffff; // All debugging features on
  517. mRenderDebugMask = 0; // All debug starts off
  518. mOldRenderDebugMask = mRenderDebugMask;
  519. for (U32 i = 0; i < 2; ++i)
  520. {
  521. mSpotLightFade[i] = 1.f;
  522. }
  523. createAuxVBs();
  524. // Enable features
  525. // Must be set before calling setShaders().
  526. mInitialized = true;
  527. // Note: this will set mVertexShadersLoaded to 1 if basic shaders get
  528. // successfully loaded, or to -1 on failure.
  529. gViewerShaderMgrp->setShaders();
  530. stop_glerror();
  531. if (!gSavedSettings.getBool("SkipStaticVectorSizing"))
  532. {
  533. // Reserve some space in permanent vectors to avoid fragmentation,
  534. // based on the statistics we got for real sessions.
  535. // By setting SkipStaticVectorSizing to true (and restarting the
  536. // viewer), you may skip this pre-sizing of vectors so to verify (via
  537. // "Advanced" -> "Consoles" -> "Info to Debug Console" ->
  538. // "Memory Stats") what capacity is naturally reached during a session
  539. // and check it against the capacity reserved below (this is how I
  540. // determined the suitable values). HB
  541. mMovedList.reserve(1024);
  542. mMovedBridge.reserve(1024);
  543. mGroupQ.reserve(8192);
  544. mMeshDirtyGroup.reserve(2048);
  545. mShiftList.reserve(65536);
  546. }
  547. // Register settings callbacks
  548. connectRefreshCachedSettingsSafe("RenderAutoMaskAlphaDeferred");
  549. connectRefreshCachedSettingsSafe("RenderAutoMaskAlphaNonDeferred");
  550. connectRefreshCachedSettingsSafe("RenderUseFarClip");
  551. connectRefreshCachedSettingsSafe("UseOcclusion");
  552. connectRefreshCachedSettingsSafe("RenderDeferred");
  553. connectRefreshCachedSettingsSafe("RenderDeferredSunWash");
  554. connectRefreshCachedSettingsSafe("RenderDeferredAASharpen");
  555. connectRefreshCachedSettingsSafe("RenderResolutionDivisor");
  556. connectRefreshCachedSettingsSafe("RenderShadowResolutionScale");
  557. connectRefreshCachedSettingsSafe("RenderDelayCreation");
  558. connectRefreshCachedSettingsSafe("RenderAnimateRes");
  559. connectRefreshCachedSettingsSafe("RenderLocalLightCount");
  560. connectRefreshCachedSettingsSafe("RenderSpotLightsInNondeferred");
  561. connectRefreshCachedSettingsSafe("PreviewAmbientColor");
  562. connectRefreshCachedSettingsSafe("PreviewDiffuse0");
  563. connectRefreshCachedSettingsSafe("PreviewSpecular0");
  564. connectRefreshCachedSettingsSafe("PreviewDiffuse1");
  565. connectRefreshCachedSettingsSafe("PreviewSpecular1");
  566. connectRefreshCachedSettingsSafe("PreviewDiffuse2");
  567. connectRefreshCachedSettingsSafe("PreviewSpecular2");
  568. connectRefreshCachedSettingsSafe("PreviewDirection0");
  569. connectRefreshCachedSettingsSafe("PreviewDirection1");
  570. connectRefreshCachedSettingsSafe("PreviewDirection2");
  571. connectRefreshCachedSettingsSafe("RenderGlowMinLuminance");
  572. connectRefreshCachedSettingsSafe("RenderGlowMaxExtractAlpha");
  573. connectRefreshCachedSettingsSafe("RenderGlowWarmthAmount");
  574. connectRefreshCachedSettingsSafe("RenderGlowLumWeights");
  575. connectRefreshCachedSettingsSafe("RenderGlowWarmthWeights");
  576. connectRefreshCachedSettingsSafe("RenderGlowIterations");
  577. connectRefreshCachedSettingsSafe("RenderGlowWidth");
  578. connectRefreshCachedSettingsSafe("RenderGlowStrength");
  579. connectRefreshCachedSettingsSafe("RenderDepthOfFieldInEditMode");
  580. connectRefreshCachedSettingsSafe("RenderHeroProbeResLevel");
  581. connectRefreshCachedSettingsSafe("RenderShadowNoise");
  582. connectRefreshCachedSettingsSafe("RenderShadowBlurSize");
  583. connectRefreshCachedSettingsSafe("RenderShadowSoften");
  584. connectRefreshCachedSettingsSafe("RenderSSAOScale");
  585. connectRefreshCachedSettingsSafe("RenderSSAOMaxScale");
  586. connectRefreshCachedSettingsSafe("RenderSSAOFactor");
  587. connectRefreshCachedSettingsSafe("RenderSSAOEffect");
  588. connectRefreshCachedSettingsSafe("RenderShadowBiasError");
  589. connectRefreshCachedSettingsSafe("RenderShadowOffset");
  590. connectRefreshCachedSettingsSafe("RenderShadowOffsetNoSSAO");
  591. connectRefreshCachedSettingsSafe("RenderShadowBias");
  592. connectRefreshCachedSettingsSafe("RenderSpotShadowOffset");
  593. connectRefreshCachedSettingsSafe("RenderSpotShadowBias");
  594. connectRefreshCachedSettingsSafe("RenderShadowGaussian");
  595. connectRefreshCachedSettingsSafe("RenderShadowBlurDistFactor");
  596. connectRefreshCachedSettingsSafe("RenderDeferredAtmospheric");
  597. connectRefreshCachedSettingsSafe("RenderShadowClipPlanes");
  598. connectRefreshCachedSettingsSafe("RenderShadowOrthoClipPlanes");
  599. connectRefreshCachedSettingsSafe("RenderFarClip");
  600. connectRefreshCachedSettingsSafe("RenderShadowSplitExponent");
  601. connectRefreshCachedSettingsSafe("RenderShadowErrorCutoff");
  602. connectRefreshCachedSettingsSafe("RenderShadowFOVCutoff");
  603. connectRefreshCachedSettingsSafe("CameraOffset");
  604. connectRefreshCachedSettingsSafe("CameraMaxCoF");
  605. connectRefreshCachedSettingsSafe("CameraDoFResScale");
  606. connectRefreshCachedSettingsSafe("RenderAutoHideGeometryMemoryLimit");
  607. connectRefreshCachedSettingsSafe("RenderAutoHideSurfaceAreaLimit");
  608. connectRefreshCachedSettingsSafe("RenderWater");
  609. // PBR related settings
  610. connectRefreshCachedSettingsSafe("RenderBufferVisualization");
  611. connectRefreshCachedSettingsSafe("RenderScreenSpaceReflections");
  612. connectRefreshCachedSettingsSafe("RenderScreenSpaceReflectionIterations");
  613. connectRefreshCachedSettingsSafe("RenderScreenSpaceReflectionRayStep");
  614. connectRefreshCachedSettingsSafe("RenderScreenSpaceReflectionDistanceBias");
  615. connectRefreshCachedSettingsSafe("RenderScreenSpaceReflectionDepthRejectBias");
  616. connectRefreshCachedSettingsSafe("RenderScreenSpaceReflectionAdaptiveStepMultiplier");
  617. connectRefreshCachedSettingsSafe("RenderScreenSpaceReflectionGlossySamples");
  618. // Beacons stuff
  619. connectRefreshCachedSettingsSafe("scriptsbeacon");
  620. connectRefreshCachedSettingsSafe("scripttouchbeacon");
  621. connectRefreshCachedSettingsSafe("physicalbeacon");
  622. connectRefreshCachedSettingsSafe("permanentbeacon");
  623. connectRefreshCachedSettingsSafe("characterbeacon");
  624. connectRefreshCachedSettingsSafe("soundsbeacon");
  625. connectRefreshCachedSettingsSafe("invisiblesoundsbeacon");
  626. connectRefreshCachedSettingsSafe("particlesbeacon");
  627. connectRefreshCachedSettingsSafe("moapbeacon");
  628. connectRefreshCachedSettingsSafe("renderhighlights");
  629. connectRefreshCachedSettingsSafe("renderbeacons");
  630. connectRefreshCachedSettingsSafe("renderattachment");
  631. connectRefreshCachedSettingsSafe("renderbyowner");
  632. connectRefreshCachedSettingsSafe("DebugBeaconLineWidth");
  633. }
  634. // This must be called at the very start of a render frame. HB
  635. void LLPipeline::toggleRenderer()
  636. {
  637. // Force a GL states check here.
  638. bool old_debug_gl = gDebugGL;
  639. gDebugGL = true;
  640. LL_GL_CHECK_STATES;
  641. gDebugGL = old_debug_gl;
  642. // First, cleanup everything.
  643. GLbitfield mask = GL_DEPTH_BUFFER_BIT | GL_COLOR_BUFFER_BIT;
  644. if (!gUsePBRShaders)
  645. {
  646. mask |= GL_STENCIL_BUFFER_BIT;
  647. }
  648. glClear(mask);
  649. mResetVertexBuffers = true;
  650. doResetVertexBuffers(true);
  651. releaseGLBuffers();
  652. LLRenderTarget::reset();
  653. gCubeSnapshot = false; // Paranoia (should already be false).
  654. while (true) // Avoids a goto.
  655. {
  656. // Toggle now.
  657. gUsePBRShaders = !gUsePBRShaders;
  658. llinfos << "Toggling the renderer to the "
  659. << (gUsePBRShaders ? "PBR" : "EE") << " mode..." << llendl;
  660. // Resync render pass pools
  661. if (gUsePBRShaders)
  662. {
  663. removePool(mAlphaPool);
  664. removePool(mInvisiblePool);
  665. getPool(LLDrawPool::POOL_ALPHA_PRE_WATER);
  666. getPool(LLDrawPool::POOL_ALPHA_POST_WATER);
  667. getPool(LLDrawPool::POOL_MAT_PBR);
  668. getPool(LLDrawPool::POOL_MAT_PBR_ALPHA_MASK);
  669. }
  670. else
  671. {
  672. mReflectionMapManager.cleanup();
  673. mHeroProbeManager.cleanup();
  674. removePool(mAlphaPoolPreWater);
  675. removePool(mAlphaPoolPostWater);
  676. removePool(mPBROpaquePool);
  677. removePool(mPBRAlphaMaskPool);
  678. getPool(LLDrawPool::POOL_ALPHA);
  679. getPool(LLDrawPool::POOL_INVISIBLE);
  680. }
  681. // Do not keep a pointer for a pool that might have been removed...
  682. mLastRebuildPool = NULL;
  683. // Reload/resync the shaders (also refreshes the pipeline cached
  684. // settings and recreates GL buffers on success).
  685. gViewerShaderMgrp->setShaders();
  686. if (gUsePBRShaders && !LLViewerShaderMgr::sInitialized)
  687. {
  688. // We failed to load the mandatory PBR shaders, and we need to
  689. // switch back to the EE renderer immediately (otherwise we would
  690. // crash later on while rendering).
  691. gSavedSettings.setBool("RenderUsePBR", false); // Resync setting
  692. // Must be set back to true so that setShaders() can proceed.
  693. LLViewerShaderMgr::sInitialized = true;
  694. continue; // Loop back...
  695. }
  696. break; // Success. Exit the "loop".
  697. }
  698. // Refresh again graphics preferences (when open) after shader loading
  699. // since the status of some check boxes depends on what got actually
  700. // loaded. HB
  701. LLPrefsGraphics::refresh();
  702. // Rebuild everything
  703. mResetVertexBuffers = true;
  704. doResetVertexBuffers(true);
  705. // Force-rebuild all objects in the render pipeline.
  706. for (S32 i = 0, count = gObjectList.getNumObjects(); i < count; ++i)
  707. {
  708. LLViewerObject* objectp = gObjectList.getObject(i);
  709. if (objectp && !objectp->isDead())
  710. {
  711. objectp->markForUpdate(true);
  712. }
  713. }
  714. // Force-rebuild all the draw info.
  715. rebuildDrawInfo();
  716. // Rebuild draw pools
  717. rebuildPools();
  718. // Force a GL states check here.
  719. gDebugGL = true;
  720. LL_GL_CHECK_STATES;
  721. gDebugGL = old_debug_gl;
  722. if (gUsePBRShaders)
  723. {
  724. // Force a reflection probes rebuild.
  725. handle_refresh_probes(NULL);
  726. }
  727. }
  728. void LLPipeline::cleanup()
  729. {
  730. llinfos << "Total number of occlusion query timeouts: "
  731. << LLOcclusionCullingGroup::getTimeouts() << llendl;
  732. mGroupQ.clear();
  733. mGroupSaveQ.clear();
  734. for (pool_set_t::iterator iter = mPools.begin(),
  735. end = mPools.end();
  736. iter != end; )
  737. {
  738. pool_set_t::iterator curiter = iter++;
  739. LLDrawPool* poolp = *curiter;
  740. if (!poolp) // Paranoia
  741. {
  742. llwarns << "Found a NULL pool !" << llendl;
  743. continue;
  744. }
  745. if (poolp->isFacePool())
  746. {
  747. LLFacePool* face_pool = (LLFacePool*)poolp;
  748. if (face_pool->mReferences.empty())
  749. {
  750. mPools.erase(curiter);
  751. removeFromQuickLookup(poolp);
  752. delete poolp;
  753. }
  754. }
  755. else
  756. {
  757. mPools.erase(curiter);
  758. removeFromQuickLookup(poolp);
  759. delete poolp;
  760. }
  761. }
  762. if (!mTerrainPools.empty())
  763. {
  764. llwarns << "Terrain pools not cleaned up" << llendl;
  765. }
  766. if (!mTreePools.empty())
  767. {
  768. llwarns << "Tree pools not cleaned up" << llendl;
  769. }
  770. delete mAlphaPool;
  771. mAlphaPool = NULL;
  772. delete mAlphaPoolPreWater;
  773. mAlphaPoolPreWater = NULL;
  774. delete mAlphaPoolPostWater;
  775. mAlphaPoolPostWater = NULL;
  776. delete mSkyPool;
  777. mSkyPool = NULL;
  778. delete mTerrainPool;
  779. mTerrainPool = NULL;
  780. delete mWaterPool;
  781. mWaterPool = NULL;
  782. delete mSimplePool;
  783. mSimplePool = NULL;
  784. delete mFullbrightPool;
  785. mFullbrightPool = NULL;
  786. delete mInvisiblePool;
  787. mInvisiblePool = NULL;
  788. delete mGlowPool;
  789. mGlowPool = NULL;
  790. delete mBumpPool;
  791. mBumpPool = NULL;
  792. #if 0 // Do not delete WL sky pool: already done above in the for loop.
  793. delete mWLSkyPool;
  794. #endif
  795. mWLSkyPool = NULL;
  796. delete mPBROpaquePool;
  797. mPBROpaquePool = NULL;
  798. delete mPBRAlphaMaskPool;
  799. mPBRAlphaMaskPool = NULL;
  800. releaseGLBuffers();
  801. mFaceSelectImagep = NULL;
  802. mMovedList.clear();
  803. mMovedBridge.clear();
  804. mShiftList.clear();
  805. mMeshDirtyGroup.clear();
  806. mInitialized = false;
  807. mDeferredVB = mGlowCombineVB = mCubeVB = mScreenTriangleVB = NULL;
  808. mReflectionMapManager.cleanup();
  809. mHeroProbeManager.cleanup();
  810. }
  811. void LLPipeline::dumpStats()
  812. {
  813. llinfos << "mMovedList vector capacity reached: " << mMovedList.capacity()
  814. << " - mMovedBridge vector capacity reached: "
  815. << mMovedBridge.capacity()
  816. << " - mShiftList vector capacity reached: "
  817. << mShiftList.capacity()
  818. << " - mGroupQ vector capacity reached: " << mGroupQ.capacity()
  819. << " - mMeshDirtyGroup vector capacity reached: "
  820. << mMeshDirtyGroup.capacity() << llendl;
  821. }
  822. void LLPipeline::destroyGL()
  823. {
  824. unloadShaders();
  825. mHighlightFaces.clear();
  826. resetDrawOrders();
  827. resetVertexBuffers();
  828. releaseGLBuffers();
  829. if (mMeshDirtyQueryObject)
  830. {
  831. glDeleteQueries(1, &mMeshDirtyQueryObject);
  832. mMeshDirtyQueryObject = 0;
  833. }
  834. stop_glerror();
  835. }
  836. void LLPipeline::resizeShadowTexture()
  837. {
  838. gResizeShadowTexture = false;
  839. releaseShadowTargets();
  840. allocateShadowBuffer(mRT->mWidth, mRT->mHeight);
  841. }
  842. void LLPipeline::resizeScreenTexture()
  843. {
  844. static U32 res_divisor = 0;
  845. LL_FAST_TIMER(FTM_RESIZE_SCREEN_TEXTURE);
  846. gResizeScreenTexture = false;
  847. if (shadersLoaded())
  848. {
  849. GLuint res_x = gViewerWindowp->getWindowDisplayWidth();
  850. GLuint res_y = gViewerWindowp->getWindowDisplayHeight();
  851. if (res_x != mRT->mScreen.getWidth() ||
  852. res_y != mRT->mScreen.getHeight() ||
  853. res_divisor != RenderResolutionDivisor)
  854. {
  855. res_divisor = RenderResolutionDivisor;
  856. releaseScreenBuffers();
  857. allocateScreenBuffer(res_x, res_y);
  858. }
  859. }
  860. }
  861. void LLPipeline::allocatePhysicsBuffer()
  862. {
  863. GLuint res_x = gViewerWindowp->getWindowDisplayWidth();
  864. GLuint res_y = gViewerWindowp->getWindowDisplayHeight();
  865. if (mPhysicsDisplay.getWidth() != res_x ||
  866. mPhysicsDisplay.getHeight() != res_y)
  867. {
  868. mPhysicsDisplay.release();
  869. mPhysicsDisplay.allocate(res_x, res_y, GL_RGBA, true, false,
  870. LLTexUnit::TT_RECT_TEXTURE);
  871. }
  872. }
  873. void LLPipeline::allocateScreenBuffer(U32 res_x, U32 res_y)
  874. {
  875. refreshCachedSettings();
  876. U32 samples = RenderFSAASamples;
  877. // Try to allocate screen buffers at requested resolution and samples:
  878. // - on failure, shrink number of samples and try again
  879. // - if not multisampled, shrink resolution and try again (favor X
  880. // resolution over Y)
  881. // Make sure to call "releaseScreenBuffers" after each failure to cleanup
  882. // the partially loaded state
  883. if (!allocateScreenBuffer(res_x, res_y, samples))
  884. {
  885. releaseScreenBuffers();
  886. // Reduce number of samples
  887. while (samples > 0)
  888. {
  889. samples /= 2;
  890. if (allocateScreenBuffer(res_x, res_y, samples))
  891. {
  892. return; // success
  893. }
  894. releaseScreenBuffers();
  895. }
  896. samples = 0;
  897. // Reduce resolution
  898. while (res_y > 0 && res_x > 0)
  899. {
  900. res_y /= 2;
  901. if (allocateScreenBuffer(res_x, res_y, samples))
  902. {
  903. return;
  904. }
  905. releaseScreenBuffers();
  906. res_x /= 2;
  907. if (allocateScreenBuffer(res_x, res_y, samples))
  908. {
  909. return;
  910. }
  911. releaseScreenBuffers();
  912. }
  913. llwarns << "Unable to allocate screen buffer at any resolution !"
  914. << llendl;
  915. }
  916. }
  917. bool LLPipeline::allocateScreenBuffer(U32 res_x, U32 res_y, U32 samples)
  918. {
  919. refreshCachedSettings();
  920. llinfos << "Allocating ";
  921. if (gCubeSnapshot)
  922. {
  923. llcont << "auxillary ";
  924. }
  925. llcont << "target buffers at size " << res_x << "x" << res_y
  926. << " with " << samples << " samples..." << llendl;
  927. // *HACK: allocate auxillary PBR buffers when mRT == &mMainRT.
  928. // Note: we now always need auxillary buffers (even when reflection probes
  929. // are disabled), because they are used for dynamic textures and PBR
  930. // materials preview... HB
  931. if (gUsePBRShaders && mRT == &mMainRT)
  932. {
  933. U32 res = 512;
  934. if (sReflectionProbesEnabled)
  935. {
  936. mReflectionMapManager.initReflectionMaps();
  937. // Multiply by 4 because probes will be 16x super sampled
  938. res = mReflectionMapManager.mProbeResolution * 4;
  939. }
  940. // Note: gCubeSnapshot acts as a flag for the auxilliary buffers
  941. // allocation step. HB
  942. gCubeSnapshot = true;
  943. mRT = &mAuxillaryRT;
  944. allocateScreenBuffer(res, res, samples);
  945. if (sRenderMirrors)
  946. {
  947. mHeroProbeManager.initReflectionMaps();
  948. // We also scale the hero probe RT to the probe res since we do not
  949. // super sample it.
  950. res = mHeroProbeManager.mProbeResolution;
  951. mRT = &mHeroProbeRT;
  952. allocateScreenBuffer(res, res, samples);
  953. }
  954. mRT = &mMainRT;
  955. gCubeSnapshot = false;
  956. }
  957. U32 res_mod = RenderResolutionDivisor;
  958. if (res_mod > 1 && res_mod < res_x && res_mod < res_y)
  959. {
  960. res_x /= res_mod;
  961. res_y /= res_mod;
  962. }
  963. // Remember these dimensions
  964. mRT->mWidth = res_x;
  965. mRT->mHeight = res_y;
  966. if (!sRenderDeferred && !gUsePBRShaders) // Forward rendering
  967. {
  968. mFXAABuffer.release();
  969. mSMAABlendBuffer.release();
  970. mSMAAEdgeBuffer.release();
  971. mScratchBuffer.release();
  972. mRT->mDeferredLight.release();
  973. releaseShadowTargets();
  974. mRT->mScreen.release();
  975. // Make sure to release any render targets that share a depth buffer
  976. // with mDeferredScreen first:
  977. mRT->mDeferredScreen.release();
  978. if (!gUsePBRShaders)
  979. {
  980. mDeferredDepth.release();
  981. mOcclusionDepth.release();
  982. }
  983. if (!mRT->mScreen.allocate(res_x, res_y, GL_RGBA, true, true,
  984. LLTexUnit::TT_RECT_TEXTURE))
  985. {
  986. llwarns << "Failed to allocate the screen buffer." << llendl;
  987. return false;
  988. }
  989. gGL.getTexUnit(0)->disable();
  990. stop_glerror();
  991. llinfos << "Allocation successful." << llendl;
  992. return true;
  993. }
  994. if (!gUsePBRShaders)
  995. {
  996. // Set this flag in case we crash while resizing window or allocating
  997. // space for deferred rendering targets
  998. gSavedSettings.setBool("RenderInitError", true);
  999. gAppViewerp->saveGlobalSettings();
  1000. }
  1001. constexpr U32 occlusion_divisor = 3;
  1002. // Allocate deferred rendering color buffers
  1003. if (gUsePBRShaders)
  1004. {
  1005. if (!mRT->mDeferredScreen.allocate(res_x, res_y, GL_RGBA, true))
  1006. {
  1007. llwarns << "Failed to allocate the deferred screen buffer."
  1008. << llendl;
  1009. return false;
  1010. }
  1011. }
  1012. else if (!mRT->mDeferredScreen.allocate(res_x, res_y, GL_SRGB8_ALPHA8,
  1013. true, true,
  1014. LLTexUnit::TT_RECT_TEXTURE))
  1015. {
  1016. llwarns << "Failed to allocate the deferred screen buffer."
  1017. << llendl;
  1018. return false;
  1019. }
  1020. if (!addDeferredAttachments(mRT->mDeferredScreen))
  1021. {
  1022. llwarns << "Failed to attach the deferred screen buffer." << llendl;
  1023. return false;
  1024. }
  1025. if (!gUsePBRShaders)
  1026. {
  1027. if (!mDeferredDepth.allocate(res_x, res_y, 0, true, false,
  1028. LLTexUnit::TT_RECT_TEXTURE))
  1029. {
  1030. llwarns << "Failed to allocate the deferred depth buffer."
  1031. << llendl;
  1032. return false;
  1033. }
  1034. if (!mOcclusionDepth.allocate(res_x / occlusion_divisor,
  1035. res_y / occlusion_divisor, 0, true,
  1036. false, LLTexUnit::TT_RECT_TEXTURE))
  1037. {
  1038. llwarns << "Failed to allocate the occlusion depth buffer."
  1039. << llendl;
  1040. return false;
  1041. }
  1042. }
  1043. if (gUsePBRShaders)
  1044. {
  1045. if (!mRT->mScreen.allocate(res_x, res_y, GL_RGBA16F))
  1046. {
  1047. llwarns << "Failed to allocate the screen buffer." << llendl;
  1048. return false;
  1049. }
  1050. }
  1051. else
  1052. {
  1053. GLuint screen_format = GL_RGBA16;
  1054. if (gGLManager.mIsAMD)
  1055. {
  1056. static LLCachedControl<bool> use_rgba16(gSavedSettings,
  1057. "RenderUseRGBA16ATI");
  1058. if (!use_rgba16 || gGLManager.mGLVersion < 4.f)
  1059. {
  1060. screen_format = GL_RGBA12;
  1061. }
  1062. }
  1063. else if (gGLManager.mIsNVIDIA && gGLManager.mGLVersion < 4.f)
  1064. {
  1065. screen_format = GL_RGBA16F;
  1066. }
  1067. if (!mRT->mScreen.allocate(res_x, res_y, screen_format, false, false,
  1068. LLTexUnit::TT_RECT_TEXTURE))
  1069. {
  1070. llwarns << "Failed to allocate the screen buffer." << llendl;
  1071. return false;
  1072. }
  1073. }
  1074. // Share depth buffer between deferred targets
  1075. mRT->mDeferredScreen.shareDepthBuffer(mRT->mScreen);
  1076. // Note: when RenderResolutionDivisor is larger than 1 (i.e. we ask for a
  1077. // lower resolution render), we do not really need any anti-aliasing... HB
  1078. if (samples > 0 && res_mod <= 1 && !gCubeSnapshot)
  1079. {
  1080. if (gUsePBRShaders)
  1081. {
  1082. if (!mFXAABuffer.allocate(res_x, res_y, GL_RGBA))
  1083. {
  1084. llwarns << "Failed to allocate the FXAA buffer." << llendl;
  1085. return false;
  1086. }
  1087. if (!mSMAAEdgeBuffer.allocate(res_x, res_y, GL_RGBA, true))
  1088. {
  1089. llwarns << "Failed to allocate the SMAA edge buffer."
  1090. << llendl;
  1091. return false;
  1092. }
  1093. if (!mSMAABlendBuffer.allocate(res_x, res_y, GL_RGBA))
  1094. {
  1095. llwarns << "Failed to allocate the SMAA blend buffer."
  1096. << llendl;
  1097. return false;
  1098. }
  1099. mSMAAEdgeBuffer.shareDepthBuffer(mSMAABlendBuffer);
  1100. if (!mScratchBuffer.allocate(res_x, res_y, GL_RGBA))
  1101. {
  1102. llwarns << "Failed to allocate the scratch buffer."
  1103. << llendl;
  1104. return false;
  1105. }
  1106. }
  1107. else
  1108. {
  1109. if (!mFXAABuffer.allocate(res_x, res_y, GL_RGBA, false, false))
  1110. {
  1111. llwarns << "Failed to allocate the FXAA buffer." << llendl;
  1112. return false;
  1113. }
  1114. if (!mSMAAEdgeBuffer.allocate(res_x, res_y, GL_RGBA, true, true))
  1115. {
  1116. llwarns << "Failed to allocate the SMAA edge buffer."
  1117. << llendl;
  1118. return false;
  1119. }
  1120. if (!mSMAABlendBuffer.allocate(res_x, res_y, GL_RGBA, false,
  1121. false))
  1122. {
  1123. llwarns << "Failed to allocate the SMAA blend buffer."
  1124. << llendl;
  1125. return false;
  1126. }
  1127. mSMAAEdgeBuffer.shareDepthBuffer(mSMAABlendBuffer);
  1128. if (!mScratchBuffer.allocate(res_x, res_y, GL_RGBA, false, false))
  1129. {
  1130. llwarns << "Failed to allocate the scratch buffer."
  1131. << llendl;
  1132. return false;
  1133. }
  1134. }
  1135. }
  1136. else if (!gCubeSnapshot)
  1137. {
  1138. mFXAABuffer.release();
  1139. mSMAABlendBuffer.release();
  1140. mSMAAEdgeBuffer.release();
  1141. mScratchBuffer.release();
  1142. }
  1143. if (samples > 0 || RenderShadowDetail || RenderDeferredSSAO ||
  1144. RenderDepthOfField)
  1145. {
  1146. // Only need mDeferredLight for shadows or SSAO or DOF or FXAA
  1147. if (gUsePBRShaders)
  1148. {
  1149. if (!mRT->mDeferredLight.allocate(res_x, res_y, GL_RGBA16F))
  1150. {
  1151. llwarns << "Failed to allocate the deferred light buffer."
  1152. << llendl;
  1153. return false;
  1154. }
  1155. }
  1156. else if (!mRT->mDeferredLight.allocate(res_x, res_y, GL_RGBA,
  1157. false, false,
  1158. LLTexUnit::TT_RECT_TEXTURE))
  1159. {
  1160. llwarns << "Failed to allocate the deferred light buffer."
  1161. << llendl;
  1162. return false;
  1163. }
  1164. }
  1165. else
  1166. {
  1167. mRT->mDeferredLight.release();
  1168. }
  1169. allocateShadowBuffer(res_x, res_y);
  1170. if (gUsePBRShaders && !gCubeSnapshot)
  1171. {
  1172. if (RenderScreenSpaceReflections &&
  1173. !mSceneMap.allocate(res_x, res_y, GL_RGB16F, true))
  1174. {
  1175. llwarns << "Failed to allocate the scene map buffer." << llendl;
  1176. return false;
  1177. }
  1178. if (!mPostMap.allocate(res_x, res_y, GL_RGBA))
  1179. {
  1180. llwarns << "Failed to allocate the post map buffer." << llendl;
  1181. return false;
  1182. }
  1183. }
  1184. if (!gUsePBRShaders)
  1185. {
  1186. // Clear the flag set to disable shaders on next session
  1187. gSavedSettings.setBool("RenderInitError", false);
  1188. gAppViewerp->saveGlobalSettings();
  1189. }
  1190. gGL.getTexUnit(0)->disable();
  1191. stop_glerror();
  1192. if (!gCubeSnapshot)
  1193. {
  1194. llinfos << "Allocation successful." << llendl;
  1195. }
  1196. return true;
  1197. }
  1198. // Must be even to avoid a stripe in the horizontal shadow blur
  1199. LL_INLINE static U32 blur_happy_size(U32 x, F32 scale)
  1200. {
  1201. return (U32((F32)x * scale) + 16) & ~0xF;
  1202. }
  1203. bool LLPipeline::allocateShadowBuffer(U32 res_x, U32 res_y)
  1204. {
  1205. refreshCachedSettings();
  1206. if (!sRenderDeferred && !gUsePBRShaders)
  1207. {
  1208. return true;
  1209. }
  1210. constexpr U32 occlusion_divisor = 3;
  1211. F32 scale = RenderShadowResolutionScale;
  1212. if (RenderShadowDetail)
  1213. {
  1214. // Allocate 4 sun shadow maps
  1215. U32 sun_shadow_map_width = blur_happy_size(res_x, scale);
  1216. U32 sun_shadow_map_height = blur_happy_size(res_y, scale);
  1217. llinfos << (gCubeSnapshot ? "Reflection probes " : "")
  1218. << "Sun shadows maps (4) size: " << sun_shadow_map_width << "x"
  1219. << sun_shadow_map_height << " pixels." << llendl;
  1220. if (gUsePBRShaders)
  1221. {
  1222. for (U32 i = 0; i < 4; ++i)
  1223. {
  1224. if (!mRT->mSunShadow[i].allocate(sun_shadow_map_width,
  1225. sun_shadow_map_height, 0,
  1226. true))
  1227. {
  1228. llwarns << "Failed to allocate the Sun shadows buffer."
  1229. << llendl;
  1230. return false;
  1231. }
  1232. }
  1233. }
  1234. else
  1235. {
  1236. for (U32 i = 0; i < 4; ++i)
  1237. {
  1238. if (!mShadow[i].allocate(sun_shadow_map_width,
  1239. sun_shadow_map_height, 0, true,
  1240. false))
  1241. {
  1242. llwarns << "Failed to allocate the Sun shadows buffers."
  1243. << llendl;
  1244. return false;
  1245. }
  1246. if (!mShadowOcclusion[i].allocate(sun_shadow_map_width /
  1247. occlusion_divisor,
  1248. sun_shadow_map_height /
  1249. occlusion_divisor,
  1250. 0, true, false))
  1251. {
  1252. llwarns << "Failed to allocate the Sun shadow occlusions buffers."
  1253. << llendl;
  1254. return false;
  1255. }
  1256. }
  1257. }
  1258. }
  1259. else if (gUsePBRShaders)
  1260. {
  1261. releaseSunShadowTargets();
  1262. }
  1263. else
  1264. {
  1265. for (U32 i = 0; i < 4; ++i)
  1266. {
  1267. releaseShadowTarget(i);
  1268. }
  1269. }
  1270. if (RenderShadowDetail > 1)
  1271. {
  1272. // Allocate two spot shadow maps
  1273. U32 size = res_x * scale;
  1274. llinfos << (gCubeSnapshot ? "Reflection probes s" : "S")
  1275. << "pot shadows maps (2) size: " << size << "x" << size
  1276. << " pixels." << llendl;
  1277. if (!gUsePBRShaders)
  1278. {
  1279. for (U32 i = 4; i < 6; ++i)
  1280. {
  1281. if (!mShadow[i].allocate(size, size, 0, true, false))
  1282. {
  1283. llwarns << "Failed to allocate the spot shadows buffers."
  1284. << llendl;
  1285. return false;
  1286. }
  1287. if (!mShadowOcclusion[i].allocate(size / occlusion_divisor,
  1288. size / occlusion_divisor,
  1289. 0, true, false))
  1290. {
  1291. llwarns << "Failed to allocate the spot shadow occlusions buffers."
  1292. << llendl;
  1293. return false;
  1294. }
  1295. }
  1296. }
  1297. // *HACK: !gCubeSnapshot to prevent allocating spot shadow maps during
  1298. // ReflectionMapManager init.
  1299. else if (!gCubeSnapshot)
  1300. {
  1301. for (U32 i = 0; i < 2; ++i)
  1302. {
  1303. if (!mSpotShadow[i].allocate(size, size, 0, true))
  1304. {
  1305. llwarns << "Failed to allocate the spot shadows buffers."
  1306. << llendl;
  1307. return false;
  1308. }
  1309. }
  1310. }
  1311. }
  1312. else if (!gUsePBRShaders)
  1313. {
  1314. for (U32 i = 4; i < 6; ++i)
  1315. {
  1316. releaseShadowTarget(i);
  1317. }
  1318. }
  1319. // *HACK: !gCubeSnapshot to prevent touching spot shadow maps during
  1320. // ReflectionMapManager init.
  1321. else if (!gCubeSnapshot)
  1322. {
  1323. releaseSpotShadowTargets();
  1324. }
  1325. if (!gUsePBRShaders || !RenderShadowDetail)
  1326. {
  1327. return true;
  1328. }
  1329. // Set up shadow map filtering and compare modes
  1330. LLTexUnit* unit0 = gGL.getTexUnit(0);
  1331. if (RenderShadowDetail)
  1332. {
  1333. for (U32 i = 0; i < 4; ++i)
  1334. {
  1335. LLRenderTarget* targetp = &mRT->mSunShadow[i];
  1336. if (!targetp) continue;
  1337. unit0->bind(targetp, true);
  1338. unit0->setTextureFilteringOption(LLTexUnit::TFO_ANISOTROPIC);
  1339. unit0->setTextureAddressMode(LLTexUnit::TAM_CLAMP);
  1340. glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_COMPARE_MODE,
  1341. GL_COMPARE_R_TO_TEXTURE);
  1342. glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_COMPARE_FUNC, GL_LEQUAL);
  1343. }
  1344. }
  1345. if (RenderShadowDetail > 1 && !gCubeSnapshot)
  1346. {
  1347. for (U32 i = 0; i < 2; ++i)
  1348. {
  1349. LLRenderTarget* targetp = &mSpotShadow[i];
  1350. if (!targetp) continue;
  1351. unit0->bind(targetp, true);
  1352. unit0->setTextureFilteringOption(LLTexUnit::TFO_ANISOTROPIC);
  1353. unit0->setTextureAddressMode(LLTexUnit::TAM_CLAMP);
  1354. glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_COMPARE_MODE,
  1355. GL_COMPARE_R_TO_TEXTURE);
  1356. glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_COMPARE_FUNC, GL_LEQUAL);
  1357. }
  1358. }
  1359. return true;
  1360. }
  1361. //static
  1362. U32 LLPipeline::waterReflectionType()
  1363. {
  1364. if (gUsePBRShaders)
  1365. {
  1366. if (!RenderTransparentWater)
  1367. {
  1368. return 0;
  1369. }
  1370. }
  1371. else if (RenderWaterReflectionType <= 1)
  1372. {
  1373. return RenderWaterReflectionType;
  1374. }
  1375. static LLCachedControl<F32> far_clip(gSavedSettings, "RenderFarClip");
  1376. if (gPipeline.mEyeAboveWater > F32(far_clip))
  1377. {
  1378. // Draw minimal water (opaque for PBR or sky reflections only for EE)
  1379. // when farther than draw distance.
  1380. return gUsePBRShaders ? 0 : 1;
  1381. }
  1382. return gUsePBRShaders ? 1 : RenderWaterReflectionType;
  1383. }
  1384. //static
  1385. void LLPipeline::updateRenderDeferred()
  1386. {
  1387. if (gUsePBRShaders)
  1388. {
  1389. // If we could switch to the PBR renderer, then we obviously can render
  1390. // in deferred mode. HB
  1391. sRenderDeferred = LLRenderTarget::sUseFBO = true;
  1392. return;
  1393. }
  1394. sRenderDeferred = RenderDeferred && !gUseWireframe &&
  1395. gFeatureManager.isFeatureAvailable("RenderDeferred");
  1396. LLRenderTarget::sUseFBO = sRenderDeferred;
  1397. }
  1398. // IMPORTANT: this method shall not reallocate buffers or call another method
  1399. // that could trigger a change in the settings it gets called for (since this
  1400. // would incur a recursion). It may also be called several times as the result
  1401. // of a single callback (e.g. when reloading shaders). HB
  1402. //static
  1403. void LLPipeline::refreshCachedSettings()
  1404. {
  1405. static LLCachedControl<bool> scriptseacon(gSavedSettings, "scriptsbeacon");
  1406. sRenderScriptedBeacons = scriptseacon;
  1407. static LLCachedControl<bool> scripttouchbeacon(gSavedSettings,
  1408. "scripttouchbeacon");
  1409. sRenderScriptedTouchBeacons = scripttouchbeacon;
  1410. static LLCachedControl<bool> physbeacon(gSavedSettings, "physicalbeacon");
  1411. sRenderPhysicalBeacons = physbeacon;
  1412. static LLCachedControl<bool> permbeacon(gSavedSettings, "permanentbeacon");
  1413. sRenderPermanentBeacons = permbeacon;
  1414. static LLCachedControl<bool> charbeacon(gSavedSettings, "characterbeacon");
  1415. sRenderCharacterBeacons = charbeacon;
  1416. static LLCachedControl<bool> soundsbeacon(gSavedSettings, "soundsbeacon");
  1417. sRenderSoundBeacons = soundsbeacon;
  1418. static LLCachedControl<bool> invisisoundsbeacon(gSavedSettings,
  1419. "invisiblesoundsbeacon");
  1420. sRenderInvisibleSoundBeacons = invisisoundsbeacon;
  1421. static LLCachedControl<bool> particlesbeacon(gSavedSettings,
  1422. "particlesbeacon");
  1423. sRenderParticleBeacons = particlesbeacon;
  1424. static LLCachedControl<bool> moapbeacon(gSavedSettings, "moapbeacon");
  1425. sRenderMOAPBeacons = moapbeacon;
  1426. static LLCachedControl<bool> renderbeacons(gSavedSettings,
  1427. "renderbeacons");
  1428. sRenderBeacons = renderbeacons;
  1429. static LLCachedControl<bool> renderhighlights(gSavedSettings,
  1430. "renderhighlights");
  1431. sRenderHighlight = renderhighlights;
  1432. static LLCachedControl<bool> renderattachment(gSavedSettings,
  1433. "renderattachment");
  1434. sRenderAttachments = renderattachment;
  1435. static LLCachedControl<U32> renderbyowner(gSavedSettings, "renderbyowner");
  1436. sRenderByOwner = renderbyowner;
  1437. static LLCachedControl<U32> beacon_width(gSavedSettings,
  1438. "DebugBeaconLineWidth");
  1439. DebugBeaconLineWidth = beacon_width;
  1440. static LLCachedControl<bool> mask_alpha_def(gSavedSettings,
  1441. "RenderAutoMaskAlphaDeferred");
  1442. sAutoMaskAlphaDeferred = mask_alpha_def;
  1443. static LLCachedControl<bool> mask_alpha(gSavedSettings,
  1444. "RenderAutoMaskAlphaNonDeferred");
  1445. sAutoMaskAlphaNonDeferred = mask_alpha;
  1446. static LLCachedControl<bool> use_far_clip(gSavedSettings,
  1447. "RenderUseFarClip");
  1448. sUseFarClip = use_far_clip;
  1449. static LLCachedControl<bool> use_occlusion(gSavedSettings, "UseOcclusion");
  1450. sUseOcclusion = use_occlusion && !gUseWireframe &&
  1451. gFeatureManager.isFeatureAvailable("UseOcclusion") ? 2 : 0;
  1452. static LLCachedControl<bool> render_water(gSavedSettings, "RenderWater");
  1453. sRenderWater = render_water;
  1454. static LLCachedControl<bool> render_deferred(gSavedSettings,
  1455. "RenderDeferred");
  1456. RenderDeferred = render_deferred;
  1457. static LLCachedControl<F32> sun_wash(gSavedSettings,
  1458. "RenderDeferredSunWash");
  1459. RenderDeferredSunWash = sun_wash;
  1460. static LLCachedControl<F32> gamma(gSavedSettings,
  1461. "RenderDeferredDisplayGamma");
  1462. RenderDeferredDisplayGamma = gamma > 0.1f ? gamma : 2.2f;
  1463. #if 0 // This should only taken into account after a restart. HB
  1464. static LLCachedControl<U32> fsaa_samp(gSavedSettings, "RenderFSAASamples");
  1465. RenderFSAASamples = llmin((U32)fsaa_samp, 16U);
  1466. #endif
  1467. static LLCachedControl<S32> aa_quality(gSavedSettings,
  1468. "RenderDeferredAAQuality");
  1469. if (aa_quality >= 0)
  1470. {
  1471. RenderDeferredAAQuality = llmin((S32)aa_quality, 3);
  1472. }
  1473. else if (RenderFSAASamples > 8)
  1474. {
  1475. RenderDeferredAAQuality = 3;
  1476. }
  1477. else if (RenderFSAASamples > 4)
  1478. {
  1479. RenderDeferredAAQuality = 2;
  1480. }
  1481. else if (RenderFSAASamples > 2)
  1482. {
  1483. RenderDeferredAAQuality = 1;
  1484. }
  1485. else
  1486. {
  1487. RenderDeferredAAQuality = 0;
  1488. }
  1489. static LLCachedControl<bool> sharpen(gSavedSettings,
  1490. "RenderDeferredAASharpen");
  1491. RenderDeferredAASharpen = sharpen;
  1492. static LLCachedControl<U32> res_divisor(gSavedSettings,
  1493. "RenderResolutionDivisor");
  1494. RenderResolutionDivisor = llclamp((U32)res_divisor, 1, 8);
  1495. static LLCachedControl<U32> shadow_detail(gSavedSettings,
  1496. "RenderShadowDetail");
  1497. RenderShadowDetail = shadow_detail;
  1498. static LLCachedControl<U32> deferred_ssao(gSavedSettings,
  1499. "RenderDeferredSSAO");
  1500. RenderDeferredSSAO = deferred_ssao > 1 ||
  1501. (deferred_ssao == 1 && RenderShadowDetail);
  1502. static LLCachedControl<bool> soften_shadows(gSavedSettings,
  1503. "RenderShadowSoften");
  1504. RenderShadowSoften = soften_shadows && RenderShadowDetail;
  1505. static LLCachedControl<F32> shadow_res_scl(gSavedSettings,
  1506. "RenderShadowResolutionScale");
  1507. RenderShadowResolutionScale = llclamp((F32)shadow_res_scl, 0.25f, 1.f);
  1508. static LLCachedControl<U32> local_lights(gSavedSettings,
  1509. "RenderLocalLightCount");
  1510. RenderLocalLightCount = llmin(U32(local_lights), 1024);
  1511. static LLCachedControl<bool> delay_creation(gSavedSettings,
  1512. "RenderDelayCreation");
  1513. RenderDelayCreation = delay_creation;
  1514. static LLCachedControl<bool> anim_res(gSavedSettings, "RenderAnimateRes");
  1515. RenderAnimateRes = anim_res;
  1516. static LLCachedControl<bool> spot_lights_nd(gSavedSettings,
  1517. "RenderSpotLightsInNondeferred");
  1518. RenderSpotLightsInNondeferred = spot_lights_nd;
  1519. static LLCachedControl<LLColor4> preview_amb_col(gSavedSettings,
  1520. "PreviewAmbientColor");
  1521. PreviewAmbientColor = preview_amb_col;
  1522. static LLCachedControl<LLColor4> preview_diff0(gSavedSettings,
  1523. "PreviewDiffuse0");
  1524. PreviewDiffuse0 = preview_diff0;
  1525. static LLCachedControl<LLColor4> preview_spec0(gSavedSettings,
  1526. "PreviewSpecular0");
  1527. PreviewSpecular0 = preview_spec0;
  1528. static LLCachedControl<LLVector3> preview_dir0(gSavedSettings,
  1529. "PreviewDirection0");
  1530. PreviewDirection0 = preview_dir0;
  1531. PreviewDirection0.normalize();
  1532. static LLCachedControl<LLColor4> preview_diff1(gSavedSettings,
  1533. "PreviewDiffuse1");
  1534. PreviewDiffuse1 = preview_diff1;
  1535. static LLCachedControl<LLColor4> preview_spec1(gSavedSettings,
  1536. "PreviewSpecular1");
  1537. PreviewSpecular1 = preview_spec1;
  1538. static LLCachedControl<LLVector3> preview_dir1(gSavedSettings,
  1539. "PreviewDirection1");
  1540. PreviewDirection1 = preview_dir1;
  1541. PreviewDirection1.normalize();
  1542. static LLCachedControl<LLColor4> preview_diff2(gSavedSettings,
  1543. "PreviewDiffuse2");
  1544. PreviewDiffuse2 = preview_diff2;
  1545. static LLCachedControl<LLColor4> preview_spec2(gSavedSettings,
  1546. "PreviewSpecular2");
  1547. PreviewSpecular2 = preview_spec2;
  1548. static LLCachedControl<LLVector3> preview_dir2(gSavedSettings,
  1549. "PreviewDirection2");
  1550. PreviewDirection2 = preview_dir2;
  1551. PreviewDirection2.normalize();
  1552. static LLCachedControl<bool> render_glow(gSavedSettings, "RenderGlow");
  1553. RenderGlow = sCanRenderGlow && render_glow;
  1554. static LLCachedControl<F32> glow_min_lum(gSavedSettings,
  1555. "RenderGlowMinLuminance");
  1556. RenderGlowMinLuminance = llmax((F32)glow_min_lum, 0.f);
  1557. static LLCachedControl<F32> glow_max_alpha(gSavedSettings,
  1558. "RenderGlowMaxExtractAlpha");
  1559. RenderGlowMaxExtractAlpha = glow_max_alpha;
  1560. static LLCachedControl<F32> glow_warmth_a(gSavedSettings,
  1561. "RenderGlowWarmthAmount");
  1562. RenderGlowWarmthAmount = glow_warmth_a;
  1563. static LLCachedControl<LLVector3> glow_lum_w(gSavedSettings,
  1564. "RenderGlowLumWeights");
  1565. RenderGlowLumWeights = glow_lum_w;
  1566. static LLCachedControl<LLVector3> glow_warmth_w(gSavedSettings,
  1567. "RenderGlowWarmthWeights");
  1568. RenderGlowWarmthWeights = glow_warmth_w;
  1569. static LLCachedControl<U32> glow_res_pow(gSavedSettings,
  1570. "RenderGlowResolutionPow");
  1571. RenderGlowResolutionPow = glow_res_pow;
  1572. static LLCachedControl<U32> glow_iterations(gSavedSettings,
  1573. "RenderGlowIterations");
  1574. RenderGlowIterations = glow_iterations;
  1575. static LLCachedControl<F32> glow_width(gSavedSettings, "RenderGlowWidth");
  1576. RenderGlowWidth = glow_width;
  1577. static LLCachedControl<F32> glow_strength(gSavedSettings,
  1578. "RenderGlowStrength");
  1579. RenderGlowStrength = llmax(0.f, (F32)glow_strength);
  1580. static LLCachedControl<bool> depth_of_field(gSavedSettings,
  1581. "RenderDepthOfField");
  1582. RenderDepthOfField = depth_of_field;
  1583. static LLCachedControl<bool> dof_in_edit(gSavedSettings,
  1584. "RenderDepthOfFieldInEditMode");
  1585. RenderDepthOfFieldInEditMode = dof_in_edit;
  1586. static LLCachedControl<F32> shadow_noise(gSavedSettings,
  1587. "RenderShadowNoise");
  1588. RenderShadowNoise = shadow_noise;
  1589. static LLCachedControl<F32> shadow_blur_size(gSavedSettings,
  1590. "RenderShadowBlurSize");
  1591. RenderShadowBlurSize = shadow_blur_size;
  1592. static LLCachedControl<F32> ssao_scale(gSavedSettings, "RenderSSAOScale");
  1593. RenderSSAOScale = ssao_scale;
  1594. static LLCachedControl<U32> ssao_max_scale(gSavedSettings,
  1595. "RenderSSAOMaxScale");
  1596. RenderSSAOMaxScale = ssao_max_scale;
  1597. static LLCachedControl<F32> ssao_fact(gSavedSettings, "RenderSSAOFactor");
  1598. RenderSSAOFactor = ssao_fact;
  1599. static LLCachedControl<LLVector3> ssao_effect(gSavedSettings,
  1600. "RenderSSAOEffect");
  1601. RenderSSAOEffect = ssao_effect;
  1602. static LLCachedControl<F32> shadow_bias_error(gSavedSettings,
  1603. "RenderShadowBiasError");
  1604. RenderShadowBiasError = shadow_bias_error;
  1605. static LLCachedControl<F32> shadow_offset(gSavedSettings,
  1606. "RenderShadowOffset");
  1607. RenderShadowOffset = shadow_offset;
  1608. static LLCachedControl<F32> shadow_off_no_ssao(gSavedSettings,
  1609. "RenderShadowOffsetNoSSAO");
  1610. RenderShadowOffsetNoSSAO = shadow_off_no_ssao;
  1611. static LLCachedControl<F32> shadow_bias(gSavedSettings,
  1612. "RenderShadowBias");
  1613. RenderShadowBias = shadow_bias;
  1614. static LLCachedControl<F32> spot_shadow_offset(gSavedSettings,
  1615. "RenderSpotShadowOffset");
  1616. RenderSpotShadowOffset = spot_shadow_offset;
  1617. static LLCachedControl<F32> spot_shadow_bias(gSavedSettings,
  1618. "RenderSpotShadowBias");
  1619. RenderSpotShadowBias = spot_shadow_bias;
  1620. static LLCachedControl<LLVector3> shadow_gaussian(gSavedSettings,
  1621. "RenderShadowGaussian");
  1622. RenderShadowGaussian = shadow_gaussian;
  1623. static LLCachedControl<F32> shadow_blur_dist(gSavedSettings,
  1624. "RenderShadowBlurDistFactor");
  1625. RenderShadowBlurDistFactor = shadow_blur_dist;
  1626. static LLCachedControl<bool> deferred_atmos(gSavedSettings,
  1627. "RenderDeferredAtmospheric");
  1628. RenderDeferredAtmospheric = deferred_atmos;
  1629. static LLCachedControl<U32> reflection_type(gSavedSettings,
  1630. "RenderWaterReflectionType");
  1631. RenderWaterReflectionType = reflection_type;
  1632. static LLCachedControl<bool> transparent_water(gSavedSettings,
  1633. "RenderTransparentWater");
  1634. RenderTransparentWater = transparent_water;
  1635. static LLCachedControl<LLVector3> shadow_planes(gSavedSettings,
  1636. "RenderShadowClipPlanes");
  1637. RenderShadowClipPlanes = shadow_planes;
  1638. static LLCachedControl<LLVector3> shadow_ortho(gSavedSettings,
  1639. "RenderShadowOrthoClipPlanes");
  1640. RenderShadowOrthoClipPlanes = shadow_ortho;
  1641. static LLCachedControl<F32> far_clip(gSavedSettings, "RenderFarClip");
  1642. RenderFarClip = far_clip;
  1643. static LLCachedControl<LLVector3> shadow_split_exp(gSavedSettings,
  1644. "RenderShadowSplitExponent");
  1645. RenderShadowSplitExponent = shadow_split_exp;
  1646. static LLCachedControl<F32> shadow_error_cutoff(gSavedSettings,
  1647. "RenderShadowErrorCutoff");
  1648. RenderShadowErrorCutoff = shadow_error_cutoff;
  1649. static LLCachedControl<F32> shadow_fov_cutoff(gSavedSettings,
  1650. "RenderShadowFOVCutoff");
  1651. RenderShadowFOVCutoff = llmin((F32)shadow_fov_cutoff, 1.4f);
  1652. static LLCachedControl<bool> camera_offset(gSavedSettings, "CameraOffset");
  1653. CameraOffset = camera_offset;
  1654. static LLCachedControl<F32> camera_max_cof(gSavedSettings, "CameraMaxCoF");
  1655. CameraMaxCoF = camera_max_cof;
  1656. static LLCachedControl<F32> camera_dof_scale(gSavedSettings,
  1657. "CameraDoFResScale");
  1658. CameraDoFResScale = camera_dof_scale;
  1659. static LLCachedControl<U32> auto_hide_mem(gSavedSettings,
  1660. "RenderAutoHideGeometryMemoryLimit");
  1661. RenderAutoHideGeometryMemoryLimit = auto_hide_mem;
  1662. static LLCachedControl<F32> auto_hide_area(gSavedSettings,
  1663. "RenderAutoHideSurfaceAreaLimit");
  1664. RenderAutoHideSurfaceAreaLimit = auto_hide_area;
  1665. sRenderSpotLight = NULL;
  1666. static LLCachedControl<S32> pbr_visu(gSavedSettings,
  1667. "RenderBufferVisualization");
  1668. RenderBufferVisualization = llclamp(S32(pbr_visu), -1, 4);
  1669. static LLCachedControl<U32> refl_iter(gSavedSettings,
  1670. "RenderScreenSpaceReflectionIterations");
  1671. RenderScreenSpaceReflectionIterations = refl_iter;
  1672. static LLCachedControl<F32> refl_step(gSavedSettings,
  1673. "RenderScreenSpaceReflectionRayStep");
  1674. RenderScreenSpaceReflectionRayStep = llmax(0.f, refl_step);
  1675. static LLCachedControl<F32> refl_bias(gSavedSettings,
  1676. "RenderScreenSpaceReflectionDistanceBias");
  1677. RenderScreenSpaceReflectionDistanceBias = llmax(0.f, refl_bias);
  1678. static LLCachedControl<F32> refl_rej(gSavedSettings,
  1679. "RenderScreenSpaceReflectionDepthRejectBias");
  1680. RenderScreenSpaceReflectionDepthRejectBias = llmax(0.f, refl_rej);
  1681. static LLCachedControl<F32> refl_mult(gSavedSettings,
  1682. "RenderScreenSpaceReflectionAdaptiveStepMultiplier");
  1683. RenderScreenSpaceReflectionDepthRejectBias = llmax(0.f, refl_mult);
  1684. static LLCachedControl<U32> refl_glos(gSavedSettings,
  1685. "RenderScreenSpaceReflectionGlossySamples");
  1686. RenderScreenSpaceReflectionGlossySamples = refl_glos;
  1687. static LLCachedControl<U32> reflections(gSavedSettings,
  1688. "RenderReflectionProbes");
  1689. sReflectionProbesEnabled = gUsePBRShaders && LLViewerShaderMgr::sHasRP &&
  1690. reflections;
  1691. bool had_mirrors = sRenderMirrors;
  1692. static LLCachedControl<U32> hero_res(gSavedSettings,
  1693. "RenderHeroProbeResLevel");
  1694. sRenderMirrors = sReflectionProbesEnabled && hero_res > 0;
  1695. if (gUsePBRShaders && sRenderMirrors != had_mirrors)
  1696. {
  1697. handle_refresh_probes(NULL);
  1698. }
  1699. static LLCachedControl<bool> refl_space(gSavedSettings,
  1700. "RenderScreenSpaceReflections");
  1701. RenderScreenSpaceReflections = sReflectionProbesEnabled && reflections;
  1702. static LLCachedControl<bool> pbr_terrain(gSavedSettings,
  1703. "RenderTerrainPBREnabled");
  1704. sRenderPBRTerrain = gUsePBRShaders && pbr_terrain &&
  1705. gGLManager.mNumTextureImageUnits >= 16;
  1706. static LLCachedControl<U32> pbr_terrain_detail(gSavedSettings,
  1707. "RenderTerrainPBRDetail");
  1708. S32 terrain_details = 0;
  1709. if (sRenderPBRTerrain)
  1710. {
  1711. terrain_details = pbr_terrain_detail;
  1712. if (terrain_details > 3 && gGLManager.mNumTextureImageUnits <= 16)
  1713. {
  1714. terrain_details = 3; // Remove emissive map support
  1715. }
  1716. }
  1717. sRenderPBRTerrainDetail = llmin(0, terrain_details - 4);
  1718. updateRenderDeferred();
  1719. LLPrefsGraphics::refresh();
  1720. }
  1721. void LLPipeline::releaseGLBuffers()
  1722. {
  1723. if (mNoiseMap)
  1724. {
  1725. LLImageGL::deleteTextures(1, &mNoiseMap);
  1726. mNoiseMap = 0;
  1727. }
  1728. if (mTrueNoiseMap)
  1729. {
  1730. LLImageGL::deleteTextures(1, &mTrueNoiseMap);
  1731. mTrueNoiseMap = 0;
  1732. }
  1733. if (mAreaMap)
  1734. {
  1735. LLImageGL::deleteTextures(1, &mAreaMap);
  1736. mAreaMap = 0;
  1737. }
  1738. if (mSearchMap)
  1739. {
  1740. LLImageGL::deleteTextures(1, &mSearchMap);
  1741. mSearchMap = 0;
  1742. }
  1743. releaseLUTBuffers();
  1744. if (gUsePBRShaders)
  1745. {
  1746. mSceneMap.release();
  1747. mPostMap.release();
  1748. }
  1749. else
  1750. {
  1751. mWaterRef.release();
  1752. }
  1753. mWaterDis.release();
  1754. for (U32 i = 0; i < 3; ++i)
  1755. {
  1756. mGlow[i].release();
  1757. }
  1758. releaseScreenBuffers();
  1759. LLVOAvatar::resetImpostors();
  1760. }
  1761. void LLPipeline::releaseLUTBuffers()
  1762. {
  1763. if (mLightFunc)
  1764. {
  1765. LLImageGL::deleteTextures(1, &mLightFunc);
  1766. mLightFunc = 0;
  1767. }
  1768. if (gUsePBRShaders)
  1769. {
  1770. mPbrBrdfLut.release();
  1771. mExposureMap.release();
  1772. mLuminanceMap.release();
  1773. mLastExposure.release();
  1774. }
  1775. }
  1776. void LLPipeline::releasePackBuffers(RenderTargetPack* packp)
  1777. {
  1778. mRT = packp;
  1779. releaseShadowTargets();
  1780. mRT->mScreen.release();
  1781. mRT->mDeferredScreen.release();
  1782. mRT->mDeferredLight.release();
  1783. }
  1784. void LLPipeline::releaseScreenBuffers()
  1785. {
  1786. mFXAABuffer.release();
  1787. mSMAAEdgeBuffer.release();
  1788. mSMAABlendBuffer.release();
  1789. mScratchBuffer.release();
  1790. if (gUsePBRShaders)
  1791. {
  1792. releasePackBuffers(&mAuxillaryRT);
  1793. releasePackBuffers(&mHeroProbeRT);
  1794. mReflectionMapManager.cleanup();
  1795. mHeroProbeManager.cleanup();
  1796. }
  1797. else
  1798. {
  1799. mPhysicsDisplay.release();
  1800. mDeferredDepth.release();
  1801. mOcclusionDepth.release();
  1802. }
  1803. releasePackBuffers(&mMainRT);
  1804. }
  1805. void LLPipeline::releaseShadowTarget(U32 index)
  1806. {
  1807. llassert(!gUsePBRShaders); // EE rendering only
  1808. mShadow[index].release();
  1809. mShadowOcclusion[index].release();
  1810. }
  1811. void LLPipeline::releaseSunShadowTargets()
  1812. {
  1813. llassert(gUsePBRShaders); // PBR rendering only
  1814. mRT->mSunShadow[0].release();
  1815. mRT->mSunShadow[1].release();
  1816. mRT->mSunShadow[2].release();
  1817. mRT->mSunShadow[3].release();
  1818. }
  1819. void LLPipeline::releaseSpotShadowTargets()
  1820. {
  1821. llassert(gUsePBRShaders); // PBR rendering only
  1822. // *HACK: do not release during auxiliary target allocation
  1823. if (!gCubeSnapshot)
  1824. {
  1825. mSpotShadow[0].release();
  1826. mSpotShadow[1].release();
  1827. }
  1828. }
  1829. void LLPipeline::releaseShadowTargets()
  1830. {
  1831. if (gUsePBRShaders)
  1832. {
  1833. releaseSunShadowTargets();
  1834. releaseSpotShadowTargets();
  1835. }
  1836. else
  1837. {
  1838. for (U32 i = 0; i < 6; ++i)
  1839. {
  1840. mShadow[i].release();
  1841. mShadowOcclusion[i].release();
  1842. }
  1843. }
  1844. }
  1845. void LLPipeline::createGLBuffers()
  1846. {
  1847. updateRenderDeferred();
  1848. GLuint res_x = gViewerWindowp->getWindowDisplayWidth();
  1849. GLuint res_y = gViewerWindowp->getWindowDisplayHeight();
  1850. if (gUsePBRShaders)
  1851. {
  1852. // We need this buffer to be as large as the screen space, since it is
  1853. // used to copy the screen buffer for screen-wide water and atmospheric
  1854. // haze as well as for water distortions and reflections... HB
  1855. mWaterDis.allocate(res_x, res_y, GL_RGBA16F, true);
  1856. }
  1857. else
  1858. {
  1859. // Water reflection texture
  1860. // Set up SRGB targets if we are doing deferred-path reflection rendering
  1861. U32 res = llmax(gSavedSettings.getU32("RenderWaterRefResolution"),
  1862. 512);
  1863. mWaterRef.allocate(res, res, GL_RGBA, true, false);
  1864. mWaterDis.allocate(res, res, GL_RGBA, true, false);
  1865. }
  1866. // Screen space glow buffers
  1867. U32 glow_pow = gSavedSettings.getU32("RenderGlowResolutionPow");
  1868. // Limited between 16 and 512
  1869. const U32 glow_res = 1 << llclamp(glow_pow, 4U, 9U);
  1870. for (U32 i = 0; i < 3; ++i)
  1871. {
  1872. if (gUsePBRShaders)
  1873. {
  1874. mGlow[i].allocate(512, glow_res, GL_RGBA);
  1875. }
  1876. else
  1877. {
  1878. mGlow[i].allocate(512, glow_res, GL_RGBA, false, false);
  1879. }
  1880. }
  1881. allocateScreenBuffer(res_x, res_y);
  1882. if (!sRenderDeferred) // Forward rendering
  1883. {
  1884. stop_glerror();
  1885. return;
  1886. }
  1887. LLTexUnit* unit0 = gGL.getTexUnit(0);
  1888. if (!mNoiseMap)
  1889. {
  1890. constexpr U32 noise_res = 128;
  1891. LLVector3 noise[noise_res * noise_res];
  1892. F32 scaler = gSavedSettings.getF32("RenderDeferredNoise") / 100.f;
  1893. for (U32 i = 0; i < noise_res * noise_res; ++i)
  1894. {
  1895. noise[i].set(ll_frand() - 0.5f, ll_frand() - 0.5f, 0.f);
  1896. noise[i].normalize();
  1897. noise[i].mV[2] = ll_frand() * scaler + 1.f - scaler * 0.5f;
  1898. }
  1899. LLImageGL::generateTextures(1, &mNoiseMap);
  1900. unit0->bindManual(LLTexUnit::TT_TEXTURE, mNoiseMap);
  1901. LLImageGL::setManualImage(LLTexUnit::getInternalType(LLTexUnit::TT_TEXTURE),
  1902. 0, GL_RGB16F, noise_res, noise_res, GL_RGB,
  1903. GL_FLOAT, noise, false);
  1904. unit0->setTextureFilteringOption(LLTexUnit::TFO_POINT);
  1905. unit0->disable();
  1906. }
  1907. if (!mTrueNoiseMap)
  1908. {
  1909. constexpr U32 noise_res = 128;
  1910. F32 noise[noise_res * noise_res * 3];
  1911. for (U32 i = 0; i < noise_res * noise_res * 3; ++i)
  1912. {
  1913. noise[i] = ll_frand() * 2.f - 1.f;
  1914. }
  1915. LLImageGL::generateTextures(1, &mTrueNoiseMap);
  1916. unit0->bindManual(LLTexUnit::TT_TEXTURE, mTrueNoiseMap);
  1917. LLImageGL::setManualImage(LLTexUnit::getInternalType(LLTexUnit::TT_TEXTURE),
  1918. 0, GL_RGB16F, noise_res, noise_res, GL_RGB,
  1919. GL_FLOAT, noise, false);
  1920. unit0->setTextureFilteringOption(LLTexUnit::TFO_POINT);
  1921. unit0->disable();
  1922. }
  1923. if (!mAreaMap)
  1924. {
  1925. std::vector<unsigned char> temp_buff(AREATEX_SIZE);
  1926. for (size_t y = 0; y < AREATEX_HEIGHT; ++y)
  1927. {
  1928. size_t src_y = AREATEX_HEIGHT - 1 - y;
  1929. memcpy(&temp_buff[y * AREATEX_PITCH],
  1930. areaTexBytes + src_y * AREATEX_PITCH, AREATEX_PITCH);
  1931. }
  1932. LLImageGL::generateTextures(1, &mAreaMap);
  1933. unit0->bindManual(LLTexUnit::TT_TEXTURE, mAreaMap);
  1934. LLImageGL::setManualImage(LLTexUnit::getInternalType(LLTexUnit::TT_TEXTURE),
  1935. 0, GL_RG8, AREATEX_WIDTH, AREATEX_HEIGHT,
  1936. GL_RG, GL_UNSIGNED_BYTE, temp_buff.data(),
  1937. false);
  1938. unit0->setTextureFilteringOption(LLTexUnit::TFO_BILINEAR);
  1939. unit0->setTextureAddressMode(LLTexUnit::TAM_CLAMP);
  1940. unit0->disable();
  1941. }
  1942. if (!mSearchMap)
  1943. {
  1944. std::vector<unsigned char> temp_buff(SEARCHTEX_SIZE);
  1945. for (size_t y = 0; y < SEARCHTEX_HEIGHT; ++y)
  1946. {
  1947. size_t src_y = SEARCHTEX_HEIGHT - 1 - y;
  1948. memcpy(&temp_buff[y * SEARCHTEX_PITCH],
  1949. searchTexBytes + src_y * SEARCHTEX_PITCH, SEARCHTEX_PITCH);
  1950. }
  1951. LLImageGL::generateTextures(1, &mSearchMap);
  1952. unit0->bindManual(LLTexUnit::TT_TEXTURE, mSearchMap);
  1953. LLImageGL::setManualImage(LLTexUnit::getInternalType(LLTexUnit::TT_TEXTURE),
  1954. 0, GL_RG8, SEARCHTEX_WIDTH, SEARCHTEX_HEIGHT,
  1955. GL_RED, GL_UNSIGNED_BYTE, temp_buff.data(),
  1956. false);
  1957. unit0->setTextureFilteringOption(LLTexUnit::TFO_BILINEAR);
  1958. unit0->setTextureAddressMode(LLTexUnit::TAM_CLAMP);
  1959. unit0->disable();
  1960. }
  1961. createLUTBuffers();
  1962. stop_glerror();
  1963. }
  1964. void LLPipeline::createLUTBuffers()
  1965. {
  1966. if (!sRenderDeferred || mLightFunc)
  1967. {
  1968. return;
  1969. }
  1970. static LLCachedControl<U32> light_res_x(gSavedSettings,
  1971. "RenderSpecularResX");
  1972. static LLCachedControl<U32> light_res_y(gSavedSettings,
  1973. "RenderSpecularResY");
  1974. static LLCachedControl<F32> spec_exp(gSavedSettings,
  1975. "RenderSpecularExponent");
  1976. F32* ls = new F32[light_res_x * light_res_y];
  1977. // Calculate the (normalized) blinn-phong specular lookup texture (with a
  1978. // few tweaks).
  1979. for (U32 y = 0; y < light_res_y; ++y)
  1980. {
  1981. for (U32 x = 0; x < light_res_x; ++x)
  1982. {
  1983. ls[y * light_res_x + x] = 0.f;
  1984. F32 sa = (F32)x / (F32)(light_res_x - 1);
  1985. F32 spec = (F32)y / (F32)(light_res_y - 1);
  1986. F32 n = spec * spec * spec_exp;
  1987. // Nothing special here, just your typical blinn-phong term
  1988. spec = powf(sa, n);
  1989. // Apply our normalization function.
  1990. // Note: This is the full equation that applies the full
  1991. // normalization curve, not an approximation. This is fine, given
  1992. // we only need to create our LUT once per buffer initialization.
  1993. // The only trade off is we have a really low dynamic range. This
  1994. // means we have to account for things not being able to exceed 0
  1995. // to 1 in our shaders.
  1996. spec *= (n + 2.f) * (n + 4.f) /
  1997. (8.f * F_PI * (powf(2.f, -0.5f * n) + n));
  1998. // Since we use R16F, we no longer have a dynamic range issue we
  1999. // need to work around here. Though some older drivers may not like
  2000. // this, newer drivers should not have this problem.
  2001. ls[y * light_res_x + x] = spec;
  2002. }
  2003. }
  2004. #if LL_DARWIN
  2005. // Work around for limited precision with 10.6.8 and older drivers
  2006. constexpr U32 pix_format = GL_R32F;
  2007. #else
  2008. constexpr U32 pix_format = GL_R16F;
  2009. #endif
  2010. LLImageGL::generateTextures(1, &mLightFunc);
  2011. LLTexUnit* unit0 = gGL.getTexUnit(0);
  2012. unit0->bindManual(LLTexUnit::TT_TEXTURE, mLightFunc);
  2013. LLImageGL::setManualImage(LLTexUnit::getInternalType(LLTexUnit::TT_TEXTURE),
  2014. 0, pix_format, light_res_x, light_res_y,
  2015. GL_RED, GL_FLOAT, ls, false);
  2016. unit0->setTextureAddressMode(LLTexUnit::TAM_CLAMP);
  2017. unit0->setTextureFilteringOption(LLTexUnit::TFO_TRILINEAR);
  2018. glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
  2019. glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST);
  2020. delete[] ls;
  2021. if (!gUsePBRShaders)
  2022. {
  2023. return;
  2024. }
  2025. mPbrBrdfLut.allocate(512, 512, GL_RG16F);
  2026. mPbrBrdfLut.bindTarget();
  2027. gDeferredGenBrdfLutProgram.bind();
  2028. gGL.begin(LLRender::TRIANGLE_STRIP);
  2029. gGL.vertex2f(-1.f, -1.f);
  2030. gGL.vertex2f(-1.f, 1.f);
  2031. gGL.vertex2f(1.f, -1.f);
  2032. gGL.vertex2f(1.f, 1.f);
  2033. gGL.end(true);
  2034. gDeferredGenBrdfLutProgram.unbind();
  2035. mPbrBrdfLut.flush();
  2036. mExposureMap.allocate(1, 1, GL_R16F);
  2037. mExposureMap.bindTarget();
  2038. glClearColor(1.f, 1.f, 1.f, 0.f);
  2039. mExposureMap.clear();
  2040. glClearColor(0.f, 0.f, 0.f, 0.f);
  2041. mExposureMap.flush();
  2042. mLuminanceMap.allocate(256, 256, GL_R16F, false, LLTexUnit::TT_TEXTURE,
  2043. LLTexUnit::TMG_AUTO);
  2044. mLastExposure.allocate(1, 1, GL_R16F);
  2045. }
  2046. void LLPipeline::restoreGL()
  2047. {
  2048. gViewerShaderMgrp->setShaders();
  2049. #if 0 // Failed attempt at properly restoring terrain after GL restart with
  2050. // core GL profile enabled. HB
  2051. LLViewerTextureList::dirty_list_t textures;
  2052. for (pool_tex_map_t::iterator it = mTerrainPools.begin(),
  2053. end = mTerrainPools.end();
  2054. it != end; ++it)
  2055. {
  2056. textures.insert((LLViewerFetchedTexture*)it->first);
  2057. }
  2058. dirtyPoolObjectTextures(textures);
  2059. #elif 0 // Second failed attempt at properly restoring terrain after GL
  2060. // restart with core GL profile enabled. HB
  2061. for (pool_tex_map_t::iterator it = mTerrainPools.begin(),
  2062. end = mTerrainPools.end();
  2063. it != end; ++it)
  2064. {
  2065. LLDrawPool* poolp = it->second;
  2066. if (poolp->isTerrainPool()) // Paranoia
  2067. {
  2068. ((LLDrawPoolTerrain*)poolp)->rebuildPatches();
  2069. }
  2070. }
  2071. #endif
  2072. }
  2073. bool LLPipeline::canUseWindLightShaders() const
  2074. {
  2075. if (gUsePBRShaders)
  2076. {
  2077. // If we could switch to PBR rendering, then we can do Windlight. HB
  2078. return true;
  2079. }
  2080. return gWLSkyProgram.mProgramObject &&
  2081. gViewerShaderMgrp->getShaderLevel(LLViewerShaderMgr::SHADER_WINDLIGHT) > 1;
  2082. }
  2083. void LLPipeline::unloadShaders()
  2084. {
  2085. gViewerShaderMgrp->unloadShaders();
  2086. if (mVertexShadersLoaded != -1)
  2087. {
  2088. mVertexShadersLoaded = 0;
  2089. }
  2090. }
  2091. class LLOctreeDirtyTexture final : public OctreeTraveler
  2092. {
  2093. public:
  2094. LLOctreeDirtyTexture(const LLViewerTextureList::dirty_list_t& textures)
  2095. : mTextures(textures)
  2096. {
  2097. }
  2098. void visit(const OctreeNode* nodep) override
  2099. {
  2100. LLSpatialGroup* groupp = (LLSpatialGroup*)nodep->getListener(0);
  2101. if (!groupp) return;
  2102. if (!groupp->isEmpty() &&
  2103. !groupp->hasState(LLSpatialGroup::GEOM_DIRTY))
  2104. {
  2105. LLViewerTextureList::dirty_list_t::const_iterator textures_end =
  2106. mTextures.end();
  2107. for (LLSpatialGroup::draw_map_t::const_iterator
  2108. it = groupp->mDrawMap.begin(), end = groupp->mDrawMap.end();
  2109. it != end; ++it)
  2110. {
  2111. const LLSpatialGroup::drawmap_elem_t& draw_info = it->second;
  2112. for (U32 i = 0, count = draw_info.size(); i < count; ++i)
  2113. {
  2114. LLDrawInfo* infop = draw_info[i].get();
  2115. LLViewerFetchedTexture* texp =
  2116. LLViewerTextureManager::staticCast(infop->mTexture);
  2117. if (texp && mTextures.find(texp) != textures_end)
  2118. {
  2119. groupp->setState(LLSpatialGroup::GEOM_DIRTY);
  2120. }
  2121. }
  2122. }
  2123. }
  2124. LLSpatialGroup::bridge_list_t& blist = groupp->mBridgeList;
  2125. for (U32 i = 0, count = blist.size(); i < count; ++i)
  2126. {
  2127. traverse(blist[i]->mOctree);
  2128. }
  2129. }
  2130. public:
  2131. const LLViewerTextureList::dirty_list_t& mTextures;
  2132. };
  2133. // Called when a texture changes # of channels (causes faces to move to alpha
  2134. // pool)
  2135. void LLPipeline::dirtyPoolObjectTextures(const LLViewerTextureList::dirty_list_t& textures)
  2136. {
  2137. // *TODO: This is inefficient and causes frame spikes; need a better way to
  2138. // do this. Most of the time is spent in dirty.traverse.
  2139. for (pool_set_t::iterator iter = mPools.begin(), end = mPools.end();
  2140. iter != end; ++iter)
  2141. {
  2142. LLDrawPool* poolp = *iter;
  2143. // Only LLDrawPoolTerrain was using dirtyTextures() so I de-virtualized
  2144. // the latter and added the isTerrainPool() virtual, which is tested
  2145. // instead of isFacePool() with a useless call to an empty virtual
  2146. // dirtyTextures() mesthod for all other face pools... HB
  2147. if (poolp->isTerrainPool())
  2148. {
  2149. ((LLDrawPoolTerrain*)poolp)->dirtyTextures(textures);
  2150. }
  2151. }
  2152. LLOctreeDirtyTexture dirty(textures);
  2153. for (LLWorld::region_list_t::const_iterator
  2154. iter = gWorld.getRegionList().begin(),
  2155. end = gWorld.getRegionList().end();
  2156. iter != end; ++iter)
  2157. {
  2158. LLViewerRegion* regionp = *iter;
  2159. for (U32 i = 0; i < LLViewerRegion::PARTITION_VO_CACHE; ++i)
  2160. {
  2161. LLSpatialPartition* partp = regionp->getSpatialPartition(i);
  2162. // None of the partitions under PARTITION_VO_CACHE can be NULL
  2163. dirty.traverse(partp->mOctree);
  2164. }
  2165. }
  2166. }
  2167. LLDrawPool* LLPipeline::findPool(U32 type, LLViewerTexture* tex0)
  2168. {
  2169. LLDrawPool* poolp = NULL;
  2170. switch (type)
  2171. {
  2172. case LLDrawPool::POOL_SIMPLE:
  2173. poolp = mSimplePool;
  2174. break;
  2175. case LLDrawPool::POOL_GRASS:
  2176. poolp = mGrassPool;
  2177. break;
  2178. case LLDrawPool::POOL_ALPHA_MASK:
  2179. poolp = mAlphaMaskPool;
  2180. break;
  2181. case LLDrawPool::POOL_FULLBRIGHT_ALPHA_MASK:
  2182. poolp = mFullbrightAlphaMaskPool;
  2183. break;
  2184. case LLDrawPool::POOL_FULLBRIGHT:
  2185. poolp = mFullbrightPool;
  2186. break;
  2187. case LLDrawPool::POOL_INVISIBLE:
  2188. poolp = mInvisiblePool;
  2189. break;
  2190. case LLDrawPool::POOL_GLOW:
  2191. poolp = mGlowPool;
  2192. break;
  2193. case LLDrawPool::POOL_TREE:
  2194. poolp = get_ptr_in_map(mTreePools, (uintptr_t)tex0);
  2195. break;
  2196. case LLDrawPool::POOL_TERRAIN:
  2197. poolp = get_ptr_in_map(mTerrainPools, (uintptr_t)tex0);
  2198. break;
  2199. case LLDrawPool::POOL_BUMP:
  2200. poolp = mBumpPool;
  2201. break;
  2202. case LLDrawPool::POOL_MATERIALS:
  2203. poolp = mMaterialsPool;
  2204. break;
  2205. case LLDrawPool::POOL_ALPHA_PRE_WATER:
  2206. poolp = mAlphaPoolPreWater;
  2207. break;
  2208. case LLDrawPool::POOL_ALPHA_POST_WATER:
  2209. poolp = mAlphaPoolPostWater;
  2210. break;
  2211. case LLDrawPool::POOL_ALPHA:
  2212. poolp = mAlphaPool;
  2213. break;
  2214. case LLDrawPool::POOL_AVATAR:
  2215. case LLDrawPool::POOL_PUPPET:
  2216. break; // Do nothing
  2217. case LLDrawPool::POOL_SKY:
  2218. poolp = mSkyPool;
  2219. break;
  2220. case LLDrawPool::POOL_WATER:
  2221. poolp = mWaterPool;
  2222. break;
  2223. case LLDrawPool::POOL_WL_SKY:
  2224. poolp = mWLSkyPool;
  2225. break;
  2226. case LLDrawPool::POOL_MAT_PBR:
  2227. poolp = mPBROpaquePool;
  2228. break;
  2229. case LLDrawPool::POOL_MAT_PBR_ALPHA_MASK:
  2230. poolp = mPBRAlphaMaskPool;
  2231. break;
  2232. default:
  2233. llerrs << "Invalid Pool Type: " << type << llendl;
  2234. }
  2235. return poolp;
  2236. }
  2237. LLDrawPool* LLPipeline::getPool(U32 type, LLViewerTexture* tex0)
  2238. {
  2239. LLDrawPool* poolp = findPool(type, tex0);
  2240. if (poolp)
  2241. {
  2242. return poolp;
  2243. }
  2244. poolp = LLDrawPool::createPool(type, tex0);
  2245. addPool(poolp);
  2246. return poolp;
  2247. }
  2248. //static
  2249. LLDrawPool* LLPipeline::getPoolFromTE(const LLTextureEntry* tep,
  2250. LLViewerTexture* imagep)
  2251. {
  2252. return gPipeline.getPool(getPoolTypeFromTE(tep, imagep), imagep);
  2253. }
  2254. //static
  2255. U32 LLPipeline::getPoolTypeFromTE(const LLTextureEntry* tep,
  2256. LLViewerTexture* imagep)
  2257. {
  2258. if (!tep || !imagep)
  2259. {
  2260. return 0;
  2261. }
  2262. LLMaterial* matp = tep->getMaterialParams().get();
  2263. LLGLTFMaterial* gltfp = NULL;
  2264. if (gUsePBRShaders)
  2265. {
  2266. gltfp = tep->getGLTFRenderMaterial();
  2267. }
  2268. bool color_alpha = tep->getAlpha() < 0.999f;
  2269. bool alpha = color_alpha;
  2270. if (!alpha && imagep)
  2271. {
  2272. S32 components = imagep->getComponents();
  2273. alpha = components == 2 ||
  2274. (components == 4 &&
  2275. imagep->getType() != LLViewerTexture::MEDIA_TEXTURE);
  2276. }
  2277. if (alpha && matp)
  2278. {
  2279. if (matp->getDiffuseAlphaMode() == 1)
  2280. {
  2281. // Material's alpha mode is set to blend. Toss it into the alpha
  2282. // draw pool.
  2283. return LLDrawPool::POOL_ALPHA;
  2284. }
  2285. // 0 -> Material alpha mode set to none, never go to alpha pool
  2286. // 3 -> Material alpha mode set to emissive, never go to alpha pool
  2287. // other -> Material alpha mode set to "mask", go to alpha pool if
  2288. // fullbright, or material alpha mode is set to none, mask or
  2289. // emissive. Toss it into the opaque material draw pool.
  2290. alpha = color_alpha; // Use the pool matching the te alpha
  2291. }
  2292. if (alpha ||
  2293. (gltfp && gltfp->mAlphaMode == LLGLTFMaterial::ALPHA_MODE_BLEND))
  2294. {
  2295. return LLDrawPool::POOL_ALPHA;
  2296. }
  2297. if ((tep->getBumpmap() || tep->getShiny()) &&
  2298. (!matp || matp->getNormalID().isNull()))
  2299. {
  2300. return LLDrawPool::POOL_BUMP;
  2301. }
  2302. if (gltfp)
  2303. {
  2304. return LLDrawPool::POOL_MAT_PBR;
  2305. }
  2306. return matp ? LLDrawPool::POOL_MATERIALS : LLDrawPool::POOL_SIMPLE;
  2307. }
  2308. void LLPipeline::addPool(LLDrawPool* new_poolp)
  2309. {
  2310. mPools.insert(new_poolp);
  2311. addToQuickLookup(new_poolp);
  2312. }
  2313. void LLPipeline::allocDrawable(LLViewerObject* objp)
  2314. {
  2315. LLDrawable* drawablep = new LLDrawable(objp);
  2316. objp->mDrawable = drawablep;
  2317. // Encompass completely sheared objects by taking the most extreme
  2318. // point possible (<1.0, 1.0, 0.5>)
  2319. F32 radius = LLVector3(1.f, 1.f, 0.5f).scaleVec(objp->getScale()).length();
  2320. drawablep->setRadius(radius);
  2321. if (objp->isOrphaned())
  2322. {
  2323. drawablep->setState(LLDrawable::FORCE_INVISIBLE);
  2324. }
  2325. drawablep->updateXform(true);
  2326. }
  2327. void LLPipeline::unlinkDrawable(LLDrawable* drawable)
  2328. {
  2329. LL_FAST_TIMER(FTM_UNLINK);
  2330. // Make sure the drawable does not get deleted before we are done
  2331. LLPointer<LLDrawable> drawablep = drawable;
  2332. // Based on flags, remove the drawable from the queues that it is on.
  2333. if (drawablep->isState(LLDrawable::ON_MOVE_LIST))
  2334. {
  2335. LL_FAST_TIMER(FTM_REMOVE_FROM_MOVE_LIST);
  2336. for (U32 i = 0, count = mMovedList.size(); i < count; ++i)
  2337. {
  2338. if (mMovedList[i] == drawablep)
  2339. {
  2340. if (i < count - 1)
  2341. {
  2342. mMovedList[i] = std::move(mMovedList.back());
  2343. }
  2344. mMovedList.pop_back();
  2345. break;
  2346. }
  2347. }
  2348. }
  2349. LLSpatialGroup* groupp = drawablep->getSpatialGroup();
  2350. if (groupp && groupp->getSpatialPartition())
  2351. {
  2352. LL_FAST_TIMER(FTM_REMOVE_FROM_SPATIAL_PARTITION);
  2353. if (!groupp->getSpatialPartition()->remove(drawablep, groupp))
  2354. {
  2355. llwarns << "Could not remove object from spatial group" << llendl;
  2356. llassert(false);
  2357. }
  2358. }
  2359. {
  2360. LL_FAST_TIMER(FTM_REMOVE_FROM_LIGHT_SET);
  2361. mLights.erase(drawablep);
  2362. for (light_set_t::iterator iter = mNearbyLights.begin(),
  2363. end = mNearbyLights.end();
  2364. iter != end; ++iter)
  2365. {
  2366. if (iter->drawable == drawablep)
  2367. {
  2368. mNearbyLights.erase(iter);
  2369. break;
  2370. }
  2371. }
  2372. }
  2373. for (U32 i = 0; i < 2; ++i)
  2374. {
  2375. if (mShadowSpotLight[i] == drawablep)
  2376. {
  2377. mShadowSpotLight[i] = NULL;
  2378. }
  2379. if (mTargetShadowSpotLight[i] == drawablep)
  2380. {
  2381. mTargetShadowSpotLight[i] = NULL;
  2382. }
  2383. }
  2384. }
  2385. U32 LLPipeline::addObject(LLViewerObject* objp)
  2386. {
  2387. if (RenderDelayCreation)
  2388. {
  2389. mCreateQ.emplace_back(objp);
  2390. }
  2391. else
  2392. {
  2393. createObject(objp);
  2394. }
  2395. return 1;
  2396. }
  2397. void LLPipeline::createObjects(F32 max_dtime)
  2398. {
  2399. LL_FAST_TIMER(FTM_PIPELINE_CREATE);
  2400. LLTimer update_timer;
  2401. while (!mCreateQ.empty() && update_timer.getElapsedTimeF32() < max_dtime)
  2402. {
  2403. LLViewerObject* objp = mCreateQ.front();
  2404. if (objp && !objp->isDead())
  2405. {
  2406. createObject(objp);
  2407. }
  2408. mCreateQ.pop_front();
  2409. }
  2410. }
  2411. void LLPipeline::createObject(LLViewerObject* objp)
  2412. {
  2413. LLDrawable* drawablep = objp->mDrawable;
  2414. if (!drawablep)
  2415. {
  2416. drawablep = objp->createDrawable();
  2417. llassert(drawablep);
  2418. }
  2419. else
  2420. {
  2421. llerrs << "Redundant drawable creation !" << llendl;
  2422. }
  2423. LLViewerObject* parentp = (LLViewerObject*)objp->getParent();
  2424. if (parentp)
  2425. {
  2426. // LLPipeline::addObject 1
  2427. objp->setDrawableParent(parentp->mDrawable);
  2428. }
  2429. else
  2430. {
  2431. // LLPipeline::addObject 2
  2432. objp->setDrawableParent(NULL);
  2433. }
  2434. markRebuild(drawablep);
  2435. if (RenderAnimateRes && drawablep->getVOVolume())
  2436. {
  2437. // Fun animated res
  2438. drawablep->updateXform(true);
  2439. drawablep->clearState(LLDrawable::MOVE_UNDAMPED);
  2440. drawablep->setScale(LLVector3::zero);
  2441. drawablep->makeActive();
  2442. }
  2443. }
  2444. void LLPipeline::resetFrameStats()
  2445. {
  2446. static LLCachedControl<bool> render_info(gSavedSettings,
  2447. "DebugShowRenderInfo");
  2448. if (render_info)
  2449. {
  2450. mNeedsDrawStats = true;
  2451. }
  2452. else if (mNeedsDrawStats && !LLFloaterStats::findInstance())
  2453. {
  2454. mNeedsDrawStats = false;
  2455. }
  2456. if (mNeedsDrawStats)
  2457. {
  2458. mTrianglesDrawnStat.addValue(F32(mTrianglesDrawn) * .001f);
  2459. mTrianglesDrawn = 0;
  2460. }
  2461. if (mOldRenderDebugMask != mRenderDebugMask)
  2462. {
  2463. gObjectList.clearDebugText();
  2464. mOldRenderDebugMask = mRenderDebugMask;
  2465. }
  2466. }
  2467. // External functions for asynchronous updating
  2468. void LLPipeline::updateMoveDampedAsync(LLDrawable* drawablep)
  2469. {
  2470. if (sFreezeTime)
  2471. {
  2472. return;
  2473. }
  2474. if (!drawablep)
  2475. {
  2476. llerrs << "Called with NULL drawable" << llendl;
  2477. return;
  2478. }
  2479. if (drawablep->isState(LLDrawable::EARLY_MOVE))
  2480. {
  2481. return;
  2482. }
  2483. // Update drawable now
  2484. drawablep->clearState(LLDrawable::MOVE_UNDAMPED); // Force to DAMPED
  2485. drawablep->updateMove(); // Returns done
  2486. // Flag says we already did an undamped move this frame:
  2487. drawablep->setState(LLDrawable::EARLY_MOVE);
  2488. // Put on move list so that EARLY_MOVE gets cleared
  2489. if (!drawablep->isState(LLDrawable::ON_MOVE_LIST))
  2490. {
  2491. mMovedList.emplace_back(drawablep);
  2492. drawablep->setState(LLDrawable::ON_MOVE_LIST);
  2493. }
  2494. }
  2495. void LLPipeline::updateMoveNormalAsync(LLDrawable* drawablep)
  2496. {
  2497. if (sFreezeTime)
  2498. {
  2499. return;
  2500. }
  2501. if (!drawablep)
  2502. {
  2503. llerrs << "Called with NULL drawable" << llendl;
  2504. }
  2505. if (drawablep->isState(LLDrawable::EARLY_MOVE))
  2506. {
  2507. return;
  2508. }
  2509. // Update drawable now
  2510. drawablep->setState(LLDrawable::MOVE_UNDAMPED); // Force to UNDAMPED
  2511. drawablep->updateMove();
  2512. // Flag says we already did an undamped move this frame:
  2513. drawablep->setState(LLDrawable::EARLY_MOVE);
  2514. // Put on move list so that EARLY_MOVE gets cleared
  2515. if (!drawablep->isState(LLDrawable::ON_MOVE_LIST))
  2516. {
  2517. mMovedList.emplace_back(drawablep);
  2518. drawablep->setState(LLDrawable::ON_MOVE_LIST);
  2519. }
  2520. }
  2521. void LLPipeline::updateMovedList(LLDrawable::draw_vec_t& moved_list)
  2522. {
  2523. LL_TRACY_TIMER(TRC_MOVED_LIST);
  2524. for (U32 i = 0, count = moved_list.size(); i < count; )
  2525. {
  2526. LLDrawable* drawablep = moved_list[i];
  2527. bool done = true;
  2528. if (!drawablep->isDead() &&
  2529. !drawablep->isState(LLDrawable::EARLY_MOVE))
  2530. {
  2531. done = drawablep->updateMove();
  2532. }
  2533. drawablep->clearState(LLDrawable::EARLY_MOVE |
  2534. LLDrawable::MOVE_UNDAMPED);
  2535. if (done)
  2536. {
  2537. if (drawablep->isRoot() && !drawablep->isState(LLDrawable::ACTIVE))
  2538. {
  2539. drawablep->makeStatic();
  2540. }
  2541. drawablep->clearState(LLDrawable::ON_MOVE_LIST);
  2542. if (drawablep->isState(LLDrawable::ANIMATED_CHILD))
  2543. {
  2544. // Will likely not receive any future world matrix updates;
  2545. // this keeps attachments from getting stuck in space and
  2546. // falling off your avatar
  2547. drawablep->clearState(LLDrawable::ANIMATED_CHILD);
  2548. markRebuild(drawablep, LLDrawable::REBUILD_VOLUME);
  2549. LLViewerObject* objp = drawablep->getVObj().get();
  2550. if (objp)
  2551. {
  2552. objp->dirtySpatialGroup();
  2553. }
  2554. }
  2555. if (i < --count)
  2556. {
  2557. moved_list[i] = std::move(moved_list.back());
  2558. }
  2559. moved_list.pop_back();
  2560. }
  2561. else
  2562. {
  2563. ++i;
  2564. }
  2565. }
  2566. }
  2567. void LLPipeline::updateMove(bool balance_vo_cache)
  2568. {
  2569. LL_FAST_TIMER(FTM_UPDATE_MOVE);
  2570. if (sFreezeTime)
  2571. {
  2572. return;
  2573. }
  2574. for (LLDrawable::draw_set_t::iterator iter = mRetexturedList.begin(),
  2575. end = mRetexturedList.end();
  2576. iter != end; ++iter)
  2577. {
  2578. LLDrawable* drawablep = *iter;
  2579. if (drawablep && !drawablep->isDead())
  2580. {
  2581. drawablep->updateTexture();
  2582. }
  2583. }
  2584. mRetexturedList.clear();
  2585. updateMovedList(mMovedList);
  2586. // Balance octrees
  2587. {
  2588. LL_FAST_TIMER(FTM_OCTREE_BALANCE);
  2589. for (LLWorld::region_list_t::const_iterator
  2590. iter = gWorld.getRegionList().begin(),
  2591. end = gWorld.getRegionList().end();
  2592. iter != end; ++iter)
  2593. {
  2594. LLViewerRegion* regionp = *iter;
  2595. for (U32 i = 0; i < LLViewerRegion::NUM_PARTITIONS; ++i)
  2596. {
  2597. if (i == LLViewerRegion::PARTITION_VO_CACHE &&
  2598. !balance_vo_cache)
  2599. {
  2600. continue;
  2601. }
  2602. LLSpatialPartition* partp = regionp->getSpatialPartition(i);
  2603. if (partp)
  2604. {
  2605. partp->mOctree->balance();
  2606. }
  2607. }
  2608. }
  2609. }
  2610. }
  2611. /////////////////////////////////////////////////////////////////////////////
  2612. // Culling and occlusion testing
  2613. /////////////////////////////////////////////////////////////////////////////
  2614. //static
  2615. F32 LLPipeline::calcPixelArea(LLVector3 center, LLVector3 size,
  2616. LLCamera& camera)
  2617. {
  2618. LLVector3 lookAt = center - camera.getOrigin();
  2619. F32 dist = lookAt.length();
  2620. // Ramp down distance for nearby objects; shrink dist by dist / 16.
  2621. if (dist < 16.f)
  2622. {
  2623. dist /= 16.f;
  2624. dist *= dist;
  2625. dist *= 16.f;
  2626. }
  2627. if (dist <= 0.f)
  2628. {
  2629. dist = F32_MIN;
  2630. }
  2631. // Get area of circle around node
  2632. F32 app_angle = atanf(size.length() / dist);
  2633. F32 radius = app_angle * LLDrawable::sCurPixelAngle;
  2634. return radius * radius * F_PI;
  2635. }
  2636. //static
  2637. F32 LLPipeline::calcPixelArea(const LLVector4a& center, const LLVector4a& size,
  2638. LLCamera& camera)
  2639. {
  2640. LLVector4a origin;
  2641. origin.load3(camera.getOrigin().mV);
  2642. LLVector4a lookAt;
  2643. lookAt.setSub(center, origin);
  2644. F32 dist = lookAt.getLength3().getF32();
  2645. // Ramp down distance for nearby objects.
  2646. // Shrink dist by dist/16.
  2647. if (dist < 16.f)
  2648. {
  2649. dist *= 0.0625f; // 1/16
  2650. dist *= dist;
  2651. dist *= 16.f;
  2652. }
  2653. if (dist <= 0.f)
  2654. {
  2655. dist = F32_MIN;
  2656. }
  2657. // Get area of circle around node
  2658. F32 app_angle = atanf(size.getLength3().getF32() / dist);
  2659. F32 radius = app_angle * LLDrawable::sCurPixelAngle;
  2660. return radius * radius * F_PI;
  2661. }
  2662. void LLPipeline::grabReferences(LLCullResult& result)
  2663. {
  2664. sCull = &result;
  2665. }
  2666. void LLPipeline::clearReferences()
  2667. {
  2668. sCull = NULL;
  2669. mGroupSaveQ.clear();
  2670. }
  2671. #if LL_DEBUG
  2672. static void check_references(LLSpatialGroup* groupp, LLDrawable* drawablep)
  2673. {
  2674. const LLSpatialGroup::element_list& data = groupp->getData();
  2675. for (U32 i = 0, count = data.size(); i < count; ++i)
  2676. {
  2677. if (drawablep == (LLDrawable*)data[i]->getDrawable())
  2678. {
  2679. llerrs << "LLDrawable deleted while actively reference by LLPipeline."
  2680. << llendl;
  2681. }
  2682. }
  2683. }
  2684. static void check_references(LLDrawable* drawablep, LLFace* facep)
  2685. {
  2686. for (S32 i = 0, count = drawablep->getNumFaces(); i < count; ++i)
  2687. {
  2688. if (drawablep->getFace(i) == facep)
  2689. {
  2690. llerrs << "LLFace deleted while actively referenced by LLPipeline."
  2691. << llendl;
  2692. }
  2693. }
  2694. }
  2695. static void check_references(LLSpatialGroup* groupp, LLFace* facep)
  2696. {
  2697. const LLSpatialGroup::element_list& data = groupp->getData();
  2698. for (U32 i = 0, count = data.size(); i < count; ++i)
  2699. {
  2700. LLDrawable* drawablep = (LLDrawable*)data[i]->getDrawable();
  2701. if (drawablep)
  2702. {
  2703. check_references(drawablep, facep);
  2704. }
  2705. }
  2706. }
  2707. static void check_references(LLSpatialGroup* groupp, LLDrawInfo* draw_infop)
  2708. {
  2709. for (LLSpatialGroup::draw_map_t::const_iterator
  2710. it = groupp->mDrawMap.begin(), end = groupp->mDrawMap.end();
  2711. it != end; ++it)
  2712. {
  2713. const LLSpatialGroup::drawmap_elem_t& draw_vec = it->second;
  2714. for (U32 i = 0, count = draw_vec.size(); i < count; ++i)
  2715. {
  2716. LLDrawInfo* paramsp = draw_vec[i].get();
  2717. if (paramsp == draw_infop)
  2718. {
  2719. llerrs << "LLDrawInfo deleted while actively referenced by LLPipeline."
  2720. << llendl;
  2721. }
  2722. }
  2723. }
  2724. }
  2725. void LLPipeline::checkReferences(LLFace* facep)
  2726. {
  2727. if (sCull)
  2728. {
  2729. LLCullResult::sg_list_t& visible_groups = sCull->getVisibleGroups();
  2730. for (U32 i = 0, count = visible_groups.size(); i < count; ++i)
  2731. {
  2732. check_references(visible_groups[i], facep);
  2733. }
  2734. LLCullResult::sg_list_t& alpha_groups = sCull->getAlphaGroups();
  2735. for (U32 i = 0, count = alpha_groups.size(); i < count; ++i)
  2736. {
  2737. check_references(alpha_groups[i], facep);
  2738. }
  2739. LLCullResult::sg_list_t& rig_groups = sCull->getRiggedAlphaGroups();
  2740. for (U32 i = 0, count = rig_groups.size(); i < count; ++i)
  2741. {
  2742. check_references(rig_groups[i], facep);
  2743. }
  2744. LLCullResult::sg_list_t& draw_groups = sCull->getDrawableGroups();
  2745. for (U32 i = 0, count = draw_groups.size(); i < count; ++i)
  2746. {
  2747. check_references(draw_groups[i], facep);
  2748. }
  2749. LLCullResult::drawable_list_t& visible = sCull->getVisibleList();
  2750. for (U32 i = 0, count = visible.size(); i < count; ++i)
  2751. {
  2752. LLDrawable* drawablep = visible[i];
  2753. check_references(drawablep, facep);
  2754. }
  2755. }
  2756. }
  2757. void LLPipeline::checkReferences(LLDrawable* drawablep)
  2758. {
  2759. if (sCull)
  2760. {
  2761. LLCullResult::sg_list_t& visible_groups = sCull->getVisibleGroups();
  2762. for (U32 i = 0, count = visible_groups.size(); i < count; ++i)
  2763. {
  2764. check_references(visible_groups[i], drawablep);
  2765. }
  2766. LLCullResult::sg_list_t& alpha_groups = sCull->getAlphaGroups();
  2767. for (U32 i = 0, count = alpha_groups.size(); i < count; ++i)
  2768. {
  2769. check_references(alpha_groups[i], drawablep);
  2770. }
  2771. LLCullResult::sg_list_t& rig_groups = sCull->getRiggedAlphaGroups();
  2772. for (U32 i = 0, count = rig_groups.size(); i < count; ++i)
  2773. {
  2774. check_references(rig_groups[i], drawablep);
  2775. }
  2776. LLCullResult::sg_list_t& draw_groups = sCull->getDrawableGroups();
  2777. for (U32 i = 0, count = draw_groups.size(); i < count; ++i)
  2778. {
  2779. check_references(draw_groups[i], drawablep);
  2780. }
  2781. LLCullResult::drawable_list_t& visible = sCull->getVisibleList();
  2782. for (U32 i = 0, count = visible.size(); i < count; ++i)
  2783. {
  2784. if (drawablep == visible[i])
  2785. {
  2786. llerrs << "LLDrawable deleted while actively referenced by LLPipeline."
  2787. << llendl;
  2788. }
  2789. }
  2790. }
  2791. }
  2792. void LLPipeline::checkReferences(LLDrawInfo* draw_infop)
  2793. {
  2794. if (sCull)
  2795. {
  2796. LLCullResult::sg_list_t& visible_groups = sCull->getVisibleGroups();
  2797. for (U32 i = 0, count = visible_groups.size(); i < count; ++i)
  2798. {
  2799. check_references(visible_groups[i], draw_infop);
  2800. }
  2801. LLCullResult::sg_list_t& alpha_groups = sCull->getAlphaGroups();
  2802. for (U32 i = 0, count = alpha_groups.size(); i < count; ++i)
  2803. {
  2804. check_references(alpha_groups[i], draw_infop);
  2805. }
  2806. LLCullResult::sg_list_t& rig_groups = sCull->getRiggedAlphaGroups();
  2807. for (U32 i = 0, count = rig_groups.size(); i < count; ++i)
  2808. {
  2809. check_references(rig_groups[i], draw_infop);
  2810. }
  2811. LLCullResult::sg_list_t& draw_groups = sCull->getDrawableGroups();
  2812. for (U32 i = 0, count = draw_groups.size(); i < count; ++i)
  2813. {
  2814. check_references(draw_groups[i], draw_infop);
  2815. }
  2816. }
  2817. }
  2818. void LLPipeline::checkReferences(LLSpatialGroup* groupp)
  2819. {
  2820. if (sCull)
  2821. {
  2822. LLCullResult::sg_list_t& visible_groups = sCull->getVisibleGroups();
  2823. for (U32 i = 0, count = visible_groups.size(); i < count; ++i)
  2824. {
  2825. if (groupp == visible_groups[i])
  2826. {
  2827. llerrs << "LLSpatialGroup deleted while actively referenced by LLPipeline."
  2828. << llendl;
  2829. }
  2830. }
  2831. LLCullResult::sg_list_t& alpha_groups = sCull->getAlphaGroups();
  2832. for (U32 i = 0, count = alpha_groups.size(); i < count; ++i)
  2833. {
  2834. if (groupp == alpha_groups[i])
  2835. {
  2836. llerrs << "LLSpatialGroup deleted while actively referenced by LLPipeline."
  2837. << llendl;
  2838. }
  2839. }
  2840. LLCullResult::sg_list_t& rig_groups = sCull->getRiggedAlphaGroups();
  2841. for (U32 i = 0, count = rig_groups.size(); i < count; ++i)
  2842. {
  2843. if (groupp == rig_groups[i])
  2844. {
  2845. llerrs << "LLSpatialGroup deleted while actively referenced by LLPipeline."
  2846. << llendl;
  2847. }
  2848. }
  2849. LLCullResult::sg_list_t& draw_groups = sCull->getDrawableGroups();
  2850. for (U32 i = 0, count = draw_groups.size(); i < count; ++i)
  2851. {
  2852. if (groupp == draw_groups[i])
  2853. {
  2854. llerrs << "LLSpatialGroup deleted while actively referenced by LLPipeline."
  2855. << llendl;
  2856. }
  2857. }
  2858. }
  2859. }
  2860. #endif
  2861. bool LLPipeline::getVisibleExtents(LLCamera& camera, LLVector3& min,
  2862. LLVector3& max)
  2863. {
  2864. constexpr F32 max_val = 65536.f;
  2865. constexpr F32 min_val = -65536.f;
  2866. min.set(max_val, max_val, max_val);
  2867. max.set(min_val, min_val, min_val);
  2868. S32 saved_camera_id = LLViewerCamera::sCurCameraID;
  2869. LLViewerCamera::sCurCameraID = LLViewerCamera::CAMERA_WORLD;
  2870. bool res = true;
  2871. for (LLWorld::region_list_t::const_iterator
  2872. iter = gWorld.getRegionList().begin(),
  2873. end = gWorld.getRegionList().end();
  2874. iter != end; ++iter)
  2875. {
  2876. LLViewerRegion* regionp = *iter;
  2877. for (U32 i = 0; i < LLViewerRegion::PARTITION_VO_CACHE; ++i)
  2878. {
  2879. LLSpatialPartition* partp = regionp->getSpatialPartition(i);
  2880. // None of the partitions under PARTITION_VO_CACHE can be NULL
  2881. if (hasRenderType(partp->mDrawableType) &&
  2882. !partp->getVisibleExtents(camera, min, max))
  2883. {
  2884. res = false;
  2885. }
  2886. }
  2887. }
  2888. LLViewerCamera::sCurCameraID = saved_camera_id;
  2889. return res;
  2890. }
  2891. //static
  2892. bool LLPipeline::isWaterClip()
  2893. {
  2894. if (sRenderMirrors && gPipeline.mHeroProbeManager.isMirrorPass())
  2895. {
  2896. // We always pretend that we are not clipping water when rendering
  2897. // mirrors.
  2898. return false;
  2899. }
  2900. return (!RenderTransparentWater || gCubeSnapshot) && !sRenderingHUDs;
  2901. }
  2902. // Branched version for the PBR renderer
  2903. void LLPipeline::updateCullPBR(LLCamera& camera, LLCullResult& result)
  2904. {
  2905. if (isWaterClip())
  2906. {
  2907. LLVector3 pnorm;
  2908. if (sUnderWaterRender)
  2909. {
  2910. // Camera is below water, cull above water
  2911. pnorm.set(0.f, 0.f, 1.f);
  2912. }
  2913. else
  2914. {
  2915. // Camera is above water, cull below water
  2916. pnorm.set(0.f, 0.f, -1.f);
  2917. }
  2918. LLPlane plane(LLVector3(0.f, 0.f, mWaterHeight), pnorm);
  2919. camera.setUserClipPlane(plane);
  2920. }
  2921. else
  2922. {
  2923. camera.disableUserClipPlane();
  2924. }
  2925. grabReferences(result);
  2926. sCull->clear();
  2927. bool do_occlusion_cull = sUseOcclusion > 0;
  2928. for (LLWorld::region_list_t::const_iterator
  2929. iter = gWorld.getRegionList().begin(),
  2930. end = gWorld.getRegionList().end();
  2931. iter != end; ++iter)
  2932. {
  2933. LLViewerRegion* regionp = *iter;
  2934. for (U32 i = 0; i < LLViewerRegion::PARTITION_VO_CACHE; ++i)
  2935. {
  2936. LLSpatialPartition* partp = regionp->getSpatialPartition(i);
  2937. // None of the partitions under PARTITION_VO_CACHE can be NULL
  2938. if (hasRenderType(partp->mDrawableType))
  2939. {
  2940. partp->cull(camera);
  2941. }
  2942. }
  2943. // Scan the VO Cache tree
  2944. LLVOCachePartition* vo_partp = regionp->getVOCachePartition();
  2945. if (vo_partp)
  2946. {
  2947. vo_partp->cull(camera, do_occlusion_cull);
  2948. }
  2949. }
  2950. if (hasRenderType(RENDER_TYPE_SKY) &&
  2951. gSky.mVOSkyp.notNull() && gSky.mVOSkyp->mDrawable.notNull())
  2952. {
  2953. gSky.mVOSkyp->mDrawable->setVisible(camera);
  2954. sCull->pushDrawable(gSky.mVOSkyp->mDrawable);
  2955. gSky.updateCull();
  2956. }
  2957. if (hasRenderType(RENDER_TYPE_WL_SKY) &&
  2958. gSky.mVOWLSkyp.notNull() && gSky.mVOWLSkyp->mDrawable.notNull())
  2959. {
  2960. gSky.mVOWLSkyp->mDrawable->setVisible(camera);
  2961. sCull->pushDrawable(gSky.mVOWLSkyp->mDrawable);
  2962. }
  2963. }
  2964. void LLPipeline::updateCull(LLCamera& camera, LLCullResult& result,
  2965. LLPlane* planep, bool hud_attachments)
  2966. {
  2967. LL_FAST_TIMER(FTM_CULL);
  2968. if (gUsePBRShaders)
  2969. {
  2970. updateCullPBR(camera, result);
  2971. return;
  2972. }
  2973. if (planep)
  2974. {
  2975. camera.setUserClipPlane(*planep);
  2976. }
  2977. else
  2978. {
  2979. camera.disableUserClipPlane();
  2980. }
  2981. grabReferences(result);
  2982. sCull->clear();
  2983. bool to_texture = sUseOcclusion > 1 && shadersLoaded();
  2984. if (to_texture)
  2985. {
  2986. if (sRenderDeferred && sUseOcclusion > 1)
  2987. {
  2988. mOcclusionDepth.bindTarget();
  2989. }
  2990. else
  2991. {
  2992. mRT->mScreen.bindTarget();
  2993. }
  2994. }
  2995. if (sUseOcclusion > 1)
  2996. {
  2997. gGL.setColorMask(false, false);
  2998. }
  2999. gGL.matrixMode(LLRender::MM_PROJECTION);
  3000. gGL.pushMatrix();
  3001. gGL.loadMatrix(gGLLastProjection);
  3002. gGL.matrixMode(LLRender::MM_MODELVIEW);
  3003. gGL.pushMatrix();
  3004. gGLLastMatrix = NULL;
  3005. gGL.loadMatrix(gGLLastModelView);
  3006. LLGLDisable blend(GL_BLEND);
  3007. gGL.getTexUnit(0)->unbind(LLTexUnit::TT_TEXTURE);
  3008. LLGLDepthTest depth(GL_TRUE, GL_FALSE);
  3009. bool bound_shader = false;
  3010. if (shadersLoaded() && !LLGLSLShader::sCurBoundShader)
  3011. {
  3012. // If no shader is currently bound, use the occlusion shader instead of
  3013. // fixed function if we can (shadow render uses a special shader that
  3014. // clamps to clip planes)
  3015. bound_shader = true;
  3016. gOcclusionCubeProgram.bind();
  3017. }
  3018. if (sUseOcclusion > 1)
  3019. {
  3020. if (mCubeVB.isNull())
  3021. {
  3022. sUseOcclusion = 0;
  3023. llwarns << "No available Cube VB, disabling occlusion" << llendl;
  3024. }
  3025. else
  3026. {
  3027. mCubeVB->setBuffer(LLVertexBuffer::MAP_VERTEX);
  3028. }
  3029. }
  3030. bool do_occlusion_cull = sUseOcclusion > 1 && !gUseWireframe;
  3031. for (LLWorld::region_list_t::const_iterator
  3032. iter = gWorld.getRegionList().begin(),
  3033. end = gWorld.getRegionList().end();
  3034. iter != end; ++iter)
  3035. {
  3036. LLViewerRegion* regionp = *iter;
  3037. for (U32 i = 0; i < LLViewerRegion::PARTITION_VO_CACHE; ++i)
  3038. {
  3039. LLSpatialPartition* partp = regionp->getSpatialPartition(i);
  3040. // None of the partitions under PARTITION_VO_CACHE can be NULL
  3041. if (hasRenderType(partp->mDrawableType) ||
  3042. (!hud_attachments && i == LLViewerRegion::PARTITION_BRIDGE))
  3043. {
  3044. partp->cull(camera);
  3045. }
  3046. }
  3047. // Scan the VO Cache tree
  3048. LLVOCachePartition* vo_partp = regionp->getVOCachePartition();
  3049. if (vo_partp)
  3050. {
  3051. vo_partp->cull(camera, do_occlusion_cull);
  3052. }
  3053. }
  3054. if (bound_shader)
  3055. {
  3056. gOcclusionCubeProgram.unbind();
  3057. }
  3058. if (hasRenderType(RENDER_TYPE_SKY) &&
  3059. gSky.mVOSkyp.notNull() && gSky.mVOSkyp->mDrawable.notNull())
  3060. {
  3061. gSky.mVOSkyp->mDrawable->setVisible(camera);
  3062. sCull->pushDrawable(gSky.mVOSkyp->mDrawable);
  3063. gSky.updateCull();
  3064. }
  3065. bool can_use_wl_shaders = canUseWindLightShaders();
  3066. if (can_use_wl_shaders &&
  3067. hasRenderType(RENDER_TYPE_WL_SKY) &&
  3068. gSky.mVOWLSkyp.notNull() && gSky.mVOWLSkyp->mDrawable.notNull())
  3069. {
  3070. gSky.mVOWLSkyp->mDrawable->setVisible(camera);
  3071. sCull->pushDrawable(gSky.mVOWLSkyp->mDrawable);
  3072. }
  3073. if (!sReflectionRender &&
  3074. (hasRenderType(RENDER_TYPE_WATER) ||
  3075. hasRenderType(RENDER_TYPE_VOIDWATER)))
  3076. {
  3077. gWorld.precullWaterObjects(camera, sCull);
  3078. }
  3079. gGL.matrixMode(LLRender::MM_PROJECTION);
  3080. gGL.popMatrix();
  3081. gGL.matrixMode(LLRender::MM_MODELVIEW);
  3082. gGL.popMatrix();
  3083. if (sUseOcclusion > 1)
  3084. {
  3085. gGL.setColorMask(true, false);
  3086. }
  3087. if (to_texture)
  3088. {
  3089. if (sRenderDeferred && sUseOcclusion > 1)
  3090. {
  3091. mOcclusionDepth.flush();
  3092. }
  3093. else
  3094. {
  3095. mRT->mScreen.flush();
  3096. }
  3097. }
  3098. }
  3099. void LLPipeline::markNotCulled(LLSpatialGroup* groupp, LLCamera& camera)
  3100. {
  3101. if (groupp->isEmpty())
  3102. {
  3103. return;
  3104. }
  3105. groupp->setVisible();
  3106. if (LLViewerCamera::sCurCameraID == LLViewerCamera::CAMERA_WORLD &&
  3107. !gCubeSnapshot)
  3108. {
  3109. groupp->updateDistance(camera);
  3110. }
  3111. if (!groupp->getSpatialPartition()->mRenderByGroup)
  3112. {
  3113. // Render by drawable
  3114. sCull->pushDrawableGroup(groupp);
  3115. }
  3116. else
  3117. {
  3118. // Render by group
  3119. sCull->pushVisibleGroup(groupp);
  3120. }
  3121. ++mNumVisibleNodes;
  3122. if (!gUsePBRShaders)
  3123. {
  3124. return;
  3125. }
  3126. S32 frame = LLViewerOctreeEntryData::getCurrentFrame() - 1;
  3127. if (groupp->needsUpdate() ||
  3128. groupp->getVisible(LLViewerCamera::sCurCameraID) < frame)
  3129. {
  3130. // Include this group in occlusion groups, not because we know it is an
  3131. // occluder, but because we want to run an occlusion query to find out
  3132. // if it is an occluder.
  3133. markOccluder(groupp);
  3134. }
  3135. }
  3136. void LLPipeline::markOccluder(LLSpatialGroup* groupp)
  3137. {
  3138. if (sUseOcclusion > 1 && groupp &&
  3139. !groupp->isOcclusionState(LLSpatialGroup::ACTIVE_OCCLUSION))
  3140. {
  3141. LLSpatialGroup* parentp = groupp->getParent();
  3142. if (!parentp || !parentp->isOcclusionState(LLSpatialGroup::OCCLUDED))
  3143. {
  3144. // Only mark top most occluders as active occlusion
  3145. sCull->pushOcclusionGroup(groupp);
  3146. groupp->setOcclusionState(LLSpatialGroup::ACTIVE_OCCLUSION);
  3147. if (parentp &&
  3148. !parentp->isOcclusionState(LLSpatialGroup::ACTIVE_OCCLUSION) &&
  3149. parentp->getElementCount() == 0 && parentp->needsUpdate())
  3150. {
  3151. sCull->pushOcclusionGroup(groupp);
  3152. parentp->setOcclusionState(LLSpatialGroup::ACTIVE_OCCLUSION);
  3153. }
  3154. }
  3155. }
  3156. }
  3157. // Used only by the EE renderer
  3158. void LLPipeline::downsampleDepthBuffer(LLRenderTarget& source,
  3159. LLRenderTarget& dest,
  3160. LLRenderTarget* scratch_space)
  3161. {
  3162. LLGLSLShader* last_shaderp = LLGLSLShader::sCurBoundShaderPtr;
  3163. if (scratch_space)
  3164. {
  3165. scratch_space->copyContents(source, 0, 0,
  3166. source.getWidth(), source.getHeight(),
  3167. 0, 0, scratch_space->getWidth(),
  3168. scratch_space->getHeight(),
  3169. GL_DEPTH_BUFFER_BIT, GL_NEAREST);
  3170. }
  3171. dest.bindTarget();
  3172. dest.clear(GL_DEPTH_BUFFER_BIT);
  3173. LLStrider<LLVector3> vert;
  3174. if (mDeferredVB.isNull() || !mDeferredVB->getVertexStrider(vert))
  3175. {
  3176. return;
  3177. }
  3178. vert[0].set(-1.f, 1.f, 0.f);
  3179. vert[1].set(-1.f, -3.f, 0.f);
  3180. vert[2].set(3.f, 1.f, 0.f);
  3181. LLGLSLShader* shaderp;
  3182. if (source.getUsage() == LLTexUnit::TT_RECT_TEXTURE)
  3183. {
  3184. shaderp = &gDownsampleDepthRectProgram;
  3185. shaderp->bind();
  3186. shaderp->uniform2f(sDelta, 1.f, 1.f);
  3187. shaderp->uniform2f(LLShaderMgr::DEFERRED_SCREEN_RES,
  3188. source.getWidth(), source.getHeight());
  3189. }
  3190. else
  3191. {
  3192. shaderp = &gDownsampleDepthProgram;
  3193. shaderp->bind();
  3194. shaderp->uniform2f(sDelta, 1.f / source.getWidth(),
  3195. 1.f / source.getHeight());
  3196. shaderp->uniform2f(LLShaderMgr::DEFERRED_SCREEN_RES, 1.f, 1.f);
  3197. }
  3198. gGL.getTexUnit(0)->bind(scratch_space ? scratch_space : &source, true);
  3199. {
  3200. LLGLDepthTest depth(GL_TRUE, GL_TRUE, GL_ALWAYS);
  3201. mDeferredVB->setBuffer(LLVertexBuffer::MAP_VERTEX);
  3202. mDeferredVB->drawArrays(LLRender::TRIANGLES, 0, 3);
  3203. }
  3204. dest.flush();
  3205. if (last_shaderp)
  3206. {
  3207. last_shaderp->bind();
  3208. }
  3209. else
  3210. {
  3211. shaderp->unbind();
  3212. }
  3213. }
  3214. // Used only by the EE renderer
  3215. void LLPipeline::doOcclusion(LLCamera& camera, LLRenderTarget& source,
  3216. LLRenderTarget& dest,
  3217. LLRenderTarget* scratch_space)
  3218. {
  3219. downsampleDepthBuffer(source, dest, scratch_space);
  3220. dest.bindTarget();
  3221. doOcclusion(camera);
  3222. dest.flush();
  3223. }
  3224. // Branched version for the PBR renderer
  3225. void LLPipeline::doOcclusionPBR(LLCamera& camera)
  3226. {
  3227. if (sReflectionProbesEnabled && !sShadowRender && !gCubeSnapshot)
  3228. {
  3229. gGL.setColorMask(false, false);
  3230. LLGLDepthTest depth(GL_TRUE, GL_FALSE);
  3231. LLGLDisable cull(GL_CULL_FACE);
  3232. gOcclusionCubeProgram.bind();
  3233. mCubeVB->setBuffer();
  3234. mReflectionMapManager.doOcclusion();
  3235. gOcclusionCubeProgram.unbind();
  3236. gGL.setColorMask(true, true);
  3237. }
  3238. if (sRenderMirrors && !sShadowRender && !gCubeSnapshot)
  3239. {
  3240. gGL.setColorMask(false, false);
  3241. LLGLDepthTest depth(GL_TRUE, GL_FALSE);
  3242. LLGLDisable cull(GL_CULL_FACE);
  3243. gOcclusionCubeProgram.bind();
  3244. mCubeVB->setBuffer();
  3245. mHeroProbeManager.doOcclusion();
  3246. gOcclusionCubeProgram.unbind();
  3247. gGL.setColorMask(true, true);
  3248. }
  3249. if (!sCull->hasOcclusionGroups() &&
  3250. !LLVOCachePartition::sNeedsOcclusionCheck)
  3251. {
  3252. return;
  3253. }
  3254. LLVertexBuffer::unbind();
  3255. gGL.setColorMask(false, false);
  3256. LLGLDisable blend(GL_BLEND);
  3257. gGL.getTexUnit(0)->unbind(LLTexUnit::TT_TEXTURE);
  3258. LLGLDepthTest depth(GL_TRUE, GL_FALSE);
  3259. LLGLDisable cull(GL_CULL_FACE);
  3260. gOcclusionCubeProgram.bind();
  3261. mCubeVB->setBuffer();
  3262. LLCullResult::sg_list_t& occ_groups = sCull->getOcclusionGroups();
  3263. for (U32 i = 0, count = occ_groups.size(); i < count; ++i)
  3264. {
  3265. LLSpatialGroup* groupp = occ_groups[i];
  3266. if (!groupp->isDead())
  3267. {
  3268. groupp->doOcclusion(&camera);
  3269. groupp->clearOcclusionState(LLSpatialGroup::ACTIVE_OCCLUSION);
  3270. }
  3271. }
  3272. // Note: only actually done in processOccluders() for CAMERA_WORLD. HB
  3273. if (LLViewerCamera::sCurCameraID == LLViewerCamera::CAMERA_WORLD)
  3274. {
  3275. // Apply occlusion culling to object cache tree
  3276. for (LLWorld::region_list_t::const_iterator
  3277. iter = gWorld.getRegionList().begin(),
  3278. end = gWorld.getRegionList().end();
  3279. iter != end; ++iter)
  3280. {
  3281. LLVOCachePartition* partp = (*iter)->getVOCachePartition();
  3282. if (partp)
  3283. {
  3284. partp->processOccluders(&camera);
  3285. }
  3286. }
  3287. }
  3288. gGL.setColorMask(true, true);
  3289. }
  3290. void LLPipeline::doOcclusion(LLCamera& camera)
  3291. {
  3292. if (sUseOcclusion <= 1)
  3293. {
  3294. return;
  3295. }
  3296. if (mCubeVB.isNull())
  3297. {
  3298. sUseOcclusion = 0;
  3299. llwarns << "No available Cube VB, disabling occlusion" << llendl;
  3300. return;
  3301. }
  3302. if (gUsePBRShaders)
  3303. {
  3304. doOcclusionPBR(camera);
  3305. return;
  3306. }
  3307. // Note: we do not stop occluding during teleportation for PBR, thus why
  3308. // this boolean is only tested here. HB
  3309. if (LLSpatialPartition::sTeleportRequested)
  3310. {
  3311. return;
  3312. }
  3313. if (!sCull->hasOcclusionGroups() &&
  3314. !LLVOCachePartition::sNeedsOcclusionCheck)
  3315. {
  3316. return;
  3317. }
  3318. LLVertexBuffer::unbind();
  3319. if (hasRenderDebugMask(RENDER_DEBUG_OCCLUSION))
  3320. {
  3321. gGL.setColorMask(true, false, false, false);
  3322. }
  3323. else
  3324. {
  3325. gGL.setColorMask(false, false);
  3326. }
  3327. LLGLDisable blend(GL_BLEND);
  3328. gGL.getTexUnit(0)->unbind(LLTexUnit::TT_TEXTURE);
  3329. LLGLDepthTest depth(GL_TRUE, GL_FALSE);
  3330. LLGLDisable cull(GL_CULL_FACE);
  3331. bool bind_shader = !LLGLSLShader::sCurBoundShader;
  3332. if (bind_shader)
  3333. {
  3334. if (sShadowRender)
  3335. {
  3336. gDeferredShadowCubeProgram.bind();
  3337. }
  3338. else
  3339. {
  3340. gOcclusionCubeProgram.bind();
  3341. }
  3342. }
  3343. mCubeVB->setBuffer(LLVertexBuffer::MAP_VERTEX);
  3344. LLCullResult::sg_list_t& occ_groups = sCull->getOcclusionGroups();
  3345. for (U32 i = 0, count = occ_groups.size(); i < count; ++i)
  3346. {
  3347. LLSpatialGroup* groupp = occ_groups[i];
  3348. if (!groupp->isDead())
  3349. {
  3350. groupp->doOcclusion(&camera);
  3351. groupp->clearOcclusionState(LLSpatialGroup::ACTIVE_OCCLUSION);
  3352. }
  3353. }
  3354. // Note: only actually done in processOccluders() for CAMERA_WORLD. HB
  3355. if (LLViewerCamera::sCurCameraID == LLViewerCamera::CAMERA_WORLD)
  3356. {
  3357. // Apply occlusion culling to object cache tree
  3358. for (LLWorld::region_list_t::const_iterator
  3359. iter = gWorld.getRegionList().begin(),
  3360. end = gWorld.getRegionList().end();
  3361. iter != end; ++iter)
  3362. {
  3363. LLVOCachePartition* partp = (*iter)->getVOCachePartition();
  3364. if (partp)
  3365. {
  3366. partp->processOccluders(&camera);
  3367. }
  3368. }
  3369. }
  3370. if (bind_shader)
  3371. {
  3372. if (sShadowRender)
  3373. {
  3374. gDeferredShadowCubeProgram.unbind();
  3375. }
  3376. else
  3377. {
  3378. gOcclusionCubeProgram.unbind();
  3379. }
  3380. }
  3381. gGL.setColorMask(true, false);
  3382. }
  3383. bool LLPipeline::updateDrawableGeom(LLDrawable* drawablep)
  3384. {
  3385. bool update_complete = drawablep->updateGeometry();
  3386. if (update_complete)
  3387. {
  3388. drawablep->setState(LLDrawable::BUILT);
  3389. }
  3390. return update_complete;
  3391. }
  3392. void LLPipeline::updateGL()
  3393. {
  3394. LL_FAST_TIMER(FTM_UPDATE_GL);
  3395. while (!LLGLUpdate::sGLQ.empty())
  3396. {
  3397. LLGLUpdate* glup = LLGLUpdate::sGLQ.front();
  3398. glup->updateGL();
  3399. glup->mInQ = false;
  3400. LLGLUpdate::sGLQ.pop_front();
  3401. LL_DEBUGS("MarkGLRebuild") << "Rebuilt GL for: " << std::hex
  3402. << (intptr_t)glup << std::dec << LL_ENDL;
  3403. }
  3404. }
  3405. // Iterates through all groups on the build queue and remove all the groups
  3406. // that do not correspond to HUD objects.
  3407. void LLPipeline::clearRebuildGroups()
  3408. {
  3409. mGroupQLocked = true;
  3410. for (S32 i = 0, count = mGroupQ.size(); i < count; )
  3411. {
  3412. LLSpatialGroup* groupp = mGroupQ[i].get();
  3413. if (!groupp->isHUDGroup())
  3414. {
  3415. // Not a HUD object, so clear the build state.
  3416. // NOTE: since isHUDGroup() also tests for isDead() and returns
  3417. // false when the group is dead, dead groups will also be properly
  3418. // removed from mGroupQ. HB
  3419. groupp->clearState(LLSpatialGroup::IN_BUILD_QUEUE);
  3420. if (i < --count)
  3421. {
  3422. mGroupQ[i] = std::move(mGroupQ.back());
  3423. }
  3424. mGroupQ.pop_back();
  3425. }
  3426. else
  3427. {
  3428. ++i;
  3429. }
  3430. }
  3431. mGroupQLocked = false;
  3432. }
  3433. void LLPipeline::clearRebuildDrawables()
  3434. {
  3435. // Clear all drawables on the build queue,
  3436. for (LLDrawable::draw_list_t::iterator iter = mBuildQ.begin(),
  3437. end = mBuildQ.end();
  3438. iter != end; ++iter)
  3439. {
  3440. LLDrawable* drawablep = *iter;
  3441. if (drawablep && !drawablep->isDead())
  3442. {
  3443. drawablep->clearState(LLDrawable::IN_REBUILD_QUEUE);
  3444. }
  3445. }
  3446. mBuildQ.clear();
  3447. // Clear all moving bridges
  3448. U32 bits = LLDrawable::EARLY_MOVE | LLDrawable::MOVE_UNDAMPED |
  3449. LLDrawable::ON_MOVE_LIST | LLDrawable::ANIMATED_CHILD;
  3450. for (U32 i = 0, count = mMovedBridge.size(); i < count; ++i)
  3451. {
  3452. LLDrawable* drawablep = mMovedBridge[i];
  3453. if (drawablep) // Paranoia
  3454. {
  3455. drawablep->clearState(bits);
  3456. }
  3457. }
  3458. mMovedBridge.clear();
  3459. // Clear all moving drawables
  3460. for (U32 i = 0, count = mMovedList.size(); i < count; ++i)
  3461. {
  3462. LLDrawable* drawablep = mMovedList[i];
  3463. if (drawablep) // Paranoia
  3464. {
  3465. drawablep->clearState(bits);
  3466. }
  3467. }
  3468. mMovedList.clear();
  3469. // Clear all shifting drawables
  3470. bits |= LLDrawable::ON_SHIFT_LIST;
  3471. for (U32 i = 0, count = mShiftList.size(); i < count; ++i)
  3472. {
  3473. LLDrawable* drawablep = mShiftList[i];
  3474. if (drawablep)
  3475. {
  3476. drawablep->clearState(bits);
  3477. }
  3478. }
  3479. mShiftList.clear();
  3480. }
  3481. void LLPipeline::rebuildPriorityGroups()
  3482. {
  3483. LL_FAST_TIMER(FTM_REBUILD_PRIORITY_GROUPS);
  3484. {
  3485. LL_FAST_TIMER(FTM_REBUILD_MESH);
  3486. gMeshRepo.notifyLoadedMeshes();
  3487. }
  3488. mGroupQLocked = true;
  3489. // Iterate through all drawables on the build queue
  3490. for (LLSpatialGroup::sg_vector_t::iterator iter = mGroupQ.begin();
  3491. iter != mGroupQ.end(); ++iter)
  3492. {
  3493. LLSpatialGroup* groupp = iter->get();
  3494. groupp->rebuildGeom();
  3495. groupp->clearState(LLSpatialGroup::IN_BUILD_QUEUE);
  3496. }
  3497. mGroupSaveQ.clear();
  3498. mGroupSaveQ.swap(mGroupQ); // This clears mGroupQ
  3499. mGroupQLocked = false;
  3500. }
  3501. void LLPipeline::updateGeom(F32 max_dtime)
  3502. {
  3503. if (gCubeSnapshot)
  3504. {
  3505. return;
  3506. }
  3507. LL_FAST_TIMER(FTM_GEO_UPDATE);
  3508. // Notify various object types to reset internal cost metrics, etc.
  3509. // for now, only LLVOVolume does this to throttle LOD changes
  3510. LLVOVolume::preUpdateGeom();
  3511. LLPointer<LLDrawable> drawablep;
  3512. // Iterate through all drawables on the priority build queue,
  3513. for (LLDrawable::draw_list_t::iterator iter = mBuildQ.begin(),
  3514. end = mBuildQ.end();
  3515. iter != end; )
  3516. {
  3517. LLDrawable::draw_list_t::iterator curiter = iter++;
  3518. drawablep = *curiter;
  3519. bool remove = drawablep.isNull() || drawablep->isDead();
  3520. if (!remove)
  3521. {
  3522. remove = updateDrawableGeom(drawablep);
  3523. }
  3524. if (remove)
  3525. {
  3526. if (drawablep)
  3527. {
  3528. drawablep->clearState(LLDrawable::IN_REBUILD_QUEUE);
  3529. }
  3530. mBuildQ.erase(curiter);
  3531. }
  3532. }
  3533. updateMovedList(mMovedBridge);
  3534. }
  3535. void LLPipeline::markVisible(LLDrawable* drawablep, LLCamera& camera)
  3536. {
  3537. if (!drawablep || drawablep->isDead())
  3538. {
  3539. return;
  3540. }
  3541. if (drawablep->isSpatialBridge())
  3542. {
  3543. const LLDrawable* rootp = ((LLSpatialBridge*)drawablep)->mDrawable;
  3544. if (rootp && rootp->getVObj()->isAttachment())
  3545. {
  3546. LLDrawable* parentp = rootp->getParent();
  3547. if (parentp) // This IS sometimes NULL
  3548. {
  3549. LLViewerObject* objp = parentp->getVObj();
  3550. if (objp)
  3551. {
  3552. LLVOAvatar* avp = objp->asAvatar();
  3553. if (avp &&
  3554. (avp->isImpostor() || avp->isInMuteList() ||
  3555. (avp->getVisualMuteSettings() ==
  3556. LLVOAvatar::AV_DO_NOT_RENDER &&
  3557. !avp->needsImpostorUpdate())))
  3558. {
  3559. return;
  3560. }
  3561. }
  3562. }
  3563. }
  3564. sCull->pushBridge((LLSpatialBridge*)drawablep);
  3565. }
  3566. else
  3567. {
  3568. sCull->pushDrawable(drawablep);
  3569. }
  3570. drawablep->setVisible(camera);
  3571. }
  3572. void LLPipeline::markMoved(LLDrawable* drawablep, bool damped_motion)
  3573. {
  3574. if (!drawablep || drawablep->isDead())
  3575. {
  3576. return;
  3577. }
  3578. LLDrawable* parentp = drawablep->getParent();
  3579. if (parentp)
  3580. {
  3581. // Ensure that parent drawables are moved first
  3582. markMoved(parentp, damped_motion);
  3583. }
  3584. if (!drawablep->isState(LLDrawable::ON_MOVE_LIST))
  3585. {
  3586. if (drawablep->isSpatialBridge())
  3587. {
  3588. mMovedBridge.emplace_back(drawablep);
  3589. }
  3590. else
  3591. {
  3592. mMovedList.emplace_back(drawablep);
  3593. }
  3594. drawablep->setState(LLDrawable::ON_MOVE_LIST);
  3595. }
  3596. if (!damped_motion)
  3597. {
  3598. // UNDAMPED trumps DAMPED
  3599. drawablep->setState(LLDrawable::MOVE_UNDAMPED);
  3600. }
  3601. else if (drawablep->isState(LLDrawable::MOVE_UNDAMPED))
  3602. {
  3603. drawablep->clearState(LLDrawable::MOVE_UNDAMPED);
  3604. }
  3605. }
  3606. void LLPipeline::markShift(LLDrawable* drawablep)
  3607. {
  3608. if (!drawablep || drawablep->isDead())
  3609. {
  3610. return;
  3611. }
  3612. if (!drawablep->isState(LLDrawable::ON_SHIFT_LIST))
  3613. {
  3614. LLViewerObject* objp = drawablep->getVObj().get();
  3615. if (objp)
  3616. {
  3617. objp->setChanged(LLXform::SHIFTED | LLXform::SILHOUETTE);
  3618. }
  3619. LLDrawable* parentp = drawablep->getParent();
  3620. if (parentp)
  3621. {
  3622. markShift(parentp);
  3623. }
  3624. mShiftList.emplace_back(drawablep);
  3625. drawablep->setState(LLDrawable::ON_SHIFT_LIST);
  3626. }
  3627. }
  3628. void LLPipeline::shiftObjects(const LLVector3& offset)
  3629. {
  3630. glClear(GL_DEPTH_BUFFER_BIT);
  3631. gDepthDirty = true;
  3632. LLVector4a offseta;
  3633. offseta.load3(offset.mV);
  3634. {
  3635. LL_FAST_TIMER(FTM_SHIFT_DRAWABLE);
  3636. for (U32 i = 0, count = mShiftList.size(); i < count; ++i)
  3637. {
  3638. LLDrawable* drawablep = mShiftList[i].get();
  3639. if (drawablep && !drawablep->isDead())
  3640. {
  3641. drawablep->shiftPos(offseta);
  3642. drawablep->clearState(LLDrawable::ON_SHIFT_LIST);
  3643. }
  3644. }
  3645. mShiftList.clear();
  3646. }
  3647. {
  3648. LL_FAST_TIMER(FTM_SHIFT_OCTREE);
  3649. for (LLWorld::region_list_t::const_iterator
  3650. iter = gWorld.getRegionList().begin(),
  3651. end = gWorld.getRegionList().end();
  3652. iter != end; ++iter)
  3653. {
  3654. LLViewerRegion* regionp = *iter;
  3655. for (U32 i = 0; i < LLViewerRegion::NUM_PARTITIONS; ++i)
  3656. {
  3657. LLSpatialPartition* partp = regionp->getSpatialPartition(i);
  3658. if (partp)
  3659. {
  3660. partp->shift(offseta);
  3661. }
  3662. }
  3663. }
  3664. }
  3665. if (gUsePBRShaders)
  3666. {
  3667. mReflectionMapManager.shift(offseta);
  3668. }
  3669. {
  3670. LL_FAST_TIMER(FTM_SHIFT_HUD);
  3671. LLHUDText::shiftAll(offset);
  3672. }
  3673. display_update_camera();
  3674. }
  3675. void LLPipeline::markTextured(LLDrawable* drawablep)
  3676. {
  3677. if (drawablep && !drawablep->isDead())
  3678. {
  3679. mRetexturedList.emplace(drawablep);
  3680. }
  3681. }
  3682. void LLPipeline::markGLRebuild(LLGLUpdate* glup)
  3683. {
  3684. if (glup && !glup->mInQ)
  3685. {
  3686. LLGLUpdate::sGLQ.push_back(glup);
  3687. glup->mInQ = true;
  3688. }
  3689. }
  3690. void LLPipeline::markPartitionMove(LLDrawable* drawablep)
  3691. {
  3692. if (!drawablep->isState(LLDrawable::PARTITION_MOVE) &&
  3693. !drawablep->getPositionGroup().equals3(LLVector4a::getZero()))
  3694. {
  3695. drawablep->setState(LLDrawable::PARTITION_MOVE);
  3696. mPartitionQ.emplace_back(drawablep);
  3697. }
  3698. }
  3699. void LLPipeline::processPartitionQ()
  3700. {
  3701. LL_FAST_TIMER(FTM_PROCESS_PARTITIONQ);
  3702. for (U32 i = 0, count = mPartitionQ.size(); i < count; ++i)
  3703. {
  3704. LLDrawable* drawablep = mPartitionQ[i].get();
  3705. if (!drawablep) continue; // Paranoia
  3706. if (!drawablep->isDead())
  3707. {
  3708. drawablep->updateBinRadius();
  3709. drawablep->movePartition();
  3710. }
  3711. drawablep->clearState(LLDrawable::PARTITION_MOVE);
  3712. }
  3713. mPartitionQ.clear();
  3714. }
  3715. void LLPipeline::markMeshDirty(LLSpatialGroup* groupp)
  3716. {
  3717. mMeshDirtyGroup.emplace_back(groupp);
  3718. }
  3719. void LLPipeline::markRebuild(LLSpatialGroup* groupp)
  3720. {
  3721. if (groupp && !groupp->isDead() && groupp->getSpatialPartition() &&
  3722. !groupp->hasState(LLSpatialGroup::IN_BUILD_QUEUE))
  3723. {
  3724. mGroupQ.emplace_back(groupp);
  3725. groupp->setState(LLSpatialGroup::IN_BUILD_QUEUE);
  3726. }
  3727. }
  3728. void LLPipeline::markRebuild(LLDrawable* drawablep,
  3729. LLDrawable::EDrawableFlags flag)
  3730. {
  3731. if (drawablep && !drawablep->isDead())
  3732. {
  3733. if (!drawablep->isState(LLDrawable::IN_REBUILD_QUEUE))
  3734. {
  3735. mBuildQ.emplace_back(drawablep);
  3736. // Mark drawable as being in build queue
  3737. drawablep->setState(LLDrawable::IN_REBUILD_QUEUE);
  3738. }
  3739. if (flag & (LLDrawable::REBUILD_VOLUME | LLDrawable::REBUILD_POSITION))
  3740. {
  3741. LLViewerObject* objp = drawablep->getVObj().get();
  3742. if (objp)
  3743. {
  3744. objp->setChanged(LLXform::SILHOUETTE);
  3745. }
  3746. }
  3747. drawablep->setState(flag);
  3748. }
  3749. }
  3750. void LLPipeline::stateSort(LLCamera& camera, LLCullResult& result)
  3751. {
  3752. if (hasAnyRenderType(RENDER_TYPE_AVATAR, RENDER_TYPE_PUPPET,
  3753. RENDER_TYPE_TERRAIN, RENDER_TYPE_TREE,
  3754. RENDER_TYPE_SKY, RENDER_TYPE_VOIDWATER,
  3755. RENDER_TYPE_WATER, END_RENDER_TYPES))
  3756. {
  3757. // Clear faces from face pools
  3758. LL_FAST_TIMER(FTM_RESET_DRAWORDER);
  3759. resetDrawOrders();
  3760. }
  3761. LL_FAST_TIMER(FTM_STATESORT);
  3762. grabReferences(result);
  3763. LLCullResult::sg_list_t& draw_groups = sCull->getDrawableGroups();
  3764. for (U32 i = 0, count = draw_groups.size(); i < count; ++i)
  3765. {
  3766. LLSpatialGroup* groupp = draw_groups[i];
  3767. if (!groupp || groupp->isDead()) continue;
  3768. groupp->checkOcclusion();
  3769. if (sUseOcclusion > 1 &&
  3770. groupp->isOcclusionState(LLSpatialGroup::OCCLUDED))
  3771. {
  3772. markOccluder(groupp);
  3773. continue;
  3774. }
  3775. groupp->setVisible();
  3776. const LLSpatialGroup::element_list& data = groupp->getData();
  3777. for (U32 j = 0, count2 = data.size(); j < count2; ++j)
  3778. {
  3779. LLDrawable* drawablep = (LLDrawable*)data[j]->getDrawable();
  3780. markVisible(drawablep, camera);
  3781. }
  3782. // Rebuild mesh as soon as we know it is visible
  3783. groupp->rebuildMesh();
  3784. }
  3785. if (LLViewerCamera::sCurCameraID == LLViewerCamera::CAMERA_WORLD &&
  3786. !gCubeSnapshot)
  3787. {
  3788. bool fov_changed = gViewerCamera.isDefaultFOVChanged();
  3789. LLSpatialGroup* last_groupp = NULL;
  3790. LLCullResult::bridge_list_t& bridges = sCull->getVisibleBridge();
  3791. for (U32 i = 0, count = bridges.size(); i < count; ++i)
  3792. {
  3793. LLSpatialBridge* bridgep = bridges[i];
  3794. if (!bridgep) continue;
  3795. LLSpatialGroup* groupp = bridgep->getSpatialGroup();
  3796. if (groupp->isDead()) continue;
  3797. if (!last_groupp)
  3798. {
  3799. last_groupp = groupp;
  3800. }
  3801. if (groupp && !bridgep->isDead() &&
  3802. !groupp->isOcclusionState(LLSpatialGroup::OCCLUDED))
  3803. {
  3804. stateSort(bridgep, camera, fov_changed);
  3805. }
  3806. if (last_groupp && last_groupp != groupp &&
  3807. last_groupp->changeLOD())
  3808. {
  3809. last_groupp->mLastUpdateDistance = last_groupp->mDistance;
  3810. }
  3811. last_groupp = groupp;
  3812. }
  3813. if (last_groupp && last_groupp->changeLOD())
  3814. {
  3815. last_groupp->mLastUpdateDistance = last_groupp->mDistance;
  3816. }
  3817. }
  3818. LLCullResult::sg_list_t& visible_groups = sCull->getVisibleGroups();
  3819. for (U32 i = 0, count = visible_groups.size(); i < count; ++i)
  3820. {
  3821. LLSpatialGroup* groupp = visible_groups[i];
  3822. if (!groupp || groupp->isDead()) continue;
  3823. groupp->checkOcclusion();
  3824. if (sUseOcclusion > 1 &&
  3825. groupp->isOcclusionState(LLSpatialGroup::OCCLUDED))
  3826. {
  3827. markOccluder(groupp);
  3828. }
  3829. else
  3830. {
  3831. groupp->setVisible();
  3832. stateSort(groupp, camera);
  3833. // Rebuild mesh as soon as we know it is visible
  3834. groupp->rebuildMesh();
  3835. }
  3836. }
  3837. {
  3838. LL_FAST_TIMER(FTM_STATESORT_DRAWABLE);
  3839. LLCullResult::drawable_list_t& visible = sCull->getVisibleList();
  3840. for (U32 i = 0, count = visible.size(); i < count; ++i)
  3841. {
  3842. LLDrawable* drawablep = visible[i];
  3843. if (drawablep && !drawablep->isDead())
  3844. {
  3845. stateSort(drawablep, camera);
  3846. }
  3847. }
  3848. }
  3849. postSort(camera);
  3850. }
  3851. void LLPipeline::stateSort(LLSpatialGroup* groupp, LLCamera& camera)
  3852. {
  3853. if (groupp->changeLOD())
  3854. {
  3855. const LLSpatialGroup::element_list& data = groupp->getData();
  3856. for (U32 i = 0, count = data.size(); i < count; ++i)
  3857. {
  3858. LLDrawable* drawablep = (LLDrawable*)data[i]->getDrawable();
  3859. stateSort(drawablep, camera);
  3860. }
  3861. if (LLViewerCamera::sCurCameraID == LLViewerCamera::CAMERA_WORLD &&
  3862. !gCubeSnapshot)
  3863. {
  3864. // Avoid redundant stateSort calls
  3865. groupp->mLastUpdateDistance = groupp->mDistance;
  3866. }
  3867. }
  3868. }
  3869. void LLPipeline::stateSort(LLSpatialBridge* bridgep, LLCamera& camera,
  3870. bool fov_changed)
  3871. {
  3872. if (fov_changed || bridgep->getSpatialGroup()->changeLOD())
  3873. {
  3874. // false = do not force update
  3875. bridgep->updateDistance(camera, false);
  3876. }
  3877. }
  3878. void LLPipeline::stateSort(LLDrawable* drawablep, LLCamera& camera)
  3879. {
  3880. if (!drawablep || drawablep->isDead() ||
  3881. !hasRenderType(drawablep->getRenderType()))
  3882. {
  3883. return;
  3884. }
  3885. // SL-11353: ignore our own geometry when rendering spotlight shadowmaps...
  3886. if (drawablep == sRenderSpotLight)
  3887. {
  3888. return;
  3889. }
  3890. LLViewerObject* objp = drawablep->getVObj().get();
  3891. if (gSelectMgr.mHideSelectedObjects && objp && objp->isSelected() &&
  3892. //MK
  3893. (!gRLenabled || !gRLInterface.mContainsEdit))
  3894. //mk
  3895. {
  3896. return;
  3897. }
  3898. if (drawablep->isAvatar())
  3899. {
  3900. // Do not draw avatars beyond render distance or if we do not have a
  3901. // spatial group.
  3902. LLSpatialGroup* groupp = drawablep->getSpatialGroup();
  3903. if (!groupp || groupp->mDistance > LLVOAvatar::sRenderDistance)
  3904. {
  3905. return;
  3906. }
  3907. LLVOAvatar* avatarp = (LLVOAvatar*)objp;
  3908. if (avatarp && !avatarp->isVisible())
  3909. {
  3910. return;
  3911. }
  3912. }
  3913. if (hasRenderType(drawablep->mRenderType) &&
  3914. !drawablep->isState(LLDrawable::INVISIBLE |
  3915. LLDrawable::FORCE_INVISIBLE))
  3916. {
  3917. drawablep->setVisible(camera, NULL, false);
  3918. }
  3919. if (LLViewerCamera::sCurCameraID == LLViewerCamera::CAMERA_WORLD &&
  3920. !gCubeSnapshot)
  3921. {
  3922. #if 0 // isVisible() check here is redundant, if it was not visible, it would
  3923. // not be here
  3924. if (drawablep->isVisible())
  3925. #endif
  3926. {
  3927. if (!drawablep->isActive())
  3928. {
  3929. // false = do not force update = false;
  3930. drawablep->updateDistance(camera, false);
  3931. }
  3932. else if (drawablep->isAvatar())
  3933. {
  3934. // Calls objp->updateLOD() which calls
  3935. // LLVOAvatar::updateVisibility()
  3936. drawablep->updateDistance(camera, false);
  3937. }
  3938. }
  3939. }
  3940. if (!drawablep->getVOVolume())
  3941. {
  3942. for (U32 i = 0, count = drawablep->mFaces.size(); i < count; ++i)
  3943. {
  3944. LLFace* facep = drawablep->mFaces[i];
  3945. if (facep->hasGeometry())
  3946. {
  3947. LLFacePool* poolp = facep->getPool();
  3948. if (!poolp)
  3949. {
  3950. break;
  3951. }
  3952. poolp->enqueue(facep);
  3953. }
  3954. }
  3955. }
  3956. }
  3957. void forAllDrawables(LLCullResult::sg_list_t& group_data,
  3958. void (*func)(LLDrawable*))
  3959. {
  3960. for (U32 i = 0, count = group_data.size(); i < count; ++i)
  3961. {
  3962. LLSpatialGroup* groupp = group_data[i];
  3963. if (groupp->isDead()) continue;
  3964. const LLSpatialGroup::element_list& octree_data = groupp->getData();
  3965. for (U32 j = 0, count2 = octree_data.size(); j < count2; ++j)
  3966. {
  3967. if (octree_data[j]->hasDrawable())
  3968. {
  3969. func((LLDrawable*)octree_data[j]->getDrawable());
  3970. }
  3971. }
  3972. }
  3973. }
  3974. void LLPipeline::forAllVisibleDrawables(void (*func)(LLDrawable*))
  3975. {
  3976. forAllDrawables(sCull->getDrawableGroups(), func);
  3977. forAllDrawables(sCull->getVisibleGroups(), func);
  3978. }
  3979. //static
  3980. U32 LLPipeline::highlightable(const LLViewerObject* objp)
  3981. {
  3982. if (!objp || objp->isAvatar()) return 0;
  3983. if (sRenderByOwner == 1 && !objp->permYouOwner()) return 0;
  3984. if (sRenderByOwner == 2 && objp->permYouOwner()) return 0;
  3985. LLViewerObject* parentp = (LLViewerObject*)objp->getParent();
  3986. if (!parentp) return 1;
  3987. if (!sRenderAttachments) return 0;
  3988. // Attachments can be highlighted but are not marked with beacons since
  3989. // it would mark the avatar itself... But, we highlight all the
  3990. // primitives of the attachments instead of just the root primitive
  3991. // (which could be buried into the avatar or be too small to be
  3992. // visible). HB
  3993. if (parentp->isAvatar())
  3994. {
  3995. return 2;
  3996. }
  3997. LLViewerObject* rootp = (LLViewerObject*)objp->getRoot();
  3998. if (rootp && rootp->isAvatar())
  3999. {
  4000. return 2;
  4001. }
  4002. return 0;
  4003. }
  4004. // Function for creating scripted beacons
  4005. void renderScriptedBeacons(LLDrawable* drawablep)
  4006. {
  4007. LLViewerObject* objp = drawablep->getVObj().get();
  4008. U32 type = LLPipeline::highlightable(objp);
  4009. if (type != 0 && objp->flagScripted())
  4010. {
  4011. if (LLPipeline::sRenderBeacons && type != 2)
  4012. {
  4013. gObjectList.addDebugBeacon(objp->getPositionAgent(), "",
  4014. LLColor4(1.f, 0.f, 0.f, 0.5f),
  4015. LLColor4(1.f, 1.f, 1.f, 0.5f),
  4016. LLPipeline::DebugBeaconLineWidth);
  4017. }
  4018. if (LLPipeline::sRenderHighlight)
  4019. {
  4020. for (S32 face_id = 0, count = drawablep->getNumFaces();
  4021. face_id < count; ++face_id)
  4022. {
  4023. LLFace* facep = drawablep->getFace(face_id);
  4024. if (facep)
  4025. {
  4026. gPipeline.mHighlightFaces.push_back(facep);
  4027. }
  4028. }
  4029. }
  4030. }
  4031. }
  4032. void renderScriptedTouchBeacons(LLDrawable* drawablep)
  4033. {
  4034. LLViewerObject* objp = drawablep->getVObj().get();
  4035. U32 type = LLPipeline::highlightable(objp);
  4036. if (type != 0 && objp->flagScripted() && objp->flagHandleTouch())
  4037. {
  4038. if (LLPipeline::sRenderBeacons && type != 2)
  4039. {
  4040. gObjectList.addDebugBeacon(objp->getPositionAgent(), "",
  4041. LLColor4(1.f, 0.f, 0.f, 0.5f),
  4042. LLColor4(1.f, 1.f, 1.f, 0.5f),
  4043. LLPipeline::DebugBeaconLineWidth);
  4044. }
  4045. if (LLPipeline::sRenderHighlight)
  4046. {
  4047. for (S32 face_id = 0, count = drawablep->getNumFaces();
  4048. face_id < count; ++face_id)
  4049. {
  4050. LLFace* facep = drawablep->getFace(face_id);
  4051. if (facep)
  4052. {
  4053. gPipeline.mHighlightFaces.push_back(facep);
  4054. }
  4055. }
  4056. }
  4057. }
  4058. }
  4059. void renderPhysicalBeacons(LLDrawable* drawablep)
  4060. {
  4061. LLViewerObject* objp = drawablep->getVObj().get();
  4062. U32 type = LLPipeline::highlightable(objp);
  4063. if (type != 0 && objp->flagUsePhysics())
  4064. {
  4065. if (LLPipeline::sRenderBeacons && type != 2)
  4066. {
  4067. gObjectList.addDebugBeacon(objp->getPositionAgent(), "",
  4068. LLColor4(0.f, 1.f, 0.f, 0.5f),
  4069. LLColor4(1.f, 1.f, 1.f, 0.5f),
  4070. LLPipeline::DebugBeaconLineWidth);
  4071. }
  4072. if (LLPipeline::sRenderHighlight)
  4073. {
  4074. for (S32 face_id = 0, count = drawablep->getNumFaces();
  4075. face_id < count; ++face_id)
  4076. {
  4077. LLFace* facep = drawablep->getFace(face_id);
  4078. if (facep)
  4079. {
  4080. gPipeline.mHighlightFaces.push_back(facep);
  4081. }
  4082. }
  4083. }
  4084. }
  4085. }
  4086. void renderPermanentBeacons(LLDrawable* drawablep)
  4087. {
  4088. LLViewerObject* objp = drawablep->getVObj().get();
  4089. U32 type = LLPipeline::highlightable(objp);
  4090. if (type != 0 && objp->flagObjectPermanent())
  4091. {
  4092. if (LLPipeline::sRenderBeacons && type != 2)
  4093. {
  4094. gObjectList.addDebugBeacon(objp->getPositionAgent(), "",
  4095. LLColor4(0.f, 1.f, 1.f, 0.5f),
  4096. LLColor4(1.f, 1.f, 1.f, 0.5f),
  4097. LLPipeline::DebugBeaconLineWidth);
  4098. }
  4099. if (LLPipeline::sRenderHighlight)
  4100. {
  4101. for (S32 face_id = 0, count = drawablep->getNumFaces();
  4102. face_id < count; ++face_id)
  4103. {
  4104. LLFace* facep = drawablep->getFace(face_id);
  4105. if (facep)
  4106. {
  4107. gPipeline.mHighlightFaces.push_back(facep);
  4108. }
  4109. }
  4110. }
  4111. }
  4112. }
  4113. void renderCharacterBeacons(LLDrawable* drawablep)
  4114. {
  4115. LLViewerObject* objp = drawablep->getVObj().get();
  4116. U32 type = LLPipeline::highlightable(objp);
  4117. if (type != 0 && objp->flagCharacter())
  4118. {
  4119. if (LLPipeline::sRenderBeacons && type != 2)
  4120. {
  4121. gObjectList.addDebugBeacon(objp->getPositionAgent(), "",
  4122. LLColor4(0.5f, 0.5f, 0.5f, 0.5f),
  4123. LLColor4(1.f, 1.f, 1.f, 0.5f),
  4124. LLPipeline::DebugBeaconLineWidth);
  4125. }
  4126. if (LLPipeline::sRenderHighlight)
  4127. {
  4128. for (S32 face_id = 0, count = drawablep->getNumFaces();
  4129. face_id < count; ++face_id)
  4130. {
  4131. LLFace* facep = drawablep->getFace(face_id);
  4132. if (facep)
  4133. {
  4134. gPipeline.mHighlightFaces.push_back(facep);
  4135. }
  4136. }
  4137. }
  4138. }
  4139. }
  4140. void renderSoundBeacons(LLDrawable* drawablep)
  4141. {
  4142. // Look for attachments, objects, etc.
  4143. LLViewerObject* objp = drawablep->getVObj().get();
  4144. U32 type = LLPipeline::highlightable(objp);
  4145. if (type != 0 && objp->isAudioSource())
  4146. {
  4147. if ((LLPipeline::sRenderBeacons ||
  4148. !LLPipeline::sRenderInvisibleSoundBeacons) && type != 2)
  4149. {
  4150. gObjectList.addDebugBeacon(objp->getPositionAgent(), "",
  4151. LLColor4(1.f, 1.f, 0.f, 0.5f),
  4152. LLColor4(1.f, 1.f, 1.f, 0.5f),
  4153. LLPipeline::DebugBeaconLineWidth);
  4154. }
  4155. if (LLPipeline::sRenderHighlight)
  4156. {
  4157. for (S32 face_id = 0, count = drawablep->getNumFaces();
  4158. face_id < count; ++face_id)
  4159. {
  4160. LLFace* facep = drawablep->getFace(face_id);
  4161. if (facep)
  4162. {
  4163. gPipeline.mHighlightFaces.push_back(facep);
  4164. }
  4165. }
  4166. }
  4167. }
  4168. }
  4169. void renderParticleBeacons(LLDrawable* drawablep)
  4170. {
  4171. // Look for attachments, objects, etc.
  4172. LLViewerObject* objp = drawablep->getVObj().get();
  4173. U32 type = LLPipeline::highlightable(objp);
  4174. if (type != 0 && objp->isParticleSource())
  4175. {
  4176. if (LLPipeline::sRenderBeacons && type != 2)
  4177. {
  4178. gObjectList.addDebugBeacon(objp->getPositionAgent(), "",
  4179. LLColor4(0.5f, 0.5f, 1.f, 0.5f),
  4180. LLColor4(1.f, 1.f, 1.f, 0.5f),
  4181. LLPipeline::DebugBeaconLineWidth);
  4182. }
  4183. if (LLPipeline::sRenderHighlight)
  4184. {
  4185. for (S32 face_id = 0, count = drawablep->getNumFaces();
  4186. face_id < count; ++face_id)
  4187. {
  4188. LLFace* facep = drawablep->getFace(face_id);
  4189. if (facep)
  4190. {
  4191. gPipeline.mHighlightFaces.push_back(facep);
  4192. }
  4193. }
  4194. }
  4195. }
  4196. }
  4197. void renderMOAPBeacons(LLDrawable* drawablep)
  4198. {
  4199. LLViewerObject* objp = drawablep->getVObj().get();
  4200. if (!objp || objp->isAvatar()) return;
  4201. U32 type = LLPipeline::highlightable(objp);
  4202. if (type == 0) return;
  4203. bool beacon = false;
  4204. U8 tecount = objp->getNumTEs();
  4205. for (S32 x = 0; x < tecount; ++x)
  4206. {
  4207. LLTextureEntry* tep = objp->getTE(x);
  4208. if (tep && tep->hasMedia())
  4209. {
  4210. beacon = true;
  4211. break;
  4212. }
  4213. }
  4214. if (beacon)
  4215. {
  4216. if (LLPipeline::sRenderBeacons && type != 2)
  4217. {
  4218. gObjectList.addDebugBeacon(objp->getPositionAgent(), "",
  4219. LLColor4(1.f, 1.f, 1.f, 0.5f),
  4220. LLColor4(1.f, 1.f, 1.f, 0.5f),
  4221. LLPipeline::DebugBeaconLineWidth);
  4222. }
  4223. if (LLPipeline::sRenderHighlight)
  4224. {
  4225. for (S32 face_id = 0, count = drawablep->getNumFaces();
  4226. face_id < count; ++face_id)
  4227. {
  4228. LLFace* facep = drawablep->getFace(face_id);
  4229. if (facep)
  4230. {
  4231. gPipeline.mHighlightFaces.push_back(facep);
  4232. }
  4233. }
  4234. }
  4235. }
  4236. }
  4237. void LLPipeline::touchTexture(LLViewerTexture* texp, F32 vsize)
  4238. {
  4239. if (texp)
  4240. {
  4241. LLImageGL* imagep = texp->getGLImage();
  4242. if (imagep && imagep->updateBindStats())
  4243. {
  4244. texp->addTextureStats(vsize);
  4245. }
  4246. }
  4247. }
  4248. void LLPipeline::touchTextures(LLDrawInfo* infop)
  4249. {
  4250. // Update out materials usage statistics. HB
  4251. static LLCachedControl<bool> attachments(gSavedSettings,
  4252. "AccountAttachmentsMaterials");
  4253. static LLCachedControl<U32> min_vsize(gSavedSettings,
  4254. "AccountMaterialsMinVSize");
  4255. if (infop->mVSize >= F32(min_vsize) &&
  4256. (!infop->mIsAgentAttachment || attachments))
  4257. {
  4258. if (infop->mHasALM)
  4259. {
  4260. sUsingALM = true;
  4261. }
  4262. if (infop->mHasPBR)
  4263. {
  4264. sUsingPBR = true;
  4265. if (!infop->mHasLegacyFallback)
  4266. {
  4267. sLacksLegacyFallback = true;
  4268. if (!infop->mHasBasecolorFallback)
  4269. {
  4270. sLacksBaseColorFallback = true;
  4271. }
  4272. }
  4273. }
  4274. }
  4275. for (U32 i = 0, count = infop->mTextureList.size(); i < count; ++i)
  4276. {
  4277. touchTexture(infop->mTextureList[i], infop->mTextureListVSize[i]);
  4278. }
  4279. F32 vsize = infop->mVSize;
  4280. if (gUsePBRShaders)
  4281. {
  4282. LLFetchedGLTFMaterial* gltfp = infop->mGLTFMaterial.get();
  4283. if (gltfp)
  4284. {
  4285. touchTexture(gltfp->mBaseColorTexture, vsize);
  4286. touchTexture(gltfp->mNormalTexture, vsize);
  4287. touchTexture(gltfp->mMetallicRoughnessTexture, vsize);
  4288. touchTexture(gltfp->mEmissiveTexture, vsize);
  4289. return;
  4290. }
  4291. }
  4292. touchTexture(infop->mTexture, vsize);
  4293. if (sRenderDeferred)
  4294. {
  4295. touchTexture(infop->mSpecularMap, vsize);
  4296. touchTexture(infop->mNormalMap, vsize);
  4297. }
  4298. }
  4299. void LLPipeline::postSort(LLCamera& camera)
  4300. {
  4301. LL_FAST_TIMER(FTM_STATESORT_POSTSORT);
  4302. if (!gCubeSnapshot)
  4303. {
  4304. // Rebuild drawable geometry
  4305. LLCullResult::sg_list_t& draw_groups = sCull->getDrawableGroups();
  4306. for (U32 i = 0, count = draw_groups.size(); i < count; ++i)
  4307. {
  4308. LLSpatialGroup* groupp = draw_groups[i];
  4309. if (groupp->isDead())
  4310. {
  4311. continue;
  4312. }
  4313. if (!sUseOcclusion ||
  4314. !groupp->isOcclusionState(LLSpatialGroup::OCCLUDED))
  4315. {
  4316. groupp->rebuildGeom();
  4317. }
  4318. }
  4319. // Rebuild groups
  4320. sCull->assertDrawMapsEmpty();
  4321. rebuildPriorityGroups();
  4322. }
  4323. // Build render map
  4324. bool has_type_pass_alpha = hasRenderType(RENDER_TYPE_PASS_ALPHA);
  4325. bool has_alpha_type = hasRenderType(LLDrawPool::POOL_ALPHA);
  4326. bool is_world_camera =
  4327. LLViewerCamera::sCurCameraID == LLViewerCamera::CAMERA_WORLD;
  4328. U32 bytes_limit = RenderAutoHideGeometryMemoryLimit * 1048576;
  4329. bool limit_surf_area = RenderAutoHideSurfaceAreaLimit > 0.f;
  4330. LLVector4a bounds;
  4331. LLCullResult::sg_list_t& visible_groups = sCull->getVisibleGroups();
  4332. for (U32 i = 0, vcount = visible_groups.size(); i < vcount; ++i)
  4333. {
  4334. LLSpatialGroup* groupp = visible_groups[i];
  4335. if (groupp->isDead())
  4336. {
  4337. continue;
  4338. }
  4339. if (sUseOcclusion &&
  4340. groupp->isOcclusionState(LLSpatialGroup::OCCLUDED))
  4341. {
  4342. continue;
  4343. }
  4344. if (bytes_limit && groupp->mGeometryBytes > bytes_limit)
  4345. {
  4346. continue;
  4347. }
  4348. if (limit_surf_area &&
  4349. groupp->mSurfaceArea >
  4350. RenderAutoHideSurfaceAreaLimit * groupp->mObjectBoxSize)
  4351. {
  4352. continue;
  4353. }
  4354. bool needs_rebuild = !gCubeSnapshot &&
  4355. groupp->hasState(LLSpatialGroup::NEW_DRAWINFO) &&
  4356. groupp->hasState(LLSpatialGroup::GEOM_DIRTY);
  4357. if (needs_rebuild)
  4358. {
  4359. // No way this group is going to be drawable without a rebuild
  4360. groupp->rebuildGeom();
  4361. }
  4362. bool needs_touch;
  4363. if (gUsePBRShaders)
  4364. {
  4365. needs_touch = !gCubeSnapshot && !sShadowRender;
  4366. }
  4367. else
  4368. {
  4369. // Note: sReflectionRender is true while rendering impostors in EE
  4370. // non-deferred mode, but then we still want impostors textures to
  4371. // be touched so that they would not vanish. HB
  4372. needs_touch = !sShadowRender &&
  4373. (!sReflectionRender || sImpostorRender);
  4374. }
  4375. for (LLSpatialGroup::draw_map_t::iterator j = groupp->mDrawMap.begin(),
  4376. end = groupp->mDrawMap.end();
  4377. j != end; ++j)
  4378. {
  4379. LLSpatialGroup::drawmap_elem_t& src_vec = j->second;
  4380. if (!hasRenderType(j->first))
  4381. {
  4382. continue;
  4383. }
  4384. for (U32 k = 0, dcount = src_vec.size(); k < dcount; ++k)
  4385. {
  4386. LLDrawInfo* infop = src_vec[k].get();
  4387. sCull->pushDrawInfo(j->first, infop);
  4388. if (needs_touch)
  4389. {
  4390. if (!needs_rebuild)
  4391. {
  4392. touchTextures(infop);
  4393. }
  4394. addTrianglesDrawn(infop->mCount);
  4395. }
  4396. }
  4397. }
  4398. if (has_type_pass_alpha)
  4399. {
  4400. if (groupp->mDrawMap.count(LLRenderPass::PASS_ALPHA))
  4401. {
  4402. // Store alpha groups for sorting
  4403. if (is_world_camera && !gCubeSnapshot)
  4404. {
  4405. LLSpatialBridge* bridgep =
  4406. groupp->getSpatialPartition()->asBridge();
  4407. if (bridgep)
  4408. {
  4409. LLCamera trans_camera =
  4410. bridgep->transformCamera(camera);
  4411. groupp->updateDistance(trans_camera);
  4412. }
  4413. else
  4414. {
  4415. groupp->updateDistance(camera);
  4416. }
  4417. }
  4418. if (has_alpha_type)
  4419. {
  4420. sCull->pushAlphaGroup(groupp);
  4421. }
  4422. }
  4423. if (has_alpha_type &&
  4424. groupp->mDrawMap.count(LLRenderPass::PASS_ALPHA_RIGGED))
  4425. {
  4426. // Store rigged alpha groups for LLDrawPoolAlpha prepass
  4427. // (skip distance update, rigged attachments use depth buffer)
  4428. sCull->pushRiggedAlphaGroup(groupp);
  4429. }
  4430. }
  4431. }
  4432. // Pack vertex buffers for groups that chose to delay their updates
  4433. for (U32 i = 0, count = mMeshDirtyGroup.size(); i < count; ++i)
  4434. {
  4435. LLSpatialGroup* groupp = mMeshDirtyGroup[i].get();
  4436. if (groupp)
  4437. {
  4438. groupp->rebuildMesh();
  4439. }
  4440. }
  4441. mMeshDirtyGroup.clear();
  4442. if (!sShadowRender)
  4443. {
  4444. // Order alpha groups by distance
  4445. std::sort(sCull->beginAlphaGroups(), sCull->endAlphaGroups(),
  4446. LLSpatialGroup::CompareDepthGreater());
  4447. // Order rigged alpha groups by avatar attachment order
  4448. std::sort(sCull->beginRiggedAlphaGroups(),
  4449. sCull->endRiggedAlphaGroups(),
  4450. LLSpatialGroup::CompareRenderOrder());
  4451. }
  4452. if (gCubeSnapshot)
  4453. {
  4454. // Do not render beacons or highlights during cube snapshot.
  4455. return;
  4456. }
  4457. // This is the position for the sounds list floater beacon:
  4458. LLVector3d selected_pos = HBFloaterSoundsList::selectedLocation();
  4459. // Only render if the flag is set. The flag is only set if we are in edit
  4460. // mode or the toggle is set in the menus.
  4461. static LLCachedControl<bool> beacons_always_on(gSavedSettings,
  4462. "BeaconAlwaysOn");
  4463. if ((sRenderBeaconsFloaterOpen || beacons_always_on) &&
  4464. //MK
  4465. !(gRLenabled &&
  4466. (gRLInterface.mContainsEdit || gRLInterface.mVisionRestricted)) &&
  4467. //mk
  4468. !sShadowRender)
  4469. {
  4470. if (sRenderScriptedTouchBeacons)
  4471. {
  4472. // Only show the beacon on the root object.
  4473. forAllVisibleDrawables(renderScriptedTouchBeacons);
  4474. }
  4475. else if (sRenderScriptedBeacons)
  4476. {
  4477. // Only show the beacon on the root object.
  4478. forAllVisibleDrawables(renderScriptedBeacons);
  4479. }
  4480. if (sRenderPhysicalBeacons)
  4481. {
  4482. // Only show the beacon on the root object.
  4483. forAllVisibleDrawables(renderPhysicalBeacons);
  4484. }
  4485. if (sRenderPermanentBeacons)
  4486. {
  4487. // Only show the beacon on the root object.
  4488. forAllVisibleDrawables(renderPermanentBeacons);
  4489. }
  4490. if (sRenderCharacterBeacons)
  4491. {
  4492. // Only show the beacon on the root object.
  4493. forAllVisibleDrawables(renderCharacterBeacons);
  4494. }
  4495. if (sRenderSoundBeacons && gAudiop)
  4496. {
  4497. if (sRenderInvisibleSoundBeacons && sRenderBeacons)
  4498. {
  4499. static const LLColor4 semi_yellow(1.f, 1.f, 0.f, 0.5f);
  4500. static const LLColor4 semi_white(1.f, 1.f, 0.f, 0.5f);
  4501. // Walk all sound sources and render out beacons for them.
  4502. // Note, this is not done in the ForAllVisibleDrawables
  4503. // function, because some are not visible.
  4504. for (LLAudioEngine::source_map_t::const_iterator
  4505. iter = gAudiop->mAllSources.begin(),
  4506. end = gAudiop->mAllSources.end();
  4507. iter != end; ++iter)
  4508. {
  4509. const LLAudioSource* sourcep = iter->second;
  4510. if (!sourcep) continue; // Paranoia
  4511. // Verify source owner and match against renderbyowner
  4512. const LLUUID& owner_id = sourcep->getOwnerID();
  4513. if ((sRenderByOwner == 1 && owner_id != gAgentID) ||
  4514. (sRenderByOwner == 2 && owner_id == gAgentID))
  4515. {
  4516. continue;
  4517. }
  4518. LLVector3d pos_global = sourcep->getPositionGlobal();
  4519. if (selected_pos.isExactlyZero() ||
  4520. pos_global != selected_pos)
  4521. {
  4522. LLVector3 pos =
  4523. gAgent.getPosAgentFromGlobal(pos_global);
  4524. gObjectList.addDebugBeacon(pos, "", semi_yellow,
  4525. semi_white,
  4526. DebugBeaconLineWidth);
  4527. }
  4528. }
  4529. }
  4530. // Now deal with highlights for all those seeable sound sources
  4531. forAllVisibleDrawables(renderSoundBeacons);
  4532. }
  4533. if (sRenderParticleBeacons)
  4534. {
  4535. forAllVisibleDrawables(renderParticleBeacons);
  4536. }
  4537. if (sRenderMOAPBeacons)
  4538. {
  4539. forAllVisibleDrawables(renderMOAPBeacons);
  4540. }
  4541. }
  4542. // Render the sound beacon for the sounds list floater, if needed.
  4543. if (!selected_pos.isExactlyZero())
  4544. {
  4545. gObjectList.addDebugBeacon(gAgent.getPosAgentFromGlobal(selected_pos),
  4546. "",
  4547. // Oranger yellow than sound normal beacons
  4548. LLColor4(1.f, 0.8f, 0.f, 0.5f),
  4549. LLColor4(1.f, 1.f, 1.f, 0.5f),
  4550. DebugBeaconLineWidth);
  4551. }
  4552. // If managing your telehub, draw beacons at telehub and currently selected
  4553. // spawnpoint.
  4554. if (LLFloaterTelehub::renderBeacons())
  4555. {
  4556. LLFloaterTelehub::addBeacons();
  4557. }
  4558. if (!sShadowRender)
  4559. {
  4560. mSelectedFaces.clear();
  4561. sRenderHighlightTextureChannel =
  4562. LLPanelFace::getTextureChannelToEdit();
  4563. // Draw face highlights for selected faces.
  4564. if (gSelectMgr.getTEMode())
  4565. {
  4566. struct f final : public LLSelectedTEFunctor
  4567. {
  4568. bool apply(LLViewerObject* object, S32 te) override
  4569. {
  4570. LLDrawable* drawablep = object->mDrawable;
  4571. if (drawablep)
  4572. {
  4573. LLFace* facep = drawablep->getFace(te);
  4574. if (facep)
  4575. {
  4576. gPipeline.mSelectedFaces.push_back(facep);
  4577. }
  4578. }
  4579. return true;
  4580. }
  4581. } func;
  4582. gSelectMgr.getSelection()->applyToTEs(&func);
  4583. }
  4584. }
  4585. }
  4586. void render_hud_elements()
  4587. {
  4588. gPipeline.disableLights();
  4589. LLGLSUIDefault gls_ui;
  4590. LLGLEnable stencil(gUsePBRShaders ? 0 : GL_STENCIL_TEST);
  4591. if (!gUsePBRShaders)
  4592. {
  4593. glStencilFunc(GL_ALWAYS, 255, 0xFFFFFFFF);
  4594. glStencilMask(0xFFFFFFFF);
  4595. glStencilOp(GL_KEEP, GL_KEEP, GL_REPLACE);
  4596. }
  4597. gUIProgram.bind();
  4598. gGL.color4f(1.f, 1.f, 1.f, 1.f);
  4599. LLGLDepthTest depth(GL_TRUE, GL_FALSE);
  4600. if (!LLPipeline::sReflectionRender &&
  4601. gPipeline.hasRenderDebugFeatureMask(LLPipeline::RENDER_DEBUG_FEATURE_UI))
  4602. {
  4603. bool sample = !gUsePBRShaders && LLPipeline::RenderFSAASamples > 0;
  4604. LLGLEnable multisample(sample ? GL_MULTISAMPLE : 0);
  4605. // For HUD version in render_ui_3d()
  4606. gViewerWindowp->renderSelections(false, false, false);
  4607. // Draw the tracking overlays
  4608. gTracker.render3D();
  4609. //MK
  4610. if (!gRLenabled || !gRLInterface.mVisionRestricted)
  4611. //mk
  4612. {
  4613. // Show the property lines
  4614. gWorld.renderPropertyLines();
  4615. gViewerParcelMgr.render();
  4616. gViewerParcelMgr.renderParcelCollision();
  4617. }
  4618. // Note: for PBR this is done in render_ui() (llviewerdisplay.cpp) so
  4619. // to avoid seeing the text anti-aliased.
  4620. if (!gUsePBRShaders)
  4621. {
  4622. // Render name tags and hover texts.
  4623. LLHUDObject::renderAll();
  4624. }
  4625. }
  4626. else if (gForceRenderLandFence)
  4627. {
  4628. // This is only set when not rendering the UI, for parcel snapshots
  4629. gViewerParcelMgr.render();
  4630. }
  4631. else if (gPipeline.hasRenderType(LLPipeline::RENDER_TYPE_HUD))
  4632. {
  4633. LLHUDText::renderAllHUD();
  4634. }
  4635. gUIProgram.unbind();
  4636. gGL.flush();
  4637. }
  4638. void LLPipeline::renderHighlights()
  4639. {
  4640. S32 selected_count = mSelectedFaces.size();
  4641. S32 highlighted_count = mHighlightFaces.size();
  4642. if ((!selected_count && !highlighted_count) ||
  4643. !hasRenderDebugFeatureMask(RENDER_DEBUG_FEATURE_SELECTED))
  4644. {
  4645. // Nothing to draw
  4646. return;
  4647. }
  4648. LLGLSPipelineAlpha gls_pipeline_alpha;
  4649. disableLights();
  4650. bool shader_interface =
  4651. gViewerShaderMgrp->getShaderLevel(LLViewerShaderMgr::SHADER_INTERFACE) > 0;
  4652. std::vector<LLFace*>::iterator sel_start, sel_end;
  4653. if (selected_count)
  4654. {
  4655. sel_start = mSelectedFaces.begin();
  4656. sel_end = mSelectedFaces.end();
  4657. }
  4658. if (highlighted_count)
  4659. {
  4660. // Beacons face highlights
  4661. if (shader_interface)
  4662. {
  4663. gHighlightProgram.bind();
  4664. }
  4665. LLColor4 color(1.f, 0.f, 0.f, .5f);
  4666. for (S32 i = 0; i < highlighted_count; ++i)
  4667. {
  4668. LLFace* facep = mHighlightFaces[i];
  4669. if (facep && !facep->getDrawable()->isDead())
  4670. {
  4671. if (!selected_count ||
  4672. // Exclude selected faces from beacon highlights
  4673. std::find(sel_start, sel_end, facep) == sel_end)
  4674. {
  4675. facep->renderSelected(LLViewerTexture::sNullImagep, color);
  4676. }
  4677. }
  4678. else if (gDebugGL)
  4679. {
  4680. llwarns << "Bad face in beacons highlights" << llendl;
  4681. }
  4682. }
  4683. if (shader_interface)
  4684. {
  4685. gHighlightProgram.unbind();
  4686. }
  4687. mHighlightFaces.clear();
  4688. }
  4689. if (selected_count)
  4690. {
  4691. // Selection image initialization if needed
  4692. if (!mFaceSelectImagep)
  4693. {
  4694. mFaceSelectImagep =
  4695. LLViewerTextureManager::getFetchedTexture(IMG_FACE_SELECT);
  4696. }
  4697. // Make sure the selection image gets downloaded and decoded
  4698. F32 max_area = gMaxImageSizeDefault * gMaxImageSizeDefault;
  4699. mFaceSelectImagep->addTextureStats(max_area);
  4700. // Use the color matching the channel we are editing
  4701. LLColor4 color;
  4702. LLRender::eTexIndex active_channel = sRenderHighlightTextureChannel;
  4703. switch (active_channel)
  4704. {
  4705. case LLRender::NORMAL_MAP:
  4706. color = LLColor4(1.f, .5f, .5f, .5f);
  4707. break;
  4708. case LLRender::SPECULAR_MAP:
  4709. color = LLColor4(0.f, .3f, 1.f, .8f);
  4710. break;
  4711. case LLRender::DIFFUSE_MAP:
  4712. default:
  4713. color = LLColor4(1.f, 1.f, 1.f, .5f);
  4714. }
  4715. LLGLSLShader* prev_shaderp = NULL;
  4716. for (S32 i = 0; i < selected_count; ++i)
  4717. {
  4718. LLFace* facep = mSelectedFaces[i];
  4719. if (facep && !facep->getDrawable()->isDead())
  4720. {
  4721. LLMaterial* matp = NULL;
  4722. if (sRenderDeferred && active_channel != LLRender::DIFFUSE_MAP)
  4723. {
  4724. // Fetch the material info, if any
  4725. const LLTextureEntry* tep = facep->getTextureEntry();
  4726. if (tep)
  4727. {
  4728. matp = tep->getMaterialParams().get();
  4729. }
  4730. }
  4731. if (shader_interface)
  4732. {
  4733. // Default to diffuse map highlighting
  4734. LLGLSLShader* new_shaderp = &gHighlightProgram;
  4735. // Use normal or specular maps highlighting where possible
  4736. // (i.e. material exists and got a corresponding map)
  4737. if (active_channel == LLRender::NORMAL_MAP && matp &&
  4738. matp->getNormalID().notNull())
  4739. {
  4740. new_shaderp = &gHighlightNormalProgram;
  4741. }
  4742. else if (active_channel == LLRender::SPECULAR_MAP &&
  4743. matp && matp->getSpecularID().notNull())
  4744. {
  4745. new_shaderp = &gHighlightSpecularProgram;
  4746. }
  4747. // Change the shader if not already the one in use
  4748. if (shader_interface && new_shaderp != prev_shaderp)
  4749. {
  4750. if (prev_shaderp)
  4751. {
  4752. prev_shaderp->unbind();
  4753. }
  4754. new_shaderp->bind();
  4755. prev_shaderp = new_shaderp;
  4756. }
  4757. }
  4758. // Draw the selection on the face.
  4759. facep->renderSelected(mFaceSelectImagep, color);
  4760. }
  4761. else if (gDebugGL)
  4762. {
  4763. llwarns << "Bad face in selection" << llendl;
  4764. }
  4765. }
  4766. // Unbind the last shader, if any
  4767. if (prev_shaderp)
  4768. {
  4769. prev_shaderp->unbind();
  4770. }
  4771. }
  4772. }
  4773. // Debug use
  4774. U32 LLPipeline::sCurRenderPoolType = 0;
  4775. // Only for use by the EE renderer (in forward rendering mode)
  4776. void LLPipeline::renderGeom(LLCamera& camera)
  4777. {
  4778. LL_FAST_TIMER(FTM_RENDER_GEOMETRY);
  4779. // HACK: preserve/restore matrices around HUD render
  4780. LLMatrix4a saved_modelview;
  4781. LLMatrix4a saved_projection;
  4782. bool hud_render = hasRenderType(RENDER_TYPE_HUD);
  4783. if (hud_render)
  4784. {
  4785. saved_modelview = gGLModelView;
  4786. saved_projection = gGLProjection;
  4787. }
  4788. ///////////////////////////////////////////
  4789. // Sync and verify GL state
  4790. LLVertexBuffer::unbind();
  4791. // Do verification of GL state
  4792. LL_GL_CHECK_STATES;
  4793. if (mRenderDebugMask & RENDER_DEBUG_VERIFY)
  4794. {
  4795. if (!verify())
  4796. {
  4797. llerrs << "Pipeline verification failed !" << llendl;
  4798. }
  4799. }
  4800. // Initialize lots of GL state to "safe" values
  4801. LLTexUnit* unit0 = gGL.getTexUnit(0);
  4802. unit0->unbind(LLTexUnit::TT_TEXTURE);
  4803. gGL.matrixMode(LLRender::MM_TEXTURE);
  4804. gGL.loadIdentity();
  4805. gGL.matrixMode(LLRender::MM_MODELVIEW);
  4806. LLGLSPipeline gls_pipeline;
  4807. LLGLEnable multisample(RenderFSAASamples > 0 ? GL_MULTISAMPLE : 0);
  4808. // Toggle backface culling for debugging
  4809. // Note: this used to be a toggle based on a mBackfaceCull boolean, but the
  4810. // latter was always true. HB
  4811. LLGLEnable cull_face(GL_CULL_FACE);
  4812. // Set fog
  4813. gSky.updateFog(camera.getFar());
  4814. if (!hasRenderDebugFeatureMask(RENDER_DEBUG_FEATURE_FOG))
  4815. {
  4816. sUnderWaterRender = false;
  4817. }
  4818. if (LLViewerFetchedTexture::sDefaultImagep)
  4819. {
  4820. unit0->bind(LLViewerFetchedTexture::sDefaultImagep);
  4821. LLViewerFetchedTexture::sDefaultImagep->setAddressMode(LLTexUnit::TAM_WRAP);
  4822. }
  4823. //////////////////////////////////////////////
  4824. // Actually render all of the geometry
  4825. pool_set_t::iterator pools_end = mPools.end();
  4826. for (pool_set_t::iterator iter = mPools.begin(); iter != pools_end; ++iter)
  4827. {
  4828. LLDrawPool* poolp = *iter;
  4829. if (hasRenderType(poolp->getType()))
  4830. {
  4831. poolp->prerender();
  4832. }
  4833. }
  4834. {
  4835. LL_FAST_TIMER(FTM_POOLS);
  4836. // *HACK: do not calculate local lights if we are rendering the HUD !
  4837. // Removing this check will cause bad flickering when there are HUD
  4838. // elements being rendered AND the user is in flycam mode. -nyx
  4839. if (!hud_render)
  4840. {
  4841. calcNearbyLights(camera);
  4842. setupHWLights();
  4843. }
  4844. bool occlude = sUseOcclusion > 1;
  4845. U32 cur_type = 0;
  4846. pool_set_t::iterator iter1 = mPools.begin();
  4847. while (iter1 != pools_end)
  4848. {
  4849. LLDrawPool* poolp = *iter1;
  4850. cur_type = poolp->getType();
  4851. // Debug use
  4852. sCurRenderPoolType = cur_type;
  4853. if (occlude && cur_type >= LLDrawPool::POOL_GRASS)
  4854. {
  4855. occlude = false;
  4856. gGLLastMatrix = NULL;
  4857. gGL.loadMatrix(gGLModelView);
  4858. LLGLSLShader::unbind();
  4859. doOcclusion(camera);
  4860. }
  4861. pool_set_t::iterator iter2 = iter1;
  4862. S32 passes;
  4863. if (hasRenderType(poolp->getType()) &&
  4864. (passes = poolp->getNumPasses()) > 0)
  4865. {
  4866. LL_FAST_TIMER(FTM_POOLRENDER);
  4867. gGLLastMatrix = NULL;
  4868. gGL.loadMatrix(gGLModelView);
  4869. for (S32 i = 0; i < passes; ++i)
  4870. {
  4871. LLVertexBuffer::unbind();
  4872. poolp->beginRenderPass(i);
  4873. for (iter2 = iter1; iter2 != pools_end; ++iter2)
  4874. {
  4875. LLDrawPool* p = *iter2;
  4876. if (p->getType() != cur_type)
  4877. {
  4878. break;
  4879. }
  4880. p->render(i);
  4881. }
  4882. poolp->endRenderPass(i);
  4883. LLVertexBuffer::unbind();
  4884. if (gDebugGL && iter2 != pools_end)
  4885. {
  4886. std::string msg = llformat("%s pass %d",
  4887. gPoolNames[cur_type].c_str(),
  4888. i);
  4889. LLGLState::checkStates(msg);
  4890. }
  4891. }
  4892. }
  4893. else
  4894. {
  4895. // Skip all pools of this type
  4896. for (iter2 = iter1; iter2 != pools_end; ++iter2)
  4897. {
  4898. LLDrawPool* p = *iter2;
  4899. if (p->getType() != cur_type)
  4900. {
  4901. break;
  4902. }
  4903. }
  4904. }
  4905. iter1 = iter2;
  4906. }
  4907. LLVertexBuffer::unbind();
  4908. gGLLastMatrix = NULL;
  4909. gGL.loadMatrix(gGLModelView);
  4910. if (occlude)
  4911. {
  4912. occlude = false;
  4913. gGLLastMatrix = NULL;
  4914. gGL.loadMatrix(gGLModelView);
  4915. LLGLSLShader::unbind();
  4916. doOcclusion(camera);
  4917. }
  4918. }
  4919. LLVertexBuffer::unbind();
  4920. LL_GL_CHECK_STATES;
  4921. if (!sImpostorRender)
  4922. {
  4923. if (!sReflectionRender)
  4924. {
  4925. renderHighlights();
  4926. }
  4927. // Contains a list of the faces of beacon-targeted objects that are
  4928. // also to be highlighted.
  4929. mHighlightFaces.clear();
  4930. renderDebug();
  4931. LLVertexBuffer::unbind();
  4932. // HACK: preserve/restore matrices around HUD render
  4933. if (hud_render)
  4934. {
  4935. gGLModelView = saved_modelview;
  4936. gGLProjection = saved_projection;
  4937. }
  4938. }
  4939. LLVertexBuffer::unbind();
  4940. LL_GL_CHECK_STATES;
  4941. }
  4942. // Version for use only by the PBR renderer
  4943. void LLPipeline::renderGeomDeferred(LLCamera& camera, bool do_occlusion)
  4944. {
  4945. LL_FAST_TIMER(FTM_RENDER_GEOMETRY);
  4946. if (gUseWireframe)
  4947. {
  4948. glPolygonMode(GL_FRONT_AND_BACK, GL_LINE);
  4949. }
  4950. if (&camera == &gViewerCamera && !sAvatarPreviewRender)
  4951. {
  4952. // A bit hacky: this is the start of the main render frame, figure out
  4953. // delta between last modelview matrix and current modelview matrix.
  4954. LLMatrix4a mat(gGLLastModelView);
  4955. // The goal is to have a matrix here that goes from the last frame's
  4956. // camera space to the current frame's camera space
  4957. mat.invert();
  4958. mat.setMul(gGLModelView, mat);
  4959. gGLDeltaModelView = mat;
  4960. mat.invert();
  4961. gGLInverseDeltaModelView = mat;
  4962. }
  4963. bool occlude = do_occlusion && sUseOcclusion > 1 &&
  4964. !LLGLSLShader::sProfileEnabled;
  4965. setupHWLights();
  4966. {
  4967. LL_FAST_TIMER(FTM_POOLS);
  4968. LLGLEnable cull(GL_CULL_FACE);
  4969. pool_set_t::iterator pools_end = mPools.end();
  4970. for (pool_set_t::iterator it = mPools.begin(); it != pools_end; ++it)
  4971. {
  4972. LLDrawPool* poolp = *it;
  4973. if (hasRenderType(poolp->getType()))
  4974. {
  4975. poolp->prerender();
  4976. }
  4977. }
  4978. LLVertexBuffer::unbind();
  4979. LL_GL_CHECK_STATES;
  4980. if (gViewerShaderMgrp->getShaderLevel(LLViewerShaderMgr::SHADER_DEFERRED) > 1)
  4981. {
  4982. // Update reflection probe uniform
  4983. mReflectionMapManager.updateUniforms();
  4984. mHeroProbeManager.updateUniforms();
  4985. }
  4986. U32 cur_type = 0;
  4987. gGL.setColorMask(true, true);
  4988. pool_set_t::iterator iter1 = mPools.begin();
  4989. while (iter1 != pools_end)
  4990. {
  4991. LLDrawPool* poolp = *iter1;
  4992. cur_type = poolp->getType();
  4993. if (occlude && cur_type >= LLDrawPool::POOL_GRASS)
  4994. {
  4995. occlude = false;
  4996. gGLLastMatrix = NULL;
  4997. gGL.loadMatrix(gGLModelView);
  4998. doOcclusion(camera);
  4999. }
  5000. pool_set_t::iterator iter2 = iter1;
  5001. S32 passes;
  5002. if (hasRenderType(poolp->getType()) &&
  5003. (passes = poolp->getNumDeferredPasses()) > 0)
  5004. {
  5005. LL_FAST_TIMER(FTM_POOLRENDER);
  5006. gGLLastMatrix = NULL;
  5007. gGL.loadMatrix(gGLModelView);
  5008. for (S32 i = 0; i < passes; ++i)
  5009. {
  5010. LLVertexBuffer::unbind();
  5011. poolp->beginDeferredPass(i);
  5012. for (iter2 = iter1; iter2 != pools_end; ++iter2)
  5013. {
  5014. LLDrawPool* p = *iter2;
  5015. if (p->getType() != cur_type)
  5016. {
  5017. break;
  5018. }
  5019. p->renderDeferred(i);
  5020. }
  5021. poolp->endDeferredPass(i);
  5022. LLVertexBuffer::unbind();
  5023. LL_GL_CHECK_STATES;
  5024. }
  5025. }
  5026. else
  5027. {
  5028. // Skip all pools of this type
  5029. for (iter2 = iter1; iter2 != pools_end; ++iter2)
  5030. {
  5031. LLDrawPool* p = *iter2;
  5032. if (p->getType() != cur_type)
  5033. {
  5034. break;
  5035. }
  5036. }
  5037. }
  5038. iter1 = iter2;
  5039. }
  5040. gGLLastMatrix = NULL;
  5041. gGL.matrixMode(LLRender::MM_MODELVIEW);
  5042. gGL.loadMatrix(gGLModelView);
  5043. gGL.setColorMask(true, false);
  5044. stop_glerror();
  5045. }
  5046. if (gUseWireframe)
  5047. {
  5048. glPolygonMode(GL_FRONT_AND_BACK, GL_FILL);
  5049. }
  5050. }
  5051. // Version for use only by the EE renderer
  5052. void LLPipeline::renderGeomDeferred(LLCamera& camera)
  5053. {
  5054. // *HACK: branch to the PBR version when used without 'do_occlusion'
  5055. // parameter, so to simplify the dual renderer code. HB
  5056. if (gUsePBRShaders)
  5057. {
  5058. renderGeomDeferred(camera, false);
  5059. return;
  5060. }
  5061. LL_FAST_TIMER(FTM_RENDER_GEOMETRY);
  5062. {
  5063. LL_FAST_TIMER(FTM_POOLS);
  5064. LLGLEnable cull(GL_CULL_FACE);
  5065. LLGLEnable stencil(GL_STENCIL_TEST);
  5066. glStencilFunc(GL_ALWAYS, 1, 0xFFFFFFFF);
  5067. glStencilOp(GL_KEEP, GL_KEEP, GL_REPLACE);
  5068. pool_set_t::iterator pools_end = mPools.end();
  5069. for (pool_set_t::iterator it = mPools.begin(); it != pools_end; ++it)
  5070. {
  5071. LLDrawPool* poolp = *it;
  5072. if (hasRenderType(poolp->getType()))
  5073. {
  5074. poolp->prerender();
  5075. }
  5076. }
  5077. LLGLEnable multisample(RenderFSAASamples > 0 ? GL_MULTISAMPLE : 0);
  5078. LLVertexBuffer::unbind();
  5079. LL_GL_CHECK_STATES;
  5080. U32 cur_type = 0;
  5081. gGL.setColorMask(true, true);
  5082. pool_set_t::iterator iter1 = mPools.begin();
  5083. while (iter1 != pools_end)
  5084. {
  5085. LLDrawPool* poolp = *iter1;
  5086. cur_type = poolp->getType();
  5087. pool_set_t::iterator iter2 = iter1;
  5088. S32 passes;
  5089. if (hasRenderType(poolp->getType()) &&
  5090. (passes = poolp->getNumDeferredPasses()) > 0)
  5091. {
  5092. LL_FAST_TIMER(FTM_POOLRENDER);
  5093. gGLLastMatrix = NULL;
  5094. gGL.loadMatrix(gGLModelView);
  5095. for (S32 i = 0; i < passes; ++i)
  5096. {
  5097. LLVertexBuffer::unbind();
  5098. poolp->beginDeferredPass(i);
  5099. for (iter2 = iter1; iter2 != pools_end; ++iter2)
  5100. {
  5101. LLDrawPool* p = *iter2;
  5102. if (p->getType() != cur_type)
  5103. {
  5104. break;
  5105. }
  5106. p->renderDeferred(i);
  5107. }
  5108. poolp->endDeferredPass(i);
  5109. LLVertexBuffer::unbind();
  5110. LL_GL_CHECK_STATES;
  5111. }
  5112. }
  5113. else
  5114. {
  5115. // Skip all pools of this type
  5116. for (iter2 = iter1; iter2 != pools_end; ++iter2)
  5117. {
  5118. LLDrawPool* p = *iter2;
  5119. if (p->getType() != cur_type)
  5120. {
  5121. break;
  5122. }
  5123. }
  5124. }
  5125. iter1 = iter2;
  5126. }
  5127. gGLLastMatrix = NULL;
  5128. gGL.matrixMode(LLRender::MM_MODELVIEW);
  5129. gGL.loadMatrix(gGLModelView);
  5130. gGL.setColorMask(true, false);
  5131. stop_glerror();
  5132. }
  5133. }
  5134. void LLPipeline::renderGeomPostDeferred(LLCamera& camera, bool do_occlusion)
  5135. {
  5136. LL_FAST_TIMER(FTM_POOLS);
  5137. bool occlude;
  5138. bool sample;
  5139. if (gUsePBRShaders)
  5140. {
  5141. sample = occlude = false;
  5142. if (gUseWireframe)
  5143. {
  5144. glPolygonMode(GL_FRONT_AND_BACK, GL_LINE);
  5145. }
  5146. }
  5147. else
  5148. {
  5149. occlude = do_occlusion && sUseOcclusion > 1;
  5150. sample = LLPipeline::RenderFSAASamples > 0;
  5151. }
  5152. LLGLEnable cull(GL_CULL_FACE);
  5153. LLGLEnable multisample(sample ? GL_MULTISAMPLE : 0);
  5154. // Skip PBR atmospherics/haze when in EE rendering, or when rendering HUDs,
  5155. // or when rendering impostors, or when not rendering atmospherics.
  5156. bool done_atmospherics = !gUsePBRShaders || sRenderingHUDs ||
  5157. sImpostorRender || !RenderDeferredAtmospheric;
  5158. bool done_water_haze = done_atmospherics;
  5159. // Do atmospheric haze just before water pass when below water, or just
  5160. // before post water alpha when above water.
  5161. U32 atm_pass = sUnderWaterRender ? LLDrawPool::POOL_WATER
  5162. : LLDrawPool::POOL_ALPHA_POST_WATER;
  5163. // Do water haze just before pre water alpha.
  5164. constexpr U32 water_haze_pass = LLDrawPool::POOL_ALPHA_PRE_WATER;
  5165. calcNearbyLights(camera);
  5166. setupHWLights();
  5167. if (gUsePBRShaders)
  5168. {
  5169. gGL.setSceneBlendType(LLRender::BT_ALPHA);
  5170. }
  5171. gGL.setColorMask(true, false);
  5172. pool_set_t::iterator iter1 = mPools.begin();
  5173. pool_set_t::iterator pools_end = mPools.end();
  5174. while (iter1 != pools_end)
  5175. {
  5176. LLDrawPool* poolp = *iter1;
  5177. U32 cur_type = poolp->getType();
  5178. // Used only in EE rendering mode. HB
  5179. if (occlude && cur_type >= LLDrawPool::POOL_GRASS)
  5180. {
  5181. occlude = false;
  5182. gGLLastMatrix = NULL;
  5183. gGL.loadMatrix(gGLModelView);
  5184. LLGLSLShader::unbind();
  5185. doOcclusion(camera, mRT->mScreen, mOcclusionDepth,
  5186. &mDeferredDepth);
  5187. gGL.setColorMask(true, false);
  5188. }
  5189. // Used only in PBR rendering mode. HB
  5190. if (!done_atmospherics && cur_type >= atm_pass)
  5191. {
  5192. doAtmospherics();
  5193. done_atmospherics = true;
  5194. }
  5195. if (!done_water_haze && cur_type >= water_haze_pass)
  5196. {
  5197. doWaterHaze();
  5198. done_water_haze = true;
  5199. }
  5200. pool_set_t::iterator iter2 = iter1;
  5201. S32 passes;
  5202. if (hasRenderType(poolp->getType()) &&
  5203. (passes = poolp->getNumPostDeferredPasses()) > 0)
  5204. {
  5205. LL_FAST_TIMER(FTM_POOLRENDER);
  5206. // In PBR rendering mode, some draw pools do not use at all the
  5207. // deferred pass, so we must ensure prerender() has been called for
  5208. // them ! HB
  5209. if (gUsePBRShaders && !poolp->getNumDeferredPasses())
  5210. {
  5211. poolp->prerender();
  5212. }
  5213. gGLLastMatrix = NULL;
  5214. gGL.loadMatrix(gGLModelView);
  5215. for (S32 i = 0; i < passes; ++i)
  5216. {
  5217. LLVertexBuffer::unbind();
  5218. poolp->beginPostDeferredPass(i);
  5219. for (iter2 = iter1; iter2 != pools_end; ++iter2)
  5220. {
  5221. LLDrawPool* p = *iter2;
  5222. if (p->getType() != cur_type)
  5223. {
  5224. break;
  5225. }
  5226. p->renderPostDeferred(i);
  5227. }
  5228. poolp->endPostDeferredPass(i);
  5229. LLVertexBuffer::unbind();
  5230. LL_GL_CHECK_STATES;
  5231. }
  5232. }
  5233. else
  5234. {
  5235. // Skip all pools of this type
  5236. for (iter2 = iter1; iter2 != pools_end; ++iter2)
  5237. {
  5238. LLDrawPool* p = *iter2;
  5239. if (p->getType() != cur_type)
  5240. {
  5241. break;
  5242. }
  5243. }
  5244. }
  5245. iter1 = iter2;
  5246. }
  5247. gGLLastMatrix = NULL;
  5248. gGL.matrixMode(LLRender::MM_MODELVIEW);
  5249. gGL.loadMatrix(gGLModelView);
  5250. if (gUsePBRShaders)
  5251. {
  5252. if (!gCubeSnapshot)
  5253. {
  5254. // Render highlights, etc.
  5255. renderHighlights();
  5256. mHighlightFaces.clear();
  5257. renderDebug();
  5258. }
  5259. if (gUseWireframe)
  5260. {
  5261. glPolygonMode(GL_FRONT_AND_BACK, GL_FILL);
  5262. }
  5263. }
  5264. else if (occlude)
  5265. {
  5266. LLGLSLShader::unbind();
  5267. doOcclusion(camera);
  5268. gGLLastMatrix = NULL;
  5269. gGL.matrixMode(LLRender::MM_MODELVIEW);
  5270. gGL.loadMatrix(gGLModelView);
  5271. }
  5272. stop_glerror();
  5273. }
  5274. // PBR only
  5275. void LLPipeline::doAtmospherics()
  5276. {
  5277. LL_TRACY_TIMER(TRC_DO_ATMOSPHERICS);
  5278. {
  5279. // Copy depth buffer for use in haze shader (use water displacement map
  5280. // as temp storage).
  5281. LLGLDepthTest depth(GL_TRUE, GL_TRUE, GL_ALWAYS);
  5282. mRT->mScreen.flush();
  5283. mWaterDis.bindTarget();
  5284. gCopyDepthProgram.bind();
  5285. S32 diff_map =
  5286. gCopyDepthProgram.getTextureChannel(LLShaderMgr::DIFFUSE_MAP);
  5287. S32 depth_map =
  5288. gCopyDepthProgram.getTextureChannel(LLShaderMgr::DEFERRED_DEPTH);
  5289. gGL.getTexUnit(diff_map)->bind(&mRT->mScreen);
  5290. gGL.getTexUnit(depth_map)->bind(&mRT->mDeferredScreen, true);
  5291. gGL.setColorMask(false, false);
  5292. mScreenTriangleVB->setBuffer();
  5293. mScreenTriangleVB->drawArrays(LLRender::TRIANGLES, 0, 3);
  5294. mWaterDis.flush();
  5295. mRT->mScreen.bindTarget();
  5296. }
  5297. LLGLEnable blend(GL_BLEND);
  5298. gGL.blendFunc(LLRender::BF_ONE, LLRender::BF_SOURCE_ALPHA,
  5299. LLRender::BF_ZERO, LLRender::BF_SOURCE_ALPHA);
  5300. gGL.setColorMask(true, true);
  5301. // Apply haze
  5302. bindDeferredShader(gHazeProgram, NULL, &mWaterDis);
  5303. gHazeProgram.uniform1i(LLShaderMgr::SUN_UP_FACTOR, mIsSunUp ? 1 : 0);
  5304. gHazeProgram.uniform3fv(LLShaderMgr::LIGHTNORM, 1,
  5305. gEnvironment.getClampedLightNorm().mV);
  5306. gHazeProgram.uniform4fv(LLShaderMgr::WATER_WATERPLANE, 1,
  5307. sWaterPlane.getF32ptr());
  5308. LLGLDepthTest depth(GL_FALSE);
  5309. // Full screen blit
  5310. mScreenTriangleVB->setBuffer();
  5311. mScreenTriangleVB->drawArrays(LLRender::TRIANGLES, 0, 3);
  5312. unbindDeferredShader(gHazeProgram);
  5313. gGL.setSceneBlendType(LLRender::BT_ALPHA);
  5314. }
  5315. // PBR only
  5316. void LLPipeline::doWaterHaze()
  5317. {
  5318. LL_TRACY_TIMER(TRC_DO_WATER_HAZE);
  5319. {
  5320. // Copy depth buffer for use in haze shader (use water displacement map
  5321. // as temp storage).
  5322. LLGLDepthTest depth(GL_TRUE, GL_TRUE, GL_ALWAYS);
  5323. mRT->mScreen.flush();
  5324. mWaterDis.bindTarget();
  5325. gCopyDepthProgram.bind();
  5326. S32 diff_map =
  5327. gCopyDepthProgram.getTextureChannel(LLShaderMgr::DIFFUSE_MAP);
  5328. S32 depth_map =
  5329. gCopyDepthProgram.getTextureChannel(LLShaderMgr::DEFERRED_DEPTH);
  5330. gGL.getTexUnit(diff_map)->bind(&mRT->mScreen);
  5331. gGL.getTexUnit(depth_map)->bind(&mRT->mDeferredScreen, true);
  5332. gGL.setColorMask(false, false);
  5333. mScreenTriangleVB->setBuffer();
  5334. mScreenTriangleVB->drawArrays(LLRender::TRIANGLES, 0, 3);
  5335. mWaterDis.flush();
  5336. mRT->mScreen.bindTarget();
  5337. }
  5338. gGL.setColorMask(true, true);
  5339. {
  5340. LLGLEnable blend(GL_BLEND);
  5341. gGL.blendFunc(LLRender::BF_ONE, LLRender::BF_SOURCE_ALPHA,
  5342. LLRender::BF_ZERO, LLRender::BF_SOURCE_ALPHA);
  5343. // Apply haze
  5344. bindDeferredShader(gHazeWaterProgram, NULL, &mWaterDis);
  5345. gHazeWaterProgram.uniform4fv(LLShaderMgr::WATER_WATERPLANE, 1,
  5346. sWaterPlane.getF32ptr());
  5347. if (sUnderWaterRender)
  5348. {
  5349. gHazeWaterProgram.uniform1i(sAboveWater, -1);
  5350. LLGLDepthTest depth(GL_FALSE);
  5351. // Full screen blit
  5352. mScreenTriangleVB->setBuffer();
  5353. mScreenTriangleVB->drawArrays(LLRender::TRIANGLES, 0, 3);
  5354. }
  5355. else
  5356. {
  5357. gHazeWaterProgram.uniform1i(sAboveWater, 1);
  5358. // Render water patches like LLDrawPoolWater does
  5359. LLGLDepthTest depth(GL_TRUE, GL_FALSE);
  5360. LLGLDisable cull(GL_CULL_FACE);
  5361. gGLLastMatrix = NULL;
  5362. gGL.loadMatrix(gGLModelView);
  5363. if (mWaterPool)
  5364. {
  5365. mWaterPool->pushFaceGeometry();
  5366. }
  5367. }
  5368. unbindDeferredShader(gHazeWaterProgram);
  5369. gGL.setSceneBlendType(LLRender::BT_ALPHA);
  5370. }
  5371. // Moved from the now removed LLDrawPoolWater::beginPostDeferredPass(): the
  5372. // latter was pointlessly executed for *each* water pool, while all we need
  5373. // is a frame buffer copy of the screen just before the first pass of the
  5374. // first water pool: do this here, since doWaterHaze() is called precisely
  5375. // at this right time ! HB
  5376. if (waterReflectionType() && !gCubeSnapshot)
  5377. {
  5378. // Copy framebuffer contents so far to a texture to be used for
  5379. // reflections and refractions
  5380. LLGLDepthTest depth(GL_TRUE, GL_TRUE, GL_ALWAYS);
  5381. mWaterDis.bindTarget();
  5382. gCopyDepthProgram.bind();
  5383. S32 diff_chan =
  5384. gCopyDepthProgram.getTextureChannel(LLShaderMgr::DIFFUSE_MAP);
  5385. S32 depth_chan =
  5386. gCopyDepthProgram.getTextureChannel(LLShaderMgr::DEFERRED_DEPTH);
  5387. gGL.getTexUnit(diff_chan)->bind(&mRT->mScreen);
  5388. gGL.getTexUnit(depth_chan)->bind(&mRT->mDeferredScreen, true);
  5389. mScreenTriangleVB->setBuffer();
  5390. mScreenTriangleVB->drawArrays(LLRender::TRIANGLES, 0, 3);
  5391. gCopyDepthProgram.unbind();
  5392. mWaterDis.flush();
  5393. }
  5394. }
  5395. void LLPipeline::renderGeomShadow(LLCamera& camera)
  5396. {
  5397. U32 cur_type = 0;
  5398. LLGLEnable cull(GL_CULL_FACE);
  5399. LLVertexBuffer::unbind();
  5400. pool_set_t::iterator iter1 = mPools.begin();
  5401. pool_set_t::iterator pools_end = mPools.end();
  5402. while (iter1 != pools_end)
  5403. {
  5404. LLDrawPool* poolp = *iter1;
  5405. cur_type = poolp->getType();
  5406. pool_set_t::iterator iter2 = iter1;
  5407. S32 passes;
  5408. if (hasRenderType(poolp->getType()) &&
  5409. (passes = poolp->getNumShadowPasses()) > 0)
  5410. {
  5411. poolp->prerender();
  5412. gGLLastMatrix = NULL;
  5413. gGL.loadMatrix(gGLModelView);
  5414. for (S32 i = 0; i < passes; ++i)
  5415. {
  5416. LLVertexBuffer::unbind();
  5417. poolp->beginShadowPass(i);
  5418. for (iter2 = iter1; iter2 != pools_end; ++iter2)
  5419. {
  5420. LLDrawPool* p = *iter2;
  5421. if (p->getType() != cur_type)
  5422. {
  5423. break;
  5424. }
  5425. p->renderShadow(i);
  5426. }
  5427. poolp->endShadowPass(i);
  5428. LLVertexBuffer::unbind();
  5429. LL_GL_CHECK_STATES;
  5430. }
  5431. }
  5432. else
  5433. {
  5434. // Skip all pools of this type
  5435. for (iter2 = iter1; iter2 != pools_end; ++iter2)
  5436. {
  5437. LLDrawPool* p = *iter2;
  5438. if (p->getType() != cur_type)
  5439. {
  5440. break;
  5441. }
  5442. }
  5443. }
  5444. iter1 = iter2;
  5445. }
  5446. gGLLastMatrix = NULL;
  5447. gGL.loadMatrix(gGLModelView);
  5448. stop_glerror();
  5449. }
  5450. void LLPipeline::addTrianglesDrawn(U32 index_count)
  5451. {
  5452. if (mNeedsDrawStats)
  5453. {
  5454. U32 count = index_count / 3;
  5455. mTrianglesDrawn += count;
  5456. if (count > mMaxBatchSize)
  5457. {
  5458. mMaxBatchSize = count;
  5459. }
  5460. if (count < mMinBatchSize)
  5461. {
  5462. mMinBatchSize = count;
  5463. }
  5464. ++mBatchCount;
  5465. }
  5466. if (sRenderFrameTest)
  5467. {
  5468. gWindowp->swapBuffers();
  5469. ms_sleep(16);
  5470. }
  5471. }
  5472. void LLPipeline::renderPhysicsDisplay()
  5473. {
  5474. if (!gUsePBRShaders)
  5475. {
  5476. allocatePhysicsBuffer();
  5477. gGL.flush();
  5478. mPhysicsDisplay.bindTarget();
  5479. glClearColor(0.f, 0.f, 0.f, 1.f);
  5480. gGL.setColorMask(true, true);
  5481. mPhysicsDisplay.clear();
  5482. glClearColor(0.f, 0.f, 0.f, 0.f);
  5483. gGL.setColorMask(true, false);
  5484. gDebugProgram.bind();
  5485. for (LLWorld::region_list_t::const_iterator
  5486. iter = gWorld.getRegionList().begin(),
  5487. end = gWorld.getRegionList().end();
  5488. iter != end; ++iter)
  5489. {
  5490. LLViewerRegion* regionp = *iter;
  5491. for (U32 i = 0; i < LLViewerRegion::PARTITION_VO_CACHE; ++i)
  5492. {
  5493. LLSpatialPartition* partp = regionp->getSpatialPartition(i);
  5494. // None of the partitions under PARTITION_VO_CACHE can be NULL
  5495. if (hasRenderType(partp->mDrawableType))
  5496. {
  5497. partp->renderPhysicsShapes();
  5498. }
  5499. }
  5500. }
  5501. gGL.flush();
  5502. gDebugProgram.unbind();
  5503. mPhysicsDisplay.flush();
  5504. return;
  5505. }
  5506. gGL.flush();
  5507. gDebugProgram.bind();
  5508. LLGLEnable(GL_POLYGON_OFFSET_LINE);
  5509. glPolygonOffset(3.f, 3.f);
  5510. glLineWidth(3.f);
  5511. LLGLEnable blend(GL_BLEND);
  5512. gGL.setSceneBlendType(LLRender::BT_ALPHA);
  5513. for (U32 pass = 0; pass < 3; ++pass)
  5514. {
  5515. // Pass 0 - depth write enabled, color write disabled, fill
  5516. // Pass 1 - depth write disabled, color write enabled, fill
  5517. // Pass 2 - depth write disabled, color write enabled, wireframe
  5518. gGL.setColorMask(pass >= 1, false);
  5519. LLGLDepthTest depth(GL_TRUE, pass == 0);
  5520. bool wireframe = pass == 2;
  5521. if (wireframe)
  5522. {
  5523. glPolygonMode(GL_FRONT_AND_BACK, GL_LINE);
  5524. }
  5525. for (LLWorld::region_list_t::const_iterator
  5526. iter = gWorld.getRegionList().begin(),
  5527. end = gWorld.getRegionList().end();
  5528. iter != end; ++iter)
  5529. {
  5530. LLViewerRegion* region = *iter;
  5531. for (U32 i = 0; i < LLViewerRegion::PARTITION_VO_CACHE; ++i)
  5532. {
  5533. LLSpatialPartition* partp = region->getSpatialPartition(i);
  5534. // None of the partitions under PARTITION_VO_CACHE can be NULL
  5535. if (hasRenderType(partp->mDrawableType))
  5536. {
  5537. partp->renderPhysicsShapes(wireframe);
  5538. }
  5539. }
  5540. }
  5541. gGL.flush();
  5542. if (wireframe)
  5543. {
  5544. glPolygonMode(GL_FRONT_AND_BACK, GL_FILL);
  5545. }
  5546. }
  5547. glLineWidth(1.f);
  5548. gDebugProgram.unbind();
  5549. }
  5550. void LLPipeline::renderDebug()
  5551. {
  5552. bool hud_only = hasRenderType(RENDER_TYPE_HUD);
  5553. bool render_blips = !hud_only && !mDebugBlips.empty();
  5554. // If no debug feature is on and there's no blip to render, return now
  5555. if (mRenderDebugMask == 0 && !render_blips) return;
  5556. gGL.color4f(1.f, 1.f, 1.f, 1.f);
  5557. gGLLastMatrix = NULL;
  5558. gGL.loadMatrix(gGLModelView);
  5559. gGL.setColorMask(true, false);
  5560. if (render_blips)
  5561. {
  5562. // Render debug blips
  5563. gUIProgram.bind();
  5564. gGL.getTexUnit(0)->bind(LLViewerFetchedTexture::sWhiteImagep, true);
  5565. glPointSize(8.f);
  5566. LLGLDepthTest depth(GL_TRUE, GL_TRUE, GL_ALWAYS);
  5567. gGL.begin(LLRender::POINTS);
  5568. for (std::list<DebugBlip>::iterator iter = mDebugBlips.begin(),
  5569. end = mDebugBlips.end();
  5570. iter != end; )
  5571. {
  5572. DebugBlip& blip = *iter;
  5573. blip.mAge += gFrameIntervalSeconds;
  5574. if (blip.mAge > 2.f)
  5575. {
  5576. iter = mDebugBlips.erase(iter);
  5577. }
  5578. else
  5579. {
  5580. ++iter;
  5581. }
  5582. blip.mPosition.mV[2] += gFrameIntervalSeconds * 2.f;
  5583. gGL.color4fv(blip.mColor.mV);
  5584. gGL.vertex3fv(blip.mPosition.mV);
  5585. }
  5586. gGL.end(true);
  5587. glPointSize(1.f);
  5588. gUIProgram.unbind();
  5589. stop_glerror();
  5590. }
  5591. // If no debug feature is on, return now
  5592. if (mRenderDebugMask == 0) return;
  5593. // This is a no-op when gUsePBRShaders is true. HB
  5594. LLGLDepthTest gls_depth(GL_TRUE, GL_FALSE, GL_LEQUAL, gUsePBRShaders);
  5595. // Debug stuff.
  5596. for (LLWorld::region_list_t::const_iterator
  5597. iter = gWorld.getRegionList().begin(),
  5598. end = gWorld.getRegionList().end();
  5599. iter != end; ++iter)
  5600. {
  5601. LLViewerRegion* regionp = *iter;
  5602. if (hud_only)
  5603. {
  5604. for (U32 i = 0; i < LLViewerRegion::PARTITION_VO_CACHE; ++i)
  5605. {
  5606. LLSpatialPartition* partp = regionp->getSpatialPartition(i);
  5607. // None of the partitions under PARTITION_VO_CACHE can be NULL
  5608. U32 type = partp->mDrawableType;
  5609. if (type == RENDER_TYPE_HUD ||
  5610. type == RENDER_TYPE_HUD_PARTICLES)
  5611. {
  5612. partp->renderDebug();
  5613. }
  5614. }
  5615. }
  5616. else
  5617. {
  5618. for (U32 i = 0; i < LLViewerRegion::PARTITION_VO_CACHE; ++i)
  5619. {
  5620. LLSpatialPartition* partp = regionp->getSpatialPartition(i);
  5621. // None of the partitions under PARTITION_VO_CACHE can be NULL
  5622. if (hasRenderType(partp->mDrawableType))
  5623. {
  5624. partp->renderDebug();
  5625. }
  5626. }
  5627. }
  5628. }
  5629. LLCullResult::bridge_list_t& bridges = sCull->getVisibleBridge();
  5630. for (U32 i = 0, count = bridges.size(); i < count; ++i)
  5631. {
  5632. LLSpatialBridge* bridgep = bridges[i];
  5633. if (bridgep && !bridgep->isDead() &&
  5634. hasRenderType(bridgep->mDrawableType))
  5635. {
  5636. gGL.pushMatrix();
  5637. gGL.multMatrix(bridgep->mDrawable->getRenderMatrix().getF32ptr());
  5638. bridgep->renderDebug();
  5639. gGL.popMatrix();
  5640. }
  5641. }
  5642. constexpr U32 GLTF_DEBUG_MASK = RENDER_DEBUG_BBOXES |
  5643. RENDER_DEBUG_RAYCAST |
  5644. RENDER_DEBUG_NODES;
  5645. if (gUsePBRShaders && hasRenderDebugMask(GLTF_DEBUG_MASK))
  5646. {
  5647. LLGLTFSceneManager::renderDebug();
  5648. }
  5649. if (hasRenderDebugMask(RENDER_DEBUG_OCCLUSION) &&
  5650. !gVisibleSelectedGroups.empty())
  5651. {
  5652. // Render visible selected group occlusion geometry
  5653. gDebugProgram.bind();
  5654. LLGLDepthTest depth(GL_TRUE, GL_FALSE);
  5655. gGL.diffuseColor3f(1.f, 0.f, 1.f);
  5656. LLVector4a fudge, size;
  5657. for (spatial_groups_set_t::iterator
  5658. iter = gVisibleSelectedGroups.begin(),
  5659. end = gVisibleSelectedGroups.end();
  5660. iter != end; ++iter)
  5661. {
  5662. LLSpatialGroup* group = *iter;
  5663. fudge.splat(0.25f); // SG_OCCLUSION_FUDGE
  5664. const LLVector4a* bounds = group->getBounds();
  5665. size.setAdd(fudge, bounds[1]);
  5666. // Used to be called in drawBox(), now replaced with gl_draw_box()
  5667. // which does not call this. HB
  5668. LLVertexBuffer::unbind();
  5669. gl_draw_box(bounds[0], size);
  5670. }
  5671. }
  5672. gVisibleSelectedGroups.clear();
  5673. bool check_probes = gUsePBRShaders && !hud_only;
  5674. if (check_probes &&
  5675. gPipeline.hasRenderDebugMask(RENDER_DEBUG_REFLECTION_PROBES))
  5676. {
  5677. mReflectionMapManager.renderDebug();
  5678. }
  5679. static LLCachedControl<bool> render_probes(gSavedSettings,
  5680. "RenderReflectionProbeVolumes");
  5681. if (check_probes && render_probes)
  5682. {
  5683. bindDeferredShader(gReflectionProbeDisplayProgram);
  5684. mScreenTriangleVB->setBuffer();
  5685. LLGLEnable blend(GL_BLEND);
  5686. LLGLDepthTest depth(GL_FALSE);
  5687. mScreenTriangleVB->drawArrays(LLRender::TRIANGLES, 0, 3);
  5688. unbindDeferredShader(gReflectionProbeDisplayProgram);
  5689. }
  5690. gUIProgram.bind();
  5691. if (!hud_only && gDebugRaycastParticle &&
  5692. hasRenderDebugMask(RENDER_DEBUG_RAYCAST))
  5693. {
  5694. // Draw crosshairs on particle intersection
  5695. gDebugProgram.bind();
  5696. gGL.getTexUnit(0)->unbind(LLTexUnit::TT_TEXTURE);
  5697. LLVector3 center(gDebugRaycastParticleIntersection.getF32ptr());
  5698. LLVector3 size(0.1f, 0.1f, 0.1f);
  5699. LLVector3 p[6];
  5700. p[0] = center + size.scaledVec(LLVector3(1.f, 0.f, 0.f));
  5701. p[1] = center + size.scaledVec(LLVector3(-1.f, 0.f, 0.f));
  5702. p[2] = center + size.scaledVec(LLVector3(0.f, 1.f, 0.f));
  5703. p[3] = center + size.scaledVec(LLVector3(0.f, -1.f, 0.f));
  5704. p[4] = center + size.scaledVec(LLVector3(0.f, 0.f, 1.f));
  5705. p[5] = center + size.scaledVec(LLVector3(0.f, 0.f, -1.f));
  5706. gGL.begin(LLRender::LINES);
  5707. gGL.diffuseColor3f(1.f, 1.f, 0.f);
  5708. for (U32 i = 0; i < 6; ++i)
  5709. {
  5710. gGL.vertex3fv(p[i].mV);
  5711. }
  5712. gGL.end(true);
  5713. gDebugProgram.unbind();
  5714. stop_glerror();
  5715. }
  5716. if (!hud_only && hasRenderDebugMask(RENDER_DEBUG_SHADOW_FRUSTA))
  5717. {
  5718. LLVertexBuffer::unbind();
  5719. LLGLEnable blend(GL_BLEND);
  5720. LLGLDepthTest depth(GL_TRUE, GL_FALSE);
  5721. LLGLDisable cull(GL_CULL_FACE);
  5722. gGL.color4f(1.f, 1.f, 1.f, 1.f);
  5723. gGL.getTexUnit(0)->unbind(LLTexUnit::TT_TEXTURE);
  5724. static const F32 colors[] =
  5725. {
  5726. 1.f, 0.f, 0.f, 0.1f,
  5727. 0.f, 1.f, 0.f, 0.1f,
  5728. 0.f, 0.f, 1.f, 0.1f,
  5729. 1.f, 0.f, 1.f, 0.1f,
  5730. 1.f, 1.f, 0.f, 0.1f,
  5731. 0.f, 1.f, 1.f, 0.1f,
  5732. 1.f, 1.f, 1.f, 0.1f,
  5733. 1.f, 0.f, 1.f, 0.1f,
  5734. };
  5735. for (U32 i = 0; i < 8; ++i)
  5736. {
  5737. LLVector3* frust = mShadowCamera[i].mAgentFrustum;
  5738. if (i > 3)
  5739. {
  5740. // Render shadow frusta as volumes
  5741. if (mShadowFrustPoints[i - 4].empty())
  5742. {
  5743. continue;
  5744. }
  5745. gGL.color4fv(colors + (i - 4) * 4);
  5746. gGL.begin(LLRender::TRIANGLE_STRIP);
  5747. {
  5748. gGL.vertex3fv(frust[0].mV);
  5749. gGL.vertex3fv(frust[4].mV);
  5750. gGL.vertex3fv(frust[1].mV);
  5751. gGL.vertex3fv(frust[5].mV);
  5752. gGL.vertex3fv(frust[2].mV);
  5753. gGL.vertex3fv(frust[6].mV);
  5754. gGL.vertex3fv(frust[3].mV);
  5755. gGL.vertex3fv(frust[7].mV);
  5756. gGL.vertex3fv(frust[0].mV);
  5757. gGL.vertex3fv(frust[4].mV);
  5758. }
  5759. gGL.end();
  5760. gGL.begin(LLRender::TRIANGLE_STRIP);
  5761. {
  5762. gGL.vertex3fv(frust[0].mV);
  5763. gGL.vertex3fv(frust[1].mV);
  5764. gGL.vertex3fv(frust[3].mV);
  5765. gGL.vertex3fv(frust[2].mV);
  5766. }
  5767. gGL.end();
  5768. gGL.begin(LLRender::TRIANGLE_STRIP);
  5769. {
  5770. gGL.vertex3fv(frust[4].mV);
  5771. gGL.vertex3fv(frust[5].mV);
  5772. gGL.vertex3fv(frust[7].mV);
  5773. gGL.vertex3fv(frust[6].mV);
  5774. }
  5775. gGL.end();
  5776. }
  5777. if (i < 4)
  5778. {
  5779. #if 0
  5780. if (i == 0 || !mShadowFrustPoints[i].empty())
  5781. #endif
  5782. {
  5783. // Render visible point cloud
  5784. gGL.flush();
  5785. glPointSize(8.f);
  5786. gGL.begin(LLRender::POINTS);
  5787. gGL.color3fv(colors + i * 4);
  5788. for (U32 j = 0, size = mShadowFrustPoints[i].size();
  5789. j < size; ++j)
  5790. {
  5791. gGL.vertex3fv(mShadowFrustPoints[i][j].mV);
  5792. }
  5793. gGL.end(true);
  5794. glPointSize(1.f);
  5795. LLVector3* ext = mShadowExtents[i];
  5796. LLVector3 pos = (ext[0] + ext[1]) * 0.5f;
  5797. LLVector3 size = (ext[1] - ext[0]) * 0.5f;
  5798. gl_draw_box_outline(pos, size);
  5799. // Render camera frustum splits as outlines
  5800. gGL.begin(LLRender::LINES);
  5801. {
  5802. gGL.vertex3fv(frust[0].mV);
  5803. gGL.vertex3fv(frust[1].mV);
  5804. gGL.vertex3fv(frust[1].mV);
  5805. gGL.vertex3fv(frust[2].mV);
  5806. gGL.vertex3fv(frust[2].mV);
  5807. gGL.vertex3fv(frust[3].mV);
  5808. gGL.vertex3fv(frust[3].mV);
  5809. gGL.vertex3fv(frust[0].mV);
  5810. gGL.vertex3fv(frust[4].mV);
  5811. gGL.vertex3fv(frust[5].mV);
  5812. gGL.vertex3fv(frust[5].mV);
  5813. gGL.vertex3fv(frust[6].mV);
  5814. gGL.vertex3fv(frust[6].mV);
  5815. gGL.vertex3fv(frust[7].mV);
  5816. gGL.vertex3fv(frust[7].mV);
  5817. gGL.vertex3fv(frust[4].mV);
  5818. gGL.vertex3fv(frust[0].mV);
  5819. gGL.vertex3fv(frust[4].mV);
  5820. gGL.vertex3fv(frust[1].mV);
  5821. gGL.vertex3fv(frust[5].mV);
  5822. gGL.vertex3fv(frust[2].mV);
  5823. gGL.vertex3fv(frust[6].mV);
  5824. gGL.vertex3fv(frust[3].mV);
  5825. gGL.vertex3fv(frust[7].mV);
  5826. }
  5827. gGL.end();
  5828. }
  5829. }
  5830. gGL.flush();
  5831. }
  5832. stop_glerror();
  5833. }
  5834. LLViewerRegion* regionp = gAgent.getRegion();
  5835. if (regionp && (mRenderDebugMask & RENDER_DEBUG_WIND_VECTORS))
  5836. {
  5837. regionp->mWind.renderVectors();
  5838. }
  5839. if (regionp && (mRenderDebugMask & RENDER_DEBUG_COMPOSITION))
  5840. {
  5841. // Debug composition layers
  5842. F32 x, y;
  5843. gGL.getTexUnit(0)->unbind(LLTexUnit::TT_TEXTURE);
  5844. gGL.begin(LLRender::POINTS);
  5845. // Draw the composition layer for the region that I am in.
  5846. for (x = 0; x <= 260; ++x)
  5847. {
  5848. for (y = 0; y <= 260; ++y)
  5849. {
  5850. if (x > 255 || y > 255)
  5851. {
  5852. gGL.color4f(1.f, 0.f, 0.f, 1.f);
  5853. }
  5854. else
  5855. {
  5856. gGL.color4f(0.f, 0.f, 1.f, 1.f);
  5857. }
  5858. F32 z = regionp->getCompositionXY((S32)x, (S32)y);
  5859. z *= 5.f;
  5860. z += 50.f;
  5861. gGL.vertex3f(x, y, z);
  5862. }
  5863. }
  5864. gGL.end();
  5865. stop_glerror();
  5866. }
  5867. gGL.flush();
  5868. gUIProgram.unbind();
  5869. }
  5870. void LLPipeline::rebuildPools()
  5871. {
  5872. S32 max_count = mPools.size();
  5873. pool_set_t::iterator iter1 = mPools.upper_bound(mLastRebuildPool);
  5874. while (max_count > 0 && mPools.size() > 0) // && num_rebuilds < MAX_REBUILDS)
  5875. {
  5876. if (iter1 == mPools.end())
  5877. {
  5878. iter1 = mPools.begin();
  5879. }
  5880. LLDrawPool* poolp = *iter1;
  5881. if (poolp->isDead())
  5882. {
  5883. mPools.erase(iter1++);
  5884. removeFromQuickLookup(poolp);
  5885. if (poolp == mLastRebuildPool)
  5886. {
  5887. mLastRebuildPool = NULL;
  5888. }
  5889. delete poolp;
  5890. }
  5891. else
  5892. {
  5893. mLastRebuildPool = poolp;
  5894. ++iter1;
  5895. }
  5896. --max_count;
  5897. }
  5898. }
  5899. void LLPipeline::addToQuickLookup(LLDrawPool* new_poolp)
  5900. {
  5901. switch (new_poolp->getType())
  5902. {
  5903. case LLDrawPool::POOL_SIMPLE:
  5904. if (mSimplePool)
  5905. {
  5906. llwarns << "Ignoring duplicate simple pool." << llendl;
  5907. llassert(false);
  5908. }
  5909. else
  5910. {
  5911. mSimplePool = (LLRenderPass*)new_poolp;
  5912. }
  5913. break;
  5914. case LLDrawPool::POOL_ALPHA_MASK:
  5915. if (mAlphaMaskPool)
  5916. {
  5917. llwarns << "Ignoring duplicate alpha mask pool." << llendl;
  5918. llassert(false);
  5919. }
  5920. else
  5921. {
  5922. mAlphaMaskPool = (LLRenderPass*)new_poolp;
  5923. }
  5924. break;
  5925. case LLDrawPool::POOL_FULLBRIGHT_ALPHA_MASK:
  5926. if (mFullbrightAlphaMaskPool)
  5927. {
  5928. llwarns << "Ignoring duplicate alpha mask pool." << llendl;
  5929. llassert(false);
  5930. }
  5931. else
  5932. {
  5933. mFullbrightAlphaMaskPool = (LLRenderPass*)new_poolp;
  5934. }
  5935. break;
  5936. case LLDrawPool::POOL_GRASS:
  5937. if (mGrassPool)
  5938. {
  5939. llwarns << "Ignoring duplicate grass pool." << llendl;
  5940. llassert(false);
  5941. }
  5942. else
  5943. {
  5944. mGrassPool = (LLRenderPass*)new_poolp;
  5945. }
  5946. break;
  5947. case LLDrawPool::POOL_FULLBRIGHT:
  5948. if (mFullbrightPool)
  5949. {
  5950. llwarns << "Ignoring duplicate simple pool." << llendl;
  5951. llassert(false);
  5952. }
  5953. else
  5954. {
  5955. mFullbrightPool = (LLRenderPass*)new_poolp;
  5956. }
  5957. break;
  5958. case LLDrawPool::POOL_INVISIBLE:
  5959. if (mInvisiblePool)
  5960. {
  5961. llwarns << "Ignoring duplicate simple pool." << llendl;
  5962. llassert(false);
  5963. }
  5964. else
  5965. {
  5966. mInvisiblePool = (LLRenderPass*)new_poolp;
  5967. }
  5968. break;
  5969. case LLDrawPool::POOL_GLOW:
  5970. if (mGlowPool)
  5971. {
  5972. llwarns << "Ignoring duplicate glow pool." << llendl;
  5973. llassert(false);
  5974. }
  5975. else
  5976. {
  5977. mGlowPool = (LLRenderPass*)new_poolp;
  5978. }
  5979. break;
  5980. case LLDrawPool::POOL_TREE:
  5981. mTreePools.emplace(uintptr_t(new_poolp->getTexture()), new_poolp);
  5982. break;
  5983. case LLDrawPool::POOL_TERRAIN:
  5984. mTerrainPools.emplace(uintptr_t(new_poolp->getTexture()),
  5985. new_poolp);
  5986. break;
  5987. case LLDrawPool::POOL_BUMP:
  5988. if (mBumpPool)
  5989. {
  5990. llwarns << "Ignoring duplicate bump pool." << llendl;
  5991. llassert(false);
  5992. }
  5993. else
  5994. {
  5995. mBumpPool = new_poolp;
  5996. }
  5997. break;
  5998. case LLDrawPool::POOL_MATERIALS:
  5999. if (mMaterialsPool)
  6000. {
  6001. llwarns << "Ignoring duplicate materials pool." << llendl;
  6002. llassert(false);
  6003. }
  6004. else
  6005. {
  6006. mMaterialsPool = new_poolp;
  6007. }
  6008. break;
  6009. case LLDrawPool::POOL_ALPHA_PRE_WATER:
  6010. if (mAlphaPoolPreWater)
  6011. {
  6012. llwarns << "Ignoring duplicate pre-water alpha pool" << llendl;
  6013. llassert(false);
  6014. }
  6015. else
  6016. {
  6017. mAlphaPoolPreWater = (LLDrawPoolAlpha*)new_poolp;
  6018. }
  6019. break;
  6020. case LLDrawPool::POOL_ALPHA_POST_WATER:
  6021. if (mAlphaPoolPostWater)
  6022. {
  6023. llwarns << "Ignoring duplicate post-water alpha pool" << llendl;
  6024. llassert(false);
  6025. }
  6026. else
  6027. {
  6028. mAlphaPoolPostWater = (LLDrawPoolAlpha*)new_poolp;
  6029. }
  6030. break;
  6031. case LLDrawPool::POOL_ALPHA:
  6032. if (mAlphaPool)
  6033. {
  6034. llwarns << "Ignoring duplicate alpha pool" << llendl;
  6035. llassert(false);
  6036. }
  6037. else
  6038. {
  6039. mAlphaPool = (LLDrawPoolAlpha*)new_poolp;
  6040. }
  6041. break;
  6042. case LLDrawPool::POOL_AVATAR:
  6043. case LLDrawPool::POOL_PUPPET:
  6044. break; // Do nothing
  6045. case LLDrawPool::POOL_SKY:
  6046. if (mSkyPool)
  6047. {
  6048. llwarns << "Ignoring duplicate sky pool" << llendl;
  6049. llassert(false);
  6050. }
  6051. else
  6052. {
  6053. mSkyPool = new_poolp;
  6054. }
  6055. break;
  6056. case LLDrawPool::POOL_WATER:
  6057. if (mWaterPool)
  6058. {
  6059. llwarns << "Ignoring duplicate water pool" << llendl;
  6060. llassert(false);
  6061. }
  6062. else
  6063. {
  6064. mWaterPool = new_poolp;
  6065. }
  6066. break;
  6067. case LLDrawPool::POOL_WL_SKY:
  6068. if (mWLSkyPool)
  6069. {
  6070. llwarns << "Ignoring duplicate Windlight sky pool" << llendl;
  6071. llassert(false);
  6072. }
  6073. else
  6074. {
  6075. mWLSkyPool = new_poolp;
  6076. }
  6077. break;
  6078. case LLDrawPool::POOL_MAT_PBR:
  6079. if (mPBROpaquePool)
  6080. {
  6081. llwarns << "Ignoring duplicate PBR opaque pool" << llendl;
  6082. llassert(false);
  6083. }
  6084. else
  6085. {
  6086. mPBROpaquePool = new_poolp;
  6087. }
  6088. break;
  6089. case LLDrawPool::POOL_MAT_PBR_ALPHA_MASK:
  6090. if (mPBRAlphaMaskPool)
  6091. {
  6092. llwarns << "Ignoring duplicate PBR alpha mask pool" << llendl;
  6093. llassert(false);
  6094. }
  6095. else
  6096. {
  6097. mPBRAlphaMaskPool = new_poolp;
  6098. }
  6099. break;
  6100. default:
  6101. llerrs << "Invalid pool type: " << new_poolp->getType() << llendl;
  6102. }
  6103. }
  6104. void LLPipeline::removePool(LLDrawPool* poolp)
  6105. {
  6106. removeFromQuickLookup(poolp);
  6107. mPools.erase(poolp);
  6108. delete poolp;
  6109. }
  6110. void LLPipeline::removeFromQuickLookup(LLDrawPool* poolp)
  6111. {
  6112. switch (poolp->getType())
  6113. {
  6114. case LLDrawPool::POOL_SIMPLE:
  6115. llassert(mSimplePool == poolp);
  6116. mSimplePool = NULL;
  6117. break;
  6118. case LLDrawPool::POOL_ALPHA_MASK:
  6119. llassert(mAlphaMaskPool == poolp);
  6120. mAlphaMaskPool = NULL;
  6121. break;
  6122. case LLDrawPool::POOL_FULLBRIGHT_ALPHA_MASK:
  6123. llassert(mFullbrightAlphaMaskPool == poolp);
  6124. mFullbrightAlphaMaskPool = NULL;
  6125. break;
  6126. case LLDrawPool::POOL_GRASS:
  6127. llassert(mGrassPool == poolp);
  6128. mGrassPool = NULL;
  6129. break;
  6130. case LLDrawPool::POOL_FULLBRIGHT:
  6131. llassert(mFullbrightPool == poolp);
  6132. mFullbrightPool = NULL;
  6133. break;
  6134. case LLDrawPool::POOL_INVISIBLE:
  6135. llassert(mInvisiblePool == poolp);
  6136. mInvisiblePool = NULL;
  6137. break;
  6138. case LLDrawPool::POOL_WL_SKY:
  6139. llassert(mWLSkyPool == poolp);
  6140. mWLSkyPool = NULL;
  6141. break;
  6142. case LLDrawPool::POOL_GLOW:
  6143. llassert(mGlowPool == poolp);
  6144. mGlowPool = NULL;
  6145. break;
  6146. case LLDrawPool::POOL_TREE:
  6147. {
  6148. #if LL_DEBUG
  6149. bool found = mTreePools.erase((uintptr_t)poolp->getTexture());
  6150. llassert(found);
  6151. #else
  6152. mTreePools.erase((uintptr_t)poolp->getTexture());
  6153. #endif
  6154. break;
  6155. }
  6156. case LLDrawPool::POOL_TERRAIN:
  6157. {
  6158. #if LL_DEBUG
  6159. bool found = mTerrainPools.erase((uintptr_t)poolp->getTexture());
  6160. llassert(found);
  6161. #else
  6162. mTerrainPools.erase((uintptr_t)poolp->getTexture());
  6163. #endif
  6164. break;
  6165. }
  6166. case LLDrawPool::POOL_BUMP:
  6167. llassert(poolp == mBumpPool);
  6168. mBumpPool = NULL;
  6169. break;
  6170. case LLDrawPool::POOL_MATERIALS:
  6171. llassert(poolp == mMaterialsPool);
  6172. mMaterialsPool = NULL;
  6173. break;
  6174. case LLDrawPool::POOL_ALPHA_PRE_WATER:
  6175. llassert(poolp == mAlphaPoolPreWater);
  6176. mAlphaPoolPreWater = NULL;
  6177. break;
  6178. case LLDrawPool::POOL_ALPHA_POST_WATER:
  6179. llassert(poolp == mAlphaPoolPostWater);
  6180. mAlphaPoolPostWater = NULL;
  6181. break;
  6182. case LLDrawPool::POOL_ALPHA:
  6183. llassert(poolp == mAlphaPool);
  6184. mAlphaPool = NULL;
  6185. break;
  6186. case LLDrawPool::POOL_AVATAR:
  6187. case LLDrawPool::POOL_PUPPET:
  6188. break; // Do nothing
  6189. case LLDrawPool::POOL_SKY:
  6190. llassert(poolp == mSkyPool);
  6191. mSkyPool = NULL;
  6192. break;
  6193. case LLDrawPool::POOL_WATER:
  6194. llassert(poolp == mWaterPool);
  6195. mWaterPool = NULL;
  6196. break;
  6197. case LLDrawPool::POOL_MAT_PBR:
  6198. llassert(poolp == mPBROpaquePool);
  6199. mPBROpaquePool = NULL;
  6200. break;
  6201. case LLDrawPool::POOL_MAT_PBR_ALPHA_MASK:
  6202. llassert(poolp == mPBRAlphaMaskPool);
  6203. mPBRAlphaMaskPool = NULL;
  6204. break;
  6205. default:
  6206. llerrs << "Invalid pool type: " << poolp->getType() << llendl;
  6207. break;
  6208. }
  6209. }
  6210. void LLPipeline::resetDrawOrders()
  6211. {
  6212. // Iterate through all of the draw pools and rebuild them.
  6213. for (pool_set_t::iterator iter = mPools.begin(), end = mPools.end();
  6214. iter != end; ++iter)
  6215. {
  6216. LLDrawPool* poolp = *iter;
  6217. poolp->resetDrawOrders();
  6218. }
  6219. }
  6220. //============================================================================
  6221. // Once-per-frame setup of hardware lights, including sun/moon, avatar
  6222. // backlight, and up to 6 local lights
  6223. void LLPipeline::setupAvatarLights(bool for_edit)
  6224. {
  6225. LLLightState* lightp = gGL.getLight(1);
  6226. if (for_edit)
  6227. {
  6228. static const LLColor4 white_transparent(1.f, 1.f, 1.f, 0.f);
  6229. mHWLightColors[1] = white_transparent;
  6230. LLMatrix4 camera_mat = gViewerCamera.getModelview();
  6231. LLMatrix4 camera_rot(camera_mat.getMat3());
  6232. camera_rot.invert();
  6233. // w = 0 => directional light
  6234. static const LLVector4 light_pos_cam(-8.f, 0.25f, 10.f, 0.f);
  6235. LLVector4 light_pos = light_pos_cam * camera_rot;
  6236. light_pos.normalize();
  6237. lightp->setDiffuse(white_transparent);
  6238. lightp->setAmbient(LLColor4::black);
  6239. lightp->setSpecular(LLColor4::black);
  6240. lightp->setPosition(light_pos);
  6241. lightp->setConstantAttenuation(1.f);
  6242. lightp->setLinearAttenuation(0.f);
  6243. lightp->setQuadraticAttenuation(0.f);
  6244. lightp->setSpotExponent(0.f);
  6245. lightp->setSpotCutoff(180.f);
  6246. }
  6247. else
  6248. {
  6249. mHWLightColors[1] = LLColor4::black;
  6250. lightp->setDiffuse(LLColor4::black);
  6251. lightp->setAmbient(LLColor4::black);
  6252. lightp->setSpecular(LLColor4::black);
  6253. }
  6254. }
  6255. static F32 calc_light_dist(LLVOVolume* lightvolp, const LLVector3& cam_pos,
  6256. F32 max_dist)
  6257. {
  6258. if (lightvolp->getLightIntensity() < .001f)
  6259. {
  6260. return max_dist;
  6261. }
  6262. if (lightvolp->isSelected())
  6263. {
  6264. return 0.f; // Selected lights get highest priority
  6265. }
  6266. F32 radius = lightvolp->getLightRadius();
  6267. F32 dist = dist_vec(lightvolp->getRenderPosition(), cam_pos) - radius;
  6268. if (lightvolp->mDrawable.notNull() &&
  6269. lightvolp->mDrawable->isState(LLDrawable::ACTIVE))
  6270. {
  6271. // Moving lights get a little higher priority (too much causes
  6272. // artifacts)
  6273. dist -= radius * 0.25f;
  6274. }
  6275. return llclamp(dist, 0.f, max_dist);
  6276. }
  6277. void LLPipeline::calcNearbyLights(LLCamera& camera)
  6278. {
  6279. if (sReflectionRender || sRenderingHUDs || sAvatarPreviewRender ||
  6280. gCubeSnapshot)
  6281. {
  6282. return;
  6283. }
  6284. if (RenderLocalLightCount)
  6285. {
  6286. // mNearbyLight (and all light_set_t's) are sorted such that
  6287. // begin() == the closest light and rbegin() == the farthest light
  6288. constexpr S32 MAX_LOCAL_LIGHTS = 6;
  6289. LLVector3 cam_pos = camera.getOrigin();
  6290. F32 max_dist;
  6291. if (sRenderDeferred)
  6292. {
  6293. max_dist = RenderFarClip;
  6294. }
  6295. else
  6296. {
  6297. max_dist = llmax(RenderFarClip, LIGHT_MAX_RADIUS * 4.f);
  6298. }
  6299. // UPDATE THE EXISTING NEARBY LIGHTS
  6300. light_set_t cur_nearby_lights;
  6301. for (light_set_t::iterator iter = mNearbyLights.begin(),
  6302. end = mNearbyLights.end();
  6303. iter != end; ++iter)
  6304. {
  6305. const Light* lightp = &(*iter);
  6306. if (!lightp) continue; // Paranoia
  6307. LLDrawable* drawablep = lightp->drawable;
  6308. if (!drawablep) continue; // Paranoia
  6309. LLVOVolume* lightvolp = drawablep->getVOVolume();
  6310. if (!lightvolp || !drawablep->isState(LLDrawable::LIGHT))
  6311. {
  6312. drawablep->clearState(LLDrawable::NEARBY_LIGHT);
  6313. continue;
  6314. }
  6315. if (lightp->fade <= -LIGHT_FADE_TIME)
  6316. {
  6317. drawablep->clearState(LLDrawable::NEARBY_LIGHT);
  6318. continue;
  6319. }
  6320. if (!sRenderAttachedLights && lightvolp->isAttachment())
  6321. {
  6322. drawablep->clearState(LLDrawable::NEARBY_LIGHT);
  6323. continue;
  6324. }
  6325. LLVOAvatar* avp = lightvolp->getAvatar();
  6326. if (avp && avp->isVisuallyMuted())
  6327. {
  6328. drawablep->clearState(LLDrawable::NEARBY_LIGHT);
  6329. continue;
  6330. }
  6331. F32 dist = calc_light_dist(lightvolp, cam_pos, max_dist);
  6332. F32 fade = lightp->fade;
  6333. // Note: actual fade gets decreased/increased by setupHWLights()
  6334. // and lightp->fade value is 'time' (positive for light to become
  6335. // visible as value increases, negative for fading out).
  6336. if (dist < max_dist)
  6337. {
  6338. if (fade < 0.f)
  6339. {
  6340. // Mark light to fade in: if fade was -LIGHT_FADE_TIME,
  6341. // it was fully invisible, if negative it was fully visible
  6342. // and visibility goes up from 0 to LIGHT_FADE_TIME.
  6343. fade += LIGHT_FADE_TIME;
  6344. }
  6345. }
  6346. // Mark light to fade out. Visibility goes down from -0 to
  6347. // -LIGHT_FADE_TIME.
  6348. else if (fade >= LIGHT_FADE_TIME)
  6349. {
  6350. fade = -0.0001f; // Was fully visible
  6351. }
  6352. else if (fade >= 0.f)
  6353. {
  6354. // 0.75 visible light should stay 0.75 visible, but should
  6355. // reverse direction.
  6356. fade -= LIGHT_FADE_TIME;
  6357. }
  6358. cur_nearby_lights.emplace(drawablep, dist, fade);
  6359. }
  6360. mNearbyLights.swap(cur_nearby_lights);
  6361. // FIND NEW LIGHTS THAT ARE IN RANGE
  6362. light_set_t new_nearby_lights;
  6363. for (LLDrawable::draw_set_t::iterator iter = mLights.begin(),
  6364. end = mLights.end();
  6365. iter != end; ++iter)
  6366. {
  6367. LLDrawable* drawablep = *iter;
  6368. LLVOVolume* lightvolp = drawablep->getVOVolume();
  6369. if (!lightvolp || drawablep->isState(LLDrawable::NEARBY_LIGHT) ||
  6370. lightvolp->isHUDAttachment())
  6371. {
  6372. continue;
  6373. }
  6374. if (!sRenderAttachedLights && lightvolp->isAttachment())
  6375. {
  6376. continue;
  6377. }
  6378. LLVOAvatar* avp = lightvolp->getAvatar();
  6379. if (avp && avp->isVisuallyMuted())
  6380. {
  6381. drawablep->clearState(LLDrawable::NEARBY_LIGHT);
  6382. continue;
  6383. }
  6384. F32 dist = calc_light_dist(lightvolp, cam_pos, max_dist);
  6385. if (dist >= max_dist)
  6386. {
  6387. continue;
  6388. }
  6389. new_nearby_lights.emplace(drawablep, dist, 0.f);
  6390. if (!sRenderDeferred &&
  6391. new_nearby_lights.size() > (U32)MAX_LOCAL_LIGHTS)
  6392. {
  6393. new_nearby_lights.erase(--new_nearby_lights.end());
  6394. const Light& last = *new_nearby_lights.rbegin();
  6395. max_dist = last.dist;
  6396. }
  6397. }
  6398. // INSERT ANY NEW LIGHTS
  6399. for (light_set_t::iterator iter = new_nearby_lights.begin(),
  6400. end = new_nearby_lights.end();
  6401. iter != end; ++iter)
  6402. {
  6403. const Light* lightp = &(*iter);
  6404. if (sRenderDeferred ||
  6405. mNearbyLights.size() < (U32)MAX_LOCAL_LIGHTS)
  6406. {
  6407. mNearbyLights.emplace(*lightp);
  6408. ((LLDrawable*)lightp->drawable)->setState(LLDrawable::NEARBY_LIGHT);
  6409. continue;
  6410. }
  6411. // Crazy cast so that we can overwrite the fade value even though
  6412. // gcc enforces sets as const (fade value does not affect sort so
  6413. // this is safe)
  6414. Light* farthestp = const_cast<Light*>(&(*(mNearbyLights.rbegin())));
  6415. if (lightp->dist >= farthestp->dist)
  6416. {
  6417. break; // None of the other lights are closer
  6418. }
  6419. // This is a mess, but for now it needs to be in sync with fade
  6420. // code above. Ex: code above detects distance < max, sets fade
  6421. // time to positive, this code then detects closer lights and sets
  6422. // fade time negative, fully compensating for the code above.
  6423. if (farthestp->fade >= LIGHT_FADE_TIME)
  6424. {
  6425. farthestp->fade = -0.0001f; // Was fully visible
  6426. }
  6427. else if (farthestp->fade >= 0.f)
  6428. {
  6429. farthestp->fade -= LIGHT_FADE_TIME;
  6430. }
  6431. }
  6432. // Mark nearby lights not-removable.
  6433. for (light_set_t::iterator iter = mNearbyLights.begin(),
  6434. end = mNearbyLights.end();
  6435. iter != end; ++iter)
  6436. {
  6437. const Light* lightp = &(*iter);
  6438. ((LLViewerOctreeEntryData*)lightp->drawable)->setVisible();
  6439. }
  6440. }
  6441. }
  6442. // This method allows to read the sky values only once per frame and caches
  6443. // them for use by the various pipeline methods called during that frame. HB
  6444. void LLPipeline::cacheEnvironment()
  6445. {
  6446. const LLSettingsSky::ptr_t& skyp = gEnvironment.getCurrentSky();
  6447. if (!skyp) return; // Paranoia
  6448. // Ambient
  6449. static LLCachedControl<U32> reflections(gSavedSettings,
  6450. "RenderReflectionProbes");
  6451. static LLCachedControl<bool> adjust(gSavedSettings,
  6452. "RenderSkyAutoAdjustLegacy");
  6453. static LLCachedControl<F32> adj_scale(gSavedSettings,
  6454. "RenderSkyAutoAdjustSunColorScale");
  6455. mTotalAmbient = skyp->getTotalAmbient();
  6456. if (gUsePBRShaders)
  6457. {
  6458. // Cache the sky settings values for the reflection probes ambiance and
  6459. // the gamma. Note: when RenderReflectionProbes is 0, we must use 1.f
  6460. // for the probe ambiance, else the rendering is way too dark. HB
  6461. mProbeAmbiance =
  6462. reflections ? skyp->getReflectionProbeAmbiance(adjust) : 1.f;
  6463. mSkyGamma = skyp->getGamma();
  6464. }
  6465. mIsSunUp = skyp->getIsSunUp();
  6466. mIsMoonUp = skyp->getIsMoonUp();
  6467. // Prevent underlighting from having neither lightsource facing us
  6468. if (!mIsSunUp && !mIsMoonUp)
  6469. {
  6470. mSunDir.set(0.f, 1.f, 0.f, 0.f);
  6471. mMoonDir.set(0.f, 1.f, 0.f, 0.f);
  6472. mSunLightColor.setToBlack();
  6473. mMoonLightColor.setToBlack();
  6474. }
  6475. else
  6476. {
  6477. mSunDir.set(skyp->getSunDirection(), 0.f);
  6478. mMoonDir.set(skyp->getMoonDirection(), 0.f);
  6479. mSunLightColor.set(skyp->getSunlightColor(), 1.f);
  6480. mMoonLightColor.set(skyp->getMoonlightColor(), 1.f);
  6481. }
  6482. // Sun or Moon (All objects)
  6483. F32 max_color = llmax(mSunLightColor.mV[0], mSunLightColor.mV[1],
  6484. mSunLightColor.mV[2]);
  6485. if (max_color > 1.f)
  6486. {
  6487. mSunLightColor *= 1.f / max_color;
  6488. }
  6489. mSunLightColor.clamp();
  6490. mSunLightColorScaled = mSunLightColor;
  6491. if (gUsePBRShaders && adjust && skyp->canAutoAdjust())
  6492. {
  6493. mSunLightColorScaled *= adj_scale;
  6494. }
  6495. max_color = llmax(mMoonLightColor.mV[0], mMoonLightColor.mV[1],
  6496. mMoonLightColor.mV[2]);
  6497. if (max_color > 1.f)
  6498. {
  6499. mMoonLightColor *= 1.f / max_color;
  6500. }
  6501. mMoonLightColor.clamp();
  6502. // Also cache this for use by render pipeline and draw pools.
  6503. LLViewerRegion* regionp = gAgent.getRegion();
  6504. mWaterHeight = regionp ? regionp->getWaterHeight() : 0.f;
  6505. mEyeAboveWater = gViewerCamera.getOrigin().mV[VZ] - mWaterHeight;
  6506. }
  6507. void LLPipeline::setupHWLights()
  6508. {
  6509. if (sRenderingHUDs || sAvatarPreviewRender)
  6510. {
  6511. return;
  6512. }
  6513. gGL.setAmbientLightColor(mTotalAmbient);
  6514. // Darken local lights when probe ambiance is above 1
  6515. F32 light_scale = gCubeSnapshot ? mReflectionMapManager.mLightScale : 1.f;
  6516. LLLightState* lightp = gGL.getLight(0);
  6517. lightp->setPosition(mIsSunUp ? mSunDir : mMoonDir);
  6518. LLColor4 light_diffuse = mIsSunUp ? mSunLightColor : mMoonLightColor;
  6519. mHWLightColors[0] = light_diffuse;
  6520. lightp->setDiffuse(light_diffuse);
  6521. lightp->setSunPrimary(mIsSunUp);
  6522. lightp->setDiffuseB(mMoonLightColor);
  6523. lightp->setAmbient(mTotalAmbient);
  6524. lightp->setSpecular(LLColor4::black);
  6525. lightp->setConstantAttenuation(1.f);
  6526. lightp->setLinearAttenuation(0.f);
  6527. lightp->setQuadraticAttenuation(0.f);
  6528. lightp->setSpotExponent(0.f);
  6529. lightp->setSpotCutoff(180.f);
  6530. // Nearby lights = LIGHT 2-7
  6531. S32 cur_light = 2;
  6532. if (RenderLocalLightCount)
  6533. {
  6534. for (light_set_t::iterator iter = mNearbyLights.begin(),
  6535. end = mNearbyLights.end();
  6536. iter != end; ++iter)
  6537. {
  6538. LLDrawable* drawablep = iter->drawable;
  6539. LLVOVolume* volp = drawablep->getVOVolume();
  6540. if (!volp)
  6541. {
  6542. continue;
  6543. }
  6544. bool is_attachment = volp->isAttachment();
  6545. if (is_attachment && !sRenderAttachedLights)
  6546. {
  6547. continue;
  6548. }
  6549. const LLViewerObject* objp = drawablep->getVObj();
  6550. if (objp)
  6551. {
  6552. LLVOAvatar* avp = is_attachment ? objp->getAvatar()
  6553. : NULL;
  6554. if (avp && !avp->isSelf() &&
  6555. (avp->isInMuteList() || avp->isTooComplex()))
  6556. {
  6557. continue;
  6558. }
  6559. }
  6560. // Send linear light color to shader
  6561. LLColor4 light_color = volp->getLightLinearColor() * light_scale;
  6562. light_color.mV[3] = 0.f;
  6563. F32 fade = iter->fade;
  6564. if (fade < LIGHT_FADE_TIME)
  6565. {
  6566. constexpr F32 LIGHT_FADE_TIME_INV = 1.f / LIGHT_FADE_TIME;
  6567. // Fade in/out light
  6568. if (fade >= 0.f)
  6569. {
  6570. fade *= LIGHT_FADE_TIME_INV;
  6571. ((Light*)(&(*iter)))->fade += gFrameIntervalSeconds;
  6572. }
  6573. else
  6574. {
  6575. fade = 1.f + fade * LIGHT_FADE_TIME_INV;
  6576. ((Light*)(&(*iter)))->fade -= gFrameIntervalSeconds;
  6577. }
  6578. fade = llclamp(fade, 0.f, 1.f);
  6579. light_color *= fade;
  6580. }
  6581. if (light_color.lengthSquared() < 0.001f)
  6582. {
  6583. continue;
  6584. }
  6585. F32 adjusted_radius = volp->getLightRadius();
  6586. if (sRenderDeferred)
  6587. {
  6588. adjusted_radius *= 1.5f;
  6589. }
  6590. if (adjusted_radius <= 0.001f)
  6591. {
  6592. continue;
  6593. }
  6594. LLVector4 light_pos_gl(volp->getRenderPosition(), 1.f);
  6595. // Why this magic ? Probably trying to match a historic behavior:
  6596. F32 x = 3.f * (1.f + volp->getLightFalloff(2.f));
  6597. F32 linatten = x / adjusted_radius;
  6598. mHWLightColors[cur_light] = light_color;
  6599. lightp = gGL.getLight(cur_light);
  6600. lightp->setPosition(light_pos_gl);
  6601. lightp->setDiffuse(light_color);
  6602. lightp->setAmbient(LLColor4::black);
  6603. lightp->setConstantAttenuation(0.f);
  6604. lightp->setLinearAttenuation(linatten);
  6605. lightp->setSize(volp->getLightRadius() * 1.5f);
  6606. F32 fall_off = volp->getLightFalloff(0.5f);
  6607. lightp->setFalloff(fall_off);
  6608. if (sRenderDeferred)
  6609. {
  6610. // Get falloff to match for forward deferred rendering lights
  6611. lightp->setQuadraticAttenuation(1.f + fall_off);
  6612. }
  6613. else
  6614. {
  6615. lightp->setQuadraticAttenuation(0);
  6616. }
  6617. if (volp->isLightSpotlight() && // Directional (spot-)light
  6618. // These are only rendered as GL spotlights if we are in
  6619. // deferred rendering mode *or* the setting forces them on:
  6620. (sRenderDeferred || RenderSpotLightsInNondeferred))
  6621. {
  6622. LLQuaternion quat = volp->getRenderRotation();
  6623. // This matches deferred rendering's object light direction:
  6624. LLVector3 at_axis(0.f, 0.f, -1.f);
  6625. at_axis *= quat;
  6626. lightp->setSpotDirection(at_axis);
  6627. lightp->setSpotCutoff(90.f);
  6628. lightp->setSpotExponent(2.f);
  6629. LLVector3 spot_params = volp->getSpotLightParams();
  6630. const LLColor4 specular(0.f, 0.f, 0.f, spot_params[2]);
  6631. lightp->setSpecular(specular);
  6632. }
  6633. else // Omnidirectional (point) light
  6634. {
  6635. lightp->setSpotExponent(0.f);
  6636. lightp->setSpotCutoff(180.f);
  6637. // We use z = 1.f as a cheap hack for the shaders to know that
  6638. // this is omnidirectional rather than a spotlight
  6639. lightp->setSpecular(LLColor4(0.f, 0.f, 1.f, 0.f));
  6640. }
  6641. if (++cur_light >= 8)
  6642. {
  6643. break; // safety
  6644. }
  6645. }
  6646. }
  6647. for ( ; cur_light < 8; ++cur_light)
  6648. {
  6649. mHWLightColors[cur_light] = LLColor4::black;
  6650. lightp = gGL.getLight(cur_light);
  6651. lightp->setSunPrimary(true);
  6652. lightp->setDiffuse(LLColor4::black);
  6653. lightp->setAmbient(LLColor4::black);
  6654. lightp->setSpecular(LLColor4::black);
  6655. }
  6656. static LLCachedControl<bool> customize_lighting(gSavedSettings,
  6657. "AvatarCustomizeLighting");
  6658. if (customize_lighting && isAgentAvatarValid() &&
  6659. gAgentAvatarp->mSpecialRenderMode == 3)
  6660. {
  6661. LLVector3 light_pos(gViewerCamera.getOrigin());
  6662. LLVector4 light_pos_gl(light_pos, 1.f);
  6663. F32 light_radius = 16.f;
  6664. F32 x = 3.f;
  6665. F32 linatten = x / light_radius; // % of brightness at radius
  6666. lightp = gGL.getLight(2);
  6667. LLColor4 light_color;
  6668. light_color = LLColor4::white;
  6669. lightp->setDiffuseB(light_color * 0.25f);
  6670. mHWLightColors[2] = light_color;
  6671. lightp->setPosition(light_pos_gl);
  6672. lightp->setDiffuse(light_color);
  6673. lightp->setDiffuseB(light_color * 0.25f);
  6674. lightp->setAmbient(LLColor4::black);
  6675. lightp->setSpecular(LLColor4::black);
  6676. lightp->setQuadraticAttenuation(0.f);
  6677. lightp->setConstantAttenuation(0.f);
  6678. lightp->setLinearAttenuation(linatten);
  6679. lightp->setSpotExponent(0.f);
  6680. lightp->setSpotCutoff(180.f);
  6681. }
  6682. for (S32 i = 0; i < 8; ++i)
  6683. {
  6684. gGL.getLight(i)->disable();
  6685. }
  6686. mLightMask = 0;
  6687. }
  6688. void LLPipeline::enableLights(U32 mask)
  6689. {
  6690. if (!RenderLocalLightCount)
  6691. {
  6692. mask &= 0xf003; // Sun and backlight only (and fullbright bit)
  6693. }
  6694. if (mLightMask != mask)
  6695. {
  6696. if (mask)
  6697. {
  6698. for (S32 i = 0; i < 8; ++i)
  6699. {
  6700. LLLightState* lightp = gGL.getLight(i);
  6701. if (mask & (1 << i))
  6702. {
  6703. lightp->enable();
  6704. lightp->setDiffuse(mHWLightColors[i]);
  6705. }
  6706. else
  6707. {
  6708. lightp->disable();
  6709. lightp->setDiffuse(LLColor4::black);
  6710. }
  6711. }
  6712. }
  6713. mLightMask = mask;
  6714. }
  6715. }
  6716. void LLPipeline::enableLightsStatic()
  6717. {
  6718. constexpr U32 mask = 0xff & ~2;
  6719. enableLights(mask);
  6720. }
  6721. void LLPipeline::enableLightsDynamic()
  6722. {
  6723. U32 mask = 0xff & (~2); // Local lights
  6724. enableLights(mask);
  6725. if (isAgentAvatarValid() && RenderLocalLightCount)
  6726. {
  6727. if (gAgentAvatarp->mSpecialRenderMode == 0) // Normal
  6728. {
  6729. enableLightsAvatar();
  6730. }
  6731. else if (gAgentAvatarp->mSpecialRenderMode >= 1) // Anim preview
  6732. {
  6733. enableLightsAvatarEdit();
  6734. }
  6735. }
  6736. }
  6737. void LLPipeline::enableLightsAvatar()
  6738. {
  6739. setupAvatarLights(false);
  6740. enableLights(0xff); // All lights
  6741. }
  6742. void LLPipeline::enableLightsPreview()
  6743. {
  6744. disableLights();
  6745. gGL.setAmbientLightColor(PreviewAmbientColor);
  6746. LLLightState* lightp = gGL.getLight(1);
  6747. lightp->enable();
  6748. lightp->setPosition(LLVector4(PreviewDirection0, 0.f));
  6749. lightp->setDiffuse(PreviewDiffuse0);
  6750. lightp->setAmbient(PreviewAmbientColor);
  6751. lightp->setSpecular(PreviewSpecular0);
  6752. lightp->setSpotExponent(0.f);
  6753. lightp->setSpotCutoff(180.f);
  6754. lightp = gGL.getLight(2);
  6755. lightp->enable();
  6756. lightp->setPosition(LLVector4(PreviewDirection1, 0.f));
  6757. lightp->setDiffuse(PreviewDiffuse1);
  6758. lightp->setAmbient(PreviewAmbientColor);
  6759. lightp->setSpecular(PreviewSpecular1);
  6760. lightp->setSpotExponent(0.f);
  6761. lightp->setSpotCutoff(180.f);
  6762. lightp = gGL.getLight(3);
  6763. lightp->enable();
  6764. lightp->setPosition(LLVector4(PreviewDirection2, 0.f));
  6765. lightp->setDiffuse(PreviewDiffuse2);
  6766. lightp->setAmbient(PreviewAmbientColor);
  6767. lightp->setSpecular(PreviewSpecular2);
  6768. lightp->setSpotExponent(0.f);
  6769. lightp->setSpotCutoff(180.f);
  6770. }
  6771. void LLPipeline::enableLightsAvatarEdit()
  6772. {
  6773. U32 mask = 0x2002; // Avatar backlight only, set ambient
  6774. setupAvatarLights(true);
  6775. enableLights(mask);
  6776. gGL.setAmbientLightColor(LLColor4(0.7f, 0.6f, 0.3f, 1.f));
  6777. }
  6778. void LLPipeline::enableLightsFullbright()
  6779. {
  6780. U32 mask = 0x1000; // Non-0 mask, set ambient
  6781. enableLights(mask);
  6782. }
  6783. void LLPipeline::disableLights()
  6784. {
  6785. enableLights(0); // No lighting (full bright)
  6786. }
  6787. #if LL_DEBUG && 0
  6788. void LLPipeline::findReferences(LLDrawable* drawablep)
  6789. {
  6790. if (mLights.find(drawablep) != mLights.end())
  6791. {
  6792. llinfos << "In mLights" << llendl;
  6793. }
  6794. if (std::find(mMovedList.begin(), mMovedList.end(),
  6795. drawablep) != mMovedList.end())
  6796. {
  6797. llinfos << "In mMovedList" << llendl;
  6798. }
  6799. if (std::find(mShiftList.begin(), mShiftList.end(),
  6800. drawablep) != mShiftList.end())
  6801. {
  6802. llinfos << "In mShiftList" << llendl;
  6803. }
  6804. if (mRetexturedList.find(drawablep) != mRetexturedList.end())
  6805. {
  6806. llinfos << "In mRetexturedList" << llendl;
  6807. }
  6808. if (std::find(mBuildQ.begin(), mBuildQ.end(), drawablep) != mBuildQ.end())
  6809. {
  6810. llinfos << "In mBuildQ" << llendl;
  6811. }
  6812. S32 count;
  6813. count = gObjectList.findReferences(drawablep);
  6814. if (count)
  6815. {
  6816. llinfos << "In other drawables: " << count << " references" << llendl;
  6817. }
  6818. }
  6819. #endif
  6820. bool LLPipeline::verify()
  6821. {
  6822. bool ok = true;
  6823. for (pool_set_t::iterator iter = mPools.begin(), end = mPools.end();
  6824. iter != end; ++iter)
  6825. {
  6826. LLDrawPool* poolp = *iter;
  6827. if (!poolp->verify())
  6828. {
  6829. ok = false;
  6830. }
  6831. }
  6832. if (!ok)
  6833. {
  6834. llwarns << "Pipeline verify failed !" << llendl;
  6835. }
  6836. return ok;
  6837. }
  6838. //////////////////////////////
  6839. // Collision detection
  6840. ///////////////////////////////////////////////////////////////////////////////
  6841. /**
  6842. * A method to compute a ray-AABB intersection.
  6843. * Original code by Andrew Woo, from "Graphics Gems", Academic Press, 1990
  6844. * Optimized code by Pierre Terdiman, 2000 (~20-30% faster on my Celeron 500)
  6845. * Epsilon value added by Klaus Hartmann. (discarding it saves a few cycles only)
  6846. *
  6847. * Hence this version is faster as well as more robust than the original one.
  6848. *
  6849. * Should work provided:
  6850. * 1) the integer representation of 0.f is 0x00000000
  6851. * 2) the sign bit of the float is the most significant one
  6852. *
  6853. * Report bugs: [email protected]
  6854. *
  6855. * \param aabb [in] the axis-aligned bounding box
  6856. * \param origin [in] ray origin
  6857. * \param dir [in] ray direction
  6858. * \param coord [out] impact coordinates
  6859. * \return true if ray intersects AABB
  6860. */
  6861. ///////////////////////////////////////////////////////////////////////////////
  6862. //#define RAYAABB_EPSILON 0.00001f
  6863. #define IR(x) ((U32&)x)
  6864. bool LLRayAABB(const LLVector3& center, const LLVector3& size,
  6865. const LLVector3& origin, const LLVector3& dir,
  6866. LLVector3& coord, F32 epsilon)
  6867. {
  6868. bool inside = true;
  6869. LLVector3 MinB = center - size;
  6870. LLVector3 MaxB = center + size;
  6871. LLVector3 MaxT;
  6872. MaxT.mV[VX] = MaxT.mV[VY] = MaxT.mV[VZ] = -1.f;
  6873. // Find candidate planes.
  6874. for (U32 i = 0; i < 3; ++i)
  6875. {
  6876. if (origin.mV[i] < MinB.mV[i])
  6877. {
  6878. coord.mV[i] = MinB.mV[i];
  6879. inside = false;
  6880. // Calculate T distances to candidate planes
  6881. if (IR(dir.mV[i]))
  6882. {
  6883. MaxT.mV[i] = (MinB.mV[i] - origin.mV[i]) / dir.mV[i];
  6884. }
  6885. }
  6886. else if (origin.mV[i] > MaxB.mV[i])
  6887. {
  6888. coord.mV[i] = MaxB.mV[i];
  6889. inside = false;
  6890. // Calculate T distances to candidate planes
  6891. if (IR(dir.mV[i]))
  6892. {
  6893. MaxT.mV[i] = (MaxB.mV[i] - origin.mV[i]) / dir.mV[i];
  6894. }
  6895. }
  6896. }
  6897. // Ray origin inside bounding box
  6898. if (inside)
  6899. {
  6900. coord = origin;
  6901. return true;
  6902. }
  6903. // Get largest of the maxT's for final choice of intersection
  6904. U32 WhichPlane = 0;
  6905. if (MaxT.mV[1] > MaxT.mV[WhichPlane])
  6906. {
  6907. WhichPlane = 1;
  6908. }
  6909. if (MaxT.mV[2] > MaxT.mV[WhichPlane])
  6910. {
  6911. WhichPlane = 2;
  6912. }
  6913. // Check final candidate actually inside box
  6914. if (IR(MaxT.mV[WhichPlane]) & 0x80000000)
  6915. {
  6916. return false;
  6917. }
  6918. for (U32 i = 0; i < 3; ++i)
  6919. {
  6920. if (i != WhichPlane)
  6921. {
  6922. coord.mV[i] = origin.mV[i] + MaxT.mV[WhichPlane] * dir.mV[i];
  6923. if (epsilon > 0)
  6924. {
  6925. if (coord.mV[i] < MinB.mV[i] - epsilon ||
  6926. coord.mV[i] > MaxB.mV[i] + epsilon)
  6927. {
  6928. return false;
  6929. }
  6930. }
  6931. else if (coord.mV[i] < MinB.mV[i] || coord.mV[i] > MaxB.mV[i])
  6932. {
  6933. return false;
  6934. }
  6935. }
  6936. }
  6937. return true; // ray hits box
  6938. }
  6939. void LLPipeline::setLight(LLDrawable* drawablep, bool is_light)
  6940. {
  6941. if (drawablep)
  6942. {
  6943. if (is_light)
  6944. {
  6945. mLights.emplace(drawablep);
  6946. drawablep->setState(LLDrawable::LIGHT);
  6947. }
  6948. else
  6949. {
  6950. drawablep->clearState(LLDrawable::LIGHT);
  6951. mLights.erase(drawablep);
  6952. }
  6953. }
  6954. }
  6955. //static
  6956. void LLPipeline::toggleRenderType(U32 type)
  6957. {
  6958. //MK
  6959. // Force the render type to true if our vision is restricted
  6960. if (gRLenabled &&
  6961. (type == RENDER_TYPE_AVATAR || type == RENDER_TYPE_PUPPET) &&
  6962. gRLInterface.mVisionRestricted)
  6963. {
  6964. gPipeline.mRenderTypeEnabled[type] = true;
  6965. return;
  6966. }
  6967. //mk
  6968. gPipeline.mRenderTypeEnabled[type] = !gPipeline.mRenderTypeEnabled[type];
  6969. if (type == RENDER_TYPE_WATER)
  6970. {
  6971. gPipeline.mRenderTypeEnabled[RENDER_TYPE_VOIDWATER] =
  6972. !gPipeline.mRenderTypeEnabled[RENDER_TYPE_VOIDWATER];
  6973. }
  6974. }
  6975. //static
  6976. void LLPipeline::toggleRenderTypeControl(void* data)
  6977. {
  6978. U32 type = (U32)(intptr_t)data;
  6979. U32 bit = 1 << type;
  6980. if (gPipeline.hasRenderType(type))
  6981. {
  6982. llinfos << "Toggling render type mask " << std::hex << bit << " off"
  6983. << std::dec << llendl;
  6984. }
  6985. else
  6986. {
  6987. llinfos << "Toggling render type mask " << std::hex << bit << " on"
  6988. << std::dec << llendl;
  6989. }
  6990. gPipeline.toggleRenderType(type);
  6991. }
  6992. //static
  6993. bool LLPipeline::hasRenderTypeControl(void* data)
  6994. {
  6995. U32 type = (U32)(intptr_t)data;
  6996. return gPipeline.hasRenderType(type);
  6997. }
  6998. // Allows UI items labeled "Hide foo" instead of "Show foo"
  6999. //static
  7000. bool LLPipeline::toggleRenderTypeControlNegated(void* data)
  7001. {
  7002. U32 type = (U32)(intptr_t)data;
  7003. return !gPipeline.hasRenderType(type);
  7004. }
  7005. //static
  7006. void LLPipeline::toggleRenderDebug(void* data)
  7007. {
  7008. U32 bit = (U32)(intptr_t)data;
  7009. //MK
  7010. if (gRLenabled && gRLInterface.mVisionRestricted)
  7011. {
  7012. bit = 0;
  7013. }
  7014. //mk
  7015. if (gPipeline.hasRenderDebugMask(bit))
  7016. {
  7017. llinfos << "Toggling render debug mask " << std::hex << bit << " off"
  7018. << std::dec << llendl;
  7019. }
  7020. else
  7021. {
  7022. llinfos << "Toggling render debug mask " << std::hex << bit << " on"
  7023. << std::dec << llendl;
  7024. }
  7025. gPipeline.mRenderDebugMask ^= bit;
  7026. }
  7027. //static
  7028. bool LLPipeline::toggleRenderDebugControl(void* data)
  7029. {
  7030. U32 bit = (U32)(intptr_t)data;
  7031. return gPipeline.hasRenderDebugMask(bit);
  7032. }
  7033. //static
  7034. void LLPipeline::toggleRenderDebugFeature(void* data)
  7035. {
  7036. U32 bit = (U32)(intptr_t)data;
  7037. gPipeline.mRenderDebugFeatureMask ^= bit;
  7038. }
  7039. //static
  7040. bool LLPipeline::toggleRenderDebugFeatureControl(void* data)
  7041. {
  7042. U32 bit = (U32)(intptr_t)data;
  7043. return gPipeline.hasRenderDebugFeatureMask(bit);
  7044. }
  7045. //static
  7046. void LLPipeline::setRenderDebugFeatureControl(U32 bit, bool value)
  7047. {
  7048. if (value)
  7049. {
  7050. gPipeline.mRenderDebugFeatureMask |= bit;
  7051. }
  7052. else
  7053. {
  7054. gPipeline.mRenderDebugFeatureMask &= !bit;
  7055. }
  7056. }
  7057. LLVOPartGroup* LLPipeline::lineSegmentIntersectParticle(const LLVector4a& start,
  7058. const LLVector4a& end,
  7059. LLVector4a* intersectp,
  7060. S32* face_hitp)
  7061. {
  7062. LLVector4a local_end = end;
  7063. LLVector4a position;
  7064. LLDrawable* drawablep = NULL;
  7065. for (LLWorld::region_list_t::const_iterator
  7066. iter = gWorld.getRegionList().begin(),
  7067. wend = gWorld.getRegionList().end();
  7068. iter != wend; ++iter)
  7069. {
  7070. LLViewerRegion* region = *iter;
  7071. if (!region) continue; // Paranoia
  7072. LLSpatialPartition* partp =
  7073. region->getSpatialPartition(LLViewerRegion::PARTITION_PARTICLE);
  7074. // PARTITION_PARTICLE cannot be NULL
  7075. if (hasRenderType(partp->mDrawableType))
  7076. {
  7077. LLDrawable* hitp = partp->lineSegmentIntersect(start, local_end,
  7078. true, false,
  7079. face_hitp,
  7080. &position);
  7081. if (hitp)
  7082. {
  7083. drawablep = hitp;
  7084. local_end = position;
  7085. }
  7086. }
  7087. }
  7088. LLVOPartGroup* partp = NULL;
  7089. if (drawablep)
  7090. {
  7091. // Make sure we are returning an LLVOPartGroup
  7092. partp = drawablep->getVObj().get()->asVOPartGroup();
  7093. }
  7094. if (intersectp)
  7095. {
  7096. *intersectp = position;
  7097. }
  7098. return partp;
  7099. }
  7100. LLViewerObject* LLPipeline::lineSegmentIntersectInWorld(const LLVector4a& start,
  7101. const LLVector4a& end,
  7102. bool pick_transparent,
  7103. bool pick_rigged,
  7104. S32* face_hitp,
  7105. S32* gltf_node_hitp,
  7106. S32* gltf_prim_hitp,
  7107. LLVector4a* intersectp,
  7108. LLVector2* tcoordp,
  7109. LLVector4a* normalp,
  7110. LLVector4a* tangentp)
  7111. {
  7112. LLDrawable* drawablep = NULL;
  7113. LLVector4a local_end = end;
  7114. LLVector4a position;
  7115. sPickAvatar = false; // !gToolMgr.inBuildMode();
  7116. // Only check these non-avatar partitions in a first step
  7117. static const U32 non_avatars[] =
  7118. {
  7119. LLViewerRegion::PARTITION_TERRAIN,
  7120. LLViewerRegion::PARTITION_TREE,
  7121. LLViewerRegion::PARTITION_GRASS,
  7122. LLViewerRegion::PARTITION_VOLUME,
  7123. LLViewerRegion::PARTITION_BRIDGE,
  7124. LLViewerRegion::PARTITION_PUPPET,
  7125. };
  7126. constexpr U32 non_avatars_count = LL_ARRAY_SIZE(non_avatars);
  7127. for (LLWorld::region_list_t::const_iterator
  7128. iter = gWorld.getRegionList().begin(),
  7129. wend = gWorld.getRegionList().end();
  7130. iter != wend; ++iter)
  7131. {
  7132. LLViewerRegion* regionp = *iter;
  7133. if (!regionp) continue; // Paranoia
  7134. for (U32 j = 0; j < non_avatars_count; ++j)
  7135. {
  7136. U32 type = non_avatars[j];
  7137. LLSpatialPartition* partp = regionp->getSpatialPartition(type);
  7138. // None of the partitions under PARTITION_VO_CACHE can be NULL
  7139. if (!hasRenderType(partp->mDrawableType))
  7140. {
  7141. continue;
  7142. }
  7143. LLDrawable* hitp =
  7144. partp->lineSegmentIntersect(start, local_end,
  7145. // Terrain, tree and grass cannot
  7146. // be transparent neither rigged !
  7147. // HB
  7148. pick_transparent && j >= 3,
  7149. pick_rigged && j >= 3,
  7150. face_hitp, &position, tcoordp,
  7151. normalp, tangentp);
  7152. if (hitp)
  7153. {
  7154. drawablep = hitp;
  7155. local_end = position;
  7156. }
  7157. }
  7158. }
  7159. if (!sPickAvatar)
  7160. {
  7161. // Save hit info in case we need to restore due to attachment override
  7162. LLVector4a local_normal;
  7163. LLVector4a local_tangent;
  7164. LLVector2 local_texcoord;
  7165. S32 local_face_hit = -1;
  7166. if (face_hitp)
  7167. {
  7168. local_face_hit = *face_hitp;
  7169. }
  7170. if (tcoordp)
  7171. {
  7172. local_texcoord = *tcoordp;
  7173. }
  7174. if (tangentp)
  7175. {
  7176. local_tangent = *tangentp;
  7177. }
  7178. else
  7179. {
  7180. local_tangent.clear();
  7181. }
  7182. if (normalp)
  7183. {
  7184. local_normal = *normalp;
  7185. }
  7186. else
  7187. {
  7188. local_normal.clear();
  7189. }
  7190. constexpr F32 ATTACHMENT_OVERRIDE_DIST = 0.1f;
  7191. // Check against avatars
  7192. sPickAvatar = true;
  7193. for (LLWorld::region_list_t::const_iterator
  7194. iter = gWorld.getRegionList().begin(),
  7195. wend = gWorld.getRegionList().end();
  7196. iter != wend; ++iter)
  7197. {
  7198. LLViewerRegion* regionp = *iter;
  7199. if (!regionp) continue; // Paranoia
  7200. LLSpatialPartition* partp =
  7201. regionp->getSpatialPartition(LLViewerRegion::PARTITION_AVATAR);
  7202. // Note: PARTITION_AVATAR cannot be NULL
  7203. if (!hasRenderType(partp->mDrawableType))
  7204. {
  7205. continue;
  7206. }
  7207. LLDrawable* hitp =
  7208. partp->lineSegmentIntersect(start, local_end, pick_transparent,
  7209. pick_rigged, face_hitp, &position,
  7210. tcoordp, normalp, tangentp);
  7211. if (hitp)
  7212. {
  7213. LLVector4a delta;
  7214. delta.setSub(position, local_end);
  7215. if (!drawablep || !drawablep->getVObj()->isAttachment() ||
  7216. delta.getLength3().getF32() > ATTACHMENT_OVERRIDE_DIST)
  7217. {
  7218. // Avatar overrides if previously hit drawable is not
  7219. // an attachment or attachment is far enough away from
  7220. // detected intersection
  7221. drawablep = hitp;
  7222. local_end = position;
  7223. }
  7224. else
  7225. {
  7226. // Prioritize attachments over avatars
  7227. position = local_end;
  7228. if (face_hitp)
  7229. {
  7230. *face_hitp = local_face_hit;
  7231. }
  7232. if (tcoordp)
  7233. {
  7234. *tcoordp = local_texcoord;
  7235. }
  7236. if (tangentp)
  7237. {
  7238. *tangentp = local_tangent;
  7239. }
  7240. if (normalp)
  7241. {
  7242. *normalp = local_normal;
  7243. }
  7244. }
  7245. }
  7246. }
  7247. }
  7248. // Check all avatar name tags
  7249. for (S32 i = 0, count = LLCharacter::sInstances.size(); i < count; ++i)
  7250. {
  7251. LLVOAvatar* avp = (LLVOAvatar*)LLCharacter::sInstances[i];
  7252. if (avp && avp->mNameText.notNull() &&
  7253. avp->mNameText->lineSegmentIntersect(start, local_end, position))
  7254. {
  7255. drawablep = avp->mDrawable;
  7256. local_end = position;
  7257. }
  7258. }
  7259. if (gUsePBRShaders)
  7260. {
  7261. S32 node_hit = -1;
  7262. S32 prim_hit = -1;
  7263. LLDrawable* hit_drawp =
  7264. LLGLTFSceneManager::lineSegmentIntersect(start, local_end,
  7265. pick_transparent,
  7266. pick_rigged, &node_hit,
  7267. &prim_hit, &position,
  7268. tcoordp, normalp,
  7269. tangentp);
  7270. if (hit_drawp)
  7271. {
  7272. drawablep = hit_drawp;
  7273. //local_end = position;
  7274. }
  7275. if (gltf_node_hitp)
  7276. {
  7277. *gltf_node_hitp = node_hit;
  7278. }
  7279. if (gltf_prim_hitp)
  7280. {
  7281. *gltf_prim_hitp = prim_hit;
  7282. }
  7283. }
  7284. if (intersectp)
  7285. {
  7286. *intersectp = position;
  7287. }
  7288. return drawablep ? drawablep->getVObj().get() : NULL;
  7289. }
  7290. LLViewerObject* LLPipeline::lineSegmentIntersectInHUD(const LLVector4a& start,
  7291. const LLVector4a& end,
  7292. bool pick_transparent,
  7293. S32* face_hitp,
  7294. LLVector4a* intersectp,
  7295. LLVector2* tcoordp,
  7296. LLVector4a* normalp,
  7297. LLVector4a* tangentp)
  7298. {
  7299. LLDrawable* drawablep = NULL;
  7300. for (LLWorld::region_list_t::const_iterator
  7301. iter = gWorld.getRegionList().begin(),
  7302. wend = gWorld.getRegionList().end();
  7303. iter != wend; ++iter)
  7304. {
  7305. LLViewerRegion* region = *iter;
  7306. bool toggle = false;
  7307. if (!hasRenderType(RENDER_TYPE_HUD))
  7308. {
  7309. toggleRenderType(RENDER_TYPE_HUD);
  7310. toggle = true;
  7311. }
  7312. LLSpatialPartition* partp =
  7313. region->getSpatialPartition(LLViewerRegion::PARTITION_HUD);
  7314. // PARTITION_HUD cannot be NULL
  7315. LLDrawable* hitp = partp->lineSegmentIntersect(start, end,
  7316. pick_transparent, false,
  7317. face_hitp, intersectp,
  7318. tcoordp, normalp,
  7319. tangentp);
  7320. if (hitp)
  7321. {
  7322. drawablep = hitp;
  7323. }
  7324. if (toggle)
  7325. {
  7326. toggleRenderType(RENDER_TYPE_HUD);
  7327. }
  7328. }
  7329. return drawablep ? drawablep->getVObj().get() : NULL;
  7330. }
  7331. LLSpatialPartition* LLPipeline::getSpatialPartition(LLViewerObject* objp)
  7332. {
  7333. if (objp)
  7334. {
  7335. LLViewerRegion* regionp = objp->getRegion();
  7336. if (regionp)
  7337. {
  7338. return regionp->getSpatialPartition(objp->getPartitionType());
  7339. }
  7340. }
  7341. return NULL;
  7342. }
  7343. void LLPipeline::resetVertexBuffers(LLDrawable* drawablep)
  7344. {
  7345. if (drawablep)
  7346. {
  7347. for (S32 i = 0, count = drawablep->getNumFaces(); i < count; ++i)
  7348. {
  7349. LLFace* facep = drawablep->getFace(i);
  7350. if (facep)
  7351. {
  7352. facep->clearVertexBuffer();
  7353. }
  7354. }
  7355. }
  7356. }
  7357. void LLPipeline::resetVertexBuffers()
  7358. {
  7359. mResetVertexBuffers = true;
  7360. updateRenderDeferred();
  7361. }
  7362. void LLPipeline::doResetVertexBuffers(bool forced)
  7363. {
  7364. if (!mResetVertexBuffers)
  7365. {
  7366. return;
  7367. }
  7368. // Wait for teleporting to finish
  7369. if (!forced && LLSpatialPartition::sTeleportRequested)
  7370. {
  7371. if (gAgent.getTeleportState() == LLAgent::TELEPORT_NONE)
  7372. {
  7373. // Teleporting aborted
  7374. LLSpatialPartition::sTeleportRequested = false;
  7375. mResetVertexBuffers = false;
  7376. }
  7377. return;
  7378. }
  7379. LL_FAST_TIMER(FTM_RESET_VB);
  7380. mResetVertexBuffers = false;
  7381. gGL.flush();
  7382. glFinish();
  7383. LLVertexBuffer::unbind();
  7384. // Delete our utility buffers
  7385. mDeferredVB = mGlowCombineVB = mCubeVB = mScreenTriangleVB = NULL;
  7386. for (LLWorld::region_list_t::const_iterator
  7387. iter = gWorld.getRegionList().begin(),
  7388. end = gWorld.getRegionList().end();
  7389. iter != end; ++iter)
  7390. {
  7391. LLViewerRegion* regionp = *iter;
  7392. for (U32 i = 0; i < LLViewerRegion::NUM_PARTITIONS; ++i)
  7393. {
  7394. LLSpatialPartition* partp = regionp->getSpatialPartition(i);
  7395. if (partp)
  7396. {
  7397. partp->resetVertexBuffers();
  7398. }
  7399. }
  7400. }
  7401. if (LLSpatialPartition::sTeleportRequested)
  7402. {
  7403. LLSpatialPartition::sTeleportRequested = false;
  7404. // Do not do this for the PBR renderer. HB
  7405. if (!gUsePBRShaders)
  7406. {
  7407. gWorld.clearAllVisibleObjects();
  7408. clearRebuildDrawables();
  7409. }
  7410. }
  7411. resetDrawOrders();
  7412. gSky.resetVertexBuffers();
  7413. gGL.resetVertexBuffer();
  7414. LLVertexBuffer::cleanupClass();
  7415. #if LL_DEBUG_VB_ALLOC
  7416. if (LLVertexBuffer::getGLCount())
  7417. {
  7418. llwarns << "VBO wipe failed: " << LLVertexBuffer::getGLCount()
  7419. << " buffers remaining." << llendl;
  7420. LLVertexBuffer::dumpInstances();
  7421. }
  7422. #endif
  7423. updateRenderDeferred();
  7424. LLVertexBuffer::initClass();
  7425. gGL.initVertexBuffer();
  7426. createAuxVBs(); // Recreate our utility buffers...
  7427. LLDrawPoolWater::restoreGL();
  7428. LLDrawPoolWLSky::restoreGL();
  7429. }
  7430. void LLPipeline::renderObjects(U32 type, U32 mask, bool texture,
  7431. bool batch_texture, bool rigged)
  7432. {
  7433. LL_TRACY_TIMER(TRC_RENDER_OBJECTS);
  7434. gGL.loadMatrix(gGLModelView);
  7435. gGLLastMatrix = NULL;
  7436. if (rigged)
  7437. {
  7438. mSimplePool->pushRiggedBatches(type + 1, mask, texture, batch_texture);
  7439. }
  7440. else
  7441. {
  7442. mSimplePool->pushBatches(type, mask, texture, batch_texture);
  7443. }
  7444. gGL.loadMatrix(gGLModelView);
  7445. gGLLastMatrix = NULL;
  7446. }
  7447. // Used only by the PBR renderer
  7448. void LLPipeline::renderGLTFObjects(U32 type, bool texture, bool rigged)
  7449. {
  7450. gGL.loadMatrix(gGLModelView);
  7451. gGLLastMatrix = NULL;
  7452. if (rigged)
  7453. {
  7454. LLRenderPass::pushRiggedGLTFBatches(type + 1, texture);
  7455. }
  7456. else
  7457. {
  7458. LLRenderPass::pushGLTFBatches(type, texture);
  7459. }
  7460. gGL.loadMatrix(gGLModelView);
  7461. gGLLastMatrix = NULL;
  7462. if (!rigged)
  7463. {
  7464. LLGLTFSceneManager::renderOpaque();
  7465. }
  7466. }
  7467. // Branched version for the PBR renderer
  7468. void LLPipeline::renderAlphaObjectsPBR(bool rigged)
  7469. {
  7470. LL_TRACY_TIMER(TRC_RENDER_ALPHA_OBJECTS);
  7471. gGL.loadMatrix(gGLModelView);
  7472. gGLLastMatrix = NULL;
  7473. constexpr U32 type = LLRenderPass::PASS_ALPHA;
  7474. const F32 width = LLRenderTarget::sCurResX;
  7475. LLVOAvatar* last_avatarp = NULL;
  7476. U64 last_hash = 0;
  7477. LLCullResult::drawinfo_list_t& draw_list = getRenderMap(type);
  7478. for (U32 i = 0, count = draw_list.size(); i < count; )
  7479. {
  7480. LLDrawInfo* paramsp = draw_list[i++];
  7481. // Draw info cache prefetching optimization.
  7482. if (i < count)
  7483. {
  7484. _mm_prefetch((char*)draw_list[i]->mVertexBuffer.get(),
  7485. _MM_HINT_NTA);
  7486. if (i + 1 < count)
  7487. {
  7488. _mm_prefetch((char*)draw_list[i + 1], _MM_HINT_NTA);
  7489. }
  7490. }
  7491. bool has_avatar = paramsp->mAvatar.notNull();
  7492. if (rigged != has_avatar)
  7493. {
  7494. // This pool contains both rigged and non-rigged DrawInfos. Only
  7495. // draw the objects we are interested in in this pass.
  7496. continue;
  7497. }
  7498. bool has_pbr_mat = paramsp->mGLTFMaterial.notNull();
  7499. LLGLSLShader* shaderp =
  7500. has_pbr_mat ? &gDeferredShadowGLTFAlphaBlendProgram
  7501. : &gDeferredShadowAlphaMaskProgram;
  7502. shaderp->bind(rigged);
  7503. shaderp = LLGLSLShader::sCurBoundShaderPtr;
  7504. shaderp->uniform1i(LLShaderMgr::SUN_UP_FACTOR, mIsSunUp);
  7505. shaderp->uniform1f(LLShaderMgr::DEFERRED_SHADOW_TARGET_WIDTH, width);
  7506. shaderp->setMinimumAlpha(ALPHA_BLEND_CUTOFF);
  7507. if (rigged && paramsp->mSkinInfo &&
  7508. (paramsp->mAvatar.get() != last_avatarp ||
  7509. paramsp->mSkinInfo->mHash != last_hash))
  7510. {
  7511. #if 0
  7512. if (!mSimplePool->uploadMatrixPalette(*paramsp))
  7513. {
  7514. continue;
  7515. }
  7516. #else
  7517. mSimplePool->uploadMatrixPalette(*paramsp);
  7518. #endif
  7519. last_avatarp = paramsp->mAvatar.get();
  7520. last_hash = paramsp->mSkinInfo->mHash;
  7521. }
  7522. if (has_pbr_mat)
  7523. {
  7524. LLRenderPass::pushGLTFBatch(*paramsp);
  7525. }
  7526. else
  7527. {
  7528. mSimplePool->pushBatch(*paramsp, 0, true, true);
  7529. }
  7530. }
  7531. gGL.loadMatrix(gGLModelView);
  7532. gGLLastMatrix = NULL;
  7533. }
  7534. void LLPipeline::renderAlphaObjects(bool rigged)
  7535. {
  7536. if (gUsePBRShaders)
  7537. {
  7538. renderAlphaObjectsPBR(rigged);
  7539. return;
  7540. }
  7541. LL_TRACY_TIMER(TRC_RENDER_ALPHA_OBJECTS);
  7542. gGL.loadMatrix(gGLModelView);
  7543. gGLLastMatrix = NULL;
  7544. constexpr U32 type = LLRenderPass::PASS_ALPHA;
  7545. constexpr U32 mask = LLVertexBuffer::MAP_VERTEX |
  7546. LLVertexBuffer::MAP_TEXCOORD0 |
  7547. LLVertexBuffer::MAP_COLOR |
  7548. LLVertexBuffer::MAP_TEXTURE_INDEX;
  7549. LLVOAvatar* last_avatarp = NULL;
  7550. U64 last_hash = 0;
  7551. LLCullResult::drawinfo_list_t& draw_list = getRenderMap(type);
  7552. for (U32 i = 0, count = draw_list.size(); i < count; )
  7553. {
  7554. LLDrawInfo* paramsp = draw_list[i++];
  7555. // Draw info cache prefetching optimization.
  7556. if (i < count)
  7557. {
  7558. _mm_prefetch((char*)draw_list[i]->mVertexBuffer.get(),
  7559. _MM_HINT_NTA);
  7560. if (i + 1 < count)
  7561. {
  7562. _mm_prefetch((char*)draw_list[i + 1], _MM_HINT_NTA);
  7563. }
  7564. }
  7565. bool has_avatar = paramsp->mAvatar.notNull();
  7566. if (rigged != has_avatar)
  7567. {
  7568. // This pool contains both rigged and non-rigged DrawInfos. Only
  7569. // draw the objects we are interested in in this pass.
  7570. continue;
  7571. }
  7572. if (!rigged)
  7573. {
  7574. mSimplePool->pushBatch(*paramsp, mask, true, true);
  7575. continue;
  7576. }
  7577. if (paramsp->mSkinInfo &&
  7578. (paramsp->mAvatar.get() != last_avatarp ||
  7579. paramsp->mSkinInfo->mHash != last_hash))
  7580. {
  7581. #if 0
  7582. if (!mSimplePool->uploadMatrixPalette(*paramsp))
  7583. {
  7584. continue;
  7585. }
  7586. #else
  7587. mSimplePool->uploadMatrixPalette(*paramsp);
  7588. #endif
  7589. last_avatarp = paramsp->mAvatar.get();
  7590. last_hash = paramsp->mSkinInfo->mHash;
  7591. }
  7592. mSimplePool->pushBatch(*paramsp, mask | LLVertexBuffer::MAP_WEIGHT4,
  7593. true, true);
  7594. }
  7595. gGL.loadMatrix(gGLModelView);
  7596. gGLLastMatrix = NULL;
  7597. }
  7598. void LLPipeline::renderMaskedObjects(U32 type, U32 mask, bool texture,
  7599. bool batch_texture, bool rigged)
  7600. {
  7601. gGL.loadMatrix(gGLModelView);
  7602. gGLLastMatrix = NULL;
  7603. if (rigged)
  7604. {
  7605. mAlphaMaskPool->pushRiggedMaskBatches(type + 1, mask, texture,
  7606. batch_texture);
  7607. }
  7608. else
  7609. {
  7610. mAlphaMaskPool->pushMaskBatches(type, mask, texture, batch_texture);
  7611. }
  7612. gGL.loadMatrix(gGLModelView);
  7613. gGLLastMatrix = NULL;
  7614. }
  7615. void LLPipeline::renderFullbrightMaskedObjects(U32 type, U32 mask,
  7616. bool texture,
  7617. bool batch_texture, bool rigged)
  7618. {
  7619. gGL.loadMatrix(gGLModelView);
  7620. gGLLastMatrix = NULL;
  7621. if (rigged)
  7622. {
  7623. mFullbrightAlphaMaskPool->pushRiggedMaskBatches(type + 1, mask,
  7624. texture,
  7625. batch_texture);
  7626. }
  7627. else
  7628. {
  7629. mFullbrightAlphaMaskPool->pushMaskBatches(type, mask, texture,
  7630. batch_texture);
  7631. }
  7632. gGL.loadMatrix(gGLModelView);
  7633. gGLLastMatrix = NULL;
  7634. }
  7635. // PBR renderer only
  7636. void LLPipeline::visualizeBuffers(LLRenderTarget* srcp, LLRenderTarget* dstp,
  7637. U32 buff_idx)
  7638. {
  7639. dstp->bindTarget();
  7640. LLGLSLShader* shaderp = &gDeferredBufferVisualProgram;
  7641. shaderp->bind();
  7642. shaderp->bindTexture(LLShaderMgr::DEFERRED_DIFFUSE, srcp, false,
  7643. LLTexUnit::TFO_BILINEAR, buff_idx);
  7644. shaderp->uniform1f(sMipLevel, RenderBufferVisualization == 4 ? 8.f : 0.f);
  7645. mScreenTriangleVB->setBuffer();
  7646. mScreenTriangleVB->drawArrays(LLRender::TRIANGLES, 0, 3);
  7647. shaderp->unbind();
  7648. dstp->flush();
  7649. }
  7650. // PBR renderer only
  7651. void LLPipeline::generateLuminance(LLRenderTarget* srcp, LLRenderTarget* dstp)
  7652. {
  7653. LL_TRACY_TIMER(TRC_RENDER_LUMINANCE);
  7654. dstp->bindTarget();
  7655. LLGLDepthTest depth(GL_FALSE, GL_FALSE);
  7656. gLuminanceProgram.bind();
  7657. S32 chan = gLuminanceProgram.enableTexture(LLShaderMgr::DEFERRED_DIFFUSE);
  7658. if (chan > -1)
  7659. {
  7660. srcp->bindTexture(0, chan, LLTexUnit::TFO_POINT);
  7661. }
  7662. chan = gLuminanceProgram.enableTexture(LLShaderMgr::DEFERRED_EMISSIVE);
  7663. if (chan > -1)
  7664. {
  7665. mGlow[1].bindTexture(0, chan);
  7666. }
  7667. chan = gLuminanceProgram.enableTexture(LLShaderMgr::DEFERRED_NORMAL);
  7668. if (chan > -1)
  7669. {
  7670. mRT->mDeferredScreen.bindTexture(2, chan, LLTexUnit::TFO_POINT);
  7671. }
  7672. static LLCachedControl<F32> diffuse_scale(gSavedSettings,
  7673. "RenderDiffuseLuminanceScale");
  7674. gLuminanceProgram.uniform1f(sLuminanceScale, diffuse_scale);
  7675. mScreenTriangleVB->setBuffer();
  7676. mScreenTriangleVB->drawArrays(LLRender::TRIANGLES, 0, 3);
  7677. dstp->flush();
  7678. gLuminanceProgram.unbind();
  7679. }
  7680. // PBR renderer only
  7681. void LLPipeline::generateExposure(LLRenderTarget* srcp, LLRenderTarget* dstp,
  7682. bool use_history)
  7683. {
  7684. LL_TRACY_TIMER(TRC_RENDER_EXPOSURE);
  7685. static LLCachedControl<F32> exp_coeff(gSavedSettings,
  7686. "RenderDynamicExposureCoefficient");
  7687. if (use_history)
  7688. {
  7689. // Copy last frame's exposure into mLastExposure
  7690. mLastExposure.bindTarget();
  7691. gCopyProgram.bind();
  7692. gGL.getTexUnit(0)->bind(dstp);
  7693. mScreenTriangleVB->setBuffer();
  7694. mScreenTriangleVB->drawArrays(LLRender::TRIANGLES, 0, 3);
  7695. mLastExposure.flush();
  7696. }
  7697. dstp->bindTarget();
  7698. LLGLDepthTest depth(GL_FALSE, GL_FALSE);
  7699. LLGLSLShader& shader = use_history ? gExposureProgram
  7700. : gExposureProgramNoFade;
  7701. shader.bind();
  7702. S32 chan = shader.enableTexture(LLShaderMgr::DEFERRED_EMISSIVE);
  7703. if (chan > -1)
  7704. {
  7705. mLuminanceMap.bindTexture(0, chan, LLTexUnit::TFO_TRILINEAR);
  7706. }
  7707. if (use_history)
  7708. {
  7709. chan = shader.enableTexture(LLShaderMgr::EXPOSURE_MAP);
  7710. if (chan > -1)
  7711. {
  7712. mLastExposure.bindTexture(0, chan);
  7713. }
  7714. }
  7715. shader.uniform1f(sDT, gFrameIntervalSeconds);
  7716. shader.uniform2f(sNoiseVec, ll_frand() * 2.f - 1.f,
  7717. ll_frand() * 2.f - 1.f);
  7718. F32 exp_min = 1.f;
  7719. F32 exp_max = 1.f;
  7720. static LLCachedControl<U32> reflections(gSavedSettings,
  7721. "RenderReflectionProbes");
  7722. if (reflections && mProbeAmbiance > 0.f)
  7723. {
  7724. F32 hdr_scale = 2.f * sqrtf(mSkyGamma);
  7725. if (hdr_scale > 1.f)
  7726. {
  7727. exp_min = 1.f / hdr_scale;
  7728. exp_max = hdr_scale;
  7729. }
  7730. }
  7731. shader.uniform3f(sExpParams, exp_coeff, exp_min, exp_max);
  7732. mScreenTriangleVB->setBuffer();
  7733. mScreenTriangleVB->drawArrays(LLRender::TRIANGLES, 0, 3);
  7734. if (use_history)
  7735. {
  7736. gGL.getTexUnit(chan)->unbind(mLastExposure.getUsage());
  7737. }
  7738. shader.unbind();
  7739. dstp->flush();
  7740. }
  7741. // PBR renderer only
  7742. void LLPipeline::generateGlow(LLRenderTarget* srcp)
  7743. {
  7744. LL_TRACY_TIMER(TRC_RENDER_GLOW);
  7745. if (!RenderGlow)
  7746. {
  7747. mGlow[1].bindTarget();
  7748. mGlow[1].clear();
  7749. mGlow[1].flush();
  7750. return;
  7751. }
  7752. mGlow[2].bindTarget();
  7753. mGlow[2].clear();
  7754. gGlowExtractProgram.bind();
  7755. gGlowExtractProgram.uniform1f(LLShaderMgr::GLOW_MIN_LUMINANCE, 9999);
  7756. gGlowExtractProgram.uniform1f(LLShaderMgr::GLOW_MAX_EXTRACT_ALPHA,
  7757. RenderGlowMaxExtractAlpha);
  7758. gGlowExtractProgram.uniform3f(LLShaderMgr::GLOW_LUM_WEIGHTS,
  7759. RenderGlowLumWeights.mV[0],
  7760. RenderGlowLumWeights.mV[1],
  7761. RenderGlowLumWeights.mV[2]);
  7762. gGlowExtractProgram.uniform3f(LLShaderMgr::GLOW_WARMTH_WEIGHTS,
  7763. RenderGlowWarmthWeights.mV[0],
  7764. RenderGlowWarmthWeights.mV[1],
  7765. RenderGlowWarmthWeights.mV[2]);
  7766. gGlowExtractProgram.uniform1f(LLShaderMgr::GLOW_WARMTH_AMOUNT,
  7767. RenderGlowWarmthAmount);
  7768. static LLCachedControl<bool> with_noise(gSavedSettings, "RenderGlowNoise");
  7769. if (with_noise)
  7770. {
  7771. S32 channel =
  7772. gGlowExtractProgram.enableTexture(LLShaderMgr::GLOW_NOISE_MAP);
  7773. if (channel > -1)
  7774. {
  7775. LLTexUnit* unitp = gGL.getTexUnit(channel);
  7776. unitp->bindManual(LLTexUnit::TT_TEXTURE, mTrueNoiseMap);
  7777. unitp->setTextureFilteringOption(LLTexUnit::TFO_POINT);
  7778. }
  7779. gGlowExtractProgram.uniform2f(LLShaderMgr::DEFERRED_SCREEN_RES,
  7780. mGlow[2].getWidth(),
  7781. mGlow[2].getHeight());
  7782. }
  7783. {
  7784. LLGLEnable blend_on(GL_BLEND);
  7785. gGL.setSceneBlendType(LLRender::BT_ADD_WITH_ALPHA);
  7786. gGlowExtractProgram.bindTexture(LLShaderMgr::DIFFUSE_MAP, srcp);
  7787. gGL.color4f(1.f, 1.f, 1.f, 1.f);
  7788. enableLightsFullbright();
  7789. mScreenTriangleVB->setBuffer();
  7790. mScreenTriangleVB->drawArrays(LLRender::TRIANGLES, 0, 3);
  7791. mGlow[2].flush();
  7792. }
  7793. gGlowExtractProgram.unbind();
  7794. // Power of two between 1 and 1024
  7795. const U32 glow_res = llclamp(1 << RenderGlowResolutionPow, 1, 1024);
  7796. S32 kernel = RenderGlowIterations * 2;
  7797. F32 delta = RenderGlowWidth / (F32)glow_res;
  7798. // Use half the glow width if we have the res set to less than 9 so that it
  7799. // looks almost the same in either case.
  7800. if (RenderGlowResolutionPow < 9)
  7801. {
  7802. delta *= 0.5f;
  7803. }
  7804. gGlowProgram.bind();
  7805. gGlowProgram.uniform1f(LLShaderMgr::GLOW_STRENGTH, RenderGlowStrength);
  7806. for (S32 i = 0; i < kernel; ++i)
  7807. {
  7808. mGlow[i % 2].bindTarget();
  7809. mGlow[i % 2].clear();
  7810. if (i == 0)
  7811. {
  7812. gGlowProgram.bindTexture(LLShaderMgr::DIFFUSE_MAP, &mGlow[2]);
  7813. }
  7814. else
  7815. {
  7816. gGlowProgram.bindTexture(LLShaderMgr::DIFFUSE_MAP,
  7817. &mGlow[(i - 1) % 2]);
  7818. }
  7819. if (i % 2 == 0)
  7820. {
  7821. gGlowProgram.uniform2f(LLShaderMgr::GLOW_DELTA, delta, 0.f);
  7822. }
  7823. else
  7824. {
  7825. gGlowProgram.uniform2f(LLShaderMgr::GLOW_DELTA, 0.f, delta);
  7826. }
  7827. mScreenTriangleVB->setBuffer();
  7828. mScreenTriangleVB->drawArrays(LLRender::TRIANGLES, 0, 3);
  7829. mGlow[i % 2].flush();
  7830. }
  7831. gGlowProgram.unbind();
  7832. }
  7833. // PBR renderer only
  7834. void LLPipeline::combineGlow(LLRenderTarget* srcp, LLRenderTarget* dstp)
  7835. {
  7836. LL_TRACY_TIMER(TRC_RENDER_GLOW_COMBINE);
  7837. dstp->bindTarget();
  7838. gGlowCombineProgram.bind();
  7839. gGlowCombineProgram.bindTexture(LLShaderMgr::DEFERRED_DIFFUSE, srcp);
  7840. gGlowCombineProgram.bindTexture(LLShaderMgr::DEFERRED_EMISSIVE, &mGlow[1]);
  7841. mScreenTriangleVB->setBuffer();
  7842. mScreenTriangleVB->drawArrays(LLRender::TRIANGLES, 0, 3);
  7843. dstp->flush();
  7844. }
  7845. // PBR renderer only
  7846. void LLPipeline::gammaCorrect(LLRenderTarget* srcp, LLRenderTarget* dstp)
  7847. {
  7848. LL_TRACY_TIMER(TRC_RENDER_GAMMA_CORRECT);
  7849. static LLCachedControl<U32> reflections(gSavedSettings,
  7850. "RenderReflectionProbes");
  7851. static LLCachedControl<bool> no_post(gSavedSettings,
  7852. "RenderDisablePostProcessing");
  7853. static LLCachedControl<F32> exposure(gSavedSettings, "RenderExposure");
  7854. dstp->bindTarget();
  7855. LLGLDepthTest depth(GL_FALSE, GL_FALSE);
  7856. static LLGLSLShader* last_shaderp = NULL;
  7857. LLGLSLShader* shaderp;
  7858. if (gSnapshotNoPost || !reflections || (no_post && gToolMgr.inBuildMode()))
  7859. {
  7860. shaderp = &gNoPostGammaCorrectProgram;
  7861. if (shaderp != last_shaderp)
  7862. {
  7863. llinfos << "Gamma shader in use: gNoPostGammaCorrectProgram"
  7864. << llendl;
  7865. }
  7866. }
  7867. else if (mProbeAmbiance <= 0.f)
  7868. {
  7869. shaderp = &gLegacyPostGammaCorrectProgram;
  7870. if (shaderp != last_shaderp)
  7871. {
  7872. llinfos << "Gamma shader in use: gLegacyPostGammaCorrectProgram"
  7873. << llendl;
  7874. }
  7875. }
  7876. else
  7877. {
  7878. shaderp = &gDeferredPostGammaCorrectProgram;
  7879. if (shaderp != last_shaderp)
  7880. {
  7881. llinfos << "Gamma shader in use: gDeferredPostGammaCorrectProgram"
  7882. << llendl;
  7883. }
  7884. }
  7885. last_shaderp = shaderp;
  7886. shaderp->bind();
  7887. shaderp->bindTexture(LLShaderMgr::DEFERRED_DIFFUSE, srcp, false,
  7888. LLTexUnit::TFO_POINT);
  7889. shaderp->bindTexture(LLShaderMgr::EXPOSURE_MAP, &mExposureMap);
  7890. shaderp->uniform2f(LLShaderMgr::DEFERRED_SCREEN_RES, srcp->getWidth(),
  7891. srcp->getHeight());
  7892. shaderp->uniform1f(sExposure, llclamp(F32(exposure), 0.5f, 4.f));
  7893. static LLCachedControl<bool> use_hdri(gSavedSettings, "RenderHDRIEnabled");
  7894. bool use_hdri_sky = use_hdri && LLDrawPoolWLSky::getHDRISky();
  7895. shaderp->uniform1f(sAcesMix, use_hdri_sky ? 0.f : 0.3f);
  7896. mScreenTriangleVB->setBuffer();
  7897. mScreenTriangleVB->drawArrays(LLRender::TRIANGLES, 0, 3);
  7898. gGL.getTexUnit(0)->unbind(srcp->getUsage());
  7899. shaderp->unbind();
  7900. dstp->flush();
  7901. }
  7902. // PBR renderer only
  7903. void LLPipeline::copyRenderTarget(LLRenderTarget* srcp, LLRenderTarget* dstp)
  7904. {
  7905. LL_TRACY_TIMER(TRC_RENDER_COPY_TARGET);
  7906. dstp->bindTarget();
  7907. LLGLSLShader* shaderp = &gDeferredPostNoDoFProgram;
  7908. shaderp->bind();
  7909. shaderp->bindTexture(LLShaderMgr::DEFERRED_DIFFUSE, srcp);
  7910. shaderp->bindTexture(LLShaderMgr::DEFERRED_DEPTH, &mRT->mDeferredScreen,
  7911. true);
  7912. mScreenTriangleVB->setBuffer();
  7913. mScreenTriangleVB->drawArrays(LLRender::TRIANGLES, 0, 3);
  7914. shaderp->unbind();
  7915. dstp->flush();
  7916. }
  7917. // PBR renderer only. Returns true when FXAA got actually applied. HB
  7918. bool LLPipeline::applyFXAA(LLRenderTarget* srcp, LLRenderTarget* dstp)
  7919. {
  7920. LL_TRACY_TIMER(TRC_RENDER_FXAA);
  7921. if (!LLViewerShaderMgr::sHasFXAA || RenderFSAASamples <= 1 ||
  7922. !mFXAABuffer.isComplete())
  7923. {
  7924. copyRenderTarget(srcp, dstp);
  7925. return false;
  7926. }
  7927. // Bake out texture2D with RGBL for FXAA shader
  7928. mFXAABuffer.bindTarget();
  7929. LLGLSLShader* shaderp = &gGlowCombineFXAAProgram;
  7930. shaderp->bind();
  7931. LLTexUnit::eTextureType mode = srcp->getUsage();
  7932. S32 channel = shaderp->enableTexture(LLShaderMgr::DEFERRED_DIFFUSE, mode);
  7933. if (channel > -1)
  7934. {
  7935. srcp->bindTexture(0, channel, LLTexUnit::TFO_BILINEAR);
  7936. }
  7937. LLGLDepthTest depth_test(GL_TRUE, GL_TRUE, GL_ALWAYS);
  7938. mScreenTriangleVB->setBuffer();
  7939. mScreenTriangleVB->drawArrays(LLRender::TRIANGLES, 0, 3);
  7940. shaderp->disableTexture(LLShaderMgr::DEFERRED_DIFFUSE, mode);
  7941. shaderp->unbind();
  7942. mFXAABuffer.flush();
  7943. dstp->bindTarget();
  7944. shaderp = &gFXAAProgram[RenderDeferredAAQuality];
  7945. shaderp->bind();
  7946. channel = shaderp->enableTexture(LLShaderMgr::DIFFUSE_MAP,
  7947. mFXAABuffer.getUsage());
  7948. if (channel > -1)
  7949. {
  7950. mFXAABuffer.bindTexture(0, channel, LLTexUnit::TFO_BILINEAR);
  7951. }
  7952. gViewerWindowp->setupViewport();
  7953. F32 inv_width = 1.f / F32(mFXAABuffer.getWidth());
  7954. F32 inv_height = 1.f / F32(mFXAABuffer.getHeight());
  7955. F32 scale_x = F32(dstp->getWidth()) * inv_width;
  7956. F32 scale_y = F32(dstp->getHeight()) * inv_height;
  7957. shaderp->uniform2f(LLShaderMgr::FXAA_TC_SCALE, scale_x, scale_y);
  7958. shaderp->uniform2f(LLShaderMgr::FXAA_RCP_SCREEN_RES, inv_width,
  7959. inv_height);
  7960. shaderp->uniform4f(LLShaderMgr::FXAA_RCP_FRAME_OPT,
  7961. -0.5f * inv_width, -0.5f * inv_height,
  7962. 0.5f * inv_width, 0.5f * inv_height);
  7963. shaderp->uniform4f(LLShaderMgr::FXAA_RCP_FRAME_OPT2,
  7964. -2.f * inv_width, -2.f * inv_height,
  7965. 2.f * inv_width, 2.f * inv_height);
  7966. channel = shaderp->getTextureChannel(LLShaderMgr::DEFERRED_DEPTH);
  7967. gGL.getTexUnit(channel)->bind(&mRT->mDeferredScreen, true);
  7968. mScreenTriangleVB->setBuffer();
  7969. mScreenTriangleVB->drawArrays(LLRender::TRIANGLES, 0, 3);
  7970. shaderp->unbind();
  7971. dstp->flush();
  7972. return true;
  7973. }
  7974. // PBR renderer only. Returns true when SMAA got actually applied. HB
  7975. bool LLPipeline::applySMAA(LLRenderTarget* srcp, LLRenderTarget* dstp)
  7976. {
  7977. LL_TRACY_TIMER(TRC_RENDER_SMAA);
  7978. if (!LLViewerShaderMgr::sHasSMAA || RenderFSAASamples <= 1 ||
  7979. !mAreaMap || !mSearchMap || !mSMAAEdgeBuffer.isComplete() ||
  7980. !mSMAABlendBuffer.isComplete() || !mScratchBuffer.isComplete())
  7981. {
  7982. copyRenderTarget(srcp, dstp);
  7983. return false;
  7984. }
  7985. LLGLSLShader* shaderp;
  7986. // Note: all buffers got the same size.
  7987. S32 width = srcp->getWidth();
  7988. S32 height = srcp->getHeight();
  7989. // Bake out texture2D with RGBL for SMAA shader
  7990. mFXAABuffer.bindTarget();
  7991. shaderp = &gGlowCombineFXAAProgram;
  7992. shaderp->bind();
  7993. LLTexUnit::eTextureType mode = srcp->getUsage();
  7994. S32 channel = shaderp->enableTexture(LLShaderMgr::DEFERRED_DIFFUSE, mode);
  7995. if (channel > -1)
  7996. {
  7997. srcp->bindTexture(0, channel, LLTexUnit::TFO_BILINEAR);
  7998. }
  7999. mScreenTriangleVB->setBuffer();
  8000. mScreenTriangleVB->drawArrays(LLRender::TRIANGLES, 0, 3);
  8001. shaderp->disableTexture(LLShaderMgr::DEFERRED_DIFFUSE, mode);
  8002. shaderp->unbind();
  8003. mFXAABuffer.flush();
  8004. glClearColor(0.f, 0.f, 0.f, 0.f);
  8005. glViewport(0, 0, width, height);
  8006. F32 rt_metrics[] = { 1.f / width, 1.f / height, (F32)width, (F32)height };
  8007. LLTexUnit* unit0 = gGL.getTexUnit(0);
  8008. LLTexUnit* unit1 = gGL.getTexUnit(1);
  8009. LLTexUnit* unit2 = gGL.getTexUnit(2);
  8010. mSMAAEdgeBuffer.bindTarget();
  8011. mSMAAEdgeBuffer.clear(GL_COLOR_BUFFER_BIT);
  8012. shaderp = &gPostSMAAEdgeDetect[RenderDeferredAAQuality];
  8013. shaderp->bind();
  8014. shaderp->uniform4fv(sSmaaRTMetrics, 1, rt_metrics);
  8015. mFXAABuffer.bindTexture(0, 0, LLTexUnit::TFO_BILINEAR);
  8016. unit0->setTextureAddressMode(LLTexUnit::TAM_CLAMP);
  8017. unit0->setTextureColorSpace(LLTexUnit::TCS_LINEAR);
  8018. LLGLDepthTest depth_test(GL_TRUE, GL_TRUE, GL_ALWAYS);
  8019. mScreenTriangleVB->setBuffer();
  8020. mScreenTriangleVB->drawArrays(LLRender::TRIANGLES, 0, 3);
  8021. unit0->disable();
  8022. shaderp->unbind();
  8023. mSMAAEdgeBuffer.flush();
  8024. mSMAABlendBuffer.bindTarget();
  8025. mSMAABlendBuffer.clear(GL_COLOR_BUFFER_BIT);
  8026. shaderp = &gPostSMAABlendWeights[RenderDeferredAAQuality];
  8027. shaderp->bind();
  8028. shaderp->uniform4fv(sSmaaRTMetrics, 1, rt_metrics);
  8029. mSMAAEdgeBuffer.bindTexture(0, 0, LLTexUnit::TFO_BILINEAR);
  8030. unit0->setTextureAddressMode(LLTexUnit::TAM_CLAMP);
  8031. unit0->setTextureColorSpace(LLTexUnit::TCS_LINEAR);
  8032. unit1->bindManual(LLTexUnit::TT_TEXTURE, mAreaMap);
  8033. unit1->setTextureFilteringOption(LLTexUnit::TFO_BILINEAR);
  8034. unit1->setTextureAddressMode(LLTexUnit::TAM_CLAMP);
  8035. unit1->setTextureColorSpace(LLTexUnit::TCS_LINEAR);
  8036. unit2->bindManual(LLTexUnit::TT_TEXTURE, mSearchMap);
  8037. unit2->setTextureFilteringOption(LLTexUnit::TFO_BILINEAR);
  8038. unit2->setTextureAddressMode(LLTexUnit::TAM_CLAMP);
  8039. unit2->setTextureColorSpace(LLTexUnit::TCS_LINEAR);
  8040. mScreenTriangleVB->setBuffer();
  8041. mScreenTriangleVB->drawArrays(LLRender::TRIANGLES, 0, 3);
  8042. unit0->disable();
  8043. unit1->disable();
  8044. unit2->disable();
  8045. shaderp->unbind();
  8046. mSMAABlendBuffer.flush();
  8047. // *TODO: it should be possible to modify the NeighborhoodBlend shader to
  8048. // save the intermediate scratch buffer step and avoid the last call to
  8049. // copyRenderTarget() used here to combine the SMAA shaders result with
  8050. // the PBR depth buffer (else things such as over-texts and avatar name
  8051. // tags vanish)... But I am not qualified enough in OpenGL to do this (my
  8052. // only attempt resulted in a red screen), so if you can do it, please be
  8053. // my guest and you will be credited for it ! HB
  8054. mScratchBuffer.bindTarget();
  8055. mScratchBuffer.clear(GL_COLOR_BUFFER_BIT);
  8056. shaderp = &gPostSMAANeighborhoodBlend[RenderDeferredAAQuality];
  8057. shaderp->bind();
  8058. shaderp->uniform4fv(sSmaaRTMetrics, 1, rt_metrics);
  8059. mFXAABuffer.bindTexture(0, 0, LLTexUnit::TFO_BILINEAR);
  8060. unit0->setTextureAddressMode(LLTexUnit::TAM_CLAMP);
  8061. unit0->setTextureColorSpace(LLTexUnit::TCS_LINEAR);
  8062. mSMAABlendBuffer.bindTexture(0, 1, LLTexUnit::TFO_BILINEAR);
  8063. unit1->setTextureAddressMode(LLTexUnit::TAM_CLAMP);
  8064. unit1->setTextureColorSpace(LLTexUnit::TCS_LINEAR);
  8065. // This might also be avoidable with a properly amended shader... HB
  8066. gViewerWindowp->setupViewport();
  8067. mScreenTriangleVB->setBuffer();
  8068. mScreenTriangleVB->drawArrays(LLRender::TRIANGLES, 0, 3);
  8069. mScratchBuffer.flush();
  8070. unit0->disable();
  8071. unit1->disable();
  8072. shaderp->unbind();
  8073. copyRenderTarget(&mScratchBuffer, dstp);
  8074. return true;
  8075. }
  8076. // PBR renderer only.
  8077. void LLPipeline::applyCAS(LLRenderTarget* srcp, LLRenderTarget* dstp)
  8078. {
  8079. LL_TRACY_TIMER(TRC_RENDER_CAS);
  8080. dstp->bindTarget();
  8081. dstp->clear();
  8082. gPostCASProgram.bind();
  8083. static LLCachedControl<LLVector3> cas_params(gSavedSettings,
  8084. "RenderDeferredCASParams");
  8085. LLVector3 params = LLVector3(cas_params);
  8086. params.clamp(0.f, 1.f);
  8087. gPostCASProgram.uniform3fv(sSharpness, 1, params.mV);
  8088. gPostCASProgram.bindTexture(LLShaderMgr::DEFERRED_DIFFUSE, srcp, false,
  8089. LLTexUnit::TFO_POINT);
  8090. LLGLDepthTest depth_test(GL_TRUE, GL_TRUE, GL_ALWAYS);
  8091. gViewerWindowp->setupViewport();
  8092. mScreenTriangleVB->setBuffer();
  8093. mScreenTriangleVB->drawArrays(LLRender::TRIANGLES, 0, 3);
  8094. gPostCASProgram.unbind();
  8095. dstp->flush();
  8096. }
  8097. // Helper function used to factorize common code in EE and PBR renderers. HB
  8098. static void calc_doff_params(F32& subject_dist, F32& blur_constant,
  8099. F32& magnification)
  8100. {
  8101. static F32 current_dist = 16.f;
  8102. static F32 start_dist = 16.f;
  8103. static F32 transition_time = 1.f;
  8104. static LLCachedControl<F32> cam_trans_time(gSavedSettings,
  8105. "CameraFocusTransitionTime");
  8106. static LLCachedControl<F32> camera_fnum(gSavedSettings, "CameraFNumber");
  8107. static LLCachedControl<F32> cam_default_focal(gSavedSettings,
  8108. "CameraFocalLength");
  8109. static LLCachedControl<F32> camera_fov(gSavedSettings,
  8110. "CameraFieldOfView");
  8111. LLVector3 focus_point;
  8112. LLViewerMediaFocus* mfocusp = LLViewerMediaFocus::getInstance();
  8113. LLViewerObject* objp = mfocusp->getFocusedObject();
  8114. if (objp && objp->mDrawable && objp->isSelected())
  8115. {
  8116. // Focus on selected media object
  8117. S32 face_idx = mfocusp->getFocusedFace();
  8118. if (objp && objp->mDrawable)
  8119. {
  8120. LLFace* facep = objp->mDrawable->getFace(face_idx);
  8121. if (facep)
  8122. {
  8123. focus_point = facep->getPositionAgent();
  8124. }
  8125. }
  8126. }
  8127. if (focus_point.isExactlyZero())
  8128. {
  8129. if (LLViewerJoystick::getInstance()->getOverrideCamera())
  8130. {
  8131. // Focus on point under cursor
  8132. focus_point.set(gDebugRaycastIntersection.getF32ptr());
  8133. }
  8134. else if (gAgent.cameraMouselook())
  8135. {
  8136. // Focus on point under mouselook crosshairs
  8137. LLVector4a result;
  8138. result.clear();
  8139. gViewerWindowp->cursorIntersect(-1, -1, 512.f, NULL, -1,
  8140. false, false, NULL, NULL, NULL,
  8141. &result);
  8142. focus_point.set(result.getF32ptr());
  8143. }
  8144. else
  8145. {
  8146. // Focus on alt-zoom target
  8147. LLViewerRegion* regionp = gAgent.getRegion();
  8148. if (regionp)
  8149. {
  8150. focus_point = LLVector3(gAgent.getFocusGlobal() -
  8151. regionp->getOriginGlobal());
  8152. }
  8153. }
  8154. }
  8155. LLVector3 eye = gViewerCamera.getOrigin();
  8156. F32 target_dist = 16.f;
  8157. if (!focus_point.isExactlyZero())
  8158. {
  8159. target_dist = gViewerCamera.getAtAxis() * (focus_point - eye);
  8160. }
  8161. if (transition_time >= 1.f &&
  8162. fabsf(current_dist - target_dist) / current_dist > 0.01f)
  8163. {
  8164. // Large shift happened, interpolate smoothly to new target distance
  8165. transition_time = 0.f;
  8166. start_dist = current_dist;
  8167. }
  8168. else if (transition_time < 1.f)
  8169. {
  8170. // Currently in a transition, continue interpolating
  8171. transition_time += 1.f / llmax(F32_MIN,
  8172. cam_trans_time * gFrameIntervalSeconds);
  8173. transition_time = llmin(transition_time, 1.f);
  8174. F32 t = cosf(transition_time * F_PI + F_PI) * 0.5f + 0.5f;
  8175. current_dist = start_dist + (target_dist - start_dist) * t;
  8176. }
  8177. else
  8178. {
  8179. // Small or no change, just snap to target distance
  8180. current_dist = target_dist;
  8181. }
  8182. // From wikipedia -- c = |s2-s1|/s2 * f^2/(N(S1-f))
  8183. // where N = CameraFNumber
  8184. // s2 = dot distance
  8185. // s1 = subject distance
  8186. // f = focal length
  8187. subject_dist = current_dist * 1000.f; // In mm
  8188. F32 dv = 2.f * cam_default_focal * tanf(camera_fov * DEG_TO_RAD * 0.5f);
  8189. F32 focal = dv / (2.f * tanf(gViewerCamera.getView() * 0.5f));
  8190. blur_constant = focal * focal / (camera_fnum * (subject_dist - focal));
  8191. blur_constant *= 0.001f; // Convert to meters for shader
  8192. if (subject_dist == focal)
  8193. {
  8194. magnification = F32_MAX;
  8195. }
  8196. else
  8197. {
  8198. magnification = focal / (subject_dist - focal);
  8199. }
  8200. subject_dist *= 0.001f; // Convert back to meters for shader
  8201. }
  8202. // PBR renderer only. Returns true when DoF got actually applied. HB
  8203. bool LLPipeline::renderDoF(LLRenderTarget* srcp, LLRenderTarget* dstp)
  8204. {
  8205. LL_TRACY_TIMER(TRC_RENDER_DOF);
  8206. if (!RenderDepthOfField || gCubeSnapshot ||
  8207. (!RenderDepthOfFieldInEditMode && gToolMgr.inBuildMode()))
  8208. {
  8209. copyRenderTarget(srcp, dstp);
  8210. return false;
  8211. }
  8212. F32 subject_dist, blur_constant, magnification;
  8213. calc_doff_params(subject_dist, blur_constant, magnification);
  8214. LLGLDisable blend(GL_BLEND);
  8215. // Build diffuse + bloom + CoF
  8216. mRT->mDeferredLight.bindTarget();
  8217. LLGLSLShader* shaderp = &gDeferredCoFProgram;
  8218. shaderp->bind();
  8219. shaderp->bindTexture(LLShaderMgr::DEFERRED_DIFFUSE, srcp,
  8220. LLTexUnit::TFO_POINT);
  8221. shaderp->bindTexture(LLShaderMgr::DEFERRED_DEPTH, &mRT->mDeferredScreen,
  8222. true);
  8223. shaderp->uniform2f(LLShaderMgr::DEFERRED_SCREEN_RES, dstp->getWidth(),
  8224. dstp->getHeight());
  8225. shaderp->uniform1f(LLShaderMgr::DOF_FOCAL_DISTANCE, -subject_dist);
  8226. shaderp->uniform1f(LLShaderMgr::DOF_BLUR_CONSTANT, blur_constant);
  8227. shaderp->uniform1f(LLShaderMgr::DOF_TAN_PIXEL_ANGLE,
  8228. tanf(1.f / LLDrawable::sCurPixelAngle));
  8229. shaderp->uniform1f(LLShaderMgr::DOF_MAGNIFICATION, magnification);
  8230. shaderp->uniform1f(LLShaderMgr::DOF_MAX_COF, CameraMaxCoF);
  8231. shaderp->uniform1f(LLShaderMgr::DOF_RES_SCALE, CameraDoFResScale);
  8232. mScreenTriangleVB->setBuffer();
  8233. mScreenTriangleVB->drawArrays(LLRender::TRIANGLES, 0, 3);
  8234. shaderp->unbind();
  8235. mRT->mDeferredLight.flush();
  8236. U32 dof_width = U32(mRT->mScreen.getWidth() * CameraDoFResScale);
  8237. U32 dof_height = U32(mRT->mScreen.getHeight() * CameraDoFResScale);
  8238. // Perform DoF sampling at half-res (preserve alpha channel)
  8239. srcp->bindTarget();
  8240. glViewport(0, 0, dof_width, dof_height);
  8241. gGL.setColorMask(true, false);
  8242. shaderp = &gDeferredPostProgram;
  8243. shaderp->bind();
  8244. shaderp->bindTexture(LLShaderMgr::DEFERRED_DIFFUSE, &mRT->mDeferredLight,
  8245. LLTexUnit::TFO_POINT);
  8246. shaderp->uniform2f(LLShaderMgr::DEFERRED_SCREEN_RES, dstp->getWidth(),
  8247. dstp->getHeight());
  8248. shaderp->uniform1f(LLShaderMgr::DOF_MAX_COF, CameraMaxCoF);
  8249. shaderp->uniform1f(LLShaderMgr::DOF_RES_SCALE, CameraDoFResScale);
  8250. mScreenTriangleVB->setBuffer();
  8251. mScreenTriangleVB->drawArrays(LLRender::TRIANGLES, 0, 3);
  8252. shaderp->unbind();
  8253. srcp->flush();
  8254. gGL.setColorMask(true, true);
  8255. // Combine result based on alpha
  8256. dstp->bindTarget();
  8257. if (RenderFSAASamples > 1 && mFXAABuffer.isComplete())
  8258. {
  8259. glViewport(0, 0, dstp->getWidth(), dstp->getHeight());
  8260. }
  8261. else
  8262. {
  8263. gViewerWindowp->setupViewport();
  8264. }
  8265. shaderp = &gDeferredDoFCombineProgram;
  8266. shaderp->bind();
  8267. shaderp->bindTexture(LLShaderMgr::DEFERRED_DIFFUSE, srcp,
  8268. LLTexUnit::TFO_POINT);
  8269. shaderp->bindTexture(LLShaderMgr::DEFERRED_LIGHT, &mRT->mDeferredLight,
  8270. LLTexUnit::TFO_POINT);
  8271. shaderp->uniform2f(LLShaderMgr::DEFERRED_SCREEN_RES, dstp->getWidth(),
  8272. dstp->getHeight());
  8273. shaderp->uniform1f(LLShaderMgr::DOF_MAX_COF, CameraMaxCoF);
  8274. shaderp->uniform1f(LLShaderMgr::DOF_RES_SCALE, CameraDoFResScale);
  8275. shaderp->uniform1f(LLShaderMgr::DOF_WIDTH,
  8276. (dof_width - 1) / (F32)srcp->getWidth());
  8277. shaderp->uniform1f(LLShaderMgr::DOF_HEIGHT,
  8278. (dof_height - 1) / (F32)srcp->getHeight());
  8279. mScreenTriangleVB->setBuffer();
  8280. mScreenTriangleVB->drawArrays(LLRender::TRIANGLES, 0, 3);
  8281. shaderp->unbind();
  8282. dstp->flush();
  8283. return true;
  8284. }
  8285. // PBR renderer only
  8286. void LLPipeline::copyScreenSpaceReflections(LLRenderTarget* srcp,
  8287. LLRenderTarget* dstp)
  8288. {
  8289. LL_TRACY_TIMER(TRC_RENDER_SSR_COPY);
  8290. if (!RenderScreenSpaceReflections || gCubeSnapshot)
  8291. {
  8292. return;
  8293. }
  8294. LLGLDepthTest depth(GL_TRUE, GL_TRUE, GL_ALWAYS);
  8295. dstp->bindTarget();
  8296. dstp->clear();
  8297. LLGLSLShader* shaderp = &gCopyDepthProgram;
  8298. shaderp->bind();
  8299. S32 diff_chan = shaderp->getTextureChannel(LLShaderMgr::DIFFUSE_MAP);
  8300. S32 depth_chan = shaderp->getTextureChannel(LLShaderMgr::DEFERRED_DEPTH);
  8301. gGL.getTexUnit(diff_chan)->bind(srcp);
  8302. gGL.getTexUnit(depth_chan)->bind(&mRT->mDeferredScreen, true);
  8303. mScreenTriangleVB->setBuffer();
  8304. mScreenTriangleVB->drawArrays(LLRender::TRIANGLES, 0, 3);
  8305. dstp->flush();
  8306. }
  8307. // Branched version for the PBR renderer
  8308. void LLPipeline::renderFinalizePBR()
  8309. {
  8310. LL_FAST_TIMER(FTM_RENDER_BLOOM);
  8311. LLVertexBuffer::unbind();
  8312. LL_GL_CHECK_STATES;
  8313. gGL.color4f(1.f, 1.f, 1.f, 1.f);
  8314. LLGLDepthTest depth(GL_FALSE);
  8315. LLGLDisable blend(GL_BLEND);
  8316. LLGLDisable cull(GL_CULL_FACE);
  8317. enableLightsFullbright();
  8318. gGL.setColorMask(true, true);
  8319. glClearColor(0.f, 0.f, 0.f, 0.f);
  8320. copyScreenSpaceReflections(&mRT->mScreen, &mSceneMap);
  8321. generateLuminance(&mRT->mScreen, &mLuminanceMap);
  8322. generateExposure(&mLuminanceMap, &mExposureMap);
  8323. gammaCorrect(&mRT->mScreen, &mPostMap);
  8324. LLVertexBuffer::unbind();
  8325. generateGlow(&mPostMap);
  8326. combineGlow(&mPostMap, &mRT->mScreen);
  8327. gViewerWindowp->setupViewport();
  8328. renderDoF(&mRT->mScreen, &mPostMap);
  8329. LLRenderTarget* final_targetp = &mRT->mScreen;
  8330. LLRenderTarget* work_targetp = &mPostMap;
  8331. static LLCachedControl<bool> use_smaa(gSavedSettings,
  8332. "RenderDeferredUseSMAA");
  8333. if (use_smaa && LLViewerShaderMgr::sHasSMAA)
  8334. {
  8335. applySMAA(work_targetp, final_targetp);
  8336. }
  8337. else
  8338. {
  8339. applyFXAA(work_targetp, final_targetp);
  8340. }
  8341. if (RenderDeferredAASharpen && LLViewerShaderMgr::sHasCAS)
  8342. {
  8343. LLRenderTarget* tempp = work_targetp;
  8344. work_targetp = final_targetp;
  8345. final_targetp = tempp;
  8346. applyCAS(work_targetp, final_targetp);
  8347. }
  8348. if (RenderBufferVisualization > -1)
  8349. {
  8350. final_targetp = work_targetp;
  8351. if (RenderBufferVisualization == 4)
  8352. {
  8353. visualizeBuffers(&mLuminanceMap, final_targetp, 0);
  8354. }
  8355. else
  8356. {
  8357. visualizeBuffers(&mRT->mDeferredScreen, final_targetp,
  8358. RenderBufferVisualization);
  8359. }
  8360. }
  8361. // Present the screen target
  8362. gDeferredPostNoDoFProgram.bind();
  8363. // Whatever is last in the above post processing chain should _always_ be
  8364. // rendered directly here. If not, expect problems.
  8365. gDeferredPostNoDoFProgram.bindTexture(LLShaderMgr::DEFERRED_DIFFUSE,
  8366. final_targetp);
  8367. gDeferredPostNoDoFProgram.bindTexture(LLShaderMgr::DEFERRED_DEPTH,
  8368. &mRT->mDeferredScreen, true);
  8369. {
  8370. LLGLDepthTest depth_test(GL_TRUE, GL_TRUE, GL_ALWAYS);
  8371. mScreenTriangleVB->setBuffer();
  8372. mScreenTriangleVB->drawArrays(LLRender::TRIANGLES, 0, 3);
  8373. }
  8374. gDeferredPostNoDoFProgram.unbind();
  8375. gGL.setSceneBlendType(LLRender::BT_ALPHA);
  8376. if (hasRenderDebugMask(RENDER_DEBUG_PHYSICS_SHAPES))
  8377. {
  8378. renderPhysicsDisplay();
  8379. }
  8380. LLVertexBuffer::unbind();
  8381. LL_GL_CHECK_STATES;
  8382. }
  8383. void LLPipeline::renderFinalize()
  8384. {
  8385. if (gUsePBRShaders)
  8386. {
  8387. renderFinalizePBR();
  8388. return;
  8389. }
  8390. LLVertexBuffer::unbind();
  8391. LL_GL_CHECK_STATES;
  8392. if (gUseWireframe)
  8393. {
  8394. glPolygonMode(GL_FRONT_AND_BACK, GL_FILL);
  8395. }
  8396. LLVector2 tc1;
  8397. LLVector2 tc2((F32)(mRT->mScreen.getWidth() * 2),
  8398. (F32)(mRT->mScreen.getHeight() * 2));
  8399. LL_FAST_TIMER(FTM_RENDER_BLOOM);
  8400. gGL.color4f(1.f, 1.f, 1.f, 1.f);
  8401. LLGLDepthTest depth(GL_FALSE);
  8402. LLGLDisable blend(GL_BLEND);
  8403. LLGLDisable cull(GL_CULL_FACE);
  8404. enableLightsFullbright();
  8405. gGL.matrixMode(LLRender::MM_PROJECTION);
  8406. gGL.pushMatrix();
  8407. gGL.loadIdentity();
  8408. gGL.matrixMode(LLRender::MM_MODELVIEW);
  8409. gGL.pushMatrix();
  8410. gGL.loadIdentity();
  8411. gGL.setColorMask(true, true);
  8412. glClearColor(0.f, 0.f, 0.f, 0.f);
  8413. LLTexUnit* unit0 = gGL.getTexUnit(0);
  8414. if (RenderGlow)
  8415. {
  8416. mGlow[2].bindTarget();
  8417. mGlow[2].clear();
  8418. gGlowExtractProgram.bind();
  8419. gGlowExtractProgram.uniform1f(LLShaderMgr::GLOW_MIN_LUMINANCE,
  8420. RenderGlowMinLuminance);
  8421. gGlowExtractProgram.uniform1f(LLShaderMgr::GLOW_MAX_EXTRACT_ALPHA,
  8422. RenderGlowMaxExtractAlpha);
  8423. gGlowExtractProgram.uniform3f(LLShaderMgr::GLOW_LUM_WEIGHTS,
  8424. RenderGlowLumWeights.mV[0],
  8425. RenderGlowLumWeights.mV[1],
  8426. RenderGlowLumWeights.mV[2]);
  8427. gGlowExtractProgram.uniform3f(LLShaderMgr::GLOW_WARMTH_WEIGHTS,
  8428. RenderGlowWarmthWeights.mV[0],
  8429. RenderGlowWarmthWeights.mV[1],
  8430. RenderGlowWarmthWeights.mV[2]);
  8431. gGlowExtractProgram.uniform1f(LLShaderMgr::GLOW_WARMTH_AMOUNT,
  8432. RenderGlowWarmthAmount);
  8433. {
  8434. LLGLEnable blend_on(GL_BLEND);
  8435. gGL.setSceneBlendType(LLRender::BT_ADD_WITH_ALPHA);
  8436. mRT->mScreen.bindTexture(0, 0, LLTexUnit::TFO_POINT);
  8437. gGL.color4f(1.f, 1.f, 1.f, 1.f);
  8438. enableLightsFullbright();
  8439. gGL.begin(LLRender::TRIANGLE_STRIP);
  8440. gGL.texCoord2f(tc1.mV[0], tc1.mV[1]);
  8441. gGL.vertex2f(-1.f, -1.f);
  8442. gGL.texCoord2f(tc1.mV[0], tc2.mV[1]);
  8443. gGL.vertex2f(-1.f, 3.f);
  8444. gGL.texCoord2f(tc2.mV[0], tc1.mV[1]);
  8445. gGL.vertex2f(3.f, -1.f);
  8446. gGL.end();
  8447. unit0->unbind(mRT->mScreen.getUsage());
  8448. mGlow[2].flush();
  8449. }
  8450. tc1.set(0, 0);
  8451. tc2.set(2, 2);
  8452. // Power of two between 1 and 1024
  8453. const U32 glow_res = llclamp(1 << RenderGlowResolutionPow, 1, 1024);
  8454. S32 kernel = RenderGlowIterations * 2;
  8455. F32 delta = RenderGlowWidth / (F32)glow_res;
  8456. // Use half the glow width if we have the res set to less than 9 so
  8457. // that it looks almost the same in either case.
  8458. if (RenderGlowResolutionPow < 9)
  8459. {
  8460. delta *= 0.5f;
  8461. }
  8462. gGlowProgram.bind();
  8463. gGlowProgram.uniform1f(LLShaderMgr::GLOW_STRENGTH, RenderGlowStrength);
  8464. for (S32 i = 0; i < kernel; ++i)
  8465. {
  8466. mGlow[i % 2].bindTarget();
  8467. mGlow[i % 2].clear();
  8468. if (i == 0)
  8469. {
  8470. unit0->bind(&mGlow[2]);
  8471. }
  8472. else
  8473. {
  8474. unit0->bind(&mGlow[(i - 1) % 2]);
  8475. }
  8476. if (i % 2 == 0)
  8477. {
  8478. gGlowProgram.uniform2f(LLShaderMgr::GLOW_DELTA, delta, 0.f);
  8479. }
  8480. else
  8481. {
  8482. gGlowProgram.uniform2f(LLShaderMgr::GLOW_DELTA, 0.f, delta);
  8483. }
  8484. gGL.begin(LLRender::TRIANGLE_STRIP);
  8485. gGL.texCoord2f(tc1.mV[0], tc1.mV[1]);
  8486. gGL.vertex2f(-1.f, -1.f);
  8487. gGL.texCoord2f(tc1.mV[0], tc2.mV[1]);
  8488. gGL.vertex2f(-1.f, 3.f);
  8489. gGL.texCoord2f(tc2.mV[0], tc1.mV[1]);
  8490. gGL.vertex2f(3.f, -1.f);
  8491. gGL.end();
  8492. mGlow[i % 2].flush();
  8493. }
  8494. gGlowProgram.unbind();
  8495. }
  8496. else // Skip the glow ping-pong and just clear the result target
  8497. {
  8498. mGlow[1].bindTarget();
  8499. mGlow[1].clear();
  8500. mGlow[1].flush();
  8501. }
  8502. gViewerWindowp->setupViewport();
  8503. tc2.set((F32)mRT->mScreen.getWidth(), (F32)mRT->mScreen.getHeight());
  8504. gGL.flush();
  8505. LLVertexBuffer::unbind();
  8506. stop_glerror();
  8507. if (sRenderDeferred)
  8508. {
  8509. bool dof_enabled = RenderDepthOfField &&
  8510. (RenderDepthOfFieldInEditMode ||
  8511. !gToolMgr.inBuildMode()) &&
  8512. !gViewerCamera.cameraUnderWater();
  8513. bool multisample = RenderFSAASamples > 1 &&
  8514. mFXAABuffer.isComplete();
  8515. if (dof_enabled)
  8516. {
  8517. // Depth of field focal plane calculations
  8518. F32 subject_dist, blur_constant, magnification;
  8519. calc_doff_params(subject_dist, blur_constant, magnification);
  8520. LLGLDisable blend(GL_BLEND);
  8521. // Build diffuse + bloom + CoF
  8522. mRT->mDeferredLight.bindTarget();
  8523. LLGLSLShader* shaderp = &gDeferredCoFProgram;
  8524. bindDeferredShader(*shaderp);
  8525. S32 channel = shaderp->enableTexture(LLShaderMgr::DEFERRED_DIFFUSE,
  8526. mRT->mScreen.getUsage());
  8527. if (channel > -1)
  8528. {
  8529. mRT->mScreen.bindTexture(0, channel);
  8530. }
  8531. shaderp->uniform1f(LLShaderMgr::DOF_FOCAL_DISTANCE, -subject_dist);
  8532. shaderp->uniform1f(LLShaderMgr::DOF_BLUR_CONSTANT, blur_constant);
  8533. shaderp->uniform1f(LLShaderMgr::DOF_TAN_PIXEL_ANGLE,
  8534. tanf(1.f / LLDrawable::sCurPixelAngle));
  8535. shaderp->uniform1f(LLShaderMgr::DOF_MAGNIFICATION, magnification);
  8536. shaderp->uniform1f(LLShaderMgr::DOF_MAX_COF, CameraMaxCoF);
  8537. shaderp->uniform1f(LLShaderMgr::DOF_RES_SCALE, CameraDoFResScale);
  8538. gGL.begin(LLRender::TRIANGLE_STRIP);
  8539. gGL.texCoord2f(tc1.mV[0], tc1.mV[1]);
  8540. gGL.vertex2f(-1.f, -1.f);
  8541. gGL.texCoord2f(tc1.mV[0], tc2.mV[1]);
  8542. gGL.vertex2f(-1.f, 3.f);
  8543. gGL.texCoord2f(tc2.mV[0], tc1.mV[1]);
  8544. gGL.vertex2f(3.f, -1.f);
  8545. gGL.end();
  8546. unbindDeferredShader(*shaderp);
  8547. mRT->mDeferredLight.flush();
  8548. U32 dof_width = U32(mRT->mScreen.getWidth() * CameraDoFResScale);
  8549. U32 dof_height = U32(mRT->mScreen.getHeight() * CameraDoFResScale);
  8550. // Perform DoF sampling at half-res (preserve alpha channel)
  8551. mRT->mScreen.bindTarget();
  8552. glViewport(0, 0, dof_width, dof_height);
  8553. gGL.setColorMask(true, false);
  8554. shaderp = &gDeferredPostProgram;
  8555. bindDeferredShader(*shaderp);
  8556. channel = shaderp->enableTexture(LLShaderMgr::DEFERRED_DIFFUSE,
  8557. mRT->mDeferredLight.getUsage());
  8558. if (channel > -1)
  8559. {
  8560. mRT->mDeferredLight.bindTexture(0, channel);
  8561. }
  8562. shaderp->uniform1f(LLShaderMgr::DOF_MAX_COF, CameraMaxCoF);
  8563. shaderp->uniform1f(LLShaderMgr::DOF_RES_SCALE, CameraDoFResScale);
  8564. gGL.begin(LLRender::TRIANGLE_STRIP);
  8565. gGL.texCoord2f(tc1.mV[0], tc1.mV[1]);
  8566. gGL.vertex2f(-1.f, -1.f);
  8567. gGL.texCoord2f(tc1.mV[0], tc2.mV[1]);
  8568. gGL.vertex2f(-1.f, 3.f);
  8569. gGL.texCoord2f(tc2.mV[0], tc1.mV[1]);
  8570. gGL.vertex2f(3.f, -1.f);
  8571. gGL.end();
  8572. unbindDeferredShader(*shaderp);
  8573. mRT->mScreen.flush();
  8574. gGL.setColorMask(true, true);
  8575. // Combine result based on alpha
  8576. if (multisample)
  8577. {
  8578. mRT->mDeferredLight.bindTarget();
  8579. glViewport(0, 0, mRT->mDeferredScreen.getWidth(),
  8580. mRT->mDeferredScreen.getHeight());
  8581. }
  8582. else
  8583. {
  8584. gViewerWindowp->setupViewport();
  8585. }
  8586. shaderp = &gDeferredDoFCombineProgram;
  8587. bindDeferredShader(*shaderp);
  8588. channel = shaderp->enableTexture(LLShaderMgr::DEFERRED_DIFFUSE,
  8589. mRT->mScreen.getUsage());
  8590. if (channel > -1)
  8591. {
  8592. mRT->mScreen.bindTexture(0, channel);
  8593. }
  8594. shaderp->uniform1f(LLShaderMgr::DOF_MAX_COF, CameraMaxCoF);
  8595. shaderp->uniform1f(LLShaderMgr::DOF_RES_SCALE, CameraDoFResScale);
  8596. shaderp->uniform1f(LLShaderMgr::DOF_WIDTH, dof_width - 1);
  8597. shaderp->uniform1f(LLShaderMgr::DOF_HEIGHT, dof_height - 1);
  8598. gGL.begin(LLRender::TRIANGLE_STRIP);
  8599. gGL.texCoord2f(tc1.mV[0], tc1.mV[1]);
  8600. gGL.vertex2f(-1.f, -1.f);
  8601. gGL.texCoord2f(tc1.mV[0], tc2.mV[1]);
  8602. gGL.vertex2f(-1.f, 3.f);
  8603. gGL.texCoord2f(tc2.mV[0], tc1.mV[1]);
  8604. gGL.vertex2f(3.f, -1.f);
  8605. gGL.end();
  8606. unbindDeferredShader(*shaderp);
  8607. if (multisample)
  8608. {
  8609. mRT->mDeferredLight.flush();
  8610. }
  8611. }
  8612. else
  8613. {
  8614. if (multisample)
  8615. {
  8616. mRT->mDeferredLight.bindTarget();
  8617. }
  8618. LLGLSLShader* shaderp = &gDeferredPostNoDoFProgram;
  8619. bindDeferredShader(*shaderp);
  8620. S32 channel = shaderp->enableTexture(LLShaderMgr::DEFERRED_DIFFUSE,
  8621. mRT->mScreen.getUsage());
  8622. if (channel > -1)
  8623. {
  8624. mRT->mScreen.bindTexture(0, channel);
  8625. }
  8626. gGL.begin(LLRender::TRIANGLE_STRIP);
  8627. gGL.texCoord2f(tc1.mV[0], tc1.mV[1]);
  8628. gGL.vertex2f(-1.f, -1.f);
  8629. gGL.texCoord2f(tc1.mV[0], tc2.mV[1]);
  8630. gGL.vertex2f(-1.f, 3.f);
  8631. gGL.texCoord2f(tc2.mV[0], tc1.mV[1]);
  8632. gGL.vertex2f(3.f, -1.f);
  8633. gGL.end();
  8634. unbindDeferredShader(*shaderp);
  8635. if (multisample)
  8636. {
  8637. mRT->mDeferredLight.flush();
  8638. }
  8639. }
  8640. if (multisample)
  8641. {
  8642. static LLCachedControl<bool> use_smaa(gSavedSettings,
  8643. "RenderDeferredUseSMAA");
  8644. static LLCachedControl<bool> use_stencil(gSavedSettings,
  8645. "RenderDeferredUseSMAAStencil");
  8646. if (use_smaa && LLViewerShaderMgr::sHasSMAA && mAreaMap &&
  8647. mSearchMap && mSMAAEdgeBuffer.isComplete() &&
  8648. mSMAABlendBuffer.isComplete() && mScratchBuffer.isComplete())
  8649. {
  8650. // Note: all buffers got the same size.
  8651. S32 width = mRT->mScreen.getWidth();
  8652. S32 height = mRT->mScreen.getHeight();
  8653. mFXAABuffer.copyContents(mRT->mDeferredLight, 0, 0, width,
  8654. height, 0, 0, width, height,
  8655. GL_COLOR_BUFFER_BIT, GL_NEAREST);
  8656. LLGLDepthTest depth(GL_FALSE, GL_FALSE);
  8657. glClearColor(0.f, 0.f, 0.f, 0.f);
  8658. glViewport(0, 0, width, height);
  8659. F32 rt_metrics[] = { 1.f / width, 1.f / height,
  8660. (F32)width, (F32)height };
  8661. LLTexUnit* unit1 = gGL.getTexUnit(1);
  8662. LLTexUnit* unit2 = gGL.getTexUnit(2);
  8663. LLGLSLShader* shaderp;
  8664. LLRenderTarget* targetp;
  8665. {
  8666. LLGLState stencil(GL_STENCIL_TEST, use_stencil);
  8667. shaderp = &gPostSMAAEdgeDetect[RenderDeferredAAQuality];
  8668. shaderp->bind();
  8669. shaderp->uniform4fv(sSmaaRTMetrics, 1, rt_metrics);
  8670. mFXAABuffer.bindTexture(0, 0, LLTexUnit::TFO_BILINEAR);
  8671. unit0->setTextureAddressMode(LLTexUnit::TAM_CLAMP);
  8672. unit0->setTextureColorSpace(LLTexUnit::TCS_LINEAR);
  8673. targetp = &mSMAAEdgeBuffer;
  8674. targetp->bindTarget();
  8675. targetp->clear(GL_COLOR_BUFFER_BIT |
  8676. GL_STENCIL_BUFFER_BIT);
  8677. if (use_stencil)
  8678. {
  8679. glStencilFunc(GL_ALWAYS, 1, 0xFF);
  8680. glStencilOp(GL_KEEP, GL_KEEP, GL_REPLACE);
  8681. glStencilMask(0xFF);
  8682. }
  8683. mScreenTriangleVB->setBuffer(LLVertexBuffer::MAP_VERTEX);
  8684. mScreenTriangleVB->drawArrays(LLRender::TRIANGLES, 0, 3);
  8685. shaderp->unbind();
  8686. targetp->flush();
  8687. unit0->disable();
  8688. }
  8689. {
  8690. LLGLState stencil(GL_STENCIL_TEST, use_stencil);
  8691. shaderp = &gPostSMAABlendWeights[RenderDeferredAAQuality];
  8692. shaderp->bind();
  8693. shaderp->uniform4fv(sSmaaRTMetrics, 1, rt_metrics);
  8694. mSMAAEdgeBuffer.bindTexture(0, 0, LLTexUnit::TFO_BILINEAR);
  8695. unit0->setTextureAddressMode(LLTexUnit::TAM_CLAMP);
  8696. unit0->setTextureColorSpace(LLTexUnit::TCS_LINEAR);
  8697. unit1->bindManual(LLTexUnit::TT_TEXTURE, mAreaMap);
  8698. unit1->setTextureFilteringOption(LLTexUnit::TFO_BILINEAR);
  8699. unit1->setTextureAddressMode(LLTexUnit::TAM_CLAMP);
  8700. unit1->setTextureColorSpace(LLTexUnit::TCS_LINEAR);
  8701. unit2->bindManual(LLTexUnit::TT_TEXTURE, mSearchMap);
  8702. unit2->setTextureFilteringOption(LLTexUnit::TFO_BILINEAR);
  8703. unit2->setTextureAddressMode(LLTexUnit::TAM_CLAMP);
  8704. unit2->setTextureColorSpace(LLTexUnit::TCS_LINEAR);
  8705. targetp = &mSMAABlendBuffer;
  8706. targetp->bindTarget();
  8707. targetp->clear(GL_COLOR_BUFFER_BIT);
  8708. if (use_stencil)
  8709. {
  8710. glStencilFunc(GL_EQUAL, 1, 0xFF);
  8711. glStencilMask(0x00);
  8712. }
  8713. mScreenTriangleVB->setBuffer(LLVertexBuffer::MAP_VERTEX);
  8714. mScreenTriangleVB->drawArrays(LLRender::TRIANGLES, 0, 3);
  8715. if (use_stencil)
  8716. {
  8717. glStencilFunc(GL_ALWAYS, 0, 0xFF);
  8718. }
  8719. shaderp->unbind();
  8720. targetp->flush();
  8721. unit0->disable();
  8722. unit1->disable();
  8723. unit2->disable();
  8724. }
  8725. {
  8726. LLGLDisable stencil(GL_STENCIL_TEST);
  8727. shaderp =
  8728. &gPostSMAANeighborhoodBlend[RenderDeferredAAQuality];
  8729. shaderp->bind();
  8730. shaderp->uniform4fv(sSmaaRTMetrics, 1, rt_metrics);
  8731. mFXAABuffer.bindTexture(0, 0, LLTexUnit::TFO_BILINEAR);
  8732. unit0->setTextureAddressMode(LLTexUnit::TAM_CLAMP);
  8733. unit0->setTextureColorSpace(LLTexUnit::TCS_LINEAR);
  8734. mSMAABlendBuffer.bindTexture(0, 1,
  8735. LLTexUnit::TFO_BILINEAR);
  8736. unit1->setTextureAddressMode(LLTexUnit::TAM_CLAMP);
  8737. unit1->setTextureColorSpace(LLTexUnit::TCS_LINEAR);
  8738. if (RenderDeferredAASharpen)
  8739. {
  8740. targetp = &mScratchBuffer;
  8741. targetp->bindTarget();
  8742. targetp->clear(GL_COLOR_BUFFER_BIT);
  8743. }
  8744. else
  8745. {
  8746. gViewerWindowp->setupViewport();
  8747. }
  8748. mScreenTriangleVB->setBuffer(LLVertexBuffer::MAP_VERTEX);
  8749. mScreenTriangleVB->drawArrays(LLRender::TRIANGLES, 0, 3);
  8750. shaderp->unbind();
  8751. if (RenderDeferredAASharpen)
  8752. {
  8753. targetp->flush();
  8754. }
  8755. unit0->disable();
  8756. unit1->disable();
  8757. }
  8758. }
  8759. else if (LLViewerShaderMgr::sHasFXAA)
  8760. {
  8761. // Bake out texture2D with RGBL for FXAA shader
  8762. mFXAABuffer.bindTarget();
  8763. S32 width = mRT->mScreen.getWidth();
  8764. S32 height = mRT->mScreen.getHeight();
  8765. glViewport(0, 0, width, height);
  8766. LLGLSLShader* shaderp = &gGlowCombineFXAAProgram;
  8767. shaderp->bind();
  8768. shaderp->uniform2f(LLShaderMgr::DEFERRED_SCREEN_RES, width,
  8769. height);
  8770. S32 channel =
  8771. shaderp->enableTexture(LLShaderMgr::DEFERRED_DIFFUSE,
  8772. mRT->mDeferredLight.getUsage());
  8773. if (channel > -1)
  8774. {
  8775. mRT->mDeferredLight.bindTexture(0, channel);
  8776. }
  8777. mScreenTriangleVB->setBuffer(LLVertexBuffer::MAP_VERTEX);
  8778. mScreenTriangleVB->drawArrays(LLRender::TRIANGLES, 0, 3);
  8779. shaderp->disableTexture(LLShaderMgr::DEFERRED_DIFFUSE,
  8780. mRT->mDeferredLight.getUsage());
  8781. shaderp->unbind();
  8782. mFXAABuffer.flush();
  8783. shaderp = &gFXAAProgram[RenderDeferredAAQuality];
  8784. shaderp->bind();
  8785. channel = shaderp->enableTexture(LLShaderMgr::DIFFUSE_MAP,
  8786. mFXAABuffer.getUsage());
  8787. if (channel > -1)
  8788. {
  8789. mFXAABuffer.bindTexture(0, channel,
  8790. LLTexUnit::TFO_BILINEAR);
  8791. }
  8792. if (RenderDeferredAASharpen)
  8793. {
  8794. mScratchBuffer.bindTarget();
  8795. glClearColor(0.f, 0.f, 0.f, 0.f);
  8796. mScratchBuffer.clear(GL_COLOR_BUFFER_BIT);
  8797. }
  8798. else
  8799. {
  8800. gViewerWindowp->setupViewport();
  8801. }
  8802. F32 inv_width = 1.f / F32(mFXAABuffer.getWidth());
  8803. F32 inv_height = 1.f / F32(mFXAABuffer.getHeight());
  8804. F32 scale_x = F32(width) * inv_width;
  8805. F32 scale_y = F32(height) * inv_height;
  8806. shaderp->uniform2f(LLShaderMgr::FXAA_TC_SCALE, scale_x, scale_y);
  8807. shaderp->uniform2f(LLShaderMgr::FXAA_RCP_SCREEN_RES, inv_width,
  8808. inv_height);
  8809. shaderp->uniform4f(LLShaderMgr::FXAA_RCP_FRAME_OPT,
  8810. -0.5f * inv_width, -0.5f * inv_height,
  8811. 0.5f * inv_width, 0.5f * inv_height);
  8812. shaderp->uniform4f(LLShaderMgr::FXAA_RCP_FRAME_OPT2,
  8813. -2.f * inv_width, -2.f * inv_height,
  8814. 2.f * inv_width, 2.f * inv_height);
  8815. mScreenTriangleVB->setBuffer(LLVertexBuffer::MAP_VERTEX);
  8816. mScreenTriangleVB->drawArrays(LLRender::TRIANGLES, 0, 3);
  8817. shaderp->unbind();
  8818. if (RenderDeferredAASharpen)
  8819. {
  8820. mScratchBuffer.flush();
  8821. }
  8822. unit0->disable();
  8823. }
  8824. if (RenderDeferredAASharpen && LLViewerShaderMgr::sHasCAS)
  8825. {
  8826. LLGLDepthTest depth(GL_FALSE, GL_FALSE);
  8827. gPostCASProgram.bind();
  8828. static LLCachedControl<LLVector3>
  8829. cas_params(gSavedSettings, "RenderDeferredCASParams");
  8830. LLVector3 params = LLVector3(cas_params);
  8831. params.clamp(0.f, 1.f);
  8832. gPostCASProgram.uniform3fv(sSharpness, 1, params.mV);
  8833. mScratchBuffer.bindTexture(0, 0, LLTexUnit::TFO_BILINEAR);
  8834. unit0->setTextureAddressMode(LLTexUnit::TAM_CLAMP);
  8835. unit0->setTextureColorSpace(LLTexUnit::TCS_LINEAR);
  8836. gViewerWindowp->setupViewport();
  8837. mScreenTriangleVB->setBuffer(LLVertexBuffer::MAP_VERTEX);
  8838. mScreenTriangleVB->drawArrays(LLRender::TRIANGLES, 0, 3);
  8839. gPostCASProgram.unbind();
  8840. unit0->disable();
  8841. }
  8842. }
  8843. }
  8844. else
  8845. {
  8846. LLStrider<LLVector2> uv2;
  8847. if (mGlowCombineVB.isNull() ||
  8848. !mGlowCombineVB->getTexCoord1Strider(uv2))
  8849. {
  8850. return;
  8851. }
  8852. uv2[0].clear();
  8853. uv2[1] = LLVector2(0.f, tc2.mV[1] * 2.f);
  8854. uv2[2] = LLVector2(tc2.mV[0] * 2.f, 0.f);
  8855. LLGLDisable blend(GL_BLEND);
  8856. LLTexUnit* unit1 = gGL.getTexUnit(1);
  8857. gGlowCombineProgram.bind();
  8858. unit0->bind(&mGlow[1]);
  8859. unit1->bind(&mRT->mScreen);
  8860. LLGLEnable multisample(RenderFSAASamples > 0 ? GL_MULTISAMPLE : 0);
  8861. mGlowCombineVB->setBuffer(AUX_VB_MASK);
  8862. mGlowCombineVB->drawArrays(LLRender::TRIANGLE_STRIP, 0, 3);
  8863. gGlowCombineProgram.unbind();
  8864. }
  8865. gGL.setSceneBlendType(LLRender::BT_ALPHA);
  8866. if (hasRenderDebugMask(RENDER_DEBUG_PHYSICS_SHAPES))
  8867. {
  8868. gSplatTextureRectProgram.bind();
  8869. gGL.setColorMask(true, false);
  8870. LLVector2 tc1;
  8871. LLVector2 tc2((F32)gViewerWindowp->getWindowDisplayWidth() * 2,
  8872. (F32)gViewerWindowp->getWindowDisplayHeight() * 2);
  8873. LLGLEnable blend(GL_BLEND);
  8874. gGL.color4f(1.f, 1.f, 1.f, 0.75f);
  8875. unit0->bind(&mPhysicsDisplay);
  8876. gGL.begin(LLRender::TRIANGLES);
  8877. gGL.texCoord2f(tc1.mV[0], tc1.mV[1]);
  8878. gGL.vertex2f(-1.f, -1.f);
  8879. gGL.texCoord2f(tc1.mV[0], tc2.mV[1]);
  8880. gGL.vertex2f(-1.f, 3.f);
  8881. gGL.texCoord2f(tc2.mV[0], tc1.mV[1]);
  8882. gGL.vertex2f(3.f, -1.f);
  8883. gGL.end(true);
  8884. gSplatTextureRectProgram.unbind();
  8885. }
  8886. if (LLRenderTarget::sUseFBO && mRT->mScreen.getFBO())
  8887. {
  8888. // Copy depth buffer from mScreen to framebuffer
  8889. LLRenderTarget::copyContentsToFramebuffer(mRT->mScreen, 0, 0,
  8890. mRT->mScreen.getWidth(),
  8891. mRT->mScreen.getHeight(),
  8892. 0, 0,
  8893. mRT->mScreen.getWidth(),
  8894. mRT->mScreen.getHeight(),
  8895. GL_DEPTH_BUFFER_BIT,
  8896. GL_NEAREST);
  8897. }
  8898. gGL.matrixMode(LLRender::MM_PROJECTION);
  8899. gGL.popMatrix();
  8900. gGL.matrixMode(LLRender::MM_MODELVIEW);
  8901. gGL.popMatrix();
  8902. LLVertexBuffer::unbind();
  8903. LL_GL_CHECK_STATES;
  8904. }
  8905. void LLPipeline::bindLightFunc(LLGLSLShader& shader)
  8906. {
  8907. S32 channel = shader.enableTexture(LLShaderMgr::DEFERRED_LIGHTFUNC);
  8908. if (channel > -1)
  8909. {
  8910. gGL.getTexUnit(channel)->bindManual(LLTexUnit::TT_TEXTURE, mLightFunc);
  8911. }
  8912. if (gUsePBRShaders)
  8913. {
  8914. channel = shader.enableTexture(LLShaderMgr::DEFERRED_BRDF_LUT,
  8915. LLTexUnit::TT_TEXTURE);
  8916. if (channel > -1)
  8917. {
  8918. mPbrBrdfLut.bindTexture(0, channel);
  8919. }
  8920. }
  8921. stop_glerror();
  8922. }
  8923. void LLPipeline::bindShadowMaps(LLGLSLShader& shader)
  8924. {
  8925. if (gUsePBRShaders)
  8926. {
  8927. for (U32 i = 0; i < 4; ++i)
  8928. {
  8929. LLRenderTarget* shadow_targetp = &mRT->mSunShadow[i];
  8930. if (!shadow_targetp) continue;
  8931. S32 chan = shader.enableTexture(LLShaderMgr::DEFERRED_SHADOW0 + i,
  8932. LLTexUnit::TT_TEXTURE);
  8933. if (chan > -1)
  8934. {
  8935. gGL.getTexUnit(chan)->bind(shadow_targetp, true);
  8936. }
  8937. }
  8938. for (U32 i = 4; i < 6; ++i)
  8939. {
  8940. S32 chan = shader.enableTexture(LLShaderMgr::DEFERRED_SHADOW0 + i);
  8941. if (chan > -1)
  8942. {
  8943. LLRenderTarget* shadow_targetp = &mSpotShadow[i - 4];
  8944. if (shadow_targetp)
  8945. {
  8946. gGL.getTexUnit(chan)->bind(shadow_targetp, true);
  8947. }
  8948. }
  8949. }
  8950. stop_glerror();
  8951. return;
  8952. }
  8953. for (U32 i = 0; i < 4; ++i)
  8954. {
  8955. LLRenderTarget* shadow_targetp = &mShadow[i];
  8956. if (!shadow_targetp) continue;
  8957. S32 chan = shader.enableTexture(LLShaderMgr::DEFERRED_SHADOW0 + i,
  8958. LLTexUnit::TT_TEXTURE);
  8959. if (chan <= -1) continue;
  8960. LLTexUnit* unitp = gGL.getTexUnit(chan);
  8961. unitp->bind(shadow_targetp, true);
  8962. unitp->setTextureFilteringOption(LLTexUnit::TFO_BILINEAR);
  8963. unitp->setTextureAddressMode(LLTexUnit::TAM_CLAMP);
  8964. glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_COMPARE_MODE,
  8965. GL_COMPARE_R_TO_TEXTURE);
  8966. glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_COMPARE_FUNC, GL_LEQUAL);
  8967. }
  8968. for (U32 i = 4; i < 6; ++i)
  8969. {
  8970. S32 chan = shader.enableTexture(LLShaderMgr::DEFERRED_SHADOW0 + i);
  8971. if (chan <= -1) continue;
  8972. LLRenderTarget* shadow_targetp = &mShadow[i];
  8973. if (!shadow_targetp) continue;
  8974. LLTexUnit* unitp = gGL.getTexUnit(chan);
  8975. unitp->bind(shadow_targetp);
  8976. unitp->setTextureFilteringOption(LLTexUnit::TFO_BILINEAR);
  8977. unitp->setTextureAddressMode(LLTexUnit::TAM_CLAMP);
  8978. glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_COMPARE_MODE,
  8979. GL_COMPARE_R_TO_TEXTURE);
  8980. glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_COMPARE_FUNC, GL_LEQUAL);
  8981. }
  8982. stop_glerror();
  8983. }
  8984. void LLPipeline::setEnvMat(LLGLSLShader& shader)
  8985. {
  8986. F32* m = gGLModelView.getF32ptr();
  8987. F32 mat[] =
  8988. {
  8989. m[0], m[1], m[2],
  8990. m[4], m[5], m[6],
  8991. m[8], m[9], m[10]
  8992. };
  8993. shader.uniformMatrix3fv(LLShaderMgr::DEFERRED_ENV_MAT, 1, GL_TRUE, mat);
  8994. }
  8995. void LLPipeline::bindReflectionProbes(LLGLSLShader& shader)
  8996. {
  8997. if (!sReflectionProbesEnabled)
  8998. {
  8999. return;
  9000. }
  9001. bool bound = false;
  9002. S32 chan = shader.enableTexture(LLShaderMgr::REFLECTION_PROBES,
  9003. LLTexUnit::TT_CUBE_MAP_ARRAY);
  9004. if (chan > -1 && mReflectionMapManager.mTexture.notNull())
  9005. {
  9006. mReflectionMapManager.mTexture->bind(chan);
  9007. bound = true;
  9008. }
  9009. chan = shader.enableTexture(LLShaderMgr::IRRADIANCE_PROBES,
  9010. LLTexUnit::TT_CUBE_MAP_ARRAY);
  9011. if (chan > -1 && mReflectionMapManager.mIrradianceMaps.notNull())
  9012. {
  9013. mReflectionMapManager.mIrradianceMaps->bind(chan);
  9014. bound = true;
  9015. }
  9016. if (sRenderMirrors)
  9017. {
  9018. chan = shader.enableTexture(LLShaderMgr::HERO_PROBE,
  9019. LLTexUnit::TT_CUBE_MAP_ARRAY);
  9020. if (chan > -1 && mHeroProbeManager.mTexture.notNull())
  9021. {
  9022. mHeroProbeManager.mTexture->bind(chan);
  9023. bound = true;
  9024. }
  9025. }
  9026. if (bound)
  9027. {
  9028. mReflectionMapManager.setUniforms();
  9029. setEnvMat(shader);
  9030. }
  9031. // Reflection probe shaders generally sample the scene map as well for SSR
  9032. chan = shader.enableTexture(LLShaderMgr::SCENE_MAP);
  9033. if (chan > -1)
  9034. {
  9035. gGL.getTexUnit(chan)->bind(&mSceneMap);
  9036. }
  9037. shader.uniform1f(LLShaderMgr::DEFERRED_SSR_ITR_COUNT,
  9038. RenderScreenSpaceReflectionIterations);
  9039. shader.uniform1f(LLShaderMgr::DEFERRED_SSR_DIST_BIAS,
  9040. RenderScreenSpaceReflectionDistanceBias);
  9041. shader.uniform1f(LLShaderMgr::DEFERRED_SSR_RAY_STEP,
  9042. RenderScreenSpaceReflectionRayStep);
  9043. shader.uniform1f(LLShaderMgr::DEFERRED_SSR_GLOSSY_SAMPLES,
  9044. RenderScreenSpaceReflectionGlossySamples);
  9045. shader.uniform1f(LLShaderMgr::DEFERRED_SSR_REJECT_BIAS,
  9046. RenderScreenSpaceReflectionDepthRejectBias);
  9047. if (++mPoissonOffset + RenderScreenSpaceReflectionGlossySamples > 128)
  9048. {
  9049. mPoissonOffset = 0;
  9050. }
  9051. shader.uniform1f(LLShaderMgr::DEFERRED_SSR_NOISE_SINE,
  9052. F32(mPoissonOffset));
  9053. shader.uniform1f(LLShaderMgr::DEFERRED_SSR_ADAPTIVE_STEP_MULT,
  9054. RenderScreenSpaceReflectionAdaptiveStepMultiplier);
  9055. chan = shader.enableTexture(LLShaderMgr::SCENE_DEPTH);
  9056. if (chan > -1)
  9057. {
  9058. gGL.getTexUnit(chan)->bind(&mSceneMap, true);
  9059. }
  9060. }
  9061. void LLPipeline::unbindReflectionProbes(LLGLSLShader& shader)
  9062. {
  9063. S32 chan = shader.disableTexture(LLShaderMgr::REFLECTION_PROBES,
  9064. LLTexUnit::TT_CUBE_MAP);
  9065. if (chan > -1 && mReflectionMapManager.mTexture.notNull())
  9066. {
  9067. mReflectionMapManager.mTexture->unbind();
  9068. if (chan == 0)
  9069. {
  9070. gGL.getTexUnit(0)->enable(LLTexUnit::TT_TEXTURE);
  9071. }
  9072. }
  9073. }
  9074. void LLPipeline::bindDeferredShaderFast(LLGLSLShader& shader)
  9075. {
  9076. // Note: what happens if a shader is flagged mCanBindFast with mMainRT,
  9077. // then reused just after with mAuxillaryRT ?... The wrong render target
  9078. // would be bound and used. I therefore added the is_main_rt tests. HB
  9079. bool is_main_rt = mRT == &mMainRT;
  9080. if (is_main_rt && shader.mCanBindFast)
  9081. {
  9082. shader.bind();
  9083. bindLightFunc(shader);
  9084. bindShadowMaps(shader);
  9085. if (gUsePBRShaders)
  9086. {
  9087. bindReflectionProbes(shader);
  9088. }
  9089. }
  9090. else
  9091. {
  9092. // Was not previously bound, use slow path
  9093. bindDeferredShader(shader);
  9094. shader.mCanBindFast = is_main_rt;
  9095. }
  9096. }
  9097. void LLPipeline::bindDeferredShader(LLGLSLShader& shader,
  9098. LLRenderTarget* light_targetp,
  9099. LLRenderTarget* depth_targetp)
  9100. {
  9101. LL_FAST_TIMER(FTM_BIND_DEFERRED);
  9102. LLRenderTarget* deferred_targetp = &mRT->mDeferredScreen;
  9103. // Note: the EE renderer uses a different buffer for depth target.
  9104. if (!gUsePBRShaders)
  9105. {
  9106. depth_targetp = &mDeferredDepth;
  9107. }
  9108. else if (!depth_targetp)
  9109. {
  9110. depth_targetp = deferred_targetp;
  9111. }
  9112. shader.bind();
  9113. LLTexUnit::eTextureType usage = deferred_targetp->getUsage();
  9114. S32 chan = shader.enableTexture(LLShaderMgr::DEFERRED_DIFFUSE, usage);
  9115. if (chan > -1)
  9116. {
  9117. deferred_targetp->bindTexture(0, chan, LLTexUnit::TFO_POINT);
  9118. if (gUsePBRShaders)
  9119. {
  9120. gGL.getTexUnit(chan)->setTextureAddressMode(LLTexUnit::TAM_CLAMP);
  9121. }
  9122. }
  9123. chan = shader.enableTexture(LLShaderMgr::DEFERRED_SPECULAR, usage);
  9124. if (chan > -1)
  9125. {
  9126. deferred_targetp->bindTexture(1, chan, LLTexUnit::TFO_POINT);
  9127. if (gUsePBRShaders)
  9128. {
  9129. gGL.getTexUnit(chan)->setTextureAddressMode(LLTexUnit::TAM_CLAMP);
  9130. }
  9131. }
  9132. chan = shader.enableTexture(LLShaderMgr::DEFERRED_NORMAL, usage);
  9133. if (chan > -1)
  9134. {
  9135. deferred_targetp->bindTexture(2, chan, LLTexUnit::TFO_POINT);
  9136. if (gUsePBRShaders)
  9137. {
  9138. gGL.getTexUnit(chan)->setTextureAddressMode(LLTexUnit::TAM_CLAMP);
  9139. }
  9140. }
  9141. if (gUsePBRShaders)
  9142. {
  9143. chan = shader.enableTexture(LLShaderMgr::DEFERRED_EMISSIVE, usage);
  9144. if (chan > -1)
  9145. {
  9146. deferred_targetp->bindTexture(3, chan, LLTexUnit::TFO_POINT);
  9147. gGL.getTexUnit(chan)->setTextureAddressMode(LLTexUnit::TAM_CLAMP);
  9148. }
  9149. }
  9150. chan = shader.enableTexture(LLShaderMgr::DEFERRED_DEPTH,
  9151. depth_targetp->getUsage());
  9152. if (chan > -1)
  9153. {
  9154. gGL.getTexUnit(chan)->bind(depth_targetp, true);
  9155. stop_glerror();
  9156. }
  9157. if (gUsePBRShaders)
  9158. {
  9159. chan = shader.enableTexture(LLShaderMgr::EXPOSURE_MAP);
  9160. if (chan > -1)
  9161. {
  9162. gGL.getTexUnit(chan)->bind(&mExposureMap);
  9163. }
  9164. }
  9165. else if (shader.getUniformLocation(LLShaderMgr::INVERSE_PROJECTION_MATRIX) != -1)
  9166. {
  9167. // This is done in LLRender::syncMatrices() for PBR shaders, but would
  9168. // not be enough for EE rendering, due to a bug involving the deferred
  9169. // renderer regular pushing and popping of matrices.
  9170. LLMatrix4a inv_proj = gGLProjection;
  9171. inv_proj.invert();
  9172. shader.uniformMatrix4fv(LLShaderMgr::INVERSE_PROJECTION_MATRIX, 1,
  9173. GL_FALSE, inv_proj.getF32ptr());
  9174. }
  9175. if (shader.getUniformLocation(LLShaderMgr::VIEWPORT) != -1)
  9176. {
  9177. shader.uniform4f(LLShaderMgr::VIEWPORT, (F32)gGLViewport[0],
  9178. (F32)gGLViewport[1], (F32)gGLViewport[2],
  9179. (F32)gGLViewport[3]);
  9180. }
  9181. // Note: mReflectionModelView is never calculated for PBR rendering, and
  9182. // sReflectionRender is normally never true, but I added !gUsePBRShaders
  9183. // for good measure. HB
  9184. if (!gUsePBRShaders && sReflectionRender &&
  9185. shader.getUniformLocation(LLShaderMgr::MODELVIEW_MATRIX) != -1)
  9186. {
  9187. shader.uniformMatrix4fv(LLShaderMgr::MODELVIEW_MATRIX, 1, GL_FALSE,
  9188. mReflectionModelView.getF32ptr());
  9189. }
  9190. chan = shader.enableTexture(LLShaderMgr::DEFERRED_NOISE);
  9191. if (chan > -1)
  9192. {
  9193. LLTexUnit* unitp = gGL.getTexUnit(chan);
  9194. unitp->bindManual(LLTexUnit::TT_TEXTURE, mNoiseMap);
  9195. unitp->setTextureFilteringOption(LLTexUnit::TFO_POINT);
  9196. }
  9197. bindLightFunc(shader);
  9198. if (!light_targetp)
  9199. {
  9200. light_targetp = &mRT->mDeferredLight;
  9201. }
  9202. chan = shader.enableTexture(LLShaderMgr::DEFERRED_LIGHT,
  9203. light_targetp->getUsage());
  9204. if (chan > -1)
  9205. {
  9206. if (!gUsePBRShaders || light_targetp->isComplete())
  9207. {
  9208. light_targetp->bindTexture(0, chan, LLTexUnit::TFO_POINT);
  9209. }
  9210. else
  9211. {
  9212. gGL.getTexUnit(chan)->bindFast(LLViewerFetchedTexture::sWhiteImagep);
  9213. }
  9214. }
  9215. if (!gUsePBRShaders)
  9216. {
  9217. chan = shader.enableTexture(LLShaderMgr::DEFERRED_BLOOM);
  9218. if (chan > -1)
  9219. {
  9220. mGlow[1].bindTexture(0, chan);
  9221. }
  9222. }
  9223. stop_glerror();
  9224. #if OPTIMIZED_UNIFORMS
  9225. if (shader.mFeatures.hasShadows)
  9226. #endif
  9227. {
  9228. bindShadowMaps(shader);
  9229. F32 mat[16 * 6];
  9230. constexpr size_t total_bytes = sizeof(F32) * 16 * 6;
  9231. memcpy((void*)mat, (void*)mSunShadowMatrix, total_bytes);
  9232. shader.uniformMatrix4fv(LLShaderMgr::DEFERRED_SHADOW_MATRIX, 6,
  9233. GL_FALSE, mat);
  9234. shader.uniform4fv(LLShaderMgr::DEFERRED_SHADOW_CLIP, 1,
  9235. mSunClipPlanes.mV);
  9236. if (gUsePBRShaders)
  9237. {
  9238. shader.uniform2f(LLShaderMgr::DEFERRED_SHADOW_RES,
  9239. mRT->mSunShadow[0].getWidth(),
  9240. mRT->mSunShadow[0].getHeight());
  9241. shader.uniform2f(LLShaderMgr::DEFERRED_PROJ_SHADOW_RES,
  9242. mSpotShadow[0].getWidth(),
  9243. mSpotShadow[0].getHeight());
  9244. }
  9245. else
  9246. {
  9247. shader.uniform2f(LLShaderMgr::DEFERRED_SHADOW_RES,
  9248. mShadow[0].getWidth(),
  9249. mShadow[0].getHeight());
  9250. shader.uniform2f(LLShaderMgr::DEFERRED_PROJ_SHADOW_RES,
  9251. mShadow[4].getWidth(),
  9252. mShadow[4].getHeight());
  9253. }
  9254. shader.uniform1f(LLShaderMgr::DEFERRED_SHADOW_OFFSET,
  9255. RenderDeferredSSAO ? RenderShadowOffset
  9256. : RenderShadowOffsetNoSSAO);
  9257. constexpr F32 ONEBYTHREETHOUSANDS = 1.f / 3000.f;
  9258. F32 shadow_bias_error = RenderShadowBiasError * ONEBYTHREETHOUSANDS *
  9259. fabsf(gViewerCamera.getOrigin().mV[2]);
  9260. shader.uniform1f(LLShaderMgr::DEFERRED_SHADOW_BIAS,
  9261. RenderShadowBias + shadow_bias_error);
  9262. shader.uniform1f(LLShaderMgr::DEFERRED_SPOT_SHADOW_OFFSET,
  9263. RenderSpotShadowOffset);
  9264. shader.uniform1f(LLShaderMgr::DEFERRED_SPOT_SHADOW_BIAS,
  9265. RenderSpotShadowBias);
  9266. }
  9267. #if DEBUG_OPTIMIZED_UNIFORMS && OPTIMIZED_UNIFORMS
  9268. else if (shader.getUniformLocation(LLShaderMgr::DEFERRED_SHADOW0) >= 0 ||
  9269. shader.getUniformLocation(LLShaderMgr::DEFERRED_SHADOW_MATRIX) >= 0 ||
  9270. shader.getUniformLocation(LLShaderMgr::DEFERRED_SHADOW_CLIP) >= 0 ||
  9271. shader.getUniformLocation(LLShaderMgr::DEFERRED_SHADOW_RES) >= 0 ||
  9272. shader.getUniformLocation(LLShaderMgr::DEFERRED_SHADOW_OFFSET) >= 0 ||
  9273. shader.getUniformLocation(LLShaderMgr::DEFERRED_SHADOW_BIAS) >= 0 ||
  9274. shader.getUniformLocation(LLShaderMgr::DEFERRED_SPOT_SHADOW_OFFSET) >= 0 ||
  9275. shader.getUniformLocation(LLShaderMgr::DEFERRED_SPOT_SHADOW_BIAS) >= 0)
  9276. {
  9277. llwarns_once << "Shader: " << shader.mName
  9278. << " shall be marked as hasShadows !" << llendl;
  9279. }
  9280. #endif
  9281. if (!sReflectionProbesEnabled)
  9282. {
  9283. chan = shader.enableTexture(LLShaderMgr::ENVIRONMENT_MAP,
  9284. LLTexUnit::TT_CUBE_MAP);
  9285. if (chan > -1)
  9286. {
  9287. LLCubeMap* cube_mapp = gSky.mVOSkyp ? gSky.mVOSkyp->getCubeMap()
  9288. : NULL;
  9289. if (cube_mapp)
  9290. {
  9291. cube_mapp->enableTexture(chan);
  9292. cube_mapp->bind();
  9293. const F32* m = gGLModelView.getF32ptr();
  9294. F32 mat[] = { m[0], m[1], m[2],
  9295. m[4], m[5], m[6],
  9296. m[8], m[9], m[10] };
  9297. shader.uniformMatrix3fv(LLShaderMgr::DEFERRED_ENV_MAT, 1,
  9298. GL_TRUE, mat);
  9299. }
  9300. }
  9301. }
  9302. if (gUsePBRShaders)
  9303. {
  9304. bindReflectionProbes(shader);
  9305. shader.uniformMatrix4fv(LLShaderMgr::MODELVIEW_DELTA_MATRIX, 1,
  9306. GL_FALSE, gGLDeltaModelView.getF32ptr());
  9307. shader.uniformMatrix4fv(LLShaderMgr::INVERSE_MODELVIEW_DELTA_MATRIX, 1,
  9308. GL_FALSE,
  9309. gGLInverseDeltaModelView.getF32ptr());
  9310. shader.uniform1i(LLShaderMgr::CUBE_SNAPSHOT, gCubeSnapshot ? 1 : 0);
  9311. }
  9312. shader.uniform1f(LLShaderMgr::DEFERRED_SUN_WASH, RenderDeferredSunWash);
  9313. shader.uniform1f(LLShaderMgr::DEFERRED_SHADOW_NOISE, RenderShadowNoise);
  9314. shader.uniform1f(LLShaderMgr::DEFERRED_BLUR_SIZE, RenderShadowBlurSize);
  9315. #if OPTIMIZED_UNIFORMS
  9316. if (shader.mFeatures.hasAmbientOcclusion)
  9317. #endif
  9318. {
  9319. shader.uniform1f(LLShaderMgr::DEFERRED_SSAO_RADIUS, RenderSSAOScale);
  9320. shader.uniform1f(LLShaderMgr::DEFERRED_SSAO_MAX_RADIUS,
  9321. RenderSSAOMaxScale);
  9322. shader.uniform1f(LLShaderMgr::DEFERRED_SSAO_FACTOR, RenderSSAOFactor);
  9323. }
  9324. #if DEBUG_OPTIMIZED_UNIFORMS && OPTIMIZED_UNIFORMS
  9325. else if (shader.getUniformLocation(LLShaderMgr::DEFERRED_SSAO_RADIUS) >= 0 ||
  9326. shader.getUniformLocation(LLShaderMgr::DEFERRED_SSAO_MAX_RADIUS) >= 0 ||
  9327. shader.getUniformLocation(LLShaderMgr::DEFERRED_SSAO_FACTOR) >= 0)
  9328. {
  9329. llwarns_once << "Shader: " << shader.mName
  9330. << " shall be marked as hasAmbientOcclusion !" << llendl;
  9331. }
  9332. #endif
  9333. constexpr F32 ONETHIRD = 1.f / 3.f;
  9334. F32 matrix_diag = (RenderSSAOEffect[0] + 2.f * RenderSSAOEffect[1]) *
  9335. ONETHIRD;
  9336. F32 matrix_nondiag = (RenderSSAOEffect[0] - RenderSSAOEffect[1]) *
  9337. ONETHIRD;
  9338. // This matrix scales (proj of color onto <1/rt(3),1/rt(3),1/rt(3)>) by
  9339. // value factor, and scales remainder by saturation factor
  9340. F32 ssao_effect_mat[] =
  9341. {
  9342. matrix_diag, matrix_nondiag, matrix_nondiag,
  9343. matrix_nondiag, matrix_diag, matrix_nondiag,
  9344. matrix_nondiag, matrix_nondiag, matrix_diag
  9345. };
  9346. shader.uniformMatrix3fv(LLShaderMgr::DEFERRED_SSAO_EFFECT_MAT, 1, GL_FALSE,
  9347. ssao_effect_mat);
  9348. shader.uniform2f(LLShaderMgr::DEFERRED_SCREEN_RES,
  9349. deferred_targetp->getWidth(),
  9350. deferred_targetp->getHeight());
  9351. shader.uniform1f(LLShaderMgr::DEFERRED_NEAR_CLIP,
  9352. gViewerCamera.getNear() * 2.f);
  9353. shader.uniform3fv(LLShaderMgr::DEFERRED_SUN_DIR, 1,
  9354. mTransformedSunDir.getF32ptr());
  9355. shader.uniform3fv(LLShaderMgr::DEFERRED_MOON_DIR, 1,
  9356. mTransformedMoonDir.getF32ptr());
  9357. if (shader.getUniformLocation(LLShaderMgr::DEFERRED_NORM_MATRIX) > -1)
  9358. {
  9359. LLMatrix4a norm_mat = gGLModelView;
  9360. norm_mat.invert();
  9361. norm_mat.transpose();
  9362. shader.uniformMatrix4fv(LLShaderMgr::DEFERRED_NORM_MATRIX, 1, GL_FALSE,
  9363. norm_mat.getF32ptr());
  9364. }
  9365. if (gUsePBRShaders)
  9366. {
  9367. shader.uniform3fv(LLShaderMgr::SUNLIGHT_COLOR, 1,
  9368. mSunLightColorScaled.mV);
  9369. shader.uniform3fv(LLShaderMgr::MOONLIGHT_COLOR, 1, mMoonLightColor.mV);
  9370. shader.uniform1f(LLShaderMgr::REFLECTION_PROBE_MAX_LOD,
  9371. mReflectionMapManager.mMaxProbeLOD);
  9372. }
  9373. else
  9374. {
  9375. shader.uniform4fv(LLShaderMgr::SUNLIGHT_COLOR, 1, mSunLightColor.mV);
  9376. shader.uniform4fv(LLShaderMgr::MOONLIGHT_COLOR, 1, mMoonLightColor.mV);
  9377. }
  9378. gEnvironment.updateShaderSkyUniforms(&shader);
  9379. }
  9380. // Branched version for the PBR renderer
  9381. void LLPipeline::renderDeferredLightingPBR()
  9382. {
  9383. F32 light_scale = gCubeSnapshot ? mReflectionMapManager.mLightScale : 1.f;
  9384. LLRenderTarget* screen_targetp = &mRT->mScreen;
  9385. LLRenderTarget* light_targetp = &mRT->mDeferredLight;
  9386. LLGLSLShader* shaderp;
  9387. {
  9388. LL_FAST_TIMER(FTM_RENDER_DEFERRED);
  9389. if (hasRenderType(LLPipeline::RENDER_TYPE_HUD))
  9390. {
  9391. toggleRenderType(LLPipeline::RENDER_TYPE_HUD);
  9392. }
  9393. gGL.setColorMask(true, true);
  9394. // Draw a cube around every light
  9395. LLVertexBuffer::unbind();
  9396. LLGLEnable cull(GL_CULL_FACE);
  9397. LLGLEnable blend(GL_BLEND);
  9398. setupHWLights();
  9399. mTransformedSunDir.loadua(mSunDir.mV);
  9400. gGLModelView.rotate(mTransformedSunDir, mTransformedSunDir);
  9401. mTransformedMoonDir.loadua(mMoonDir.mV);
  9402. gGLModelView.rotate(mTransformedMoonDir, mTransformedMoonDir);
  9403. if (RenderDeferredSSAO || RenderShadowDetail > 0)
  9404. {
  9405. // Paint shadow/SSAO light map (direct lighting lightmap)
  9406. LL_FAST_TIMER(FTM_SUN_SHADOW);
  9407. light_targetp->bindTarget();
  9408. shaderp = &gDeferredSunProgram;
  9409. bindDeferredShader(*shaderp, light_targetp);
  9410. mScreenTriangleVB->setBuffer();
  9411. glClearColor(1.f, 1.f, 1.f, 1.f);
  9412. light_targetp->clear(GL_COLOR_BUFFER_BIT);
  9413. glClearColor(0.f, 0.f, 0.f, 0.f);
  9414. #if 0 // The "offset" uniform does not appear to be used... HB
  9415. static LLStaticHashedString sOffset("offset");
  9416. LLMatrix4a inv_trans = gGLModelView;
  9417. inv_trans.invert();
  9418. inv_trans.transpose();
  9419. constexpr U32 slices = 32;
  9420. F32 offset[slices * 3];
  9421. constexpr F32 indice_to_rad = F_PI / 4.f;
  9422. for (U32 i = 0; i < 4; ++i)
  9423. {
  9424. F32 z = -i;
  9425. for (U32 j = 0; j < 8; ++j)
  9426. {
  9427. F32 angle = indice_to_rad * j;
  9428. LLVector4a v(sinf(angle), cosf(angle), z);
  9429. inv_trans.affineTransform(v, v);
  9430. v.normalize3();
  9431. U32 idx = 3 * (8 * i + j);
  9432. offset[idx++] = v[0];
  9433. offset[idx++] = v[2];
  9434. offset[idx] = v[1];
  9435. }
  9436. }
  9437. shaderp->uniform3fv(sOffset, slices, offset);
  9438. #endif
  9439. shaderp->uniform2f(LLShaderMgr::DEFERRED_SCREEN_RES,
  9440. light_targetp->getWidth(),
  9441. light_targetp->getHeight());
  9442. {
  9443. LLGLDisable blend(GL_BLEND);
  9444. LLGLDepthTest depth(GL_TRUE, GL_FALSE, GL_ALWAYS);
  9445. mScreenTriangleVB->drawArrays(LLRender::TRIANGLES, 0, 3);
  9446. }
  9447. unbindDeferredShader(*shaderp);
  9448. light_targetp->flush();
  9449. }
  9450. if (RenderShadowSoften)
  9451. {
  9452. // Soften direct lighting lightmap
  9453. LL_FAST_TIMER(FTM_SOFTEN_SHADOW);
  9454. // Blur lightmap
  9455. screen_targetp->bindTarget();
  9456. glClearColor(1.f, 1.f, 1.f, 1.f);
  9457. screen_targetp->clear(GL_COLOR_BUFFER_BIT);
  9458. glClearColor(0.f, 0.f, 0.f, 0.f);
  9459. shaderp = &gDeferredBlurLightProgram;
  9460. bindDeferredShader(*shaderp);
  9461. // Sample symmetrically with the middle sample falling exactly on
  9462. // 0.0
  9463. F32 x = 0.f;
  9464. F32 shadow_gaussian_x = RenderShadowGaussian.mV[0];
  9465. F32 shadow_gaussian_y = RenderShadowGaussian.mV[1];
  9466. constexpr U32 kern_length = 4;
  9467. LLVector3 gauss[kern_length]; // xweight, yweight, offset
  9468. for (U32 i = 0; i < kern_length; ++i)
  9469. {
  9470. gauss[i].mV[0] = llgaussian(x, shadow_gaussian_x);
  9471. gauss[i].mV[1] = llgaussian(x, shadow_gaussian_y);
  9472. gauss[i].mV[2] = x;
  9473. x += 1.f;
  9474. }
  9475. shaderp->uniform2f(sDelta, 1.f, 0.f);
  9476. shaderp->uniform1f(sDistFactor, RenderShadowBlurDistFactor);
  9477. shaderp->uniform3fv(sKern, kern_length, gauss[0].mV);
  9478. shaderp->uniform1f(sKernScale,
  9479. RenderShadowBlurSize *
  9480. (kern_length * 0.5f - 0.5f));
  9481. {
  9482. LLGLDisable blend(GL_BLEND);
  9483. LLGLDepthTest depth(GL_TRUE, GL_FALSE, GL_ALWAYS);
  9484. mScreenTriangleVB->setBuffer();
  9485. mScreenTriangleVB->drawArrays(LLRender::TRIANGLES, 0, 3);
  9486. }
  9487. screen_targetp->flush();
  9488. unbindDeferredShader(*shaderp);
  9489. bindDeferredShader(*shaderp, screen_targetp);
  9490. light_targetp->bindTarget();
  9491. shaderp->uniform2f(sDelta, 0.f, 1.f);
  9492. {
  9493. LLGLDisable blend(GL_BLEND);
  9494. LLGLDepthTest depth(GL_TRUE, GL_FALSE, GL_ALWAYS);
  9495. mScreenTriangleVB->setBuffer();
  9496. mScreenTriangleVB->drawArrays(LLRender::TRIANGLES, 0, 3);
  9497. }
  9498. light_targetp->flush();
  9499. unbindDeferredShader(*shaderp);
  9500. }
  9501. screen_targetp->bindTarget();
  9502. // Clear color buffer here; zeroing alpha (glow) is important or it
  9503. // will accumulate against sky
  9504. glClearColor(0.f, 0.f, 0.f, 0.f);
  9505. screen_targetp->clear(GL_COLOR_BUFFER_BIT);
  9506. if (RenderDeferredAtmospheric)
  9507. {
  9508. // Apply sunlight contribution
  9509. LL_FAST_TIMER(FTM_ATMOSPHERICS);
  9510. shaderp = &gDeferredSoftenProgram;
  9511. bindDeferredShader(*shaderp);
  9512. static LLCachedControl<F32> ssao_scale(gSavedSettings,
  9513. "RenderSSAOIrradianceScale");
  9514. static LLCachedControl<F32> ssao_max(gSavedSettings,
  9515. "RenderSSAOIrradianceMax");
  9516. shaderp->uniform1f(sIrradianceScale, ssao_scale);
  9517. shaderp->uniform1f(sIrradianceMax, ssao_max);
  9518. shaderp->uniform1i(LLShaderMgr::SUN_UP_FACTOR, mIsSunUp ? 1 : 0);
  9519. shaderp->uniform3fv(LLShaderMgr::LIGHTNORM, 1,
  9520. gEnvironment.getClampedLightNorm().mV);
  9521. shaderp->uniform4fv(LLShaderMgr::WATER_WATERPLANE, 1,
  9522. sWaterPlane.getF32ptr());
  9523. {
  9524. LLGLDepthTest depth(GL_FALSE);
  9525. LLGLDisable blend(GL_BLEND);
  9526. // Full screen blit
  9527. mScreenTriangleVB->setBuffer();
  9528. mScreenTriangleVB->drawArrays(LLRender::TRIANGLES, 0, 3);
  9529. }
  9530. unbindDeferredShader(*shaderp);
  9531. }
  9532. if (RenderLocalLightCount && mCubeVB.notNull())
  9533. {
  9534. gGL.setSceneBlendType(LLRender::BT_ADD);
  9535. std::list<LLVector4> fullscreen_lights, light_colors;
  9536. LLDrawable::draw_list_t spot_lights, fullscreen_spot_lights;
  9537. if (!gCubeSnapshot)
  9538. {
  9539. for (U32 i = 0; i < 2; ++i)
  9540. {
  9541. mTargetShadowSpotLight[i] = NULL;
  9542. }
  9543. }
  9544. LLVertexBuffer::unbind();
  9545. {
  9546. shaderp = &gDeferredLightProgram;
  9547. bindDeferredShader(*shaderp);
  9548. mCubeVB->setBuffer();
  9549. const LLVector3& cam_origin = gViewerCamera.getOrigin();
  9550. F32 cam_x = cam_origin.mV[0];
  9551. F32 cam_y = cam_origin.mV[1];
  9552. F32 cam_z = cam_origin.mV[2];
  9553. LLGLDepthTest depth(GL_TRUE, GL_FALSE);
  9554. LLVector4a center, sa;
  9555. LLColor3 col;
  9556. U32 rendered_count = 0;
  9557. // mNearbyLights already includes distance calculation and
  9558. // excludes muted avatars. It is calculated from mLights and
  9559. // also provides fade value to gracefully fade-out out of range
  9560. // lights
  9561. for (light_set_t::iterator iter = mNearbyLights.begin(),
  9562. end = mNearbyLights.end();
  9563. iter != end; ++iter)
  9564. {
  9565. if (++rendered_count > RenderLocalLightCount)
  9566. {
  9567. break;
  9568. }
  9569. LLDrawable* drawablep = iter->drawable;
  9570. LLVOVolume* volp = drawablep->getVOVolume();
  9571. if (!volp)
  9572. {
  9573. continue;
  9574. }
  9575. bool is_attachment = volp->isAttachment();
  9576. if (is_attachment && !sRenderAttachedLights)
  9577. {
  9578. continue;
  9579. }
  9580. center.load3(drawablep->getPositionAgent().mV);
  9581. const F32* c = center.getF32ptr();
  9582. F32 s = volp->getLightRadius() * 1.5f;
  9583. if (s <= 0.001f)
  9584. {
  9585. continue;
  9586. }
  9587. // Send light color to shader in linear space
  9588. col = volp->getLightLinearColor() * light_scale;
  9589. if (col.lengthSquared() < 0.001f)
  9590. {
  9591. continue;
  9592. }
  9593. sa.splat(s);
  9594. if (gViewerCamera.AABBInFrustumNoFarClip(center, sa) == 0)
  9595. {
  9596. continue;
  9597. }
  9598. ++sVisibleLightCount;
  9599. if (cam_x > c[0] + s + 0.2f || cam_x < c[0] - s - 0.2f ||
  9600. cam_y > c[1] + s + 0.2f || cam_y < c[1] - s - 0.2f ||
  9601. cam_z > c[2] + s + 0.2f || cam_z < c[2] - s - 0.2f)
  9602. {
  9603. // Draw box if camera is outside box
  9604. if (RenderLocalLightCount)
  9605. {
  9606. if (volp->isLightSpotlight())
  9607. {
  9608. drawablep->getVOVolume()->updateSpotLightPriority();
  9609. spot_lights.emplace_back(drawablep);
  9610. continue;
  9611. }
  9612. LL_FAST_TIMER(FTM_LOCAL_LIGHTS);
  9613. shaderp->uniform3fv(LLShaderMgr::LIGHT_CENTER, 1,
  9614. c);
  9615. shaderp->uniform1f(LLShaderMgr::LIGHT_SIZE, s);
  9616. shaderp->uniform3fv(LLShaderMgr::DIFFUSE_COLOR, 1,
  9617. col.mV);
  9618. shaderp->uniform1f(LLShaderMgr::LIGHT_FALLOFF,
  9619. volp->getLightFalloff(0.5f));
  9620. gGL.syncMatrices();
  9621. mCubeVB->drawRange(LLRender::TRIANGLE_FAN, 0, 7, 8,
  9622. get_box_fan_indices(&gViewerCamera,
  9623. center));
  9624. }
  9625. }
  9626. else
  9627. {
  9628. if (volp->isLightSpotlight())
  9629. {
  9630. drawablep->getVOVolume()->updateSpotLightPriority();
  9631. fullscreen_spot_lights.emplace_back(drawablep);
  9632. continue;
  9633. }
  9634. gGLModelView.affineTransform(center, center);
  9635. LLVector4 tc(center.getF32ptr());
  9636. tc.mV[VW] = s;
  9637. fullscreen_lights.emplace_back(tc);
  9638. light_colors.emplace_back(col.mV[0], col.mV[1],
  9639. col.mV[2],
  9640. volp->getLightFalloff(0.5f));
  9641. }
  9642. }
  9643. // When editting appearance, we need to add the corresponding
  9644. // light at the camera position.
  9645. static LLCachedControl<bool> custlight(gSavedSettings,
  9646. "AvatarCustomizeLighting");
  9647. if (custlight && isAgentAvatarValid() &&
  9648. gAgentAvatarp->mSpecialRenderMode == 3)
  9649. {
  9650. fullscreen_lights.emplace_back(0.f, 0.f, 0.f, 15.f);
  9651. light_colors.emplace_back(1.f, 1.f, 1.f, 0.f);
  9652. }
  9653. unbindDeferredShader(*shaderp);
  9654. }
  9655. if (!spot_lights.empty())
  9656. {
  9657. LL_FAST_TIMER(FTM_PROJECTORS);
  9658. LLGLDepthTest depth(GL_TRUE, GL_FALSE);
  9659. shaderp = &gDeferredSpotLightProgram;
  9660. bindDeferredShader(*shaderp);
  9661. mCubeVB->setBuffer();
  9662. shaderp->enableTexture(LLShaderMgr::DEFERRED_PROJECTION);
  9663. for (LLDrawable::draw_list_t::iterator
  9664. iter = spot_lights.begin(), end = spot_lights.end();
  9665. iter != end; ++iter)
  9666. {
  9667. LLDrawable* drawablep = *iter;
  9668. LLVOVolume* volp = drawablep->getVOVolume();
  9669. LLVector4a center;
  9670. center.load3(drawablep->getPositionAgent().mV);
  9671. const F32* c = center.getF32ptr();
  9672. F32 s = volp->getLightRadius() * 1.5f;
  9673. ++sVisibleLightCount;
  9674. setupSpotLight(*shaderp, drawablep);
  9675. LLColor3 col = volp->getLightLinearColor() * light_scale;
  9676. shaderp->uniform3fv(LLShaderMgr::LIGHT_CENTER, 1, c);
  9677. shaderp->uniform1f(LLShaderMgr::LIGHT_SIZE, s);
  9678. shaderp->uniform3fv(LLShaderMgr::DIFFUSE_COLOR, 1, col.mV);
  9679. shaderp->uniform1f(LLShaderMgr::LIGHT_FALLOFF,
  9680. volp->getLightFalloff(0.5f));
  9681. gGL.syncMatrices();
  9682. mCubeVB->drawRange(LLRender::TRIANGLE_FAN, 0, 7, 8,
  9683. get_box_fan_indices(&gViewerCamera,
  9684. center));
  9685. }
  9686. shaderp->disableTexture(LLShaderMgr::DEFERRED_PROJECTION);
  9687. unbindDeferredShader(*shaderp);
  9688. }
  9689. {
  9690. LLGLDepthTest depth(GL_FALSE);
  9691. constexpr U32 max_count = LL_DEFERRED_MULTI_LIGHT_COUNT;
  9692. LLVector4 light[max_count];
  9693. LLVector4 col[max_count];
  9694. U32 count = 0;
  9695. F32 far_z = 0.f;
  9696. {
  9697. LL_FAST_TIMER(FTM_FULLSCREEN_LIGHTS);
  9698. while (count < max_count && !fullscreen_lights.empty())
  9699. {
  9700. light[count] = fullscreen_lights.front();
  9701. fullscreen_lights.pop_front();
  9702. col[count] = light_colors.front();
  9703. light_colors.pop_front();
  9704. far_z = llmin(light[count].mV[2] - light[count].mV[3],
  9705. far_z);
  9706. if (++count == max_count || fullscreen_lights.empty())
  9707. {
  9708. U32 idx = count - 1;
  9709. shaderp = &gDeferredMultiLightProgram[idx];
  9710. bindDeferredShader(*shaderp);
  9711. shaderp->uniform1i(LLShaderMgr::MULTI_LIGHT_COUNT,
  9712. count);
  9713. shaderp->uniform4fv(LLShaderMgr::MULTI_LIGHT,
  9714. count, (F32*)light);
  9715. shaderp->uniform4fv(LLShaderMgr::MULTI_LIGHT_COL,
  9716. count, (F32*)col);
  9717. shaderp->uniform1f(LLShaderMgr::MULTI_LIGHT_FAR_Z,
  9718. far_z);
  9719. mScreenTriangleVB->setBuffer();
  9720. mScreenTriangleVB->drawArrays(LLRender::TRIANGLES,
  9721. 0, 3);
  9722. unbindDeferredShader(*shaderp);
  9723. far_z = 0.f;
  9724. count = 0;
  9725. }
  9726. }
  9727. }
  9728. shaderp = &gDeferredMultiSpotLightProgram;
  9729. bindDeferredShader(*shaderp);
  9730. shaderp->enableTexture(LLShaderMgr::DEFERRED_PROJECTION);
  9731. mScreenTriangleVB->setBuffer();
  9732. {
  9733. LL_FAST_TIMER(FTM_PROJECTORS);
  9734. for (LLDrawable::draw_list_t::iterator
  9735. iter = fullscreen_spot_lights.begin(),
  9736. end = fullscreen_spot_lights.end();
  9737. iter != end; ++iter)
  9738. {
  9739. LLDrawable* drawablep = *iter;
  9740. LLVOVolume* volp = drawablep->getVOVolume();
  9741. LLVector4a center;
  9742. center.load3(drawablep->getPositionAgent().mV);
  9743. F32 s = volp->getLightRadius() * 1.5f;
  9744. ++sVisibleLightCount;
  9745. gGLModelView.affineTransform(center, center);
  9746. setupSpotLight(*shaderp, drawablep);
  9747. LLColor3 col = volp->getLightLinearColor() *
  9748. light_scale;
  9749. shaderp->uniform3fv(LLShaderMgr::LIGHT_CENTER, 1,
  9750. center.getF32ptr());
  9751. shaderp->uniform1f(LLShaderMgr::LIGHT_SIZE, s);
  9752. shaderp->uniform3fv(LLShaderMgr::DIFFUSE_COLOR, 1,
  9753. col.mV);
  9754. shaderp->uniform1f(LLShaderMgr::LIGHT_FALLOFF,
  9755. volp->getLightFalloff(0.5f));
  9756. mScreenTriangleVB->drawArrays(LLRender::TRIANGLES,
  9757. 0, 3);
  9758. }
  9759. }
  9760. shaderp->disableTexture(LLShaderMgr::DEFERRED_PROJECTION);
  9761. unbindDeferredShader(*shaderp);
  9762. }
  9763. }
  9764. gGL.setColorMask(true, true);
  9765. }
  9766. {
  9767. // Render non-deferred geometry (alpha, fullbright, glow)
  9768. LLGLDisable blend(GL_BLEND);
  9769. pushRenderTypeMask();
  9770. andRenderTypeMask(RENDER_TYPE_ALPHA,
  9771. RENDER_TYPE_ALPHA_PRE_WATER,
  9772. RENDER_TYPE_ALPHA_POST_WATER,
  9773. RENDER_TYPE_FULLBRIGHT,
  9774. RENDER_TYPE_VOLUME,
  9775. RENDER_TYPE_GLOW,
  9776. RENDER_TYPE_BUMP,
  9777. RENDER_TYPE_MAT_PBR,
  9778. RENDER_TYPE_PASS_SIMPLE,
  9779. RENDER_TYPE_PASS_ALPHA,
  9780. RENDER_TYPE_PASS_ALPHA_MASK,
  9781. RENDER_TYPE_PASS_BUMP,
  9782. RENDER_TYPE_PASS_POST_BUMP,
  9783. RENDER_TYPE_PASS_FULLBRIGHT,
  9784. RENDER_TYPE_PASS_FULLBRIGHT_ALPHA_MASK,
  9785. RENDER_TYPE_PASS_FULLBRIGHT_SHINY,
  9786. RENDER_TYPE_PASS_GLOW,
  9787. RENDER_TYPE_PASS_PBR_GLOW,
  9788. RENDER_TYPE_PASS_GRASS,
  9789. RENDER_TYPE_PASS_SHINY,
  9790. RENDER_TYPE_PASS_INVISIBLE,
  9791. RENDER_TYPE_PASS_INVISI_SHINY,
  9792. RENDER_TYPE_AVATAR,
  9793. RENDER_TYPE_PUPPET,
  9794. RENDER_TYPE_ALPHA_MASK,
  9795. RENDER_TYPE_FULLBRIGHT_ALPHA_MASK,
  9796. RENDER_TYPE_TERRAIN,
  9797. RENDER_TYPE_WATER,
  9798. END_RENDER_TYPES);
  9799. renderGeomPostDeferred(gViewerCamera);
  9800. popRenderTypeMask();
  9801. }
  9802. screen_targetp->flush();
  9803. if (!gCubeSnapshot)
  9804. {
  9805. // This is the end of the 3D scene render, grab a copy of the model
  9806. // view and projection matrix for use in off-by-one-frame effects in
  9807. // the next frame.
  9808. gGLLastModelView = gGLModelView;
  9809. gGLLastProjection = gGLProjection;
  9810. }
  9811. gGL.setColorMask(true, true);
  9812. }
  9813. void LLPipeline::renderDeferredLighting()
  9814. {
  9815. if (!sCull)
  9816. {
  9817. return;
  9818. }
  9819. if (gUsePBRShaders)
  9820. {
  9821. renderDeferredLightingPBR();
  9822. return;
  9823. }
  9824. LLGLSLShader* shaderp;
  9825. {
  9826. LL_FAST_TIMER(FTM_RENDER_DEFERRED);
  9827. {
  9828. LLGLDepthTest depth(GL_TRUE);
  9829. mDeferredDepth.copyContents(mRT->mDeferredScreen, 0, 0,
  9830. mRT->mDeferredScreen.getWidth(),
  9831. mRT->mDeferredScreen.getHeight(),
  9832. 0, 0,
  9833. mDeferredDepth.getWidth(),
  9834. mDeferredDepth.getHeight(),
  9835. GL_DEPTH_BUFFER_BIT, GL_NEAREST);
  9836. }
  9837. LLGLEnable multisample(RenderFSAASamples > 0 ? GL_MULTISAMPLE : 0);
  9838. if (hasRenderType(RENDER_TYPE_HUD))
  9839. {
  9840. toggleRenderType(RENDER_TYPE_HUD);
  9841. }
  9842. // ATI does not seem to love actually using the stencil buffer on FBOs
  9843. LLGLDisable stencil(GL_STENCIL_TEST);
  9844. #if 0
  9845. glStencilFunc(GL_EQUAL, 1, 0xFFFFFFFF);
  9846. glStencilOp(GL_KEEP, GL_KEEP, GL_KEEP);
  9847. #endif
  9848. gGL.setColorMask(true, true);
  9849. // Draw a cube around every light
  9850. LLVertexBuffer::unbind();
  9851. LLGLEnable cull(GL_CULL_FACE);
  9852. LLGLEnable blend(GL_BLEND);
  9853. LLStrider<LLVector3> vert;
  9854. if (mDeferredVB.isNull() || !mDeferredVB->getVertexStrider(vert))
  9855. {
  9856. return;
  9857. }
  9858. vert[0].set(-1.f, 1.f, 0.f);
  9859. vert[1].set(-1.f, -3.f, 0.f);
  9860. vert[2].set(3.f, 1.f, 0.f);
  9861. setupHWLights(); // To set mSunDir/mMoonDir;
  9862. mTransformedSunDir.loadua(mSunDir.mV);
  9863. gGLModelView.rotate(mTransformedSunDir, mTransformedSunDir);
  9864. mTransformedMoonDir.loadua(mMoonDir.mV);
  9865. gGLModelView.rotate(mTransformedMoonDir, mTransformedMoonDir);
  9866. gGL.pushMatrix();
  9867. gGL.loadIdentity();
  9868. gGL.matrixMode(LLRender::MM_PROJECTION);
  9869. gGL.pushMatrix();
  9870. gGL.loadIdentity();
  9871. if (RenderDeferredSSAO || RenderShadowDetail > 0)
  9872. {
  9873. mRT->mDeferredLight.bindTarget();
  9874. {
  9875. // Paint shadow/SSAO light map (direct lighting lightmap)
  9876. LL_FAST_TIMER(FTM_SUN_SHADOW);
  9877. shaderp = &gDeferredSunProgram;
  9878. bindDeferredShader(*shaderp, &mRT->mDeferredLight);
  9879. glClearColor(1.f, 1.f, 1.f, 1.f);
  9880. mRT->mDeferredLight.clear(GL_COLOR_BUFFER_BIT);
  9881. glClearColor(0.f, 0.f, 0.f, 0.f);
  9882. shaderp->uniform2f(LLShaderMgr::DEFERRED_SCREEN_RES,
  9883. mRT->mDeferredLight.getWidth(),
  9884. mRT->mDeferredLight.getHeight());
  9885. {
  9886. LLGLDisable blend(GL_BLEND);
  9887. LLGLDepthTest depth(GL_TRUE, GL_FALSE, GL_ALWAYS);
  9888. mDeferredVB->setBuffer(LLVertexBuffer::MAP_VERTEX);
  9889. mDeferredVB->drawArrays(LLRender::TRIANGLES, 0, 3);
  9890. }
  9891. unbindDeferredShader(*shaderp);
  9892. }
  9893. mRT->mDeferredLight.flush();
  9894. }
  9895. if (RenderShadowSoften)
  9896. {
  9897. // Soften direct lighting lightmap
  9898. LL_FAST_TIMER(FTM_SOFTEN_SHADOW);
  9899. // Blur lightmap
  9900. mRT->mScreen.bindTarget();
  9901. glClearColor(1.f, 1.f, 1.f, 1.f);
  9902. mRT->mScreen.clear(GL_COLOR_BUFFER_BIT);
  9903. glClearColor(0.f, 0.f, 0.f, 0.f);
  9904. shaderp = &gDeferredBlurLightProgram;
  9905. bindDeferredShader(*shaderp);
  9906. mDeferredVB->setBuffer(LLVertexBuffer::MAP_VERTEX);
  9907. // Sample symmetrically with the middle sample falling exactly on
  9908. // 0.0
  9909. F32 x = 0.f;
  9910. F32 shadow_gaussian_x = RenderShadowGaussian.mV[0];
  9911. F32 shadow_gaussian_y = RenderShadowGaussian.mV[1];
  9912. constexpr U32 kern_length = 4;
  9913. LLVector3 gauss[kern_length]; // xweight, yweight, offset
  9914. for (U32 i = 0; i < kern_length; ++i)
  9915. {
  9916. gauss[i].mV[0] = llgaussian(x, shadow_gaussian_x);
  9917. gauss[i].mV[1] = llgaussian(x, shadow_gaussian_y);
  9918. gauss[i].mV[2] = x;
  9919. x += 1.f;
  9920. }
  9921. shaderp->uniform2f(sDelta, 1.f, 0.f);
  9922. shaderp->uniform1f(sDistFactor, RenderShadowBlurDistFactor);
  9923. shaderp->uniform3fv(sKern, kern_length, gauss[0].mV);
  9924. shaderp->uniform1f(sKernScale,
  9925. RenderShadowBlurSize *
  9926. (kern_length * 0.5f - 0.5f));
  9927. {
  9928. LLGLDisable blend(GL_BLEND);
  9929. LLGLDepthTest depth(GL_TRUE, GL_FALSE, GL_ALWAYS);
  9930. mDeferredVB->drawArrays(LLRender::TRIANGLES, 0, 3);
  9931. }
  9932. mRT->mScreen.flush();
  9933. unbindDeferredShader(*shaderp);
  9934. bindDeferredShader(*shaderp, &mRT->mScreen);
  9935. mDeferredVB->setBuffer(LLVertexBuffer::MAP_VERTEX);
  9936. mRT->mDeferredLight.bindTarget();
  9937. shaderp->uniform2f(sDelta, 0.f, 1.f);
  9938. {
  9939. LLGLDisable blend(GL_BLEND);
  9940. LLGLDepthTest depth(GL_TRUE, GL_FALSE, GL_ALWAYS);
  9941. mDeferredVB->drawArrays(LLRender::TRIANGLES, 0, 3);
  9942. }
  9943. mRT->mDeferredLight.flush();
  9944. unbindDeferredShader(*shaderp);
  9945. }
  9946. gGL.popMatrix();
  9947. gGL.matrixMode(LLRender::MM_MODELVIEW);
  9948. gGL.popMatrix();
  9949. mRT->mScreen.bindTarget();
  9950. // Clear color buffer here; zeroing alpha (glow) is important or it
  9951. // will accumulate against sky
  9952. glClearColor(0.f, 0.f, 0.f, 0.f);
  9953. mRT->mScreen.clear(GL_COLOR_BUFFER_BIT);
  9954. if (RenderDeferredAtmospheric)
  9955. {
  9956. // Apply sunlight contribution
  9957. LL_FAST_TIMER(FTM_ATMOSPHERICS);
  9958. shaderp = sUnderWaterRender ? &gDeferredSoftenWaterProgram
  9959. : &gDeferredSoftenProgram;
  9960. bindDeferredShader(*shaderp);
  9961. shaderp->uniform1i(LLShaderMgr::SUN_UP_FACTOR, mIsSunUp ? 1 : 0);
  9962. shaderp->uniform4fv(LLShaderMgr::LIGHTNORM, 1,
  9963. gEnvironment.getClampedLightNorm().mV);
  9964. {
  9965. LLGLDepthTest depth(GL_FALSE);
  9966. LLGLDisable blend(GL_BLEND);
  9967. // Full screen blit
  9968. gGL.pushMatrix();
  9969. gGL.loadIdentity();
  9970. gGL.matrixMode(LLRender::MM_PROJECTION);
  9971. gGL.pushMatrix();
  9972. gGL.loadIdentity();
  9973. mDeferredVB->setBuffer(LLVertexBuffer::MAP_VERTEX);
  9974. mDeferredVB->drawArrays(LLRender::TRIANGLES, 0, 3);
  9975. gGL.popMatrix();
  9976. gGL.matrixMode(LLRender::MM_MODELVIEW);
  9977. gGL.popMatrix();
  9978. }
  9979. unbindDeferredShader(*shaderp);
  9980. }
  9981. {
  9982. // Render non-deferred geometry (fullbright, alpha, etc)
  9983. LLGLDisable blend(GL_BLEND);
  9984. LLGLDisable stencil(GL_STENCIL_TEST);
  9985. gGL.setSceneBlendType(LLRender::BT_ALPHA);
  9986. pushRenderTypeMask();
  9987. andRenderTypeMask(RENDER_TYPE_SKY,
  9988. RENDER_TYPE_CLOUDS,
  9989. RENDER_TYPE_WL_SKY,
  9990. END_RENDER_TYPES);
  9991. renderGeomPostDeferred(gViewerCamera, false);
  9992. popRenderTypeMask();
  9993. }
  9994. if (RenderLocalLightCount)
  9995. {
  9996. gGL.setSceneBlendType(LLRender::BT_ADD);
  9997. std::list<LLVector4> fullscreen_lights, light_colors;
  9998. LLDrawable::draw_list_t spot_lights, fullscreen_spot_lights;
  9999. for (U32 i = 0; i < 2; ++i)
  10000. {
  10001. mTargetShadowSpotLight[i] = NULL;
  10002. }
  10003. LLVertexBuffer::unbind();
  10004. {
  10005. shaderp = &gDeferredLightProgram;
  10006. bindDeferredShader(*shaderp);
  10007. if (mCubeVB.notNull())
  10008. {
  10009. mCubeVB->setBuffer(LLVertexBuffer::MAP_VERTEX);
  10010. }
  10011. const LLVector3& cam_origin = gViewerCamera.getOrigin();
  10012. F32 cam_x = cam_origin.mV[0];
  10013. F32 cam_y = cam_origin.mV[1];
  10014. F32 cam_z = cam_origin.mV[2];
  10015. LLGLDepthTest depth(GL_TRUE, GL_FALSE);
  10016. LLVector4a center, sa;
  10017. LLColor3 col;
  10018. U32 rendered_count = 0;
  10019. // mNearbyLights already includes distance calculation and
  10020. // excludes muted avatars. It is calculated from mLights and
  10021. // also provides fade value to gracefully fade-out out of range
  10022. // lights
  10023. for (light_set_t::iterator iter = mNearbyLights.begin(),
  10024. end = mNearbyLights.end();
  10025. iter != end; ++iter)
  10026. {
  10027. if (++rendered_count > RenderLocalLightCount)
  10028. {
  10029. break;
  10030. }
  10031. LLDrawable* drawablep = iter->drawable;
  10032. LLVOVolume* volp = drawablep->getVOVolume();
  10033. if (!volp)
  10034. {
  10035. continue;
  10036. }
  10037. bool is_attachment = volp->isAttachment();
  10038. if (is_attachment && !sRenderAttachedLights)
  10039. {
  10040. continue;
  10041. }
  10042. center.load3(drawablep->getPositionAgent().mV);
  10043. const F32* c = center.getF32ptr();
  10044. F32 s = volp->getLightRadius() * 1.5f;
  10045. if (s <= 0.001f)
  10046. {
  10047. continue;
  10048. }
  10049. // Send light color to shader in linear space
  10050. col = volp->getLightLinearColor();
  10051. if (col.lengthSquared() < 0.001f)
  10052. {
  10053. continue;
  10054. }
  10055. sa.splat(s);
  10056. if (gViewerCamera.AABBInFrustumNoFarClip(center, sa) == 0)
  10057. {
  10058. continue;
  10059. }
  10060. ++sVisibleLightCount;
  10061. if (cam_x > c[0] + s + 0.2f || cam_x < c[0] - s - 0.2f ||
  10062. cam_y > c[1] + s + 0.2f || cam_y < c[1] - s - 0.2f ||
  10063. cam_z > c[2] + s + 0.2f || cam_z < c[2] - s - 0.2f)
  10064. {
  10065. // Draw box if camera is outside box
  10066. if (RenderLocalLightCount && mCubeVB.notNull())
  10067. {
  10068. if (volp->isLightSpotlight())
  10069. {
  10070. drawablep->getVOVolume()->updateSpotLightPriority();
  10071. spot_lights.emplace_back(drawablep);
  10072. continue;
  10073. }
  10074. LL_FAST_TIMER(FTM_LOCAL_LIGHTS);
  10075. shaderp->uniform3fv(LLShaderMgr::LIGHT_CENTER, 1,
  10076. c);
  10077. shaderp->uniform1f(LLShaderMgr::LIGHT_SIZE, s);
  10078. shaderp->uniform3fv(LLShaderMgr::DIFFUSE_COLOR, 1,
  10079. col.mV);
  10080. shaderp->uniform1f(LLShaderMgr::LIGHT_FALLOFF,
  10081. volp->getLightFalloff(0.5f));
  10082. gGL.syncMatrices();
  10083. mCubeVB->drawRange(LLRender::TRIANGLE_FAN, 0, 7, 8,
  10084. get_box_fan_indices(&gViewerCamera,
  10085. center));
  10086. }
  10087. }
  10088. else
  10089. {
  10090. if (volp->isLightSpotlight())
  10091. {
  10092. drawablep->getVOVolume()->updateSpotLightPriority();
  10093. fullscreen_spot_lights.emplace_back(drawablep);
  10094. continue;
  10095. }
  10096. gGLModelView.affineTransform(center, center);
  10097. LLVector4 tc(center.getF32ptr());
  10098. tc.mV[VW] = s;
  10099. fullscreen_lights.emplace_back(tc);
  10100. light_colors.emplace_back(col.mV[0], col.mV[1],
  10101. col.mV[2],
  10102. volp->getLightFalloff(0.5f));
  10103. }
  10104. }
  10105. stop_glerror();
  10106. // When editting appearance, we need to add the corresponding
  10107. // light at the camera position.
  10108. static LLCachedControl<bool> custlight(gSavedSettings,
  10109. "AvatarCustomizeLighting");
  10110. if (custlight && isAgentAvatarValid() &&
  10111. gAgentAvatarp->mSpecialRenderMode == 3)
  10112. {
  10113. fullscreen_lights.emplace_back(0.f, 0.f, 0.f, 15.f);
  10114. light_colors.emplace_back(1.f, 1.f, 1.f, 0.f);
  10115. }
  10116. unbindDeferredShader(*shaderp);
  10117. }
  10118. if (!spot_lights.empty() && mCubeVB.notNull())
  10119. {
  10120. LL_FAST_TIMER(FTM_PROJECTORS);
  10121. LLGLDepthTest depth(GL_TRUE, GL_FALSE);
  10122. shaderp = &gDeferredSpotLightProgram;
  10123. bindDeferredShader(*shaderp);
  10124. mCubeVB->setBuffer(LLVertexBuffer::MAP_VERTEX);
  10125. shaderp->enableTexture(LLShaderMgr::DEFERRED_PROJECTION);
  10126. for (LLDrawable::draw_list_t::iterator
  10127. iter = spot_lights.begin(), end = spot_lights.end();
  10128. iter != end; ++iter)
  10129. {
  10130. LLDrawable* drawablep = *iter;
  10131. LLVOVolume* volp = drawablep->getVOVolume();
  10132. LLVector4a center;
  10133. center.load3(drawablep->getPositionAgent().mV);
  10134. const F32* c = center.getF32ptr();
  10135. F32 s = volp->getLightRadius() * 1.5f;
  10136. ++sVisibleLightCount;
  10137. setupSpotLight(*shaderp, drawablep);
  10138. LLColor3 col = volp->getLightLinearColor();
  10139. shaderp->uniform3fv(LLShaderMgr::LIGHT_CENTER, 1, c);
  10140. shaderp->uniform1f(LLShaderMgr::LIGHT_SIZE, s);
  10141. shaderp->uniform3fv(LLShaderMgr::DIFFUSE_COLOR, 1, col.mV);
  10142. shaderp->uniform1f(LLShaderMgr::LIGHT_FALLOFF,
  10143. volp->getLightFalloff(0.5f));
  10144. gGL.syncMatrices();
  10145. mCubeVB->drawRange(LLRender::TRIANGLE_FAN, 0, 7, 8,
  10146. get_box_fan_indices(&gViewerCamera,
  10147. center));
  10148. }
  10149. shaderp->disableTexture(LLShaderMgr::DEFERRED_PROJECTION);
  10150. unbindDeferredShader(*shaderp);
  10151. }
  10152. // Reset mDeferredVB to fullscreen triangle
  10153. if (!mDeferredVB->getVertexStrider(vert))
  10154. {
  10155. return;
  10156. }
  10157. vert[0].set(-1.f, 1.f, 0.f);
  10158. vert[1].set(-1.f, -3.f, 0.f);
  10159. vert[2].set(3.f, 1.f, 0.f);
  10160. {
  10161. LLGLDepthTest depth(GL_FALSE);
  10162. // Full screen blit
  10163. gGL.pushMatrix();
  10164. gGL.loadIdentity();
  10165. gGL.matrixMode(LLRender::MM_PROJECTION);
  10166. gGL.pushMatrix();
  10167. gGL.loadIdentity();
  10168. constexpr U32 max_count = LL_DEFERRED_MULTI_LIGHT_COUNT;
  10169. LLVector4 light[max_count];
  10170. LLVector4 col[max_count];
  10171. U32 count = 0;
  10172. F32 far_z = 0.f;
  10173. {
  10174. LL_FAST_TIMER(FTM_FULLSCREEN_LIGHTS);
  10175. while (count < max_count && !fullscreen_lights.empty())
  10176. {
  10177. light[count] = fullscreen_lights.front();
  10178. fullscreen_lights.pop_front();
  10179. col[count] = light_colors.front();
  10180. light_colors.pop_front();
  10181. far_z = llmin(light[count].mV[2] - light[count].mV[3],
  10182. far_z);
  10183. if (++count == max_count || fullscreen_lights.empty())
  10184. {
  10185. U32 idx = count - 1;
  10186. shaderp = &gDeferredMultiLightProgram[idx];
  10187. bindDeferredShader(*shaderp);
  10188. shaderp->uniform1i(LLShaderMgr::MULTI_LIGHT_COUNT,
  10189. count);
  10190. shaderp->uniform4fv(LLShaderMgr::MULTI_LIGHT,
  10191. count, (F32*)light);
  10192. shaderp->uniform4fv(LLShaderMgr::MULTI_LIGHT_COL,
  10193. count, (F32*)col);
  10194. shaderp->uniform1f(LLShaderMgr::MULTI_LIGHT_FAR_Z,
  10195. far_z);
  10196. mDeferredVB->setBuffer(LLVertexBuffer::MAP_VERTEX);
  10197. mDeferredVB->drawArrays(LLRender::TRIANGLES, 0, 3);
  10198. unbindDeferredShader(*shaderp);
  10199. far_z = 0.f;
  10200. count = 0;
  10201. }
  10202. }
  10203. }
  10204. shaderp = &gDeferredMultiSpotLightProgram;
  10205. bindDeferredShader(*shaderp);
  10206. shaderp->enableTexture(LLShaderMgr::DEFERRED_PROJECTION);
  10207. mDeferredVB->setBuffer(LLVertexBuffer::MAP_VERTEX);
  10208. {
  10209. LL_FAST_TIMER(FTM_PROJECTORS);
  10210. for (LLDrawable::draw_list_t::iterator
  10211. iter = fullscreen_spot_lights.begin(),
  10212. end = fullscreen_spot_lights.end();
  10213. iter != end; ++iter)
  10214. {
  10215. LLDrawable* drawablep = *iter;
  10216. LLVOVolume* volp = drawablep->getVOVolume();
  10217. LLVector4a center;
  10218. center.load3(drawablep->getPositionAgent().mV);
  10219. F32 s = volp->getLightRadius() * 1.5f;
  10220. ++sVisibleLightCount;
  10221. gGLModelView.affineTransform(center, center);
  10222. setupSpotLight(*shaderp, drawablep);
  10223. LLColor3 col = volp->getLightLinearColor();
  10224. shaderp->uniform3fv(LLShaderMgr::LIGHT_CENTER, 1,
  10225. center.getF32ptr());
  10226. shaderp->uniform1f(LLShaderMgr::LIGHT_SIZE, s);
  10227. shaderp->uniform3fv(LLShaderMgr::DIFFUSE_COLOR, 1,
  10228. col.mV);
  10229. shaderp->uniform1f(LLShaderMgr::LIGHT_FALLOFF,
  10230. volp->getLightFalloff(0.5f));
  10231. mDeferredVB->drawArrays(LLRender::TRIANGLES, 0, 3);
  10232. }
  10233. }
  10234. shaderp->disableTexture(LLShaderMgr::DEFERRED_PROJECTION);
  10235. unbindDeferredShader(*shaderp);
  10236. gGL.popMatrix();
  10237. gGL.matrixMode(LLRender::MM_MODELVIEW);
  10238. gGL.popMatrix();
  10239. }
  10240. gGL.setSceneBlendType(LLRender::BT_ALPHA);
  10241. }
  10242. gGL.setColorMask(true, true);
  10243. }
  10244. mRT->mScreen.flush();
  10245. // Gamma-correct lighting
  10246. gGL.matrixMode(LLRender::MM_PROJECTION);
  10247. gGL.pushMatrix();
  10248. gGL.loadIdentity();
  10249. gGL.matrixMode(LLRender::MM_MODELVIEW);
  10250. gGL.pushMatrix();
  10251. gGL.loadIdentity();
  10252. {
  10253. LLGLDepthTest depth(GL_FALSE, GL_FALSE);
  10254. LLVector2 tc1;
  10255. LLVector2 tc2((F32)(mRT->mScreen.getWidth() * 2),
  10256. (F32)(mRT->mScreen.getHeight() * 2));
  10257. mRT->mScreen.bindTarget();
  10258. // Apply gamma correction to the frame here.
  10259. shaderp = &gDeferredPostGammaCorrectProgram;
  10260. shaderp->bind();
  10261. S32 channel = shaderp->enableTexture(LLShaderMgr::DEFERRED_DIFFUSE,
  10262. mRT->mScreen.getUsage());
  10263. if (channel > -1)
  10264. {
  10265. mRT->mScreen.bindTexture(0, channel, LLTexUnit::TFO_POINT);
  10266. }
  10267. shaderp->uniform2f(LLShaderMgr::DEFERRED_SCREEN_RES,
  10268. mRT->mScreen.getWidth(), mRT->mScreen.getHeight());
  10269. shaderp->uniform1f(LLShaderMgr::DISPLAY_GAMMA,
  10270. 1.f / RenderDeferredDisplayGamma);
  10271. gGL.begin(LLRender::TRIANGLE_STRIP);
  10272. gGL.texCoord2f(tc1.mV[0], tc1.mV[1]);
  10273. gGL.vertex2f(-1.f, -1.f);
  10274. gGL.texCoord2f(tc1.mV[0], tc2.mV[1]);
  10275. gGL.vertex2f(-1.f, 3.f);
  10276. gGL.texCoord2f(tc2.mV[0], tc1.mV[1]);
  10277. gGL.vertex2f(3.f, -1.f);
  10278. gGL.end();
  10279. if (channel > -1)
  10280. {
  10281. gGL.getTexUnit(channel)->unbind(mRT->mScreen.getUsage());
  10282. }
  10283. shaderp->unbind();
  10284. mRT->mScreen.flush();
  10285. stop_glerror();
  10286. }
  10287. gGL.matrixMode(LLRender::MM_PROJECTION);
  10288. gGL.popMatrix();
  10289. gGL.matrixMode(LLRender::MM_MODELVIEW);
  10290. gGL.popMatrix();
  10291. mRT->mScreen.bindTarget();
  10292. {
  10293. // Render non-deferred geometry (alpha, fullbright, glow)
  10294. LLGLDisable blend(GL_BLEND);
  10295. LLGLDisable stencil(GL_STENCIL_TEST);
  10296. pushRenderTypeMask();
  10297. andRenderTypeMask(RENDER_TYPE_ALPHA,
  10298. RENDER_TYPE_FULLBRIGHT,
  10299. RENDER_TYPE_VOLUME,
  10300. RENDER_TYPE_GLOW,
  10301. RENDER_TYPE_BUMP,
  10302. RENDER_TYPE_PASS_SIMPLE,
  10303. RENDER_TYPE_PASS_ALPHA,
  10304. RENDER_TYPE_PASS_ALPHA_MASK,
  10305. RENDER_TYPE_PASS_BUMP,
  10306. RENDER_TYPE_PASS_POST_BUMP,
  10307. RENDER_TYPE_PASS_FULLBRIGHT,
  10308. RENDER_TYPE_PASS_FULLBRIGHT_ALPHA_MASK,
  10309. RENDER_TYPE_PASS_FULLBRIGHT_SHINY,
  10310. RENDER_TYPE_PASS_GLOW,
  10311. RENDER_TYPE_PASS_GRASS,
  10312. RENDER_TYPE_PASS_SHINY,
  10313. RENDER_TYPE_PASS_INVISIBLE,
  10314. RENDER_TYPE_PASS_INVISI_SHINY,
  10315. RENDER_TYPE_AVATAR,
  10316. RENDER_TYPE_PUPPET,
  10317. RENDER_TYPE_ALPHA_MASK,
  10318. RENDER_TYPE_FULLBRIGHT_ALPHA_MASK,
  10319. END_RENDER_TYPES);
  10320. renderGeomPostDeferred(gViewerCamera);
  10321. popRenderTypeMask();
  10322. }
  10323. // Render highlights, etc.
  10324. renderHighlights();
  10325. mHighlightFaces.clear();
  10326. renderDebug();
  10327. LLVertexBuffer::unbind();
  10328. mRT->mScreen.flush();
  10329. }
  10330. void LLPipeline::setupSpotLight(LLGLSLShader& shader, LLDrawable* drawablep)
  10331. {
  10332. // Construct frustum
  10333. LLVOVolume* volp = drawablep->getVOVolume();
  10334. LLVector3 params = volp->getSpotLightParams();
  10335. F32 fov = params.mV[0];
  10336. F32 focus = params.mV[1];
  10337. LLVector3 pos = drawablep->getPositionAgent();
  10338. LLQuaternion quat = volp->getRenderRotation();
  10339. LLVector3 scale = volp->getScale();
  10340. // Get near clip plane
  10341. LLVector3 at_axis(0.f, 0.f, -scale.mV[2] * 0.5f);
  10342. at_axis *= quat;
  10343. LLVector3 np = pos + at_axis;
  10344. at_axis.normalize();
  10345. // Get origin that has given fov for plane np, at_axis, and given scale
  10346. F32 dist = scale.mV[1] * 0.5f / tanf(fov * 0.5f);
  10347. LLVector3 origin = np - at_axis * dist;
  10348. // Matrix from volume space to agent space
  10349. LLMatrix4 light_mat4(quat, LLVector4(origin,1.f));
  10350. LLMatrix4a light_mat;
  10351. light_mat.loadu(light_mat4.getF32ptr());
  10352. LLMatrix4a light_to_screen;
  10353. light_to_screen.setMul(gGLModelView, light_mat);
  10354. LLMatrix4a screen_to_light = light_to_screen;
  10355. screen_to_light.invert();
  10356. F32 s = volp->getLightRadius() * 1.5f;
  10357. F32 near_clip = dist;
  10358. F32 width = scale.mV[VX];
  10359. F32 height = scale.mV[VY];
  10360. F32 far_clip = s + dist - scale.mV[VZ];
  10361. F32 fovy = fov * RAD_TO_DEG;
  10362. F32 aspect = width / height;
  10363. LLVector4a p1(0.f, 0.f, -(near_clip + 0.01f));
  10364. LLVector4a p2(0.f, 0.f, -(near_clip + 1.f));
  10365. LLVector4a screen_origin;
  10366. screen_origin.clear();
  10367. light_to_screen.affineTransform(p1, p1);
  10368. light_to_screen.affineTransform(p2, p2);
  10369. light_to_screen.affineTransform(screen_origin, screen_origin);
  10370. LLVector4a n;
  10371. n.setSub(p2, p1);
  10372. n.normalize3fast();
  10373. F32 proj_range = far_clip - near_clip;
  10374. LLMatrix4a light_proj = gl_perspective(fovy, aspect, near_clip, far_clip);
  10375. light_proj.setMul(TRANS_MAT, light_proj);
  10376. screen_to_light.setMul(light_proj, screen_to_light);
  10377. shader.uniformMatrix4fv(LLShaderMgr::PROJECTOR_MATRIX, 1, GL_FALSE,
  10378. screen_to_light.getF32ptr());
  10379. shader.uniform3fv(LLShaderMgr::PROJECTOR_P, 1, p1.getF32ptr());
  10380. shader.uniform3fv(LLShaderMgr::PROJECTOR_N, 1, n.getF32ptr());
  10381. shader.uniform3fv(LLShaderMgr::PROJECTOR_ORIGIN, 1,
  10382. screen_origin.getF32ptr());
  10383. shader.uniform1f(LLShaderMgr::PROJECTOR_RANGE, proj_range);
  10384. shader.uniform1f(LLShaderMgr::PROJECTOR_AMBIANCE, params.mV[2]);
  10385. #if OPTIMIZED_UNIFORMS
  10386. if (shader.mFeatures.hasShadows)
  10387. #endif
  10388. {
  10389. S32 s_idx = -1;
  10390. for (U32 i = 0; i < 2; ++i)
  10391. {
  10392. if (mShadowSpotLight[i] == drawablep)
  10393. {
  10394. s_idx = i;
  10395. }
  10396. }
  10397. shader.uniform1i(LLShaderMgr::PROJECTOR_SHADOW_INDEX, s_idx);
  10398. if (s_idx >= 0)
  10399. {
  10400. shader.uniform1f(LLShaderMgr::PROJECTOR_SHADOW_FADE,
  10401. 1.f - mSpotLightFade[s_idx]);
  10402. }
  10403. else
  10404. {
  10405. shader.uniform1f(LLShaderMgr::PROJECTOR_SHADOW_FADE, 1.f);
  10406. }
  10407. if (!gCubeSnapshot)
  10408. {
  10409. LLDrawable* potentialp = drawablep;
  10410. // Determine if this is a good light for casting shadows
  10411. F32 m_pri = volp->getSpotLightPriority();
  10412. for (U32 i = 0; i < 2; ++i)
  10413. {
  10414. F32 pri = 0.f;
  10415. LLDrawable* slightp = mTargetShadowSpotLight[i].get();
  10416. if (slightp)
  10417. {
  10418. pri = slightp->getVOVolume()->getSpotLightPriority();
  10419. }
  10420. if (m_pri > pri)
  10421. {
  10422. LLDrawable* tempp = mTargetShadowSpotLight[i];
  10423. mTargetShadowSpotLight[i] = potentialp;
  10424. potentialp = tempp;
  10425. m_pri = pri;
  10426. }
  10427. }
  10428. }
  10429. }
  10430. #if DEBUG_OPTIMIZED_UNIFORMS && OPTIMIZED_UNIFORMS
  10431. else if (shader.getUniformLocation(LLShaderMgr::PROJECTOR_SHADOW_INDEX) >= 0 ||
  10432. shader.getUniformLocation(LLShaderMgr::PROJECTOR_SHADOW_FADE) >= 0)
  10433. {
  10434. llwarns_once << "Shader: " << shader.mName
  10435. << " shall be marked as hasShadows !" << llendl;
  10436. }
  10437. #endif
  10438. LLViewerTexture* texp = volp->getLightTexture();
  10439. if (!texp)
  10440. {
  10441. texp = LLViewerFetchedTexture::sWhiteImagep;
  10442. }
  10443. S32 channel = shader.enableTexture(LLShaderMgr::DEFERRED_PROJECTION);
  10444. if (channel > -1)
  10445. {
  10446. if (texp)
  10447. {
  10448. gGL.getTexUnit(channel)->bind(texp);
  10449. shader.uniform1f(LLShaderMgr::PROJECTOR_FOCUS, focus);
  10450. static const F32 INVLOG2 = 1.f / logf(2.f);
  10451. F32 lod_range = logf(texp->getWidth()) * INVLOG2;
  10452. shader.uniform1f(LLShaderMgr::PROJECTOR_LOD, lod_range);
  10453. }
  10454. }
  10455. stop_glerror();
  10456. }
  10457. void LLPipeline::unbindDeferredShader(LLGLSLShader& shader)
  10458. {
  10459. LLTexUnit::eTextureType usage = mRT->mDeferredScreen.getUsage();
  10460. shader.disableTexture(LLShaderMgr::DEFERRED_NORMAL, usage);
  10461. shader.disableTexture(LLShaderMgr::DEFERRED_DIFFUSE, usage);
  10462. shader.disableTexture(LLShaderMgr::DEFERRED_SPECULAR, usage);
  10463. if (gUsePBRShaders)
  10464. {
  10465. shader.disableTexture(LLShaderMgr::DEFERRED_EMISSIVE, usage);
  10466. shader.disableTexture(LLShaderMgr::DEFERRED_BRDF_LUT);
  10467. }
  10468. else
  10469. {
  10470. // Not the same buffer used for DEFERRED_DEPTH in EE rendering, unlike
  10471. // what happens for PBR rendering. HB
  10472. usage = mDeferredDepth.getUsage();
  10473. }
  10474. shader.disableTexture(LLShaderMgr::DEFERRED_DEPTH, usage);
  10475. shader.disableTexture(LLShaderMgr::DEFERRED_LIGHT,
  10476. mRT->mDeferredLight.getUsage());
  10477. shader.disableTexture(LLShaderMgr::DIFFUSE_MAP);
  10478. if (!gUsePBRShaders) // "bloomMap" not in use any more in PBR shaders. HB
  10479. {
  10480. shader.disableTexture(LLShaderMgr::DEFERRED_BLOOM);
  10481. }
  10482. #if OPTIMIZED_UNIFORMS
  10483. if (shader.mFeatures.hasShadows)
  10484. #endif
  10485. {
  10486. for (U32 i = 0; i < 6; ++i)
  10487. {
  10488. if (shader.disableTexture(LLShaderMgr::DEFERRED_SHADOW0 + i) > -1)
  10489. {
  10490. glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_COMPARE_MODE,
  10491. GL_NONE);
  10492. }
  10493. }
  10494. }
  10495. shader.disableTexture(LLShaderMgr::DEFERRED_NOISE);
  10496. shader.disableTexture(LLShaderMgr::DEFERRED_LIGHTFUNC);
  10497. if (!sReflectionProbesEnabled)
  10498. {
  10499. S32 channel = shader.disableTexture(LLShaderMgr::ENVIRONMENT_MAP,
  10500. LLTexUnit::TT_CUBE_MAP);
  10501. if (channel > -1)
  10502. {
  10503. LLCubeMap* cube_mapp = gSky.mVOSkyp ? gSky.mVOSkyp->getCubeMap()
  10504. : NULL;
  10505. if (cube_mapp)
  10506. {
  10507. cube_mapp->disableTexture();
  10508. }
  10509. }
  10510. }
  10511. if (gUsePBRShaders)
  10512. {
  10513. unbindReflectionProbes(shader);
  10514. }
  10515. LLTexUnit* unit0 = gGL.getTexUnit(0);
  10516. unit0->unbind(LLTexUnit::TT_TEXTURE);
  10517. unit0->activate();
  10518. shader.unbind();
  10519. stop_glerror();
  10520. }
  10521. // For EE rendering only
  10522. void LLPipeline::generateWaterReflection()
  10523. {
  10524. if (!LLDrawPoolWater::sNeedsReflectionUpdate)
  10525. {
  10526. if (!gViewerCamera.cameraUnderWater())
  10527. {
  10528. // Initial sky pass is still needed even if water reflection is not
  10529. // rendering.
  10530. pushRenderTypeMask();
  10531. andRenderTypeMask(RENDER_TYPE_SKY, RENDER_TYPE_WL_SKY,
  10532. RENDER_TYPE_CLOUDS, END_RENDER_TYPES);
  10533. LLCamera camera = gViewerCamera;
  10534. camera.setFar(camera.getFar() * 0.75f);
  10535. updateCull(camera, mSky);
  10536. stateSort(camera, mSky);
  10537. renderGeom(camera);
  10538. popRenderTypeMask();
  10539. }
  10540. return;
  10541. }
  10542. // Disable occlusion culling for reflection/refraction passes.
  10543. LLDisableOcclusionCulling no_occlusion;
  10544. bool skip_avatar_update = false;
  10545. if (!isAgentAvatarValid() || gAgent.getCameraAnimating() ||
  10546. gAgent.getCameraMode() != CAMERA_MODE_MOUSELOOK ||
  10547. !LLVOAvatar::sVisibleInFirstPerson)
  10548. {
  10549. skip_avatar_update = true;
  10550. }
  10551. LLCamera camera = gViewerCamera;
  10552. camera.setFar(camera.getFar() * 0.75f);
  10553. sReflectionRender = true;
  10554. pushRenderTypeMask();
  10555. LLMatrix4a current = gGLModelView;
  10556. LLMatrix4a projection = gGLProjection;
  10557. F32 camera_height = gViewerCamera.getOrigin().mV[VZ];
  10558. LLVector3 reflection_offset(0.f, 0.f,
  10559. fabsf(camera_height - mWaterHeight) * 2.f);
  10560. LLVector3 reflect_origin = gViewerCamera.getOrigin() - reflection_offset;
  10561. const LLVector3& camera_look_at = gViewerCamera.getAtAxis();
  10562. LLVector3 reflection_look_at(camera_look_at.mV[VX], camera_look_at.mV[VY],
  10563. -camera_look_at.mV[VZ]);
  10564. LLVector3 reflect_interest_point = reflect_origin +
  10565. reflection_look_at * 5.f;
  10566. camera.setOriginAndLookAt(reflect_origin, LLVector3::z_axis,
  10567. reflect_interest_point);
  10568. // Plane params
  10569. LLVector3 pnorm;
  10570. bool camera_is_underwater = gViewerCamera.cameraUnderWater();
  10571. if (camera_is_underwater)
  10572. {
  10573. // Camera is below water, cull above water
  10574. pnorm.set(0.f, 0.f, 1.f);
  10575. }
  10576. else
  10577. {
  10578. // Camera is above water, cull below water
  10579. pnorm.set(0.f, 0.f, -1.f);
  10580. }
  10581. LLPlane plane(LLVector3(0.f, 0.f, mWaterHeight), pnorm);
  10582. if (!camera_is_underwater)
  10583. {
  10584. // Generate planar reflection map
  10585. LLViewerCamera::sCurCameraID = LLViewerCamera::CAMERA_WATER0;
  10586. gGL.matrixMode(LLRender::MM_MODELVIEW);
  10587. gGL.pushMatrix();
  10588. LLMatrix4a mat;
  10589. mat.setIdentity();
  10590. mat.getRow<2>().negate();
  10591. mat.setTranslateAffine(LLVector3(0.f, 0.f, mWaterHeight * 2.f));
  10592. mat.setMul(current, mat);
  10593. mReflectionModelView = mat;
  10594. gGLModelView = mat;
  10595. gGL.loadMatrix(mat);
  10596. LLViewerCamera::updateFrustumPlanes(camera, false, true);
  10597. LLVector4a origin;
  10598. origin.clear();
  10599. LLMatrix4a inv_mat = mat;
  10600. inv_mat.invert();
  10601. inv_mat.affineTransform(origin, origin);
  10602. camera.setOrigin(origin.getF32ptr());
  10603. glCullFace(GL_FRONT);
  10604. if (LLDrawPoolWater::sNeedsReflectionUpdate)
  10605. {
  10606. gGL.getTexUnit(0)->unbind(LLTexUnit::TT_TEXTURE);
  10607. glClearColor(0.f, 0.f, 0.f, 0.f);
  10608. mWaterRef.bindTarget();
  10609. gGL.setColorMask(true, true);
  10610. mWaterRef.clear();
  10611. gGL.setColorMask(true, false);
  10612. mWaterRef.getViewport(gGLViewport);
  10613. // Initial sky pass (no user clip plane)
  10614. // Mask out everything but the sky
  10615. pushRenderTypeMask();
  10616. U32 reflection_type = waterReflectionType();
  10617. if (reflection_type < 5) // Render sky without clouds
  10618. {
  10619. andRenderTypeMask(RENDER_TYPE_SKY, RENDER_TYPE_WL_SKY,
  10620. END_RENDER_TYPES);
  10621. }
  10622. else // Render sky with clouds
  10623. {
  10624. andRenderTypeMask(RENDER_TYPE_SKY, RENDER_TYPE_WL_SKY,
  10625. RENDER_TYPE_CLOUDS, END_RENDER_TYPES);
  10626. }
  10627. updateCull(camera, mSky);
  10628. stateSort(camera, mSky);
  10629. renderGeom(camera);
  10630. popRenderTypeMask();
  10631. if (reflection_type > 1) // If not just sky to render
  10632. {
  10633. pushRenderTypeMask();
  10634. // These have just been rendered above: remove them now.
  10635. clearRenderTypeMask(RENDER_TYPE_WATER, RENDER_TYPE_VOIDWATER,
  10636. RENDER_TYPE_SKY, RENDER_TYPE_CLOUDS,
  10637. END_RENDER_TYPES);
  10638. // Mask out selected geometry based on reflection type
  10639. if (reflection_type < 5) // Remove particles
  10640. {
  10641. clearRenderTypeMask(RENDER_TYPE_PARTICLES,
  10642. END_RENDER_TYPES);
  10643. if (reflection_type < 4) // Remove avatars
  10644. {
  10645. clearRenderTypeMask(RENDER_TYPE_AVATAR,
  10646. RENDER_TYPE_PUPPET,
  10647. END_RENDER_TYPES);
  10648. if (reflection_type < 3) // Remove objects
  10649. {
  10650. clearRenderTypeMask(RENDER_TYPE_VOLUME,
  10651. END_RENDER_TYPES);
  10652. }
  10653. }
  10654. }
  10655. LLGLUserClipPlane clip_plane(plane, mReflectionModelView,
  10656. projection);
  10657. LLGLDisable cull(GL_CULL_FACE);
  10658. updateCull(camera, mReflectedObjects, &plane);
  10659. stateSort(camera, mReflectedObjects);
  10660. renderGeom(camera);
  10661. popRenderTypeMask();
  10662. }
  10663. mWaterRef.flush();
  10664. }
  10665. glCullFace(GL_BACK);
  10666. gGL.matrixMode(LLRender::MM_MODELVIEW);
  10667. gGL.popMatrix();
  10668. gGLModelView = current;
  10669. }
  10670. camera.setOrigin(gViewerCamera.getOrigin());
  10671. // Render distortion map
  10672. static bool last_update = true;
  10673. if (last_update)
  10674. {
  10675. pushRenderTypeMask();
  10676. camera.setFar(gViewerCamera.getFar());
  10677. clearRenderTypeMask(RENDER_TYPE_WATER, RENDER_TYPE_VOIDWATER,
  10678. END_RENDER_TYPES);
  10679. // Intentionally inverted so that distortion map contents (objects
  10680. // under the water when we are above it) will properly include water
  10681. // fog effects.
  10682. sUnderWaterRender = !camera_is_underwater;
  10683. if (sUnderWaterRender)
  10684. {
  10685. clearRenderTypeMask(RENDER_TYPE_SKY, RENDER_TYPE_CLOUDS,
  10686. RENDER_TYPE_WL_SKY, END_RENDER_TYPES);
  10687. }
  10688. LLViewerCamera::updateFrustumPlanes(camera);
  10689. gGL.getTexUnit(0)->unbind(LLTexUnit::TT_TEXTURE);
  10690. if (sUnderWaterRender || LLDrawPoolWater::sNeedsReflectionUpdate)
  10691. {
  10692. LLColor4& col = LLDrawPoolWater::sWaterFogColor;
  10693. glClearColor(col.mV[0], col.mV[1], col.mV[2], 0.f);
  10694. // *HACK: pretend underwater camera is the world camera to fix
  10695. // weird visibility artifacts during distortion render (does not
  10696. // break main render because the camera is the same perspective as
  10697. // world camera and occlusion culling is disabled for this pass).
  10698. LLViewerCamera::sCurCameraID = LLViewerCamera::CAMERA_WORLD;
  10699. mWaterDis.bindTarget();
  10700. mWaterDis.getViewport(gGLViewport);
  10701. gGL.setColorMask(true, true);
  10702. mWaterDis.clear();
  10703. gGL.setColorMask(true, false);
  10704. // Clip out geometry on the same side of water as the camera with
  10705. // enough margin to not include the water geo itself, but not so
  10706. // much as to clip out parts of avatars that should be seen under
  10707. // the water in the distortion map.
  10708. constexpr F32 nudge_factor = 1.0125f;
  10709. F32 water_dist = mWaterHeight;
  10710. if (camera_is_underwater)
  10711. {
  10712. // Nudge clip plane below water to avoid visible holes in
  10713. // objects intersecting the water surface.
  10714. water_dist *= 1.f / nudge_factor;
  10715. // Camera is below water, clip plane points up
  10716. pnorm.set(0.f, 0.f, -1.f);
  10717. }
  10718. else
  10719. {
  10720. // Nudge clip plane above water to avoid visible holes in
  10721. // objects intersecting the water surface.
  10722. water_dist *= nudge_factor;
  10723. // Camera is above water, clip plane points down
  10724. pnorm.set(0.f, 0.f, 1.f);
  10725. }
  10726. LLPlane plane(LLVector3(0.f, 0.f, water_dist), pnorm);
  10727. LLGLUserClipPlane clip_plane(plane, current, projection);
  10728. gGL.setColorMask(true, true);
  10729. mWaterDis.clear();
  10730. gGL.setColorMask(true, false);
  10731. if (RenderWaterReflectionType)
  10732. {
  10733. updateCull(camera, mRefractedObjects, &plane);
  10734. stateSort(camera, mRefractedObjects);
  10735. renderGeom(camera);
  10736. }
  10737. gUIProgram.bind();
  10738. gWorld.renderPropertyLines();
  10739. gUIProgram.unbind();
  10740. mWaterDis.flush();
  10741. }
  10742. popRenderTypeMask();
  10743. }
  10744. last_update = LLDrawPoolWater::sNeedsReflectionUpdate;
  10745. LLDrawPoolWater::sNeedsReflectionUpdate = false;
  10746. popRenderTypeMask();
  10747. sUnderWaterRender = false;
  10748. sReflectionRender = false;
  10749. if (!LLRenderTarget::sUseFBO)
  10750. {
  10751. glClear(GL_DEPTH_BUFFER_BIT);
  10752. }
  10753. glClearColor(0.f, 0.f, 0.f, 0.f);
  10754. gViewerWindowp->setupViewport();
  10755. LL_GL_CHECK_STATES;
  10756. if (!skip_avatar_update)
  10757. {
  10758. gAgentAvatarp->updateAttachmentVisibility(gAgent.getCameraMode());
  10759. }
  10760. LLViewerCamera::sCurCameraID = LLViewerCamera::CAMERA_WORLD;
  10761. }
  10762. // For PBR rendering only
  10763. void LLPipeline::renderShadow(const LLMatrix4a& view, const LLMatrix4a& proj,
  10764. LLCamera& shadow_cam, LLCullResult& result,
  10765. bool depth_clamp)
  10766. {
  10767. LL_FAST_TIMER(FTM_SHADOW_RENDER);
  10768. // Disable occlusion culling during shadow render
  10769. LLDisableOcclusionCulling no_occlusion;
  10770. sShadowRender = true;
  10771. static const U32 types[] =
  10772. {
  10773. LLRenderPass::PASS_SIMPLE,
  10774. LLRenderPass::PASS_FULLBRIGHT,
  10775. LLRenderPass::PASS_SHINY,
  10776. LLRenderPass::PASS_BUMP,
  10777. LLRenderPass::PASS_FULLBRIGHT_SHINY,
  10778. LLRenderPass::PASS_MATERIAL,
  10779. LLRenderPass::PASS_MATERIAL_ALPHA_EMISSIVE,
  10780. LLRenderPass::PASS_SPECMAP,
  10781. LLRenderPass::PASS_SPECMAP_EMISSIVE,
  10782. LLRenderPass::PASS_NORMMAP,
  10783. LLRenderPass::PASS_NORMMAP_EMISSIVE,
  10784. LLRenderPass::PASS_NORMSPEC,
  10785. LLRenderPass::PASS_NORMSPEC_EMISSIVE
  10786. };
  10787. LLGLEnable cull(GL_CULL_FACE);
  10788. #if 0 // The OpenGL version required by PBR should have depth clamp. HB
  10789. if (depth_clamp)
  10790. {
  10791. static LLCachedControl<bool> dclamp(gSavedSettings,
  10792. "RenderDepthClampShadows");
  10793. if (!dclamp || !gGLManager.mUseDepthClamp)
  10794. {
  10795. depth_clamp = false;
  10796. }
  10797. }
  10798. #endif
  10799. LLGLEnable clamp_depth(depth_clamp ? GL_DEPTH_CLAMP : 0);
  10800. LLGLDepthTest depth_test(GL_TRUE, GL_TRUE, GL_LESS);
  10801. updateCull(shadow_cam, result);
  10802. stateSort(shadow_cam, result);
  10803. // Generate shadow map
  10804. gGL.matrixMode(LLRender::MM_PROJECTION);
  10805. gGL.pushMatrix();
  10806. gGL.loadMatrix(proj);
  10807. gGL.matrixMode(LLRender::MM_MODELVIEW);
  10808. gGL.pushMatrix();
  10809. gGL.loadMatrix(view);
  10810. gGLLastMatrix = NULL;
  10811. LLTexUnit* unit0 = gGL.getTexUnit(0);
  10812. unit0->unbind(LLTexUnit::TT_TEXTURE);
  10813. stop_glerror();
  10814. LLVertexBuffer::unbind();
  10815. for (U32 rigged = 0; rigged < 2; ++rigged)
  10816. {
  10817. gDeferredShadowProgram.bind(rigged);
  10818. gGL.diffuseColor4f(1.f, 1.f, 1.f, 1.f);
  10819. // If not using VSM, disable color writes
  10820. if (RenderShadowDetail <= 2)
  10821. {
  10822. gGL.setColorMask(false, false);
  10823. }
  10824. LL_FAST_TIMER(FTM_SHADOW_SIMPLE);
  10825. unit0->disable();
  10826. constexpr U32 count = sizeof(types) / sizeof(U32);
  10827. for (U32 i = 0; i < count; ++i)
  10828. {
  10829. renderObjects(types[i], 0, false, false, rigged);
  10830. }
  10831. renderGLTFObjects(LLRenderPass::PASS_MAT_PBR, false, rigged);
  10832. unit0->enable(LLTexUnit::TT_TEXTURE);
  10833. }
  10834. {
  10835. LL_TRACY_TIMER(TRC_SHADOW_GEOM);
  10836. renderGeomShadow(shadow_cam);
  10837. }
  10838. S32 sun_up = mIsSunUp ? 1 : 0;
  10839. const F32 width = LLRenderTarget::sCurResX;
  10840. LLGLSLShader* shaderp;
  10841. {
  10842. LL_FAST_TIMER(FTM_SHADOW_ALPHA);
  10843. for (U32 rigged = 0; rigged < 2; ++rigged)
  10844. {
  10845. gDeferredShadowAlphaMaskProgram.bind(rigged);
  10846. shaderp = LLGLSLShader::sCurBoundShaderPtr;
  10847. shaderp->uniform1i(LLShaderMgr::SUN_UP_FACTOR, sun_up);
  10848. shaderp->uniform1f(LLShaderMgr::DEFERRED_SHADOW_TARGET_WIDTH,
  10849. width);
  10850. renderMaskedObjects(LLRenderPass::PASS_ALPHA_MASK, 0, true, true,
  10851. rigged);
  10852. renderAlphaObjects(rigged);
  10853. gDeferredShadowFullbrightAlphaMaskProgram.bind(rigged);
  10854. shaderp = LLGLSLShader::sCurBoundShaderPtr;
  10855. shaderp->uniform1i(LLShaderMgr::SUN_UP_FACTOR, sun_up);
  10856. shaderp->uniform1f(LLShaderMgr::DEFERRED_SHADOW_TARGET_WIDTH,
  10857. width);
  10858. renderFullbrightMaskedObjects(LLRenderPass::PASS_FULLBRIGHT_ALPHA_MASK,
  10859. 0, true, true, rigged);
  10860. gDeferredTreeShadowProgram.bind(rigged);
  10861. shaderp = LLGLSLShader::sCurBoundShaderPtr;
  10862. shaderp->setMinimumAlpha(0.598f);
  10863. if (!rigged)
  10864. {
  10865. renderObjects(LLRenderPass::PASS_GRASS, 0, true);
  10866. }
  10867. renderMaskedObjects(LLRenderPass::PASS_NORMSPEC_MASK,
  10868. 0, true, false, rigged);
  10869. renderMaskedObjects(LLRenderPass::PASS_MATERIAL_ALPHA_MASK,
  10870. 0, true, false, rigged);
  10871. renderMaskedObjects(LLRenderPass::PASS_SPECMAP_MASK,
  10872. 0, true, false, rigged);
  10873. renderMaskedObjects(LLRenderPass::PASS_NORMMAP_MASK,
  10874. 0, true, false, rigged);
  10875. }
  10876. }
  10877. for (U32 rigged = 0; rigged < 2; ++rigged)
  10878. {
  10879. gDeferredShadowGLTFAlphaMaskProgram.bind(rigged);
  10880. shaderp = LLGLSLShader::sCurBoundShaderPtr;
  10881. shaderp->uniform1i(LLShaderMgr::SUN_UP_FACTOR, sun_up);
  10882. shaderp->uniform1f(LLShaderMgr::DEFERRED_SHADOW_TARGET_WIDTH, width);
  10883. gGL.loadMatrix(gGLModelView);
  10884. gGLLastMatrix = NULL;
  10885. constexpr U32 type = LLRenderPass::PASS_MAT_PBR_ALPHA_MASK;
  10886. if (rigged)
  10887. {
  10888. LLRenderPass::pushRiggedGLTFBatches(type + 1);
  10889. }
  10890. else
  10891. {
  10892. LLRenderPass::pushGLTFBatches(type);
  10893. }
  10894. gGL.loadMatrix(gGLModelView);
  10895. gGLLastMatrix = NULL;
  10896. }
  10897. gDeferredShadowCubeProgram.bind();
  10898. gGLLastMatrix = NULL;
  10899. gGL.loadMatrix(gGLModelView);
  10900. gGL.setColorMask(true, true);
  10901. gGL.matrixMode(LLRender::MM_PROJECTION);
  10902. gGL.popMatrix();
  10903. gGL.matrixMode(LLRender::MM_MODELVIEW);
  10904. gGL.popMatrix();
  10905. gGLLastMatrix = NULL;
  10906. sShadowRender = false;
  10907. }
  10908. // For EE rendering only
  10909. void LLPipeline::renderShadow(const LLMatrix4a& view, const LLMatrix4a& proj,
  10910. LLCamera& shadow_cam, LLCullResult& result,
  10911. bool use_shader, bool use_occlusion,
  10912. U32 target_width)
  10913. {
  10914. LL_FAST_TIMER(FTM_SHADOW_RENDER);
  10915. // Clip out geometry on the same side of water as the camera
  10916. S32 occlude = sUseOcclusion;
  10917. if (!use_occlusion)
  10918. {
  10919. sUseOcclusion = 0;
  10920. }
  10921. sShadowRender = true;
  10922. static const U32 types[] =
  10923. {
  10924. LLRenderPass::PASS_SIMPLE,
  10925. LLRenderPass::PASS_FULLBRIGHT,
  10926. LLRenderPass::PASS_SHINY,
  10927. LLRenderPass::PASS_BUMP,
  10928. LLRenderPass::PASS_FULLBRIGHT_SHINY,
  10929. LLRenderPass::PASS_MATERIAL,
  10930. LLRenderPass::PASS_MATERIAL_ALPHA_EMISSIVE,
  10931. LLRenderPass::PASS_SPECMAP,
  10932. LLRenderPass::PASS_SPECMAP_EMISSIVE,
  10933. LLRenderPass::PASS_NORMMAP,
  10934. LLRenderPass::PASS_NORMMAP_EMISSIVE,
  10935. LLRenderPass::PASS_NORMSPEC,
  10936. LLRenderPass::PASS_NORMSPEC_EMISSIVE,
  10937. };
  10938. LLGLEnable cull(GL_CULL_FACE);
  10939. // Enable depth clamping if available and in use for shaders.
  10940. U32 depth_clamp_state = 0;
  10941. if (gGLManager.mUseDepthClamp)
  10942. {
  10943. // Added a debug setting to see if it makes any difference for
  10944. // projectors with some GPU/drivers (no difference seen by me
  10945. // for NVIDIA GPU + proprietary drivers). HB
  10946. static LLCachedControl<bool> dclamp(gSavedSettings,
  10947. "RenderDepthClampShadows");
  10948. if (dclamp)
  10949. {
  10950. depth_clamp_state = GL_DEPTH_CLAMP;
  10951. }
  10952. }
  10953. LLGLEnable depth_clamp(depth_clamp_state);
  10954. if (use_shader)
  10955. {
  10956. gDeferredShadowCubeProgram.bind();
  10957. }
  10958. LLRenderTarget& occlusion_target =
  10959. mShadowOcclusion[LLViewerCamera::sCurCameraID - 1];
  10960. occlusion_target.bindTarget();
  10961. updateCull(shadow_cam, result);
  10962. occlusion_target.flush();
  10963. stateSort(shadow_cam, result);
  10964. // Generate shadow map
  10965. gGL.matrixMode(LLRender::MM_PROJECTION);
  10966. gGL.pushMatrix();
  10967. gGL.loadMatrix(proj);
  10968. gGL.matrixMode(LLRender::MM_MODELVIEW);
  10969. gGL.pushMatrix();
  10970. gGL.loadMatrix(view);
  10971. gGLLastMatrix = NULL;
  10972. LLTexUnit* unit0 = gGL.getTexUnit(0);
  10973. unit0->unbind(LLTexUnit::TT_TEXTURE);
  10974. stop_glerror();
  10975. LLVertexBuffer::unbind();
  10976. S32 sun_up = mIsSunUp ? 1 : 0;
  10977. for (U32 rigged = 0; rigged < 2; ++rigged)
  10978. {
  10979. if (!use_shader)
  10980. {
  10981. // Occlusion program is general purpose depth-only no-textures
  10982. gOcclusionProgram.bind(rigged);
  10983. }
  10984. else
  10985. {
  10986. gDeferredShadowProgram.bind(rigged);
  10987. gDeferredShadowProgram.uniform1i(LLShaderMgr::SUN_UP_FACTOR,
  10988. sun_up);
  10989. }
  10990. gGL.diffuseColor4f(1.f, 1.f, 1.f, 1.f);
  10991. // If not using VSM, disable color writes
  10992. if (RenderShadowDetail <= 2)
  10993. {
  10994. gGL.setColorMask(false, false);
  10995. }
  10996. LL_FAST_TIMER(FTM_SHADOW_SIMPLE);
  10997. unit0->disable();
  10998. constexpr U32 count = sizeof(types) / sizeof(U32);
  10999. for (U32 i = 0; i < count; ++i)
  11000. {
  11001. renderObjects(types[i], LLVertexBuffer::MAP_VERTEX, false, false,
  11002. rigged);
  11003. }
  11004. unit0->enable(LLTexUnit::TT_TEXTURE);
  11005. if (!use_shader)
  11006. {
  11007. gOcclusionProgram.unbind();
  11008. }
  11009. stop_glerror();
  11010. }
  11011. if (use_shader)
  11012. {
  11013. LL_TRACY_TIMER(TRC_SHADOW_GEOM);
  11014. gDeferredShadowProgram.unbind();
  11015. renderGeomShadow(shadow_cam);
  11016. gDeferredShadowProgram.bind();
  11017. gDeferredShadowProgram.uniform1i(LLShaderMgr::SUN_UP_FACTOR, sun_up);
  11018. }
  11019. else
  11020. {
  11021. LL_TRACY_TIMER(TRC_SHADOW_GEOM);
  11022. renderGeomShadow(shadow_cam);
  11023. }
  11024. stop_glerror();
  11025. {
  11026. LL_FAST_TIMER(FTM_SHADOW_ALPHA);
  11027. constexpr U32 NO_IDX_MASK = LLVertexBuffer::MAP_VERTEX |
  11028. LLVertexBuffer::MAP_TEXCOORD0 |
  11029. LLVertexBuffer::MAP_COLOR;
  11030. constexpr U32 IDX_MASK = NO_IDX_MASK |
  11031. LLVertexBuffer::MAP_TEXTURE_INDEX;
  11032. LLGLSLShader* shaderp;
  11033. for (U32 rigged = 0; rigged < 2; ++rigged)
  11034. {
  11035. gDeferredShadowAlphaMaskProgram.bind(rigged);
  11036. shaderp = LLGLSLShader::sCurBoundShaderPtr;
  11037. shaderp->uniform1f(LLShaderMgr::DEFERRED_SHADOW_TARGET_WIDTH,
  11038. (F32)target_width);
  11039. shaderp->uniform1i(LLShaderMgr::SUN_UP_FACTOR, sun_up);
  11040. renderMaskedObjects(LLRenderPass::PASS_ALPHA_MASK, IDX_MASK,
  11041. true, true, rigged);
  11042. shaderp->setMinimumAlpha(0.598f);
  11043. renderAlphaObjects(rigged);
  11044. gDeferredShadowFullbrightAlphaMaskProgram.bind(rigged);
  11045. shaderp = LLGLSLShader::sCurBoundShaderPtr;
  11046. shaderp->uniform1f(LLShaderMgr::DEFERRED_SHADOW_TARGET_WIDTH,
  11047. (F32)target_width);
  11048. shaderp->uniform1i(LLShaderMgr::SUN_UP_FACTOR, sun_up);
  11049. renderFullbrightMaskedObjects(LLRenderPass::PASS_FULLBRIGHT_ALPHA_MASK,
  11050. IDX_MASK, true, true, rigged);
  11051. gDeferredTreeShadowProgram.bind(rigged);
  11052. shaderp = LLGLSLShader::sCurBoundShaderPtr;
  11053. if (!rigged)
  11054. {
  11055. shaderp->setMinimumAlpha(0.598f);
  11056. renderObjects(LLRenderPass::PASS_GRASS,
  11057. LLVertexBuffer::MAP_VERTEX |
  11058. LLVertexBuffer::MAP_TEXCOORD0,
  11059. true);
  11060. }
  11061. shaderp->uniform1i(LLShaderMgr::SUN_UP_FACTOR, sun_up);
  11062. renderMaskedObjects(LLRenderPass::PASS_NORMSPEC_MASK,
  11063. NO_IDX_MASK, true, false, rigged);
  11064. renderMaskedObjects(LLRenderPass::PASS_MATERIAL_ALPHA_MASK,
  11065. NO_IDX_MASK, true, false, rigged);
  11066. renderMaskedObjects(LLRenderPass::PASS_SPECMAP_MASK,
  11067. NO_IDX_MASK, true, false, rigged);
  11068. renderMaskedObjects(LLRenderPass::PASS_NORMMAP_MASK,
  11069. NO_IDX_MASK, true, false, rigged);
  11070. }
  11071. stop_glerror();
  11072. }
  11073. #if 0
  11074. glCullFace(GL_BACK);
  11075. #endif
  11076. gDeferredShadowCubeProgram.bind();
  11077. gGLLastMatrix = NULL;
  11078. gGL.loadMatrix(gGLModelView);
  11079. LLRenderTarget& occlusion_source =
  11080. mShadow[LLViewerCamera::sCurCameraID - 1];
  11081. doOcclusion(shadow_cam, occlusion_source, occlusion_target);
  11082. if (use_shader)
  11083. {
  11084. gDeferredShadowProgram.unbind();
  11085. }
  11086. gGL.setColorMask(true, true);
  11087. gGL.matrixMode(LLRender::MM_PROJECTION);
  11088. gGL.popMatrix();
  11089. gGL.matrixMode(LLRender::MM_MODELVIEW);
  11090. gGL.popMatrix();
  11091. gGLLastMatrix = NULL;
  11092. sUseOcclusion = occlude;
  11093. sShadowRender = false;
  11094. stop_glerror();
  11095. }
  11096. // Gets point cloud of intersection of frust and min, max
  11097. bool LLPipeline::getVisiblePointCloud(LLCamera& camera,
  11098. LLVector3& min, LLVector3& max,
  11099. std::vector<LLVector3>& fp,
  11100. LLVector3 light_dir)
  11101. {
  11102. LL_FAST_TIMER(FTM_VISIBLE_CLOUD);
  11103. if (getVisibleExtents(camera, min, max))
  11104. {
  11105. return false;
  11106. }
  11107. // Get set of planes on bounding box
  11108. LLPlane bp[] =
  11109. {
  11110. LLPlane(min, LLVector3(-1.f, 0.f, 0.f)),
  11111. LLPlane(min, LLVector3(0.f, -1.f, 0.f)),
  11112. LLPlane(min, LLVector3(0.f, 0.f, -1.f)),
  11113. LLPlane(max, LLVector3(1.f, 0.f, 0.f)),
  11114. LLPlane(max, LLVector3(0.f, 1.f, 0.f)),
  11115. LLPlane(max, LLVector3(0.f, 0.f, 1.f))
  11116. };
  11117. // Potential points
  11118. std::vector<LLVector3> pp;
  11119. // Add corners of AABB
  11120. pp.emplace_back(min.mV[0], min.mV[1], min.mV[2]);
  11121. pp.emplace_back(max.mV[0], min.mV[1], min.mV[2]);
  11122. pp.emplace_back(min.mV[0], max.mV[1], min.mV[2]);
  11123. pp.emplace_back(max.mV[0], max.mV[1], min.mV[2]);
  11124. pp.emplace_back(min.mV[0], min.mV[1], max.mV[2]);
  11125. pp.emplace_back(max.mV[0], min.mV[1], max.mV[2]);
  11126. pp.emplace_back(min.mV[0], max.mV[1], max.mV[2]);
  11127. pp.emplace_back(max.mV[0], max.mV[1], max.mV[2]);
  11128. // Add corners of camera frustum
  11129. for (U32 i = 0; i < LLCamera::AGENT_FRUSTRUM_NUM; ++i)
  11130. {
  11131. pp.emplace_back(camera.mAgentFrustum[i]);
  11132. }
  11133. // Bounding box line segments
  11134. static const U32 bs[] =
  11135. {
  11136. 0, 1,
  11137. 1, 3,
  11138. 3, 2,
  11139. 2, 0,
  11140. 4, 5,
  11141. 5, 7,
  11142. 7, 6,
  11143. 6, 4,
  11144. 0, 4,
  11145. 1, 5,
  11146. 3, 7,
  11147. 2, 6
  11148. };
  11149. for (U32 i = 0; i < 12; ++i) // For each line segment in bounding box
  11150. {
  11151. const LLVector3& v1 = pp[bs[i * 2]];
  11152. const LLVector3& v2 = pp[bs[i * 2 + 1]];
  11153. LLVector3 n, line;
  11154. // For each plane in camera frustum
  11155. for (U32 j = 0; j < LLCamera::AGENT_PLANE_NO_USER_CLIP_NUM; ++j)
  11156. {
  11157. const LLPlane& cp = camera.getAgentPlane(j);
  11158. cp.getVector3(n);
  11159. line = v1 - v2;
  11160. F32 d1 = line * n;
  11161. F32 d2 = -cp.dist(v2);
  11162. F32 t = d2 / d1;
  11163. if (t > 0.f && t < 1.f)
  11164. {
  11165. pp.emplace_back(v2 + line * t);
  11166. }
  11167. }
  11168. }
  11169. // Camera frustum line segments
  11170. static const U32 fs[] =
  11171. {
  11172. 0, 1,
  11173. 1, 2,
  11174. 2, 3,
  11175. 3, 0,
  11176. 4, 5,
  11177. 5, 6,
  11178. 6, 7,
  11179. 7, 4,
  11180. 0, 4,
  11181. 1, 5,
  11182. 2, 6,
  11183. 3, 7
  11184. };
  11185. for (U32 i = 0; i < 12; ++i)
  11186. {
  11187. const LLVector3& v1 = pp[fs[i * 2] + 8];
  11188. const LLVector3& v2 = pp[fs[i * 2 + 1] + 8];
  11189. LLVector3 n, line;
  11190. for (U32 j = 0; j < 6; ++j)
  11191. {
  11192. const LLPlane& cp = bp[j];
  11193. cp.getVector3(n);
  11194. line = v1 - v2;
  11195. F32 d1 = line * n;
  11196. F32 d2 = -cp.dist(v2);
  11197. F32 t = d2 / d1;
  11198. if (t > 0.f && t < 1.f)
  11199. {
  11200. pp.emplace_back(v2 + line * t);
  11201. }
  11202. }
  11203. }
  11204. LLVector3 ext[] = { min - LLVector3(0.05f, 0.05f, 0.05f),
  11205. max + LLVector3(0.05f, 0.05f, 0.05f) };
  11206. for (U32 i = 0, count = pp.size(); i < count; ++i)
  11207. {
  11208. bool found = true;
  11209. const F32* p = pp[i].mV;
  11210. for (U32 j = 0; j < 3; ++j)
  11211. {
  11212. if (p[j] < ext[0].mV[j] || p[j] > ext[1].mV[j])
  11213. {
  11214. found = false;
  11215. break;
  11216. }
  11217. }
  11218. for (U32 j = 0; j < LLCamera::AGENT_PLANE_NO_USER_CLIP_NUM; ++j)
  11219. {
  11220. const LLPlane& cp = camera.getAgentPlane(j);
  11221. F32 dist = cp.dist(pp[i]);
  11222. if (dist > 0.05f)
  11223. {
  11224. // point is above some plane, not contained
  11225. found = false;
  11226. break;
  11227. }
  11228. }
  11229. if (found)
  11230. {
  11231. fp.emplace_back(pp[i]);
  11232. }
  11233. }
  11234. return !fp.empty();
  11235. }
  11236. void LLPipeline::renderHighlight(const LLViewerObject* objp, F32 fade)
  11237. {
  11238. if (!objp || objp->isDead() || !objp->getVolume())
  11239. {
  11240. return;
  11241. }
  11242. for (LLViewerObject::child_list_t::const_iterator
  11243. it = objp->getChildren().begin(), end = objp->getChildren().end();
  11244. it != end; ++it)
  11245. {
  11246. renderHighlight(*it, fade);
  11247. }
  11248. LLDrawable* drawablep = objp->mDrawable;
  11249. if (!drawablep)
  11250. {
  11251. return;
  11252. }
  11253. LLColor4 color(1.f, 1.f, 1.f, fade);
  11254. for (S32 i = 0, count = drawablep->getNumFaces(); i < count; ++i)
  11255. {
  11256. LLFace* facep = drawablep->getFace(i);
  11257. if (facep)
  11258. {
  11259. facep->renderSelected(LLViewerTexture::sNullImagep, color);
  11260. }
  11261. }
  11262. }
  11263. // Branched version for the PBR renderer
  11264. void LLPipeline::generateSunShadowPBR()
  11265. {
  11266. LLDisableOcclusionCulling no_occlusion;
  11267. bool skip_avatar_update = false;
  11268. if (!isAgentAvatarValid() || gAgent.getCameraAnimating() ||
  11269. gAgent.getCameraMode() != CAMERA_MODE_MOUSELOOK ||
  11270. !LLVOAvatar::sVisibleInFirstPerson)
  11271. {
  11272. skip_avatar_update = true;
  11273. }
  11274. if (!skip_avatar_update)
  11275. {
  11276. gAgentAvatarp->updateAttachmentVisibility(CAMERA_MODE_THIRD_PERSON);
  11277. }
  11278. // Store last_modelview of world camera
  11279. LLMatrix4a last_modelview = gGLLastModelView;
  11280. LLMatrix4a last_projection = gGLLastProjection;
  11281. pushRenderTypeMask();
  11282. andRenderTypeMask(RENDER_TYPE_SIMPLE,
  11283. RENDER_TYPE_ALPHA,
  11284. RENDER_TYPE_ALPHA_PRE_WATER,
  11285. RENDER_TYPE_ALPHA_POST_WATER,
  11286. RENDER_TYPE_GRASS,
  11287. RENDER_TYPE_MAT_PBR,
  11288. RENDER_TYPE_FULLBRIGHT,
  11289. RENDER_TYPE_BUMP,
  11290. RENDER_TYPE_VOLUME,
  11291. RENDER_TYPE_AVATAR,
  11292. RENDER_TYPE_PUPPET,
  11293. RENDER_TYPE_TREE,
  11294. RENDER_TYPE_TERRAIN,
  11295. RENDER_TYPE_WATER,
  11296. RENDER_TYPE_VOIDWATER,
  11297. RENDER_TYPE_PASS_ALPHA,
  11298. RENDER_TYPE_PASS_ALPHA_MASK,
  11299. RENDER_TYPE_PASS_FULLBRIGHT_ALPHA_MASK,
  11300. RENDER_TYPE_PASS_GRASS,
  11301. RENDER_TYPE_PASS_SIMPLE,
  11302. RENDER_TYPE_PASS_BUMP,
  11303. RENDER_TYPE_PASS_FULLBRIGHT,
  11304. RENDER_TYPE_PASS_SHINY,
  11305. RENDER_TYPE_PASS_FULLBRIGHT_SHINY,
  11306. RENDER_TYPE_PASS_MATERIAL,
  11307. RENDER_TYPE_PASS_MATERIAL_ALPHA,
  11308. RENDER_TYPE_PASS_MATERIAL_ALPHA_MASK,
  11309. RENDER_TYPE_PASS_MATERIAL_ALPHA_EMISSIVE,
  11310. RENDER_TYPE_PASS_SPECMAP,
  11311. RENDER_TYPE_PASS_SPECMAP_BLEND,
  11312. RENDER_TYPE_PASS_SPECMAP_MASK,
  11313. RENDER_TYPE_PASS_SPECMAP_EMISSIVE,
  11314. RENDER_TYPE_PASS_NORMMAP,
  11315. RENDER_TYPE_PASS_NORMMAP_BLEND,
  11316. RENDER_TYPE_PASS_NORMMAP_MASK,
  11317. RENDER_TYPE_PASS_NORMMAP_EMISSIVE,
  11318. RENDER_TYPE_PASS_NORMSPEC,
  11319. RENDER_TYPE_PASS_NORMSPEC_BLEND,
  11320. RENDER_TYPE_PASS_NORMSPEC_MASK,
  11321. RENDER_TYPE_PASS_NORMSPEC_EMISSIVE,
  11322. RENDER_TYPE_PASS_ALPHA_MASK_RIGGED,
  11323. RENDER_TYPE_PASS_FULLBRIGHT_ALPHA_MASK_RIGGED,
  11324. RENDER_TYPE_PASS_SIMPLE_RIGGED,
  11325. RENDER_TYPE_PASS_BUMP_RIGGED,
  11326. RENDER_TYPE_PASS_FULLBRIGHT_RIGGED,
  11327. RENDER_TYPE_PASS_SHINY_RIGGED,
  11328. RENDER_TYPE_PASS_FULLBRIGHT_SHINY_RIGGED,
  11329. RENDER_TYPE_PASS_MATERIAL_RIGGED,
  11330. RENDER_TYPE_PASS_MATERIAL_ALPHA_RIGGED,
  11331. RENDER_TYPE_PASS_MATERIAL_ALPHA_MASK_RIGGED,
  11332. RENDER_TYPE_PASS_MATERIAL_ALPHA_EMISSIVE_RIGGED,
  11333. RENDER_TYPE_PASS_SPECMAP_RIGGED,
  11334. RENDER_TYPE_PASS_SPECMAP_BLEND_RIGGED,
  11335. RENDER_TYPE_PASS_SPECMAP_MASK_RIGGED,
  11336. RENDER_TYPE_PASS_SPECMAP_EMISSIVE_RIGGED,
  11337. RENDER_TYPE_PASS_NORMMAP_RIGGED,
  11338. RENDER_TYPE_PASS_NORMMAP_BLEND_RIGGED,
  11339. RENDER_TYPE_PASS_NORMMAP_MASK_RIGGED,
  11340. RENDER_TYPE_PASS_NORMMAP_EMISSIVE_RIGGED,
  11341. RENDER_TYPE_PASS_NORMSPEC_RIGGED,
  11342. RENDER_TYPE_PASS_NORMSPEC_BLEND_RIGGED,
  11343. RENDER_TYPE_PASS_NORMSPEC_MASK_RIGGED,
  11344. RENDER_TYPE_PASS_NORMSPEC_EMISSIVE_RIGGED,
  11345. RENDER_TYPE_PASS_MAT_PBR,
  11346. RENDER_TYPE_PASS_MAT_PBR_RIGGED,
  11347. RENDER_TYPE_PASS_MAT_ALPHA_MASK_PBR,
  11348. RENDER_TYPE_PASS_MAT_ALPHA_MASK_PBR_RIGGED,
  11349. END_RENDER_TYPES);
  11350. gGL.setColorMask(false, false);
  11351. // Get sun view matrix
  11352. // Store current projection/modelview matrix
  11353. const LLMatrix4a saved_proj = gGLProjection;
  11354. const LLMatrix4a saved_view = gGLModelView;
  11355. LLMatrix4a inv_view(saved_view);
  11356. inv_view.invert();
  11357. LLMatrix4a view[6];
  11358. LLMatrix4a proj[6];
  11359. LLVector3 caster_dir(mIsSunUp ? mSunDir : mMoonDir);
  11360. // Put together a universal "near clip" plane for shadow frusta
  11361. LLPlane shadow_near_clip;
  11362. LLVector3 p = gViewerCamera.getOrigin();
  11363. p += caster_dir * RenderFarClip * 2.f;
  11364. shadow_near_clip.setVec(p, caster_dir);
  11365. LLVector3 light_dir = -caster_dir;
  11366. light_dir.normalize();
  11367. // Create light space camera matrix
  11368. LLVector3 at = light_dir;
  11369. LLVector3 up = gViewerCamera.getAtAxis();
  11370. if (fabsf(up * light_dir) > 0.75f)
  11371. {
  11372. up = gViewerCamera.getUpAxis();
  11373. }
  11374. up.normalize();
  11375. at.normalize();
  11376. LLCamera main_camera = gViewerCamera;
  11377. bool no_shadow_frustra = !gCubeSnapshot &&
  11378. !hasRenderDebugMask(RENDER_DEBUG_SHADOW_FRUSTA);
  11379. F32 near_clip = 0.f;
  11380. // Get visible point cloud
  11381. main_camera.calcAgentFrustumPlanes(main_camera.mAgentFrustum);
  11382. LLVector3 min, max;
  11383. std::vector<LLVector3> fp;
  11384. getVisiblePointCloud(main_camera, min, max, fp);
  11385. if (fp.empty())
  11386. {
  11387. if (no_shadow_frustra)
  11388. {
  11389. mShadowCamera[0] = main_camera;
  11390. mShadowExtents[0][0] = min;
  11391. mShadowExtents[0][1] = max;
  11392. mShadowFrustPoints[0].clear();
  11393. mShadowFrustPoints[1].clear();
  11394. mShadowFrustPoints[2].clear();
  11395. mShadowFrustPoints[3].clear();
  11396. }
  11397. popRenderTypeMask();
  11398. if (!skip_avatar_update)
  11399. {
  11400. gAgentAvatarp->updateAttachmentVisibility(gAgent.getCameraMode());
  11401. }
  11402. return;
  11403. }
  11404. LLVector4a v;
  11405. // Get good split distances for frustum
  11406. for (U32 i = 0; i < fp.size(); ++i)
  11407. {
  11408. v.load3(fp[i].mV);
  11409. saved_view.affineTransform(v, v);
  11410. fp[i].set(v.getF32ptr());
  11411. }
  11412. min = fp[0];
  11413. max = fp[0];
  11414. // Get camera space bounding box
  11415. for (U32 i = 1; i < fp.size(); ++i)
  11416. {
  11417. update_min_max(min, max, fp[i]);
  11418. }
  11419. near_clip = llclamp(-max.mV[2], 0.01f, 4.f);
  11420. F32 far_clip = llclamp(-min.mV[2] * 2.f, 16.f, 512.f);
  11421. far_clip = llmin(far_clip, gViewerCamera.getFar());
  11422. F32 range = far_clip - near_clip;
  11423. LLVector3 split_exp = RenderShadowSplitExponent;
  11424. F32 da = 1.f - llmax(fabsf(light_dir * up),
  11425. fabsf(light_dir * gViewerCamera.getLeftAxis()));
  11426. da = powf(da, split_exp.mV[2]);
  11427. F32 sxp = split_exp.mV[1] + (split_exp.mV[0] - split_exp.mV[1]) * da;
  11428. for (U32 i = 0; i < 4; ++i)
  11429. {
  11430. F32 x = (F32)(i + 1) * 0.25f;
  11431. x = powf(x, sxp);
  11432. mSunClipPlanes.mV[i] = near_clip + range * x;
  11433. }
  11434. // Bump back first split for transition padding
  11435. mSunClipPlanes.mV[0] *= 1.25f;
  11436. if (gCubeSnapshot)
  11437. {
  11438. // Stretch clip planes for reflection probe renders to reduce number
  11439. // of shadow passes
  11440. mSunClipPlanes.mV[1] = mSunClipPlanes.mV[2];
  11441. mSunClipPlanes.mV[2] = mSunClipPlanes.mV[3];
  11442. mSunClipPlanes.mV[3] *= 1.5f;
  11443. }
  11444. // Convenience array of 4 near clip plane distances
  11445. F32 dist[] = { near_clip,
  11446. mSunClipPlanes.mV[0],
  11447. mSunClipPlanes.mV[1],
  11448. mSunClipPlanes.mV[2],
  11449. mSunClipPlanes.mV[3] };
  11450. if (mSunLightColor == LLColor4::black)
  11451. {
  11452. // Sun diffuse is totally black, shadows do not matter
  11453. LLGLDepthTest depth(GL_TRUE);
  11454. for (S32 j = 0; j < 4; ++j)
  11455. {
  11456. mRT->mSunShadow[j].bindTarget();
  11457. mRT->mSunShadow[j].clear();
  11458. mRT->mSunShadow[j].flush();
  11459. }
  11460. }
  11461. else
  11462. {
  11463. static LLCachedControl<U32> splits(gSavedSettings,
  11464. "RenderShadowSplits");
  11465. U32 max_splits = llclamp((U32)splits, 0, 3);
  11466. for (U32 j = 0, count = gCubeSnapshot ? 2 : 4; j < count; ++j)
  11467. {
  11468. if (no_shadow_frustra)
  11469. {
  11470. mShadowFrustPoints[j].clear();
  11471. }
  11472. LLViewerCamera::sCurCameraID =
  11473. LLViewerCamera::CAMERA_SUN_SHADOW0 + j;
  11474. // Restore render matrices
  11475. gGLModelView = saved_view;
  11476. gGLProjection = saved_proj;
  11477. LLVector3 eye = gViewerCamera.getOrigin();
  11478. // Camera used for shadow cull/render
  11479. LLCamera shadow_cam = gViewerCamera;
  11480. shadow_cam.setFar(16.f);
  11481. // Create world space camera frustum for this split
  11482. LLViewerCamera::updateFrustumPlanes(shadow_cam, false, false,
  11483. true);
  11484. LLVector3* frust = shadow_cam.mAgentFrustum;
  11485. LLVector3 pn = shadow_cam.getAtAxis();
  11486. // Construct 8 corners of split frustum section
  11487. for (U32 i = 0; i < 4; ++i)
  11488. {
  11489. LLVector3 delta = frust[i + 4] - eye;
  11490. delta += (frust[i + 4] - frust[(i + 2) % 4 + 4]) * 0.05f;
  11491. delta.normalize();
  11492. F32 dp = delta * pn;
  11493. frust[i] = eye + (delta * dist[j] * 0.75f) / dp;
  11494. frust[i + 4] = eye + (delta * dist[j + 1] * 1.25f) / dp;
  11495. }
  11496. shadow_cam.calcAgentFrustumPlanes(frust);
  11497. shadow_cam.mFrustumCornerDist = 0.f;
  11498. if (no_shadow_frustra)
  11499. {
  11500. mShadowCamera[j] = shadow_cam;
  11501. }
  11502. std::vector<LLVector3> fp;
  11503. LLVector3 min, max;
  11504. if (!getVisiblePointCloud(shadow_cam, min, max, fp, light_dir) ||
  11505. j > max_splits)
  11506. {
  11507. // No possible shadow receivers
  11508. if (no_shadow_frustra)
  11509. {
  11510. mShadowExtents[j][0].clear();
  11511. mShadowExtents[j][1].clear();
  11512. mShadowCamera[j + 4] = shadow_cam;
  11513. }
  11514. mRT->mSunShadow[j].bindTarget();
  11515. {
  11516. LLGLDepthTest depth(GL_TRUE);
  11517. mRT->mSunShadow[j].clear();
  11518. }
  11519. mRT->mSunShadow[j].flush();
  11520. continue;
  11521. }
  11522. if (no_shadow_frustra)
  11523. {
  11524. mShadowExtents[j][0] = min;
  11525. mShadowExtents[j][1] = max;
  11526. mShadowFrustPoints[j] = fp;
  11527. }
  11528. // Find a good origin for shadow projection
  11529. LLVector3 origin;
  11530. // Get a temporary view projection
  11531. view[j] = look_proj(gViewerCamera.getOrigin(), light_dir, -up);
  11532. std::vector<LLVector3> wpf;
  11533. LLVector4a p;
  11534. for (U32 i = 0; i < fp.size(); ++i)
  11535. {
  11536. p.load3(fp[i].mV);
  11537. view[j].affineTransform(p, p);
  11538. wpf.emplace_back(LLVector3(p.getF32ptr()));
  11539. }
  11540. min = max = wpf[0];
  11541. for (U32 i = 0; i < fp.size(); ++i)
  11542. {
  11543. // Get AABB in camera space
  11544. update_min_max(min, max, wpf[i]);
  11545. }
  11546. // Construct a perspective transform with perspective along y-axis
  11547. // that contains points in wpf
  11548. // Known:
  11549. // - far clip plane
  11550. // - near clip plane
  11551. // - points in frustum
  11552. // Find:
  11553. // - origin
  11554. // Get some "interesting" points of reference
  11555. LLVector3 center = (min + max) * 0.5f;
  11556. LLVector3 size = (max - min) * 0.5f;
  11557. LLVector3 near_center = center;
  11558. near_center.mV[1] += size.mV[1] * 2.f;
  11559. // Put all points in wpf in quadrant 0, reletive to center of
  11560. // min/max get the best fit line using least squares
  11561. for (U32 i = 0; i < wpf.size(); ++i)
  11562. {
  11563. wpf[i] -= center;
  11564. wpf[i].mV[0] = fabsf(wpf[i].mV[0]);
  11565. wpf[i].mV[2] = fabsf(wpf[i].mV[2]);
  11566. }
  11567. F32 bfm = 0.f;
  11568. F32 bfb = 0.f;
  11569. if (!wpf.empty())
  11570. {
  11571. F32 sx = 0.f;
  11572. F32 sx2 = 0.f;
  11573. F32 sy = 0.f;
  11574. F32 sxy = 0.f;
  11575. for (U32 i = 0; i < wpf.size(); ++i)
  11576. {
  11577. sx += wpf[i].mV[0];
  11578. sx2 += wpf[i].mV[0] * wpf[i].mV[0];
  11579. sy += wpf[i].mV[1];
  11580. sxy += wpf[i].mV[0] * wpf[i].mV[1];
  11581. }
  11582. bfm = (sy * sx - wpf.size() * sxy) /
  11583. (sx * sx - wpf.size() * sx2);
  11584. bfb = (sx * sxy - sy * sx2) / (sx * sx - bfm * sx2);
  11585. }
  11586. if (llisnan(bfm) || llisnan(bfb))
  11587. {
  11588. LL_DEBUGS("Pipeline") << "NaN found. Corresponding shadow rendering aborted. Camera ID: "
  11589. << LLViewerCamera::sCurCameraID
  11590. << LL_ENDL;
  11591. continue;
  11592. }
  11593. {
  11594. // Best fit line is y = bfm * x + bfb
  11595. // Find point that is furthest to the right of line
  11596. F32 off_x = -1.f;
  11597. LLVector3 lp;
  11598. for (U32 i = 0; i < wpf.size(); ++i)
  11599. {
  11600. // y = bfm * x + bfb
  11601. // x = (y - bfb) / bfm
  11602. F32 lx = (wpf[i].mV[1] - bfb) / bfm;
  11603. lx = wpf[i].mV[0] - lx;
  11604. if (off_x < lx)
  11605. {
  11606. off_x = lx;
  11607. lp = wpf[i];
  11608. }
  11609. }
  11610. // Get line with slope bfm through lp
  11611. // bfb = y - bfm * x
  11612. bfb = lp.mV[1] - bfm * lp.mV[0];
  11613. // Calculate error
  11614. F32 shadow_error = 0.f;
  11615. for (U32 i = 0; i < wpf.size(); ++i)
  11616. {
  11617. F32 lx = (wpf[i].mV[1] - bfb) / bfm;
  11618. shadow_error += fabsf(wpf[i].mV[0] - lx);
  11619. }
  11620. shadow_error /= wpf.size() * size.mV[0];
  11621. if (llisnan(shadow_error) ||
  11622. shadow_error > RenderShadowErrorCutoff)
  11623. {
  11624. // Just use ortho projection
  11625. origin.clear();
  11626. proj[j] = gl_ortho(min.mV[0], max.mV[0], min.mV[1],
  11627. max.mV[1], -max.mV[2], -min.mV[2]);
  11628. }
  11629. else
  11630. {
  11631. // Origin is where line x = 0;
  11632. origin.set(0, bfb, 0);
  11633. F32 fovz = 1.f;
  11634. F32 fovx = 1.f;
  11635. LLVector3 zp;
  11636. LLVector3 xp;
  11637. for (U32 i = 0; i < wpf.size(); ++i)
  11638. {
  11639. LLVector3 atz = wpf[i] - origin;
  11640. atz.mV[0] = 0.f;
  11641. atz.normalize();
  11642. if (fovz > -atz.mV[1])
  11643. {
  11644. zp = wpf[i];
  11645. fovz = -atz.mV[1];
  11646. }
  11647. LLVector3 atx = wpf[i] - origin;
  11648. atx.mV[2] = 0.f;
  11649. atx.normalize();
  11650. if (fovx > -atx.mV[1])
  11651. {
  11652. fovx = -atx.mV[1];
  11653. xp = wpf[i];
  11654. }
  11655. }
  11656. fovx = acosf(fovx);
  11657. fovz = acosf(fovz);
  11658. F32 cutoff = RenderShadowFOVCutoff;
  11659. if (fovx < cutoff && fovz > cutoff)
  11660. {
  11661. // x is a good fit, but z is too big, move away from zp
  11662. // enough so that fovz matches cutoff
  11663. F32 d = zp.mV[2] / tanf(cutoff);
  11664. F32 ny = zp.mV[1] + fabsf(d);
  11665. origin.mV[1] = ny;
  11666. fovz = fovx = 1.f;
  11667. for (U32 i = 0; i < wpf.size(); ++i)
  11668. {
  11669. LLVector3 atz = wpf[i] - origin;
  11670. atz.mV[0] = 0.f;
  11671. atz.normalize();
  11672. fovz = llmin(fovz, -atz.mV[1]);
  11673. LLVector3 atx = wpf[i] - origin;
  11674. atx.mV[2] = 0.f;
  11675. atx.normalize();
  11676. fovx = llmin(fovx, -atx.mV[1]);
  11677. }
  11678. fovx = acosf(fovx);
  11679. fovz = acosf(fovz);
  11680. }
  11681. origin += center;
  11682. F32 ynear = origin.mV[1] - max.mV[1];
  11683. F32 yfar = origin.mV[1] - min.mV[1];
  11684. if (ynear < 0.1f) // keep a sensible near clip plane
  11685. {
  11686. F32 diff = 0.1f - ynear;
  11687. origin.mV[1] += diff;
  11688. ynear += diff;
  11689. yfar += diff;
  11690. }
  11691. if (fovx > cutoff)
  11692. {
  11693. // Just use ortho projection
  11694. origin.clear();
  11695. proj[j] = gl_ortho(min.mV[0], max.mV[0], min.mV[1],
  11696. max.mV[1], -max.mV[2], -min.mV[2]);
  11697. }
  11698. else
  11699. {
  11700. // Get perspective projection
  11701. view[j].invert();
  11702. // Translate view to origin
  11703. LLVector4a origin_agent;
  11704. origin_agent.load3(origin.mV);
  11705. view[j].affineTransform(origin_agent, origin_agent);
  11706. eye = LLVector3(origin_agent.getF32ptr());
  11707. view[j] = look_proj(LLVector3(origin_agent.getF32ptr()),
  11708. light_dir, -up);
  11709. F32 fx = 1.f / tanf(fovx);
  11710. F32 fz = 1.f / tanf(fovz);
  11711. const F32 y1 = (yfar + ynear) / (ynear - yfar);
  11712. const F32 y3 = 2.f * yfar * ynear / (ynear - yfar);
  11713. proj[j].setRow<0>(LLVector4a(-fx, 0.f, 0.f, 0.f));
  11714. proj[j].setRow<1>(LLVector4a(0.f, y1, 0.f, -1.f));
  11715. proj[j].setRow<2>(LLVector4a(0.f, 0.f, -fz, 0.f));
  11716. proj[j].setRow<3>(LLVector4a(0.f, y3, 0.f, 0.f));
  11717. }
  11718. }
  11719. }
  11720. #if 0
  11721. shadow_cam.setFar(128.f);
  11722. #endif
  11723. if (llisnan(eye.mV[VX]) || llisnan(eye.mV[VY]) ||
  11724. llisnan(eye.mV[VZ]))
  11725. {
  11726. LL_DEBUGS("Pipeline") << "NaN found in eye origin. Corresponding shadow rendering aborted. Camera ID: "
  11727. << LLViewerCamera::sCurCameraID
  11728. << LL_ENDL;
  11729. continue;
  11730. }
  11731. shadow_cam.setOriginAndLookAt(eye, up, center);
  11732. shadow_cam.setOrigin(0.f, 0.f, 0.f);
  11733. gGLModelView = view[j];
  11734. gGLProjection = proj[j];
  11735. LLViewerCamera::updateFrustumPlanes(shadow_cam, false, false,
  11736. true);
  11737. // shadow_cam.ignoreAgentFrustumPlane(LLCamera::AGENT_PLANE_NEAR);
  11738. shadow_cam.agentPlane(LLCamera::AGENT_PLANE_NEAR).set(shadow_near_clip);
  11739. gGLModelView = view[j];
  11740. gGLProjection = proj[j];
  11741. gGLLastModelView = mShadowModelview[j];
  11742. gGLLastProjection = mShadowProjection[j];
  11743. mShadowModelview[j] = view[j];
  11744. mShadowProjection[j] = proj[j];
  11745. mSunShadowMatrix[j].setMul(TRANS_MAT, proj[j]);
  11746. mSunShadowMatrix[j].mulAffine(view[j]);
  11747. mSunShadowMatrix[j].mulAffine(inv_view);
  11748. mRT->mSunShadow[j].bindTarget();
  11749. mRT->mSunShadow[j].getViewport(gGLViewport);
  11750. mRT->mSunShadow[j].clear();
  11751. static LLCullResult result[4];
  11752. renderShadow(view[j], proj[j], shadow_cam, result[j], true);
  11753. mRT->mSunShadow[j].flush();
  11754. if (no_shadow_frustra)
  11755. {
  11756. mShadowCamera[j + 4] = shadow_cam;
  11757. }
  11758. }
  11759. }
  11760. // HACK to disable projector shadows
  11761. bool gen_shadow = RenderShadowDetail > 1;
  11762. // Note: skip updating spot shadow maps during cubemap updates
  11763. if (gen_shadow && !gCubeSnapshot)
  11764. {
  11765. F32 fade_amt =
  11766. gFrameIntervalSeconds *
  11767. llmax(LLViewerCamera::getVelocityStat().getCurrentPerSec(), 1.f);
  11768. // Update shadow targets
  11769. for (U32 i = 0; i < 2; ++i)
  11770. {
  11771. // For each current shadow
  11772. LLViewerCamera::sCurCameraID =
  11773. LLViewerCamera::CAMERA_SPOT_SHADOW0 + i;
  11774. if (mShadowSpotLight[i].notNull() &&
  11775. (mShadowSpotLight[i] == mTargetShadowSpotLight[0] ||
  11776. mShadowSpotLight[i] == mTargetShadowSpotLight[1]))
  11777. {
  11778. // Keep this spotlight
  11779. mSpotLightFade[i] = llmin(mSpotLightFade[i] + fade_amt, 1.f);
  11780. }
  11781. else
  11782. {
  11783. // Fade out this light
  11784. mSpotLightFade[i] = llmax(mSpotLightFade[i] - fade_amt, 0.f);
  11785. if (mSpotLightFade[i] == 0.f || mShadowSpotLight[i].isNull())
  11786. {
  11787. // Faded out, grab one of the pending spots (whichever one
  11788. // is not already taken)
  11789. if (mTargetShadowSpotLight[0] != mShadowSpotLight[(i + 1) % 2])
  11790. {
  11791. mShadowSpotLight[i] = mTargetShadowSpotLight[0];
  11792. }
  11793. else
  11794. {
  11795. mShadowSpotLight[i] = mTargetShadowSpotLight[1];
  11796. }
  11797. }
  11798. }
  11799. }
  11800. }
  11801. if (gen_shadow)
  11802. {
  11803. for (S32 i = 0; i < 2; ++i)
  11804. {
  11805. gGLModelView = saved_view;
  11806. gGLProjection = saved_proj;
  11807. if (mShadowSpotLight[i].isNull())
  11808. {
  11809. continue;
  11810. }
  11811. LLVOVolume* volp = mShadowSpotLight[i]->getVOVolume();
  11812. if (!volp)
  11813. {
  11814. mShadowSpotLight[i] = NULL;
  11815. continue;
  11816. }
  11817. LLDrawable* drawablep = mShadowSpotLight[i];
  11818. LLVector3 params = volp->getSpotLightParams();
  11819. F32 fov = params.mV[0];
  11820. // Get agent->light space matrix (modelview)
  11821. LLVector3 center = drawablep->getPositionAgent();
  11822. LLQuaternion quat = volp->getRenderRotation();
  11823. // Get near clip plane
  11824. LLVector3 scale = volp->getScale();
  11825. LLVector3 at_axis(0.f, 0.f, -scale.mV[2] * 0.5f);
  11826. at_axis *= quat;
  11827. LLVector3 np = center + at_axis;
  11828. at_axis.normalize();
  11829. // Get origin that has given fov for plane np, at_axis, and given
  11830. // scale
  11831. F32 divisor = tanf(fov * 0.5f);
  11832. // Seen happening and causing NaNs in setOrigin() below. HB
  11833. if (divisor == 0.f) continue;
  11834. F32 dist = (scale.mV[1] * 0.5f) / divisor;
  11835. LLVector3 origin = np - at_axis * dist;
  11836. LLMatrix4 mat(quat, LLVector4(origin, 1.f));
  11837. view[i + 4].loadu(mat.getF32ptr());
  11838. view[i + 4].invert();
  11839. // Get perspective matrix
  11840. F32 near_clip = dist + 0.01f;
  11841. F32 width = scale.mV[VX];
  11842. F32 height = scale.mV[VY];
  11843. F32 far_clip = dist + volp->getLightRadius() * 1.5f;
  11844. F32 fovy = fov * RAD_TO_DEG;
  11845. F32 aspect = width / height;
  11846. proj[i + 4] = gl_perspective(fovy, aspect, near_clip, far_clip);
  11847. // Translate and scale from [-1, 1] to [0, 1]
  11848. gGLModelView = view[i + 4];
  11849. gGLProjection = proj[i + 4];
  11850. mSunShadowMatrix[i + 4].setMul(TRANS_MAT, proj[i + 4]);
  11851. mSunShadowMatrix[i + 4].mulAffine(view[i + 4]);
  11852. mSunShadowMatrix[i + 4].mulAffine(inv_view);
  11853. gGLLastModelView = mShadowModelview[i + 4];
  11854. gGLLastProjection = mShadowProjection[i + 4];
  11855. mShadowModelview[i + 4] = view[i + 4];
  11856. mShadowProjection[i + 4] = proj[i + 4];
  11857. // Skip updating spot shadow maps during cubemap updates
  11858. if (!gCubeSnapshot)
  11859. {
  11860. LLCamera shadow_cam = gViewerCamera;
  11861. shadow_cam.setFar(far_clip);
  11862. shadow_cam.setOrigin(origin);
  11863. LLViewerCamera::updateFrustumPlanes(shadow_cam, false, false,
  11864. true);
  11865. mSpotShadow[i].bindTarget();
  11866. mSpotShadow[i].getViewport(gGLViewport);
  11867. mSpotShadow[i].clear();
  11868. LLViewerCamera::sCurCameraID =
  11869. LLViewerCamera::CAMERA_SPOT_SHADOW0 + i;
  11870. sRenderSpotLight = drawablep;
  11871. static LLCullResult result[2];
  11872. renderShadow(view[i + 4], proj[i + 4], shadow_cam, result[i],
  11873. false);
  11874. sRenderSpotLight = NULL;
  11875. mSpotShadow[i].flush();
  11876. }
  11877. }
  11878. }
  11879. else
  11880. {
  11881. // No spotlight shadows
  11882. mShadowSpotLight[0] = mShadowSpotLight[1] = NULL;
  11883. }
  11884. if (!CameraOffset)
  11885. {
  11886. gGLModelView = saved_view;
  11887. gGLProjection = saved_proj;
  11888. }
  11889. else
  11890. {
  11891. gGLModelView = view[1];
  11892. gGLProjection = proj[1];
  11893. gGL.loadMatrix(view[1]);
  11894. gGL.matrixMode(LLRender::MM_PROJECTION);
  11895. gGL.loadMatrix(proj[1]);
  11896. gGL.matrixMode(LLRender::MM_MODELVIEW);
  11897. }
  11898. gGL.setColorMask(true, true);
  11899. gGLLastModelView = last_modelview;
  11900. gGLLastProjection = last_projection;
  11901. popRenderTypeMask();
  11902. if (!skip_avatar_update)
  11903. {
  11904. gAgentAvatarp->updateAttachmentVisibility(gAgent.getCameraMode());
  11905. }
  11906. }
  11907. void LLPipeline::generateSunShadow()
  11908. {
  11909. if (!sRenderDeferred || !RenderShadowDetail)
  11910. {
  11911. return;
  11912. }
  11913. LL_FAST_TIMER(FTM_GEN_SUN_SHADOW);
  11914. if (gUsePBRShaders)
  11915. {
  11916. generateSunShadowPBR();
  11917. return;
  11918. }
  11919. bool skip_avatar_update = false;
  11920. if (!isAgentAvatarValid() || gAgent.getCameraAnimating() ||
  11921. gAgent.getCameraMode() != CAMERA_MODE_MOUSELOOK ||
  11922. !LLVOAvatar::sVisibleInFirstPerson)
  11923. {
  11924. skip_avatar_update = true;
  11925. }
  11926. if (!skip_avatar_update)
  11927. {
  11928. gAgentAvatarp->updateAttachmentVisibility(CAMERA_MODE_THIRD_PERSON);
  11929. }
  11930. // Store last_modelview of world camera
  11931. LLMatrix4a last_modelview = gGLLastModelView;
  11932. LLMatrix4a last_projection = gGLLastProjection;
  11933. pushRenderTypeMask();
  11934. andRenderTypeMask(RENDER_TYPE_SIMPLE,
  11935. RENDER_TYPE_ALPHA,
  11936. RENDER_TYPE_GRASS,
  11937. RENDER_TYPE_FULLBRIGHT,
  11938. RENDER_TYPE_BUMP,
  11939. RENDER_TYPE_VOLUME,
  11940. RENDER_TYPE_AVATAR,
  11941. RENDER_TYPE_PUPPET,
  11942. RENDER_TYPE_TREE,
  11943. RENDER_TYPE_TERRAIN,
  11944. RENDER_TYPE_WATER,
  11945. RENDER_TYPE_VOIDWATER,
  11946. RENDER_TYPE_PASS_ALPHA,
  11947. RENDER_TYPE_PASS_ALPHA_MASK,
  11948. RENDER_TYPE_PASS_FULLBRIGHT_ALPHA_MASK,
  11949. RENDER_TYPE_PASS_GRASS,
  11950. RENDER_TYPE_PASS_SIMPLE,
  11951. RENDER_TYPE_PASS_BUMP,
  11952. RENDER_TYPE_PASS_FULLBRIGHT,
  11953. RENDER_TYPE_PASS_SHINY,
  11954. RENDER_TYPE_PASS_FULLBRIGHT_SHINY,
  11955. RENDER_TYPE_PASS_MATERIAL,
  11956. RENDER_TYPE_PASS_MATERIAL_ALPHA,
  11957. RENDER_TYPE_PASS_MATERIAL_ALPHA_MASK,
  11958. RENDER_TYPE_PASS_MATERIAL_ALPHA_EMISSIVE,
  11959. RENDER_TYPE_PASS_SPECMAP,
  11960. RENDER_TYPE_PASS_SPECMAP_BLEND,
  11961. RENDER_TYPE_PASS_SPECMAP_MASK,
  11962. RENDER_TYPE_PASS_SPECMAP_EMISSIVE,
  11963. RENDER_TYPE_PASS_NORMMAP,
  11964. RENDER_TYPE_PASS_NORMMAP_BLEND,
  11965. RENDER_TYPE_PASS_NORMMAP_MASK,
  11966. RENDER_TYPE_PASS_NORMMAP_EMISSIVE,
  11967. RENDER_TYPE_PASS_NORMSPEC,
  11968. RENDER_TYPE_PASS_NORMSPEC_BLEND,
  11969. RENDER_TYPE_PASS_NORMSPEC_MASK,
  11970. RENDER_TYPE_PASS_NORMSPEC_EMISSIVE,
  11971. RENDER_TYPE_PASS_ALPHA_MASK_RIGGED,
  11972. RENDER_TYPE_PASS_FULLBRIGHT_ALPHA_MASK_RIGGED,
  11973. RENDER_TYPE_PASS_SIMPLE_RIGGED,
  11974. RENDER_TYPE_PASS_BUMP_RIGGED,
  11975. RENDER_TYPE_PASS_FULLBRIGHT_RIGGED,
  11976. RENDER_TYPE_PASS_SHINY_RIGGED,
  11977. RENDER_TYPE_PASS_FULLBRIGHT_SHINY_RIGGED,
  11978. RENDER_TYPE_PASS_MATERIAL_RIGGED,
  11979. RENDER_TYPE_PASS_MATERIAL_ALPHA_RIGGED,
  11980. RENDER_TYPE_PASS_MATERIAL_ALPHA_MASK_RIGGED,
  11981. RENDER_TYPE_PASS_MATERIAL_ALPHA_EMISSIVE_RIGGED,
  11982. RENDER_TYPE_PASS_SPECMAP_RIGGED,
  11983. RENDER_TYPE_PASS_SPECMAP_BLEND_RIGGED,
  11984. RENDER_TYPE_PASS_SPECMAP_MASK_RIGGED,
  11985. RENDER_TYPE_PASS_SPECMAP_EMISSIVE_RIGGED,
  11986. RENDER_TYPE_PASS_NORMMAP_RIGGED,
  11987. RENDER_TYPE_PASS_NORMMAP_BLEND_RIGGED,
  11988. RENDER_TYPE_PASS_NORMMAP_MASK_RIGGED,
  11989. RENDER_TYPE_PASS_NORMMAP_EMISSIVE_RIGGED,
  11990. RENDER_TYPE_PASS_NORMSPEC_RIGGED,
  11991. RENDER_TYPE_PASS_NORMSPEC_BLEND_RIGGED,
  11992. RENDER_TYPE_PASS_NORMSPEC_MASK_RIGGED,
  11993. RENDER_TYPE_PASS_NORMSPEC_EMISSIVE_RIGGED,
  11994. END_RENDER_TYPES);
  11995. gGL.setColorMask(false, false);
  11996. // Get sun view matrix
  11997. // Store current projection/modelview matrix
  11998. const LLMatrix4a saved_proj = gGLProjection;
  11999. const LLMatrix4a saved_view = gGLModelView;
  12000. LLMatrix4a inv_view(saved_view);
  12001. inv_view.invert();
  12002. LLMatrix4a view[6];
  12003. LLMatrix4a proj[6];
  12004. // Clip contains parallel split distances for 3 splits
  12005. LLVector3 clip = RenderShadowClipPlanes;
  12006. LLVector3 caster_dir(mIsSunUp ? mSunDir : mMoonDir);
  12007. // Far clip on last split is minimum of camera view distance and 128
  12008. mSunClipPlanes = LLVector4(clip, clip.mV[2] * clip.mV[2] / clip.mV[1]);
  12009. // Put together a universal "near clip" plane for shadow frusta
  12010. LLPlane shadow_near_clip;
  12011. {
  12012. LLVector3 p = gAgent.getPositionAgent();
  12013. p += caster_dir * RenderFarClip * 2.f;
  12014. shadow_near_clip.setVec(p, caster_dir);
  12015. }
  12016. LLVector3 light_dir = -caster_dir;
  12017. light_dir.normalize();
  12018. // Create light space camera matrix
  12019. LLVector3 at = light_dir;
  12020. LLVector3 up = gViewerCamera.getAtAxis();
  12021. if (fabsf(up * light_dir) > 0.75f)
  12022. {
  12023. up = gViewerCamera.getUpAxis();
  12024. }
  12025. up.normalize();
  12026. at.normalize();
  12027. LLCamera main_camera = gViewerCamera;
  12028. bool no_shadow_frustra = !hasRenderDebugMask(RENDER_DEBUG_SHADOW_FRUSTA);
  12029. F32 near_clip = 0.f;
  12030. {
  12031. // Get visible point cloud
  12032. main_camera.calcAgentFrustumPlanes(main_camera.mAgentFrustum);
  12033. LLVector3 min, max;
  12034. std::vector<LLVector3> fp;
  12035. getVisiblePointCloud(main_camera, min, max, fp);
  12036. if (fp.empty())
  12037. {
  12038. if (no_shadow_frustra)
  12039. {
  12040. mShadowCamera[0] = main_camera;
  12041. mShadowExtents[0][0] = min;
  12042. mShadowExtents[0][1] = max;
  12043. mShadowFrustPoints[0].clear();
  12044. mShadowFrustPoints[1].clear();
  12045. mShadowFrustPoints[2].clear();
  12046. mShadowFrustPoints[3].clear();
  12047. }
  12048. popRenderTypeMask();
  12049. if (!skip_avatar_update)
  12050. {
  12051. gAgentAvatarp->updateAttachmentVisibility(gAgent.getCameraMode());
  12052. }
  12053. return;
  12054. }
  12055. LLVector4a v;
  12056. // Get good split distances for frustum
  12057. for (U32 i = 0; i < fp.size(); ++i)
  12058. {
  12059. v.load3(fp[i].mV);
  12060. saved_view.affineTransform(v, v);
  12061. fp[i].set(v.getF32ptr());
  12062. }
  12063. min = fp[0];
  12064. max = fp[0];
  12065. // Get camera space bounding box
  12066. for (U32 i = 1; i < fp.size(); ++i)
  12067. {
  12068. update_min_max(min, max, fp[i]);
  12069. }
  12070. near_clip = llclamp(-max.mV[2], 0.01f, 4.0f);
  12071. F32 far_clip = llclamp(-min.mV[2] * 2.f, 16.0f, 512.0f);
  12072. far_clip = llmin(far_clip, gViewerCamera.getFar());
  12073. F32 range = far_clip - near_clip;
  12074. LLVector3 split_exp = RenderShadowSplitExponent;
  12075. F32 da = 1.f - llmax(fabsf(light_dir * up),
  12076. fabsf(light_dir * gViewerCamera.getLeftAxis()));
  12077. da = powf(da, split_exp.mV[2]);
  12078. F32 sxp = split_exp.mV[1] + (split_exp.mV[0] - split_exp.mV[1]) * da;
  12079. for (U32 i = 0; i < 4; ++i)
  12080. {
  12081. F32 x = (F32)(i + 1) * 0.25f;
  12082. x = powf(x, sxp);
  12083. mSunClipPlanes.mV[i] = near_clip + range * x;
  12084. }
  12085. // Bump back first split for transition padding
  12086. mSunClipPlanes.mV[0] *= 1.25f;
  12087. }
  12088. // Convenience array of 4 near clip plane distances
  12089. F32 dist[] = { near_clip,
  12090. mSunClipPlanes.mV[0],
  12091. mSunClipPlanes.mV[1],
  12092. mSunClipPlanes.mV[2],
  12093. mSunClipPlanes.mV[3] };
  12094. if (mSunLightColor == LLColor4::black)
  12095. {
  12096. // Sun diffuse is totally black, shadows do not matter
  12097. LLGLDepthTest depth(GL_TRUE);
  12098. for (S32 j = 0; j < 4; ++j)
  12099. {
  12100. mShadow[j].bindTarget();
  12101. mShadow[j].clear();
  12102. mShadow[j].flush();
  12103. }
  12104. }
  12105. else
  12106. {
  12107. for (S32 j = 0; j < 4; ++j)
  12108. {
  12109. if (no_shadow_frustra)
  12110. {
  12111. mShadowFrustPoints[j].clear();
  12112. }
  12113. LLViewerCamera::sCurCameraID =
  12114. LLViewerCamera::CAMERA_SUN_SHADOW0 + j;
  12115. // Restore render matrices
  12116. gGLModelView = saved_view;
  12117. gGLProjection = saved_proj;
  12118. LLVector3 eye = gViewerCamera.getOrigin();
  12119. // Camera used for shadow cull/render
  12120. LLCamera shadow_cam = gViewerCamera;
  12121. shadow_cam.setFar(16.f);
  12122. // Create world space camera frustum for this split
  12123. LLViewerCamera::updateFrustumPlanes(shadow_cam, false, false,
  12124. true);
  12125. LLVector3* frust = shadow_cam.mAgentFrustum;
  12126. LLVector3 pn = shadow_cam.getAtAxis();
  12127. // Construct 8 corners of split frustum section
  12128. for (U32 i = 0; i < 4; ++i)
  12129. {
  12130. LLVector3 delta = frust[i + 4] - eye;
  12131. delta += (frust[i + 4] - frust[(i + 2) % 4 + 4]) * 0.05f;
  12132. delta.normalize();
  12133. F32 dp = delta * pn;
  12134. frust[i] = eye + (delta * dist[j] * 0.75f) / dp;
  12135. frust[i + 4] = eye + (delta * dist[j + 1] * 1.25f) / dp;
  12136. }
  12137. shadow_cam.calcAgentFrustumPlanes(frust);
  12138. shadow_cam.mFrustumCornerDist = 0.f;
  12139. if (no_shadow_frustra)
  12140. {
  12141. mShadowCamera[j] = shadow_cam;
  12142. }
  12143. std::vector<LLVector3> fp;
  12144. LLVector3 min, max;
  12145. if (!getVisiblePointCloud(shadow_cam, min, max, fp, light_dir))
  12146. {
  12147. // No possible shadow receivers
  12148. if (no_shadow_frustra)
  12149. {
  12150. mShadowExtents[j][0].clear();
  12151. mShadowExtents[j][1].clear();
  12152. mShadowCamera[j + 4] = shadow_cam;
  12153. }
  12154. mShadow[j].bindTarget();
  12155. {
  12156. LLGLDepthTest depth(GL_TRUE);
  12157. mShadow[j].clear();
  12158. }
  12159. mShadow[j].flush();
  12160. continue;
  12161. }
  12162. if (no_shadow_frustra)
  12163. {
  12164. mShadowExtents[j][0] = min;
  12165. mShadowExtents[j][1] = max;
  12166. mShadowFrustPoints[j] = fp;
  12167. }
  12168. // Find a good origin for shadow projection
  12169. LLVector3 origin;
  12170. // Get a temporary view projection
  12171. view[j] = look_proj(gViewerCamera.getOrigin(), light_dir, -up);
  12172. std::vector<LLVector3> wpf;
  12173. LLVector4a p;
  12174. for (U32 i = 0; i < fp.size(); ++i)
  12175. {
  12176. p.load3(fp[i].mV);
  12177. view[j].affineTransform(p, p);
  12178. wpf.emplace_back(LLVector3(p.getF32ptr()));
  12179. }
  12180. min = max = wpf[0];
  12181. for (U32 i = 0; i < fp.size(); ++i)
  12182. {
  12183. // Get AABB in camera space
  12184. update_min_max(min, max, wpf[i]);
  12185. }
  12186. // Construct a perspective transform with perspective along y-axis
  12187. // that contains points in wpf
  12188. // Known:
  12189. // - far clip plane
  12190. // - near clip plane
  12191. // - points in frustum
  12192. // Find:
  12193. // - origin
  12194. // Get some "interesting" points of reference
  12195. LLVector3 center = (min + max) * 0.5f;
  12196. LLVector3 size = (max - min) * 0.5f;
  12197. LLVector3 near_center = center;
  12198. near_center.mV[1] += size.mV[1] * 2.f;
  12199. // Put all points in wpf in quadrant 0, reletive to center of
  12200. // min/max get the best fit line using least squares
  12201. for (U32 i = 0; i < wpf.size(); ++i)
  12202. {
  12203. wpf[i] -= center;
  12204. wpf[i].mV[0] = fabsf(wpf[i].mV[0]);
  12205. wpf[i].mV[2] = fabsf(wpf[i].mV[2]);
  12206. }
  12207. F32 bfm = 0.f;
  12208. F32 bfb = 0.f;
  12209. if (!wpf.empty())
  12210. {
  12211. F32 sx = 0.f;
  12212. F32 sx2 = 0.f;
  12213. F32 sy = 0.f;
  12214. F32 sxy = 0.f;
  12215. for (U32 i = 0; i < wpf.size(); ++i)
  12216. {
  12217. sx += wpf[i].mV[0];
  12218. sx2 += wpf[i].mV[0] * wpf[i].mV[0];
  12219. sy += wpf[i].mV[1];
  12220. sxy += wpf[i].mV[0] * wpf[i].mV[1];
  12221. }
  12222. bfm = (sy * sx - wpf.size() * sxy) /
  12223. (sx * sx - wpf.size() * sx2);
  12224. bfb = (sx * sxy - sy * sx2) / (sx * sx - bfm * sx2);
  12225. }
  12226. if (llisnan(bfm) || llisnan(bfb))
  12227. {
  12228. LL_DEBUGS("Pipeline") << "NaN found. Corresponding shadow rendering aborted. Camera ID: "
  12229. << LLViewerCamera::sCurCameraID
  12230. << LL_ENDL;
  12231. continue;
  12232. }
  12233. {
  12234. // Best fit line is y = bfm * x + bfb
  12235. // Find point that is furthest to the right of line
  12236. F32 off_x = -1.f;
  12237. LLVector3 lp;
  12238. for (U32 i = 0; i < wpf.size(); ++i)
  12239. {
  12240. // y = bfm * x + bfb
  12241. // x = (y - bfb) / bfm
  12242. F32 lx = (wpf[i].mV[1] - bfb) / bfm;
  12243. lx = wpf[i].mV[0] - lx;
  12244. if (off_x < lx)
  12245. {
  12246. off_x = lx;
  12247. lp = wpf[i];
  12248. }
  12249. }
  12250. // Get line with slope bfm through lp
  12251. // bfb = y - bfm * x
  12252. bfb = lp.mV[1] - bfm * lp.mV[0];
  12253. // Calculate error
  12254. F32 shadow_error = 0.f;
  12255. for (U32 i = 0; i < wpf.size(); ++i)
  12256. {
  12257. F32 lx = (wpf[i].mV[1] - bfb) / bfm;
  12258. shadow_error += fabsf(wpf[i].mV[0] - lx);
  12259. }
  12260. shadow_error /= wpf.size() * size.mV[0];
  12261. if (llisnan(shadow_error) ||
  12262. shadow_error > RenderShadowErrorCutoff)
  12263. {
  12264. // Just use ortho projection
  12265. origin.clear();
  12266. proj[j] = gl_ortho(min.mV[0], max.mV[0], min.mV[1],
  12267. max.mV[1], -max.mV[2], -min.mV[2]);
  12268. }
  12269. else
  12270. {
  12271. // Origin is where line x = 0;
  12272. origin.set(0, bfb, 0);
  12273. F32 fovz = 1.f;
  12274. F32 fovx = 1.f;
  12275. LLVector3 zp;
  12276. LLVector3 xp;
  12277. for (U32 i = 0; i < wpf.size(); ++i)
  12278. {
  12279. LLVector3 atz = wpf[i] - origin;
  12280. atz.mV[0] = 0.f;
  12281. atz.normalize();
  12282. if (fovz > -atz.mV[1])
  12283. {
  12284. zp = wpf[i];
  12285. fovz = -atz.mV[1];
  12286. }
  12287. LLVector3 atx = wpf[i] - origin;
  12288. atx.mV[2] = 0.f;
  12289. atx.normalize();
  12290. if (fovx > -atx.mV[1])
  12291. {
  12292. fovx = -atx.mV[1];
  12293. xp = wpf[i];
  12294. }
  12295. }
  12296. fovx = acosf(fovx);
  12297. fovz = acosf(fovz);
  12298. F32 cutoff = RenderShadowFOVCutoff;
  12299. if (fovx < cutoff && fovz > cutoff)
  12300. {
  12301. // x is a good fit, but z is too big, move away from zp
  12302. // enough so that fovz matches cutoff
  12303. F32 d = zp.mV[2] / tanf(cutoff);
  12304. F32 ny = zp.mV[1] + fabsf(d);
  12305. origin.mV[1] = ny;
  12306. fovz = fovx = 1.f;
  12307. for (U32 i = 0; i < wpf.size(); ++i)
  12308. {
  12309. LLVector3 atz = wpf[i] - origin;
  12310. atz.mV[0] = 0.f;
  12311. atz.normalize();
  12312. fovz = llmin(fovz, -atz.mV[1]);
  12313. LLVector3 atx = wpf[i] - origin;
  12314. atx.mV[2] = 0.f;
  12315. atx.normalize();
  12316. fovx = llmin(fovx, -atx.mV[1]);
  12317. }
  12318. fovx = acosf(fovx);
  12319. fovz = acosf(fovz);
  12320. }
  12321. origin += center;
  12322. F32 ynear = origin.mV[1] - max.mV[1];
  12323. F32 yfar = origin.mV[1] - min.mV[1];
  12324. if (ynear < 0.1f) // keep a sensible near clip plane
  12325. {
  12326. F32 diff = 0.1f - ynear;
  12327. origin.mV[1] += diff;
  12328. ynear += diff;
  12329. yfar += diff;
  12330. }
  12331. if (fovx > cutoff)
  12332. {
  12333. // Just use ortho projection
  12334. origin.clear();
  12335. proj[j] = gl_ortho(min.mV[0], max.mV[0], min.mV[1],
  12336. max.mV[1], -max.mV[2], -min.mV[2]);
  12337. }
  12338. else
  12339. {
  12340. // Get perspective projection
  12341. view[j].invert();
  12342. // Translate view to origin
  12343. LLVector4a origin_agent;
  12344. origin_agent.load3(origin.mV);
  12345. view[j].affineTransform(origin_agent, origin_agent);
  12346. eye = LLVector3(origin_agent.getF32ptr());
  12347. view[j] = look_proj(LLVector3(origin_agent.getF32ptr()),
  12348. light_dir, -up);
  12349. F32 fx = 1.f / tanf(fovx);
  12350. F32 fz = 1.f / tanf(fovz);
  12351. const F32 y1 = (yfar + ynear) / (ynear - yfar);
  12352. const F32 y3 = 2.f * yfar * ynear / (ynear - yfar);
  12353. proj[j].setRow<0>(LLVector4a(-fx, 0.f, 0.f, 0.f));
  12354. proj[j].setRow<1>(LLVector4a(0.f, y1, 0.f, -1.f));
  12355. proj[j].setRow<2>(LLVector4a(0.f, 0.f, -fz, 0.f));
  12356. proj[j].setRow<3>(LLVector4a(0.f, y3, 0.f, 0.f));
  12357. }
  12358. }
  12359. }
  12360. #if 0
  12361. shadow_cam.setFar(128.f);
  12362. #endif
  12363. if (llisnan(eye.mV[VX]) || llisnan(eye.mV[VY]) ||
  12364. llisnan(eye.mV[VZ]))
  12365. {
  12366. LL_DEBUGS("Pipeline") << "NaN found in eye origin. Corresponding shadow rendering aborted. Camera ID: "
  12367. << LLViewerCamera::sCurCameraID
  12368. << LL_ENDL;
  12369. continue;
  12370. }
  12371. shadow_cam.setOriginAndLookAt(eye, up, center);
  12372. shadow_cam.setOrigin(0.f, 0.f, 0.f);
  12373. gGLModelView = view[j];
  12374. gGLProjection = proj[j];
  12375. LLViewerCamera::updateFrustumPlanes(shadow_cam, false, false,
  12376. true);
  12377. // shadow_cam.ignoreAgentFrustumPlane(LLCamera::AGENT_PLANE_NEAR);
  12378. shadow_cam.agentPlane(LLCamera::AGENT_PLANE_NEAR).set(shadow_near_clip);
  12379. gGLModelView = view[j];
  12380. gGLProjection = proj[j];
  12381. gGLLastModelView = mShadowModelview[j];
  12382. gGLLastProjection = mShadowProjection[j];
  12383. mShadowModelview[j] = view[j];
  12384. mShadowProjection[j] = proj[j];
  12385. mSunShadowMatrix[j].setMul(TRANS_MAT, proj[j]);
  12386. mSunShadowMatrix[j].mulAffine(view[j]);
  12387. mSunShadowMatrix[j].mulAffine(inv_view);
  12388. mShadow[j].bindTarget();
  12389. mShadow[j].getViewport(gGLViewport);
  12390. mShadow[j].clear();
  12391. static LLCullResult result[4];
  12392. renderShadow(view[j], proj[j], shadow_cam, result[j], true, false,
  12393. mShadow[j].getWidth());
  12394. mShadow[j].flush();
  12395. if (no_shadow_frustra)
  12396. {
  12397. mShadowCamera[j + 4] = shadow_cam;
  12398. }
  12399. }
  12400. }
  12401. // HACK to disable projector shadows
  12402. bool gen_shadow = RenderShadowDetail > 1;
  12403. if (gen_shadow)
  12404. {
  12405. F32 fade_amt =
  12406. gFrameIntervalSeconds *
  12407. llmax(LLViewerCamera::getVelocityStat().getCurrentPerSec(), 1.f);
  12408. // Update shadow targets
  12409. for (U32 i = 0; i < 2; ++i)
  12410. {
  12411. // For each current shadow
  12412. LLViewerCamera::sCurCameraID =
  12413. LLViewerCamera::CAMERA_SPOT_SHADOW0 + i;
  12414. if (mShadowSpotLight[i].notNull() &&
  12415. (mShadowSpotLight[i] == mTargetShadowSpotLight[0] ||
  12416. mShadowSpotLight[i] == mTargetShadowSpotLight[1]))
  12417. {
  12418. // Keep this spotlight
  12419. mSpotLightFade[i] = llmin(mSpotLightFade[i] + fade_amt, 1.f);
  12420. }
  12421. else
  12422. {
  12423. // Fade out this light
  12424. mSpotLightFade[i] = llmax(mSpotLightFade[i] - fade_amt, 0.f);
  12425. if (mSpotLightFade[i] == 0.f || mShadowSpotLight[i].isNull())
  12426. {
  12427. // Faded out, grab one of the pending spots (whichever one
  12428. // is not already taken)
  12429. if (mTargetShadowSpotLight[0] != mShadowSpotLight[(i + 1) % 2])
  12430. {
  12431. mShadowSpotLight[i] = mTargetShadowSpotLight[0];
  12432. }
  12433. else
  12434. {
  12435. mShadowSpotLight[i] = mTargetShadowSpotLight[1];
  12436. }
  12437. }
  12438. }
  12439. }
  12440. for (S32 i = 0; i < 2; ++i)
  12441. {
  12442. gGLModelView = saved_view;
  12443. gGLProjection = saved_proj;
  12444. if (mShadowSpotLight[i].isNull())
  12445. {
  12446. continue;
  12447. }
  12448. LLVOVolume* volp = mShadowSpotLight[i]->getVOVolume();
  12449. if (!volp)
  12450. {
  12451. mShadowSpotLight[i] = NULL;
  12452. continue;
  12453. }
  12454. LLDrawable* drawablep = mShadowSpotLight[i];
  12455. LLVector3 params = volp->getSpotLightParams();
  12456. F32 fov = params.mV[0];
  12457. // Get agent->light space matrix (modelview)
  12458. LLVector3 center = drawablep->getPositionAgent();
  12459. LLQuaternion quat = volp->getRenderRotation();
  12460. // Get near clip plane
  12461. LLVector3 scale = volp->getScale();
  12462. LLVector3 at_axis(0.f, 0.f, -scale.mV[2] * 0.5f);
  12463. at_axis *= quat;
  12464. LLVector3 np = center + at_axis;
  12465. at_axis.normalize();
  12466. // Get origin that has given fov for plane np, at_axis, and given
  12467. // scale
  12468. F32 divisor = tanf(fov * 0.5f);
  12469. // Seen happening and causing NaNs in setOrigin() below. HB
  12470. if (divisor == 0.f) continue;
  12471. F32 dist = (scale.mV[1] * 0.5f) / divisor;
  12472. LLVector3 origin = np - at_axis * dist;
  12473. LLMatrix4 mat(quat, LLVector4(origin, 1.f));
  12474. view[i + 4].loadu(mat.getF32ptr());
  12475. view[i + 4].invert();
  12476. // Get perspective matrix
  12477. F32 near_clip = dist + 0.01f;
  12478. F32 width = scale.mV[VX];
  12479. F32 height = scale.mV[VY];
  12480. F32 far_clip = dist + volp->getLightRadius() * 1.5f;
  12481. F32 fovy = fov * RAD_TO_DEG;
  12482. F32 aspect = width / height;
  12483. proj[i + 4] = gl_perspective(fovy, aspect, near_clip, far_clip);
  12484. // Translate and scale from [-1, 1] to [0, 1]
  12485. gGLModelView = view[i + 4];
  12486. gGLProjection = proj[i + 4];
  12487. mSunShadowMatrix[i + 4].setMul(TRANS_MAT, proj[i + 4]);
  12488. mSunShadowMatrix[i + 4].mulAffine(view[i + 4]);
  12489. mSunShadowMatrix[i + 4].mulAffine(inv_view);
  12490. gGLLastModelView = mShadowModelview[i + 4];
  12491. gGLLastProjection = mShadowProjection[i + 4];
  12492. mShadowModelview[i + 4] = view[i + 4];
  12493. mShadowProjection[i + 4] = proj[i + 4];
  12494. LLCamera shadow_cam = gViewerCamera;
  12495. shadow_cam.setFar(far_clip);
  12496. shadow_cam.setOrigin(origin);
  12497. LLViewerCamera::updateFrustumPlanes(shadow_cam, false, false,
  12498. true);
  12499. mShadow[i + 4].bindTarget();
  12500. mShadow[i + 4].getViewport(gGLViewport);
  12501. mShadow[i + 4].clear();
  12502. LLViewerCamera::sCurCameraID =
  12503. LLViewerCamera::CAMERA_SPOT_SHADOW0 + i;
  12504. sRenderSpotLight = drawablep;
  12505. static LLCullResult result[2];
  12506. renderShadow(view[i + 4], proj[i + 4], shadow_cam, result[i],
  12507. false, false, mShadow[i + 4].getWidth());
  12508. sRenderSpotLight = NULL;
  12509. mShadow[i + 4].flush();
  12510. }
  12511. }
  12512. else
  12513. {
  12514. // No spotlight shadows
  12515. mShadowSpotLight[0] = mShadowSpotLight[1] = NULL;
  12516. }
  12517. if (!CameraOffset)
  12518. {
  12519. gGLModelView = saved_view;
  12520. gGLProjection = saved_proj;
  12521. }
  12522. else
  12523. {
  12524. gGLModelView = view[1];
  12525. gGLProjection = proj[1];
  12526. gGL.loadMatrix(view[1]);
  12527. gGL.matrixMode(LLRender::MM_PROJECTION);
  12528. gGL.loadMatrix(proj[1]);
  12529. gGL.matrixMode(LLRender::MM_MODELVIEW);
  12530. }
  12531. gGL.setColorMask(true, false);
  12532. gGLLastModelView = last_modelview;
  12533. gGLLastProjection = last_projection;
  12534. popRenderTypeMask();
  12535. if (!skip_avatar_update)
  12536. {
  12537. gAgentAvatarp->updateAttachmentVisibility(gAgent.getCameraMode());
  12538. }
  12539. stop_glerror();
  12540. }
  12541. void LLPipeline::renderGroups(LLRenderPass* passp, U32 type, U32 mask,
  12542. bool texture)
  12543. {
  12544. LLCullResult::sg_list_t& visible_groups = sCull->getVisibleGroups();
  12545. for (U32 i = 0, count = visible_groups.size(); i < count; ++i)
  12546. {
  12547. LLSpatialGroup* groupp = visible_groups[i];
  12548. if (groupp && !groupp->isDead() &&
  12549. (!sUseOcclusion ||
  12550. !groupp->isOcclusionState(LLSpatialGroup::OCCLUDED)) &&
  12551. hasRenderType(groupp->getSpatialPartition()->mDrawableType) &&
  12552. groupp->mDrawMap.count(type))
  12553. {
  12554. passp->renderGroup(groupp, type, mask, texture);
  12555. }
  12556. }
  12557. }
  12558. void LLPipeline::renderRiggedGroups(LLRenderPass* passp, U32 type, U32 mask,
  12559. bool texture)
  12560. {
  12561. LLCullResult::sg_list_t& visible_groups = sCull->getVisibleGroups();
  12562. for (U32 i = 0, count = visible_groups.size(); i < count; ++i)
  12563. {
  12564. LLSpatialGroup* groupp = visible_groups[i];
  12565. if (groupp && !groupp->isDead() &&
  12566. (!sUseOcclusion ||
  12567. !groupp->isOcclusionState(LLSpatialGroup::OCCLUDED)) &&
  12568. hasRenderType(groupp->getSpatialPartition()->mDrawableType) &&
  12569. groupp->mDrawMap.count(type))
  12570. {
  12571. passp->renderRiggedGroup(groupp, type, mask, texture);
  12572. }
  12573. }
  12574. }
  12575. void LLPipeline::generateImpostor(LLVOAvatar* avatarp)
  12576. {
  12577. if (!avatarp || avatarp->isDead() || !avatarp->mDrawable)
  12578. {
  12579. return;
  12580. }
  12581. LL_GL_CHECK_STATES;
  12582. static LLCullResult result;
  12583. result.clear();
  12584. grabReferences(result);
  12585. pushRenderTypeMask();
  12586. bool visually_muted = avatarp->isVisuallyMuted();
  12587. //MK
  12588. bool vision_restricted = gRLenabled && gRLInterface.mVisionRestricted;
  12589. if (vision_restricted)
  12590. {
  12591. // Render everything on impostors
  12592. andRenderTypeMask(RENDER_TYPE_ALPHA,
  12593. RENDER_TYPE_FULLBRIGHT,
  12594. RENDER_TYPE_VOLUME,
  12595. RENDER_TYPE_GLOW,
  12596. RENDER_TYPE_BUMP,
  12597. RENDER_TYPE_PASS_SIMPLE,
  12598. RENDER_TYPE_PASS_ALPHA,
  12599. RENDER_TYPE_PASS_ALPHA_MASK,
  12600. RENDER_TYPE_PASS_BUMP,
  12601. RENDER_TYPE_PASS_POST_BUMP,
  12602. RENDER_TYPE_PASS_FULLBRIGHT,
  12603. RENDER_TYPE_PASS_FULLBRIGHT_ALPHA_MASK,
  12604. RENDER_TYPE_PASS_FULLBRIGHT_SHINY,
  12605. RENDER_TYPE_PASS_GLOW,
  12606. RENDER_TYPE_PASS_GRASS,
  12607. RENDER_TYPE_PASS_SHINY,
  12608. RENDER_TYPE_PASS_MATERIAL,
  12609. RENDER_TYPE_PASS_MATERIAL_ALPHA,
  12610. RENDER_TYPE_PASS_MATERIAL_ALPHA_MASK,
  12611. RENDER_TYPE_PASS_MATERIAL_ALPHA_EMISSIVE,
  12612. RENDER_TYPE_PASS_SPECMAP,
  12613. RENDER_TYPE_PASS_SPECMAP_BLEND,
  12614. RENDER_TYPE_PASS_SPECMAP_MASK,
  12615. RENDER_TYPE_PASS_SPECMAP_EMISSIVE,
  12616. RENDER_TYPE_PASS_NORMMAP,
  12617. RENDER_TYPE_PASS_NORMMAP_BLEND,
  12618. RENDER_TYPE_PASS_NORMMAP_MASK,
  12619. RENDER_TYPE_PASS_NORMMAP_EMISSIVE,
  12620. RENDER_TYPE_PASS_NORMSPEC,
  12621. RENDER_TYPE_PASS_NORMSPEC_BLEND,
  12622. RENDER_TYPE_PASS_NORMSPEC_MASK,
  12623. RENDER_TYPE_PASS_NORMSPEC_EMISSIVE,
  12624. RENDER_TYPE_AVATAR,
  12625. RENDER_TYPE_PUPPET,
  12626. RENDER_TYPE_ALPHA_MASK,
  12627. RENDER_TYPE_FULLBRIGHT_ALPHA_MASK,
  12628. RENDER_TYPE_SIMPLE,
  12629. RENDER_TYPE_MATERIALS,
  12630. END_RENDER_TYPES);
  12631. }
  12632. else
  12633. //mk
  12634. if (visually_muted)
  12635. {
  12636. // Only show jelly doll geometry
  12637. andRenderTypeMask(RENDER_TYPE_AVATAR, RENDER_TYPE_PUPPET,
  12638. END_RENDER_TYPES);
  12639. }
  12640. else if (gUsePBRShaders)
  12641. {
  12642. clearRenderTypeMask(RENDER_TYPE_SKY,
  12643. RENDER_TYPE_WL_SKY,
  12644. RENDER_TYPE_TERRAIN,
  12645. RENDER_TYPE_GRASS,
  12646. RENDER_TYPE_PUPPET, // Animesh
  12647. RENDER_TYPE_TREE,
  12648. RENDER_TYPE_VOIDWATER,
  12649. RENDER_TYPE_WATER,
  12650. RENDER_TYPE_ALPHA_PRE_WATER,
  12651. RENDER_TYPE_PASS_GRASS,
  12652. RENDER_TYPE_HUD,
  12653. RENDER_TYPE_PARTICLES,
  12654. RENDER_TYPE_CLOUDS,
  12655. RENDER_TYPE_HUD_PARTICLES,
  12656. END_RENDER_TYPES);
  12657. }
  12658. else
  12659. {
  12660. // Hide world geometry
  12661. clearRenderTypeMask(RENDER_TYPE_SKY,
  12662. RENDER_TYPE_WL_SKY,
  12663. RENDER_TYPE_TERRAIN,
  12664. RENDER_TYPE_GRASS,
  12665. RENDER_TYPE_PUPPET, // Animesh
  12666. RENDER_TYPE_TREE,
  12667. RENDER_TYPE_VOIDWATER,
  12668. RENDER_TYPE_WATER,
  12669. RENDER_TYPE_PASS_GRASS,
  12670. RENDER_TYPE_HUD,
  12671. RENDER_TYPE_PARTICLES,
  12672. RENDER_TYPE_CLOUDS,
  12673. RENDER_TYPE_HUD_PARTICLES,
  12674. END_RENDER_TYPES);
  12675. }
  12676. LLDisableOcclusionCulling no_occlusion;
  12677. sReflectionRender = !sRenderDeferred;
  12678. sShadowRender = sImpostorRender = true;
  12679. {
  12680. LL_FAST_TIMER(FTM_IMPOSTOR_MARK_VISIBLE);
  12681. markVisible(avatarp->mDrawable, gViewerCamera);
  12682. LLVOAvatar::sUseImpostors = false;
  12683. for (S32 i = 0, count = avatarp->mAttachedObjectsVector.size();
  12684. i < count; ++i)
  12685. {
  12686. LLViewerObject* object = avatarp->mAttachedObjectsVector[i].first;
  12687. if (object)
  12688. {
  12689. markVisible(object->mDrawable->getSpatialBridge(),
  12690. gViewerCamera);
  12691. }
  12692. }
  12693. }
  12694. stateSort(gViewerCamera, result);
  12695. LLTexUnit* unit0 = gGL.getTexUnit(0);
  12696. LLCamera camera = gViewerCamera;
  12697. LLVector2 tdim;
  12698. U32 res_y = 0;
  12699. U32 res_x = 0;
  12700. {
  12701. LL_FAST_TIMER(FTM_IMPOSTOR_SETUP);
  12702. const LLVector4a* ext = avatarp->mDrawable->getSpatialExtents();
  12703. LLVector3 pos = avatarp->getRenderPosition() +
  12704. avatarp->getImpostorOffset();
  12705. camera.lookAt(gViewerCamera.getOrigin(), pos,
  12706. gViewerCamera.getUpAxis());
  12707. LLVector4a half_height;
  12708. half_height.setSub(ext[1], ext[0]);
  12709. half_height.mul(0.5f);
  12710. LLVector4a left;
  12711. left.load3(camera.getLeftAxis().mV);
  12712. left.mul(left);
  12713. left.normalize3fast();
  12714. LLVector4a up;
  12715. up.load3(camera.getUpAxis().mV);
  12716. up.mul(up);
  12717. up.normalize3fast();
  12718. tdim.mV[0] = fabsf(half_height.dot3(left).getF32());
  12719. tdim.mV[1] = fabsf(half_height.dot3(up).getF32());
  12720. gGL.matrixMode(LLRender::MM_PROJECTION);
  12721. gGL.pushMatrix();
  12722. F32 distance = (pos - camera.getOrigin()).length();
  12723. F32 fov = atanf(tdim.mV[1] / distance) * 2.f * RAD_TO_DEG;
  12724. F32 aspect = tdim.mV[0] / tdim.mV[1];
  12725. LLMatrix4a persp = gl_perspective(fov, aspect, .001f, 256.f);
  12726. gGLProjection = persp;
  12727. gGL.loadMatrix(persp);
  12728. gGL.matrixMode(LLRender::MM_MODELVIEW);
  12729. gGL.pushMatrix();
  12730. LLMatrix4a mat;
  12731. camera.getOpenGLTransform(mat.getF32ptr());
  12732. mat.setMul(OGL_TO_CFR_ROT4A, mat);
  12733. gGL.loadMatrix(mat);
  12734. gGLModelView = mat;
  12735. glClearColor(0.f, 0.f, 0.f, 0.f);
  12736. gGL.setColorMask(true, true);
  12737. // Get the number of pixels per angle
  12738. F32 pa = gViewerWindowp->getWindowDisplayHeight() /
  12739. (RAD_TO_DEG * gViewerCamera.getView());
  12740. // Get resolution based on angle width and height of impostor (double
  12741. // desired resolution to prevent aliasing)
  12742. res_y = llmin(nhpo2((U32)(fov * pa)), 512U);
  12743. res_x = llmin(nhpo2((U32)(atanf(tdim.mV[0] / distance) * 2.f *
  12744. RAD_TO_DEG * pa)),
  12745. 512U);
  12746. if (!avatarp->mImpostor.isComplete())
  12747. {
  12748. LL_FAST_TIMER(FTM_IMPOSTOR_ALLOCATE);
  12749. if (gUsePBRShaders)
  12750. {
  12751. avatarp->mImpostor.allocate(res_x, res_y, GL_RGBA, true);
  12752. }
  12753. else
  12754. {
  12755. U32 format = sRenderDeferred ? GL_SRGB8_ALPHA8 : GL_RGBA;
  12756. avatarp->mImpostor.allocate(res_x, res_y, format, true, false);
  12757. }
  12758. if (sRenderDeferred)
  12759. {
  12760. addDeferredAttachments(avatarp->mImpostor);
  12761. }
  12762. unit0->bind(&avatarp->mImpostor);
  12763. unit0->setTextureFilteringOption(LLTexUnit::TFO_POINT);
  12764. unit0->unbind(LLTexUnit::TT_TEXTURE);
  12765. }
  12766. else if (res_x != avatarp->mImpostor.getWidth() ||
  12767. res_y != avatarp->mImpostor.getHeight())
  12768. {
  12769. LL_FAST_TIMER(FTM_IMPOSTOR_RESIZE);
  12770. avatarp->mImpostor.resize(res_x, res_y);
  12771. }
  12772. avatarp->mImpostor.bindTarget();
  12773. stop_glerror();
  12774. }
  12775. F32 old_alpha = LLDrawPoolAvatar::sMinimumAlpha;
  12776. if (visually_muted)
  12777. {
  12778. // Disable alpha masking for muted avatars (get whole skin silhouette)
  12779. LLDrawPoolAvatar::sMinimumAlpha = 0.f;
  12780. }
  12781. if (sRenderDeferred)
  12782. {
  12783. avatarp->mImpostor.clear();
  12784. renderGeomDeferred(camera);
  12785. renderGeomPostDeferred(camera);
  12786. // Shameless hack time: render it all again, this time writing the
  12787. // depth values we need to generate the alpha mask below while
  12788. // preserving the alpha-sorted color rendering from the previous pass.
  12789. sImpostorRenderAlphaDepthPass = true;
  12790. // Depth-only here...
  12791. gGL.setColorMask(false, false);
  12792. renderGeomPostDeferred(camera);
  12793. sImpostorRenderAlphaDepthPass = false;
  12794. }
  12795. else
  12796. {
  12797. LLGLEnable scissor(GL_SCISSOR_TEST);
  12798. glScissor(0, 0, res_x, res_y);
  12799. avatarp->mImpostor.clear();
  12800. renderGeom(camera);
  12801. // Shameless hack time: render it all again, this time writing the
  12802. // depth values we need to generate the alpha mask below while
  12803. // preserving the alpha-sorted color rendering from the previous pass.
  12804. sImpostorRenderAlphaDepthPass = true;
  12805. // Depth-only here...
  12806. gGL.setColorMask(false, false);
  12807. renderGeom(camera);
  12808. sImpostorRenderAlphaDepthPass = false;
  12809. }
  12810. LLDrawPoolAvatar::sMinimumAlpha = old_alpha;
  12811. {
  12812. // Create alpha mask based on depth buffer (grey out if muted)
  12813. LL_FAST_TIMER(FTM_IMPOSTOR_BACKGROUND);
  12814. if (sRenderDeferred)
  12815. {
  12816. GLuint buff = GL_COLOR_ATTACHMENT0;
  12817. glDrawBuffers(1, &buff);
  12818. }
  12819. LLGLDisable blend(vision_restricted ? 0 : GL_BLEND); // mk
  12820. if (visually_muted)
  12821. {
  12822. gGL.setColorMask(true, true);
  12823. }
  12824. else
  12825. {
  12826. gGL.setColorMask(false, true);
  12827. }
  12828. unit0->unbind(LLTexUnit::TT_TEXTURE);
  12829. LLGLDepthTest depth(GL_TRUE, GL_FALSE, GL_GREATER);
  12830. gGL.flush();
  12831. gGL.pushMatrix();
  12832. gGL.loadIdentity();
  12833. gGL.matrixMode(LLRender::MM_PROJECTION);
  12834. gGL.pushMatrix();
  12835. gGL.loadIdentity();
  12836. constexpr F32 clip_plane = 0.99999f;
  12837. gDebugProgram.bind();
  12838. LLColor4 muted_color(avatarp->getMutedAVColor());
  12839. gGL.diffuseColor4fv(muted_color.mV);
  12840. gGL.begin(LLRender::TRIANGLES);
  12841. {
  12842. gGL.vertex3f(-1.f, -1.f, clip_plane);
  12843. gGL.vertex3f(1.f, -1.f, clip_plane);
  12844. gGL.vertex3f(1.f, 1.f, clip_plane);
  12845. gGL.vertex3f(-1.f, -1.f, clip_plane);
  12846. gGL.vertex3f(1.f, 1.f, clip_plane);
  12847. gGL.vertex3f(-1.f, 1.f, clip_plane);
  12848. }
  12849. gGL.end(true);
  12850. gDebugProgram.unbind();
  12851. gGL.popMatrix();
  12852. gGL.matrixMode(LLRender::MM_MODELVIEW);
  12853. gGL.popMatrix();
  12854. stop_glerror();
  12855. }
  12856. avatarp->mImpostor.flush();
  12857. avatarp->setImpostorDim(tdim);
  12858. LLVOAvatar::sUseImpostors = LLVOAvatar::sMaxNonImpostors != 0;
  12859. sReflectionRender = false;
  12860. sImpostorRender = false;
  12861. sShadowRender = false;
  12862. popRenderTypeMask();
  12863. gGL.matrixMode(LLRender::MM_PROJECTION);
  12864. gGL.popMatrix();
  12865. gGL.matrixMode(LLRender::MM_MODELVIEW);
  12866. gGL.popMatrix();
  12867. avatarp->mNeedsImpostorUpdate = false;
  12868. avatarp->cacheImpostorValues();
  12869. LLVertexBuffer::unbind();
  12870. LL_GL_CHECK_STATES;
  12871. }
  12872. void LLPipeline::previewAvatar(LLVOAvatar* avatarp)
  12873. {
  12874. LL_TRACY_TIMER(TRC_PREVIEW_AVATAR);
  12875. if (!avatarp || avatarp->isDead() || !avatarp->mDrawable)
  12876. {
  12877. llwarns_once << "Avatar is " << (avatarp ? "not drawable" : "NULL")
  12878. << llendl;
  12879. return;
  12880. }
  12881. gGL.flush();
  12882. stop_glerror();
  12883. LLGLDepthTest gls_depth(GL_TRUE);
  12884. #if 0
  12885. LLGLSDefault gls_default;
  12886. #endif
  12887. gGL.setSceneBlendType(LLRender::BT_REPLACE);
  12888. LL_GL_CHECK_STATES;
  12889. static LLCullResult result;
  12890. result.clear();
  12891. grabReferences(result);
  12892. pushRenderTypeMask();
  12893. // Hide world geometry
  12894. if (gUsePBRShaders)
  12895. {
  12896. clearRenderTypeMask(RENDER_TYPE_SKY,
  12897. RENDER_TYPE_WL_SKY,
  12898. RENDER_TYPE_TERRAIN,
  12899. RENDER_TYPE_GRASS,
  12900. RENDER_TYPE_PUPPET, // Animesh
  12901. RENDER_TYPE_TREE,
  12902. RENDER_TYPE_VOIDWATER,
  12903. RENDER_TYPE_WATER,
  12904. RENDER_TYPE_ALPHA_PRE_WATER,
  12905. RENDER_TYPE_PASS_GRASS,
  12906. RENDER_TYPE_HUD,
  12907. RENDER_TYPE_PARTICLES,
  12908. RENDER_TYPE_CLOUDS,
  12909. RENDER_TYPE_HUD_PARTICLES,
  12910. END_RENDER_TYPES);
  12911. }
  12912. else
  12913. {
  12914. clearRenderTypeMask(RENDER_TYPE_SKY,
  12915. RENDER_TYPE_WL_SKY,
  12916. RENDER_TYPE_TERRAIN,
  12917. RENDER_TYPE_GRASS,
  12918. RENDER_TYPE_PUPPET, // Animesh
  12919. RENDER_TYPE_TREE,
  12920. RENDER_TYPE_VOIDWATER,
  12921. RENDER_TYPE_WATER,
  12922. RENDER_TYPE_PASS_GRASS,
  12923. RENDER_TYPE_HUD,
  12924. RENDER_TYPE_PARTICLES,
  12925. RENDER_TYPE_CLOUDS,
  12926. RENDER_TYPE_HUD_PARTICLES,
  12927. END_RENDER_TYPES);
  12928. }
  12929. LLDisableOcclusionCulling no_occlusion;
  12930. sReflectionRender = sShadowRender = false;
  12931. sImpostorRender = true;
  12932. markVisible(avatarp->mDrawable, gViewerCamera);
  12933. static LLCachedControl<bool> with_rigged_meshes(gSavedSettings,
  12934. "PreviewAvatarWithRigged");
  12935. if (with_rigged_meshes)
  12936. {
  12937. // Only show rigged attachments for preview, for the sake of
  12938. // performance, and so that static objects would not obstruct
  12939. // previewing changes.
  12940. for (U32 i = 0, count = avatarp->mAttachedObjectsVector.size();
  12941. i < count; ++i)
  12942. {
  12943. LLViewerObject* objectp = avatarp->mAttachedObjectsVector[i].first;
  12944. if (!objectp || objectp->isDead())
  12945. {
  12946. continue;
  12947. }
  12948. bool is_rigged_mesh = objectp->isRiggedMesh();
  12949. if (!is_rigged_mesh)
  12950. {
  12951. // Sometimes object is a linkset and rigged mesh is a child
  12952. LLViewerObject::const_child_list_t& clist =
  12953. objectp->getChildren();
  12954. for (LLViewerObject::const_child_list_t::const_iterator
  12955. it = clist.begin(), end = clist.end();
  12956. it != end; ++it)
  12957. {
  12958. const LLViewerObject* childp = *it;
  12959. if (childp && childp->isRiggedMesh())
  12960. {
  12961. is_rigged_mesh = true;
  12962. break;
  12963. }
  12964. }
  12965. }
  12966. if (is_rigged_mesh)
  12967. {
  12968. LLDrawable* drawablep = objectp->mDrawable;
  12969. if (drawablep && !drawablep->isDead())
  12970. {
  12971. markVisible(drawablep->getSpatialBridge(), gViewerCamera);
  12972. }
  12973. }
  12974. }
  12975. }
  12976. stateSort(gViewerCamera, result);
  12977. F32 old_alpha = LLDrawPoolAvatar::sMinimumAlpha;
  12978. LLDrawPoolAvatar::sMinimumAlpha = 0.f;
  12979. if (sRenderDeferred)
  12980. {
  12981. renderGeomDeferred(gViewerCamera);
  12982. renderGeomPostDeferred(gViewerCamera);
  12983. }
  12984. else
  12985. {
  12986. renderGeom(gViewerCamera);
  12987. }
  12988. LLDrawPoolAvatar::sMinimumAlpha = old_alpha;
  12989. // Create an alpha mask based on depth buffer
  12990. {
  12991. if (sRenderDeferred)
  12992. {
  12993. GLuint buff = GL_COLOR_ATTACHMENT0;
  12994. glDrawBuffers(1, &buff);
  12995. }
  12996. LLGLDisable blend(GL_BLEND);
  12997. gGL.setColorMask(false, true);
  12998. gGL.getTexUnit(0)->unbind(LLTexUnit::TT_TEXTURE);
  12999. LLGLDepthTest depth(GL_TRUE, GL_FALSE, GL_GREATER);
  13000. gGL.flush();
  13001. gGL.pushMatrix();
  13002. gGL.loadIdentity();
  13003. gGL.matrixMode(LLRender::MM_PROJECTION);
  13004. gGL.pushMatrix();
  13005. gGL.loadIdentity();
  13006. gDebugProgram.bind();
  13007. gGL.begin(LLRender::TRIANGLES);
  13008. constexpr F32 clip_plane = 0.99999f;
  13009. gGL.vertex3f(-1.f, -1.f, clip_plane);
  13010. gGL.vertex3f(-1.f, 1.f, clip_plane);
  13011. gGL.vertex3f(1.f, -1.f, clip_plane);
  13012. gGL.vertex3f(1.f, -1.f, clip_plane);
  13013. gGL.vertex3f(-1.f, 1.f, clip_plane);
  13014. gGL.vertex3f(1.f, 1.f, clip_plane);
  13015. gGL.end(true);
  13016. gDebugProgram.unbind();
  13017. gGL.popMatrix();
  13018. gGL.matrixMode(LLRender::MM_MODELVIEW);
  13019. gGL.popMatrix();
  13020. }
  13021. popRenderTypeMask();
  13022. sImpostorRender = false;
  13023. LLVertexBuffer::unbind();
  13024. LL_GL_CHECK_STATES;
  13025. gGL.setSceneBlendType(LLRender::BT_ALPHA);
  13026. gGL.flush();
  13027. }
  13028. void LLPipeline::setRenderTypeMask(U32 type, ...)
  13029. {
  13030. va_list args;
  13031. va_start(args, type);
  13032. while (type < END_RENDER_TYPES)
  13033. {
  13034. mRenderTypeEnabled[type] = true;
  13035. type = va_arg(args, U32);
  13036. }
  13037. va_end(args);
  13038. if (type > END_RENDER_TYPES)
  13039. {
  13040. llerrs << "Invalid render type." << llendl;
  13041. }
  13042. }
  13043. bool LLPipeline::hasAnyRenderType(U32 type, ...) const
  13044. {
  13045. va_list args;
  13046. va_start(args, type);
  13047. while (type < END_RENDER_TYPES)
  13048. {
  13049. if (mRenderTypeEnabled[type])
  13050. {
  13051. va_end(args);
  13052. return true;
  13053. }
  13054. type = va_arg(args, U32);
  13055. }
  13056. va_end(args);
  13057. if (type > END_RENDER_TYPES)
  13058. {
  13059. llerrs << "Invalid render type." << llendl;
  13060. }
  13061. return false;
  13062. }
  13063. void LLPipeline::pushRenderTypeMask()
  13064. {
  13065. mRenderTypeEnableStack.emplace((const char*)mRenderTypeEnabled,
  13066. sizeof(mRenderTypeEnabled));
  13067. }
  13068. void LLPipeline::popRenderTypeMask()
  13069. {
  13070. if (mRenderTypeEnableStack.empty())
  13071. {
  13072. llerrs << "Depleted render type stack." << llendl;
  13073. }
  13074. memcpy(mRenderTypeEnabled, mRenderTypeEnableStack.top().data(),
  13075. sizeof(mRenderTypeEnabled));
  13076. mRenderTypeEnableStack.pop();
  13077. }
  13078. void LLPipeline::andRenderTypeMask(U32 type, ...)
  13079. {
  13080. bool tmp[NUM_RENDER_TYPES];
  13081. for (U32 i = 0; i < NUM_RENDER_TYPES; ++i)
  13082. {
  13083. tmp[i] = false;
  13084. }
  13085. va_list args;
  13086. va_start(args, type);
  13087. while (type < END_RENDER_TYPES)
  13088. {
  13089. if (mRenderTypeEnabled[type])
  13090. {
  13091. tmp[type] = true;
  13092. }
  13093. type = va_arg(args, U32);
  13094. }
  13095. va_end(args);
  13096. if (type > END_RENDER_TYPES)
  13097. {
  13098. llerrs << "Invalid render type." << llendl;
  13099. }
  13100. for (U32 i = 0; i < NUM_RENDER_TYPES; ++i)
  13101. {
  13102. mRenderTypeEnabled[i] = tmp[i];
  13103. }
  13104. }
  13105. void LLPipeline::clearRenderTypeMask(U32 type, ...)
  13106. {
  13107. va_list args;
  13108. va_start(args, type);
  13109. while (type < END_RENDER_TYPES)
  13110. {
  13111. mRenderTypeEnabled[type] = false;
  13112. type = va_arg(args, U32);
  13113. }
  13114. va_end(args);
  13115. if (type > END_RENDER_TYPES)
  13116. {
  13117. llerrs << "Invalid render type." << llendl;
  13118. }
  13119. }
  13120. void LLPipeline::setAllRenderTypes()
  13121. {
  13122. for (U32 i = 0; i < NUM_RENDER_TYPES; ++i)
  13123. {
  13124. mRenderTypeEnabled[i] = true;
  13125. }
  13126. }
  13127. void LLPipeline::addDebugBlip(const LLVector3& position, const LLColor4& color)
  13128. {
  13129. mDebugBlips.emplace_back(position, color);
  13130. }
  13131. class LLOctreeDirtyInfo : public OctreeTraveler
  13132. {
  13133. public:
  13134. void visit(const OctreeNode* nodep) override
  13135. {
  13136. LLSpatialGroup* groupp = (LLSpatialGroup*)nodep->getListener(0);
  13137. if (groupp->getSpatialPartition()->mRenderByGroup)
  13138. {
  13139. groupp->setState(LLSpatialGroup::GEOM_DIRTY);
  13140. gPipeline.markRebuild(groupp);
  13141. LLSpatialGroup::bridge_list_t& blist = groupp->mBridgeList;
  13142. for (U32 i = 0, count = blist.size(); i < count; ++i)
  13143. {
  13144. traverse(blist[i]->mOctree);
  13145. }
  13146. }
  13147. }
  13148. };
  13149. void LLPipeline::rebuildDrawInfo()
  13150. {
  13151. for (LLWorld::region_list_t::const_iterator
  13152. iter = gWorld.getRegionList().begin(),
  13153. end = gWorld.getRegionList().end();
  13154. iter != end; ++iter)
  13155. {
  13156. LLViewerRegion* regionp = *iter;
  13157. LLOctreeDirtyInfo dirty;
  13158. LLSpatialPartition* partp =
  13159. regionp->getSpatialPartition(LLViewerRegion::PARTITION_VOLUME);
  13160. dirty.traverse(partp->mOctree);
  13161. partp = regionp->getSpatialPartition(LLViewerRegion::PARTITION_BRIDGE);
  13162. dirty.traverse(partp->mOctree);
  13163. }
  13164. }