12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243224422452246224722482249225022512252225322542255225622572258225922602261226222632264226522662267226822692270227122722273227422752276227722782279228022812282228322842285228622872288228922902291229222932294229522962297229822992300230123022303230423052306230723082309231023112312231323142315231623172318231923202321232223232324232523262327232823292330233123322333233423352336233723382339234023412342234323442345234623472348234923502351235223532354235523562357235823592360236123622363236423652366236723682369237023712372237323742375237623772378237923802381238223832384238523862387238823892390239123922393239423952396239723982399240024012402240324042405240624072408240924102411241224132414241524162417241824192420242124222423242424252426242724282429243024312432243324342435243624372438243924402441244224432444244524462447244824492450245124522453245424552456245724582459246024612462246324642465246624672468246924702471247224732474247524762477247824792480248124822483248424852486248724882489249024912492249324942495249624972498249925002501250225032504250525062507250825092510251125122513251425152516251725182519252025212522252325242525252625272528252925302531253225332534253525362537253825392540254125422543254425452546254725482549255025512552255325542555255625572558255925602561256225632564256525662567256825692570257125722573257425752576257725782579258025812582258325842585258625872588258925902591259225932594259525962597259825992600260126022603260426052606260726082609261026112612261326142615261626172618261926202621262226232624262526262627262826292630263126322633263426352636263726382639264026412642264326442645264626472648264926502651265226532654265526562657265826592660266126622663266426652666266726682669267026712672267326742675267626772678267926802681268226832684268526862687268826892690269126922693269426952696269726982699270027012702270327042705270627072708270927102711271227132714271527162717271827192720272127222723272427252726272727282729273027312732273327342735273627372738273927402741274227432744274527462747274827492750275127522753275427552756275727582759276027612762276327642765276627672768276927702771277227732774277527762777277827792780278127822783278427852786278727882789279027912792279327942795279627972798279928002801280228032804280528062807280828092810281128122813281428152816281728182819282028212822282328242825282628272828282928302831283228332834283528362837283828392840284128422843284428452846284728482849285028512852285328542855285628572858285928602861286228632864286528662867286828692870287128722873287428752876287728782879288028812882288328842885288628872888288928902891289228932894289528962897289828992900290129022903290429052906290729082909291029112912291329142915291629172918291929202921292229232924292529262927292829292930293129322933293429352936293729382939294029412942294329442945294629472948294929502951295229532954295529562957295829592960296129622963296429652966296729682969297029712972297329742975297629772978297929802981298229832984298529862987298829892990299129922993299429952996299729982999300030013002300330043005300630073008300930103011301230133014301530163017301830193020302130223023302430253026302730283029303030313032303330343035303630373038303930403041304230433044304530463047304830493050305130523053305430553056305730583059306030613062306330643065306630673068306930703071307230733074307530763077307830793080308130823083308430853086308730883089309030913092309330943095309630973098309931003101310231033104310531063107310831093110311131123113311431153116311731183119312031213122312331243125312631273128312931303131313231333134313531363137313831393140314131423143314431453146314731483149315031513152315331543155315631573158315931603161316231633164316531663167316831693170317131723173317431753176317731783179318031813182318331843185318631873188318931903191319231933194319531963197319831993200320132023203320432053206320732083209321032113212321332143215321632173218321932203221322232233224322532263227322832293230323132323233323432353236323732383239324032413242324332443245324632473248324932503251325232533254325532563257325832593260326132623263326432653266326732683269327032713272327332743275327632773278327932803281328232833284328532863287328832893290329132923293329432953296329732983299330033013302330333043305330633073308330933103311331233133314331533163317331833193320332133223323332433253326332733283329333033313332333333343335333633373338333933403341334233433344334533463347334833493350335133523353335433553356335733583359336033613362336333643365336633673368336933703371337233733374337533763377337833793380338133823383338433853386338733883389339033913392339333943395339633973398339934003401340234033404340534063407340834093410341134123413341434153416341734183419342034213422342334243425342634273428342934303431343234333434343534363437343834393440344134423443344434453446344734483449345034513452345334543455345634573458345934603461346234633464346534663467346834693470347134723473347434753476347734783479348034813482348334843485348634873488348934903491349234933494349534963497349834993500350135023503350435053506350735083509351035113512351335143515351635173518351935203521352235233524352535263527352835293530353135323533353435353536353735383539354035413542354335443545354635473548354935503551355235533554355535563557355835593560356135623563356435653566356735683569357035713572357335743575357635773578357935803581358235833584358535863587358835893590359135923593359435953596359735983599360036013602360336043605360636073608360936103611361236133614361536163617361836193620362136223623362436253626362736283629363036313632363336343635363636373638363936403641364236433644364536463647364836493650365136523653365436553656365736583659366036613662366336643665366636673668366936703671367236733674367536763677367836793680368136823683368436853686368736883689369036913692369336943695369636973698369937003701370237033704370537063707370837093710371137123713371437153716371737183719372037213722372337243725372637273728372937303731373237333734373537363737373837393740374137423743374437453746374737483749375037513752375337543755375637573758375937603761376237633764376537663767376837693770377137723773377437753776377737783779378037813782378337843785378637873788378937903791379237933794379537963797379837993800380138023803380438053806380738083809381038113812381338143815381638173818381938203821382238233824382538263827382838293830383138323833383438353836383738383839384038413842384338443845384638473848384938503851385238533854385538563857385838593860386138623863386438653866386738683869387038713872387338743875387638773878387938803881388238833884388538863887388838893890389138923893389438953896389738983899390039013902390339043905390639073908390939103911391239133914391539163917391839193920392139223923392439253926392739283929393039313932393339343935393639373938393939403941394239433944394539463947394839493950395139523953395439553956395739583959396039613962396339643965396639673968396939703971397239733974397539763977397839793980398139823983398439853986398739883989399039913992399339943995399639973998399940004001400240034004400540064007400840094010401140124013401440154016401740184019402040214022402340244025402640274028402940304031403240334034403540364037403840394040404140424043404440454046404740484049405040514052405340544055405640574058405940604061406240634064406540664067406840694070407140724073407440754076407740784079408040814082408340844085408640874088408940904091409240934094409540964097409840994100410141024103410441054106410741084109411041114112411341144115411641174118411941204121412241234124412541264127412841294130413141324133413441354136413741384139414041414142414341444145414641474148414941504151415241534154415541564157415841594160416141624163416441654166416741684169417041714172417341744175417641774178417941804181418241834184418541864187418841894190419141924193419441954196419741984199420042014202420342044205420642074208420942104211421242134214421542164217421842194220422142224223422442254226422742284229423042314232423342344235423642374238423942404241424242434244424542464247424842494250425142524253425442554256425742584259426042614262426342644265426642674268426942704271427242734274427542764277427842794280428142824283428442854286428742884289429042914292429342944295429642974298429943004301430243034304430543064307430843094310431143124313431443154316431743184319432043214322432343244325432643274328432943304331433243334334433543364337433843394340434143424343434443454346434743484349435043514352435343544355435643574358435943604361436243634364436543664367436843694370437143724373437443754376437743784379438043814382438343844385438643874388438943904391439243934394439543964397439843994400440144024403440444054406440744084409441044114412441344144415441644174418441944204421442244234424442544264427442844294430443144324433443444354436443744384439444044414442444344444445444644474448444944504451445244534454445544564457445844594460446144624463446444654466446744684469447044714472447344744475447644774478447944804481448244834484448544864487448844894490449144924493449444954496449744984499450045014502450345044505450645074508450945104511451245134514451545164517451845194520452145224523452445254526452745284529453045314532453345344535453645374538453945404541454245434544454545464547454845494550455145524553455445554556455745584559456045614562456345644565456645674568456945704571457245734574457545764577457845794580458145824583458445854586458745884589459045914592459345944595459645974598459946004601460246034604460546064607460846094610461146124613461446154616461746184619462046214622462346244625462646274628462946304631463246334634463546364637463846394640464146424643464446454646464746484649465046514652465346544655465646574658465946604661466246634664466546664667466846694670467146724673467446754676467746784679468046814682468346844685468646874688468946904691469246934694469546964697469846994700470147024703470447054706470747084709471047114712471347144715471647174718471947204721472247234724472547264727472847294730473147324733473447354736473747384739474047414742474347444745474647474748474947504751475247534754475547564757475847594760476147624763476447654766476747684769477047714772477347744775477647774778477947804781478247834784478547864787478847894790479147924793479447954796479747984799480048014802480348044805480648074808480948104811481248134814481548164817481848194820482148224823482448254826482748284829483048314832483348344835483648374838483948404841484248434844484548464847484848494850485148524853485448554856485748584859486048614862486348644865486648674868486948704871487248734874487548764877487848794880488148824883488448854886488748884889489048914892489348944895489648974898489949004901490249034904490549064907490849094910491149124913491449154916491749184919492049214922492349244925492649274928492949304931493249334934493549364937493849394940494149424943494449454946494749484949495049514952495349544955495649574958495949604961496249634964496549664967496849694970497149724973497449754976497749784979498049814982498349844985498649874988498949904991499249934994499549964997499849995000500150025003500450055006500750085009501050115012501350145015501650175018501950205021502250235024502550265027502850295030503150325033503450355036503750385039504050415042504350445045504650475048504950505051505250535054505550565057505850595060506150625063506450655066506750685069507050715072507350745075507650775078507950805081508250835084508550865087508850895090509150925093509450955096509750985099510051015102510351045105510651075108510951105111511251135114511551165117511851195120512151225123512451255126512751285129513051315132513351345135513651375138513951405141514251435144514551465147514851495150515151525153515451555156515751585159516051615162516351645165516651675168516951705171517251735174517551765177517851795180518151825183518451855186518751885189519051915192519351945195519651975198519952005201520252035204520552065207520852095210521152125213521452155216521752185219522052215222522352245225522652275228522952305231523252335234523552365237523852395240524152425243524452455246524752485249525052515252525352545255525652575258525952605261526252635264526552665267526852695270527152725273527452755276527752785279528052815282528352845285528652875288528952905291529252935294529552965297529852995300530153025303530453055306530753085309531053115312531353145315531653175318531953205321532253235324532553265327532853295330533153325333533453355336533753385339534053415342534353445345534653475348534953505351535253535354535553565357535853595360536153625363536453655366536753685369537053715372537353745375537653775378537953805381538253835384538553865387538853895390539153925393539453955396539753985399540054015402540354045405540654075408540954105411541254135414541554165417541854195420542154225423542454255426542754285429543054315432543354345435543654375438543954405441544254435444544554465447544854495450545154525453545454555456545754585459546054615462546354645465546654675468546954705471547254735474547554765477547854795480548154825483548454855486548754885489549054915492549354945495549654975498549955005501550255035504550555065507550855095510551155125513551455155516551755185519552055215522552355245525552655275528552955305531553255335534553555365537553855395540554155425543554455455546554755485549555055515552555355545555555655575558555955605561556255635564556555665567556855695570557155725573557455755576557755785579558055815582558355845585558655875588558955905591559255935594559555965597559855995600560156025603560456055606560756085609561056115612561356145615561656175618561956205621562256235624562556265627562856295630563156325633563456355636563756385639564056415642564356445645564656475648564956505651565256535654565556565657565856595660566156625663566456655666566756685669567056715672567356745675567656775678567956805681568256835684568556865687568856895690569156925693569456955696569756985699570057015702570357045705570657075708570957105711571257135714571557165717571857195720572157225723572457255726572757285729573057315732573357345735573657375738573957405741574257435744574557465747574857495750575157525753575457555756575757585759576057615762576357645765576657675768576957705771577257735774577557765777577857795780578157825783578457855786578757885789579057915792579357945795579657975798579958005801580258035804580558065807580858095810581158125813581458155816581758185819582058215822582358245825582658275828582958305831583258335834583558365837583858395840584158425843584458455846584758485849585058515852585358545855585658575858585958605861586258635864586558665867586858695870587158725873587458755876587758785879588058815882588358845885588658875888588958905891589258935894589558965897589858995900590159025903590459055906590759085909591059115912591359145915591659175918591959205921592259235924592559265927592859295930593159325933593459355936593759385939594059415942594359445945594659475948594959505951595259535954595559565957595859595960596159625963596459655966596759685969597059715972597359745975597659775978597959805981598259835984598559865987598859895990599159925993599459955996599759985999600060016002600360046005600660076008600960106011601260136014601560166017601860196020602160226023602460256026602760286029603060316032603360346035603660376038603960406041604260436044604560466047604860496050605160526053605460556056605760586059606060616062606360646065606660676068606960706071607260736074607560766077607860796080608160826083608460856086608760886089609060916092609360946095609660976098609961006101610261036104610561066107610861096110611161126113611461156116611761186119612061216122612361246125612661276128612961306131613261336134613561366137613861396140614161426143614461456146614761486149615061516152615361546155615661576158615961606161616261636164616561666167616861696170617161726173617461756176617761786179618061816182618361846185618661876188618961906191619261936194619561966197619861996200620162026203620462056206620762086209621062116212621362146215621662176218621962206221622262236224622562266227622862296230623162326233623462356236623762386239624062416242624362446245624662476248624962506251625262536254625562566257625862596260626162626263626462656266626762686269627062716272627362746275627662776278627962806281628262836284628562866287628862896290629162926293629462956296629762986299630063016302630363046305630663076308630963106311631263136314631563166317631863196320632163226323632463256326632763286329633063316332633363346335633663376338633963406341634263436344634563466347634863496350635163526353635463556356635763586359636063616362636363646365636663676368636963706371637263736374637563766377637863796380638163826383638463856386638763886389639063916392639363946395639663976398639964006401640264036404640564066407640864096410641164126413641464156416641764186419642064216422642364246425642664276428642964306431643264336434643564366437643864396440644164426443644464456446644764486449645064516452645364546455645664576458645964606461646264636464646564666467646864696470647164726473647464756476647764786479648064816482648364846485648664876488648964906491649264936494649564966497649864996500650165026503650465056506650765086509651065116512651365146515651665176518651965206521652265236524652565266527652865296530653165326533653465356536653765386539654065416542654365446545654665476548654965506551655265536554655565566557655865596560656165626563656465656566656765686569657065716572657365746575657665776578657965806581658265836584658565866587658865896590659165926593659465956596659765986599660066016602660366046605660666076608660966106611661266136614661566166617661866196620662166226623662466256626662766286629663066316632663366346635663666376638663966406641664266436644664566466647664866496650665166526653665466556656665766586659666066616662666366646665666666676668666966706671667266736674667566766677667866796680668166826683668466856686668766886689669066916692669366946695669666976698669967006701670267036704670567066707670867096710671167126713671467156716671767186719672067216722672367246725672667276728672967306731673267336734673567366737673867396740674167426743674467456746674767486749675067516752675367546755675667576758675967606761676267636764676567666767676867696770677167726773677467756776677767786779678067816782678367846785678667876788678967906791679267936794679567966797679867996800680168026803680468056806680768086809681068116812681368146815681668176818681968206821682268236824682568266827682868296830683168326833683468356836683768386839684068416842684368446845684668476848684968506851685268536854685568566857685868596860686168626863686468656866686768686869687068716872687368746875687668776878687968806881688268836884688568866887688868896890689168926893689468956896689768986899690069016902690369046905690669076908690969106911691269136914691569166917691869196920692169226923692469256926692769286929693069316932693369346935693669376938693969406941694269436944694569466947694869496950695169526953695469556956695769586959696069616962696369646965696669676968696969706971697269736974697569766977697869796980698169826983698469856986698769886989699069916992699369946995699669976998699970007001700270037004700570067007700870097010701170127013701470157016701770187019702070217022702370247025702670277028702970307031703270337034703570367037703870397040704170427043704470457046704770487049705070517052705370547055705670577058705970607061706270637064706570667067706870697070707170727073707470757076707770787079708070817082708370847085708670877088708970907091709270937094709570967097709870997100710171027103710471057106710771087109711071117112 |
- //
- // The multimedia graphics platform GLScene https://github.com/glscene
- //
- unit GLS.MaterialEx;
- (*
- Handles extended material and it components:
- textures, samplers, combiners, shaders and etc.
- Features:
- - material can contain different level of applying accordingly to hardware i.e.
- Feateres scaling.
- - if automatically or by user selected level failed, material down to lower level.
- - direct state access can be used for uniforms setting.
- - economy mode for texture binding to active units,
- i.e. if textures less than maximum units may be not one binding occur per frame.
- *)
- interface
- {$I GLScene.inc}
- uses
- Winapi.OpenGL,
- Winapi.OpenGLext,
- System.Classes,
- System.SysUtils,
- System.Math,
- Vcl.Graphics,
- GLS.OpenGLTokens,
- GLS.RenderContextInfo,
- GLS.PipelineTransformation,
- GLS.BaseClasses,
- GLS.Context,
- GLS.VectorTypes,
- GLS.Material,
- GLS.Texture,
- GLS.Color,
- GLS.Coordinates,
- GLS.VectorGeometry,
- GLS.Graphics,
- GLS.PersistentClasses,
- GLS.State,
- GLS.TextureFormat,
- GLS.XCollection,
- GLS.TextureCombiners,
- GLSL.ShaderParameter,
- GLS.ApplicationFileIO,
- GLS.Strings,
- GLS.ImageUtils,
- GLS.Utils,
- GLS.XOpenGL,
- GLS.Logger;
- type
- TGLMaterialComponentName = string;
- TGLMaterialLibraryEx = class;
- TGLMatLibComponents = class;
- TGLLibMaterialEx = class;
- TGLBaseShaderModel = class;
- TGLASMVertexProgram = class;
- TOnAsmProgSetting = procedure(Sender: TGLASMVertexProgram;
- var ARci: TGLRenderContextInfo) of object;
- TOnUniformInitialize = procedure(Sender: TGLBaseShaderModel) of object;
- TOnUniformSetting = procedure(Sender: TGLBaseShaderModel;
- var ARci: TGLRenderContextInfo) of object;
- TGLBaseMaterialCollectionItem = class(
- TXCollectionItem,
- IGLMaterialLibrarySupported)
- private
- FNameHashKey: Integer;
- FUserList: TGLPersistentObjectList;
- FDefferedInit: Boolean;
- FNotifying: Boolean;
- FIsValid: Boolean;
- function GetUserList: TGLPersistentObjectList;
- function GetMaterialLibraryEx: TGLMaterialLibraryEx;
- protected
- procedure SetName(const AValue: TGLMaterialComponentName); override;
- procedure NotifyChange(Sender: TObject); virtual;
- property UserList: TGLPersistentObjectList read GetUserList;
- procedure DoOnPrepare(Sender: TGLContext); virtual; abstract;
- public
- destructor Destroy; override;
- procedure RegisterUser(AUser: TGLUpdateAbleObject);
- procedure UnregisterUser(AUser: TGLUpdateAbleObject);
- function GetUserCount: Integer;
- function GetMaterialLibrary: TGLAbstractMaterialLibrary;
- property MaterialLibrary: TGLMaterialLibraryEx read GetMaterialLibraryEx;
- property IsValid: Boolean read FIsValid;
- published
- property Name: TGLMaterialComponentName read GetName write SetName;
- (* Run-time flag, indicate that resource
- should initialize in case of failure material's level. *)
- property DefferedInit: Boolean read FDefferedInit write FDefferedInit
- default False;
- end;
- CGLBaseMaterialCollectionItem = class of TGLBaseMaterialCollectionItem;
- TGLLibMaterialProperty = class(TGLUpdateAbleObject, IGLMaterialLibrarySupported)
- protected
- FEnabled: Boolean;
- FNextPassName: TGLLibMaterialName;
- function GetMaterial: TGLLibMaterialEx;
- function GetMaterialLibraryEx: TGLMaterialLibraryEx;
- procedure SetEnabled(AValue: Boolean); virtual;
- procedure SetNextPass(const AValue: TGLLibMaterialName);
- procedure Loaded; virtual;
- property NextPass: TGLLibMaterialName read FNextPassName write SetNextPass;
- public
- procedure NotifyChange(Sender: TObject); override;
- function GetMaterialLibrary: TGLAbstractMaterialLibrary;
- property MaterialLibrary: TGLMaterialLibraryEx read GetMaterialLibraryEx;
- published
- property Enabled: Boolean read FEnabled write SetEnabled;
- end;
- TGLTextureSampler = class(TGLBaseMaterialCollectionItem)
- protected
- procedure WriteToFiler(AWriter: TWriter); override;
- procedure ReadFromFiler(AReader: TReader); override;
- private
- FHandle: TGLSamplerHandle;
- FMinFilter: TGLMinFilter;
- FMagFilter: TGLMagFilter;
- FFilteringQuality: TGLTextureFilteringQuality;
- FLODBias: Integer;
- FLODBiasFract: Single;
- FWrap: array[0..2] of TGLSeparateTextureWrap;
- FBorderColor: TGLColor;
- FCompareMode: TGLTextureCompareMode;
- FCompareFunc: TGLDepthFunction;
- FDecodeSRGB: Boolean;
- procedure SetMagFilter(AValue: TGLMagFilter);
- procedure SetMinFilter(AValue: TGLMinFilter);
- procedure SetLODBias(AValue: Integer);
- procedure SetFilteringQuality(AValue: TGLTextureFilteringQuality);
- function GetWrap(Index: Integer): TGLSeparateTextureWrap;
- procedure SetWrap(Index: Integer; AValue: TGLSeparateTextureWrap);
- procedure SetBorderColor(const AValue: TGLColor);
- procedure SetCompareMode(AValue: TGLTextureCompareMode);
- procedure SetCompareFunc(AValue: TGLDepthFunction);
- procedure SetDecodeSRGB(AValue: Boolean);
- public
- constructor Create(AOwner: TXCollection); override;
- destructor Destroy; override;
- procedure Assign(Source: TPersistent); override;
- procedure NotifyChange(Sender: TObject); override;
- procedure DoOnPrepare(Sender: TGLContext); override;
- procedure Apply(var ARci: TGLRenderContextInfo);
- procedure UnApply(var ARci: TGLRenderContextInfo);
- class function FriendlyName: string; override;
- property Handle: TGLSamplerHandle read FHandle;
- published
- // Texture magnification filter.
- property MagFilter: TGLMagFilter read FMagFilter write SetMagFilter
- default maLinear;
- // Texture minification filter.
- property MinFilter: TGLMinFilter read FMinFilter write SetMinFilter
- default miLinearMipMapLinear;
- property FilteringQuality: TGLTextureFilteringQuality read FFilteringQuality
- write SetFilteringQuality default tfAnisotropic;
- // Texture LOD bias.
- property LodBias: Integer read FLODBias write SetLODBias default 0;
- // Address mode for the texture.
- property WrapX: TGLSeparateTextureWrap index 0 read GetWrap write SetWrap
- default twRepeat;
- property WrapY: TGLSeparateTextureWrap index 1 read GetWrap write SetWrap
- default twRepeat;
- property WrapZ: TGLSeparateTextureWrap index 2 read GetWrap write SetWrap
- default twRepeat;
- // Texture border color.
- property BorderColor: TGLColor read FBorderColor
- write SetBorderColor;
- // Compare mode and function for depth texture
- property CompareMode: TGLTextureCompareMode read FCompareMode
- write SetCompareMode default tcmNone;
- property CompareFunc: TGLDepthFunction read FCompareFunc
- write SetCompareFunc default cfLEqual;
- (* Force retrieving the undecoded sRGB data from the
- texture and manipulate that directly. *)
- property sRGB_Encode: Boolean read FDecodeSRGB write SetDecodeSRGB
- default True;
- end;
- TGLAbstractTexture = class(TGLBaseMaterialCollectionItem)
- protected
- FHandle: TGLTextureHandle;
- FInternalFormat: TGLInternalFormat;
- FWidth: Integer;
- FHeight: Integer;
- FDepth: Integer;
- FSwizzles: TSwizzleVector;
- FApplicableSampler: TGLTextureSampler;
- FLastSampler: TGLTextureSampler;
- function GetTextureTarget: TGLTextureTarget;
- procedure Apply(var ARci: TGLRenderContextInfo); virtual; abstract;
- procedure UnApply(var ARci: TGLRenderContextInfo); virtual; abstract;
- public
- property Handle: TGLTextureHandle read FHandle;
- published
- property Shape: TGLTextureTarget read GetTextureTarget;
- end;
- TMipmapGenerationMode =
- (
- mgmNoMip,
- mgmLeaveExisting,
- mgmOnFly,
- mgmBoxFilter,
- mgmTriangleFilter,
- mgmHermiteFilter,
- mgmBellFilter,
- mgmSplineFilter,
- mgmLanczos3Filter,
- mgmMitchellFilter
- );
- TGLTextureImageEx = class(TGLAbstractTexture)
- protected
- procedure WriteToFiler(AWriter: TWriter); override;
- procedure ReadFromFiler(AReader: TReader); override;
- private
- FCompression: TGLTextureCompression;
- FImage: TGLBaseImage;
- FImageAlpha: TGLTextureImageAlpha;
- FImageBrightness: Single;
- FImageGamma: Single;
- FHeightToNormalScale: Single;
- FSourceFile: string;
- FApplyCounter: Integer;
- FInternallyStored: Boolean;
- FMipGenMode: TMipmapGenerationMode;
- FUseStreaming: Boolean;
- FBaseLevel: Integer;
- FMaxLevel: Integer;
- FLastTime: Double;
- procedure SetInternalFormat(const AValue: TGLInternalFormat);
- procedure SetImageAlpha(const AValue: TGLTextureImageAlpha);
- procedure SetImageBrightness(const AValue: Single);
- function StoreBrightness: Boolean;
- procedure SetImageGamma(const AValue: Single);
- function StoreGamma: Boolean;
- procedure SetNormalMapScale(const AValue: Single);
- function StoreNormalMapScale: Boolean;
- procedure SetCompression(const AValue: TGLTextureCompression);
- procedure SetSourceFile(AValue: string);
- procedure SetInternallyStored(const AValue: Boolean);
- procedure SetMipGenMode(const AValue: TMipmapGenerationMode);
- procedure SetUseStreaming(const AValue: Boolean);
- procedure PrepareImage;
- procedure FullTransfer;
- procedure StreamTransfer;
- procedure CalcLODRange(out AFirstLOD, ALastLOD: Integer);
- public
- constructor Create(AOwner: TXCollection); override;
- destructor Destroy; override;
- procedure Assign(Source: TPersistent); override;
- procedure NotifyChange(Sender: TObject); override;
- procedure DoOnPrepare(Sender: TGLContext); override;
- procedure Apply(var ARci: TGLRenderContextInfo); override;
- procedure UnApply(var ARci: TGLRenderContextInfo); override;
- class function FriendlyName: string; override;
- published
- // Factual texture properties
- property InternalWidth: Integer read FWidth;
- property InternalHeight: Integer read FHeight;
- property InternalDepth: Integer read FDepth;
- property InternalFormat: TGLInternalFormat read FInternalFormat
- write SetInternalFormat default tfRGBA8;
- (* Automatic Image Alpha setting.
- Allows to control how and if the image's Alpha channel (transparency)
- is computed. *)
- property ImageAlpha: TGLTextureImageAlpha read FImageAlpha write
- SetImageAlpha default tiaDefault;
- (* Texture brightness correction.
- This correction is applied upon loading a TGLTextureImage, it's a
- simple saturating scaling applied to the RGB components of
- the 32 bits image, before it is passed to OpenGL, and before
- gamma correction (if any). *)
- property ImageBrightness: Single read FImageBrightness write
- SetImageBrightness stored StoreBrightness;
- (*Texture gamma correction.
- The gamma correction is applied upon loading a TGLTextureImage,
- applied to the RGB components of the 32 bits image, before it is
- passed to OpenGL, after brightness correction (if any). *)
- property ImageGamma: Single read FImageGamma write SetImageGamma stored
- StoreGamma;
- (* Texture compression control.
- If True the compressed TextureFormat variant (the OpenGL ICD must
- support GL_ARB_texture_compression, or this option is ignored). *)
- property Compression: TGLTextureCompression read FCompression write
- SetCompression default tcDefault;
- (* Normal Map scaling.
- Force normal map generation from height map and controls
- the intensity of the bumps. *)
- property HeightToNormalScale: Single read FHeightToNormalScale
- write SetNormalMapScale stored StoreNormalMapScale;
- // Source file path and name.
- property SourceFile: string read FSourceFile write SetSourceFile;
- // Force to store image levels in separate files in ready to transfer format
- property InternallyStored: Boolean read FInternallyStored
- write SetInternallyStored default False;
- // Mipmap generation mode.
- property MipGenMode: TMipmapGenerationMode read FMipGenMode
- write SetMipGenMode default mgmOnFly;
- // Enable streaming loading.
- property UseStreaming: Boolean read FUseStreaming
- write SetUseStreaming default False;
- end;
- TGLFrameBufferAttachment = class(TGLAbstractTexture)
- protected
- procedure WriteToFiler(AWriter: TWriter); override;
- procedure ReadFromFiler(AReader: TReader); override;
- private
- FRenderBufferHandle: TGLRenderbufferHandle;
- FLayered: Boolean;
- FCubeMap: Boolean;
- FSamples: Integer;
- FOnlyWrite: Boolean;
- FFixedSamplesLocation: Boolean;
- procedure SetWidth(AValue: Integer);
- procedure SetHeight(AValue: Integer);
- procedure SetDepth(AValue: Integer);
- procedure SetInternalFormat(const AValue: TGLInternalFormat);
- procedure SetOnlyWrite(AValue: Boolean);
- procedure SetLayered(AValue: Boolean);
- procedure SetCubeMap(AValue: Boolean);
- procedure SetSamples(AValue: Integer);
- procedure SetFixedSamplesLocation(AValue: Boolean);
- public
- constructor Create(AOwner: TXCollection); override;
- destructor Destroy; override;
- procedure Assign(Source: TPersistent); override;
- procedure NotifyChange(Sender: TObject); override;
- procedure DoOnPrepare(Sender: TGLContext); override;
- procedure Apply(var ARci: TGLRenderContextInfo); override;
- procedure UnApply(var ARci: TGLRenderContextInfo); override;
- class function FriendlyName: string; override;
- published
- property InternalWidth: Integer read FWidth
- write SetWidth default 256;
- property InternalHeight: Integer read FHeight
- write SetHeight default 256;
- property InternalDepth: Integer read FDepth
- write SetDepth default 0;
- property InternalFormat: TGLInternalFormat read FInternalFormat
- write SetInternalFormat default tfRGBA8;
- (* This flag makes use render buffer as target which makes
- it impossible to read it as texture, but improves efficiency. *)
- property OnlyWrite: Boolean read FOnlyWrite
- write SetOnlyWrite default False;
- // Force targe be texture array.
- property Layered: Boolean read FLayered
- write SetLayered default False;
- // Force target be cube map.
- property CubeMap: Boolean read FCubeMap
- write SetCubeMap default False;
- // Number of samples. Positive value makes texture be multisample.
- property Samples: Integer read FSamples
- write SetSamples default -1;
- (* FixedSamplesLocation flag makes image will use identical
- sample locations and the same number of samples for all texels in
- the image, and the sample locations will not depend on the
- internalformat or size of the image. *)
- property FixedSamplesLocation: Boolean read FFixedSamplesLocation
- write SetFixedSamplesLocation default False;
- end;
- (* Swizzle the components of a texture fetches in
- shader or fixed-function pipeline. *)
- TGLTextureSwizzling = class(TGLUpdateAbleObject)
- private
- FSwizzles: TSwizzleVector;
- function GetSwizzle(AIndex: Integer): TGLTextureSwizzle;
- procedure SetSwizzle(AIndex: Integer; AValue: TGLTextureSwizzle);
- function StoreSwizzle(AIndex: Integer): Boolean;
- public
- constructor Create(AOwner: TPersistent); override;
- procedure Assign(Source: TPersistent); override;
- procedure WriteToFiler(AWriter: TWriter);
- procedure ReadFromFiler(AReader: TReader);
- published
- property RedFrom: TGLTextureSwizzle index 0 read GetSwizzle
- write SetSwizzle stored StoreSwizzle;
- property GreenFrom: TGLTextureSwizzle index 1 read GetSwizzle
- write SetSwizzle stored StoreSwizzle;
- property BlueFrom: TGLTextureSwizzle index 2 read GetSwizzle
- write SetSwizzle stored StoreSwizzle;
- property AlphaFrom: TGLTextureSwizzle index 3 read GetSwizzle
- write SetSwizzle stored StoreSwizzle;
- end;
- TGLTextureProperties = class(TGLLibMaterialProperty)
- private
- FLibTextureName: TGLMaterialComponentName;
- FLibSamplerName: TGLMaterialComponentName;
- FLibTexture: TGLAbstractTexture;
- FLibSampler: TGLTextureSampler;
- FTextureOffset, FTextureScale: TGLCoordinates;
- FTextureRotate: Single;
- FTextureMatrixIsIdentity: Boolean;
- FTextureOverride: Boolean;
- FTextureMatrix: TGLMatrix;
- FMappingMode: TGLTextureMappingMode;
- FEnvColor: TGLColor;
- FMapSCoordinates: TGLCoordinates4;
- FMapTCoordinates: TGLCoordinates4;
- FMapRCoordinates: TGLCoordinates4;
- FMapQCoordinates: TGLCoordinates4;
- FSwizzling: TGLTextureSwizzling;
- function GetLibTextureName: TGLMaterialComponentName;
- function GetLibSamplerName: TGLMaterialComponentName;
- procedure SetLibTextureName(const AValue: TGLMaterialComponentName);
- procedure SetLibSamplerName(const AValue: TGLMaterialComponentName);
- function GetTextureOffset: TGLCoordinates;
- procedure SetTextureOffset(const AValue: TGLCoordinates);
- function StoreTextureOffset: Boolean;
- function GetTextureScale: TGLCoordinates;
- procedure SetTextureScale(const AValue: TGLCoordinates);
- function StoreTextureScale: Boolean;
- procedure SetTextureMatrix(const AValue: TGLMatrix);
- procedure SetTextureRotate(AValue: Single);
- function StoreTextureRotate: Boolean;
- procedure SetMappingMode(const AValue: TGLTextureMappingMode);
- function GetMappingSCoordinates: TGLCoordinates4;
- procedure SetMappingSCoordinates(const AValue: TGLCoordinates4);
- function StoreMappingSCoordinates: Boolean;
- function GetMappingTCoordinates: TGLCoordinates4;
- procedure SetMappingTCoordinates(const AValue: TGLCoordinates4);
- function StoreMappingTCoordinates: Boolean;
- function GetMappingRCoordinates: TGLCoordinates4;
- procedure SetMappingRCoordinates(const AValue: TGLCoordinates4);
- function StoreMappingRCoordinates: Boolean;
- function GetMappingQCoordinates: TGLCoordinates4;
- procedure SetMappingQCoordinates(const AValue: TGLCoordinates4);
- function StoreMappingQCoordinates: Boolean;
- procedure SetSwizzling(const AValue: TGLTextureSwizzling);
- function StoreSwizzling: Boolean;
- procedure SetEnvColor(const AValue: TGLColor);
- procedure CalculateTextureMatrix;
- procedure ApplyMappingMode;
- procedure UnApplyMappingMode;
- protected
- procedure Loaded; override;
- public
- constructor Create(AOwner: TPersistent); override;
- destructor Destroy; override;
- procedure Assign(Source: TPersistent); override;
- procedure NotifyChange(Sender: TObject); override;
- procedure Notification(Sender: TObject; Operation: TOperation); override;
- function IsValid: Boolean;
- procedure Apply(var ARci: TGLRenderContextInfo);
- procedure UnApply(var ARci: TGLRenderContextInfo);
- property TextureMatrix: TGLMatrix read FTextureMatrix write SetTextureMatrix;
- published
- property LibTextureName: TGLMaterialComponentName read GetLibTextureName
- write SetLibTextureName;
- property LibSamplerName: TGLMaterialComponentName read GetLibSamplerName
- write SetLibSamplerName;
- property TextureOffset: TGLCoordinates read GetTextureOffset write
- SetTextureOffset stored StoreTextureOffset;
- (* Texture coordinates scaling.
- Scaling is applied before applying the offset, and is applied
- to the texture coordinates, meaning that a scale factor of (2, 2, 2)
- will make your texture look twice smaller. *)
- property TextureScale: TGLCoordinates read GetTextureScale write
- SetTextureScale stored StoreTextureScale;
- (* Texture coordinates rotating.
- Rotating is applied after applying offset and scale,
- and rotate ST direction around R axis. *)
- property TextureRotate: Single read FTextureRotate write
- SetTextureRotate stored StoreTextureRotate;
- // Texture Environment color.
- property EnvColor: TGLColor read FEnvColor write SetEnvColor;
- (* Texture coordinates mapping mode.
- This property controls automatic texture coordinates generation. *)
- property MappingMode: TGLTextureMappingMode read FMappingMode write
- SetMappingMode default tmmUser;
- (* Texture mapping coordinates mode for S, T, R and Q axis.
- This property stores the coordinates for automatic texture
- coordinates generation. *)
- property MappingSCoordinates: TGLCoordinates4 read GetMappingSCoordinates
- write SetMappingSCoordinates stored StoreMappingSCoordinates;
- property MappingTCoordinates: TGLCoordinates4 read GetMappingTCoordinates
- write SetMappingTCoordinates stored StoreMappingTCoordinates;
- property MappingRCoordinates: TGLCoordinates4 read GetMappingRCoordinates
- write SetMappingRCoordinates stored StoreMappingRCoordinates;
- property MappingQCoordinates: TGLCoordinates4 read GetMappingQCoordinates
- write SetMappingQCoordinates stored StoreMappingQCoordinates;
- // Texture color fetching parameters.
- property Swizzling: TGLTextureSwizzling read FSwizzling write
- SetSwizzling stored StoreSwizzling;
- end;
- TGLFixedFunctionProperties = class(TGLLibMaterialProperty)
- private
- FFrontProperties: TGLFaceProperties;
- FBackProperties: TGLFaceProperties;
- FDepthProperties: TGLDepthProperties;
- FBlendingMode: TGLBlendingMode;
- FBlendingParams: TGLBlendingParameters;
- FTexProp: TGLTextureProperties;
- FMaterialOptions: TGLMaterialOptions;
- FFaceCulling: TGLFaceCulling;
- FPolygonMode: TGLPolygonMode;
- FTextureMode: TGLTextureMode;
- function GetBackProperties: TGLFaceProperties;
- procedure SetBackProperties(AValues: TGLFaceProperties);
- procedure SetFrontProperties(AValues: TGLFaceProperties);
- procedure SetDepthProperties(AValues: TGLDepthProperties);
- procedure SetBlendingMode(const AValue: TGLBlendingMode);
- procedure SetMaterialOptions(const AValue: TGLMaterialOptions);
- procedure SetFaceCulling(const AValue: TGLFaceCulling);
- procedure SetPolygonMode(AValue: TGLPolygonMode);
- procedure SetBlendingParams(const AValue: TGLBlendingParameters);
- procedure SetTexProp(AValue: TGLTextureProperties);
- procedure SetTextureMode(AValue: TGLTextureMode);
- public
- constructor Create(AOwner: TPersistent); override;
- destructor Destroy; override;
- procedure Assign(Source: TPersistent); override;
- procedure Apply(var ARci: TGLRenderContextInfo);
- procedure UnApply(var ARci: TGLRenderContextInfo);
- // Returns True if the material is blended.
- function Blended: Boolean;
- published
- property MaterialOptions: TGLMaterialOptions read FMaterialOptions write
- SetMaterialOptions default [];
- property BackProperties: TGLFaceProperties read GetBackProperties write
- SetBackProperties;
- property FrontProperties: TGLFaceProperties read FFrontProperties write
- SetFrontProperties;
- property DepthProperties: TGLDepthProperties read FDepthProperties write
- SetDepthProperties;
- property BlendingMode: TGLBlendingMode read FBlendingMode write SetBlendingMode
- default bmOpaque;
- property BlendingParams: TGLBlendingParameters read FBlendingParams write
- SetBlendingParams;
- property FaceCulling: TGLFaceCulling read FFaceCulling write SetFaceCulling
- default fcBufferDefault;
- property PolygonMode: TGLPolygonMode read FPolygonMode write SetPolygonMode
- default pmFill;
- property Texture: TGLTextureProperties read FTexProp write SetTexProp;
- // Texture application mode.
- property TextureMode: TGLTextureMode read FTextureMode write SetTextureMode
- default tmDecal;
- // Next pass of FFP.
- property NextPass;
- end;
- TGLTextureCombiner = class(TGLBaseMaterialCollectionItem)
- protected
- procedure WriteToFiler(AWriter: TWriter); override;
- procedure ReadFromFiler(AReader: TReader); override;
- private
- FHandle: TGLVirtualHandle;
- FScript: TStringList;
- FCommandCache: TCombinerCache;
- procedure SetScript(AValue: TStringList);
- procedure DoAllocate(Sender: TGLVirtualHandle; var handle: Cardinal);
- procedure DoDeallocate(Sender: TGLVirtualHandle; var handle: Cardinal);
- public
- constructor Create(AOwner: TXCollection); override;
- destructor Destroy; override;
- procedure Assign(Source: TPersistent); override;
- procedure NotifyChange(Sender: TObject); override;
- procedure DoOnPrepare(Sender: TGLContext); override;
- class function FriendlyName: string; override;
- published
- property Script: TStringList read FScript write SetScript;
- end;
- TGLASMVertexProgram = class(TGLBaseMaterialCollectionItem)
- protected
- procedure WriteToFiler(AWriter: TWriter); override;
- procedure ReadFromFiler(AReader: TReader); override;
- private
- FHandle: TGLARBVertexProgramHandle;
- FSource: TStringList;
- FSourceFile: string;
- FInfoLog: string;
- procedure SetSource(AValue: TStringList);
- procedure SetSourceFile(AValue: string);
- function GetHandle: TGLARBVertexProgramHandle;
- public
- constructor Create(AOwner: TXCollection); override;
- destructor Destroy; override;
- procedure Assign(Source: TPersistent); override;
- procedure DoOnPrepare(Sender: TGLContext); override;
- class function FriendlyName: string; override;
- procedure NotifyChange(Sender: TObject); override;
- property Handle: TGLARBVertexProgramHandle read GetHandle;
- published
- property Source: TStringList read FSource write SetSource;
- property SourceFile: string read FSourceFile write SetSourceFile;
- property InfoLog: string read FInfoLog;
- end;
- TLightDir2TexEnvColor = (
- l2eNone,
- l2eEnvColor0,
- l2eEnvColor1,
- l2eEnvColor2,
- l2eEnvColor3
- );
- TGLMultitexturingProperties = class(TGLLibMaterialProperty)
- private
- FLibCombiner: TGLTextureCombiner;
- FLibAsmProg: TGLASMVertexProgram;
- FLibCombinerName: TGLMaterialComponentName;
- FLibAsmProgName: TGLMaterialComponentName;
- FTexProps: array[0..3] of TGLTextureProperties;
- FTextureMode: TGLTextureMode;
- FLightDir: TLightDir2TexEnvColor;
- FLightSourceIndex: Integer;
- function GetLibCombinerName: string;
- function GetLibAsmProgName: string;
- procedure SetLibCombinerName(const AValue: string);
- procedure SetLibAsmProgName(const AValue: string);
- function GetTexProps(AIndex: Integer): TGLTextureProperties;
- procedure SetTexProps(AIndex: Integer; AValue: TGLTextureProperties);
- procedure SetTextureMode(AValue: TGLTextureMode);
- procedure SetLightSourceIndex(AValue: Integer);
- protected
- procedure Loaded; override;
- public
- constructor Create(AOwner: TPersistent); override;
- destructor Destroy; override;
- procedure Notification(Sender: TObject; Operation: TOperation); override;
- function IsValid: Boolean;
- procedure Apply(var ARci: TGLRenderContextInfo);
- procedure UnApply(var ARci: TGLRenderContextInfo);
- published
- property LibCombinerName: string read GetLibCombinerName
- write SetLibCombinerName;
- property LibAsmProgName: string read GetLibAsmProgName
- write SetLibAsmProgName;
- property Texture0: TGLTextureProperties index 0 read GetTexProps write
- SetTexProps;
- property Texture1: TGLTextureProperties index 1 read GetTexProps write
- SetTexProps;
- property Texture2: TGLTextureProperties index 2 read GetTexProps write
- SetTexProps;
- property Texture3: TGLTextureProperties index 3 read GetTexProps write
- SetTexProps;
- // Texture application mode.
- property TextureMode: TGLTextureMode read FTextureMode write SetTextureMode
- default tmDecal;
- (* Pass light source direction to enviroment color of choosen texture.
- Vector in model space. *)
- property LightDirTo: TLightDir2TexEnvColor read FLightDir
- write FLightDir default l2eNone;
- // Specify index of light source for LightDirTo.
- property LightSourceIndex: Integer read FLightSourceIndex
- write SetLightSourceIndex default 0;
- // Next pass of combiner.
- property NextPass;
- end;
- TGLShaderType =
- (
- shtVertex,
- shtControl,
- shtEvaluation,
- shtGeometry,
- shtFragment
- );
- TGLShaderEx = class(TGLBaseMaterialCollectionItem)
- protected
- procedure WriteToFiler(AWriter: TWriter); override;
- procedure ReadFromFiler(AReader: TReader); override;
- private
- FHandle: array[TGLShaderType] of TGLShaderHandle;
- FSource: TStringList;
- FSourceFile: string;
- FShaderType: TGLShaderType;
- FInfoLog: string;
- FGeometryInput: TGLgsInTypes;
- FGeometryOutput: TGLgsOutTypes;
- FGeometryVerticesOut: Integer;
- procedure SetSource(AValue: TStringList);
- procedure SetSourceFile(AValue: string);
- procedure SetShaderType(AValue: TGLShaderType);
- procedure SetGeometryInput(AValue: TGLgsInTypes);
- procedure SetGeometryOutput(AValue: TGLgsOutTypes);
- procedure SetGeometryVerticesOut(AValue: Integer);
- function GetHandle: TGLShaderHandle;
- public
- constructor Create(AOwner: TXCollection); override;
- destructor Destroy; override;
- procedure Assign(Source: TPersistent); override;
- procedure DoOnPrepare(Sender: TGLContext); override;
- class function FriendlyName: string; override;
- procedure NotifyChange(Sender: TObject); override;
- property Handle: TGLShaderHandle read GetHandle;
- published
- property Source: TStringList read FSource write SetSource;
- property SourceFile: string read FSourceFile write SetSourceFile;
- property ShaderType: TGLShaderType read FShaderType
- write SetShaderType default shtVertex;
- property InfoLog: string read FInfoLog;
- property GeometryInput: TGLgsInTypes read FGeometryInput
- write SetGeometryInput default gsInPoints;
- property GeometryOutput: TGLgsOutTypes read FGeometryOutput
- write SetGeometryOutput default gsOutPoints;
- property GeometryVerticesOut: Integer read FGeometryVerticesOut
- write SetGeometryVerticesOut default 1;
- end;
- TGLAbstractShaderUniform = class(TGLUpdateAbleObject, IShaderParameter)
- protected
- FName: string;
- FNameHashCode: Integer;
- FType: TGLSLDataType;
- FSamplerType: TGLSLSamplerType;
- function GetName: string;
- function GetGLSLType: TGLSLDataType;
- function GetGLSLSamplerType: TGLSLSamplerType;
- function GetAutoSetMethod: string; virtual;
- function GetTextureName: string; virtual;
- function GetSamplerName: string; virtual;
- function GetTextureSwizzle: TSwizzleVector; virtual;
- procedure SetTextureName(const AValue: string); virtual;
- procedure SetSamplerName(const AValue: string); virtual;
- procedure SetAutoSetMethod(const AValue: string); virtual;
- procedure SetTextureSwizzle(const AValue: TSwizzleVector); virtual;
- function GetFloat: Single; virtual;
- function GetVec2: TVector2f; virtual;
- function GetVec3: TVector3f; virtual;
- function GetVec4: TGLVector; virtual;
- function GetInt: TGLint; virtual;
- function GetIVec2: TVector2i; virtual;
- function GetIVec3: TVector3i; virtual;
- function GetIVec4: TVector4i; virtual;
- function GetUInt: Cardinal; virtual;
- function GetUVec2: TVector2ui; virtual;
- function GetUVec3: TVector3ui; virtual;
- function GetUVec4: TVector4ui; virtual;
- procedure SetFloat(const Value: TGLFloat); virtual;
- procedure SetVec2(const Value: TVector2f); virtual;
- procedure SetVec3(const Value: TVector3f); virtual;
- procedure SetVec4(const Value: TVector4f); virtual;
- procedure SetInt(const Value: Integer); virtual;
- procedure SetIVec2(const Value: TVector2i); virtual;
- procedure SetIVec3(const Value: TVector3i); virtual;
- procedure SetIVec4(const Value: TVector4i); virtual;
- procedure SetUInt(const Value: Cardinal); virtual;
- procedure SetUVec2(const Value: TVector2ui); virtual;
- procedure SetUVec3(const Value: TVector3ui); virtual;
- procedure SetUVec4(const Value: TVector4ui); virtual;
- function GetMat2: TMatrix2f; virtual;
- function GetMat3: TMatrix3f; virtual;
- function GetMat4: TMatrix4f; virtual;
- procedure SetMat2(const Value: TMatrix2f); virtual;
- procedure SetMat3(const Value: TMatrix3f); virtual;
- procedure SetMat4(const Value: TMatrix4f); virtual;
- procedure SetFloatArray(const Values: PGLFloat; Count: Integer); virtual;
- procedure SetIntArray(const Values: PGLInt; Count: Integer); virtual;
- procedure SetUIntArray(const Values: PGLUInt; Count: Integer); virtual;
- procedure WriteToFiler(AWriter: TWriter); virtual;
- procedure ReadFromFiler(AReader: TReader); virtual;
- procedure Apply(var ARci: TGLRenderContextInfo); virtual;
- end;
- CGLAbstractShaderUniform = class of TGLAbstractShaderUniform;
- TGLShaderUniform = class(TGLAbstractShaderUniform, IShaderParameter)
- protected
-
- FLocation: Integer;
- FStoreProgram: Cardinal;
- FAutoSet: TUniformAutoSetMethod;
- function GetProgram: Cardinal; inline;
- procedure PushProgram; inline;
- procedure PopProgram; inline;
- function GetFloat: Single; override;
- function GetVec2: TVector2f; override;
- function GetVec3: TVector3f; override;
- function GetVec4: TGLVector; override;
- function GetInt: TGLInt; override;
- function GetIVec2: TVector2i; override;
- function GetIVec3: TVector3i; override;
- function GetIVec4: TVector4i; override;
- function GetUInt: Cardinal; override;
- function GetUVec2: TVector2ui; override;
- function GetUVec3: TVector3ui; override;
- function GetUVec4: TVector4ui; override;
- procedure SetFloat(const Value: TGLFloat); override;
- procedure SetVec2(const Value: TVector2f); override;
- procedure SetVec3(const Value: TVector3f); override;
- procedure SetVec4(const Value: TVector4f); override;
- procedure SetInt(const Value: Integer); override;
- procedure SetIVec2(const Value: TVector2i); override;
- procedure SetIVec3(const Value: TVector3i); override;
- procedure SetIVec4(const Value: TVector4i); override;
- procedure SetUInt(const Value: Cardinal); override;
- procedure SetUVec2(const Value: TVector2ui); override;
- procedure SetUVec3(const Value: TVector3ui); override;
- procedure SetUVec4(const Value: TVector4ui); override;
- function GetMat2: TMatrix2f; override;
- function GetMat3: TMatrix3f; override;
- function GetMat4: TMatrix4f; override;
- procedure SetMat2(const Value: TMatrix2f); override;
- procedure SetMat3(const Value: TMatrix3f); override;
- procedure SetMat4(const Value: TMatrix4f); override;
- function GetAutoSetMethod: string; override;
- procedure SetAutoSetMethod(const AValue: string); override;
- procedure WriteToFiler(AWriter: TWriter); override;
- procedure ReadFromFiler(AReader: TReader); override;
- public
- procedure SetFloatArray(const Values: PGLFloat; Count: Integer); override;
- procedure SetIntArray(const Values: PGLInt; Count: Integer); override;
- procedure SetUIntArray(const Values: PGLUInt; Count: Integer); override;
- procedure Assign(Source: TPersistent); override;
- procedure Apply(var ARci: TGLRenderContextInfo); override;
- property Name: string read GetName;
- property Location: Integer read FLocation;
- property GLSLType: TGLSLDataType read GetGLSLType;
- end;
- TGLShaderUniformDSA = class(TGLShaderUniform)
- protected
- procedure SetFloat(const Value: TGLFloat); override;
- procedure SetVec2(const Value: TVector2f); override;
- procedure SetVec3(const Value: TVector3f); override;
- procedure SetVec4(const Value: TVector4f); override;
- procedure SetInt(const Value: Integer); override;
- procedure SetIVec2(const Value: TVector2i); override;
- procedure SetIVec3(const Value: TVector3i); override;
- procedure SetIVec4(const Value: TVector4i); override;
- procedure SetUInt(const Value: Cardinal); override;
- procedure SetUVec2(const Value: TVector2ui); override;
- procedure SetUVec3(const Value: TVector3ui); override;
- procedure SetUVec4(const Value: TVector4ui); override;
- procedure SetMat2(const Value: TMatrix2f); override;
- procedure SetMat3(const Value: TMatrix3f); override;
- procedure SetMat4(const Value: TMatrix4f); override;
- public
- procedure SetFloatArray(const Values: PGLFloat; Count: Integer); override;
- procedure SetIntArray(const Values: PGLInt; Count: Integer); override;
- procedure SetUIntArray(const Values: PGLUInt; Count: Integer); override;
- end;
- TGLShaderUniformTexture = class(TGLShaderUniform)
- private
- FLibTexture: TGLAbstractTexture;
- FLibSampler: TGLTextureSampler;
- FTarget: TGLTextureTarget;
- FSwizzling: TSwizzleVector;
- protected
- FLibTexureName: TGLMaterialComponentName;
- FLibSamplerName: TGLMaterialComponentName;
- function GetTextureName: string; override;
- function GetSamplerName: string; override;
- function GetTextureSwizzle: TSwizzleVector; override;
- procedure SetTextureName(const AValue: string); override;
- procedure SetSamplerName(const AValue: string); override;
- procedure SetTextureSwizzle(const AValue: TSwizzleVector); override;
- procedure WriteToFiler(AWriter: TWriter); override;
- procedure ReadFromFiler(AReader: TReader); override;
- procedure Loaded;
- public
- constructor Create(AOwner: TPersistent); override;
- destructor Destroy; override;
- procedure Assign(Source: TPersistent); override;
- procedure Notification(Sender: TObject; Operation: TOperation); override;
- procedure Apply(var ARci: TGLRenderContextInfo); override;
- property LibTextureName: TGLMaterialComponentName read GetTextureName
- write SetTextureName;
- property LibSamplerName: TGLMaterialComponentName read GetSamplerName
- write SetSamplerName;
- property GLSLSampler: TGLSLSamplerType read GetGLSLSamplerType;
- property Swizzling: TSwizzleVector read GetTextureSwizzle write
- SetTextureSwizzle;
- end;
- TGLBaseShaderModel = class(TGLLibMaterialProperty)
- protected
- FHandle: TGLProgramHandle;
- FLibShaderName: array[TGLShaderType] of string;
- FShaders: array[TGLShaderType] of TGLShaderEx;
- FIsValid: Boolean;
- FInfoLog: string;
- FUniforms: TGLPersistentObjectList;
- FAutoFill: Boolean;
- function GetLibShaderName(AType: TGLShaderType): string;
- procedure SetLibShaderName(AType: TGLShaderType; const AValue: string);
- function GetUniform(const AName: string): IShaderParameter;
- class procedure ReleaseUniforms(AList: TGLPersistentObjectList);
- property LibVertexShaderName: TGLMaterialComponentName index shtVertex
- read GetLibShaderName write SetLibShaderName;
- property LibFragmentShaderName: TGLMaterialComponentName index shtFragment
- read GetLibShaderName write SetLibShaderName;
- property LibGeometryShaderName: TGLMaterialComponentName index shtGeometry
- read GetLibShaderName write SetLibShaderName;
- property LibTessEvalShaderName: TGLMaterialComponentName index shtEvaluation
- read GetLibShaderName write SetLibShaderName;
- property LibTessControlShaderName: TGLMaterialComponentName index shtControl
- read GetLibShaderName write SetLibShaderName;
- procedure DefineProperties(Filer: TFiler); override;
- procedure ReadUniforms(AStream: TStream);
- procedure WriteUniforms(AStream: TStream);
- procedure Loaded; override;
- class function IsSupported: Boolean; virtual; abstract;
- public
- constructor Create(AOwner: TPersistent); override;
- destructor Destroy; override;
- procedure Assign(Source: TPersistent); override;
- procedure NotifyChange(Sender: TObject); override;
- procedure Notification(Sender: TObject; Operation: TOperation); override;
- procedure DoOnPrepare(Sender: TGLContext);
- procedure Apply(var ARci: TGLRenderContextInfo); virtual;
- procedure UnApply(var ARci: TGLRenderContextInfo); virtual;
- procedure GetUniformNames(Proc: TGetStrProc);
- property Handle: TGLProgramHandle read FHandle;
- property IsValid: Boolean read FIsValid;
- property Uniforms[const AName: string]: IShaderParameter read GetUniform;
- published
- // Compilation info log for design time
- property InfoLog: string read FInfoLog;
- // Turn on autofill of uniforms
- property AutoFillOfUniforms: Boolean read FAutoFill
- write FAutoFill stored False;
- property NextPass;
- end;
- TGLShaderModel3 = class(TGLBaseShaderModel)
- public
- class function IsSupported: Boolean; override;
- published
- property LibVertexShaderName;
- property LibFragmentShaderName;
- end;
- TGLShaderModel4 = class(TGLBaseShaderModel)
- public
- class function IsSupported: Boolean; override;
- published
- property LibVertexShaderName;
- property LibGeometryShaderName;
- property LibFragmentShaderName;
- end;
- TGLShaderModel5 = class(TGLBaseShaderModel)
- public
- procedure Apply(var ARci: TGLRenderContextInfo); override;
- procedure UnApply(var ARci: TGLRenderContextInfo); override;
- class function IsSupported: Boolean; override;
- published
- property LibTessControlShaderName;
- property LibTessEvalShaderName;
- property LibVertexShaderName;
- property LibGeometryShaderName;
- property LibFragmentShaderName;
- end;
- TGLLibMaterialEx = class(TGLAbstractLibMaterial)
- private
- FHandle: TGLVirtualHandle;
- FApplicableLevel: TGLMaterialLevel;
- FSelectedLevel: TGLMaterialLevel;
- FFixedFunc: TGLFixedFunctionProperties;
- FMultitexturing: TGLMultitexturingProperties;
- FSM3: TGLShaderModel3;
- FSM4: TGLShaderModel4;
- FSM5: TGLShaderModel5;
- FOnAsmProgSetting: TOnAsmProgSetting;
- FOnSM3UniformInit: TOnUniformInitialize;
- FOnSM3UniformSetting: TOnUniformSetting;
- FOnSM4UniformInit: TOnUniformInitialize;
- FOnSM4UniformSetting: TOnUniformSetting;
- FOnSM5UniformInit: TOnUniformInitialize;
- FOnSM5UniformSetting: TOnUniformSetting;
- FNextPass: TGLLibMaterialEx;
- FStoreAmalgamating: Boolean;
- procedure SetLevel(AValue: TGLMaterialLevel);
- procedure SetFixedFunc(AValue: TGLFixedFunctionProperties);
- procedure SetMultitexturing(AValue: TGLMultitexturingProperties);
- procedure SetSM3(AValue: TGLShaderModel3);
- procedure SetSM4(AValue: TGLShaderModel4);
- procedure SetSM5(AValue: TGLShaderModel5);
- procedure DoAllocate(Sender: TGLVirtualHandle; var handle: Cardinal);
- procedure DoDeallocate(Sender: TGLVirtualHandle; var handle: Cardinal);
- protected
- procedure Loaded; override;
- procedure RemoveDefferedInit;
- procedure DoOnPrepare(Sender: TGLContext);
- public
- constructor Create(ACollection: TCollection); override;
- destructor Destroy; override;
- procedure Assign(Source: TPersistent); override;
- procedure NotifyChange(Sender: TObject); override;
- procedure Apply(var ARci: TGLRenderContextInfo); override;
- function UnApply(var ARci: TGLRenderContextInfo): Boolean; override;
- function Blended: Boolean; override;
- published
- property ApplicableLevel: TGLMaterialLevel read FApplicableLevel write SetLevel default mlAuto;
- property SelectedLevel: TGLMaterialLevel read FSelectedLevel;
- property FixedFunction: TGLFixedFunctionProperties read FFixedFunc write SetFixedFunc;
- property Multitexturing: TGLMultitexturingProperties read FMultitexturing write SetMultitexturing;
- property ShaderModel3: TGLShaderModel3 read FSM3 write SetSM3;
- property ShaderModel4: TGLShaderModel4 read FSM4 write SetSM4;
- property ShaderModel5: TGLShaderModel5 read FSM5 write SetSM5;
- // Asm vertex program event
- property OnAsmProgSetting: TOnAsmProgSetting read FOnAsmProgSetting
- write FOnAsmProgSetting;
- // Shader model 3 event
- property OnSM3UniformInitialize: TOnUniformInitialize read FOnSM3UniformInit
- write FOnSM3UniformInit;
- property OnSM3UniformSetting: TOnUniformSetting read FOnSM3UniformSetting
- write FOnSM3UniformSetting;
- // Shader model 4 event
- property OnSM4UniformInitialize: TOnUniformInitialize read FOnSM4UniformInit
- write FOnSM4UniformInit;
- property OnSM4UniformSetting: TOnUniformSetting read FOnSM4UniformSetting
- write FOnSM4UniformSetting;
- // Shader model 5 event
- property OnSM5UniformInitialize: TOnUniformInitialize read FOnSM5UniformInit
- write FOnSM5UniformInit;
- property OnSM5UniformSetting: TOnUniformSetting read FOnSM5UniformSetting
- write FOnSM5UniformSetting;
- end;
- TGLLibMaterialsEx = class(TGLAbstractLibMaterials)
- protected
- procedure SetItems(AIndex: Integer; const AValue: TGLLibMaterialEx);
- function GetItems(AIndex: Integer): TGLLibMaterialEx;
- public
- constructor Create(AOwner: TComponent);
- function MaterialLibrary: TGLMaterialLibraryEx;
- function IndexOf(const Item: TGLLibMaterialEx): Integer;
- function Add: TGLLibMaterialEx;
- function FindItemID(ID: Integer): TGLLibMaterialEx;
- property Items[index: Integer]: TGLLibMaterialEx read GetItems
- write SetItems; default;
- function GetLibMaterialByName(const AName: TGLLibMaterialName):
- TGLLibMaterialEx;
- end;
- TGLMatLibComponents = class(TXCollection)
- protected
- function GetItems(index: Integer): TGLBaseMaterialCollectionItem;
- public
- function GetNamePath: string; override;
- class function ItemsClass: TXCollectionItemClass; override;
- property Items[index: Integer]: TGLBaseMaterialCollectionItem
- read GetItems; default;
- function GetItemByName(const AName: TGLMaterialComponentName):
- TGLBaseMaterialCollectionItem;
- function GetTextureByName(const AName: TGLMaterialComponentName):
- TGLAbstractTexture;
- function GetAttachmentByName(const AName: TGLMaterialComponentName):
- TGLFrameBufferAttachment;
- function GetSamplerByName(const AName: TGLMaterialComponentName):
- TGLTextureSampler;
- function GetCombinerByName(const AName: TGLMaterialComponentName):
- TGLTextureCombiner;
- function GetShaderByName(const AName: TGLMaterialComponentName):
- TGLShaderEx;
- function GetAsmProgByName(const AName: TGLMaterialComponentName):
- TGLASMVertexProgram;
- function MakeUniqueName(const AName: TGLMaterialComponentName):
- TGLMaterialComponentName;
- end;
- TGLMaterialLibraryEx = class(TGLAbstractMaterialLibrary)
- private
- FComponents: TGLMatLibComponents;
- protected
- procedure Loaded; override;
- function GetMaterials: TGLLibMaterialsEx;
- procedure SetMaterials(AValue: TGLLibMaterialsEx);
- function StoreMaterials: Boolean;
- procedure SetComponents(AValue: TGLMatLibComponents);
- procedure DefineProperties(Filer: TFiler); override;
- procedure WriteComponents(AStream: TStream);
- procedure ReadComponents(AStream: TStream);
- public
- constructor Create(AOwner: TComponent); override;
- destructor Destroy; override;
- procedure GetNames(Proc: TGetStrProc;
- AClass: CGLBaseMaterialCollectionItem); overload;
- function AddTexture(const AName: TGLMaterialComponentName):
- TGLTextureImageEx;
- function AddAttachment(const AName: TGLMaterialComponentName):
- TGLFrameBufferAttachment;
- function AddSampler(const AName: TGLMaterialComponentName):
- TGLTextureSampler;
- function AddCombiner(const AName: TGLMaterialComponentName):
- TGLTextureCombiner;
- function AddShader(const AName: TGLMaterialComponentName): TGLShaderEx;
- function AddAsmProg(const AName: TGLMaterialComponentName):
- TGLASMVertexProgram;
- procedure SetLevelForAll(const ALevel: TGLMaterialLevel);
- published
- // The materials collection.
- property Materials: TGLLibMaterialsEx read GetMaterials write SetMaterials
- stored StoreMaterials;
- property Components: TGLMatLibComponents read FComponents
- write SetComponents;
- property TexturePaths;
- end;
- procedure RegisterGLMaterialExNameChangeEvent(AEvent: TNotifyEvent);
- procedure DeRegisterGLMaterialExNameChangeEvent(AEvent: TNotifyEvent);
- //=================================================================
- implementation
- //=================================================================
- const
- cTextureMagFilter: array[maNearest..maLinear] of Cardinal =
- (GL_NEAREST, GL_LINEAR);
- cTextureMinFilter: array[miNearest..miLinearMipmapLinear] of Cardinal =
- (GL_NEAREST, GL_LINEAR, GL_NEAREST_MIPMAP_NEAREST,
- GL_LINEAR_MIPMAP_NEAREST, GL_NEAREST_MIPMAP_LINEAR,
- GL_LINEAR_MIPMAP_LINEAR);
- cTextureWrapMode: array[twRepeat..twMirrorClampToBorder] of Cardinal =
- (GL_REPEAT, GL_CLAMP_TO_EDGE, GL_CLAMP_TO_BORDER,
- GL_MIRRORED_REPEAT, GL_MIRROR_CLAMP_TO_EDGE_ATI,
- GL_MIRROR_CLAMP_TO_BORDER_EXT);
- cTextureCompareMode: array[tcmNone..tcmCompareRtoTexture] of Cardinal =
- (GL_NONE, GL_COMPARE_R_TO_TEXTURE);
- cSamplerToTexture: array[TGLSLSamplerType] of TGLTextureTarget =
- (
- ttNoShape,
- ttTexture1D,
- ttTexture2D,
- ttTexture3D,
- ttTextureCube,
- ttTexture1D,
- ttTexture2D,
- ttTexture1DArray,
- ttTexture2DArray,
- ttTexture1DArray,
- ttTexture1DArray,
- ttTextureCube,
- ttTexture1D,
- ttTexture2D,
- ttTexture3D,
- ttTextureCube,
- ttTexture1DArray,
- ttTexture2DArray,
- ttTexture1D,
- ttTexture2D,
- ttTexture3D,
- ttTextureCube,
- ttTexture1DArray,
- ttTexture2DArray,
- ttTextureRect,
- ttTextureRect,
- ttTextureBuffer,
- ttTextureRect,
- ttTextureBuffer,
- ttTextureRect,
- ttTextureBuffer,
- ttTexture2DMultisample,
- ttTexture2DMultisample,
- ttTexture2DMultisample,
- ttTexture2DMultisampleArray,
- ttTexture2DMultisampleArray,
- ttTexture2DMultisample
- );
- cTextureSwizzle: array[TGLTextureSwizzle] of Cardinal =
- (
- GL_RED,
- GL_GREEN,
- GL_BLUE,
- GL_ALPHA,
- GL_ZERO,
- GL_ONE
- );
- const
- cTextureMode: array[TGLTextureMode] of Cardinal =
- (GL_DECAL, GL_MODULATE, GL_BLEND, GL_REPLACE, GL_ADD);
- const
- cShaderTypeName: array[TGLShaderType] of string =
- ('vertex', 'control', 'evaluation', 'geomtery', 'fragment');
- type
- TFriendlyImage = class(TGLBaseImage);
- TStandartUniformAutoSetExecutor = class
- public
- constructor Create;
- procedure SetModelMatrix(Sender: IShaderParameter; var ARci: TGLRenderContextInfo);
- procedure SetViewMatrix(Sender: IShaderParameter; var ARci: TGLRenderContextInfo);
- procedure SetProjectionMatrix(Sender: IShaderParameter; var ARci: TGLRenderContextInfo);
- procedure SetInvModelMatrix(Sender: IShaderParameter; var ARci: TGLRenderContextInfo);
- procedure SetModelViewMatrix(Sender: IShaderParameter; var ARci: TGLRenderContextInfo);
- procedure SetNormalModelMatrix(Sender: IShaderParameter; var ARci: TGLRenderContextInfo);
- procedure SetInvModelViewMatrix(Sender: IShaderParameter; var ARci: TGLRenderContextInfo);
- procedure SetViewProjectionMatrix(Sender: IShaderParameter; var ARci: TGLRenderContextInfo);
- procedure SetWorldViewProjectionMatrix(Sender: IShaderParameter; var ARci: TGLRenderContextInfo);
- procedure SetCameraPosition(Sender: IShaderParameter; var ARci: TGLRenderContextInfo);
- // Lighting
- procedure SetLightSource0Position(Sender: IShaderParameter; var ARci: TGLRenderContextInfo);
- // Material
- procedure SetMaterialFrontAmbient(Sender: IShaderParameter; var ARci: TGLRenderContextInfo);
- procedure SetMaterialFrontDiffuse(Sender: IShaderParameter; var ARci: TGLRenderContextInfo);
- procedure SetMaterialFrontSpecular(Sender: IShaderParameter; var ARci: TGLRenderContextInfo);
- procedure SetMaterialFrontEmission(Sender: IShaderParameter; var ARci: TGLRenderContextInfo);
- procedure SetMaterialFrontShininess(Sender: IShaderParameter; var ARci: TGLRenderContextInfo);
- procedure SetMaterialBackAmbient(Sender: IShaderParameter; var ARci: TGLRenderContextInfo);
- procedure SetMaterialBackDiffuse(Sender: IShaderParameter; var ARci: TGLRenderContextInfo);
- procedure SetMaterialBackSpecular(Sender: IShaderParameter; var ARci: TGLRenderContextInfo);
- procedure SetMaterialBackShininess(Sender: IShaderParameter; var ARci: TGLRenderContextInfo);
- procedure SetMaterialBackEmission(Sender: IShaderParameter; var ARci: TGLRenderContextInfo);
- end;
- var
- vGLMaterialExNameChangeEvent: TNotifyEvent;
- vStandartUniformAutoSetExecutor: TStandartUniformAutoSetExecutor;
- vStoreBegin: procedure(mode: Cardinal);{$IFDEF MSWINDOWS}stdcall;{$ELSE}cdecl;{$ENDIF}
- procedure RegisterGLMaterialExNameChangeEvent(AEvent: TNotifyEvent);
- begin
- vGLMaterialExNameChangeEvent := AEvent;
- end;
- procedure DeRegisterGLMaterialExNameChangeEvent(AEvent: TNotifyEvent);
- begin
- vGLMaterialExNameChangeEvent := nil;
- end;
- function ComputeNameHashKey(
- const AName: string): Integer;
- var
- i, n: Integer;
- begin
- n := Length(AName);
- Result := n;
- for i := 1 to n do
- Result := (Result shl 1) + Byte(AName[i]);
- end;
- procedure Div2(var Value: Integer); inline;
- begin
- Value := Value div 2;
- if Value = 0 then
- Value := 1;
- end;
- function CalcTextureLevelNumber(ATarget: TGLTextureTarget; w, h, d: Integer):
- Integer;
- begin
- Result := 0;
- case ATarget of
- ttNoShape: ;
- ttTexture1D, ttTexture1DArray, ttTextureCube, ttTextureCubeArray:
- repeat
- Inc(Result);
- Div2(w);
- until w <= 1;
- ttTexture2D, ttTexture2DArray:
- repeat
- Inc(Result);
- Div2(w);
- Div2(h);
- until (w <= 1) and (h <= 1);
- ttTexture3D:
- repeat
- Inc(Result);
- Div2(w);
- Div2(h);
- Div2(d);
- until (w <= 1) and (h <= 1) and (d <= 1);
- ttTextureRect, ttTextureBuffer,
- ttTexture2DMultisample, ttTexture2DMultisampleArray:
- Result := 1;
- end;
- end;
- destructor TGLBaseMaterialCollectionItem.Destroy;
- var
- I: Integer;
- begin
- if Assigned(FUserList) then
- begin
- FNotifying := True;
- for I := FUserList.Count - 1 downto 0 do
- TGLLibMaterialProperty(FUserList[I]).Notification(Self, opRemove);
- FreeAndNil(FUserList);
- end;
- inherited;
- end;
- function TGLBaseMaterialCollectionItem.GetMaterialLibrary:
- TGLAbstractMaterialLibrary;
- begin
- Result := TGLAbstractMaterialLibrary(TGLMatLibComponents(Owner).Owner);
- end;
- function TGLBaseMaterialCollectionItem.GetMaterialLibraryEx:
- TGLMaterialLibraryEx;
- begin
- Result := TGLMaterialLibraryEx(TGLMatLibComponents(Owner).Owner);
- end;
- function TGLBaseMaterialCollectionItem.GetUserCount: Integer;
- begin
- if Assigned(FUserList) then
- Result := FUserList.Count
- else
- Result := 0;
- end;
- function TGLBaseMaterialCollectionItem.GetUserList: TGLPersistentObjectList;
- begin
- if FUserList = nil then
- begin
- FUserList := TGLPersistentObjectList.Create;
- FNotifying := False;
- end;
- Result := FUserList;
- end;
- procedure TGLBaseMaterialCollectionItem.NotifyChange(Sender: TObject);
- var
- I: Integer;
- begin
- if FNotifying then
- exit;
- FNotifying := True;
- if GetUserCount > 0 then
- for I := 0 to FUserList.Count - 1 do
- TGLUpdateAbleObject(FUserList[I]).NotifyChange(Self);
- FNotifying := False;
- end;
- procedure TGLBaseMaterialCollectionItem.RegisterUser(
- AUser: TGLUpdateAbleObject);
- begin
- if not FNotifying and (UserList.IndexOf(AUser) < 0) then
- UserList.Add(AUser);
- end;
- procedure TGLBaseMaterialCollectionItem.UnregisterUser(
- AUser: TGLUpdateAbleObject);
- begin
- if not FNotifying then
- UserList.Remove(AUser);
- end;
- procedure TGLBaseMaterialCollectionItem.SetName(const AValue: string);
- begin
- if AValue <> Name then
- begin
- if not IsValidIdent(AValue) then
- begin
- if IsDesignTime then
- InformationDlg(AValue + ' - is not valid component name');
- exit;
- end;
- if not (csLoading in MaterialLibrary.ComponentState) then
- begin
- if TGLMatLibComponents(Owner).GetItemByName(AValue) <> Self then
- inherited SetName(TGLMatLibComponents(Owner).MakeUniqueName(AValue))
- else
- inherited SetName(AValue);
- end
- else
- inherited SetName(AValue);
- FNameHashKey := ComputeNameHashKey(Name);
- // Notify users
- NotifyChange(Self);
- // Notify designer
- if Assigned(vGLMaterialExNameChangeEvent) then
- vGLMaterialExNameChangeEvent(Self);
- end;
- end;
- procedure TGLFixedFunctionProperties.Apply(var ARci: TGLRenderContextInfo);
- begin
- with ARci.GLStates do
- begin
- Disable(stColorMaterial);
- PolygonMode := FPolygonMode;
- // Fixed functionality state
- if True{ not ARci.GLStates.ForwardContext} then
- begin
- // Lighting switch
- if (moNoLighting in MaterialOptions) or not ARci.bufferLighting then
- begin
- Disable(stLighting);
- FFrontProperties.ApplyNoLighting(ARci, cmFront);
- end
- else
- begin
- Enable(stLighting);
- FFrontProperties.Apply(ARci, cmFront);
- end;
- if FPolygonMode = pmLines then
- Disable(stLineStipple);
- // Fog switch
- if (moIgnoreFog in MaterialOptions) or not ARci.bufferFog then
- Disable(stFog)
- else
- Enable(stFog);
- end;
- // Apply FaceCulling and BackProperties (if needs be)
- case FFaceCulling of
- fcBufferDefault:
- begin
- if ARci.bufferFaceCull then
- Enable(stCullFace)
- else
- Disable(stCullFace);
- BackProperties.Apply(ARci, cmBack);
- end;
- fcCull: Enable(stCullFace);
- fcNoCull:
- begin
- Disable(stCullFace);
- BackProperties.Apply(ARci, cmBack);
- end;
- end;
- // note: Front + Back with different PolygonMode are no longer supported.
- // Currently state cache just ignores back facing mode changes, changes to
- // front affect both front + back PolygonMode
- // Apply Blending mode
- if not ARci.ignoreBlendingRequests then
- case FBlendingMode of
- bmOpaque:
- begin
- Disable(stBlend);
- Disable(stAlphaTest);
- end;
- bmTransparency:
- begin
- Enable(stBlend);
- Enable(stAlphaTest);
- SetBlendFunc(bfSrcAlpha, bfOneMinusSrcAlpha);
- SetGLAlphaFunction(cfGreater, 0);
- end;
- bmAdditive:
- begin
- Enable(stBlend);
- Enable(stAlphaTest);
- SetBlendFunc(bfSrcAlpha, bfOne);
- SetGLAlphaFunction(cfGreater, 0);
- end;
- bmAlphaTest50:
- begin
- Disable(stBlend);
- Enable(stAlphaTest);
- SetGLAlphaFunction(cfGEqual, 0.5);
- end;
- bmAlphaTest100:
- begin
- Disable(stBlend);
- Enable(stAlphaTest);
- SetGLAlphaFunction(cfGEqual, 1.0);
- end;
- bmModulate:
- begin
- Enable(stBlend);
- Enable(stAlphaTest);
- SetBlendFunc(bfDstColor, bfZero);
- SetGLAlphaFunction(cfGreater, 0);
- end;
- bmCustom:
- begin
- FBlendingParams.Apply(ARci);
- end;
- end;
- // Apply depth properties
- if not ARci.ignoreDepthRequests then
- FDepthProperties.Apply(ARci);
- // Apply texturing
- if ARci.currentMaterialLevel = mlFixedFunction then
- begin
- if FTexProp.Enabled and FTexProp.IsValid then
- begin
- ARci.GLStates.ActiveTexture := 0;
- FTexProp.Apply(ARci);
- gl.TexEnvi(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE,
- cTextureMode[FTextureMode]);
- end;
- end;
- end;
- end;
- procedure TGLFixedFunctionProperties.Assign(Source: TPersistent);
- var
- LFFP: TGLFixedFunctionProperties;
- begin
- if Source is TGLFixedFunctionProperties then
- begin
- LFFP := TGLFixedFunctionProperties(Source);
- if Assigned(LFFP.FBackProperties) then
- BackProperties.Assign(LFFP.BackProperties)
- else
- FreeAndNil(FBackProperties);
- FFrontProperties.Assign(LFFP.FFrontProperties);
- FPolygonMode := LFFP.FPolygonMode;
- FBlendingMode := LFFP.FBlendingMode;
- FMaterialOptions := LFFP.FMaterialOptions;
- FFaceCulling := LFFP.FFaceCulling;
- FDepthProperties.Assign(LFFP.FDepthProperties);
- FTexProp.Assign(LFFP.FTexProp);
- FTextureMode := LFFP.TextureMode;
- NotifyChange(Self);
- end;
- inherited;
- end;
- function TGLFixedFunctionProperties.Blended: Boolean;
- begin
- Result := not (FBlendingMode in [bmOpaque, bmAlphaTest50, bmAlphaTest100, bmCustom]);
- end;
- constructor TGLFixedFunctionProperties.Create(AOwner: TPersistent);
- begin
- inherited;
- FFrontProperties := TGLFaceProperties.Create(Self);
- FFaceCulling := fcBufferDefault;
- FPolygonMode := pmFill;
- FBlendingParams := TGLBlendingParameters.Create(Self);
- FDepthProperties := TGLDepthProperties.Create(Self);
- FTexProp := TGLTextureProperties.Create(Self);
- FTextureMode := tmDecal;
- FEnabled := True;
- end;
- destructor TGLFixedFunctionProperties.Destroy;
- begin
- FFrontProperties.Destroy;
- FBackProperties.Free;
- FDepthProperties.Destroy;
- FBlendingParams.Destroy;
- FTexProp.Destroy;
- inherited;
- end;
- function TGLFixedFunctionProperties.GetBackProperties: TGLFaceProperties;
- begin
- if not Assigned(FBackProperties) then
- FBackProperties := TGLFaceProperties.Create(Self);
- Result := FBackProperties;
- end;
- procedure TGLFixedFunctionProperties.SetBackProperties(AValues:
- TGLFaceProperties);
- begin
- BackProperties.Assign(AValues);
- NotifyChange(Self);
- end;
- procedure TGLFixedFunctionProperties.SetBlendingMode(const AValue:
- TGLBlendingMode);
- begin
- if AValue <> FBlendingMode then
- begin
- FBlendingMode := AValue;
- NotifyChange(Self);
- end;
- end;
- procedure TGLFixedFunctionProperties.SetBlendingParams(const AValue:
- TGLBlendingParameters);
- begin
- FBlendingParams.Assign(AValue);
- NotifyChange(Self);
- end;
- procedure TGLFixedFunctionProperties.SetDepthProperties(AValues:
- TGLDepthProperties);
- begin
- FDepthProperties.Assign(AValues);
- NotifyChange(Self);
- end;
- procedure TGLFixedFunctionProperties.SetTexProp(AValue: TGLTextureProperties);
- begin
- FTexProp.Assign(AValue);
- end;
- procedure TGLFixedFunctionProperties.SetTextureMode(AValue: TGLTextureMode);
- begin
- if AValue <> FTextureMode then
- begin
- FTextureMode := AValue;
- NotifyChange(Self);
- end;
- end;
- procedure TGLFixedFunctionProperties.SetFaceCulling(const AValue: TGLFaceCulling);
- begin
- if AValue <> FFaceCulling then
- begin
- FFaceCulling := AValue;
- NotifyChange(Self);
- end;
- end;
- procedure TGLFixedFunctionProperties.SetFrontProperties(AValues:
- TGLFaceProperties);
- begin
- FFrontProperties.Assign(AValues);
- NotifyChange(Self);
- end;
- procedure TGLFixedFunctionProperties.SetMaterialOptions(const AValue:
- TGLMaterialOptions);
- begin
- if AValue <> FMaterialOptions then
- begin
- FMaterialOptions := AValue;
- NotifyChange(Self);
- end;
- end;
- procedure TGLFixedFunctionProperties.SetPolygonMode(AValue: TGLPolygonMode);
- begin
- if AValue <> FPolygonMode then
- begin
- FPolygonMode := AValue;
- NotifyChange(Self);
- end;
- end;
- procedure TGLFixedFunctionProperties.UnApply(var ARci: TGLRenderContextInfo);
- begin
- if FTexProp.Enabled and FTexProp.IsValid then
- FTexProp.UnApply(ARci);
- end;
- function TGLAbstractTexture.GetTextureTarget: TGLTextureTarget;
- begin
- Result := FHandle.Target;
- end;
- procedure TGLTextureImageEx.Apply(var ARci: TGLRenderContextInfo);
- begin
- if FIsValid then
- begin
- // Just bind
- with ARci.GLStates do
- begin
- TextureBinding[ActiveTexture, FHandle.Target] := FHandle.Handle;
- ActiveTextureEnabled[FHandle.Target] := True;
- end;
- if not IsDesignTime then
- begin
- if not FUseStreaming and Assigned(FImage) then
- begin
- Inc(FApplyCounter);
- if FApplyCounter > 16 then
- FreeAndNil(FImage);
- end;
- if FUseStreaming then
- begin
- StreamTransfer;
- end;
- end;
- end
- else with ARci.GLStates do
- TextureBinding[ActiveTexture, FHandle.Target] := 0;
- end;
- procedure TGLTextureImageEx.Assign(Source: TPersistent);
- var
- LTexture: TGLTextureImageEx;
- begin
- if Source is TGLTextureImageEx then
- begin
- LTexture := TGLTextureImageEx(Source);
- FCompression := LTexture.FCompression;
- if Assigned(LTexture.FImage) then
- begin
- if not Assigned(FImage) then
- FImage := TGLImage.Create;
- FImage.Assign(LTexture.FImage);
- end
- else
- FreeAndNil(FImage);
- FImageAlpha := LTexture.FImageAlpha;
- FImageBrightness := LTexture.FImageBrightness;
- FImageGamma := LTexture.FImageGamma;
- FHeightToNormalScale := LTexture.FHeightToNormalScale;
- FSourceFile := LTexture.FSourceFile;
- NotifyChange(Self);
- end;
- inherited;
- end;
- constructor TGLTextureImageEx.Create(AOwner: TXCollection);
- begin
- inherited;
- FDefferedInit := False;
- FHandle := TGLTextureHandle.Create;
- FHandle.OnPrapare := DoOnPrepare;
- FCompression := tcDefault;
- FImageAlpha := tiaDefault;
- FImageBrightness := 1.0;
- FImageGamma := 1.0;
- FHeightToNormalScale := 1.0;
- FInternalFormat := tfRGBA8;
- FInternallyStored := False;
- FMipGenMode := mgmOnFly;
- FUseStreaming := False;
- Name := TGLMatLibComponents(AOwner).MakeUniqueName('Texture');
- end;
- destructor TGLTextureImageEx.Destroy;
- begin
- FHandle.Destroy;
- FImage.Free;
- inherited;
- end;
- procedure TGLTextureImageEx.NotifyChange(Sender: TObject);
- begin
- FHandle.NotifyChangesOfData;
- inherited;
- end;
- procedure TGLTextureImageEx.DoOnPrepare(Sender: TGLContext);
- var
- LTarget: TGLTextureTarget;
- rowSize: Integer;
- begin
- if IsDesignTime and FDefferedInit then
- exit;
- FHandle.AllocateHandle;
- if not FHandle.IsDataNeedUpdate then
- exit;
- try
- PrepareImage;
- // Target
- LTarget := FImage.GetTextureTarget;
- // Check supporting
- if not IsTargetSupported(LTarget)
- or not IsFormatSupported(FInternalFormat) then
- Abort;
- if (FHandle.Target <> LTarget)
- and (FHandle.Target <> ttNoShape) then
- begin
- FHandle.DestroyHandle;
- FHandle.AllocateHandle;
- end;
- FHandle.Target := LTarget;
- // Check streaming support
- if not IsDesignTime then
- begin
- FUseStreaming := FUseStreaming and TGLUnpackPBOHandle.IsSupported;
- FUseStreaming := FUseStreaming and IsServiceContextAvaible;
- FUseStreaming := FUseStreaming and (LTarget = ttTexture2D);
- end;
- with Sender.GLStates do
- begin
- ActiveTextureEnabled[FHandle.Target] := True;
- TextureBinding[ActiveTexture, FHandle.Target] := FHandle.Handle;
- UnpackRowLength := 0;
- UnpackSkipRows := 0;
- UnpackSkipPixels := 0;
- rowSize := FImage.LevelWidth[0] * FImage.ElementSize;
- if (rowSize mod 8 = 0) and (FImage.ElementSize > 4) then
- UnpackAlignment := 8
- else
- if rowSize mod 4 = 0 then
- UnpackAlignment := 4
- else if rowSize mod 2 = 0 then
- UnpackAlignment := 2
- else
- UnpackAlignment := 1;
- end;
- if not IsDesignTime and FUseStreaming then
- begin
- TFriendlyImage(FImage).StartStreaming;
- FLastTime := AppTime;
- StreamTransfer;
- FHandle.NotifyDataUpdated;
- end
- else
- FullTransfer;
- Sender.GLStates.ActiveTextureEnabled[FHandle.Target] := False;
- FApplyCounter := 0;
- FIsValid := True;
- except
- FIsValid := False;
- end;
- end;
- procedure TGLTextureImageEx.FullTransfer;
- var
- LCompression: TGLTextureCompression;
- glFormat: Cardinal;
- begin
- begin
- if GL.ARB_texture_compression then
- begin
- if Compression = tcDefault then
- if vDefaultTextureCompression = tcDefault then
- LCompression := tcNone
- else
- LCompression := vDefaultTextureCompression
- else
- LCompression := Compression;
- end
- else
- LCompression := tcNone;
- if LCompression <> tcNone then
- with CurrentGLContext.GLStates do
- begin
- case LCompression of
- tcStandard: TextureCompressionHint := hintDontCare;
- tcHighQuality: TextureCompressionHint := hintNicest;
- tcHighSpeed: TextureCompressionHint := hintFastest;
- else
- Assert(False, strErrorEx + strUnknownType);
- end;
- if not GetGenericCompressedFormat(
- FInternalFormat,
- FImage.ColorFormat, glFormat) then
- glFormat := InternalFormatToOpenGLFormat(FInternalFormat);
- end
- else
- glFormat := InternalFormatToOpenGLFormat(FInternalFormat);
- FImage.RegisterAsOpenGLTexture(
- FHandle,
- FMipGenMode = mgmOnFly,
- glFormat,
- FWidth,
- FHeight,
- FDepth);
- if gl.GetError <> GL_NO_ERROR then
- begin
- gl.ClearError;
- CurrentGLContext.GLStates.ActiveTextureEnabled[FHandle.Target] := False;
- GLSLogger.LogErrorFmt('Unable to create texture "%s"', [Self.Name]);
- Abort;
- end
- else
- FHandle.NotifyDataUpdated;
- end;
- end;
- procedure TGLTextureImageEx.CalcLODRange(out AFirstLOD, ALastLOD: Integer);
- var
- I, MaxLODSize, MinLODSize, MaxLODZSize: Integer;
- begin
- case FHandle.Target of
- ttTexture3D:
- begin
- MaxLODSize := CurrentGLContext.GLStates.Max3DTextureSize;
- MaxLODZSize := MaxLODSize;
- end;
- ttTextureCube:
- begin
- MaxLODSize := CurrentGLContext.GLStates.MaxCubeTextureSize;
- MaxLODZSize := 0;
- end;
- ttTexture1DArray,
- ttTexture2DArray,
- ttTextureCubeArray,
- ttTexture2DMultisampleArray:
- begin
- MaxLODSize := CurrentGLContext.GLStates.MaxTextureSize;
- MaxLODZSize := CurrentGLContext.GLStates.MaxArrayTextureSize;
- end;
- else
- begin
- MaxLODSize := CurrentGLContext.GLStates.MaxTextureSize;
- MaxLODZSize := 0;
- end;
- end;
- MinLODSize := 1;
- AFirstLOD := 0;
- for I := 0 to High(TGLImagePiramid) do
- begin
- if (FImage.LevelWidth[I] <= MaxLODSize)
- and (FImage.LevelHeight[I] <= MaxLODSize)
- and (FImage.LevelDepth[I] <= MaxLODZSize) then
- break;
- Inc(AFirstLOD);
- end;
- AFirstLOD := MinInteger(AFirstLOD, FImage.LevelCount - 1);
- ALastLOD := AFirstLOD;
- for I := AFirstLOD to High(TGLImagePiramid) do
- begin
- if (FImage.LevelWidth[I] < MinLODSize)
- or (FImage.LevelHeight[I] < MinLODSize) then
- break;
- Inc(ALastLOD);
- end;
- ALastLOD := MinInteger(ALastLOD, FImage.LevelCount - 1);
- end;
- procedure TGLTextureImageEx.StreamTransfer;
- var
- LImage: TFriendlyImage;
- bContinueStreaming: Boolean;
- OldBaseLevel, level: Integer;
- newTime: Double;
- glInternalFormat: Cardinal;
- transferMethod: 0..3;
- begin
- LImage := TFriendlyImage(FImage);
- OldBaseLevel := FBaseLevel;
- CalcLODRange(FBaseLevel, FMaxLevel);
- // Select transfer method
- if FImage.IsCompressed then
- transferMethod := 1
- else
- transferMethod := 0;
- if gl.EXT_direct_state_access then
- transferMethod := transferMethod + 2;
- bContinueStreaming := False;
- for level := FMaxLevel downto FBaseLevel do
- begin
- case LImage.LevelStreamingState[level] of
- ssKeeping:
- begin
- if FBaseLevel < Level then
- FBaseLevel := FMaxLevel;
- LImage.LevelStreamingState[Level] := ssLoading;
- LImage.DoStreaming;
- bContinueStreaming := True;
- end;
- ssLoading:
- begin
- LImage.DoStreaming;
- bContinueStreaming := True;
- if FBaseLevel < Level then
- FBaseLevel := FMaxLevel;
- end;
- ssLoaded:
- begin
- LImage.LevelPixelBuffer[Level].AllocateHandle;
- LImage.LevelPixelBuffer[Level].Bind;
- glInternalFormat := InternalFormatToOpenGLFormat(FInternalFormat);
- case transferMethod of
- 0: gl.TexImage2D(GL_TEXTURE_2D, Level, glInternalFormat, FImage.LevelWidth[level], FImage.LevelHeight[level], 0, FImage.ColorFormat, FImage.DataType, nil);
- 1: gl.CompressedTexImage2D(GL_TEXTURE_2D, Level, glInternalFormat, FImage.LevelWidth[level], FImage.LevelHeight[level], 0, FImage.LevelSizeInByte[Level], nil);
- 2: gl.TextureImage2D(FHandle.Handle, GL_TEXTURE_2D, Level, glInternalFormat, FImage.LevelWidth[level], FImage.LevelHeight[level], 0, FImage.ColorFormat, FImage.DataType, nil);
- 3: gl.CompressedTextureImage2D(FHandle.Handle, GL_TEXTURE_2D, Level, glInternalFormat, FImage.LevelWidth[level], FImage.LevelHeight[level], 0, FImage.LevelSizeInByte[Level], nil);
- end;
- LImage.LevelPixelBuffer[Level].UnBind;
- LImage.LevelStreamingState[Level] := ssTransfered;
- GLSLogger.LogDebug(Format('Texture "%s" level %d loaded', [Name, Level]));
- end;
- ssTransfered:
- begin
- if LImage.LevelPixelBuffer[Level].IsAllocatedForContext then
- LImage.LevelPixelBuffer[Level].DestroyHandle;
- FBaseLevel := Level;
- end;
- end; // of case
- if bContinueStreaming then
- break;
- end; // for level
- if bContinueStreaming then
- begin
- gl.TexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAX_LEVEL, FMaxLevel);
- gl.TexParameteri(GL_TEXTURE_2D, GL_TEXTURE_BASE_LEVEL, FBaseLevel);
- end;
- // Smooth transition between levels
- if Assigned(FApplicableSampler) then
- with FApplicableSampler do
- begin
- newTime := AppTime;
- if FLODBiasFract > 0 then
- FLODBiasFract := FLODBiasFract - 0.05 * (newTime - FLastTime)
- else if FLODBiasFract < 0 then
- FLODBiasFract := 0;
- FLastTime := newTime;
- if OldBaseLevel > FBaseLevel then
- FLODBiasFract := FLODBiasFract + (OldBaseLevel - FBaseLevel);
- if FApplicableSampler.IsValid then
- gl.SamplerParameterf(FApplicableSampler.Handle.Handle,
- GL_TEXTURE_LOD_BIAS, FLODBias + FLODBiasFract)
- else
- // To refrash texture parameters when sampler object not supported
- FLastSampler := nil;
- end;
- end;
- class function TGLTextureImageEx.FriendlyName: string;
- begin
- Result := 'Texture Image';
- end;
- procedure TGLTextureImageEx.PrepareImage;
- const
- cAlphaProc: array[TGLTextureImageAlpha] of TImageAlphaProc =
- (
- nil,
- ImageAlphaFromIntensity,
- ImageAlphaSuperBlackTransparent,
- ImageAlphaLuminance,
- ImageAlphaLuminanceSqrt,
- ImageAlphaOpaque,
- ImageAlphaTopLeftPointColorTransparent,
- ImageAlphaInverseLuminance,
- ImageAlphaInverseLuminanceSqrt,
- ImageAlphaBottomRightPointColorTransparent
- );
- var
- ext, filename: string;
- BaseImageClass: TGLBaseImageClass;
- LPicture: TPicture;
- LGraphic: TGraphic;
- LImage: TGLImage;
- level: Integer;
- glColorFormat, glDataType: Cardinal;
- bReadFromSource: Boolean;
- LStream: TStream;
- ptr: PByte;
- procedure ReplaceImageClass;
- begin
- if not (FImage is TGLImage) then
- begin
- LImage := TGLImage.Create;
- LImage.Assign(FImage);
- FImage.Destroy;
- FImage := LImage;
- end
- else
- LImage := TGLImage(FImage);
- end;
- begin
- if not Assigned(FImage) then
- begin
- try
- SetExeDirectory;
- bReadFromSource := True;
- if FInternallyStored and not IsDesignTime then
- begin
- filename := Name+'.image';
- if FileStreamExists(filename) then
- begin
- FImage := TGLImage.Create;
- FImage.ResourceName := filename;
- TFriendlyImage(FImage).LoadHeader;
- if not FUseStreaming then
- begin
- ReallocMem(TFriendlyImage(FImage).fData, FImage.DataSize);
- for level := FImage.LevelCount - 1 downto 0 do
- begin
- LStream := TFileStream.Create(filename + IntToHex(level, 2), fmOpenRead);
- ptr := PByte(TFriendlyImage(FImage).GetLevelAddress(level));
- LStream.Read(ptr^, FImage.LevelSizeInByte[level]);
- LStream.Destroy;
- end;
- end;
- bReadFromSource := False;
- end
- else
- begin
- FInternallyStored := False;
- FUseStreaming := False;
- end;
- end;
- if bReadFromSource then
- begin
- if (Length(FSourceFile) > 0) and FileStreamExists(FSourceFile) then
- begin
- // At first check moder image file loaders
- ext := ExtractFileExt(FSourceFile);
- System.Delete(ext, 1, 1);
- BaseImageClass := GetRasterFileFormats.FindExt(ext);
- if Assigned(BaseImageClass) then
- begin
- FImage := BaseImageClass.Create;
- FImage.LoadFromFile(FSourceFile);
- end
- else
- begin
- // Check old loaders
- FImage := TGLImage.Create;
- if ApplicationFileIODefined then
- begin
- LGraphic := CreateGraphicFromFile(FSourceFile);
- FImage.Assign(LGraphic);
- LGraphic.Free;
- end
- else
- begin
- LPicture := TPicture.Create;
- LPicture.LoadFromFile(FSourceFile);
- FImage.Assign(LPicture.Graphic);
- LPicture.Destroy;
- end;
- end;
- if FInternalFormat <> FImage.InternalFormat then
- begin
- ReplaceImageClass;
- FindCompatibleDataFormat(FInternalFormat, glColorFormat, glDataType);
- TGLImage(FImage).SetColorFormatDataType(glColorFormat, glDataType);
- TFriendlyImage(FImage).fInternalFormat := FInternalFormat;
- end;
- if (ImageAlpha <> tiaDefault)
- or (FImageBrightness <> 1.0)
- or (FImageGamma <> 1.0) then
- begin
- ReplaceImageClass;
- for level := 0 to FImage.LevelCount - 1 do
- begin
- AlphaGammaBrightCorrection(
- TFriendlyImage(FImage).GetLevelAddress(level),
- FImage.ColorFormat,
- FImage.DataType,
- FImage.LevelWidth[level],
- FImage.LevelHeight[level],
- cAlphaProc[ImageAlpha],
- FImageBrightness,
- FImageGamma);
- end;
- end
- else if FHeightToNormalScale <> 1.0 then
- begin
- ReplaceImageClass;
- // HeightToNormalMap();
- {$Message Hint 'TGLTextureImageEx.HeightToNormalScale not yet implemented' }
- end;
- case FMipGenMode of
- mgmNoMip:
- FImage.UnMipmap;
- mgmLeaveExisting, mgmOnFly: ;
- mgmBoxFilter:
- FImage.GenerateMipmap(ImageBoxFilter);
- mgmTriangleFilter:
- FImage.GenerateMipmap(ImageTriangleFilter);
- mgmHermiteFilter:
- FImage.GenerateMipmap(ImageHermiteFilter);
- mgmBellFilter:
- FImage.GenerateMipmap(ImageBellFilter);
- mgmSplineFilter:
- FImage.GenerateMipmap(ImageSplineFilter);
- mgmLanczos3Filter:
- FImage.GenerateMipmap(ImageLanczos3Filter);
- mgmMitchellFilter:
- FImage.GenerateMipmap(ImageMitchellFilter);
- end;
- // Store cooked image
- if FInternallyStored and IsDesignTime then
- begin
- filename := Name+'.image';
- FImage.ResourceName := filename;
- TFriendlyImage(FImage).SaveHeader;
- for level := FImage.LevelCount - 1 downto 0 do
- begin
- LStream := TFileStream.Create(filename + IntToHex(level, 2),
- fmOpenWrite or fmCreate);
- ptr := PByte(TFriendlyImage(FImage).GetLevelAddress(level));
- LStream.Write(ptr^, FImage.LevelSizeInByte[level]);
- LStream.Destroy;
- end;
- end;
- end
- else
- begin // no SourceFile
- FImage := TGLImage.Create;
- FImage.SetErrorImage;
- GLSLogger.LogErrorFmt('Source file of texture "%s" image not found',
- [Self.Name]);
- end;
- end; // if bReadFromSource
- except
- on E: Exception do
- begin
- FImage.Free;
- FImage := TGLImage.Create;
- FImage.SetErrorImage;
- if IsDesignTime then
- InformationDlg(Self.Name + ' - ' + E.ClassName + ': ' + E.Message)
- else
- GLSLogger.LogError(Self.Name + ' - ' + E.ClassName + ': ' +
- E.Message);
- end;
- end;
- end; // of not Assigned
- end;
- procedure TGLTextureImageEx.ReadFromFiler(AReader: TReader);
- var
- archiveVersion: Integer;
- begin
- with AReader do
- begin
- archiveVersion := ReadInteger;
- if archiveVersion = 0 then
- begin
- Name := ReadString;
- FDefferedInit := ReadBoolean;
- FInternalFormat := TGLInternalFormat(ReadInteger);
- FCompression := TGLTextureCompression(ReadInteger);
- FImageAlpha := TGLTextureImageAlpha(ReadInteger);
- FImageBrightness := ReadFloat;
- FImageBrightness := ReadFloat;
- FImageGamma := ReadFloat;
- FHeightToNormalScale := ReadFloat;
- FSourceFile := ReadString;
- FInternallyStored := ReadBoolean;
- FMipGenMode := TMipmapGenerationMode(ReadInteger);
- FUseStreaming := ReadBoolean;
- end
- else
- RaiseFilerException(archiveVersion);
- end;
- end;
- procedure TGLTextureImageEx.SetCompression(const AValue: TGLTextureCompression);
- begin
- if AValue <> FCompression then
- begin
- FCompression := AValue;
- NotifyChange(Self);
- end;
- end;
- procedure TGLTextureImageEx.SetImageAlpha(const AValue: TGLTextureImageAlpha);
- begin
- if FImageAlpha <> AValue then
- begin
- FImageAlpha := AValue;
- FreeAndNil(FImage);
- NotifyChange(Self);
- end;
- end;
- procedure TGLTextureImageEx.SetImageBrightness(const AValue: Single);
- begin
- if FImageBrightness <> AValue then
- begin
- FImageBrightness := AValue;
- FreeAndNil(FImage);
- NotifyChange(Self);
- end;
- end;
- procedure TGLTextureImageEx.SetImageGamma(const AValue: Single);
- begin
- if FImageGamma <> AValue then
- begin
- FImageGamma := AValue;
- FreeAndNil(FImage);
- NotifyChange(Self);
- end;
- end;
- procedure TGLTextureImageEx.SetInternalFormat(const AValue: TGLInternalFormat);
- begin
- if AValue <> FInternalFormat then
- begin
- FInternalFormat := AValue;
- FreeAndNil(FImage);
- NotifyChange(Self);
- end;
- end;
- procedure TGLTextureImageEx.SetInternallyStored(const AValue: Boolean);
- begin
- if FInternallyStored <> AValue then
- begin
- FInternallyStored := AValue;
- if not AValue then
- FUseStreaming := AValue
- else
- FreeAndNil(FImage);
- NotifyChange(Self);
- end;
- end;
- procedure TGLTextureImageEx.SetMipGenMode(const AValue: TMipmapGenerationMode);
- begin
- if FMipGenMode <> AValue then
- begin
- FMipGenMode := AValue;
- FreeAndNil(FImage);
- NotifyChange(Self);
- end;
- end;
- procedure TGLTextureImageEx.SetNormalMapScale(const AValue: Single);
- begin
- if AValue <> FHeightToNormalScale then
- begin
- FHeightToNormalScale := AValue;
- NotifyChange(Self);
- end;
- end;
- procedure TGLTextureImageEx.SetSourceFile(AValue: string);
- begin
- FixPathDelimiter(AValue);
- if FSourceFile <> AValue then
- begin
- FSourceFile := AValue;
- FUseStreaming := False;
- FreeAndNil(FImage);
- NotifyChange(Self);
- end;
- end;
- procedure TGLTextureImageEx.SetUseStreaming(const AValue: Boolean);
- begin
- if AValue <> FUseStreaming then
- begin
- if AValue then
- begin
- if not Assigned(FImage) then
- exit;
- if FImage.LevelCount = 1 then
- begin
- if IsDesignTime then
- InformationDlg('Image must be more than one level');
- exit;
- end;
- FInternallyStored := True;
- end;
- FUseStreaming := AValue;
- NotifyChange(Self);
- end;
- end;
- function TGLTextureImageEx.StoreBrightness: Boolean;
- begin
- Result := (FImageBrightness <> 1.0);
- end;
- function TGLTextureImageEx.StoreGamma: Boolean;
- begin
- Result := (FImageGamma <> 1.0);
- end;
- function TGLTextureImageEx.StoreNormalMapScale: Boolean;
- begin
- Result := (FHeightToNormalScale <> cDefaultNormalMapScale);
- end;
- procedure TGLTextureImageEx.UnApply(var ARci: TGLRenderContextInfo);
- begin
- ARci.GLStates.ActiveTextureEnabled[FHandle.Target] := False;
- end;
- procedure TGLTextureImageEx.WriteToFiler(AWriter: TWriter);
- begin
- with AWriter do
- begin
- WriteInteger(0); // archive version
- WriteString(Name);
- WriteBoolean(FDefferedInit);
- WriteInteger(Integer(FInternalFormat));
- WriteInteger(Integer(FCompression));
- WriteInteger(Integer(FImageAlpha));
- WriteFloat(FImageBrightness);
- WriteFloat(FImageBrightness);
- WriteFloat(FImageGamma);
- WriteFloat(FHeightToNormalScale);
- WriteString(FSourceFile);
- WriteBoolean(FInternallyStored);
- WriteInteger(Integer(FMipGenMode));
- WriteBoolean(FUseStreaming);
- end;
- end;
- procedure TGLTextureSampler.Apply(var ARci: TGLRenderContextInfo);
- begin
- if FIsValid then
- ARci.GLStates.SamplerBinding[ARci.GLStates.ActiveTexture] := FHandle.Handle;
- end;
- procedure TGLTextureSampler.Assign(Source: TPersistent);
- var
- LSampler: TGLTextureSampler;
- begin
- if Source is TGLTextureSampler then
- begin
- LSampler := TGLTextureSampler(Source);
- FMinFilter := LSampler.FMinFilter;
- FMagFilter := LSampler.FMagFilter;
- FFilteringQuality := LSampler.FFilteringQuality;
- FLODBias := LSampler.FLODBias;
- FLODBiasFract := 0;
- FBorderColor.Assign(LSampler.FBorderColor);
- FWrap := LSampler.FWrap;
- FCompareMode := LSampler.FCompareMode;
- FCompareFunc := LSampler.FCompareFunc;
- FDecodeSRGB := LSampler.FDecodeSRGB;
- NotifyChange(Self);
- end;
- inherited;
- end;
- constructor TGLTextureSampler.Create(AOwner: TXCollection);
- begin
- inherited;
- FDefferedInit := False;
- FHandle := TGLSamplerHandle.Create;
- FHandle.OnPrapare := DoOnPrepare;
- FMagFilter := maLinear;
- FMinFilter := miLinearMipMapLinear;
- FFilteringQuality := tfAnisotropic;
- FLODBias := 0;
- FLODBiasFract := 0;
- FWrap[0] := twRepeat;
- FWrap[1] := twRepeat;
- FWrap[2] := twRepeat;
- FBorderColor := TGLColor.CreateInitialized(Self, clrTransparent);
- FCompareMode := tcmNone;
- FCompareFunc := cfLequal;
- FDecodeSRGB := True;
- Name := TGLMatLibComponents(AOwner).MakeUniqueName('Sampler');
- end;
- destructor TGLTextureSampler.Destroy;
- begin
- FHandle.Destroy;
- FBorderColor.Destroy;
- inherited;
- end;
- function TGLTextureSampler.GetWrap(Index: Integer): TGLSeparateTextureWrap;
- begin
- Result := FWrap[Index];
- end;
- procedure TGLTextureSampler.NotifyChange(Sender: TObject);
- begin
- FHandle.NotifyChangesOfData;
- inherited;
- end;
- procedure TGLTextureSampler.DoOnPrepare(Sender: TGLContext);
- var
- ID: Cardinal;
- begin
- if IsDesignTime and FDefferedInit then
- exit;
- try
- if FHandle.IsSupported then
- begin
- FHandle.AllocateHandle;
- ID := FHandle.Handle;
- if FHandle.IsDataNeedUpdate then
- with Sender.GL do
- begin
- SamplerParameterfv(ID, GL_TEXTURE_BORDER_COLOR, FBorderColor.AsAddress);
- SamplerParameteri(ID, GL_TEXTURE_WRAP_S, cTextureWrapMode[FWrap[0]]);
- SamplerParameteri(ID, GL_TEXTURE_WRAP_T, cTextureWrapMode[FWrap[1]]);
- SamplerParameteri(ID, GL_TEXTURE_WRAP_R, cTextureWrapMode[FWrap[2]]);
- SamplerParameterf(ID, GL_TEXTURE_LOD_BIAS, FLODBias + FLODBiasFract);
- SamplerParameteri(ID, GL_TEXTURE_MIN_FILTER,
- cTextureMinFilter[FMinFilter]);
- SamplerParameteri(ID, GL_TEXTURE_MAG_FILTER,
- cTextureMagFilter[FMagFilter]);
- if EXT_texture_filter_anisotropic then
- begin
- if FFilteringQuality = tfAnisotropic then
- SamplerParameteri(ID, GL_TEXTURE_MAX_ANISOTROPY_EXT,
- CurrentGLContext.GLStates.MaxTextureAnisotropy)
- else
- SamplerParameteri(ID, GL_TEXTURE_MAX_ANISOTROPY_EXT, 1);
- end;
- SamplerParameteri(ID, GL_TEXTURE_COMPARE_MODE,
- cTextureCompareMode[FCompareMode]);
- SamplerParameteri(ID, GL_TEXTURE_COMPARE_FUNC,
- cGLComparisonFunctionToGLEnum[FCompareFunc]);
- if EXT_texture_sRGB_decode then
- begin
- if FDecodeSRGB then
- SamplerParameteri(ID, GL_TEXTURE_SRGB_DECODE_EXT, GL_DECODE_EXT)
- else
- SamplerParameteri(ID, GL_TEXTURE_SRGB_DECODE_EXT,
- GL_SKIP_DECODE_EXT);
- end;
- {$IFDEF USE_OPENGL_DEBUG}
- CheckError;
- {$ENDIF}
- FHandle.NotifyDataUpdated;
- end;
- FIsValid := True;
- end
- else
- FIsValid := False;
- except
- FIsValid := False;
- end;
- end;
- class function TGLTextureSampler.FriendlyName: string;
- begin
- Result := 'Texture Sampler';
- end;
- procedure TGLTextureSampler.ReadFromFiler(AReader: TReader);
- var
- archiveVersion: Integer;
- begin
- with AReader do
- begin
- archiveVersion := ReadInteger;
- if archiveVersion = 0 then
- begin
- Name := ReadString;
- FDefferedInit := ReadBoolean;
- FMinFilter := TGLMinFilter(ReadInteger);
- FMagFilter := TGLMagFilter(ReadInteger);
- FFilteringQuality := TGLTextureFilteringQuality(ReadInteger);
- FLODBias := ReadInteger;
- FWrap[0] := TGLSeparateTextureWrap(ReadInteger);
- FWrap[1] := TGLSeparateTextureWrap(ReadInteger);
- FWrap[2] := TGLSeparateTextureWrap(ReadInteger);
- Read(FBorderColor.AsAddress^, SizeOf(TGLColorVector));
- FCompareMode := TGLTextureCompareMode(ReadInteger);
- FCompareFunc := TGLDepthFunction(ReadInteger);
- FDecodeSRGB := ReadBoolean;
- end
- else
- RaiseFilerException(archiveVersion);
- end;
- end;
- procedure TGLTextureSampler.SetBorderColor(const AValue: TGLColor);
- begin
- FBorderColor.Assign(AValue);
- NotifyChange(Self);
- end;
- procedure TGLTextureSampler.SetCompareFunc(AValue: TGLDepthFunction);
- begin
- if FCompareFunc <> AValue then
- begin
- FCompareFunc := AValue;
- NotifyChange(Self);
- end;
- end;
- procedure TGLTextureSampler.SetCompareMode(AValue: TGLTextureCompareMode);
- begin
- if FCompareMode <> AValue then
- begin
- FCompareMode := AValue;
- NotifyChange(Self);
- end;
- end;
- procedure TGLTextureSampler.SetDecodeSRGB(AValue: Boolean);
- begin
- if FDecodeSRGB <> AValue then
- begin
- FDecodeSRGB := AValue;
- NotifyChange(Self);
- end;
- end;
- procedure TGLTextureSampler.SetFilteringQuality(
- AValue: TGLTextureFilteringQuality);
- begin
- if FFilteringQuality <> AValue then
- begin
- FFilteringQuality := AValue;
- NotifyChange(Self);
- end;
- end;
- procedure TGLTextureSampler.SetLODBias(AValue: Integer);
- begin
- if FLODBias <> AValue then
- begin
- FLODBias := AValue;
- NotifyChange(Self);
- end;
- end;
- procedure TGLTextureSampler.SetMagFilter(AValue: TGLMagFilter);
- begin
- if FMagFilter <> AValue then
- begin
- FMagFilter := AValue;
- NotifyChange(Self);
- end;
- end;
- procedure TGLTextureSampler.SetMinFilter(AValue: TGLMinFilter);
- begin
- if FMinFilter <> AValue then
- begin
- FMinFilter := AValue;
- NotifyChange(Self);
- end;
- end;
- procedure TGLTextureSampler.SetWrap(Index: Integer;
- AValue: TGLSeparateTextureWrap);
- begin
- if FWrap[Index] <> AValue then
- begin
- FWrap[Index] := AValue;
- NotifyChange(Self);
- end;
- end;
- procedure TGLTextureSampler.UnApply(var ARci: TGLRenderContextInfo);
- begin
- if FHandle.IsSupported then
- with ARci.GLStates do
- SamplerBinding[ActiveTexture] := 0;
- end;
- procedure TGLTextureSampler.WriteToFiler(AWriter: TWriter);
- begin
- with AWriter do
- begin
- WriteInteger(0); // archive version
- WriteString(Name);
- WriteBoolean(FDefferedInit);
- WriteInteger(Integer(FMinFilter));
- WriteInteger(Integer(FMagFilter));
- WriteInteger(Integer(FFilteringQuality));
- WriteInteger(FLODBias);
- WriteInteger(Integer(FWrap[0]));
- WriteInteger(Integer(FWrap[1]));
- WriteInteger(Integer(FWrap[2]));
- Write(FBorderColor.AsAddress^, SizeOf(TGLColorVector));
- WriteInteger(Integer(FCompareMode));
- WriteInteger(Integer(FCompareFunc));
- WriteBoolean(FDecodeSRGB);
- end;
- end;
- { TVXTextureCombiner }
- procedure TGLTextureCombiner.Assign(Source: TPersistent);
- var
- LCombiner: TGLTextureCombiner;
- begin
- if Source is TGLTextureCombiner then
- begin
- LCombiner := TGLTextureCombiner(Source);
- FScript.Assign(LCombiner.FScript);
- end;
- inherited;
- end;
- constructor TGLTextureCombiner.Create(AOwner: TXCollection);
- begin
- inherited;
- FDefferedInit := False;
- FHandle := TGLVirtualHandle.Create;
- FHandle.OnAllocate := DoAllocate;
- FHandle.OnDestroy := DoDeallocate;
- FHandle.OnPrapare := DoOnPrepare;
- FScript := TStringList.Create;
- FScript.OnChange := NotifyChange;
- FIsValid := True;
- Name := TGLMatLibComponents(AOwner).MakeUniqueName('Combiner');
- end;
- destructor TGLTextureCombiner.Destroy;
- begin
- FHandle.Destroy;
- FScript.Destroy;
- inherited;
- end;
- procedure TGLTextureCombiner.NotifyChange(Sender: TObject);
- begin
- FHandle.NotifyChangesOfData;
- inherited;
- end;
- procedure TGLTextureCombiner.DoAllocate(Sender: TGLVirtualHandle;
- var handle: Cardinal);
- begin
- handle := 1;
- end;
- procedure TGLTextureCombiner.DoDeallocate(Sender: TGLVirtualHandle;
- var handle: Cardinal);
- begin
- handle := 0;
- end;
- procedure TGLTextureCombiner.DoOnPrepare(Sender: TGLContext);
- begin
- if IsDesignTime and FDefferedInit then
- exit;
- if Sender.gl.ARB_multitexture then
- begin
- FHandle.AllocateHandle;
- if FHandle.IsDataNeedUpdate then
- begin
- try
- FCommandCache := GetTextureCombiners(FScript);
- FIsValid := True;
- except
- on E: Exception do
- begin
- FIsValid := False;
- if IsDesignTime then
- InformationDlg(E.ClassName + ': ' + E.Message)
- else
- GLSLogger.LogError(E.ClassName + ': ' + E.Message);
- end;
- end;
- FHandle.NotifyDataUpdated;
- end;
- end
- else
- FIsValid := False;
- end;
- class function TGLTextureCombiner.FriendlyName: string;
- begin
- Result := 'Texture Combiner';
- end;
- procedure TGLTextureCombiner.ReadFromFiler(AReader: TReader);
- var
- archiveVersion: Integer;
- begin
- with AReader do
- begin
- archiveVersion := ReadInteger;
- if archiveVersion = 0 then
- begin
- Name := ReadString;
- FDefferedInit := ReadBoolean;
- FScript.Text := ReadString;
- end
- else
- RaiseFilerException(archiveVersion);
- end;
- end;
- procedure TGLTextureCombiner.SetScript(AValue: TStringList);
- begin
- FScript.Assign(AValue);
- NotifyChange(Self);
- end;
- procedure TGLTextureCombiner.WriteToFiler(AWriter: TWriter);
- begin
- with AWriter do
- begin
- WriteInteger(0); // archive version
- WriteString(Name);
- WriteBoolean(FDefferedInit);
- WriteString(FScript.Text);
- end;
- end;
- { TVXLibMaterialEx }
- procedure TGLLibMaterialEx.Apply(var ARci: TGLRenderContextInfo);
- var
- LevelReady: array[TGLMaterialLevel] of Boolean;
- L, MaxLevel: TGLMaterialLevel;
- begin
- if Assigned(FNextPass) then
- begin
- FNextPass := nil;
- exit;
- end;
- FHandle.AllocateHandle;
- if FHandle.IsDataNeedUpdate then
- begin
- // Other value than mlAuto indicates a level failure
- // Need remove deffered initialization and reinitialize used resources
- if not IsDesignTime and (FSelectedLevel <> mlAuto) then
- RemoveDefferedInit;
- // Level selection
- LevelReady[mlFixedFunction] := FFixedFunc.Enabled;
- LevelReady[mlMultitexturing] := FMultitexturing.Enabled and
- FMultitexturing.IsValid;
- LevelReady[mlSM3] := FSM3.Enabled and FSM3.IsValid;
- LevelReady[mlSM4] := FSM4.Enabled and FSM4.IsValid;
- LevelReady[mlSM5] := FSM5.Enabled and FSM5.IsValid;
- if FApplicableLevel = mlAuto then
- MaxLevel := mlSM5
- else
- MaxLevel := FApplicableLevel;
- FSelectedLevel := mlAuto;
- for L := MaxLevel downto mlFixedFunction do
- if LevelReady[L] then
- begin
- FSelectedLevel := L;
- break;
- end;
- FStoreAmalgamating := ARci.amalgamating;
- ARci.amalgamating := True;
- FHandle.NotifyDataUpdated;
- end;
- ARci.currentMaterialLevel := FSelectedLevel;
- case FSelectedLevel of
- mlAuto: ; // No one level can be used. Worst case.
- mlFixedFunction:
- begin
- FFixedFunc.Apply(ARci);
- end;
- mlMultitexturing:
- begin
- if LevelReady[mlFixedFunction] then
- FFixedFunc.Apply(ARci);
- FMultitexturing.Apply(ARci);
- end;
- mlSM3:
- begin
- if LevelReady[mlFixedFunction] then
- FFixedFunc.Apply(ARci);
- FSM3.Apply(ARci);
- end;
- mlSM4:
- begin
- if LevelReady[mlFixedFunction] then
- FFixedFunc.Apply(ARci);
- FSM4.Apply(ARci);
- end;
- mlSM5:
- begin
- if LevelReady[mlFixedFunction] then
- FFixedFunc.Apply(ARci);
- FSM5.Apply(ARci);
- end;
- end;
- end;
- procedure TGLLibMaterialEx.Assign(Source: TPersistent);
- var
- LMaterial: TGLLibMaterialEx;
- begin
- if Source is TGLLibMaterialEx then
- begin
- LMaterial := TGLLibMaterialEx(Source);
- FFixedFunc.Assign(LMaterial.FFixedFunc);
- FMultitexturing.Assign(LMaterial.FMultitexturing);
- FSM3.Assign(LMaterial.FSM3);
- FSM4.Assign(LMaterial.FSM4);
- FSM5.Assign(LMaterial.FSM5);
- FApplicableLevel := LMaterial.FApplicableLevel;
- NotifyChange(Self);
- end;
- inherited;
- end;
- function TGLLibMaterialEx.Blended: Boolean;
- begin
- Result := FFixedFunc.Blended;
- end;
- constructor TGLLibMaterialEx.Create(ACollection: TCollection);
- begin
- inherited;
- FHandle := TGLVirtualHandle.Create;
- FHandle.OnAllocate := DoAllocate;
- FHandle.OnDestroy := DoDeallocate;
- FHandle.OnPrapare := DoOnPrepare;
- FApplicableLevel := mlAuto;
- FSelectedLevel := mlAuto;
- FFixedFunc := TGLFixedFunctionProperties.Create(Self);
- FMultitexturing := TGLMultitexturingProperties.Create(Self);
- FSM3 := TGLShaderModel3.Create(Self);
- FSM4 := TGLShaderModel4.Create(Self);
- FSM5 := TGLShaderModel5.Create(Self);
- end;
- type
- TGLFreindlyMaterial = class(TGLMaterial);
- destructor TGLLibMaterialEx.Destroy;
- var
- I: Integer;
- LUser: TObject;
- begin
- FHandle.Destroy;
- FFixedFunc.Destroy;
- FMultitexturing.Destroy;
- FSM3.Destroy;
- FSM4.Destroy;
- FSM5.Destroy;
- for I := 0 to FUserList.Count - 1 do
- begin
- LUser := TObject(FUserList[i]);
- if LUser is TGLMaterial then
- TGLFreindlyMaterial(LUser).NotifyLibMaterialDestruction;
- end;
- inherited;
- end;
- procedure TGLLibMaterialEx.DoAllocate(Sender: TGLVirtualHandle;
- var handle: Cardinal);
- begin
- handle := 1;
- end;
- procedure TGLLibMaterialEx.DoDeallocate(Sender: TGLVirtualHandle;
- var handle: Cardinal);
- begin
- handle := 0;
- end;
- procedure TGLLibMaterialEx.DoOnPrepare(Sender: TGLContext);
- begin
- end;
- procedure TGLLibMaterialEx.Loaded;
- begin
- FFixedFunc.FTexProp.Loaded;
- FMultitexturing.Loaded;
- FSM3.Loaded;
- FSM4.Loaded;
- FSM5.Loaded;
- end;
- procedure TGLLibMaterialEx.NotifyChange(Sender: TObject);
- begin
- inherited;
- FHandle.NotifyChangesOfData;
- end;
- procedure TGLLibMaterialEx.RemoveDefferedInit;
- var
- I: Integer;
- ST: TGLShaderType;
- begin
- if FFixedFunc.FTexProp.Enabled then
- begin
- if Assigned(FFixedFunc.FTexProp.FLibTexture) then
- FFixedFunc.FTexProp.FLibTexture.FDefferedInit := False;
- if Assigned(FFixedFunc.FTexProp.FLibSampler) then
- FFixedFunc.FTexProp.FLibSampler.FDefferedInit := False;
- end;
- if FMultitexturing.Enabled then
- begin
- if Assigned(FMultitexturing.FLibCombiner) then
- begin
- FMultitexturing.FLibCombiner.FDefferedInit := False;
- for I := 0 to 3 do
- if Assigned(FMultitexturing.FTexProps[I]) then
- with FMultitexturing.FTexProps[I] do
- begin
- if Assigned(FLibTexture) then
- FLibTexture.FDefferedInit := False;
- if Assigned(FLibSampler) then
- FLibSampler.FDefferedInit := False;
- end;
- end;
- end;
- if FSM3.Enabled then
- begin
- for ST := Low(TGLShaderType) to High(TGLShaderType) do
- if Assigned(FSM3.FShaders[ST]) then
- FSM3.FShaders[ST].FDefferedInit := False;
- end;
- if FSM4.Enabled then
- begin
- for ST := Low(TGLShaderType) to High(TGLShaderType) do
- if Assigned(FSM4.FShaders[ST]) then
- FSM4.FShaders[ST].FDefferedInit := False;
- end;
- if FSM5.Enabled then
- begin
- for ST := Low(TGLShaderType) to High(TGLShaderType) do
- if Assigned(FSM5.FShaders[ST]) then
- FSM5.FShaders[ST].FDefferedInit := False;
- end;
- CurrentGLContext.PrepareHandlesData;
- end;
- procedure TGLLibMaterialEx.SetMultitexturing(AValue:
- TGLMultitexturingProperties);
- begin
- FMultitexturing.Assign(AValue);
- end;
- procedure TGLLibMaterialEx.SetFixedFunc(AValue: TGLFixedFunctionProperties);
- begin
- FFixedFunc.Assign(AValue);
- end;
- procedure TGLLibMaterialEx.SetLevel(AValue: TGLMaterialLevel);
- begin
- if FApplicableLevel <> AValue then
- begin
- FApplicableLevel := AValue;
- NotifyChange(Self);
- end;
- end;
- procedure TGLLibMaterialEx.SetSM3(AValue: TGLShaderModel3);
- begin
- FSM3.Assign(AValue);
- end;
- procedure TGLLibMaterialEx.SetSM4(AValue: TGLShaderModel4);
- begin
- FSM4.Assign(AValue);
- end;
- procedure TGLLibMaterialEx.SetSM5(AValue: TGLShaderModel5);
- begin
- FSM5.Assign(AValue);
- end;
- function TGLLibMaterialEx.UnApply(var ARci: TGLRenderContextInfo): Boolean;
- procedure GetNextPass(AProp: TGLLibMaterialProperty);
- begin
- if Length(AProp.NextPass) > 0 then
- FNextPass :=
- TGLMaterialLibraryEx(GetMaterialLibrary).Materials.GetLibMaterialByName(AProp.NextPass)
- else
- FNextPass := nil;
- if FNextPass = Self then
- begin
- AProp.NextPass := '';
- FNextPass := nil;
- end;
- end;
- begin
- if FStoreAmalgamating <> ARci.amalgamating then
- ARci.amalgamating := FStoreAmalgamating;
- if Assigned(FNextPass) then
- begin
- Result := FNextPass.UnApply(ARci);
- if Result then
- FNextPass.Apply(ARci)
- else
- FNextPass := nil;
- exit;
- end;
- case FSelectedLevel of
- mlFixedFunction:
- begin
- FFixedFunc.UnApply(ARci);
- GetNextPass(FFixedFunc);
- end;
- mlMultitexturing:
- begin
- if FFixedFunc.Enabled then
- FFixedFunc.UnApply(ARci);
- FMultitexturing.UnApply(ARci);
- GetNextPass(FMultitexturing);
- end;
- mlSM3:
- begin
- if FFixedFunc.Enabled then
- FFixedFunc.UnApply(ARci);
- FSM3.UnApply(ARci);
- GetNextPass(FSM3);
- end;
- mlSM4:
- begin
- if FFixedFunc.Enabled then
- FFixedFunc.UnApply(ARci);
- FSM4.UnApply(ARci);
- GetNextPass(FSM4);
- end;
- mlSM5:
- begin
- if FFixedFunc.Enabled then
- FFixedFunc.UnApply(ARci);
- FSM5.UnApply(ARci);
- GetNextPass(FSM5);
- end;
- else
- FNextPass := nil;
- end;
- ARci.GLStates.ActiveTexture := 0;
- Result := Assigned(FNextPass);
- if Result then
- FNextPass.Apply(ARCi);
- end;
- { TVXMultitexturingProperties }
- procedure TGLMultitexturingProperties.Apply(var ARci: TGLRenderContextInfo);
- var
- N, U: Integer;
- LDir: TGLVector;
- begin
- if FEnabled then
- begin
- if Assigned(FLibCombiner) and not FLibCombiner.FIsValid then
- exit;
- if Assigned(FLibAsmProg) and not FLibAsmProg.FIsValid then
- exit;
- U := 0;
- for N := 0 to High(FTexProps) do
- begin
- if Assigned(FTexProps[N]) and FTexProps[N].Enabled then
- begin
- ARci.GLStates.ActiveTexture := N;
- FTexProps[N].Apply(ARci);
- if Ord(FLightDir) = N+1 then
- begin
- LDir := ARci.GLStates.LightPosition[FLightSourceIndex];
- LDir := VectorTransform(LDir, ARci.PipelineTransformation.InvModelMatrix^);
- NormalizeVector(LDir);
- gl.TexEnvfv(GL_TEXTURE_ENV, GL_TEXTURE_ENV_COLOR, @LDir);
- end;
- U := U or (1 shl N);
- end;
- end;
- if Assigned(FLibAsmProg) then
- begin
- FLibAsmProg.Handle.Bind;
- gl.Enable(GL_VERTEX_PROGRAM_ARB);
- if Assigned(GetMaterial.FOnAsmProgSetting) then
- GetMaterial.FOnAsmProgSetting(Self.FLibAsmProg, ARci);
- end;
- with GL, ARci.GLStates do
- begin
- if Assigned(FLibCombiner) and (Length(FLibCombiner.FCommandCache) > 0)
- then
- begin
- for N := 0 to High(FLibCombiner.FCommandCache) do
- begin
- ActiveTexture := FLibCombiner.FCommandCache[N].ActiveUnit;
- TexEnvi(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_COMBINE);
- TexEnvi(GL_TEXTURE_ENV,
- FLibCombiner.FCommandCache[N].Arg1,
- FLibCombiner.FCommandCache[N].Arg2);
- end;
- end;
- TexEnvi(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, cTextureMode[FTextureMode]);
- ActiveTexture := 0;
- end;
- XGL.BeginUpdate;
- if U > 3 then
- XGL.MapTexCoordToArbitrary(U)
- else if (FTexProps[0].Enabled)
- and (FTexProps[0].MappingMode = tmmUser) then
- if FTexProps[1].MappingMode = tmmUser then
- XGL.MapTexCoordToDual
- else
- XGL.MapTexCoordToMain
- else if FTexProps[1].MappingMode = tmmUser then
- XGL.MapTexCoordToSecond
- else
- XGL.MapTexCoordToMain;
- XGL.EndUpdate;
- end;
- end;
- constructor TGLMultitexturingProperties.Create(AOwner: TPersistent);
- begin
- inherited;
- FEnabled := False;
- FTextureMode := tmDecal;
- FLightDir := l2eNone;
- FLightSourceIndex := 0;
- end;
- destructor TGLMultitexturingProperties.Destroy;
- begin
- if Assigned(FLibCombiner) then
- FLibCombiner.UnregisterUser(Self);
- if Assigned(FLibAsmProg) then
- FLibAsmProg.UnregisterUser(Self);
- FTexProps[0].Free;
- FTexProps[1].Free;
- FTexProps[2].Free;
- FTexProps[3].Free;
- inherited;
- end;
- function TGLMultitexturingProperties.GetLibCombinerName: string;
- begin
- if Assigned(FLibCombiner) then
- Result := FLibCombiner.Name
- else
- Result := '';
- end;
- function TGLMultitexturingProperties.GetLibAsmProgName: string;
- begin
- if Assigned(FLibAsmProg) then
- Result := FLibAsmProg.Name
- else
- Result := '';
- end;
- function TGLMultitexturingProperties.IsValid: Boolean;
- var
- I: Integer;
- begin
- Result := True;
- if Assigned(FLibCombiner) then
- Result := Result and FLibCombiner.IsValid;
- if Assigned(FLibAsmProg) then
- Result := Result and FLibAsmProg.IsValid;
- for I := 0 to High(FTexProps) do
- if Assigned(FTexProps[I]) and FTexProps[I].FEnabled then
- Result := Result and FTexProps[I].IsValid;
- end;
- procedure TGLMultitexturingProperties.Loaded;
- var
- I: Integer;
- begin
- SetLibCombinerName(FLibCombinerName);
- SetLibAsmProgName(FLibAsmProgName);
- for I := 0 to High(FTexProps) do
- if Assigned(FTexProps[I]) then
- FTexProps[I].Loaded;
- end;
- procedure TGLMultitexturingProperties.Notification(Sender: TObject; Operation:
- TOperation);
- begin
- if Operation = opRemove then
- begin
- if Sender = FLibCombiner then
- FLibCombiner := nil;
- if Sender = FLibAsmProg then
- FLibAsmProg := nil;
- end;
- inherited;
- end;
- procedure TGLMultitexturingProperties.SetLibCombinerName(const AValue: string);
- var
- LCombiner: TGLTextureCombiner;
- begin
- if csLoading in GetMaterialLibraryEx.ComponentState then
- begin
- FLibCombinerName := AValue;
- exit;
- end;
- if Assigned(FLibCombiner) then
- begin
- if FLibCombiner.Name = AValue then
- exit;
- FLibCombiner.UnregisterUser(Self);
- FLibCombiner := nil;
- end;
- LCombiner := GetMaterialLibraryEx.Components.GetCombinerByName(AValue);
- if Assigned(LCombiner) then
- begin
- LCombiner.RegisterUser(Self);
- FLibCombiner := LCombiner;
- end;
- NotifyChange(Self);
- end;
- procedure TGLMultitexturingProperties.SetLightSourceIndex(AValue: Integer);
- begin
- if AValue < 0 then
- AValue := 0
- else if AValue > 7 then
- AValue := 7;
- FLightSourceIndex := AValue;
- end;
- procedure TGLMultitexturingProperties.SetLibAsmProgName(const AValue: string);
- var
- LProg: TGLASMVertexProgram;
- begin
- if csLoading in GetMaterialLibraryEx.ComponentState then
- begin
- FLibAsmProgName := AValue;
- exit;
- end;
- if Assigned(FLibAsmProg) then
- begin
- if FLibAsmProg.Name = AValue then
- exit;
- FLibAsmProg.UnregisterUser(Self);
- FLibAsmProg := nil;
- end;
- LProg := GetMaterialLibraryEx.Components.GetAsmProgByName(AValue);
- if Assigned(LProg) then
- begin
- LProg.RegisterUser(Self);
- FLibAsmProg := LProg;
- end;
- NotifyChange(Self);
- end;
- function TGLMultitexturingProperties.GetTexProps(AIndex: Integer):
- TGLTextureProperties;
- begin
- if not Assigned(FTexProps[AIndex]) then
- FTexProps[AIndex] := TGLTextureProperties.Create(Self);
- Result := FTexProps[AIndex];
- end;
- procedure TGLMultitexturingProperties.SetTexProps(AIndex: Integer;
- AValue: TGLTextureProperties);
- begin
- FTexProps[AIndex].Assign(AValue);
- end;
- procedure TGLMultitexturingProperties.SetTextureMode(AValue: TGLTextureMode);
- begin
- if AValue <> FTextureMode then
- begin
- FTextureMode := AValue;
- NotifyChange(Self);
- end;
- end;
- procedure TGLMultitexturingProperties.UnApply(var ARci: TGLRenderContextInfo);
- var
- N: Integer;
- begin
- for N := 0 to High(FTexProps) do
- begin
- if FTexProps[N].Enabled then
- begin
- ARci.GLStates.ActiveTexture := N;
- FTexProps[N].UnApply(ARci);
- end;
- end;
- ARci.GLStates.ActiveTexture := 0;
- if Assigned(FLibAsmProg) then
- gl.Disable(GL_VERTEX_PROGRAM_ARB);
- end;
- { TVXTextureProperties }
- procedure TGLTextureProperties.Apply(var ARci: TGLRenderContextInfo);
- var
- glTarget: Cardinal;
- begin
- if Assigned(FLibTexture) then
- begin
- FLibTexture.FApplicableSampler := FLibSampler;
- FLibTexture.Apply(ARci);
- // Apply swizzling if possible
- glTarget := DecodeTextureTarget(FLibTexture.Shape);
- if GL.ARB_texture_swizzle or GL.EXT_texture_swizzle then
- begin
- if FSwizzling.FSwizzles[0] <> FLibTexture.FSwizzles[0] then
- begin
- FLibTexture.FSwizzles[0] := FSwizzling.FSwizzles[0];
- gl.TexParameteri(glTarget, GL_TEXTURE_SWIZZLE_R,
- cTextureSwizzle[FSwizzling.FSwizzles[0]]);
- end;
- if FSwizzling.FSwizzles[1] <> FLibTexture.FSwizzles[1] then
- begin
- FLibTexture.FSwizzles[1] := FSwizzling.FSwizzles[1];
- gl.TexParameteri(glTarget, GL_TEXTURE_SWIZZLE_G,
- cTextureSwizzle[FSwizzling.FSwizzles[1]]);
- end;
- if FSwizzling.FSwizzles[2] <> FLibTexture.FSwizzles[2] then
- begin
- FLibTexture.FSwizzles[2] := FSwizzling.FSwizzles[2];
- gl.TexParameteri(glTarget, GL_TEXTURE_SWIZZLE_B,
- cTextureSwizzle[FSwizzling.FSwizzles[2]]);
- end;
- if FSwizzling.FSwizzles[3] <> FLibTexture.FSwizzles[3] then
- begin
- FLibTexture.FSwizzles[3] := FSwizzling.FSwizzles[3];
- gl.TexParameteri(glTarget, GL_TEXTURE_SWIZZLE_A,
- cTextureSwizzle[FSwizzling.FSwizzles[3]]);
- end;
- end;
- if Assigned(FLibSampler) then
- begin
- if FLibSampler.IsValid then
- FLibSampler.Apply(ARci)
- else if FLibTexture.FLastSampler <> FLibSampler then
- begin
- // Sampler object not supported, lets use texture states
- gl.TexParameterfv(glTarget, GL_TEXTURE_BORDER_COLOR,
- FLibSampler.BorderColor.AsAddress);
- gl.TexParameteri(glTarget, GL_TEXTURE_WRAP_S,
- cTextureWrapMode[FLibSampler.WrapX]);
- gl.TexParameteri(glTarget, GL_TEXTURE_WRAP_T,
- cTextureWrapMode[FLibSampler.WrapY]);
- gl.TexParameteri(glTarget, GL_TEXTURE_WRAP_R,
- cTextureWrapMode[FLibSampler.WrapZ]);
- gl.TexParameterf(glTarget, GL_TEXTURE_LOD_BIAS, FLibSampler.LODBias +
- FLibSampler.FLODBiasFract);
- gl.TexParameteri(glTarget, GL_TEXTURE_MIN_FILTER,
- cTextureMinFilter[FLibSampler.MinFilter]);
- gl.TexParameteri(glTarget, GL_TEXTURE_MAG_FILTER,
- cTextureMagFilter[FLibSampler.MagFilter]);
- if GL.EXT_texture_filter_anisotropic then
- begin
- if FLibSampler.FilteringQuality = tfAnisotropic then
- gl.TexParameteri(glTarget, GL_TEXTURE_MAX_ANISOTROPY_EXT,
- CurrentGLContext.GLStates.MaxTextureAnisotropy)
- else
- gl.TexParameteri(glTarget, GL_TEXTURE_MAX_ANISOTROPY_EXT, 1);
- end;
- gl.TexParameteri(glTarget, GL_TEXTURE_COMPARE_MODE,
- cTextureCompareMode[FLibSampler.CompareMode]);
- gl.TexParameteri(glTarget, GL_TEXTURE_COMPARE_FUNC,
- cGLComparisonFunctionToGLEnum[FLibSampler.CompareFunc]);
- if GL.EXT_texture_sRGB_decode then
- begin
- if FLibSampler.sRGB_Encode then
- gl.TexParameteri(glTarget, GL_TEXTURE_SRGB_DECODE_EXT, GL_DECODE_EXT)
- else
- gl.TexParameteri(glTarget, GL_TEXTURE_SRGB_DECODE_EXT,
- GL_SKIP_DECODE_EXT);
- end;
- FLibTexture.FLastSampler := FLibSampler;
- end;
- end;
- if not FTextureMatrixIsIdentity and (MappingMode = tmmUser) then
- ARci.GLStates.SetGLTextureMatrix(FTextureMatrix);
- if ARci.currentMaterialLevel < mlSM3 then
- begin
- gl.TexEnvfv(GL_TEXTURE_ENV, GL_TEXTURE_ENV_COLOR, FEnvColor.AsAddress);
- ApplyMappingMode;
- if ARci.currentMaterialLevel = mlFixedFunction then
- XGL.MapTexCoordToMain;
- end;
- end;
- end;
- procedure TGLTextureProperties.ApplyMappingMode;
- var
- R_Dim: Boolean;
- begin
- begin
- R_Dim := GL.ARB_texture_cube_map or GL.EXT_texture3D;
- case MappingMode of
- tmmUser: ; // nothing to do, but checked first (common case)
- tmmObjectLinear:
- begin
- gl.TexGeni(GL_S, GL_TEXTURE_GEN_MODE, GL_OBJECT_LINEAR);
- gl.TexGeni(GL_T, GL_TEXTURE_GEN_MODE, GL_OBJECT_LINEAR);
- gl.TexGenfv(GL_S, GL_OBJECT_PLANE, @MappingSCoordinates.DirectVector);
- gl.TexGenfv(GL_T, GL_OBJECT_PLANE, @MappingTCoordinates.DirectVector);
- gl.Enable(GL_TEXTURE_GEN_S);
- gl.Enable(GL_TEXTURE_GEN_T);
- if R_Dim then
- begin
- gl.TexGeni(GL_R, GL_TEXTURE_GEN_MODE, GL_OBJECT_LINEAR);
- gl.TexGeni(GL_Q, GL_TEXTURE_GEN_MODE, GL_OBJECT_LINEAR);
- gl.TexGenfv(GL_R, GL_OBJECT_PLANE, @MappingRCoordinates.DirectVector);
- gl.TexGenfv(GL_Q, GL_OBJECT_PLANE, @MappingQCoordinates.DirectVector);
- gl.Enable(GL_TEXTURE_GEN_R);
- gl.Enable(GL_TEXTURE_GEN_Q);
- end;
- end;
- tmmEyeLinear:
- begin
- gl.TexGeni(GL_S, GL_TEXTURE_GEN_MODE, GL_EYE_LINEAR);
- gl.TexGeni(GL_T, GL_TEXTURE_GEN_MODE, GL_EYE_LINEAR);
- // specify planes in eye space, not world space
- gl.MatrixMode(GL_MODELVIEW);
- gl.PushMatrix;
- gl.LoadIdentity;
- gl.TexGenfv(GL_S, GL_EYE_PLANE, @MappingSCoordinates.DirectVector);
- gl.TexGenfv(GL_T, GL_EYE_PLANE, @MappingTCoordinates.DirectVector);
- gl.Enable(GL_TEXTURE_GEN_S);
- gl.Enable(GL_TEXTURE_GEN_T);
- if R_Dim then
- begin
- gl.TexGenfv(GL_R, GL_EYE_PLANE, @MappingRCoordinates.DirectVector);
- gl.TexGenfv(GL_Q, GL_EYE_PLANE, @MappingQCoordinates.DirectVector);
- gl.Enable(GL_TEXTURE_GEN_R);
- gl.Enable(GL_TEXTURE_GEN_Q);
- end;
- gl.PopMatrix;
- end;
- tmmSphere:
- begin
- gl.TexGeni(GL_S, GL_TEXTURE_GEN_MODE, GL_SPHERE_MAP);
- gl.TexGeni(GL_T, GL_TEXTURE_GEN_MODE, GL_SPHERE_MAP);
- gl.Enable(GL_TEXTURE_GEN_S);
- gl.Enable(GL_TEXTURE_GEN_T);
- end;
- tmmCubeMapReflection, tmmCubeMapCamera:
- if R_Dim then
- begin
- gl.TexGeni(GL_S, GL_TEXTURE_GEN_MODE, GL_REFLECTION_MAP);
- gl.TexGeni(GL_T, GL_TEXTURE_GEN_MODE, GL_REFLECTION_MAP);
- gl.TexGeni(GL_R, GL_TEXTURE_GEN_MODE, GL_REFLECTION_MAP);
- gl.Enable(GL_TEXTURE_GEN_S);
- gl.Enable(GL_TEXTURE_GEN_T);
- gl.Enable(GL_TEXTURE_GEN_R);
- end;
- tmmCubeMapNormal, tmmCubeMapLight0:
- if R_Dim then
- begin
- gl.TexGeni(GL_S, GL_TEXTURE_GEN_MODE, GL_NORMAL_MAP);
- gl.TexGeni(GL_T, GL_TEXTURE_GEN_MODE, GL_NORMAL_MAP);
- gl.TexGeni(GL_R, GL_TEXTURE_GEN_MODE, GL_NORMAL_MAP);
- gl.Enable(GL_TEXTURE_GEN_S);
- gl.Enable(GL_TEXTURE_GEN_T);
- gl.Enable(GL_TEXTURE_GEN_R);
- end;
- end;
- end;
- end;
- procedure TGLTextureProperties.Assign(Source: TPersistent);
- var
- LTexProp: TGLTextureProperties;
- begin
- if Source is TGLTextureProperties then
- begin
- LTexProp := TGLTextureProperties(Source);
- LibTextureName := LTexProp.LibTextureName;
- LibSamplerName := LTexProp.LibSamplerName;
- TextureOffset.Assign(LTexProp.TextureOffset);
- TextureScale.Assign(LTexProp.TextureScale);
- FTextureRotate := LTexProp.TextureRotate;
- FEnvColor.Assign(LTexProp.EnvColor);
- FMappingMode := LTexProp.MappingMode;
- MappingSCoordinates.Assign(LTexProp.MappingSCoordinates);
- MappingTCoordinates.Assign(LTexProp.MappingTCoordinates);
- MappingRCoordinates.Assign(LTexProp.MappingRCoordinates);
- MappingQCoordinates.Assign(LTexProp.MappingQCoordinates);
- end;
- inherited;
- end;
- procedure TGLTextureProperties.CalculateTextureMatrix;
- begin
- if not (Assigned(FTextureOffset) or Assigned(FTextureScale)
- or StoreTextureRotate) then
- begin
- FTextureMatrixIsIdentity := True;
- exit;
- end;
- if TextureOffset.Equals(NullHmgVector)
- and TextureScale.Equals(XYZHmgVector)
- and not StoreTextureRotate then
- FTextureMatrixIsIdentity := True
- else
- begin
- FTextureMatrixIsIdentity := False;
- FTextureMatrix := CreateScaleAndTranslationMatrix(
- TextureScale.AsVector,
- TextureOffset.AsVector);
- if StoreTextureRotate then
- FTextureMatrix := MatrixMultiply(FTextureMatrix,
- CreateRotationMatrixZ(DegToRad(FTextureRotate)));
- end;
- FTextureOverride := False;
- NotifyChange(Self);
- end;
- constructor TGLTextureProperties.Create(AOwner: TPersistent);
- begin
- inherited;
- FTextureRotate := 0;
- FMappingMode := tmmUser;
- FTextureMatrix := IdentityHmgMatrix;
- FEnabled := False;
- FSwizzling := TGLTextureSwizzling.Create(Self);
- FEnvColor := TGLColor.CreateInitialized(Self, clrTransparent);
- end;
- destructor TGLTextureProperties.Destroy;
- begin
- if Assigned(FLibSampler) then
- FLibSampler.UnregisterUser(Self);
- if Assigned(FLibTexture) then
- FLibTexture.UnregisterUser(Self);
- FTextureOffset.Free;
- FTextureScale.Free;
- FMapSCoordinates.Free;
- FMapTCoordinates.Free;
- FMapRCoordinates.Free;
- FMapQCoordinates.Free;
- FSwizzling.Destroy;
- FEnvColor.Destroy;
- inherited;
- end;
- function TGLTextureProperties.GetLibSamplerName: TGLMaterialComponentName;
- begin
- if Assigned(FLibSampler) then
- Result := FLibSampler.Name
- else
- Result := '';
- end;
- function TGLTextureProperties.GetLibTextureName: TGLMaterialComponentName;
- begin
- if Assigned(FLibTexture) then
- Result := FLibTexture.Name
- else
- Result := '';
- end;
- function TGLTextureProperties.GetMappingQCoordinates: TGLCoordinates4;
- begin
- if not Assigned(FMapQCoordinates) then
- FMapQCoordinates := TGLCoordinates4.CreateInitialized(Self, WHmgVector,
- csVector);
- Result := FMapQCoordinates;
- end;
- function TGLTextureProperties.GetMappingRCoordinates: TGLCoordinates4;
- begin
- if not Assigned(FMapRCoordinates) then
- FMapRCoordinates := TGLCoordinates4.CreateInitialized(Self, ZHmgVector,
- csVector);
- Result := FMapRCoordinates;
- end;
- function TGLTextureProperties.GetMappingSCoordinates: TGLCoordinates4;
- begin
- if not Assigned(FMapSCoordinates) then
- FMapSCoordinates := TGLCoordinates4.CreateInitialized(Self, XHmgVector,
- csVector);
- Result := FMapSCoordinates;
- end;
- function TGLTextureProperties.GetMappingTCoordinates: TGLCoordinates4;
- begin
- if not Assigned(FMapTCoordinates) then
- FMapTCoordinates := TGLCoordinates4.CreateInitialized(Self, YHmgVector,
- csVector);
- Result := FMapTCoordinates;
- end;
- function TGLTextureProperties.GetTextureOffset: TGLCoordinates;
- begin
- if not Assigned(FTextureOffset) then
- FTextureOffset :=
- TGLCoordinates3.CreateInitialized(Self, NullHmgVector, csPoint);
- Result := FTextureOffset;
- end;
- function TGLTextureProperties.GetTextureScale: TGLCoordinates;
- begin
- if not Assigned(FTextureScale) then
- FTextureScale :=
- TGLCoordinates3.CreateInitialized(Self, VectorMake(1, 1, 1, 1), csVector);
- Result := FTextureScale;
- end;
- function TGLTextureProperties.IsValid: Boolean;
- begin
- if Assigned(FLibTexture) then
- Result := FLibTexture.IsValid
- else
- Result := False;
- end;
- procedure TGLTextureProperties.Loaded;
- begin
- SetLibTextureName(FLibTextureName);
- SetLibSamplerName(FLibSamplerName);
- CalculateTextureMatrix;
- end;
- procedure TGLTextureProperties.Notification(Sender: TObject;
- Operation: TOperation);
- begin
- if Operation = opRemove then
- begin
- if Sender = FLibTexture then
- FLibTexture := nil
- else if Sender = FLibSampler then
- FLibSampler := nil;
- end;
- end;
- procedure TGLTextureProperties.NotifyChange(Sender: TObject);
- begin
- inherited;
- if (Sender = FTextureOffset) or (Sender = FTextureScale) then
- CalculateTextureMatrix;
- if (Sender = FLibSampler) and Assigned(FLibTexture) then
- FLibTexture.FLastSampler := nil;
- end;
- procedure TGLTextureProperties.SetLibSamplerName(const AValue:
- TGLMaterialComponentName);
- var
- LSampler: TGLTextureSampler;
- begin
- if csLoading in GetMaterialLibraryEx.ComponentState then
- begin
- FLibSamplerName := AValue;
- exit;
- end;
- if Assigned(FLibSampler) then
- begin
- if FLibSampler.Name = AValue then
- exit;
- FLibSampler.UnregisterUser(Self);
- FLibSampler := nil;
- end;
- LSampler := GetMaterialLibraryEx.Components.GetSamplerByName(AValue);
- if Assigned(LSampler) then
- begin
- LSampler.RegisterUser(Self);
- FLibSampler := LSampler;
- end;
- NotifyChange(Self);
- end;
- procedure TGLTextureProperties.SetLibTextureName(const AValue:
- TGLMaterialComponentName);
- var
- LTexture: TGLAbstractTexture;
- begin
- if csLoading in GetMaterialLibraryEx.ComponentState then
- begin
- FLibTextureName := AValue;
- exit;
- end;
- if Assigned(FLibTexture) then
- begin
- if FLibTexture.Name = AValue then
- exit;
- FLibTexture.UnregisterUser(Self);
- FLibTexture := nil;
- end;
- LTexture := GetMaterialLibraryEx.Components.GetTextureByName(AValue);
- if Assigned(LTexture) then
- begin
- if LTexture is TGLFrameBufferAttachment then
- begin
- if TGLFrameBufferAttachment(LTexture).OnlyWrite then
- begin
- if IsDesignTime then
- InformationDlg('Can not use write only attachment as texture')
- else
- GLSLogger.LogErrorFmt('Attempt to use write only attachment "%s" as texture',
- [LTexture.Name]);
- NotifyChange(Self);
- exit;
- end;
- end;
- LTexture.RegisterUser(Self);
- FLibTexture := LTexture;
- end;
- NotifyChange(Self);
- end;
- procedure TGLTextureProperties.SetMappingMode(
- const AValue: TGLTextureMappingMode);
- begin
- if AValue <> FMappingMode then
- begin
- FMappingMode := AValue;
- NotifyChange(Self);
- end;
- end;
- procedure TGLTextureProperties.SetMappingQCoordinates(
- const AValue: TGLCoordinates4);
- begin
- MappingQCoordinates.Assign(AValue);
- end;
- procedure TGLTextureProperties.SetMappingRCoordinates(
- const AValue: TGLCoordinates4);
- begin
- MappingRCoordinates.Assign(AValue);
- end;
- procedure TGLTextureProperties.SetMappingSCoordinates(
- const AValue: TGLCoordinates4);
- begin
- MappingSCoordinates.Assign(AValue);
- end;
- procedure TGLTextureProperties.SetMappingTCoordinates(
- const AValue: TGLCoordinates4);
- begin
- MappingTCoordinates.Assign(AValue);
- end;
- procedure TGLTextureProperties.SetSwizzling(const AValue: TGLTextureSwizzling);
- begin
- FSwizzling.Assign(AValue);
- end;
- procedure TGLTextureProperties.SetTextureMatrix(const AValue: TGLMatrix);
- begin
- FTextureMatrixIsIdentity := CompareMem(@AValue.V[0], @IdentityHmgMatrix.V[0],
- SizeOf(TGLMatrix));
- FTextureMatrix := AValue;
- FTextureOverride := True;
- NotifyChange(Self);
- end;
- procedure TGLTextureProperties.SetTextureOffset(const AValue: TGLCoordinates);
- begin
- TextureOffset.Assign(AValue);
- CalculateTextureMatrix;
- end;
- procedure TGLTextureProperties.SetTextureRotate(AValue: Single);
- begin
- if AValue <> FTextureRotate then
- begin
- FTextureRotate := AValue;
- CalculateTextureMatrix;
- NotifyChange(Self);
- end;
- end;
- procedure TGLTextureProperties.SetTextureScale(const AValue: TGLCoordinates);
- begin
- TextureScale.Assign(AValue);
- CalculateTextureMatrix;
- end;
- function TGLTextureProperties.StoreMappingQCoordinates: Boolean;
- begin
- if Assigned(FMapQCoordinates) then
- Result := not VectorEquals(FMapQCoordinates.AsVector, WHmgVector)
- else
- Result := false;
- end;
- function TGLTextureProperties.StoreMappingRCoordinates: Boolean;
- begin
- if Assigned(FMapRCoordinates) then
- Result := not VectorEquals(FMapRCoordinates.AsVector, ZHmgVector)
- else
- Result := false;
- end;
- function TGLTextureProperties.StoreMappingSCoordinates: Boolean;
- begin
- if Assigned(FMapSCoordinates) then
- Result := not VectorEquals(FMapSCoordinates.AsVector, XHmgVector)
- else
- Result := false;
- end;
- function TGLTextureProperties.StoreMappingTCoordinates: Boolean;
- begin
- if Assigned(FMapTCoordinates) then
- Result := not VectorEquals(FMapTCoordinates.AsVector, YHmgVector)
- else
- Result := false;
- end;
- function TGLTextureProperties.StoreSwizzling: Boolean;
- begin
- Result := FSwizzling.StoreSwizzle(0);
- end;
- function TGLTextureProperties.StoreTextureOffset: Boolean;
- begin
- Result := Assigned(FTextureOffset);
- end;
- function TGLTextureProperties.StoreTextureRotate: Boolean;
- begin
- Result := Abs(FTextureRotate) > EPSILON;
- end;
- function TGLTextureProperties.StoreTextureScale: Boolean;
- begin
- Result := Assigned(FTextureScale);
- end;
- procedure TGLTextureProperties.SetEnvColor(const AValue:
- TGLColor);
- begin
- FEnvColor.Assign(AValue);
- NotifyChange(Self);
- end;
- procedure TGLTextureProperties.UnApply(var ARci: TGLRenderContextInfo);
- begin
- if Assigned(FLibTexture) then
- begin
- FLibTexture.UnApply(ARci);
- if Assigned(FLibSampler) then
- FLibSampler.UnApply(ARci);
- if ARci.currentMaterialLevel < mlSM3 then
- begin
- if not FTextureMatrixIsIdentity and (MappingMode = tmmUser) then
- ARci.GLStates.SetGLTextureMatrix(IdentityHmgMatrix);
- UnApplyMappingMode;
- end;
- end;
- end;
- procedure TGLTextureProperties.UnApplyMappingMode;
- begin
- if MappingMode <> tmmUser then
- begin
- gl.Disable(GL_TEXTURE_GEN_S);
- gl.Disable(GL_TEXTURE_GEN_T);
- if gl.EXT_texture3D or gl.ARB_texture_cube_map then
- begin
- gl.Disable(GL_TEXTURE_GEN_R);
- gl.Disable(GL_TEXTURE_GEN_Q);
- end;
- end;
- end;
- { TVXShaderEx }
- procedure TGLShaderEx.Assign(Source: TPersistent);
- var
- LShader: TGLShaderEx;
- begin
- if Source is TGLShaderEx then
- begin
- LShader := TGLShaderEx(Source);
- FSource.Assign(LShader.Source);
- FShaderType := LShader.FShaderType;
- NotifyChange(Self);
- end;
- inherited;
- end;
- constructor TGLShaderEx.Create(AOwner: TXCollection);
- const
- cShaderClasses: array[TGLShaderType] of TGLShaderHandleClass =
- (
- TGLVertexShaderHandle,
- TGLTessControlShaderHandle,
- TGLTessEvaluationShaderHandle,
- TGLGeometryShaderHandle,
- TGLFragmentShaderHandle
- );
- var
- S: TGLShaderType;
- begin
- inherited;
- FDefferedInit := False;
- for S := Low(TGLShaderType) to High(TGLShaderType) do
- begin
- FHandle[S] := cShaderClasses[S].Create;
- FHandle[S].OnPrapare := DoOnPrepare;
- end;
- FSource := TStringList.Create;
- FSource.OnChange := NotifyChange;
- FShaderType := shtVertex;
- FGeometryInput := gsInPoints;
- FGeometryOutput := gsOutPoints;
- FGeometryVerticesOut := 1;
- Name := TGLMatLibComponents(AOwner).MakeUniqueName('Shader');
- end;
- destructor TGLShaderEx.Destroy;
- var
- S: TGLShaderType;
- begin
- for S := Low(TGLShaderType) to High(TGLShaderType) do
- FHandle[S].Destroy;
- FSource.Destroy;
- inherited;
- end;
- procedure TGLShaderEx.NotifyChange(Sender: TObject);
- var
- S: TGLShaderType;
- begin
- for S := Low(TGLShaderType) to High(TGLShaderType) do
- FHandle[S].NotifyChangesOfData;
- if (Sender = FSource) and IsDesignTime and (Length(FSourceFile) > 0) then
- FSource.SaveToFile(FSourceFile);
- inherited;
- end;
- procedure TGLShaderEx.DoOnPrepare(Sender: TGLContext);
- begin
- if not IsDesignTime and FDefferedInit then
- exit;
- try
- if FHandle[FShaderType].IsSupported then
- begin
- FHandle[FShaderType].AllocateHandle;
- if FHandle[FShaderType].IsDataNeedUpdate then
- begin
- SetExeDirectory;
- if (Length(FSourceFile) > 0) and FileStreamExists(FSourceFile) then
- FSource.LoadFromFile(FSourceFile);
- FHandle[FShaderType].ShaderSource(AnsiString(FSource.Text));
- FIsValid := FHandle[FShaderType].CompileShader;
- if IsDesignTime then
- begin
- FInfoLog := FHandle[FShaderType].InfoLog;
- if (Length(FInfoLog) = 0) and FIsValid then
- FInfoLog := 'Compilation successful';
- end
- else if FIsValid then
- GLSLogger.LogInfoFmt('Shader "%s" compilation successful - %s',
- [Name, FHandle[FShaderType].InfoLog])
- else
- GLSLogger.LogErrorFmt('Shader "%s" compilation failed - %s',
- [Name, FHandle[FShaderType].InfoLog]);
- FHandle[FShaderType].NotifyDataUpdated;
- end;
- end
- else
- begin
- FIsValid := False;
- if IsDesignTime then
- FInfoLog := 'Not supported by hardware';
- end;
- except
- on E: Exception do
- begin
- FIsValid := False;
- if IsDesignTime then
- InformationDlg(E.ClassName + ': ' + E.Message)
- else
- GLSLogger.LogError(E.ClassName + ': ' + E.Message);
- end;
- end;
- end;
- class function TGLShaderEx.FriendlyName: string;
- begin
- Result := 'GLSL Shader';
- end;
- function TGLShaderEx.GetHandle: TGLShaderHandle;
- begin
- Result := FHandle[FShaderType];
- end;
- procedure TGLShaderEx.ReadFromFiler(AReader: TReader);
- var
- archiveVersion: Integer;
- begin
- with AReader do
- begin
- archiveVersion := ReadInteger;
- if archiveVersion = 0 then
- begin
- Name := ReadString;
- FDefferedInit := ReadBoolean;
- FSource.Text := ReadString;
- FSourceFile := ReadString;
- FShaderType := TGLShaderType(ReadInteger);
- FGeometryInput := TGLgsInTypes(ReadInteger);
- FGeometryOutput := TGLgsOutTypes(ReadInteger);
- FGeometryVerticesOut := ReadInteger;
- end
- else
- RaiseFilerException(archiveVersion);
- end;
- end;
- procedure TGLShaderEx.SetGeometryInput(AValue: TGLgsInTypes);
- begin
- if AValue <> FGeometryInput then
- begin
- FGeometryInput := AValue;
- NotifyChange(Self);
- end;
- end;
- procedure TGLShaderEx.SetGeometryOutput(AValue: TGLgsOutTypes);
- begin
- if AValue <> FGeometryOutput then
- begin
- FGeometryOutput := AValue;
- NotifyChange(Self);
- end;
- end;
- procedure TGLShaderEx.SetGeometryVerticesOut(AValue: TGLint);
- begin
- if AValue < 1 then
- AValue := 1
- else if AValue > 1024 then
- AValue := 1024;
- if AValue <> FGeometryVerticesOut then
- begin
- FGeometryVerticesOut := AValue;
- NotifyChange(Self);
- end;
- end;
- procedure TGLShaderEx.SetShaderType(AValue: TGLShaderType);
- begin
- if FShaderType <> AValue then
- begin
- FShaderType := AValue;
- NotifyChange(Self);
- end;
- end;
- procedure TGLShaderEx.SetSource(AValue: TStringList);
- begin
- FSource.Assign(AValue);
- end;
- procedure TGLShaderEx.SetSourceFile(AValue: string);
- begin
- FixPathDelimiter(AValue);
- if FSourceFile <> AValue then
- begin
- FSourceFile := AValue;
- NotifyChange(Self);
- end;
- end;
- procedure TGLShaderEx.WriteToFiler(AWriter: TWriter);
- begin
- with AWriter do
- begin
- WriteInteger(0); // archive version
- WriteString(Name);
- WriteBoolean(FDefferedInit);
- if Length(FSourceFile) = 0 then
- WriteString(FSource.Text)
- else
- WriteString('');
- WriteString(FSourceFile);
- WriteInteger(Integer(FShaderType));
- WriteInteger(Integer(FGeometryInput));
- WriteInteger(Integer(FGeometryOutput));
- WriteInteger(FGeometryVerticesOut);
- end;
- end;
- { TVXLibMaterialProperty }
- function TGLLibMaterialProperty.GetMaterial: TGLLibMaterialEx;
- begin
- if Owner is TGLLibMaterialEx then
- Result := TGLLibMaterialEx(Owner)
- else if Owner is TGLLibMaterialProperty then
- Result := TGLLibMaterialProperty(Owner).GetMaterial
- else
- Result := nil;
- end;
- function TGLLibMaterialProperty.GetMaterialLibrary: TGLAbstractMaterialLibrary;
- begin
- if Owner is TGLBaseMaterialCollectionItem then
- Result := TGLBaseMaterialCollectionItem(Owner).GetMaterialLibrary
- else
- Result := GetMaterial.GetMaterialLibrary;
- end;
- function TGLLibMaterialProperty.GetMaterialLibraryEx: TGLMaterialLibraryEx;
- begin
- if Owner is TGLBaseMaterialCollectionItem then
- Result := TGLBaseMaterialCollectionItem(Owner).GetMaterialLibraryEx
- else
- Result := TGLMaterialLibraryEx(GetMaterial.GetMaterialLibrary);
- end;
- procedure TGLLibMaterialProperty.SetNextPass(const AValue: TGLLibMaterialName);
- begin
- if AValue <> FNextPassName then
- begin
- FNextPassName := AValue;
- NotifyChange(Self);
- end;
- end;
- procedure TGLLibMaterialProperty.Loaded;
- begin
- end;
- procedure TGLLibMaterialProperty.NotifyChange(Sender: TObject);
- var
- NA: IGLNotifyAble;
- begin
- if Assigned(Owner) then
- begin
- if Supports(Owner, IGLNotifyAble, NA) then
- NA.NotifyChange(Self)
- end;
- if Assigned(OnNotifyChange) then
- OnNotifyChange(Self);
- end;
- procedure TGLLibMaterialProperty.SetEnabled(AValue: Boolean);
- begin
- if FEnabled <> AValue then
- begin
- FEnabled := AValue;
- if Owner is TGLLibMaterialEx then
- GetMaterial.NotifyChange(Self);
- end;
- end;
- { TVXLibMaterialsEx }
- function TGLLibMaterialsEx.Add: TGLLibMaterialEx;
- begin
- Result := (inherited Add) as TGLLibMaterialEx;
- end;
- constructor TGLLibMaterialsEx.Create(AOwner: TComponent);
- begin
- inherited Create(AOwner, TGLLibMaterialEx);
- end;
- function TGLLibMaterialsEx.FindItemID(ID: Integer): TGLLibMaterialEx;
- begin
- Result := (inherited FindItemID(ID)) as TGLLibMaterialEx;
- end;
- function TGLLibMaterialsEx.GetItems(AIndex: Integer): TGLLibMaterialEx;
- begin
- Result := TGLLibMaterialEx(inherited Items[AIndex]);
- end;
- function TGLLibMaterialsEx.GetLibMaterialByName(
- const AName: string): TGLLibMaterialEx;
- var
- LMaterial: TGLAbstractLibMaterial;
- begin
- LMaterial := GetMaterial(AName);
- if Assigned(LMaterial) and (LMaterial is TGLLibMaterialEx) then
- Result := TGLLibMaterialEx(LMaterial)
- else
- Result := nil;
- end;
- function TGLLibMaterialsEx.IndexOf(const Item: TGLLibMaterialEx): Integer;
- var
- I: Integer;
- begin
- Result := -1;
- if Count <> 0 then
- for I := 0 to Count - 1 do
- if GetItems(I) = Item then
- begin
- Result := I;
- Exit;
- end;
- end;
- function TGLLibMaterialsEx.MaterialLibrary: TGLMaterialLibraryEx;
- begin
- Result := TGLMaterialLibraryEx(GetOwner);
- end;
- procedure TGLLibMaterialsEx.SetItems(AIndex: Integer;
- const AValue: TGLLibMaterialEx);
- begin
- inherited Items[AIndex] := AValue;
- end;
- { TVXBaseShaderModel }
- procedure TGLBaseShaderModel.Apply(var ARci: TGLRenderContextInfo);
- var
- I: Integer;
- LEvent: TOnUniformSetting;
- begin
- if FIsValid then
- begin
- FHandle.UseProgramObject;
- if FAutoFill then
- for I := FUniforms.Count - 1 downto 0 do
- TGLAbstractShaderUniform(FUniforms[I]).Apply(ARci);
- if Self is TGLShaderModel3 then
- LEvent := GetMaterial.FOnSM3UniformSetting
- else if Self is TGLShaderModel4 then
- LEvent := GetMaterial.FOnSM4UniformSetting
- else if Self is TGLShaderModel5 then
- LEvent := GetMaterial.FOnSM5UniformSetting
- else
- LEvent := nil;
- if Assigned(LEvent) then
- LEvent(Self, ARci);
- end;
- end;
- procedure TGLBaseShaderModel.Assign(Source: TPersistent);
- var
- SM: TGLBaseShaderModel;
- begin
- if Source is TGLBaseShaderModel then
- begin
- SM := TGLBaseShaderModel(Source);
- LibVertexShaderName := SM.LibVertexShaderName;
- LibFragmentShaderName := SM.LibFragmentShaderName;
- LibGeometryShaderName := SM.LibGeometryShaderName;
- LibTessControlShaderName := SM.LibTessControlShaderName;
- LibTessEvalShaderName := SM.LibTessEvalShaderName;
- end;
- inherited;
- end;
- constructor TGLBaseShaderModel.Create(AOwner: TPersistent);
- begin
- inherited;
- FHandle := TGLProgramHandle.Create;
- FHandle.OnPrapare := DoOnPrepare;
- FEnabled := False;
- FUniforms := TGLPersistentObjectList.Create;
- FAutoFill := True;
- end;
- procedure TGLBaseShaderModel.DefineProperties(Filer: TFiler);
- begin
- inherited;
- Filer.DefineBinaryProperty(
- 'Uniforms',
- ReadUniforms,
- WriteUniforms,
- FUniforms.Count > 0);
- end;
- destructor TGLBaseShaderModel.Destroy;
- begin
- FHandle.Destroy;
- LibVertexShaderName := '';
- LibFragmentShaderName := '';
- LibGeometryShaderName := '';
- LibTessControlShaderName := '';
- LibTessEvalShaderName := '';
- FUniforms.CleanFree;
- inherited;
- end;
- procedure TGLBaseShaderModel.DoOnPrepare(Sender: TGLContext);
- var
- T: TGLShaderType;
- LUniforms: TGLPersistentObjectList;
- LUniform, LUniform2: TGLShaderUniform;
- ID: Cardinal;
- I, J, C: Integer;
- buff: array[0..255] of AnsiChar;
- Size: TGLInt;
- Len: TGLsizei;
- Loc: TGLint;
- AType: Cardinal;
- UName: string;
- GLSLData: TGLSLDataType;
- GLSLSampler: TGLSLSamplerType;
- bSampler: Boolean;
- bNew: Boolean;
- LEvent: TOnUniformInitialize;
- begin
- if FEnabled then
- try
- if IsSupported and FHandle.IsSupported then
- begin
- FHandle.AllocateHandle;
- if FHandle.IsDataNeedUpdate then
- begin
- // Validate shaders
- for T := Low(TGLShaderType) to High(TGLShaderType) do
- if Assigned(FShaders[T]) then
- begin
- FShaders[T].DoOnPrepare(Sender);
- if not FShaders[T].IsValid then
- begin
- if IsDesignTime then
- FInfoLog := Format('%s shader "%s" is invalid',
- [cShaderTypeName[FShaders[T].ShaderType],
- FShaders[T].Name]);
- FIsValid := False;
- exit;
- end;
- end;
- // Gather shader
- FHandle.DetachAllObject;
- for T := Low(TGLShaderType) to High(TGLShaderType) do
- if Assigned(FShaders[T]) then
- FHandle.AttachObject(FShaders[T].Handle);
- ID := FHandle.Handle;
- begin
- // Can be override by layouts in shader
- if Assigned(FShaders[shtGeometry]) then
- begin
- gl.ProgramParameteri(ID, GL_GEOMETRY_INPUT_TYPE_EXT,
- cGLgsInTypes[FShaders[shtGeometry].GeometryInput]);
- gl.ProgramParameteri(ID, GL_GEOMETRY_OUTPUT_TYPE_EXT,
- cGLgsOutTypes[FShaders[shtGeometry].GeometryOutput]);
- gl.ProgramParameteri(ID, GL_GEOMETRY_VERTICES_OUT_EXT,
- FShaders[shtGeometry].GeometryVerticesOut);
- end;
- if FHandle.LinkProgram then
- begin
- // Get final values
- if Assigned(FShaders[shtGeometry]) then
- begin
- gl.GetProgramiv(ID, GL_GEOMETRY_INPUT_TYPE_EXT, @AType);
- case AType of
- GL_POINTS: FShaders[shtGeometry].FGeometryInput := gsInPoints;
- GL_LINES: FShaders[shtGeometry].FGeometryInput := gsInLines;
- GL_LINES_ADJACENCY_EXT: FShaders[shtGeometry].FGeometryInput
- := gsInAdjLines;
- GL_TRIANGLES: FShaders[shtGeometry].FGeometryInput :=
- gsInTriangles;
- GL_TRIANGLES_ADJACENCY_EXT:
- FShaders[shtGeometry].FGeometryInput := gsInAdjTriangles;
- end;
- gl.GetProgramiv(ID, GL_GEOMETRY_OUTPUT_TYPE_EXT, @AType);
- case AType of
- GL_POINTS: FShaders[shtGeometry].FGeometryOutput :=
- gsOutPoints;
- GL_LINE_STRIP: FShaders[shtGeometry].FGeometryOutput :=
- gsOutLineStrip;
- GL_TRIANGLE_STRIP: FShaders[shtGeometry].FGeometryOutput :=
- sOutTriangleStrip;
- end;
- gl.GetProgramiv(ID, GL_GEOMETRY_VERTICES_OUT_EXT, @I);
- if I > 0 then
- FShaders[shtGeometry].FGeometryVerticesOut := I;
- gl.ClearError;
- end;
- // Get uniforms
- LUniforms := TGLPersistentObjectList.Create;
- gl.GetProgramiv(ID, GL_ACTIVE_UNIFORMS, @C);
- for I := 0 to C - 1 do
- begin
- gl.GetActiveUniform(
- ID,
- Cardinal(I),
- Length(buff),
- @Len,
- @Size,
- @AType,
- @buff[0]);
- Loc := gl.GetUniformLocation(ID, @buff[0]);
- if Loc < 0 then
- continue;
- UName := Copy(string(buff), 0, Len);
- GLSLData := GLSLTypeUndefined;
- GLSLSampler := GLSLSamplerUndefined;
- case AType of
- GL_FLOAT: GLSLData := GLSLType1F;
- GL_FLOAT_VEC2: GLSLData := GLSLType2F;
- GL_FLOAT_VEC3: GLSLData := GLSLType3F;
- GL_FLOAT_VEC4: GLSLData := GLSLType4F;
- GL_INT: GLSLData := GLSLType1I;
- GL_INT_VEC2: GLSLData := GLSLType2I;
- GL_INT_VEC3: GLSLData := GLSLType3I;
- GL_INT_VEC4: GLSLData := GLSLType4I;
- GL_UNSIGNED_INT: GLSLData := GLSLType1UI;
- GL_UNSIGNED_INT_VEC2: GLSLData := GLSLType2UI;
- GL_UNSIGNED_INT_VEC3: GLSLData := GLSLType3UI;
- GL_UNSIGNED_INT_VEC4: GLSLData := GLSLType4UI;
- GL_BOOL: GLSLData := GLSLType1I;
- GL_BOOL_VEC2: GLSLData := GLSLType2I;
- GL_BOOL_VEC3: GLSLData := GLSLType3I;
- GL_BOOL_VEC4: GLSLData := GLSLType4I;
- GL_FLOAT_MAT2: GLSLData := GLSLTypeMat2F;
- GL_FLOAT_MAT3: GLSLData := GLSLTypeMat3F;
- GL_FLOAT_MAT4: GLSLData := GLSLTypeMat4F;
- //------------------------------------------------------------------------------
- GL_SAMPLER_1D: GLSLSampler := GLSLSampler1D;
- GL_SAMPLER_2D: GLSLSampler := GLSLSampler2D;
- GL_SAMPLER_3D: GLSLSampler := GLSLSampler3D;
- GL_SAMPLER_CUBE: GLSLSampler := GLSLSamplerCube;
- GL_SAMPLER_1D_SHADOW: GLSLSampler := GLSLSampler1DShadow;
- GL_SAMPLER_2D_SHADOW: GLSLSampler := GLSLSampler2DShadow;
- GL_SAMPLER_2D_RECT: GLSLSampler := GLSLSamplerRect;
- GL_SAMPLER_2D_RECT_SHADOW: GLSLSampler :=
- GLSLSamplerRectShadow;
- GL_SAMPLER_BUFFER: GLSLSampler := GLSLSamplerBuffer;
- GL_INT_SAMPLER_2D_RECT: GLSLSampler :=
- GLSLIntSamplerRect;
- GL_INT_SAMPLER_BUFFER: GLSLSampler :=
- GLSLIntSamplerBuffer;
- GL_UNSIGNED_INT_SAMPLER_1D: GLSLSampler :=
- GLSLUIntSampler1D;
- GL_UNSIGNED_INT_SAMPLER_2D: GLSLSampler :=
- GLSLUIntSampler2D;
- GL_UNSIGNED_INT_SAMPLER_3D: GLSLSampler :=
- GLSLUIntSampler3D;
- GL_UNSIGNED_INT_SAMPLER_CUBE: GLSLSampler :=
- GLSLUIntSamplerCube;
- GL_UNSIGNED_INT_SAMPLER_1D_ARRAY: GLSLSampler :=
- GLSLUIntSampler1DArray;
- GL_UNSIGNED_INT_SAMPLER_2D_ARRAY: GLSLSampler :=
- GLSLUIntSampler2DArray;
- GL_UNSIGNED_INT_SAMPLER_2D_RECT: GLSLSampler :=
- GLSLUIntSamplerRect;
- GL_UNSIGNED_INT_SAMPLER_BUFFER: GLSLSampler :=
- GLSLUIntSamplerBuffer;
- GL_SAMPLER_2D_MULTISAMPLE: GLSLSampler :=
- GLSLSamplerMS;
- GL_INT_SAMPLER_2D_MULTISAMPLE: GLSLSampler :=
- GLSLIntSamplerMS;
- GL_UNSIGNED_INT_SAMPLER_2D_MULTISAMPLE: GLSLSampler :=
- GLSLUIntSamplerMS;
- GL_SAMPLER_2D_MULTISAMPLE_ARRAY: GLSLSampler :=
- GLSLSamplerMSArray;
- GL_INT_SAMPLER_2D_MULTISAMPLE_ARRAY: GLSLSampler :=
- GLSLIntSamplerMSArray;
- GL_UNSIGNED_INT_SAMPLER_2D_MULTISAMPLE_ARRAY: GLSLSampler :=
- GLSLUIntSamplerMSArray;
- end;
- bSampler := False;
- if (GLSLData = GLSLTypeUndefined) and (GLSLSampler =
- GLSLSamplerUndefined) then
- begin
- GLSLogger.LogWarningFmt(
- 'Detected active uniform "%s" with unknown type', [UName]);
- continue;
- end
- else if GLSLData <> GLSLTypeUndefined then
- begin
- GLSLogger.LogInfoFmt('Detected active uniform: %s %s',
- [cGLSLTypeString[GLSLData], UName]);
- end
- else
- begin
- bSampler := True;
- GLSLogger.LogInfoFmt('Detected active uniform: %s %s',
- [cGLSLSamplerString[GLSLSampler], UName]);
- end;
- // Find already existing uniform
- bNew := True;
- for J := 0 to FUniforms.Count - 1 do
- begin
- if not (FUniforms[J] is TGLShaderUniform) then
- continue;
- LUniform := TGLShaderUniform(FUniforms[J]);
- if not Assigned(LUniform) then
- continue;
- if LUniform.Name = UName then
- begin
- if bSampler and (LUniform is TGLShaderUniformTexture) then
- begin
- if TGLShaderUniformTexture(LUniform).FSamplerType =
- GLSLSampler then
- begin
- LUniform.FLocation := Loc;
- LUniform.FType := GLSLType1I;
- TGLShaderUniformTexture(LUniform).FTarget :=
- cSamplerToTexture[GLSLSampler];
- LUniforms.Add(LUniform);
- FUniforms[J] := nil;
- bNew := False;
- break;
- end
- end
- else
- begin
- if LUniform.FType = GLSLData then
- begin
- if (LUniform is TGLShaderUniformDSA)
- and not GL.EXT_direct_state_access then
- begin
- LUniform2 := LUniform;
- LUniform := TGLShaderUniform.Create(Self);
- LUniform._AddRef;
- LUniform.Assign(LUniform2);
- LUniform2._Release;
- end;
- LUniform.FLocation := Loc;
- LUniforms.Add(LUniform);
- FUniforms[J] := nil;
- bNew := False;
- break;
- end;
- end;
- end;
- end; // for J
- if bNew then
- begin
- // Creates new uniform
- if bSampler then
- begin
- LUniform := TGLShaderUniformTexture.Create(Self);
- LUniform.FType := GLSLType1I;
- TGLShaderUniformTexture(LUniform).FSamplerType :=
- GLSLSampler;
- TGLShaderUniformTexture(LUniform).FTarget :=
- cSamplerToTexture[GLSLSampler];
- end
- else
- begin
- if GL.EXT_direct_state_access then
- LUniform := TGLShaderUniformDSA.Create(Self)
- else
- LUniform := TGLShaderUniform.Create(Self);
- LUniform.FType := GLSLData;
- end;
- LUniform._AddRef;
- LUniform.FName := UName;
- LUniform.FNameHashCode := ComputeNameHashKey(UName);
- LUniform.FLocation := Loc;
- LUniforms.Add(LUniform);
- end;
- end; // for I
- // Clean old unused uniforms
- ReleaseUniforms(FUniforms);
- // Assign new one
- FUniforms := LUniforms;
- FHandle.NotifyDataUpdated;
- FIsValid := True;
- if Self is TGLShaderModel3 then
- LEvent := GetMaterial.FOnSM3UniformInit
- else if Self is TGLShaderModel4 then
- LEvent := GetMaterial.FOnSM4UniformInit
- else if Self is TGLShaderModel5 then
- LEvent := GetMaterial.FOnSM5UniformInit
- else
- LEvent := nil;
- if Assigned(LEvent) then
- LEvent(Self);
- end // if LinkProgram
- else
- FIsValid := False;
- end; // with GL
- if IsDesignTime then
- begin
- FInfoLog := FHandle.InfoLog;
- if (Length(FInfoLog) = 0) and FIsValid then
- FInfoLog := 'Link successful';
- end
- else if FIsValid then
- GLSLogger.LogInfoFmt('Program "%s" link successful - %s',
- [GetMaterial.Name, FHandle.InfoLog])
- else
- GLSLogger.LogErrorFmt('Program "%s" link failed! - %s',
- [GetMaterial.Name, FHandle.InfoLog]);
- end;
- end
- else
- begin
- if IsDesignTime then
- FInfoLog := 'Not supported by hardware';
- FIsValid := False;
- end;
- except
- on E: Exception do
- begin
- FIsValid := False;
- if IsDesignTime then
- InformationDlg(E.ClassName + ': ' + E.Message)
- else
- GLSLogger.LogError(E.ClassName + ': ' + E.Message);
- end;
- end;
- end;
- procedure TGLBaseShaderModel.Notification(Sender: TObject; Operation:
- TOperation);
- var
- st: TGLShaderType;
- begin
- if Operation = opRemove then
- begin
- for st := Low(TGLShaderType) to High(TGLShaderType) do
- if FShaders[st] = Sender then
- begin
- FShaders[st] := nil;
- FLibShaderName[st] := '';
- NotifyChange(Self);
- exit;
- end;
- end;
- end;
- procedure TGLBaseShaderModel.NotifyChange(Sender: TObject);
- begin
- FHandle.NotifyChangesOfData;
- inherited;
- end;
- procedure TGLBaseShaderModel.ReadUniforms(AStream: TStream);
- var
- LReader: TReader;
- N, I: Integer;
- str: string;
- LUniform: TGLAbstractShaderUniform;
- LClass: CGLAbstractShaderUniform;
- begin
- LReader := TReader.Create(AStream, 16384);
- try
- N := LReader.ReadInteger;
- for I := 0 to N - 1 do
- begin
- str := LReader.ReadString;
- LClass := CGLAbstractShaderUniform(FindClass(str));
- LUniform := LClass.Create(Self);
- LUniform._AddRef;
- LUniform.ReadFromFiler(LReader);
- FUniforms.Add(LUniform);
- end;
- finally
- LReader.Free;
- end;
- end;
- class procedure TGLBaseShaderModel.ReleaseUniforms(
- AList: TGLPersistentObjectList);
- var
- I: Integer;
- begin
- for I := 0 to AList.Count - 1 do
- if Assigned(AList[I]) then
- TGLAbstractShaderUniform(AList[I])._Release;
- AList.Destroy;
- end;
- function TGLBaseShaderModel.GetLibShaderName(AType: TGLShaderType): string;
- begin
- if Assigned(FShaders[AType]) then
- Result := FShaders[AType].Name
- else
- Result := '';
- end;
- function TGLBaseShaderModel.GetUniform(const AName: string): IShaderParameter;
- var
- H, I: Integer;
- U: TGLAbstractShaderUniform;
- begin
- Result := nil;
- H := ComputeNameHashKey(AName);
- for I := 0 to FUniforms.Count - 1 do
- begin
- U := TGLAbstractShaderUniform(FUniforms[I]);
- if (U.FNameHashCode = H) and (U.FName = AName) then
- begin
- Result := U;
- exit;
- end;
- end;
- if not IsDesignTime then
- begin
- GLSLogger.LogErrorFmt('Attempt to use unknow uniform "%s" for material "%s"',
- [AName, GetMaterial.Name]);
- U := TGLAbstractShaderUniform.Create(Self);
- U._AddRef;
- U.FName := AName;
- U.FNameHashCode := H;
- FUniforms.Add(U);
- Result := U;
- end;
- end;
- procedure TGLBaseShaderModel.Loaded;
- var
- T: TGLShaderType;
- I: Integer;
- begin
- for T := Low(TGLShaderType) to High(TGLShaderType) do
- SetLibShaderName(T, FLibShaderName[T]);
- for I := 0 to FUniforms.Count - 1 do
- if FUniforms[I] is TGLShaderUniformTexture then
- TGLShaderUniformTexture(FUniforms[I]).Loaded;
- end;
- procedure TGLBaseShaderModel.GetUniformNames(Proc: TGetStrProc);
- var
- I: Integer;
- begin
- for I := 0 to FUniforms.Count - 1 do
- Proc(TGLAbstractShaderUniform(FUniforms[I]).FName);
- end;
- procedure TGLBaseShaderModel.SetLibShaderName(AType: TGLShaderType;
- const AValue: string);
- var
- LShader: TGLShaderEx;
- begin
- if csLoading in GetMaterialLibraryEx.ComponentState then
- begin
- FLibShaderName[AType] := AValue;
- exit;
- end;
- if Assigned(FShaders[AType]) then
- begin
- FShaders[AType].UnregisterUser(Self);
- FShaders[AType] := nil;
- FLibShaderName[AType] := '';
- end;
- LShader := GetMaterialLibraryEx.Components.GetShaderByName(AValue);
- if Assigned(LShader) then
- begin
- if LShader.ShaderType <> AType then
- begin
- if IsDesignTime then
- InformationDlg(Format('Incompatible shader type, need %s shader',
- [cShaderTypeName[AType]]));
- exit;
- end;
- LShader.RegisterUser(Self);
- FShaders[AType] := LShader;
- FLibShaderName[AType] := AValue;
- end;
- NotifyChange(Self);
- end;
- procedure TGLBaseShaderModel.UnApply(var ARci: TGLRenderContextInfo);
- begin
- if FIsValid {and not ARci.GLStates.ForwardContext} then
- FHandle.EndUseProgramObject;
- end;
- procedure TGLBaseShaderModel.WriteUniforms(AStream: TStream);
- var
- LWriter: TWriter;
- I: Integer;
- begin
- LWriter := TWriter.Create(AStream, 16384);
- try
- LWriter.WriteInteger(FUniforms.Count);
- for I := 0 to FUniforms.Count - 1 do
- begin
- LWriter.WriteString(FUniforms[I].ClassName);
- TGLAbstractShaderUniform(FUniforms[I]).WriteToFiler(LWriter);
- end;
- finally
- LWriter.Free;
- end;
- end;
- class function TGLShaderModel3.IsSupported: Boolean;
- begin
- Result := gl.ARB_shader_objects;
- end;
- class function TGLShaderModel4.IsSupported: Boolean;
- begin
- Result := gl.EXT_gpu_shader4;
- end;
- class function TGLShaderModel5.IsSupported: Boolean;
- begin
- Result := gl.ARB_gpu_shader5;
- end;
- procedure BeginPatch(mode: Cardinal);{$IFDEF MSWINDOWS} stdcall{$ELSE}cdecl{$ENDIF};
- begin
- if mode = GL_PATCHES then
- vStoreBegin(GL_PATCHES)
- else if (mode = GL_TRIANGLES)
- or (mode = GL_TRIANGLE_STRIP)
- or (mode = GL_TRIANGLE_FAN)
- or (mode = GL_QUADS) then
- begin
- if mode = GL_QUADS then
- gl.PatchParameteri(GL_PATCH_VERTICES, 4)
- else
- gl.PatchParameteri(GL_PATCH_VERTICES, 3);
- vStoreBegin(GL_PATCHES);
- end
- else
- begin
- gl.Begin_ := vStoreBegin;
- GLSLogger.LogError('glBegin called with unsupported primitive for tessellation');
- Abort;
- end;
- end;
- procedure TGLShaderModel5.Apply(var ARci: TGLRenderContextInfo);
- begin
- if Assigned(FShaders[shtControl]) or Assigned(FShaders[shtEvaluation]) then
- begin
- vStoreBegin := gl.Begin_;
- gl.Begin_ := BeginPatch;
- ARci.amalgamating := True;
- end;
- inherited;
- end;
- procedure TGLShaderModel5.UnApply(var ARci: TGLRenderContextInfo);
- begin
- inherited;
- if Assigned(FShaders[shtControl]) or Assigned(FShaders[shtEvaluation]) then
- gl.Begin_ := vStoreBegin;
- ARci.amalgamating := False;
- end;
- { TVXMatLibComponents }
- function TGLMatLibComponents.GetAttachmentByName(
- const AName: TGLMaterialComponentName): TGLFrameBufferAttachment;
- var
- N, I: Integer;
- begin
- N := ComputeNameHashKey(AName);
- for I := 0 to Count - 1 do
- begin
- if (Items[I] is TGLFrameBufferAttachment) and (Items[I].FNameHashKey = N)
- then
- begin
- if Items[I].Name = AName then
- begin
- Result := TGLFrameBufferAttachment(Items[I]);
- exit;
- end;
- end;
- end;
- Result := nil;
- end;
- function TGLMatLibComponents.GetCombinerByName(
- const AName: TGLMaterialComponentName): TGLTextureCombiner;
- var
- N, I: Integer;
- begin
- N := ComputeNameHashKey(AName);
- for I := 0 to Count - 1 do
- begin
- if (Items[I] is TGLTextureCombiner) and (Items[I].FNameHashKey = N) then
- begin
- if Items[I].Name = AName then
- begin
- Result := TGLTextureCombiner(Items[I]);
- exit;
- end;
- end;
- end;
- Result := nil;
- end;
- function TGLMatLibComponents.GetItemByName(
- const AName: TGLMaterialComponentName): TGLBaseMaterialCollectionItem;
- var
- N, I: Integer;
- begin
- N := ComputeNameHashKey(AName);
- for I := 0 to Count - 1 do
- begin
- if (Items[I].FNameHashKey = N) and (Items[I].Name = AName) then
- begin
- Result := Items[I];
- exit;
- end;
- end;
- Result := nil;
- end;
- function TGLMatLibComponents.GetItems(
- index: Integer): TGLBaseMaterialCollectionItem;
- begin
- Result := TGLBaseMaterialCollectionItem(inherited GetItems(index));
- end;
- function TGLMatLibComponents.GetNamePath: string;
- var
- s: string;
- begin
- Result := ClassName;
- if GetOwner = nil then
- Exit;
- s := GetOwner.GetNamePath;
- if s = '' then
- Exit;
- Result := s + '.Components';
- end;
- function TGLMatLibComponents.GetSamplerByName(
- const AName: TGLMaterialComponentName): TGLTextureSampler;
- var
- N, I: Integer;
- begin
- N := ComputeNameHashKey(AName);
- for I := 0 to Count - 1 do
- begin
- if (Items[I] is TGLTextureSampler) and (Items[I].FNameHashKey = N) then
- begin
- if Items[I].Name = AName then
- begin
- Result := TGLTextureSampler(Items[I]);
- exit;
- end;
- end;
- end;
- Result := nil;
- end;
- function TGLMatLibComponents.GetShaderByName(
- const AName: TGLMaterialComponentName): TGLShaderEx;
- var
- N, I: Integer;
- begin
- N := ComputeNameHashKey(AName);
- for I := 0 to Count - 1 do
- begin
- if (Items[I] is TGLShaderEx) and (Items[I].FNameHashKey = N) then
- begin
- if Items[I].Name = AName then
- begin
- Result := TGLShaderEx(Items[I]);
- exit;
- end;
- end;
- end;
- Result := nil;
- end;
- function TGLMatLibComponents.GetAsmProgByName(
- const AName: TGLMaterialComponentName): TGLASMVertexProgram;
- var
- N, I: Integer;
- begin
- N := ComputeNameHashKey(AName);
- for I := 0 to Count - 1 do
- begin
- if (Items[I] is TGLASMVertexProgram) and (Items[I].FNameHashKey = N) then
- begin
- if Items[I].Name = AName then
- begin
- Result := TGLASMVertexProgram(Items[I]);
- exit;
- end;
- end;
- end;
- Result := nil;
- end;
- function TGLMatLibComponents.GetTextureByName(
- const AName: TGLMaterialComponentName): TGLAbstractTexture;
- var
- N, I: Integer;
- begin
- N := ComputeNameHashKey(AName);
- for I := 0 to Count - 1 do
- begin
- if (Items[I] is TGLAbstractTexture) and (Items[I].FNameHashKey = N) then
- begin
- if Items[I].Name = AName then
- begin
- Result := TGLTextureImageEx(Items[I]);
- exit;
- end;
- end;
- end;
- Result := nil;
- end;
- class function TGLMatLibComponents.ItemsClass: TXCollectionItemClass;
- begin
- Result := TGLBaseMaterialCollectionItem;
- end;
- function TGLMatLibComponents.MakeUniqueName(const AName:
- TGLMaterialComponentName): TGLMaterialComponentName;
- var
- I: Integer;
- begin
- Result := AName;
- I := 1;
- while GetItemByName(Result) <> nil do
- begin
- Result := AName + IntToStr(i);
- Inc(i);
- end;
- end;
- { TVXMaterialLibraryEx }
- function TGLMaterialLibraryEx.AddAttachment(
- const AName: TGLMaterialComponentName): TGLFrameBufferAttachment;
- begin
- Result := TGLFrameBufferAttachment.Create(Components);
- Result.Name := AName;
- Components.Add(Result);
- end;
- function TGLMaterialLibraryEx.AddCombiner(
- const AName: TGLMaterialComponentName): TGLTextureCombiner;
- begin
- Result := TGLTextureCombiner.Create(Components);
- Result.Name := AName;
- Components.Add(Result);
- end;
- function TGLMaterialLibraryEx.AddSampler(
- const AName: TGLMaterialComponentName): TGLTextureSampler;
- begin
- Result := TGLTextureSampler.Create(Components);
- Result.Name := AName;
- Components.Add(Result);
- end;
- function TGLMaterialLibraryEx.AddShader(
- const AName: TGLMaterialComponentName): TGLShaderEx;
- begin
- Result := TGLShaderEx.Create(Components);
- Result.Name := AName;
- Components.Add(Result);
- end;
- function TGLMaterialLibraryEx.AddAsmProg(
- const AName: TGLMaterialComponentName): TGLASMVertexProgram;
- begin
- Result := TGLASMVertexProgram.Create(Components);
- Result.Name := AName;
- Components.Add(Result);
- end;
- function TGLMaterialLibraryEx.AddTexture(
- const AName: TGLMaterialComponentName): TGLTextureImageEx;
- begin
- Result := TGLTextureImageEx.Create(Components);
- Result.Name := AName;
- Components.Add(Result);
- end;
- constructor TGLMaterialLibraryEx.Create(AOwner: TComponent);
- begin
- inherited;
- FMaterials := TGLLibMaterialsEx.Create(Self);
- FComponents := TGLMatLibComponents.Create(Self);
- end;
- procedure TGLMaterialLibraryEx.DefineProperties(Filer: TFiler);
- begin
- Filer.DefineBinaryProperty(
- 'ComponentsData',
- ReadComponents,
- WriteComponents,
- Components.Count > 0);
- inherited;
- end;
- destructor TGLMaterialLibraryEx.Destroy;
- begin
- FMaterials.Destroy;
- FComponents.Destroy;
- inherited;
- end;
- function TGLMaterialLibraryEx.GetMaterials: TGLLibMaterialsEx;
- begin
- Result := TGLLibMaterialsEx(FMaterials);
- end;
- procedure TGLMaterialLibraryEx.GetNames(Proc: TGetStrProc;
- AClass: CGLBaseMaterialCollectionItem);
- var
- I: Integer;
- begin
- for I := 0 to Components.Count - 1 do
- if Components[I].ClassType = AClass then
- Proc(Components[I].Name)
- end;
- procedure TGLMaterialLibraryEx.Loaded;
- begin
- inherited;
- end;
- procedure TGLMaterialLibraryEx.ReadComponents(AStream: TStream);
- var
- LReader: TReader;
- begin
- LReader := TReader.Create(AStream, 16384);
- try
- Components.ReadFromFiler(LReader);
- finally
- LReader.Free;
- end;
- end;
- procedure TGLMaterialLibraryEx.SetComponents(AValue: TGLMatLibComponents);
- begin
- FComponents.Assign(AValue);
- end;
- procedure TGLMaterialLibraryEx.SetLevelForAll(const ALevel: TGLMaterialLevel);
- var
- I: Integer;
- begin
- for I := Materials.Count - 1 downto 0 do
- Materials[I].ApplicableLevel := ALevel;
- end;
- procedure TGLMaterialLibraryEx.SetMaterials(AValue: TGLLibMaterialsEx);
- begin
- FMaterials.Assign(AValue);
- end;
- function TGLMaterialLibraryEx.StoreMaterials: Boolean;
- begin
- Result := (FMaterials.Count > 0);
- end;
- procedure TGLMaterialLibraryEx.WriteComponents(AStream: TStream);
- var
- LWriter: TWriter;
- begin
- LWriter := TWriter.Create(AStream, 16384);
- try
- Components.WriteToFiler(LWriter);
- finally
- LWriter.Free;
- end;
- end;
- { TVXShaderUniformTexture }
- procedure TGLShaderUniformTexture.Apply(var ARci: TGLRenderContextInfo);
- function FindHotActiveUnit: Boolean;
- var
- ID: Cardinal;
- I, J: Integer;
- bindTime, minTime: Double;
- LTex: TGLTextureImageEx;
- begin
- with ARci.GLStates do
- begin
- if Assigned(FLibTexture) and FLibTexture.IsValid then
- begin
- ID := FLibTexture.FHandle.Handle;
- // Yar: may be need exract this to new method of TGLTextureImageEx ???
- if FLibTexture is TGLTextureImageEx then
- begin
- LTex := TGLTextureImageEx(FLibTexture);
- Inc(LTex.FApplyCounter);
- if LTex.FApplyCounter > 16 then
- FreeAndNil(LTex.FImage);
- end;
- end
- else
- ID := 0;
- // Find alredy binded texture unit
- for I := 0 to MaxTextureImageUnits - 1 do
- begin
- if TextureBinding[I, FTarget] = ID then
- begin
- gl.Uniform1i(FLocation, I);
- ActiveTexture := I;
- Result := True;
- exit;
- end;
- end;
- // Find unused texture unit
- for I := 0 to MaxTextureImageUnits - 1 do
- begin
- if TextureBinding[I, FTarget] = 0 then
- begin
- TextureBinding[I, FTarget] := ID;
- gl.Uniform1i(FLocation, I);
- ActiveTexture := I;
- Result := True;
- exit;
- end;
- end;
- // Find most useless texture unit
- minTime := AppTime;
- J := 0;
- for I := 0 to MaxTextureImageUnits - 1 do
- begin
- bindTime := TextureBindingTime[I, FTarget];
- if bindTime < minTime then
- begin
- minTime := bindTime;
- J := I;
- end;
- end;
- TextureBinding[J, FTarget] := ID;
- ActiveTexture := J;
- gl.Uniform1i(FLocation, J);
- Result := True;
- exit;
- end;
- Result := False;
- end;
- var
- glTarget: Cardinal;
- begin
- if FLocation > -1 then
- begin
- if FindHotActiveUnit and Assigned(FLibTexture) and Assigned(FLibSampler)
- then
- begin
- // Apply swizzling if possible
- glTarget := DecodeTextureTarget(FLibTexture.Shape);
- if GL.ARB_texture_swizzle or GL.EXT_texture_swizzle then
- begin
- if FSwizzling[0] <> FLibTexture.FSwizzles[0] then
- begin
- FLibTexture.FSwizzles[0] := FSwizzling[0];
- gl.TexParameteri(glTarget, GL_TEXTURE_SWIZZLE_R,
- cTextureSwizzle[FSwizzling[0]]);
- end;
- if FSwizzling[1] <> FLibTexture.FSwizzles[1] then
- begin
- FLibTexture.FSwizzles[1] := FSwizzling[1];
- gl.TexParameteri(glTarget, GL_TEXTURE_SWIZZLE_G,
- cTextureSwizzle[FSwizzling[1]]);
- end;
- if FSwizzling[2] <> FLibTexture.FSwizzles[2] then
- begin
- FLibTexture.FSwizzles[2] := FSwizzling[2];
- gl.TexParameteri(glTarget, GL_TEXTURE_SWIZZLE_B,
- cTextureSwizzle[FSwizzling[2]]);
- end;
- if FSwizzling[3] <> FLibTexture.FSwizzles[3] then
- begin
- FLibTexture.FSwizzles[3] := FSwizzling[3];
- gl.TexParameteri(glTarget, GL_TEXTURE_SWIZZLE_A,
- cTextureSwizzle[FSwizzling[3]]);
- end;
- end;
- if FLibSampler.IsValid then
- FLibSampler.Apply(ARci)
- else if FLibTexture.FLastSampler <> FLibSampler then
- begin
- // Sampler object not supported, lets use texture states
- gl.TexParameterfv(glTarget, GL_TEXTURE_BORDER_COLOR,
- FLibSampler.BorderColor.AsAddress);
- gl.TexParameteri(glTarget, GL_TEXTURE_WRAP_S,
- cTextureWrapMode[FLibSampler.WrapX]);
- gl.TexParameteri(glTarget, GL_TEXTURE_WRAP_T,
- cTextureWrapMode[FLibSampler.WrapY]);
- gl.TexParameteri(glTarget, GL_TEXTURE_WRAP_R,
- cTextureWrapMode[FLibSampler.WrapZ]);
- gl.TexParameterf(glTarget, GL_TEXTURE_LOD_BIAS, FLibSampler.LODBias +
- FLibSampler.FLODBiasFract);
- gl.TexParameteri(glTarget, GL_TEXTURE_MIN_FILTER,
- cTextureMinFilter[FLibSampler.MinFilter]);
- gl.TexParameteri(glTarget, GL_TEXTURE_MAG_FILTER,
- cTextureMagFilter[FLibSampler.MagFilter]);
- if GL.EXT_texture_filter_anisotropic then
- begin
- if FLibSampler.FilteringQuality = tfAnisotropic then
- gl.TexParameteri(glTarget, GL_TEXTURE_MAX_ANISOTROPY_EXT,
- CurrentGLContext.GLStates.MaxTextureAnisotropy)
- else
- gl.TexParameteri(glTarget, GL_TEXTURE_MAX_ANISOTROPY_EXT, 1);
- end;
- gl.TexParameteri(glTarget, GL_TEXTURE_COMPARE_MODE,
- cTextureCompareMode[FLibSampler.CompareMode]);
- gl.TexParameteri(glTarget, GL_TEXTURE_COMPARE_FUNC,
- cGLComparisonFunctionToGLEnum[FLibSampler.CompareFunc]);
- if GL.EXT_texture_sRGB_decode then
- begin
- if FLibSampler.sRGB_Encode then
- gl.TexParameteri(glTarget, GL_TEXTURE_SRGB_DECODE_EXT, GL_DECODE_EXT)
- else
- gl.TexParameteri(glTarget, GL_TEXTURE_SRGB_DECODE_EXT,
- GL_SKIP_DECODE_EXT);
- end;
- FLibTexture.FLastSampler := FLibSampler;
- end;
- end;
- end;
- end;
- procedure TGLShaderUniformTexture.Assign(Source: TPersistent);
- var
- LUniform: TGLShaderUniformTexture;
- begin
- if Source is TGLShaderUniformTexture then
- begin
- LUniform := TGLShaderUniformTexture(Source);
- LibTextureName := LUniform.LibTextureName;
- LibSamplerName := LUniform.LibSamplerName;
- end;
- inherited;
- end;
- constructor TGLShaderUniformTexture.Create(AOwner: TPersistent);
- begin
- inherited;
- FSwizzling := cDefaultSwizzleVector;
- end;
- destructor TGLShaderUniformTexture.Destroy;
- begin
- LibTextureName := '';
- LibSamplerName := '';
- inherited;
- end;
- function TGLShaderUniformTexture.GetSamplerName: string;
- begin
- if Assigned(FLibSampler) then
- Result := FLibSampler.Name
- else
- Result := strNothing;
- end;
- function TGLShaderUniformTexture.GetTextureName: string;
- begin
- if Assigned(FLibTexture) then
- Result := FLibTexture.Name
- else
- Result := strNothing;
- end;
- function TGLShaderUniformTexture.GetTextureSwizzle: TSwizzleVector;
- begin
- Result := FSwizzling;
- end;
- procedure TGLShaderUniformTexture.Loaded;
- begin
- SetTextureName(FLibTexureName);
- SetSamplerName(FLibSamplerName);
- end;
- procedure TGLShaderUniformTexture.Notification(Sender: TObject;
- Operation: TOperation);
- begin
- if Operation = opRemove then
- begin
- if Sender = FLibTexture then
- FLibTexture := nil
- else if Sender = FLibSampler then
- FLibSampler := nil;
- end;
- end;
- procedure TGLShaderUniformTexture.ReadFromFiler(AReader: TReader);
- begin
- with AReader do
- begin
- inherited;
- LibTextureName := ReadString;
- LibSamplerName := ReadString;
- FSwizzling[0] := TGLTextureSwizzle(ReadInteger);
- FSwizzling[1] := TGLTextureSwizzle(ReadInteger);
- FSwizzling[2] := TGLTextureSwizzle(ReadInteger);
- FSwizzling[3] := TGLTextureSwizzle(ReadInteger);
- end;
- end;
- procedure TGLShaderUniformTexture.SetTextureName(
- const AValue: string);
- var
- LTexture: TGLAbstractTexture;
- begin
- if csLoading in TGLBaseShaderModel(Owner).GetMaterialLibraryEx.ComponentState
- then
- begin
- FLibTexureName := AValue;
- exit;
- end;
- if Assigned(FLibTexture) then
- begin
- if FLibTexture.Name = AValue then
- exit;
- FLibTexture.UnregisterUser(Self);
- FLibTexture := nil;
- end;
- LTexture :=
- TGLBaseShaderModel(Owner).GetMaterialLibraryEx.Components.GetTextureByName(AValue);
- if Assigned(LTexture) then
- begin
- if LTexture is TGLFrameBufferAttachment then
- begin
- if TGLFrameBufferAttachment(LTexture).OnlyWrite then
- begin
- if IsDesignTime then
- InformationDlg('Can not use write only attachment as texture')
- else
- GLSLogger.LogErrorFmt('Attempt to write only attachment "%s" for uniform "%s"',
- [LTexture.Name, Name]);
- NotifyChange(Self);
- exit;
- end;
- end;
- LTexture.RegisterUser(Self);
- FLibTexture := LTexture;
- end;
- NotifyChange(Self);
- end;
- procedure TGLShaderUniformTexture.SetSamplerName(const AValue: string);
- var
- LSampler: TGLTextureSampler;
- begin
- if csLoading in TGLBaseShaderModel(Owner).GetMaterialLibraryEx.ComponentState
- then
- begin
- FLibSamplerName := AValue;
- exit;
- end;
- if Assigned(FLibSampler) then
- begin
- if FLibSampler.Name = AValue then
- exit;
- FLibSampler.UnregisterUser(Self);
- FLibSampler := nil;
- end;
- LSampler :=
- TGLBaseShaderModel(Owner).GetMaterialLibraryEx.Components.GetSamplerByName(AValue);
- if Assigned(LSampler) then
- begin
- LSampler.RegisterUser(Self);
- FLibSampler := LSampler;
- end;
- NotifyChange(Self);
- end;
- procedure TGLShaderUniformTexture.SetTextureSwizzle(const AValue:
- TSwizzleVector);
- begin
- FSwizzling := AValue;
- end;
- procedure TGLShaderUniformTexture.WriteToFiler(AWriter: TWriter);
- begin
- with AWriter do
- begin
- inherited;
- WriteString(LibTextureName);
- WriteString(LibSamplerName);
- WriteInteger(Integer(FSwizzling[0]));
- WriteInteger(Integer(FSwizzling[1]));
- WriteInteger(Integer(FSwizzling[2]));
- WriteInteger(Integer(FSwizzling[3]));
- end;
- end;
- { TVXAbstractShaderUniform }
- function TGLAbstractShaderUniform.GetFloat: Single;
- begin
- FillChar(Result, SizeOf(Result), $00);
- end;
- function TGLAbstractShaderUniform.GetGLSLSamplerType: TGLSLSamplerType;
- begin
- Result := FSamplerType;
- end;
- function TGLAbstractShaderUniform.GetGLSLType: TGLSLDataType;
- begin
- Result := FType;
- end;
- function TGLAbstractShaderUniform.GetInt: TGLint;
- begin
- FillChar(Result, SizeOf(Result), $00);
- end;
- function TGLAbstractShaderUniform.GetIVec2: TVector2i;
- begin
- FillChar(Result, SizeOf(Result), $00);
- end;
- function TGLAbstractShaderUniform.GetIVec3: TVector3i;
- begin
- FillChar(Result, SizeOf(Result), $00);
- end;
- function TGLAbstractShaderUniform.GetIVec4: TVector4i;
- begin
- FillChar(Result, SizeOf(Result), $00);
- end;
- function TGLAbstractShaderUniform.GetMat2: TMatrix2f;
- begin
- FillChar(Result, SizeOf(Result), $00);
- end;
- function TGLAbstractShaderUniform.GetMat3: TMatrix3f;
- begin
- FillChar(Result, SizeOf(Result), $00);
- end;
- function TGLAbstractShaderUniform.GetMat4: TMatrix4f;
- begin
- FillChar(Result, SizeOf(Result), $00);
- end;
- function TGLAbstractShaderUniform.GetName: string;
- begin
- Result := FName;
- end;
- function TGLAbstractShaderUniform.GetSamplerName: string;
- begin
- Result := strNothing;
- end;
- procedure TGLAbstractShaderUniform.Apply(var ARci: TGLRenderContextInfo);
- begin
- end;
- function TGLAbstractShaderUniform.GetAutoSetMethod: string;
- begin
- Result := strNothing;
- end;
- function TGLAbstractShaderUniform.GetTextureName: string;
- begin
- Result := strNothing;
- end;
- function TGLAbstractShaderUniform.GetTextureSwizzle: TSwizzleVector;
- begin
- Result := cDefaultSwizzleVector;
- end;
- function TGLAbstractShaderUniform.GetUInt: Cardinal;
- begin
- FillChar(Result, SizeOf(Result), $00);
- end;
- function TGLAbstractShaderUniform.GetUVec2: TVector2ui;
- begin
- FillChar(Result, SizeOf(Result), $00);
- end;
- function TGLAbstractShaderUniform.GetUVec3: TVector3ui;
- begin
- FillChar(Result, SizeOf(Result), $00);
- end;
- function TGLAbstractShaderUniform.GetUVec4: TVector4ui;
- begin
- FillChar(Result, SizeOf(Result), $00);
- end;
- function TGLAbstractShaderUniform.GetVec2: TVector2f;
- begin
- FillChar(Result, SizeOf(Result), $00);
- end;
- function TGLAbstractShaderUniform.GetVec3: TVector3f;
- begin
- FillChar(Result, SizeOf(Result), $00);
- end;
- function TGLAbstractShaderUniform.GetVec4: TGLVector;
- begin
- FillChar(Result, SizeOf(Result), $00);
- end;
- procedure TGLAbstractShaderUniform.ReadFromFiler(AReader: TReader);
- begin
- end;
- procedure TGLAbstractShaderUniform.SetFloat(const Value: TGLFloat);
- begin
- end;
- procedure TGLAbstractShaderUniform.SetFloatArray(const Values: PGLFloat;
- Count: Integer);
- begin
- end;
- procedure TGLAbstractShaderUniform.SetInt(const Value: Integer);
- begin
- end;
- procedure TGLAbstractShaderUniform.SetIntArray(const Values: PGLInt;
- Count: Integer);
- begin
- end;
- procedure TGLAbstractShaderUniform.SetIVec2(const Value: TVector2i);
- begin
- end;
- procedure TGLAbstractShaderUniform.SetIVec3(const Value: TVector3i);
- begin
- end;
- procedure TGLAbstractShaderUniform.SetIVec4(const Value: TVector4i);
- begin
- end;
- procedure TGLAbstractShaderUniform.SetMat2(const Value: TMatrix2f);
- begin
- end;
- procedure TGLAbstractShaderUniform.SetMat3(const Value: TMatrix3f);
- begin
- end;
- procedure TGLAbstractShaderUniform.SetMat4(const Value: TMatrix4f);
- begin
- end;
- procedure TGLAbstractShaderUniform.SetSamplerName(const AValue: string);
- begin
- end;
- procedure TGLAbstractShaderUniform.SetAutoSetMethod(const AValue: string);
- begin
- end;
- procedure TGLAbstractShaderUniform.SetTextureName(const AValue: string);
- begin
- end;
- procedure TGLAbstractShaderUniform.SetTextureSwizzle(const AValue:
- TSwizzleVector);
- begin
- end;
- procedure TGLAbstractShaderUniform.SetUInt(const Value: Cardinal);
- begin
- end;
- procedure TGLAbstractShaderUniform.SetUIntArray(const Values: PGLUInt;
- Count: Integer);
- begin
- end;
- procedure TGLAbstractShaderUniform.SetUVec2(const Value: TVector2ui);
- begin
- end;
- procedure TGLAbstractShaderUniform.SetUVec3(const Value: TVector3ui);
- begin
- end;
- procedure TGLAbstractShaderUniform.SetUVec4(const Value: TVector4ui);
- begin
- end;
- procedure TGLAbstractShaderUniform.SetVec2(const Value: TVector2f);
- begin
- end;
- procedure TGLAbstractShaderUniform.SetVec3(const Value: TVector3f);
- begin
- end;
- procedure TGLAbstractShaderUniform.SetVec4(const Value: TVector4f);
- begin
- end;
- procedure TGLAbstractShaderUniform.WriteToFiler(AWriter: TWriter);
- begin
- end;
- { TVXShaderUniform }
- function TGLShaderUniform.GetFloat: Single;
- begin
- // TODO: Type checking
- gl.GetUniformfv(GetProgram, FLocation, @Result);
- end;
- function TGLShaderUniform.GetInt: TGLint;
- begin
- gl.GetUniformiv(GetProgram, FLocation, @Result);
- end;
- function TGLShaderUniform.GetIVec2: TVector2i;
- begin
- gl.GetUniformiv(GetProgram, FLocation, @Result);
- end;
- function TGLShaderUniform.GetIVec3: TVector3i;
- begin
- gl.GetUniformiv(GetProgram, FLocation, @Result);
- end;
- function TGLShaderUniform.GetIVec4: TVector4i;
- begin
- gl.GetUniformiv(GetProgram, FLocation, @Result);
- end;
- function TGLShaderUniform.GetMat2: TMatrix2f;
- begin
- gl.GetUniformfv(GetProgram, FLocation, @Result);
- end;
- function TGLShaderUniform.GetMat3: TMatrix3f;
- begin
- gl.GetUniformfv(GetProgram, FLocation, @Result);
- end;
- function TGLShaderUniform.GetMat4: TMatrix4f;
- begin
- gl.GetUniformfv(GetProgram, FLocation, @Result);
- end;
- function TGLShaderUniform.GetProgram: Cardinal;
- begin
- Result := TGLBaseShaderModel(Owner).FHandle.Handle;
- end;
- procedure TGLShaderUniform.Apply(var ARci: TGLRenderContextInfo);
- begin
- if Assigned(FAutoSet) then
- FAutoSet(Self, ARci);
- end;
- procedure TGLShaderUniform.Assign(Source: TPersistent);
- var
- LUniform: TGLShaderUniform;
- begin
- if Source is TGLShaderUniform then
- begin
- LUniform := TGLShaderUniform(Source);
- FName := LUniform.Name;
- FNameHashCode := LUniform.FNameHashCode;
- FType := LUniform.FType;
- FSamplerType := LUniform.FSamplerType;
- FAutoSet := LUniform.FAutoSet;
- end;
- inherited;
- end;
- function TGLShaderUniform.GetAutoSetMethod: string;
- begin
- Result := GetUniformAutoSetMethodName(FAutoSet);
- end;
- function TGLShaderUniform.GetUInt: Cardinal;
- begin
- gl.GetUniformuiv(GetProgram, FLocation, @Result);
- end;
- function TGLShaderUniform.GetUVec2: TVector2ui;
- begin
- gl.GetUniformuiv(GetProgram, FLocation, @Result);
- end;
- function TGLShaderUniform.GetUVec3: TVector3ui;
- begin
- gl.GetUniformuiv(GetProgram, FLocation, @Result);
- end;
- function TGLShaderUniform.GetUVec4: TVector4ui;
- begin
- gl.GetUniformuiv(GetProgram, FLocation, @Result);
- end;
- function TGLShaderUniform.GetVec2: TVector2f;
- begin
- gl.GetUniformfv(GetProgram, FLocation, @Result);
- end;
- function TGLShaderUniform.GetVec3: TVector3f;
- begin
- gl.GetUniformfv(GetProgram, FLocation, @Result);
- end;
- function TGLShaderUniform.GetVec4: TGLVector;
- begin
- gl.GetUniformfv(GetProgram, FLocation, @Result);
- end;
- procedure TGLShaderUniform.PopProgram;
- begin
- CurrentGLContext.GLStates.CurrentProgram := FStoreProgram;
- end;
- procedure TGLShaderUniform.PushProgram;
- begin
- with CurrentGLContext.GLStates do
- begin
- FStoreProgram := CurrentProgram;
- CurrentProgram := GetProgram;
- end;
- end;
- procedure TGLShaderUniform.ReadFromFiler(AReader: TReader);
- begin
- with AReader do
- begin
- FName := ReadString;
- FNameHashCode := ComputeNameHashKey(FName);
- FType := TGLSLDataType(ReadInteger);
- FSamplerType := TGLSLSamplerType(ReadInteger);
- SetAutoSetMethod(ReadString);
- end;
- end;
- procedure TGLShaderUniform.SetFloat(const Value: TGLFloat);
- begin
- PushProgram;
- gl.Uniform1f(FLocation, Value);
- PopProgram;
- end;
- procedure TGLShaderUniform.SetFloatArray(const Values: PGLFloat;
- Count: Integer);
- begin
- PushProgram;
- gl.Uniform1fv(FLocation, Count, Values);
- PopProgram;
- end;
- procedure TGLShaderUniform.SetInt(const Value: Integer);
- begin
- PushProgram;
- gl.Uniform1i(FLocation, Value);
- PopProgram;
- end;
- procedure TGLShaderUniform.SetIntArray(const Values: PGLInt; Count: Integer);
- begin
- PushProgram;
- gl.Uniform1iv(FLocation, Count, Values);
- PopProgram;
- end;
- procedure TGLShaderUniform.SetIVec2(const Value: TVector2i);
- begin
- PushProgram;
- gl.Uniform2i(FLocation, Value.X, Value.Y);
- PopProgram;
- end;
- procedure TGLShaderUniform.SetIVec3(const Value: TVector3i);
- begin
- PushProgram;
- gl.Uniform3i(FLocation, Value.X, Value.Y, Value.Z);
- PopProgram;
- end;
- procedure TGLShaderUniform.SetIVec4(const Value: TVector4i);
- begin
- PushProgram;
- gl.Uniform4i(FLocation, Value.X, Value.Y, Value.Z, Value.W);
- PopProgram;
- end;
- procedure TGLShaderUniform.SetMat2(const Value: TMatrix2f);
- begin
- PushProgram;
- gl.UniformMatrix2fv(FLocation, 1, False, @Value);
- PopProgram;
- end;
- procedure TGLShaderUniform.SetMat3(const Value: TMatrix3f);
- begin
- PushProgram;
- gl.UniformMatrix2fv(FLocation, 1, False, @Value);
- PopProgram;
- end;
- procedure TGLShaderUniform.SetMat4(const Value: TMatrix4f);
- begin
- PushProgram;
- gl.UniformMatrix4fv(FLocation, 1, False, @Value);
- PopProgram;
- end;
- procedure TGLShaderUniform.SetAutoSetMethod(const AValue: string);
- begin
- FAutoSet := GetUniformAutoSetMethod(AValue);
- end;
- procedure TGLShaderUniform.SetUInt(const Value: Cardinal);
- begin
- PushProgram;
- gl.Uniform1ui(FLocation, Value);
- PopProgram;
- end;
- procedure TGLShaderUniform.SetUIntArray(const Values: PGLUInt; Count: Integer);
- begin
- PushProgram;
- gl.Uniform1uiv(FLocation, Count, Values);
- PopProgram;
- end;
- procedure TGLShaderUniform.SetUVec2(const Value: TVector2ui);
- begin
- PushProgram;
- gl.Uniform2ui(FLocation, Value.X, Value.Y);
- PopProgram;
- end;
- procedure TGLShaderUniform.SetUVec3(const Value: TVector3ui);
- begin
- PushProgram;
- gl.Uniform3ui(FLocation, Value.X, Value.Y, Value.Z);
- PopProgram;
- end;
- procedure TGLShaderUniform.SetUVec4(const Value: TVector4ui);
- begin
- PushProgram;
- gl.Uniform4ui(FLocation, Value.X, Value.Y, Value.Z, Value.W);
- PopProgram;
- end;
- procedure TGLShaderUniform.SetVec2(const Value: TVector2f);
- begin
- PushProgram;
- gl.Uniform2f(FLocation, Value.X, Value.Y);
- PopProgram;
- end;
- procedure TGLShaderUniform.SetVec3(const Value: TVector3f);
- begin
- PushProgram;
- gl.Uniform3f(FLocation, Value.X, Value.Y, Value.Z);
- PopProgram;
- end;
- procedure TGLShaderUniform.SetVec4(const Value: TVector4f);
- begin
- PushProgram;
- gl.Uniform4f(FLocation, Value.X, Value.Y, Value.Z, Value.W);
- PopProgram;
- end;
- procedure TGLShaderUniform.WriteToFiler(AWriter: TWriter);
- begin
- with AWriter do
- begin
- WriteString(FName);
- WriteInteger(Integer(FType));
- WriteInteger(Integer(FSamplerType));
- WriteString(GetAutoSetMethod);
- end;
- end;
- { TVXShaderUniformDSA }
- procedure TGLShaderUniformDSA.SetFloat(const Value: TGLFloat);
- begin
- gl.ProgramUniform1f(GetProgram, FLocation, Value);
- end;
- procedure TGLShaderUniformDSA.SetFloatArray(const Values: PGLFloat;
- Count: Integer);
- begin
- gl.ProgramUniform1fv(GetProgram, FLocation, Count, Values);
- end;
- procedure TGLShaderUniformDSA.SetInt(const Value: Integer);
- begin
- gl.ProgramUniform1i(GetProgram, FLocation, Value);
- end;
- procedure TGLShaderUniformDSA.SetIntArray(const Values: PGLInt; Count: Integer);
- begin
- gl.ProgramUniform1iv(GetProgram, FLocation, Count, Values);
- end;
- procedure TGLShaderUniformDSA.SetIVec2(const Value: TVector2i);
- begin
- gl.ProgramUniform2i(GetProgram, FLocation, Value.X, Value.Y);
- end;
- procedure TGLShaderUniformDSA.SetIVec3(const Value: TVector3i);
- begin
- gl.ProgramUniform3i(GetProgram, FLocation, Value.X, Value.Y, Value.Z);
- end;
- procedure TGLShaderUniformDSA.SetIVec4(const Value: TVector4i);
- begin
- gl.ProgramUniform4i(GetProgram, FLocation, Value.X, Value.Y, Value.Z,
- Value.W);
- end;
- procedure TGLShaderUniformDSA.SetMat2(const Value: TMatrix2f);
- begin
- gl.ProgramUniformMatrix2fv(GetProgram, FLocation, 1, False, @Value);
- end;
- procedure TGLShaderUniformDSA.SetMat3(const Value: TMatrix3f);
- begin
- gl.ProgramUniformMatrix3fv(GetProgram, FLocation, 1, False, @Value);
- end;
- procedure TGLShaderUniformDSA.SetMat4(const Value: TMatrix4f);
- begin
- gl.ProgramUniformMatrix4fv(GetProgram, FLocation, 1, False, @Value);
- end;
- procedure TGLShaderUniformDSA.SetUInt(const Value: Cardinal);
- begin
- gl.ProgramUniform1ui(GetProgram, FLocation, Value);
- end;
- procedure TGLShaderUniformDSA.SetUIntArray(const Values: PGLUInt;
- Count: Integer);
- begin
- gl.ProgramUniform1uiv(GetProgram, FLocation, Count, Values);
- end;
- procedure TGLShaderUniformDSA.SetUVec2(const Value: TVector2ui);
- begin
- gl.ProgramUniform2ui(GetProgram, FLocation, Value.X, Value.Y);
- end;
- procedure TGLShaderUniformDSA.SetUVec3(const Value: TVector3ui);
- begin
- gl.ProgramUniform3ui(GetProgram, FLocation, Value.X, Value.Y, Value.Z);
- end;
- procedure TGLShaderUniformDSA.SetUVec4(const Value: TVector4ui);
- begin
- gl.ProgramUniform4ui(GetProgram, FLocation, Value.X, Value.Y, Value.Z,
- Value.W);
- end;
- procedure TGLShaderUniformDSA.SetVec2(const Value: TVector2f);
- begin
- gl.ProgramUniform2f(GetProgram, FLocation, Value.X, Value.Y);
- end;
- procedure TGLShaderUniformDSA.SetVec3(const Value: TVector3f);
- begin
- gl.ProgramUniform3f(GetProgram, FLocation, Value.X, Value.Y, Value.Z);
- end;
- procedure TGLShaderUniformDSA.SetVec4(const Value: TVector4f);
- begin
- gl.ProgramUniform4f(GetProgram, FLocation, Value.X, Value.Y, Value.Z,
- Value.W);
- end;
- { TVXTextureSwizzling }
- procedure TGLTextureSwizzling.Assign(Source: TPersistent);
- var
- LSwizzling: TGLTextureSwizzling;
- begin
- if Source is TGLTextureSwizzling then
- begin
- LSwizzling := TGLTextureSwizzling(Source);
- FSwizzles[0] := LSwizzling.FSwizzles[0];
- FSwizzles[1] := LSwizzling.FSwizzles[1];
- FSwizzles[2] := LSwizzling.FSwizzles[2];
- FSwizzles[3] := LSwizzling.FSwizzles[3];
- end;
- inherited;
- end;
- constructor TGLTextureSwizzling.Create(AOwner: TPersistent);
- begin
- inherited;
- FSwizzles := cDefaultSwizzleVector;
- end;
- function TGLTextureSwizzling.GetSwizzle(AIndex: Integer): TGLTextureSwizzle;
- begin
- Result := FSwizzles[AIndex];
- end;
- procedure TGLTextureSwizzling.ReadFromFiler(AReader: TReader);
- begin
- with AReader do
- begin
- ReadInteger;
- FSwizzles[0] := TGLTextureSwizzle(ReadInteger);
- FSwizzles[1] := TGLTextureSwizzle(ReadInteger);
- FSwizzles[2] := TGLTextureSwizzle(ReadInteger);
- FSwizzles[3] := TGLTextureSwizzle(ReadInteger);
- end;
- end;
- procedure TGLTextureSwizzling.SetSwizzle(AIndex: Integer;
- AValue: TGLTextureSwizzle);
- begin
- if AValue <> FSwizzles[AIndex] then
- begin
- FSwizzles[AIndex] := AValue;
- NotifyChange(Self);
- end;
- end;
- function TGLTextureSwizzling.StoreSwizzle(AIndex: Integer): Boolean;
- begin
- Result := (FSwizzles[AIndex] <> cDefaultSwizzleVector[AIndex]);
- end;
- procedure TGLTextureSwizzling.WriteToFiler(AWriter: TWriter);
- begin
- with AWriter do
- begin
- WriteInteger(0);
- WriteInteger(Integer(FSwizzles[0]));
- WriteInteger(Integer(FSwizzles[1]));
- WriteInteger(Integer(FSwizzles[2]));
- WriteInteger(Integer(FSwizzles[3]));
- end;
- end;
- { TVXFrameBufferAttachment }
- procedure TGLFrameBufferAttachment.Apply(var ARci: TGLRenderContextInfo);
- begin
- if FIsValid and not FOnlyWrite then
- begin
- // Just bind
- with ARci.GLStates do
- begin
- ActiveTextureEnabled[FHandle.Target] := True;
- TextureBinding[ActiveTexture, FHandle.Target] := FHandle.Handle;
- end;
- end
- else
- ARci.GLStates.TextureBinding[ARci.GLStates.ActiveTexture, FHandle.Target] :=
- 0;
- end;
- procedure TGLFrameBufferAttachment.Assign(Source: TPersistent);
- var
- LAttachment: TGLFrameBufferAttachment;
- begin
- if Source is TGLFrameBufferAttachment then
- begin
- LAttachment := TGLFrameBufferAttachment(Source);
- FLayered := LAttachment.Layered;
- FCubeMap := LAttachment.CubeMap;
- FSamples := LAttachment.Samples;
- FOnlyWrite := LAttachment.OnlyWrite;
- FFixedSamplesLocation := LAttachment.FixedSamplesLocation;
- FWidth := LAttachment.InternalWidth;
- FHeight := LAttachment.InternalHeight;
- FDepth := LAttachment.InternalDepth;
- FInternalFormat := LAttachment.InternalFormat;
- NotifyChange(Self);
- end;
- inherited;
- end;
- constructor TGLFrameBufferAttachment.Create(AOwner: TXCollection);
- begin
- inherited;
- FDefferedInit := False;
- FHandle := TGLTextureHandle.Create;
- FHandle.OnPrapare := DoOnPrepare;
- FRenderBufferHandle := TGLRenderbufferHandle.Create;
- FRenderBufferHandle.OnPrapare := DoOnPrepare;
- FInternalFormat := tfRGBA8;
- FWidth := 256;
- FHeight := 256;
- FDepth := 0;
- FSamples := -1;
- FLayered := False;
- FCubeMap := False;
- FOnlyWrite := False;
- FFixedSamplesLocation := False;
- Name := TGLMatLibComponents(AOwner).MakeUniqueName('Attachment');
- end;
- destructor TGLFrameBufferAttachment.Destroy;
- begin
- FHandle.Destroy;
- FRenderBufferHandle.Destroy;
- inherited;
- end;
- procedure TGLFrameBufferAttachment.DoOnPrepare(Sender: TGLContext);
- var
- LTarget: TGLTextureTarget;
- w, h, d, s, Level, MaxLevel: Integer;
- glTarget, glFormat, glFace: Cardinal;
- begin
- if IsDesignTime and FDefferedInit then
- exit;
- FHandle.AllocateHandle;
- FRenderBufferHandle.AllocateHandle;
- if not (FHandle.IsDataNeedUpdate or FRenderBufferHandle.IsDataNeedUpdate) then
- exit;
- // Target
- if FSamples < 0 then
- begin
- LTarget := ttTexture2D;
- if FHeight = 1 then
- LTarget := ttTexture1D;
- if FCubeMap then
- LTarget := ttTextureCube;
- if FDepth > 0 then
- LTarget := ttTexture3D;
- if FLayered then
- begin
- if FDepth < 2 then
- LTarget := ttTexture1DArray
- else
- LTarget := ttTexture2DArray;
- if FCubeMap then
- LTarget := ttTextureCubeArray;
- end;
- end
- else
- begin
- if FDepth > 0 then
- LTarget := ttTexture2DMultisampleArray
- else
- LTarget := ttTexture2DMultisample;
- end;
- // Check target support
- if FOnlyWrite and (LTarget = ttTexture2DMultisample)
- and not Sender.gl.EXT_framebuffer_multisample then
- begin
- FIsValid := False;
- exit;
- end;
- if not IsTargetSupported(LTarget) then
- begin
- FIsValid := False;
- exit;
- end;
- // Adjust dimension
- w := FWidth;
- h := FHeight;
- d := FDepth;
- s := FSamples;
- if FCubeMap then
- begin
- if w > Integer(Sender.GLStates.MaxCubeTextureSize) then
- w := Sender.GLStates.MaxCubeTextureSize;
- h := w;
- if FLayered then
- begin
- if d < 6 then
- d := 6
- else if (d mod 6) > 0 then
- d := 6 * (d div 6 + 1);
- end;
- end
- else if w > Integer(Sender.GLStates.MaxTextureSize) then
- w := Sender.GLStates.MaxTextureSize;
- if h > Integer(Sender.GLStates.MaxTextureSize) then
- h := Sender.GLStates.MaxTextureSize;
- if FLayered then
- begin
- if d > Integer(Sender.GLStates.MaxArrayTextureSize) then
- d := Sender.GLStates.MaxArrayTextureSize;
- end
- else if d > Integer(Sender.GLStates.Max3DTextureSize) then
- d := Sender.GLStates.Max3DTextureSize;
- if (s > -1) and (s > Integer(Sender.GLStates.MaxSamples)) then
- s := Sender.GLStates.MaxSamples;
- glTarget := DecodeTextureTarget(LTarget);
- if (FHandle.Target <> LTarget)
- and (FHandle.Target <> ttNoShape) then
- begin
- FHandle.DestroyHandle;
- FHandle.AllocateHandle;
- end;
- FHandle.Target := LTarget;
- glFormat := InternalFormatToOpenGLFormat(FInternalFormat);
- if FOnlyWrite and ((LTarget = ttTexture2D) or (LTarget =
- ttTexture2DMultisample))
- and FRenderBufferHandle.IsSupported then
- begin
- if LTarget = ttTexture2D then
- FRenderBufferHandle.SetStorage(glFormat, w, h)
- else
- FRenderBufferHandle.SetStorageMultisample(glFormat, s, w, h);
- end
- else
- with Sender do
- begin
- GLStates.ActiveTextureEnabled[FHandle.Target] := True;
- GLStates.TextureBinding[GLStates.ActiveTexture, FHandle.Target] :=
- FHandle.Handle;
- MaxLevel := CalcTextureLevelNumber(LTarget, w, h, d);
- case glTarget of
- GL_TEXTURE_1D:
- for Level := 0 to MaxLevel - 1 do
- begin
- gl.TexImage1D(glTarget, Level, glFormat, w, 0, GL_RGBA,
- GL_UNSIGNED_BYTE, nil);
- Div2(w);
- end;
- GL_TEXTURE_2D:
- for Level := 0 to MaxLevel - 1 do
- begin
- gl.TexImage2D(glTarget, Level, glFormat, w, h, 0, GL_RGBA,
- GL_UNSIGNED_BYTE, nil);
- Div2(w);
- Div2(h);
- end;
- GL_TEXTURE_RECTANGLE:
- begin
- gl.TexImage2D(glTarget, 0, glFormat, w, h, 0, GL_RGBA,
- GL_UNSIGNED_BYTE, nil);
- end;
- GL_TEXTURE_3D:
- for Level := 0 to MaxLevel - 1 do
- begin
- gl.TexImage3D(glTarget, Level, glFormat, w, h, d, 0, GL_RGBA,
- GL_UNSIGNED_BYTE, nil);
- Div2(w);
- Div2(h);
- Div2(d);
- end;
- GL_TEXTURE_CUBE_MAP:
- for Level := 0 to MaxLevel - 1 do
- begin
- for glFace := GL_TEXTURE_CUBE_MAP_POSITIVE_X to
- GL_TEXTURE_CUBE_MAP_NEGATIVE_Z do
- gl.TexImage2D(glFace, Level, glFormat, w, w, 0, GL_RGBA,
- GL_UNSIGNED_BYTE, nil);
- Div2(w);
- end;
- GL_TEXTURE_1D_ARRAY:
- for Level := 0 to MaxLevel - 1 do
- begin
- gl.TexImage2D(glTarget, Level, glFormat, w, h, 0, GL_RGBA,
- GL_UNSIGNED_BYTE, nil);
- Div2(w);
- end;
- GL_TEXTURE_2D_ARRAY:
- for Level := 0 to MaxLevel - 1 do
- begin
- gl.TexImage3D(glTarget, Level, glFormat, w, h, d, 0, GL_RGBA,
- GL_UNSIGNED_BYTE, nil);
- Div2(w);
- Div2(h);
- end;
- GL_TEXTURE_CUBE_MAP_ARRAY:
- for Level := 0 to MaxLevel - 1 do
- begin
- gl.TexImage3D(glTarget, Level, glFormat, w, w, d, 0, GL_RGBA,
- GL_UNSIGNED_BYTE, nil);
- Div2(w);
- end;
- end; // of case
- GLStates.ActiveTextureEnabled[FHandle.Target] := False;
- FOnlyWrite := False;
- end; // of texture
- if gl.GetError <> GL_NO_ERROR then
- begin
- gl.ClearError;
- GLSLogger.LogErrorFmt('Unable to create attachment "%s"', [Self.Name]);
- exit;
- end
- else
- FIsValid := True;
- FHandle.NotifyDataUpdated;
- FRenderBufferHandle.NotifyDataUpdated;
- end;
- class function TGLFrameBufferAttachment.FriendlyName: string;
- begin
- Result := 'Framebuffer Attachment';
- end;
- procedure TGLFrameBufferAttachment.NotifyChange(Sender: TObject);
- begin
- FHandle.NotifyChangesOfData;
- FRenderBufferHandle.NotifyChangesOfData;
- inherited;
- end;
- procedure TGLFrameBufferAttachment.ReadFromFiler(AReader: TReader);
- var
- archiveVersion: Integer;
- begin
- with AReader do
- begin
- archiveVersion := ReadInteger;
- if archiveVersion = 0 then
- begin
- Name := ReadString;
- FDefferedInit := ReadBoolean;
- FLayered := ReadBoolean;
- FCubeMap := ReadBoolean;
- FSamples := ReadInteger;
- FOnlyWrite := ReadBoolean;
- FFixedSamplesLocation := ReadBoolean;
- FWidth := ReadInteger;
- FHeight := ReadInteger;
- FDepth := ReadInteger;
- FInternalFormat := TGLInternalFormat(ReadInteger);
- end
- else
- RaiseFilerException(archiveVersion);
- end;
- end;
- procedure TGLFrameBufferAttachment.SetCubeMap(AValue: Boolean);
- begin
- if FCubeMap <> AValue then
- begin
- FCubeMap := AValue;
- NotifyChange(Self);
- end;
- end;
- procedure TGLFrameBufferAttachment.SetDepth(AValue: Integer);
- begin
- if FDepth < 0 then
- FDepth := 0
- else if FDepth > 256 then
- FDepth := 256;
- if FDepth <> AValue then
- begin
- FDepth := AValue;
- NotifyChange(Self);
- end;
- end;
- procedure TGLFrameBufferAttachment.SetFixedSamplesLocation(AValue: Boolean);
- begin
- if FFixedSamplesLocation <> AValue then
- begin
- FFixedSamplesLocation := AValue;
- NotifyChange(Self);
- end;
- end;
- procedure TGLFrameBufferAttachment.SetHeight(AValue: Integer);
- begin
- if FHeight < 1 then
- FHeight := 1
- else if FHeight > 8192 then
- FHeight := 8192;
- if FHeight <> AValue then
- begin
- FHeight := AValue;
- NotifyChange(Self);
- end;
- end;
- procedure TGLFrameBufferAttachment.SetInternalFormat(
- const AValue: TGLInternalFormat);
- begin
- if FInternalFormat <> AValue then
- begin
- FInternalFormat := AValue;
- NotifyChange(Self);
- end;
- end;
- procedure TGLFrameBufferAttachment.SetLayered(AValue: Boolean);
- begin
- if FLayered <> AValue then
- begin
- FLayered := AValue;
- NotifyChange(Self);
- end;
- end;
- procedure TGLFrameBufferAttachment.SetOnlyWrite(AValue: Boolean);
- begin
- if FOnlyWrite <> AValue then
- begin
- if AValue
- and ((FDepth > 0) or FLayered or FFixedSamplesLocation or FCubeMap) then
- exit;
- FOnlyWrite := AValue;
- NotifyChange(Self);
- end;
- end;
- procedure TGLFrameBufferAttachment.SetSamples(AValue: Integer);
- begin
- if AValue < -1 then
- AValue := -1;
- if FSamples <> AValue then
- begin
- FSamples := AValue;
- NotifyChange(Self);
- end;
- end;
- procedure TGLFrameBufferAttachment.SetWidth(AValue: Integer);
- begin
- if FWidth < 1 then
- FWidth := 1
- else if FWidth > 8192 then
- FWidth := 8192;
- if FWidth <> AValue then
- begin
- FWidth := AValue;
- NotifyChange(Self);
- end;
- end;
- procedure TGLFrameBufferAttachment.UnApply(var ARci: TGLRenderContextInfo);
- begin
- ARci.GLStates.ActiveTextureEnabled[FHandle.Target] := False;
- end;
- procedure TGLFrameBufferAttachment.WriteToFiler(AWriter: TWriter);
- begin
- with AWriter do
- begin
- WriteInteger(0); // archive version
- WriteString(Name);
- WriteBoolean(FDefferedInit);
- WriteBoolean(FLayered);
- WriteBoolean(FCubeMap);
- WriteInteger(FSamples);
- WriteBoolean(FOnlyWrite);
- WriteBoolean(FFixedSamplesLocation);
- WriteInteger(FWidth);
- WriteInteger(FHeight);
- WriteInteger(FDepth);
- WriteInteger(Integer(FInternalFormat));
- end;
- end;
- constructor TStandartUniformAutoSetExecutor.Create;
- begin
- RegisterUniformAutoSetMethod('Camera world position', GLSLType4F,
- SetCameraPosition);
- RegisterUniformAutoSetMethod('LightSource[0] world position', GLSLType4F,
- SetLightSource0Position);
- RegisterUniformAutoSetMethod('World (model) matrix', GLSLTypeMat4F,
- SetModelMatrix);
- RegisterUniformAutoSetMethod('WorldView matrix', GLSLTypeMat4F,
- SetModelViewMatrix);
- RegisterUniformAutoSetMethod('WorldNormal matrix', GLSLTypeMat3F,
- SetNormalModelMatrix);
- RegisterUniformAutoSetMethod('Inverse World matrix', GLSLTypeMat4F,
- SetInvModelMatrix);
- RegisterUniformAutoSetMethod('View matrix', GLSLTypeMat4F, SetViewMatrix);
- RegisterUniformAutoSetMethod('Inverse WorldView matrix', GLSLTypeMat4F,
- SetInvModelViewMatrix);
- RegisterUniformAutoSetMethod('Projection matrix', GLSLTypeMat4F,
- SetProjectionMatrix);
- RegisterUniformAutoSetMethod('ViewProjection matrix', GLSLTypeMat4F,
- SetViewProjectionMatrix);
- RegisterUniformAutoSetMethod('WorldViewProjection matrix', GLSLTypeMat4F,
- SetWorldViewProjectionMatrix);
- RegisterUniformAutoSetMethod('Material front face emission', GLSLType4F,
- SetMaterialFrontEmission);
- RegisterUniformAutoSetMethod('Material front face ambient', GLSLType4F,
- SetMaterialFrontAmbient);
- RegisterUniformAutoSetMethod('Material front face diffuse', GLSLType4F,
- SetMaterialFrontDiffuse);
- RegisterUniformAutoSetMethod('Material front face specular', GLSLType4F,
- SetMaterialFrontSpecular);
- RegisterUniformAutoSetMethod('Material front face shininess', GLSLType1F,
- SetMaterialFrontShininess);
- RegisterUniformAutoSetMethod('Material back face emission', GLSLType4F,
- SetMaterialBackEmission);
- RegisterUniformAutoSetMethod('Material back face ambient', GLSLType4F,
- SetMaterialBackAmbient);
- RegisterUniformAutoSetMethod('Material back face diffuse', GLSLType4F,
- SetMaterialBackDiffuse);
- RegisterUniformAutoSetMethod('Material back face specular', GLSLType4F,
- SetMaterialBackSpecular);
- RegisterUniformAutoSetMethod('Material back face shininess', GLSLType1F,
- SetMaterialBackShininess)
- end;
- procedure TStandartUniformAutoSetExecutor.SetCameraPosition(Sender:
- IShaderParameter; var ARci: TGLRenderContextInfo);
- begin
- Sender.vec4 := ARci.cameraPosition;
- end;
- procedure TStandartUniformAutoSetExecutor.SetInvModelMatrix(Sender:
- IShaderParameter; var ARci: TGLRenderContextInfo);
- begin
- Sender.mat4 := ARci.PipelineTransformation.InvModelMatrix^;
- end;
- procedure TStandartUniformAutoSetExecutor.SetInvModelViewMatrix(Sender:
- IShaderParameter; var ARci: TGLRenderContextInfo);
- begin
- Sender.mat4 := ARci.PipelineTransformation.InvModelViewMatrix^;
- end;
- procedure TStandartUniformAutoSetExecutor.SetLightSource0Position(Sender:
- IShaderParameter; var ARci: TGLRenderContextInfo);
- begin
- Sender.vec4 := ARci.GLStates.LightPosition[0];
- end;
- procedure TStandartUniformAutoSetExecutor.SetMaterialBackAmbient(Sender:
- IShaderParameter; var ARci: TGLRenderContextInfo);
- begin
- Sender.vec4 := ARci.GLStates.MaterialAmbient[cmBack];
- end;
- procedure TStandartUniformAutoSetExecutor.SetMaterialBackDiffuse(Sender:
- IShaderParameter; var ARci: TGLRenderContextInfo);
- begin
- Sender.vec4 := ARci.GLStates.MaterialDiffuse[cmBack];
- end;
- procedure TStandartUniformAutoSetExecutor.SetMaterialBackEmission(Sender:
- IShaderParameter; var ARci: TGLRenderContextInfo);
- begin
- Sender.vec4 := ARci.GLStates.MaterialEmission[cmBack];
- end;
- procedure TStandartUniformAutoSetExecutor.SetMaterialBackShininess(Sender:
- IShaderParameter; var ARci: TGLRenderContextInfo);
- begin
- Sender.float := ARci.GLStates.MaterialShininess[cmBack];
- end;
- procedure TStandartUniformAutoSetExecutor.SetMaterialBackSpecular(Sender:
- IShaderParameter; var ARci: TGLRenderContextInfo);
- begin
- Sender.vec4 := ARci.GLStates.MaterialSpecular[cmBack];
- end;
- procedure TStandartUniformAutoSetExecutor.SetMaterialFrontAmbient(Sender:
- IShaderParameter; var ARci: TGLRenderContextInfo);
- begin
- Sender.vec4 := ARci.GLStates.MaterialAmbient[cmFront];
- end;
- procedure TStandartUniformAutoSetExecutor.SetMaterialFrontDiffuse(Sender:
- IShaderParameter; var ARci: TGLRenderContextInfo);
- begin
- Sender.vec4 := ARci.GLStates.MaterialDiffuse[cmFront];
- end;
- procedure TStandartUniformAutoSetExecutor.SetMaterialFrontEmission(Sender:
- IShaderParameter; var ARci: TGLRenderContextInfo);
- begin
- Sender.vec4 := ARci.GLStates.MaterialEmission[cmFront];
- end;
- procedure TStandartUniformAutoSetExecutor.SetMaterialFrontShininess(Sender:
- IShaderParameter; var ARci: TGLRenderContextInfo);
- begin
- Sender.float := ARci.GLStates.MaterialShininess[cmFront];
- end;
- procedure TStandartUniformAutoSetExecutor.SetMaterialFrontSpecular(Sender:
- IShaderParameter; var ARci: TGLRenderContextInfo);
- begin
- Sender.vec4 := ARci.GLStates.MaterialSpecular[cmFront];
- end;
- procedure TStandartUniformAutoSetExecutor.SetModelMatrix(Sender:
- IShaderParameter; var ARci: TGLRenderContextInfo);
- begin
- Sender.mat4 := ARci.PipelineTransformation.ModelMatrix^;
- end;
- procedure TStandartUniformAutoSetExecutor.SetModelViewMatrix(Sender:
- IShaderParameter; var ARci: TGLRenderContextInfo);
- begin
- Sender.mat4 := ARci.PipelineTransformation.ModelViewMatrix^;
- end;
- procedure TStandartUniformAutoSetExecutor.SetNormalModelMatrix(Sender:
- IShaderParameter; var ARci: TGLRenderContextInfo);
- begin
- Sender.mat3 := ARci.PipelineTransformation.NormalModelMatrix^;
- end;
- procedure TStandartUniformAutoSetExecutor.SetProjectionMatrix(Sender:
- IShaderParameter; var ARci: TGLRenderContextInfo);
- begin
- Sender.mat4 := ARci.PipelineTransformation.ProjectionMatrix^;
- end;
- procedure TStandartUniformAutoSetExecutor.SetViewMatrix(Sender:
- IShaderParameter; var ARci: TGLRenderContextInfo);
- begin
- Sender.mat4 := ARci.PipelineTransformation.ViewMatrix^;
- end;
- procedure TStandartUniformAutoSetExecutor.SetViewProjectionMatrix(Sender:
- IShaderParameter; var ARci: TGLRenderContextInfo);
- begin
- Sender.mat4 := ARci.PipelineTransformation.ViewProjectionMatrix^;
- end;
- procedure TStandartUniformAutoSetExecutor.SetWorldViewProjectionMatrix(Sender:
- IShaderParameter; var ARci: TGLRenderContextInfo);
- begin
- Sender.mat4 := MatrixMultiply(
- ARci.PipelineTransformation.ModelViewMatrix^,
- ARci.PipelineTransformation.ProjectionMatrix^);
- end;
- { TVXASMVertexProgram }
- procedure TGLASMVertexProgram.Assign(Source: TPersistent);
- var
- LProg: TGLASMVertexProgram;
- begin
- if Source is TGLASMVertexProgram then
- begin
- LProg := TGLASMVertexProgram(Source);
- FSource.Assign(LProg.FSource);
- end;
- inherited;
- end;
- constructor TGLASMVertexProgram.Create(AOwner: TXCollection);
- begin
- inherited;
- FHandle := TGLARBVertexProgramHandle.Create;
- FHandle.OnPrapare := DoOnPrepare;
- FSource := TStringList.Create;
- FSource.OnChange := NotifyChange;
- Name := TGLMatLibComponents(AOwner).MakeUniqueName('VertexProg');
- end;
- destructor TGLASMVertexProgram.Destroy;
- begin
- FHandle.Destroy;
- FSource.Destroy;
- inherited;
- end;
- procedure TGLASMVertexProgram.DoOnPrepare(Sender: TGLContext);
- begin
- if FDefferedInit and not IsDesignTime then
- exit;
- try
- if FHandle.IsSupported then
- begin
- FHandle.AllocateHandle;
- if FHandle.IsDataNeedUpdate then
- begin
- SetExeDirectory;
- if (Length(FSourceFile) > 0) and FileStreamExists(FSourceFile) then
- FSource.LoadFromFile(FSourceFile);
- if FSource.Count > 0 then
- begin
- FHandle.LoadARBProgram(FSource.Text);
- FIsValid := FHandle.Ready;
- if IsDesignTime then
- begin
- FInfoLog := FHandle.InfoLog;
- if (Length(FInfoLog) = 0) and FIsValid then
- FInfoLog := 'Compilation successful';
- end
- else if FIsValid then
- GLSLogger.LogInfoFmt('Program "%s" compilation successful - %s',
- [Name, FHandle.InfoLog])
- else
- GLSLogger.LogErrorFmt('Program "%s" compilation failed - %s',
- [Name, FHandle.InfoLog]);
- FHandle.NotifyDataUpdated;
- end
- else
- begin
- if IsDesignTime then
- FInfoLog := 'No source'
- else
- GLSLogger.LogInfoFmt('Program "%s" has no source code', [Name]);
- FIsValid := False;
- end;
- end;
- end
- else
- begin
- FIsValid := False;
- if IsDesignTime then
- FInfoLog := 'Not supported by hardware';
- end;
- except
- on E: Exception do
- begin
- FIsValid := False;
- if IsDesignTime then
- InformationDlg(E.ClassName + ': ' + E.Message)
- else
- GLSLogger.LogError(E.ClassName + ': ' + E.Message);
- end;
- end;
- end;
- class function TGLASMVertexProgram.FriendlyName: string;
- begin
- Result := 'ASM Vertex Program';
- end;
- function TGLASMVertexProgram.GetHandle: TGLARBVertexProgramHandle;
- begin
- Result := FHandle;
- end;
- procedure TGLASMVertexProgram.NotifyChange(Sender: TObject);
- begin
- FHandle.NotifyChangesOfData;
- inherited;
- end;
- procedure TGLASMVertexProgram.ReadFromFiler(AReader: TReader);
- var
- archiveVersion: Integer;
- begin
- with AReader do
- begin
- archiveVersion := ReadInteger;
- if archiveVersion = 0 then
- begin
- Name := ReadString;
- FDefferedInit := ReadBoolean;
- FSource.Text := ReadString;
- FSourceFile := ReadString;
- end
- else
- RaiseFilerException(archiveVersion);
- end;
- end;
- procedure TGLASMVertexProgram.SetSource(AValue: TStringList);
- begin
- FSource.Assign(AValue);
- end;
- procedure TGLASMVertexProgram.SetSourceFile(AValue: string);
- begin
- FixPathDelimiter(AValue);
- if FSourceFile <> AValue then
- begin
- FSourceFile := AValue;
- NotifyChange(Self);
- end;
- end;
- procedure TGLASMVertexProgram.WriteToFiler(AWriter: TWriter);
- begin
- with AWriter do
- begin
- WriteInteger(0); // archive version
- WriteString(Name);
- WriteBoolean(FDefferedInit);
- if Length(FSourceFile) = 0 then
- WriteString(FSource.Text)
- else
- WriteString('');
- WriteString(FSourceFile);
- end;
- end;
- initialization
- RegisterClasses(
- [
- TGLTextureImageEx,
- TGLFrameBufferAttachment,
- TGLTextureSampler,
- TGLTextureCombiner,
- TGLShaderEx,
- TGLASMVertexProgram,
- TGLMaterialLibraryEx,
- TGLShaderUniform,
- TGLShaderUniformDSA,
- TGLShaderUniformTexture
- ]);
- RegisterXCollectionItemClass(TGLTextureImageEx);
- RegisterXCollectionItemClass(TGLTextureSampler);
- RegisterXCollectionItemClass(TGLFrameBufferAttachment);
- RegisterXCollectionItemClass(TGLTextureCombiner);
- RegisterXCollectionItemClass(TGLShaderEx);
- RegisterXCollectionItemClass(TGLASMVertexProgram);
- vStandartUniformAutoSetExecutor := TStandartUniformAutoSetExecutor.Create;
- finalization
- vStandartUniformAutoSetExecutor.Destroy;
- end.
|