image.cpp 210 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243224422452246224722482249225022512252225322542255225622572258225922602261226222632264226522662267226822692270227122722273227422752276227722782279228022812282228322842285228622872288228922902291229222932294229522962297229822992300230123022303230423052306230723082309231023112312231323142315231623172318231923202321232223232324232523262327232823292330233123322333233423352336233723382339234023412342234323442345234623472348234923502351235223532354235523562357235823592360236123622363236423652366236723682369237023712372237323742375237623772378237923802381238223832384238523862387238823892390239123922393239423952396239723982399240024012402240324042405240624072408240924102411241224132414241524162417241824192420242124222423242424252426242724282429243024312432243324342435243624372438243924402441244224432444244524462447244824492450245124522453245424552456245724582459246024612462246324642465246624672468246924702471247224732474247524762477247824792480248124822483248424852486248724882489249024912492249324942495249624972498249925002501250225032504250525062507250825092510251125122513251425152516251725182519252025212522252325242525252625272528252925302531253225332534253525362537253825392540254125422543254425452546254725482549255025512552255325542555255625572558255925602561256225632564256525662567256825692570257125722573257425752576257725782579258025812582258325842585258625872588258925902591259225932594259525962597259825992600260126022603260426052606260726082609261026112612261326142615261626172618261926202621262226232624262526262627262826292630263126322633263426352636263726382639264026412642264326442645264626472648264926502651265226532654265526562657265826592660266126622663266426652666266726682669267026712672267326742675267626772678267926802681268226832684268526862687268826892690269126922693269426952696269726982699270027012702270327042705270627072708270927102711271227132714271527162717271827192720272127222723272427252726272727282729273027312732273327342735273627372738273927402741274227432744274527462747274827492750275127522753275427552756275727582759276027612762276327642765276627672768276927702771277227732774277527762777277827792780278127822783278427852786278727882789279027912792279327942795279627972798279928002801280228032804280528062807280828092810281128122813281428152816281728182819282028212822282328242825282628272828282928302831283228332834283528362837283828392840284128422843284428452846284728482849285028512852285328542855285628572858285928602861286228632864286528662867286828692870287128722873287428752876287728782879288028812882288328842885288628872888288928902891289228932894289528962897289828992900290129022903290429052906290729082909291029112912291329142915291629172918291929202921292229232924292529262927292829292930293129322933293429352936293729382939294029412942294329442945294629472948294929502951295229532954295529562957295829592960296129622963296429652966296729682969297029712972297329742975297629772978297929802981298229832984298529862987298829892990299129922993299429952996299729982999300030013002300330043005300630073008300930103011301230133014301530163017301830193020302130223023302430253026302730283029303030313032303330343035303630373038303930403041304230433044304530463047304830493050305130523053305430553056305730583059306030613062306330643065306630673068306930703071307230733074307530763077307830793080308130823083308430853086308730883089309030913092309330943095309630973098309931003101310231033104310531063107310831093110311131123113311431153116311731183119312031213122312331243125312631273128312931303131313231333134313531363137313831393140314131423143314431453146314731483149315031513152315331543155315631573158315931603161316231633164316531663167316831693170317131723173317431753176317731783179318031813182318331843185318631873188318931903191319231933194319531963197319831993200320132023203320432053206320732083209321032113212321332143215321632173218321932203221322232233224322532263227322832293230323132323233323432353236323732383239324032413242324332443245324632473248324932503251325232533254325532563257325832593260326132623263326432653266326732683269327032713272327332743275327632773278327932803281328232833284328532863287328832893290329132923293329432953296329732983299330033013302330333043305330633073308330933103311331233133314331533163317331833193320332133223323332433253326332733283329333033313332333333343335333633373338333933403341334233433344334533463347334833493350335133523353335433553356335733583359336033613362336333643365336633673368336933703371337233733374337533763377337833793380338133823383338433853386338733883389339033913392339333943395339633973398339934003401340234033404340534063407340834093410341134123413341434153416341734183419342034213422342334243425342634273428342934303431343234333434343534363437343834393440344134423443344434453446344734483449345034513452345334543455345634573458345934603461346234633464346534663467346834693470347134723473347434753476347734783479348034813482348334843485348634873488348934903491349234933494349534963497349834993500350135023503350435053506350735083509351035113512351335143515351635173518351935203521352235233524352535263527352835293530353135323533353435353536353735383539354035413542354335443545354635473548354935503551355235533554355535563557355835593560356135623563356435653566356735683569357035713572357335743575357635773578357935803581358235833584358535863587358835893590359135923593359435953596359735983599360036013602360336043605360636073608360936103611361236133614361536163617361836193620362136223623362436253626362736283629363036313632363336343635363636373638363936403641364236433644364536463647364836493650365136523653365436553656365736583659366036613662366336643665366636673668366936703671367236733674367536763677367836793680368136823683368436853686368736883689369036913692369336943695369636973698369937003701370237033704370537063707370837093710371137123713371437153716371737183719372037213722372337243725372637273728372937303731373237333734373537363737373837393740374137423743374437453746374737483749375037513752375337543755375637573758375937603761376237633764376537663767376837693770377137723773377437753776377737783779378037813782378337843785378637873788378937903791379237933794379537963797379837993800380138023803380438053806380738083809381038113812381338143815381638173818381938203821382238233824382538263827382838293830383138323833383438353836383738383839384038413842384338443845384638473848384938503851385238533854385538563857385838593860386138623863386438653866386738683869387038713872387338743875387638773878387938803881388238833884388538863887388838893890389138923893389438953896389738983899390039013902390339043905390639073908390939103911391239133914391539163917391839193920392139223923392439253926392739283929393039313932393339343935393639373938393939403941394239433944394539463947394839493950395139523953395439553956395739583959396039613962396339643965396639673968396939703971397239733974397539763977397839793980398139823983398439853986398739883989399039913992399339943995399639973998399940004001400240034004400540064007400840094010401140124013401440154016401740184019402040214022402340244025402640274028402940304031403240334034403540364037403840394040404140424043404440454046404740484049405040514052405340544055405640574058405940604061406240634064406540664067406840694070407140724073407440754076407740784079408040814082408340844085408640874088408940904091409240934094409540964097409840994100410141024103410441054106410741084109411041114112411341144115411641174118411941204121412241234124412541264127412841294130413141324133413441354136413741384139414041414142414341444145414641474148414941504151415241534154415541564157415841594160416141624163416441654166416741684169417041714172417341744175417641774178417941804181418241834184418541864187418841894190419141924193419441954196419741984199420042014202420342044205420642074208420942104211421242134214421542164217421842194220422142224223422442254226422742284229423042314232423342344235423642374238423942404241424242434244424542464247424842494250425142524253425442554256425742584259426042614262426342644265426642674268426942704271427242734274427542764277427842794280428142824283428442854286428742884289429042914292429342944295429642974298429943004301430243034304430543064307430843094310431143124313431443154316431743184319432043214322432343244325432643274328432943304331433243334334433543364337433843394340434143424343434443454346434743484349435043514352435343544355435643574358435943604361436243634364436543664367436843694370437143724373437443754376437743784379438043814382438343844385438643874388438943904391439243934394439543964397439843994400440144024403440444054406440744084409441044114412441344144415441644174418441944204421442244234424442544264427442844294430443144324433443444354436443744384439444044414442444344444445444644474448444944504451445244534454445544564457445844594460446144624463446444654466446744684469447044714472447344744475447644774478447944804481448244834484448544864487448844894490449144924493449444954496449744984499450045014502450345044505450645074508450945104511451245134514451545164517451845194520452145224523452445254526452745284529453045314532453345344535453645374538453945404541454245434544454545464547454845494550455145524553455445554556455745584559456045614562456345644565456645674568456945704571457245734574457545764577457845794580458145824583458445854586458745884589459045914592459345944595459645974598459946004601460246034604460546064607460846094610461146124613461446154616461746184619462046214622462346244625462646274628462946304631463246334634463546364637463846394640464146424643464446454646464746484649465046514652465346544655465646574658465946604661466246634664466546664667466846694670467146724673467446754676467746784679468046814682468346844685468646874688468946904691469246934694469546964697469846994700470147024703470447054706470747084709471047114712471347144715471647174718471947204721472247234724472547264727472847294730473147324733473447354736473747384739474047414742474347444745474647474748474947504751475247534754475547564757475847594760476147624763476447654766476747684769477047714772477347744775477647774778477947804781478247834784478547864787478847894790479147924793479447954796479747984799480048014802480348044805480648074808480948104811481248134814481548164817481848194820482148224823482448254826482748284829483048314832483348344835483648374838483948404841484248434844484548464847484848494850485148524853485448554856485748584859486048614862486348644865486648674868486948704871487248734874487548764877487848794880488148824883488448854886488748884889489048914892489348944895489648974898489949004901490249034904490549064907490849094910491149124913491449154916491749184919492049214922492349244925492649274928492949304931493249334934493549364937493849394940494149424943494449454946494749484949495049514952495349544955495649574958495949604961496249634964496549664967496849694970497149724973497449754976497749784979498049814982498349844985498649874988498949904991499249934994499549964997499849995000500150025003500450055006500750085009501050115012501350145015501650175018501950205021502250235024502550265027502850295030503150325033503450355036503750385039504050415042504350445045504650475048504950505051505250535054505550565057505850595060506150625063506450655066506750685069507050715072507350745075507650775078507950805081508250835084508550865087508850895090509150925093509450955096509750985099510051015102510351045105510651075108510951105111511251135114511551165117511851195120512151225123512451255126512751285129513051315132513351345135513651375138513951405141514251435144514551465147514851495150515151525153515451555156515751585159516051615162516351645165516651675168516951705171517251735174517551765177517851795180518151825183518451855186518751885189519051915192519351945195519651975198519952005201520252035204520552065207520852095210521152125213521452155216521752185219522052215222522352245225522652275228522952305231523252335234523552365237523852395240524152425243524452455246524752485249525052515252525352545255525652575258525952605261526252635264526552665267526852695270527152725273527452755276527752785279528052815282528352845285528652875288528952905291529252935294529552965297529852995300530153025303530453055306530753085309531053115312531353145315531653175318531953205321532253235324532553265327532853295330533153325333533453355336533753385339534053415342534353445345534653475348534953505351535253535354535553565357535853595360536153625363536453655366536753685369537053715372537353745375537653775378537953805381538253835384538553865387538853895390539153925393539453955396539753985399540054015402540354045405540654075408540954105411541254135414541554165417541854195420542154225423542454255426542754285429543054315432543354345435543654375438543954405441544254435444544554465447544854495450545154525453545454555456545754585459546054615462546354645465546654675468546954705471547254735474547554765477547854795480548154825483548454855486548754885489549054915492549354945495549654975498549955005501550255035504550555065507550855095510551155125513551455155516551755185519552055215522552355245525552655275528552955305531553255335534553555365537553855395540554155425543554455455546554755485549555055515552555355545555555655575558555955605561556255635564556555665567556855695570557155725573557455755576557755785579558055815582558355845585558655875588558955905591559255935594559555965597559855995600560156025603560456055606560756085609561056115612561356145615561656175618561956205621562256235624562556265627562856295630563156325633563456355636563756385639564056415642564356445645564656475648564956505651565256535654565556565657565856595660566156625663566456655666566756685669567056715672567356745675567656775678567956805681568256835684568556865687568856895690569156925693569456955696569756985699570057015702570357045705570657075708570957105711571257135714571557165717571857195720572157225723572457255726572757285729573057315732573357345735573657375738573957405741574257435744574557465747574857495750575157525753575457555756575757585759576057615762576357645765576657675768576957705771577257735774577557765777577857795780578157825783578457855786578757885789579057915792579357945795579657975798579958005801580258035804580558065807580858095810581158125813581458155816581758185819582058215822582358245825582658275828582958305831583258335834583558365837583858395840584158425843584458455846584758485849585058515852585358545855585658575858585958605861586258635864586558665867586858695870587158725873587458755876587758785879588058815882588358845885588658875888588958905891589258935894589558965897589858995900590159025903590459055906590759085909591059115912591359145915591659175918591959205921592259235924592559265927592859295930593159325933593459355936
  1. /*
  2. * Copyright 2011-2022 Branimir Karadzic. All rights reserved.
  3. * License: https://github.com/bkaradzic/bimg/blob/master/LICENSE
  4. */
  5. #include "bimg_p.h"
  6. #include <bx/hash.h>
  7. #include <astcenc.h>
  8. #include <bx/debug.h>
  9. namespace bimg
  10. {
  11. static const ImageBlockInfo s_imageBlockInfo[] =
  12. {
  13. // +--------------------------------------------- bits per pixel
  14. // | +----------------------------------------- block width
  15. // | | +-------------------------------------- block height
  16. // | | | +---------------------------------- block size
  17. // | | | | +------------------------------- min blocks x
  18. // | | | | | +---------------------------- min blocks y
  19. // | | | | | | +------------------------ depth bits
  20. // | | | | | | | +--------------------- stencil bits
  21. // | | | | | | | | +---+---+---+----- r, g, b, a bits
  22. // | | | | | | | | r g b a +-- encoding type
  23. // | | | | | | | | | | | | |
  24. { 4, 4, 4, 8, 1, 1, 0, 0, 0, 0, 0, 0, uint8_t(bx::EncodingType::Unorm) }, // BC1
  25. { 8, 4, 4, 16, 1, 1, 0, 0, 0, 0, 0, 0, uint8_t(bx::EncodingType::Unorm) }, // BC2
  26. { 8, 4, 4, 16, 1, 1, 0, 0, 0, 0, 0, 0, uint8_t(bx::EncodingType::Unorm) }, // BC3
  27. { 4, 4, 4, 8, 1, 1, 0, 0, 0, 0, 0, 0, uint8_t(bx::EncodingType::Unorm) }, // BC4
  28. { 8, 4, 4, 16, 1, 1, 0, 0, 0, 0, 0, 0, uint8_t(bx::EncodingType::Unorm) }, // BC5
  29. { 8, 4, 4, 16, 1, 1, 0, 0, 0, 0, 0, 0, uint8_t(bx::EncodingType::Float) }, // BC6H
  30. { 8, 4, 4, 16, 1, 1, 0, 0, 0, 0, 0, 0, uint8_t(bx::EncodingType::Unorm) }, // BC7
  31. { 4, 4, 4, 8, 1, 1, 0, 0, 0, 0, 0, 0, uint8_t(bx::EncodingType::Unorm) }, // ETC1
  32. { 4, 4, 4, 8, 1, 1, 0, 0, 0, 0, 0, 0, uint8_t(bx::EncodingType::Unorm) }, // ETC2
  33. { 8, 4, 4, 16, 1, 1, 0, 0, 0, 0, 0, 0, uint8_t(bx::EncodingType::Unorm) }, // ETC2A
  34. { 4, 4, 4, 8, 1, 1, 0, 0, 0, 0, 0, 0, uint8_t(bx::EncodingType::Unorm) }, // ETC2A1
  35. { 2, 8, 4, 8, 2, 2, 0, 0, 0, 0, 0, 0, uint8_t(bx::EncodingType::Unorm) }, // PTC12
  36. { 4, 4, 4, 8, 2, 2, 0, 0, 0, 0, 0, 0, uint8_t(bx::EncodingType::Unorm) }, // PTC14
  37. { 2, 8, 4, 8, 2, 2, 0, 0, 0, 0, 0, 0, uint8_t(bx::EncodingType::Unorm) }, // PTC12A
  38. { 4, 4, 4, 8, 2, 2, 0, 0, 0, 0, 0, 0, uint8_t(bx::EncodingType::Unorm) }, // PTC14A
  39. { 2, 8, 4, 8, 2, 2, 0, 0, 0, 0, 0, 0, uint8_t(bx::EncodingType::Unorm) }, // PTC22
  40. { 4, 4, 4, 8, 2, 2, 0, 0, 0, 0, 0, 0, uint8_t(bx::EncodingType::Unorm) }, // PTC24
  41. { 4, 4, 4, 8, 1, 1, 0, 0, 0, 0, 0, 0, uint8_t(bx::EncodingType::Unorm) }, // ATC
  42. { 8, 4, 4, 16, 1, 1, 0, 0, 0, 0, 0, 0, uint8_t(bx::EncodingType::Unorm) }, // ATCE
  43. { 8, 4, 4, 16, 1, 1, 0, 0, 0, 0, 0, 0, uint8_t(bx::EncodingType::Unorm) }, // ATCI
  44. { 8, 4, 4, 16, 1, 1, 0, 0, 0, 0, 0, 0, uint8_t(bx::EncodingType::Unorm) }, // ASTC4x4
  45. { 6, 5, 4, 16, 1, 1, 0, 0, 0, 0, 0, 0, uint8_t(bx::EncodingType::Unorm) }, // ASTC5x4
  46. { 6, 5, 5, 16, 1, 1, 0, 0, 0, 0, 0, 0, uint8_t(bx::EncodingType::Unorm) }, // ASTC5x5
  47. { 4, 6, 5, 16, 1, 1, 0, 0, 0, 0, 0, 0, uint8_t(bx::EncodingType::Unorm) }, // ASTC6x5
  48. { 4, 6, 6, 16, 1, 1, 0, 0, 0, 0, 0, 0, uint8_t(bx::EncodingType::Unorm) }, // ASTC6x6
  49. { 4, 8, 5, 16, 1, 1, 0, 0, 0, 0, 0, 0, uint8_t(bx::EncodingType::Unorm) }, // ASTC8x5
  50. { 3, 8, 6, 16, 1, 1, 0, 0, 0, 0, 0, 0, uint8_t(bx::EncodingType::Unorm) }, // ASTC8x6
  51. { 2, 8, 8, 16, 1, 1, 0, 0, 0, 0, 0, 0, uint8_t(bx::EncodingType::Unorm) }, // ASTC8x8
  52. { 3, 10, 5, 16, 1, 1, 0, 0, 0, 0, 0, 0, uint8_t(bx::EncodingType::Unorm) }, // ASTC10x5
  53. { 2, 10, 6, 16, 1, 1, 0, 0, 0, 0, 0, 0, uint8_t(bx::EncodingType::Unorm) }, // ASTC10x6
  54. { 2, 10, 8, 16, 1, 1, 0, 0, 0, 0, 0, 0, uint8_t(bx::EncodingType::Unorm) }, // ASTC10x8
  55. { 1, 10,10, 16, 1, 1, 0, 0, 0, 0, 0, 0, uint8_t(bx::EncodingType::Unorm) }, // ASTC10x10
  56. { 1, 12,10, 16, 1, 1, 0, 0, 0, 0, 0, 0, uint8_t(bx::EncodingType::Unorm) }, // ASTC12x10
  57. { 1, 12,12, 16, 1, 1, 0, 0, 0, 0, 0, 0, uint8_t(bx::EncodingType::Unorm) }, // ASTC12x12
  58. { 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, uint8_t(bx::EncodingType::Count) }, // Unknown
  59. { 1, 8, 1, 1, 1, 1, 0, 0, 1, 0, 0, 0, uint8_t(bx::EncodingType::Unorm) }, // R1
  60. { 8, 1, 1, 1, 1, 1, 0, 0, 0, 0, 0, 8, uint8_t(bx::EncodingType::Unorm) }, // A8
  61. { 8, 1, 1, 1, 1, 1, 0, 0, 8, 0, 0, 0, uint8_t(bx::EncodingType::Unorm) }, // R8
  62. { 8, 1, 1, 1, 1, 1, 0, 0, 8, 0, 0, 0, uint8_t(bx::EncodingType::Int ) }, // R8I
  63. { 8, 1, 1, 1, 1, 1, 0, 0, 8, 0, 0, 0, uint8_t(bx::EncodingType::Uint ) }, // R8U
  64. { 8, 1, 1, 1, 1, 1, 0, 0, 8, 0, 0, 0, uint8_t(bx::EncodingType::Snorm) }, // R8S
  65. { 16, 1, 1, 2, 1, 1, 0, 0, 16, 0, 0, 0, uint8_t(bx::EncodingType::Unorm) }, // R16
  66. { 16, 1, 1, 2, 1, 1, 0, 0, 16, 0, 0, 0, uint8_t(bx::EncodingType::Int ) }, // R16I
  67. { 16, 1, 1, 2, 1, 1, 0, 0, 16, 0, 0, 0, uint8_t(bx::EncodingType::Uint ) }, // R16U
  68. { 16, 1, 1, 2, 1, 1, 0, 0, 16, 0, 0, 0, uint8_t(bx::EncodingType::Float) }, // R16F
  69. { 16, 1, 1, 2, 1, 1, 0, 0, 16, 0, 0, 0, uint8_t(bx::EncodingType::Snorm) }, // R16S
  70. { 32, 1, 1, 4, 1, 1, 0, 0, 32, 0, 0, 0, uint8_t(bx::EncodingType::Int ) }, // R32I
  71. { 32, 1, 1, 4, 1, 1, 0, 0, 32, 0, 0, 0, uint8_t(bx::EncodingType::Uint ) }, // R32U
  72. { 32, 1, 1, 4, 1, 1, 0, 0, 32, 0, 0, 0, uint8_t(bx::EncodingType::Float) }, // R32F
  73. { 16, 1, 1, 2, 1, 1, 0, 0, 8, 8, 0, 0, uint8_t(bx::EncodingType::Unorm) }, // RG8
  74. { 16, 1, 1, 2, 1, 1, 0, 0, 8, 8, 0, 0, uint8_t(bx::EncodingType::Int ) }, // RG8I
  75. { 16, 1, 1, 2, 1, 1, 0, 0, 8, 8, 0, 0, uint8_t(bx::EncodingType::Uint ) }, // RG8U
  76. { 16, 1, 1, 2, 1, 1, 0, 0, 8, 8, 0, 0, uint8_t(bx::EncodingType::Snorm) }, // RG8S
  77. { 32, 1, 1, 4, 1, 1, 0, 0, 16, 16, 0, 0, uint8_t(bx::EncodingType::Unorm) }, // RG16
  78. { 32, 1, 1, 4, 1, 1, 0, 0, 16, 16, 0, 0, uint8_t(bx::EncodingType::Int ) }, // RG16I
  79. { 32, 1, 1, 4, 1, 1, 0, 0, 16, 16, 0, 0, uint8_t(bx::EncodingType::Uint ) }, // RG16U
  80. { 32, 1, 1, 4, 1, 1, 0, 0, 16, 16, 0, 0, uint8_t(bx::EncodingType::Float) }, // RG16F
  81. { 32, 1, 1, 4, 1, 1, 0, 0, 16, 16, 0, 0, uint8_t(bx::EncodingType::Snorm) }, // RG16S
  82. { 64, 1, 1, 8, 1, 1, 0, 0, 32, 32, 0, 0, uint8_t(bx::EncodingType::Int ) }, // RG32I
  83. { 64, 1, 1, 8, 1, 1, 0, 0, 32, 32, 0, 0, uint8_t(bx::EncodingType::Uint ) }, // RG32U
  84. { 64, 1, 1, 8, 1, 1, 0, 0, 32, 32, 0, 0, uint8_t(bx::EncodingType::Float) }, // RG32F
  85. { 24, 1, 1, 3, 1, 1, 0, 0, 8, 8, 8, 0, uint8_t(bx::EncodingType::Unorm) }, // RGB8
  86. { 24, 1, 1, 3, 1, 1, 0, 0, 8, 8, 8, 0, uint8_t(bx::EncodingType::Int ) }, // RGB8I
  87. { 24, 1, 1, 3, 1, 1, 0, 0, 8, 8, 8, 0, uint8_t(bx::EncodingType::Uint ) }, // RGB8U
  88. { 24, 1, 1, 3, 1, 1, 0, 0, 8, 8, 8, 0, uint8_t(bx::EncodingType::Snorm) }, // RGB8S
  89. { 32, 1, 1, 4, 1, 1, 0, 0, 9, 9, 9, 5, uint8_t(bx::EncodingType::Float) }, // RGB9E5F
  90. { 32, 1, 1, 4, 1, 1, 0, 0, 8, 8, 8, 8, uint8_t(bx::EncodingType::Unorm) }, // BGRA8
  91. { 32, 1, 1, 4, 1, 1, 0, 0, 8, 8, 8, 8, uint8_t(bx::EncodingType::Unorm) }, // RGBA8
  92. { 32, 1, 1, 4, 1, 1, 0, 0, 8, 8, 8, 8, uint8_t(bx::EncodingType::Int ) }, // RGBA8I
  93. { 32, 1, 1, 4, 1, 1, 0, 0, 8, 8, 8, 8, uint8_t(bx::EncodingType::Uint ) }, // RGBA8U
  94. { 32, 1, 1, 4, 1, 1, 0, 0, 8, 8, 8, 8, uint8_t(bx::EncodingType::Snorm) }, // RGBA8S
  95. { 64, 1, 1, 8, 1, 1, 0, 0, 16, 16, 16, 16, uint8_t(bx::EncodingType::Unorm) }, // RGBA16
  96. { 64, 1, 1, 8, 1, 1, 0, 0, 16, 16, 16, 16, uint8_t(bx::EncodingType::Int ) }, // RGBA16I
  97. { 64, 1, 1, 8, 1, 1, 0, 0, 16, 16, 16, 16, uint8_t(bx::EncodingType::Uint ) }, // RGBA16U
  98. { 64, 1, 1, 8, 1, 1, 0, 0, 16, 16, 16, 16, uint8_t(bx::EncodingType::Float) }, // RGBA16F
  99. { 64, 1, 1, 8, 1, 1, 0, 0, 16, 16, 16, 16, uint8_t(bx::EncodingType::Snorm) }, // RGBA16S
  100. { 128, 1, 1, 16, 1, 1, 0, 0, 32, 32, 32, 32, uint8_t(bx::EncodingType::Int ) }, // RGBA32I
  101. { 128, 1, 1, 16, 1, 1, 0, 0, 32, 32, 32, 32, uint8_t(bx::EncodingType::Uint ) }, // RGBA32U
  102. { 128, 1, 1, 16, 1, 1, 0, 0, 32, 32, 32, 32, uint8_t(bx::EncodingType::Float) }, // RGBA32F
  103. { 16, 1, 1, 2, 1, 1, 0, 0, 5, 6, 5, 0, uint8_t(bx::EncodingType::Unorm) }, // B5G6R5
  104. { 16, 1, 1, 2, 1, 1, 0, 0, 5, 6, 5, 0, uint8_t(bx::EncodingType::Unorm) }, // R5G6B5
  105. { 16, 1, 1, 2, 1, 1, 0, 0, 4, 4, 4, 4, uint8_t(bx::EncodingType::Unorm) }, // BGRA4
  106. { 16, 1, 1, 2, 1, 1, 0, 0, 4, 4, 4, 4, uint8_t(bx::EncodingType::Unorm) }, // RGBA4
  107. { 16, 1, 1, 2, 1, 1, 0, 0, 5, 5, 5, 1, uint8_t(bx::EncodingType::Unorm) }, // BGR5A1
  108. { 16, 1, 1, 2, 1, 1, 0, 0, 5, 5, 5, 1, uint8_t(bx::EncodingType::Unorm) }, // RGB5A1
  109. { 32, 1, 1, 4, 1, 1, 0, 0, 10, 10, 10, 2, uint8_t(bx::EncodingType::Unorm) }, // RGB10A2
  110. { 32, 1, 1, 4, 1, 1, 0, 0, 11, 11, 10, 0, uint8_t(bx::EncodingType::Unorm) }, // RG11B10F
  111. { 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, uint8_t(bx::EncodingType::Count) }, // UnknownDepth
  112. { 16, 1, 1, 2, 1, 1, 16, 0, 0, 0, 0, 0, uint8_t(bx::EncodingType::Unorm) }, // D16
  113. { 24, 1, 1, 3, 1, 1, 24, 0, 0, 0, 0, 0, uint8_t(bx::EncodingType::Unorm) }, // D24
  114. { 32, 1, 1, 4, 1, 1, 24, 8, 0, 0, 0, 0, uint8_t(bx::EncodingType::Unorm) }, // D24S8
  115. { 32, 1, 1, 4, 1, 1, 32, 0, 0, 0, 0, 0, uint8_t(bx::EncodingType::Unorm) }, // D32
  116. { 16, 1, 1, 2, 1, 1, 16, 0, 0, 0, 0, 0, uint8_t(bx::EncodingType::Float) }, // D16F
  117. { 24, 1, 1, 3, 1, 1, 24, 0, 0, 0, 0, 0, uint8_t(bx::EncodingType::Float) }, // D24F
  118. { 32, 1, 1, 4, 1, 1, 32, 0, 0, 0, 0, 0, uint8_t(bx::EncodingType::Float) }, // D32F
  119. { 8, 1, 1, 1, 1, 1, 0, 8, 0, 0, 0, 0, uint8_t(bx::EncodingType::Unorm) }, // D0S8
  120. };
  121. BX_STATIC_ASSERT(TextureFormat::Count == BX_COUNTOF(s_imageBlockInfo) );
  122. static const char* s_textureFormatName[] =
  123. {
  124. "BC1", // BC1
  125. "BC2", // BC2
  126. "BC3", // BC3
  127. "BC4", // BC4
  128. "BC5", // BC5
  129. "BC6H", // BC6H
  130. "BC7", // BC7
  131. "ETC1", // ETC1
  132. "ETC2", // ETC2
  133. "ETC2A", // ETC2A
  134. "ETC2A1", // ETC2A1
  135. "PTC12", // PTC12
  136. "PTC14", // PTC14
  137. "PTC12A", // PTC12A
  138. "PTC14A", // PTC14A
  139. "PTC22", // PTC22
  140. "PTC24", // PTC24
  141. "ATC", // ATC
  142. "ATCE", // ATCE
  143. "ATCI", // ATCI
  144. "ASTC4x4", // ASTC4x4
  145. "ASTC5x4", // ASTC5x4
  146. "ASTC5x5", // ASTC5x5
  147. "ASTC6x5", // ASTC6x5
  148. "ASTC6x6", // ASTC6x6
  149. "ASTC8x5", // ASTC8x5
  150. "ASTC8x6", // ASTC8x6
  151. "ASTC8x8", // ASTC8x8
  152. "ASTC10x5", // ASTC10x5
  153. "ASTC10x6", // ASTC10x6
  154. "ASTC10x8", // ASTC10x8
  155. "ASTC10x10", // ASTC10x10
  156. "ASTC12x10", // ASTC12x10
  157. "ASTC12x12", // ASTC12x12
  158. "<unknown>", // Unknown
  159. "R1", // R1
  160. "A8", // A8
  161. "R8", // R8
  162. "R8I", // R8I
  163. "R8U", // R8U
  164. "R8S", // R8S
  165. "R16", // R16
  166. "R16I", // R16I
  167. "R16U", // R16U
  168. "R16F", // R16F
  169. "R16S", // R16S
  170. "R32I", // R32I
  171. "R32U", // R32U
  172. "R32F", // R32F
  173. "RG8", // RG8
  174. "RG8I", // RG8I
  175. "RG8U", // RG8U
  176. "RG8S", // RG8S
  177. "RG16", // RG16
  178. "RG16I", // RG16I
  179. "RG16U", // RG16U
  180. "RG16F", // RG16F
  181. "RG16S", // RG16S
  182. "RG32I", // RG32I
  183. "RG32U", // RG32U
  184. "RG32F", // RG32F
  185. "RGB8", // RGB8
  186. "RGB8I", // RGB8I
  187. "RGB8U", // RGB8U
  188. "RGB8S", // RGB8S
  189. "RGB9E5", // RGB9E5F
  190. "BGRA8", // BGRA8
  191. "RGBA8", // RGBA8
  192. "RGBA8I", // RGBA8I
  193. "RGBA8U", // RGBA8U
  194. "RGBA8S", // RGBA8S
  195. "RGBA16", // RGBA16
  196. "RGBA16I", // RGBA16I
  197. "RGBA16U", // RGBA16U
  198. "RGBA16F", // RGBA16F
  199. "RGBA16S", // RGBA16S
  200. "RGBA32I", // RGBA32I
  201. "RGBA32U", // RGBA32U
  202. "RGBA32F", // RGBA32F
  203. "B5G6R5", // B5G6R5
  204. "R5G6B5", // R5G6B5
  205. "BGRA4", // BGRA4
  206. "RGBA4", // RGBA4
  207. "BGR5A1", // BGR5A1
  208. "RGB5A1", // RGB5A1
  209. "RGB10A2", // RGB10A2
  210. "RG11B10F", // RG11B10F
  211. "<unknown>", // UnknownDepth
  212. "D16", // D16
  213. "D24", // D24
  214. "D24S8", // D24S8
  215. "D32", // D32
  216. "D16F", // D16F
  217. "D24F", // D24F
  218. "D32F", // D32F
  219. "D0S8", // D0S8
  220. };
  221. BX_STATIC_ASSERT(TextureFormat::Count == BX_COUNTOF(s_textureFormatName) );
  222. bool isCompressed(TextureFormat::Enum _format)
  223. {
  224. return _format < TextureFormat::Unknown;
  225. }
  226. bool isColor(TextureFormat::Enum _format)
  227. {
  228. return _format > TextureFormat::Unknown
  229. && _format < TextureFormat::UnknownDepth
  230. ;
  231. }
  232. bool isDepth(TextureFormat::Enum _format)
  233. {
  234. return _format > TextureFormat::UnknownDepth
  235. && _format < TextureFormat::Count
  236. ;
  237. }
  238. bool isValid(TextureFormat::Enum _format)
  239. {
  240. return _format != TextureFormat::Unknown
  241. && _format != TextureFormat::UnknownDepth
  242. && _format != TextureFormat::Count
  243. ;
  244. }
  245. bool isFloat(TextureFormat::Enum _format)
  246. {
  247. return uint8_t(bx::EncodingType::Float) == s_imageBlockInfo[_format].encoding;
  248. }
  249. uint8_t getBitsPerPixel(TextureFormat::Enum _format)
  250. {
  251. return s_imageBlockInfo[_format].bitsPerPixel;
  252. }
  253. const ImageBlockInfo& getBlockInfo(TextureFormat::Enum _format)
  254. {
  255. return s_imageBlockInfo[_format];
  256. }
  257. uint8_t getBlockSize(TextureFormat::Enum _format)
  258. {
  259. return s_imageBlockInfo[_format].blockSize;
  260. }
  261. const char* getName(TextureFormat::Enum _format)
  262. {
  263. if (_format >= TextureFormat::Count)
  264. {
  265. return "Unknown?!";
  266. }
  267. return s_textureFormatName[_format];
  268. }
  269. TextureFormat::Enum getFormat(const char* _name)
  270. {
  271. for (uint32_t ii = 0; ii < TextureFormat::Count; ++ii)
  272. {
  273. const TextureFormat::Enum fmt = TextureFormat::Enum(ii);
  274. if (isValid(fmt) )
  275. {
  276. if (0 == bx::strCmpI(s_textureFormatName[ii], _name) )
  277. {
  278. return fmt;
  279. }
  280. }
  281. }
  282. return TextureFormat::Unknown;
  283. }
  284. uint8_t imageGetNumMips(TextureFormat::Enum _format, uint16_t _width, uint16_t _height, uint16_t _depth)
  285. {
  286. const ImageBlockInfo& blockInfo = getBlockInfo(_format);
  287. const uint16_t blockWidth = blockInfo.blockWidth;
  288. const uint16_t blockHeight = blockInfo.blockHeight;
  289. const uint16_t minBlockX = blockInfo.minBlockX;
  290. const uint16_t minBlockY = blockInfo.minBlockY;
  291. _width = bx::max<uint16_t>(blockWidth * minBlockX, ( (_width + blockWidth - 1) / blockWidth )*blockWidth);
  292. _height = bx::max<uint16_t>(blockHeight * minBlockY, ( (_height + blockHeight - 1) / blockHeight)*blockHeight);
  293. _depth = bx::max<uint16_t>(1, _depth);
  294. uint8_t numMips = calcNumMips(true, _width, _height, _depth);
  295. return numMips;
  296. }
  297. uint32_t imageGetSize(TextureInfo* _info, uint16_t _width, uint16_t _height, uint16_t _depth, bool _cubeMap, bool _hasMips, uint16_t _numLayers, TextureFormat::Enum _format)
  298. {
  299. const ImageBlockInfo& blockInfo = getBlockInfo(_format);
  300. const uint8_t bpp = blockInfo.bitsPerPixel;
  301. const uint16_t blockWidth = blockInfo.blockWidth;
  302. const uint16_t blockHeight = blockInfo.blockHeight;
  303. const uint16_t minBlockX = blockInfo.minBlockX;
  304. const uint16_t minBlockY = blockInfo.minBlockY;
  305. const uint8_t blockSize = blockInfo.blockSize;
  306. _width = bx::max<uint16_t>(blockWidth * minBlockX, ( (_width + blockWidth - 1) / blockWidth)*blockWidth);
  307. _height = bx::max<uint16_t>(blockHeight * minBlockY, ( (_height + blockHeight - 1) / blockHeight)*blockHeight);
  308. _depth = bx::max<uint16_t>(1, _depth);
  309. const uint8_t numMips = calcNumMips(_hasMips, _width, _height, _depth);
  310. const uint32_t sides = _cubeMap ? 6 : 1;
  311. uint32_t width = _width;
  312. uint32_t height = _height;
  313. uint32_t depth = _depth;
  314. uint32_t size = 0;
  315. for (uint32_t lod = 0; lod < numMips; ++lod)
  316. {
  317. width = bx::max<uint32_t>(blockWidth * minBlockX, ( (width + blockWidth - 1) / blockWidth )*blockWidth);
  318. height = bx::max<uint32_t>(blockHeight * minBlockY, ( (height + blockHeight - 1) / blockHeight)*blockHeight);
  319. depth = bx::max<uint32_t>(1, depth);
  320. size += uint32_t(uint64_t(width/blockWidth * height/blockHeight * depth)*blockSize * sides);
  321. width >>= 1;
  322. height >>= 1;
  323. depth >>= 1;
  324. }
  325. size *= _numLayers;
  326. if (NULL != _info)
  327. {
  328. _info->format = _format;
  329. _info->width = _width;
  330. _info->height = _height;
  331. _info->depth = _depth;
  332. _info->numMips = numMips;
  333. _info->numLayers = _numLayers;
  334. _info->cubeMap = _cubeMap;
  335. _info->storageSize = size;
  336. _info->bitsPerPixel = bpp;
  337. }
  338. return size;
  339. }
  340. BX_NO_INLINE void imageSolid(void* _dst, uint32_t _width, uint32_t _height, uint32_t _solid)
  341. {
  342. uint32_t* dst = (uint32_t*)_dst;
  343. for (uint32_t ii = 0, num = _width*_height; ii < num; ++ii)
  344. {
  345. *dst++ = _solid;
  346. }
  347. }
  348. BX_NO_INLINE void imageCheckerboard(void* _dst, uint32_t _width, uint32_t _height, uint32_t _step, uint32_t _0, uint32_t _1)
  349. {
  350. uint32_t* dst = (uint32_t*)_dst;
  351. for (uint32_t yy = 0; yy < _height; ++yy)
  352. {
  353. for (uint32_t xx = 0; xx < _width; ++xx)
  354. {
  355. uint32_t abgr = ( (xx/_step)&1) ^ ( (yy/_step)&1) ? _1 : _0;
  356. *dst++ = abgr;
  357. }
  358. }
  359. }
  360. void imageRgba8Downsample2x2Ref(void* _dst, uint32_t _width, uint32_t _height, uint32_t _depth, uint32_t _srcPitch, uint32_t _dstPitch, const void* _src)
  361. {
  362. const uint32_t dstWidth = _width/2;
  363. const uint32_t dstHeight = _height/2;
  364. if (0 == dstWidth
  365. || 0 == dstHeight)
  366. {
  367. return;
  368. }
  369. const uint8_t* src = (const uint8_t*)_src;
  370. for (uint32_t zz = 0; zz < _depth; ++zz)
  371. {
  372. for (uint32_t yy = 0, ystep = _srcPitch*2; yy < dstHeight; ++yy, src += ystep)
  373. {
  374. uint8_t* dst = (uint8_t*)_dst + _dstPitch*yy;
  375. const uint8_t* rgba = src;
  376. for (uint32_t xx = 0; xx < dstWidth; ++xx, rgba += 8, dst += 4)
  377. {
  378. float rr = bx::toLinear(rgba[ 0]);
  379. float gg = bx::toLinear(rgba[ 1]);
  380. float bb = bx::toLinear(rgba[ 2]);
  381. float aa = rgba[ 3];
  382. rr += bx::toLinear(rgba[ 4]);
  383. gg += bx::toLinear(rgba[ 5]);
  384. bb += bx::toLinear(rgba[ 6]);
  385. aa += rgba[ 7];
  386. rr += bx::toLinear(rgba[_srcPitch+0]);
  387. gg += bx::toLinear(rgba[_srcPitch+1]);
  388. bb += bx::toLinear(rgba[_srcPitch+2]);
  389. aa += rgba[_srcPitch+3];
  390. rr += bx::toLinear(rgba[_srcPitch+4]);
  391. gg += bx::toLinear(rgba[_srcPitch+5]);
  392. bb += bx::toLinear(rgba[_srcPitch+6]);
  393. aa += rgba[_srcPitch+7];
  394. rr *= 0.25f;
  395. gg *= 0.25f;
  396. bb *= 0.25f;
  397. aa *= 0.25f;
  398. rr = bx::toGamma(rr);
  399. gg = bx::toGamma(gg);
  400. bb = bx::toGamma(bb);
  401. dst[0] = (uint8_t)rr;
  402. dst[1] = (uint8_t)gg;
  403. dst[2] = (uint8_t)bb;
  404. dst[3] = (uint8_t)aa;
  405. }
  406. }
  407. }
  408. }
  409. BX_SIMD_INLINE bx::simd128_t simd_to_linear(bx::simd128_t _a)
  410. {
  411. using namespace bx;
  412. const simd128_t f12_92 = simd_ld(12.92f, 12.92f, 12.92f, 1.0f);
  413. const simd128_t f0_055 = simd_ld(0.055f, 0.055f, 0.055f, 0.0f);
  414. const simd128_t f1_055 = simd_ld(1.055f, 1.055f, 1.055f, 1.0f);
  415. const simd128_t f2_4 = simd_ld(2.4f, 2.4f, 2.4f, 1.0f);
  416. const simd128_t f0_04045 = simd_ld(0.04045f, 0.04045f, 0.04045f, 0.0f);
  417. const simd128_t lo = simd_div(_a, f12_92);
  418. const simd128_t tmp0 = simd_add(_a, f0_055);
  419. const simd128_t tmp1 = simd_div(tmp0, f1_055);
  420. const simd128_t hi = simd_pow(tmp1, f2_4);
  421. const simd128_t mask = simd_cmple(_a, f0_04045);
  422. const simd128_t result = simd_selb(mask, hi, lo);
  423. return result;
  424. }
  425. BX_SIMD_INLINE bx::simd128_t simd_to_gamma(bx::simd128_t _a)
  426. {
  427. using namespace bx;
  428. const simd128_t f12_92 = simd_ld(12.92f, 12.92f, 12.92f, 1.0f);
  429. const simd128_t f0_055 = simd_ld(0.055f, 0.055f, 0.055f, 0.0f);
  430. const simd128_t f1_055 = simd_ld(1.055f, 1.055f, 1.055f, 1.0f);
  431. const simd128_t f1o2_4 = simd_ld(1.0f/2.4f, 1.0f/2.4f, 1.0f/2.4f, 1.0f);
  432. const simd128_t f0_0031308 = simd_ld(0.0031308f, 0.0031308f, 0.0031308f, 0.0f);
  433. const simd128_t lo = simd_mul(_a, f12_92);
  434. const simd128_t absa = simd_abs(_a);
  435. const simd128_t tmp0 = simd_pow(absa, f1o2_4);
  436. const simd128_t tmp1 = simd_mul(tmp0, f1_055);
  437. const simd128_t hi = simd_sub(tmp1, f0_055);
  438. const simd128_t mask = simd_cmple(_a, f0_0031308);
  439. const simd128_t result = simd_selb(mask, hi, lo);
  440. return result;
  441. }
  442. void imageRgba8Downsample2x2(void* _dst, uint32_t _width, uint32_t _height, uint32_t _depth, uint32_t _srcPitch, uint32_t _dstPitch, const void* _src)
  443. {
  444. const uint32_t dstWidth = _width/2;
  445. const uint32_t dstHeight = _height/2;
  446. if (0 == dstWidth
  447. || 0 == dstHeight)
  448. {
  449. return;
  450. }
  451. const uint8_t* src = (const uint8_t*)_src;
  452. using namespace bx;
  453. const simd128_t unpack = simd_ld(1.0f, 1.0f/256.0f, 1.0f/65536.0f, 1.0f/16777216.0f);
  454. const simd128_t pack = simd_ld(1.0f, 256.0f*0.5f, 65536.0f, 16777216.0f*0.5f);
  455. const simd128_t umask = simd_ild(0xff, 0xff00, 0xff0000, 0xff000000);
  456. const simd128_t pmask = simd_ild(0xff, 0x7f80, 0xff0000, 0x7f800000);
  457. const simd128_t wflip = simd_ild(0, 0, 0, 0x80000000);
  458. const simd128_t wadd = simd_ld(0.0f, 0.0f, 0.0f, 32768.0f*65536.0f);
  459. const simd128_t quater = simd_splat(0.25f);
  460. for (uint32_t zz = 0; zz < _depth; ++zz)
  461. {
  462. for (uint32_t yy = 0, ystep = _srcPitch*2; yy < dstHeight; ++yy, src += ystep)
  463. {
  464. uint8_t* dst = (uint8_t*)_dst + _dstPitch*yy;
  465. const uint8_t* rgba = src;
  466. for (uint32_t xx = 0; xx < dstWidth; ++xx, rgba += 8, dst += 4)
  467. {
  468. const simd128_t abgr0 = simd_splat(rgba);
  469. const simd128_t abgr1 = simd_splat(rgba+4);
  470. const simd128_t abgr2 = simd_splat(rgba+_srcPitch);
  471. const simd128_t abgr3 = simd_splat(rgba+_srcPitch+4);
  472. const simd128_t abgr0m = simd_and(abgr0, umask);
  473. const simd128_t abgr1m = simd_and(abgr1, umask);
  474. const simd128_t abgr2m = simd_and(abgr2, umask);
  475. const simd128_t abgr3m = simd_and(abgr3, umask);
  476. const simd128_t abgr0x = simd_xor(abgr0m, wflip);
  477. const simd128_t abgr1x = simd_xor(abgr1m, wflip);
  478. const simd128_t abgr2x = simd_xor(abgr2m, wflip);
  479. const simd128_t abgr3x = simd_xor(abgr3m, wflip);
  480. const simd128_t abgr0f = simd_itof(abgr0x);
  481. const simd128_t abgr1f = simd_itof(abgr1x);
  482. const simd128_t abgr2f = simd_itof(abgr2x);
  483. const simd128_t abgr3f = simd_itof(abgr3x);
  484. const simd128_t abgr0c = simd_add(abgr0f, wadd);
  485. const simd128_t abgr1c = simd_add(abgr1f, wadd);
  486. const simd128_t abgr2c = simd_add(abgr2f, wadd);
  487. const simd128_t abgr3c = simd_add(abgr3f, wadd);
  488. const simd128_t abgr0n = simd_mul(abgr0c, unpack);
  489. const simd128_t abgr1n = simd_mul(abgr1c, unpack);
  490. const simd128_t abgr2n = simd_mul(abgr2c, unpack);
  491. const simd128_t abgr3n = simd_mul(abgr3c, unpack);
  492. const simd128_t abgr0l = simd_to_linear(abgr0n);
  493. const simd128_t abgr1l = simd_to_linear(abgr1n);
  494. const simd128_t abgr2l = simd_to_linear(abgr2n);
  495. const simd128_t abgr3l = simd_to_linear(abgr3n);
  496. const simd128_t sum0 = simd_add(abgr0l, abgr1l);
  497. const simd128_t sum1 = simd_add(abgr2l, abgr3l);
  498. const simd128_t sum2 = simd_add(sum0, sum1);
  499. const simd128_t avg0 = simd_mul(sum2, quater);
  500. const simd128_t avg1 = simd_to_gamma(avg0);
  501. const simd128_t avg2 = simd_mul(avg1, pack);
  502. const simd128_t ftoi0 = simd_ftoi(avg2);
  503. const simd128_t ftoi1 = simd_and(ftoi0, pmask);
  504. const simd128_t zwxy = simd_swiz_zwxy(ftoi1);
  505. const simd128_t tmp0 = simd_or(ftoi1, zwxy);
  506. const simd128_t yyyy = simd_swiz_yyyy(tmp0);
  507. const simd128_t tmp1 = simd_iadd(yyyy, yyyy);
  508. const simd128_t result = simd_or(tmp0, tmp1);
  509. simd_stx(dst, result);
  510. }
  511. }
  512. }
  513. }
  514. void imageRgba32fToLinear(void* _dst, uint32_t _width, uint32_t _height, uint32_t _depth, uint32_t _srcPitch, const void* _src)
  515. {
  516. uint8_t* dst = ( uint8_t*)_dst;
  517. const uint8_t* src = (const uint8_t*)_src;
  518. for (uint32_t zz = 0; zz < _depth; ++zz)
  519. {
  520. for (uint32_t yy = 0; yy < _height; ++yy, src += _srcPitch, dst += _width*16)
  521. {
  522. for (uint32_t xx = 0; xx < _width; ++xx)
  523. {
  524. const uint32_t offset = xx * 16;
  525. float* fd = ( float*)(dst + offset);
  526. const float* fs = (const float*)(src + offset);
  527. fd[0] = bx::toLinear(fs[0]);
  528. fd[1] = bx::toLinear(fs[1]);
  529. fd[2] = bx::toLinear(fs[2]);
  530. fd[3] = fs[3];
  531. }
  532. }
  533. }
  534. }
  535. void imageRgba32fToLinear(ImageContainer* _imageContainer)
  536. {
  537. const uint16_t numSides = _imageContainer->m_numLayers * (_imageContainer->m_cubeMap ? 6 : 1);
  538. for (uint16_t side = 0; side < numSides; ++side)
  539. {
  540. ImageMip mip;
  541. imageGetRawData(*_imageContainer, side, 0, _imageContainer->m_data, _imageContainer->m_size, mip);
  542. const uint32_t pitch = _imageContainer->m_width*16;
  543. const uint32_t slice = _imageContainer->m_height*pitch;
  544. for (uint32_t zz = 0, depth = _imageContainer->m_depth; zz < depth; ++zz)
  545. {
  546. const uint32_t srcDataStep = uint32_t(bx::floor(zz * _imageContainer->m_depth / float(depth) ) );
  547. const uint8_t* srcData = &mip.m_data[srcDataStep*slice];
  548. imageRgba32fToLinear(const_cast<uint8_t*>(srcData), mip.m_width, mip.m_height, 1, pitch, srcData);
  549. }
  550. }
  551. }
  552. void imageRgba32fToGamma(void* _dst, uint32_t _width, uint32_t _height, uint32_t _depth, uint32_t _srcPitch, const void* _src)
  553. {
  554. uint8_t* dst = ( uint8_t*)_dst;
  555. const uint8_t* src = (const uint8_t*)_src;
  556. for (uint32_t zz = 0; zz < _depth; ++zz)
  557. {
  558. for (uint32_t yy = 0; yy < _height; ++yy, src += _srcPitch, dst += _width*16)
  559. {
  560. for (uint32_t xx = 0; xx < _width; ++xx)
  561. {
  562. const uint32_t offset = xx * 16;
  563. float* fd = ( float*)(dst + offset);
  564. const float* fs = (const float*)(src + offset);
  565. fd[0] = bx::toGamma(fs[0]);
  566. fd[1] = bx::toGamma(fs[1]);
  567. fd[2] = bx::toGamma(fs[2]);
  568. fd[3] = fs[3];
  569. }
  570. }
  571. }
  572. }
  573. void imageRgba32fToGamma(ImageContainer* _imageContainer)
  574. {
  575. const uint16_t numSides = _imageContainer->m_numLayers * (_imageContainer->m_cubeMap ? 6 : 1);
  576. for (uint16_t side = 0; side < numSides; ++side)
  577. {
  578. ImageMip mip;
  579. imageGetRawData(*_imageContainer, side, 0, _imageContainer->m_data, _imageContainer->m_size, mip);
  580. const uint32_t pitch = _imageContainer->m_width*16;
  581. const uint32_t slice = _imageContainer->m_height*pitch;
  582. for (uint32_t zz = 0, depth = _imageContainer->m_depth; zz < depth; ++zz)
  583. {
  584. const uint32_t srcDataStep = uint32_t(bx::floor(zz * _imageContainer->m_depth / float(depth) ) );
  585. const uint8_t* srcData = &mip.m_data[srcDataStep*slice];
  586. imageRgba32fToGamma(const_cast<uint8_t*>(srcData), mip.m_width, mip.m_height, 1, pitch, srcData);
  587. }
  588. }
  589. }
  590. void imageRgba32fLinearDownsample2x2Ref(void* _dst, uint32_t _width, uint32_t _height, uint32_t _depth, uint32_t _srcPitch, const void* _src)
  591. {
  592. const uint32_t dstWidth = _width/2;
  593. const uint32_t dstHeight = _height/2;
  594. const uint32_t dstDepth = _depth/2;
  595. if (0 == dstWidth
  596. || 0 == dstHeight)
  597. {
  598. return;
  599. }
  600. const uint8_t* src = (const uint8_t*)_src;
  601. uint8_t* dst = (uint8_t*)_dst;
  602. if (0 == dstDepth)
  603. {
  604. for (uint32_t yy = 0, ystep = _srcPitch*2; yy < dstHeight; ++yy, src += ystep)
  605. {
  606. const float* rgba0 = (const float*)&src[0];
  607. const float* rgba1 = (const float*)&src[_srcPitch];
  608. for (uint32_t xx = 0; xx < dstWidth; ++xx, rgba0 += 8, rgba1 += 8, dst += 16)
  609. {
  610. float xyz[4];
  611. xyz[0] = rgba0[0];
  612. xyz[1] = rgba0[1];
  613. xyz[2] = rgba0[2];
  614. xyz[3] = rgba0[3];
  615. xyz[0] += rgba0[4];
  616. xyz[1] += rgba0[5];
  617. xyz[2] += rgba0[6];
  618. xyz[3] += rgba0[7];
  619. xyz[0] += rgba1[0];
  620. xyz[1] += rgba1[1];
  621. xyz[2] += rgba1[2];
  622. xyz[3] += rgba1[3];
  623. xyz[0] += rgba1[4];
  624. xyz[1] += rgba1[5];
  625. xyz[2] += rgba1[6];
  626. xyz[3] += rgba1[7];
  627. xyz[0] *= 1.0f/4.0f;
  628. xyz[1] *= 1.0f/4.0f;
  629. xyz[2] *= 1.0f/4.0f;
  630. xyz[3] *= 1.0f/4.0f;
  631. bx::packRgba32F(dst, xyz);
  632. }
  633. }
  634. }
  635. else
  636. {
  637. const uint32_t slicePitch = _srcPitch*_height;
  638. for (uint32_t zz = 0; zz < dstDepth; ++zz, src += slicePitch)
  639. {
  640. for (uint32_t yy = 0, ystep = _srcPitch*2; yy < dstHeight; ++yy, src += ystep)
  641. {
  642. const float* rgba0 = (const float*)&src[0];
  643. const float* rgba1 = (const float*)&src[_srcPitch];
  644. const float* rgba2 = (const float*)&src[slicePitch];
  645. const float* rgba3 = (const float*)&src[slicePitch+_srcPitch];
  646. for (uint32_t xx = 0
  647. ; xx < dstWidth
  648. ; ++xx, rgba0 += 8, rgba1 += 8, rgba2 += 8, rgba3 += 8, dst += 16
  649. )
  650. {
  651. float xyz[4];
  652. xyz[0] = rgba0[0];
  653. xyz[1] = rgba0[1];
  654. xyz[2] = rgba0[2];
  655. xyz[3] = rgba0[3];
  656. xyz[0] += rgba0[4];
  657. xyz[1] += rgba0[5];
  658. xyz[2] += rgba0[6];
  659. xyz[3] += rgba0[7];
  660. xyz[0] += rgba1[0];
  661. xyz[1] += rgba1[1];
  662. xyz[2] += rgba1[2];
  663. xyz[3] += rgba1[3];
  664. xyz[0] += rgba1[4];
  665. xyz[1] += rgba1[5];
  666. xyz[2] += rgba1[6];
  667. xyz[3] += rgba1[7];
  668. xyz[0] += rgba2[0];
  669. xyz[1] += rgba2[1];
  670. xyz[2] += rgba2[2];
  671. xyz[3] += rgba2[3];
  672. xyz[0] += rgba2[4];
  673. xyz[1] += rgba2[5];
  674. xyz[2] += rgba2[6];
  675. xyz[3] += rgba2[7];
  676. xyz[0] += rgba3[0];
  677. xyz[1] += rgba3[1];
  678. xyz[2] += rgba3[2];
  679. xyz[3] += rgba3[3];
  680. xyz[0] += rgba3[4];
  681. xyz[1] += rgba3[5];
  682. xyz[2] += rgba3[6];
  683. xyz[3] += rgba3[7];
  684. xyz[0] *= 1.0f/8.0f;
  685. xyz[1] *= 1.0f/8.0f;
  686. xyz[2] *= 1.0f/8.0f;
  687. xyz[3] *= 1.0f/8.0f;
  688. bx::packRgba32F(dst, xyz);
  689. }
  690. }
  691. }
  692. }
  693. }
  694. void imageRgba32fLinearDownsample2x2(void* _dst, uint32_t _width, uint32_t _height, uint32_t _depth, uint32_t _srcPitch, const void* _src)
  695. {
  696. imageRgba32fLinearDownsample2x2Ref(_dst, _width, _height, _depth, _srcPitch, _src);
  697. }
  698. void imageRgba32fDownsample2x2Ref(void* _dst, uint32_t _width, uint32_t _height, uint32_t _depth, uint32_t _srcPitch, const void* _src)
  699. {
  700. const uint32_t dstWidth = _width/2;
  701. const uint32_t dstHeight = _height/2;
  702. const uint32_t dstDepth = _depth/2;
  703. if (0 == dstWidth
  704. || 0 == dstHeight)
  705. {
  706. return;
  707. }
  708. const uint8_t* src = (const uint8_t*)_src;
  709. uint8_t* dst = (uint8_t*)_dst;
  710. if (0 == dstDepth)
  711. {
  712. for (uint32_t yy = 0, ystep = _srcPitch*2; yy < dstHeight; ++yy, src += ystep)
  713. {
  714. const float* rgba0 = (const float*)&src[0];
  715. const float* rgba1 = (const float*)&src[_srcPitch];
  716. for (uint32_t xx = 0; xx < dstWidth; ++xx, rgba0 += 8, rgba1 += 8, dst += 16)
  717. {
  718. float xyz[4];
  719. xyz[0] = bx::toLinear(rgba0[0]);
  720. xyz[1] = bx::toLinear(rgba0[1]);
  721. xyz[2] = bx::toLinear(rgba0[2]);
  722. xyz[3] = rgba0[3];
  723. xyz[0] += bx::toLinear(rgba0[4]);
  724. xyz[1] += bx::toLinear(rgba0[5]);
  725. xyz[2] += bx::toLinear(rgba0[6]);
  726. xyz[3] += rgba0[7];
  727. xyz[0] += bx::toLinear(rgba1[0]);
  728. xyz[1] += bx::toLinear(rgba1[1]);
  729. xyz[2] += bx::toLinear(rgba1[2]);
  730. xyz[3] += rgba1[3];
  731. xyz[0] += bx::toLinear(rgba1[4]);
  732. xyz[1] += bx::toLinear(rgba1[5]);
  733. xyz[2] += bx::toLinear(rgba1[6]);
  734. xyz[3] += rgba1[7];
  735. xyz[0] = bx::toGamma(xyz[0]/4.0f);
  736. xyz[1] = bx::toGamma(xyz[1]/4.0f);
  737. xyz[2] = bx::toGamma(xyz[2]/4.0f);
  738. xyz[3] = xyz[3]/4.0f;
  739. bx::packRgba32F(dst, xyz);
  740. }
  741. }
  742. }
  743. else
  744. {
  745. const uint32_t slicePitch = _srcPitch*_height;
  746. for (uint32_t zz = 0; zz < dstDepth; ++zz, src += slicePitch)
  747. {
  748. for (uint32_t yy = 0, ystep = _srcPitch*2; yy < dstHeight; ++yy, src += ystep)
  749. {
  750. const float* rgba0 = (const float*)&src[0];
  751. const float* rgba1 = (const float*)&src[_srcPitch];
  752. const float* rgba2 = (const float*)&src[slicePitch];
  753. const float* rgba3 = (const float*)&src[slicePitch+_srcPitch];
  754. for (uint32_t xx = 0
  755. ; xx < dstWidth
  756. ; ++xx, rgba0 += 8, rgba1 += 8, rgba2 += 8, rgba3 += 8, dst += 16
  757. )
  758. {
  759. float xyz[4];
  760. xyz[0] = bx::toLinear(rgba0[0]);
  761. xyz[1] = bx::toLinear(rgba0[1]);
  762. xyz[2] = bx::toLinear(rgba0[2]);
  763. xyz[3] = rgba0[3];
  764. xyz[0] += bx::toLinear(rgba0[4]);
  765. xyz[1] += bx::toLinear(rgba0[5]);
  766. xyz[2] += bx::toLinear(rgba0[6]);
  767. xyz[3] += rgba0[7];
  768. xyz[0] += bx::toLinear(rgba1[0]);
  769. xyz[1] += bx::toLinear(rgba1[1]);
  770. xyz[2] += bx::toLinear(rgba1[2]);
  771. xyz[3] += rgba1[3];
  772. xyz[0] += bx::toLinear(rgba1[4]);
  773. xyz[1] += bx::toLinear(rgba1[5]);
  774. xyz[2] += bx::toLinear(rgba1[6]);
  775. xyz[3] += rgba1[7];
  776. xyz[0] += bx::toLinear(rgba2[0]);
  777. xyz[1] += bx::toLinear(rgba2[1]);
  778. xyz[2] += bx::toLinear(rgba2[2]);
  779. xyz[3] += rgba2[3];
  780. xyz[0] += bx::toLinear(rgba2[4]);
  781. xyz[1] += bx::toLinear(rgba2[5]);
  782. xyz[2] += bx::toLinear(rgba2[6]);
  783. xyz[3] += rgba2[7];
  784. xyz[0] += bx::toLinear(rgba3[0]);
  785. xyz[1] += bx::toLinear(rgba3[1]);
  786. xyz[2] += bx::toLinear(rgba3[2]);
  787. xyz[3] += rgba3[3];
  788. xyz[0] += bx::toLinear(rgba3[4]);
  789. xyz[1] += bx::toLinear(rgba3[5]);
  790. xyz[2] += bx::toLinear(rgba3[6]);
  791. xyz[3] += rgba3[7];
  792. xyz[0] = bx::toGamma(xyz[0]/8.0f);
  793. xyz[1] = bx::toGamma(xyz[1]/8.0f);
  794. xyz[2] = bx::toGamma(xyz[2]/8.0f);
  795. xyz[3] = xyz[3]/8.0f;
  796. bx::packRgba32F(dst, xyz);
  797. }
  798. }
  799. }
  800. }
  801. }
  802. void imageRgba32fDownsample2x2(void* _dst, uint32_t _width, uint32_t _height, uint32_t _depth, uint32_t _srcPitch, const void* _src)
  803. {
  804. imageRgba32fDownsample2x2Ref(_dst, _width, _height, _depth, _srcPitch, _src);
  805. }
  806. void imageRgba32fDownsample2x2NormalMapRef(void* _dst, uint32_t _width, uint32_t _height, uint32_t _srcPitch, uint32_t _dstPitch, const void* _src)
  807. {
  808. const uint32_t dstWidth = _width/2;
  809. const uint32_t dstHeight = _height/2;
  810. if (0 == dstWidth
  811. || 0 == dstHeight)
  812. {
  813. return;
  814. }
  815. const uint8_t* src = (const uint8_t*)_src;
  816. for (uint32_t yy = 0, ystep = _srcPitch*2; yy < dstHeight; ++yy, src += ystep)
  817. {
  818. const float* rgba0 = (const float*)&src[0];
  819. const float* rgba1 = (const float*)&src[_srcPitch];
  820. uint8_t* dst = (uint8_t*)_dst + _dstPitch*yy;
  821. for (uint32_t xx = 0; xx < dstWidth; ++xx, rgba0 += 8, rgba1 += 8, dst += 16)
  822. {
  823. float xyz[3];
  824. xyz[0] = rgba0[0];
  825. xyz[1] = rgba0[1];
  826. xyz[2] = rgba0[2];
  827. xyz[0] += rgba0[4];
  828. xyz[1] += rgba0[5];
  829. xyz[2] += rgba0[6];
  830. xyz[0] += rgba1[0];
  831. xyz[1] += rgba1[1];
  832. xyz[2] += rgba1[2];
  833. xyz[0] += rgba1[4];
  834. xyz[1] += rgba1[5];
  835. xyz[2] += rgba1[6];
  836. bx::store(dst, bx::normalize(bx::load<bx::Vec3>(xyz) ) );
  837. }
  838. }
  839. }
  840. void imageRgba32fDownsample2x2NormalMap(void* _dst, uint32_t _width, uint32_t _height, uint32_t _srcPitch, uint32_t _dstPitch, const void* _src)
  841. {
  842. imageRgba32fDownsample2x2NormalMapRef(_dst, _width, _height, _srcPitch, _dstPitch, _src);
  843. }
  844. void imageSwizzleBgra8Ref(void* _dst, uint32_t _dstPitch, uint32_t _width, uint32_t _height, const void* _src, uint32_t _srcPitch)
  845. {
  846. const uint8_t* srcData = (uint8_t*) _src;
  847. uint8_t* dstData = (uint8_t*)_dst;
  848. for (uint32_t yy = 0; yy < _height; ++yy, srcData += _srcPitch, dstData += _dstPitch)
  849. {
  850. const uint8_t* src = srcData;
  851. uint8_t* dst = dstData;
  852. for (uint32_t xx = 0; xx < _width; ++xx, src += 4, dst += 4)
  853. {
  854. uint8_t rr = src[0];
  855. uint8_t gg = src[1];
  856. uint8_t bb = src[2];
  857. uint8_t aa = src[3];
  858. dst[0] = bb;
  859. dst[1] = gg;
  860. dst[2] = rr;
  861. dst[3] = aa;
  862. }
  863. }
  864. }
  865. void imageSwizzleBgra8(void* _dst, uint32_t _dstPitch, uint32_t _width, uint32_t _height, const void* _src, uint32_t _srcPitch)
  866. {
  867. // Test can we do four 4-byte pixels at the time.
  868. if (0 != (_width&0x3)
  869. || _width < 4
  870. || !bx::isAligned(_src, 16)
  871. || !bx::isAligned(_dst, 16) )
  872. {
  873. BX_WARN(false, "Image swizzle is taking slow path.");
  874. BX_WARN(bx::isAligned(_src, 16), "Source %p is not 16-byte aligned.", _src);
  875. BX_WARN(bx::isAligned(_dst, 16), "Destination %p is not 16-byte aligned.", _dst);
  876. BX_WARN(_width < 4, "Image width must be multiple of 4 (width %d).", _width);
  877. imageSwizzleBgra8Ref(_dst, _dstPitch, _width, _height, _src, _srcPitch);
  878. return;
  879. }
  880. using namespace bx;
  881. const simd128_t mf0f0 = simd_isplat(0xff00ff00);
  882. const simd128_t m0f0f = simd_isplat(0x00ff00ff);
  883. const uint32_t width = _width/4;
  884. const uint8_t* srcData = (uint8_t*) _src;
  885. uint8_t* dstData = (uint8_t*)_dst;
  886. for (uint32_t yy = 0; yy < _height; ++yy, srcData += _srcPitch, dstData += _dstPitch)
  887. {
  888. const uint8_t* src = srcData;
  889. uint8_t* dst = dstData;
  890. for (uint32_t xx = 0; xx < width; ++xx, src += 16, dst += 16)
  891. {
  892. const simd128_t tabgr = simd_ld(src);
  893. const simd128_t t00ab = simd_srl(tabgr, 16);
  894. const simd128_t tgr00 = simd_sll(tabgr, 16);
  895. const simd128_t tgrab = simd_or(t00ab, tgr00);
  896. const simd128_t ta0g0 = simd_and(tabgr, mf0f0);
  897. const simd128_t t0r0b = simd_and(tgrab, m0f0f);
  898. const simd128_t targb = simd_or(ta0g0, t0r0b);
  899. simd_st(dst, targb);
  900. }
  901. }
  902. }
  903. void imageCopy(void* _dst, uint32_t _height, uint32_t _srcPitch, uint32_t _depth, const void* _src, uint32_t _dstPitch)
  904. {
  905. const uint32_t pitch = bx::uint32_min(_srcPitch, _dstPitch);
  906. const uint8_t* src = (uint8_t*)_src;
  907. uint8_t* dst = (uint8_t*)_dst;
  908. for (uint32_t zz = 0; zz < _depth; ++zz, src += _srcPitch*_height, dst += _dstPitch*_height)
  909. {
  910. bx::memCopy(dst, _dstPitch, src, _srcPitch, pitch, _height);
  911. }
  912. }
  913. void imageCopy(void* _dst, uint32_t _width, uint32_t _height, uint32_t _depth, uint32_t _bpp, uint32_t _srcPitch, const void* _src)
  914. {
  915. const uint32_t dstPitch = _width*_bpp/8;
  916. imageCopy(_dst, _height, _srcPitch, _depth, _src, dstPitch);
  917. }
  918. struct PackUnpack
  919. {
  920. PackFn pack;
  921. UnpackFn unpack;
  922. };
  923. static const PackUnpack s_packUnpack[] =
  924. {
  925. { NULL, NULL }, // BC1
  926. { NULL, NULL }, // BC2
  927. { NULL, NULL }, // BC3
  928. { NULL, NULL }, // BC4
  929. { NULL, NULL }, // BC5
  930. { NULL, NULL }, // BC6H
  931. { NULL, NULL }, // BC7
  932. { NULL, NULL }, // ETC1
  933. { NULL, NULL }, // ETC2
  934. { NULL, NULL }, // ETC2A
  935. { NULL, NULL }, // ETC2A1
  936. { NULL, NULL }, // PTC12
  937. { NULL, NULL }, // PTC14
  938. { NULL, NULL }, // PTC12A
  939. { NULL, NULL }, // PTC14A
  940. { NULL, NULL }, // PTC22
  941. { NULL, NULL }, // PTC24
  942. { NULL, NULL }, // ATC
  943. { NULL, NULL }, // ATCE
  944. { NULL, NULL }, // ATCI
  945. { NULL, NULL }, // ASTC4x4
  946. { NULL, NULL }, // ASTC5x4
  947. { NULL, NULL }, // ASTC5x5
  948. { NULL, NULL }, // ASTC6x5
  949. { NULL, NULL }, // ASTC6x6
  950. { NULL, NULL }, // ASTC8x5
  951. { NULL, NULL }, // ASTC8x6
  952. { NULL, NULL }, // ASTC8x8
  953. { NULL, NULL }, // ASTC10x5
  954. { NULL, NULL }, // ASTC10x6
  955. { NULL, NULL }, // ASTC10x8
  956. { NULL, NULL }, // ASTC10x10
  957. { NULL, NULL }, // ASTC12x10
  958. { NULL, NULL }, // ASTC12x12
  959. { NULL, NULL }, // Unknown
  960. { NULL, NULL }, // R1
  961. { bx::packA8, bx::unpackA8 }, // A8
  962. { bx::packR8, bx::unpackR8 }, // R8
  963. { bx::packR8I, bx::unpackR8I }, // R8I
  964. { bx::packR8U, bx::unpackR8U }, // R8U
  965. { bx::packR8S, bx::unpackR8S }, // R8S
  966. { bx::packR16, bx::unpackR16 }, // R16
  967. { bx::packR16I, bx::unpackR16I }, // R16I
  968. { bx::packR16U, bx::unpackR16U }, // R16U
  969. { bx::packR16F, bx::unpackR16F }, // R16F
  970. { bx::packR16S, bx::unpackR16S }, // R16S
  971. { bx::packR32I, bx::unpackR32I }, // R32I
  972. { bx::packR32U, bx::unpackR32U }, // R32U
  973. { bx::packR32F, bx::unpackR32F }, // R32F
  974. { bx::packRg8, bx::unpackRg8 }, // RG8
  975. { bx::packRg8I, bx::unpackRg8I }, // RG8I
  976. { bx::packRg8U, bx::unpackRg8U }, // RG8U
  977. { bx::packRg8S, bx::unpackRg8S }, // RG8S
  978. { bx::packRg16, bx::unpackRg16 }, // RG16
  979. { bx::packRg16I, bx::unpackRg16I }, // RG16I
  980. { bx::packRg16U, bx::unpackRg16U }, // RG16U
  981. { bx::packRg16F, bx::unpackRg16F }, // RG16F
  982. { bx::packRg16S, bx::unpackRg16S }, // RG16S
  983. { bx::packRg32I, bx::unpackRg32I }, // RG32I
  984. { bx::packRg32U, bx::unpackRg32U }, // RG32U
  985. { bx::packRg32F, bx::unpackRg32F }, // RG32F
  986. { bx::packRgb8, bx::unpackRgb8 }, // RGB8
  987. { bx::packRgb8S, bx::unpackRgb8S }, // RGB8S
  988. { bx::packRgb8I, bx::unpackRgb8I }, // RGB8I
  989. { bx::packRgb8U, bx::unpackRgb8U }, // RGB8U
  990. { bx::packRgb9E5F, bx::unpackRgb9E5F }, // RGB9E5F
  991. { bx::packBgra8, bx::unpackBgra8 }, // BGRA8
  992. { bx::packRgba8, bx::unpackRgba8 }, // RGBA8
  993. { bx::packRgba8I, bx::unpackRgba8I }, // RGBA8I
  994. { bx::packRgba8U, bx::unpackRgba8U }, // RGBA8U
  995. { bx::packRgba8S, bx::unpackRgba8S }, // RGBA8S
  996. { bx::packRgba16, bx::unpackRgba16 }, // RGBA16
  997. { bx::packRgba16I, bx::unpackRgba16I }, // RGBA16I
  998. { bx::packRgba16U, bx::unpackRgba16U }, // RGBA16U
  999. { bx::packRgba16F, bx::unpackRgba16F }, // RGBA16F
  1000. { bx::packRgba16S, bx::unpackRgba16S }, // RGBA16S
  1001. { bx::packRgba32I, bx::unpackRgba32I }, // RGBA32I
  1002. { bx::packRgba32U, bx::unpackRgba32U }, // RGBA32U
  1003. { bx::packRgba32F, bx::unpackRgba32F }, // RGBA32F
  1004. { bx::packB5G6R5, bx::unpackB5G6R5 }, // B5G6R5
  1005. { bx::packR5G6B5, bx::unpackR5G6B5 }, // R5G6B5
  1006. { bx::packBgra4, bx::unpackBgra4 }, // BGRA4
  1007. { bx::packRgba4, bx::unpackRgba4 }, // RGBA4
  1008. { bx::packBgr5a1, bx::unpackBgr5a1 }, // BGR5A1
  1009. { bx::packRgb5a1, bx::unpackRgb5a1 }, // RGB5A1
  1010. { bx::packRgb10A2, bx::unpackRgb10A2 }, // RGB10A2
  1011. { bx::packRG11B10F, bx::unpackRG11B10F }, // RG11B10F
  1012. { NULL, NULL }, // UnknownDepth
  1013. { bx::packR16, bx::unpackR16 }, // D16
  1014. { bx::packR24, bx::unpackR24 }, // D24
  1015. { bx::packR24G8, bx::unpackR24G8 }, // D24S8
  1016. { NULL, NULL }, // D32
  1017. { bx::packR16F, bx::unpackR16F }, // D16F
  1018. { NULL, NULL }, // D24F
  1019. { bx::packR32F, bx::unpackR32F }, // D32F
  1020. { bx::packR8, bx::unpackR8 }, // D0S8
  1021. };
  1022. BX_STATIC_ASSERT(TextureFormat::Count == BX_COUNTOF(s_packUnpack) );
  1023. PackFn getPack(TextureFormat::Enum _format)
  1024. {
  1025. return s_packUnpack[_format].pack;
  1026. }
  1027. UnpackFn getUnpack(TextureFormat::Enum _format)
  1028. {
  1029. return s_packUnpack[_format].unpack;
  1030. }
  1031. bool imageConvert(TextureFormat::Enum _dstFormat, TextureFormat::Enum _srcFormat)
  1032. {
  1033. UnpackFn unpack = s_packUnpack[_srcFormat].unpack;
  1034. PackFn pack = s_packUnpack[_dstFormat].pack;
  1035. return NULL != pack
  1036. && NULL != unpack
  1037. ;
  1038. }
  1039. void imageConvert(void* _dst, uint32_t _bpp, PackFn _pack, const void* _src, UnpackFn _unpack, uint32_t _size)
  1040. {
  1041. const uint8_t* src = (uint8_t*)_src;
  1042. uint8_t* dst = (uint8_t*)_dst;
  1043. const uint32_t size = _size * 8 / _bpp;
  1044. for (uint32_t ii = 0; ii < size; ++ii)
  1045. {
  1046. float rgba[4];
  1047. _unpack(rgba, &src[ii*_bpp/8]);
  1048. _pack(&dst[ii*_bpp/8], rgba);
  1049. }
  1050. }
  1051. void imageConvert(void* _dst, uint32_t _dstBpp, PackFn _pack, const void* _src, uint32_t _srcBpp, UnpackFn _unpack, uint32_t _width, uint32_t _height, uint32_t _depth, uint32_t _srcPitch, uint32_t _dstPitch)
  1052. {
  1053. const uint8_t* src = (uint8_t*)_src;
  1054. uint8_t* dst = (uint8_t*)_dst;
  1055. for (uint32_t zz = 0; zz < _depth; ++zz)
  1056. {
  1057. for (uint32_t yy = 0; yy < _height; ++yy, src += _srcPitch, dst += _dstPitch)
  1058. {
  1059. for (uint32_t xx = 0; xx < _width; ++xx)
  1060. {
  1061. float rgba[4];
  1062. _unpack(rgba, &src[xx*_srcBpp/8]);
  1063. _pack(&dst[xx*_dstBpp/8], rgba);
  1064. }
  1065. }
  1066. }
  1067. }
  1068. bool imageConvert(bx::AllocatorI* _allocator, void* _dst, TextureFormat::Enum _dstFormat, const void* _src, TextureFormat::Enum _srcFormat, uint32_t _width, uint32_t _height, uint32_t _depth, uint32_t _srcPitch, uint32_t _dstPitch)
  1069. {
  1070. UnpackFn unpack = s_packUnpack[_srcFormat].unpack;
  1071. PackFn pack = s_packUnpack[_dstFormat].pack;
  1072. if (NULL == pack
  1073. || NULL == unpack)
  1074. {
  1075. switch (_dstFormat)
  1076. {
  1077. case TextureFormat::RGBA8:
  1078. imageDecodeToRgba8(_allocator, _dst, _src, _width, _height, _width*4, _srcFormat);
  1079. return true;
  1080. case TextureFormat::BGRA8:
  1081. imageDecodeToBgra8(_allocator, _dst, _src, _width, _height, _width*4, _srcFormat);
  1082. return true;
  1083. case TextureFormat::RGBA32F:
  1084. imageDecodeToRgba32f(_allocator, _dst, _src, _width, _height, 1, _width*16, _srcFormat);
  1085. return true;
  1086. default:
  1087. break;
  1088. }
  1089. return false;
  1090. }
  1091. const uint32_t srcBpp = s_imageBlockInfo[_srcFormat].bitsPerPixel;
  1092. const uint32_t dstBpp = s_imageBlockInfo[_dstFormat].bitsPerPixel;
  1093. imageConvert(_dst, dstBpp, pack, _src, srcBpp, unpack, _width, _height, _depth, _srcPitch, _dstPitch);
  1094. return true;
  1095. }
  1096. bool imageConvert(bx::AllocatorI* _allocator, void* _dst, TextureFormat::Enum _dstFormat, const void* _src, TextureFormat::Enum _srcFormat, uint32_t _width, uint32_t _height, uint32_t _depth)
  1097. {
  1098. const uint32_t srcBpp = s_imageBlockInfo[_srcFormat].bitsPerPixel;
  1099. if (_dstFormat == _srcFormat)
  1100. {
  1101. bx::memCopy(_dst, _src, _width*_height*_depth*(srcBpp/8) );
  1102. return true;
  1103. }
  1104. const uint32_t dstBpp = s_imageBlockInfo[_dstFormat].bitsPerPixel;
  1105. const uint32_t dstPitch = _width * dstBpp / 8;
  1106. return imageConvert(_allocator, _dst, _dstFormat, _src, _srcFormat, _width, _height, _depth, _width*srcBpp/8, dstPitch);
  1107. }
  1108. ImageContainer* imageConvert(bx::AllocatorI* _allocator, TextureFormat::Enum _dstFormat, const ImageContainer& _input, bool _convertMips)
  1109. {
  1110. ImageContainer* output = imageAlloc(_allocator
  1111. , _dstFormat
  1112. , uint16_t(_input.m_width)
  1113. , uint16_t(_input.m_height)
  1114. , uint16_t(_input.m_depth)
  1115. , _input.m_numLayers
  1116. , _input.m_cubeMap
  1117. , _convertMips && 1 < _input.m_numMips
  1118. );
  1119. const uint16_t numSides = _input.m_numLayers * (_input.m_cubeMap ? 6 : 1);
  1120. for (uint16_t side = 0; side < numSides; ++side)
  1121. {
  1122. for (uint8_t lod = 0, num = _convertMips ? _input.m_numMips : 1; lod < num; ++lod)
  1123. {
  1124. ImageMip mip;
  1125. if (imageGetRawData(_input, side, lod, _input.m_data, _input.m_size, mip) )
  1126. {
  1127. ImageMip dstMip;
  1128. imageGetRawData(*output, side, lod, output->m_data, output->m_size, dstMip);
  1129. uint8_t* dstData = const_cast<uint8_t*>(dstMip.m_data);
  1130. bool ok = imageConvert(
  1131. _allocator
  1132. , dstData
  1133. , _dstFormat
  1134. , mip.m_data
  1135. , mip.m_format
  1136. , mip.m_width
  1137. , mip.m_height
  1138. , mip.m_depth
  1139. );
  1140. BX_ASSERT(ok, "Conversion from %s to %s failed!"
  1141. , getName(_input.m_format)
  1142. , getName(output->m_format)
  1143. );
  1144. BX_UNUSED(ok);
  1145. }
  1146. }
  1147. }
  1148. return output;
  1149. }
  1150. typedef bool (*ParseFn)(ImageContainer&, bx::ReaderSeekerI*, bx::Error*);
  1151. template<uint32_t magicT, ParseFn parseFnT>
  1152. ImageContainer* imageParseT(bx::AllocatorI* _allocator, const void* _src, uint32_t _size, bx::Error* _err)
  1153. {
  1154. bx::MemoryReader reader(_src, _size);
  1155. uint32_t magic;
  1156. bx::read(&reader, magic, bx::ErrorIgnore{});
  1157. ImageContainer imageContainer;
  1158. if (magicT != magic)
  1159. {
  1160. return NULL;
  1161. }
  1162. if (!parseFnT(imageContainer, &reader, _err) )
  1163. {
  1164. return NULL;
  1165. }
  1166. ImageContainer* output = imageAlloc(_allocator
  1167. , imageContainer.m_format
  1168. , uint16_t(imageContainer.m_width)
  1169. , uint16_t(imageContainer.m_height)
  1170. , uint16_t(imageContainer.m_depth)
  1171. , imageContainer.m_numLayers
  1172. , imageContainer.m_cubeMap
  1173. , 1 < imageContainer.m_numMips
  1174. );
  1175. const uint16_t numSides = imageContainer.m_numLayers * (imageContainer.m_cubeMap ? 6 : 1);
  1176. for (uint16_t side = 0; side < numSides; ++side)
  1177. {
  1178. for (uint8_t lod = 0, num = imageContainer.m_numMips; lod < num; ++lod)
  1179. {
  1180. ImageMip dstMip;
  1181. if (imageGetRawData(*output, side, lod, output->m_data, output->m_size, dstMip) )
  1182. {
  1183. ImageMip mip;
  1184. if (imageGetRawData(imageContainer, side, lod, _src, _size, mip) )
  1185. {
  1186. uint8_t* dstData = const_cast<uint8_t*>(dstMip.m_data);
  1187. bx::memCopy(dstData, mip.m_data, mip.m_size);
  1188. }
  1189. }
  1190. }
  1191. }
  1192. return output;
  1193. }
  1194. static uint8_t bitRangeConvert(uint32_t _in, uint32_t _from, uint32_t _to)
  1195. {
  1196. using namespace bx;
  1197. uint32_t tmp0 = uint32_sll(1, _to);
  1198. uint32_t tmp1 = uint32_sll(1, _from);
  1199. uint32_t tmp2 = uint32_dec(tmp0);
  1200. uint32_t tmp3 = uint32_dec(tmp1);
  1201. uint32_t tmp4 = uint32_mul(_in, tmp2);
  1202. uint32_t tmp5 = uint32_add(tmp3, tmp4);
  1203. uint32_t tmp6 = uint32_srl(tmp5, _from);
  1204. uint32_t tmp7 = uint32_add(tmp5, tmp6);
  1205. uint32_t result = uint32_srl(tmp7, _from);
  1206. return uint8_t(result);
  1207. }
  1208. static void decodeBlockDxt(uint8_t _dst[16*4], const uint8_t _src[8])
  1209. {
  1210. if (!BX_ENABLED(BIMG_DECODE_BC2 || BIMG_DECODE_BC3) )
  1211. {
  1212. return;
  1213. }
  1214. uint8_t colors[4*3];
  1215. uint32_t c0 = _src[0] | (_src[1] << 8);
  1216. colors[0] = bitRangeConvert( (c0>> 0)&0x1f, 5, 8);
  1217. colors[1] = bitRangeConvert( (c0>> 5)&0x3f, 6, 8);
  1218. colors[2] = bitRangeConvert( (c0>>11)&0x1f, 5, 8);
  1219. uint32_t c1 = _src[2] | (_src[3] << 8);
  1220. colors[3] = bitRangeConvert( (c1>> 0)&0x1f, 5, 8);
  1221. colors[4] = bitRangeConvert( (c1>> 5)&0x3f, 6, 8);
  1222. colors[5] = bitRangeConvert( (c1>>11)&0x1f, 5, 8);
  1223. colors[6] = (2*colors[0] + colors[3]) / 3;
  1224. colors[7] = (2*colors[1] + colors[4]) / 3;
  1225. colors[8] = (2*colors[2] + colors[5]) / 3;
  1226. colors[ 9] = (colors[0] + 2*colors[3]) / 3;
  1227. colors[10] = (colors[1] + 2*colors[4]) / 3;
  1228. colors[11] = (colors[2] + 2*colors[5]) / 3;
  1229. for (uint32_t ii = 0, next = 8*4; ii < 16*4; ii += 4, next += 2)
  1230. {
  1231. int idx = ( (_src[next>>3] >> (next & 7) ) & 3) * 3;
  1232. _dst[ii+0] = colors[idx+0];
  1233. _dst[ii+1] = colors[idx+1];
  1234. _dst[ii+2] = colors[idx+2];
  1235. }
  1236. }
  1237. static void decodeBlockDxt1(uint8_t _dst[16*4], const uint8_t _src[8])
  1238. {
  1239. if (!BX_ENABLED(BIMG_DECODE_BC1 || BIMG_DECODE_BC2 || BIMG_DECODE_BC3) )
  1240. {
  1241. return;
  1242. }
  1243. uint8_t colors[4*4];
  1244. uint32_t c0 = _src[0] | (_src[1] << 8);
  1245. colors[0] = bitRangeConvert( (c0>> 0)&0x1f, 5, 8);
  1246. colors[1] = bitRangeConvert( (c0>> 5)&0x3f, 6, 8);
  1247. colors[2] = bitRangeConvert( (c0>>11)&0x1f, 5, 8);
  1248. colors[3] = 255;
  1249. uint32_t c1 = _src[2] | (_src[3] << 8);
  1250. colors[4] = bitRangeConvert( (c1>> 0)&0x1f, 5, 8);
  1251. colors[5] = bitRangeConvert( (c1>> 5)&0x3f, 6, 8);
  1252. colors[6] = bitRangeConvert( (c1>>11)&0x1f, 5, 8);
  1253. colors[7] = 255;
  1254. if (c0 > c1)
  1255. {
  1256. colors[ 8] = (2*colors[0] + colors[4]) / 3;
  1257. colors[ 9] = (2*colors[1] + colors[5]) / 3;
  1258. colors[10] = (2*colors[2] + colors[6]) / 3;
  1259. colors[11] = 255;
  1260. colors[12] = (colors[0] + 2*colors[4]) / 3;
  1261. colors[13] = (colors[1] + 2*colors[5]) / 3;
  1262. colors[14] = (colors[2] + 2*colors[6]) / 3;
  1263. colors[15] = 255;
  1264. }
  1265. else
  1266. {
  1267. colors[ 8] = (colors[0] + colors[4]) / 2;
  1268. colors[ 9] = (colors[1] + colors[5]) / 2;
  1269. colors[10] = (colors[2] + colors[6]) / 2;
  1270. colors[11] = 255;
  1271. colors[12] = 0;
  1272. colors[13] = 0;
  1273. colors[14] = 0;
  1274. colors[15] = 0;
  1275. }
  1276. for (uint32_t ii = 0, next = 8*4; ii < 16*4; ii += 4, next += 2)
  1277. {
  1278. int idx = ( (_src[next>>3] >> (next & 7) ) & 3) * 4;
  1279. _dst[ii+0] = colors[idx+0];
  1280. _dst[ii+1] = colors[idx+1];
  1281. _dst[ii+2] = colors[idx+2];
  1282. _dst[ii+3] = colors[idx+3];
  1283. }
  1284. }
  1285. static void decodeBlockDxt23A(uint8_t _dst[16*4], const uint8_t _src[8])
  1286. {
  1287. if (!BX_ENABLED(BIMG_DECODE_BC2) )
  1288. {
  1289. return;
  1290. }
  1291. for (uint32_t ii = 0, next = 0; ii < 16*4; ii += 4, next += 4)
  1292. {
  1293. uint32_t c0 = (_src[next>>3] >> (next&7) ) & 0xf;
  1294. _dst[ii] = bitRangeConvert(c0, 4, 8);
  1295. }
  1296. }
  1297. static void decodeBlockDxt45A(uint8_t _dst[16*4], const uint8_t _src[8])
  1298. {
  1299. if (!BX_ENABLED(BIMG_DECODE_BC3 || BIMG_DECODE_BC4 || BIMG_DECODE_BC5) )
  1300. {
  1301. return;
  1302. }
  1303. uint8_t alpha[8];
  1304. alpha[0] = _src[0];
  1305. alpha[1] = _src[1];
  1306. if (alpha[0] > alpha[1])
  1307. {
  1308. alpha[2] = (6*alpha[0] + 1*alpha[1]) / 7;
  1309. alpha[3] = (5*alpha[0] + 2*alpha[1]) / 7;
  1310. alpha[4] = (4*alpha[0] + 3*alpha[1]) / 7;
  1311. alpha[5] = (3*alpha[0] + 4*alpha[1]) / 7;
  1312. alpha[6] = (2*alpha[0] + 5*alpha[1]) / 7;
  1313. alpha[7] = (1*alpha[0] + 6*alpha[1]) / 7;
  1314. }
  1315. else
  1316. {
  1317. alpha[2] = (4*alpha[0] + 1*alpha[1]) / 5;
  1318. alpha[3] = (3*alpha[0] + 2*alpha[1]) / 5;
  1319. alpha[4] = (2*alpha[0] + 3*alpha[1]) / 5;
  1320. alpha[5] = (1*alpha[0] + 4*alpha[1]) / 5;
  1321. alpha[6] = 0;
  1322. alpha[7] = 255;
  1323. }
  1324. uint32_t idx0 = _src[2];
  1325. uint32_t idx1 = _src[5];
  1326. idx0 |= uint32_t(_src[3])<<8;
  1327. idx1 |= uint32_t(_src[6])<<8;
  1328. idx0 |= uint32_t(_src[4])<<16;
  1329. idx1 |= uint32_t(_src[7])<<16;
  1330. for (uint32_t ii = 0; ii < 8*4; ii += 4)
  1331. {
  1332. _dst[ii] = alpha[idx0&7];
  1333. _dst[ii+32] = alpha[idx1&7];
  1334. idx0 >>= 3;
  1335. idx1 >>= 3;
  1336. }
  1337. }
  1338. // BC6H, BC7
  1339. //
  1340. // Reference(s):
  1341. // - https://web.archive.org/web/20181126035446/https://www.khronos.org/registry/OpenGL/extensions/ARB/ARB_texture_compression_bptc.txt
  1342. // - https://web.archive.org/web/20181126035538/https://docs.microsoft.com/en-us/windows/desktop/direct3d11/bc6h-format
  1343. //
  1344. static const uint16_t s_bptcP2[] =
  1345. { // 3210 0000000000 1111111111 2222222222 3333333333
  1346. 0xcccc, // 0, 0, 1, 1, 0, 0, 1, 1, 0, 0, 1, 1, 0, 0, 1, 1
  1347. 0x8888, // 0, 0, 0, 1, 0, 0, 0, 1, 0, 0, 0, 1, 0, 0, 0, 1
  1348. 0xeeee, // 0, 1, 1, 1, 0, 1, 1, 1, 0, 1, 1, 1, 0, 1, 1, 1
  1349. 0xecc8, // 0, 0, 0, 1, 0, 0, 1, 1, 0, 0, 1, 1, 0, 1, 1, 1
  1350. 0xc880, // 0, 0, 0, 0, 0, 0, 0, 1, 0, 0, 0, 1, 0, 0, 1, 1
  1351. 0xfeec, // 0, 0, 1, 1, 0, 1, 1, 1, 0, 1, 1, 1, 1, 1, 1, 1
  1352. 0xfec8, // 0, 0, 0, 1, 0, 0, 1, 1, 0, 1, 1, 1, 1, 1, 1, 1
  1353. 0xec80, // 0, 0, 0, 0, 0, 0, 0, 1, 0, 0, 1, 1, 0, 1, 1, 1
  1354. 0xc800, // 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 0, 0, 1, 1
  1355. 0xffec, // 0, 0, 1, 1, 0, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1
  1356. 0xfe80, // 0, 0, 0, 0, 0, 0, 0, 1, 0, 1, 1, 1, 1, 1, 1, 1
  1357. 0xe800, // 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 0, 1, 1, 1
  1358. 0xffe8, // 0, 0, 0, 1, 0, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1
  1359. 0xff00, // 0, 0, 0, 0, 0, 0, 0, 0, 1, 1, 1, 1, 1, 1, 1, 1
  1360. 0xfff0, // 0, 0, 0, 0, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1
  1361. 0xf000, // 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 1, 1, 1
  1362. 0xf710, // 0, 0, 0, 0, 1, 0, 0, 0, 1, 1, 1, 0, 1, 1, 1, 1
  1363. 0x008e, // 0, 1, 1, 1, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0
  1364. 0x7100, // 0, 0, 0, 0, 0, 0, 0, 0, 1, 0, 0, 0, 1, 1, 1, 0
  1365. 0x08ce, // 0, 1, 1, 1, 0, 0, 1, 1, 0, 0, 0, 1, 0, 0, 0, 0
  1366. 0x008c, // 0, 0, 1, 1, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0
  1367. 0x7310, // 0, 0, 0, 0, 1, 0, 0, 0, 1, 1, 0, 0, 1, 1, 1, 0
  1368. 0x3100, // 0, 0, 0, 0, 0, 0, 0, 0, 1, 0, 0, 0, 1, 1, 0, 0
  1369. 0x8cce, // 0, 1, 1, 1, 0, 0, 1, 1, 0, 0, 1, 1, 0, 0, 0, 1
  1370. 0x088c, // 0, 0, 1, 1, 0, 0, 0, 1, 0, 0, 0, 1, 0, 0, 0, 0
  1371. 0x3110, // 0, 0, 0, 0, 1, 0, 0, 0, 1, 0, 0, 0, 1, 1, 0, 0
  1372. 0x6666, // 0, 1, 1, 0, 0, 1, 1, 0, 0, 1, 1, 0, 0, 1, 1, 0
  1373. 0x366c, // 0, 0, 1, 1, 0, 1, 1, 0, 0, 1, 1, 0, 1, 1, 0, 0
  1374. 0x17e8, // 0, 0, 0, 1, 0, 1, 1, 1, 1, 1, 1, 0, 1, 0, 0, 0
  1375. 0x0ff0, // 0, 0, 0, 0, 1, 1, 1, 1, 1, 1, 1, 1, 0, 0, 0, 0
  1376. 0x718e, // 0, 1, 1, 1, 0, 0, 0, 1, 1, 0, 0, 0, 1, 1, 1, 0
  1377. 0x399c, // 0, 0, 1, 1, 1, 0, 0, 1, 1, 0, 0, 1, 1, 1, 0, 0
  1378. 0xaaaa, // 0, 1, 0, 1, 0, 1, 0, 1, 0, 1, 0, 1, 0, 1, 0, 1
  1379. 0xf0f0, // 0, 0, 0, 0, 1, 1, 1, 1, 0, 0, 0, 0, 1, 1, 1, 1
  1380. 0x5a5a, // 0, 1, 0, 1, 1, 0, 1, 0, 0, 1, 0, 1, 1, 0, 1, 0
  1381. 0x33cc, // 0, 0, 1, 1, 0, 0, 1, 1, 1, 1, 0, 0, 1, 1, 0, 0
  1382. 0x3c3c, // 0, 0, 1, 1, 1, 1, 0, 0, 0, 0, 1, 1, 1, 1, 0, 0
  1383. 0x55aa, // 0, 1, 0, 1, 0, 1, 0, 1, 1, 0, 1, 0, 1, 0, 1, 0
  1384. 0x9696, // 0, 1, 1, 0, 1, 0, 0, 1, 0, 1, 1, 0, 1, 0, 0, 1
  1385. 0xa55a, // 0, 1, 0, 1, 1, 0, 1, 0, 1, 0, 1, 0, 0, 1, 0, 1
  1386. 0x73ce, // 0, 1, 1, 1, 0, 0, 1, 1, 1, 1, 0, 0, 1, 1, 1, 0
  1387. 0x13c8, // 0, 0, 0, 1, 0, 0, 1, 1, 1, 1, 0, 0, 1, 0, 0, 0
  1388. 0x324c, // 0, 0, 1, 1, 0, 0, 1, 0, 0, 1, 0, 0, 1, 1, 0, 0
  1389. 0x3bdc, // 0, 0, 1, 1, 1, 0, 1, 1, 1, 1, 0, 1, 1, 1, 0, 0
  1390. 0x6996, // 0, 1, 1, 0, 1, 0, 0, 1, 1, 0, 0, 1, 0, 1, 1, 0
  1391. 0xc33c, // 0, 0, 1, 1, 1, 1, 0, 0, 1, 1, 0, 0, 0, 0, 1, 1
  1392. 0x9966, // 0, 1, 1, 0, 0, 1, 1, 0, 1, 0, 0, 1, 1, 0, 0, 1
  1393. 0x0660, // 0, 0, 0, 0, 0, 1, 1, 0, 0, 1, 1, 0, 0, 0, 0, 0
  1394. 0x0272, // 0, 1, 0, 0, 1, 1, 1, 0, 0, 1, 0, 0, 0, 0, 0, 0
  1395. 0x04e4, // 0, 0, 1, 0, 0, 1, 1, 1, 0, 0, 1, 0, 0, 0, 0, 0
  1396. 0x4e40, // 0, 0, 0, 0, 0, 0, 1, 0, 0, 1, 1, 1, 0, 0, 1, 0
  1397. 0x2720, // 0, 0, 0, 0, 0, 1, 0, 0, 1, 1, 1, 0, 0, 1, 0, 0
  1398. 0xc936, // 0, 1, 1, 0, 1, 1, 0, 0, 1, 0, 0, 1, 0, 0, 1, 1
  1399. 0x936c, // 0, 0, 1, 1, 0, 1, 1, 0, 1, 1, 0, 0, 1, 0, 0, 1
  1400. 0x39c6, // 0, 1, 1, 0, 0, 0, 1, 1, 1, 0, 0, 1, 1, 1, 0, 0
  1401. 0x639c, // 0, 0, 1, 1, 1, 0, 0, 1, 1, 1, 0, 0, 0, 1, 1, 0
  1402. 0x9336, // 0, 1, 1, 0, 1, 1, 0, 0, 1, 1, 0, 0, 1, 0, 0, 1
  1403. 0x9cc6, // 0, 1, 1, 0, 0, 0, 1, 1, 0, 0, 1, 1, 1, 0, 0, 1
  1404. 0x817e, // 0, 1, 1, 1, 1, 1, 1, 0, 1, 0, 0, 0, 0, 0, 0, 1
  1405. 0xe718, // 0, 0, 0, 1, 1, 0, 0, 0, 1, 1, 1, 0, 0, 1, 1, 1
  1406. 0xccf0, // 0, 0, 0, 0, 1, 1, 1, 1, 0, 0, 1, 1, 0, 0, 1, 1
  1407. 0x0fcc, // 0, 0, 1, 1, 0, 0, 1, 1, 1, 1, 1, 1, 0, 0, 0, 0
  1408. 0x7744, // 0, 0, 1, 0, 0, 0, 1, 0, 1, 1, 1, 0, 1, 1, 1, 0
  1409. 0xee22, // 0, 1, 0, 0, 0, 1, 0, 0, 0, 1, 1, 1, 0, 1, 1, 1
  1410. };
  1411. static const uint32_t s_bptcP3[] =
  1412. { // 76543210 0000 1111 2222 3333 4444 5555 6666 7777
  1413. 0xaa685050, // 0, 0, 1, 1, 0, 0, 1, 1, 0, 2, 2, 1, 2, 2, 2, 2
  1414. 0x6a5a5040, // 0, 0, 0, 1, 0, 0, 1, 1, 2, 2, 1, 1, 2, 2, 2, 1
  1415. 0x5a5a4200, // 0, 0, 0, 0, 2, 0, 0, 1, 2, 2, 1, 1, 2, 2, 1, 1
  1416. 0x5450a0a8, // 0, 2, 2, 2, 0, 0, 2, 2, 0, 0, 1, 1, 0, 1, 1, 1
  1417. 0xa5a50000, // 0, 0, 0, 0, 0, 0, 0, 0, 1, 1, 2, 2, 1, 1, 2, 2
  1418. 0xa0a05050, // 0, 0, 1, 1, 0, 0, 1, 1, 0, 0, 2, 2, 0, 0, 2, 2
  1419. 0x5555a0a0, // 0, 0, 2, 2, 0, 0, 2, 2, 1, 1, 1, 1, 1, 1, 1, 1
  1420. 0x5a5a5050, // 0, 0, 1, 1, 0, 0, 1, 1, 2, 2, 1, 1, 2, 2, 1, 1
  1421. 0xaa550000, // 0, 0, 0, 0, 0, 0, 0, 0, 1, 1, 1, 1, 2, 2, 2, 2
  1422. 0xaa555500, // 0, 0, 0, 0, 1, 1, 1, 1, 1, 1, 1, 1, 2, 2, 2, 2
  1423. 0xaaaa5500, // 0, 0, 0, 0, 1, 1, 1, 1, 2, 2, 2, 2, 2, 2, 2, 2
  1424. 0x90909090, // 0, 0, 1, 2, 0, 0, 1, 2, 0, 0, 1, 2, 0, 0, 1, 2
  1425. 0x94949494, // 0, 1, 1, 2, 0, 1, 1, 2, 0, 1, 1, 2, 0, 1, 1, 2
  1426. 0xa4a4a4a4, // 0, 1, 2, 2, 0, 1, 2, 2, 0, 1, 2, 2, 0, 1, 2, 2
  1427. 0xa9a59450, // 0, 0, 1, 1, 0, 1, 1, 2, 1, 1, 2, 2, 1, 2, 2, 2
  1428. 0x2a0a4250, // 0, 0, 1, 1, 2, 0, 0, 1, 2, 2, 0, 0, 2, 2, 2, 0
  1429. 0xa5945040, // 0, 0, 0, 1, 0, 0, 1, 1, 0, 1, 1, 2, 1, 1, 2, 2
  1430. 0x0a425054, // 0, 1, 1, 1, 0, 0, 1, 1, 2, 0, 0, 1, 2, 2, 0, 0
  1431. 0xa5a5a500, // 0, 0, 0, 0, 1, 1, 2, 2, 1, 1, 2, 2, 1, 1, 2, 2
  1432. 0x55a0a0a0, // 0, 0, 2, 2, 0, 0, 2, 2, 0, 0, 2, 2, 1, 1, 1, 1
  1433. 0xa8a85454, // 0, 1, 1, 1, 0, 1, 1, 1, 0, 2, 2, 2, 0, 2, 2, 2
  1434. 0x6a6a4040, // 0, 0, 0, 1, 0, 0, 0, 1, 2, 2, 2, 1, 2, 2, 2, 1
  1435. 0xa4a45000, // 0, 0, 0, 0, 0, 0, 1, 1, 0, 1, 2, 2, 0, 1, 2, 2
  1436. 0x1a1a0500, // 0, 0, 0, 0, 1, 1, 0, 0, 2, 2, 1, 0, 2, 2, 1, 0
  1437. 0x0050a4a4, // 0, 1, 2, 2, 0, 1, 2, 2, 0, 0, 1, 1, 0, 0, 0, 0
  1438. 0xaaa59090, // 0, 0, 1, 2, 0, 0, 1, 2, 1, 1, 2, 2, 2, 2, 2, 2
  1439. 0x14696914, // 0, 1, 1, 0, 1, 2, 2, 1, 1, 2, 2, 1, 0, 1, 1, 0
  1440. 0x69691400, // 0, 0, 0, 0, 0, 1, 1, 0, 1, 2, 2, 1, 1, 2, 2, 1
  1441. 0xa08585a0, // 0, 0, 2, 2, 1, 1, 0, 2, 1, 1, 0, 2, 0, 0, 2, 2
  1442. 0xaa821414, // 0, 1, 1, 0, 0, 1, 1, 0, 2, 0, 0, 2, 2, 2, 2, 2
  1443. 0x50a4a450, // 0, 0, 1, 1, 0, 1, 2, 2, 0, 1, 2, 2, 0, 0, 1, 1
  1444. 0x6a5a0200, // 0, 0, 0, 0, 2, 0, 0, 0, 2, 2, 1, 1, 2, 2, 2, 1
  1445. 0xa9a58000, // 0, 0, 0, 0, 0, 0, 0, 2, 1, 1, 2, 2, 1, 2, 2, 2
  1446. 0x5090a0a8, // 0, 2, 2, 2, 0, 0, 2, 2, 0, 0, 1, 2, 0, 0, 1, 1
  1447. 0xa8a09050, // 0, 0, 1, 1, 0, 0, 1, 2, 0, 0, 2, 2, 0, 2, 2, 2
  1448. 0x24242424, // 0, 1, 2, 0, 0, 1, 2, 0, 0, 1, 2, 0, 0, 1, 2, 0
  1449. 0x00aa5500, // 0, 0, 0, 0, 1, 1, 1, 1, 2, 2, 2, 2, 0, 0, 0, 0
  1450. 0x24924924, // 0, 1, 2, 0, 1, 2, 0, 1, 2, 0, 1, 2, 0, 1, 2, 0
  1451. 0x24499224, // 0, 1, 2, 0, 2, 0, 1, 2, 1, 2, 0, 1, 0, 1, 2, 0
  1452. 0x50a50a50, // 0, 0, 1, 1, 2, 2, 0, 0, 1, 1, 2, 2, 0, 0, 1, 1
  1453. 0x500aa550, // 0, 0, 1, 1, 1, 1, 2, 2, 2, 2, 0, 0, 0, 0, 1, 1
  1454. 0xaaaa4444, // 0, 1, 0, 1, 0, 1, 0, 1, 2, 2, 2, 2, 2, 2, 2, 2
  1455. 0x66660000, // 0, 0, 0, 0, 0, 0, 0, 0, 2, 1, 2, 1, 2, 1, 2, 1
  1456. 0xa5a0a5a0, // 0, 0, 2, 2, 1, 1, 2, 2, 0, 0, 2, 2, 1, 1, 2, 2
  1457. 0x50a050a0, // 0, 0, 2, 2, 0, 0, 1, 1, 0, 0, 2, 2, 0, 0, 1, 1
  1458. 0x69286928, // 0, 2, 2, 0, 1, 2, 2, 1, 0, 2, 2, 0, 1, 2, 2, 1
  1459. 0x44aaaa44, // 0, 1, 0, 1, 2, 2, 2, 2, 2, 2, 2, 2, 0, 1, 0, 1
  1460. 0x66666600, // 0, 0, 0, 0, 2, 1, 2, 1, 2, 1, 2, 1, 2, 1, 2, 1
  1461. 0xaa444444, // 0, 1, 0, 1, 0, 1, 0, 1, 0, 1, 0, 1, 2, 2, 2, 2
  1462. 0x54a854a8, // 0, 2, 2, 2, 0, 1, 1, 1, 0, 2, 2, 2, 0, 1, 1, 1
  1463. 0x95809580, // 0, 0, 0, 2, 1, 1, 1, 2, 0, 0, 0, 2, 1, 1, 1, 2
  1464. 0x96969600, // 0, 0, 0, 0, 2, 1, 1, 2, 2, 1, 1, 2, 2, 1, 1, 2
  1465. 0xa85454a8, // 0, 2, 2, 2, 0, 1, 1, 1, 0, 1, 1, 1, 0, 2, 2, 2
  1466. 0x80959580, // 0, 0, 0, 2, 1, 1, 1, 2, 1, 1, 1, 2, 0, 0, 0, 2
  1467. 0xaa141414, // 0, 1, 1, 0, 0, 1, 1, 0, 0, 1, 1, 0, 2, 2, 2, 2
  1468. 0x96960000, // 0, 0, 0, 0, 0, 0, 0, 0, 2, 1, 1, 2, 2, 1, 1, 2
  1469. 0xaaaa1414, // 0, 1, 1, 0, 0, 1, 1, 0, 2, 2, 2, 2, 2, 2, 2, 2
  1470. 0xa05050a0, // 0, 0, 2, 2, 0, 0, 1, 1, 0, 0, 1, 1, 0, 0, 2, 2
  1471. 0xa0a5a5a0, // 0, 0, 2, 2, 1, 1, 2, 2, 1, 1, 2, 2, 0, 0, 2, 2
  1472. 0x96000000, // 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 2, 1, 1, 2
  1473. 0x40804080, // 0, 0, 0, 2, 0, 0, 0, 1, 0, 0, 0, 2, 0, 0, 0, 1
  1474. 0xa9a8a9a8, // 0, 2, 2, 2, 1, 2, 2, 2, 0, 2, 2, 2, 1, 2, 2, 2
  1475. 0xaaaaaa44, // 0, 1, 0, 1, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2
  1476. 0x2a4a5254, // 0, 1, 1, 1, 2, 0, 1, 1, 2, 2, 0, 1, 2, 2, 2, 0
  1477. };
  1478. static const uint8_t s_bptcA2[] =
  1479. {
  1480. 15, 15, 15, 15, 15, 15, 15, 15,
  1481. 15, 15, 15, 15, 15, 15, 15, 15,
  1482. 15, 2, 8, 2, 2, 8, 8, 15,
  1483. 2, 8, 2, 2, 8, 8, 2, 2,
  1484. 15, 15, 6, 8, 2, 8, 15, 15,
  1485. 2, 8, 2, 2, 2, 15, 15, 6,
  1486. 6, 2, 6, 8, 15, 15, 2, 2,
  1487. 15, 15, 15, 15, 15, 2, 2, 15,
  1488. };
  1489. static const uint8_t s_bptcA3[2][64] =
  1490. {
  1491. {
  1492. 3, 3, 15, 15, 8, 3, 15, 15,
  1493. 8, 8, 6, 6, 6, 5, 3, 3,
  1494. 3, 3, 8, 15, 3, 3, 6, 10,
  1495. 5, 8, 8, 6, 8, 5, 15, 15,
  1496. 8, 15, 3, 5, 6, 10, 8, 15,
  1497. 15, 3, 15, 5, 15, 15, 15, 15,
  1498. 3, 15, 5, 5, 5, 8, 5, 10,
  1499. 5, 10, 8, 13, 15, 12, 3, 3,
  1500. },
  1501. {
  1502. 15, 8, 8, 3, 15, 15, 3, 8,
  1503. 15, 15, 15, 15, 15, 15, 15, 8,
  1504. 15, 8, 15, 3, 15, 8, 15, 8,
  1505. 3, 15, 6, 10, 15, 15, 10, 8,
  1506. 15, 3, 15, 10, 10, 8, 9, 10,
  1507. 6, 15, 8, 15, 3, 6, 6, 8,
  1508. 15, 3, 15, 15, 15, 15, 15, 15,
  1509. 15, 15, 15, 15, 3, 15, 15, 8,
  1510. },
  1511. };
  1512. static const uint8_t s_bptcFactors[3][16] =
  1513. {
  1514. { 0, 21, 43, 64, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 },
  1515. { 0, 9, 18, 27, 37, 46, 55, 64, 0, 0, 0, 0, 0, 0, 0, 0 },
  1516. { 0, 4, 9, 13, 17, 21, 26, 30, 34, 38, 43, 47, 51, 55, 60, 64 },
  1517. };
  1518. struct BitReader
  1519. {
  1520. BitReader(const uint8_t* _data, uint16_t _bitPos = 0)
  1521. : m_data(_data)
  1522. , m_bitPos(_bitPos)
  1523. {
  1524. }
  1525. uint16_t read(uint8_t _numBits)
  1526. {
  1527. const uint16_t pos = m_bitPos / 8;
  1528. const uint16_t shift = m_bitPos & 7;
  1529. uint32_t data = 0;
  1530. bx::memCopy(&data, &m_data[pos], bx::min(4, 16-pos) );
  1531. m_bitPos += _numBits;
  1532. return uint16_t( (data >> shift) & ( (1 << _numBits)-1) );
  1533. }
  1534. uint16_t peek(uint16_t _offset, uint8_t _numBits)
  1535. {
  1536. const uint16_t bitPos = m_bitPos + _offset;
  1537. const uint16_t shift = bitPos & 7;
  1538. uint16_t pos = bitPos / 8;
  1539. uint32_t data = 0;
  1540. bx::memCopy(&data, &m_data[pos], bx::min(4, 16-pos) );
  1541. return uint8_t( (data >> shift) & ( (1 << _numBits)-1) );
  1542. }
  1543. const uint8_t* m_data;
  1544. uint16_t m_bitPos;
  1545. };
  1546. static uint16_t bc6hUnquantize(uint16_t _value, bool _signed, uint8_t _endpointBits)
  1547. {
  1548. const uint16_t maxValue = 1<<(_endpointBits-1);
  1549. if (_signed)
  1550. {
  1551. if (_endpointBits >= 16)
  1552. {
  1553. return _value;
  1554. }
  1555. const bool sign = !!(_value & 0x8000);
  1556. _value &= 0x7fff;
  1557. uint16_t unq;
  1558. if (0 == _value)
  1559. {
  1560. unq = 0;
  1561. }
  1562. else if (_value >= maxValue-1)
  1563. {
  1564. unq = 0x7fff;
  1565. }
  1566. else
  1567. {
  1568. unq = ( (_value<<15) + 0x4000) >> (_endpointBits-1);
  1569. }
  1570. return sign ? -unq : unq;
  1571. }
  1572. if (_endpointBits >= 15)
  1573. {
  1574. return _value;
  1575. }
  1576. if (0 == _value)
  1577. {
  1578. return 0;
  1579. }
  1580. if (_value == maxValue)
  1581. {
  1582. return UINT16_MAX;
  1583. }
  1584. return ( (_value<<15) + 0x4000) >> (_endpointBits-1);
  1585. }
  1586. static uint16_t bc6hUnquantizeFinal(uint16_t _value, bool _signed)
  1587. {
  1588. if (_signed)
  1589. {
  1590. const uint16_t sign = _value & 0x8000;
  1591. _value &= 0x7fff;
  1592. return ( (_value * 31) >> 5) | sign;
  1593. }
  1594. return (_value * 31) >> 6;
  1595. }
  1596. static uint16_t signExtend(uint16_t _value, uint8_t _numBits)
  1597. {
  1598. const uint16_t mask = 1 << (_numBits - 1);
  1599. const uint16_t result = (_value ^ mask) - mask;
  1600. return result;
  1601. }
  1602. struct Bc6hModeInfo
  1603. {
  1604. uint8_t transformed;
  1605. uint8_t partitionBits;
  1606. uint8_t endpointBits;
  1607. uint8_t deltaBits[3];
  1608. };
  1609. static const Bc6hModeInfo s_bc6hModeInfo[] =
  1610. { // +--------------------------- transformed
  1611. // | +------------------------ partition bits
  1612. // | | +--------------------- endpoint bits
  1613. // | | | +-------------- delta bits
  1614. { 1, 5, 10, { 5, 5, 5 } }, // 00 2-bits
  1615. { 1, 5, 7, { 6, 6, 6 } }, // 01
  1616. { 1, 5, 11, { 5, 4, 4 } }, // 00010 5-bits
  1617. { 0, 0, 10, { 10, 10, 10 } }, // 00011
  1618. { 0, 0, 0, { 0, 0, 0 } }, // -
  1619. { 0, 0, 0, { 0, 0, 0 } }, // -
  1620. { 1, 5, 11, { 4, 5, 4 } }, // 00110
  1621. { 1, 0, 11, { 9, 9, 9 } }, // 00010
  1622. { 0, 0, 0, { 0, 0, 0 } }, // -
  1623. { 0, 0, 0, { 0, 0, 0 } }, // -
  1624. { 1, 5, 11, { 4, 4, 5 } }, // 00010
  1625. { 1, 0, 12, { 8, 8, 8 } }, // 00010
  1626. { 0, 0, 0, { 0, 0, 0 } }, // -
  1627. { 0, 0, 0, { 0, 0, 0 } }, // -
  1628. { 1, 5, 9, { 5, 5, 5 } }, // 00010
  1629. { 1, 0, 16, { 4, 4, 4 } }, // 00010
  1630. { 0, 0, 0, { 0, 0, 0 } }, // -
  1631. { 0, 0, 0, { 0, 0, 0 } }, // -
  1632. { 1, 5, 8, { 6, 5, 5 } }, // 00010
  1633. { 0, 0, 0, { 0, 0, 0 } }, // -
  1634. { 0, 0, 0, { 0, 0, 0 } }, // -
  1635. { 0, 0, 0, { 0, 0, 0 } }, // -
  1636. { 1, 5, 8, { 5, 6, 5 } }, // 00010
  1637. { 0, 0, 0, { 0, 0, 0 } }, // -
  1638. { 0, 0, 0, { 0, 0, 0 } }, // -
  1639. { 0, 0, 0, { 0, 0, 0 } }, // -
  1640. { 1, 5, 8, { 5, 5, 6 } }, // 00010
  1641. { 0, 0, 0, { 0, 0, 0 } }, // -
  1642. { 0, 0, 0, { 0, 0, 0 } }, // -
  1643. { 0, 0, 0, { 0, 0, 0 } }, // -
  1644. { 0, 5, 6, { 6, 6, 6 } }, // 00010
  1645. { 0, 0, 0, { 0, 0, 0 } }, // -
  1646. };
  1647. static void decodeBlockBc6h(uint16_t _dst[16*3], const uint8_t _src[16], bool _signed)
  1648. {
  1649. if (!BX_ENABLED(BIMG_DECODE_BC6) )
  1650. {
  1651. return;
  1652. }
  1653. uint8_t src[16];
  1654. bx::memCopy(src, _src, 16);
  1655. BitReader bit(src);
  1656. uint8_t mode = uint8_t(bit.read(2));
  1657. uint16_t epR[4] = { /* rw, rx, ry, rz */ };
  1658. uint16_t epG[4] = { /* gw, gx, gy, gz */ };
  1659. uint16_t epB[4] = { /* bw, bx, by, bz */ };
  1660. if (mode & 2)
  1661. {
  1662. // 5-bit mode
  1663. mode |= bit.read(3) << 2;
  1664. if (0 == s_bc6hModeInfo[mode].endpointBits)
  1665. {
  1666. bx::memSet(_dst, 0, 16*3*2);
  1667. return;
  1668. }
  1669. switch (mode)
  1670. {
  1671. case 2:
  1672. epR[0] |= bit.read(10) << 0;
  1673. epG[0] |= bit.read(10) << 0;
  1674. epB[0] |= bit.read(10) << 0;
  1675. epR[1] |= bit.read( 5) << 0;
  1676. epR[0] |= bit.read( 1) << 10;
  1677. epG[2] |= bit.read( 4) << 0;
  1678. epG[1] |= bit.read( 4) << 0;
  1679. epG[0] |= bit.read( 1) << 10;
  1680. epB[3] |= bit.read( 1) << 0;
  1681. epG[3] |= bit.read( 4) << 0;
  1682. epB[1] |= bit.read( 4) << 0;
  1683. epB[0] |= bit.read( 1) << 10;
  1684. epB[3] |= bit.read( 1) << 1;
  1685. epB[2] |= bit.read( 4) << 0;
  1686. epR[2] |= bit.read( 5) << 0;
  1687. epB[3] |= bit.read( 1) << 2;
  1688. epR[3] |= bit.read( 5) << 0;
  1689. epB[3] |= bit.read( 1) << 3;
  1690. break;
  1691. case 3:
  1692. epR[0] |= bit.read(10) << 0;
  1693. epG[0] |= bit.read(10) << 0;
  1694. epB[0] |= bit.read(10) << 0;
  1695. epR[1] |= bit.read(10) << 0;
  1696. epG[1] |= bit.read(10) << 0;
  1697. epB[1] |= bit.read(10) << 0;
  1698. break;
  1699. case 6:
  1700. epR[0] |= bit.read(10) << 0;
  1701. epG[0] |= bit.read(10) << 0;
  1702. epB[0] |= bit.read(10) << 0;
  1703. epR[1] |= bit.read( 4) << 0;
  1704. epR[0] |= bit.read( 1) << 10;
  1705. epG[3] |= bit.read( 1) << 4;
  1706. epG[2] |= bit.read( 4) << 0;
  1707. epG[1] |= bit.read( 5) << 0;
  1708. epG[0] |= bit.read( 1) << 10;
  1709. epG[3] |= bit.read( 4) << 0;
  1710. epB[1] |= bit.read( 4) << 0;
  1711. epB[0] |= bit.read( 1) << 10;
  1712. epB[3] |= bit.read( 1) << 1;
  1713. epB[2] |= bit.read( 4) << 0;
  1714. epR[2] |= bit.read( 4) << 0;
  1715. epB[3] |= bit.read( 1) << 0;
  1716. epB[3] |= bit.read( 1) << 2;
  1717. epR[3] |= bit.read( 4) << 0;
  1718. epG[2] |= bit.read( 1) << 4;
  1719. epB[3] |= bit.read( 1) << 3;
  1720. break;
  1721. case 7:
  1722. epR[0] |= bit.read(10) << 0;
  1723. epG[0] |= bit.read(10) << 0;
  1724. epB[0] |= bit.read(10) << 0;
  1725. epR[1] |= bit.read( 9) << 0;
  1726. epR[0] |= bit.read( 1) << 10;
  1727. epG[1] |= bit.read( 9) << 0;
  1728. epG[0] |= bit.read( 1) << 10;
  1729. epB[1] |= bit.read( 9) << 0;
  1730. epB[0] |= bit.read( 1) << 10;
  1731. break;
  1732. case 10:
  1733. epR[0] |= bit.read(10) << 0;
  1734. epG[0] |= bit.read(10) << 0;
  1735. epB[0] |= bit.read(10) << 0;
  1736. epR[1] |= bit.read( 4) << 0;
  1737. epR[0] |= bit.read( 1) << 10;
  1738. epB[2] |= bit.read( 1) << 4;
  1739. epG[2] |= bit.read( 4) << 0;
  1740. epG[1] |= bit.read( 4) << 0;
  1741. epG[0] |= bit.read( 1) << 10;
  1742. epB[3] |= bit.read( 1) << 0;
  1743. epG[3] |= bit.read( 4) << 0;
  1744. epB[1] |= bit.read( 5) << 0;
  1745. epB[0] |= bit.read( 1) << 10;
  1746. epB[2] |= bit.read( 4) << 0;
  1747. epR[2] |= bit.read( 4) << 0;
  1748. epB[3] |= bit.read( 1) << 1;
  1749. epB[3] |= bit.read( 1) << 2;
  1750. epR[3] |= bit.read( 4) << 0;
  1751. epB[3] |= bit.read( 1) << 4;
  1752. epB[3] |= bit.read( 1) << 3;
  1753. break;
  1754. case 11:
  1755. epR[0] |= bit.read(10) << 0;
  1756. epG[0] |= bit.read(10) << 0;
  1757. epB[0] |= bit.read(10) << 0;
  1758. epR[1] |= bit.read( 8) << 0;
  1759. epR[0] |= bit.read( 1) << 11;
  1760. epR[0] |= bit.read( 1) << 10;
  1761. epG[1] |= bit.read( 8) << 0;
  1762. epG[0] |= bit.read( 1) << 11;
  1763. epG[0] |= bit.read( 1) << 10;
  1764. epB[1] |= bit.read( 8) << 0;
  1765. epB[0] |= bit.read( 1) << 11;
  1766. epB[0] |= bit.read( 1) << 10;
  1767. break;
  1768. case 14:
  1769. epR[0] |= bit.read( 9) << 0;
  1770. epB[2] |= bit.read( 1) << 4;
  1771. epG[0] |= bit.read( 9) << 0;
  1772. epG[2] |= bit.read( 1) << 4;
  1773. epB[0] |= bit.read( 9) << 0;
  1774. epB[3] |= bit.read( 1) << 4;
  1775. epR[1] |= bit.read( 5) << 0;
  1776. epG[3] |= bit.read( 1) << 4;
  1777. epG[2] |= bit.read( 4) << 0;
  1778. epG[1] |= bit.read( 5) << 0;
  1779. epB[3] |= bit.read( 1) << 0;
  1780. epG[3] |= bit.read( 4) << 0;
  1781. epB[1] |= bit.read( 5) << 0;
  1782. epB[3] |= bit.read( 1) << 1;
  1783. epB[2] |= bit.read( 4) << 0;
  1784. epR[2] |= bit.read( 5) << 0;
  1785. epB[3] |= bit.read( 1) << 2;
  1786. epR[3] |= bit.read( 5) << 0;
  1787. epB[3] |= bit.read( 1) << 3;
  1788. break;
  1789. case 15:
  1790. epR[0] |= bit.read(10) << 0;
  1791. epG[0] |= bit.read(10) << 0;
  1792. epB[0] |= bit.read(10) << 0;
  1793. epR[1] |= bit.read( 4) << 0;
  1794. epR[0] |= bit.read( 1) << 15;
  1795. epR[0] |= bit.read( 1) << 14;
  1796. epR[0] |= bit.read( 1) << 13;
  1797. epR[0] |= bit.read( 1) << 12;
  1798. epR[0] |= bit.read( 1) << 11;
  1799. epR[0] |= bit.read( 1) << 10;
  1800. epG[1] |= bit.read( 4) << 0;
  1801. epG[0] |= bit.read( 1) << 15;
  1802. epG[0] |= bit.read( 1) << 14;
  1803. epG[0] |= bit.read( 1) << 13;
  1804. epG[0] |= bit.read( 1) << 12;
  1805. epG[0] |= bit.read( 1) << 11;
  1806. epG[0] |= bit.read( 1) << 10;
  1807. epB[1] |= bit.read( 4) << 0;
  1808. epB[0] |= bit.read( 1) << 15;
  1809. epB[0] |= bit.read( 1) << 14;
  1810. epB[0] |= bit.read( 1) << 13;
  1811. epB[0] |= bit.read( 1) << 12;
  1812. epB[0] |= bit.read( 1) << 11;
  1813. epB[0] |= bit.read( 1) << 10;
  1814. break;
  1815. case 18:
  1816. epR[0] |= bit.read( 8) << 0;
  1817. epG[3] |= bit.read( 1) << 4;
  1818. epB[2] |= bit.read( 1) << 4;
  1819. epG[0] |= bit.read( 8) << 0;
  1820. epB[3] |= bit.read( 1) << 2;
  1821. epG[2] |= bit.read( 1) << 4;
  1822. epB[0] |= bit.read( 8) << 0;
  1823. epB[3] |= bit.read( 1) << 3;
  1824. epB[3] |= bit.read( 1) << 4;
  1825. epR[1] |= bit.read( 6) << 0;
  1826. epG[2] |= bit.read( 4) << 0;
  1827. epG[1] |= bit.read( 5) << 0;
  1828. epB[3] |= bit.read( 1) << 0;
  1829. epG[3] |= bit.read( 4) << 0;
  1830. epB[1] |= bit.read( 5) << 0;
  1831. epB[3] |= bit.read( 1) << 1;
  1832. epB[2] |= bit.read( 4) << 0;
  1833. epR[2] |= bit.read( 6) << 0;
  1834. epR[3] |= bit.read( 6) << 0;
  1835. break;
  1836. case 22:
  1837. epR[0] |= bit.read( 8) << 0;
  1838. epB[3] |= bit.read( 1) << 0;
  1839. epB[2] |= bit.read( 1) << 4;
  1840. epG[0] |= bit.read( 8) << 0;
  1841. epG[2] |= bit.read( 1) << 5;
  1842. epG[2] |= bit.read( 1) << 4;
  1843. epB[0] |= bit.read( 8) << 0;
  1844. epG[3] |= bit.read( 1) << 5;
  1845. epB[3] |= bit.read( 1) << 4;
  1846. epR[1] |= bit.read( 5) << 0;
  1847. epG[3] |= bit.read( 1) << 4;
  1848. epG[2] |= bit.read( 4) << 0;
  1849. epG[1] |= bit.read( 6) << 0;
  1850. epG[3] |= bit.read( 4) << 0;
  1851. epB[1] |= bit.read( 5) << 0;
  1852. epB[3] |= bit.read( 1) << 1;
  1853. epB[2] |= bit.read( 4) << 0;
  1854. epR[2] |= bit.read( 5) << 0;
  1855. epB[3] |= bit.read( 1) << 2;
  1856. epR[3] |= bit.read( 5) << 0;
  1857. epB[3] |= bit.read( 1) << 3;
  1858. break;
  1859. case 26:
  1860. epR[0] |= bit.read( 8) << 0;
  1861. epB[3] |= bit.read( 1) << 1;
  1862. epB[2] |= bit.read( 1) << 4;
  1863. epG[0] |= bit.read( 8) << 0;
  1864. epB[2] |= bit.read( 1) << 5;
  1865. epG[2] |= bit.read( 1) << 4;
  1866. epB[0] |= bit.read( 8) << 0;
  1867. epB[3] |= bit.read( 1) << 5;
  1868. epB[3] |= bit.read( 1) << 4;
  1869. epR[1] |= bit.read( 5) << 0;
  1870. epG[3] |= bit.read( 1) << 4;
  1871. epG[2] |= bit.read( 4) << 0;
  1872. epG[1] |= bit.read( 5) << 0;
  1873. epB[3] |= bit.read( 1) << 0;
  1874. epG[3] |= bit.read( 4) << 0;
  1875. epB[1] |= bit.read( 6) << 0;
  1876. epB[2] |= bit.read( 4) << 0;
  1877. epR[2] |= bit.read( 5) << 0;
  1878. epB[3] |= bit.read( 1) << 2;
  1879. epR[3] |= bit.read( 5) << 0;
  1880. epB[3] |= bit.read( 1) << 3;
  1881. break;
  1882. case 30:
  1883. epR[0] |= bit.read( 6) << 0;
  1884. epG[3] |= bit.read( 1) << 4;
  1885. epB[3] |= bit.read( 1) << 0;
  1886. epB[3] |= bit.read( 1) << 1;
  1887. epB[2] |= bit.read( 1) << 4;
  1888. epG[0] |= bit.read( 6) << 0;
  1889. epG[2] |= bit.read( 1) << 5;
  1890. epB[2] |= bit.read( 1) << 5;
  1891. epB[3] |= bit.read( 1) << 2;
  1892. epG[2] |= bit.read( 1) << 4;
  1893. epB[0] |= bit.read( 6) << 0;
  1894. epG[3] |= bit.read( 1) << 5;
  1895. epB[3] |= bit.read( 1) << 3;
  1896. epB[3] |= bit.read( 1) << 5;
  1897. epB[3] |= bit.read( 1) << 4;
  1898. epR[1] |= bit.read( 6) << 0;
  1899. epG[2] |= bit.read( 4) << 0;
  1900. epG[1] |= bit.read( 6) << 0;
  1901. epG[3] |= bit.read( 4) << 0;
  1902. epB[1] |= bit.read( 6) << 0;
  1903. epB[2] |= bit.read( 4) << 0;
  1904. epR[2] |= bit.read( 6) << 0;
  1905. epR[3] |= bit.read( 6) << 0;
  1906. break;
  1907. default:
  1908. break;
  1909. }
  1910. }
  1911. else
  1912. {
  1913. switch (mode)
  1914. {
  1915. case 0:
  1916. epG[2] |= bit.read( 1) << 4;
  1917. epB[2] |= bit.read( 1) << 4;
  1918. epB[3] |= bit.read( 1) << 4;
  1919. epR[0] |= bit.read(10) << 0;
  1920. epG[0] |= bit.read(10) << 0;
  1921. epB[0] |= bit.read(10) << 0;
  1922. epR[1] |= bit.read( 5) << 0;
  1923. epG[3] |= bit.read( 1) << 4;
  1924. epG[2] |= bit.read( 4) << 0;
  1925. epG[1] |= bit.read( 5) << 0;
  1926. epB[3] |= bit.read( 1) << 0;
  1927. epG[3] |= bit.read( 4) << 0;
  1928. epB[1] |= bit.read( 5) << 0;
  1929. epB[3] |= bit.read( 1) << 1;
  1930. epB[2] |= bit.read( 4) << 0;
  1931. epR[2] |= bit.read( 5) << 0;
  1932. epB[3] |= bit.read( 1) << 2;
  1933. epR[3] |= bit.read( 5) << 0;
  1934. epB[3] |= bit.read( 1) << 3;
  1935. break;
  1936. case 1:
  1937. epG[2] |= bit.read( 1) << 5;
  1938. epG[3] |= bit.read( 1) << 4;
  1939. epG[3] |= bit.read( 1) << 5;
  1940. epR[0] |= bit.read( 7) << 0;
  1941. epB[3] |= bit.read( 1) << 0;
  1942. epB[3] |= bit.read( 1) << 1;
  1943. epB[2] |= bit.read( 1) << 4;
  1944. epG[0] |= bit.read( 7) << 0;
  1945. epB[2] |= bit.read( 1) << 5;
  1946. epB[3] |= bit.read( 1) << 2;
  1947. epG[2] |= bit.read( 1) << 4;
  1948. epB[0] |= bit.read( 7) << 0;
  1949. epB[3] |= bit.read( 1) << 3;
  1950. epB[3] |= bit.read( 1) << 5;
  1951. epB[3] |= bit.read( 1) << 4;
  1952. epR[1] |= bit.read( 6) << 0;
  1953. epG[2] |= bit.read( 4) << 0;
  1954. epG[1] |= bit.read( 6) << 0;
  1955. epG[3] |= bit.read( 4) << 0;
  1956. epB[1] |= bit.read( 6) << 0;
  1957. epB[2] |= bit.read( 4) << 0;
  1958. epR[2] |= bit.read( 6) << 0;
  1959. epR[3] |= bit.read( 6) << 0;
  1960. break;
  1961. default:
  1962. break;
  1963. }
  1964. }
  1965. const Bc6hModeInfo mi = s_bc6hModeInfo[mode];
  1966. if (_signed)
  1967. {
  1968. epR[0] = signExtend(epR[0], mi.endpointBits);
  1969. epG[0] = signExtend(epG[0], mi.endpointBits);
  1970. epB[0] = signExtend(epB[0], mi.endpointBits);
  1971. }
  1972. const uint8_t numSubsets = !!mi.partitionBits + 1;
  1973. for (uint8_t ii = 1, num = numSubsets*2; ii < num; ++ii)
  1974. {
  1975. if (_signed
  1976. || mi.transformed)
  1977. {
  1978. epR[ii] = signExtend(epR[ii], mi.deltaBits[0]);
  1979. epG[ii] = signExtend(epG[ii], mi.deltaBits[1]);
  1980. epB[ii] = signExtend(epB[ii], mi.deltaBits[2]);
  1981. }
  1982. if (mi.transformed)
  1983. {
  1984. const uint16_t mask = (1<<mi.endpointBits) - 1;
  1985. epR[ii] = (epR[ii] + epR[0]) & mask;
  1986. epG[ii] = (epG[ii] + epG[0]) & mask;
  1987. epB[ii] = (epB[ii] + epB[0]) & mask;
  1988. if (_signed)
  1989. {
  1990. epR[ii] = signExtend(epR[ii], mi.endpointBits);
  1991. epG[ii] = signExtend(epG[ii], mi.endpointBits);
  1992. epB[ii] = signExtend(epB[ii], mi.endpointBits);
  1993. }
  1994. }
  1995. }
  1996. for (uint8_t ii = 0, num = numSubsets*2; ii < num; ++ii)
  1997. {
  1998. epR[ii] = bc6hUnquantize(epR[ii], _signed, mi.endpointBits);
  1999. epG[ii] = bc6hUnquantize(epG[ii], _signed, mi.endpointBits);
  2000. epB[ii] = bc6hUnquantize(epB[ii], _signed, mi.endpointBits);
  2001. }
  2002. const uint8_t partitionSetIdx = uint8_t(mi.partitionBits ? bit.read(5) : 0);
  2003. const uint8_t indexBits = mi.partitionBits ? 3 : 4;
  2004. const uint8_t* factors = s_bptcFactors[indexBits-2];
  2005. for (uint8_t yy = 0; yy < 4; ++yy)
  2006. {
  2007. for (uint8_t xx = 0; xx < 4; ++xx)
  2008. {
  2009. const uint8_t idx = yy*4+xx;
  2010. uint8_t subsetIndex = 0;
  2011. uint8_t indexAnchor = 0;
  2012. if (0 != mi.partitionBits)
  2013. {
  2014. subsetIndex = (s_bptcP2[partitionSetIdx] >> idx) & 1;
  2015. indexAnchor = subsetIndex ? s_bptcA2[partitionSetIdx] : 0;
  2016. }
  2017. const uint8_t anchor = idx == indexAnchor;
  2018. const uint8_t num = indexBits - anchor;
  2019. const uint8_t index = (uint8_t)bit.read(num);
  2020. const uint8_t fc = factors[index];
  2021. const uint8_t fca = 64 - fc;
  2022. const uint8_t fcb = fc;
  2023. subsetIndex *= 2;
  2024. uint16_t rr = bc6hUnquantizeFinal( (epR[subsetIndex]*fca + epR[subsetIndex + 1]*fcb + 32) >> 6, _signed);
  2025. uint16_t gg = bc6hUnquantizeFinal( (epG[subsetIndex]*fca + epG[subsetIndex + 1]*fcb + 32) >> 6, _signed);
  2026. uint16_t bb = bc6hUnquantizeFinal( (epB[subsetIndex]*fca + epB[subsetIndex + 1]*fcb + 32) >> 6, _signed);
  2027. uint16_t* rgba = &_dst[idx*3];
  2028. rgba[0] = rr;
  2029. rgba[1] = gg;
  2030. rgba[2] = bb;
  2031. }
  2032. }
  2033. }
  2034. static void decodeBlockBc6h(float _dst[16*4], const uint8_t _src[16])
  2035. {
  2036. if (!BX_ENABLED(BIMG_DECODE_BC6) )
  2037. {
  2038. return;
  2039. }
  2040. uint16_t tmp[16*3];
  2041. decodeBlockBc6h(tmp, _src, true);
  2042. for (uint32_t ii = 0; ii < 16; ++ii)
  2043. {
  2044. _dst[ii*4+0] = bx::halfToFloat(tmp[ii*3+0]);
  2045. _dst[ii*4+1] = bx::halfToFloat(tmp[ii*3+1]);
  2046. _dst[ii*4+2] = bx::halfToFloat(tmp[ii*3+2]);
  2047. _dst[ii*4+3] = 1.0f;
  2048. }
  2049. }
  2050. struct Bc7ModeInfo
  2051. {
  2052. uint8_t numSubsets;
  2053. uint8_t partitionBits;
  2054. uint8_t rotationBits;
  2055. uint8_t indexSelectionBits;
  2056. uint8_t colorBits;
  2057. uint8_t alphaBits;
  2058. uint8_t endpointPBits;
  2059. uint8_t sharedPBits;
  2060. uint8_t indexBits[2];
  2061. };
  2062. static const Bc7ModeInfo s_bp7ModeInfo[] =
  2063. { // +---------------------------- num subsets
  2064. // | +------------------------- partition bits
  2065. // | | +---------------------- rotation bits
  2066. // | | | +------------------- index selection bits
  2067. // | | | | +---------------- color bits
  2068. // | | | | | +------------- alpha bits
  2069. // | | | | | | +---------- endpoint P-bits
  2070. // | | | | | | | +------- shared P-bits
  2071. // | | | | | | | | +-- 2x index bits
  2072. { 3, 4, 0, 0, 4, 0, 1, 0, { 3, 0 } }, // 0
  2073. { 2, 6, 0, 0, 6, 0, 0, 1, { 3, 0 } }, // 1
  2074. { 3, 6, 0, 0, 5, 0, 0, 0, { 2, 0 } }, // 2
  2075. { 2, 6, 0, 0, 7, 0, 1, 0, { 2, 0 } }, // 3
  2076. { 1, 0, 2, 1, 5, 6, 0, 0, { 2, 3 } }, // 4
  2077. { 1, 0, 2, 0, 7, 8, 0, 0, { 2, 2 } }, // 5
  2078. { 1, 0, 0, 0, 7, 7, 1, 0, { 4, 0 } }, // 6
  2079. { 2, 6, 0, 0, 5, 5, 1, 0, { 2, 0 } }, // 7
  2080. };
  2081. static void decodeBlockBc7(uint8_t _dst[16*4], const uint8_t _src[16])
  2082. {
  2083. if (!BX_ENABLED(BIMG_DECODE_BC7) )
  2084. {
  2085. return;
  2086. }
  2087. BitReader bit(_src);
  2088. uint8_t mode = 0;
  2089. for (; mode < 8 && 0 == bit.read(1); ++mode)
  2090. {
  2091. }
  2092. if (mode == 8)
  2093. {
  2094. bx::memSet(_dst, 0, 16*4);
  2095. return;
  2096. }
  2097. const Bc7ModeInfo& mi = s_bp7ModeInfo[mode];
  2098. const uint8_t modePBits = 0 != mi.endpointPBits
  2099. ? mi.endpointPBits
  2100. : mi.sharedPBits
  2101. ;
  2102. const uint8_t partitionSetIdx = uint8_t(bit.read(mi.partitionBits) );
  2103. const uint8_t rotationMode = uint8_t(bit.read(mi.rotationBits) );
  2104. const uint8_t indexSelectionMode = uint8_t(bit.read(mi.indexSelectionBits) );
  2105. uint8_t epR[6];
  2106. uint8_t epG[6];
  2107. uint8_t epB[6];
  2108. uint8_t epA[6];
  2109. for (uint8_t ii = 0; ii < mi.numSubsets; ++ii)
  2110. {
  2111. epR[ii*2+0] = uint8_t(bit.read(mi.colorBits) << modePBits);
  2112. epR[ii*2+1] = uint8_t(bit.read(mi.colorBits) << modePBits);
  2113. }
  2114. for (uint8_t ii = 0; ii < mi.numSubsets; ++ii)
  2115. {
  2116. epG[ii*2+0] = uint8_t(bit.read(mi.colorBits) << modePBits);
  2117. epG[ii*2+1] = uint8_t(bit.read(mi.colorBits) << modePBits);
  2118. }
  2119. for (uint8_t ii = 0; ii < mi.numSubsets; ++ii)
  2120. {
  2121. epB[ii*2+0] = uint8_t(bit.read(mi.colorBits) << modePBits);
  2122. epB[ii*2+1] = uint8_t(bit.read(mi.colorBits) << modePBits);
  2123. }
  2124. if (mi.alphaBits)
  2125. {
  2126. for (uint8_t ii = 0; ii < mi.numSubsets; ++ii)
  2127. {
  2128. epA[ii*2+0] = uint8_t(bit.read(mi.alphaBits) << modePBits);
  2129. epA[ii*2+1] = uint8_t(bit.read(mi.alphaBits) << modePBits);
  2130. }
  2131. }
  2132. else
  2133. {
  2134. bx::memSet(epA, 0xff, 6);
  2135. }
  2136. if (0 != modePBits)
  2137. {
  2138. for (uint8_t ii = 0; ii < mi.numSubsets; ++ii)
  2139. {
  2140. const uint8_t pda = uint8_t( bit.read(modePBits) );
  2141. const uint8_t pdb = uint8_t(0 == mi.sharedPBits ? bit.read(modePBits) : pda);
  2142. epR[ii*2+0] |= pda;
  2143. epR[ii*2+1] |= pdb;
  2144. epG[ii*2+0] |= pda;
  2145. epG[ii*2+1] |= pdb;
  2146. epB[ii*2+0] |= pda;
  2147. epB[ii*2+1] |= pdb;
  2148. epA[ii*2+0] |= pda;
  2149. epA[ii*2+1] |= pdb;
  2150. }
  2151. }
  2152. const uint8_t colorBits = mi.colorBits + modePBits;
  2153. for (uint8_t ii = 0; ii < mi.numSubsets; ++ii)
  2154. {
  2155. epR[ii*2+0] = bitRangeConvert(epR[ii*2+0], colorBits, 8);
  2156. epR[ii*2+1] = bitRangeConvert(epR[ii*2+1], colorBits, 8);
  2157. epG[ii*2+0] = bitRangeConvert(epG[ii*2+0], colorBits, 8);
  2158. epG[ii*2+1] = bitRangeConvert(epG[ii*2+1], colorBits, 8);
  2159. epB[ii*2+0] = bitRangeConvert(epB[ii*2+0], colorBits, 8);
  2160. epB[ii*2+1] = bitRangeConvert(epB[ii*2+1], colorBits, 8);
  2161. }
  2162. if (mi.alphaBits)
  2163. {
  2164. const uint8_t alphaBits = mi.alphaBits + modePBits;
  2165. for (uint8_t ii = 0; ii < mi.numSubsets; ++ii)
  2166. {
  2167. epA[ii*2+0] = bitRangeConvert(epA[ii*2+0], alphaBits, 8);
  2168. epA[ii*2+1] = bitRangeConvert(epA[ii*2+1], alphaBits, 8);
  2169. }
  2170. }
  2171. const bool hasIndexBits1 = 0 != mi.indexBits[1];
  2172. const uint8_t* factors[] =
  2173. {
  2174. s_bptcFactors[mi.indexBits[0]-2],
  2175. hasIndexBits1 ? s_bptcFactors[mi.indexBits[1]-2] : factors[0],
  2176. };
  2177. uint16_t offset[2] =
  2178. {
  2179. 0,
  2180. uint16_t(mi.numSubsets*(16*mi.indexBits[0]-1) ),
  2181. };
  2182. for (uint8_t yy = 0; yy < 4; ++yy)
  2183. {
  2184. for (uint8_t xx = 0; xx < 4; ++xx)
  2185. {
  2186. const uint8_t idx = yy*4+xx;
  2187. uint8_t subsetIndex = 0;
  2188. uint8_t indexAnchor = 0;
  2189. switch (mi.numSubsets)
  2190. {
  2191. case 2:
  2192. subsetIndex = (s_bptcP2[partitionSetIdx] >> idx) & 1;
  2193. indexAnchor = 0 != subsetIndex ? s_bptcA2[partitionSetIdx] : 0;
  2194. break;
  2195. case 3:
  2196. subsetIndex = (s_bptcP3[partitionSetIdx] >> (2*idx) ) & 3;
  2197. indexAnchor = 0 != subsetIndex ? s_bptcA3[subsetIndex-1][partitionSetIdx] : 0;
  2198. break;
  2199. default:
  2200. break;
  2201. }
  2202. const uint8_t anchor = idx == indexAnchor;
  2203. const uint8_t num[2] =
  2204. {
  2205. uint8_t( mi.indexBits[0] - anchor ),
  2206. uint8_t(hasIndexBits1 ? mi.indexBits[1] - anchor : 0),
  2207. };
  2208. const uint8_t index[2] =
  2209. {
  2210. (uint8_t)bit.peek(offset[0], num[0]),
  2211. hasIndexBits1 ? (uint8_t)bit.peek(offset[1], num[1]) : index[0],
  2212. };
  2213. offset[0] += num[0];
  2214. offset[1] += num[1];
  2215. const uint8_t fc = factors[ indexSelectionMode][index[ indexSelectionMode] ];
  2216. const uint8_t fa = factors[!indexSelectionMode][index[!indexSelectionMode] ];
  2217. const uint8_t fca = 64 - fc;
  2218. const uint8_t fcb = fc;
  2219. const uint8_t faa = 64 - fa;
  2220. const uint8_t fab = fa;
  2221. subsetIndex *= 2;
  2222. uint8_t rr = uint8_t(uint16_t(epR[subsetIndex]*fca + epR[subsetIndex + 1]*fcb + 32) >> 6);
  2223. uint8_t gg = uint8_t(uint16_t(epG[subsetIndex]*fca + epG[subsetIndex + 1]*fcb + 32) >> 6);
  2224. uint8_t bb = uint8_t(uint16_t(epB[subsetIndex]*fca + epB[subsetIndex + 1]*fcb + 32) >> 6);
  2225. uint8_t aa = uint8_t(uint16_t(epA[subsetIndex]*faa + epA[subsetIndex + 1]*fab + 32) >> 6);
  2226. switch (rotationMode)
  2227. {
  2228. case 1: bx::swap(aa, rr); break;
  2229. case 2: bx::swap(aa, gg); break;
  2230. case 3: bx::swap(aa, bb); break;
  2231. default: break;
  2232. };
  2233. uint8_t* bgra = &_dst[idx*4];
  2234. bgra[0] = bb;
  2235. bgra[1] = gg;
  2236. bgra[2] = rr;
  2237. bgra[3] = aa;
  2238. }
  2239. }
  2240. }
  2241. // ATC
  2242. //
  2243. static void decodeBlockATC(uint8_t _dst[16*4], const uint8_t _src[8])
  2244. {
  2245. if (!BX_ENABLED(BIMG_DECODE_ATC) )
  2246. {
  2247. return;
  2248. }
  2249. uint8_t colors[4*4];
  2250. uint32_t c0 = _src[0] | (_src[1] << 8);
  2251. uint32_t c1 = _src[2] | (_src[3] << 8);
  2252. if (0 == (c0 & 0x8000) )
  2253. {
  2254. colors[ 0] = bitRangeConvert( (c0>> 0)&0x1f, 5, 8);
  2255. colors[ 1] = bitRangeConvert( (c0>> 5)&0x1f, 5, 8);
  2256. colors[ 2] = bitRangeConvert( (c0>>10)&0x1f, 5, 8);
  2257. colors[12] = bitRangeConvert( (c1>> 0)&0x1f, 5, 8);
  2258. colors[13] = bitRangeConvert( (c1>> 5)&0x3f, 6, 8);
  2259. colors[14] = bitRangeConvert( (c1>>11)&0x1f, 5, 8);
  2260. colors[ 4] = (2 * colors[0] + colors[12]) / 3;
  2261. colors[ 5] = (2 * colors[1] + colors[13]) / 3;
  2262. colors[ 6] = (2 * colors[2] + colors[14]) / 3;
  2263. colors[ 8] = (colors[0] + 2 * colors[12]) / 3;
  2264. colors[ 9] = (colors[1] + 2 * colors[13]) / 3;
  2265. colors[10] = (colors[2] + 2 * colors[14]) / 3;
  2266. }
  2267. else
  2268. {
  2269. colors[ 0] = 0;
  2270. colors[ 1] = 0;
  2271. colors[ 2] = 0;
  2272. colors[ 8] = bitRangeConvert( (c0>> 0)&0x1f, 5, 8);
  2273. colors[ 9] = bitRangeConvert( (c0>> 5)&0x1f, 5, 8);
  2274. colors[10] = bitRangeConvert( (c0>>10)&0x1f, 5, 8);
  2275. colors[12] = bitRangeConvert( (c1>> 0)&0x1f, 5, 8);
  2276. colors[13] = bitRangeConvert( (c1>> 5)&0x3f, 6, 8);
  2277. colors[14] = bitRangeConvert( (c1>>11)&0x1f, 5, 8);
  2278. colors[ 4] = colors[ 8] - colors[12] / 4;
  2279. colors[ 5] = colors[ 9] - colors[13] / 4;
  2280. colors[ 6] = colors[10] - colors[14] / 4;
  2281. }
  2282. for (uint32_t ii = 0, next = 8*4; ii < 16*4; ii += 4, next += 2)
  2283. {
  2284. int32_t idx = ( (_src[next>>3] >> (next & 7) ) & 3) * 4;
  2285. _dst[ii+0] = colors[idx+0];
  2286. _dst[ii+1] = colors[idx+1];
  2287. _dst[ii+2] = colors[idx+2];
  2288. _dst[ii+3] = colors[idx+3];
  2289. }
  2290. }
  2291. static const int32_t s_etc1Mod[8][4] =
  2292. {
  2293. { 2, 8, -2, -8 },
  2294. { 5, 17, -5, -17 },
  2295. { 9, 29, -9, -29 },
  2296. { 13, 42, -13, -42 },
  2297. { 18, 60, -18, -60 },
  2298. { 24, 80, -24, -80 },
  2299. { 33, 106, -33, -106 },
  2300. { 47, 183, -47, -183 },
  2301. };
  2302. static const uint8_t s_etc2Mod[] = { 3, 6, 11, 16, 23, 32, 41, 64 };
  2303. static uint8_t uint8_sat(int32_t _a)
  2304. {
  2305. using namespace bx;
  2306. const uint32_t min = uint32_imin(_a, 255);
  2307. const uint32_t result = uint32_imax(min, 0);
  2308. return (uint8_t)result;
  2309. }
  2310. static uint8_t uint8_satadd(int32_t _a, int32_t _b)
  2311. {
  2312. const int32_t add = _a + _b;
  2313. return uint8_sat(add);
  2314. }
  2315. static void decodeBlockEtc2ModeT(uint8_t _dst[16*4], const uint8_t _src[8])
  2316. {
  2317. uint8_t rgb[16];
  2318. // 0 1 2 3 4 5 6 7
  2319. // 7654321076543210765432107654321076543210765432107654321076543210
  2320. // ...rr.rrggggbbbbrrrrggggbbbbDD.Dmmmmmmmmmmmmmmmmllllllllllllllll
  2321. // ^ ^ ^ ^ ^
  2322. // +-- c0 +-- c1 | +-- msb +-- lsb
  2323. // +-- dist
  2324. rgb[ 0] = ( (_src[0] >> 1) & 0xc)
  2325. | (_src[0] & 0x3)
  2326. ;
  2327. rgb[ 1] = _src[1] >> 4;
  2328. rgb[ 2] = _src[1] & 0xf;
  2329. rgb[ 8] = _src[2] >> 4;
  2330. rgb[ 9] = _src[2] & 0xf;
  2331. rgb[10] = _src[3] >> 4;
  2332. rgb[ 0] = bitRangeConvert(rgb[ 0], 4, 8);
  2333. rgb[ 1] = bitRangeConvert(rgb[ 1], 4, 8);
  2334. rgb[ 2] = bitRangeConvert(rgb[ 2], 4, 8);
  2335. rgb[ 8] = bitRangeConvert(rgb[ 8], 4, 8);
  2336. rgb[ 9] = bitRangeConvert(rgb[ 9], 4, 8);
  2337. rgb[10] = bitRangeConvert(rgb[10], 4, 8);
  2338. uint8_t dist = ((_src[3] >> 1) & 0x6) | (_src[3] & 0x1);
  2339. int32_t mod = s_etc2Mod[dist];
  2340. rgb[ 4] = uint8_satadd(rgb[ 8], mod);
  2341. rgb[ 5] = uint8_satadd(rgb[ 9], mod);
  2342. rgb[ 6] = uint8_satadd(rgb[10], mod);
  2343. rgb[12] = uint8_satadd(rgb[ 8], -mod);
  2344. rgb[13] = uint8_satadd(rgb[ 9], -mod);
  2345. rgb[14] = uint8_satadd(rgb[10], -mod);
  2346. uint32_t indexMsb = (_src[4]<<8) | _src[5];
  2347. uint32_t indexLsb = (_src[6]<<8) | _src[7];
  2348. for (uint32_t ii = 0; ii < 16; ++ii)
  2349. {
  2350. const uint32_t idx = (ii&0xc) | ( (ii & 0x3)<<4);
  2351. const uint32_t lsbi = indexLsb & 1;
  2352. const uint32_t msbi = (indexMsb & 1)<<1;
  2353. const uint32_t pal = (lsbi | msbi)<<2;
  2354. _dst[idx + 0] = rgb[pal+2];
  2355. _dst[idx + 1] = rgb[pal+1];
  2356. _dst[idx + 2] = rgb[pal+0];
  2357. _dst[idx + 3] = 255;
  2358. indexLsb >>= 1;
  2359. indexMsb >>= 1;
  2360. }
  2361. }
  2362. static void decodeBlockEtc2ModeH(uint8_t _dst[16*4], const uint8_t _src[8])
  2363. {
  2364. uint8_t rgb[16];
  2365. // 0 1 2 3 4 5 6 7
  2366. // 7654321076543210765432107654321076543210765432107654321076543210
  2367. // .rrrrggg...gb.bbbrrrrggggbbbbD.Dmmmmmmmmmmmmmmmmllllllllllllllll
  2368. // ^ ^ ^ ^ ^
  2369. // +-- c0 +-- c1 | +-- msb +-- lsb
  2370. // +-- dist
  2371. rgb[ 0] = (_src[0] >> 3) & 0xf;
  2372. rgb[ 1] = ( (_src[0] << 1) & 0xe)
  2373. | ( (_src[1] >> 4) & 0x1)
  2374. ;
  2375. rgb[ 2] = (_src[1] & 0x8)
  2376. | ( (_src[1] << 1) & 0x6)
  2377. | (_src[2] >> 7)
  2378. ;
  2379. rgb[ 8] = (_src[2] >> 3) & 0xf;
  2380. rgb[ 9] = ( (_src[2] << 1) & 0xe)
  2381. | (_src[3] >> 7)
  2382. ;
  2383. rgb[10] = (_src[3] >> 3) & 0xf;
  2384. rgb[ 0] = bitRangeConvert(rgb[ 0], 4, 8);
  2385. rgb[ 1] = bitRangeConvert(rgb[ 1], 4, 8);
  2386. rgb[ 2] = bitRangeConvert(rgb[ 2], 4, 8);
  2387. rgb[ 8] = bitRangeConvert(rgb[ 8], 4, 8);
  2388. rgb[ 9] = bitRangeConvert(rgb[ 9], 4, 8);
  2389. rgb[10] = bitRangeConvert(rgb[10], 4, 8);
  2390. uint32_t col0 = uint32_t(rgb[0]<<16) | uint32_t(rgb[1]<<8) | uint32_t(rgb[ 2]);
  2391. uint32_t col1 = uint32_t(rgb[8]<<16) | uint32_t(rgb[9]<<8) | uint32_t(rgb[10]);
  2392. uint8_t dist = (_src[3] & 0x4) | ((_src[3]<<1)&0x2) | (col0 >= col1);
  2393. int32_t mod = s_etc2Mod[dist];
  2394. rgb[ 4] = uint8_satadd(rgb[ 0], -mod);
  2395. rgb[ 5] = uint8_satadd(rgb[ 1], -mod);
  2396. rgb[ 6] = uint8_satadd(rgb[ 2], -mod);
  2397. rgb[ 0] = uint8_satadd(rgb[ 0], mod);
  2398. rgb[ 1] = uint8_satadd(rgb[ 1], mod);
  2399. rgb[ 2] = uint8_satadd(rgb[ 2], mod);
  2400. rgb[12] = uint8_satadd(rgb[ 8], -mod);
  2401. rgb[13] = uint8_satadd(rgb[ 9], -mod);
  2402. rgb[14] = uint8_satadd(rgb[10], -mod);
  2403. rgb[ 8] = uint8_satadd(rgb[ 8], mod);
  2404. rgb[ 9] = uint8_satadd(rgb[ 9], mod);
  2405. rgb[10] = uint8_satadd(rgb[10], mod);
  2406. uint32_t indexMsb = (_src[4]<<8) | _src[5];
  2407. uint32_t indexLsb = (_src[6]<<8) | _src[7];
  2408. for (uint32_t ii = 0; ii < 16; ++ii)
  2409. {
  2410. const uint32_t idx = (ii&0xc) | ( (ii & 0x3)<<4);
  2411. const uint32_t lsbi = indexLsb & 1;
  2412. const uint32_t msbi = (indexMsb & 1)<<1;
  2413. const uint32_t pal = (lsbi | msbi)<<2;
  2414. _dst[idx + 0] = rgb[pal+2];
  2415. _dst[idx + 1] = rgb[pal+1];
  2416. _dst[idx + 2] = rgb[pal+0];
  2417. _dst[idx + 3] = 255;
  2418. indexLsb >>= 1;
  2419. indexMsb >>= 1;
  2420. }
  2421. }
  2422. static void decodeBlockEtc2ModePlanar(uint8_t _dst[16*4], const uint8_t _src[8])
  2423. {
  2424. // 0 1 2 3 4 5 6 7
  2425. // 7654321076543210765432107654321076543210765432107654321076543210
  2426. // .rrrrrrg.ggggggb...bb.bbbrrrrr.rgggggggbbbbbbrrrrrrgggggggbbbbbb
  2427. // ^ ^ ^
  2428. // +-- c0 +-- cH +-- cV
  2429. uint8_t c0[3];
  2430. uint8_t cH[3];
  2431. uint8_t cV[3];
  2432. c0[0] = (_src[0] >> 1) & 0x3f;
  2433. c0[1] = ( (_src[0] & 1) << 6)
  2434. | ( (_src[1] >> 1) & 0x3f)
  2435. ;
  2436. c0[2] = ( (_src[1] & 1) << 5)
  2437. | ( (_src[2] & 0x18) )
  2438. | ( (_src[2] << 1) & 6)
  2439. | ( (_src[3] >> 7) )
  2440. ;
  2441. cH[0] = ( (_src[3] >> 1) & 0x3e)
  2442. | (_src[3] & 1)
  2443. ;
  2444. cH[1] = _src[4] >> 1;
  2445. cH[2] = ( (_src[4] & 1) << 5)
  2446. | (_src[5] >> 3)
  2447. ;
  2448. cV[0] = ( (_src[5] & 0x7) << 3)
  2449. | (_src[6] >> 5)
  2450. ;
  2451. cV[1] = ( (_src[6] & 0x1f) << 2)
  2452. | (_src[7] >> 5)
  2453. ;
  2454. cV[2] = _src[7] & 0x3f;
  2455. c0[0] = bitRangeConvert(c0[0], 6, 8);
  2456. c0[1] = bitRangeConvert(c0[1], 7, 8);
  2457. c0[2] = bitRangeConvert(c0[2], 6, 8);
  2458. cH[0] = bitRangeConvert(cH[0], 6, 8);
  2459. cH[1] = bitRangeConvert(cH[1], 7, 8);
  2460. cH[2] = bitRangeConvert(cH[2], 6, 8);
  2461. cV[0] = bitRangeConvert(cV[0], 6, 8);
  2462. cV[1] = bitRangeConvert(cV[1], 7, 8);
  2463. cV[2] = bitRangeConvert(cV[2], 6, 8);
  2464. int16_t dy[3];
  2465. dy[0] = cV[0] - c0[0];
  2466. dy[1] = cV[1] - c0[1];
  2467. dy[2] = cV[2] - c0[2];
  2468. int16_t sx[3];
  2469. sx[0] = int16_t(c0[0])<<2;
  2470. sx[1] = int16_t(c0[1])<<2;
  2471. sx[2] = int16_t(c0[2])<<2;
  2472. int16_t ex[3];
  2473. ex[0] = int16_t(cH[0])<<2;
  2474. ex[1] = int16_t(cH[1])<<2;
  2475. ex[2] = int16_t(cH[2])<<2;
  2476. for (int32_t vv = 0; vv < 4; ++vv)
  2477. {
  2478. int16_t dx[3];
  2479. dx[0] = (ex[0] - sx[0])>>2;
  2480. dx[1] = (ex[1] - sx[1])>>2;
  2481. dx[2] = (ex[2] - sx[2])>>2;
  2482. for (int32_t hh = 0; hh < 4; ++hh)
  2483. {
  2484. const uint32_t idx = (vv<<4) + (hh<<2);
  2485. _dst[idx + 0] = uint8_sat( (sx[2] + dx[2]*hh)>>2);
  2486. _dst[idx + 1] = uint8_sat( (sx[1] + dx[1]*hh)>>2);
  2487. _dst[idx + 2] = uint8_sat( (sx[0] + dx[0]*hh)>>2);
  2488. _dst[idx + 3] = 255;
  2489. }
  2490. sx[0] += dy[0];
  2491. sx[1] += dy[1];
  2492. sx[2] += dy[2];
  2493. ex[0] += dy[0];
  2494. ex[1] += dy[1];
  2495. ex[2] += dy[2];
  2496. }
  2497. }
  2498. static void decodeBlockEtc12(uint8_t _dst[16*4], const uint8_t _src[8])
  2499. {
  2500. if (!BX_ENABLED(BIMG_DECODE_ETC1 || BIMG_DECODE_ETC2) )
  2501. {
  2502. return;
  2503. }
  2504. bool flipBit = 0 != (_src[3] & 0x1);
  2505. bool diffBit = 0 != (_src[3] & 0x2);
  2506. uint8_t rgb[8];
  2507. if (diffBit)
  2508. {
  2509. rgb[0] = _src[0] >> 3;
  2510. rgb[1] = _src[1] >> 3;
  2511. rgb[2] = _src[2] >> 3;
  2512. int8_t diff[3];
  2513. diff[0] = int8_t( (_src[0] & 0x7)<<5)>>5;
  2514. diff[1] = int8_t( (_src[1] & 0x7)<<5)>>5;
  2515. diff[2] = int8_t( (_src[2] & 0x7)<<5)>>5;
  2516. int8_t rr = rgb[0] + diff[0];
  2517. int8_t gg = rgb[1] + diff[1];
  2518. int8_t bb = rgb[2] + diff[2];
  2519. // Etc2 3-modes
  2520. if (rr < 0 || rr > 31)
  2521. {
  2522. decodeBlockEtc2ModeT(_dst, _src);
  2523. return;
  2524. }
  2525. if (gg < 0 || gg > 31)
  2526. {
  2527. decodeBlockEtc2ModeH(_dst, _src);
  2528. return;
  2529. }
  2530. if (bb < 0 || bb > 31)
  2531. {
  2532. decodeBlockEtc2ModePlanar(_dst, _src);
  2533. return;
  2534. }
  2535. // Etc1
  2536. rgb[0] = bitRangeConvert(rgb[0], 5, 8);
  2537. rgb[1] = bitRangeConvert(rgb[1], 5, 8);
  2538. rgb[2] = bitRangeConvert(rgb[2], 5, 8);
  2539. rgb[4] = bitRangeConvert(rr, 5, 8);
  2540. rgb[5] = bitRangeConvert(gg, 5, 8);
  2541. rgb[6] = bitRangeConvert(bb, 5, 8);
  2542. }
  2543. else
  2544. {
  2545. rgb[0] = _src[0] >> 4;
  2546. rgb[1] = _src[1] >> 4;
  2547. rgb[2] = _src[2] >> 4;
  2548. rgb[4] = _src[0] & 0xf;
  2549. rgb[5] = _src[1] & 0xf;
  2550. rgb[6] = _src[2] & 0xf;
  2551. rgb[0] = bitRangeConvert(rgb[0], 4, 8);
  2552. rgb[1] = bitRangeConvert(rgb[1], 4, 8);
  2553. rgb[2] = bitRangeConvert(rgb[2], 4, 8);
  2554. rgb[4] = bitRangeConvert(rgb[4], 4, 8);
  2555. rgb[5] = bitRangeConvert(rgb[5], 4, 8);
  2556. rgb[6] = bitRangeConvert(rgb[6], 4, 8);
  2557. }
  2558. uint32_t table[2];
  2559. table[0] = (_src[3] >> 5) & 0x7;
  2560. table[1] = (_src[3] >> 2) & 0x7;
  2561. uint32_t indexMsb = (_src[4]<<8) | _src[5];
  2562. uint32_t indexLsb = (_src[6]<<8) | _src[7];
  2563. if (flipBit)
  2564. {
  2565. for (uint32_t ii = 0; ii < 16; ++ii)
  2566. {
  2567. const uint32_t block = (ii>>1)&1;
  2568. const uint32_t color = block<<2;
  2569. const uint32_t idx = (ii&0xc) | ( (ii & 0x3)<<4);
  2570. const uint32_t lsbi = indexLsb & 1;
  2571. const uint32_t msbi = (indexMsb & 1)<<1;
  2572. const int32_t mod = s_etc1Mod[table[block] ][lsbi | msbi];
  2573. _dst[idx + 0] = uint8_satadd(rgb[color+2], mod);
  2574. _dst[idx + 1] = uint8_satadd(rgb[color+1], mod);
  2575. _dst[idx + 2] = uint8_satadd(rgb[color+0], mod);
  2576. _dst[idx + 3] = 255;
  2577. indexLsb >>= 1;
  2578. indexMsb >>= 1;
  2579. }
  2580. }
  2581. else
  2582. {
  2583. for (uint32_t ii = 0; ii < 16; ++ii)
  2584. {
  2585. const uint32_t block = ii>>3;
  2586. const uint32_t color = block<<2;
  2587. const uint32_t idx = (ii&0xc) | ( (ii & 0x3)<<4);
  2588. const uint32_t lsbi = indexLsb & 1;
  2589. const uint32_t msbi = (indexMsb & 1)<<1;
  2590. const int32_t mod = s_etc1Mod[table[block] ][lsbi | msbi];
  2591. _dst[idx + 0] = uint8_satadd(rgb[color+2], mod);
  2592. _dst[idx + 1] = uint8_satadd(rgb[color+1], mod);
  2593. _dst[idx + 2] = uint8_satadd(rgb[color+0], mod);
  2594. _dst[idx + 3] = 255;
  2595. indexLsb >>= 1;
  2596. indexMsb >>= 1;
  2597. }
  2598. }
  2599. }
  2600. static const int8_t s_etc2aMod[16][8] =
  2601. {
  2602. { -3, -6, -9, -15, 2, 5, 8, 14 },
  2603. { -3, -7, -10, -13, 2, 6, 9, 12 },
  2604. { -2, -5, -8, -13, 1, 4, 7, 12 },
  2605. { -2, -4, -6, -13, 1, 3, 5, 12 },
  2606. { -3, -6, -8, -12, 2, 5, 7, 11 },
  2607. { -3, -7, -9, -11, 2, 6, 8, 10 },
  2608. { -4, -7, -8, -11, 3, 6, 7, 10 },
  2609. { -3, -5, -8, -11, 2, 4, 7, 10 },
  2610. { -2, -6, -8, -10, 1, 5, 7, 9 },
  2611. { -2, -5, -8, -10, 1, 4, 7, 9 },
  2612. { -2, -4, -8, -10, 1, 3, 7, 9 },
  2613. { -2, -5, -7, -10, 1, 4, 6, 9 },
  2614. { -3, -4, -7, -10, 2, 3, 6, 9 },
  2615. { -1, -2, -3, -10, 0, 1, 2, 9 },
  2616. { -4, -6, -8, -9, 3, 5, 7, 8 },
  2617. { -3, -5, -7, -9, 2, 4, 6, 8 }
  2618. };
  2619. void decodeBlockEtc2Alpha(uint8_t _dst[16 * 4], const uint8_t _src[8])
  2620. {
  2621. if (!BX_ENABLED(BIMG_DECODE_ETC2))
  2622. {
  2623. return;
  2624. }
  2625. const int32_t bc = _src[0];
  2626. const int8_t *modTable = s_etc2aMod[_src[1] & 0x0f];
  2627. const int32_t mult = (_src[1] & 0xf0) >> 4;
  2628. const uint64_t indices = ((uint64_t)_src[2] << 40)
  2629. | ((uint64_t)_src[3] << 32)
  2630. | ((uint64_t)_src[4] << 24)
  2631. | ((uint64_t)_src[5] << 16)
  2632. | ((uint64_t)_src[6] << 8)
  2633. | _src[7];
  2634. for (int ii = 0; ii < 16; ii++) {
  2635. const uint32_t idx = (ii & 0xc) | ((ii & 0x3) << 4);
  2636. const int32_t mod = modTable[(indices >> (45 - ii * 3)) & 0x7];
  2637. _dst[idx + 3] = uint8_satadd(bc, mod*mult);
  2638. }
  2639. }
  2640. static const uint8_t s_pvrtcFactors[16][4] =
  2641. {
  2642. { 4, 4, 4, 4 },
  2643. { 2, 6, 2, 6 },
  2644. { 8, 0, 8, 0 },
  2645. { 6, 2, 6, 2 },
  2646. { 2, 2, 6, 6 },
  2647. { 1, 3, 3, 9 },
  2648. { 4, 0, 12, 0 },
  2649. { 3, 1, 9, 3 },
  2650. { 8, 8, 0, 0 },
  2651. { 4, 12, 0, 0 },
  2652. { 16, 0, 0, 0 },
  2653. { 12, 4, 0, 0 },
  2654. { 6, 6, 2, 2 },
  2655. { 3, 9, 1, 3 },
  2656. { 12, 0, 4, 0 },
  2657. { 9, 3, 3, 1 },
  2658. };
  2659. static const uint8_t s_pvrtcWeights[8][4] =
  2660. {
  2661. { 8, 0, 8, 0 },
  2662. { 5, 3, 5, 3 },
  2663. { 3, 5, 3, 5 },
  2664. { 0, 8, 0, 8 },
  2665. { 8, 0, 8, 0 },
  2666. { 4, 4, 4, 4 },
  2667. { 4, 4, 4, 4 },
  2668. { 0, 8, 0, 8 },
  2669. };
  2670. uint32_t morton2d(uint32_t _x, uint32_t _y)
  2671. {
  2672. using namespace bx;
  2673. const uint32_t tmpx = uint32_part1by1(_x);
  2674. const uint32_t xbits = uint32_sll(tmpx, 1);
  2675. const uint32_t ybits = uint32_part1by1(_y);
  2676. const uint32_t result = uint32_or(xbits, ybits);
  2677. return result;
  2678. }
  2679. uint32_t getColor(const uint8_t _src[8])
  2680. {
  2681. return 0
  2682. | _src[7]<<24
  2683. | _src[6]<<16
  2684. | _src[5]<<8
  2685. | _src[4]
  2686. ;
  2687. }
  2688. static void decodeBlockPtc14RgbAddA(uint32_t _block, uint32_t* _r, uint32_t* _g, uint32_t* _b, uint8_t _factor)
  2689. {
  2690. if (0 != (_block & (1<<15) ) )
  2691. {
  2692. *_r += bitRangeConvert( (_block >> 10) & 0x1f, 5, 8) * _factor;
  2693. *_g += bitRangeConvert( (_block >> 5) & 0x1f, 5, 8) * _factor;
  2694. *_b += bitRangeConvert( (_block >> 1) & 0x0f, 4, 8) * _factor;
  2695. }
  2696. else
  2697. {
  2698. *_r += bitRangeConvert( (_block >> 8) & 0xf, 4, 8) * _factor;
  2699. *_g += bitRangeConvert( (_block >> 4) & 0xf, 4, 8) * _factor;
  2700. *_b += bitRangeConvert( (_block >> 1) & 0x7, 3, 8) * _factor;
  2701. }
  2702. }
  2703. static void decodeBlockPtc14RgbAddB(uint32_t _block, uint32_t* _r, uint32_t* _g, uint32_t* _b, uint8_t _factor)
  2704. {
  2705. if (0 != (_block & (1<<31) ) )
  2706. {
  2707. *_r += bitRangeConvert( (_block >> 26) & 0x1f, 5, 8) * _factor;
  2708. *_g += bitRangeConvert( (_block >> 21) & 0x1f, 5, 8) * _factor;
  2709. *_b += bitRangeConvert( (_block >> 16) & 0x1f, 5, 8) * _factor;
  2710. }
  2711. else
  2712. {
  2713. *_r += bitRangeConvert( (_block >> 24) & 0xf, 4, 8) * _factor;
  2714. *_g += bitRangeConvert( (_block >> 20) & 0xf, 4, 8) * _factor;
  2715. *_b += bitRangeConvert( (_block >> 16) & 0xf, 4, 8) * _factor;
  2716. }
  2717. }
  2718. static void decodeBlockPtc14(uint8_t _dst[16*4], const uint8_t* _src, uint32_t _x, uint32_t _y, uint32_t _width, uint32_t _height)
  2719. {
  2720. // 0 1 2 3 4 5 6 7
  2721. // 7654321076543210765432107654321076543210765432107654321076543210
  2722. // mmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmyrrrrrgggggbbbbbxrrrrrgggggbbbbp
  2723. // ^ ^^ ^^ ^
  2724. // +-- modulation data |+- B color |+- A color |
  2725. // +-- B opaque +-- A opaque |
  2726. // alpha punchthrough --+
  2727. const uint8_t* bc = &_src[morton2d(_x, _y) * 8];
  2728. uint32_t mod = 0
  2729. | bc[3]<<24
  2730. | bc[2]<<16
  2731. | bc[1]<<8
  2732. | bc[0]
  2733. ;
  2734. const bool punchthrough = !!(bc[7] & 1);
  2735. const uint8_t* weightTable = s_pvrtcWeights[4 * punchthrough];
  2736. const uint8_t* factorTable = s_pvrtcFactors[0];
  2737. for (int yy = 0; yy < 4; ++yy)
  2738. {
  2739. const uint32_t yOffset = (yy < 2) ? -1 : 0;
  2740. const uint32_t y0 = (_y + yOffset) % _height;
  2741. const uint32_t y1 = (y0 + 1) % _height;
  2742. for (int xx = 0; xx < 4; ++xx)
  2743. {
  2744. const uint32_t xOffset = (xx < 2) ? -1 : 0;
  2745. const uint32_t x0 = (_x + xOffset) % _width;
  2746. const uint32_t x1 = (x0 + 1) % _width;
  2747. const uint32_t bc0 = getColor(&_src[morton2d(x0, y0) * 8]);
  2748. const uint32_t bc1 = getColor(&_src[morton2d(x1, y0) * 8]);
  2749. const uint32_t bc2 = getColor(&_src[morton2d(x0, y1) * 8]);
  2750. const uint32_t bc3 = getColor(&_src[morton2d(x1, y1) * 8]);
  2751. const uint8_t f0 = factorTable[0];
  2752. const uint8_t f1 = factorTable[1];
  2753. const uint8_t f2 = factorTable[2];
  2754. const uint8_t f3 = factorTable[3];
  2755. uint32_t ar = 0, ag = 0, ab = 0;
  2756. decodeBlockPtc14RgbAddA(bc0, &ar, &ag, &ab, f0);
  2757. decodeBlockPtc14RgbAddA(bc1, &ar, &ag, &ab, f1);
  2758. decodeBlockPtc14RgbAddA(bc2, &ar, &ag, &ab, f2);
  2759. decodeBlockPtc14RgbAddA(bc3, &ar, &ag, &ab, f3);
  2760. uint32_t br = 0, bg = 0, bb = 0;
  2761. decodeBlockPtc14RgbAddB(bc0, &br, &bg, &bb, f0);
  2762. decodeBlockPtc14RgbAddB(bc1, &br, &bg, &bb, f1);
  2763. decodeBlockPtc14RgbAddB(bc2, &br, &bg, &bb, f2);
  2764. decodeBlockPtc14RgbAddB(bc3, &br, &bg, &bb, f3);
  2765. const uint8_t* weight = &weightTable[(mod & 3)*4];
  2766. const uint8_t wa = weight[0];
  2767. const uint8_t wb = weight[1];
  2768. _dst[(yy*4 + xx)*4+0] = uint8_t( (ab * wa + bb * wb) >> 7);
  2769. _dst[(yy*4 + xx)*4+1] = uint8_t( (ag * wa + bg * wb) >> 7);
  2770. _dst[(yy*4 + xx)*4+2] = uint8_t( (ar * wa + br * wb) >> 7);
  2771. _dst[(yy*4 + xx)*4+3] = 255;
  2772. mod >>= 2;
  2773. factorTable += 4;
  2774. }
  2775. }
  2776. }
  2777. static void decodeBlockPtc14ARgbaAddA(uint32_t _block, uint32_t* _r, uint32_t* _g, uint32_t* _b, uint32_t* _a, uint8_t _factor)
  2778. {
  2779. if (0 != (_block & (1<<15) ) )
  2780. {
  2781. *_r += bitRangeConvert( (_block >> 10) & 0x1f, 5, 8) * _factor;
  2782. *_g += bitRangeConvert( (_block >> 5) & 0x1f, 5, 8) * _factor;
  2783. *_b += bitRangeConvert( (_block >> 1) & 0x0f, 4, 8) * _factor;
  2784. *_a += 255 * _factor;
  2785. }
  2786. else
  2787. {
  2788. *_r += bitRangeConvert( (_block >> 8) & 0xf, 4, 8) * _factor;
  2789. *_g += bitRangeConvert( (_block >> 4) & 0xf, 4, 8) * _factor;
  2790. *_b += bitRangeConvert( (_block >> 1) & 0x7, 3, 8) * _factor;
  2791. *_a += bitRangeConvert( (_block >> 12) & 0x7, 3, 8) * _factor;
  2792. }
  2793. }
  2794. static void decodeBlockPtc14ARgbaAddB(uint32_t _block, uint32_t* _r, uint32_t* _g, uint32_t* _b, uint32_t* _a, uint8_t _factor)
  2795. {
  2796. if (0 != (_block & (1<<31) ) )
  2797. {
  2798. *_r += bitRangeConvert( (_block >> 26) & 0x1f, 5, 8) * _factor;
  2799. *_g += bitRangeConvert( (_block >> 21) & 0x1f, 5, 8) * _factor;
  2800. *_b += bitRangeConvert( (_block >> 16) & 0x1f, 5, 8) * _factor;
  2801. *_a += 255 * _factor;
  2802. }
  2803. else
  2804. {
  2805. *_r += bitRangeConvert( (_block >> 24) & 0xf, 4, 8) * _factor;
  2806. *_g += bitRangeConvert( (_block >> 20) & 0xf, 4, 8) * _factor;
  2807. *_b += bitRangeConvert( (_block >> 16) & 0xf, 4, 8) * _factor;
  2808. *_a += bitRangeConvert( (_block >> 28) & 0x7, 3, 8) * _factor;
  2809. }
  2810. }
  2811. static void decodeBlockPtc14A(uint8_t _dst[16*4], const uint8_t* _src, uint32_t _x, uint32_t _y, uint32_t _width, uint32_t _height)
  2812. {
  2813. // 0 1 2 3 4 5 6 7
  2814. // 7654321076543210765432107654321076543210765432107654321076543210
  2815. // mmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmyrrrrrgggggbbbbbxrrrrrgggggbbbbp
  2816. // ^ ^^ ^^ ^
  2817. // +-- modulation data |+- B color |+- A color |
  2818. // +-- B opaque +-- A opaque |
  2819. // alpha punchthrough --+
  2820. const uint8_t* bc = &_src[morton2d(_x, _y) * 8];
  2821. uint32_t mod = 0
  2822. | bc[3]<<24
  2823. | bc[2]<<16
  2824. | bc[1]<<8
  2825. | bc[0]
  2826. ;
  2827. const bool punchthrough = !!(bc[7] & 1);
  2828. const uint8_t* weightTable = s_pvrtcWeights[4 * punchthrough];
  2829. const uint8_t* factorTable = s_pvrtcFactors[0];
  2830. for (int yy = 0; yy < 4; ++yy)
  2831. {
  2832. const uint32_t yOffset = (yy < 2) ? -1 : 0;
  2833. const uint32_t y0 = (_y + yOffset) % _height;
  2834. const uint32_t y1 = (y0 + 1) % _height;
  2835. for (int xx = 0; xx < 4; ++xx)
  2836. {
  2837. const uint32_t xOffset = (xx < 2) ? -1 : 0;
  2838. const uint32_t x0 = (_x + xOffset) % _width;
  2839. const uint32_t x1 = (x0 + 1) % _width;
  2840. const uint32_t bc0 = getColor(&_src[morton2d(x0, y0) * 8]);
  2841. const uint32_t bc1 = getColor(&_src[morton2d(x1, y0) * 8]);
  2842. const uint32_t bc2 = getColor(&_src[morton2d(x0, y1) * 8]);
  2843. const uint32_t bc3 = getColor(&_src[morton2d(x1, y1) * 8]);
  2844. const uint8_t f0 = factorTable[0];
  2845. const uint8_t f1 = factorTable[1];
  2846. const uint8_t f2 = factorTable[2];
  2847. const uint8_t f3 = factorTable[3];
  2848. uint32_t ar = 0, ag = 0, ab = 0, aa = 0;
  2849. decodeBlockPtc14ARgbaAddA(bc0, &ar, &ag, &ab, &aa, f0);
  2850. decodeBlockPtc14ARgbaAddA(bc1, &ar, &ag, &ab, &aa, f1);
  2851. decodeBlockPtc14ARgbaAddA(bc2, &ar, &ag, &ab, &aa, f2);
  2852. decodeBlockPtc14ARgbaAddA(bc3, &ar, &ag, &ab, &aa, f3);
  2853. uint32_t br = 0, bg = 0, bb = 0, ba = 0;
  2854. decodeBlockPtc14ARgbaAddB(bc0, &br, &bg, &bb, &ba, f0);
  2855. decodeBlockPtc14ARgbaAddB(bc1, &br, &bg, &bb, &ba, f1);
  2856. decodeBlockPtc14ARgbaAddB(bc2, &br, &bg, &bb, &ba, f2);
  2857. decodeBlockPtc14ARgbaAddB(bc3, &br, &bg, &bb, &ba, f3);
  2858. const uint8_t* weight = &weightTable[(mod & 3)*4];
  2859. const uint8_t wa = weight[0];
  2860. const uint8_t wb = weight[1];
  2861. const uint8_t wc = weight[2];
  2862. const uint8_t wd = weight[3];
  2863. _dst[(yy*4 + xx)*4+0] = uint8_t( (ab * wa + bb * wb) >> 7);
  2864. _dst[(yy*4 + xx)*4+1] = uint8_t( (ag * wa + bg * wb) >> 7);
  2865. _dst[(yy*4 + xx)*4+2] = uint8_t( (ar * wa + br * wb) >> 7);
  2866. _dst[(yy*4 + xx)*4+3] = uint8_t( (aa * wc + ba * wd) >> 7);
  2867. mod >>= 2;
  2868. factorTable += 4;
  2869. }
  2870. }
  2871. }
  2872. ImageContainer* imageAlloc(bx::AllocatorI* _allocator, TextureFormat::Enum _format, uint16_t _width, uint16_t _height, uint16_t _depth, uint16_t _numLayers, bool _cubeMap, bool _hasMips, const void* _data)
  2873. {
  2874. const ImageBlockInfo& blockInfo = getBlockInfo(_format);
  2875. const uint16_t blockWidth = blockInfo.blockWidth;
  2876. const uint16_t blockHeight = blockInfo.blockHeight;
  2877. const uint16_t minBlockX = blockInfo.minBlockX;
  2878. const uint16_t minBlockY = blockInfo.minBlockY;
  2879. _width = bx::max<uint16_t>(blockWidth * minBlockX, ( (_width + blockWidth - 1) / blockWidth)*blockWidth);
  2880. _height = bx::max<uint16_t>(blockHeight * minBlockY, ( (_height + blockHeight - 1) / blockHeight)*blockHeight);
  2881. _depth = bx::max<uint16_t>(1, _depth);
  2882. _numLayers = bx::max<uint16_t>(1, _numLayers);
  2883. const uint8_t numMips = _hasMips ? imageGetNumMips(_format, _width, _height, _depth) : 1;
  2884. uint32_t size = imageGetSize(NULL, _width, _height, _depth, _cubeMap, _hasMips, _numLayers, _format);
  2885. ImageContainer* imageContainer = (ImageContainer*)BX_ALIGNED_ALLOC(_allocator, size + bx::alignUp(sizeof(ImageContainer), 16), 16);
  2886. imageContainer->m_allocator = _allocator;
  2887. imageContainer->m_data = bx::alignPtr(imageContainer + 1, 0, 16);
  2888. imageContainer->m_format = _format;
  2889. imageContainer->m_orientation = Orientation::R0;
  2890. imageContainer->m_size = size;
  2891. imageContainer->m_offset = 0;
  2892. imageContainer->m_width = _width;
  2893. imageContainer->m_height = _height;
  2894. imageContainer->m_depth = _depth;
  2895. imageContainer->m_numLayers = _numLayers;
  2896. imageContainer->m_numMips = numMips;
  2897. imageContainer->m_hasAlpha = false;
  2898. imageContainer->m_cubeMap = _cubeMap;
  2899. imageContainer->m_ktx = false;
  2900. imageContainer->m_pvr3 = false;
  2901. imageContainer->m_ktxLE = false;
  2902. imageContainer->m_srgb = false;
  2903. if (NULL != _data)
  2904. {
  2905. bx::memCopy(imageContainer->m_data, _data, imageContainer->m_size);
  2906. }
  2907. return imageContainer;
  2908. }
  2909. void imageFree(ImageContainer* _imageContainer)
  2910. {
  2911. BX_ALIGNED_FREE(_imageContainer->m_allocator, _imageContainer, 16);
  2912. }
  2913. // DDS
  2914. #define DDS_MAGIC BX_MAKEFOURCC('D', 'D', 'S', ' ')
  2915. #define DDS_HEADER_SIZE 124
  2916. #define DDS_DXT1 BX_MAKEFOURCC('D', 'X', 'T', '1')
  2917. #define DDS_DXT2 BX_MAKEFOURCC('D', 'X', 'T', '2')
  2918. #define DDS_DXT3 BX_MAKEFOURCC('D', 'X', 'T', '3')
  2919. #define DDS_DXT4 BX_MAKEFOURCC('D', 'X', 'T', '4')
  2920. #define DDS_DXT5 BX_MAKEFOURCC('D', 'X', 'T', '5')
  2921. #define DDS_ATI1 BX_MAKEFOURCC('A', 'T', 'I', '1')
  2922. #define DDS_BC4U BX_MAKEFOURCC('B', 'C', '4', 'U')
  2923. #define DDS_ATI2 BX_MAKEFOURCC('A', 'T', 'I', '2')
  2924. #define DDS_BC5U BX_MAKEFOURCC('B', 'C', '5', 'U')
  2925. #define DDS_DX10 BX_MAKEFOURCC('D', 'X', '1', '0')
  2926. #define DDS_ETC1 BX_MAKEFOURCC('E', 'T', 'C', '1')
  2927. #define DDS_ETC2 BX_MAKEFOURCC('E', 'T', 'C', '2')
  2928. #define DDS_ET2A BX_MAKEFOURCC('E', 'T', '2', 'A')
  2929. #define DDS_PTC2 BX_MAKEFOURCC('P', 'T', 'C', '2')
  2930. #define DDS_PTC4 BX_MAKEFOURCC('P', 'T', 'C', '4')
  2931. #define DDS_ATC BX_MAKEFOURCC('A', 'T', 'C', ' ')
  2932. #define DDS_ATCE BX_MAKEFOURCC('A', 'T', 'C', 'E')
  2933. #define DDS_ATCI BX_MAKEFOURCC('A', 'T', 'C', 'I')
  2934. #define DDS_ASTC4x4 BX_MAKEFOURCC('A', 'S', '4', '4')
  2935. #define DDS_ASTC5x4 BX_MAKEFOURCC('A', 'S', '5', '4')
  2936. #define DDS_ASTC5x5 BX_MAKEFOURCC('A', 'S', '5', '5')
  2937. #define DDS_ASTC6x5 BX_MAKEFOURCC('A', 'S', '6', '5')
  2938. #define DDS_ASTC6x6 BX_MAKEFOURCC('A', 'S', '6', '6')
  2939. #define DDS_ASTC8x5 BX_MAKEFOURCC('A', 'S', '8', '5')
  2940. #define DDS_ASTC8x6 BX_MAKEFOURCC('A', 'S', '8', '6')
  2941. #define DDS_ASTC8x8 BX_MAKEFOURCC('A', 'S', '8', '8')
  2942. #define DDS_ASTC10x5 BX_MAKEFOURCC('A', 'S', ':', '5')
  2943. #define DDS_ASTC10x6 BX_MAKEFOURCC('A', 'S', ':', '6')
  2944. #define DDS_ASTC10x8 BX_MAKEFOURCC('A', 'S', ':', '8')
  2945. #define DDS_ASTC10x10 BX_MAKEFOURCC('A', 'S', ':', ':')
  2946. #define DDS_ASTC12x10 BX_MAKEFOURCC('A', 'S', '<', ':')
  2947. #define DDS_ASTC12x12 BX_MAKEFOURCC('A', 'S', '<', '<')
  2948. #define DDS_R8G8B8 20
  2949. #define DDS_A8R8G8B8 21
  2950. #define DDS_R5G6B5 23
  2951. #define DDS_A1R5G5B5 25
  2952. #define DDS_A4R4G4B4 26
  2953. #define DDS_A2B10G10R10 31
  2954. #define DDS_G16R16 34
  2955. #define DDS_A2R10G10B10 35
  2956. #define DDS_A16B16G16R16 36
  2957. #define DDS_A8L8 51
  2958. #define DDS_R16F 111
  2959. #define DDS_G16R16F 112
  2960. #define DDS_A16B16G16R16F 113
  2961. #define DDS_R32F 114
  2962. #define DDS_G32R32F 115
  2963. #define DDS_A32B32G32R32F 116
  2964. #define DDS_FORMAT_R32G32B32A32_FLOAT 2
  2965. #define DDS_FORMAT_R32G32B32A32_UINT 3
  2966. #define DDS_FORMAT_R16G16B16A16_FLOAT 10
  2967. #define DDS_FORMAT_R16G16B16A16_UNORM 11
  2968. #define DDS_FORMAT_R16G16B16A16_UINT 12
  2969. #define DDS_FORMAT_R32G32_FLOAT 16
  2970. #define DDS_FORMAT_R32G32_UINT 17
  2971. #define DDS_FORMAT_R10G10B10A2_UNORM 24
  2972. #define DDS_FORMAT_R11G11B10_FLOAT 26
  2973. #define DDS_FORMAT_R8G8B8A8_UNORM 28
  2974. #define DDS_FORMAT_R8G8B8A8_UNORM_SRGB 29
  2975. #define DDS_FORMAT_R16G16_FLOAT 34
  2976. #define DDS_FORMAT_R16G16_UNORM 35
  2977. #define DDS_FORMAT_R32_FLOAT 41
  2978. #define DDS_FORMAT_R32_UINT 42
  2979. #define DDS_FORMAT_R8G8_UNORM 49
  2980. #define DDS_FORMAT_R16_FLOAT 54
  2981. #define DDS_FORMAT_R16_UNORM 56
  2982. #define DDS_FORMAT_R8_UNORM 61
  2983. #define DDS_FORMAT_R1_UNORM 66
  2984. #define DDS_FORMAT_BC1_UNORM 71
  2985. #define DDS_FORMAT_BC1_UNORM_SRGB 72
  2986. #define DDS_FORMAT_BC2_UNORM 74
  2987. #define DDS_FORMAT_BC2_UNORM_SRGB 75
  2988. #define DDS_FORMAT_BC3_UNORM 77
  2989. #define DDS_FORMAT_BC3_UNORM_SRGB 78
  2990. #define DDS_FORMAT_BC4_UNORM 80
  2991. #define DDS_FORMAT_BC5_UNORM 83
  2992. #define DDS_FORMAT_B5G6R5_UNORM 85
  2993. #define DDS_FORMAT_B5G5R5A1_UNORM 86
  2994. #define DDS_FORMAT_B8G8R8A8_UNORM 87
  2995. #define DDS_FORMAT_B8G8R8A8_UNORM_SRGB 91
  2996. #define DDS_FORMAT_BC6H_SF16 96
  2997. #define DDS_FORMAT_BC7_UNORM 98
  2998. #define DDS_FORMAT_BC7_UNORM_SRGB 99
  2999. #define DDS_FORMAT_B4G4R4A4_UNORM 115
  3000. #define DDS_DX10_DIMENSION_TEXTURE2D 3
  3001. #define DDS_DX10_DIMENSION_TEXTURE3D 4
  3002. #define DDS_DX10_MISC_TEXTURECUBE 4
  3003. #define DDSD_CAPS 0x00000001
  3004. #define DDSD_HEIGHT 0x00000002
  3005. #define DDSD_WIDTH 0x00000004
  3006. #define DDSD_PITCH 0x00000008
  3007. #define DDSD_PIXELFORMAT 0x00001000
  3008. #define DDSD_MIPMAPCOUNT 0x00020000
  3009. #define DDSD_LINEARSIZE 0x00080000
  3010. #define DDSD_DEPTH 0x00800000
  3011. #define DDPF_ALPHAPIXELS 0x00000001
  3012. #define DDPF_ALPHA 0x00000002
  3013. #define DDPF_FOURCC 0x00000004
  3014. #define DDPF_INDEXED 0x00000020
  3015. #define DDPF_RGB 0x00000040
  3016. #define DDPF_YUV 0x00000200
  3017. #define DDPF_LUMINANCE 0x00020000
  3018. #define DDPF_BUMPDUDV 0x00080000
  3019. #define DDSCAPS_COMPLEX 0x00000008
  3020. #define DDSCAPS_TEXTURE 0x00001000
  3021. #define DDSCAPS_MIPMAP 0x00400000
  3022. #define DDSCAPS2_VOLUME 0x00200000
  3023. #define DDSCAPS2_CUBEMAP 0x00000200
  3024. #define DDSCAPS2_CUBEMAP_POSITIVEX 0x00000400
  3025. #define DDSCAPS2_CUBEMAP_NEGATIVEX 0x00000800
  3026. #define DDSCAPS2_CUBEMAP_POSITIVEY 0x00001000
  3027. #define DDSCAPS2_CUBEMAP_NEGATIVEY 0x00002000
  3028. #define DDSCAPS2_CUBEMAP_POSITIVEZ 0x00004000
  3029. #define DDSCAPS2_CUBEMAP_NEGATIVEZ 0x00008000
  3030. #define DSCAPS2_CUBEMAP_ALLSIDES (0 \
  3031. | DDSCAPS2_CUBEMAP_POSITIVEX \
  3032. | DDSCAPS2_CUBEMAP_NEGATIVEX \
  3033. | DDSCAPS2_CUBEMAP_POSITIVEY \
  3034. | DDSCAPS2_CUBEMAP_NEGATIVEY \
  3035. | DDSCAPS2_CUBEMAP_POSITIVEZ \
  3036. | DDSCAPS2_CUBEMAP_NEGATIVEZ \
  3037. )
  3038. struct TranslateDdsFormat
  3039. {
  3040. uint32_t m_format;
  3041. TextureFormat::Enum m_textureFormat;
  3042. bool m_srgb;
  3043. };
  3044. static const TranslateDdsFormat s_translateDdsFourccFormat[] =
  3045. {
  3046. { DDS_DXT1, TextureFormat::BC1, false },
  3047. { DDS_DXT2, TextureFormat::BC2, false },
  3048. { DDS_DXT3, TextureFormat::BC2, false },
  3049. { DDS_DXT4, TextureFormat::BC3, false },
  3050. { DDS_DXT5, TextureFormat::BC3, false },
  3051. { DDS_ATI1, TextureFormat::BC4, false },
  3052. { DDS_BC4U, TextureFormat::BC4, false },
  3053. { DDS_ATI2, TextureFormat::BC5, false },
  3054. { DDS_BC5U, TextureFormat::BC5, false },
  3055. { DDS_ETC1, TextureFormat::ETC1, false },
  3056. { DDS_ETC2, TextureFormat::ETC2, false },
  3057. { DDS_ET2A, TextureFormat::ETC2A, false },
  3058. { DDS_PTC2, TextureFormat::PTC12A, false },
  3059. { DDS_PTC4, TextureFormat::PTC14A, false },
  3060. { DDS_ATC , TextureFormat::ATC, false },
  3061. { DDS_ATCE, TextureFormat::ATCE, false },
  3062. { DDS_ATCI, TextureFormat::ATCI, false },
  3063. { DDS_ASTC4x4, TextureFormat::ASTC4x4, false },
  3064. { DDS_ASTC5x4, TextureFormat::ASTC5x4, false },
  3065. { DDS_ASTC5x5, TextureFormat::ASTC5x5, false },
  3066. { DDS_ASTC6x5, TextureFormat::ASTC6x5, false },
  3067. { DDS_ASTC6x6, TextureFormat::ASTC6x6, false },
  3068. { DDS_ASTC8x5, TextureFormat::ASTC8x5, false },
  3069. { DDS_ASTC8x6, TextureFormat::ASTC8x6, false },
  3070. { DDS_ASTC8x8, TextureFormat::ASTC8x8, false },
  3071. { DDS_ASTC10x5, TextureFormat::ASTC10x5, false },
  3072. { DDS_ASTC10x6, TextureFormat::ASTC10x6, false },
  3073. { DDS_ASTC10x8, TextureFormat::ASTC10x8, false },
  3074. { DDS_ASTC10x10, TextureFormat::ASTC10x10,false },
  3075. { DDS_ASTC12x10, TextureFormat::ASTC12x10,false },
  3076. { DDS_ASTC12x12, TextureFormat::ASTC12x12,false },
  3077. { DDS_A16B16G16R16, TextureFormat::RGBA16, false },
  3078. { DDS_A16B16G16R16F, TextureFormat::RGBA16F, false },
  3079. { DDPF_RGB|DDPF_ALPHAPIXELS, TextureFormat::BGRA8, false },
  3080. { DDPF_INDEXED, TextureFormat::R8, false },
  3081. { DDPF_LUMINANCE, TextureFormat::R8, false },
  3082. { DDPF_ALPHA, TextureFormat::R8, false },
  3083. { DDS_R16F, TextureFormat::R16F, false },
  3084. { DDS_R32F, TextureFormat::R32F, false },
  3085. { DDS_A8L8, TextureFormat::RG8, false },
  3086. { DDS_G16R16, TextureFormat::RG16, false },
  3087. { DDS_G16R16F, TextureFormat::RG16F, false },
  3088. { DDS_G32R32F, TextureFormat::RG32F, false },
  3089. { DDS_R8G8B8, TextureFormat::RGB8, false },
  3090. { DDS_A8R8G8B8, TextureFormat::BGRA8, false },
  3091. { DDS_A16B16G16R16, TextureFormat::RGBA16, false },
  3092. { DDS_A16B16G16R16F, TextureFormat::RGBA16F, false },
  3093. { DDS_A32B32G32R32F, TextureFormat::RGBA32F, false },
  3094. { DDS_R5G6B5, TextureFormat::B5G6R5, false },
  3095. { DDS_R5G6B5, TextureFormat::R5G6B5, false },
  3096. { DDS_A4R4G4B4, TextureFormat::BGRA4, false },
  3097. { DDS_A4R4G4B4, TextureFormat::RGBA4, false },
  3098. { DDS_A1R5G5B5, TextureFormat::BGR5A1, false },
  3099. { DDS_A1R5G5B5, TextureFormat::RGB5A1, false },
  3100. { DDS_A2B10G10R10, TextureFormat::RGB10A2, false },
  3101. };
  3102. static const TranslateDdsFormat s_translateDxgiFormat[] =
  3103. {
  3104. { DDS_FORMAT_BC1_UNORM, TextureFormat::BC1, false },
  3105. { DDS_FORMAT_BC1_UNORM_SRGB, TextureFormat::BC1, true },
  3106. { DDS_FORMAT_BC2_UNORM, TextureFormat::BC2, false },
  3107. { DDS_FORMAT_BC2_UNORM_SRGB, TextureFormat::BC2, true },
  3108. { DDS_FORMAT_BC3_UNORM, TextureFormat::BC3, false },
  3109. { DDS_FORMAT_BC3_UNORM_SRGB, TextureFormat::BC3, true },
  3110. { DDS_FORMAT_BC4_UNORM, TextureFormat::BC4, false },
  3111. { DDS_FORMAT_BC5_UNORM, TextureFormat::BC5, false },
  3112. { DDS_FORMAT_BC6H_SF16, TextureFormat::BC6H, false },
  3113. { DDS_FORMAT_BC7_UNORM, TextureFormat::BC7, false },
  3114. { DDS_FORMAT_BC7_UNORM_SRGB, TextureFormat::BC7, true },
  3115. { DDS_FORMAT_R1_UNORM, TextureFormat::R1, false },
  3116. { DDS_FORMAT_R8_UNORM, TextureFormat::R8, false },
  3117. { DDS_FORMAT_R16_UNORM, TextureFormat::R16, false },
  3118. { DDS_FORMAT_R16_FLOAT, TextureFormat::R16F, false },
  3119. { DDS_FORMAT_R32_UINT, TextureFormat::R32U, false },
  3120. { DDS_FORMAT_R32_FLOAT, TextureFormat::R32F, false },
  3121. { DDS_FORMAT_R8G8_UNORM, TextureFormat::RG8, false },
  3122. { DDS_FORMAT_R16G16_UNORM, TextureFormat::RG16, false },
  3123. { DDS_FORMAT_R16G16_FLOAT, TextureFormat::RG16F, false },
  3124. { DDS_FORMAT_R32G32_UINT, TextureFormat::RG32U, false },
  3125. { DDS_FORMAT_R32G32_FLOAT, TextureFormat::RG32F, false },
  3126. { DDS_FORMAT_B8G8R8A8_UNORM, TextureFormat::BGRA8, false },
  3127. { DDS_FORMAT_B8G8R8A8_UNORM_SRGB, TextureFormat::BGRA8, true },
  3128. { DDS_FORMAT_R8G8B8A8_UNORM, TextureFormat::RGBA8, false },
  3129. { DDS_FORMAT_R8G8B8A8_UNORM_SRGB, TextureFormat::RGBA8, true },
  3130. { DDS_FORMAT_R16G16B16A16_UNORM, TextureFormat::RGBA16, false },
  3131. { DDS_FORMAT_R16G16B16A16_FLOAT, TextureFormat::RGBA16F, false },
  3132. { DDS_FORMAT_R32G32B32A32_UINT, TextureFormat::RGBA32U, false },
  3133. { DDS_FORMAT_R32G32B32A32_FLOAT, TextureFormat::RGBA32F, false },
  3134. { DDS_FORMAT_B5G6R5_UNORM, TextureFormat::B5G6R5, false },
  3135. { DDS_FORMAT_B5G6R5_UNORM, TextureFormat::R5G6B5, false },
  3136. { DDS_FORMAT_B4G4R4A4_UNORM, TextureFormat::BGRA4, false },
  3137. { DDS_FORMAT_B4G4R4A4_UNORM, TextureFormat::RGBA4, false },
  3138. { DDS_FORMAT_B5G5R5A1_UNORM, TextureFormat::BGR5A1, false },
  3139. { DDS_FORMAT_B5G5R5A1_UNORM, TextureFormat::RGB5A1, false },
  3140. { DDS_FORMAT_R10G10B10A2_UNORM, TextureFormat::RGB10A2, false },
  3141. { DDS_FORMAT_R11G11B10_FLOAT, TextureFormat::RG11B10F, false },
  3142. };
  3143. struct TranslateDdsPixelFormat
  3144. {
  3145. uint32_t m_bitCount;
  3146. uint32_t m_flags;
  3147. uint32_t m_bitmask[4];
  3148. TextureFormat::Enum m_textureFormat;
  3149. };
  3150. static const TranslateDdsPixelFormat s_translateDdsPixelFormat[] =
  3151. {
  3152. { 8, DDPF_LUMINANCE, { 0x000000ff, 0x00000000, 0x00000000, 0x00000000 }, TextureFormat::R8 },
  3153. { 16, DDPF_BUMPDUDV, { 0x000000ff, 0x0000ff00, 0x00000000, 0x00000000 }, TextureFormat::RG8S },
  3154. { 16, DDPF_RGB, { 0x0000ffff, 0x00000000, 0x00000000, 0x00000000 }, TextureFormat::R16U },
  3155. { 16, DDPF_RGB|DDPF_ALPHAPIXELS, { 0x0000000f, 0x000000f0, 0x00000f00, 0x0000f000 }, TextureFormat::BGRA4 },
  3156. { 16, DDPF_RGB|DDPF_ALPHAPIXELS, { 0x00000f00, 0x000000f0, 0x0000000f, 0x0000f000 }, TextureFormat::RGBA4 },
  3157. { 16, DDPF_RGB, { 0x0000001f, 0x000007e0, 0x0000f800, 0x00000000 }, TextureFormat::B5G6R5 },
  3158. { 16, DDPF_RGB, { 0x0000f800, 0x000007e0, 0x0000001f, 0x00000000 }, TextureFormat::R5G6B5 },
  3159. { 16, DDPF_RGB, { 0x0000001f, 0x000003e0, 0x00007c00, 0x00008000 }, TextureFormat::BGR5A1 },
  3160. { 16, DDPF_RGB, { 0x00007c00, 0x000003e0, 0x0000001f, 0x00008000 }, TextureFormat::RGB5A1 },
  3161. { 24, DDPF_RGB, { 0x00ff0000, 0x0000ff00, 0x000000ff, 0x00000000 }, TextureFormat::RGB8 },
  3162. { 24, DDPF_RGB, { 0x000000ff, 0x0000ff00, 0x00ff0000, 0x00000000 }, TextureFormat::RGB8 },
  3163. { 32, DDPF_RGB, { 0x00ff0000, 0x0000ff00, 0x000000ff, 0x00000000 }, TextureFormat::BGRA8 },
  3164. { 32, DDPF_RGB|DDPF_ALPHAPIXELS, { 0x000000ff, 0x0000ff00, 0x00ff0000, 0xff000000 }, TextureFormat::RGBA8 },
  3165. { 32, DDPF_BUMPDUDV, { 0x000000ff, 0x0000ff00, 0x00ff0000, 0xff000000 }, TextureFormat::RGBA8S },
  3166. { 32, DDPF_RGB, { 0x00ff0000, 0x0000ff00, 0x000000ff, 0xff000000 }, TextureFormat::BGRA8 },
  3167. { 32, DDPF_RGB|DDPF_ALPHAPIXELS, { 0x00ff0000, 0x0000ff00, 0x000000ff, 0xff000000 }, TextureFormat::BGRA8 }, // D3DFMT_A8R8G8B8
  3168. { 32, DDPF_RGB|DDPF_ALPHAPIXELS, { 0x00ff0000, 0x0000ff00, 0x000000ff, 0x00000000 }, TextureFormat::BGRA8 }, // D3DFMT_X8R8G8B8
  3169. { 32, DDPF_RGB|DDPF_ALPHAPIXELS, { 0x000003ff, 0x000ffc00, 0x3ff00000, 0xc0000000 }, TextureFormat::RGB10A2 },
  3170. { 32, DDPF_RGB, { 0x0000ffff, 0xffff0000, 0x00000000, 0x00000000 }, TextureFormat::RG16 },
  3171. { 32, DDPF_BUMPDUDV, { 0x0000ffff, 0xffff0000, 0x00000000, 0x00000000 }, TextureFormat::RG16S },
  3172. { 32, DDPF_RGB, { 0xffffffff, 0x00000000, 0x00000000, 0x00000000 }, TextureFormat::R32U },
  3173. };
  3174. bool imageParseDds(ImageContainer& _imageContainer, bx::ReaderSeekerI* _reader, bx::Error* _err)
  3175. {
  3176. BX_ERROR_SCOPE(_err);
  3177. int32_t total = 0;
  3178. uint32_t headerSize;
  3179. total += bx::read(_reader, headerSize, _err);
  3180. if (!_err->isOk()
  3181. || headerSize < DDS_HEADER_SIZE)
  3182. {
  3183. BX_ERROR_SET(_err, BIMG_ERROR, "DDS: Invalid header size.");
  3184. return false;
  3185. }
  3186. uint32_t flags;
  3187. total += bx::read(_reader, flags, _err);
  3188. if (!_err->isOk() )
  3189. {
  3190. return false;
  3191. }
  3192. if ( (flags & (DDSD_CAPS|DDSD_HEIGHT|DDSD_WIDTH|DDSD_PIXELFORMAT) ) != (DDSD_CAPS|DDSD_HEIGHT|DDSD_WIDTH|DDSD_PIXELFORMAT) )
  3193. {
  3194. BX_ERROR_SET(_err, BIMG_ERROR, "DDS: Invalid flags.");
  3195. return false;
  3196. }
  3197. uint32_t height;
  3198. total += bx::read(_reader, height, _err);
  3199. uint32_t width;
  3200. total += bx::read(_reader, width, _err);
  3201. uint32_t pitch;
  3202. total += bx::read(_reader, pitch, _err);
  3203. uint32_t depth;
  3204. total += bx::read(_reader, depth, _err);
  3205. uint32_t mips;
  3206. total += bx::read(_reader, mips, _err);
  3207. bx::skip(_reader, 44); // reserved
  3208. total += 44;
  3209. uint32_t pixelFormatSize;
  3210. total += bx::read(_reader, pixelFormatSize, _err);
  3211. uint32_t pixelFlags;
  3212. total += bx::read(_reader, pixelFlags, _err);
  3213. uint32_t fourcc;
  3214. total += bx::read(_reader, fourcc, _err);
  3215. uint32_t bitCount;
  3216. total += bx::read(_reader, bitCount, _err);
  3217. uint32_t bitmask[4];
  3218. total += bx::read(_reader, bitmask, sizeof(bitmask), _err);
  3219. uint32_t caps[4];
  3220. total += bx::read(_reader, caps, _err);
  3221. bx::skip(_reader, 4);
  3222. total += 4; // reserved
  3223. if (!_err->isOk() )
  3224. {
  3225. return false;
  3226. }
  3227. uint32_t dxgiFormat = 0;
  3228. uint32_t arraySize = 1;
  3229. if (DDPF_FOURCC == (pixelFlags & DDPF_FOURCC)
  3230. && DDS_DX10 == fourcc)
  3231. {
  3232. total += bx::read(_reader, dxgiFormat, _err);
  3233. uint32_t dims;
  3234. total += bx::read(_reader, dims, _err);
  3235. uint32_t miscFlags;
  3236. total += bx::read(_reader, miscFlags, _err);
  3237. total += bx::read(_reader, arraySize, _err);
  3238. uint32_t miscFlags2;
  3239. total += bx::read(_reader, miscFlags2, _err);
  3240. }
  3241. BX_UNUSED(total);
  3242. if (!_err->isOk() )
  3243. {
  3244. return false;
  3245. }
  3246. if ( (caps[0] & DDSCAPS_TEXTURE) == 0)
  3247. {
  3248. BX_ERROR_SET(_err, BIMG_ERROR, "DDS: Unsupported caps.");
  3249. return false;
  3250. }
  3251. bool cubeMap = 0 != (caps[1] & DDSCAPS2_CUBEMAP);
  3252. if (cubeMap)
  3253. {
  3254. if ( (caps[1] & DSCAPS2_CUBEMAP_ALLSIDES) != DSCAPS2_CUBEMAP_ALLSIDES)
  3255. {
  3256. // partial cube map is not supported.
  3257. BX_ERROR_SET(_err, BIMG_ERROR, "DDS: Incomplete cubemap.");
  3258. return false;
  3259. }
  3260. }
  3261. TextureFormat::Enum format = TextureFormat::Unknown;
  3262. bool hasAlpha = pixelFlags & DDPF_ALPHAPIXELS;
  3263. bool srgb = false;
  3264. if (dxgiFormat == 0)
  3265. {
  3266. if (DDPF_FOURCC == (pixelFlags & DDPF_FOURCC) )
  3267. {
  3268. for (uint32_t ii = 0; ii < BX_COUNTOF(s_translateDdsFourccFormat); ++ii)
  3269. {
  3270. if (s_translateDdsFourccFormat[ii].m_format == fourcc)
  3271. {
  3272. format = s_translateDdsFourccFormat[ii].m_textureFormat;
  3273. break;
  3274. }
  3275. }
  3276. }
  3277. else
  3278. {
  3279. for (uint32_t ii = 0; ii < BX_COUNTOF(s_translateDdsPixelFormat); ++ii)
  3280. {
  3281. const TranslateDdsPixelFormat& pf = s_translateDdsPixelFormat[ii];
  3282. if (pf.m_bitCount == bitCount
  3283. && pf.m_flags == pixelFlags
  3284. && pf.m_bitmask[0] == bitmask[0]
  3285. && pf.m_bitmask[1] == bitmask[1]
  3286. && pf.m_bitmask[2] == bitmask[2]
  3287. && pf.m_bitmask[3] == bitmask[3])
  3288. {
  3289. format = pf.m_textureFormat;
  3290. break;
  3291. }
  3292. }
  3293. }
  3294. }
  3295. else
  3296. {
  3297. for (uint32_t ii = 0; ii < BX_COUNTOF(s_translateDxgiFormat); ++ii)
  3298. {
  3299. if (s_translateDxgiFormat[ii].m_format == dxgiFormat)
  3300. {
  3301. format = s_translateDxgiFormat[ii].m_textureFormat;
  3302. srgb = s_translateDxgiFormat[ii].m_srgb;
  3303. break;
  3304. }
  3305. }
  3306. }
  3307. if (TextureFormat::Unknown == format)
  3308. {
  3309. BX_ERROR_SET(_err, BIMG_ERROR, "DDS: Unknown texture format.");
  3310. return false;
  3311. }
  3312. _imageContainer.m_allocator = NULL;
  3313. _imageContainer.m_data = NULL;
  3314. _imageContainer.m_size = 0;
  3315. _imageContainer.m_offset = (uint32_t)bx::seek(_reader);
  3316. _imageContainer.m_width = width;
  3317. _imageContainer.m_height = height;
  3318. _imageContainer.m_depth = depth;
  3319. _imageContainer.m_format = format;
  3320. _imageContainer.m_orientation = Orientation::R0;
  3321. _imageContainer.m_numLayers = uint16_t(arraySize);
  3322. _imageContainer.m_numMips = uint8_t( (caps[0] & DDSCAPS_MIPMAP) ? mips : 1);
  3323. _imageContainer.m_hasAlpha = hasAlpha;
  3324. _imageContainer.m_cubeMap = cubeMap;
  3325. _imageContainer.m_ktx = false;
  3326. _imageContainer.m_ktxLE = false;
  3327. _imageContainer.m_pvr3 = false;
  3328. _imageContainer.m_srgb = srgb;
  3329. return true;
  3330. }
  3331. ImageContainer* imageParseDds(bx::AllocatorI* _allocator, const void* _src, uint32_t _size, bx::Error* _err)
  3332. {
  3333. return imageParseT<DDS_MAGIC, imageParseDds>(_allocator, _src, _size, _err);
  3334. }
  3335. // KTX
  3336. #define KTX_MAGIC BX_MAKEFOURCC(0xAB, 'K', 'T', 'X')
  3337. #define KTX_HEADER_SIZE 64
  3338. #define KTX_ETC1_RGB8_OES 0x8D64
  3339. #define KTX_COMPRESSED_R11_EAC 0x9270
  3340. #define KTX_COMPRESSED_SIGNED_R11_EAC 0x9271
  3341. #define KTX_COMPRESSED_RG11_EAC 0x9272
  3342. #define KTX_COMPRESSED_SIGNED_RG11_EAC 0x9273
  3343. #define KTX_COMPRESSED_RGB8_ETC2 0x9274
  3344. #define KTX_COMPRESSED_SRGB8_ETC2 0x9275
  3345. #define KTX_COMPRESSED_RGB8_PUNCHTHROUGH_ALPHA1_ETC2 0x9276
  3346. #define KTX_COMPRESSED_SRGB8_PUNCHTHROUGH_ALPHA1_ETC2 0x9277
  3347. #define KTX_COMPRESSED_RGBA8_ETC2_EAC 0x9278
  3348. #define KTX_COMPRESSED_SRGB8_ALPHA8_ETC2_EAC 0x9279
  3349. #define KTX_COMPRESSED_RGB_PVRTC_4BPPV1_IMG 0x8C00
  3350. #define KTX_COMPRESSED_RGB_PVRTC_2BPPV1_IMG 0x8C01
  3351. #define KTX_COMPRESSED_RGBA_PVRTC_4BPPV1_IMG 0x8C02
  3352. #define KTX_COMPRESSED_RGBA_PVRTC_2BPPV1_IMG 0x8C03
  3353. #define KTX_COMPRESSED_RGBA_PVRTC_2BPPV2_IMG 0x9137
  3354. #define KTX_COMPRESSED_RGBA_PVRTC_4BPPV2_IMG 0x9138
  3355. #define KTX_COMPRESSED_RGB_S3TC_DXT1_EXT 0x83F0
  3356. #define KTX_COMPRESSED_RGBA_S3TC_DXT1_EXT 0x83F1
  3357. #define KTX_COMPRESSED_RGBA_S3TC_DXT3_EXT 0x83F2
  3358. #define KTX_COMPRESSED_RGBA_S3TC_DXT5_EXT 0x83F3
  3359. #define KTX_COMPRESSED_SRGB_ALPHA_S3TC_DXT1_EXT 0x8C4D
  3360. #define KTX_COMPRESSED_SRGB_ALPHA_S3TC_DXT3_EXT 0x8C4E
  3361. #define KTX_COMPRESSED_SRGB_ALPHA_S3TC_DXT5_EXT 0x8C4F
  3362. #define KTX_COMPRESSED_LUMINANCE_LATC1_EXT 0x8C70
  3363. #define KTX_COMPRESSED_LUMINANCE_ALPHA_LATC2_EXT 0x8C72
  3364. #define KTX_COMPRESSED_RGBA_BPTC_UNORM_ARB 0x8E8C
  3365. #define KTX_COMPRESSED_SRGB_ALPHA_BPTC_UNORM_ARB 0x8E8D
  3366. #define KTX_COMPRESSED_RGB_BPTC_SIGNED_FLOAT_ARB 0x8E8E
  3367. #define KTX_COMPRESSED_RGB_BPTC_UNSIGNED_FLOAT_ARB 0x8E8F
  3368. #define KTX_COMPRESSED_SRGB_PVRTC_2BPPV1_EXT 0x8A54
  3369. #define KTX_COMPRESSED_SRGB_PVRTC_4BPPV1_EXT 0x8A55
  3370. #define KTX_COMPRESSED_SRGB_ALPHA_PVRTC_2BPPV1_EXT 0x8A56
  3371. #define KTX_COMPRESSED_SRGB_ALPHA_PVRTC_4BPPV1_EXT 0x8A57
  3372. #define KTX_ATC_RGB_AMD 0x8C92
  3373. #define KTX_ATC_RGBA_EXPLICIT_ALPHA_AMD 0x8C93
  3374. #define KTX_ATC_RGBA_INTERPOLATED_ALPHA_AMD 0x87EE
  3375. #define KTX_COMPRESSED_RGBA_ASTC_4x4_KHR 0x93B0
  3376. #define KTX_COMPRESSED_RGBA_ASTC_5x4_KHR 0x93B1
  3377. #define KTX_COMPRESSED_RGBA_ASTC_5x5_KHR 0x93B2
  3378. #define KTX_COMPRESSED_RGBA_ASTC_6x5_KHR 0x93B3
  3379. #define KTX_COMPRESSED_RGBA_ASTC_6x6_KHR 0x93B4
  3380. #define KTX_COMPRESSED_RGBA_ASTC_8x5_KHR 0x93B5
  3381. #define KTX_COMPRESSED_RGBA_ASTC_8x6_KHR 0x93B6
  3382. #define KTX_COMPRESSED_RGBA_ASTC_8x8_KHR 0x93B7
  3383. #define KTX_COMPRESSED_RGBA_ASTC_10x5_KHR 0x93B8
  3384. #define KTX_COMPRESSED_RGBA_ASTC_10x6_KHR 0x93B9
  3385. #define KTX_COMPRESSED_RGBA_ASTC_10x8_KHR 0x93BA
  3386. #define KTX_COMPRESSED_RGBA_ASTC_10x10_KHR 0x93BB
  3387. #define KTX_COMPRESSED_RGBA_ASTC_12x10_KHR 0x93BC
  3388. #define KTX_COMPRESSED_RGBA_ASTC_12x12_KHR 0x93BD
  3389. #define KTX_COMPRESSED_SRGB8_ALPHA8_ASTC_4x4_KHR 0x93D0
  3390. #define KTX_COMPRESSED_SRGB8_ALPHA8_ASTC_5x4_KHR 0x93D1
  3391. #define KTX_COMPRESSED_SRGB8_ALPHA8_ASTC_5x5_KHR 0x93D2
  3392. #define KTX_COMPRESSED_SRGB8_ALPHA8_ASTC_6x5_KHR 0x93D3
  3393. #define KTX_COMPRESSED_SRGB8_ALPHA8_ASTC_6x6_KHR 0x93D4
  3394. #define KTX_COMPRESSED_SRGB8_ALPHA8_ASTC_8x5_KHR 0x93D5
  3395. #define KTX_COMPRESSED_SRGB8_ALPHA8_ASTC_8x6_KHR 0x93D6
  3396. #define KTX_COMPRESSED_SRGB8_ALPHA8_ASTC_8x8_KHR 0x93D7
  3397. #define KTX_COMPRESSED_SRGB8_ALPHA8_ASTC_10x5_KHR 0x93D8
  3398. #define KTX_COMPRESSED_SRGB8_ALPHA8_ASTC_10x6_KHR 0x93D9
  3399. #define KTX_COMPRESSED_SRGB8_ALPHA8_ASTC_10x8_KHR 0x93DA
  3400. #define KTX_COMPRESSED_SRGB8_ALPHA8_ASTC_10x10_KHR 0x93DB
  3401. #define KTX_COMPRESSED_SRGB8_ALPHA8_ASTC_12x10_KHR 0x93DC
  3402. #define KTX_COMPRESSED_SRGB8_ALPHA8_ASTC_12x12_KHR 0x93DD
  3403. #define KTX_A8 0x803C
  3404. #define KTX_R8 0x8229
  3405. #define KTX_R16 0x822A
  3406. #define KTX_RG8 0x822B
  3407. #define KTX_RG16 0x822C
  3408. #define KTX_R16F 0x822D
  3409. #define KTX_R32F 0x822E
  3410. #define KTX_RG16F 0x822F
  3411. #define KTX_RG32F 0x8230
  3412. #define KTX_RGBA8 0x8058
  3413. #define KTX_RGBA16 0x805B
  3414. #define KTX_RGBA16F 0x881A
  3415. #define KTX_R32UI 0x8236
  3416. #define KTX_RG32UI 0x823C
  3417. #define KTX_RGBA32UI 0x8D70
  3418. #define KTX_RGBA32F 0x8814
  3419. #define KTX_RGB565 0x8D62
  3420. #define KTX_RGBA4 0x8056
  3421. #define KTX_RGB5_A1 0x8057
  3422. #define KTX_RGB10_A2 0x8059
  3423. #define KTX_R8I 0x8231
  3424. #define KTX_R8UI 0x8232
  3425. #define KTX_R16I 0x8233
  3426. #define KTX_R16UI 0x8234
  3427. #define KTX_R32I 0x8235
  3428. #define KTX_R32UI 0x8236
  3429. #define KTX_RG8I 0x8237
  3430. #define KTX_RG8UI 0x8238
  3431. #define KTX_RG16I 0x8239
  3432. #define KTX_RG16UI 0x823A
  3433. #define KTX_RG32I 0x823B
  3434. #define KTX_RG32UI 0x823C
  3435. #define KTX_R8_SNORM 0x8F94
  3436. #define KTX_RG8_SNORM 0x8F95
  3437. #define KTX_RGB8_SNORM 0x8F96
  3438. #define KTX_RGBA8_SNORM 0x8F97
  3439. #define KTX_R16_SNORM 0x8F98
  3440. #define KTX_RG16_SNORM 0x8F99
  3441. #define KTX_RGB16_SNORM 0x8F9A
  3442. #define KTX_RGBA16_SNORM 0x8F9B
  3443. #define KTX_SRGB8 0x8C41
  3444. #define KTX_SRGB8_ALPHA8 0x8C43
  3445. #define KTX_RGBA32UI 0x8D70
  3446. #define KTX_RGB32UI 0x8D71
  3447. #define KTX_RGBA16UI 0x8D76
  3448. #define KTX_RGB16UI 0x8D77
  3449. #define KTX_RGBA8UI 0x8D7C
  3450. #define KTX_RGB8UI 0x8D7D
  3451. #define KTX_RGBA32I 0x8D82
  3452. #define KTX_RGB32I 0x8D83
  3453. #define KTX_RGBA16I 0x8D88
  3454. #define KTX_RGB16I 0x8D89
  3455. #define KTX_RGBA8I 0x8D8E
  3456. #define KTX_RGB8 0x8051
  3457. #define KTX_RGB8I 0x8D8F
  3458. #define KTX_RGB9_E5 0x8C3D
  3459. #define KTX_R11F_G11F_B10F 0x8C3A
  3460. #define KTX_ZERO 0
  3461. #define KTX_RED 0x1903
  3462. #define KTX_ALPHA 0x1906
  3463. #define KTX_RGB 0x1907
  3464. #define KTX_RGBA 0x1908
  3465. #define KTX_BGRA 0x80E1
  3466. #define KTX_RG 0x8227
  3467. #define KTX_BYTE 0x1400
  3468. #define KTX_UNSIGNED_BYTE 0x1401
  3469. #define KTX_SHORT 0x1402
  3470. #define KTX_UNSIGNED_SHORT 0x1403
  3471. #define KTX_INT 0x1404
  3472. #define KTX_UNSIGNED_INT 0x1405
  3473. #define KTX_FLOAT 0x1406
  3474. #define KTX_HALF_FLOAT 0x140B
  3475. #define KTX_UNSIGNED_INT_5_9_9_9_REV 0x8C3E
  3476. #define KTX_UNSIGNED_SHORT_5_6_5 0x8363
  3477. #define KTX_UNSIGNED_SHORT_4_4_4_4 0x8033
  3478. #define KTX_UNSIGNED_SHORT_5_5_5_1 0x8034
  3479. #define KTX_UNSIGNED_INT_2_10_10_10_REV 0x8368
  3480. #define KTX_UNSIGNED_INT_10F_11F_11F_REV 0x8C3B
  3481. struct KtxFormatInfo
  3482. {
  3483. uint32_t m_internalFmt;
  3484. uint32_t m_internalFmtSrgb;
  3485. uint32_t m_fmt;
  3486. uint32_t m_type;
  3487. };
  3488. static const KtxFormatInfo s_translateKtxFormat[] =
  3489. {
  3490. { KTX_COMPRESSED_RGBA_S3TC_DXT1_EXT, KTX_COMPRESSED_SRGB_ALPHA_S3TC_DXT1_EXT, KTX_COMPRESSED_RGBA_S3TC_DXT1_EXT, KTX_ZERO, }, // BC1
  3491. { KTX_COMPRESSED_RGBA_S3TC_DXT3_EXT, KTX_COMPRESSED_SRGB_ALPHA_S3TC_DXT3_EXT, KTX_COMPRESSED_RGBA_S3TC_DXT3_EXT, KTX_ZERO, }, // BC2
  3492. { KTX_COMPRESSED_RGBA_S3TC_DXT5_EXT, KTX_COMPRESSED_SRGB_ALPHA_S3TC_DXT5_EXT, KTX_COMPRESSED_RGBA_S3TC_DXT5_EXT, KTX_ZERO, }, // BC3
  3493. { KTX_COMPRESSED_LUMINANCE_LATC1_EXT, KTX_ZERO, KTX_COMPRESSED_LUMINANCE_LATC1_EXT, KTX_ZERO, }, // BC4
  3494. { KTX_COMPRESSED_LUMINANCE_ALPHA_LATC2_EXT, KTX_ZERO, KTX_COMPRESSED_LUMINANCE_ALPHA_LATC2_EXT, KTX_ZERO, }, // BC5
  3495. { KTX_COMPRESSED_RGB_BPTC_SIGNED_FLOAT_ARB, KTX_ZERO, KTX_COMPRESSED_RGB_BPTC_SIGNED_FLOAT_ARB, KTX_ZERO, }, // BC6H
  3496. { KTX_COMPRESSED_RGBA_BPTC_UNORM_ARB, KTX_ZERO, KTX_COMPRESSED_RGBA_BPTC_UNORM_ARB, KTX_ZERO, }, // BC7
  3497. { KTX_ETC1_RGB8_OES, KTX_ZERO, KTX_ETC1_RGB8_OES, KTX_ZERO, }, // ETC1
  3498. { KTX_COMPRESSED_RGB8_ETC2, KTX_ZERO, KTX_COMPRESSED_RGB8_ETC2, KTX_ZERO, }, // ETC2
  3499. { KTX_COMPRESSED_RGBA8_ETC2_EAC, KTX_COMPRESSED_SRGB8_ETC2, KTX_COMPRESSED_RGBA8_ETC2_EAC, KTX_ZERO, }, // ETC2A
  3500. { KTX_COMPRESSED_RGB8_PUNCHTHROUGH_ALPHA1_ETC2, KTX_COMPRESSED_SRGB8_PUNCHTHROUGH_ALPHA1_ETC2, KTX_COMPRESSED_RGB8_PUNCHTHROUGH_ALPHA1_ETC2, KTX_ZERO, }, // ETC2A1
  3501. { KTX_COMPRESSED_RGB_PVRTC_2BPPV1_IMG, KTX_COMPRESSED_SRGB_PVRTC_2BPPV1_EXT, KTX_COMPRESSED_RGB_PVRTC_2BPPV1_IMG, KTX_ZERO, }, // PTC12
  3502. { KTX_COMPRESSED_RGB_PVRTC_4BPPV1_IMG, KTX_COMPRESSED_SRGB_PVRTC_4BPPV1_EXT, KTX_COMPRESSED_RGB_PVRTC_4BPPV1_IMG, KTX_ZERO, }, // PTC14
  3503. { KTX_COMPRESSED_RGBA_PVRTC_2BPPV1_IMG, KTX_COMPRESSED_SRGB_ALPHA_PVRTC_2BPPV1_EXT, KTX_COMPRESSED_RGBA_PVRTC_2BPPV1_IMG, KTX_ZERO, }, // PTC12A
  3504. { KTX_COMPRESSED_RGBA_PVRTC_4BPPV1_IMG, KTX_COMPRESSED_SRGB_ALPHA_PVRTC_4BPPV1_EXT, KTX_COMPRESSED_RGBA_PVRTC_4BPPV1_IMG, KTX_ZERO, }, // PTC14A
  3505. { KTX_COMPRESSED_RGBA_PVRTC_2BPPV2_IMG, KTX_ZERO, KTX_COMPRESSED_RGBA_PVRTC_2BPPV2_IMG, KTX_ZERO, }, // PTC22
  3506. { KTX_COMPRESSED_RGBA_PVRTC_4BPPV2_IMG, KTX_ZERO, KTX_COMPRESSED_RGBA_PVRTC_4BPPV2_IMG, KTX_ZERO, }, // PTC24
  3507. { KTX_ATC_RGB_AMD, KTX_ZERO, KTX_ATC_RGB_AMD, KTX_ZERO, }, // ATC
  3508. { KTX_ATC_RGBA_EXPLICIT_ALPHA_AMD, KTX_ZERO, KTX_ATC_RGBA_EXPLICIT_ALPHA_AMD, KTX_ZERO, }, // ATCE
  3509. { KTX_ATC_RGBA_INTERPOLATED_ALPHA_AMD, KTX_ZERO, KTX_ATC_RGBA_INTERPOLATED_ALPHA_AMD, KTX_ZERO, }, // ATCI
  3510. { KTX_COMPRESSED_RGBA_ASTC_4x4_KHR, KTX_COMPRESSED_SRGB8_ALPHA8_ASTC_4x4_KHR, KTX_COMPRESSED_RGBA_ASTC_4x4_KHR, KTX_ZERO, }, // ASTC4x4
  3511. { KTX_COMPRESSED_RGBA_ASTC_5x4_KHR, KTX_COMPRESSED_SRGB8_ALPHA8_ASTC_5x4_KHR, KTX_COMPRESSED_RGBA_ASTC_5x4_KHR, KTX_ZERO, }, // ASTC5x4
  3512. { KTX_COMPRESSED_RGBA_ASTC_5x5_KHR, KTX_COMPRESSED_SRGB8_ALPHA8_ASTC_5x5_KHR, KTX_COMPRESSED_RGBA_ASTC_5x5_KHR, KTX_ZERO, }, // ASTC5x5
  3513. { KTX_COMPRESSED_RGBA_ASTC_6x5_KHR, KTX_COMPRESSED_SRGB8_ALPHA8_ASTC_6x5_KHR, KTX_COMPRESSED_RGBA_ASTC_6x5_KHR, KTX_ZERO, }, // ASTC6x5
  3514. { KTX_COMPRESSED_RGBA_ASTC_6x6_KHR, KTX_COMPRESSED_SRGB8_ALPHA8_ASTC_6x6_KHR, KTX_COMPRESSED_RGBA_ASTC_6x6_KHR, KTX_ZERO, }, // ASTC6x6
  3515. { KTX_COMPRESSED_RGBA_ASTC_8x5_KHR, KTX_COMPRESSED_SRGB8_ALPHA8_ASTC_8x5_KHR, KTX_COMPRESSED_RGBA_ASTC_8x5_KHR, KTX_ZERO, }, // ASTC8x5
  3516. { KTX_COMPRESSED_RGBA_ASTC_8x6_KHR, KTX_COMPRESSED_SRGB8_ALPHA8_ASTC_8x6_KHR, KTX_COMPRESSED_RGBA_ASTC_8x6_KHR, KTX_ZERO, }, // ASTC8x6
  3517. { KTX_COMPRESSED_RGBA_ASTC_8x8_KHR, KTX_COMPRESSED_SRGB8_ALPHA8_ASTC_8x8_KHR, KTX_COMPRESSED_RGBA_ASTC_8x8_KHR, KTX_ZERO, }, // ASTC8x8
  3518. { KTX_COMPRESSED_RGBA_ASTC_10x5_KHR, KTX_COMPRESSED_SRGB8_ALPHA8_ASTC_10x5_KHR, KTX_COMPRESSED_RGBA_ASTC_10x5_KHR, KTX_ZERO, }, // ASTC10x5
  3519. { KTX_COMPRESSED_RGBA_ASTC_10x6_KHR, KTX_COMPRESSED_SRGB8_ALPHA8_ASTC_10x6_KHR, KTX_COMPRESSED_RGBA_ASTC_10x6_KHR, KTX_ZERO, }, // ASTC10x6
  3520. { KTX_COMPRESSED_RGBA_ASTC_10x8_KHR, KTX_COMPRESSED_SRGB8_ALPHA8_ASTC_10x8_KHR, KTX_COMPRESSED_RGBA_ASTC_10x8_KHR, KTX_ZERO, }, // ASTC10x8
  3521. { KTX_COMPRESSED_RGBA_ASTC_10x10_KHR, KTX_COMPRESSED_SRGB8_ALPHA8_ASTC_10x10_KHR, KTX_COMPRESSED_RGBA_ASTC_10x10_KHR, KTX_ZERO, }, // ASTC10x10
  3522. { KTX_COMPRESSED_RGBA_ASTC_12x10_KHR, KTX_COMPRESSED_SRGB8_ALPHA8_ASTC_12x10_KHR, KTX_COMPRESSED_RGBA_ASTC_12x10_KHR, KTX_ZERO, }, // ASTC12x10
  3523. { KTX_COMPRESSED_RGBA_ASTC_12x12_KHR, KTX_COMPRESSED_SRGB8_ALPHA8_ASTC_12x12_KHR, KTX_COMPRESSED_RGBA_ASTC_12x12_KHR, KTX_ZERO, }, // ASTC12x12
  3524. { KTX_ZERO, KTX_ZERO, KTX_ZERO, KTX_ZERO, }, // Unknown
  3525. { KTX_ZERO, KTX_ZERO, KTX_ZERO, KTX_ZERO, }, // R1
  3526. { KTX_ALPHA, KTX_ZERO, KTX_ALPHA, KTX_UNSIGNED_BYTE, }, // A8
  3527. { KTX_R8, KTX_ZERO, KTX_RED, KTX_UNSIGNED_BYTE, }, // R8
  3528. { KTX_R8I, KTX_ZERO, KTX_RED, KTX_BYTE, }, // R8S
  3529. { KTX_R8UI, KTX_ZERO, KTX_RED, KTX_UNSIGNED_BYTE, }, // R8S
  3530. { KTX_R8_SNORM, KTX_ZERO, KTX_RED, KTX_BYTE, }, // R8S
  3531. { KTX_R16, KTX_ZERO, KTX_RED, KTX_UNSIGNED_SHORT, }, // R16
  3532. { KTX_R16I, KTX_ZERO, KTX_RED, KTX_SHORT, }, // R16I
  3533. { KTX_R16UI, KTX_ZERO, KTX_RED, KTX_UNSIGNED_SHORT, }, // R16U
  3534. { KTX_R16F, KTX_ZERO, KTX_RED, KTX_HALF_FLOAT, }, // R16F
  3535. { KTX_R16_SNORM, KTX_ZERO, KTX_RED, KTX_SHORT, }, // R16S
  3536. { KTX_R32I, KTX_ZERO, KTX_RED, KTX_INT, }, // R32I
  3537. { KTX_R32UI, KTX_ZERO, KTX_RED, KTX_UNSIGNED_INT, }, // R32U
  3538. { KTX_R32F, KTX_ZERO, KTX_RED, KTX_FLOAT, }, // R32F
  3539. { KTX_RG8, KTX_ZERO, KTX_RG, KTX_UNSIGNED_BYTE, }, // RG8
  3540. { KTX_RG8I, KTX_ZERO, KTX_RG, KTX_BYTE, }, // RG8I
  3541. { KTX_RG8UI, KTX_ZERO, KTX_RG, KTX_UNSIGNED_BYTE, }, // RG8U
  3542. { KTX_RG8_SNORM, KTX_ZERO, KTX_RG, KTX_BYTE, }, // RG8S
  3543. { KTX_RG16, KTX_ZERO, KTX_RG, KTX_UNSIGNED_SHORT, }, // RG16
  3544. { KTX_RG16I, KTX_ZERO, KTX_RG, KTX_SHORT, }, // RG16
  3545. { KTX_RG16UI, KTX_ZERO, KTX_RG, KTX_UNSIGNED_SHORT, }, // RG16
  3546. { KTX_RG16F, KTX_ZERO, KTX_RG, KTX_FLOAT, }, // RG16F
  3547. { KTX_RG16_SNORM, KTX_ZERO, KTX_RG, KTX_SHORT, }, // RG16S
  3548. { KTX_RG32I, KTX_ZERO, KTX_RG, KTX_INT, }, // RG32I
  3549. { KTX_RG32UI, KTX_ZERO, KTX_RG, KTX_UNSIGNED_INT, }, // RG32U
  3550. { KTX_RG32F, KTX_ZERO, KTX_RG, KTX_FLOAT, }, // RG32F
  3551. { KTX_RGB8, KTX_SRGB8, KTX_RGB, KTX_UNSIGNED_BYTE, }, // RGB8
  3552. { KTX_RGB8I, KTX_ZERO, KTX_RGB, KTX_BYTE, }, // RGB8I
  3553. { KTX_RGB8UI, KTX_ZERO, KTX_RGB, KTX_UNSIGNED_BYTE, }, // RGB8U
  3554. { KTX_RGB8_SNORM, KTX_ZERO, KTX_RGB, KTX_BYTE, }, // RGB8S
  3555. { KTX_RGB9_E5, KTX_ZERO, KTX_RGB, KTX_UNSIGNED_INT_5_9_9_9_REV, }, // RGB9E5F
  3556. { KTX_BGRA, KTX_SRGB8_ALPHA8, KTX_BGRA, KTX_UNSIGNED_BYTE, }, // BGRA8
  3557. { KTX_RGBA8, KTX_SRGB8_ALPHA8, KTX_RGBA, KTX_UNSIGNED_BYTE, }, // RGBA8
  3558. { KTX_RGBA8I, KTX_ZERO, KTX_RGBA, KTX_BYTE, }, // RGBA8I
  3559. { KTX_RGBA8UI, KTX_ZERO, KTX_RGBA, KTX_UNSIGNED_BYTE, }, // RGBA8U
  3560. { KTX_RGBA8_SNORM, KTX_ZERO, KTX_RGBA, KTX_BYTE, }, // RGBA8S
  3561. { KTX_RGBA16, KTX_ZERO, KTX_RGBA, KTX_UNSIGNED_SHORT, }, // RGBA16
  3562. { KTX_RGBA16I, KTX_ZERO, KTX_RGBA, KTX_SHORT, }, // RGBA16I
  3563. { KTX_RGBA16UI, KTX_ZERO, KTX_RGBA, KTX_UNSIGNED_SHORT, }, // RGBA16U
  3564. { KTX_RGBA16F, KTX_ZERO, KTX_RGBA, KTX_HALF_FLOAT, }, // RGBA16F
  3565. { KTX_RGBA16_SNORM, KTX_ZERO, KTX_RGBA, KTX_SHORT, }, // RGBA16S
  3566. { KTX_RGBA32I, KTX_ZERO, KTX_RGBA, KTX_INT, }, // RGBA32I
  3567. { KTX_RGBA32UI, KTX_ZERO, KTX_RGBA, KTX_UNSIGNED_INT, }, // RGBA32U
  3568. { KTX_RGBA32F, KTX_ZERO, KTX_RGBA, KTX_FLOAT, }, // RGBA32F
  3569. { KTX_RGB565, KTX_ZERO, KTX_RGB, KTX_UNSIGNED_SHORT_5_6_5, }, // B5G6R5
  3570. { KTX_RGB565, KTX_ZERO, KTX_RGB, KTX_UNSIGNED_SHORT_5_6_5, }, // R5G6B5
  3571. { KTX_RGBA4, KTX_ZERO, KTX_BGRA, KTX_UNSIGNED_SHORT_4_4_4_4, }, // BGRA4
  3572. { KTX_RGBA4, KTX_ZERO, KTX_RGBA, KTX_UNSIGNED_SHORT_4_4_4_4, }, // RGBA4
  3573. { KTX_RGB5_A1, KTX_ZERO, KTX_BGRA, KTX_UNSIGNED_SHORT_5_5_5_1, }, // BGR5A1
  3574. { KTX_RGB5_A1, KTX_ZERO, KTX_RGBA, KTX_UNSIGNED_SHORT_5_5_5_1, }, // RGB5A1
  3575. { KTX_RGB10_A2, KTX_ZERO, KTX_RGBA, KTX_UNSIGNED_INT_2_10_10_10_REV, }, // RGB10A2
  3576. { KTX_R11F_G11F_B10F, KTX_ZERO, KTX_RGB, KTX_UNSIGNED_INT_10F_11F_11F_REV, }, // RG11B10F
  3577. };
  3578. BX_STATIC_ASSERT(TextureFormat::UnknownDepth == BX_COUNTOF(s_translateKtxFormat) );
  3579. struct KtxFormatInfo2
  3580. {
  3581. uint32_t m_internalFmt;
  3582. TextureFormat::Enum m_format;
  3583. };
  3584. static const KtxFormatInfo2 s_translateKtxFormat2[] =
  3585. {
  3586. { KTX_A8, TextureFormat::A8 },
  3587. { KTX_RED, TextureFormat::R8 },
  3588. { KTX_RGB, TextureFormat::RGB8 },
  3589. { KTX_RGBA, TextureFormat::RGBA8 },
  3590. { KTX_COMPRESSED_RGB_S3TC_DXT1_EXT, TextureFormat::BC1 },
  3591. };
  3592. bool imageParseKtx(ImageContainer& _imageContainer, bx::ReaderSeekerI* _reader, bx::Error* _err)
  3593. {
  3594. BX_ERROR_SCOPE(_err);
  3595. uint8_t identifier[8];
  3596. bx::read(_reader, identifier, _err);
  3597. if (identifier[1] != '1'
  3598. && identifier[2] != '1')
  3599. {
  3600. BX_ERROR_SET(_err, BIMG_ERROR, "KTX: Unrecognized version.");
  3601. return false;
  3602. }
  3603. uint32_t endianness;
  3604. bx::read(_reader, endianness, _err);
  3605. bool fromLittleEndian = 0x04030201 == endianness;
  3606. uint32_t glType;
  3607. bx::readHE(_reader, glType, fromLittleEndian, _err);
  3608. uint32_t glTypeSize;
  3609. bx::readHE(_reader, glTypeSize, fromLittleEndian, _err);
  3610. uint32_t glFormat;
  3611. bx::readHE(_reader, glFormat, fromLittleEndian, _err);
  3612. uint32_t glInternalFormat;
  3613. bx::readHE(_reader, glInternalFormat, fromLittleEndian, _err);
  3614. uint32_t glBaseInternalFormat;
  3615. bx::readHE(_reader, glBaseInternalFormat, fromLittleEndian, _err);
  3616. uint32_t width;
  3617. bx::readHE(_reader, width, fromLittleEndian, _err);
  3618. uint32_t height;
  3619. bx::readHE(_reader, height, fromLittleEndian, _err);
  3620. uint32_t depth;
  3621. bx::readHE(_reader, depth, fromLittleEndian, _err);
  3622. uint32_t numberOfArrayElements;
  3623. bx::readHE(_reader, numberOfArrayElements, fromLittleEndian, _err);
  3624. uint32_t numFaces;
  3625. bx::readHE(_reader, numFaces, fromLittleEndian, _err);
  3626. uint32_t numMips;
  3627. bx::readHE(_reader, numMips, fromLittleEndian, _err);
  3628. uint32_t metaDataSize;
  3629. bx::readHE(_reader, metaDataSize, fromLittleEndian, _err);
  3630. if (!_err->isOk() )
  3631. {
  3632. return false;
  3633. }
  3634. // skip meta garbage...
  3635. int64_t offset = bx::skip(_reader, metaDataSize);
  3636. TextureFormat::Enum format = TextureFormat::Unknown;
  3637. bool hasAlpha = false;
  3638. bool srgb = false;
  3639. for (uint32_t ii = 0; ii < BX_COUNTOF(s_translateKtxFormat); ++ii)
  3640. {
  3641. if (s_translateKtxFormat[ii].m_internalFmt == glInternalFormat)
  3642. {
  3643. format = TextureFormat::Enum(ii);
  3644. break;
  3645. }
  3646. if (s_translateKtxFormat[ii].m_internalFmtSrgb == glInternalFormat
  3647. && s_translateKtxFormat[ii].m_fmt == glBaseInternalFormat)
  3648. {
  3649. format = TextureFormat::Enum(ii);
  3650. srgb = true;
  3651. break;
  3652. }
  3653. }
  3654. if (TextureFormat::Unknown == format)
  3655. {
  3656. for (uint32_t ii = 0; ii < BX_COUNTOF(s_translateKtxFormat2); ++ii)
  3657. {
  3658. if (s_translateKtxFormat2[ii].m_internalFmt == glInternalFormat)
  3659. {
  3660. format = s_translateKtxFormat2[ii].m_format;
  3661. break;
  3662. }
  3663. }
  3664. }
  3665. _imageContainer.m_allocator = NULL;
  3666. _imageContainer.m_data = NULL;
  3667. _imageContainer.m_size = 0;
  3668. _imageContainer.m_offset = (uint32_t)offset;
  3669. _imageContainer.m_width = width;
  3670. _imageContainer.m_height = height;
  3671. _imageContainer.m_depth = depth;
  3672. _imageContainer.m_format = format;
  3673. _imageContainer.m_orientation = Orientation::R0;
  3674. _imageContainer.m_numLayers = uint16_t(bx::max<uint32_t>(numberOfArrayElements, 1) );
  3675. _imageContainer.m_numMips = uint8_t(bx::max<uint32_t>(numMips, 1) );
  3676. _imageContainer.m_hasAlpha = hasAlpha;
  3677. _imageContainer.m_cubeMap = numFaces > 1;
  3678. _imageContainer.m_ktx = true;
  3679. _imageContainer.m_ktxLE = fromLittleEndian;
  3680. _imageContainer.m_pvr3 = false;
  3681. _imageContainer.m_srgb = srgb;
  3682. if (TextureFormat::Unknown == format)
  3683. {
  3684. BX_ERROR_SET(_err, BIMG_ERROR, "KTX: Unrecognized image format.");
  3685. return false;
  3686. }
  3687. return true;
  3688. }
  3689. ImageContainer* imageParseKtx(bx::AllocatorI* _allocator, const void* _src, uint32_t _size, bx::Error* _err)
  3690. {
  3691. return imageParseT<KTX_MAGIC, imageParseKtx>(_allocator, _src, _size, _err);
  3692. }
  3693. // PVR3
  3694. #define PVR3_MAKE8CC(_a, _b, _c, _d, _e, _f, _g, _h) (uint64_t(BX_MAKEFOURCC(_a, _b, _c, _d) ) | (uint64_t(BX_MAKEFOURCC(_e, _f, _g, _h) )<<32) )
  3695. #define PVR3_MAGIC BX_MAKEFOURCC('P', 'V', 'R', 3)
  3696. #define PVR3_HEADER_SIZE 52
  3697. #define PVR3_PVRTC1_2BPP_RGB 0
  3698. #define PVR3_PVRTC1_2BPP_RGBA 1
  3699. #define PVR3_PVRTC1_4BPP_RGB 2
  3700. #define PVR3_PVRTC1_4BPP_RGBA 3
  3701. #define PVR3_PVRTC2_2BPP_RGBA 4
  3702. #define PVR3_PVRTC2_4BPP_RGBA 5
  3703. #define PVR3_ETC1 6
  3704. #define PVR3_DXT1 7
  3705. #define PVR3_DXT2 8
  3706. #define PVR3_DXT3 9
  3707. #define PVR3_DXT4 10
  3708. #define PVR3_DXT5 11
  3709. #define PVR3_BC4 12
  3710. #define PVR3_BC5 13
  3711. #define PVR3_R8 PVR3_MAKE8CC('r', 0, 0, 0, 8, 0, 0, 0)
  3712. #define PVR3_R16 PVR3_MAKE8CC('r', 0, 0, 0, 16, 0, 0, 0)
  3713. #define PVR3_R32 PVR3_MAKE8CC('r', 0, 0, 0, 32, 0, 0, 0)
  3714. #define PVR3_RG8 PVR3_MAKE8CC('r', 'g', 0, 0, 8, 8, 0, 0)
  3715. #define PVR3_RG16 PVR3_MAKE8CC('r', 'g', 0, 0, 16, 16, 0, 0)
  3716. #define PVR3_RG32 PVR3_MAKE8CC('r', 'g', 0, 0, 32, 32, 0, 0)
  3717. #define PVR3_BGRA8 PVR3_MAKE8CC('b', 'g', 'r', 'a', 8, 8, 8, 8)
  3718. #define PVR3_RGBA16 PVR3_MAKE8CC('r', 'g', 'b', 'a', 16, 16, 16, 16)
  3719. #define PVR3_RGBA32 PVR3_MAKE8CC('r', 'g', 'b', 'a', 32, 32, 32, 32)
  3720. #define PVR3_BGR565 PVR3_MAKE8CC('b', 'g', 'r', 0, 5, 6, 5, 0)
  3721. #define PVR3_RGB565 PVR3_MAKE8CC('r', 'g', 'b', 0, 5, 6, 5, 0)
  3722. #define PVR3_BGRA4 PVR3_MAKE8CC('b', 'g', 'r', 'a', 4, 4, 4, 4)
  3723. #define PVR3_RGBA4 PVR3_MAKE8CC('r', 'g', 'b', 'a', 4, 4, 4, 4)
  3724. #define PVR3_BGRA51 PVR3_MAKE8CC('b', 'g', 'r', 'a', 5, 5, 5, 1)
  3725. #define PVR3_RGBA51 PVR3_MAKE8CC('r', 'g', 'b', 'a', 5, 5, 5, 1)
  3726. #define PVR3_RGB10A2 PVR3_MAKE8CC('r', 'g', 'b', 'a', 10, 10, 10, 2)
  3727. #define PVR3_CHANNEL_TYPE_ANY UINT32_MAX
  3728. #define PVR3_CHANNEL_TYPE_FLOAT UINT32_C(12)
  3729. struct TranslatePvr3Format
  3730. {
  3731. uint64_t m_format;
  3732. uint32_t m_channelTypeMask;
  3733. TextureFormat::Enum m_textureFormat;
  3734. };
  3735. static const TranslatePvr3Format s_translatePvr3Format[] =
  3736. {
  3737. { PVR3_PVRTC1_2BPP_RGB, PVR3_CHANNEL_TYPE_ANY, TextureFormat::PTC12 },
  3738. { PVR3_PVRTC1_2BPP_RGBA, PVR3_CHANNEL_TYPE_ANY, TextureFormat::PTC12A },
  3739. { PVR3_PVRTC1_4BPP_RGB, PVR3_CHANNEL_TYPE_ANY, TextureFormat::PTC14 },
  3740. { PVR3_PVRTC1_4BPP_RGBA, PVR3_CHANNEL_TYPE_ANY, TextureFormat::PTC14A },
  3741. { PVR3_PVRTC2_2BPP_RGBA, PVR3_CHANNEL_TYPE_ANY, TextureFormat::PTC22 },
  3742. { PVR3_PVRTC2_4BPP_RGBA, PVR3_CHANNEL_TYPE_ANY, TextureFormat::PTC24 },
  3743. { PVR3_ETC1, PVR3_CHANNEL_TYPE_ANY, TextureFormat::ETC1 },
  3744. { PVR3_DXT1, PVR3_CHANNEL_TYPE_ANY, TextureFormat::BC1 },
  3745. { PVR3_DXT2, PVR3_CHANNEL_TYPE_ANY, TextureFormat::BC2 },
  3746. { PVR3_DXT3, PVR3_CHANNEL_TYPE_ANY, TextureFormat::BC2 },
  3747. { PVR3_DXT4, PVR3_CHANNEL_TYPE_ANY, TextureFormat::BC3 },
  3748. { PVR3_DXT5, PVR3_CHANNEL_TYPE_ANY, TextureFormat::BC3 },
  3749. { PVR3_BC4, PVR3_CHANNEL_TYPE_ANY, TextureFormat::BC4 },
  3750. { PVR3_BC5, PVR3_CHANNEL_TYPE_ANY, TextureFormat::BC5 },
  3751. { PVR3_R8, PVR3_CHANNEL_TYPE_ANY, TextureFormat::R8 },
  3752. { PVR3_R16, PVR3_CHANNEL_TYPE_ANY, TextureFormat::R16U },
  3753. { PVR3_R16, PVR3_CHANNEL_TYPE_FLOAT, TextureFormat::R16F },
  3754. { PVR3_R32, PVR3_CHANNEL_TYPE_ANY, TextureFormat::R32U },
  3755. { PVR3_R32, PVR3_CHANNEL_TYPE_FLOAT, TextureFormat::R32F },
  3756. { PVR3_RG8, PVR3_CHANNEL_TYPE_ANY, TextureFormat::RG8 },
  3757. { PVR3_RG16, PVR3_CHANNEL_TYPE_ANY, TextureFormat::RG16 },
  3758. { PVR3_RG16, PVR3_CHANNEL_TYPE_FLOAT, TextureFormat::RG16F },
  3759. { PVR3_RG32, PVR3_CHANNEL_TYPE_ANY, TextureFormat::RG16 },
  3760. { PVR3_RG32, PVR3_CHANNEL_TYPE_FLOAT, TextureFormat::RG32F },
  3761. { PVR3_BGRA8, PVR3_CHANNEL_TYPE_ANY, TextureFormat::BGRA8 },
  3762. { PVR3_RGBA16, PVR3_CHANNEL_TYPE_ANY, TextureFormat::RGBA16 },
  3763. { PVR3_RGBA16, PVR3_CHANNEL_TYPE_FLOAT, TextureFormat::RGBA16F },
  3764. { PVR3_RGBA32, PVR3_CHANNEL_TYPE_ANY, TextureFormat::RGBA32U },
  3765. { PVR3_RGBA32, PVR3_CHANNEL_TYPE_FLOAT, TextureFormat::RGBA32F },
  3766. { PVR3_RGB565, PVR3_CHANNEL_TYPE_ANY, TextureFormat::B5G6R5 },
  3767. { PVR3_RGB565, PVR3_CHANNEL_TYPE_ANY, TextureFormat::R5G6B5 },
  3768. { PVR3_BGRA4, PVR3_CHANNEL_TYPE_ANY, TextureFormat::BGRA4 },
  3769. { PVR3_RGBA4, PVR3_CHANNEL_TYPE_ANY, TextureFormat::RGBA4 },
  3770. { PVR3_BGRA51, PVR3_CHANNEL_TYPE_ANY, TextureFormat::BGR5A1 },
  3771. { PVR3_RGBA51, PVR3_CHANNEL_TYPE_ANY, TextureFormat::RGB5A1 },
  3772. { PVR3_RGB10A2, PVR3_CHANNEL_TYPE_ANY, TextureFormat::RGB10A2 },
  3773. };
  3774. bool imageParsePvr3(ImageContainer& _imageContainer, bx::ReaderSeekerI* _reader, bx::Error* _err)
  3775. {
  3776. BX_ERROR_SCOPE(_err);
  3777. uint32_t flags;
  3778. bx::read(_reader, flags, _err);
  3779. uint64_t pixelFormat;
  3780. bx::read(_reader, pixelFormat, _err);
  3781. uint32_t colorSpace;
  3782. bx::read(_reader, colorSpace, _err); // 0 - linearRGB, 1 - sRGB
  3783. uint32_t channelType;
  3784. bx::read(_reader, channelType, _err);
  3785. uint32_t height;
  3786. bx::read(_reader, height, _err);
  3787. uint32_t width;
  3788. bx::read(_reader, width, _err);
  3789. uint32_t depth;
  3790. bx::read(_reader, depth, _err);
  3791. uint32_t numSurfaces;
  3792. bx::read(_reader, numSurfaces, _err);
  3793. uint32_t numFaces;
  3794. bx::read(_reader, numFaces, _err);
  3795. uint32_t numMips;
  3796. bx::read(_reader, numMips, _err);
  3797. uint32_t metaDataSize;
  3798. bx::read(_reader, metaDataSize, _err);
  3799. if (!_err->isOk() )
  3800. {
  3801. return false;
  3802. }
  3803. // skip meta garbage...
  3804. int64_t offset = bx::skip(_reader, metaDataSize);
  3805. TextureFormat::Enum format = TextureFormat::Unknown;
  3806. bool hasAlpha = false;
  3807. for (uint32_t ii = 0; ii < BX_COUNTOF(s_translatePvr3Format); ++ii)
  3808. {
  3809. if (s_translatePvr3Format[ii].m_format == pixelFormat
  3810. && channelType == (s_translatePvr3Format[ii].m_channelTypeMask & channelType) )
  3811. {
  3812. format = s_translatePvr3Format[ii].m_textureFormat;
  3813. break;
  3814. }
  3815. }
  3816. _imageContainer.m_allocator = NULL;
  3817. _imageContainer.m_data = NULL;
  3818. _imageContainer.m_size = 0;
  3819. _imageContainer.m_offset = (uint32_t)offset;
  3820. _imageContainer.m_width = width;
  3821. _imageContainer.m_height = height;
  3822. _imageContainer.m_depth = depth;
  3823. _imageContainer.m_format = format;
  3824. _imageContainer.m_orientation = Orientation::R0;
  3825. _imageContainer.m_numLayers = 1;
  3826. _imageContainer.m_numMips = uint8_t(bx::max<uint32_t>(numMips, 1) );
  3827. _imageContainer.m_hasAlpha = hasAlpha;
  3828. _imageContainer.m_cubeMap = numFaces > 1;
  3829. _imageContainer.m_ktx = false;
  3830. _imageContainer.m_ktxLE = false;
  3831. _imageContainer.m_pvr3 = true;
  3832. _imageContainer.m_srgb = colorSpace > 0;
  3833. return TextureFormat::Unknown != format;
  3834. }
  3835. ImageContainer* imageParsePvr3(bx::AllocatorI* _allocator, const void* _src, uint32_t _size, bx::Error* _err)
  3836. {
  3837. return imageParseT<PVR3_MAGIC, imageParsePvr3>(_allocator, _src, _size, _err);
  3838. }
  3839. bool imageParse(ImageContainer& _imageContainer, bx::ReaderSeekerI* _reader, bx::Error* _err)
  3840. {
  3841. BX_ERROR_SCOPE(_err);
  3842. uint32_t magic;
  3843. bx::read(_reader, magic, _err);
  3844. if (DDS_MAGIC == magic)
  3845. {
  3846. return imageParseDds(_imageContainer, _reader, _err);
  3847. }
  3848. else if (KTX_MAGIC == magic)
  3849. {
  3850. return imageParseKtx(_imageContainer, _reader, _err);
  3851. }
  3852. else if (PVR3_MAGIC == magic)
  3853. {
  3854. return imageParsePvr3(_imageContainer, _reader, _err);
  3855. }
  3856. else if (BIMG_CHUNK_MAGIC_GNF == magic)
  3857. {
  3858. return imageParseGnf(_imageContainer, _reader, _err);
  3859. }
  3860. else if (BIMG_CHUNK_MAGIC_TEX == magic)
  3861. {
  3862. TextureCreate tc;
  3863. bx::read(_reader, tc, _err);
  3864. _imageContainer.m_format = tc.m_format;
  3865. _imageContainer.m_orientation = Orientation::R0;
  3866. _imageContainer.m_offset = UINT32_MAX;
  3867. _imageContainer.m_allocator = NULL;
  3868. if (NULL == tc.m_mem)
  3869. {
  3870. _imageContainer.m_data = NULL;
  3871. _imageContainer.m_size = 0;
  3872. }
  3873. else
  3874. {
  3875. _imageContainer.m_data = tc.m_mem->data;
  3876. _imageContainer.m_size = tc.m_mem->size;
  3877. }
  3878. _imageContainer.m_width = tc.m_width;
  3879. _imageContainer.m_height = tc.m_height;
  3880. _imageContainer.m_depth = tc.m_depth;
  3881. _imageContainer.m_numLayers = tc.m_numLayers;
  3882. _imageContainer.m_numMips = tc.m_numMips;
  3883. _imageContainer.m_hasAlpha = false;
  3884. _imageContainer.m_cubeMap = tc.m_cubeMap;
  3885. _imageContainer.m_ktx = false;
  3886. _imageContainer.m_ktxLE = false;
  3887. _imageContainer.m_pvr3 = false;
  3888. _imageContainer.m_srgb = false;
  3889. return _err->isOk();
  3890. }
  3891. BX_TRACE("Unrecognized image format (magic: 0x%08x)!", magic);
  3892. BX_ERROR_SET(_err, BIMG_ERROR, "Unrecognized image format.");
  3893. return false;
  3894. }
  3895. bool imageParse(ImageContainer& _imageContainer, const void* _data, uint32_t _size, bx::Error* _err)
  3896. {
  3897. BX_ERROR_SCOPE(_err);
  3898. bx::MemoryReader reader(_data, _size);
  3899. return imageParse(_imageContainer, &reader, _err);
  3900. }
  3901. void imageDecodeToR8(bx::AllocatorI* _allocator, void* _dst, const void* _src, uint32_t _width, uint32_t _height, uint32_t _depth, uint32_t _dstPitch, TextureFormat::Enum _srcFormat)
  3902. {
  3903. const uint8_t* src = (const uint8_t*)_src;
  3904. uint8_t* dst = (uint8_t*)_dst;
  3905. const uint32_t srcBpp = s_imageBlockInfo[_srcFormat].bitsPerPixel;
  3906. const uint32_t srcPitch = _width*srcBpp/8;
  3907. for (uint32_t zz = 0; zz < _depth; ++zz, src += _height*srcPitch, dst += _height*_dstPitch)
  3908. {
  3909. if (isCompressed(_srcFormat))
  3910. {
  3911. uint32_t size = imageGetSize(NULL, uint16_t(_width), uint16_t(_height), 0, false, false, 1, TextureFormat::RGBA8);
  3912. void* temp = BX_ALLOC(_allocator, size);
  3913. imageDecodeToRgba8(_allocator, temp, _src, _width, _height, _width*4, _srcFormat);
  3914. imageConvert(_allocator, dst, TextureFormat::R8, temp, TextureFormat::RGBA8, _width, _height, 1, _width*4, _dstPitch);
  3915. BX_FREE(_allocator, temp);
  3916. }
  3917. else
  3918. {
  3919. imageConvert(_allocator, dst, TextureFormat::R8, src, _srcFormat, _width, _height, 1, srcPitch, _dstPitch);
  3920. }
  3921. }
  3922. }
  3923. void imageDecodeToBgra8(bx::AllocatorI* _allocator, void* _dst, const void* _src, uint32_t _width, uint32_t _height, uint32_t _dstPitch, TextureFormat::Enum _srcFormat)
  3924. {
  3925. const uint8_t* src = (const uint8_t*)_src;
  3926. uint8_t* dst = (uint8_t*)_dst;
  3927. uint32_t width = _width/4;
  3928. uint32_t height = _height/4;
  3929. uint8_t temp[16*4];
  3930. switch (_srcFormat)
  3931. {
  3932. case TextureFormat::BC1:
  3933. if (BX_ENABLED(BIMG_DECODE_BC1) )
  3934. {
  3935. for (uint32_t yy = 0; yy < height; ++yy)
  3936. {
  3937. for (uint32_t xx = 0; xx < width; ++xx)
  3938. {
  3939. decodeBlockDxt1(temp, src);
  3940. src += 8;
  3941. uint8_t* block = &dst[yy*_dstPitch*4 + xx*16];
  3942. bx::memCopy(&block[0*_dstPitch], &temp[ 0], 16);
  3943. bx::memCopy(&block[1*_dstPitch], &temp[16], 16);
  3944. bx::memCopy(&block[2*_dstPitch], &temp[32], 16);
  3945. bx::memCopy(&block[3*_dstPitch], &temp[48], 16);
  3946. }
  3947. }
  3948. }
  3949. else
  3950. {
  3951. BX_WARN(false, "BC1 decoder is disabled (BIMG_DECODE_BC1).");
  3952. imageCheckerboard(_dst, _width, _height, 16, UINT32_C(0xff000000), UINT32_C(0xff00ff00) );
  3953. }
  3954. break;
  3955. case TextureFormat::BC2:
  3956. if (BX_ENABLED(BIMG_DECODE_BC2) )
  3957. {
  3958. for (uint32_t yy = 0; yy < height; ++yy)
  3959. {
  3960. for (uint32_t xx = 0; xx < width; ++xx)
  3961. {
  3962. decodeBlockDxt23A(temp+3, src);
  3963. src += 8;
  3964. decodeBlockDxt(temp, src);
  3965. src += 8;
  3966. uint8_t* block = &dst[yy*_dstPitch*4 + xx*16];
  3967. bx::memCopy(&block[0*_dstPitch], &temp[ 0], 16);
  3968. bx::memCopy(&block[1*_dstPitch], &temp[16], 16);
  3969. bx::memCopy(&block[2*_dstPitch], &temp[32], 16);
  3970. bx::memCopy(&block[3*_dstPitch], &temp[48], 16);
  3971. }
  3972. }
  3973. }
  3974. else
  3975. {
  3976. BX_WARN(false, "BC2 decoder is disabled (BIMG_DECODE_BC2).");
  3977. imageCheckerboard(_dst, _width, _height, 16, UINT32_C(0xff000000), UINT32_C(0xff00ff00) );
  3978. }
  3979. break;
  3980. case TextureFormat::BC3:
  3981. if (BX_ENABLED(BIMG_DECODE_BC3) )
  3982. {
  3983. for (uint32_t yy = 0; yy < height; ++yy)
  3984. {
  3985. for (uint32_t xx = 0; xx < width; ++xx)
  3986. {
  3987. decodeBlockDxt45A(temp+3, src);
  3988. src += 8;
  3989. decodeBlockDxt(temp, src);
  3990. src += 8;
  3991. uint8_t* block = &dst[yy*_dstPitch*4 + xx*16];
  3992. bx::memCopy(&block[0*_dstPitch], &temp[ 0], 16);
  3993. bx::memCopy(&block[1*_dstPitch], &temp[16], 16);
  3994. bx::memCopy(&block[2*_dstPitch], &temp[32], 16);
  3995. bx::memCopy(&block[3*_dstPitch], &temp[48], 16);
  3996. }
  3997. }
  3998. }
  3999. else
  4000. {
  4001. BX_WARN(false, "BC3 decoder is disabled (BIMG_DECODE_BC3).");
  4002. imageCheckerboard(_dst, _width, _height, 16, UINT32_C(0xff000000), UINT32_C(0xff00ff00) );
  4003. }
  4004. break;
  4005. case TextureFormat::BC4:
  4006. if (BX_ENABLED(BIMG_DECODE_BC4) )
  4007. {
  4008. for (uint32_t yy = 0; yy < height; ++yy)
  4009. {
  4010. for (uint32_t xx = 0; xx < width; ++xx)
  4011. {
  4012. decodeBlockDxt45A(temp, src);
  4013. src += 8;
  4014. uint8_t* block = &dst[yy*_dstPitch*4 + xx*16];
  4015. bx::memCopy(&block[0*_dstPitch], &temp[ 0], 16);
  4016. bx::memCopy(&block[1*_dstPitch], &temp[16], 16);
  4017. bx::memCopy(&block[2*_dstPitch], &temp[32], 16);
  4018. bx::memCopy(&block[3*_dstPitch], &temp[48], 16);
  4019. }
  4020. }
  4021. }
  4022. else
  4023. {
  4024. BX_WARN(false, "BC4 decoder is disabled (BIMG_DECODE_BC4).");
  4025. imageCheckerboard(_dst, _width, _height, 16, UINT32_C(0xff000000), UINT32_C(0xff00ff00) );
  4026. }
  4027. break;
  4028. case TextureFormat::BC5:
  4029. if (BX_ENABLED(BIMG_DECODE_BC5) )
  4030. {
  4031. for (uint32_t yy = 0; yy < height; ++yy)
  4032. {
  4033. for (uint32_t xx = 0; xx < width; ++xx)
  4034. {
  4035. decodeBlockDxt45A(temp+2, src);
  4036. src += 8;
  4037. decodeBlockDxt45A(temp+1, src);
  4038. src += 8;
  4039. for (uint32_t ii = 0; ii < 16; ++ii)
  4040. {
  4041. float nx = temp[ii*4+2]*2.0f/255.0f - 1.0f;
  4042. float ny = temp[ii*4+1]*2.0f/255.0f - 1.0f;
  4043. float nz = bx::sqrt(1.0f - nx*nx - ny*ny);
  4044. temp[ii*4+0] = uint8_t( (nz + 1.0f)*255.0f/2.0f);
  4045. temp[ii*4+3] = 0;
  4046. }
  4047. uint8_t* block = &dst[yy*_dstPitch*4 + xx*16];
  4048. bx::memCopy(&block[0*_dstPitch], &temp[ 0], 16);
  4049. bx::memCopy(&block[1*_dstPitch], &temp[16], 16);
  4050. bx::memCopy(&block[2*_dstPitch], &temp[32], 16);
  4051. bx::memCopy(&block[3*_dstPitch], &temp[48], 16);
  4052. }
  4053. }
  4054. }
  4055. else
  4056. {
  4057. BX_WARN(false, "BC5 decoder is disabled (BIMG_DECODE_BC5).");
  4058. imageCheckerboard(_dst, _width, _height, 16, UINT32_C(0xff000000), UINT32_C(0xff00ff00) );
  4059. }
  4060. break;
  4061. case TextureFormat::BC6H:
  4062. if (BX_ENABLED(BIMG_DECODE_BC6) )
  4063. {
  4064. ImageContainer* rgba32f = imageAlloc(_allocator
  4065. , TextureFormat::RGBA32F
  4066. , uint16_t(_width)
  4067. , uint16_t(_height)
  4068. , uint16_t(1)
  4069. , 1
  4070. , false
  4071. , false
  4072. );
  4073. imageDecodeToRgba32f(_allocator, rgba32f->m_data, _src, _width, _height, 1, _width*16, _srcFormat);
  4074. imageConvert(_allocator, _dst, TextureFormat::BGRA8, rgba32f->m_data, TextureFormat::RGBA32F, _width, _height, 1, _width*16, _dstPitch);
  4075. imageFree(rgba32f);
  4076. }
  4077. else
  4078. {
  4079. BX_WARN(false, "BC6 decoder is disabled (BIMG_DECODE_BC6).");
  4080. imageCheckerboard(_dst, _width, _height, 16, UINT32_C(0xff000000), UINT32_C(0xff00ff00) );
  4081. }
  4082. break;
  4083. case TextureFormat::BC7:
  4084. if (BX_ENABLED(BIMG_DECODE_BC7) )
  4085. {
  4086. for (uint32_t yy = 0; yy < height; ++yy)
  4087. {
  4088. for (uint32_t xx = 0; xx < width; ++xx)
  4089. {
  4090. decodeBlockBc7(temp, src);
  4091. src += 16;
  4092. uint8_t* block = &dst[yy*_dstPitch*4 + xx*16];
  4093. bx::memCopy(&block[0*_dstPitch], &temp[ 0], 16);
  4094. bx::memCopy(&block[1*_dstPitch], &temp[16], 16);
  4095. bx::memCopy(&block[2*_dstPitch], &temp[32], 16);
  4096. bx::memCopy(&block[3*_dstPitch], &temp[48], 16);
  4097. }
  4098. }
  4099. }
  4100. else
  4101. {
  4102. BX_WARN(false, "BC7 decoder is disabled (BIMG_DECODE_BC7).");
  4103. imageCheckerboard(_dst, _width, _height, 16, UINT32_C(0xff000000), UINT32_C(0xff00ff00) );
  4104. }
  4105. break;
  4106. case TextureFormat::ETC1:
  4107. case TextureFormat::ETC2:
  4108. if (BX_ENABLED(BIMG_DECODE_ETC1 || BIMG_DECODE_ETC2) )
  4109. {
  4110. for (uint32_t yy = 0; yy < height; ++yy)
  4111. {
  4112. for (uint32_t xx = 0; xx < width; ++xx)
  4113. {
  4114. decodeBlockEtc12(temp, src);
  4115. src += 8;
  4116. uint8_t* block = &dst[yy*_dstPitch*4 + xx*16];
  4117. bx::memCopy(&block[0*_dstPitch], &temp[ 0], 16);
  4118. bx::memCopy(&block[1*_dstPitch], &temp[16], 16);
  4119. bx::memCopy(&block[2*_dstPitch], &temp[32], 16);
  4120. bx::memCopy(&block[3*_dstPitch], &temp[48], 16);
  4121. }
  4122. }
  4123. }
  4124. else
  4125. {
  4126. BX_WARN(false, "ETC1/ETC2 decoder is disabled (BIMG_DECODE_ETC1/ETC2).");
  4127. imageCheckerboard(_dst, _width, _height, 16, UINT32_C(0xff000000), UINT32_C(0xff00ff00) );
  4128. }
  4129. break;
  4130. case TextureFormat::ETC2A:
  4131. if (BX_ENABLED(BIMG_DECODE_ETC2))
  4132. {
  4133. for (uint32_t yy = 0; yy < height; ++yy)
  4134. {
  4135. for (uint32_t xx = 0; xx < width; ++xx)
  4136. {
  4137. decodeBlockEtc12(temp, src + 8);
  4138. decodeBlockEtc2Alpha(temp, src);
  4139. src += 16;
  4140. uint8_t* block = &dst[yy*_dstPitch * 4 + xx * 16];
  4141. bx::memCopy(&block[0 * _dstPitch], &temp[0], 16);
  4142. bx::memCopy(&block[1 * _dstPitch], &temp[16], 16);
  4143. bx::memCopy(&block[2 * _dstPitch], &temp[32], 16);
  4144. bx::memCopy(&block[3 * _dstPitch], &temp[48], 16);
  4145. }
  4146. }
  4147. }
  4148. else
  4149. {
  4150. BX_WARN(false, "ETC2 decoder is disabled (BIMG_DECODE_ETC2).");
  4151. imageCheckerboard(_dst, _width, _height, 16, UINT32_C(0xff000000), UINT32_C(0xff00ff00));
  4152. }
  4153. break;
  4154. case TextureFormat::ETC2A1:
  4155. BX_WARN(false, "ETC2A1 decoder is not implemented.");
  4156. imageCheckerboard(_dst, _width, _height, 16, UINT32_C(0xff000000), UINT32_C(0xffff0000) );
  4157. break;
  4158. case TextureFormat::PTC12:
  4159. BX_WARN(false, "PTC12 decoder is not implemented.");
  4160. imageCheckerboard(_dst, _width, _height, 16, UINT32_C(0xff000000), UINT32_C(0xffff00ff) );
  4161. break;
  4162. case TextureFormat::PTC12A:
  4163. BX_WARN(false, "PTC12A decoder is not implemented.");
  4164. imageCheckerboard(_dst, _width, _height, 16, UINT32_C(0xff000000), UINT32_C(0xffffff00) );
  4165. break;
  4166. case TextureFormat::PTC14:
  4167. for (uint32_t yy = 0; yy < height; ++yy)
  4168. {
  4169. for (uint32_t xx = 0; xx < width; ++xx)
  4170. {
  4171. decodeBlockPtc14(temp, src, xx, yy, width, height);
  4172. uint8_t* block = &dst[yy*_dstPitch*4 + xx*16];
  4173. bx::memCopy(&block[0*_dstPitch], &temp[ 0], 16);
  4174. bx::memCopy(&block[1*_dstPitch], &temp[16], 16);
  4175. bx::memCopy(&block[2*_dstPitch], &temp[32], 16);
  4176. bx::memCopy(&block[3*_dstPitch], &temp[48], 16);
  4177. }
  4178. }
  4179. break;
  4180. case TextureFormat::PTC14A:
  4181. for (uint32_t yy = 0; yy < height; ++yy)
  4182. {
  4183. for (uint32_t xx = 0; xx < width; ++xx)
  4184. {
  4185. decodeBlockPtc14A(temp, src, xx, yy, width, height);
  4186. uint8_t* block = &dst[yy*_dstPitch*4 + xx*16];
  4187. bx::memCopy(&block[0*_dstPitch], &temp[ 0], 16);
  4188. bx::memCopy(&block[1*_dstPitch], &temp[16], 16);
  4189. bx::memCopy(&block[2*_dstPitch], &temp[32], 16);
  4190. bx::memCopy(&block[3*_dstPitch], &temp[48], 16);
  4191. }
  4192. }
  4193. break;
  4194. case TextureFormat::PTC22:
  4195. BX_WARN(false, "PTC22 decoder is not implemented.");
  4196. imageCheckerboard(_dst, _width, _height, 16, UINT32_C(0xff00ff00), UINT32_C(0xff0000ff) );
  4197. break;
  4198. case TextureFormat::PTC24:
  4199. BX_WARN(false, "PTC24 decoder is not implemented.");
  4200. imageCheckerboard(_dst, _width, _height, 16, UINT32_C(0xff000000), UINT32_C(0xffffffff) );
  4201. break;
  4202. case TextureFormat::ATC:
  4203. for (uint32_t yy = 0; yy < height; ++yy)
  4204. {
  4205. for (uint32_t xx = 0; xx < width; ++xx)
  4206. {
  4207. decodeBlockATC(temp, src);
  4208. src += 8;
  4209. uint8_t* block = &dst[(yy*_dstPitch+xx*4)*4];
  4210. bx::memCopy(&block[0*_dstPitch], &temp[ 0], 16);
  4211. bx::memCopy(&block[1*_dstPitch], &temp[16], 16);
  4212. bx::memCopy(&block[2*_dstPitch], &temp[32], 16);
  4213. bx::memCopy(&block[3*_dstPitch], &temp[48], 16);
  4214. }
  4215. }
  4216. break;
  4217. case TextureFormat::ATCE:
  4218. for (uint32_t yy = 0; yy < height; ++yy)
  4219. {
  4220. for (uint32_t xx = 0; xx < width; ++xx)
  4221. {
  4222. decodeBlockDxt23A(temp+3, src);
  4223. src += 8;
  4224. decodeBlockATC(temp, src);
  4225. src += 8;
  4226. uint8_t* block = &dst[(yy*_dstPitch+xx*4)*4];
  4227. bx::memCopy(&block[0*_dstPitch], &temp[ 0], 16);
  4228. bx::memCopy(&block[1*_dstPitch], &temp[16], 16);
  4229. bx::memCopy(&block[2*_dstPitch], &temp[32], 16);
  4230. bx::memCopy(&block[3*_dstPitch], &temp[48], 16);
  4231. }
  4232. }
  4233. break;
  4234. case TextureFormat::ATCI:
  4235. for (uint32_t yy = 0; yy < height; ++yy)
  4236. {
  4237. for (uint32_t xx = 0; xx < width; ++xx)
  4238. {
  4239. decodeBlockDxt45A(temp+3, src);
  4240. src += 8;
  4241. decodeBlockATC(temp, src);
  4242. src += 8;
  4243. uint8_t* block = &dst[(yy*_dstPitch+xx*4)*4];
  4244. bx::memCopy(&block[0*_dstPitch], &temp[ 0], 16);
  4245. bx::memCopy(&block[1*_dstPitch], &temp[16], 16);
  4246. bx::memCopy(&block[2*_dstPitch], &temp[32], 16);
  4247. bx::memCopy(&block[3*_dstPitch], &temp[48], 16);
  4248. }
  4249. }
  4250. break;
  4251. case TextureFormat::ASTC4x4:
  4252. case TextureFormat::ASTC5x4:
  4253. case TextureFormat::ASTC5x5:
  4254. case TextureFormat::ASTC6x5:
  4255. case TextureFormat::ASTC6x6:
  4256. case TextureFormat::ASTC8x5:
  4257. case TextureFormat::ASTC8x6:
  4258. case TextureFormat::ASTC8x8:
  4259. case TextureFormat::ASTC10x5:
  4260. case TextureFormat::ASTC10x6:
  4261. case TextureFormat::ASTC10x8:
  4262. case TextureFormat::ASTC10x10:
  4263. case TextureFormat::ASTC12x10:
  4264. case TextureFormat::ASTC12x12:
  4265. imageDecodeToRgba8(_allocator, _dst, _src, _width, _height, _dstPitch, _srcFormat);
  4266. imageSwizzleBgra8(_dst, _dstPitch, _width, _height, _dst, _dstPitch);
  4267. break;
  4268. case TextureFormat::RGBA8:
  4269. {
  4270. const uint32_t srcPitch = _width * 4;
  4271. imageSwizzleBgra8(_dst, _dstPitch, _width, _height, _src, srcPitch);
  4272. }
  4273. break;
  4274. case TextureFormat::BGRA8:
  4275. {
  4276. const uint32_t srcPitch = _width * 4;
  4277. const uint32_t size = bx::uint32_min(srcPitch, _dstPitch);
  4278. bx::memCopy(_dst, _dstPitch, _src, srcPitch, size, _height);
  4279. }
  4280. break;
  4281. default:
  4282. {
  4283. const uint32_t srcBpp = s_imageBlockInfo[_srcFormat].bitsPerPixel;
  4284. const uint32_t srcPitch = _width * srcBpp / 8;
  4285. if (!imageConvert(_allocator, _dst, TextureFormat::BGRA8, _src, _srcFormat, _width, _height, 1, srcPitch, _dstPitch) )
  4286. {
  4287. // Failed to convert, just make ugly red-yellow checkerboard texture.
  4288. imageCheckerboard(_dst, _width, _height, 16, UINT32_C(0xffff0000), UINT32_C(0xffffff00) );
  4289. }
  4290. }
  4291. break;
  4292. }
  4293. }
  4294. void imageDecodeToRgba8(bx::AllocatorI* _allocator, void* _dst, const void* _src, uint32_t _width, uint32_t _height, uint32_t _dstPitch, TextureFormat::Enum _srcFormat)
  4295. {
  4296. switch (_srcFormat)
  4297. {
  4298. case TextureFormat::RGBA8:
  4299. {
  4300. const uint32_t srcPitch = _width * 4;
  4301. const uint32_t size = bx::uint32_min(srcPitch, _dstPitch);
  4302. bx::memCopy(_dst, _dstPitch, _src, srcPitch, size, _height);
  4303. }
  4304. break;
  4305. case TextureFormat::BGRA8:
  4306. {
  4307. const uint32_t srcPitch = _width * 4;
  4308. imageSwizzleBgra8(_dst, _dstPitch, _width, _height, _src, srcPitch);
  4309. }
  4310. break;
  4311. case TextureFormat::ASTC4x4:
  4312. case TextureFormat::ASTC5x4:
  4313. case TextureFormat::ASTC5x5:
  4314. case TextureFormat::ASTC6x5:
  4315. case TextureFormat::ASTC6x6:
  4316. case TextureFormat::ASTC8x5:
  4317. case TextureFormat::ASTC8x6:
  4318. case TextureFormat::ASTC8x8:
  4319. case TextureFormat::ASTC10x5:
  4320. case TextureFormat::ASTC10x6:
  4321. case TextureFormat::ASTC10x8:
  4322. case TextureFormat::ASTC10x10:
  4323. case TextureFormat::ASTC12x10:
  4324. case TextureFormat::ASTC12x12:
  4325. if (BX_ENABLED(BIMG_DECODE_ASTC) )
  4326. {
  4327. const unsigned int thread_count = 1;
  4328. const bimg::ImageBlockInfo& astcBlockInfo = bimg::getBlockInfo(_srcFormat);
  4329. const float quality = ASTCENC_PRE_MEDIUM;
  4330. const astcenc_profile profile = ASTCENC_PRF_LDR; //Linear LDR color profile
  4331. astcenc_error status;
  4332. //Create and init config and context
  4333. astcenc_config config{};
  4334. const unsigned int astcFlags = ASTCENC_FLG_DECOMPRESS_ONLY;
  4335. status = astcenc_config_init(profile, astcBlockInfo.blockWidth, astcBlockInfo.blockHeight, 1, quality, astcFlags, &config);
  4336. if (status != ASTCENC_SUCCESS) {
  4337. BX_TRACE("astc error in config init %s", astcenc_get_error_string(status));
  4338. imageCheckerboard(_dst, _width, _height, 16, UINT32_C(0xff000000), UINT32_C(0xffffff00) );
  4339. break;
  4340. }
  4341. astcenc_context* context;
  4342. status = astcenc_context_alloc(&config, thread_count, &context);
  4343. if (status != ASTCENC_SUCCESS) {
  4344. BX_TRACE("astc error in context alloc %s", astcenc_get_error_string(status));
  4345. imageCheckerboard(_dst, _width, _height, 16, UINT32_C(0xff000000), UINT32_C(0xffffff00) );
  4346. break;
  4347. }
  4348. //Put image data into an astcenc_image
  4349. astcenc_image image{};
  4350. image.dim_x = _width;
  4351. image.dim_y = _height;
  4352. image.dim_z = 1;
  4353. image.data_type = ASTCENC_TYPE_U8;
  4354. image.data = &_dst;
  4355. const uint32_t size = imageGetSize(NULL, uint16_t(_width), uint16_t(_height), 0, false, false, 1, _srcFormat);
  4356. static const astcenc_swizzle swizzle { //0123/rgba swizzle corresponds to ASTC_RGBA
  4357. ASTCENC_SWZ_R, ASTCENC_SWZ_G, ASTCENC_SWZ_B, ASTCENC_SWZ_A
  4358. };
  4359. status = astcenc_decompress_image(context, static_cast<const uint8_t*>(_src), size, &image, &swizzle, 0);
  4360. if (status != ASTCENC_SUCCESS) {
  4361. BX_TRACE("astc error in compress image %s", astcenc_get_error_string(status));
  4362. imageCheckerboard(_dst, _width, _height, 16, UINT32_C(0xff000000), UINT32_C(0xffffff00) );
  4363. astcenc_context_free(context);
  4364. break;
  4365. }
  4366. astcenc_context_free(context);
  4367. }
  4368. else
  4369. {
  4370. BX_WARN(false, "ASTC decoder is disabled (BIMG_DECODE_ASTC).");
  4371. imageCheckerboard(_dst, _width, _height, 16, UINT32_C(0xff000000), UINT32_C(0xff00ff00) );
  4372. }
  4373. break;
  4374. default:
  4375. {
  4376. const uint32_t srcPitch = _width * 4;
  4377. imageDecodeToBgra8(_allocator, _dst, _src, _width, _height, _dstPitch, _srcFormat);
  4378. imageSwizzleBgra8(_dst, _dstPitch, _width, _height, _dst, srcPitch);
  4379. }
  4380. break;
  4381. }
  4382. }
  4383. void imageRgba8ToRgba32fRef(void* _dst, uint32_t _width, uint32_t _height, uint32_t _srcPitch, const void* _src)
  4384. {
  4385. const uint32_t dstWidth = _width;
  4386. const uint32_t dstHeight = _height;
  4387. if (0 == dstWidth
  4388. || 0 == dstHeight)
  4389. {
  4390. return;
  4391. }
  4392. float* dst = (float*)_dst;
  4393. const uint8_t* src = (const uint8_t*)_src;
  4394. for (uint32_t yy = 0, ystep = _srcPitch; yy < dstHeight; ++yy, src += ystep)
  4395. {
  4396. const uint8_t* rgba = src;
  4397. for (uint32_t xx = 0; xx < dstWidth; ++xx, rgba += 4, dst += 4)
  4398. {
  4399. dst[0] = bx::toLinear(rgba[0]);
  4400. dst[1] = bx::toLinear(rgba[1]);
  4401. dst[2] = bx::toLinear(rgba[2]);
  4402. dst[3] = rgba[3];
  4403. }
  4404. }
  4405. }
  4406. void imageRgba8ToRgba32f(void* _dst, uint32_t _width, uint32_t _height, uint32_t _srcPitch, const void* _src)
  4407. {
  4408. const uint32_t dstWidth = _width;
  4409. const uint32_t dstHeight = _height;
  4410. if (0 == dstWidth
  4411. || 0 == dstHeight)
  4412. {
  4413. return;
  4414. }
  4415. float* dst = (float*)_dst;
  4416. const uint8_t* src = (const uint8_t*)_src;
  4417. using namespace bx;
  4418. const simd128_t unpack = simd_ld(1.0f/256.0f, 1.0f/256.0f/256.0f, 1.0f/65536.0f/256.0f, 1.0f/16777216.0f/256.0f);
  4419. const simd128_t umask = simd_ild(0xff, 0xff00, 0xff0000, 0xff000000);
  4420. const simd128_t wflip = simd_ild(0, 0, 0, 0x80000000);
  4421. const simd128_t wadd = simd_ld(0.0f, 0.0f, 0.0f, 32768.0f*65536.0f);
  4422. for (uint32_t yy = 0, ystep = _srcPitch; yy < dstHeight; ++yy, src += ystep)
  4423. {
  4424. const uint8_t* rgba = src;
  4425. for (uint32_t xx = 0; xx < dstWidth; ++xx, rgba += 4, dst += 4)
  4426. {
  4427. const simd128_t abgr0 = simd_splat(rgba);
  4428. const simd128_t abgr0m = simd_and(abgr0, umask);
  4429. const simd128_t abgr0x = simd_xor(abgr0m, wflip);
  4430. const simd128_t abgr0f = simd_itof(abgr0x);
  4431. const simd128_t abgr0c = simd_add(abgr0f, wadd);
  4432. const simd128_t abgr0n = simd_mul(abgr0c, unpack);
  4433. simd_st(dst, abgr0n);
  4434. }
  4435. }
  4436. }
  4437. void imageDecodeToRgba32f(bx::AllocatorI* _allocator, void* _dst, const void* _src, uint32_t _width, uint32_t _height, uint32_t _depth, uint32_t _dstPitch, TextureFormat::Enum _srcFormat)
  4438. {
  4439. const uint8_t* src = (const uint8_t*)_src;
  4440. uint8_t* dst = (uint8_t*)_dst;
  4441. const uint32_t srcBpp = s_imageBlockInfo[_srcFormat].bitsPerPixel;
  4442. const uint32_t srcPitch = _width*srcBpp/8;
  4443. for (uint32_t zz = 0; zz < _depth; ++zz, src += _height*srcPitch, dst += _height*_dstPitch)
  4444. {
  4445. switch (_srcFormat)
  4446. {
  4447. case TextureFormat::BC5:
  4448. {
  4449. uint32_t width = _width/4;
  4450. uint32_t height = _height/4;
  4451. const uint8_t* srcData = src;
  4452. for (uint32_t yy = 0; yy < height; ++yy)
  4453. {
  4454. for (uint32_t xx = 0; xx < width; ++xx)
  4455. {
  4456. uint8_t temp[16*4];
  4457. decodeBlockDxt45A(temp+2, srcData);
  4458. srcData += 8;
  4459. decodeBlockDxt45A(temp+1, srcData);
  4460. srcData += 8;
  4461. for (uint32_t ii = 0; ii < 16; ++ii)
  4462. {
  4463. float nx = temp[ii*4+2]*2.0f/255.0f - 1.0f;
  4464. float ny = temp[ii*4+1]*2.0f/255.0f - 1.0f;
  4465. float nz = bx::sqrt(1.0f - nx*nx - ny*ny);
  4466. const uint32_t offset = (yy*4 + ii/4)*_width*16 + (xx*4 + ii%4)*16;
  4467. float* block = (float*)&dst[offset];
  4468. block[0] = nx;
  4469. block[1] = ny;
  4470. block[2] = nz;
  4471. block[3] = 0.0f;
  4472. }
  4473. }
  4474. }
  4475. }
  4476. break;
  4477. case TextureFormat::BC6H:
  4478. {
  4479. uint32_t width = _width/4;
  4480. uint32_t height = _height/4;
  4481. const uint8_t* srcData = src;
  4482. for (uint32_t yy = 0; yy < height; ++yy)
  4483. {
  4484. for (uint32_t xx = 0; xx < width; ++xx)
  4485. {
  4486. float tmp[16*4];
  4487. decodeBlockBc6h(tmp, srcData);
  4488. srcData += 16;
  4489. uint8_t* block = (uint8_t*)&dst[yy*_dstPitch*4 + xx*64];
  4490. bx::memCopy(&block[0*_dstPitch], &tmp[ 0], 64);
  4491. bx::memCopy(&block[1*_dstPitch], &tmp[16], 64);
  4492. bx::memCopy(&block[2*_dstPitch], &tmp[32], 64);
  4493. bx::memCopy(&block[3*_dstPitch], &tmp[48], 64);
  4494. }
  4495. }
  4496. }
  4497. break;
  4498. case TextureFormat::RGBA32F:
  4499. bx::memCopy(dst, src, _dstPitch*_height);
  4500. break;
  4501. default:
  4502. if (isCompressed(_srcFormat) )
  4503. {
  4504. uint32_t size = imageGetSize(NULL, uint16_t(_width), uint16_t(_height), 0, false, false, 1, TextureFormat::RGBA8);
  4505. void* temp = BX_ALLOC(_allocator, size);
  4506. imageDecodeToRgba8(_allocator, temp, src, _width, _height, _width*4, _srcFormat);
  4507. imageRgba8ToRgba32f(dst, _width, _height, _width*4, temp);
  4508. BX_FREE(_allocator, temp);
  4509. }
  4510. else
  4511. {
  4512. imageConvert(_allocator, dst, TextureFormat::RGBA32F, src, _srcFormat, _width, _height, 1, srcPitch, _dstPitch);
  4513. }
  4514. break;
  4515. }
  4516. }
  4517. }
  4518. bool imageGetRawData(const ImageContainer& _imageContainer, uint16_t _side, uint8_t _lod, const void* _data, uint32_t _size, ImageMip& _mip)
  4519. {
  4520. uint32_t offset = _imageContainer.m_offset;
  4521. TextureFormat::Enum format = TextureFormat::Enum(_imageContainer.m_format);
  4522. bool hasAlpha = _imageContainer.m_hasAlpha;
  4523. const ImageBlockInfo& blockInfo = s_imageBlockInfo[format];
  4524. const uint8_t bpp = blockInfo.bitsPerPixel;
  4525. const uint32_t blockSize = blockInfo.blockSize;
  4526. const uint32_t blockWidth = blockInfo.blockWidth;
  4527. const uint32_t blockHeight = blockInfo.blockHeight;
  4528. const uint32_t minBlockX = blockInfo.minBlockX;
  4529. const uint32_t minBlockY = blockInfo.minBlockY;
  4530. if (UINT32_MAX == _imageContainer.m_offset)
  4531. {
  4532. if (NULL == _imageContainer.m_data)
  4533. {
  4534. return false;
  4535. }
  4536. offset = 0;
  4537. _data = _imageContainer.m_data;
  4538. _size = _imageContainer.m_size;
  4539. }
  4540. const uint8_t* data = (const uint8_t*)_data;
  4541. const uint16_t numSides = _imageContainer.m_numLayers * (_imageContainer.m_cubeMap ? 6 : 1);
  4542. if (_imageContainer.m_ktx || _imageContainer.m_pvr3)
  4543. {
  4544. uint32_t width = _imageContainer.m_width;
  4545. uint32_t height = _imageContainer.m_height;
  4546. uint32_t depth = _imageContainer.m_depth;
  4547. for (uint8_t lod = 0, num = _imageContainer.m_numMips; lod < num; ++lod)
  4548. {
  4549. width = bx::max<uint32_t>(blockWidth * minBlockX, ( (width + blockWidth - 1) / blockWidth )*blockWidth);
  4550. height = bx::max<uint32_t>(blockHeight * minBlockY, ( (height + blockHeight - 1) / blockHeight)*blockHeight);
  4551. depth = bx::max<uint32_t>(1, depth);
  4552. const uint32_t mipSize = width/blockWidth * height/blockHeight * depth * blockSize;
  4553. if (_imageContainer.m_ktx)
  4554. {
  4555. const uint32_t size = mipSize * numSides;
  4556. uint32_t imageSize = bx::toHostEndian(*(const uint32_t*)&data[offset], _imageContainer.m_ktxLE);
  4557. BX_ASSERT(size == imageSize, "KTX: Image size mismatch %d (expected %d).", size, imageSize);
  4558. BX_UNUSED(size, imageSize);
  4559. offset += sizeof(uint32_t);
  4560. }
  4561. for (uint16_t side = 0; side < numSides; ++side)
  4562. {
  4563. BX_ASSERT(offset <= _size, "Reading past size of data buffer! (offset %d, size %d)", offset, _size);
  4564. if (side == _side
  4565. && lod == _lod)
  4566. {
  4567. _mip.m_width = width;
  4568. _mip.m_height = height;
  4569. _mip.m_depth = depth;
  4570. _mip.m_blockSize = blockSize;
  4571. _mip.m_size = mipSize;
  4572. _mip.m_data = &data[offset];
  4573. _mip.m_bpp = bpp;
  4574. _mip.m_format = format;
  4575. _mip.m_hasAlpha = hasAlpha;
  4576. return true;
  4577. }
  4578. offset += mipSize;
  4579. BX_UNUSED(_size);
  4580. }
  4581. width >>= 1;
  4582. height >>= 1;
  4583. depth >>= 1;
  4584. }
  4585. }
  4586. else
  4587. {
  4588. for (uint16_t side = 0; side < numSides; ++side)
  4589. {
  4590. uint32_t width = _imageContainer.m_width;
  4591. uint32_t height = _imageContainer.m_height;
  4592. uint32_t depth = _imageContainer.m_depth;
  4593. for (uint8_t lod = 0, num = _imageContainer.m_numMips; lod < num; ++lod)
  4594. {
  4595. BX_ASSERT(offset <= _size, "Reading past size of data buffer! (offset %d, size %d)", offset, _size);
  4596. width = bx::max<uint32_t>(blockWidth * minBlockX, ( (width + blockWidth - 1) / blockWidth )*blockWidth);
  4597. height = bx::max<uint32_t>(blockHeight * minBlockY, ( (height + blockHeight - 1) / blockHeight)*blockHeight);
  4598. depth = bx::max<uint32_t>(1, depth);
  4599. uint32_t mipSize = width/blockWidth * height/blockHeight * depth * blockSize;
  4600. if (side == _side
  4601. && lod == _lod)
  4602. {
  4603. _mip.m_width = width;
  4604. _mip.m_height = height;
  4605. _mip.m_depth = depth;
  4606. _mip.m_blockSize = blockSize;
  4607. _mip.m_size = mipSize;
  4608. _mip.m_data = &data[offset];
  4609. _mip.m_bpp = bpp;
  4610. _mip.m_format = format;
  4611. _mip.m_hasAlpha = hasAlpha;
  4612. return true;
  4613. }
  4614. offset += mipSize;
  4615. BX_UNUSED(_size);
  4616. width >>= 1;
  4617. height >>= 1;
  4618. depth >>= 1;
  4619. }
  4620. }
  4621. }
  4622. return false;
  4623. }
  4624. int32_t imageWriteTga(bx::WriterI* _writer, uint32_t _width, uint32_t _height, uint32_t _srcPitch, const void* _src, bool _grayscale, bool _yflip, bx::Error* _err)
  4625. {
  4626. BX_ERROR_SCOPE(_err);
  4627. uint8_t type = _grayscale ? 3 : 2;
  4628. uint8_t bpp = _grayscale ? 8 : 32;
  4629. uint8_t header[18] = {};
  4630. header[ 2] = type;
  4631. header[12] = _width &0xff;
  4632. header[13] = (_width >>8)&0xff;
  4633. header[14] = _height &0xff;
  4634. header[15] = (_height>>8)&0xff;
  4635. header[16] = bpp;
  4636. header[17] = 32;
  4637. int32_t total = 0;
  4638. total += bx::write(_writer, header, sizeof(header), _err);
  4639. uint32_t dstPitch = _width*bpp/8;
  4640. if (_yflip)
  4641. {
  4642. const uint8_t* data = (const uint8_t*)_src + _srcPitch*_height - _srcPitch;
  4643. for (uint32_t yy = 0; yy < _height && _err->isOk(); ++yy)
  4644. {
  4645. total += bx::write(_writer, data, dstPitch, _err);
  4646. data -= _srcPitch;
  4647. }
  4648. }
  4649. else if (_srcPitch == dstPitch)
  4650. {
  4651. total += bx::write(_writer, _src, _height*_srcPitch, _err);
  4652. }
  4653. else
  4654. {
  4655. const uint8_t* data = (const uint8_t*)_src;
  4656. for (uint32_t yy = 0; yy < _height && _err->isOk(); ++yy)
  4657. {
  4658. total += bx::write(_writer, data, dstPitch, _err);
  4659. data += _srcPitch;
  4660. }
  4661. }
  4662. return total;
  4663. }
  4664. template<typename Ty>
  4665. class HashWriter : public bx::WriterI
  4666. {
  4667. public:
  4668. HashWriter(bx::WriterI* _writer)
  4669. : m_writer(_writer)
  4670. {
  4671. begin();
  4672. }
  4673. void begin()
  4674. {
  4675. m_hash.begin();
  4676. }
  4677. uint32_t end()
  4678. {
  4679. return m_hash.end();
  4680. }
  4681. virtual int32_t write(const void* _data, int32_t _size, bx::Error* _err) override
  4682. {
  4683. m_hash.add(_data, _size);
  4684. return m_writer->write(_data, _size, _err);
  4685. }
  4686. private:
  4687. Ty m_hash;
  4688. bx::WriterI* m_writer;
  4689. };
  4690. int32_t imageWritePng(bx::WriterI* _writer, uint32_t _width, uint32_t _height, uint32_t _srcPitch, const void* _src, TextureFormat::Enum _format, bool _yflip, bx::Error* _err)
  4691. {
  4692. BX_ERROR_SCOPE(_err);
  4693. switch (_format)
  4694. {
  4695. case TextureFormat::R8:
  4696. case TextureFormat::RGBA8:
  4697. case TextureFormat::BGRA8:
  4698. break;
  4699. default:
  4700. BX_ERROR_SET(_err, BIMG_ERROR, "PNG: Unsupported texture format.");
  4701. return 0;
  4702. }
  4703. const bool grayscale = TextureFormat::R8 == _format;
  4704. const bool bgra = TextureFormat::BGRA8 == _format;
  4705. int32_t total = 0;
  4706. total += bx::write(_writer, "\x89PNG\r\n\x1a\n", _err);
  4707. total += bx::write(_writer, bx::toBigEndian<uint32_t>(13), _err);
  4708. HashWriter<bx::HashCrc32> writerC(_writer);
  4709. total += bx::write(&writerC, "IHDR", _err);
  4710. total += bx::write(&writerC, bx::toBigEndian(_width), _err);
  4711. total += bx::write(&writerC, bx::toBigEndian(_height), _err);
  4712. total += bx::write(&writerC, "\x08\x06", _err);
  4713. total += bx::writeRep(&writerC, 0, 3, _err);
  4714. total += bx::write(_writer, bx::toBigEndian(writerC.end() ), _err);
  4715. const uint32_t bpp = grayscale ? 8 : 32;
  4716. const uint32_t stride = _width*bpp/8;
  4717. const uint16_t zlen = bx::toLittleEndian<uint16_t>(uint16_t(stride + 1) );
  4718. const uint16_t zlenC = bx::toLittleEndian<uint16_t>(~zlen);
  4719. total += bx::write(_writer, bx::toBigEndian<uint32_t>(_height*(stride+6)+6), _err);
  4720. writerC.begin();
  4721. total += bx::write(&writerC, "IDAT", _err);
  4722. total += bx::write(&writerC, "\x78\x9c", _err);
  4723. const uint8_t* data = (const uint8_t*)_src;
  4724. int32_t step = int32_t(_srcPitch);
  4725. if (_yflip)
  4726. {
  4727. data += _srcPitch*_height - _srcPitch;
  4728. step = -step;
  4729. }
  4730. HashWriter<bx::HashAdler32> writerA(&writerC);
  4731. for (uint32_t ii = 0; ii < _height && _err->isOk(); ++ii)
  4732. {
  4733. total += bx::write(&writerC, uint8_t(ii == _height-1 ? 1 : 0), _err);
  4734. total += bx::write(&writerC, zlen, _err);
  4735. total += bx::write(&writerC, zlenC, _err);
  4736. total += bx::write(&writerA, uint8_t(0), _err);
  4737. if (bgra)
  4738. {
  4739. for (uint32_t xx = 0; xx < _width; ++xx)
  4740. {
  4741. const uint8_t* texel = &data[xx*4];
  4742. const uint8_t bb = texel[0];
  4743. const uint8_t gg = texel[1];
  4744. const uint8_t rr = texel[2];
  4745. const uint8_t aa = texel[3];
  4746. total += bx::write(&writerA, rr, _err);
  4747. total += bx::write(&writerA, gg, _err);
  4748. total += bx::write(&writerA, bb, _err);
  4749. total += bx::write(&writerA, aa, _err);
  4750. }
  4751. }
  4752. else
  4753. {
  4754. total += bx::write(&writerA, data, stride, _err);
  4755. }
  4756. data += step;
  4757. }
  4758. total += bx::write(&writerC, bx::toBigEndian(writerA.end() ), _err);
  4759. total += bx::write(_writer, bx::toBigEndian(writerC.end() ), _err);
  4760. total += bx::write(&writerC, uint32_t(0), _err);
  4761. writerC.begin();
  4762. total += bx::write(&writerC, "IEND", _err);
  4763. total += bx::write(_writer, bx::toBigEndian(writerC.end() ), _err);
  4764. return total;
  4765. }
  4766. int32_t imageWriteExr(bx::WriterI* _writer, uint32_t _width, uint32_t _height, uint32_t _srcPitch, const void* _src, TextureFormat::Enum _format, bool _yflip, bx::Error* _err)
  4767. {
  4768. BX_ERROR_SCOPE(_err);
  4769. const uint32_t bpp = getBitsPerPixel(_format);
  4770. uint32_t bytesPerChannel = 0;
  4771. switch (_format)
  4772. {
  4773. case TextureFormat::RGBA16F:
  4774. bytesPerChannel = 2;
  4775. break;
  4776. default:
  4777. BX_ERROR_SET(_err, BIMG_ERROR, "EXR: Unsupported texture format.");
  4778. return 0;
  4779. }
  4780. int32_t total = 0;
  4781. total += bx::write(_writer, "v/1\x01", _err);
  4782. total += bx::writeLE(_writer, uint32_t(2), _err);
  4783. total += bx::write(_writer, "channels", _err);
  4784. total += bx::write(_writer, '\0', _err);
  4785. total += bx::write(_writer, "chlist", _err);
  4786. total += bx::write(_writer, '\0', _err);
  4787. total += bx::writeLE(_writer, uint32_t(18*4+1), _err);
  4788. const uint8_t cdata[] = { 0, 1, 0, 0, 0, 0, 0, 0, 0, 1, 0, 0, 0, 1, 0, 0, 0 };
  4789. // Order is always ABGR order because Photoshop and GIMP ignore these fields and
  4790. // assume it's in ABGR order.
  4791. total += bx::write(_writer, 'A', _err);
  4792. total += bx::write(_writer, cdata, BX_COUNTOF(cdata), _err);
  4793. total += bx::write(_writer, 'B', _err);
  4794. total += bx::write(_writer, cdata, BX_COUNTOF(cdata), _err);
  4795. total += bx::write(_writer, 'G', _err);
  4796. total += bx::write(_writer, cdata, BX_COUNTOF(cdata), _err);
  4797. total += bx::write(_writer, 'R', _err);
  4798. total += bx::write(_writer, cdata, BX_COUNTOF(cdata), _err);
  4799. total += bx::write(_writer, '\0', _err);
  4800. total += bx::write(_writer, "compression", _err);
  4801. total += bx::write(_writer, '\0', _err);
  4802. total += bx::write(_writer, "compression", _err);
  4803. total += bx::write(_writer, '\0', _err);
  4804. total += bx::writeLE(_writer, uint32_t(1), _err);
  4805. total += bx::write(_writer, '\0', _err); // no compression
  4806. total += bx::write(_writer, "dataWindow", _err);
  4807. total += bx::write(_writer, '\0', _err);
  4808. total += bx::write(_writer, "box2i", _err);
  4809. total += bx::write(_writer, '\0', _err);
  4810. total += bx::writeLE(_writer, uint32_t(16), _err);
  4811. total += bx::writeRep(_writer, '\0', 8, _err);
  4812. total += bx::writeLE(_writer, _width-1, _err);
  4813. total += bx::writeLE(_writer, _height-1, _err);
  4814. total += bx::write(_writer, "displayWindow", _err);
  4815. total += bx::write(_writer, '\0', _err);
  4816. total += bx::write(_writer, "box2i", _err);
  4817. total += bx::write(_writer, '\0', _err);
  4818. total += bx::writeLE(_writer, uint32_t(16), _err);
  4819. total += bx::writeRep(_writer, '\0', 8, _err);
  4820. total += bx::writeLE(_writer, _width-1, _err);
  4821. total += bx::writeLE(_writer, _height-1, _err);
  4822. total += bx::write(_writer, "lineOrder", _err);
  4823. total += bx::write(_writer, '\0', _err);
  4824. total += bx::write(_writer, "lineOrder", _err);
  4825. total += bx::write(_writer, '\0', _err);
  4826. total += bx::writeLE(_writer, uint32_t(1), _err);
  4827. total += bx::write(_writer, _yflip, _err);
  4828. total += bx::write(_writer, "pixelAspectRatio", _err);
  4829. total += bx::write(_writer, '\0', _err);
  4830. total += bx::write(_writer, "float", _err);
  4831. total += bx::write(_writer, '\0', _err);
  4832. total += bx::writeLE(_writer, uint32_t(4), _err);
  4833. total += bx::writeLE(_writer, 1.0f, _err);
  4834. total += bx::write(_writer, "screenWindowCenter", _err);
  4835. total += bx::write(_writer, '\0', _err);
  4836. total += bx::write(_writer, "v2f", _err);
  4837. total += bx::write(_writer, '\0', _err);
  4838. total += bx::writeLE(_writer, uint32_t(8), _err);
  4839. total += bx::writeRep(_writer, '\0', 8, _err);
  4840. total += bx::write(_writer, "screenWindowWidth", _err);
  4841. total += bx::write(_writer, '\0', _err);
  4842. total += bx::write(_writer, "float", _err);
  4843. total += bx::write(_writer, '\0', _err);
  4844. total += bx::writeLE(_writer, uint32_t(4), _err);
  4845. total += bx::writeLE(_writer, 1.0f, _err);
  4846. total += bx::write(_writer, '\0', _err);
  4847. const uint32_t exrStride = _width*bpp/8;
  4848. uint64_t offset = 0;
  4849. for (uint32_t yy = 0; yy < _height && _err->isOk(); ++yy)
  4850. {
  4851. total += bx::writeLE(_writer, (offset), _err);
  4852. offset += exrStride + 8 /* offset */;
  4853. }
  4854. const uint8_t* data = (const uint8_t*)_src;
  4855. for (uint32_t yy = 0; yy < _height && _err->isOk(); ++yy)
  4856. {
  4857. total += bx::writeLE(_writer, yy, _err);
  4858. total += bx::writeLE(_writer, exrStride, _err);
  4859. for (uint32_t xx = 0; xx < _width && _err->isOk(); ++xx)
  4860. {
  4861. total += bx::write(_writer, &data[xx*bpp/8+3*bytesPerChannel], bytesPerChannel, _err);
  4862. }
  4863. for (uint32_t xx = 0; xx < _width && _err->isOk(); ++xx)
  4864. {
  4865. total += bx::write(_writer, &data[xx*bpp/8+2*bytesPerChannel], bytesPerChannel, _err);
  4866. }
  4867. for (uint32_t xx = 0; xx < _width && _err->isOk(); ++xx)
  4868. {
  4869. total += bx::write(_writer, &data[xx*bpp/8+1*bytesPerChannel], bytesPerChannel, _err);
  4870. }
  4871. for (uint32_t xx = 0; xx < _width && _err->isOk(); ++xx)
  4872. {
  4873. total += bx::write(_writer, &data[xx*bpp/8+0*bytesPerChannel], bytesPerChannel, _err);
  4874. }
  4875. data += _srcPitch;
  4876. }
  4877. return total;
  4878. }
  4879. int32_t imageWriteHdr(bx::WriterI* _writer, uint32_t _width, uint32_t _height, uint32_t _srcPitch, const void* _src, TextureFormat::Enum _format, bool _yflip, bx::Error* _err)
  4880. {
  4881. BX_ERROR_SCOPE(_err);
  4882. int32_t total = 0;
  4883. total += bx::write(_writer, "#?RADIANCE\n" , _err);
  4884. total += bx::write(_writer, "FORMAT=32-bit_rle_rgbe\n" , _err);
  4885. total += bx::write(_writer, '\n' , _err);
  4886. total += bx::write(_writer, _err, "%cY %d +X %d\n", _yflip ? '+' : '-', _height, _width);
  4887. UnpackFn unpack = getUnpack(_format);
  4888. const uint32_t bpp = getBitsPerPixel(_format);
  4889. const uint8_t* data = (const uint8_t*)_src;
  4890. for (uint32_t yy = 0; yy < _height && _err->isOk(); ++yy)
  4891. {
  4892. for (uint32_t xx = 0; xx < _width && _err->isOk(); ++xx)
  4893. {
  4894. float rgba[4];
  4895. unpack(rgba, &data[xx*bpp/8]);
  4896. const float maxVal = bx::max(rgba[0], rgba[1], rgba[2]);
  4897. const float exp = bx::ceil(bx::log2(maxVal) );
  4898. const float toRgb8 = 255.0f * 1.0f/bx::ldexp(1.0f, int(exp) );
  4899. uint8_t rgbe[4];
  4900. rgbe[0] = uint8_t(rgba[0] * toRgb8);
  4901. rgbe[1] = uint8_t(rgba[1] * toRgb8);
  4902. rgbe[2] = uint8_t(rgba[2] * toRgb8);
  4903. rgbe[3] = uint8_t(exp+128.0f);
  4904. total += bx::write(_writer, rgbe, 4, _err);
  4905. }
  4906. data += _srcPitch;
  4907. }
  4908. return total;
  4909. }
  4910. static int32_t imageWriteDdsHeader(bx::WriterI* _writer, TextureFormat::Enum _format, bool _cubeMap, uint32_t _width, uint32_t _height, uint32_t _depth, uint8_t _numMips, uint32_t _numLayers, bx::Error* _err)
  4911. {
  4912. BX_ERROR_SCOPE(_err);
  4913. uint32_t ddspf = UINT32_MAX;
  4914. uint32_t dxgiFormat = UINT32_MAX;
  4915. uint32_t fourccFormat = UINT32_MAX;
  4916. for (uint32_t ii = 0; ii < BX_COUNTOF(s_translateDdsPixelFormat); ++ii)
  4917. {
  4918. if (s_translateDdsPixelFormat[ii].m_textureFormat == _format)
  4919. {
  4920. ddspf = ii;
  4921. break;
  4922. }
  4923. }
  4924. if (UINT32_MAX == ddspf)
  4925. {
  4926. for (uint32_t ii = 0; ii < BX_COUNTOF(s_translateDxgiFormat); ++ii)
  4927. {
  4928. if (s_translateDxgiFormat[ii].m_textureFormat == _format)
  4929. {
  4930. dxgiFormat = s_translateDxgiFormat[ii].m_format;
  4931. break;
  4932. }
  4933. }
  4934. }
  4935. if (UINT32_MAX == ddspf && UINT32_MAX == dxgiFormat)
  4936. {
  4937. for (uint32_t ii = 0; ii < BX_COUNTOF(s_translateDdsFourccFormat); ++ii)
  4938. {
  4939. if (s_translateDdsFourccFormat[ii].m_textureFormat == _format)
  4940. {
  4941. fourccFormat = s_translateDdsFourccFormat[ii].m_format;
  4942. break;
  4943. }
  4944. }
  4945. }
  4946. if (UINT32_MAX == ddspf
  4947. && UINT32_MAX == dxgiFormat
  4948. && UINT32_MAX == fourccFormat)
  4949. {
  4950. BX_ERROR_SET(_err, BIMG_ERROR, "DDS: output format not supported.");
  4951. return 0;
  4952. }
  4953. const uint32_t bpp = getBitsPerPixel(_format);
  4954. uint32_t total = 0;
  4955. total += bx::write(_writer, uint32_t(DDS_MAGIC), _err);
  4956. uint32_t headerStart = total;
  4957. total += bx::write(_writer, uint32_t(DDS_HEADER_SIZE), _err);
  4958. total += bx::write(_writer, uint32_t(0
  4959. | DDSD_HEIGHT
  4960. | DDSD_WIDTH
  4961. | DDSD_PIXELFORMAT
  4962. | DDSD_CAPS
  4963. | (1 < _depth ? DDSD_DEPTH : 0)
  4964. | (1 < _numMips ? DDSD_MIPMAPCOUNT : 0)
  4965. | (isCompressed(_format) ? DDSD_LINEARSIZE : DDSD_PITCH)
  4966. )
  4967. , _err
  4968. );
  4969. const uint32_t pitchOrLinearSize = isCompressed(_format)
  4970. ? _width*_height*bpp/8
  4971. : _width*bpp/8
  4972. ;
  4973. total += bx::write(_writer, _height, _err);
  4974. total += bx::write(_writer, _width, _err);
  4975. total += bx::write(_writer, pitchOrLinearSize, _err);
  4976. total += bx::write(_writer, _depth, _err);
  4977. total += bx::write(_writer, uint32_t(_numMips), _err);
  4978. total += bx::writeRep(_writer, 0, 44, _err); // reserved1
  4979. if (UINT32_MAX != ddspf)
  4980. {
  4981. const TranslateDdsPixelFormat& pf = s_translateDdsPixelFormat[ddspf];
  4982. total += bx::write(_writer, uint32_t(8*sizeof(uint32_t) ), _err); // pixelFormatSize
  4983. total += bx::write(_writer, pf.m_flags, _err);
  4984. total += bx::write(_writer, uint32_t(0), _err);
  4985. total += bx::write(_writer, pf.m_bitCount, _err);
  4986. total += bx::write(_writer, pf.m_bitmask, _err);
  4987. }
  4988. else
  4989. {
  4990. total += bx::write(_writer, uint32_t(8*sizeof(uint32_t) ), _err); // pixelFormatSize
  4991. total += bx::write(_writer, uint32_t(DDPF_FOURCC), _err);
  4992. if (UINT32_MAX != fourccFormat)
  4993. {
  4994. total += bx::write(_writer, fourccFormat, _err);
  4995. }
  4996. else
  4997. {
  4998. total += bx::write(_writer, uint32_t(DDS_DX10), _err);
  4999. }
  5000. total += bx::write(_writer, uint32_t(0), _err); // bitCount
  5001. total += bx::writeRep(_writer, 0, 4*sizeof(uint32_t), _err); // bitmask
  5002. }
  5003. uint32_t caps[4] =
  5004. {
  5005. uint32_t(DDSCAPS_TEXTURE | (1 < _numMips ? DDSCAPS_COMPLEX|DDSCAPS_MIPMAP : 0) ),
  5006. uint32_t(_cubeMap ? DDSCAPS2_CUBEMAP|DSCAPS2_CUBEMAP_ALLSIDES : 0),
  5007. 0,
  5008. 0,
  5009. };
  5010. total += bx::write(_writer, caps, sizeof(caps), _err);
  5011. total += bx::writeRep(_writer, 0, 4, _err); // reserved2
  5012. BX_WARN(total-headerStart == DDS_HEADER_SIZE
  5013. , "DDS: Failed to write header size %d (expected: %d)."
  5014. , total-headerStart
  5015. , DDS_HEADER_SIZE
  5016. );
  5017. if (UINT32_MAX != dxgiFormat)
  5018. {
  5019. total += bx::write(_writer, dxgiFormat, _err);
  5020. total += bx::write(_writer, uint32_t(1 < _depth ? DDS_DX10_DIMENSION_TEXTURE3D : DDS_DX10_DIMENSION_TEXTURE2D), _err); // dims
  5021. total += bx::write(_writer, uint32_t(_cubeMap ? DDS_DX10_MISC_TEXTURECUBE : 0), _err); // miscFlags
  5022. total += bx::write(_writer, uint32_t(_numLayers), _err); // arraySize
  5023. total += bx::write(_writer, uint32_t(0), _err); // miscFlags2
  5024. BX_WARN(total-headerStart == DDS_HEADER_SIZE+20
  5025. , "DDS: Failed to write header size %d (expected: %d)."
  5026. , total-headerStart
  5027. , DDS_HEADER_SIZE+20
  5028. );
  5029. BX_UNUSED(headerStart);
  5030. }
  5031. return total;
  5032. }
  5033. int32_t imageWriteDds(bx::WriterI* _writer, ImageContainer& _imageContainer, const void* _data, uint32_t _size, bx::Error* _err)
  5034. {
  5035. BX_ERROR_SCOPE(_err);
  5036. int32_t total = 0;
  5037. total += imageWriteDdsHeader(_writer
  5038. , TextureFormat::Enum(_imageContainer.m_format)
  5039. , _imageContainer.m_cubeMap
  5040. , _imageContainer.m_width
  5041. , _imageContainer.m_height
  5042. , _imageContainer.m_depth
  5043. , _imageContainer.m_numMips
  5044. , _imageContainer.m_numLayers
  5045. , _err
  5046. );
  5047. if (!_err->isOk() )
  5048. {
  5049. return total;
  5050. }
  5051. for (uint8_t side = 0, numSides = _imageContainer.m_numLayers * (_imageContainer.m_cubeMap ? 6 : 1); side < numSides && _err->isOk(); ++side)
  5052. {
  5053. for (uint8_t lod = 0, num = _imageContainer.m_numMips; lod < num && _err->isOk(); ++lod)
  5054. {
  5055. ImageMip mip;
  5056. if (imageGetRawData(_imageContainer, side, lod, _data, _size, mip) )
  5057. {
  5058. total += bx::write(_writer, mip.m_data, mip.m_size, _err);
  5059. }
  5060. }
  5061. }
  5062. return total;
  5063. }
  5064. static int32_t imageWriteKtxHeader(bx::WriterI* _writer, TextureFormat::Enum _format, bool _cubeMap, uint32_t _width, uint32_t _height, uint32_t _depth, uint8_t _numMips, uint32_t _numLayers, bool _srgb, bx::Error* _err)
  5065. {
  5066. BX_ERROR_SCOPE(_err);
  5067. const KtxFormatInfo& tfi = s_translateKtxFormat[_format];
  5068. uint32_t internalFmt = tfi.m_internalFmt;
  5069. if (_srgb && tfi.m_internalFmtSrgb != KTX_ZERO) {
  5070. internalFmt = tfi.m_internalFmtSrgb;
  5071. }
  5072. int32_t total = 0;
  5073. total += bx::write(_writer, "\xabKTX 11\xbb\r\n\x1a\n", 12, _err);
  5074. total += bx::write(_writer, uint32_t(0x04030201), _err);
  5075. total += bx::write(_writer, uint32_t(0), _err); // glType
  5076. total += bx::write(_writer, uint32_t(1), _err); // glTypeSize
  5077. total += bx::write(_writer, uint32_t(0), _err); // glFormat
  5078. total += bx::write(_writer, internalFmt, _err); // glInternalFormat
  5079. total += bx::write(_writer, tfi.m_fmt, _err); // glBaseInternalFormat
  5080. total += bx::write(_writer, _width, _err);
  5081. total += bx::write(_writer, _height, _err);
  5082. total += bx::write(_writer, _depth, _err);
  5083. total += bx::write(_writer, _numLayers, _err); // numberOfArrayElements
  5084. total += bx::write(_writer, _cubeMap ? uint32_t(6) : uint32_t(0), _err);
  5085. total += bx::write(_writer, uint32_t(_numMips), _err);
  5086. total += bx::write(_writer, uint32_t(0), _err); // Meta-data size.
  5087. BX_WARN(total == 64, "KTX: Failed to write header size %d (expected: %d).", total, 64);
  5088. return total;
  5089. }
  5090. int32_t imageWriteKtx(bx::WriterI* _writer, TextureFormat::Enum _format, bool _cubeMap, uint32_t _width, uint32_t _height, uint32_t _depth, uint8_t _numMips, uint32_t _numLayers, bool _srgb, const void* _src, bx::Error* _err)
  5091. {
  5092. BX_ERROR_SCOPE(_err);
  5093. int32_t total = 0;
  5094. total += imageWriteKtxHeader(_writer, _format, _cubeMap, _width, _height, _depth, _numMips, _numLayers, _srgb, _err);
  5095. if (!_err->isOk() )
  5096. {
  5097. return total;
  5098. }
  5099. const ImageBlockInfo& blockInfo = s_imageBlockInfo[_format];
  5100. const uint32_t blockWidth = blockInfo.blockWidth;
  5101. const uint32_t blockHeight = blockInfo.blockHeight;
  5102. const uint32_t minBlockX = blockInfo.minBlockX;
  5103. const uint32_t minBlockY = blockInfo.minBlockY;
  5104. const uint8_t blockSize = blockInfo.blockSize;
  5105. const uint8_t* src = (const uint8_t*)_src;
  5106. const uint32_t numLayers = bx::max<uint32_t>(_numLayers, 1);
  5107. const uint32_t numSides = _cubeMap ? 6 : 1;
  5108. uint32_t width = _width;
  5109. uint32_t height = _height;
  5110. uint32_t depth = _depth;
  5111. for (uint8_t lod = 0; lod < _numMips && _err->isOk(); ++lod)
  5112. {
  5113. width = bx::max<uint32_t>(blockWidth * minBlockX, ( (width + blockWidth - 1) / blockWidth )*blockWidth);
  5114. height = bx::max<uint32_t>(blockHeight * minBlockY, ( (height + blockHeight - 1) / blockHeight)*blockHeight);
  5115. depth = bx::max<uint32_t>(1, depth);
  5116. const uint32_t mipSize = width/blockWidth * height/blockHeight * depth * blockSize;
  5117. const uint32_t size = mipSize * numLayers * numSides;
  5118. total += bx::write(_writer, size, _err);
  5119. for (uint32_t layer = 0; layer < numLayers && _err->isOk(); ++layer)
  5120. {
  5121. for (uint8_t side = 0; side < numSides && _err->isOk(); ++side)
  5122. {
  5123. total += bx::write(_writer, src, mipSize, _err);
  5124. src += mipSize;
  5125. }
  5126. }
  5127. width >>= 1;
  5128. height >>= 1;
  5129. depth >>= 1;
  5130. }
  5131. return total;
  5132. }
  5133. int32_t imageWriteKtx(bx::WriterI* _writer, ImageContainer& _imageContainer, const void* _data, uint32_t _size, bx::Error* _err)
  5134. {
  5135. BX_ERROR_SCOPE(_err);
  5136. int32_t total = 0;
  5137. total += imageWriteKtxHeader(_writer
  5138. , TextureFormat::Enum(_imageContainer.m_format)
  5139. , _imageContainer.m_cubeMap
  5140. , _imageContainer.m_width
  5141. , _imageContainer.m_height
  5142. , _imageContainer.m_depth
  5143. , _imageContainer.m_numMips
  5144. , _imageContainer.m_numLayers
  5145. , _imageContainer.m_srgb
  5146. , _err
  5147. );
  5148. if (!_err->isOk() )
  5149. {
  5150. return total;
  5151. }
  5152. const uint32_t numMips = _imageContainer.m_numMips;
  5153. const uint32_t numLayers = bx::max<uint32_t>(_imageContainer.m_numLayers, 1);
  5154. const uint32_t numSides = _imageContainer.m_cubeMap ? 6 : 1;
  5155. for (uint8_t lod = 0; lod < numMips && _err->isOk(); ++lod)
  5156. {
  5157. ImageMip mip;
  5158. imageGetRawData(_imageContainer, 0, lod, _data, _size, mip);
  5159. const uint32_t size = mip.m_size*numSides*numLayers;
  5160. total += bx::write(_writer, size, _err);
  5161. for (uint32_t layer = 0; layer < numLayers && _err->isOk(); ++layer)
  5162. {
  5163. for (uint8_t side = 0; side < numSides && _err->isOk(); ++side)
  5164. {
  5165. if (imageGetRawData(_imageContainer, uint16_t(layer*numSides + side), lod, _data, _size, mip) )
  5166. {
  5167. total += bx::write(_writer, mip.m_data, mip.m_size, _err);
  5168. }
  5169. }
  5170. }
  5171. }
  5172. return total;
  5173. }
  5174. } // namespace bimg