scanlines-horizontal-apply-mask.vs 313 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243224422452246224722482249225022512252225322542255225622572258225922602261226222632264226522662267226822692270227122722273227422752276227722782279228022812282228322842285228622872288228922902291229222932294229522962297229822992300230123022303230423052306230723082309231023112312231323142315231623172318231923202321232223232324232523262327232823292330233123322333233423352336233723382339234023412342234323442345234623472348234923502351235223532354235523562357235823592360236123622363236423652366236723682369237023712372237323742375237623772378237923802381238223832384238523862387238823892390239123922393239423952396239723982399240024012402240324042405240624072408240924102411241224132414241524162417241824192420242124222423242424252426242724282429243024312432243324342435243624372438243924402441244224432444244524462447244824492450245124522453245424552456245724582459246024612462246324642465246624672468246924702471247224732474247524762477247824792480248124822483248424852486248724882489249024912492249324942495249624972498249925002501250225032504250525062507250825092510251125122513251425152516251725182519252025212522252325242525252625272528252925302531253225332534253525362537253825392540254125422543254425452546254725482549255025512552255325542555255625572558255925602561256225632564256525662567256825692570257125722573257425752576257725782579258025812582258325842585258625872588258925902591259225932594259525962597259825992600260126022603260426052606260726082609261026112612261326142615261626172618261926202621262226232624262526262627262826292630263126322633263426352636263726382639264026412642264326442645264626472648264926502651265226532654265526562657265826592660266126622663266426652666266726682669267026712672267326742675267626772678267926802681268226832684268526862687268826892690269126922693269426952696269726982699270027012702270327042705270627072708270927102711271227132714271527162717271827192720272127222723272427252726272727282729273027312732273327342735273627372738273927402741274227432744274527462747274827492750275127522753275427552756275727582759276027612762276327642765276627672768276927702771277227732774277527762777277827792780278127822783278427852786278727882789279027912792279327942795279627972798279928002801280228032804280528062807280828092810281128122813281428152816281728182819282028212822282328242825282628272828282928302831283228332834283528362837283828392840284128422843284428452846284728482849285028512852285328542855285628572858285928602861286228632864286528662867286828692870287128722873287428752876287728782879288028812882288328842885288628872888288928902891289228932894289528962897289828992900290129022903290429052906290729082909291029112912291329142915291629172918291929202921292229232924292529262927292829292930293129322933293429352936293729382939294029412942294329442945294629472948294929502951295229532954295529562957295829592960296129622963296429652966296729682969297029712972297329742975297629772978297929802981298229832984298529862987298829892990299129922993299429952996299729982999300030013002300330043005300630073008300930103011301230133014301530163017301830193020302130223023302430253026302730283029303030313032303330343035303630373038303930403041304230433044304530463047304830493050305130523053305430553056305730583059306030613062306330643065306630673068306930703071307230733074307530763077307830793080308130823083308430853086308730883089309030913092309330943095309630973098309931003101310231033104310531063107310831093110311131123113311431153116311731183119312031213122312331243125312631273128312931303131313231333134313531363137313831393140314131423143314431453146314731483149315031513152315331543155315631573158315931603161316231633164316531663167316831693170317131723173317431753176317731783179318031813182318331843185318631873188318931903191319231933194319531963197319831993200320132023203320432053206320732083209321032113212321332143215321632173218321932203221322232233224322532263227322832293230323132323233323432353236323732383239324032413242324332443245324632473248324932503251325232533254325532563257325832593260326132623263326432653266326732683269327032713272327332743275327632773278327932803281328232833284328532863287328832893290329132923293329432953296329732983299330033013302330333043305330633073308330933103311331233133314331533163317331833193320332133223323332433253326332733283329333033313332333333343335333633373338333933403341334233433344334533463347334833493350335133523353335433553356335733583359336033613362336333643365336633673368336933703371337233733374337533763377337833793380338133823383338433853386338733883389339033913392339333943395339633973398339934003401340234033404340534063407340834093410341134123413341434153416341734183419342034213422342334243425342634273428342934303431343234333434343534363437343834393440344134423443344434453446344734483449345034513452345334543455345634573458345934603461346234633464346534663467346834693470347134723473347434753476347734783479348034813482348334843485348634873488348934903491349234933494349534963497349834993500350135023503350435053506350735083509351035113512351335143515351635173518351935203521352235233524352535263527352835293530353135323533353435353536353735383539354035413542354335443545354635473548354935503551355235533554355535563557355835593560356135623563356435653566356735683569357035713572357335743575357635773578357935803581358235833584358535863587358835893590359135923593359435953596359735983599360036013602360336043605360636073608360936103611361236133614361536163617361836193620362136223623362436253626362736283629363036313632363336343635363636373638363936403641364236433644364536463647364836493650365136523653365436553656365736583659366036613662366336643665366636673668366936703671367236733674367536763677367836793680368136823683368436853686368736883689369036913692369336943695369636973698369937003701370237033704370537063707370837093710371137123713371437153716371737183719372037213722372337243725372637273728372937303731373237333734373537363737373837393740374137423743374437453746374737483749375037513752375337543755375637573758375937603761376237633764376537663767376837693770377137723773377437753776377737783779378037813782378337843785378637873788378937903791379237933794379537963797379837993800380138023803380438053806380738083809381038113812381338143815381638173818381938203821382238233824382538263827382838293830383138323833383438353836383738383839384038413842384338443845384638473848384938503851385238533854385538563857385838593860386138623863386438653866386738683869387038713872387338743875387638773878387938803881388238833884388538863887388838893890389138923893389438953896389738983899390039013902390339043905390639073908390939103911391239133914391539163917391839193920392139223923392439253926392739283929393039313932393339343935393639373938393939403941394239433944394539463947394839493950395139523953395439553956395739583959396039613962396339643965396639673968396939703971397239733974397539763977397839793980398139823983398439853986398739883989399039913992399339943995399639973998399940004001400240034004400540064007400840094010401140124013401440154016401740184019402040214022402340244025402640274028402940304031403240334034403540364037403840394040404140424043404440454046404740484049405040514052405340544055405640574058405940604061406240634064406540664067406840694070407140724073407440754076407740784079408040814082408340844085408640874088408940904091409240934094409540964097409840994100410141024103410441054106410741084109411041114112411341144115411641174118411941204121412241234124412541264127412841294130413141324133413441354136413741384139414041414142414341444145414641474148414941504151415241534154415541564157415841594160416141624163416441654166416741684169417041714172417341744175417641774178417941804181418241834184418541864187418841894190419141924193419441954196419741984199420042014202420342044205420642074208420942104211421242134214421542164217421842194220422142224223422442254226422742284229423042314232423342344235423642374238423942404241424242434244424542464247424842494250425142524253425442554256425742584259426042614262426342644265426642674268426942704271427242734274427542764277427842794280428142824283428442854286428742884289429042914292429342944295429642974298429943004301430243034304430543064307430843094310431143124313431443154316431743184319432043214322432343244325432643274328432943304331433243334334433543364337433843394340434143424343434443454346434743484349435043514352435343544355435643574358435943604361436243634364436543664367436843694370437143724373437443754376437743784379438043814382438343844385438643874388438943904391439243934394439543964397439843994400440144024403440444054406440744084409441044114412441344144415441644174418441944204421442244234424442544264427442844294430443144324433443444354436443744384439444044414442444344444445444644474448444944504451445244534454445544564457445844594460446144624463446444654466446744684469447044714472447344744475447644774478447944804481448244834484448544864487448844894490449144924493449444954496449744984499450045014502450345044505450645074508450945104511451245134514451545164517451845194520452145224523452445254526452745284529453045314532453345344535453645374538453945404541454245434544454545464547454845494550455145524553455445554556455745584559456045614562456345644565456645674568456945704571457245734574457545764577457845794580458145824583458445854586458745884589459045914592459345944595459645974598459946004601460246034604460546064607460846094610461146124613461446154616461746184619462046214622462346244625462646274628462946304631463246334634463546364637463846394640464146424643464446454646464746484649465046514652465346544655465646574658465946604661466246634664466546664667466846694670467146724673467446754676467746784679468046814682468346844685468646874688468946904691469246934694469546964697469846994700470147024703470447054706470747084709471047114712471347144715471647174718471947204721472247234724472547264727472847294730473147324733473447354736473747384739474047414742474347444745474647474748474947504751475247534754475547564757475847594760476147624763476447654766476747684769477047714772477347744775477647774778477947804781478247834784478547864787478847894790479147924793479447954796479747984799480048014802480348044805480648074808480948104811481248134814481548164817481848194820482148224823482448254826482748284829483048314832483348344835483648374838483948404841484248434844484548464847484848494850485148524853485448554856485748584859486048614862486348644865486648674868486948704871487248734874487548764877487848794880488148824883488448854886488748884889489048914892489348944895489648974898489949004901490249034904490549064907490849094910491149124913491449154916491749184919492049214922492349244925492649274928492949304931493249334934493549364937493849394940494149424943494449454946494749484949495049514952495349544955495649574958495949604961496249634964496549664967496849694970497149724973497449754976497749784979498049814982498349844985498649874988498949904991499249934994499549964997499849995000500150025003500450055006500750085009501050115012501350145015501650175018501950205021502250235024502550265027502850295030503150325033503450355036503750385039504050415042504350445045504650475048504950505051505250535054505550565057505850595060506150625063506450655066506750685069507050715072507350745075507650775078507950805081508250835084508550865087508850895090509150925093509450955096509750985099510051015102510351045105510651075108510951105111511251135114511551165117511851195120512151225123512451255126512751285129513051315132513351345135513651375138513951405141514251435144514551465147514851495150515151525153515451555156515751585159516051615162516351645165516651675168516951705171517251735174517551765177517851795180518151825183518451855186518751885189519051915192519351945195519651975198519952005201520252035204520552065207520852095210521152125213521452155216521752185219522052215222522352245225522652275228522952305231523252335234523552365237523852395240524152425243524452455246524752485249525052515252525352545255525652575258525952605261526252635264526552665267526852695270527152725273527452755276527752785279528052815282528352845285528652875288528952905291529252935294529552965297529852995300530153025303530453055306530753085309531053115312531353145315531653175318531953205321532253235324532553265327532853295330533153325333533453355336533753385339534053415342534353445345534653475348534953505351535253535354535553565357535853595360536153625363536453655366536753685369537053715372537353745375537653775378537953805381538253835384538553865387538853895390539153925393539453955396539753985399540054015402540354045405540654075408540954105411541254135414541554165417541854195420542154225423542454255426542754285429543054315432543354345435543654375438543954405441544254435444544554465447544854495450545154525453545454555456545754585459546054615462546354645465546654675468546954705471547254735474547554765477547854795480548154825483548454855486548754885489549054915492549354945495549654975498549955005501550255035504550555065507550855095510551155125513551455155516551755185519552055215522552355245525552655275528552955305531553255335534553555365537553855395540554155425543554455455546554755485549555055515552555355545555555655575558555955605561556255635564556555665567556855695570557155725573557455755576557755785579558055815582558355845585558655875588558955905591559255935594559555965597559855995600560156025603560456055606560756085609561056115612561356145615561656175618561956205621562256235624562556265627562856295630563156325633563456355636563756385639564056415642564356445645564656475648564956505651565256535654565556565657565856595660566156625663566456655666566756685669567056715672567356745675567656775678567956805681568256835684568556865687568856895690569156925693569456955696569756985699570057015702570357045705570657075708570957105711571257135714571557165717571857195720572157225723572457255726572757285729573057315732573357345735573657375738573957405741574257435744574557465747574857495750575157525753575457555756575757585759576057615762576357645765576657675768576957705771577257735774577557765777577857795780578157825783578457855786578757885789579057915792579357945795579657975798579958005801580258035804580558065807580858095810581158125813581458155816581758185819582058215822582358245825582658275828582958305831583258335834583558365837583858395840584158425843584458455846584758485849585058515852585358545855585658575858585958605861586258635864586558665867586858695870587158725873587458755876587758785879588058815882588358845885588658875888588958905891589258935894589558965897589858995900590159025903590459055906590759085909591059115912591359145915591659175918591959205921592259235924592559265927592859295930593159325933593459355936593759385939594059415942594359445945594659475948594959505951595259535954595559565957595859595960596159625963596459655966596759685969597059715972597359745975597659775978597959805981598259835984598559865987598859895990599159925993599459955996599759985999600060016002600360046005600660076008600960106011601260136014601560166017601860196020602160226023602460256026602760286029603060316032603360346035603660376038603960406041604260436044604560466047
  1. #version 150
  2. ///////////////////////////// GPL LICENSE NOTICE /////////////////////////////
  3. // crt-royale: A full-featured CRT shader, with cheese.
  4. // Copyright (C) 2014 TroggleMonkey <trogglemonkey@gmx.com>
  5. //
  6. // This program is free software; you can redistribute it and/or modify it
  7. // under the terms of the GNU General Public License as published by the Free
  8. // Software Foundation; either version 2 of the License, or any later version.
  9. //
  10. // This program is distributed in the hope that it will be useful, but WITHOUT
  11. // ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
  12. // FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
  13. // more details.
  14. //
  15. // You should have received a copy of the GNU General Public License along with
  16. // this program; if not, write to the Free Software Foundation, Inc., 59 Temple
  17. // Place, Suite 330, Boston, MA 02111-1307 USA
  18. in vec4 position;
  19. in vec2 texCoord;
  20. out Vertex {
  21. vec2 vTexCoord;
  22. vec2 video_uv;
  23. vec2 scanline_tex_uv;
  24. vec2 blur3x3_tex_uv;
  25. vec2 halation_tex_uv;
  26. vec2 scanline_texture_size_inv;
  27. vec4 mask_tile_start_uv_and_size;
  28. vec2 mask_tiles_per_screen;
  29. };
  30. uniform vec4 targetSize;
  31. uniform vec4 sourceSize[];
  32. uniform int phase;
  33. // USER SETTINGS BLOCK //
  34. #define crt_gamma 2.500000
  35. #define lcd_gamma 2.200000
  36. #define levels_contrast 1.0
  37. #define halation_weight 0.0
  38. #define diffusion_weight 0.075
  39. #define bloom_underestimate_levels 0.8
  40. #define bloom_excess 0.000000
  41. #define beam_min_sigma 0.020000
  42. #define beam_max_sigma 0.300000
  43. #define beam_spot_power 0.330000
  44. #define beam_min_shape 2.000000
  45. #define beam_max_shape 4.000000
  46. #define beam_shape_power 0.250000
  47. #define beam_horiz_filter 0.000000
  48. #define beam_horiz_sigma 0.35
  49. #define beam_horiz_linear_rgb_weight 1.000000
  50. #define convergence_offset_x_r -0.000000
  51. #define convergence_offset_x_g 0.000000
  52. #define convergence_offset_x_b 0.000000
  53. #define convergence_offset_y_r 0.000000
  54. #define convergence_offset_y_g -0.000000
  55. #define convergence_offset_y_b 0.000000
  56. #define mask_type 1.000000
  57. #define mask_sample_mode_desired 0.000000
  58. #define mask_specify_num_triads 0.000000
  59. #define mask_triad_size_desired 3.000000
  60. #define mask_num_triads_desired 480.000000
  61. #define aa_subpixel_r_offset_x_runtime -0.0
  62. #define aa_subpixel_r_offset_y_runtime 0.000000
  63. #define aa_cubic_c 0.500000
  64. #define aa_gauss_sigma 0.500000
  65. #define geom_mode_runtime 0.000000
  66. #define geom_radius 2.000000
  67. #define geom_view_dist 2.000000
  68. #define geom_tilt_angle_x 0.000000
  69. #define geom_tilt_angle_y 0.000000
  70. #define geom_aspect_ratio_x 432.000000
  71. #define geom_aspect_ratio_y 329.000000
  72. #define geom_overscan_x 1.000000
  73. #define geom_overscan_y 1.000000
  74. #define border_size 0.015
  75. #define border_darkness 2.0
  76. #define border_compress 2.500000
  77. #define interlace_bff 0.000000
  78. #define interlace_1080i 0.000000
  79. #define VERTICAL_SCANLINEStexture source[5]
  80. #define VERTICAL_SCANLINEStexture_size sourceSize[5].xy
  81. #define VERTICAL_SCANLINESvideo_size sourceSize[5].xy
  82. #define BLOOM_APPROXtexture source[3]
  83. #define BLOOM_APPROXtexture_size sourceSize[3].xy
  84. #define BLOOM_APPROXvideo_size sourceSize[3].xy
  85. #define HALATION_BLURtexture source[1]
  86. #define HALATION_BLURtexture_size sourceSize[1].xy
  87. #define HALATION_BLURvideo_size sourceSize[1].xy
  88. #ifdef INTEGRATED_GRAPHICS_COMPATIBILITY_MODE
  89. #define MASK_RESIZEtexture source[0]
  90. #else
  91. #define MASK_RESIZEtexture source[0]
  92. #endif
  93. #define MASK_RESIZEtexture_size sourceSize[0].xy
  94. #define MASK_RESIZEvideo_size sourceSize[0].xy
  95. // END USER SETTINGS BLOCK //
  96. // compatibility macros for transparently converting HLSLisms into GLSLisms
  97. #define mul(a,b) (b*a)
  98. #define lerp(a,b,c) mix(a,b,c)
  99. #define saturate(c) clamp(c, 0.0, 1.0)
  100. #define frac(x) (fract(x))
  101. #define float2 vec2
  102. #define float3 vec3
  103. #define float4 vec4
  104. #define bool2 bvec2
  105. #define bool3 bvec3
  106. #define bool4 bvec4
  107. #define float2x2 mat2x2
  108. #define float3x3 mat3x3
  109. #define float4x4 mat4x4
  110. #define float4x3 mat4x3
  111. #define float2x4 mat2x4
  112. #define IN params
  113. #define texture_size sourceSize[0].xy
  114. #define video_size sourceSize[0].xy
  115. #define output_size targetSize.xy
  116. #define frame_count phase
  117. #define static
  118. #define inline
  119. #define const
  120. #define fmod(x,y) mod(x,y)
  121. #define ddx(c) dFdx(c)
  122. #define ddy(c) dFdy(c)
  123. #define atan2(x,y) atan(y,x)
  124. #define rsqrt(c) inversesqrt(c)
  125. #define input_texture source[0]
  126. #if defined(GL_ES)
  127. #define COMPAT_PRECISION mediump
  128. #else
  129. #define COMPAT_PRECISION
  130. #endif
  131. #if __VERSION__ >= 130
  132. #define COMPAT_TEXTURE texture
  133. #else
  134. #define COMPAT_TEXTURE texture2D
  135. #endif
  136. // VERTEX INCLUDES //
  137. ///////////////////////////// SETTINGS MANAGEMENT ////////////////////////////
  138. //#include "../user-settings.h"
  139. ///////////////////////////// BEGIN USER-SETTINGS ////////////////////////////
  140. #ifndef USER_SETTINGS_H
  141. #define USER_SETTINGS_H
  142. ///////////////////////////// DRIVER CAPABILITIES ////////////////////////////
  143. // The Cg compiler uses different "profiles" with different capabilities.
  144. // This shader requires a Cg compilation profile >= arbfp1, but a few options
  145. // require higher profiles like fp30 or fp40. The shader can't detect profile
  146. // or driver capabilities, so instead you must comment or uncomment the lines
  147. // below with "//" before "#define." Disable an option if you get compilation
  148. // errors resembling those listed. Generally speaking, all of these options
  149. // will run on nVidia cards, but only DRIVERS_ALLOW_TEX2DBIAS (if that) is
  150. // likely to run on ATI/AMD, due to the Cg compiler's profile limitations.
  151. // Derivatives: Unsupported on fp20, ps_1_1, ps_1_2, ps_1_3, and arbfp1.
  152. // Among other things, derivatives help us fix anisotropic filtering artifacts
  153. // with curved manually tiled phosphor mask coords. Related errors:
  154. // error C3004: function "float2 ddx(float2);" not supported in this profile
  155. // error C3004: function "float2 ddy(float2);" not supported in this profile
  156. //#define DRIVERS_ALLOW_DERIVATIVES
  157. // Fine derivatives: Unsupported on older ATI cards.
  158. // Fine derivatives enable 2x2 fragment block communication, letting us perform
  159. // fast single-pass blur operations. If your card uses coarse derivatives and
  160. // these are enabled, blurs could look broken. Derivatives are a prerequisite.
  161. #ifdef DRIVERS_ALLOW_DERIVATIVES
  162. #define DRIVERS_ALLOW_FINE_DERIVATIVES
  163. #endif
  164. // Dynamic looping: Requires an fp30 or newer profile.
  165. // This makes phosphor mask resampling faster in some cases. Related errors:
  166. // error C5013: profile does not support "for" statements and "for" could not
  167. // be unrolled
  168. //#define DRIVERS_ALLOW_DYNAMIC_BRANCHES
  169. // Without DRIVERS_ALLOW_DYNAMIC_BRANCHES, we need to use unrollable loops.
  170. // Using one static loop avoids overhead if the user is right, but if the user
  171. // is wrong (loops are allowed), breaking a loop into if-blocked pieces with a
  172. // binary search can potentially save some iterations. However, it may fail:
  173. // error C6001: Temporary register limit of 32 exceeded; 35 registers
  174. // needed to compile program
  175. //#define ACCOMODATE_POSSIBLE_DYNAMIC_LOOPS
  176. // tex2Dlod: Requires an fp40 or newer profile. This can be used to disable
  177. // anisotropic filtering, thereby fixing related artifacts. Related errors:
  178. // error C3004: function "float4 tex2Dlod(sampler2D, float4);" not supported in
  179. // this profile
  180. //#define DRIVERS_ALLOW_TEX2DLOD
  181. // tex2Dbias: Requires an fp30 or newer profile. This can be used to alleviate
  182. // artifacts from anisotropic filtering and mipmapping. Related errors:
  183. // error C3004: function "float4 tex2Dbias(sampler2D, float4);" not supported
  184. // in this profile
  185. //#define DRIVERS_ALLOW_TEX2DBIAS
  186. // Integrated graphics compatibility: Integrated graphics like Intel HD 4000
  187. // impose stricter limitations on register counts and instructions. Enable
  188. // INTEGRATED_GRAPHICS_COMPATIBILITY_MODE if you still see error C6001 or:
  189. // error C6002: Instruction limit of 1024 exceeded: 1523 instructions needed
  190. // to compile program.
  191. // Enabling integrated graphics compatibility mode will automatically disable:
  192. // 1.) PHOSPHOR_MASK_MANUALLY_RESIZE: The phosphor mask will be softer.
  193. // (This may be reenabled in a later release.)
  194. // 2.) RUNTIME_GEOMETRY_MODE
  195. // 3.) The high-quality 4x4 Gaussian resize for the bloom approximation
  196. //#define INTEGRATED_GRAPHICS_COMPATIBILITY_MODE
  197. //////////////////////////// USER CODEPATH OPTIONS ///////////////////////////
  198. // To disable a #define option, turn its line into a comment with "//."
  199. // RUNTIME VS. COMPILE-TIME OPTIONS (Major Performance Implications):
  200. // Enable runtime shader parameters in the Retroarch (etc.) GUI? They override
  201. // many of the options in this file and allow real-time tuning, but many of
  202. // them are slower. Disabling them and using this text file will boost FPS.
  203. #define RUNTIME_SHADER_PARAMS_ENABLE
  204. // Specify the phosphor bloom sigma at runtime? This option is 10% slower, but
  205. // it's the only way to do a wide-enough full bloom with a runtime dot pitch.
  206. #define RUNTIME_PHOSPHOR_BLOOM_SIGMA
  207. // Specify antialiasing weight parameters at runtime? (Costs ~20% with cubics)
  208. #define RUNTIME_ANTIALIAS_WEIGHTS
  209. // Specify subpixel offsets at runtime? (WARNING: EXTREMELY EXPENSIVE!)
  210. //#define RUNTIME_ANTIALIAS_SUBPIXEL_OFFSETS
  211. // Make beam_horiz_filter and beam_horiz_linear_rgb_weight into runtime shader
  212. // parameters? This will require more math or dynamic branching.
  213. #define RUNTIME_SCANLINES_HORIZ_FILTER_COLORSPACE
  214. // Specify the tilt at runtime? This makes things about 3% slower.
  215. #define RUNTIME_GEOMETRY_TILT
  216. // Specify the geometry mode at runtime?
  217. #define RUNTIME_GEOMETRY_MODE
  218. // Specify the phosphor mask type (aperture grille, slot mask, shadow mask) and
  219. // mode (Lanczos-resize, hardware resize, or tile 1:1) at runtime, even without
  220. // dynamic branches? This is cheap if mask_resize_viewport_scale is small.
  221. #define FORCE_RUNTIME_PHOSPHOR_MASK_MODE_TYPE_SELECT
  222. // PHOSPHOR MASK:
  223. // Manually resize the phosphor mask for best results (slower)? Disabling this
  224. // removes the option to do so, but it may be faster without dynamic branches.
  225. #define PHOSPHOR_MASK_MANUALLY_RESIZE
  226. // If we sinc-resize the mask, should we Lanczos-window it (slower but better)?
  227. #define PHOSPHOR_MASK_RESIZE_LANCZOS_WINDOW
  228. // Larger blurs are expensive, but we need them to blur larger triads. We can
  229. // detect the right blur if the triad size is static or our profile allows
  230. // dynamic branches, but otherwise we use the largest blur the user indicates
  231. // they might need:
  232. #define PHOSPHOR_BLOOM_TRIADS_LARGER_THAN_3_PIXELS
  233. //#define PHOSPHOR_BLOOM_TRIADS_LARGER_THAN_6_PIXELS
  234. //#define PHOSPHOR_BLOOM_TRIADS_LARGER_THAN_9_PIXELS
  235. //#define PHOSPHOR_BLOOM_TRIADS_LARGER_THAN_12_PIXELS
  236. // Here's a helpful chart:
  237. // MaxTriadSize BlurSize MinTriadCountsByResolution
  238. // 3.0 9.0 480/640/960/1920 triads at 1080p/1440p/2160p/4320p, 4:3 aspect
  239. // 6.0 17.0 240/320/480/960 triads at 1080p/1440p/2160p/4320p, 4:3 aspect
  240. // 9.0 25.0 160/213/320/640 triads at 1080p/1440p/2160p/4320p, 4:3 aspect
  241. // 12.0 31.0 120/160/240/480 triads at 1080p/1440p/2160p/4320p, 4:3 aspect
  242. // 18.0 43.0 80/107/160/320 triads at 1080p/1440p/2160p/4320p, 4:3 aspect
  243. /////////////////////////////// USER PARAMETERS //////////////////////////////
  244. // Note: Many of these static parameters are overridden by runtime shader
  245. // parameters when those are enabled. However, many others are static codepath
  246. // options that were cleaner or more convert to code as static constants.
  247. // GAMMA:
  248. static const float crt_gamma_static = 2.5; // range [1, 5]
  249. static const float lcd_gamma_static = 2.2; // range [1, 5]
  250. // LEVELS MANAGEMENT:
  251. // Control the final multiplicative image contrast:
  252. static const float levels_contrast_static = 1.0; // range [0, 4)
  253. // We auto-dim to avoid clipping between passes and restore brightness
  254. // later. Control the dim factor here: Lower values clip less but crush
  255. // blacks more (static only for now).
  256. static const float levels_autodim_temp = 0.5; // range (0, 1] default is 0.5 but that was unnecessarily dark for me, so I set it to 1.0
  257. // HALATION/DIFFUSION/BLOOM:
  258. // Halation weight: How much energy should be lost to electrons bounding
  259. // around under the CRT glass and exciting random phosphors?
  260. static const float halation_weight_static = 0.0; // range [0, 1]
  261. // Refractive diffusion weight: How much light should spread/diffuse from
  262. // refracting through the CRT glass?
  263. static const float diffusion_weight_static = 0.075; // range [0, 1]
  264. // Underestimate brightness: Bright areas bloom more, but we can base the
  265. // bloom brightpass on a lower brightness to sharpen phosphors, or a higher
  266. // brightness to soften them. Low values clip, but >= 0.8 looks okay.
  267. static const float bloom_underestimate_levels_static = 0.8; // range [0, 5]
  268. // Blur all colors more than necessary for a softer phosphor bloom?
  269. static const float bloom_excess_static = 0.0; // range [0, 1]
  270. // The BLOOM_APPROX pass approximates a phosphor blur early on with a small
  271. // blurred resize of the input (convergence offsets are applied as well).
  272. // There are three filter options (static option only for now):
  273. // 0.) Bilinear resize: A fast, close approximation to a 4x4 resize
  274. // if min_allowed_viewport_triads and the BLOOM_APPROX resolution are sane
  275. // and beam_max_sigma is low.
  276. // 1.) 3x3 resize blur: Medium speed, soft/smeared from bilinear blurring,
  277. // always uses a static sigma regardless of beam_max_sigma or
  278. // mask_num_triads_desired.
  279. // 2.) True 4x4 Gaussian resize: Slowest, technically correct.
  280. // These options are more pronounced for the fast, unbloomed shader version.
  281. #ifndef RADEON_FIX
  282. static const float bloom_approx_filter_static = 2.0;
  283. #else
  284. static const float bloom_approx_filter_static = 1.0;
  285. #endif
  286. // ELECTRON BEAM SCANLINE DISTRIBUTION:
  287. // How many scanlines should contribute light to each pixel? Using more
  288. // scanlines is slower (especially for a generalized Gaussian) but less
  289. // distorted with larger beam sigmas (especially for a pure Gaussian). The
  290. // max_beam_sigma at which the closest unused weight is guaranteed <
  291. // 1.0/255.0 (for a 3x antialiased pure Gaussian) is:
  292. // 2 scanlines: max_beam_sigma = 0.2089; distortions begin ~0.34; 141.7 FPS pure, 131.9 FPS generalized
  293. // 3 scanlines, max_beam_sigma = 0.3879; distortions begin ~0.52; 137.5 FPS pure; 123.8 FPS generalized
  294. // 4 scanlines, max_beam_sigma = 0.5723; distortions begin ~0.70; 134.7 FPS pure; 117.2 FPS generalized
  295. // 5 scanlines, max_beam_sigma = 0.7591; distortions begin ~0.89; 131.6 FPS pure; 112.1 FPS generalized
  296. // 6 scanlines, max_beam_sigma = 0.9483; distortions begin ~1.08; 127.9 FPS pure; 105.6 FPS generalized
  297. static const float beam_num_scanlines = 3.0; // range [2, 6]
  298. // A generalized Gaussian beam varies shape with color too, now just width.
  299. // It's slower but more flexible (static option only for now).
  300. static const bool beam_generalized_gaussian = true;
  301. // What kind of scanline antialiasing do you want?
  302. // 0: Sample weights at 1x; 1: Sample weights at 3x; 2: Compute an integral
  303. // Integrals are slow (especially for generalized Gaussians) and rarely any
  304. // better than 3x antialiasing (static option only for now).
  305. static const float beam_antialias_level = 1.0; // range [0, 2]
  306. // Min/max standard deviations for scanline beams: Higher values widen and
  307. // soften scanlines. Depending on other options, low min sigmas can alias.
  308. static const float beam_min_sigma_static = 0.02; // range (0, 1]
  309. static const float beam_max_sigma_static = 0.3; // range (0, 1]
  310. // Beam width varies as a function of color: A power function (0) is more
  311. // configurable, but a spherical function (1) gives the widest beam
  312. // variability without aliasing (static option only for now).
  313. static const float beam_spot_shape_function = 0.0;
  314. // Spot shape power: Powers <= 1 give smoother spot shapes but lower
  315. // sharpness. Powers >= 1.0 are awful unless mix/max sigmas are close.
  316. static const float beam_spot_power_static = 1.0/3.0; // range (0, 16]
  317. // Generalized Gaussian max shape parameters: Higher values give flatter
  318. // scanline plateaus and steeper dropoffs, simultaneously widening and
  319. // sharpening scanlines at the cost of aliasing. 2.0 is pure Gaussian, and
  320. // values > ~40.0 cause artifacts with integrals.
  321. static const float beam_min_shape_static = 2.0; // range [2, 32]
  322. static const float beam_max_shape_static = 4.0; // range [2, 32]
  323. // Generalized Gaussian shape power: Affects how quickly the distribution
  324. // changes shape from Gaussian to steep/plateaued as color increases from 0
  325. // to 1.0. Higher powers appear softer for most colors, and lower powers
  326. // appear sharper for most colors.
  327. static const float beam_shape_power_static = 1.0/4.0; // range (0, 16]
  328. // What filter should be used to sample scanlines horizontally?
  329. // 0: Quilez (fast), 1: Gaussian (configurable), 2: Lanczos2 (sharp)
  330. static const float beam_horiz_filter_static = 0.0;
  331. // Standard deviation for horizontal Gaussian resampling:
  332. static const float beam_horiz_sigma_static = 0.35; // range (0, 2/3]
  333. // Do horizontal scanline sampling in linear RGB (correct light mixing),
  334. // gamma-encoded RGB (darker, hard spot shape, may better match bandwidth-
  335. // limiting circuitry in some CRT's), or a weighted avg.?
  336. static const float beam_horiz_linear_rgb_weight_static = 1.0; // range [0, 1]
  337. // Simulate scanline misconvergence? This needs 3x horizontal texture
  338. // samples and 3x texture samples of BLOOM_APPROX and HALATION_BLUR in
  339. // later passes (static option only for now).
  340. static const bool beam_misconvergence = true;
  341. // Convergence offsets in x/y directions for R/G/B scanline beams in units
  342. // of scanlines. Positive offsets go right/down; ranges [-2, 2]
  343. static const float2 convergence_offsets_r_static = float2(0.1, 0.2);
  344. static const float2 convergence_offsets_g_static = float2(0.3, 0.4);
  345. static const float2 convergence_offsets_b_static = float2(0.5, 0.6);
  346. // Detect interlacing (static option only for now)?
  347. static const bool interlace_detect = true;
  348. // Assume 1080-line sources are interlaced?
  349. static const bool interlace_1080i_static = false;
  350. // For interlaced sources, assume TFF (top-field first) or BFF order?
  351. // (Whether this matters depends on the nature of the interlaced input.)
  352. static const bool interlace_bff_static = false;
  353. // ANTIALIASING:
  354. // What AA level do you want for curvature/overscan/subpixels? Options:
  355. // 0x (none), 1x (sample subpixels), 4x, 5x, 6x, 7x, 8x, 12x, 16x, 20x, 24x
  356. // (Static option only for now)
  357. static const float aa_level = 12.0; // range [0, 24]
  358. // What antialiasing filter do you want (static option only)? Options:
  359. // 0: Box (separable), 1: Box (cylindrical),
  360. // 2: Tent (separable), 3: Tent (cylindrical),
  361. // 4: Gaussian (separable), 5: Gaussian (cylindrical),
  362. // 6: Cubic* (separable), 7: Cubic* (cylindrical, poor)
  363. // 8: Lanczos Sinc (separable), 9: Lanczos Jinc (cylindrical, poor)
  364. // * = Especially slow with RUNTIME_ANTIALIAS_WEIGHTS
  365. static const float aa_filter = 6.0; // range [0, 9]
  366. // Flip the sample grid on odd/even frames (static option only for now)?
  367. static const bool aa_temporal = false;
  368. // Use RGB subpixel offsets for antialiasing? The pixel is at green, and
  369. // the blue offset is the negative r offset; range [0, 0.5]
  370. static const float2 aa_subpixel_r_offset_static = float2(-1.0/3.0, 0.0);//float2(0.0);
  371. // Cubics: See http://www.imagemagick.org/Usage/filter/#mitchell
  372. // 1.) "Keys cubics" with B = 1 - 2C are considered the highest quality.
  373. // 2.) C = 0.5 (default) is Catmull-Rom; higher C's apply sharpening.
  374. // 3.) C = 1.0/3.0 is the Mitchell-Netravali filter.
  375. // 4.) C = 0.0 is a soft spline filter.
  376. static const float aa_cubic_c_static = 0.5; // range [0, 4]
  377. // Standard deviation for Gaussian antialiasing: Try 0.5/aa_pixel_diameter.
  378. static const float aa_gauss_sigma_static = 0.5; // range [0.0625, 1.0]
  379. // PHOSPHOR MASK:
  380. // Mask type: 0 = aperture grille, 1 = slot mask, 2 = EDP shadow mask
  381. static const float mask_type_static = 1.0; // range [0, 2]
  382. // We can sample the mask three ways. Pick 2/3 from: Pretty/Fast/Flexible.
  383. // 0.) Sinc-resize to the desired dot pitch manually (pretty/slow/flexible).
  384. // This requires PHOSPHOR_MASK_MANUALLY_RESIZE to be #defined.
  385. // 1.) Hardware-resize to the desired dot pitch (ugly/fast/flexible). This
  386. // is halfway decent with LUT mipmapping but atrocious without it.
  387. // 2.) Tile it without resizing at a 1:1 texel:pixel ratio for flat coords
  388. // (pretty/fast/inflexible). Each input LUT has a fixed dot pitch.
  389. // This mode reuses the same masks, so triads will be enormous unless
  390. // you change the mask LUT filenames in your .cgp file.
  391. static const float mask_sample_mode_static = 0.0; // range [0, 2]
  392. // Prefer setting the triad size (0.0) or number on the screen (1.0)?
  393. // If RUNTIME_PHOSPHOR_BLOOM_SIGMA isn't #defined, the specified triad size
  394. // will always be used to calculate the full bloom sigma statically.
  395. static const float mask_specify_num_triads_static = 0.0; // range [0, 1]
  396. // Specify the phosphor triad size, in pixels. Each tile (usually with 8
  397. // triads) will be rounded to the nearest integer tile size and clamped to
  398. // obey minimum size constraints (imposed to reduce downsize taps) and
  399. // maximum size constraints (imposed to have a sane MASK_RESIZE FBO size).
  400. // To increase the size limit, double the viewport-relative scales for the
  401. // two MASK_RESIZE passes in crt-royale.cgp and user-cgp-contants.h.
  402. // range [1, mask_texture_small_size/mask_triads_per_tile]
  403. static const float mask_triad_size_desired_static = 24.0 / 8.0;
  404. // If mask_specify_num_triads is 1.0/true, we'll go by this instead (the
  405. // final size will be rounded and constrained as above); default 480.0
  406. static const float mask_num_triads_desired_static = 480.0;
  407. // How many lobes should the sinc/Lanczos resizer use? More lobes require
  408. // more samples and avoid moire a bit better, but some is unavoidable
  409. // depending on the destination size (static option for now).
  410. static const float mask_sinc_lobes = 3.0; // range [2, 4]
  411. // The mask is resized using a variable number of taps in each dimension,
  412. // but some Cg profiles always fetch a constant number of taps no matter
  413. // what (no dynamic branching). We can limit the maximum number of taps if
  414. // we statically limit the minimum phosphor triad size. Larger values are
  415. // faster, but the limit IS enforced (static option only, forever);
  416. // range [1, mask_texture_small_size/mask_triads_per_tile]
  417. // TODO: Make this 1.0 and compensate with smarter sampling!
  418. static const float mask_min_allowed_triad_size = 2.0;
  419. // GEOMETRY:
  420. // Geometry mode:
  421. // 0: Off (default), 1: Spherical mapping (like cgwg's),
  422. // 2: Alt. spherical mapping (more bulbous), 3: Cylindrical/Trinitron
  423. static const float geom_mode_static = 0.0; // range [0, 3]
  424. // Radius of curvature: Measured in units of your viewport's diagonal size.
  425. static const float geom_radius_static = 2.0; // range [1/(2*pi), 1024]
  426. // View dist is the distance from the player to their physical screen, in
  427. // units of the viewport's diagonal size. It controls the field of view.
  428. static const float geom_view_dist_static = 2.0; // range [0.5, 1024]
  429. // Tilt angle in radians (clockwise around up and right vectors):
  430. static const float2 geom_tilt_angle_static = float2(0.0, 0.0); // range [-pi, pi]
  431. // Aspect ratio: When the true viewport size is unknown, this value is used
  432. // to help convert between the phosphor triad size and count, along with
  433. // the mask_resize_viewport_scale constant from user-cgp-constants.h. Set
  434. // this equal to Retroarch's display aspect ratio (DAR) for best results;
  435. // range [1, geom_max_aspect_ratio from user-cgp-constants.h];
  436. // default (256/224)*(54/47) = 1.313069909 (see below)
  437. static const float geom_aspect_ratio_static = 1.313069909;
  438. // Before getting into overscan, here's some general aspect ratio info:
  439. // - DAR = display aspect ratio = SAR * PAR; as in your Retroarch setting
  440. // - SAR = storage aspect ratio = DAR / PAR; square pixel emulator frame AR
  441. // - PAR = pixel aspect ratio = DAR / SAR; holds regardless of cropping
  442. // Geometry processing has to "undo" the screen-space 2D DAR to calculate
  443. // 3D view vectors, then reapplies the aspect ratio to the simulated CRT in
  444. // uv-space. To ensure the source SAR is intended for a ~4:3 DAR, either:
  445. // a.) Enable Retroarch's "Crop Overscan"
  446. // b.) Readd horizontal padding: Set overscan to e.g. N*(1.0, 240.0/224.0)
  447. // Real consoles use horizontal black padding in the signal, but emulators
  448. // often crop this without cropping the vertical padding; a 256x224 [S]NES
  449. // frame (8:7 SAR) is intended for a ~4:3 DAR, but a 256x240 frame is not.
  450. // The correct [S]NES PAR is 54:47, found by blargg and NewRisingSun:
  451. // http://board.zsnes.com/phpBB3/viewtopic.php?f=22&t=11928&start=50
  452. // http://forums.nesdev.com/viewtopic.php?p=24815#p24815
  453. // For flat output, it's okay to set DAR = [existing] SAR * [correct] PAR
  454. // without doing a. or b., but horizontal image borders will be tighter
  455. // than vertical ones, messing up curvature and overscan. Fixing the
  456. // padding first corrects this.
  457. // Overscan: Amount to "zoom in" before cropping. You can zoom uniformly
  458. // or adjust x/y independently to e.g. readd horizontal padding, as noted
  459. // above: Values < 1.0 zoom out; range (0, inf)
  460. static const float2 geom_overscan_static = float2(1.0, 1.0);// * 1.005 * (1.0, 240/224.0)
  461. // Compute a proper pixel-space to texture-space matrix even without ddx()/
  462. // ddy()? This is ~8.5% slower but improves antialiasing/subpixel filtering
  463. // with strong curvature (static option only for now).
  464. static const bool geom_force_correct_tangent_matrix = true;
  465. // BORDERS:
  466. // Rounded border size in texture uv coords:
  467. static const float border_size_static = 0.015; // range [0, 0.5]
  468. // Border darkness: Moderate values darken the border smoothly, and high
  469. // values make the image very dark just inside the border:
  470. static const float border_darkness_static = 2.0; // range [0, inf)
  471. // Border compression: High numbers compress border transitions, narrowing
  472. // the dark border area.
  473. static const float border_compress_static = 2.5; // range [1, inf)
  474. #endif // USER_SETTINGS_H
  475. //////////////////////////// END USER-SETTINGS //////////////////////////
  476. //#include "derived-settings-and-constants.h"
  477. //////////////////// BEGIN DERIVED-SETTINGS-AND-CONSTANTS ////////////////////
  478. #ifndef DERIVED_SETTINGS_AND_CONSTANTS_H
  479. #define DERIVED_SETTINGS_AND_CONSTANTS_H
  480. ///////////////////////////// GPL LICENSE NOTICE /////////////////////////////
  481. // crt-royale: A full-featured CRT shader, with cheese.
  482. // Copyright (C) 2014 TroggleMonkey <trogglemonkey@gmx.com>
  483. //
  484. // This program is free software; you can redistribute it and/or modify it
  485. // under the terms of the GNU General Public License as published by the Free
  486. // Software Foundation; either version 2 of the License, or any later version.
  487. //
  488. // This program is distributed in the hope that it will be useful, but WITHOUT
  489. // ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
  490. // FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
  491. // more details.
  492. //
  493. // You should have received a copy of the GNU General Public License along with
  494. // this program; if not, write to the Free Software Foundation, Inc., 59 Temple
  495. // Place, Suite 330, Boston, MA 02111-1307 USA
  496. ///////////////////////////////// DESCRIPTION ////////////////////////////////
  497. // These macros and constants can be used across the whole codebase.
  498. // Unlike the values in user-settings.cgh, end users shouldn't modify these.
  499. /////////////////////////////// BEGIN INCLUDES ///////////////////////////////
  500. //#include "../user-settings.h"
  501. ///////////////////////////// BEGIN USER-SETTINGS ////////////////////////////
  502. #ifndef USER_SETTINGS_H
  503. #define USER_SETTINGS_H
  504. ///////////////////////////// DRIVER CAPABILITIES ////////////////////////////
  505. // The Cg compiler uses different "profiles" with different capabilities.
  506. // This shader requires a Cg compilation profile >= arbfp1, but a few options
  507. // require higher profiles like fp30 or fp40. The shader can't detect profile
  508. // or driver capabilities, so instead you must comment or uncomment the lines
  509. // below with "//" before "#define." Disable an option if you get compilation
  510. // errors resembling those listed. Generally speaking, all of these options
  511. // will run on nVidia cards, but only DRIVERS_ALLOW_TEX2DBIAS (if that) is
  512. // likely to run on ATI/AMD, due to the Cg compiler's profile limitations.
  513. // Derivatives: Unsupported on fp20, ps_1_1, ps_1_2, ps_1_3, and arbfp1.
  514. // Among other things, derivatives help us fix anisotropic filtering artifacts
  515. // with curved manually tiled phosphor mask coords. Related errors:
  516. // error C3004: function "float2 ddx(float2);" not supported in this profile
  517. // error C3004: function "float2 ddy(float2);" not supported in this profile
  518. //#define DRIVERS_ALLOW_DERIVATIVES
  519. // Fine derivatives: Unsupported on older ATI cards.
  520. // Fine derivatives enable 2x2 fragment block communication, letting us perform
  521. // fast single-pass blur operations. If your card uses coarse derivatives and
  522. // these are enabled, blurs could look broken. Derivatives are a prerequisite.
  523. #ifdef DRIVERS_ALLOW_DERIVATIVES
  524. #define DRIVERS_ALLOW_FINE_DERIVATIVES
  525. #endif
  526. // Dynamic looping: Requires an fp30 or newer profile.
  527. // This makes phosphor mask resampling faster in some cases. Related errors:
  528. // error C5013: profile does not support "for" statements and "for" could not
  529. // be unrolled
  530. //#define DRIVERS_ALLOW_DYNAMIC_BRANCHES
  531. // Without DRIVERS_ALLOW_DYNAMIC_BRANCHES, we need to use unrollable loops.
  532. // Using one static loop avoids overhead if the user is right, but if the user
  533. // is wrong (loops are allowed), breaking a loop into if-blocked pieces with a
  534. // binary search can potentially save some iterations. However, it may fail:
  535. // error C6001: Temporary register limit of 32 exceeded; 35 registers
  536. // needed to compile program
  537. //#define ACCOMODATE_POSSIBLE_DYNAMIC_LOOPS
  538. // tex2Dlod: Requires an fp40 or newer profile. This can be used to disable
  539. // anisotropic filtering, thereby fixing related artifacts. Related errors:
  540. // error C3004: function "float4 tex2Dlod(sampler2D, float4);" not supported in
  541. // this profile
  542. //#define DRIVERS_ALLOW_TEX2DLOD
  543. // tex2Dbias: Requires an fp30 or newer profile. This can be used to alleviate
  544. // artifacts from anisotropic filtering and mipmapping. Related errors:
  545. // error C3004: function "float4 tex2Dbias(sampler2D, float4);" not supported
  546. // in this profile
  547. //#define DRIVERS_ALLOW_TEX2DBIAS
  548. // Integrated graphics compatibility: Integrated graphics like Intel HD 4000
  549. // impose stricter limitations on register counts and instructions. Enable
  550. // INTEGRATED_GRAPHICS_COMPATIBILITY_MODE if you still see error C6001 or:
  551. // error C6002: Instruction limit of 1024 exceeded: 1523 instructions needed
  552. // to compile program.
  553. // Enabling integrated graphics compatibility mode will automatically disable:
  554. // 1.) PHOSPHOR_MASK_MANUALLY_RESIZE: The phosphor mask will be softer.
  555. // (This may be reenabled in a later release.)
  556. // 2.) RUNTIME_GEOMETRY_MODE
  557. // 3.) The high-quality 4x4 Gaussian resize for the bloom approximation
  558. //#define INTEGRATED_GRAPHICS_COMPATIBILITY_MODE
  559. //////////////////////////// USER CODEPATH OPTIONS ///////////////////////////
  560. // To disable a #define option, turn its line into a comment with "//."
  561. // RUNTIME VS. COMPILE-TIME OPTIONS (Major Performance Implications):
  562. // Enable runtime shader parameters in the Retroarch (etc.) GUI? They override
  563. // many of the options in this file and allow real-time tuning, but many of
  564. // them are slower. Disabling them and using this text file will boost FPS.
  565. #define RUNTIME_SHADER_PARAMS_ENABLE
  566. // Specify the phosphor bloom sigma at runtime? This option is 10% slower, but
  567. // it's the only way to do a wide-enough full bloom with a runtime dot pitch.
  568. #define RUNTIME_PHOSPHOR_BLOOM_SIGMA
  569. // Specify antialiasing weight parameters at runtime? (Costs ~20% with cubics)
  570. #define RUNTIME_ANTIALIAS_WEIGHTS
  571. // Specify subpixel offsets at runtime? (WARNING: EXTREMELY EXPENSIVE!)
  572. //#define RUNTIME_ANTIALIAS_SUBPIXEL_OFFSETS
  573. // Make beam_horiz_filter and beam_horiz_linear_rgb_weight into runtime shader
  574. // parameters? This will require more math or dynamic branching.
  575. #define RUNTIME_SCANLINES_HORIZ_FILTER_COLORSPACE
  576. // Specify the tilt at runtime? This makes things about 3% slower.
  577. #define RUNTIME_GEOMETRY_TILT
  578. // Specify the geometry mode at runtime?
  579. #define RUNTIME_GEOMETRY_MODE
  580. // Specify the phosphor mask type (aperture grille, slot mask, shadow mask) and
  581. // mode (Lanczos-resize, hardware resize, or tile 1:1) at runtime, even without
  582. // dynamic branches? This is cheap if mask_resize_viewport_scale is small.
  583. #define FORCE_RUNTIME_PHOSPHOR_MASK_MODE_TYPE_SELECT
  584. // PHOSPHOR MASK:
  585. // Manually resize the phosphor mask for best results (slower)? Disabling this
  586. // removes the option to do so, but it may be faster without dynamic branches.
  587. #define PHOSPHOR_MASK_MANUALLY_RESIZE
  588. // If we sinc-resize the mask, should we Lanczos-window it (slower but better)?
  589. #define PHOSPHOR_MASK_RESIZE_LANCZOS_WINDOW
  590. // Larger blurs are expensive, but we need them to blur larger triads. We can
  591. // detect the right blur if the triad size is static or our profile allows
  592. // dynamic branches, but otherwise we use the largest blur the user indicates
  593. // they might need:
  594. #define PHOSPHOR_BLOOM_TRIADS_LARGER_THAN_3_PIXELS
  595. //#define PHOSPHOR_BLOOM_TRIADS_LARGER_THAN_6_PIXELS
  596. //#define PHOSPHOR_BLOOM_TRIADS_LARGER_THAN_9_PIXELS
  597. //#define PHOSPHOR_BLOOM_TRIADS_LARGER_THAN_12_PIXELS
  598. // Here's a helpful chart:
  599. // MaxTriadSize BlurSize MinTriadCountsByResolution
  600. // 3.0 9.0 480/640/960/1920 triads at 1080p/1440p/2160p/4320p, 4:3 aspect
  601. // 6.0 17.0 240/320/480/960 triads at 1080p/1440p/2160p/4320p, 4:3 aspect
  602. // 9.0 25.0 160/213/320/640 triads at 1080p/1440p/2160p/4320p, 4:3 aspect
  603. // 12.0 31.0 120/160/240/480 triads at 1080p/1440p/2160p/4320p, 4:3 aspect
  604. // 18.0 43.0 80/107/160/320 triads at 1080p/1440p/2160p/4320p, 4:3 aspect
  605. /////////////////////////////// USER PARAMETERS //////////////////////////////
  606. // Note: Many of these static parameters are overridden by runtime shader
  607. // parameters when those are enabled. However, many others are static codepath
  608. // options that were cleaner or more convert to code as static constants.
  609. // GAMMA:
  610. static const float crt_gamma_static = 2.5; // range [1, 5]
  611. static const float lcd_gamma_static = 2.2; // range [1, 5]
  612. // LEVELS MANAGEMENT:
  613. // Control the final multiplicative image contrast:
  614. static const float levels_contrast_static = 1.0; // range [0, 4)
  615. // We auto-dim to avoid clipping between passes and restore brightness
  616. // later. Control the dim factor here: Lower values clip less but crush
  617. // blacks more (static only for now).
  618. static const float levels_autodim_temp = 0.5; // range (0, 1] default is 0.5 but that was unnecessarily dark for me, so I set it to 1.0
  619. // HALATION/DIFFUSION/BLOOM:
  620. // Halation weight: How much energy should be lost to electrons bounding
  621. // around under the CRT glass and exciting random phosphors?
  622. static const float halation_weight_static = 0.0; // range [0, 1]
  623. // Refractive diffusion weight: How much light should spread/diffuse from
  624. // refracting through the CRT glass?
  625. static const float diffusion_weight_static = 0.075; // range [0, 1]
  626. // Underestimate brightness: Bright areas bloom more, but we can base the
  627. // bloom brightpass on a lower brightness to sharpen phosphors, or a higher
  628. // brightness to soften them. Low values clip, but >= 0.8 looks okay.
  629. static const float bloom_underestimate_levels_static = 0.8; // range [0, 5]
  630. // Blur all colors more than necessary for a softer phosphor bloom?
  631. static const float bloom_excess_static = 0.0; // range [0, 1]
  632. // The BLOOM_APPROX pass approximates a phosphor blur early on with a small
  633. // blurred resize of the input (convergence offsets are applied as well).
  634. // There are three filter options (static option only for now):
  635. // 0.) Bilinear resize: A fast, close approximation to a 4x4 resize
  636. // if min_allowed_viewport_triads and the BLOOM_APPROX resolution are sane
  637. // and beam_max_sigma is low.
  638. // 1.) 3x3 resize blur: Medium speed, soft/smeared from bilinear blurring,
  639. // always uses a static sigma regardless of beam_max_sigma or
  640. // mask_num_triads_desired.
  641. // 2.) True 4x4 Gaussian resize: Slowest, technically correct.
  642. // These options are more pronounced for the fast, unbloomed shader version.
  643. #ifndef RADEON_FIX
  644. static const float bloom_approx_filter_static = 2.0;
  645. #else
  646. static const float bloom_approx_filter_static = 1.0;
  647. #endif
  648. // ELECTRON BEAM SCANLINE DISTRIBUTION:
  649. // How many scanlines should contribute light to each pixel? Using more
  650. // scanlines is slower (especially for a generalized Gaussian) but less
  651. // distorted with larger beam sigmas (especially for a pure Gaussian). The
  652. // max_beam_sigma at which the closest unused weight is guaranteed <
  653. // 1.0/255.0 (for a 3x antialiased pure Gaussian) is:
  654. // 2 scanlines: max_beam_sigma = 0.2089; distortions begin ~0.34; 141.7 FPS pure, 131.9 FPS generalized
  655. // 3 scanlines, max_beam_sigma = 0.3879; distortions begin ~0.52; 137.5 FPS pure; 123.8 FPS generalized
  656. // 4 scanlines, max_beam_sigma = 0.5723; distortions begin ~0.70; 134.7 FPS pure; 117.2 FPS generalized
  657. // 5 scanlines, max_beam_sigma = 0.7591; distortions begin ~0.89; 131.6 FPS pure; 112.1 FPS generalized
  658. // 6 scanlines, max_beam_sigma = 0.9483; distortions begin ~1.08; 127.9 FPS pure; 105.6 FPS generalized
  659. static const float beam_num_scanlines = 3.0; // range [2, 6]
  660. // A generalized Gaussian beam varies shape with color too, now just width.
  661. // It's slower but more flexible (static option only for now).
  662. static const bool beam_generalized_gaussian = true;
  663. // What kind of scanline antialiasing do you want?
  664. // 0: Sample weights at 1x; 1: Sample weights at 3x; 2: Compute an integral
  665. // Integrals are slow (especially for generalized Gaussians) and rarely any
  666. // better than 3x antialiasing (static option only for now).
  667. static const float beam_antialias_level = 1.0; // range [0, 2]
  668. // Min/max standard deviations for scanline beams: Higher values widen and
  669. // soften scanlines. Depending on other options, low min sigmas can alias.
  670. static const float beam_min_sigma_static = 0.02; // range (0, 1]
  671. static const float beam_max_sigma_static = 0.3; // range (0, 1]
  672. // Beam width varies as a function of color: A power function (0) is more
  673. // configurable, but a spherical function (1) gives the widest beam
  674. // variability without aliasing (static option only for now).
  675. static const float beam_spot_shape_function = 0.0;
  676. // Spot shape power: Powers <= 1 give smoother spot shapes but lower
  677. // sharpness. Powers >= 1.0 are awful unless mix/max sigmas are close.
  678. static const float beam_spot_power_static = 1.0/3.0; // range (0, 16]
  679. // Generalized Gaussian max shape parameters: Higher values give flatter
  680. // scanline plateaus and steeper dropoffs, simultaneously widening and
  681. // sharpening scanlines at the cost of aliasing. 2.0 is pure Gaussian, and
  682. // values > ~40.0 cause artifacts with integrals.
  683. static const float beam_min_shape_static = 2.0; // range [2, 32]
  684. static const float beam_max_shape_static = 4.0; // range [2, 32]
  685. // Generalized Gaussian shape power: Affects how quickly the distribution
  686. // changes shape from Gaussian to steep/plateaued as color increases from 0
  687. // to 1.0. Higher powers appear softer for most colors, and lower powers
  688. // appear sharper for most colors.
  689. static const float beam_shape_power_static = 1.0/4.0; // range (0, 16]
  690. // What filter should be used to sample scanlines horizontally?
  691. // 0: Quilez (fast), 1: Gaussian (configurable), 2: Lanczos2 (sharp)
  692. static const float beam_horiz_filter_static = 0.0;
  693. // Standard deviation for horizontal Gaussian resampling:
  694. static const float beam_horiz_sigma_static = 0.35; // range (0, 2/3]
  695. // Do horizontal scanline sampling in linear RGB (correct light mixing),
  696. // gamma-encoded RGB (darker, hard spot shape, may better match bandwidth-
  697. // limiting circuitry in some CRT's), or a weighted avg.?
  698. static const float beam_horiz_linear_rgb_weight_static = 1.0; // range [0, 1]
  699. // Simulate scanline misconvergence? This needs 3x horizontal texture
  700. // samples and 3x texture samples of BLOOM_APPROX and HALATION_BLUR in
  701. // later passes (static option only for now).
  702. static const bool beam_misconvergence = true;
  703. // Convergence offsets in x/y directions for R/G/B scanline beams in units
  704. // of scanlines. Positive offsets go right/down; ranges [-2, 2]
  705. static const float2 convergence_offsets_r_static = float2(0.1, 0.2);
  706. static const float2 convergence_offsets_g_static = float2(0.3, 0.4);
  707. static const float2 convergence_offsets_b_static = float2(0.5, 0.6);
  708. // Detect interlacing (static option only for now)?
  709. static const bool interlace_detect = true;
  710. // Assume 1080-line sources are interlaced?
  711. static const bool interlace_1080i_static = false;
  712. // For interlaced sources, assume TFF (top-field first) or BFF order?
  713. // (Whether this matters depends on the nature of the interlaced input.)
  714. static const bool interlace_bff_static = false;
  715. // ANTIALIASING:
  716. // What AA level do you want for curvature/overscan/subpixels? Options:
  717. // 0x (none), 1x (sample subpixels), 4x, 5x, 6x, 7x, 8x, 12x, 16x, 20x, 24x
  718. // (Static option only for now)
  719. static const float aa_level = 12.0; // range [0, 24]
  720. // What antialiasing filter do you want (static option only)? Options:
  721. // 0: Box (separable), 1: Box (cylindrical),
  722. // 2: Tent (separable), 3: Tent (cylindrical),
  723. // 4: Gaussian (separable), 5: Gaussian (cylindrical),
  724. // 6: Cubic* (separable), 7: Cubic* (cylindrical, poor)
  725. // 8: Lanczos Sinc (separable), 9: Lanczos Jinc (cylindrical, poor)
  726. // * = Especially slow with RUNTIME_ANTIALIAS_WEIGHTS
  727. static const float aa_filter = 6.0; // range [0, 9]
  728. // Flip the sample grid on odd/even frames (static option only for now)?
  729. static const bool aa_temporal = false;
  730. // Use RGB subpixel offsets for antialiasing? The pixel is at green, and
  731. // the blue offset is the negative r offset; range [0, 0.5]
  732. static const float2 aa_subpixel_r_offset_static = float2(-1.0/3.0, 0.0);//float2(0.0);
  733. // Cubics: See http://www.imagemagick.org/Usage/filter/#mitchell
  734. // 1.) "Keys cubics" with B = 1 - 2C are considered the highest quality.
  735. // 2.) C = 0.5 (default) is Catmull-Rom; higher C's apply sharpening.
  736. // 3.) C = 1.0/3.0 is the Mitchell-Netravali filter.
  737. // 4.) C = 0.0 is a soft spline filter.
  738. static const float aa_cubic_c_static = 0.5; // range [0, 4]
  739. // Standard deviation for Gaussian antialiasing: Try 0.5/aa_pixel_diameter.
  740. static const float aa_gauss_sigma_static = 0.5; // range [0.0625, 1.0]
  741. // PHOSPHOR MASK:
  742. // Mask type: 0 = aperture grille, 1 = slot mask, 2 = EDP shadow mask
  743. static const float mask_type_static = 1.0; // range [0, 2]
  744. // We can sample the mask three ways. Pick 2/3 from: Pretty/Fast/Flexible.
  745. // 0.) Sinc-resize to the desired dot pitch manually (pretty/slow/flexible).
  746. // This requires PHOSPHOR_MASK_MANUALLY_RESIZE to be #defined.
  747. // 1.) Hardware-resize to the desired dot pitch (ugly/fast/flexible). This
  748. // is halfway decent with LUT mipmapping but atrocious without it.
  749. // 2.) Tile it without resizing at a 1:1 texel:pixel ratio for flat coords
  750. // (pretty/fast/inflexible). Each input LUT has a fixed dot pitch.
  751. // This mode reuses the same masks, so triads will be enormous unless
  752. // you change the mask LUT filenames in your .cgp file.
  753. static const float mask_sample_mode_static = 0.0; // range [0, 2]
  754. // Prefer setting the triad size (0.0) or number on the screen (1.0)?
  755. // If RUNTIME_PHOSPHOR_BLOOM_SIGMA isn't #defined, the specified triad size
  756. // will always be used to calculate the full bloom sigma statically.
  757. static const float mask_specify_num_triads_static = 0.0; // range [0, 1]
  758. // Specify the phosphor triad size, in pixels. Each tile (usually with 8
  759. // triads) will be rounded to the nearest integer tile size and clamped to
  760. // obey minimum size constraints (imposed to reduce downsize taps) and
  761. // maximum size constraints (imposed to have a sane MASK_RESIZE FBO size).
  762. // To increase the size limit, double the viewport-relative scales for the
  763. // two MASK_RESIZE passes in crt-royale.cgp and user-cgp-contants.h.
  764. // range [1, mask_texture_small_size/mask_triads_per_tile]
  765. static const float mask_triad_size_desired_static = 24.0 / 8.0;
  766. // If mask_specify_num_triads is 1.0/true, we'll go by this instead (the
  767. // final size will be rounded and constrained as above); default 480.0
  768. static const float mask_num_triads_desired_static = 480.0;
  769. // How many lobes should the sinc/Lanczos resizer use? More lobes require
  770. // more samples and avoid moire a bit better, but some is unavoidable
  771. // depending on the destination size (static option for now).
  772. static const float mask_sinc_lobes = 3.0; // range [2, 4]
  773. // The mask is resized using a variable number of taps in each dimension,
  774. // but some Cg profiles always fetch a constant number of taps no matter
  775. // what (no dynamic branching). We can limit the maximum number of taps if
  776. // we statically limit the minimum phosphor triad size. Larger values are
  777. // faster, but the limit IS enforced (static option only, forever);
  778. // range [1, mask_texture_small_size/mask_triads_per_tile]
  779. // TODO: Make this 1.0 and compensate with smarter sampling!
  780. static const float mask_min_allowed_triad_size = 2.0;
  781. // GEOMETRY:
  782. // Geometry mode:
  783. // 0: Off (default), 1: Spherical mapping (like cgwg's),
  784. // 2: Alt. spherical mapping (more bulbous), 3: Cylindrical/Trinitron
  785. static const float geom_mode_static = 0.0; // range [0, 3]
  786. // Radius of curvature: Measured in units of your viewport's diagonal size.
  787. static const float geom_radius_static = 2.0; // range [1/(2*pi), 1024]
  788. // View dist is the distance from the player to their physical screen, in
  789. // units of the viewport's diagonal size. It controls the field of view.
  790. static const float geom_view_dist_static = 2.0; // range [0.5, 1024]
  791. // Tilt angle in radians (clockwise around up and right vectors):
  792. static const float2 geom_tilt_angle_static = float2(0.0, 0.0); // range [-pi, pi]
  793. // Aspect ratio: When the true viewport size is unknown, this value is used
  794. // to help convert between the phosphor triad size and count, along with
  795. // the mask_resize_viewport_scale constant from user-cgp-constants.h. Set
  796. // this equal to Retroarch's display aspect ratio (DAR) for best results;
  797. // range [1, geom_max_aspect_ratio from user-cgp-constants.h];
  798. // default (256/224)*(54/47) = 1.313069909 (see below)
  799. static const float geom_aspect_ratio_static = 1.313069909;
  800. // Before getting into overscan, here's some general aspect ratio info:
  801. // - DAR = display aspect ratio = SAR * PAR; as in your Retroarch setting
  802. // - SAR = storage aspect ratio = DAR / PAR; square pixel emulator frame AR
  803. // - PAR = pixel aspect ratio = DAR / SAR; holds regardless of cropping
  804. // Geometry processing has to "undo" the screen-space 2D DAR to calculate
  805. // 3D view vectors, then reapplies the aspect ratio to the simulated CRT in
  806. // uv-space. To ensure the source SAR is intended for a ~4:3 DAR, either:
  807. // a.) Enable Retroarch's "Crop Overscan"
  808. // b.) Readd horizontal padding: Set overscan to e.g. N*(1.0, 240.0/224.0)
  809. // Real consoles use horizontal black padding in the signal, but emulators
  810. // often crop this without cropping the vertical padding; a 256x224 [S]NES
  811. // frame (8:7 SAR) is intended for a ~4:3 DAR, but a 256x240 frame is not.
  812. // The correct [S]NES PAR is 54:47, found by blargg and NewRisingSun:
  813. // http://board.zsnes.com/phpBB3/viewtopic.php?f=22&t=11928&start=50
  814. // http://forums.nesdev.com/viewtopic.php?p=24815#p24815
  815. // For flat output, it's okay to set DAR = [existing] SAR * [correct] PAR
  816. // without doing a. or b., but horizontal image borders will be tighter
  817. // than vertical ones, messing up curvature and overscan. Fixing the
  818. // padding first corrects this.
  819. // Overscan: Amount to "zoom in" before cropping. You can zoom uniformly
  820. // or adjust x/y independently to e.g. readd horizontal padding, as noted
  821. // above: Values < 1.0 zoom out; range (0, inf)
  822. static const float2 geom_overscan_static = float2(1.0, 1.0);// * 1.005 * (1.0, 240/224.0)
  823. // Compute a proper pixel-space to texture-space matrix even without ddx()/
  824. // ddy()? This is ~8.5% slower but improves antialiasing/subpixel filtering
  825. // with strong curvature (static option only for now).
  826. static const bool geom_force_correct_tangent_matrix = true;
  827. // BORDERS:
  828. // Rounded border size in texture uv coords:
  829. static const float border_size_static = 0.015; // range [0, 0.5]
  830. // Border darkness: Moderate values darken the border smoothly, and high
  831. // values make the image very dark just inside the border:
  832. static const float border_darkness_static = 2.0; // range [0, inf)
  833. // Border compression: High numbers compress border transitions, narrowing
  834. // the dark border area.
  835. static const float border_compress_static = 2.5; // range [1, inf)
  836. #endif // USER_SETTINGS_H
  837. ///////////////////////////// END USER-SETTINGS ////////////////////////////
  838. //#include "user-cgp-constants.h"
  839. ///////////////////////// BEGIN USER-CGP-CONSTANTS /////////////////////////
  840. #ifndef USER_CGP_CONSTANTS_H
  841. #define USER_CGP_CONSTANTS_H
  842. // IMPORTANT:
  843. // These constants MUST be set appropriately for the settings in crt-royale.cgp
  844. // (or whatever related .cgp file you're using). If they aren't, you're likely
  845. // to get artifacts, the wrong phosphor mask size, etc. I wish these could be
  846. // set directly in the .cgp file to make things easier, but...they can't.
  847. // PASS SCALES AND RELATED CONSTANTS:
  848. // Copy the absolute scale_x for BLOOM_APPROX. There are two major versions of
  849. // this shader: One does a viewport-scale bloom, and the other skips it. The
  850. // latter benefits from a higher bloom_approx_scale_x, so save both separately:
  851. static const float bloom_approx_size_x = 320.0;
  852. static const float bloom_approx_size_x_for_fake = 400.0;
  853. // Copy the viewport-relative scales of the phosphor mask resize passes
  854. // (MASK_RESIZE and the pass immediately preceding it):
  855. static const float2 mask_resize_viewport_scale = float2(0.0625, 0.0625);
  856. // Copy the geom_max_aspect_ratio used to calculate the MASK_RESIZE scales, etc.:
  857. static const float geom_max_aspect_ratio = 4.0/3.0;
  858. // PHOSPHOR MASK TEXTURE CONSTANTS:
  859. // Set the following constants to reflect the properties of the phosphor mask
  860. // texture named in crt-royale.cgp. The shader optionally resizes a mask tile
  861. // based on user settings, then repeats a single tile until filling the screen.
  862. // The shader must know the input texture size (default 64x64), and to manually
  863. // resize, it must also know the horizontal triads per tile (default 8).
  864. static const float2 mask_texture_small_size = float2(64.0, 64.0);
  865. static const float2 mask_texture_large_size = float2(512.0, 512.0);
  866. static const float mask_triads_per_tile = 8.0;
  867. // We need the average brightness of the phosphor mask to compensate for the
  868. // dimming it causes. The following four values are roughly correct for the
  869. // masks included with the shader. Update the value for any LUT texture you
  870. // change. [Un]comment "#define PHOSPHOR_MASK_GRILLE14" depending on whether
  871. // the loaded aperture grille uses 14-pixel or 15-pixel stripes (default 15).
  872. //#define PHOSPHOR_MASK_GRILLE14
  873. static const float mask_grille14_avg_color = 50.6666666/255.0;
  874. // TileableLinearApertureGrille14Wide7d33Spacing*.png
  875. // TileableLinearApertureGrille14Wide10And6Spacing*.png
  876. static const float mask_grille15_avg_color = 53.0/255.0;
  877. // TileableLinearApertureGrille15Wide6d33Spacing*.png
  878. // TileableLinearApertureGrille15Wide8And5d5Spacing*.png
  879. static const float mask_slot_avg_color = 46.0/255.0;
  880. // TileableLinearSlotMask15Wide9And4d5Horizontal8VerticalSpacing*.png
  881. // TileableLinearSlotMaskTall15Wide9And4d5Horizontal9d14VerticalSpacing*.png
  882. static const float mask_shadow_avg_color = 41.0/255.0;
  883. // TileableLinearShadowMask*.png
  884. // TileableLinearShadowMaskEDP*.png
  885. #ifdef PHOSPHOR_MASK_GRILLE14
  886. static const float mask_grille_avg_color = mask_grille14_avg_color;
  887. #else
  888. static const float mask_grille_avg_color = mask_grille15_avg_color;
  889. #endif
  890. #endif // USER_CGP_CONSTANTS_H
  891. ////////////////////////// END USER-CGP-CONSTANTS //////////////////////////
  892. //////////////////////////////// END INCLUDES ////////////////////////////////
  893. /////////////////////////////// FIXED SETTINGS ///////////////////////////////
  894. // Avoid dividing by zero; using a macro overloads for float, float2, etc.:
  895. #define FIX_ZERO(c) (max(abs(c), 0.0000152587890625)) // 2^-16
  896. // Ensure the first pass decodes CRT gamma and the last encodes LCD gamma.
  897. #ifndef SIMULATE_CRT_ON_LCD
  898. #define SIMULATE_CRT_ON_LCD
  899. #endif
  900. // Manually tiling a manually resized texture creates texture coord derivative
  901. // discontinuities and confuses anisotropic filtering, causing discolored tile
  902. // seams in the phosphor mask. Workarounds:
  903. // a.) Using tex2Dlod disables anisotropic filtering for tiled masks. It's
  904. // downgraded to tex2Dbias without DRIVERS_ALLOW_TEX2DLOD #defined and
  905. // disabled without DRIVERS_ALLOW_TEX2DBIAS #defined either.
  906. // b.) "Tile flat twice" requires drawing two full tiles without border padding
  907. // to the resized mask FBO, and it's incompatible with same-pass curvature.
  908. // (Same-pass curvature isn't used but could be in the future...maybe.)
  909. // c.) "Fix discontinuities" requires derivatives and drawing one tile with
  910. // border padding to the resized mask FBO, but it works with same-pass
  911. // curvature. It's disabled without DRIVERS_ALLOW_DERIVATIVES #defined.
  912. // Precedence: a, then, b, then c (if multiple strategies are #defined).
  913. #define ANISOTROPIC_TILING_COMPAT_TEX2DLOD // 129.7 FPS, 4x, flat; 101.8 at fullscreen
  914. #define ANISOTROPIC_TILING_COMPAT_TILE_FLAT_TWICE // 128.1 FPS, 4x, flat; 101.5 at fullscreen
  915. #define ANISOTROPIC_TILING_COMPAT_FIX_DISCONTINUITIES // 124.4 FPS, 4x, flat; 97.4 at fullscreen
  916. // Also, manually resampling the phosphor mask is slightly blurrier with
  917. // anisotropic filtering. (Resampling with mipmapping is even worse: It
  918. // creates artifacts, but only with the fully bloomed shader.) The difference
  919. // is subtle with small triads, but you can fix it for a small cost.
  920. //#define ANISOTROPIC_RESAMPLING_COMPAT_TEX2DLOD
  921. ////////////////////////////// DERIVED SETTINGS //////////////////////////////
  922. // Intel HD 4000 GPU's can't handle manual mask resizing (for now), setting the
  923. // geometry mode at runtime, or a 4x4 true Gaussian resize. Disable
  924. // incompatible settings ASAP. (INTEGRATED_GRAPHICS_COMPATIBILITY_MODE may be
  925. // #defined by either user-settings.h or a wrapper .cg that #includes the
  926. // current .cg pass.)
  927. #ifdef INTEGRATED_GRAPHICS_COMPATIBILITY_MODE
  928. #ifdef PHOSPHOR_MASK_MANUALLY_RESIZE
  929. #undef PHOSPHOR_MASK_MANUALLY_RESIZE
  930. #endif
  931. #ifdef RUNTIME_GEOMETRY_MODE
  932. #undef RUNTIME_GEOMETRY_MODE
  933. #endif
  934. // Mode 2 (4x4 Gaussian resize) won't work, and mode 1 (3x3 blur) is
  935. // inferior in most cases, so replace 2.0 with 0.0:
  936. static const float bloom_approx_filter =
  937. bloom_approx_filter_static > 1.5 ? 0.0 : bloom_approx_filter_static;
  938. #else
  939. static const float bloom_approx_filter = bloom_approx_filter_static;
  940. #endif
  941. // Disable slow runtime paths if static parameters are used. Most of these
  942. // won't be a problem anyway once the params are disabled, but some will.
  943. #ifndef RUNTIME_SHADER_PARAMS_ENABLE
  944. #ifdef RUNTIME_PHOSPHOR_BLOOM_SIGMA
  945. #undef RUNTIME_PHOSPHOR_BLOOM_SIGMA
  946. #endif
  947. #ifdef RUNTIME_ANTIALIAS_WEIGHTS
  948. #undef RUNTIME_ANTIALIAS_WEIGHTS
  949. #endif
  950. #ifdef RUNTIME_ANTIALIAS_SUBPIXEL_OFFSETS
  951. #undef RUNTIME_ANTIALIAS_SUBPIXEL_OFFSETS
  952. #endif
  953. #ifdef RUNTIME_SCANLINES_HORIZ_FILTER_COLORSPACE
  954. #undef RUNTIME_SCANLINES_HORIZ_FILTER_COLORSPACE
  955. #endif
  956. #ifdef RUNTIME_GEOMETRY_TILT
  957. #undef RUNTIME_GEOMETRY_TILT
  958. #endif
  959. #ifdef RUNTIME_GEOMETRY_MODE
  960. #undef RUNTIME_GEOMETRY_MODE
  961. #endif
  962. #ifdef FORCE_RUNTIME_PHOSPHOR_MASK_MODE_TYPE_SELECT
  963. #undef FORCE_RUNTIME_PHOSPHOR_MASK_MODE_TYPE_SELECT
  964. #endif
  965. #endif
  966. // Make tex2Dbias a backup for tex2Dlod for wider compatibility.
  967. #ifdef ANISOTROPIC_TILING_COMPAT_TEX2DLOD
  968. #define ANISOTROPIC_TILING_COMPAT_TEX2DBIAS
  969. #endif
  970. #ifdef ANISOTROPIC_RESAMPLING_COMPAT_TEX2DLOD
  971. #define ANISOTROPIC_RESAMPLING_COMPAT_TEX2DBIAS
  972. #endif
  973. // Rule out unavailable anisotropic compatibility strategies:
  974. #ifndef DRIVERS_ALLOW_DERIVATIVES
  975. #ifdef ANISOTROPIC_TILING_COMPAT_FIX_DISCONTINUITIES
  976. #undef ANISOTROPIC_TILING_COMPAT_FIX_DISCONTINUITIES
  977. #endif
  978. #endif
  979. #ifndef DRIVERS_ALLOW_TEX2DLOD
  980. #ifdef ANISOTROPIC_TILING_COMPAT_TEX2DLOD
  981. #undef ANISOTROPIC_TILING_COMPAT_TEX2DLOD
  982. #endif
  983. #ifdef ANISOTROPIC_RESAMPLING_COMPAT_TEX2DLOD
  984. #undef ANISOTROPIC_RESAMPLING_COMPAT_TEX2DLOD
  985. #endif
  986. #ifdef ANTIALIAS_DISABLE_ANISOTROPIC
  987. #undef ANTIALIAS_DISABLE_ANISOTROPIC
  988. #endif
  989. #endif
  990. #ifndef DRIVERS_ALLOW_TEX2DBIAS
  991. #ifdef ANISOTROPIC_TILING_COMPAT_TEX2DBIAS
  992. #undef ANISOTROPIC_TILING_COMPAT_TEX2DBIAS
  993. #endif
  994. #ifdef ANISOTROPIC_RESAMPLING_COMPAT_TEX2DBIAS
  995. #undef ANISOTROPIC_RESAMPLING_COMPAT_TEX2DBIAS
  996. #endif
  997. #endif
  998. // Prioritize anisotropic tiling compatibility strategies by performance and
  999. // disable unused strategies. This concentrates all the nesting in one place.
  1000. #ifdef ANISOTROPIC_TILING_COMPAT_TEX2DLOD
  1001. #ifdef ANISOTROPIC_TILING_COMPAT_TEX2DBIAS
  1002. #undef ANISOTROPIC_TILING_COMPAT_TEX2DBIAS
  1003. #endif
  1004. #ifdef ANISOTROPIC_TILING_COMPAT_TILE_FLAT_TWICE
  1005. #undef ANISOTROPIC_TILING_COMPAT_TILE_FLAT_TWICE
  1006. #endif
  1007. #ifdef ANISOTROPIC_TILING_COMPAT_FIX_DISCONTINUITIES
  1008. #undef ANISOTROPIC_TILING_COMPAT_FIX_DISCONTINUITIES
  1009. #endif
  1010. #else
  1011. #ifdef ANISOTROPIC_TILING_COMPAT_TEX2DBIAS
  1012. #ifdef ANISOTROPIC_TILING_COMPAT_TILE_FLAT_TWICE
  1013. #undef ANISOTROPIC_TILING_COMPAT_TILE_FLAT_TWICE
  1014. #endif
  1015. #ifdef ANISOTROPIC_TILING_COMPAT_FIX_DISCONTINUITIES
  1016. #undef ANISOTROPIC_TILING_COMPAT_FIX_DISCONTINUITIES
  1017. #endif
  1018. #else
  1019. // ANISOTROPIC_TILING_COMPAT_TILE_FLAT_TWICE is only compatible with
  1020. // flat texture coords in the same pass, but that's all we use.
  1021. #ifdef ANISOTROPIC_TILING_COMPAT_TILE_FLAT_TWICE
  1022. #ifdef ANISOTROPIC_TILING_COMPAT_FIX_DISCONTINUITIES
  1023. #undef ANISOTROPIC_TILING_COMPAT_FIX_DISCONTINUITIES
  1024. #endif
  1025. #endif
  1026. #endif
  1027. #endif
  1028. // The tex2Dlod and tex2Dbias strategies share a lot in common, and we can
  1029. // reduce some #ifdef nesting in the next section by essentially OR'ing them:
  1030. #ifdef ANISOTROPIC_TILING_COMPAT_TEX2DLOD
  1031. #define ANISOTROPIC_TILING_COMPAT_TEX2DLOD_FAMILY
  1032. #endif
  1033. #ifdef ANISOTROPIC_TILING_COMPAT_TEX2DBIAS
  1034. #define ANISOTROPIC_TILING_COMPAT_TEX2DLOD_FAMILY
  1035. #endif
  1036. // Prioritize anisotropic resampling compatibility strategies the same way:
  1037. #ifdef ANISOTROPIC_RESAMPLING_COMPAT_TEX2DLOD
  1038. #ifdef ANISOTROPIC_RESAMPLING_COMPAT_TEX2DBIAS
  1039. #undef ANISOTROPIC_RESAMPLING_COMPAT_TEX2DBIAS
  1040. #endif
  1041. #endif
  1042. /////////////////////// DERIVED PHOSPHOR MASK CONSTANTS //////////////////////
  1043. // If we can use the large mipmapped LUT without mipmapping artifacts, we
  1044. // should: It gives us more options for using fewer samples.
  1045. #ifdef DRIVERS_ALLOW_TEX2DLOD
  1046. #ifdef ANISOTROPIC_RESAMPLING_COMPAT_TEX2DLOD
  1047. // TODO: Take advantage of this!
  1048. #define PHOSPHOR_MASK_RESIZE_MIPMAPPED_LUT
  1049. static const float2 mask_resize_src_lut_size = mask_texture_large_size;
  1050. #else
  1051. static const float2 mask_resize_src_lut_size = mask_texture_small_size;
  1052. #endif
  1053. #else
  1054. static const float2 mask_resize_src_lut_size = mask_texture_small_size;
  1055. #endif
  1056. // tex2D's sampler2D parameter MUST be a uniform global, a uniform input to
  1057. // main_fragment, or a static alias of one of the above. This makes it hard
  1058. // to select the phosphor mask at runtime: We can't even assign to a uniform
  1059. // global in the vertex shader or select a sampler2D in the vertex shader and
  1060. // pass it to the fragment shader (even with explicit TEXUNIT# bindings),
  1061. // because it just gives us the input texture or a black screen. However, we
  1062. // can get around these limitations by calling tex2D three times with different
  1063. // uniform samplers (or resizing the phosphor mask three times altogether).
  1064. // With dynamic branches, we can process only one of these branches on top of
  1065. // quickly discarding fragments we don't need (cgc seems able to overcome
  1066. // limigations around dependent texture fetches inside of branches). Without
  1067. // dynamic branches, we have to process every branch for every fragment...which
  1068. // is slower. Runtime sampling mode selection is slower without dynamic
  1069. // branches as well. Let the user's static #defines decide if it's worth it.
  1070. #ifdef DRIVERS_ALLOW_DYNAMIC_BRANCHES
  1071. #define RUNTIME_PHOSPHOR_MASK_MODE_TYPE_SELECT
  1072. #else
  1073. #ifdef FORCE_RUNTIME_PHOSPHOR_MASK_MODE_TYPE_SELECT
  1074. #define RUNTIME_PHOSPHOR_MASK_MODE_TYPE_SELECT
  1075. #endif
  1076. #endif
  1077. // We need to render some minimum number of tiles in the resize passes.
  1078. // We need at least 1.0 just to repeat a single tile, and we need extra
  1079. // padding beyond that for anisotropic filtering, discontinuitity fixing,
  1080. // antialiasing, same-pass curvature (not currently used), etc. First
  1081. // determine how many border texels and tiles we need, based on how the result
  1082. // will be sampled:
  1083. #ifdef GEOMETRY_EARLY
  1084. static const float max_subpixel_offset = aa_subpixel_r_offset_static.x;
  1085. // Most antialiasing filters have a base radius of 4.0 pixels:
  1086. static const float max_aa_base_pixel_border = 4.0 +
  1087. max_subpixel_offset;
  1088. #else
  1089. static const float max_aa_base_pixel_border = 0.0;
  1090. #endif
  1091. // Anisotropic filtering adds about 0.5 to the pixel border:
  1092. #ifndef ANISOTROPIC_TILING_COMPAT_TEX2DLOD_FAMILY
  1093. static const float max_aniso_pixel_border = max_aa_base_pixel_border + 0.5;
  1094. #else
  1095. static const float max_aniso_pixel_border = max_aa_base_pixel_border;
  1096. #endif
  1097. // Fixing discontinuities adds 1.0 more to the pixel border:
  1098. #ifdef ANISOTROPIC_TILING_COMPAT_FIX_DISCONTINUITIES
  1099. static const float max_tiled_pixel_border = max_aniso_pixel_border + 1.0;
  1100. #else
  1101. static const float max_tiled_pixel_border = max_aniso_pixel_border;
  1102. #endif
  1103. // Convert the pixel border to an integer texel border. Assume same-pass
  1104. // curvature about triples the texel frequency:
  1105. #ifdef GEOMETRY_EARLY
  1106. static const float max_mask_texel_border =
  1107. ceil(max_tiled_pixel_border * 3.0);
  1108. #else
  1109. static const float max_mask_texel_border = ceil(max_tiled_pixel_border);
  1110. #endif
  1111. // Convert the texel border to a tile border using worst-case assumptions:
  1112. static const float max_mask_tile_border = max_mask_texel_border/
  1113. (mask_min_allowed_triad_size * mask_triads_per_tile);
  1114. // Finally, set the number of resized tiles to render to MASK_RESIZE, and set
  1115. // the starting texel (inside borders) for sampling it.
  1116. #ifndef GEOMETRY_EARLY
  1117. #ifdef ANISOTROPIC_TILING_COMPAT_TILE_FLAT_TWICE
  1118. // Special case: Render two tiles without borders. Anisotropic
  1119. // filtering doesn't seem to be a problem here.
  1120. static const float mask_resize_num_tiles = 1.0 + 1.0;
  1121. static const float mask_start_texels = 0.0;
  1122. #else
  1123. static const float mask_resize_num_tiles = 1.0 +
  1124. 2.0 * max_mask_tile_border;
  1125. static const float mask_start_texels = max_mask_texel_border;
  1126. #endif
  1127. #else
  1128. static const float mask_resize_num_tiles = 1.0 + 2.0*max_mask_tile_border;
  1129. static const float mask_start_texels = max_mask_texel_border;
  1130. #endif
  1131. // We have to fit mask_resize_num_tiles into an FBO with a viewport scale of
  1132. // mask_resize_viewport_scale. This limits the maximum final triad size.
  1133. // Estimate the minimum number of triads we can split the screen into in each
  1134. // dimension (we'll be as correct as mask_resize_viewport_scale is):
  1135. static const float mask_resize_num_triads =
  1136. mask_resize_num_tiles * mask_triads_per_tile;
  1137. static const float2 min_allowed_viewport_triads =
  1138. float2(mask_resize_num_triads) / mask_resize_viewport_scale;
  1139. //////////////////////// COMMON MATHEMATICAL CONSTANTS ///////////////////////
  1140. static const float pi = 3.141592653589;
  1141. // We often want to find the location of the previous texel, e.g.:
  1142. // const float2 curr_texel = uv * texture_size;
  1143. // const float2 prev_texel = floor(curr_texel - float2(0.5)) + float2(0.5);
  1144. // const float2 prev_texel_uv = prev_texel / texture_size;
  1145. // However, many GPU drivers round incorrectly around exact texel locations.
  1146. // We need to subtract a little less than 0.5 before flooring, and some GPU's
  1147. // require this value to be farther from 0.5 than others; define it here.
  1148. // const float2 prev_texel =
  1149. // floor(curr_texel - float2(under_half)) + float2(0.5);
  1150. static const float under_half = 0.4995;
  1151. #endif // DERIVED_SETTINGS_AND_CONSTANTS_H
  1152. ///////////////////////////// END DERIVED-SETTINGS-AND-CONSTANTS ////////////////////////////
  1153. //#include "bind-shader-h"
  1154. ///////////////////////////// BEGIN BIND-SHADER-PARAMS ////////////////////////////
  1155. #ifndef BIND_SHADER_PARAMS_H
  1156. #define BIND_SHADER_PARAMS_H
  1157. ///////////////////////////// GPL LICENSE NOTICE /////////////////////////////
  1158. // crt-royale: A full-featured CRT shader, with cheese.
  1159. // Copyright (C) 2014 TroggleMonkey <trogglemonkey@gmx.com>
  1160. //
  1161. // This program is free software; you can redistribute it and/or modify it
  1162. // under the terms of the GNU General Public License as published by the Free
  1163. // Software Foundation; either version 2 of the License, or any later version.
  1164. //
  1165. // This program is distributed in the hope that it will be useful, but WITHOUT
  1166. // ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
  1167. // FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
  1168. // more details.
  1169. //
  1170. // You should have received a copy of the GNU General Public License along with
  1171. // this program; if not, write to the Free Software Foundation, Inc., 59 Temple
  1172. // Place, Suite 330, Boston, MA 02111-1307 USA
  1173. ///////////////////////////// SETTINGS MANAGEMENT ////////////////////////////
  1174. /////////////////////////////// BEGIN INCLUDES ///////////////////////////////
  1175. //#include "../user-settings.h"
  1176. ///////////////////////////// BEGIN USER-SETTINGS ////////////////////////////
  1177. #ifndef USER_SETTINGS_H
  1178. #define USER_SETTINGS_H
  1179. ///////////////////////////// DRIVER CAPABILITIES ////////////////////////////
  1180. // The Cg compiler uses different "profiles" with different capabilities.
  1181. // This shader requires a Cg compilation profile >= arbfp1, but a few options
  1182. // require higher profiles like fp30 or fp40. The shader can't detect profile
  1183. // or driver capabilities, so instead you must comment or uncomment the lines
  1184. // below with "//" before "#define." Disable an option if you get compilation
  1185. // errors resembling those listed. Generally speaking, all of these options
  1186. // will run on nVidia cards, but only DRIVERS_ALLOW_TEX2DBIAS (if that) is
  1187. // likely to run on ATI/AMD, due to the Cg compiler's profile limitations.
  1188. // Derivatives: Unsupported on fp20, ps_1_1, ps_1_2, ps_1_3, and arbfp1.
  1189. // Among other things, derivatives help us fix anisotropic filtering artifacts
  1190. // with curved manually tiled phosphor mask coords. Related errors:
  1191. // error C3004: function "float2 ddx(float2);" not supported in this profile
  1192. // error C3004: function "float2 ddy(float2);" not supported in this profile
  1193. //#define DRIVERS_ALLOW_DERIVATIVES
  1194. // Fine derivatives: Unsupported on older ATI cards.
  1195. // Fine derivatives enable 2x2 fragment block communication, letting us perform
  1196. // fast single-pass blur operations. If your card uses coarse derivatives and
  1197. // these are enabled, blurs could look broken. Derivatives are a prerequisite.
  1198. #ifdef DRIVERS_ALLOW_DERIVATIVES
  1199. #define DRIVERS_ALLOW_FINE_DERIVATIVES
  1200. #endif
  1201. // Dynamic looping: Requires an fp30 or newer profile.
  1202. // This makes phosphor mask resampling faster in some cases. Related errors:
  1203. // error C5013: profile does not support "for" statements and "for" could not
  1204. // be unrolled
  1205. //#define DRIVERS_ALLOW_DYNAMIC_BRANCHES
  1206. // Without DRIVERS_ALLOW_DYNAMIC_BRANCHES, we need to use unrollable loops.
  1207. // Using one static loop avoids overhead if the user is right, but if the user
  1208. // is wrong (loops are allowed), breaking a loop into if-blocked pieces with a
  1209. // binary search can potentially save some iterations. However, it may fail:
  1210. // error C6001: Temporary register limit of 32 exceeded; 35 registers
  1211. // needed to compile program
  1212. //#define ACCOMODATE_POSSIBLE_DYNAMIC_LOOPS
  1213. // tex2Dlod: Requires an fp40 or newer profile. This can be used to disable
  1214. // anisotropic filtering, thereby fixing related artifacts. Related errors:
  1215. // error C3004: function "float4 tex2Dlod(sampler2D, float4);" not supported in
  1216. // this profile
  1217. //#define DRIVERS_ALLOW_TEX2DLOD
  1218. // tex2Dbias: Requires an fp30 or newer profile. This can be used to alleviate
  1219. // artifacts from anisotropic filtering and mipmapping. Related errors:
  1220. // error C3004: function "float4 tex2Dbias(sampler2D, float4);" not supported
  1221. // in this profile
  1222. //#define DRIVERS_ALLOW_TEX2DBIAS
  1223. // Integrated graphics compatibility: Integrated graphics like Intel HD 4000
  1224. // impose stricter limitations on register counts and instructions. Enable
  1225. // INTEGRATED_GRAPHICS_COMPATIBILITY_MODE if you still see error C6001 or:
  1226. // error C6002: Instruction limit of 1024 exceeded: 1523 instructions needed
  1227. // to compile program.
  1228. // Enabling integrated graphics compatibility mode will automatically disable:
  1229. // 1.) PHOSPHOR_MASK_MANUALLY_RESIZE: The phosphor mask will be softer.
  1230. // (This may be reenabled in a later release.)
  1231. // 2.) RUNTIME_GEOMETRY_MODE
  1232. // 3.) The high-quality 4x4 Gaussian resize for the bloom approximation
  1233. //#define INTEGRATED_GRAPHICS_COMPATIBILITY_MODE
  1234. //////////////////////////// USER CODEPATH OPTIONS ///////////////////////////
  1235. // To disable a #define option, turn its line into a comment with "//."
  1236. // RUNTIME VS. COMPILE-TIME OPTIONS (Major Performance Implications):
  1237. // Enable runtime shader parameters in the Retroarch (etc.) GUI? They override
  1238. // many of the options in this file and allow real-time tuning, but many of
  1239. // them are slower. Disabling them and using this text file will boost FPS.
  1240. #define RUNTIME_SHADER_PARAMS_ENABLE
  1241. // Specify the phosphor bloom sigma at runtime? This option is 10% slower, but
  1242. // it's the only way to do a wide-enough full bloom with a runtime dot pitch.
  1243. #define RUNTIME_PHOSPHOR_BLOOM_SIGMA
  1244. // Specify antialiasing weight parameters at runtime? (Costs ~20% with cubics)
  1245. #define RUNTIME_ANTIALIAS_WEIGHTS
  1246. // Specify subpixel offsets at runtime? (WARNING: EXTREMELY EXPENSIVE!)
  1247. //#define RUNTIME_ANTIALIAS_SUBPIXEL_OFFSETS
  1248. // Make beam_horiz_filter and beam_horiz_linear_rgb_weight into runtime shader
  1249. // parameters? This will require more math or dynamic branching.
  1250. #define RUNTIME_SCANLINES_HORIZ_FILTER_COLORSPACE
  1251. // Specify the tilt at runtime? This makes things about 3% slower.
  1252. #define RUNTIME_GEOMETRY_TILT
  1253. // Specify the geometry mode at runtime?
  1254. #define RUNTIME_GEOMETRY_MODE
  1255. // Specify the phosphor mask type (aperture grille, slot mask, shadow mask) and
  1256. // mode (Lanczos-resize, hardware resize, or tile 1:1) at runtime, even without
  1257. // dynamic branches? This is cheap if mask_resize_viewport_scale is small.
  1258. #define FORCE_RUNTIME_PHOSPHOR_MASK_MODE_TYPE_SELECT
  1259. // PHOSPHOR MASK:
  1260. // Manually resize the phosphor mask for best results (slower)? Disabling this
  1261. // removes the option to do so, but it may be faster without dynamic branches.
  1262. #define PHOSPHOR_MASK_MANUALLY_RESIZE
  1263. // If we sinc-resize the mask, should we Lanczos-window it (slower but better)?
  1264. #define PHOSPHOR_MASK_RESIZE_LANCZOS_WINDOW
  1265. // Larger blurs are expensive, but we need them to blur larger triads. We can
  1266. // detect the right blur if the triad size is static or our profile allows
  1267. // dynamic branches, but otherwise we use the largest blur the user indicates
  1268. // they might need:
  1269. #define PHOSPHOR_BLOOM_TRIADS_LARGER_THAN_3_PIXELS
  1270. //#define PHOSPHOR_BLOOM_TRIADS_LARGER_THAN_6_PIXELS
  1271. //#define PHOSPHOR_BLOOM_TRIADS_LARGER_THAN_9_PIXELS
  1272. //#define PHOSPHOR_BLOOM_TRIADS_LARGER_THAN_12_PIXELS
  1273. // Here's a helpful chart:
  1274. // MaxTriadSize BlurSize MinTriadCountsByResolution
  1275. // 3.0 9.0 480/640/960/1920 triads at 1080p/1440p/2160p/4320p, 4:3 aspect
  1276. // 6.0 17.0 240/320/480/960 triads at 1080p/1440p/2160p/4320p, 4:3 aspect
  1277. // 9.0 25.0 160/213/320/640 triads at 1080p/1440p/2160p/4320p, 4:3 aspect
  1278. // 12.0 31.0 120/160/240/480 triads at 1080p/1440p/2160p/4320p, 4:3 aspect
  1279. // 18.0 43.0 80/107/160/320 triads at 1080p/1440p/2160p/4320p, 4:3 aspect
  1280. /////////////////////////////// USER PARAMETERS //////////////////////////////
  1281. // Note: Many of these static parameters are overridden by runtime shader
  1282. // parameters when those are enabled. However, many others are static codepath
  1283. // options that were cleaner or more convert to code as static constants.
  1284. // GAMMA:
  1285. static const float crt_gamma_static = 2.5; // range [1, 5]
  1286. static const float lcd_gamma_static = 2.2; // range [1, 5]
  1287. // LEVELS MANAGEMENT:
  1288. // Control the final multiplicative image contrast:
  1289. static const float levels_contrast_static = 1.0; // range [0, 4)
  1290. // We auto-dim to avoid clipping between passes and restore brightness
  1291. // later. Control the dim factor here: Lower values clip less but crush
  1292. // blacks more (static only for now).
  1293. static const float levels_autodim_temp = 0.5; // range (0, 1] default is 0.5 but that was unnecessarily dark for me, so I set it to 1.0
  1294. // HALATION/DIFFUSION/BLOOM:
  1295. // Halation weight: How much energy should be lost to electrons bounding
  1296. // around under the CRT glass and exciting random phosphors?
  1297. static const float halation_weight_static = 0.0; // range [0, 1]
  1298. // Refractive diffusion weight: How much light should spread/diffuse from
  1299. // refracting through the CRT glass?
  1300. static const float diffusion_weight_static = 0.075; // range [0, 1]
  1301. // Underestimate brightness: Bright areas bloom more, but we can base the
  1302. // bloom brightpass on a lower brightness to sharpen phosphors, or a higher
  1303. // brightness to soften them. Low values clip, but >= 0.8 looks okay.
  1304. static const float bloom_underestimate_levels_static = 0.8; // range [0, 5]
  1305. // Blur all colors more than necessary for a softer phosphor bloom?
  1306. static const float bloom_excess_static = 0.0; // range [0, 1]
  1307. // The BLOOM_APPROX pass approximates a phosphor blur early on with a small
  1308. // blurred resize of the input (convergence offsets are applied as well).
  1309. // There are three filter options (static option only for now):
  1310. // 0.) Bilinear resize: A fast, close approximation to a 4x4 resize
  1311. // if min_allowed_viewport_triads and the BLOOM_APPROX resolution are sane
  1312. // and beam_max_sigma is low.
  1313. // 1.) 3x3 resize blur: Medium speed, soft/smeared from bilinear blurring,
  1314. // always uses a static sigma regardless of beam_max_sigma or
  1315. // mask_num_triads_desired.
  1316. // 2.) True 4x4 Gaussian resize: Slowest, technically correct.
  1317. // These options are more pronounced for the fast, unbloomed shader version.
  1318. #ifndef RADEON_FIX
  1319. static const float bloom_approx_filter_static = 2.0;
  1320. #else
  1321. static const float bloom_approx_filter_static = 1.0;
  1322. #endif
  1323. // ELECTRON BEAM SCANLINE DISTRIBUTION:
  1324. // How many scanlines should contribute light to each pixel? Using more
  1325. // scanlines is slower (especially for a generalized Gaussian) but less
  1326. // distorted with larger beam sigmas (especially for a pure Gaussian). The
  1327. // max_beam_sigma at which the closest unused weight is guaranteed <
  1328. // 1.0/255.0 (for a 3x antialiased pure Gaussian) is:
  1329. // 2 scanlines: max_beam_sigma = 0.2089; distortions begin ~0.34; 141.7 FPS pure, 131.9 FPS generalized
  1330. // 3 scanlines, max_beam_sigma = 0.3879; distortions begin ~0.52; 137.5 FPS pure; 123.8 FPS generalized
  1331. // 4 scanlines, max_beam_sigma = 0.5723; distortions begin ~0.70; 134.7 FPS pure; 117.2 FPS generalized
  1332. // 5 scanlines, max_beam_sigma = 0.7591; distortions begin ~0.89; 131.6 FPS pure; 112.1 FPS generalized
  1333. // 6 scanlines, max_beam_sigma = 0.9483; distortions begin ~1.08; 127.9 FPS pure; 105.6 FPS generalized
  1334. static const float beam_num_scanlines = 3.0; // range [2, 6]
  1335. // A generalized Gaussian beam varies shape with color too, now just width.
  1336. // It's slower but more flexible (static option only for now).
  1337. static const bool beam_generalized_gaussian = true;
  1338. // What kind of scanline antialiasing do you want?
  1339. // 0: Sample weights at 1x; 1: Sample weights at 3x; 2: Compute an integral
  1340. // Integrals are slow (especially for generalized Gaussians) and rarely any
  1341. // better than 3x antialiasing (static option only for now).
  1342. static const float beam_antialias_level = 1.0; // range [0, 2]
  1343. // Min/max standard deviations for scanline beams: Higher values widen and
  1344. // soften scanlines. Depending on other options, low min sigmas can alias.
  1345. static const float beam_min_sigma_static = 0.02; // range (0, 1]
  1346. static const float beam_max_sigma_static = 0.3; // range (0, 1]
  1347. // Beam width varies as a function of color: A power function (0) is more
  1348. // configurable, but a spherical function (1) gives the widest beam
  1349. // variability without aliasing (static option only for now).
  1350. static const float beam_spot_shape_function = 0.0;
  1351. // Spot shape power: Powers <= 1 give smoother spot shapes but lower
  1352. // sharpness. Powers >= 1.0 are awful unless mix/max sigmas are close.
  1353. static const float beam_spot_power_static = 1.0/3.0; // range (0, 16]
  1354. // Generalized Gaussian max shape parameters: Higher values give flatter
  1355. // scanline plateaus and steeper dropoffs, simultaneously widening and
  1356. // sharpening scanlines at the cost of aliasing. 2.0 is pure Gaussian, and
  1357. // values > ~40.0 cause artifacts with integrals.
  1358. static const float beam_min_shape_static = 2.0; // range [2, 32]
  1359. static const float beam_max_shape_static = 4.0; // range [2, 32]
  1360. // Generalized Gaussian shape power: Affects how quickly the distribution
  1361. // changes shape from Gaussian to steep/plateaued as color increases from 0
  1362. // to 1.0. Higher powers appear softer for most colors, and lower powers
  1363. // appear sharper for most colors.
  1364. static const float beam_shape_power_static = 1.0/4.0; // range (0, 16]
  1365. // What filter should be used to sample scanlines horizontally?
  1366. // 0: Quilez (fast), 1: Gaussian (configurable), 2: Lanczos2 (sharp)
  1367. static const float beam_horiz_filter_static = 0.0;
  1368. // Standard deviation for horizontal Gaussian resampling:
  1369. static const float beam_horiz_sigma_static = 0.35; // range (0, 2/3]
  1370. // Do horizontal scanline sampling in linear RGB (correct light mixing),
  1371. // gamma-encoded RGB (darker, hard spot shape, may better match bandwidth-
  1372. // limiting circuitry in some CRT's), or a weighted avg.?
  1373. static const float beam_horiz_linear_rgb_weight_static = 1.0; // range [0, 1]
  1374. // Simulate scanline misconvergence? This needs 3x horizontal texture
  1375. // samples and 3x texture samples of BLOOM_APPROX and HALATION_BLUR in
  1376. // later passes (static option only for now).
  1377. static const bool beam_misconvergence = true;
  1378. // Convergence offsets in x/y directions for R/G/B scanline beams in units
  1379. // of scanlines. Positive offsets go right/down; ranges [-2, 2]
  1380. static const float2 convergence_offsets_r_static = float2(0.1, 0.2);
  1381. static const float2 convergence_offsets_g_static = float2(0.3, 0.4);
  1382. static const float2 convergence_offsets_b_static = float2(0.5, 0.6);
  1383. // Detect interlacing (static option only for now)?
  1384. static const bool interlace_detect = true;
  1385. // Assume 1080-line sources are interlaced?
  1386. static const bool interlace_1080i_static = false;
  1387. // For interlaced sources, assume TFF (top-field first) or BFF order?
  1388. // (Whether this matters depends on the nature of the interlaced input.)
  1389. static const bool interlace_bff_static = false;
  1390. // ANTIALIASING:
  1391. // What AA level do you want for curvature/overscan/subpixels? Options:
  1392. // 0x (none), 1x (sample subpixels), 4x, 5x, 6x, 7x, 8x, 12x, 16x, 20x, 24x
  1393. // (Static option only for now)
  1394. static const float aa_level = 12.0; // range [0, 24]
  1395. // What antialiasing filter do you want (static option only)? Options:
  1396. // 0: Box (separable), 1: Box (cylindrical),
  1397. // 2: Tent (separable), 3: Tent (cylindrical),
  1398. // 4: Gaussian (separable), 5: Gaussian (cylindrical),
  1399. // 6: Cubic* (separable), 7: Cubic* (cylindrical, poor)
  1400. // 8: Lanczos Sinc (separable), 9: Lanczos Jinc (cylindrical, poor)
  1401. // * = Especially slow with RUNTIME_ANTIALIAS_WEIGHTS
  1402. static const float aa_filter = 6.0; // range [0, 9]
  1403. // Flip the sample grid on odd/even frames (static option only for now)?
  1404. static const bool aa_temporal = false;
  1405. // Use RGB subpixel offsets for antialiasing? The pixel is at green, and
  1406. // the blue offset is the negative r offset; range [0, 0.5]
  1407. static const float2 aa_subpixel_r_offset_static = float2(-1.0/3.0, 0.0);//float2(0.0);
  1408. // Cubics: See http://www.imagemagick.org/Usage/filter/#mitchell
  1409. // 1.) "Keys cubics" with B = 1 - 2C are considered the highest quality.
  1410. // 2.) C = 0.5 (default) is Catmull-Rom; higher C's apply sharpening.
  1411. // 3.) C = 1.0/3.0 is the Mitchell-Netravali filter.
  1412. // 4.) C = 0.0 is a soft spline filter.
  1413. static const float aa_cubic_c_static = 0.5; // range [0, 4]
  1414. // Standard deviation for Gaussian antialiasing: Try 0.5/aa_pixel_diameter.
  1415. static const float aa_gauss_sigma_static = 0.5; // range [0.0625, 1.0]
  1416. // PHOSPHOR MASK:
  1417. // Mask type: 0 = aperture grille, 1 = slot mask, 2 = EDP shadow mask
  1418. static const float mask_type_static = 1.0; // range [0, 2]
  1419. // We can sample the mask three ways. Pick 2/3 from: Pretty/Fast/Flexible.
  1420. // 0.) Sinc-resize to the desired dot pitch manually (pretty/slow/flexible).
  1421. // This requires PHOSPHOR_MASK_MANUALLY_RESIZE to be #defined.
  1422. // 1.) Hardware-resize to the desired dot pitch (ugly/fast/flexible). This
  1423. // is halfway decent with LUT mipmapping but atrocious without it.
  1424. // 2.) Tile it without resizing at a 1:1 texel:pixel ratio for flat coords
  1425. // (pretty/fast/inflexible). Each input LUT has a fixed dot pitch.
  1426. // This mode reuses the same masks, so triads will be enormous unless
  1427. // you change the mask LUT filenames in your .cgp file.
  1428. static const float mask_sample_mode_static = 0.0; // range [0, 2]
  1429. // Prefer setting the triad size (0.0) or number on the screen (1.0)?
  1430. // If RUNTIME_PHOSPHOR_BLOOM_SIGMA isn't #defined, the specified triad size
  1431. // will always be used to calculate the full bloom sigma statically.
  1432. static const float mask_specify_num_triads_static = 0.0; // range [0, 1]
  1433. // Specify the phosphor triad size, in pixels. Each tile (usually with 8
  1434. // triads) will be rounded to the nearest integer tile size and clamped to
  1435. // obey minimum size constraints (imposed to reduce downsize taps) and
  1436. // maximum size constraints (imposed to have a sane MASK_RESIZE FBO size).
  1437. // To increase the size limit, double the viewport-relative scales for the
  1438. // two MASK_RESIZE passes in crt-royale.cgp and user-cgp-contants.h.
  1439. // range [1, mask_texture_small_size/mask_triads_per_tile]
  1440. static const float mask_triad_size_desired_static = 24.0 / 8.0;
  1441. // If mask_specify_num_triads is 1.0/true, we'll go by this instead (the
  1442. // final size will be rounded and constrained as above); default 480.0
  1443. static const float mask_num_triads_desired_static = 480.0;
  1444. // How many lobes should the sinc/Lanczos resizer use? More lobes require
  1445. // more samples and avoid moire a bit better, but some is unavoidable
  1446. // depending on the destination size (static option for now).
  1447. static const float mask_sinc_lobes = 3.0; // range [2, 4]
  1448. // The mask is resized using a variable number of taps in each dimension,
  1449. // but some Cg profiles always fetch a constant number of taps no matter
  1450. // what (no dynamic branching). We can limit the maximum number of taps if
  1451. // we statically limit the minimum phosphor triad size. Larger values are
  1452. // faster, but the limit IS enforced (static option only, forever);
  1453. // range [1, mask_texture_small_size/mask_triads_per_tile]
  1454. // TODO: Make this 1.0 and compensate with smarter sampling!
  1455. static const float mask_min_allowed_triad_size = 2.0;
  1456. // GEOMETRY:
  1457. // Geometry mode:
  1458. // 0: Off (default), 1: Spherical mapping (like cgwg's),
  1459. // 2: Alt. spherical mapping (more bulbous), 3: Cylindrical/Trinitron
  1460. static const float geom_mode_static = 0.0; // range [0, 3]
  1461. // Radius of curvature: Measured in units of your viewport's diagonal size.
  1462. static const float geom_radius_static = 2.0; // range [1/(2*pi), 1024]
  1463. // View dist is the distance from the player to their physical screen, in
  1464. // units of the viewport's diagonal size. It controls the field of view.
  1465. static const float geom_view_dist_static = 2.0; // range [0.5, 1024]
  1466. // Tilt angle in radians (clockwise around up and right vectors):
  1467. static const float2 geom_tilt_angle_static = float2(0.0, 0.0); // range [-pi, pi]
  1468. // Aspect ratio: When the true viewport size is unknown, this value is used
  1469. // to help convert between the phosphor triad size and count, along with
  1470. // the mask_resize_viewport_scale constant from user-cgp-constants.h. Set
  1471. // this equal to Retroarch's display aspect ratio (DAR) for best results;
  1472. // range [1, geom_max_aspect_ratio from user-cgp-constants.h];
  1473. // default (256/224)*(54/47) = 1.313069909 (see below)
  1474. static const float geom_aspect_ratio_static = 1.313069909;
  1475. // Before getting into overscan, here's some general aspect ratio info:
  1476. // - DAR = display aspect ratio = SAR * PAR; as in your Retroarch setting
  1477. // - SAR = storage aspect ratio = DAR / PAR; square pixel emulator frame AR
  1478. // - PAR = pixel aspect ratio = DAR / SAR; holds regardless of cropping
  1479. // Geometry processing has to "undo" the screen-space 2D DAR to calculate
  1480. // 3D view vectors, then reapplies the aspect ratio to the simulated CRT in
  1481. // uv-space. To ensure the source SAR is intended for a ~4:3 DAR, either:
  1482. // a.) Enable Retroarch's "Crop Overscan"
  1483. // b.) Readd horizontal padding: Set overscan to e.g. N*(1.0, 240.0/224.0)
  1484. // Real consoles use horizontal black padding in the signal, but emulators
  1485. // often crop this without cropping the vertical padding; a 256x224 [S]NES
  1486. // frame (8:7 SAR) is intended for a ~4:3 DAR, but a 256x240 frame is not.
  1487. // The correct [S]NES PAR is 54:47, found by blargg and NewRisingSun:
  1488. // http://board.zsnes.com/phpBB3/viewtopic.php?f=22&t=11928&start=50
  1489. // http://forums.nesdev.com/viewtopic.php?p=24815#p24815
  1490. // For flat output, it's okay to set DAR = [existing] SAR * [correct] PAR
  1491. // without doing a. or b., but horizontal image borders will be tighter
  1492. // than vertical ones, messing up curvature and overscan. Fixing the
  1493. // padding first corrects this.
  1494. // Overscan: Amount to "zoom in" before cropping. You can zoom uniformly
  1495. // or adjust x/y independently to e.g. readd horizontal padding, as noted
  1496. // above: Values < 1.0 zoom out; range (0, inf)
  1497. static const float2 geom_overscan_static = float2(1.0, 1.0);// * 1.005 * (1.0, 240/224.0)
  1498. // Compute a proper pixel-space to texture-space matrix even without ddx()/
  1499. // ddy()? This is ~8.5% slower but improves antialiasing/subpixel filtering
  1500. // with strong curvature (static option only for now).
  1501. static const bool geom_force_correct_tangent_matrix = true;
  1502. // BORDERS:
  1503. // Rounded border size in texture uv coords:
  1504. static const float border_size_static = 0.015; // range [0, 0.5]
  1505. // Border darkness: Moderate values darken the border smoothly, and high
  1506. // values make the image very dark just inside the border:
  1507. static const float border_darkness_static = 2.0; // range [0, inf)
  1508. // Border compression: High numbers compress border transitions, narrowing
  1509. // the dark border area.
  1510. static const float border_compress_static = 2.5; // range [1, inf)
  1511. #endif // USER_SETTINGS_H
  1512. ///////////////////////////// END USER-SETTINGS ////////////////////////////
  1513. //#include "derived-settings-and-constants.h"
  1514. ///////////////////// BEGIN DERIVED-SETTINGS-AND-CONSTANTS ////////////////////
  1515. #ifndef DERIVED_SETTINGS_AND_CONSTANTS_H
  1516. #define DERIVED_SETTINGS_AND_CONSTANTS_H
  1517. ///////////////////////////// GPL LICENSE NOTICE /////////////////////////////
  1518. // crt-royale: A full-featured CRT shader, with cheese.
  1519. // Copyright (C) 2014 TroggleMonkey <trogglemonkey@gmx.com>
  1520. //
  1521. // This program is free software; you can redistribute it and/or modify it
  1522. // under the terms of the GNU General Public License as published by the Free
  1523. // Software Foundation; either version 2 of the License, or any later version.
  1524. //
  1525. // This program is distributed in the hope that it will be useful, but WITHOUT
  1526. // ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
  1527. // FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
  1528. // more details.
  1529. //
  1530. // You should have received a copy of the GNU General Public License along with
  1531. // this program; if not, write to the Free Software Foundation, Inc., 59 Temple
  1532. // Place, Suite 330, Boston, MA 02111-1307 USA
  1533. ///////////////////////////////// DESCRIPTION ////////////////////////////////
  1534. // These macros and constants can be used across the whole codebase.
  1535. // Unlike the values in user-settings.cgh, end users shouldn't modify these.
  1536. /////////////////////////////// BEGIN INCLUDES ///////////////////////////////
  1537. //#include "../user-settings.h"
  1538. ///////////////////////////// BEGIN USER-SETTINGS ////////////////////////////
  1539. #ifndef USER_SETTINGS_H
  1540. #define USER_SETTINGS_H
  1541. ///////////////////////////// DRIVER CAPABILITIES ////////////////////////////
  1542. // The Cg compiler uses different "profiles" with different capabilities.
  1543. // This shader requires a Cg compilation profile >= arbfp1, but a few options
  1544. // require higher profiles like fp30 or fp40. The shader can't detect profile
  1545. // or driver capabilities, so instead you must comment or uncomment the lines
  1546. // below with "//" before "#define." Disable an option if you get compilation
  1547. // errors resembling those listed. Generally speaking, all of these options
  1548. // will run on nVidia cards, but only DRIVERS_ALLOW_TEX2DBIAS (if that) is
  1549. // likely to run on ATI/AMD, due to the Cg compiler's profile limitations.
  1550. // Derivatives: Unsupported on fp20, ps_1_1, ps_1_2, ps_1_3, and arbfp1.
  1551. // Among other things, derivatives help us fix anisotropic filtering artifacts
  1552. // with curved manually tiled phosphor mask coords. Related errors:
  1553. // error C3004: function "float2 ddx(float2);" not supported in this profile
  1554. // error C3004: function "float2 ddy(float2);" not supported in this profile
  1555. //#define DRIVERS_ALLOW_DERIVATIVES
  1556. // Fine derivatives: Unsupported on older ATI cards.
  1557. // Fine derivatives enable 2x2 fragment block communication, letting us perform
  1558. // fast single-pass blur operations. If your card uses coarse derivatives and
  1559. // these are enabled, blurs could look broken. Derivatives are a prerequisite.
  1560. #ifdef DRIVERS_ALLOW_DERIVATIVES
  1561. #define DRIVERS_ALLOW_FINE_DERIVATIVES
  1562. #endif
  1563. // Dynamic looping: Requires an fp30 or newer profile.
  1564. // This makes phosphor mask resampling faster in some cases. Related errors:
  1565. // error C5013: profile does not support "for" statements and "for" could not
  1566. // be unrolled
  1567. //#define DRIVERS_ALLOW_DYNAMIC_BRANCHES
  1568. // Without DRIVERS_ALLOW_DYNAMIC_BRANCHES, we need to use unrollable loops.
  1569. // Using one static loop avoids overhead if the user is right, but if the user
  1570. // is wrong (loops are allowed), breaking a loop into if-blocked pieces with a
  1571. // binary search can potentially save some iterations. However, it may fail:
  1572. // error C6001: Temporary register limit of 32 exceeded; 35 registers
  1573. // needed to compile program
  1574. //#define ACCOMODATE_POSSIBLE_DYNAMIC_LOOPS
  1575. // tex2Dlod: Requires an fp40 or newer profile. This can be used to disable
  1576. // anisotropic filtering, thereby fixing related artifacts. Related errors:
  1577. // error C3004: function "float4 tex2Dlod(sampler2D, float4);" not supported in
  1578. // this profile
  1579. //#define DRIVERS_ALLOW_TEX2DLOD
  1580. // tex2Dbias: Requires an fp30 or newer profile. This can be used to alleviate
  1581. // artifacts from anisotropic filtering and mipmapping. Related errors:
  1582. // error C3004: function "float4 tex2Dbias(sampler2D, float4);" not supported
  1583. // in this profile
  1584. //#define DRIVERS_ALLOW_TEX2DBIAS
  1585. // Integrated graphics compatibility: Integrated graphics like Intel HD 4000
  1586. // impose stricter limitations on register counts and instructions. Enable
  1587. // INTEGRATED_GRAPHICS_COMPATIBILITY_MODE if you still see error C6001 or:
  1588. // error C6002: Instruction limit of 1024 exceeded: 1523 instructions needed
  1589. // to compile program.
  1590. // Enabling integrated graphics compatibility mode will automatically disable:
  1591. // 1.) PHOSPHOR_MASK_MANUALLY_RESIZE: The phosphor mask will be softer.
  1592. // (This may be reenabled in a later release.)
  1593. // 2.) RUNTIME_GEOMETRY_MODE
  1594. // 3.) The high-quality 4x4 Gaussian resize for the bloom approximation
  1595. //#define INTEGRATED_GRAPHICS_COMPATIBILITY_MODE
  1596. //////////////////////////// USER CODEPATH OPTIONS ///////////////////////////
  1597. // To disable a #define option, turn its line into a comment with "//."
  1598. // RUNTIME VS. COMPILE-TIME OPTIONS (Major Performance Implications):
  1599. // Enable runtime shader parameters in the Retroarch (etc.) GUI? They override
  1600. // many of the options in this file and allow real-time tuning, but many of
  1601. // them are slower. Disabling them and using this text file will boost FPS.
  1602. #define RUNTIME_SHADER_PARAMS_ENABLE
  1603. // Specify the phosphor bloom sigma at runtime? This option is 10% slower, but
  1604. // it's the only way to do a wide-enough full bloom with a runtime dot pitch.
  1605. #define RUNTIME_PHOSPHOR_BLOOM_SIGMA
  1606. // Specify antialiasing weight parameters at runtime? (Costs ~20% with cubics)
  1607. #define RUNTIME_ANTIALIAS_WEIGHTS
  1608. // Specify subpixel offsets at runtime? (WARNING: EXTREMELY EXPENSIVE!)
  1609. //#define RUNTIME_ANTIALIAS_SUBPIXEL_OFFSETS
  1610. // Make beam_horiz_filter and beam_horiz_linear_rgb_weight into runtime shader
  1611. // parameters? This will require more math or dynamic branching.
  1612. #define RUNTIME_SCANLINES_HORIZ_FILTER_COLORSPACE
  1613. // Specify the tilt at runtime? This makes things about 3% slower.
  1614. #define RUNTIME_GEOMETRY_TILT
  1615. // Specify the geometry mode at runtime?
  1616. #define RUNTIME_GEOMETRY_MODE
  1617. // Specify the phosphor mask type (aperture grille, slot mask, shadow mask) and
  1618. // mode (Lanczos-resize, hardware resize, or tile 1:1) at runtime, even without
  1619. // dynamic branches? This is cheap if mask_resize_viewport_scale is small.
  1620. #define FORCE_RUNTIME_PHOSPHOR_MASK_MODE_TYPE_SELECT
  1621. // PHOSPHOR MASK:
  1622. // Manually resize the phosphor mask for best results (slower)? Disabling this
  1623. // removes the option to do so, but it may be faster without dynamic branches.
  1624. #define PHOSPHOR_MASK_MANUALLY_RESIZE
  1625. // If we sinc-resize the mask, should we Lanczos-window it (slower but better)?
  1626. #define PHOSPHOR_MASK_RESIZE_LANCZOS_WINDOW
  1627. // Larger blurs are expensive, but we need them to blur larger triads. We can
  1628. // detect the right blur if the triad size is static or our profile allows
  1629. // dynamic branches, but otherwise we use the largest blur the user indicates
  1630. // they might need:
  1631. #define PHOSPHOR_BLOOM_TRIADS_LARGER_THAN_3_PIXELS
  1632. //#define PHOSPHOR_BLOOM_TRIADS_LARGER_THAN_6_PIXELS
  1633. //#define PHOSPHOR_BLOOM_TRIADS_LARGER_THAN_9_PIXELS
  1634. //#define PHOSPHOR_BLOOM_TRIADS_LARGER_THAN_12_PIXELS
  1635. // Here's a helpful chart:
  1636. // MaxTriadSize BlurSize MinTriadCountsByResolution
  1637. // 3.0 9.0 480/640/960/1920 triads at 1080p/1440p/2160p/4320p, 4:3 aspect
  1638. // 6.0 17.0 240/320/480/960 triads at 1080p/1440p/2160p/4320p, 4:3 aspect
  1639. // 9.0 25.0 160/213/320/640 triads at 1080p/1440p/2160p/4320p, 4:3 aspect
  1640. // 12.0 31.0 120/160/240/480 triads at 1080p/1440p/2160p/4320p, 4:3 aspect
  1641. // 18.0 43.0 80/107/160/320 triads at 1080p/1440p/2160p/4320p, 4:3 aspect
  1642. /////////////////////////////// USER PARAMETERS //////////////////////////////
  1643. // Note: Many of these static parameters are overridden by runtime shader
  1644. // parameters when those are enabled. However, many others are static codepath
  1645. // options that were cleaner or more convert to code as static constants.
  1646. // GAMMA:
  1647. static const float crt_gamma_static = 2.5; // range [1, 5]
  1648. static const float lcd_gamma_static = 2.2; // range [1, 5]
  1649. // LEVELS MANAGEMENT:
  1650. // Control the final multiplicative image contrast:
  1651. static const float levels_contrast_static = 1.0; // range [0, 4)
  1652. // We auto-dim to avoid clipping between passes and restore brightness
  1653. // later. Control the dim factor here: Lower values clip less but crush
  1654. // blacks more (static only for now).
  1655. static const float levels_autodim_temp = 0.5; // range (0, 1] default is 0.5 but that was unnecessarily dark for me, so I set it to 1.0
  1656. // HALATION/DIFFUSION/BLOOM:
  1657. // Halation weight: How much energy should be lost to electrons bounding
  1658. // around under the CRT glass and exciting random phosphors?
  1659. static const float halation_weight_static = 0.0; // range [0, 1]
  1660. // Refractive diffusion weight: How much light should spread/diffuse from
  1661. // refracting through the CRT glass?
  1662. static const float diffusion_weight_static = 0.075; // range [0, 1]
  1663. // Underestimate brightness: Bright areas bloom more, but we can base the
  1664. // bloom brightpass on a lower brightness to sharpen phosphors, or a higher
  1665. // brightness to soften them. Low values clip, but >= 0.8 looks okay.
  1666. static const float bloom_underestimate_levels_static = 0.8; // range [0, 5]
  1667. // Blur all colors more than necessary for a softer phosphor bloom?
  1668. static const float bloom_excess_static = 0.0; // range [0, 1]
  1669. // The BLOOM_APPROX pass approximates a phosphor blur early on with a small
  1670. // blurred resize of the input (convergence offsets are applied as well).
  1671. // There are three filter options (static option only for now):
  1672. // 0.) Bilinear resize: A fast, close approximation to a 4x4 resize
  1673. // if min_allowed_viewport_triads and the BLOOM_APPROX resolution are sane
  1674. // and beam_max_sigma is low.
  1675. // 1.) 3x3 resize blur: Medium speed, soft/smeared from bilinear blurring,
  1676. // always uses a static sigma regardless of beam_max_sigma or
  1677. // mask_num_triads_desired.
  1678. // 2.) True 4x4 Gaussian resize: Slowest, technically correct.
  1679. // These options are more pronounced for the fast, unbloomed shader version.
  1680. #ifndef RADEON_FIX
  1681. static const float bloom_approx_filter_static = 2.0;
  1682. #else
  1683. static const float bloom_approx_filter_static = 1.0;
  1684. #endif
  1685. // ELECTRON BEAM SCANLINE DISTRIBUTION:
  1686. // How many scanlines should contribute light to each pixel? Using more
  1687. // scanlines is slower (especially for a generalized Gaussian) but less
  1688. // distorted with larger beam sigmas (especially for a pure Gaussian). The
  1689. // max_beam_sigma at which the closest unused weight is guaranteed <
  1690. // 1.0/255.0 (for a 3x antialiased pure Gaussian) is:
  1691. // 2 scanlines: max_beam_sigma = 0.2089; distortions begin ~0.34; 141.7 FPS pure, 131.9 FPS generalized
  1692. // 3 scanlines, max_beam_sigma = 0.3879; distortions begin ~0.52; 137.5 FPS pure; 123.8 FPS generalized
  1693. // 4 scanlines, max_beam_sigma = 0.5723; distortions begin ~0.70; 134.7 FPS pure; 117.2 FPS generalized
  1694. // 5 scanlines, max_beam_sigma = 0.7591; distortions begin ~0.89; 131.6 FPS pure; 112.1 FPS generalized
  1695. // 6 scanlines, max_beam_sigma = 0.9483; distortions begin ~1.08; 127.9 FPS pure; 105.6 FPS generalized
  1696. static const float beam_num_scanlines = 3.0; // range [2, 6]
  1697. // A generalized Gaussian beam varies shape with color too, now just width.
  1698. // It's slower but more flexible (static option only for now).
  1699. static const bool beam_generalized_gaussian = true;
  1700. // What kind of scanline antialiasing do you want?
  1701. // 0: Sample weights at 1x; 1: Sample weights at 3x; 2: Compute an integral
  1702. // Integrals are slow (especially for generalized Gaussians) and rarely any
  1703. // better than 3x antialiasing (static option only for now).
  1704. static const float beam_antialias_level = 1.0; // range [0, 2]
  1705. // Min/max standard deviations for scanline beams: Higher values widen and
  1706. // soften scanlines. Depending on other options, low min sigmas can alias.
  1707. static const float beam_min_sigma_static = 0.02; // range (0, 1]
  1708. static const float beam_max_sigma_static = 0.3; // range (0, 1]
  1709. // Beam width varies as a function of color: A power function (0) is more
  1710. // configurable, but a spherical function (1) gives the widest beam
  1711. // variability without aliasing (static option only for now).
  1712. static const float beam_spot_shape_function = 0.0;
  1713. // Spot shape power: Powers <= 1 give smoother spot shapes but lower
  1714. // sharpness. Powers >= 1.0 are awful unless mix/max sigmas are close.
  1715. static const float beam_spot_power_static = 1.0/3.0; // range (0, 16]
  1716. // Generalized Gaussian max shape parameters: Higher values give flatter
  1717. // scanline plateaus and steeper dropoffs, simultaneously widening and
  1718. // sharpening scanlines at the cost of aliasing. 2.0 is pure Gaussian, and
  1719. // values > ~40.0 cause artifacts with integrals.
  1720. static const float beam_min_shape_static = 2.0; // range [2, 32]
  1721. static const float beam_max_shape_static = 4.0; // range [2, 32]
  1722. // Generalized Gaussian shape power: Affects how quickly the distribution
  1723. // changes shape from Gaussian to steep/plateaued as color increases from 0
  1724. // to 1.0. Higher powers appear softer for most colors, and lower powers
  1725. // appear sharper for most colors.
  1726. static const float beam_shape_power_static = 1.0/4.0; // range (0, 16]
  1727. // What filter should be used to sample scanlines horizontally?
  1728. // 0: Quilez (fast), 1: Gaussian (configurable), 2: Lanczos2 (sharp)
  1729. static const float beam_horiz_filter_static = 0.0;
  1730. // Standard deviation for horizontal Gaussian resampling:
  1731. static const float beam_horiz_sigma_static = 0.35; // range (0, 2/3]
  1732. // Do horizontal scanline sampling in linear RGB (correct light mixing),
  1733. // gamma-encoded RGB (darker, hard spot shape, may better match bandwidth-
  1734. // limiting circuitry in some CRT's), or a weighted avg.?
  1735. static const float beam_horiz_linear_rgb_weight_static = 1.0; // range [0, 1]
  1736. // Simulate scanline misconvergence? This needs 3x horizontal texture
  1737. // samples and 3x texture samples of BLOOM_APPROX and HALATION_BLUR in
  1738. // later passes (static option only for now).
  1739. static const bool beam_misconvergence = true;
  1740. // Convergence offsets in x/y directions for R/G/B scanline beams in units
  1741. // of scanlines. Positive offsets go right/down; ranges [-2, 2]
  1742. static const float2 convergence_offsets_r_static = float2(0.1, 0.2);
  1743. static const float2 convergence_offsets_g_static = float2(0.3, 0.4);
  1744. static const float2 convergence_offsets_b_static = float2(0.5, 0.6);
  1745. // Detect interlacing (static option only for now)?
  1746. static const bool interlace_detect = true;
  1747. // Assume 1080-line sources are interlaced?
  1748. static const bool interlace_1080i_static = false;
  1749. // For interlaced sources, assume TFF (top-field first) or BFF order?
  1750. // (Whether this matters depends on the nature of the interlaced input.)
  1751. static const bool interlace_bff_static = false;
  1752. // ANTIALIASING:
  1753. // What AA level do you want for curvature/overscan/subpixels? Options:
  1754. // 0x (none), 1x (sample subpixels), 4x, 5x, 6x, 7x, 8x, 12x, 16x, 20x, 24x
  1755. // (Static option only for now)
  1756. static const float aa_level = 12.0; // range [0, 24]
  1757. // What antialiasing filter do you want (static option only)? Options:
  1758. // 0: Box (separable), 1: Box (cylindrical),
  1759. // 2: Tent (separable), 3: Tent (cylindrical),
  1760. // 4: Gaussian (separable), 5: Gaussian (cylindrical),
  1761. // 6: Cubic* (separable), 7: Cubic* (cylindrical, poor)
  1762. // 8: Lanczos Sinc (separable), 9: Lanczos Jinc (cylindrical, poor)
  1763. // * = Especially slow with RUNTIME_ANTIALIAS_WEIGHTS
  1764. static const float aa_filter = 6.0; // range [0, 9]
  1765. // Flip the sample grid on odd/even frames (static option only for now)?
  1766. static const bool aa_temporal = false;
  1767. // Use RGB subpixel offsets for antialiasing? The pixel is at green, and
  1768. // the blue offset is the negative r offset; range [0, 0.5]
  1769. static const float2 aa_subpixel_r_offset_static = float2(-1.0/3.0, 0.0);//float2(0.0);
  1770. // Cubics: See http://www.imagemagick.org/Usage/filter/#mitchell
  1771. // 1.) "Keys cubics" with B = 1 - 2C are considered the highest quality.
  1772. // 2.) C = 0.5 (default) is Catmull-Rom; higher C's apply sharpening.
  1773. // 3.) C = 1.0/3.0 is the Mitchell-Netravali filter.
  1774. // 4.) C = 0.0 is a soft spline filter.
  1775. static const float aa_cubic_c_static = 0.5; // range [0, 4]
  1776. // Standard deviation for Gaussian antialiasing: Try 0.5/aa_pixel_diameter.
  1777. static const float aa_gauss_sigma_static = 0.5; // range [0.0625, 1.0]
  1778. // PHOSPHOR MASK:
  1779. // Mask type: 0 = aperture grille, 1 = slot mask, 2 = EDP shadow mask
  1780. static const float mask_type_static = 1.0; // range [0, 2]
  1781. // We can sample the mask three ways. Pick 2/3 from: Pretty/Fast/Flexible.
  1782. // 0.) Sinc-resize to the desired dot pitch manually (pretty/slow/flexible).
  1783. // This requires PHOSPHOR_MASK_MANUALLY_RESIZE to be #defined.
  1784. // 1.) Hardware-resize to the desired dot pitch (ugly/fast/flexible). This
  1785. // is halfway decent with LUT mipmapping but atrocious without it.
  1786. // 2.) Tile it without resizing at a 1:1 texel:pixel ratio for flat coords
  1787. // (pretty/fast/inflexible). Each input LUT has a fixed dot pitch.
  1788. // This mode reuses the same masks, so triads will be enormous unless
  1789. // you change the mask LUT filenames in your .cgp file.
  1790. static const float mask_sample_mode_static = 0.0; // range [0, 2]
  1791. // Prefer setting the triad size (0.0) or number on the screen (1.0)?
  1792. // If RUNTIME_PHOSPHOR_BLOOM_SIGMA isn't #defined, the specified triad size
  1793. // will always be used to calculate the full bloom sigma statically.
  1794. static const float mask_specify_num_triads_static = 0.0; // range [0, 1]
  1795. // Specify the phosphor triad size, in pixels. Each tile (usually with 8
  1796. // triads) will be rounded to the nearest integer tile size and clamped to
  1797. // obey minimum size constraints (imposed to reduce downsize taps) and
  1798. // maximum size constraints (imposed to have a sane MASK_RESIZE FBO size).
  1799. // To increase the size limit, double the viewport-relative scales for the
  1800. // two MASK_RESIZE passes in crt-royale.cgp and user-cgp-contants.h.
  1801. // range [1, mask_texture_small_size/mask_triads_per_tile]
  1802. static const float mask_triad_size_desired_static = 24.0 / 8.0;
  1803. // If mask_specify_num_triads is 1.0/true, we'll go by this instead (the
  1804. // final size will be rounded and constrained as above); default 480.0
  1805. static const float mask_num_triads_desired_static = 480.0;
  1806. // How many lobes should the sinc/Lanczos resizer use? More lobes require
  1807. // more samples and avoid moire a bit better, but some is unavoidable
  1808. // depending on the destination size (static option for now).
  1809. static const float mask_sinc_lobes = 3.0; // range [2, 4]
  1810. // The mask is resized using a variable number of taps in each dimension,
  1811. // but some Cg profiles always fetch a constant number of taps no matter
  1812. // what (no dynamic branching). We can limit the maximum number of taps if
  1813. // we statically limit the minimum phosphor triad size. Larger values are
  1814. // faster, but the limit IS enforced (static option only, forever);
  1815. // range [1, mask_texture_small_size/mask_triads_per_tile]
  1816. // TODO: Make this 1.0 and compensate with smarter sampling!
  1817. static const float mask_min_allowed_triad_size = 2.0;
  1818. // GEOMETRY:
  1819. // Geometry mode:
  1820. // 0: Off (default), 1: Spherical mapping (like cgwg's),
  1821. // 2: Alt. spherical mapping (more bulbous), 3: Cylindrical/Trinitron
  1822. static const float geom_mode_static = 0.0; // range [0, 3]
  1823. // Radius of curvature: Measured in units of your viewport's diagonal size.
  1824. static const float geom_radius_static = 2.0; // range [1/(2*pi), 1024]
  1825. // View dist is the distance from the player to their physical screen, in
  1826. // units of the viewport's diagonal size. It controls the field of view.
  1827. static const float geom_view_dist_static = 2.0; // range [0.5, 1024]
  1828. // Tilt angle in radians (clockwise around up and right vectors):
  1829. static const float2 geom_tilt_angle_static = float2(0.0, 0.0); // range [-pi, pi]
  1830. // Aspect ratio: When the true viewport size is unknown, this value is used
  1831. // to help convert between the phosphor triad size and count, along with
  1832. // the mask_resize_viewport_scale constant from user-cgp-constants.h. Set
  1833. // this equal to Retroarch's display aspect ratio (DAR) for best results;
  1834. // range [1, geom_max_aspect_ratio from user-cgp-constants.h];
  1835. // default (256/224)*(54/47) = 1.313069909 (see below)
  1836. static const float geom_aspect_ratio_static = 1.313069909;
  1837. // Before getting into overscan, here's some general aspect ratio info:
  1838. // - DAR = display aspect ratio = SAR * PAR; as in your Retroarch setting
  1839. // - SAR = storage aspect ratio = DAR / PAR; square pixel emulator frame AR
  1840. // - PAR = pixel aspect ratio = DAR / SAR; holds regardless of cropping
  1841. // Geometry processing has to "undo" the screen-space 2D DAR to calculate
  1842. // 3D view vectors, then reapplies the aspect ratio to the simulated CRT in
  1843. // uv-space. To ensure the source SAR is intended for a ~4:3 DAR, either:
  1844. // a.) Enable Retroarch's "Crop Overscan"
  1845. // b.) Readd horizontal padding: Set overscan to e.g. N*(1.0, 240.0/224.0)
  1846. // Real consoles use horizontal black padding in the signal, but emulators
  1847. // often crop this without cropping the vertical padding; a 256x224 [S]NES
  1848. // frame (8:7 SAR) is intended for a ~4:3 DAR, but a 256x240 frame is not.
  1849. // The correct [S]NES PAR is 54:47, found by blargg and NewRisingSun:
  1850. // http://board.zsnes.com/phpBB3/viewtopic.php?f=22&t=11928&start=50
  1851. // http://forums.nesdev.com/viewtopic.php?p=24815#p24815
  1852. // For flat output, it's okay to set DAR = [existing] SAR * [correct] PAR
  1853. // without doing a. or b., but horizontal image borders will be tighter
  1854. // than vertical ones, messing up curvature and overscan. Fixing the
  1855. // padding first corrects this.
  1856. // Overscan: Amount to "zoom in" before cropping. You can zoom uniformly
  1857. // or adjust x/y independently to e.g. readd horizontal padding, as noted
  1858. // above: Values < 1.0 zoom out; range (0, inf)
  1859. static const float2 geom_overscan_static = float2(1.0, 1.0);// * 1.005 * (1.0, 240/224.0)
  1860. // Compute a proper pixel-space to texture-space matrix even without ddx()/
  1861. // ddy()? This is ~8.5% slower but improves antialiasing/subpixel filtering
  1862. // with strong curvature (static option only for now).
  1863. static const bool geom_force_correct_tangent_matrix = true;
  1864. // BORDERS:
  1865. // Rounded border size in texture uv coords:
  1866. static const float border_size_static = 0.015; // range [0, 0.5]
  1867. // Border darkness: Moderate values darken the border smoothly, and high
  1868. // values make the image very dark just inside the border:
  1869. static const float border_darkness_static = 2.0; // range [0, inf)
  1870. // Border compression: High numbers compress border transitions, narrowing
  1871. // the dark border area.
  1872. static const float border_compress_static = 2.5; // range [1, inf)
  1873. #endif // USER_SETTINGS_H
  1874. ///////////////////////////// END USER-SETTINGS ////////////////////////////
  1875. //#include "user-cgp-constants.h"
  1876. ///////////////////////// BEGIN USER-CGP-CONSTANTS /////////////////////////
  1877. #ifndef USER_CGP_CONSTANTS_H
  1878. #define USER_CGP_CONSTANTS_H
  1879. // IMPORTANT:
  1880. // These constants MUST be set appropriately for the settings in crt-royale.cgp
  1881. // (or whatever related .cgp file you're using). If they aren't, you're likely
  1882. // to get artifacts, the wrong phosphor mask size, etc. I wish these could be
  1883. // set directly in the .cgp file to make things easier, but...they can't.
  1884. // PASS SCALES AND RELATED CONSTANTS:
  1885. // Copy the absolute scale_x for BLOOM_APPROX. There are two major versions of
  1886. // this shader: One does a viewport-scale bloom, and the other skips it. The
  1887. // latter benefits from a higher bloom_approx_scale_x, so save both separately:
  1888. static const float bloom_approx_size_x = 320.0;
  1889. static const float bloom_approx_size_x_for_fake = 400.0;
  1890. // Copy the viewport-relative scales of the phosphor mask resize passes
  1891. // (MASK_RESIZE and the pass immediately preceding it):
  1892. static const float2 mask_resize_viewport_scale = float2(0.0625, 0.0625);
  1893. // Copy the geom_max_aspect_ratio used to calculate the MASK_RESIZE scales, etc.:
  1894. static const float geom_max_aspect_ratio = 4.0/3.0;
  1895. // PHOSPHOR MASK TEXTURE CONSTANTS:
  1896. // Set the following constants to reflect the properties of the phosphor mask
  1897. // texture named in crt-royale.cgp. The shader optionally resizes a mask tile
  1898. // based on user settings, then repeats a single tile until filling the screen.
  1899. // The shader must know the input texture size (default 64x64), and to manually
  1900. // resize, it must also know the horizontal triads per tile (default 8).
  1901. static const float2 mask_texture_small_size = float2(64.0, 64.0);
  1902. static const float2 mask_texture_large_size = float2(512.0, 512.0);
  1903. static const float mask_triads_per_tile = 8.0;
  1904. // We need the average brightness of the phosphor mask to compensate for the
  1905. // dimming it causes. The following four values are roughly correct for the
  1906. // masks included with the shader. Update the value for any LUT texture you
  1907. // change. [Un]comment "#define PHOSPHOR_MASK_GRILLE14" depending on whether
  1908. // the loaded aperture grille uses 14-pixel or 15-pixel stripes (default 15).
  1909. //#define PHOSPHOR_MASK_GRILLE14
  1910. static const float mask_grille14_avg_color = 50.6666666/255.0;
  1911. // TileableLinearApertureGrille14Wide7d33Spacing*.png
  1912. // TileableLinearApertureGrille14Wide10And6Spacing*.png
  1913. static const float mask_grille15_avg_color = 53.0/255.0;
  1914. // TileableLinearApertureGrille15Wide6d33Spacing*.png
  1915. // TileableLinearApertureGrille15Wide8And5d5Spacing*.png
  1916. static const float mask_slot_avg_color = 46.0/255.0;
  1917. // TileableLinearSlotMask15Wide9And4d5Horizontal8VerticalSpacing*.png
  1918. // TileableLinearSlotMaskTall15Wide9And4d5Horizontal9d14VerticalSpacing*.png
  1919. static const float mask_shadow_avg_color = 41.0/255.0;
  1920. // TileableLinearShadowMask*.png
  1921. // TileableLinearShadowMaskEDP*.png
  1922. #ifdef PHOSPHOR_MASK_GRILLE14
  1923. static const float mask_grille_avg_color = mask_grille14_avg_color;
  1924. #else
  1925. static const float mask_grille_avg_color = mask_grille15_avg_color;
  1926. #endif
  1927. #endif // USER_CGP_CONSTANTS_H
  1928. ////////////////////////// END USER-CGP-CONSTANTS //////////////////////////
  1929. //////////////////////////////// END INCLUDES ////////////////////////////////
  1930. /////////////////////////////// FIXED SETTINGS ///////////////////////////////
  1931. // Avoid dividing by zero; using a macro overloads for float, float2, etc.:
  1932. #define FIX_ZERO(c) (max(abs(c), 0.0000152587890625)) // 2^-16
  1933. // Ensure the first pass decodes CRT gamma and the last encodes LCD gamma.
  1934. #ifndef SIMULATE_CRT_ON_LCD
  1935. #define SIMULATE_CRT_ON_LCD
  1936. #endif
  1937. // Manually tiling a manually resized texture creates texture coord derivative
  1938. // discontinuities and confuses anisotropic filtering, causing discolored tile
  1939. // seams in the phosphor mask. Workarounds:
  1940. // a.) Using tex2Dlod disables anisotropic filtering for tiled masks. It's
  1941. // downgraded to tex2Dbias without DRIVERS_ALLOW_TEX2DLOD #defined and
  1942. // disabled without DRIVERS_ALLOW_TEX2DBIAS #defined either.
  1943. // b.) "Tile flat twice" requires drawing two full tiles without border padding
  1944. // to the resized mask FBO, and it's incompatible with same-pass curvature.
  1945. // (Same-pass curvature isn't used but could be in the future...maybe.)
  1946. // c.) "Fix discontinuities" requires derivatives and drawing one tile with
  1947. // border padding to the resized mask FBO, but it works with same-pass
  1948. // curvature. It's disabled without DRIVERS_ALLOW_DERIVATIVES #defined.
  1949. // Precedence: a, then, b, then c (if multiple strategies are #defined).
  1950. #define ANISOTROPIC_TILING_COMPAT_TEX2DLOD // 129.7 FPS, 4x, flat; 101.8 at fullscreen
  1951. #define ANISOTROPIC_TILING_COMPAT_TILE_FLAT_TWICE // 128.1 FPS, 4x, flat; 101.5 at fullscreen
  1952. #define ANISOTROPIC_TILING_COMPAT_FIX_DISCONTINUITIES // 124.4 FPS, 4x, flat; 97.4 at fullscreen
  1953. // Also, manually resampling the phosphor mask is slightly blurrier with
  1954. // anisotropic filtering. (Resampling with mipmapping is even worse: It
  1955. // creates artifacts, but only with the fully bloomed shader.) The difference
  1956. // is subtle with small triads, but you can fix it for a small cost.
  1957. //#define ANISOTROPIC_RESAMPLING_COMPAT_TEX2DLOD
  1958. ////////////////////////////// DERIVED SETTINGS //////////////////////////////
  1959. // Intel HD 4000 GPU's can't handle manual mask resizing (for now), setting the
  1960. // geometry mode at runtime, or a 4x4 true Gaussian resize. Disable
  1961. // incompatible settings ASAP. (INTEGRATED_GRAPHICS_COMPATIBILITY_MODE may be
  1962. // #defined by either user-settings.h or a wrapper .cg that #includes the
  1963. // current .cg pass.)
  1964. #ifdef INTEGRATED_GRAPHICS_COMPATIBILITY_MODE
  1965. #ifdef PHOSPHOR_MASK_MANUALLY_RESIZE
  1966. #undef PHOSPHOR_MASK_MANUALLY_RESIZE
  1967. #endif
  1968. #ifdef RUNTIME_GEOMETRY_MODE
  1969. #undef RUNTIME_GEOMETRY_MODE
  1970. #endif
  1971. // Mode 2 (4x4 Gaussian resize) won't work, and mode 1 (3x3 blur) is
  1972. // inferior in most cases, so replace 2.0 with 0.0:
  1973. static const float bloom_approx_filter =
  1974. bloom_approx_filter_static > 1.5 ? 0.0 : bloom_approx_filter_static;
  1975. #else
  1976. static const float bloom_approx_filter = bloom_approx_filter_static;
  1977. #endif
  1978. // Disable slow runtime paths if static parameters are used. Most of these
  1979. // won't be a problem anyway once the params are disabled, but some will.
  1980. #ifndef RUNTIME_SHADER_PARAMS_ENABLE
  1981. #ifdef RUNTIME_PHOSPHOR_BLOOM_SIGMA
  1982. #undef RUNTIME_PHOSPHOR_BLOOM_SIGMA
  1983. #endif
  1984. #ifdef RUNTIME_ANTIALIAS_WEIGHTS
  1985. #undef RUNTIME_ANTIALIAS_WEIGHTS
  1986. #endif
  1987. #ifdef RUNTIME_ANTIALIAS_SUBPIXEL_OFFSETS
  1988. #undef RUNTIME_ANTIALIAS_SUBPIXEL_OFFSETS
  1989. #endif
  1990. #ifdef RUNTIME_SCANLINES_HORIZ_FILTER_COLORSPACE
  1991. #undef RUNTIME_SCANLINES_HORIZ_FILTER_COLORSPACE
  1992. #endif
  1993. #ifdef RUNTIME_GEOMETRY_TILT
  1994. #undef RUNTIME_GEOMETRY_TILT
  1995. #endif
  1996. #ifdef RUNTIME_GEOMETRY_MODE
  1997. #undef RUNTIME_GEOMETRY_MODE
  1998. #endif
  1999. #ifdef FORCE_RUNTIME_PHOSPHOR_MASK_MODE_TYPE_SELECT
  2000. #undef FORCE_RUNTIME_PHOSPHOR_MASK_MODE_TYPE_SELECT
  2001. #endif
  2002. #endif
  2003. // Make tex2Dbias a backup for tex2Dlod for wider compatibility.
  2004. #ifdef ANISOTROPIC_TILING_COMPAT_TEX2DLOD
  2005. #define ANISOTROPIC_TILING_COMPAT_TEX2DBIAS
  2006. #endif
  2007. #ifdef ANISOTROPIC_RESAMPLING_COMPAT_TEX2DLOD
  2008. #define ANISOTROPIC_RESAMPLING_COMPAT_TEX2DBIAS
  2009. #endif
  2010. // Rule out unavailable anisotropic compatibility strategies:
  2011. #ifndef DRIVERS_ALLOW_DERIVATIVES
  2012. #ifdef ANISOTROPIC_TILING_COMPAT_FIX_DISCONTINUITIES
  2013. #undef ANISOTROPIC_TILING_COMPAT_FIX_DISCONTINUITIES
  2014. #endif
  2015. #endif
  2016. #ifndef DRIVERS_ALLOW_TEX2DLOD
  2017. #ifdef ANISOTROPIC_TILING_COMPAT_TEX2DLOD
  2018. #undef ANISOTROPIC_TILING_COMPAT_TEX2DLOD
  2019. #endif
  2020. #ifdef ANISOTROPIC_RESAMPLING_COMPAT_TEX2DLOD
  2021. #undef ANISOTROPIC_RESAMPLING_COMPAT_TEX2DLOD
  2022. #endif
  2023. #ifdef ANTIALIAS_DISABLE_ANISOTROPIC
  2024. #undef ANTIALIAS_DISABLE_ANISOTROPIC
  2025. #endif
  2026. #endif
  2027. #ifndef DRIVERS_ALLOW_TEX2DBIAS
  2028. #ifdef ANISOTROPIC_TILING_COMPAT_TEX2DBIAS
  2029. #undef ANISOTROPIC_TILING_COMPAT_TEX2DBIAS
  2030. #endif
  2031. #ifdef ANISOTROPIC_RESAMPLING_COMPAT_TEX2DBIAS
  2032. #undef ANISOTROPIC_RESAMPLING_COMPAT_TEX2DBIAS
  2033. #endif
  2034. #endif
  2035. // Prioritize anisotropic tiling compatibility strategies by performance and
  2036. // disable unused strategies. This concentrates all the nesting in one place.
  2037. #ifdef ANISOTROPIC_TILING_COMPAT_TEX2DLOD
  2038. #ifdef ANISOTROPIC_TILING_COMPAT_TEX2DBIAS
  2039. #undef ANISOTROPIC_TILING_COMPAT_TEX2DBIAS
  2040. #endif
  2041. #ifdef ANISOTROPIC_TILING_COMPAT_TILE_FLAT_TWICE
  2042. #undef ANISOTROPIC_TILING_COMPAT_TILE_FLAT_TWICE
  2043. #endif
  2044. #ifdef ANISOTROPIC_TILING_COMPAT_FIX_DISCONTINUITIES
  2045. #undef ANISOTROPIC_TILING_COMPAT_FIX_DISCONTINUITIES
  2046. #endif
  2047. #else
  2048. #ifdef ANISOTROPIC_TILING_COMPAT_TEX2DBIAS
  2049. #ifdef ANISOTROPIC_TILING_COMPAT_TILE_FLAT_TWICE
  2050. #undef ANISOTROPIC_TILING_COMPAT_TILE_FLAT_TWICE
  2051. #endif
  2052. #ifdef ANISOTROPIC_TILING_COMPAT_FIX_DISCONTINUITIES
  2053. #undef ANISOTROPIC_TILING_COMPAT_FIX_DISCONTINUITIES
  2054. #endif
  2055. #else
  2056. // ANISOTROPIC_TILING_COMPAT_TILE_FLAT_TWICE is only compatible with
  2057. // flat texture coords in the same pass, but that's all we use.
  2058. #ifdef ANISOTROPIC_TILING_COMPAT_TILE_FLAT_TWICE
  2059. #ifdef ANISOTROPIC_TILING_COMPAT_FIX_DISCONTINUITIES
  2060. #undef ANISOTROPIC_TILING_COMPAT_FIX_DISCONTINUITIES
  2061. #endif
  2062. #endif
  2063. #endif
  2064. #endif
  2065. // The tex2Dlod and tex2Dbias strategies share a lot in common, and we can
  2066. // reduce some #ifdef nesting in the next section by essentially OR'ing them:
  2067. #ifdef ANISOTROPIC_TILING_COMPAT_TEX2DLOD
  2068. #define ANISOTROPIC_TILING_COMPAT_TEX2DLOD_FAMILY
  2069. #endif
  2070. #ifdef ANISOTROPIC_TILING_COMPAT_TEX2DBIAS
  2071. #define ANISOTROPIC_TILING_COMPAT_TEX2DLOD_FAMILY
  2072. #endif
  2073. // Prioritize anisotropic resampling compatibility strategies the same way:
  2074. #ifdef ANISOTROPIC_RESAMPLING_COMPAT_TEX2DLOD
  2075. #ifdef ANISOTROPIC_RESAMPLING_COMPAT_TEX2DBIAS
  2076. #undef ANISOTROPIC_RESAMPLING_COMPAT_TEX2DBIAS
  2077. #endif
  2078. #endif
  2079. /////////////////////// DERIVED PHOSPHOR MASK CONSTANTS //////////////////////
  2080. // If we can use the large mipmapped LUT without mipmapping artifacts, we
  2081. // should: It gives us more options for using fewer samples.
  2082. #ifdef DRIVERS_ALLOW_TEX2DLOD
  2083. #ifdef ANISOTROPIC_RESAMPLING_COMPAT_TEX2DLOD
  2084. // TODO: Take advantage of this!
  2085. #define PHOSPHOR_MASK_RESIZE_MIPMAPPED_LUT
  2086. static const float2 mask_resize_src_lut_size = mask_texture_large_size;
  2087. #else
  2088. static const float2 mask_resize_src_lut_size = mask_texture_small_size;
  2089. #endif
  2090. #else
  2091. static const float2 mask_resize_src_lut_size = mask_texture_small_size;
  2092. #endif
  2093. // tex2D's sampler2D parameter MUST be a uniform global, a uniform input to
  2094. // main_fragment, or a static alias of one of the above. This makes it hard
  2095. // to select the phosphor mask at runtime: We can't even assign to a uniform
  2096. // global in the vertex shader or select a sampler2D in the vertex shader and
  2097. // pass it to the fragment shader (even with explicit TEXUNIT# bindings),
  2098. // because it just gives us the input texture or a black screen. However, we
  2099. // can get around these limitations by calling tex2D three times with different
  2100. // uniform samplers (or resizing the phosphor mask three times altogether).
  2101. // With dynamic branches, we can process only one of these branches on top of
  2102. // quickly discarding fragments we don't need (cgc seems able to overcome
  2103. // limigations around dependent texture fetches inside of branches). Without
  2104. // dynamic branches, we have to process every branch for every fragment...which
  2105. // is slower. Runtime sampling mode selection is slower without dynamic
  2106. // branches as well. Let the user's static #defines decide if it's worth it.
  2107. #ifdef DRIVERS_ALLOW_DYNAMIC_BRANCHES
  2108. #define RUNTIME_PHOSPHOR_MASK_MODE_TYPE_SELECT
  2109. #else
  2110. #ifdef FORCE_RUNTIME_PHOSPHOR_MASK_MODE_TYPE_SELECT
  2111. #define RUNTIME_PHOSPHOR_MASK_MODE_TYPE_SELECT
  2112. #endif
  2113. #endif
  2114. // We need to render some minimum number of tiles in the resize passes.
  2115. // We need at least 1.0 just to repeat a single tile, and we need extra
  2116. // padding beyond that for anisotropic filtering, discontinuitity fixing,
  2117. // antialiasing, same-pass curvature (not currently used), etc. First
  2118. // determine how many border texels and tiles we need, based on how the result
  2119. // will be sampled:
  2120. #ifdef GEOMETRY_EARLY
  2121. static const float max_subpixel_offset = aa_subpixel_r_offset_static.x;
  2122. // Most antialiasing filters have a base radius of 4.0 pixels:
  2123. static const float max_aa_base_pixel_border = 4.0 +
  2124. max_subpixel_offset;
  2125. #else
  2126. static const float max_aa_base_pixel_border = 0.0;
  2127. #endif
  2128. // Anisotropic filtering adds about 0.5 to the pixel border:
  2129. #ifndef ANISOTROPIC_TILING_COMPAT_TEX2DLOD_FAMILY
  2130. static const float max_aniso_pixel_border = max_aa_base_pixel_border + 0.5;
  2131. #else
  2132. static const float max_aniso_pixel_border = max_aa_base_pixel_border;
  2133. #endif
  2134. // Fixing discontinuities adds 1.0 more to the pixel border:
  2135. #ifdef ANISOTROPIC_TILING_COMPAT_FIX_DISCONTINUITIES
  2136. static const float max_tiled_pixel_border = max_aniso_pixel_border + 1.0;
  2137. #else
  2138. static const float max_tiled_pixel_border = max_aniso_pixel_border;
  2139. #endif
  2140. // Convert the pixel border to an integer texel border. Assume same-pass
  2141. // curvature about triples the texel frequency:
  2142. #ifdef GEOMETRY_EARLY
  2143. static const float max_mask_texel_border =
  2144. ceil(max_tiled_pixel_border * 3.0);
  2145. #else
  2146. static const float max_mask_texel_border = ceil(max_tiled_pixel_border);
  2147. #endif
  2148. // Convert the texel border to a tile border using worst-case assumptions:
  2149. static const float max_mask_tile_border = max_mask_texel_border/
  2150. (mask_min_allowed_triad_size * mask_triads_per_tile);
  2151. // Finally, set the number of resized tiles to render to MASK_RESIZE, and set
  2152. // the starting texel (inside borders) for sampling it.
  2153. #ifndef GEOMETRY_EARLY
  2154. #ifdef ANISOTROPIC_TILING_COMPAT_TILE_FLAT_TWICE
  2155. // Special case: Render two tiles without borders. Anisotropic
  2156. // filtering doesn't seem to be a problem here.
  2157. static const float mask_resize_num_tiles = 1.0 + 1.0;
  2158. static const float mask_start_texels = 0.0;
  2159. #else
  2160. static const float mask_resize_num_tiles = 1.0 +
  2161. 2.0 * max_mask_tile_border;
  2162. static const float mask_start_texels = max_mask_texel_border;
  2163. #endif
  2164. #else
  2165. static const float mask_resize_num_tiles = 1.0 + 2.0*max_mask_tile_border;
  2166. static const float mask_start_texels = max_mask_texel_border;
  2167. #endif
  2168. // We have to fit mask_resize_num_tiles into an FBO with a viewport scale of
  2169. // mask_resize_viewport_scale. This limits the maximum final triad size.
  2170. // Estimate the minimum number of triads we can split the screen into in each
  2171. // dimension (we'll be as correct as mask_resize_viewport_scale is):
  2172. static const float mask_resize_num_triads =
  2173. mask_resize_num_tiles * mask_triads_per_tile;
  2174. static const float2 min_allowed_viewport_triads =
  2175. float2(mask_resize_num_triads) / mask_resize_viewport_scale;
  2176. //////////////////////// COMMON MATHEMATICAL CONSTANTS ///////////////////////
  2177. static const float pi = 3.141592653589;
  2178. // We often want to find the location of the previous texel, e.g.:
  2179. // const float2 curr_texel = uv * texture_size;
  2180. // const float2 prev_texel = floor(curr_texel - float2(0.5)) + float2(0.5);
  2181. // const float2 prev_texel_uv = prev_texel / texture_size;
  2182. // However, many GPU drivers round incorrectly around exact texel locations.
  2183. // We need to subtract a little less than 0.5 before flooring, and some GPU's
  2184. // require this value to be farther from 0.5 than others; define it here.
  2185. // const float2 prev_texel =
  2186. // floor(curr_texel - float2(under_half)) + float2(0.5);
  2187. static const float under_half = 0.4995;
  2188. #endif // DERIVED_SETTINGS_AND_CONSTANTS_H
  2189. //////////////////// END DERIVED-SETTINGS-AND-CONSTANTS /////////////////////
  2190. //////////////////////////////// END INCLUDES ////////////////////////////////
  2191. // Override some parameters for gamma-management.h and tex2Dantialias.h:
  2192. #define OVERRIDE_DEVICE_GAMMA
  2193. static const float gba_gamma = 3.5; // Irrelevant but necessary to define.
  2194. #define ANTIALIAS_OVERRIDE_BASICS
  2195. #define ANTIALIAS_OVERRIDE_PARAMETERS
  2196. // Provide accessors for vector constants that pack scalar uniforms:
  2197. inline float2 get_aspect_vector(const float geom_aspect_ratio)
  2198. {
  2199. // Get an aspect ratio vector. Enforce geom_max_aspect_ratio, and prevent
  2200. // the absolute scale from affecting the uv-mapping for curvature:
  2201. const float geom_clamped_aspect_ratio =
  2202. min(geom_aspect_ratio, geom_max_aspect_ratio);
  2203. const float2 geom_aspect =
  2204. normalize(float2(geom_clamped_aspect_ratio, 1.0));
  2205. return geom_aspect;
  2206. }
  2207. inline float2 get_geom_overscan_vector()
  2208. {
  2209. return float2(geom_overscan_x, geom_overscan_y);
  2210. }
  2211. inline float2 get_geom_tilt_angle_vector()
  2212. {
  2213. return float2(geom_tilt_angle_x, geom_tilt_angle_y);
  2214. }
  2215. inline float3 get_convergence_offsets_x_vector()
  2216. {
  2217. return float3(convergence_offset_x_r, convergence_offset_x_g,
  2218. convergence_offset_x_b);
  2219. }
  2220. inline float3 get_convergence_offsets_y_vector()
  2221. {
  2222. return float3(convergence_offset_y_r, convergence_offset_y_g,
  2223. convergence_offset_y_b);
  2224. }
  2225. inline float2 get_convergence_offsets_r_vector()
  2226. {
  2227. return float2(convergence_offset_x_r, convergence_offset_y_r);
  2228. }
  2229. inline float2 get_convergence_offsets_g_vector()
  2230. {
  2231. return float2(convergence_offset_x_g, convergence_offset_y_g);
  2232. }
  2233. inline float2 get_convergence_offsets_b_vector()
  2234. {
  2235. return float2(convergence_offset_x_b, convergence_offset_y_b);
  2236. }
  2237. inline float2 get_aa_subpixel_r_offset()
  2238. {
  2239. #ifdef RUNTIME_ANTIALIAS_WEIGHTS
  2240. #ifdef RUNTIME_ANTIALIAS_SUBPIXEL_OFFSETS
  2241. // WARNING: THIS IS EXTREMELY EXPENSIVE.
  2242. return float2(aa_subpixel_r_offset_x_runtime,
  2243. aa_subpixel_r_offset_y_runtime);
  2244. #else
  2245. return aa_subpixel_r_offset_static;
  2246. #endif
  2247. #else
  2248. return aa_subpixel_r_offset_static;
  2249. #endif
  2250. }
  2251. // Provide accessors settings which still need "cooking:"
  2252. inline float get_mask_amplify()
  2253. {
  2254. static const float mask_grille_amplify = 1.0/mask_grille_avg_color;
  2255. static const float mask_slot_amplify = 1.0/mask_slot_avg_color;
  2256. static const float mask_shadow_amplify = 1.0/mask_shadow_avg_color;
  2257. return mask_type < 0.5 ? mask_grille_amplify :
  2258. mask_type < 1.5 ? mask_slot_amplify :
  2259. mask_shadow_amplify;
  2260. }
  2261. inline float get_mask_sample_mode()
  2262. {
  2263. #ifdef RUNTIME_PHOSPHOR_MASK_MODE_TYPE_SELECT
  2264. #ifdef PHOSPHOR_MASK_MANUALLY_RESIZE
  2265. return mask_sample_mode_desired;
  2266. #else
  2267. return clamp(mask_sample_mode_desired, 1.0, 2.0);
  2268. #endif
  2269. #else
  2270. #ifdef PHOSPHOR_MASK_MANUALLY_RESIZE
  2271. return mask_sample_mode_static;
  2272. #else
  2273. return clamp(mask_sample_mode_static, 1.0, 2.0);
  2274. #endif
  2275. #endif
  2276. }
  2277. #endif // BIND_SHADER_PARAMS_H
  2278. //////////////////////////// END BIND-SHADER-PARAMS ///////////////////////////
  2279. /////////////////////////////// VERTEX INCLUDES ///////////////////////////////
  2280. //#include "scanline-functions.h"
  2281. ///////////////////////////// BEGIN SCANLINE-FUNCTIONS ////////////////////////////
  2282. #ifndef SCANLINE_FUNCTIONS_H
  2283. #define SCANLINE_FUNCTIONS_H
  2284. ///////////////////////////// GPL LICENSE NOTICE /////////////////////////////
  2285. // crt-royale: A full-featured CRT shader, with cheese.
  2286. // Copyright (C) 2014 TroggleMonkey <trogglemonkey@gmx.com>
  2287. //
  2288. // This program is free software; you can redistribute it and/or modify it
  2289. // under the terms of the GNU General Public License as published by the Free
  2290. // Software Foundation; either version 2 of the License, or any later version.
  2291. //
  2292. // This program is distributed in the hope that it will be useful, but WITHOUT
  2293. // ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
  2294. // FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
  2295. // more details.
  2296. //
  2297. // You should have received a copy of the GNU General Public License along with
  2298. // this program; if not, write to the Free Software Foundation, Inc., 59 Temple
  2299. // Place, Suite 330, Boston, MA 02111-1307 USA
  2300. /////////////////////////////// BEGIN INCLUDES ///////////////////////////////
  2301. //#include "../user-settings.h"
  2302. ///////////////////////////// BEGIN USER-SETTINGS ////////////////////////////
  2303. #ifndef USER_SETTINGS_H
  2304. #define USER_SETTINGS_H
  2305. ///////////////////////////// DRIVER CAPABILITIES ////////////////////////////
  2306. // The Cg compiler uses different "profiles" with different capabilities.
  2307. // This shader requires a Cg compilation profile >= arbfp1, but a few options
  2308. // require higher profiles like fp30 or fp40. The shader can't detect profile
  2309. // or driver capabilities, so instead you must comment or uncomment the lines
  2310. // below with "//" before "#define." Disable an option if you get compilation
  2311. // errors resembling those listed. Generally speaking, all of these options
  2312. // will run on nVidia cards, but only DRIVERS_ALLOW_TEX2DBIAS (if that) is
  2313. // likely to run on ATI/AMD, due to the Cg compiler's profile limitations.
  2314. // Derivatives: Unsupported on fp20, ps_1_1, ps_1_2, ps_1_3, and arbfp1.
  2315. // Among other things, derivatives help us fix anisotropic filtering artifacts
  2316. // with curved manually tiled phosphor mask coords. Related errors:
  2317. // error C3004: function "float2 ddx(float2);" not supported in this profile
  2318. // error C3004: function "float2 ddy(float2);" not supported in this profile
  2319. //#define DRIVERS_ALLOW_DERIVATIVES
  2320. // Fine derivatives: Unsupported on older ATI cards.
  2321. // Fine derivatives enable 2x2 fragment block communication, letting us perform
  2322. // fast single-pass blur operations. If your card uses coarse derivatives and
  2323. // these are enabled, blurs could look broken. Derivatives are a prerequisite.
  2324. #ifdef DRIVERS_ALLOW_DERIVATIVES
  2325. #define DRIVERS_ALLOW_FINE_DERIVATIVES
  2326. #endif
  2327. // Dynamic looping: Requires an fp30 or newer profile.
  2328. // This makes phosphor mask resampling faster in some cases. Related errors:
  2329. // error C5013: profile does not support "for" statements and "for" could not
  2330. // be unrolled
  2331. //#define DRIVERS_ALLOW_DYNAMIC_BRANCHES
  2332. // Without DRIVERS_ALLOW_DYNAMIC_BRANCHES, we need to use unrollable loops.
  2333. // Using one static loop avoids overhead if the user is right, but if the user
  2334. // is wrong (loops are allowed), breaking a loop into if-blocked pieces with a
  2335. // binary search can potentially save some iterations. However, it may fail:
  2336. // error C6001: Temporary register limit of 32 exceeded; 35 registers
  2337. // needed to compile program
  2338. //#define ACCOMODATE_POSSIBLE_DYNAMIC_LOOPS
  2339. // tex2Dlod: Requires an fp40 or newer profile. This can be used to disable
  2340. // anisotropic filtering, thereby fixing related artifacts. Related errors:
  2341. // error C3004: function "float4 tex2Dlod(sampler2D, float4);" not supported in
  2342. // this profile
  2343. //#define DRIVERS_ALLOW_TEX2DLOD
  2344. // tex2Dbias: Requires an fp30 or newer profile. This can be used to alleviate
  2345. // artifacts from anisotropic filtering and mipmapping. Related errors:
  2346. // error C3004: function "float4 tex2Dbias(sampler2D, float4);" not supported
  2347. // in this profile
  2348. //#define DRIVERS_ALLOW_TEX2DBIAS
  2349. // Integrated graphics compatibility: Integrated graphics like Intel HD 4000
  2350. // impose stricter limitations on register counts and instructions. Enable
  2351. // INTEGRATED_GRAPHICS_COMPATIBILITY_MODE if you still see error C6001 or:
  2352. // error C6002: Instruction limit of 1024 exceeded: 1523 instructions needed
  2353. // to compile program.
  2354. // Enabling integrated graphics compatibility mode will automatically disable:
  2355. // 1.) PHOSPHOR_MASK_MANUALLY_RESIZE: The phosphor mask will be softer.
  2356. // (This may be reenabled in a later release.)
  2357. // 2.) RUNTIME_GEOMETRY_MODE
  2358. // 3.) The high-quality 4x4 Gaussian resize for the bloom approximation
  2359. //#define INTEGRATED_GRAPHICS_COMPATIBILITY_MODE
  2360. //////////////////////////// USER CODEPATH OPTIONS ///////////////////////////
  2361. // To disable a #define option, turn its line into a comment with "//."
  2362. // RUNTIME VS. COMPILE-TIME OPTIONS (Major Performance Implications):
  2363. // Enable runtime shader parameters in the Retroarch (etc.) GUI? They override
  2364. // many of the options in this file and allow real-time tuning, but many of
  2365. // them are slower. Disabling them and using this text file will boost FPS.
  2366. #define RUNTIME_SHADER_PARAMS_ENABLE
  2367. // Specify the phosphor bloom sigma at runtime? This option is 10% slower, but
  2368. // it's the only way to do a wide-enough full bloom with a runtime dot pitch.
  2369. #define RUNTIME_PHOSPHOR_BLOOM_SIGMA
  2370. // Specify antialiasing weight parameters at runtime? (Costs ~20% with cubics)
  2371. #define RUNTIME_ANTIALIAS_WEIGHTS
  2372. // Specify subpixel offsets at runtime? (WARNING: EXTREMELY EXPENSIVE!)
  2373. //#define RUNTIME_ANTIALIAS_SUBPIXEL_OFFSETS
  2374. // Make beam_horiz_filter and beam_horiz_linear_rgb_weight into runtime shader
  2375. // parameters? This will require more math or dynamic branching.
  2376. #define RUNTIME_SCANLINES_HORIZ_FILTER_COLORSPACE
  2377. // Specify the tilt at runtime? This makes things about 3% slower.
  2378. #define RUNTIME_GEOMETRY_TILT
  2379. // Specify the geometry mode at runtime?
  2380. #define RUNTIME_GEOMETRY_MODE
  2381. // Specify the phosphor mask type (aperture grille, slot mask, shadow mask) and
  2382. // mode (Lanczos-resize, hardware resize, or tile 1:1) at runtime, even without
  2383. // dynamic branches? This is cheap if mask_resize_viewport_scale is small.
  2384. #define FORCE_RUNTIME_PHOSPHOR_MASK_MODE_TYPE_SELECT
  2385. // PHOSPHOR MASK:
  2386. // Manually resize the phosphor mask for best results (slower)? Disabling this
  2387. // removes the option to do so, but it may be faster without dynamic branches.
  2388. #define PHOSPHOR_MASK_MANUALLY_RESIZE
  2389. // If we sinc-resize the mask, should we Lanczos-window it (slower but better)?
  2390. #define PHOSPHOR_MASK_RESIZE_LANCZOS_WINDOW
  2391. // Larger blurs are expensive, but we need them to blur larger triads. We can
  2392. // detect the right blur if the triad size is static or our profile allows
  2393. // dynamic branches, but otherwise we use the largest blur the user indicates
  2394. // they might need:
  2395. #define PHOSPHOR_BLOOM_TRIADS_LARGER_THAN_3_PIXELS
  2396. //#define PHOSPHOR_BLOOM_TRIADS_LARGER_THAN_6_PIXELS
  2397. //#define PHOSPHOR_BLOOM_TRIADS_LARGER_THAN_9_PIXELS
  2398. //#define PHOSPHOR_BLOOM_TRIADS_LARGER_THAN_12_PIXELS
  2399. // Here's a helpful chart:
  2400. // MaxTriadSize BlurSize MinTriadCountsByResolution
  2401. // 3.0 9.0 480/640/960/1920 triads at 1080p/1440p/2160p/4320p, 4:3 aspect
  2402. // 6.0 17.0 240/320/480/960 triads at 1080p/1440p/2160p/4320p, 4:3 aspect
  2403. // 9.0 25.0 160/213/320/640 triads at 1080p/1440p/2160p/4320p, 4:3 aspect
  2404. // 12.0 31.0 120/160/240/480 triads at 1080p/1440p/2160p/4320p, 4:3 aspect
  2405. // 18.0 43.0 80/107/160/320 triads at 1080p/1440p/2160p/4320p, 4:3 aspect
  2406. /////////////////////////////// USER PARAMETERS //////////////////////////////
  2407. // Note: Many of these static parameters are overridden by runtime shader
  2408. // parameters when those are enabled. However, many others are static codepath
  2409. // options that were cleaner or more convert to code as static constants.
  2410. // GAMMA:
  2411. static const float crt_gamma_static = 2.5; // range [1, 5]
  2412. static const float lcd_gamma_static = 2.2; // range [1, 5]
  2413. // LEVELS MANAGEMENT:
  2414. // Control the final multiplicative image contrast:
  2415. static const float levels_contrast_static = 1.0; // range [0, 4)
  2416. // We auto-dim to avoid clipping between passes and restore brightness
  2417. // later. Control the dim factor here: Lower values clip less but crush
  2418. // blacks more (static only for now).
  2419. static const float levels_autodim_temp = 0.5; // range (0, 1] default is 0.5 but that was unnecessarily dark for me, so I set it to 1.0
  2420. // HALATION/DIFFUSION/BLOOM:
  2421. // Halation weight: How much energy should be lost to electrons bounding
  2422. // around under the CRT glass and exciting random phosphors?
  2423. static const float halation_weight_static = 0.0; // range [0, 1]
  2424. // Refractive diffusion weight: How much light should spread/diffuse from
  2425. // refracting through the CRT glass?
  2426. static const float diffusion_weight_static = 0.075; // range [0, 1]
  2427. // Underestimate brightness: Bright areas bloom more, but we can base the
  2428. // bloom brightpass on a lower brightness to sharpen phosphors, or a higher
  2429. // brightness to soften them. Low values clip, but >= 0.8 looks okay.
  2430. static const float bloom_underestimate_levels_static = 0.8; // range [0, 5]
  2431. // Blur all colors more than necessary for a softer phosphor bloom?
  2432. static const float bloom_excess_static = 0.0; // range [0, 1]
  2433. // The BLOOM_APPROX pass approximates a phosphor blur early on with a small
  2434. // blurred resize of the input (convergence offsets are applied as well).
  2435. // There are three filter options (static option only for now):
  2436. // 0.) Bilinear resize: A fast, close approximation to a 4x4 resize
  2437. // if min_allowed_viewport_triads and the BLOOM_APPROX resolution are sane
  2438. // and beam_max_sigma is low.
  2439. // 1.) 3x3 resize blur: Medium speed, soft/smeared from bilinear blurring,
  2440. // always uses a static sigma regardless of beam_max_sigma or
  2441. // mask_num_triads_desired.
  2442. // 2.) True 4x4 Gaussian resize: Slowest, technically correct.
  2443. // These options are more pronounced for the fast, unbloomed shader version.
  2444. #ifndef RADEON_FIX
  2445. static const float bloom_approx_filter_static = 2.0;
  2446. #else
  2447. static const float bloom_approx_filter_static = 1.0;
  2448. #endif
  2449. // ELECTRON BEAM SCANLINE DISTRIBUTION:
  2450. // How many scanlines should contribute light to each pixel? Using more
  2451. // scanlines is slower (especially for a generalized Gaussian) but less
  2452. // distorted with larger beam sigmas (especially for a pure Gaussian). The
  2453. // max_beam_sigma at which the closest unused weight is guaranteed <
  2454. // 1.0/255.0 (for a 3x antialiased pure Gaussian) is:
  2455. // 2 scanlines: max_beam_sigma = 0.2089; distortions begin ~0.34; 141.7 FPS pure, 131.9 FPS generalized
  2456. // 3 scanlines, max_beam_sigma = 0.3879; distortions begin ~0.52; 137.5 FPS pure; 123.8 FPS generalized
  2457. // 4 scanlines, max_beam_sigma = 0.5723; distortions begin ~0.70; 134.7 FPS pure; 117.2 FPS generalized
  2458. // 5 scanlines, max_beam_sigma = 0.7591; distortions begin ~0.89; 131.6 FPS pure; 112.1 FPS generalized
  2459. // 6 scanlines, max_beam_sigma = 0.9483; distortions begin ~1.08; 127.9 FPS pure; 105.6 FPS generalized
  2460. static const float beam_num_scanlines = 3.0; // range [2, 6]
  2461. // A generalized Gaussian beam varies shape with color too, now just width.
  2462. // It's slower but more flexible (static option only for now).
  2463. static const bool beam_generalized_gaussian = true;
  2464. // What kind of scanline antialiasing do you want?
  2465. // 0: Sample weights at 1x; 1: Sample weights at 3x; 2: Compute an integral
  2466. // Integrals are slow (especially for generalized Gaussians) and rarely any
  2467. // better than 3x antialiasing (static option only for now).
  2468. static const float beam_antialias_level = 1.0; // range [0, 2]
  2469. // Min/max standard deviations for scanline beams: Higher values widen and
  2470. // soften scanlines. Depending on other options, low min sigmas can alias.
  2471. static const float beam_min_sigma_static = 0.02; // range (0, 1]
  2472. static const float beam_max_sigma_static = 0.3; // range (0, 1]
  2473. // Beam width varies as a function of color: A power function (0) is more
  2474. // configurable, but a spherical function (1) gives the widest beam
  2475. // variability without aliasing (static option only for now).
  2476. static const float beam_spot_shape_function = 0.0;
  2477. // Spot shape power: Powers <= 1 give smoother spot shapes but lower
  2478. // sharpness. Powers >= 1.0 are awful unless mix/max sigmas are close.
  2479. static const float beam_spot_power_static = 1.0/3.0; // range (0, 16]
  2480. // Generalized Gaussian max shape parameters: Higher values give flatter
  2481. // scanline plateaus and steeper dropoffs, simultaneously widening and
  2482. // sharpening scanlines at the cost of aliasing. 2.0 is pure Gaussian, and
  2483. // values > ~40.0 cause artifacts with integrals.
  2484. static const float beam_min_shape_static = 2.0; // range [2, 32]
  2485. static const float beam_max_shape_static = 4.0; // range [2, 32]
  2486. // Generalized Gaussian shape power: Affects how quickly the distribution
  2487. // changes shape from Gaussian to steep/plateaued as color increases from 0
  2488. // to 1.0. Higher powers appear softer for most colors, and lower powers
  2489. // appear sharper for most colors.
  2490. static const float beam_shape_power_static = 1.0/4.0; // range (0, 16]
  2491. // What filter should be used to sample scanlines horizontally?
  2492. // 0: Quilez (fast), 1: Gaussian (configurable), 2: Lanczos2 (sharp)
  2493. static const float beam_horiz_filter_static = 0.0;
  2494. // Standard deviation for horizontal Gaussian resampling:
  2495. static const float beam_horiz_sigma_static = 0.35; // range (0, 2/3]
  2496. // Do horizontal scanline sampling in linear RGB (correct light mixing),
  2497. // gamma-encoded RGB (darker, hard spot shape, may better match bandwidth-
  2498. // limiting circuitry in some CRT's), or a weighted avg.?
  2499. static const float beam_horiz_linear_rgb_weight_static = 1.0; // range [0, 1]
  2500. // Simulate scanline misconvergence? This needs 3x horizontal texture
  2501. // samples and 3x texture samples of BLOOM_APPROX and HALATION_BLUR in
  2502. // later passes (static option only for now).
  2503. static const bool beam_misconvergence = true;
  2504. // Convergence offsets in x/y directions for R/G/B scanline beams in units
  2505. // of scanlines. Positive offsets go right/down; ranges [-2, 2]
  2506. static const float2 convergence_offsets_r_static = float2(0.1, 0.2);
  2507. static const float2 convergence_offsets_g_static = float2(0.3, 0.4);
  2508. static const float2 convergence_offsets_b_static = float2(0.5, 0.6);
  2509. // Detect interlacing (static option only for now)?
  2510. static const bool interlace_detect = true;
  2511. // Assume 1080-line sources are interlaced?
  2512. static const bool interlace_1080i_static = false;
  2513. // For interlaced sources, assume TFF (top-field first) or BFF order?
  2514. // (Whether this matters depends on the nature of the interlaced input.)
  2515. static const bool interlace_bff_static = false;
  2516. // ANTIALIASING:
  2517. // What AA level do you want for curvature/overscan/subpixels? Options:
  2518. // 0x (none), 1x (sample subpixels), 4x, 5x, 6x, 7x, 8x, 12x, 16x, 20x, 24x
  2519. // (Static option only for now)
  2520. static const float aa_level = 12.0; // range [0, 24]
  2521. // What antialiasing filter do you want (static option only)? Options:
  2522. // 0: Box (separable), 1: Box (cylindrical),
  2523. // 2: Tent (separable), 3: Tent (cylindrical),
  2524. // 4: Gaussian (separable), 5: Gaussian (cylindrical),
  2525. // 6: Cubic* (separable), 7: Cubic* (cylindrical, poor)
  2526. // 8: Lanczos Sinc (separable), 9: Lanczos Jinc (cylindrical, poor)
  2527. // * = Especially slow with RUNTIME_ANTIALIAS_WEIGHTS
  2528. static const float aa_filter = 6.0; // range [0, 9]
  2529. // Flip the sample grid on odd/even frames (static option only for now)?
  2530. static const bool aa_temporal = false;
  2531. // Use RGB subpixel offsets for antialiasing? The pixel is at green, and
  2532. // the blue offset is the negative r offset; range [0, 0.5]
  2533. static const float2 aa_subpixel_r_offset_static = float2(-1.0/3.0, 0.0);//float2(0.0);
  2534. // Cubics: See http://www.imagemagick.org/Usage/filter/#mitchell
  2535. // 1.) "Keys cubics" with B = 1 - 2C are considered the highest quality.
  2536. // 2.) C = 0.5 (default) is Catmull-Rom; higher C's apply sharpening.
  2537. // 3.) C = 1.0/3.0 is the Mitchell-Netravali filter.
  2538. // 4.) C = 0.0 is a soft spline filter.
  2539. static const float aa_cubic_c_static = 0.5; // range [0, 4]
  2540. // Standard deviation for Gaussian antialiasing: Try 0.5/aa_pixel_diameter.
  2541. static const float aa_gauss_sigma_static = 0.5; // range [0.0625, 1.0]
  2542. // PHOSPHOR MASK:
  2543. // Mask type: 0 = aperture grille, 1 = slot mask, 2 = EDP shadow mask
  2544. static const float mask_type_static = 1.0; // range [0, 2]
  2545. // We can sample the mask three ways. Pick 2/3 from: Pretty/Fast/Flexible.
  2546. // 0.) Sinc-resize to the desired dot pitch manually (pretty/slow/flexible).
  2547. // This requires PHOSPHOR_MASK_MANUALLY_RESIZE to be #defined.
  2548. // 1.) Hardware-resize to the desired dot pitch (ugly/fast/flexible). This
  2549. // is halfway decent with LUT mipmapping but atrocious without it.
  2550. // 2.) Tile it without resizing at a 1:1 texel:pixel ratio for flat coords
  2551. // (pretty/fast/inflexible). Each input LUT has a fixed dot pitch.
  2552. // This mode reuses the same masks, so triads will be enormous unless
  2553. // you change the mask LUT filenames in your .cgp file.
  2554. static const float mask_sample_mode_static = 0.0; // range [0, 2]
  2555. // Prefer setting the triad size (0.0) or number on the screen (1.0)?
  2556. // If RUNTIME_PHOSPHOR_BLOOM_SIGMA isn't #defined, the specified triad size
  2557. // will always be used to calculate the full bloom sigma statically.
  2558. static const float mask_specify_num_triads_static = 0.0; // range [0, 1]
  2559. // Specify the phosphor triad size, in pixels. Each tile (usually with 8
  2560. // triads) will be rounded to the nearest integer tile size and clamped to
  2561. // obey minimum size constraints (imposed to reduce downsize taps) and
  2562. // maximum size constraints (imposed to have a sane MASK_RESIZE FBO size).
  2563. // To increase the size limit, double the viewport-relative scales for the
  2564. // two MASK_RESIZE passes in crt-royale.cgp and user-cgp-contants.h.
  2565. // range [1, mask_texture_small_size/mask_triads_per_tile]
  2566. static const float mask_triad_size_desired_static = 24.0 / 8.0;
  2567. // If mask_specify_num_triads is 1.0/true, we'll go by this instead (the
  2568. // final size will be rounded and constrained as above); default 480.0
  2569. static const float mask_num_triads_desired_static = 480.0;
  2570. // How many lobes should the sinc/Lanczos resizer use? More lobes require
  2571. // more samples and avoid moire a bit better, but some is unavoidable
  2572. // depending on the destination size (static option for now).
  2573. static const float mask_sinc_lobes = 3.0; // range [2, 4]
  2574. // The mask is resized using a variable number of taps in each dimension,
  2575. // but some Cg profiles always fetch a constant number of taps no matter
  2576. // what (no dynamic branching). We can limit the maximum number of taps if
  2577. // we statically limit the minimum phosphor triad size. Larger values are
  2578. // faster, but the limit IS enforced (static option only, forever);
  2579. // range [1, mask_texture_small_size/mask_triads_per_tile]
  2580. // TODO: Make this 1.0 and compensate with smarter sampling!
  2581. static const float mask_min_allowed_triad_size = 2.0;
  2582. // GEOMETRY:
  2583. // Geometry mode:
  2584. // 0: Off (default), 1: Spherical mapping (like cgwg's),
  2585. // 2: Alt. spherical mapping (more bulbous), 3: Cylindrical/Trinitron
  2586. static const float geom_mode_static = 0.0; // range [0, 3]
  2587. // Radius of curvature: Measured in units of your viewport's diagonal size.
  2588. static const float geom_radius_static = 2.0; // range [1/(2*pi), 1024]
  2589. // View dist is the distance from the player to their physical screen, in
  2590. // units of the viewport's diagonal size. It controls the field of view.
  2591. static const float geom_view_dist_static = 2.0; // range [0.5, 1024]
  2592. // Tilt angle in radians (clockwise around up and right vectors):
  2593. static const float2 geom_tilt_angle_static = float2(0.0, 0.0); // range [-pi, pi]
  2594. // Aspect ratio: When the true viewport size is unknown, this value is used
  2595. // to help convert between the phosphor triad size and count, along with
  2596. // the mask_resize_viewport_scale constant from user-cgp-constants.h. Set
  2597. // this equal to Retroarch's display aspect ratio (DAR) for best results;
  2598. // range [1, geom_max_aspect_ratio from user-cgp-constants.h];
  2599. // default (256/224)*(54/47) = 1.313069909 (see below)
  2600. static const float geom_aspect_ratio_static = 1.313069909;
  2601. // Before getting into overscan, here's some general aspect ratio info:
  2602. // - DAR = display aspect ratio = SAR * PAR; as in your Retroarch setting
  2603. // - SAR = storage aspect ratio = DAR / PAR; square pixel emulator frame AR
  2604. // - PAR = pixel aspect ratio = DAR / SAR; holds regardless of cropping
  2605. // Geometry processing has to "undo" the screen-space 2D DAR to calculate
  2606. // 3D view vectors, then reapplies the aspect ratio to the simulated CRT in
  2607. // uv-space. To ensure the source SAR is intended for a ~4:3 DAR, either:
  2608. // a.) Enable Retroarch's "Crop Overscan"
  2609. // b.) Readd horizontal padding: Set overscan to e.g. N*(1.0, 240.0/224.0)
  2610. // Real consoles use horizontal black padding in the signal, but emulators
  2611. // often crop this without cropping the vertical padding; a 256x224 [S]NES
  2612. // frame (8:7 SAR) is intended for a ~4:3 DAR, but a 256x240 frame is not.
  2613. // The correct [S]NES PAR is 54:47, found by blargg and NewRisingSun:
  2614. // http://board.zsnes.com/phpBB3/viewtopic.php?f=22&t=11928&start=50
  2615. // http://forums.nesdev.com/viewtopic.php?p=24815#p24815
  2616. // For flat output, it's okay to set DAR = [existing] SAR * [correct] PAR
  2617. // without doing a. or b., but horizontal image borders will be tighter
  2618. // than vertical ones, messing up curvature and overscan. Fixing the
  2619. // padding first corrects this.
  2620. // Overscan: Amount to "zoom in" before cropping. You can zoom uniformly
  2621. // or adjust x/y independently to e.g. readd horizontal padding, as noted
  2622. // above: Values < 1.0 zoom out; range (0, inf)
  2623. static const float2 geom_overscan_static = float2(1.0, 1.0);// * 1.005 * (1.0, 240/224.0)
  2624. // Compute a proper pixel-space to texture-space matrix even without ddx()/
  2625. // ddy()? This is ~8.5% slower but improves antialiasing/subpixel filtering
  2626. // with strong curvature (static option only for now).
  2627. static const bool geom_force_correct_tangent_matrix = true;
  2628. // BORDERS:
  2629. // Rounded border size in texture uv coords:
  2630. static const float border_size_static = 0.015; // range [0, 0.5]
  2631. // Border darkness: Moderate values darken the border smoothly, and high
  2632. // values make the image very dark just inside the border:
  2633. static const float border_darkness_static = 2.0; // range [0, inf)
  2634. // Border compression: High numbers compress border transitions, narrowing
  2635. // the dark border area.
  2636. static const float border_compress_static = 2.5; // range [1, inf)
  2637. #endif // USER_SETTINGS_H
  2638. //////////////////////////// END USER-SETTINGS //////////////////////////
  2639. //#include "derived-settings-and-constants.h"
  2640. //////////////////// BEGIN DERIVED-SETTINGS-AND-CONSTANTS ////////////////////
  2641. #ifndef DERIVED_SETTINGS_AND_CONSTANTS_H
  2642. #define DERIVED_SETTINGS_AND_CONSTANTS_H
  2643. ///////////////////////////// GPL LICENSE NOTICE /////////////////////////////
  2644. // crt-royale: A full-featured CRT shader, with cheese.
  2645. // Copyright (C) 2014 TroggleMonkey <trogglemonkey@gmx.com>
  2646. //
  2647. // This program is free software; you can redistribute it and/or modify it
  2648. // under the terms of the GNU General Public License as published by the Free
  2649. // Software Foundation; either version 2 of the License, or any later version.
  2650. //
  2651. // This program is distributed in the hope that it will be useful, but WITHOUT
  2652. // ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
  2653. // FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
  2654. // more details.
  2655. //
  2656. // You should have received a copy of the GNU General Public License along with
  2657. // this program; if not, write to the Free Software Foundation, Inc., 59 Temple
  2658. // Place, Suite 330, Boston, MA 02111-1307 USA
  2659. ///////////////////////////////// DESCRIPTION ////////////////////////////////
  2660. // These macros and constants can be used across the whole codebase.
  2661. // Unlike the values in user-settings.cgh, end users shouldn't modify these.
  2662. /////////////////////////////// BEGIN INCLUDES ///////////////////////////////
  2663. //#include "../user-settings.h"
  2664. ///////////////////////////// BEGIN USER-SETTINGS ////////////////////////////
  2665. #ifndef USER_SETTINGS_H
  2666. #define USER_SETTINGS_H
  2667. ///////////////////////////// DRIVER CAPABILITIES ////////////////////////////
  2668. // The Cg compiler uses different "profiles" with different capabilities.
  2669. // This shader requires a Cg compilation profile >= arbfp1, but a few options
  2670. // require higher profiles like fp30 or fp40. The shader can't detect profile
  2671. // or driver capabilities, so instead you must comment or uncomment the lines
  2672. // below with "//" before "#define." Disable an option if you get compilation
  2673. // errors resembling those listed. Generally speaking, all of these options
  2674. // will run on nVidia cards, but only DRIVERS_ALLOW_TEX2DBIAS (if that) is
  2675. // likely to run on ATI/AMD, due to the Cg compiler's profile limitations.
  2676. // Derivatives: Unsupported on fp20, ps_1_1, ps_1_2, ps_1_3, and arbfp1.
  2677. // Among other things, derivatives help us fix anisotropic filtering artifacts
  2678. // with curved manually tiled phosphor mask coords. Related errors:
  2679. // error C3004: function "float2 ddx(float2);" not supported in this profile
  2680. // error C3004: function "float2 ddy(float2);" not supported in this profile
  2681. //#define DRIVERS_ALLOW_DERIVATIVES
  2682. // Fine derivatives: Unsupported on older ATI cards.
  2683. // Fine derivatives enable 2x2 fragment block communication, letting us perform
  2684. // fast single-pass blur operations. If your card uses coarse derivatives and
  2685. // these are enabled, blurs could look broken. Derivatives are a prerequisite.
  2686. #ifdef DRIVERS_ALLOW_DERIVATIVES
  2687. #define DRIVERS_ALLOW_FINE_DERIVATIVES
  2688. #endif
  2689. // Dynamic looping: Requires an fp30 or newer profile.
  2690. // This makes phosphor mask resampling faster in some cases. Related errors:
  2691. // error C5013: profile does not support "for" statements and "for" could not
  2692. // be unrolled
  2693. //#define DRIVERS_ALLOW_DYNAMIC_BRANCHES
  2694. // Without DRIVERS_ALLOW_DYNAMIC_BRANCHES, we need to use unrollable loops.
  2695. // Using one static loop avoids overhead if the user is right, but if the user
  2696. // is wrong (loops are allowed), breaking a loop into if-blocked pieces with a
  2697. // binary search can potentially save some iterations. However, it may fail:
  2698. // error C6001: Temporary register limit of 32 exceeded; 35 registers
  2699. // needed to compile program
  2700. //#define ACCOMODATE_POSSIBLE_DYNAMIC_LOOPS
  2701. // tex2Dlod: Requires an fp40 or newer profile. This can be used to disable
  2702. // anisotropic filtering, thereby fixing related artifacts. Related errors:
  2703. // error C3004: function "float4 tex2Dlod(sampler2D, float4);" not supported in
  2704. // this profile
  2705. //#define DRIVERS_ALLOW_TEX2DLOD
  2706. // tex2Dbias: Requires an fp30 or newer profile. This can be used to alleviate
  2707. // artifacts from anisotropic filtering and mipmapping. Related errors:
  2708. // error C3004: function "float4 tex2Dbias(sampler2D, float4);" not supported
  2709. // in this profile
  2710. //#define DRIVERS_ALLOW_TEX2DBIAS
  2711. // Integrated graphics compatibility: Integrated graphics like Intel HD 4000
  2712. // impose stricter limitations on register counts and instructions. Enable
  2713. // INTEGRATED_GRAPHICS_COMPATIBILITY_MODE if you still see error C6001 or:
  2714. // error C6002: Instruction limit of 1024 exceeded: 1523 instructions needed
  2715. // to compile program.
  2716. // Enabling integrated graphics compatibility mode will automatically disable:
  2717. // 1.) PHOSPHOR_MASK_MANUALLY_RESIZE: The phosphor mask will be softer.
  2718. // (This may be reenabled in a later release.)
  2719. // 2.) RUNTIME_GEOMETRY_MODE
  2720. // 3.) The high-quality 4x4 Gaussian resize for the bloom approximation
  2721. //#define INTEGRATED_GRAPHICS_COMPATIBILITY_MODE
  2722. //////////////////////////// USER CODEPATH OPTIONS ///////////////////////////
  2723. // To disable a #define option, turn its line into a comment with "//."
  2724. // RUNTIME VS. COMPILE-TIME OPTIONS (Major Performance Implications):
  2725. // Enable runtime shader parameters in the Retroarch (etc.) GUI? They override
  2726. // many of the options in this file and allow real-time tuning, but many of
  2727. // them are slower. Disabling them and using this text file will boost FPS.
  2728. #define RUNTIME_SHADER_PARAMS_ENABLE
  2729. // Specify the phosphor bloom sigma at runtime? This option is 10% slower, but
  2730. // it's the only way to do a wide-enough full bloom with a runtime dot pitch.
  2731. #define RUNTIME_PHOSPHOR_BLOOM_SIGMA
  2732. // Specify antialiasing weight parameters at runtime? (Costs ~20% with cubics)
  2733. #define RUNTIME_ANTIALIAS_WEIGHTS
  2734. // Specify subpixel offsets at runtime? (WARNING: EXTREMELY EXPENSIVE!)
  2735. //#define RUNTIME_ANTIALIAS_SUBPIXEL_OFFSETS
  2736. // Make beam_horiz_filter and beam_horiz_linear_rgb_weight into runtime shader
  2737. // parameters? This will require more math or dynamic branching.
  2738. #define RUNTIME_SCANLINES_HORIZ_FILTER_COLORSPACE
  2739. // Specify the tilt at runtime? This makes things about 3% slower.
  2740. #define RUNTIME_GEOMETRY_TILT
  2741. // Specify the geometry mode at runtime?
  2742. #define RUNTIME_GEOMETRY_MODE
  2743. // Specify the phosphor mask type (aperture grille, slot mask, shadow mask) and
  2744. // mode (Lanczos-resize, hardware resize, or tile 1:1) at runtime, even without
  2745. // dynamic branches? This is cheap if mask_resize_viewport_scale is small.
  2746. #define FORCE_RUNTIME_PHOSPHOR_MASK_MODE_TYPE_SELECT
  2747. // PHOSPHOR MASK:
  2748. // Manually resize the phosphor mask for best results (slower)? Disabling this
  2749. // removes the option to do so, but it may be faster without dynamic branches.
  2750. #define PHOSPHOR_MASK_MANUALLY_RESIZE
  2751. // If we sinc-resize the mask, should we Lanczos-window it (slower but better)?
  2752. #define PHOSPHOR_MASK_RESIZE_LANCZOS_WINDOW
  2753. // Larger blurs are expensive, but we need them to blur larger triads. We can
  2754. // detect the right blur if the triad size is static or our profile allows
  2755. // dynamic branches, but otherwise we use the largest blur the user indicates
  2756. // they might need:
  2757. #define PHOSPHOR_BLOOM_TRIADS_LARGER_THAN_3_PIXELS
  2758. //#define PHOSPHOR_BLOOM_TRIADS_LARGER_THAN_6_PIXELS
  2759. //#define PHOSPHOR_BLOOM_TRIADS_LARGER_THAN_9_PIXELS
  2760. //#define PHOSPHOR_BLOOM_TRIADS_LARGER_THAN_12_PIXELS
  2761. // Here's a helpful chart:
  2762. // MaxTriadSize BlurSize MinTriadCountsByResolution
  2763. // 3.0 9.0 480/640/960/1920 triads at 1080p/1440p/2160p/4320p, 4:3 aspect
  2764. // 6.0 17.0 240/320/480/960 triads at 1080p/1440p/2160p/4320p, 4:3 aspect
  2765. // 9.0 25.0 160/213/320/640 triads at 1080p/1440p/2160p/4320p, 4:3 aspect
  2766. // 12.0 31.0 120/160/240/480 triads at 1080p/1440p/2160p/4320p, 4:3 aspect
  2767. // 18.0 43.0 80/107/160/320 triads at 1080p/1440p/2160p/4320p, 4:3 aspect
  2768. /////////////////////////////// USER PARAMETERS //////////////////////////////
  2769. // Note: Many of these static parameters are overridden by runtime shader
  2770. // parameters when those are enabled. However, many others are static codepath
  2771. // options that were cleaner or more convert to code as static constants.
  2772. // GAMMA:
  2773. static const float crt_gamma_static = 2.5; // range [1, 5]
  2774. static const float lcd_gamma_static = 2.2; // range [1, 5]
  2775. // LEVELS MANAGEMENT:
  2776. // Control the final multiplicative image contrast:
  2777. static const float levels_contrast_static = 1.0; // range [0, 4)
  2778. // We auto-dim to avoid clipping between passes and restore brightness
  2779. // later. Control the dim factor here: Lower values clip less but crush
  2780. // blacks more (static only for now).
  2781. static const float levels_autodim_temp = 0.5; // range (0, 1] default is 0.5 but that was unnecessarily dark for me, so I set it to 1.0
  2782. // HALATION/DIFFUSION/BLOOM:
  2783. // Halation weight: How much energy should be lost to electrons bounding
  2784. // around under the CRT glass and exciting random phosphors?
  2785. static const float halation_weight_static = 0.0; // range [0, 1]
  2786. // Refractive diffusion weight: How much light should spread/diffuse from
  2787. // refracting through the CRT glass?
  2788. static const float diffusion_weight_static = 0.075; // range [0, 1]
  2789. // Underestimate brightness: Bright areas bloom more, but we can base the
  2790. // bloom brightpass on a lower brightness to sharpen phosphors, or a higher
  2791. // brightness to soften them. Low values clip, but >= 0.8 looks okay.
  2792. static const float bloom_underestimate_levels_static = 0.8; // range [0, 5]
  2793. // Blur all colors more than necessary for a softer phosphor bloom?
  2794. static const float bloom_excess_static = 0.0; // range [0, 1]
  2795. // The BLOOM_APPROX pass approximates a phosphor blur early on with a small
  2796. // blurred resize of the input (convergence offsets are applied as well).
  2797. // There are three filter options (static option only for now):
  2798. // 0.) Bilinear resize: A fast, close approximation to a 4x4 resize
  2799. // if min_allowed_viewport_triads and the BLOOM_APPROX resolution are sane
  2800. // and beam_max_sigma is low.
  2801. // 1.) 3x3 resize blur: Medium speed, soft/smeared from bilinear blurring,
  2802. // always uses a static sigma regardless of beam_max_sigma or
  2803. // mask_num_triads_desired.
  2804. // 2.) True 4x4 Gaussian resize: Slowest, technically correct.
  2805. // These options are more pronounced for the fast, unbloomed shader version.
  2806. #ifndef RADEON_FIX
  2807. static const float bloom_approx_filter_static = 2.0;
  2808. #else
  2809. static const float bloom_approx_filter_static = 1.0;
  2810. #endif
  2811. // ELECTRON BEAM SCANLINE DISTRIBUTION:
  2812. // How many scanlines should contribute light to each pixel? Using more
  2813. // scanlines is slower (especially for a generalized Gaussian) but less
  2814. // distorted with larger beam sigmas (especially for a pure Gaussian). The
  2815. // max_beam_sigma at which the closest unused weight is guaranteed <
  2816. // 1.0/255.0 (for a 3x antialiased pure Gaussian) is:
  2817. // 2 scanlines: max_beam_sigma = 0.2089; distortions begin ~0.34; 141.7 FPS pure, 131.9 FPS generalized
  2818. // 3 scanlines, max_beam_sigma = 0.3879; distortions begin ~0.52; 137.5 FPS pure; 123.8 FPS generalized
  2819. // 4 scanlines, max_beam_sigma = 0.5723; distortions begin ~0.70; 134.7 FPS pure; 117.2 FPS generalized
  2820. // 5 scanlines, max_beam_sigma = 0.7591; distortions begin ~0.89; 131.6 FPS pure; 112.1 FPS generalized
  2821. // 6 scanlines, max_beam_sigma = 0.9483; distortions begin ~1.08; 127.9 FPS pure; 105.6 FPS generalized
  2822. static const float beam_num_scanlines = 3.0; // range [2, 6]
  2823. // A generalized Gaussian beam varies shape with color too, now just width.
  2824. // It's slower but more flexible (static option only for now).
  2825. static const bool beam_generalized_gaussian = true;
  2826. // What kind of scanline antialiasing do you want?
  2827. // 0: Sample weights at 1x; 1: Sample weights at 3x; 2: Compute an integral
  2828. // Integrals are slow (especially for generalized Gaussians) and rarely any
  2829. // better than 3x antialiasing (static option only for now).
  2830. static const float beam_antialias_level = 1.0; // range [0, 2]
  2831. // Min/max standard deviations for scanline beams: Higher values widen and
  2832. // soften scanlines. Depending on other options, low min sigmas can alias.
  2833. static const float beam_min_sigma_static = 0.02; // range (0, 1]
  2834. static const float beam_max_sigma_static = 0.3; // range (0, 1]
  2835. // Beam width varies as a function of color: A power function (0) is more
  2836. // configurable, but a spherical function (1) gives the widest beam
  2837. // variability without aliasing (static option only for now).
  2838. static const float beam_spot_shape_function = 0.0;
  2839. // Spot shape power: Powers <= 1 give smoother spot shapes but lower
  2840. // sharpness. Powers >= 1.0 are awful unless mix/max sigmas are close.
  2841. static const float beam_spot_power_static = 1.0/3.0; // range (0, 16]
  2842. // Generalized Gaussian max shape parameters: Higher values give flatter
  2843. // scanline plateaus and steeper dropoffs, simultaneously widening and
  2844. // sharpening scanlines at the cost of aliasing. 2.0 is pure Gaussian, and
  2845. // values > ~40.0 cause artifacts with integrals.
  2846. static const float beam_min_shape_static = 2.0; // range [2, 32]
  2847. static const float beam_max_shape_static = 4.0; // range [2, 32]
  2848. // Generalized Gaussian shape power: Affects how quickly the distribution
  2849. // changes shape from Gaussian to steep/plateaued as color increases from 0
  2850. // to 1.0. Higher powers appear softer for most colors, and lower powers
  2851. // appear sharper for most colors.
  2852. static const float beam_shape_power_static = 1.0/4.0; // range (0, 16]
  2853. // What filter should be used to sample scanlines horizontally?
  2854. // 0: Quilez (fast), 1: Gaussian (configurable), 2: Lanczos2 (sharp)
  2855. static const float beam_horiz_filter_static = 0.0;
  2856. // Standard deviation for horizontal Gaussian resampling:
  2857. static const float beam_horiz_sigma_static = 0.35; // range (0, 2/3]
  2858. // Do horizontal scanline sampling in linear RGB (correct light mixing),
  2859. // gamma-encoded RGB (darker, hard spot shape, may better match bandwidth-
  2860. // limiting circuitry in some CRT's), or a weighted avg.?
  2861. static const float beam_horiz_linear_rgb_weight_static = 1.0; // range [0, 1]
  2862. // Simulate scanline misconvergence? This needs 3x horizontal texture
  2863. // samples and 3x texture samples of BLOOM_APPROX and HALATION_BLUR in
  2864. // later passes (static option only for now).
  2865. static const bool beam_misconvergence = true;
  2866. // Convergence offsets in x/y directions for R/G/B scanline beams in units
  2867. // of scanlines. Positive offsets go right/down; ranges [-2, 2]
  2868. static const float2 convergence_offsets_r_static = float2(0.1, 0.2);
  2869. static const float2 convergence_offsets_g_static = float2(0.3, 0.4);
  2870. static const float2 convergence_offsets_b_static = float2(0.5, 0.6);
  2871. // Detect interlacing (static option only for now)?
  2872. static const bool interlace_detect = true;
  2873. // Assume 1080-line sources are interlaced?
  2874. static const bool interlace_1080i_static = false;
  2875. // For interlaced sources, assume TFF (top-field first) or BFF order?
  2876. // (Whether this matters depends on the nature of the interlaced input.)
  2877. static const bool interlace_bff_static = false;
  2878. // ANTIALIASING:
  2879. // What AA level do you want for curvature/overscan/subpixels? Options:
  2880. // 0x (none), 1x (sample subpixels), 4x, 5x, 6x, 7x, 8x, 12x, 16x, 20x, 24x
  2881. // (Static option only for now)
  2882. static const float aa_level = 12.0; // range [0, 24]
  2883. // What antialiasing filter do you want (static option only)? Options:
  2884. // 0: Box (separable), 1: Box (cylindrical),
  2885. // 2: Tent (separable), 3: Tent (cylindrical),
  2886. // 4: Gaussian (separable), 5: Gaussian (cylindrical),
  2887. // 6: Cubic* (separable), 7: Cubic* (cylindrical, poor)
  2888. // 8: Lanczos Sinc (separable), 9: Lanczos Jinc (cylindrical, poor)
  2889. // * = Especially slow with RUNTIME_ANTIALIAS_WEIGHTS
  2890. static const float aa_filter = 6.0; // range [0, 9]
  2891. // Flip the sample grid on odd/even frames (static option only for now)?
  2892. static const bool aa_temporal = false;
  2893. // Use RGB subpixel offsets for antialiasing? The pixel is at green, and
  2894. // the blue offset is the negative r offset; range [0, 0.5]
  2895. static const float2 aa_subpixel_r_offset_static = float2(-1.0/3.0, 0.0);//float2(0.0);
  2896. // Cubics: See http://www.imagemagick.org/Usage/filter/#mitchell
  2897. // 1.) "Keys cubics" with B = 1 - 2C are considered the highest quality.
  2898. // 2.) C = 0.5 (default) is Catmull-Rom; higher C's apply sharpening.
  2899. // 3.) C = 1.0/3.0 is the Mitchell-Netravali filter.
  2900. // 4.) C = 0.0 is a soft spline filter.
  2901. static const float aa_cubic_c_static = 0.5; // range [0, 4]
  2902. // Standard deviation for Gaussian antialiasing: Try 0.5/aa_pixel_diameter.
  2903. static const float aa_gauss_sigma_static = 0.5; // range [0.0625, 1.0]
  2904. // PHOSPHOR MASK:
  2905. // Mask type: 0 = aperture grille, 1 = slot mask, 2 = EDP shadow mask
  2906. static const float mask_type_static = 1.0; // range [0, 2]
  2907. // We can sample the mask three ways. Pick 2/3 from: Pretty/Fast/Flexible.
  2908. // 0.) Sinc-resize to the desired dot pitch manually (pretty/slow/flexible).
  2909. // This requires PHOSPHOR_MASK_MANUALLY_RESIZE to be #defined.
  2910. // 1.) Hardware-resize to the desired dot pitch (ugly/fast/flexible). This
  2911. // is halfway decent with LUT mipmapping but atrocious without it.
  2912. // 2.) Tile it without resizing at a 1:1 texel:pixel ratio for flat coords
  2913. // (pretty/fast/inflexible). Each input LUT has a fixed dot pitch.
  2914. // This mode reuses the same masks, so triads will be enormous unless
  2915. // you change the mask LUT filenames in your .cgp file.
  2916. static const float mask_sample_mode_static = 0.0; // range [0, 2]
  2917. // Prefer setting the triad size (0.0) or number on the screen (1.0)?
  2918. // If RUNTIME_PHOSPHOR_BLOOM_SIGMA isn't #defined, the specified triad size
  2919. // will always be used to calculate the full bloom sigma statically.
  2920. static const float mask_specify_num_triads_static = 0.0; // range [0, 1]
  2921. // Specify the phosphor triad size, in pixels. Each tile (usually with 8
  2922. // triads) will be rounded to the nearest integer tile size and clamped to
  2923. // obey minimum size constraints (imposed to reduce downsize taps) and
  2924. // maximum size constraints (imposed to have a sane MASK_RESIZE FBO size).
  2925. // To increase the size limit, double the viewport-relative scales for the
  2926. // two MASK_RESIZE passes in crt-royale.cgp and user-cgp-contants.h.
  2927. // range [1, mask_texture_small_size/mask_triads_per_tile]
  2928. static const float mask_triad_size_desired_static = 24.0 / 8.0;
  2929. // If mask_specify_num_triads is 1.0/true, we'll go by this instead (the
  2930. // final size will be rounded and constrained as above); default 480.0
  2931. static const float mask_num_triads_desired_static = 480.0;
  2932. // How many lobes should the sinc/Lanczos resizer use? More lobes require
  2933. // more samples and avoid moire a bit better, but some is unavoidable
  2934. // depending on the destination size (static option for now).
  2935. static const float mask_sinc_lobes = 3.0; // range [2, 4]
  2936. // The mask is resized using a variable number of taps in each dimension,
  2937. // but some Cg profiles always fetch a constant number of taps no matter
  2938. // what (no dynamic branching). We can limit the maximum number of taps if
  2939. // we statically limit the minimum phosphor triad size. Larger values are
  2940. // faster, but the limit IS enforced (static option only, forever);
  2941. // range [1, mask_texture_small_size/mask_triads_per_tile]
  2942. // TODO: Make this 1.0 and compensate with smarter sampling!
  2943. static const float mask_min_allowed_triad_size = 2.0;
  2944. // GEOMETRY:
  2945. // Geometry mode:
  2946. // 0: Off (default), 1: Spherical mapping (like cgwg's),
  2947. // 2: Alt. spherical mapping (more bulbous), 3: Cylindrical/Trinitron
  2948. static const float geom_mode_static = 0.0; // range [0, 3]
  2949. // Radius of curvature: Measured in units of your viewport's diagonal size.
  2950. static const float geom_radius_static = 2.0; // range [1/(2*pi), 1024]
  2951. // View dist is the distance from the player to their physical screen, in
  2952. // units of the viewport's diagonal size. It controls the field of view.
  2953. static const float geom_view_dist_static = 2.0; // range [0.5, 1024]
  2954. // Tilt angle in radians (clockwise around up and right vectors):
  2955. static const float2 geom_tilt_angle_static = float2(0.0, 0.0); // range [-pi, pi]
  2956. // Aspect ratio: When the true viewport size is unknown, this value is used
  2957. // to help convert between the phosphor triad size and count, along with
  2958. // the mask_resize_viewport_scale constant from user-cgp-constants.h. Set
  2959. // this equal to Retroarch's display aspect ratio (DAR) for best results;
  2960. // range [1, geom_max_aspect_ratio from user-cgp-constants.h];
  2961. // default (256/224)*(54/47) = 1.313069909 (see below)
  2962. static const float geom_aspect_ratio_static = 1.313069909;
  2963. // Before getting into overscan, here's some general aspect ratio info:
  2964. // - DAR = display aspect ratio = SAR * PAR; as in your Retroarch setting
  2965. // - SAR = storage aspect ratio = DAR / PAR; square pixel emulator frame AR
  2966. // - PAR = pixel aspect ratio = DAR / SAR; holds regardless of cropping
  2967. // Geometry processing has to "undo" the screen-space 2D DAR to calculate
  2968. // 3D view vectors, then reapplies the aspect ratio to the simulated CRT in
  2969. // uv-space. To ensure the source SAR is intended for a ~4:3 DAR, either:
  2970. // a.) Enable Retroarch's "Crop Overscan"
  2971. // b.) Readd horizontal padding: Set overscan to e.g. N*(1.0, 240.0/224.0)
  2972. // Real consoles use horizontal black padding in the signal, but emulators
  2973. // often crop this without cropping the vertical padding; a 256x224 [S]NES
  2974. // frame (8:7 SAR) is intended for a ~4:3 DAR, but a 256x240 frame is not.
  2975. // The correct [S]NES PAR is 54:47, found by blargg and NewRisingSun:
  2976. // http://board.zsnes.com/phpBB3/viewtopic.php?f=22&t=11928&start=50
  2977. // http://forums.nesdev.com/viewtopic.php?p=24815#p24815
  2978. // For flat output, it's okay to set DAR = [existing] SAR * [correct] PAR
  2979. // without doing a. or b., but horizontal image borders will be tighter
  2980. // than vertical ones, messing up curvature and overscan. Fixing the
  2981. // padding first corrects this.
  2982. // Overscan: Amount to "zoom in" before cropping. You can zoom uniformly
  2983. // or adjust x/y independently to e.g. readd horizontal padding, as noted
  2984. // above: Values < 1.0 zoom out; range (0, inf)
  2985. static const float2 geom_overscan_static = float2(1.0, 1.0);// * 1.005 * (1.0, 240/224.0)
  2986. // Compute a proper pixel-space to texture-space matrix even without ddx()/
  2987. // ddy()? This is ~8.5% slower but improves antialiasing/subpixel filtering
  2988. // with strong curvature (static option only for now).
  2989. static const bool geom_force_correct_tangent_matrix = true;
  2990. // BORDERS:
  2991. // Rounded border size in texture uv coords:
  2992. static const float border_size_static = 0.015; // range [0, 0.5]
  2993. // Border darkness: Moderate values darken the border smoothly, and high
  2994. // values make the image very dark just inside the border:
  2995. static const float border_darkness_static = 2.0; // range [0, inf)
  2996. // Border compression: High numbers compress border transitions, narrowing
  2997. // the dark border area.
  2998. static const float border_compress_static = 2.5; // range [1, inf)
  2999. #endif // USER_SETTINGS_H
  3000. ///////////////////////////// END USER-SETTINGS ////////////////////////////
  3001. //#include "user-cgp-constants.h"
  3002. ///////////////////////// BEGIN USER-CGP-CONSTANTS /////////////////////////
  3003. #ifndef USER_CGP_CONSTANTS_H
  3004. #define USER_CGP_CONSTANTS_H
  3005. // IMPORTANT:
  3006. // These constants MUST be set appropriately for the settings in crt-royale.cgp
  3007. // (or whatever related .cgp file you're using). If they aren't, you're likely
  3008. // to get artifacts, the wrong phosphor mask size, etc. I wish these could be
  3009. // set directly in the .cgp file to make things easier, but...they can't.
  3010. // PASS SCALES AND RELATED CONSTANTS:
  3011. // Copy the absolute scale_x for BLOOM_APPROX. There are two major versions of
  3012. // this shader: One does a viewport-scale bloom, and the other skips it. The
  3013. // latter benefits from a higher bloom_approx_scale_x, so save both separately:
  3014. static const float bloom_approx_size_x = 320.0;
  3015. static const float bloom_approx_size_x_for_fake = 400.0;
  3016. // Copy the viewport-relative scales of the phosphor mask resize passes
  3017. // (MASK_RESIZE and the pass immediately preceding it):
  3018. static const float2 mask_resize_viewport_scale = float2(0.0625, 0.0625);
  3019. // Copy the geom_max_aspect_ratio used to calculate the MASK_RESIZE scales, etc.:
  3020. static const float geom_max_aspect_ratio = 4.0/3.0;
  3021. // PHOSPHOR MASK TEXTURE CONSTANTS:
  3022. // Set the following constants to reflect the properties of the phosphor mask
  3023. // texture named in crt-royale.cgp. The shader optionally resizes a mask tile
  3024. // based on user settings, then repeats a single tile until filling the screen.
  3025. // The shader must know the input texture size (default 64x64), and to manually
  3026. // resize, it must also know the horizontal triads per tile (default 8).
  3027. static const float2 mask_texture_small_size = float2(64.0, 64.0);
  3028. static const float2 mask_texture_large_size = float2(512.0, 512.0);
  3029. static const float mask_triads_per_tile = 8.0;
  3030. // We need the average brightness of the phosphor mask to compensate for the
  3031. // dimming it causes. The following four values are roughly correct for the
  3032. // masks included with the shader. Update the value for any LUT texture you
  3033. // change. [Un]comment "#define PHOSPHOR_MASK_GRILLE14" depending on whether
  3034. // the loaded aperture grille uses 14-pixel or 15-pixel stripes (default 15).
  3035. //#define PHOSPHOR_MASK_GRILLE14
  3036. static const float mask_grille14_avg_color = 50.6666666/255.0;
  3037. // TileableLinearApertureGrille14Wide7d33Spacing*.png
  3038. // TileableLinearApertureGrille14Wide10And6Spacing*.png
  3039. static const float mask_grille15_avg_color = 53.0/255.0;
  3040. // TileableLinearApertureGrille15Wide6d33Spacing*.png
  3041. // TileableLinearApertureGrille15Wide8And5d5Spacing*.png
  3042. static const float mask_slot_avg_color = 46.0/255.0;
  3043. // TileableLinearSlotMask15Wide9And4d5Horizontal8VerticalSpacing*.png
  3044. // TileableLinearSlotMaskTall15Wide9And4d5Horizontal9d14VerticalSpacing*.png
  3045. static const float mask_shadow_avg_color = 41.0/255.0;
  3046. // TileableLinearShadowMask*.png
  3047. // TileableLinearShadowMaskEDP*.png
  3048. #ifdef PHOSPHOR_MASK_GRILLE14
  3049. static const float mask_grille_avg_color = mask_grille14_avg_color;
  3050. #else
  3051. static const float mask_grille_avg_color = mask_grille15_avg_color;
  3052. #endif
  3053. #endif // USER_CGP_CONSTANTS_H
  3054. ////////////////////////// END USER-CGP-CONSTANTS //////////////////////////
  3055. //////////////////////////////// END INCLUDES ////////////////////////////////
  3056. /////////////////////////////// FIXED SETTINGS ///////////////////////////////
  3057. // Avoid dividing by zero; using a macro overloads for float, float2, etc.:
  3058. #define FIX_ZERO(c) (max(abs(c), 0.0000152587890625)) // 2^-16
  3059. // Ensure the first pass decodes CRT gamma and the last encodes LCD gamma.
  3060. #ifndef SIMULATE_CRT_ON_LCD
  3061. #define SIMULATE_CRT_ON_LCD
  3062. #endif
  3063. // Manually tiling a manually resized texture creates texture coord derivative
  3064. // discontinuities and confuses anisotropic filtering, causing discolored tile
  3065. // seams in the phosphor mask. Workarounds:
  3066. // a.) Using tex2Dlod disables anisotropic filtering for tiled masks. It's
  3067. // downgraded to tex2Dbias without DRIVERS_ALLOW_TEX2DLOD #defined and
  3068. // disabled without DRIVERS_ALLOW_TEX2DBIAS #defined either.
  3069. // b.) "Tile flat twice" requires drawing two full tiles without border padding
  3070. // to the resized mask FBO, and it's incompatible with same-pass curvature.
  3071. // (Same-pass curvature isn't used but could be in the future...maybe.)
  3072. // c.) "Fix discontinuities" requires derivatives and drawing one tile with
  3073. // border padding to the resized mask FBO, but it works with same-pass
  3074. // curvature. It's disabled without DRIVERS_ALLOW_DERIVATIVES #defined.
  3075. // Precedence: a, then, b, then c (if multiple strategies are #defined).
  3076. #define ANISOTROPIC_TILING_COMPAT_TEX2DLOD // 129.7 FPS, 4x, flat; 101.8 at fullscreen
  3077. #define ANISOTROPIC_TILING_COMPAT_TILE_FLAT_TWICE // 128.1 FPS, 4x, flat; 101.5 at fullscreen
  3078. #define ANISOTROPIC_TILING_COMPAT_FIX_DISCONTINUITIES // 124.4 FPS, 4x, flat; 97.4 at fullscreen
  3079. // Also, manually resampling the phosphor mask is slightly blurrier with
  3080. // anisotropic filtering. (Resampling with mipmapping is even worse: It
  3081. // creates artifacts, but only with the fully bloomed shader.) The difference
  3082. // is subtle with small triads, but you can fix it for a small cost.
  3083. //#define ANISOTROPIC_RESAMPLING_COMPAT_TEX2DLOD
  3084. ////////////////////////////// DERIVED SETTINGS //////////////////////////////
  3085. // Intel HD 4000 GPU's can't handle manual mask resizing (for now), setting the
  3086. // geometry mode at runtime, or a 4x4 true Gaussian resize. Disable
  3087. // incompatible settings ASAP. (INTEGRATED_GRAPHICS_COMPATIBILITY_MODE may be
  3088. // #defined by either user-settings.h or a wrapper .cg that #includes the
  3089. // current .cg pass.)
  3090. #ifdef INTEGRATED_GRAPHICS_COMPATIBILITY_MODE
  3091. #ifdef PHOSPHOR_MASK_MANUALLY_RESIZE
  3092. #undef PHOSPHOR_MASK_MANUALLY_RESIZE
  3093. #endif
  3094. #ifdef RUNTIME_GEOMETRY_MODE
  3095. #undef RUNTIME_GEOMETRY_MODE
  3096. #endif
  3097. // Mode 2 (4x4 Gaussian resize) won't work, and mode 1 (3x3 blur) is
  3098. // inferior in most cases, so replace 2.0 with 0.0:
  3099. static const float bloom_approx_filter =
  3100. bloom_approx_filter_static > 1.5 ? 0.0 : bloom_approx_filter_static;
  3101. #else
  3102. static const float bloom_approx_filter = bloom_approx_filter_static;
  3103. #endif
  3104. // Disable slow runtime paths if static parameters are used. Most of these
  3105. // won't be a problem anyway once the params are disabled, but some will.
  3106. #ifndef RUNTIME_SHADER_PARAMS_ENABLE
  3107. #ifdef RUNTIME_PHOSPHOR_BLOOM_SIGMA
  3108. #undef RUNTIME_PHOSPHOR_BLOOM_SIGMA
  3109. #endif
  3110. #ifdef RUNTIME_ANTIALIAS_WEIGHTS
  3111. #undef RUNTIME_ANTIALIAS_WEIGHTS
  3112. #endif
  3113. #ifdef RUNTIME_ANTIALIAS_SUBPIXEL_OFFSETS
  3114. #undef RUNTIME_ANTIALIAS_SUBPIXEL_OFFSETS
  3115. #endif
  3116. #ifdef RUNTIME_SCANLINES_HORIZ_FILTER_COLORSPACE
  3117. #undef RUNTIME_SCANLINES_HORIZ_FILTER_COLORSPACE
  3118. #endif
  3119. #ifdef RUNTIME_GEOMETRY_TILT
  3120. #undef RUNTIME_GEOMETRY_TILT
  3121. #endif
  3122. #ifdef RUNTIME_GEOMETRY_MODE
  3123. #undef RUNTIME_GEOMETRY_MODE
  3124. #endif
  3125. #ifdef FORCE_RUNTIME_PHOSPHOR_MASK_MODE_TYPE_SELECT
  3126. #undef FORCE_RUNTIME_PHOSPHOR_MASK_MODE_TYPE_SELECT
  3127. #endif
  3128. #endif
  3129. // Make tex2Dbias a backup for tex2Dlod for wider compatibility.
  3130. #ifdef ANISOTROPIC_TILING_COMPAT_TEX2DLOD
  3131. #define ANISOTROPIC_TILING_COMPAT_TEX2DBIAS
  3132. #endif
  3133. #ifdef ANISOTROPIC_RESAMPLING_COMPAT_TEX2DLOD
  3134. #define ANISOTROPIC_RESAMPLING_COMPAT_TEX2DBIAS
  3135. #endif
  3136. // Rule out unavailable anisotropic compatibility strategies:
  3137. #ifndef DRIVERS_ALLOW_DERIVATIVES
  3138. #ifdef ANISOTROPIC_TILING_COMPAT_FIX_DISCONTINUITIES
  3139. #undef ANISOTROPIC_TILING_COMPAT_FIX_DISCONTINUITIES
  3140. #endif
  3141. #endif
  3142. #ifndef DRIVERS_ALLOW_TEX2DLOD
  3143. #ifdef ANISOTROPIC_TILING_COMPAT_TEX2DLOD
  3144. #undef ANISOTROPIC_TILING_COMPAT_TEX2DLOD
  3145. #endif
  3146. #ifdef ANISOTROPIC_RESAMPLING_COMPAT_TEX2DLOD
  3147. #undef ANISOTROPIC_RESAMPLING_COMPAT_TEX2DLOD
  3148. #endif
  3149. #ifdef ANTIALIAS_DISABLE_ANISOTROPIC
  3150. #undef ANTIALIAS_DISABLE_ANISOTROPIC
  3151. #endif
  3152. #endif
  3153. #ifndef DRIVERS_ALLOW_TEX2DBIAS
  3154. #ifdef ANISOTROPIC_TILING_COMPAT_TEX2DBIAS
  3155. #undef ANISOTROPIC_TILING_COMPAT_TEX2DBIAS
  3156. #endif
  3157. #ifdef ANISOTROPIC_RESAMPLING_COMPAT_TEX2DBIAS
  3158. #undef ANISOTROPIC_RESAMPLING_COMPAT_TEX2DBIAS
  3159. #endif
  3160. #endif
  3161. // Prioritize anisotropic tiling compatibility strategies by performance and
  3162. // disable unused strategies. This concentrates all the nesting in one place.
  3163. #ifdef ANISOTROPIC_TILING_COMPAT_TEX2DLOD
  3164. #ifdef ANISOTROPIC_TILING_COMPAT_TEX2DBIAS
  3165. #undef ANISOTROPIC_TILING_COMPAT_TEX2DBIAS
  3166. #endif
  3167. #ifdef ANISOTROPIC_TILING_COMPAT_TILE_FLAT_TWICE
  3168. #undef ANISOTROPIC_TILING_COMPAT_TILE_FLAT_TWICE
  3169. #endif
  3170. #ifdef ANISOTROPIC_TILING_COMPAT_FIX_DISCONTINUITIES
  3171. #undef ANISOTROPIC_TILING_COMPAT_FIX_DISCONTINUITIES
  3172. #endif
  3173. #else
  3174. #ifdef ANISOTROPIC_TILING_COMPAT_TEX2DBIAS
  3175. #ifdef ANISOTROPIC_TILING_COMPAT_TILE_FLAT_TWICE
  3176. #undef ANISOTROPIC_TILING_COMPAT_TILE_FLAT_TWICE
  3177. #endif
  3178. #ifdef ANISOTROPIC_TILING_COMPAT_FIX_DISCONTINUITIES
  3179. #undef ANISOTROPIC_TILING_COMPAT_FIX_DISCONTINUITIES
  3180. #endif
  3181. #else
  3182. // ANISOTROPIC_TILING_COMPAT_TILE_FLAT_TWICE is only compatible with
  3183. // flat texture coords in the same pass, but that's all we use.
  3184. #ifdef ANISOTROPIC_TILING_COMPAT_TILE_FLAT_TWICE
  3185. #ifdef ANISOTROPIC_TILING_COMPAT_FIX_DISCONTINUITIES
  3186. #undef ANISOTROPIC_TILING_COMPAT_FIX_DISCONTINUITIES
  3187. #endif
  3188. #endif
  3189. #endif
  3190. #endif
  3191. // The tex2Dlod and tex2Dbias strategies share a lot in common, and we can
  3192. // reduce some #ifdef nesting in the next section by essentially OR'ing them:
  3193. #ifdef ANISOTROPIC_TILING_COMPAT_TEX2DLOD
  3194. #define ANISOTROPIC_TILING_COMPAT_TEX2DLOD_FAMILY
  3195. #endif
  3196. #ifdef ANISOTROPIC_TILING_COMPAT_TEX2DBIAS
  3197. #define ANISOTROPIC_TILING_COMPAT_TEX2DLOD_FAMILY
  3198. #endif
  3199. // Prioritize anisotropic resampling compatibility strategies the same way:
  3200. #ifdef ANISOTROPIC_RESAMPLING_COMPAT_TEX2DLOD
  3201. #ifdef ANISOTROPIC_RESAMPLING_COMPAT_TEX2DBIAS
  3202. #undef ANISOTROPIC_RESAMPLING_COMPAT_TEX2DBIAS
  3203. #endif
  3204. #endif
  3205. /////////////////////// DERIVED PHOSPHOR MASK CONSTANTS //////////////////////
  3206. // If we can use the large mipmapped LUT without mipmapping artifacts, we
  3207. // should: It gives us more options for using fewer samples.
  3208. #ifdef DRIVERS_ALLOW_TEX2DLOD
  3209. #ifdef ANISOTROPIC_RESAMPLING_COMPAT_TEX2DLOD
  3210. // TODO: Take advantage of this!
  3211. #define PHOSPHOR_MASK_RESIZE_MIPMAPPED_LUT
  3212. static const float2 mask_resize_src_lut_size = mask_texture_large_size;
  3213. #else
  3214. static const float2 mask_resize_src_lut_size = mask_texture_small_size;
  3215. #endif
  3216. #else
  3217. static const float2 mask_resize_src_lut_size = mask_texture_small_size;
  3218. #endif
  3219. // tex2D's sampler2D parameter MUST be a uniform global, a uniform input to
  3220. // main_fragment, or a static alias of one of the above. This makes it hard
  3221. // to select the phosphor mask at runtime: We can't even assign to a uniform
  3222. // global in the vertex shader or select a sampler2D in the vertex shader and
  3223. // pass it to the fragment shader (even with explicit TEXUNIT# bindings),
  3224. // because it just gives us the input texture or a black screen. However, we
  3225. // can get around these limitations by calling tex2D three times with different
  3226. // uniform samplers (or resizing the phosphor mask three times altogether).
  3227. // With dynamic branches, we can process only one of these branches on top of
  3228. // quickly discarding fragments we don't need (cgc seems able to overcome
  3229. // limigations around dependent texture fetches inside of branches). Without
  3230. // dynamic branches, we have to process every branch for every fragment...which
  3231. // is slower. Runtime sampling mode selection is slower without dynamic
  3232. // branches as well. Let the user's static #defines decide if it's worth it.
  3233. #ifdef DRIVERS_ALLOW_DYNAMIC_BRANCHES
  3234. #define RUNTIME_PHOSPHOR_MASK_MODE_TYPE_SELECT
  3235. #else
  3236. #ifdef FORCE_RUNTIME_PHOSPHOR_MASK_MODE_TYPE_SELECT
  3237. #define RUNTIME_PHOSPHOR_MASK_MODE_TYPE_SELECT
  3238. #endif
  3239. #endif
  3240. // We need to render some minimum number of tiles in the resize passes.
  3241. // We need at least 1.0 just to repeat a single tile, and we need extra
  3242. // padding beyond that for anisotropic filtering, discontinuitity fixing,
  3243. // antialiasing, same-pass curvature (not currently used), etc. First
  3244. // determine how many border texels and tiles we need, based on how the result
  3245. // will be sampled:
  3246. #ifdef GEOMETRY_EARLY
  3247. static const float max_subpixel_offset = aa_subpixel_r_offset_static.x;
  3248. // Most antialiasing filters have a base radius of 4.0 pixels:
  3249. static const float max_aa_base_pixel_border = 4.0 +
  3250. max_subpixel_offset;
  3251. #else
  3252. static const float max_aa_base_pixel_border = 0.0;
  3253. #endif
  3254. // Anisotropic filtering adds about 0.5 to the pixel border:
  3255. #ifndef ANISOTROPIC_TILING_COMPAT_TEX2DLOD_FAMILY
  3256. static const float max_aniso_pixel_border = max_aa_base_pixel_border + 0.5;
  3257. #else
  3258. static const float max_aniso_pixel_border = max_aa_base_pixel_border;
  3259. #endif
  3260. // Fixing discontinuities adds 1.0 more to the pixel border:
  3261. #ifdef ANISOTROPIC_TILING_COMPAT_FIX_DISCONTINUITIES
  3262. static const float max_tiled_pixel_border = max_aniso_pixel_border + 1.0;
  3263. #else
  3264. static const float max_tiled_pixel_border = max_aniso_pixel_border;
  3265. #endif
  3266. // Convert the pixel border to an integer texel border. Assume same-pass
  3267. // curvature about triples the texel frequency:
  3268. #ifdef GEOMETRY_EARLY
  3269. static const float max_mask_texel_border =
  3270. ceil(max_tiled_pixel_border * 3.0);
  3271. #else
  3272. static const float max_mask_texel_border = ceil(max_tiled_pixel_border);
  3273. #endif
  3274. // Convert the texel border to a tile border using worst-case assumptions:
  3275. static const float max_mask_tile_border = max_mask_texel_border/
  3276. (mask_min_allowed_triad_size * mask_triads_per_tile);
  3277. // Finally, set the number of resized tiles to render to MASK_RESIZE, and set
  3278. // the starting texel (inside borders) for sampling it.
  3279. #ifndef GEOMETRY_EARLY
  3280. #ifdef ANISOTROPIC_TILING_COMPAT_TILE_FLAT_TWICE
  3281. // Special case: Render two tiles without borders. Anisotropic
  3282. // filtering doesn't seem to be a problem here.
  3283. static const float mask_resize_num_tiles = 1.0 + 1.0;
  3284. static const float mask_start_texels = 0.0;
  3285. #else
  3286. static const float mask_resize_num_tiles = 1.0 +
  3287. 2.0 * max_mask_tile_border;
  3288. static const float mask_start_texels = max_mask_texel_border;
  3289. #endif
  3290. #else
  3291. static const float mask_resize_num_tiles = 1.0 + 2.0*max_mask_tile_border;
  3292. static const float mask_start_texels = max_mask_texel_border;
  3293. #endif
  3294. // We have to fit mask_resize_num_tiles into an FBO with a viewport scale of
  3295. // mask_resize_viewport_scale. This limits the maximum final triad size.
  3296. // Estimate the minimum number of triads we can split the screen into in each
  3297. // dimension (we'll be as correct as mask_resize_viewport_scale is):
  3298. static const float mask_resize_num_triads =
  3299. mask_resize_num_tiles * mask_triads_per_tile;
  3300. static const float2 min_allowed_viewport_triads =
  3301. float2(mask_resize_num_triads) / mask_resize_viewport_scale;
  3302. //////////////////////// COMMON MATHEMATICAL CONSTANTS ///////////////////////
  3303. static const float pi = 3.141592653589;
  3304. // We often want to find the location of the previous texel, e.g.:
  3305. // const float2 curr_texel = uv * texture_size;
  3306. // const float2 prev_texel = floor(curr_texel - float2(0.5)) + float2(0.5);
  3307. // const float2 prev_texel_uv = prev_texel / texture_size;
  3308. // However, many GPU drivers round incorrectly around exact texel locations.
  3309. // We need to subtract a little less than 0.5 before flooring, and some GPU's
  3310. // require this value to be farther from 0.5 than others; define it here.
  3311. // const float2 prev_texel =
  3312. // floor(curr_texel - float2(under_half)) + float2(0.5);
  3313. static const float under_half = 0.4995;
  3314. #endif // DERIVED_SETTINGS_AND_CONSTANTS_H
  3315. ///////////////////////////// END DERIVED-SETTINGS-AND-CONSTANTS ////////////////////////////
  3316. //#include "../../../../include/special-functions.h"
  3317. /////////////////////////// BEGIN SPECIAL-FUNCTIONS //////////////////////////
  3318. #ifndef SPECIAL_FUNCTIONS_H
  3319. #define SPECIAL_FUNCTIONS_H
  3320. ///////////////////////////////// MIT LICENSE ////////////////////////////////
  3321. // Copyright (C) 2014 TroggleMonkey
  3322. //
  3323. // Permission is hereby granted, free of charge, to any person obtaining a copy
  3324. // of this software and associated documentation files (the "Software"), to
  3325. // deal in the Software without restriction, including without limitation the
  3326. // rights to use, copy, modify, merge, publish, distribute, sublicense, and/or
  3327. // sell copies of the Software, and to permit persons to whom the Software is
  3328. // furnished to do so, subject to the following conditions:
  3329. //
  3330. // The above copyright notice and this permission notice shall be included in
  3331. // all copies or substantial portions of the Software.
  3332. //
  3333. // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  3334. // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  3335. // FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  3336. // AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  3337. // LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
  3338. // FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
  3339. // IN THE SOFTWARE.
  3340. ///////////////////////////////// DESCRIPTION ////////////////////////////////
  3341. // This file implements the following mathematical special functions:
  3342. // 1.) erf() = 2/sqrt(pi) * indefinite_integral(e**(-x**2))
  3343. // 2.) gamma(s), a real-numbered extension of the integer factorial function
  3344. // It also implements normalized_ligamma(s, z), a normalized lower incomplete
  3345. // gamma function for s < 0.5 only. Both gamma() and normalized_ligamma() can
  3346. // be called with an _impl suffix to use an implementation version with a few
  3347. // extra precomputed parameters (which may be useful for the caller to reuse).
  3348. // See below for details.
  3349. //
  3350. // Design Rationale:
  3351. // Pretty much every line of code in this file is duplicated four times for
  3352. // different input types (float4/float3/float2/float). This is unfortunate,
  3353. // but Cg doesn't allow function templates. Macros would be far less verbose,
  3354. // but they would make the code harder to document and read. I don't expect
  3355. // these functions will require a whole lot of maintenance changes unless
  3356. // someone ever has need for more robust incomplete gamma functions, so code
  3357. // duplication seems to be the lesser evil in this case.
  3358. /////////////////////////// GAUSSIAN ERROR FUNCTION //////////////////////////
  3359. float4 erf6(float4 x)
  3360. {
  3361. // Requires: x is the standard parameter to erf().
  3362. // Returns: Return an Abramowitz/Stegun approximation of erf(), where:
  3363. // erf(x) = 2/sqrt(pi) * integral(e**(-x**2))
  3364. // This approximation has a max absolute error of 2.5*10**-5
  3365. // with solid numerical robustness and efficiency. See:
  3366. // https://en.wikipedia.org/wiki/Error_function#Approximation_with_elementary_functions
  3367. static const float4 one = float4(1.0);
  3368. const float4 sign_x = sign(x);
  3369. const float4 t = one/(one + 0.47047*abs(x));
  3370. const float4 result = one - t*(0.3480242 + t*(-0.0958798 + t*0.7478556))*
  3371. exp(-(x*x));
  3372. return result * sign_x;
  3373. }
  3374. float3 erf6(const float3 x)
  3375. {
  3376. // Float3 version:
  3377. static const float3 one = float3(1.0);
  3378. const float3 sign_x = sign(x);
  3379. const float3 t = one/(one + 0.47047*abs(x));
  3380. const float3 result = one - t*(0.3480242 + t*(-0.0958798 + t*0.7478556))*
  3381. exp(-(x*x));
  3382. return result * sign_x;
  3383. }
  3384. float2 erf6(const float2 x)
  3385. {
  3386. // Float2 version:
  3387. static const float2 one = float2(1.0);
  3388. const float2 sign_x = sign(x);
  3389. const float2 t = one/(one + 0.47047*abs(x));
  3390. const float2 result = one - t*(0.3480242 + t*(-0.0958798 + t*0.7478556))*
  3391. exp(-(x*x));
  3392. return result * sign_x;
  3393. }
  3394. float erf6(const float x)
  3395. {
  3396. // Float version:
  3397. const float sign_x = sign(x);
  3398. const float t = 1.0/(1.0 + 0.47047*abs(x));
  3399. const float result = 1.0 - t*(0.3480242 + t*(-0.0958798 + t*0.7478556))*
  3400. exp(-(x*x));
  3401. return result * sign_x;
  3402. }
  3403. float4 erft(const float4 x)
  3404. {
  3405. // Requires: x is the standard parameter to erf().
  3406. // Returns: Approximate erf() with the hyperbolic tangent. The error is
  3407. // visually noticeable, but it's blazing fast and perceptually
  3408. // close...at least on ATI hardware. See:
  3409. // http://www.maplesoft.com/applications/view.aspx?SID=5525&view=html
  3410. // Warning: Only use this if your hardware drivers correctly implement
  3411. // tanh(): My nVidia 8800GTS returns garbage output.
  3412. return tanh(1.202760580 * x);
  3413. }
  3414. float3 erft(const float3 x)
  3415. {
  3416. // Float3 version:
  3417. return tanh(1.202760580 * x);
  3418. }
  3419. float2 erft(const float2 x)
  3420. {
  3421. // Float2 version:
  3422. return tanh(1.202760580 * x);
  3423. }
  3424. float erft(const float x)
  3425. {
  3426. // Float version:
  3427. return tanh(1.202760580 * x);
  3428. }
  3429. inline float4 erf(const float4 x)
  3430. {
  3431. // Requires: x is the standard parameter to erf().
  3432. // Returns: Some approximation of erf(x), depending on user settings.
  3433. #ifdef ERF_FAST_APPROXIMATION
  3434. return erft(x);
  3435. #else
  3436. return erf6(x);
  3437. #endif
  3438. }
  3439. inline float3 erf(const float3 x)
  3440. {
  3441. // Float3 version:
  3442. #ifdef ERF_FAST_APPROXIMATION
  3443. return erft(x);
  3444. #else
  3445. return erf6(x);
  3446. #endif
  3447. }
  3448. inline float2 erf(const float2 x)
  3449. {
  3450. // Float2 version:
  3451. #ifdef ERF_FAST_APPROXIMATION
  3452. return erft(x);
  3453. #else
  3454. return erf6(x);
  3455. #endif
  3456. }
  3457. inline float erf(const float x)
  3458. {
  3459. // Float version:
  3460. #ifdef ERF_FAST_APPROXIMATION
  3461. return erft(x);
  3462. #else
  3463. return erf6(x);
  3464. #endif
  3465. }
  3466. /////////////////////////// COMPLETE GAMMA FUNCTION //////////////////////////
  3467. float4 gamma_impl(const float4 s, const float4 s_inv)
  3468. {
  3469. // Requires: 1.) s is the standard parameter to the gamma function, and
  3470. // it should lie in the [0, 36] range.
  3471. // 2.) s_inv = 1.0/s. This implementation function requires
  3472. // the caller to precompute this value, giving users the
  3473. // opportunity to reuse it.
  3474. // Returns: Return approximate gamma function (real-numbered factorial)
  3475. // output using the Lanczos approximation with two coefficients
  3476. // calculated using Paul Godfrey's method here:
  3477. // http://my.fit.edu/~gabdo/gamma.txt
  3478. // An optimal g value for s in [0, 36] is ~1.12906830989, with
  3479. // a maximum relative error of 0.000463 for 2**16 equally
  3480. // evals. We could use three coeffs (0.0000346 error) without
  3481. // hurting latency, but this allows more parallelism with
  3482. // outside instructions.
  3483. static const float4 g = float4(1.12906830989);
  3484. static const float4 c0 = float4(0.8109119309638332633713423362694399653724431);
  3485. static const float4 c1 = float4(0.4808354605142681877121661197951496120000040);
  3486. static const float4 e = float4(2.71828182845904523536028747135266249775724709);
  3487. const float4 sph = s + float4(0.5);
  3488. const float4 lanczos_sum = c0 + c1/(s + float4(1.0));
  3489. const float4 base = (sph + g)/e; // or (s + g + float4(0.5))/e
  3490. // gamma(s + 1) = base**sph * lanczos_sum; divide by s for gamma(s).
  3491. // This has less error for small s's than (s -= 1.0) at the beginning.
  3492. return (pow(base, sph) * lanczos_sum) * s_inv;
  3493. }
  3494. float3 gamma_impl(const float3 s, const float3 s_inv)
  3495. {
  3496. // Float3 version:
  3497. static const float3 g = float3(1.12906830989);
  3498. static const float3 c0 = float3(0.8109119309638332633713423362694399653724431);
  3499. static const float3 c1 = float3(0.4808354605142681877121661197951496120000040);
  3500. static const float3 e = float3(2.71828182845904523536028747135266249775724709);
  3501. const float3 sph = s + float3(0.5);
  3502. const float3 lanczos_sum = c0 + c1/(s + float3(1.0));
  3503. const float3 base = (sph + g)/e;
  3504. return (pow(base, sph) * lanczos_sum) * s_inv;
  3505. }
  3506. float2 gamma_impl(const float2 s, const float2 s_inv)
  3507. {
  3508. // Float2 version:
  3509. static const float2 g = float2(1.12906830989);
  3510. static const float2 c0 = float2(0.8109119309638332633713423362694399653724431);
  3511. static const float2 c1 = float2(0.4808354605142681877121661197951496120000040);
  3512. static const float2 e = float2(2.71828182845904523536028747135266249775724709);
  3513. const float2 sph = s + float2(0.5);
  3514. const float2 lanczos_sum = c0 + c1/(s + float2(1.0));
  3515. const float2 base = (sph + g)/e;
  3516. return (pow(base, sph) * lanczos_sum) * s_inv;
  3517. }
  3518. float gamma_impl(const float s, const float s_inv)
  3519. {
  3520. // Float version:
  3521. static const float g = 1.12906830989;
  3522. static const float c0 = 0.8109119309638332633713423362694399653724431;
  3523. static const float c1 = 0.4808354605142681877121661197951496120000040;
  3524. static const float e = 2.71828182845904523536028747135266249775724709;
  3525. const float sph = s + 0.5;
  3526. const float lanczos_sum = c0 + c1/(s + 1.0);
  3527. const float base = (sph + g)/e;
  3528. return (pow(base, sph) * lanczos_sum) * s_inv;
  3529. }
  3530. float4 gamma(const float4 s)
  3531. {
  3532. // Requires: s is the standard parameter to the gamma function, and it
  3533. // should lie in the [0, 36] range.
  3534. // Returns: Return approximate gamma function output with a maximum
  3535. // relative error of 0.000463. See gamma_impl for details.
  3536. return gamma_impl(s, float4(1.0)/s);
  3537. }
  3538. float3 gamma(const float3 s)
  3539. {
  3540. // Float3 version:
  3541. return gamma_impl(s, float3(1.0)/s);
  3542. }
  3543. float2 gamma(const float2 s)
  3544. {
  3545. // Float2 version:
  3546. return gamma_impl(s, float2(1.0)/s);
  3547. }
  3548. float gamma(const float s)
  3549. {
  3550. // Float version:
  3551. return gamma_impl(s, 1.0/s);
  3552. }
  3553. //////////////// INCOMPLETE GAMMA FUNCTIONS (RESTRICTED INPUT) ///////////////
  3554. // Lower incomplete gamma function for small s and z (implementation):
  3555. float4 ligamma_small_z_impl(const float4 s, const float4 z, const float4 s_inv)
  3556. {
  3557. // Requires: 1.) s < ~0.5
  3558. // 2.) z <= ~0.775075
  3559. // 3.) s_inv = 1.0/s (precomputed for outside reuse)
  3560. // Returns: A series representation for the lower incomplete gamma
  3561. // function for small s and small z (4 terms).
  3562. // The actual "rolled up" summation looks like:
  3563. // last_sign = 1.0; last_pow = 1.0; last_factorial = 1.0;
  3564. // sum = last_sign * last_pow / ((s + k) * last_factorial)
  3565. // for(int i = 0; i < 4; ++i)
  3566. // {
  3567. // last_sign *= -1.0; last_pow *= z; last_factorial *= i;
  3568. // sum += last_sign * last_pow / ((s + k) * last_factorial);
  3569. // }
  3570. // Unrolled, constant-unfolded and arranged for madds and parallelism:
  3571. const float4 scale = pow(z, s);
  3572. float4 sum = s_inv; // Summation iteration 0 result
  3573. // Summation iterations 1, 2, and 3:
  3574. const float4 z_sq = z*z;
  3575. const float4 denom1 = s + float4(1.0);
  3576. const float4 denom2 = 2.0*s + float4(4.0);
  3577. const float4 denom3 = 6.0*s + float4(18.0);
  3578. //float4 denom4 = 24.0*s + float4(96.0);
  3579. sum -= z/denom1;
  3580. sum += z_sq/denom2;
  3581. sum -= z * z_sq/denom3;
  3582. //sum += z_sq * z_sq / denom4;
  3583. // Scale and return:
  3584. return scale * sum;
  3585. }
  3586. float3 ligamma_small_z_impl(const float3 s, const float3 z, const float3 s_inv)
  3587. {
  3588. // Float3 version:
  3589. const float3 scale = pow(z, s);
  3590. float3 sum = s_inv;
  3591. const float3 z_sq = z*z;
  3592. const float3 denom1 = s + float3(1.0);
  3593. const float3 denom2 = 2.0*s + float3(4.0);
  3594. const float3 denom3 = 6.0*s + float3(18.0);
  3595. sum -= z/denom1;
  3596. sum += z_sq/denom2;
  3597. sum -= z * z_sq/denom3;
  3598. return scale * sum;
  3599. }
  3600. float2 ligamma_small_z_impl(const float2 s, const float2 z, const float2 s_inv)
  3601. {
  3602. // Float2 version:
  3603. const float2 scale = pow(z, s);
  3604. float2 sum = s_inv;
  3605. const float2 z_sq = z*z;
  3606. const float2 denom1 = s + float2(1.0);
  3607. const float2 denom2 = 2.0*s + float2(4.0);
  3608. const float2 denom3 = 6.0*s + float2(18.0);
  3609. sum -= z/denom1;
  3610. sum += z_sq/denom2;
  3611. sum -= z * z_sq/denom3;
  3612. return scale * sum;
  3613. }
  3614. float ligamma_small_z_impl(const float s, const float z, const float s_inv)
  3615. {
  3616. // Float version:
  3617. const float scale = pow(z, s);
  3618. float sum = s_inv;
  3619. const float z_sq = z*z;
  3620. const float denom1 = s + 1.0;
  3621. const float denom2 = 2.0*s + 4.0;
  3622. const float denom3 = 6.0*s + 18.0;
  3623. sum -= z/denom1;
  3624. sum += z_sq/denom2;
  3625. sum -= z * z_sq/denom3;
  3626. return scale * sum;
  3627. }
  3628. // Upper incomplete gamma function for small s and large z (implementation):
  3629. float4 uigamma_large_z_impl(const float4 s, const float4 z)
  3630. {
  3631. // Requires: 1.) s < ~0.5
  3632. // 2.) z > ~0.775075
  3633. // Returns: Gauss's continued fraction representation for the upper
  3634. // incomplete gamma function (4 terms).
  3635. // The "rolled up" continued fraction looks like this. The denominator
  3636. // is truncated, and it's calculated "from the bottom up:"
  3637. // denom = float4('inf');
  3638. // float4 one = float4(1.0);
  3639. // for(int i = 4; i > 0; --i)
  3640. // {
  3641. // denom = ((i * 2.0) - one) + z - s + (i * (s - i))/denom;
  3642. // }
  3643. // Unrolled and constant-unfolded for madds and parallelism:
  3644. const float4 numerator = pow(z, s) * exp(-z);
  3645. float4 denom = float4(7.0) + z - s;
  3646. denom = float4(5.0) + z - s + (3.0*s - float4(9.0))/denom;
  3647. denom = float4(3.0) + z - s + (2.0*s - float4(4.0))/denom;
  3648. denom = float4(1.0) + z - s + (s - float4(1.0))/denom;
  3649. return numerator / denom;
  3650. }
  3651. float3 uigamma_large_z_impl(const float3 s, const float3 z)
  3652. {
  3653. // Float3 version:
  3654. const float3 numerator = pow(z, s) * exp(-z);
  3655. float3 denom = float3(7.0) + z - s;
  3656. denom = float3(5.0) + z - s + (3.0*s - float3(9.0))/denom;
  3657. denom = float3(3.0) + z - s + (2.0*s - float3(4.0))/denom;
  3658. denom = float3(1.0) + z - s + (s - float3(1.0))/denom;
  3659. return numerator / denom;
  3660. }
  3661. float2 uigamma_large_z_impl(const float2 s, const float2 z)
  3662. {
  3663. // Float2 version:
  3664. const float2 numerator = pow(z, s) * exp(-z);
  3665. float2 denom = float2(7.0) + z - s;
  3666. denom = float2(5.0) + z - s + (3.0*s - float2(9.0))/denom;
  3667. denom = float2(3.0) + z - s + (2.0*s - float2(4.0))/denom;
  3668. denom = float2(1.0) + z - s + (s - float2(1.0))/denom;
  3669. return numerator / denom;
  3670. }
  3671. float uigamma_large_z_impl(const float s, const float z)
  3672. {
  3673. // Float version:
  3674. const float numerator = pow(z, s) * exp(-z);
  3675. float denom = 7.0 + z - s;
  3676. denom = 5.0 + z - s + (3.0*s - 9.0)/denom;
  3677. denom = 3.0 + z - s + (2.0*s - 4.0)/denom;
  3678. denom = 1.0 + z - s + (s - 1.0)/denom;
  3679. return numerator / denom;
  3680. }
  3681. // Normalized lower incomplete gamma function for small s (implementation):
  3682. float4 normalized_ligamma_impl(const float4 s, const float4 z,
  3683. const float4 s_inv, const float4 gamma_s_inv)
  3684. {
  3685. // Requires: 1.) s < ~0.5
  3686. // 2.) s_inv = 1/s (precomputed for outside reuse)
  3687. // 3.) gamma_s_inv = 1/gamma(s) (precomputed for outside reuse)
  3688. // Returns: Approximate the normalized lower incomplete gamma function
  3689. // for s < 0.5. Since we only care about s < 0.5, we only need
  3690. // to evaluate two branches (not four) based on z. Each branch
  3691. // uses four terms, with a max relative error of ~0.00182. The
  3692. // branch threshold and specifics were adapted for fewer terms
  3693. // from Gil/Segura/Temme's paper here:
  3694. // http://oai.cwi.nl/oai/asset/20433/20433B.pdf
  3695. // Evaluate both branches: Real branches test slower even when available.
  3696. static const float4 thresh = float4(0.775075);
  3697. bool4 z_is_large;
  3698. z_is_large.x = z.x > thresh.x;
  3699. z_is_large.y = z.y > thresh.y;
  3700. z_is_large.z = z.z > thresh.z;
  3701. z_is_large.w = z.w > thresh.w;
  3702. const float4 large_z = float4(1.0) - uigamma_large_z_impl(s, z) * gamma_s_inv;
  3703. const float4 small_z = ligamma_small_z_impl(s, z, s_inv) * gamma_s_inv;
  3704. // Combine the results from both branches:
  3705. bool4 inverse_z_is_large = not(z_is_large);
  3706. return large_z * float4(z_is_large) + small_z * float4(inverse_z_is_large);
  3707. }
  3708. float3 normalized_ligamma_impl(const float3 s, const float3 z,
  3709. const float3 s_inv, const float3 gamma_s_inv)
  3710. {
  3711. // Float3 version:
  3712. static const float3 thresh = float3(0.775075);
  3713. bool3 z_is_large;
  3714. z_is_large.x = z.x > thresh.x;
  3715. z_is_large.y = z.y > thresh.y;
  3716. z_is_large.z = z.z > thresh.z;
  3717. const float3 large_z = float3(1.0) - uigamma_large_z_impl(s, z) * gamma_s_inv;
  3718. const float3 small_z = ligamma_small_z_impl(s, z, s_inv) * gamma_s_inv;
  3719. bool3 inverse_z_is_large = not(z_is_large);
  3720. return large_z * float3(z_is_large) + small_z * float3(inverse_z_is_large);
  3721. }
  3722. float2 normalized_ligamma_impl(const float2 s, const float2 z,
  3723. const float2 s_inv, const float2 gamma_s_inv)
  3724. {
  3725. // Float2 version:
  3726. static const float2 thresh = float2(0.775075);
  3727. bool2 z_is_large;
  3728. z_is_large.x = z.x > thresh.x;
  3729. z_is_large.y = z.y > thresh.y;
  3730. const float2 large_z = float2(1.0) - uigamma_large_z_impl(s, z) * gamma_s_inv;
  3731. const float2 small_z = ligamma_small_z_impl(s, z, s_inv) * gamma_s_inv;
  3732. bool2 inverse_z_is_large = not(z_is_large);
  3733. return large_z * float2(z_is_large) + small_z * float2(inverse_z_is_large);
  3734. }
  3735. float normalized_ligamma_impl(const float s, const float z,
  3736. const float s_inv, const float gamma_s_inv)
  3737. {
  3738. // Float version:
  3739. static const float thresh = 0.775075;
  3740. const bool z_is_large = z > thresh;
  3741. const float large_z = 1.0 - uigamma_large_z_impl(s, z) * gamma_s_inv;
  3742. const float small_z = ligamma_small_z_impl(s, z, s_inv) * gamma_s_inv;
  3743. return large_z * float(z_is_large) + small_z * float(!z_is_large);
  3744. }
  3745. // Normalized lower incomplete gamma function for small s:
  3746. float4 normalized_ligamma(const float4 s, const float4 z)
  3747. {
  3748. // Requires: s < ~0.5
  3749. // Returns: Approximate the normalized lower incomplete gamma function
  3750. // for s < 0.5. See normalized_ligamma_impl() for details.
  3751. const float4 s_inv = float4(1.0)/s;
  3752. const float4 gamma_s_inv = float4(1.0)/gamma_impl(s, s_inv);
  3753. return normalized_ligamma_impl(s, z, s_inv, gamma_s_inv);
  3754. }
  3755. float3 normalized_ligamma(const float3 s, const float3 z)
  3756. {
  3757. // Float3 version:
  3758. const float3 s_inv = float3(1.0)/s;
  3759. const float3 gamma_s_inv = float3(1.0)/gamma_impl(s, s_inv);
  3760. return normalized_ligamma_impl(s, z, s_inv, gamma_s_inv);
  3761. }
  3762. float2 normalized_ligamma(const float2 s, const float2 z)
  3763. {
  3764. // Float2 version:
  3765. const float2 s_inv = float2(1.0)/s;
  3766. const float2 gamma_s_inv = float2(1.0)/gamma_impl(s, s_inv);
  3767. return normalized_ligamma_impl(s, z, s_inv, gamma_s_inv);
  3768. }
  3769. float normalized_ligamma(const float s, const float z)
  3770. {
  3771. // Float version:
  3772. const float s_inv = 1.0/s;
  3773. const float gamma_s_inv = 1.0/gamma_impl(s, s_inv);
  3774. return normalized_ligamma_impl(s, z, s_inv, gamma_s_inv);
  3775. }
  3776. #endif // SPECIAL_FUNCTIONS_H
  3777. //////////////////////////// END SPECIAL-FUNCTIONS ///////////////////////////
  3778. //#include "../../../../include/gamma-management.h"
  3779. //////////////////////////// BEGIN GAMMA-MANAGEMENT //////////////////////////
  3780. #ifndef GAMMA_MANAGEMENT_H
  3781. #define GAMMA_MANAGEMENT_H
  3782. ///////////////////////////////// MIT LICENSE ////////////////////////////////
  3783. // Copyright (C) 2014 TroggleMonkey
  3784. //
  3785. // Permission is hereby granted, free of charge, to any person obtaining a copy
  3786. // of this software and associated documentation files (the "Software"), to
  3787. // deal in the Software without restriction, including without limitation the
  3788. // rights to use, copy, modify, merge, publish, distribute, sublicense, and/or
  3789. // sell copies of the Software, and to permit persons to whom the Software is
  3790. // furnished to do so, subject to the following conditions:
  3791. //
  3792. // The above copyright notice and this permission notice shall be included in
  3793. // all copies or substantial portions of the Software.
  3794. //
  3795. // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  3796. // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  3797. // FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  3798. // AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  3799. // LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
  3800. // FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
  3801. // IN THE SOFTWARE.
  3802. ///////////////////////////////// DESCRIPTION ////////////////////////////////
  3803. // This file provides gamma-aware tex*D*() and encode_output() functions.
  3804. // Requires: Before #include-ing this file, the including file must #define
  3805. // the following macros when applicable and follow their rules:
  3806. // 1.) #define FIRST_PASS if this is the first pass.
  3807. // 2.) #define LAST_PASS if this is the last pass.
  3808. // 3.) If sRGB is available, set srgb_framebufferN = "true" for
  3809. // every pass except the last in your .cgp preset.
  3810. // 4.) If sRGB isn't available but you want gamma-correctness with
  3811. // no banding, #define GAMMA_ENCODE_EVERY_FBO each pass.
  3812. // 5.) #define SIMULATE_CRT_ON_LCD if desired (precedence over 5-7)
  3813. // 6.) #define SIMULATE_GBA_ON_LCD if desired (precedence over 6-7)
  3814. // 7.) #define SIMULATE_LCD_ON_CRT if desired (precedence over 7)
  3815. // 8.) #define SIMULATE_GBA_ON_CRT if desired (precedence over -)
  3816. // If an option in [5, 8] is #defined in the first or last pass, it
  3817. // should be #defined for both. It shouldn't make a difference
  3818. // whether it's #defined for intermediate passes or not.
  3819. // Optional: The including file (or an earlier included file) may optionally
  3820. // #define a number of macros indicating it will override certain
  3821. // macros and associated constants are as follows:
  3822. // static constants with either static or uniform constants. The
  3823. // 1.) OVERRIDE_STANDARD_GAMMA: The user must first define:
  3824. // static const float ntsc_gamma
  3825. // static const float pal_gamma
  3826. // static const float crt_reference_gamma_high
  3827. // static const float crt_reference_gamma_low
  3828. // static const float lcd_reference_gamma
  3829. // static const float crt_office_gamma
  3830. // static const float lcd_office_gamma
  3831. // 2.) OVERRIDE_DEVICE_GAMMA: The user must first define:
  3832. // static const float crt_gamma
  3833. // static const float gba_gamma
  3834. // static const float lcd_gamma
  3835. // 3.) OVERRIDE_FINAL_GAMMA: The user must first define:
  3836. // static const float input_gamma
  3837. // static const float intermediate_gamma
  3838. // static const float output_gamma
  3839. // (intermediate_gamma is for GAMMA_ENCODE_EVERY_FBO.)
  3840. // 4.) OVERRIDE_ALPHA_ASSUMPTIONS: The user must first define:
  3841. // static const bool assume_opaque_alpha
  3842. // The gamma constant overrides must be used in every pass or none,
  3843. // and OVERRIDE_FINAL_GAMMA bypasses all of the SIMULATE* macros.
  3844. // OVERRIDE_ALPHA_ASSUMPTIONS may be set on a per-pass basis.
  3845. // Usage: After setting macros appropriately, ignore gamma correction and
  3846. // replace all tex*D*() calls with equivalent gamma-aware
  3847. // tex*D*_linearize calls, except:
  3848. // 1.) When you read an LUT, use regular tex*D or a gamma-specified
  3849. // function, depending on its gamma encoding:
  3850. // tex*D*_linearize_gamma (takes a runtime gamma parameter)
  3851. // 2.) If you must read pass0's original input in a later pass, use
  3852. // tex2D_linearize_ntsc_gamma. If you want to read pass0's
  3853. // input with gamma-corrected bilinear filtering, consider
  3854. // creating a first linearizing pass and reading from the input
  3855. // of pass1 later.
  3856. // Then, return encode_output(color) from every fragment shader.
  3857. // Finally, use the global gamma_aware_bilinear boolean if you want
  3858. // to statically branch based on whether bilinear filtering is
  3859. // gamma-correct or not (e.g. for placing Gaussian blur samples).
  3860. //
  3861. // Detailed Policy:
  3862. // tex*D*_linearize() functions enforce a consistent gamma-management policy
  3863. // based on the FIRST_PASS and GAMMA_ENCODE_EVERY_FBO settings. They assume
  3864. // their input texture has the same encoding characteristics as the input for
  3865. // the current pass (which doesn't apply to the exceptions listed above).
  3866. // Similarly, encode_output() enforces a policy based on the LAST_PASS and
  3867. // GAMMA_ENCODE_EVERY_FBO settings. Together, they result in one of the
  3868. // following two pipelines.
  3869. // Typical pipeline with intermediate sRGB framebuffers:
  3870. // linear_color = pow(pass0_encoded_color, input_gamma);
  3871. // intermediate_output = linear_color; // Automatic sRGB encoding
  3872. // linear_color = intermediate_output; // Automatic sRGB decoding
  3873. // final_output = pow(intermediate_output, 1.0/output_gamma);
  3874. // Typical pipeline without intermediate sRGB framebuffers:
  3875. // linear_color = pow(pass0_encoded_color, input_gamma);
  3876. // intermediate_output = pow(linear_color, 1.0/intermediate_gamma);
  3877. // linear_color = pow(intermediate_output, intermediate_gamma);
  3878. // final_output = pow(intermediate_output, 1.0/output_gamma);
  3879. // Using GAMMA_ENCODE_EVERY_FBO is much slower, but it's provided as a way to
  3880. // easily get gamma-correctness without banding on devices where sRGB isn't
  3881. // supported.
  3882. //
  3883. // Use This Header to Maximize Code Reuse:
  3884. // The purpose of this header is to provide a consistent interface for texture
  3885. // reads and output gamma-encoding that localizes and abstracts away all the
  3886. // annoying details. This greatly reduces the amount of code in each shader
  3887. // pass that depends on the pass number in the .cgp preset or whether sRGB
  3888. // FBO's are being used: You can trivially change the gamma behavior of your
  3889. // whole pass by commenting or uncommenting 1-3 #defines. To reuse the same
  3890. // code in your first, Nth, and last passes, you can even put it all in another
  3891. // header file and #include it from skeleton .cg files that #define the
  3892. // appropriate pass-specific settings.
  3893. //
  3894. // Rationale for Using Three Macros:
  3895. // This file uses GAMMA_ENCODE_EVERY_FBO instead of an opposite macro like
  3896. // SRGB_PIPELINE to ensure sRGB is assumed by default, which hopefully imposes
  3897. // a lower maintenance burden on each pass. At first glance it seems we could
  3898. // accomplish everything with two macros: GAMMA_CORRECT_IN / GAMMA_CORRECT_OUT.
  3899. // This works for simple use cases where input_gamma == output_gamma, but it
  3900. // breaks down for more complex scenarios like CRT simulation, where the pass
  3901. // number determines the gamma encoding of the input and output.
  3902. /////////////////////////////// BASE CONSTANTS ///////////////////////////////
  3903. // Set standard gamma constants, but allow users to override them:
  3904. #ifndef OVERRIDE_STANDARD_GAMMA
  3905. // Standard encoding gammas:
  3906. static const float ntsc_gamma = 2.2; // Best to use NTSC for PAL too?
  3907. static const float pal_gamma = 2.8; // Never actually 2.8 in practice
  3908. // Typical device decoding gammas (only use for emulating devices):
  3909. // CRT/LCD reference gammas are higher than NTSC and Rec.709 video standard
  3910. // gammas: The standards purposely undercorrected for an analog CRT's
  3911. // assumed 2.5 reference display gamma to maintain contrast in assumed
  3912. // [dark] viewing conditions: http://www.poynton.com/PDFs/GammaFAQ.pdf
  3913. // These unstated assumptions about display gamma and perceptual rendering
  3914. // intent caused a lot of confusion, and more modern CRT's seemed to target
  3915. // NTSC 2.2 gamma with circuitry. LCD displays seem to have followed suit
  3916. // (they struggle near black with 2.5 gamma anyway), especially PC/laptop
  3917. // displays designed to view sRGB in bright environments. (Standards are
  3918. // also in flux again with BT.1886, but it's underspecified for displays.)
  3919. static const float crt_reference_gamma_high = 2.5; // In (2.35, 2.55)
  3920. static const float crt_reference_gamma_low = 2.35; // In (2.35, 2.55)
  3921. static const float lcd_reference_gamma = 2.5; // To match CRT
  3922. static const float crt_office_gamma = 2.2; // Circuitry-adjusted for NTSC
  3923. static const float lcd_office_gamma = 2.2; // Approximates sRGB
  3924. #endif // OVERRIDE_STANDARD_GAMMA
  3925. // Assuming alpha == 1.0 might make it easier for users to avoid some bugs,
  3926. // but only if they're aware of it.
  3927. #ifndef OVERRIDE_ALPHA_ASSUMPTIONS
  3928. static const bool assume_opaque_alpha = false;
  3929. #endif
  3930. /////////////////////// DERIVED CONSTANTS AS FUNCTIONS ///////////////////////
  3931. // gamma-management.h should be compatible with overriding gamma values with
  3932. // runtime user parameters, but we can only define other global constants in
  3933. // terms of static constants, not uniform user parameters. To get around this
  3934. // limitation, we need to define derived constants using functions.
  3935. // Set device gamma constants, but allow users to override them:
  3936. #ifdef OVERRIDE_DEVICE_GAMMA
  3937. // The user promises to globally define the appropriate constants:
  3938. inline float get_crt_gamma() { return crt_gamma; }
  3939. inline float get_gba_gamma() { return gba_gamma; }
  3940. inline float get_lcd_gamma() { return lcd_gamma; }
  3941. #else
  3942. inline float get_crt_gamma() { return crt_reference_gamma_high; }
  3943. inline float get_gba_gamma() { return 3.5; } // Game Boy Advance; in (3.0, 4.0)
  3944. inline float get_lcd_gamma() { return lcd_office_gamma; }
  3945. #endif // OVERRIDE_DEVICE_GAMMA
  3946. // Set decoding/encoding gammas for the first/lass passes, but allow overrides:
  3947. #ifdef OVERRIDE_FINAL_GAMMA
  3948. // The user promises to globally define the appropriate constants:
  3949. inline float get_intermediate_gamma() { return intermediate_gamma; }
  3950. inline float get_input_gamma() { return input_gamma; }
  3951. inline float get_output_gamma() { return output_gamma; }
  3952. #else
  3953. // If we gamma-correct every pass, always use ntsc_gamma between passes to
  3954. // ensure middle passes don't need to care if anything is being simulated:
  3955. inline float get_intermediate_gamma() { return ntsc_gamma; }
  3956. #ifdef SIMULATE_CRT_ON_LCD
  3957. inline float get_input_gamma() { return get_crt_gamma(); }
  3958. inline float get_output_gamma() { return get_lcd_gamma(); }
  3959. #else
  3960. #ifdef SIMULATE_GBA_ON_LCD
  3961. inline float get_input_gamma() { return get_gba_gamma(); }
  3962. inline float get_output_gamma() { return get_lcd_gamma(); }
  3963. #else
  3964. #ifdef SIMULATE_LCD_ON_CRT
  3965. inline float get_input_gamma() { return get_lcd_gamma(); }
  3966. inline float get_output_gamma() { return get_crt_gamma(); }
  3967. #else
  3968. #ifdef SIMULATE_GBA_ON_CRT
  3969. inline float get_input_gamma() { return get_gba_gamma(); }
  3970. inline float get_output_gamma() { return get_crt_gamma(); }
  3971. #else // Don't simulate anything:
  3972. inline float get_input_gamma() { return ntsc_gamma; }
  3973. inline float get_output_gamma() { return ntsc_gamma; }
  3974. #endif // SIMULATE_GBA_ON_CRT
  3975. #endif // SIMULATE_LCD_ON_CRT
  3976. #endif // SIMULATE_GBA_ON_LCD
  3977. #endif // SIMULATE_CRT_ON_LCD
  3978. #endif // OVERRIDE_FINAL_GAMMA
  3979. // Set decoding/encoding gammas for the current pass. Use static constants for
  3980. // linearize_input and gamma_encode_output, because they aren't derived, and
  3981. // they let the compiler do dead-code elimination.
  3982. #ifndef GAMMA_ENCODE_EVERY_FBO
  3983. #ifdef FIRST_PASS
  3984. static const bool linearize_input = true;
  3985. inline float get_pass_input_gamma() { return get_input_gamma(); }
  3986. #else
  3987. static const bool linearize_input = false;
  3988. inline float get_pass_input_gamma() { return 1.0; }
  3989. #endif
  3990. #ifdef LAST_PASS
  3991. static const bool gamma_encode_output = true;
  3992. inline float get_pass_output_gamma() { return get_output_gamma(); }
  3993. #else
  3994. static const bool gamma_encode_output = false;
  3995. inline float get_pass_output_gamma() { return 1.0; }
  3996. #endif
  3997. #else
  3998. static const bool linearize_input = true;
  3999. static const bool gamma_encode_output = true;
  4000. #ifdef FIRST_PASS
  4001. inline float get_pass_input_gamma() { return get_input_gamma(); }
  4002. #else
  4003. inline float get_pass_input_gamma() { return get_intermediate_gamma(); }
  4004. #endif
  4005. #ifdef LAST_PASS
  4006. inline float get_pass_output_gamma() { return get_output_gamma(); }
  4007. #else
  4008. inline float get_pass_output_gamma() { return get_intermediate_gamma(); }
  4009. #endif
  4010. #endif
  4011. // Users might want to know if bilinear filtering will be gamma-correct:
  4012. static const bool gamma_aware_bilinear = !linearize_input;
  4013. ////////////////////// COLOR ENCODING/DECODING FUNCTIONS /////////////////////
  4014. inline float4 encode_output(const float4 color)
  4015. {
  4016. if(gamma_encode_output)
  4017. {
  4018. if(assume_opaque_alpha)
  4019. {
  4020. return float4(pow(color.rgb, float3(1.0/get_pass_output_gamma())), 1.0);
  4021. }
  4022. else
  4023. {
  4024. return float4(pow(color.rgb, float3(1.0/get_pass_output_gamma())), color.a);
  4025. }
  4026. }
  4027. else
  4028. {
  4029. return color;
  4030. }
  4031. }
  4032. inline float4 decode_input(const float4 color)
  4033. {
  4034. if(linearize_input)
  4035. {
  4036. if(assume_opaque_alpha)
  4037. {
  4038. return float4(pow(color.rgb, float3(get_pass_input_gamma())), 1.0);
  4039. }
  4040. else
  4041. {
  4042. return float4(pow(color.rgb, float3(get_pass_input_gamma())), color.a);
  4043. }
  4044. }
  4045. else
  4046. {
  4047. return color;
  4048. }
  4049. }
  4050. inline float4 decode_gamma_input(const float4 color, const float3 gamma)
  4051. {
  4052. if(assume_opaque_alpha)
  4053. {
  4054. return float4(pow(color.rgb, gamma), 1.0);
  4055. }
  4056. else
  4057. {
  4058. return float4(pow(color.rgb, gamma), color.a);
  4059. }
  4060. }
  4061. //TODO/FIXME: I have no idea why replacing the lookup wrappers with this macro fixes the blurs being offset ¯\_(ツ)_/¯
  4062. //#define tex2D_linearize(C, D) decode_input(vec4(texture(C, D)))
  4063. // EDIT: it's the 'const' in front of the coords that's doing it
  4064. /////////////////////////// TEXTURE LOOKUP WRAPPERS //////////////////////////
  4065. // "SMART" LINEARIZING TEXTURE LOOKUP FUNCTIONS:
  4066. // Provide a wide array of linearizing texture lookup wrapper functions. The
  4067. // Cg shader spec Retroarch uses only allows for 2D textures, but 1D and 3D
  4068. // lookups are provided for completeness in case that changes someday. Nobody
  4069. // is likely to use the *fetch and *proj functions, but they're included just
  4070. // in case. The only tex*D texture sampling functions omitted are:
  4071. // - tex*Dcmpbias
  4072. // - tex*Dcmplod
  4073. // - tex*DARRAY*
  4074. // - tex*DMS*
  4075. // - Variants returning integers
  4076. // Standard line length restrictions are ignored below for vertical brevity.
  4077. /*
  4078. // tex1D:
  4079. inline float4 tex1D_linearize(const sampler1D tex, const float tex_coords)
  4080. { return decode_input(tex1D(tex, tex_coords)); }
  4081. inline float4 tex1D_linearize(const sampler1D tex, const float2 tex_coords)
  4082. { return decode_input(tex1D(tex, tex_coords)); }
  4083. inline float4 tex1D_linearize(const sampler1D tex, const float tex_coords, const int texel_off)
  4084. { return decode_input(tex1D(tex, tex_coords, texel_off)); }
  4085. inline float4 tex1D_linearize(const sampler1D tex, const float2 tex_coords, const int texel_off)
  4086. { return decode_input(tex1D(tex, tex_coords, texel_off)); }
  4087. inline float4 tex1D_linearize(const sampler1D tex, const float tex_coords, const float dx, const float dy)
  4088. { return decode_input(tex1D(tex, tex_coords, dx, dy)); }
  4089. inline float4 tex1D_linearize(const sampler1D tex, const float2 tex_coords, const float dx, const float dy)
  4090. { return decode_input(tex1D(tex, tex_coords, dx, dy)); }
  4091. inline float4 tex1D_linearize(const sampler1D tex, const float tex_coords, const float dx, const float dy, const int texel_off)
  4092. { return decode_input(tex1D(tex, tex_coords, dx, dy, texel_off)); }
  4093. inline float4 tex1D_linearize(const sampler1D tex, const float2 tex_coords, const float dx, const float dy, const int texel_off)
  4094. { return decode_input(tex1D(tex, tex_coords, dx, dy, texel_off)); }
  4095. // tex1Dbias:
  4096. inline float4 tex1Dbias_linearize(const sampler1D tex, const float4 tex_coords)
  4097. { return decode_input(tex1Dbias(tex, tex_coords)); }
  4098. inline float4 tex1Dbias_linearize(const sampler1D tex, const float4 tex_coords, const int texel_off)
  4099. { return decode_input(tex1Dbias(tex, tex_coords, texel_off)); }
  4100. // tex1Dfetch:
  4101. inline float4 tex1Dfetch_linearize(const sampler1D tex, const int4 tex_coords)
  4102. { return decode_input(tex1Dfetch(tex, tex_coords)); }
  4103. inline float4 tex1Dfetch_linearize(const sampler1D tex, const int4 tex_coords, const int texel_off)
  4104. { return decode_input(tex1Dfetch(tex, tex_coords, texel_off)); }
  4105. // tex1Dlod:
  4106. inline float4 tex1Dlod_linearize(const sampler1D tex, const float4 tex_coords)
  4107. { return decode_input(tex1Dlod(tex, tex_coords)); }
  4108. inline float4 tex1Dlod_linearize(const sampler1D tex, const float4 tex_coords, const int texel_off)
  4109. { return decode_input(tex1Dlod(tex, tex_coords, texel_off)); }
  4110. // tex1Dproj:
  4111. inline float4 tex1Dproj_linearize(const sampler1D tex, const float2 tex_coords)
  4112. { return decode_input(tex1Dproj(tex, tex_coords)); }
  4113. inline float4 tex1Dproj_linearize(const sampler1D tex, const float3 tex_coords)
  4114. { return decode_input(tex1Dproj(tex, tex_coords)); }
  4115. inline float4 tex1Dproj_linearize(const sampler1D tex, const float2 tex_coords, const int texel_off)
  4116. { return decode_input(tex1Dproj(tex, tex_coords, texel_off)); }
  4117. inline float4 tex1Dproj_linearize(const sampler1D tex, const float3 tex_coords, const int texel_off)
  4118. { return decode_input(tex1Dproj(tex, tex_coords, texel_off)); }
  4119. */
  4120. // tex2D:
  4121. inline float4 tex2D_linearize(const sampler2D tex, float2 tex_coords)
  4122. { return decode_input(COMPAT_TEXTURE(tex, tex_coords)); }
  4123. inline float4 tex2D_linearize(const sampler2D tex, float3 tex_coords)
  4124. { return decode_input(COMPAT_TEXTURE(tex, tex_coords.xy)); }
  4125. inline float4 tex2D_linearize(const sampler2D tex, float2 tex_coords, int texel_off)
  4126. { return decode_input(textureLod(tex, tex_coords, texel_off)); }
  4127. inline float4 tex2D_linearize(const sampler2D tex, float3 tex_coords, int texel_off)
  4128. { return decode_input(textureLod(tex, tex_coords.xy, texel_off)); }
  4129. //inline float4 tex2D_linearize(const sampler2D tex, const float2 tex_coords, const float2 dx, const float2 dy)
  4130. //{ return decode_input(COMPAT_TEXTURE(tex, tex_coords, dx, dy)); }
  4131. //inline float4 tex2D_linearize(const sampler2D tex, const float3 tex_coords, const float2 dx, const float2 dy)
  4132. //{ return decode_input(COMPAT_TEXTURE(tex, tex_coords, dx, dy)); }
  4133. //inline float4 tex2D_linearize(const sampler2D tex, const float2 tex_coords, const float2 dx, const float2 dy, const int texel_off)
  4134. //{ return decode_input(COMPAT_TEXTURE(tex, tex_coords, dx, dy, texel_off)); }
  4135. //inline float4 tex2D_linearize(const sampler2D tex, const float3 tex_coords, const float2 dx, const float2 dy, const int texel_off)
  4136. //{ return decode_input(COMPAT_TEXTURE(tex, tex_coords, dx, dy, texel_off)); }
  4137. // tex2Dbias:
  4138. //inline float4 tex2Dbias_linearize(const sampler2D tex, const float4 tex_coords)
  4139. //{ return decode_input(tex2Dbias(tex, tex_coords)); }
  4140. //inline float4 tex2Dbias_linearize(const sampler2D tex, const float4 tex_coords, const int texel_off)
  4141. //{ return decode_input(tex2Dbias(tex, tex_coords, texel_off)); }
  4142. // tex2Dfetch:
  4143. //inline float4 tex2Dfetch_linearize(const sampler2D tex, const int4 tex_coords)
  4144. //{ return decode_input(tex2Dfetch(tex, tex_coords)); }
  4145. //inline float4 tex2Dfetch_linearize(const sampler2D tex, const int4 tex_coords, const int texel_off)
  4146. //{ return decode_input(tex2Dfetch(tex, tex_coords, texel_off)); }
  4147. // tex2Dlod:
  4148. inline float4 tex2Dlod_linearize(const sampler2D tex, float4 tex_coords)
  4149. { return decode_input(textureLod(tex, tex_coords.xy, 0.0)); }
  4150. inline float4 tex2Dlod_linearize(const sampler2D tex, float4 tex_coords, int texel_off)
  4151. { return decode_input(textureLod(tex, tex_coords.xy, texel_off)); }
  4152. /*
  4153. // tex2Dproj:
  4154. inline float4 tex2Dproj_linearize(const sampler2D tex, const float3 tex_coords)
  4155. { return decode_input(tex2Dproj(tex, tex_coords)); }
  4156. inline float4 tex2Dproj_linearize(const sampler2D tex, const float4 tex_coords)
  4157. { return decode_input(tex2Dproj(tex, tex_coords)); }
  4158. inline float4 tex2Dproj_linearize(const sampler2D tex, const float3 tex_coords, const int texel_off)
  4159. { return decode_input(tex2Dproj(tex, tex_coords, texel_off)); }
  4160. inline float4 tex2Dproj_linearize(const sampler2D tex, const float4 tex_coords, const int texel_off)
  4161. { return decode_input(tex2Dproj(tex, tex_coords, texel_off)); }
  4162. */
  4163. /*
  4164. // tex3D:
  4165. inline float4 tex3D_linearize(const sampler3D tex, const float3 tex_coords)
  4166. { return decode_input(tex3D(tex, tex_coords)); }
  4167. inline float4 tex3D_linearize(const sampler3D tex, const float3 tex_coords, const int texel_off)
  4168. { return decode_input(tex3D(tex, tex_coords, texel_off)); }
  4169. inline float4 tex3D_linearize(const sampler3D tex, const float3 tex_coords, const float3 dx, const float3 dy)
  4170. { return decode_input(tex3D(tex, tex_coords, dx, dy)); }
  4171. inline float4 tex3D_linearize(const sampler3D tex, const float3 tex_coords, const float3 dx, const float3 dy, const int texel_off)
  4172. { return decode_input(tex3D(tex, tex_coords, dx, dy, texel_off)); }
  4173. // tex3Dbias:
  4174. inline float4 tex3Dbias_linearize(const sampler3D tex, const float4 tex_coords)
  4175. { return decode_input(tex3Dbias(tex, tex_coords)); }
  4176. inline float4 tex3Dbias_linearize(const sampler3D tex, const float4 tex_coords, const int texel_off)
  4177. { return decode_input(tex3Dbias(tex, tex_coords, texel_off)); }
  4178. // tex3Dfetch:
  4179. inline float4 tex3Dfetch_linearize(const sampler3D tex, const int4 tex_coords)
  4180. { return decode_input(tex3Dfetch(tex, tex_coords)); }
  4181. inline float4 tex3Dfetch_linearize(const sampler3D tex, const int4 tex_coords, const int texel_off)
  4182. { return decode_input(tex3Dfetch(tex, tex_coords, texel_off)); }
  4183. // tex3Dlod:
  4184. inline float4 tex3Dlod_linearize(const sampler3D tex, const float4 tex_coords)
  4185. { return decode_input(tex3Dlod(tex, tex_coords)); }
  4186. inline float4 tex3Dlod_linearize(const sampler3D tex, const float4 tex_coords, const int texel_off)
  4187. { return decode_input(tex3Dlod(tex, tex_coords, texel_off)); }
  4188. // tex3Dproj:
  4189. inline float4 tex3Dproj_linearize(const sampler3D tex, const float4 tex_coords)
  4190. { return decode_input(tex3Dproj(tex, tex_coords)); }
  4191. inline float4 tex3Dproj_linearize(const sampler3D tex, const float4 tex_coords, const int texel_off)
  4192. { return decode_input(tex3Dproj(tex, tex_coords, texel_off)); }
  4193. /////////*
  4194. // NONSTANDARD "SMART" LINEARIZING TEXTURE LOOKUP FUNCTIONS:
  4195. // This narrow selection of nonstandard tex2D* functions can be useful:
  4196. // tex2Dlod0: Automatically fill in the tex2D LOD parameter for mip level 0.
  4197. //inline float4 tex2Dlod0_linearize(const sampler2D tex, const float2 tex_coords)
  4198. //{ return decode_input(tex2Dlod(tex, float4(tex_coords, 0.0, 0.0))); }
  4199. //inline float4 tex2Dlod0_linearize(const sampler2D tex, const float2 tex_coords, const int texel_off)
  4200. //{ return decode_input(tex2Dlod(tex, float4(tex_coords, 0.0, 0.0), texel_off)); }
  4201. // MANUALLY LINEARIZING TEXTURE LOOKUP FUNCTIONS:
  4202. // Provide a narrower selection of tex2D* wrapper functions that decode an
  4203. // input sample with a specified gamma value. These are useful for reading
  4204. // LUT's and for reading the input of pass0 in a later pass.
  4205. // tex2D:
  4206. inline float4 tex2D_linearize_gamma(const sampler2D tex, const float2 tex_coords, const float3 gamma)
  4207. { return decode_gamma_input(COMPAT_TEXTURE(tex, tex_coords), gamma); }
  4208. inline float4 tex2D_linearize_gamma(const sampler2D tex, const float3 tex_coords, const float3 gamma)
  4209. { return decode_gamma_input(COMPAT_TEXTURE(tex, tex_coords.xy), gamma); }
  4210. //inline float4 tex2D_linearize_gamma(const sampler2D tex, const float2 tex_coords, const int texel_off, const float3 gamma)
  4211. //{ return decode_gamma_input(COMPAT_TEXTURE(tex, tex_coords, texel_off), gamma); }
  4212. //inline float4 tex2D_linearize_gamma(const sampler2D tex, const float3 tex_coords, const int texel_off, const float3 gamma)
  4213. //{ return decode_gamma_input(COMPAT_TEXTURE(tex, tex_coords, texel_off), gamma); }
  4214. //inline float4 tex2D_linearize_gamma(const sampler2D tex, const float2 tex_coords, const float2 dx, const float2 dy, const float3 gamma)
  4215. //{ return decode_gamma_input(COMPAT_TEXTURE(tex, tex_coords, dx, dy), gamma); }
  4216. //inline float4 tex2D_linearize_gamma(const sampler2D tex, const float3 tex_coords, const float2 dx, const float2 dy, const float3 gamma)
  4217. //{ return decode_gamma_input(COMPAT_TEXTURE(tex, tex_coords, dx, dy), gamma); }
  4218. //inline float4 tex2D_linearize_gamma(const sampler2D tex, const float2 tex_coords, const float2 dx, const float2 dy, const int texel_off, const float3 gamma)
  4219. //{ return decode_gamma_input(COMPAT_TEXTURE(tex, tex_coords, dx, dy, texel_off), gamma); }
  4220. //inline float4 tex2D_linearize_gamma(const sampler2D tex, const float3 tex_coords, const float2 dx, const float2 dy, const int texel_off, const float3 gamma)
  4221. //{ return decode_gamma_input(COMPAT_TEXTURE(tex, tex_coords, dx, dy, texel_off), gamma); }
  4222. /*
  4223. // tex2Dbias:
  4224. inline float4 tex2Dbias_linearize_gamma(const sampler2D tex, const float4 tex_coords, const float3 gamma)
  4225. { return decode_gamma_input(tex2Dbias(tex, tex_coords), gamma); }
  4226. inline float4 tex2Dbias_linearize_gamma(const sampler2D tex, const float4 tex_coords, const int texel_off, const float3 gamma)
  4227. { return decode_gamma_input(tex2Dbias(tex, tex_coords, texel_off), gamma); }
  4228. // tex2Dfetch:
  4229. inline float4 tex2Dfetch_linearize_gamma(const sampler2D tex, const int4 tex_coords, const float3 gamma)
  4230. { return decode_gamma_input(tex2Dfetch(tex, tex_coords), gamma); }
  4231. inline float4 tex2Dfetch_linearize_gamma(const sampler2D tex, const int4 tex_coords, const int texel_off, const float3 gamma)
  4232. { return decode_gamma_input(tex2Dfetch(tex, tex_coords, texel_off), gamma); }
  4233. */
  4234. // tex2Dlod:
  4235. inline float4 tex2Dlod_linearize_gamma(const sampler2D tex, float4 tex_coords, float3 gamma)
  4236. { return decode_gamma_input(textureLod(tex, tex_coords.xy, 0.0), gamma); }
  4237. inline float4 tex2Dlod_linearize_gamma(const sampler2D tex, float4 tex_coords, int texel_off, float3 gamma)
  4238. { return decode_gamma_input(textureLod(tex, tex_coords.xy, texel_off), gamma); }
  4239. #endif // GAMMA_MANAGEMENT_H
  4240. //////////////////////////// END GAMMA-MANAGEMENT //////////////////////////
  4241. //////////////////////////////// END INCLUDES ////////////////////////////////
  4242. ///////////////////////////// SCANLINE FUNCTIONS /////////////////////////////
  4243. inline float3 get_gaussian_sigma(const float3 color, const float sigma_range)
  4244. {
  4245. // Requires: Globals:
  4246. // 1.) beam_min_sigma and beam_max_sigma are global floats
  4247. // containing the desired minimum and maximum beam standard
  4248. // deviations, for dim and bright colors respectively.
  4249. // 2.) beam_max_sigma must be > 0.0
  4250. // 3.) beam_min_sigma must be in (0.0, beam_max_sigma]
  4251. // 4.) beam_spot_power must be defined as a global float.
  4252. // Parameters:
  4253. // 1.) color is the underlying source color along a scanline
  4254. // 2.) sigma_range = beam_max_sigma - beam_min_sigma; we take
  4255. // sigma_range as a parameter to avoid repeated computation
  4256. // when beam_{min, max}_sigma are runtime shader parameters
  4257. // Optional: Users may set beam_spot_shape_function to 1 to define the
  4258. // inner f(color) subfunction (see below) as:
  4259. // f(color) = sqrt(1.0 - (color - 1.0)*(color - 1.0))
  4260. // Otherwise (technically, if beam_spot_shape_function < 0.5):
  4261. // f(color) = pow(color, beam_spot_power)
  4262. // Returns: The standard deviation of the Gaussian beam for "color:"
  4263. // sigma = beam_min_sigma + sigma_range * f(color)
  4264. // Details/Discussion:
  4265. // The beam's spot shape vaguely resembles an aspect-corrected f() in the
  4266. // range [0, 1] (not quite, but it's related). f(color) = color makes
  4267. // spots look like diamonds, and a spherical function or cube balances
  4268. // between variable width and a soft/realistic shape. A beam_spot_power
  4269. // > 1.0 can produce an ugly spot shape and more initial clipping, but the
  4270. // final shape also differs based on the horizontal resampling filter and
  4271. // the phosphor bloom. For instance, resampling horizontally in nonlinear
  4272. // light and/or with a sharp (e.g. Lanczos) filter will sharpen the spot
  4273. // shape, but a sixth root is still quite soft. A power function (default
  4274. // 1.0/3.0 beam_spot_power) is most flexible, but a fixed spherical curve
  4275. // has the highest variability without an awful spot shape.
  4276. //
  4277. // beam_min_sigma affects scanline sharpness/aliasing in dim areas, and its
  4278. // difference from beam_max_sigma affects beam width variability. It only
  4279. // affects clipping [for pure Gaussians] if beam_spot_power > 1.0 (which is
  4280. // a conservative estimate for a more complex constraint).
  4281. //
  4282. // beam_max_sigma affects clipping and increasing scanline width/softness
  4283. // as color increases. The wider this is, the more scanlines need to be
  4284. // evaluated to avoid distortion. For a pure Gaussian, the max_beam_sigma
  4285. // at which the first unused scanline always has a weight < 1.0/255.0 is:
  4286. // num scanlines = 2, max_beam_sigma = 0.2089; distortions begin ~0.34
  4287. // num scanlines = 3, max_beam_sigma = 0.3879; distortions begin ~0.52
  4288. // num scanlines = 4, max_beam_sigma = 0.5723; distortions begin ~0.70
  4289. // num scanlines = 5, max_beam_sigma = 0.7591; distortions begin ~0.89
  4290. // num scanlines = 6, max_beam_sigma = 0.9483; distortions begin ~1.08
  4291. // Generalized Gaussians permit more leeway here as steepness increases.
  4292. if(beam_spot_shape_function < 0.5)
  4293. {
  4294. // Use a power function:
  4295. return float3(beam_min_sigma) + sigma_range *
  4296. pow(color, float3(beam_spot_power));
  4297. }
  4298. else
  4299. {
  4300. // Use a spherical function:
  4301. const float3 color_minus_1 = color - float3(1.0);
  4302. return float3(beam_min_sigma) + sigma_range *
  4303. sqrt(float3(1.0) - color_minus_1*color_minus_1);
  4304. }
  4305. }
  4306. inline float3 get_generalized_gaussian_beta(const float3 color,
  4307. const float shape_range)
  4308. {
  4309. // Requires: Globals:
  4310. // 1.) beam_min_shape and beam_max_shape are global floats
  4311. // containing the desired min/max generalized Gaussian
  4312. // beta parameters, for dim and bright colors respectively.
  4313. // 2.) beam_max_shape must be >= 2.0
  4314. // 3.) beam_min_shape must be in [2.0, beam_max_shape]
  4315. // 4.) beam_shape_power must be defined as a global float.
  4316. // Parameters:
  4317. // 1.) color is the underlying source color along a scanline
  4318. // 2.) shape_range = beam_max_shape - beam_min_shape; we take
  4319. // shape_range as a parameter to avoid repeated computation
  4320. // when beam_{min, max}_shape are runtime shader parameters
  4321. // Returns: The type-I generalized Gaussian "shape" parameter beta for
  4322. // the given color.
  4323. // Details/Discussion:
  4324. // Beta affects the scanline distribution as follows:
  4325. // a.) beta < 2.0 narrows the peak to a spike with a discontinuous slope
  4326. // b.) beta == 2.0 just degenerates to a Gaussian
  4327. // c.) beta > 2.0 flattens and widens the peak, then drops off more steeply
  4328. // than a Gaussian. Whereas high sigmas widen and soften peaks, high
  4329. // beta widen and sharpen peaks at the risk of aliasing.
  4330. // Unlike high beam_spot_powers, high beam_shape_powers actually soften shape
  4331. // transitions, whereas lower ones sharpen them (at the risk of aliasing).
  4332. return beam_min_shape + shape_range * pow(color, float3(beam_shape_power));
  4333. }
  4334. float3 scanline_gaussian_integral_contrib(const float3 dist,
  4335. const float3 color, const float pixel_height, const float sigma_range)
  4336. {
  4337. // Requires: 1.) dist is the distance of the [potentially separate R/G/B]
  4338. // point(s) from a scanline in units of scanlines, where
  4339. // 1.0 means the sample point straddles the next scanline.
  4340. // 2.) color is the underlying source color along a scanline.
  4341. // 3.) pixel_height is the output pixel height in scanlines.
  4342. // 4.) Requirements of get_gaussian_sigma() must be met.
  4343. // Returns: Return a scanline's light output over a given pixel.
  4344. // Details:
  4345. // The CRT beam profile follows a roughly Gaussian distribution which is
  4346. // wider for bright colors than dark ones. The integral over the full
  4347. // range of a Gaussian function is always 1.0, so we can vary the beam
  4348. // with a standard deviation without affecting brightness. 'x' = distance:
  4349. // gaussian sample = 1/(sigma*sqrt(2*pi)) * e**(-(x**2)/(2*sigma**2))
  4350. // gaussian integral = 0.5 (1.0 + erf(x/(sigma * sqrt(2))))
  4351. // Use a numerical approximation of the "error function" (the Gaussian
  4352. // indefinite integral) to find the definite integral of the scanline's
  4353. // average brightness over a given pixel area. Even if curved coords were
  4354. // used in this pass, a flat scalar pixel height works almost as well as a
  4355. // pixel height computed from a full pixel-space to scanline-space matrix.
  4356. const float3 sigma = get_gaussian_sigma(color, sigma_range);
  4357. const float3 ph_offset = float3(pixel_height * 0.5);
  4358. const float3 denom_inv = 1.0/(sigma*sqrt(2.0));
  4359. const float3 integral_high = erf((dist + ph_offset)*denom_inv);
  4360. const float3 integral_low = erf((dist - ph_offset)*denom_inv);
  4361. return color * 0.5*(integral_high - integral_low)/pixel_height;
  4362. }
  4363. float3 scanline_generalized_gaussian_integral_contrib(float3 dist,
  4364. float3 color, float pixel_height, float sigma_range,
  4365. float shape_range)
  4366. {
  4367. // Requires: 1.) Requirements of scanline_gaussian_integral_contrib()
  4368. // must be met.
  4369. // 2.) Requirements of get_gaussian_sigma() must be met.
  4370. // 3.) Requirements of get_generalized_gaussian_beta() must be
  4371. // met.
  4372. // Returns: Return a scanline's light output over a given pixel.
  4373. // A generalized Gaussian distribution allows the shape (beta) to vary
  4374. // as well as the width (alpha). "gamma" refers to the gamma function:
  4375. // generalized sample =
  4376. // beta/(2*alpha*gamma(1/beta)) * e**(-(|x|/alpha)**beta)
  4377. // ligamma(s, z) is the lower incomplete gamma function, for which we only
  4378. // implement two of four branches (because we keep 1/beta <= 0.5):
  4379. // generalized integral = 0.5 + 0.5* sign(x) *
  4380. // ligamma(1/beta, (|x|/alpha)**beta)/gamma(1/beta)
  4381. // See get_generalized_gaussian_beta() for a discussion of beta.
  4382. // We base alpha on the intended Gaussian sigma, but it only strictly
  4383. // models models standard deviation at beta == 2, because the standard
  4384. // deviation depends on both alpha and beta (keeping alpha independent is
  4385. // faster and preserves intuitive behavior and a full spectrum of results).
  4386. const float3 alpha = sqrt(2.0) * get_gaussian_sigma(color, sigma_range);
  4387. const float3 beta = get_generalized_gaussian_beta(color, shape_range);
  4388. const float3 alpha_inv = float3(1.0)/alpha;
  4389. const float3 s = float3(1.0)/beta;
  4390. const float3 ph_offset = float3(pixel_height * 0.5);
  4391. // Pass beta to gamma_impl to avoid repeated divides. Similarly pass
  4392. // beta (i.e. 1/s) and 1/gamma(s) to normalized_ligamma_impl.
  4393. const float3 gamma_s_inv = float3(1.0)/gamma_impl(s, beta);
  4394. const float3 dist1 = dist + ph_offset;
  4395. const float3 dist0 = dist - ph_offset;
  4396. const float3 integral_high = sign(dist1) * normalized_ligamma_impl(
  4397. s, pow(abs(dist1)*alpha_inv, beta), beta, gamma_s_inv);
  4398. const float3 integral_low = sign(dist0) * normalized_ligamma_impl(
  4399. s, pow(abs(dist0)*alpha_inv, beta), beta, gamma_s_inv);
  4400. return color * 0.5*(integral_high - integral_low)/pixel_height;
  4401. }
  4402. float3 scanline_gaussian_sampled_contrib(const float3 dist, const float3 color,
  4403. const float pixel_height, const float sigma_range)
  4404. {
  4405. // See scanline_gaussian integral_contrib() for detailed comments!
  4406. // gaussian sample = 1/(sigma*sqrt(2*pi)) * e**(-(x**2)/(2*sigma**2))
  4407. const float3 sigma = get_gaussian_sigma(color, sigma_range);
  4408. // Avoid repeated divides:
  4409. const float3 sigma_inv = float3(1.0)/sigma;
  4410. const float3 inner_denom_inv = 0.5 * sigma_inv * sigma_inv;
  4411. const float3 outer_denom_inv = sigma_inv/sqrt(2.0*pi);
  4412. if(beam_antialias_level > 0.5)
  4413. {
  4414. // Sample 1/3 pixel away in each direction as well:
  4415. const float3 sample_offset = float3(pixel_height/3.0);
  4416. const float3 dist2 = dist + sample_offset;
  4417. const float3 dist3 = abs(dist - sample_offset);
  4418. // Average three pure Gaussian samples:
  4419. const float3 scale = color/3.0 * outer_denom_inv;
  4420. const float3 weight1 = exp(-(dist*dist)*inner_denom_inv);
  4421. const float3 weight2 = exp(-(dist2*dist2)*inner_denom_inv);
  4422. const float3 weight3 = exp(-(dist3*dist3)*inner_denom_inv);
  4423. return scale * (weight1 + weight2 + weight3);
  4424. }
  4425. else
  4426. {
  4427. return color*exp(-(dist*dist)*inner_denom_inv)*outer_denom_inv;
  4428. }
  4429. }
  4430. float3 scanline_generalized_gaussian_sampled_contrib(float3 dist,
  4431. float3 color, float pixel_height, float sigma_range,
  4432. float shape_range)
  4433. {
  4434. // See scanline_generalized_gaussian_integral_contrib() for details!
  4435. // generalized sample =
  4436. // beta/(2*alpha*gamma(1/beta)) * e**(-(|x|/alpha)**beta)
  4437. const float3 alpha = sqrt(2.0) * get_gaussian_sigma(color, sigma_range);
  4438. const float3 beta = get_generalized_gaussian_beta(color, shape_range);
  4439. // Avoid repeated divides:
  4440. const float3 alpha_inv = float3(1.0)/alpha;
  4441. const float3 beta_inv = float3(1.0)/beta;
  4442. const float3 scale = color * beta * 0.5 * alpha_inv /
  4443. gamma_impl(beta_inv, beta);
  4444. if(beam_antialias_level > 0.5)
  4445. {
  4446. // Sample 1/3 pixel closer to and farther from the scanline too.
  4447. const float3 sample_offset = float3(pixel_height/3.0);
  4448. const float3 dist2 = dist + sample_offset;
  4449. const float3 dist3 = abs(dist - sample_offset);
  4450. // Average three generalized Gaussian samples:
  4451. const float3 weight1 = exp(-pow(abs(dist*alpha_inv), beta));
  4452. const float3 weight2 = exp(-pow(abs(dist2*alpha_inv), beta));
  4453. const float3 weight3 = exp(-pow(abs(dist3*alpha_inv), beta));
  4454. return scale/3.0 * (weight1 + weight2 + weight3);
  4455. }
  4456. else
  4457. {
  4458. return scale * exp(-pow(abs(dist*alpha_inv), beta));
  4459. }
  4460. }
  4461. inline float3 scanline_contrib(float3 dist, float3 color,
  4462. float pixel_height, const float sigma_range, const float shape_range)
  4463. {
  4464. // Requires: 1.) Requirements of scanline_gaussian_integral_contrib()
  4465. // must be met.
  4466. // 2.) Requirements of get_gaussian_sigma() must be met.
  4467. // 3.) Requirements of get_generalized_gaussian_beta() must be
  4468. // met.
  4469. // Returns: Return a scanline's light output over a given pixel, using
  4470. // a generalized or pure Gaussian distribution and sampling or
  4471. // integrals as desired by user codepath choices.
  4472. if(beam_generalized_gaussian)
  4473. {
  4474. if(beam_antialias_level > 1.5)
  4475. {
  4476. return scanline_generalized_gaussian_integral_contrib(
  4477. dist, color, pixel_height, sigma_range, shape_range);
  4478. }
  4479. else
  4480. {
  4481. return scanline_generalized_gaussian_sampled_contrib(
  4482. dist, color, pixel_height, sigma_range, shape_range);
  4483. }
  4484. }
  4485. else
  4486. {
  4487. if(beam_antialias_level > 1.5)
  4488. {
  4489. return scanline_gaussian_integral_contrib(
  4490. dist, color, pixel_height, sigma_range);
  4491. }
  4492. else
  4493. {
  4494. return scanline_gaussian_sampled_contrib(
  4495. dist, color, pixel_height, sigma_range);
  4496. }
  4497. }
  4498. }
  4499. inline float3 get_raw_interpolated_color(const float3 color0,
  4500. const float3 color1, const float3 color2, const float3 color3,
  4501. const float4 weights)
  4502. {
  4503. // Use max to avoid bizarre artifacts from negative colors:
  4504. return max(mul(weights, float4x3(color0, color1, color2, color3)), 0.0);
  4505. }
  4506. float3 get_interpolated_linear_color(const float3 color0, const float3 color1,
  4507. const float3 color2, const float3 color3, const float4 weights)
  4508. {
  4509. // Requires: 1.) Requirements of include/gamma-management.h must be met:
  4510. // intermediate_gamma must be globally defined, and input
  4511. // colors are interpreted as linear RGB unless you #define
  4512. // GAMMA_ENCODE_EVERY_FBO (in which case they are
  4513. // interpreted as gamma-encoded with intermediate_gamma).
  4514. // 2.) color0-3 are colors sampled from a texture with tex2D().
  4515. // They are interpreted as defined in requirement 1.
  4516. // 3.) weights contains weights for each color, summing to 1.0.
  4517. // 4.) beam_horiz_linear_rgb_weight must be defined as a global
  4518. // float in [0.0, 1.0] describing how much blending should
  4519. // be done in linear RGB (rest is gamma-corrected RGB).
  4520. // 5.) RUNTIME_SCANLINES_HORIZ_FILTER_COLORSPACE must be #defined
  4521. // if beam_horiz_linear_rgb_weight is anything other than a
  4522. // static constant, or we may try branching at runtime
  4523. // without dynamic branches allowed (slow).
  4524. // Returns: Return an interpolated color lookup between the four input
  4525. // colors based on the weights in weights. The final color will
  4526. // be a linear RGB value, but the blending will be done as
  4527. // indicated above.
  4528. const float intermediate_gamma = get_intermediate_gamma();
  4529. // Branch if beam_horiz_linear_rgb_weight is static (for free) or if the
  4530. // profile allows dynamic branches (faster than computing extra pows):
  4531. #ifndef RUNTIME_SCANLINES_HORIZ_FILTER_COLORSPACE
  4532. #define SCANLINES_BRANCH_FOR_LINEAR_RGB_WEIGHT
  4533. #else
  4534. #ifdef DRIVERS_ALLOW_DYNAMIC_BRANCHES
  4535. #define SCANLINES_BRANCH_FOR_LINEAR_RGB_WEIGHT
  4536. #endif
  4537. #endif
  4538. #ifdef SCANLINES_BRANCH_FOR_LINEAR_RGB_WEIGHT
  4539. // beam_horiz_linear_rgb_weight is static, so we can branch:
  4540. #ifdef GAMMA_ENCODE_EVERY_FBO
  4541. const float3 gamma_mixed_color = pow(get_raw_interpolated_color(
  4542. color0, color1, color2, color3, weights), float3(intermediate_gamma));
  4543. if(beam_horiz_linear_rgb_weight > 0.0)
  4544. {
  4545. const float3 linear_mixed_color = get_raw_interpolated_color(
  4546. pow(color0, float3(intermediate_gamma)),
  4547. pow(color1, float3(intermediate_gamma)),
  4548. pow(color2, float3(intermediate_gamma)),
  4549. pow(color3, float3(intermediate_gamma)),
  4550. weights);
  4551. return lerp(gamma_mixed_color, linear_mixed_color,
  4552. beam_horiz_linear_rgb_weight);
  4553. }
  4554. else
  4555. {
  4556. return gamma_mixed_color;
  4557. }
  4558. #else
  4559. const float3 linear_mixed_color = get_raw_interpolated_color(
  4560. color0, color1, color2, color3, weights);
  4561. if(beam_horiz_linear_rgb_weight < 1.0)
  4562. {
  4563. const float3 gamma_mixed_color = get_raw_interpolated_color(
  4564. pow(color0, float3(1.0/intermediate_gamma)),
  4565. pow(color1, float3(1.0/intermediate_gamma)),
  4566. pow(color2, float3(1.0/intermediate_gamma)),
  4567. pow(color3, float3(1.0/intermediate_gamma)),
  4568. weights);
  4569. return lerp(gamma_mixed_color, linear_mixed_color,
  4570. beam_horiz_linear_rgb_weight);
  4571. }
  4572. else
  4573. {
  4574. return linear_mixed_color;
  4575. }
  4576. #endif // GAMMA_ENCODE_EVERY_FBO
  4577. #else
  4578. #ifdef GAMMA_ENCODE_EVERY_FBO
  4579. // Inputs: color0-3 are colors in gamma-encoded RGB.
  4580. const float3 gamma_mixed_color = pow(get_raw_interpolated_color(
  4581. color0, color1, color2, color3, weights), intermediate_gamma);
  4582. const float3 linear_mixed_color = get_raw_interpolated_color(
  4583. pow(color0, float3(intermediate_gamma)),
  4584. pow(color1, float3(intermediate_gamma)),
  4585. pow(color2, float3(intermediate_gamma)),
  4586. pow(color3, float3(intermediate_gamma)),
  4587. weights);
  4588. return lerp(gamma_mixed_color, linear_mixed_color,
  4589. beam_horiz_linear_rgb_weight);
  4590. #else
  4591. // Inputs: color0-3 are colors in linear RGB.
  4592. const float3 linear_mixed_color = get_raw_interpolated_color(
  4593. color0, color1, color2, color3, weights);
  4594. const float3 gamma_mixed_color = get_raw_interpolated_color(
  4595. pow(color0, float3(1.0/intermediate_gamma)),
  4596. pow(color1, float3(1.0/intermediate_gamma)),
  4597. pow(color2, float3(1.0/intermediate_gamma)),
  4598. pow(color3, float3(1.0/intermediate_gamma)),
  4599. weights);
  4600. // wtf fixme
  4601. // const float beam_horiz_linear_rgb_weight1 = 1.0;
  4602. return lerp(gamma_mixed_color, linear_mixed_color,
  4603. beam_horiz_linear_rgb_weight);
  4604. #endif // GAMMA_ENCODE_EVERY_FBO
  4605. #endif // SCANLINES_BRANCH_FOR_LINEAR_RGB_WEIGHT
  4606. }
  4607. float3 get_scanline_color(const sampler2D tex, const float2 scanline_uv,
  4608. const float2 uv_step_x, const float4 weights)
  4609. {
  4610. // Requires: 1.) scanline_uv must be vertically snapped to the caller's
  4611. // desired line or scanline and horizontally snapped to the
  4612. // texel just left of the output pixel (color1)
  4613. // 2.) uv_step_x must contain the horizontal uv distance
  4614. // between texels.
  4615. // 3.) weights must contain interpolation filter weights for
  4616. // color0, color1, color2, and color3, where color1 is just
  4617. // left of the output pixel.
  4618. // Returns: Return a horizontally interpolated texture lookup using 2-4
  4619. // nearby texels, according to weights and the conventions of
  4620. // get_interpolated_linear_color().
  4621. // We can ignore the outside texture lookups for Quilez resampling.
  4622. const float3 color1 = COMPAT_TEXTURE(tex, scanline_uv).rgb;
  4623. const float3 color2 = COMPAT_TEXTURE(tex, scanline_uv + uv_step_x).rgb;
  4624. float3 color0 = float3(0.0);
  4625. float3 color3 = float3(0.0);
  4626. if(beam_horiz_filter > 0.5)
  4627. {
  4628. color0 = COMPAT_TEXTURE(tex, scanline_uv - uv_step_x).rgb;
  4629. color3 = COMPAT_TEXTURE(tex, scanline_uv + 2.0 * uv_step_x).rgb;
  4630. }
  4631. // Sample the texture as-is, whether it's linear or gamma-encoded:
  4632. // get_interpolated_linear_color() will handle the difference.
  4633. return get_interpolated_linear_color(color0, color1, color2, color3, weights);
  4634. }
  4635. float3 sample_single_scanline_horizontal(const sampler2D tex,
  4636. const float2 tex_uv, const float2 tex_size,
  4637. const float2 texture_size_inv)
  4638. {
  4639. // TODO: Add function requirements.
  4640. // Snap to the previous texel and get sample dists from 2/4 nearby texels:
  4641. const float2 curr_texel = tex_uv * tex_size;
  4642. // Use under_half to fix a rounding bug right around exact texel locations.
  4643. const float2 prev_texel =
  4644. floor(curr_texel - float2(under_half)) + float2(0.5);
  4645. const float2 prev_texel_hor = float2(prev_texel.x, curr_texel.y);
  4646. const float2 prev_texel_hor_uv = prev_texel_hor * texture_size_inv;
  4647. const float prev_dist = curr_texel.x - prev_texel_hor.x;
  4648. const float4 sample_dists = float4(1.0 + prev_dist, prev_dist,
  4649. 1.0 - prev_dist, 2.0 - prev_dist);
  4650. // Get Quilez, Lanczos2, or Gaussian resize weights for 2/4 nearby texels:
  4651. float4 weights;
  4652. if(beam_horiz_filter < 0.5)
  4653. {
  4654. // Quilez:
  4655. const float x = sample_dists.y;
  4656. const float w2 = x*x*x*(x*(x*6.0 - 15.0) + 10.0);
  4657. weights = float4(0.0, 1.0 - w2, w2, 0.0);
  4658. }
  4659. else if(beam_horiz_filter < 1.5)
  4660. {
  4661. // Gaussian:
  4662. float inner_denom_inv = 1.0/(2.0*beam_horiz_sigma*beam_horiz_sigma);
  4663. weights = exp(-(sample_dists*sample_dists)*inner_denom_inv);
  4664. }
  4665. else
  4666. {
  4667. // Lanczos2:
  4668. const float4 pi_dists = FIX_ZERO(sample_dists * pi);
  4669. weights = 2.0 * sin(pi_dists) * sin(pi_dists * 0.5) /
  4670. (pi_dists * pi_dists);
  4671. }
  4672. // Ensure the weight sum == 1.0:
  4673. const float4 final_weights = weights/dot(weights, float4(1.0));
  4674. // Get the interpolated horizontal scanline color:
  4675. const float2 uv_step_x = float2(texture_size_inv.x, 0.0);
  4676. return get_scanline_color(
  4677. tex, prev_texel_hor_uv, uv_step_x, final_weights);
  4678. }
  4679. float3 sample_rgb_scanline_horizontal(const sampler2D tex,
  4680. const float2 tex_uv, const float2 tex_size,
  4681. const float2 texture_size_inv)
  4682. {
  4683. // TODO: Add function requirements.
  4684. // Rely on a helper to make convergence easier.
  4685. if(beam_misconvergence)
  4686. {
  4687. const float3 convergence_offsets_rgb =
  4688. get_convergence_offsets_x_vector();
  4689. const float3 offset_u_rgb =
  4690. convergence_offsets_rgb * texture_size_inv.xxx;
  4691. const float2 scanline_uv_r = tex_uv - float2(offset_u_rgb.r, 0.0);
  4692. const float2 scanline_uv_g = tex_uv - float2(offset_u_rgb.g, 0.0);
  4693. const float2 scanline_uv_b = tex_uv - float2(offset_u_rgb.b, 0.0);
  4694. const float3 sample_r = sample_single_scanline_horizontal(
  4695. tex, scanline_uv_r, tex_size, texture_size_inv);
  4696. const float3 sample_g = sample_single_scanline_horizontal(
  4697. tex, scanline_uv_g, tex_size, texture_size_inv);
  4698. const float3 sample_b = sample_single_scanline_horizontal(
  4699. tex, scanline_uv_b, tex_size, texture_size_inv);
  4700. return float3(sample_r.r, sample_g.g, sample_b.b);
  4701. }
  4702. else
  4703. {
  4704. return sample_single_scanline_horizontal(tex, tex_uv, tex_size,
  4705. texture_size_inv);
  4706. }
  4707. }
  4708. float2 get_last_scanline_uv(const float2 tex_uv, const float2 tex_size,
  4709. const float2 texture_size_inv, const float2 il_step_multiple,
  4710. const float frame_count, out float dist)
  4711. {
  4712. // Compute texture coords for the last/upper scanline, accounting for
  4713. // interlacing: With interlacing, only consider even/odd scanlines every
  4714. // other frame. Top-field first (TFF) order puts even scanlines on even
  4715. // frames, and BFF order puts them on odd frames. Texels are centered at:
  4716. // frac(tex_uv * tex_size) == x.5
  4717. // Caution: If these coordinates ever seem incorrect, first make sure it's
  4718. // not because anisotropic filtering is blurring across field boundaries.
  4719. // Note: TFF/BFF won't matter for sources that double-weave or similar.
  4720. // wtf fixme
  4721. // const float interlace_bff1 = 1.0;
  4722. const float field_offset = floor(il_step_multiple.y * 0.75) *
  4723. fmod(frame_count + float(interlace_bff), 2.0);
  4724. const float2 curr_texel = tex_uv * tex_size;
  4725. // Use under_half to fix a rounding bug right around exact texel locations.
  4726. const float2 prev_texel_num = floor(curr_texel - float2(under_half));
  4727. const float wrong_field = fmod(
  4728. prev_texel_num.y + field_offset, il_step_multiple.y);
  4729. const float2 scanline_texel_num = prev_texel_num - float2(0.0, wrong_field);
  4730. // Snap to the center of the previous scanline in the current field:
  4731. const float2 scanline_texel = scanline_texel_num + float2(0.5);
  4732. const float2 scanline_uv = scanline_texel * texture_size_inv;
  4733. // Save the sample's distance from the scanline, in units of scanlines:
  4734. dist = (curr_texel.y - scanline_texel.y)/il_step_multiple.y;
  4735. return scanline_uv;
  4736. }
  4737. inline bool is_interlaced(float num_lines)
  4738. {
  4739. // Detect interlacing based on the number of lines in the source.
  4740. if(interlace_detect)
  4741. {
  4742. // NTSC: 525 lines, 262.5/field; 486 active (2 half-lines), 243/field
  4743. // NTSC Emulators: Typically 224 or 240 lines
  4744. // PAL: 625 lines, 312.5/field; 576 active (typical), 288/field
  4745. // PAL Emulators: ?
  4746. // ATSC: 720p, 1080i, 1080p
  4747. // Where do we place our cutoffs? Assumptions:
  4748. // 1.) We only need to care about active lines.
  4749. // 2.) Anything > 288 and <= 576 lines is probably interlaced.
  4750. // 3.) Anything > 576 lines is probably not interlaced...
  4751. // 4.) ...except 1080 lines, which is a crapshoot (user decision).
  4752. // 5.) Just in case the main program uses calculated video sizes,
  4753. // we should nudge the float thresholds a bit.
  4754. const bool sd_interlace = ((num_lines > 288.5) && (num_lines < 576.5));
  4755. const bool hd_interlace = bool(interlace_1080i) ?
  4756. ((num_lines > 1079.5) && (num_lines < 1080.5)) :
  4757. false;
  4758. return (sd_interlace || hd_interlace);
  4759. }
  4760. else
  4761. {
  4762. return false;
  4763. }
  4764. }
  4765. #endif // SCANLINE_FUNCTIONS_H
  4766. ///////////////////////////// END SCANLINE-FUNCTIONS ////////////////////////////
  4767. //#include "phosphor-mask-resizing.h"
  4768. //////////////////////// BEGIN PHOSPHOR-MASK-RESIZING ////////////////////////
  4769. #ifndef PHOSPHOR_MASK_RESIZING_H
  4770. #define PHOSPHOR_MASK_RESIZING_H
  4771. ///////////////////////////// GPL LICENSE NOTICE /////////////////////////////
  4772. // crt-royale: A full-featured CRT shader, with cheese.
  4773. // Copyright (C) 2014 TroggleMonkey <trogglemonkey@gmx.com>
  4774. //
  4775. // This program is free software; you can redistribute it and/or modify it
  4776. // under the terms of the GNU General Public License as published by the Free
  4777. // Software Foundation; either version 2 of the License, or any later version.
  4778. //
  4779. // This program is distributed in the hope that it will be useful, but WITHOUT
  4780. // ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
  4781. // FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
  4782. // more details.
  4783. //
  4784. // You should have received a copy of the GNU General Public License along with
  4785. // this program; if not, write to the Free Software Foundation, Inc., 59 Temple
  4786. // Place, Suite 330, Boston, MA 02111-1307 USA
  4787. ////////////////////////////////// INCLUDES //////////////////////////////////
  4788. //#include "../user-settings.h"
  4789. //#include "derived-settings-and-constants.h"
  4790. ///////////////////////////// CODEPATH SELECTION /////////////////////////////
  4791. // Choose a looping strategy based on what's allowed:
  4792. // Dynamic loops not allowed: Use a flat static loop.
  4793. // Dynamic loops accomodated: Coarsely branch around static loops.
  4794. // Dynamic loops assumed allowed: Use a flat dynamic loop.
  4795. #ifndef DRIVERS_ALLOW_DYNAMIC_BRANCHES
  4796. #ifdef ACCOMODATE_POSSIBLE_DYNAMIC_LOOPS
  4797. #define BREAK_LOOPS_INTO_PIECES
  4798. #else
  4799. #define USE_SINGLE_STATIC_LOOP
  4800. #endif
  4801. #endif // No else needed: Dynamic loops assumed.
  4802. ////////////////////////////////// CONSTANTS /////////////////////////////////
  4803. // The larger the resized tile, the fewer samples we'll need for downsizing.
  4804. // See if we can get a static min tile size > mask_min_allowed_tile_size:
  4805. static const float mask_min_allowed_tile_size = ceil(
  4806. mask_min_allowed_triad_size * mask_triads_per_tile);
  4807. static const float mask_min_expected_tile_size =
  4808. mask_min_allowed_tile_size;
  4809. // Limit the number of sinc resize taps by the maximum minification factor:
  4810. static const float pi_over_lobes = pi/mask_sinc_lobes;
  4811. static const float max_sinc_resize_samples_float = 2.0 * mask_sinc_lobes *
  4812. mask_resize_src_lut_size.x/mask_min_expected_tile_size;
  4813. // Vectorized loops sample in multiples of 4. Round up to be safe:
  4814. static const float max_sinc_resize_samples_m4 = ceil(
  4815. max_sinc_resize_samples_float * 0.25) * 4.0;
  4816. ///////////////////////// RESAMPLING FUNCTION HELPERS ////////////////////////
  4817. inline float get_dynamic_loop_size(const float magnification_scale)
  4818. {
  4819. // Requires: The following global constants must be defined:
  4820. // 1.) mask_sinc_lobes
  4821. // 2.) max_sinc_resize_samples_m4
  4822. // Returns: The minimum number of texture samples for a correct downsize
  4823. // at magnification_scale.
  4824. // We're downsizing, so the filter is sized across 2*lobes output pixels
  4825. // (not 2*lobes input texels). This impacts distance measurements and the
  4826. // minimum number of input samples needed.
  4827. const float min_samples_float = 2.0 * mask_sinc_lobes / magnification_scale;
  4828. const float min_samples_m4 = ceil(min_samples_float * 0.25) * 4.0;
  4829. #ifdef DRIVERS_ALLOW_DYNAMIC_BRANCHES
  4830. const float max_samples_m4 = max_sinc_resize_samples_m4;
  4831. #else // ifdef BREAK_LOOPS_INTO_PIECES
  4832. // Simulating loops with branches imposes a 128-sample limit.
  4833. const float max_samples_m4 = min(128.0, max_sinc_resize_samples_m4);
  4834. #endif
  4835. return min(min_samples_m4, max_samples_m4);
  4836. }
  4837. float2 get_first_texel_tile_uv_and_dist(const float2 tex_uv,
  4838. const float2 tex_size, const float dr,
  4839. const float input_tiles_per_texture_r, const float samples,
  4840. static const bool vertical)
  4841. {
  4842. // Requires: 1.) dr == du == 1.0/texture_size.x or
  4843. // dr == dv == 1.0/texture_size.y
  4844. // (whichever direction we're resampling in).
  4845. // It's a scalar to save register space.
  4846. // 2.) input_tiles_per_texture_r is the number of input tiles
  4847. // that can fit in the input texture in the direction we're
  4848. // resampling this pass.
  4849. // 3.) vertical indicates whether we're resampling vertically
  4850. // this pass (or horizontally).
  4851. // Returns: Pack and return the first sample's tile_uv coord in [0, 1]
  4852. // and its texel distance from the destination pixel, in the
  4853. // resized dimension only.
  4854. // We'll start with the topmost or leftmost sample and work down or right,
  4855. // so get the first sample location and distance. Modify both dimensions
  4856. // as if we're doing a one-pass 2D resize; we'll throw away the unneeded
  4857. // (and incorrect) dimension at the end.
  4858. const float2 curr_texel = tex_uv * tex_size;
  4859. const float2 prev_texel =
  4860. floor(curr_texel - float2(under_half)) + float2(0.5);
  4861. const float2 first_texel = prev_texel - float2(samples/2.0 - 1.0);
  4862. const float2 first_texel_uv_wrap_2D = first_texel * dr;
  4863. const float2 first_texel_dist_2D = curr_texel - first_texel;
  4864. // Convert from tex_uv to tile_uv coords so we can sub fracs for fmods.
  4865. const float2 first_texel_tile_uv_wrap_2D =
  4866. first_texel_uv_wrap_2D * input_tiles_per_texture_r;
  4867. // Project wrapped coordinates to the [0, 1] range. We'll do this with all
  4868. // samples,but the first texel is special, since it might be negative.
  4869. const float2 coord_negative =
  4870. float2((first_texel_tile_uv_wrap_2D.x < 0.),(first_texel_tile_uv_wrap_2D.y < 0.));
  4871. const float2 first_texel_tile_uv_2D =
  4872. frac(first_texel_tile_uv_wrap_2D) + coord_negative;
  4873. // Pack the first texel's tile_uv coord and texel distance in 1D:
  4874. const float2 tile_u_and_dist =
  4875. float2(first_texel_tile_uv_2D.x, first_texel_dist_2D.x);
  4876. const float2 tile_v_and_dist =
  4877. float2(first_texel_tile_uv_2D.y, first_texel_dist_2D.y);
  4878. return vertical ? tile_v_and_dist : tile_u_and_dist;
  4879. //return lerp(tile_u_and_dist, tile_v_and_dist, float(vertical));
  4880. }
  4881. inline float4 tex2Dlod0try(const sampler2D tex, const float2 tex_uv)
  4882. {
  4883. // Mipmapping and anisotropic filtering get confused by sinc-resampling.
  4884. // One [slow] workaround is to select the lowest mip level:
  4885. #ifdef ANISOTROPIC_RESAMPLING_COMPAT_TEX2DLOD
  4886. return textureLod(tex, float4(tex_uv, 0.0, 0.0).xy);
  4887. #else
  4888. #ifdef ANISOTROPIC_RESAMPLING_COMPAT_TEX2DBIAS
  4889. return tex2Dbias(tex, float4(tex_uv, 0.0, -16.0));
  4890. #else
  4891. return texture(tex, tex_uv);
  4892. #endif
  4893. #endif
  4894. }
  4895. ////////////////////////////// LOOP BODY MACROS //////////////////////////////
  4896. // Using inline functions can exceed the temporary register limit, so we're
  4897. // stuck with #define macros (I'm TRULY sorry). They're declared here instead
  4898. // of above to be closer to the actual invocation sites. Steps:
  4899. // 1.) Get the exact texel location.
  4900. // 2.) Sample the phosphor mask (already assumed encoded in linear RGB).
  4901. // 3.) Get the distance from the current pixel and sinc weight:
  4902. // sinc(dist) = sin(pi * dist)/(pi * dist)
  4903. // We can also use the slower/smoother Lanczos instead:
  4904. // L(x) = sinc(dist) * sinc(dist / lobes)
  4905. // 4.) Accumulate the weight sum in weights, and accumulate the weighted texels
  4906. // in pixel_color (we'll normalize outside the loop at the end).
  4907. // We vectorize the loop to help reduce the Lanczos window's cost.
  4908. // The r coord is the coord in the dimension we're resizing along (u or v),
  4909. // and first_texel_tile_uv_rrrr is a float4 of the first texel's u or v
  4910. // tile_uv coord in [0, 1]. tex_uv_r will contain the tile_uv u or v coord
  4911. // for four new texel samples.
  4912. #define CALCULATE_R_COORD_FOR_4_SAMPLES \
  4913. const float4 true_i = float4(i_base + i) + float4(0.0, 1.0, 2.0, 3.0); \
  4914. const float4 tile_uv_r = frac( \
  4915. first_texel_tile_uv_rrrr + true_i * tile_dr); \
  4916. const float4 tex_uv_r = tile_uv_r * tile_size_uv_r;
  4917. #ifdef PHOSPHOR_MASK_RESIZE_LANCZOS_WINDOW
  4918. #define CALCULATE_SINC_RESAMPLE_WEIGHTS \
  4919. const float4 pi_dist_over_lobes = pi_over_lobes * dist; \
  4920. const float4 weights = min(sin(pi_dist) * sin(pi_dist_over_lobes) /\
  4921. (pi_dist*pi_dist_over_lobes), float4(1.0));
  4922. #else
  4923. #define CALCULATE_SINC_RESAMPLE_WEIGHTS \
  4924. const float4 weights = min(sin(pi_dist)/pi_dist, float4(1.0));
  4925. #endif
  4926. #define UPDATE_COLOR_AND_WEIGHT_SUMS \
  4927. const float4 dist = magnification_scale * \
  4928. abs(first_dist_unscaled - true_i); \
  4929. const float4 pi_dist = pi * dist; \
  4930. CALCULATE_SINC_RESAMPLE_WEIGHTS; \
  4931. pixel_color += new_sample0 * weights.xxx; \
  4932. pixel_color += new_sample1 * weights.yyy; \
  4933. pixel_color += new_sample2 * weights.zzz; \
  4934. pixel_color += new_sample3 * weights.www; \
  4935. weight_sum += weights;
  4936. #define VERTICAL_SINC_RESAMPLE_LOOP_BODY \
  4937. CALCULATE_R_COORD_FOR_4_SAMPLES; \
  4938. const float3 new_sample0 = tex2Dlod0try(tex, \
  4939. float2(tex_uv.x, tex_uv_r.x)).rgb; \
  4940. const float3 new_sample1 = tex2Dlod0try(tex, \
  4941. float2(tex_uv.x, tex_uv_r.y)).rgb; \
  4942. const float3 new_sample2 = tex2Dlod0try(tex, \
  4943. float2(tex_uv.x, tex_uv_r.z)).rgb; \
  4944. const float3 new_sample3 = tex2Dlod0try(tex, \
  4945. float2(tex_uv.x, tex_uv_r.w)).rgb; \
  4946. UPDATE_COLOR_AND_WEIGHT_SUMS;
  4947. #define HORIZONTAL_SINC_RESAMPLE_LOOP_BODY \
  4948. CALCULATE_R_COORD_FOR_4_SAMPLES; \
  4949. const float3 new_sample0 = tex2Dlod0try(tex, \
  4950. float2(tex_uv_r.x, tex_uv.y)).rgb; \
  4951. const float3 new_sample1 = tex2Dlod0try(tex, \
  4952. float2(tex_uv_r.y, tex_uv.y)).rgb; \
  4953. const float3 new_sample2 = tex2Dlod0try(tex, \
  4954. float2(tex_uv_r.z, tex_uv.y)).rgb; \
  4955. const float3 new_sample3 = tex2Dlod0try(tex, \
  4956. float2(tex_uv_r.w, tex_uv.y)).rgb; \
  4957. UPDATE_COLOR_AND_WEIGHT_SUMS;
  4958. //////////////////////////// RESAMPLING FUNCTIONS ////////////////////////////
  4959. float3 downsample_vertical_sinc_tiled(const sampler2D tex,
  4960. const float2 tex_uv, const float2 tex_size, static const float dr,
  4961. const float magnification_scale, static const float tile_size_uv_r)
  4962. {
  4963. // Requires: 1.) dr == du == 1.0/texture_size.x or
  4964. // dr == dv == 1.0/texture_size.y
  4965. // (whichever direction we're resampling in).
  4966. // It's a scalar to save register space.
  4967. // 2.) tile_size_uv_r is the number of texels an input tile
  4968. // takes up in the input texture, in the direction we're
  4969. // resampling this pass.
  4970. // 3.) magnification_scale must be <= 1.0.
  4971. // Returns: Return a [Lanczos] sinc-resampled pixel of a vertically
  4972. // downsized input tile embedded in an input texture. (The
  4973. // vertical version is special-cased though: It assumes the
  4974. // tile size equals the [static] texture size, since it's used
  4975. // on an LUT texture input containing one tile. For more
  4976. // generic use, eliminate the "static" in the parameters.)
  4977. // The "r" in "dr," "tile_size_uv_r," etc. refers to the dimension
  4978. // we're resizing along, e.g. "dy" in this case.
  4979. #ifdef USE_SINGLE_STATIC_LOOP
  4980. // A static loop can be faster, but it might blur too much from using
  4981. // more samples than it should.
  4982. static const int samples = int(max_sinc_resize_samples_m4);
  4983. #else
  4984. const int samples = int(get_dynamic_loop_size(magnification_scale));
  4985. #endif
  4986. // Get the first sample location (scalar tile uv coord along the resized
  4987. // dimension) and distance from the output location (in texels):
  4988. static const float input_tiles_per_texture_r = 1.0/tile_size_uv_r;
  4989. // true = vertical resize:
  4990. const float2 first_texel_tile_r_and_dist = get_first_texel_tile_uv_and_dist(
  4991. tex_uv, tex_size, dr, input_tiles_per_texture_r, samples, true);
  4992. const float4 first_texel_tile_uv_rrrr = first_texel_tile_r_and_dist.xxxx;
  4993. const float4 first_dist_unscaled = first_texel_tile_r_and_dist.yyyy;
  4994. // Get the tile sample offset:
  4995. static const float tile_dr = dr * input_tiles_per_texture_r;
  4996. // Sum up each weight and weighted sample color, varying the looping
  4997. // strategy based on our expected dynamic loop capabilities. See the
  4998. // loop body macros above.
  4999. int i_base = 0;
  5000. float4 weight_sum = float4(0.0);
  5001. float3 pixel_color = float3(0.0);
  5002. static const int i_step = 4;
  5003. #ifdef BREAK_LOOPS_INTO_PIECES
  5004. if(samples - i_base >= 64)
  5005. {
  5006. for(int i = 0; i < 64; i += i_step)
  5007. {
  5008. VERTICAL_SINC_RESAMPLE_LOOP_BODY;
  5009. }
  5010. i_base += 64;
  5011. }
  5012. if(samples - i_base >= 32)
  5013. {
  5014. for(int i = 0; i < 32; i += i_step)
  5015. {
  5016. VERTICAL_SINC_RESAMPLE_LOOP_BODY;
  5017. }
  5018. i_base += 32;
  5019. }
  5020. if(samples - i_base >= 16)
  5021. {
  5022. for(int i = 0; i < 16; i += i_step)
  5023. {
  5024. VERTICAL_SINC_RESAMPLE_LOOP_BODY;
  5025. }
  5026. i_base += 16;
  5027. }
  5028. if(samples - i_base >= 8)
  5029. {
  5030. for(int i = 0; i < 8; i += i_step)
  5031. {
  5032. VERTICAL_SINC_RESAMPLE_LOOP_BODY;
  5033. }
  5034. i_base += 8;
  5035. }
  5036. if(samples - i_base >= 4)
  5037. {
  5038. for(int i = 0; i < 4; i += i_step)
  5039. {
  5040. VERTICAL_SINC_RESAMPLE_LOOP_BODY;
  5041. }
  5042. i_base += 4;
  5043. }
  5044. // Do another 4-sample block for a total of 128 max samples.
  5045. if(samples - i_base > 0)
  5046. {
  5047. for(int i = 0; i < 4; i += i_step)
  5048. {
  5049. VERTICAL_SINC_RESAMPLE_LOOP_BODY;
  5050. }
  5051. }
  5052. #else
  5053. for(int i = 0; i < samples; i += i_step)
  5054. {
  5055. VERTICAL_SINC_RESAMPLE_LOOP_BODY;
  5056. }
  5057. #endif
  5058. // Normalize so the weight_sum == 1.0, and return:
  5059. const float2 weight_sum_reduce = weight_sum.xy + weight_sum.zw;
  5060. const float3 scalar_weight_sum = float3(weight_sum_reduce.x +
  5061. weight_sum_reduce.y);
  5062. return (pixel_color/scalar_weight_sum);
  5063. }
  5064. float3 downsample_horizontal_sinc_tiled(const sampler2D tex,
  5065. const float2 tex_uv, const float2 tex_size, const float dr,
  5066. const float magnification_scale, const float tile_size_uv_r)
  5067. {
  5068. // Differences from downsample_horizontal_sinc_tiled:
  5069. // 1.) The dr and tile_size_uv_r parameters are not static consts.
  5070. // 2.) The "vertical" parameter to get_first_texel_tile_uv_and_dist is
  5071. // set to false instead of true.
  5072. // 3.) The horizontal version of the loop body is used.
  5073. // TODO: If we can get guaranteed compile-time dead code elimination,
  5074. // we can combine the vertical/horizontal downsampling functions by:
  5075. // 1.) Add an extra static const bool parameter called "vertical."
  5076. // 2.) Supply it with the result of get_first_texel_tile_uv_and_dist().
  5077. // 3.) Use a conditional assignment in the loop body macro. This is the
  5078. // tricky part: We DO NOT want to incur the extra conditional
  5079. // assignment in the inner loop at runtime!
  5080. // The "r" in "dr," "tile_size_uv_r," etc. refers to the dimension
  5081. // we're resizing along, e.g. "dx" in this case.
  5082. #ifdef USE_SINGLE_STATIC_LOOP
  5083. // If we have to load all samples, we might as well use them.
  5084. static const int samples = int(max_sinc_resize_samples_m4);
  5085. #else
  5086. const int samples = int(get_dynamic_loop_size(magnification_scale));
  5087. #endif
  5088. // Get the first sample location (scalar tile uv coord along resized
  5089. // dimension) and distance from the output location (in texels):
  5090. const float input_tiles_per_texture_r = 1.0/tile_size_uv_r;
  5091. // false = horizontal resize:
  5092. const float2 first_texel_tile_r_and_dist = get_first_texel_tile_uv_and_dist(
  5093. tex_uv, tex_size, dr, input_tiles_per_texture_r, samples, false);
  5094. const float4 first_texel_tile_uv_rrrr = first_texel_tile_r_and_dist.xxxx;
  5095. const float4 first_dist_unscaled = first_texel_tile_r_and_dist.yyyy;
  5096. // Get the tile sample offset:
  5097. const float tile_dr = dr * input_tiles_per_texture_r;
  5098. // Sum up each weight and weighted sample color, varying the looping
  5099. // strategy based on our expected dynamic loop capabilities. See the
  5100. // loop body macros above.
  5101. int i_base = 0;
  5102. float4 weight_sum = float4(0.0);
  5103. float3 pixel_color = float3(0.0);
  5104. static const int i_step = 4;
  5105. #ifdef BREAK_LOOPS_INTO_PIECES
  5106. if(samples - i_base >= 64)
  5107. {
  5108. for(int i = 0; i < 64; i += i_step)
  5109. {
  5110. HORIZONTAL_SINC_RESAMPLE_LOOP_BODY;
  5111. }
  5112. i_base += 64;
  5113. }
  5114. if(samples - i_base >= 32)
  5115. {
  5116. for(int i = 0; i < 32; i += i_step)
  5117. {
  5118. HORIZONTAL_SINC_RESAMPLE_LOOP_BODY;
  5119. }
  5120. i_base += 32;
  5121. }
  5122. if(samples - i_base >= 16)
  5123. {
  5124. for(int i = 0; i < 16; i += i_step)
  5125. {
  5126. HORIZONTAL_SINC_RESAMPLE_LOOP_BODY;
  5127. }
  5128. i_base += 16;
  5129. }
  5130. if(samples - i_base >= 8)
  5131. {
  5132. for(int i = 0; i < 8; i += i_step)
  5133. {
  5134. HORIZONTAL_SINC_RESAMPLE_LOOP_BODY;
  5135. }
  5136. i_base += 8;
  5137. }
  5138. if(samples - i_base >= 4)
  5139. {
  5140. for(int i = 0; i < 4; i += i_step)
  5141. {
  5142. HORIZONTAL_SINC_RESAMPLE_LOOP_BODY;
  5143. }
  5144. i_base += 4;
  5145. }
  5146. // Do another 4-sample block for a total of 128 max samples.
  5147. if(samples - i_base > 0)
  5148. {
  5149. for(int i = 0; i < 4; i += i_step)
  5150. {
  5151. HORIZONTAL_SINC_RESAMPLE_LOOP_BODY;
  5152. }
  5153. }
  5154. #else
  5155. for(int i = 0; i < samples; i += i_step)
  5156. {
  5157. HORIZONTAL_SINC_RESAMPLE_LOOP_BODY;
  5158. }
  5159. #endif
  5160. // Normalize so the weight_sum == 1.0, and return:
  5161. const float2 weight_sum_reduce = weight_sum.xy + weight_sum.zw;
  5162. const float3 scalar_weight_sum = float3(weight_sum_reduce.x +
  5163. weight_sum_reduce.y);
  5164. return (pixel_color/scalar_weight_sum);
  5165. }
  5166. //////////////////////////// TILE SIZE CALCULATION ///////////////////////////
  5167. float2 get_resized_mask_tile_size(const float2 estimated_viewport_size,
  5168. const float2 estimated_mask_resize_output_size,
  5169. const bool solemnly_swear_same_inputs_for_every_pass)
  5170. {
  5171. // Requires: The following global constants must be defined according to
  5172. // certain constraints:
  5173. // 1.) mask_resize_num_triads: Must be high enough that our
  5174. // mask sampling method won't have artifacts later
  5175. // (long story; see derived-settings-and-constants.h)
  5176. // 2.) mask_resize_src_lut_size: Texel size of our mask LUT
  5177. // 3.) mask_triads_per_tile: Num horizontal triads in our LUT
  5178. // 4.) mask_min_allowed_triad_size: User setting (the more
  5179. // restrictive it is, the faster the resize will go)
  5180. // 5.) mask_min_allowed_tile_size_x < mask_resize_src_lut_size.x
  5181. // 6.) mask_triad_size_desired_{runtime, static}
  5182. // 7.) mask_num_triads_desired_{runtime, static}
  5183. // 8.) mask_specify_num_triads must be 0.0/1.0 (false/true)
  5184. // The function parameters must be defined as follows:
  5185. // 1.) estimated_viewport_size == (final viewport size);
  5186. // If mask_specify_num_triads is 1.0/true and the viewport
  5187. // estimate is wrong, the number of triads will differ from
  5188. // the user's preference by about the same factor.
  5189. // 2.) estimated_mask_resize_output_size: Must equal the
  5190. // output size of the MASK_RESIZE pass.
  5191. // Exception: The x component may be estimated garbage if
  5192. // and only if the caller throws away the x result.
  5193. // 3.) solemnly_swear_same_inputs_for_every_pass: Set to false,
  5194. // unless you can guarantee that every call across every
  5195. // pass will use the same sizes for the other parameters.
  5196. // When calling this across multiple passes, always use the
  5197. // same y viewport size/scale, and always use the same x
  5198. // viewport size/scale when using the x result.
  5199. // Returns: Return the final size of a manually resized mask tile, after
  5200. // constraining the desired size to avoid artifacts. Under
  5201. // unusual circumstances, tiles may become stretched vertically
  5202. // (see wall of text below).
  5203. // Stated tile properties must be correct:
  5204. static const float tile_aspect_ratio_inv =
  5205. mask_resize_src_lut_size.y/mask_resize_src_lut_size.x;
  5206. static const float tile_aspect_ratio = 1.0/tile_aspect_ratio_inv;
  5207. static const float2 tile_aspect = float2(1.0, tile_aspect_ratio_inv);
  5208. // If mask_specify_num_triads is 1.0/true and estimated_viewport_size.x is
  5209. // wrong, the user preference will be misinterpreted:
  5210. const float desired_tile_size_x = mask_triads_per_tile * lerp(
  5211. mask_triad_size_desired,
  5212. estimated_viewport_size.x / mask_num_triads_desired,
  5213. mask_specify_num_triads);
  5214. if(get_mask_sample_mode() > 0.5)
  5215. {
  5216. // We don't need constraints unless we're sampling MASK_RESIZE.
  5217. return desired_tile_size_x * tile_aspect;
  5218. }
  5219. // Make sure we're not upsizing:
  5220. const float temp_tile_size_x =
  5221. min(desired_tile_size_x, mask_resize_src_lut_size.x);
  5222. // Enforce min_tile_size and max_tile_size in both dimensions:
  5223. const float2 temp_tile_size = temp_tile_size_x * tile_aspect;
  5224. static const float2 min_tile_size =
  5225. mask_min_allowed_tile_size * tile_aspect;
  5226. const float2 max_tile_size =
  5227. estimated_mask_resize_output_size / mask_resize_num_tiles;
  5228. const float2 clamped_tile_size =
  5229. clamp(temp_tile_size, min_tile_size, max_tile_size);
  5230. // Try to maintain tile_aspect_ratio. This is the tricky part:
  5231. // If we're currently resizing in the y dimension, the x components
  5232. // could be MEANINGLESS. (If estimated_mask_resize_output_size.x is
  5233. // bogus, then so is max_tile_size.x and clamped_tile_size.x.)
  5234. // We can't adjust the y size based on clamped_tile_size.x. If it
  5235. // clamps when it shouldn't, it won't clamp again when later passes
  5236. // call this function with the correct sizes, and the discrepancy will
  5237. // break the sampling coords in MASKED_SCANLINES. Instead, we'll limit
  5238. // the x size based on the y size, but not vice versa, unless the
  5239. // caller swears the parameters were the same (correct) in every pass.
  5240. // As a result, triads could appear vertically stretched if:
  5241. // a.) mask_resize_src_lut_size.x > mask_resize_src_lut_size.y: Wide
  5242. // LUT's might clamp x more than y (all provided LUT's are square)
  5243. // b.) true_viewport_size.x < true_viewport_size.y: The user is playing
  5244. // with a vertically oriented screen (not accounted for anyway)
  5245. // c.) mask_resize_viewport_scale.x < masked_resize_viewport_scale.y:
  5246. // Viewport scales are equal by default.
  5247. // If any of these are the case, you can fix the stretching by setting:
  5248. // mask_resize_viewport_scale.x = mask_resize_viewport_scale.y *
  5249. // (1.0 / min_expected_aspect_ratio) *
  5250. // (mask_resize_src_lut_size.x / mask_resize_src_lut_size.y)
  5251. const float x_tile_size_from_y =
  5252. clamped_tile_size.y * tile_aspect_ratio;
  5253. const float y_tile_size_from_x = lerp(clamped_tile_size.y,
  5254. clamped_tile_size.x * tile_aspect_ratio_inv,
  5255. float(solemnly_swear_same_inputs_for_every_pass));
  5256. const float2 reclamped_tile_size = float2(
  5257. min(clamped_tile_size.x, x_tile_size_from_y),
  5258. min(clamped_tile_size.y, y_tile_size_from_x));
  5259. // We need integer tile sizes in both directions for tiled sampling to
  5260. // work correctly. Use floor (to make sure we don't round up), but be
  5261. // careful to avoid a rounding bug where floor decreases whole numbers:
  5262. const float2 final_resized_tile_size =
  5263. floor(reclamped_tile_size + float2(FIX_ZERO(0.0)));
  5264. return final_resized_tile_size;
  5265. }
  5266. ///////////////////////// FINAL MASK SAMPLING HELPERS ////////////////////////
  5267. float4 get_mask_sampling_parameters(const float2 mask_resize_texture_size,
  5268. const float2 mask_resize_video_size, const float2 true_viewport_size,
  5269. out float2 mask_tiles_per_screen)
  5270. {
  5271. // Requires: 1.) Requirements of get_resized_mask_tile_size() must be
  5272. // met, particularly regarding global constants.
  5273. // The function parameters must be defined as follows:
  5274. // 1.) mask_resize_texture_size == MASK_RESIZE.texture_size
  5275. // if get_mask_sample_mode() is 0 (otherwise anything)
  5276. // 2.) mask_resize_video_size == MASK_RESIZE.video_size
  5277. // if get_mask_sample_mode() is 0 (otherwise anything)
  5278. // 3.) true_viewport_size == output_size for a pass set to
  5279. // 1.0 viewport scale (i.e. it must be correct)
  5280. // Returns: Return a float4 containing:
  5281. // xy: tex_uv coords for the start of the mask tile
  5282. // zw: tex_uv size of the mask tile from start to end
  5283. // mask_tiles_per_screen is an out parameter containing the
  5284. // number of mask tiles that will fit on the screen.
  5285. // First get the final resized tile size. The viewport size and mask
  5286. // resize viewport scale must be correct, but don't solemnly swear they
  5287. // were correct in both mask resize passes unless you know it's true.
  5288. // (We can better ensure a correct tile aspect ratio if the parameters are
  5289. // guaranteed correct in all passes...but if we lie, we'll get inconsistent
  5290. // sizes across passes, resulting in broken texture coordinates.)
  5291. const float mask_sample_mode = get_mask_sample_mode();
  5292. const float2 mask_resize_tile_size = get_resized_mask_tile_size(
  5293. true_viewport_size, mask_resize_video_size, false);
  5294. if(mask_sample_mode < 0.5)
  5295. {
  5296. // Sample MASK_RESIZE: The resized tile is a fraction of the texture
  5297. // size and starts at a nonzero offset to allow for border texels:
  5298. const float2 mask_tile_uv_size = mask_resize_tile_size /
  5299. mask_resize_texture_size;
  5300. const float2 skipped_tiles = mask_start_texels/mask_resize_tile_size;
  5301. const float2 mask_tile_start_uv = skipped_tiles * mask_tile_uv_size;
  5302. // mask_tiles_per_screen must be based on the *true* viewport size:
  5303. mask_tiles_per_screen = true_viewport_size / mask_resize_tile_size;
  5304. return float4(mask_tile_start_uv, mask_tile_uv_size);
  5305. }
  5306. else
  5307. {
  5308. // If we're tiling at the original size (1:1 pixel:texel), redefine a
  5309. // "tile" to be the full texture containing many triads. Otherwise,
  5310. // we're hardware-resampling an LUT, and the texture truly contains a
  5311. // single unresized phosphor mask tile anyway.
  5312. static const float2 mask_tile_uv_size = float2(1.0);
  5313. static const float2 mask_tile_start_uv = float2(0.0);
  5314. if(mask_sample_mode > 1.5)
  5315. {
  5316. // Repeat the full LUT at a 1:1 pixel:texel ratio without resizing:
  5317. mask_tiles_per_screen = true_viewport_size/mask_texture_large_size;
  5318. }
  5319. else
  5320. {
  5321. // Hardware-resize the original LUT:
  5322. mask_tiles_per_screen = true_viewport_size / mask_resize_tile_size;
  5323. }
  5324. return float4(mask_tile_start_uv, mask_tile_uv_size);
  5325. }
  5326. }
  5327. /*
  5328. float2 fix_tiling_discontinuities_normalized(const float2 tile_uv,
  5329. float2 duv_dx, float2 duv_dy)
  5330. {
  5331. // Requires: 1.) duv_dx == ddx(tile_uv)
  5332. // 2.) duv_dy == ddy(tile_uv)
  5333. // 3.) tile_uv contains tile-relative uv coords in [0, 1],
  5334. // such that (0.5, 0.5) is the center of a tile, etc.
  5335. // ("Tile" can mean texture, the video embedded in the
  5336. // texture, or some other "tile" embedded in a texture.)
  5337. // Returns: Return new tile_uv coords that contain no discontinuities
  5338. // across a 2x2 pixel quad.
  5339. // Description:
  5340. // When uv coords wrap from 1.0 to 0.0, they create a discontinuity in the
  5341. // derivatives, which we assume happened if the absolute difference between
  5342. // any fragment in a 2x2 block is > ~half a tile. If the current block has
  5343. // a u or v discontinuity and the current fragment is in the first half of
  5344. // the tile along that axis (i.e. it wrapped from 1.0 to 0.0), add a tile
  5345. // to that coord to make the 2x2 block continuous. (It will now have a
  5346. // coord > 1.0 in the padding area beyond the tile.) This function takes
  5347. // derivatives as parameters so the caller can reuse them.
  5348. // In case we're using high-quality (nVidia-style) derivatives, ensure
  5349. // diagonically opposite fragments see each other for correctness:
  5350. duv_dx = abs(duv_dx) + abs(ddy(duv_dx));
  5351. duv_dy = abs(duv_dy) + abs(ddx(duv_dy));
  5352. const float2 pixel_in_first_half_tile = float2((tile_uv.x < 0.5),(tile_uv.y < 0.5));
  5353. const float2 jump_exists = float2(((duv_dx + duv_dy).x > 0.5),((duv_dx + duv_dy).y > 0.5));
  5354. return tile_uv + jump_exists * pixel_in_first_half_tile;
  5355. }
  5356. */
  5357. float2 convert_phosphor_tile_uv_wrap_to_tex_uv(const float2 tile_uv_wrap,
  5358. const float4 mask_tile_start_uv_and_size)
  5359. {
  5360. // Requires: 1.) tile_uv_wrap contains tile-relative uv coords, where the
  5361. // tile spans from [0, 1], such that (0.5, 0.5) is at the
  5362. // tile center. The input coords can range from [0, inf],
  5363. // and their fractional parts map to a repeated tile.
  5364. // ("Tile" can mean texture, the video embedded in the
  5365. // texture, or some other "tile" embedded in a texture.)
  5366. // 2.) mask_tile_start_uv_and_size.xy contains tex_uv coords
  5367. // for the start of the embedded tile in the full texture.
  5368. // 3.) mask_tile_start_uv_and_size.zw contains the [fractional]
  5369. // tex_uv size of the embedded tile in the full texture.
  5370. // Returns: Return tex_uv coords (used for texture sampling)
  5371. // corresponding to tile_uv_wrap.
  5372. if(get_mask_sample_mode() < 0.5)
  5373. {
  5374. // Manually repeat the resized mask tile to fill the screen:
  5375. // First get fractional tile_uv coords. Using frac/fmod on coords
  5376. // confuses anisotropic filtering; fix it as user options dictate.
  5377. // derived-settings-and-constants.h disables incompatible options.
  5378. #ifdef ANISOTROPIC_TILING_COMPAT_TILE_FLAT_TWICE
  5379. float2 tile_uv = frac(tile_uv_wrap * 0.5) * 2.0;
  5380. #else
  5381. float2 tile_uv = frac(tile_uv_wrap);
  5382. #endif
  5383. #ifdef ANISOTROPIC_TILING_COMPAT_FIX_DISCONTINUITIES
  5384. const float2 tile_uv_dx = ddx(tile_uv);
  5385. const float2 tile_uv_dy = ddy(tile_uv);
  5386. tile_uv = fix_tiling_discontinuities_normalized(tile_uv,
  5387. tile_uv_dx, tile_uv_dy);
  5388. #endif
  5389. // The tile is embedded in a padded FBO, and it may start at a
  5390. // nonzero offset if border texels are used to avoid artifacts:
  5391. const float2 mask_tex_uv = mask_tile_start_uv_and_size.xy +
  5392. tile_uv * mask_tile_start_uv_and_size.zw;
  5393. return mask_tex_uv;
  5394. }
  5395. else
  5396. {
  5397. // Sample from the input phosphor mask texture with hardware tiling.
  5398. // If we're tiling at the original size (mode 2), the "tile" is the
  5399. // whole texture, and it contains a large number of triads mapped with
  5400. // a 1:1 pixel:texel ratio. OTHERWISE, the texture contains a single
  5401. // unresized tile. tile_uv_wrap already has correct coords for both!
  5402. return tile_uv_wrap;
  5403. }
  5404. }
  5405. #endif // PHOSPHOR_MASK_RESIZING_H
  5406. ///////////////////////// END PHOSPHOR-MASK-RESIZING /////////////////////////
  5407. //#include "../../../../include/gamma-management.h"
  5408. // already got it
  5409. //////////////////////////////// END INCLUDES ////////////////////////////////
  5410. /////////////////////////////////// HELPERS //////////////////////////////////
  5411. inline float4 tex2Dtiled_mask_linearize(const sampler2D tex,
  5412. const float2 tex_uv)
  5413. {
  5414. // If we're manually tiling a texture, anisotropic filtering can get
  5415. // confused. One workaround is to just select the lowest mip level:
  5416. #ifdef PHOSPHOR_MASK_MANUALLY_RESIZE
  5417. #ifdef ANISOTROPIC_TILING_COMPAT_TEX2DLOD
  5418. // TODO: Use tex2Dlod_linearize with a calculated mip level.
  5419. return tex2Dlod_linearize(tex, float4(tex_uv, 0.0, 0.0));
  5420. #else
  5421. #ifdef ANISOTROPIC_TILING_COMPAT_TEX2DBIAS
  5422. return tex2Dbias_linearize(tex, float4(tex_uv, 0.0, -16.0));
  5423. #else
  5424. return tex2D_linearize(tex, tex_uv);
  5425. #endif
  5426. #endif
  5427. #else
  5428. return tex2D_linearize(tex, tex_uv);
  5429. #endif
  5430. }
  5431. #undef COMPAT_PRECISION
  5432. #undef COMPAT_TEXTURE
  5433. // END VERTEX INCLUDES //
  5434. float bloom_approx_scale_x = targetSize.x / sourceSize[0].y;
  5435. const float max_viewport_size_x = 1080.0*1024.0*(4.0/3.0);
  5436. void main() {
  5437. gl_Position = position;
  5438. vTexCoord = texCoord * 1.00001;
  5439. float2 tex_uv = vTexCoord.xy;
  5440. // Our various input textures use different coords.
  5441. video_uv = tex_uv * texture_size/video_size;
  5442. scanline_texture_size_inv =
  5443. float2(1.0, 1.0)/VERTICAL_SCANLINEStexture_size;
  5444. //video_uv = video_uv;
  5445. scanline_tex_uv = video_uv * VERTICAL_SCANLINESvideo_size *
  5446. scanline_texture_size_inv;
  5447. blur3x3_tex_uv = video_uv * BLOOM_APPROXvideo_size /
  5448. BLOOM_APPROXtexture_size;
  5449. halation_tex_uv = video_uv * HALATION_BLURvideo_size /
  5450. HALATION_BLURtexture_size;
  5451. //scanline_texture_size_inv = scanline_texture_size_inv;
  5452. // Get a consistent name for the final mask texture size. Sample mode 0
  5453. // uses the manually resized mask, but ignore it if we never resized.
  5454. #ifdef PHOSPHOR_MASK_MANUALLY_RESIZE
  5455. const float mask_sample_mode = get_mask_sample_mode();
  5456. const float2 mask_resize_texture_size = mask_sample_mode < 0.5 ?
  5457. MASK_RESIZEtexture_size : mask_texture_large_size;
  5458. const float2 mask_resize_video_size = mask_sample_mode < 0.5 ?
  5459. MASK_RESIZEvideo_size : mask_texture_large_size;
  5460. #else
  5461. const float2 mask_resize_texture_size = mask_texture_large_size;
  5462. const float2 mask_resize_video_size = mask_texture_large_size;
  5463. #endif
  5464. // Compute mask tile dimensions, starting points, etc.:
  5465. //float2 mask_tiles_per_screen;
  5466. mask_tile_start_uv_and_size = get_mask_sampling_parameters(
  5467. mask_resize_texture_size, mask_resize_video_size, output_size,
  5468. mask_tiles_per_screen);
  5469. //mask_tiles_per_screen = mask_tiles_per_screen;
  5470. }