image.cpp 214 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243224422452246224722482249225022512252225322542255225622572258225922602261226222632264226522662267226822692270227122722273227422752276227722782279228022812282228322842285228622872288228922902291229222932294229522962297229822992300230123022303230423052306230723082309231023112312231323142315231623172318231923202321232223232324232523262327232823292330233123322333233423352336233723382339234023412342234323442345234623472348234923502351235223532354235523562357235823592360236123622363236423652366236723682369237023712372237323742375237623772378237923802381238223832384238523862387238823892390239123922393239423952396239723982399240024012402240324042405240624072408240924102411241224132414241524162417241824192420242124222423242424252426242724282429243024312432243324342435243624372438243924402441244224432444244524462447244824492450245124522453245424552456245724582459246024612462246324642465246624672468246924702471247224732474247524762477247824792480248124822483248424852486248724882489249024912492249324942495249624972498249925002501250225032504250525062507250825092510251125122513251425152516251725182519252025212522252325242525252625272528252925302531253225332534253525362537253825392540254125422543254425452546254725482549255025512552255325542555255625572558255925602561256225632564256525662567256825692570257125722573257425752576257725782579258025812582258325842585258625872588258925902591259225932594259525962597259825992600260126022603260426052606260726082609261026112612261326142615261626172618261926202621262226232624262526262627262826292630263126322633263426352636263726382639264026412642264326442645264626472648264926502651265226532654265526562657265826592660266126622663266426652666266726682669267026712672267326742675267626772678267926802681268226832684268526862687268826892690269126922693269426952696269726982699270027012702270327042705270627072708270927102711271227132714271527162717271827192720272127222723272427252726272727282729273027312732273327342735273627372738273927402741274227432744274527462747274827492750275127522753275427552756275727582759276027612762276327642765276627672768276927702771277227732774277527762777277827792780278127822783278427852786278727882789279027912792279327942795279627972798279928002801280228032804280528062807280828092810281128122813281428152816281728182819282028212822282328242825282628272828282928302831283228332834283528362837283828392840284128422843284428452846284728482849285028512852285328542855285628572858285928602861286228632864286528662867286828692870287128722873287428752876287728782879288028812882288328842885288628872888288928902891289228932894289528962897289828992900290129022903290429052906290729082909291029112912291329142915291629172918291929202921292229232924292529262927292829292930293129322933293429352936293729382939294029412942294329442945294629472948294929502951295229532954295529562957295829592960296129622963296429652966296729682969297029712972297329742975297629772978297929802981298229832984298529862987298829892990299129922993299429952996299729982999300030013002300330043005300630073008300930103011301230133014301530163017301830193020302130223023302430253026302730283029303030313032303330343035303630373038303930403041304230433044304530463047304830493050305130523053305430553056305730583059306030613062306330643065306630673068306930703071307230733074307530763077307830793080308130823083308430853086308730883089309030913092309330943095309630973098309931003101310231033104310531063107310831093110311131123113311431153116311731183119312031213122312331243125312631273128312931303131313231333134313531363137313831393140314131423143314431453146314731483149315031513152315331543155315631573158315931603161316231633164316531663167316831693170317131723173317431753176317731783179318031813182318331843185318631873188318931903191319231933194319531963197319831993200320132023203320432053206320732083209321032113212321332143215321632173218321932203221322232233224322532263227322832293230323132323233323432353236323732383239324032413242324332443245324632473248324932503251325232533254325532563257325832593260326132623263326432653266326732683269327032713272327332743275327632773278327932803281328232833284328532863287328832893290329132923293329432953296329732983299330033013302330333043305330633073308330933103311331233133314331533163317331833193320332133223323332433253326332733283329333033313332333333343335333633373338333933403341334233433344334533463347334833493350335133523353335433553356335733583359336033613362336333643365336633673368336933703371337233733374337533763377337833793380338133823383338433853386338733883389339033913392339333943395339633973398339934003401340234033404340534063407340834093410341134123413341434153416341734183419342034213422342334243425342634273428342934303431343234333434343534363437343834393440344134423443344434453446344734483449345034513452345334543455345634573458345934603461346234633464346534663467346834693470347134723473347434753476347734783479348034813482348334843485348634873488348934903491349234933494349534963497349834993500350135023503350435053506350735083509351035113512351335143515351635173518351935203521352235233524352535263527352835293530353135323533353435353536353735383539354035413542354335443545354635473548354935503551355235533554355535563557355835593560356135623563356435653566356735683569357035713572357335743575357635773578357935803581358235833584358535863587358835893590359135923593359435953596359735983599360036013602360336043605360636073608360936103611361236133614361536163617361836193620362136223623362436253626362736283629363036313632363336343635363636373638363936403641364236433644364536463647364836493650365136523653365436553656365736583659366036613662366336643665366636673668366936703671367236733674367536763677367836793680368136823683368436853686368736883689369036913692369336943695369636973698369937003701370237033704370537063707370837093710371137123713371437153716371737183719372037213722372337243725372637273728372937303731373237333734373537363737373837393740374137423743374437453746374737483749375037513752375337543755375637573758375937603761376237633764376537663767376837693770377137723773377437753776377737783779378037813782378337843785378637873788378937903791379237933794379537963797379837993800380138023803380438053806380738083809381038113812381338143815381638173818381938203821382238233824382538263827382838293830383138323833383438353836383738383839384038413842384338443845384638473848384938503851385238533854385538563857385838593860386138623863386438653866386738683869387038713872387338743875387638773878387938803881388238833884388538863887388838893890389138923893389438953896389738983899390039013902390339043905390639073908390939103911391239133914391539163917391839193920392139223923392439253926392739283929393039313932393339343935393639373938393939403941394239433944394539463947394839493950395139523953395439553956395739583959396039613962396339643965396639673968396939703971397239733974397539763977397839793980398139823983398439853986398739883989399039913992399339943995399639973998399940004001400240034004400540064007400840094010401140124013401440154016401740184019402040214022402340244025402640274028402940304031403240334034403540364037403840394040404140424043404440454046404740484049405040514052405340544055405640574058405940604061406240634064406540664067406840694070407140724073407440754076407740784079408040814082408340844085408640874088408940904091409240934094409540964097409840994100410141024103410441054106410741084109411041114112411341144115411641174118411941204121412241234124412541264127412841294130413141324133413441354136413741384139414041414142414341444145414641474148414941504151415241534154415541564157415841594160416141624163416441654166416741684169417041714172417341744175417641774178417941804181418241834184418541864187418841894190419141924193419441954196419741984199420042014202420342044205420642074208420942104211421242134214421542164217421842194220422142224223422442254226422742284229423042314232423342344235423642374238423942404241424242434244424542464247424842494250425142524253425442554256425742584259426042614262426342644265426642674268426942704271427242734274427542764277427842794280428142824283428442854286428742884289429042914292429342944295429642974298429943004301430243034304430543064307430843094310431143124313431443154316431743184319432043214322432343244325432643274328432943304331433243334334433543364337433843394340434143424343434443454346434743484349435043514352435343544355435643574358435943604361436243634364436543664367436843694370437143724373437443754376437743784379438043814382438343844385438643874388438943904391439243934394439543964397439843994400440144024403440444054406440744084409441044114412441344144415441644174418441944204421442244234424442544264427442844294430443144324433443444354436443744384439444044414442444344444445444644474448444944504451445244534454445544564457445844594460446144624463446444654466446744684469447044714472447344744475447644774478447944804481448244834484448544864487448844894490449144924493449444954496449744984499450045014502450345044505450645074508450945104511451245134514451545164517451845194520452145224523452445254526452745284529453045314532453345344535453645374538453945404541454245434544454545464547454845494550455145524553455445554556455745584559456045614562456345644565456645674568456945704571457245734574457545764577457845794580458145824583458445854586458745884589459045914592459345944595459645974598459946004601460246034604460546064607460846094610461146124613461446154616461746184619462046214622462346244625462646274628462946304631463246334634463546364637463846394640464146424643464446454646464746484649465046514652465346544655465646574658465946604661466246634664466546664667466846694670467146724673467446754676467746784679468046814682468346844685468646874688468946904691469246934694469546964697469846994700470147024703470447054706470747084709471047114712471347144715471647174718471947204721472247234724472547264727472847294730473147324733473447354736473747384739474047414742474347444745474647474748474947504751475247534754475547564757475847594760476147624763476447654766476747684769477047714772477347744775477647774778477947804781478247834784478547864787478847894790479147924793479447954796479747984799480048014802480348044805480648074808480948104811481248134814481548164817481848194820482148224823482448254826482748284829483048314832483348344835483648374838483948404841484248434844484548464847484848494850485148524853485448554856485748584859486048614862486348644865486648674868486948704871487248734874487548764877487848794880488148824883488448854886488748884889489048914892489348944895489648974898489949004901490249034904490549064907490849094910491149124913491449154916491749184919492049214922492349244925492649274928492949304931493249334934493549364937493849394940494149424943494449454946494749484949495049514952495349544955495649574958495949604961496249634964496549664967496849694970497149724973497449754976497749784979498049814982498349844985498649874988498949904991499249934994499549964997499849995000500150025003500450055006500750085009501050115012501350145015501650175018501950205021502250235024502550265027502850295030503150325033503450355036503750385039504050415042504350445045504650475048504950505051505250535054505550565057505850595060506150625063506450655066506750685069507050715072507350745075507650775078507950805081508250835084508550865087508850895090509150925093509450955096509750985099510051015102510351045105510651075108510951105111511251135114511551165117511851195120512151225123512451255126512751285129513051315132513351345135513651375138513951405141514251435144514551465147514851495150515151525153515451555156515751585159516051615162516351645165516651675168516951705171517251735174517551765177517851795180518151825183518451855186518751885189519051915192519351945195519651975198519952005201520252035204520552065207520852095210521152125213521452155216521752185219522052215222522352245225522652275228522952305231523252335234523552365237523852395240524152425243524452455246524752485249525052515252525352545255525652575258525952605261526252635264526552665267526852695270527152725273527452755276527752785279528052815282528352845285528652875288528952905291529252935294529552965297529852995300530153025303530453055306530753085309531053115312531353145315531653175318531953205321532253235324532553265327532853295330533153325333533453355336533753385339534053415342534353445345534653475348534953505351535253535354535553565357535853595360536153625363536453655366536753685369537053715372537353745375537653775378537953805381538253835384538553865387538853895390539153925393539453955396539753985399540054015402540354045405540654075408540954105411541254135414541554165417541854195420542154225423542454255426542754285429543054315432543354345435543654375438543954405441544254435444544554465447544854495450545154525453545454555456545754585459546054615462546354645465546654675468546954705471547254735474547554765477547854795480548154825483548454855486548754885489549054915492549354945495549654975498549955005501550255035504550555065507550855095510551155125513551455155516551755185519552055215522552355245525552655275528552955305531553255335534553555365537553855395540554155425543554455455546554755485549555055515552555355545555555655575558555955605561556255635564556555665567556855695570557155725573557455755576557755785579558055815582558355845585558655875588558955905591559255935594559555965597559855995600560156025603560456055606560756085609561056115612561356145615561656175618561956205621562256235624562556265627562856295630563156325633563456355636563756385639564056415642564356445645564656475648564956505651565256535654565556565657565856595660566156625663566456655666566756685669567056715672567356745675567656775678567956805681568256835684568556865687568856895690569156925693569456955696569756985699570057015702570357045705570657075708570957105711571257135714571557165717571857195720572157225723572457255726572757285729573057315732573357345735573657375738573957405741574257435744574557465747574857495750575157525753575457555756575757585759576057615762576357645765576657675768576957705771577257735774577557765777577857795780578157825783578457855786578757885789579057915792579357945795579657975798579958005801580258035804580558065807580858095810581158125813581458155816581758185819582058215822582358245825582658275828582958305831583258335834583558365837583858395840584158425843584458455846584758485849585058515852585358545855585658575858585958605861586258635864586558665867586858695870587158725873587458755876587758785879588058815882588358845885588658875888588958905891589258935894589558965897589858995900590159025903590459055906590759085909591059115912591359145915591659175918591959205921592259235924592559265927592859295930593159325933593459355936593759385939594059415942594359445945594659475948594959505951595259535954595559565957595859595960596159625963596459655966596759685969597059715972597359745975597659775978597959805981598259835984598559865987598859895990599159925993599459955996599759985999600060016002600360046005600660076008600960106011601260136014601560166017601860196020602160226023
  1. /*
  2. * Copyright 2011-2025 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. 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. 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. 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. output->m_srgb = imageContainer.m_srgb;
  1176. output->m_hasAlpha = imageContainer.m_hasAlpha;
  1177. const uint16_t numSides = imageContainer.m_numLayers * (imageContainer.m_cubeMap ? 6 : 1);
  1178. for (uint16_t side = 0; side < numSides; ++side)
  1179. {
  1180. for (uint8_t lod = 0, num = imageContainer.m_numMips; lod < num; ++lod)
  1181. {
  1182. ImageMip dstMip;
  1183. if (imageGetRawData(*output, side, lod, output->m_data, output->m_size, dstMip) )
  1184. {
  1185. ImageMip mip;
  1186. if (imageGetRawData(imageContainer, side, lod, _src, _size, mip) )
  1187. {
  1188. uint8_t* dstData = const_cast<uint8_t*>(dstMip.m_data);
  1189. bx::memCopy(dstData, mip.m_data, mip.m_size);
  1190. }
  1191. }
  1192. }
  1193. }
  1194. return output;
  1195. }
  1196. static uint8_t bitRangeConvert(uint32_t _in, uint32_t _from, uint32_t _to)
  1197. {
  1198. using namespace bx;
  1199. uint32_t tmp0 = uint32_sll(1, _to);
  1200. uint32_t tmp1 = uint32_sll(1, _from);
  1201. uint32_t tmp2 = uint32_dec(tmp0);
  1202. uint32_t tmp3 = uint32_dec(tmp1);
  1203. uint32_t tmp4 = uint32_mul(_in, tmp2);
  1204. uint32_t tmp5 = uint32_add(tmp3, tmp4);
  1205. uint32_t tmp6 = uint32_srl(tmp5, _from);
  1206. uint32_t tmp7 = uint32_add(tmp5, tmp6);
  1207. uint32_t result = uint32_srl(tmp7, _from);
  1208. return uint8_t(result);
  1209. }
  1210. static void decodeBlockDxt(uint8_t _dst[16*4], const uint8_t _src[8])
  1211. {
  1212. if (!BX_ENABLED(BIMG_DECODE_BC2 || BIMG_DECODE_BC3) )
  1213. {
  1214. return;
  1215. }
  1216. uint8_t colors[4*3];
  1217. uint32_t c0 = _src[0] | (_src[1] << 8);
  1218. colors[0] = bitRangeConvert( (c0>> 0)&0x1f, 5, 8);
  1219. colors[1] = bitRangeConvert( (c0>> 5)&0x3f, 6, 8);
  1220. colors[2] = bitRangeConvert( (c0>>11)&0x1f, 5, 8);
  1221. uint32_t c1 = _src[2] | (_src[3] << 8);
  1222. colors[3] = bitRangeConvert( (c1>> 0)&0x1f, 5, 8);
  1223. colors[4] = bitRangeConvert( (c1>> 5)&0x3f, 6, 8);
  1224. colors[5] = bitRangeConvert( (c1>>11)&0x1f, 5, 8);
  1225. colors[6] = (2*colors[0] + colors[3]) / 3;
  1226. colors[7] = (2*colors[1] + colors[4]) / 3;
  1227. colors[8] = (2*colors[2] + colors[5]) / 3;
  1228. colors[ 9] = (colors[0] + 2*colors[3]) / 3;
  1229. colors[10] = (colors[1] + 2*colors[4]) / 3;
  1230. colors[11] = (colors[2] + 2*colors[5]) / 3;
  1231. for (uint32_t ii = 0, next = 8*4; ii < 16*4; ii += 4, next += 2)
  1232. {
  1233. int idx = ( (_src[next>>3] >> (next & 7) ) & 3) * 3;
  1234. _dst[ii+0] = colors[idx+0];
  1235. _dst[ii+1] = colors[idx+1];
  1236. _dst[ii+2] = colors[idx+2];
  1237. }
  1238. }
  1239. static void decodeBlockDxt1(uint8_t _dst[16*4], const uint8_t _src[8])
  1240. {
  1241. if (!BX_ENABLED(BIMG_DECODE_BC1 || BIMG_DECODE_BC2 || BIMG_DECODE_BC3) )
  1242. {
  1243. return;
  1244. }
  1245. uint8_t colors[4*4];
  1246. uint32_t c0 = _src[0] | (_src[1] << 8);
  1247. colors[0] = bitRangeConvert( (c0>> 0)&0x1f, 5, 8);
  1248. colors[1] = bitRangeConvert( (c0>> 5)&0x3f, 6, 8);
  1249. colors[2] = bitRangeConvert( (c0>>11)&0x1f, 5, 8);
  1250. colors[3] = 255;
  1251. uint32_t c1 = _src[2] | (_src[3] << 8);
  1252. colors[4] = bitRangeConvert( (c1>> 0)&0x1f, 5, 8);
  1253. colors[5] = bitRangeConvert( (c1>> 5)&0x3f, 6, 8);
  1254. colors[6] = bitRangeConvert( (c1>>11)&0x1f, 5, 8);
  1255. colors[7] = 255;
  1256. if (c0 > c1)
  1257. {
  1258. colors[ 8] = (2*colors[0] + colors[4]) / 3;
  1259. colors[ 9] = (2*colors[1] + colors[5]) / 3;
  1260. colors[10] = (2*colors[2] + colors[6]) / 3;
  1261. colors[11] = 255;
  1262. colors[12] = (colors[0] + 2*colors[4]) / 3;
  1263. colors[13] = (colors[1] + 2*colors[5]) / 3;
  1264. colors[14] = (colors[2] + 2*colors[6]) / 3;
  1265. colors[15] = 255;
  1266. }
  1267. else
  1268. {
  1269. colors[ 8] = (colors[0] + colors[4]) / 2;
  1270. colors[ 9] = (colors[1] + colors[5]) / 2;
  1271. colors[10] = (colors[2] + colors[6]) / 2;
  1272. colors[11] = 255;
  1273. colors[12] = 0;
  1274. colors[13] = 0;
  1275. colors[14] = 0;
  1276. colors[15] = 0;
  1277. }
  1278. for (uint32_t ii = 0, next = 8*4; ii < 16*4; ii += 4, next += 2)
  1279. {
  1280. int idx = ( (_src[next>>3] >> (next & 7) ) & 3) * 4;
  1281. _dst[ii+0] = colors[idx+0];
  1282. _dst[ii+1] = colors[idx+1];
  1283. _dst[ii+2] = colors[idx+2];
  1284. _dst[ii+3] = colors[idx+3];
  1285. }
  1286. }
  1287. static void decodeBlockDxt23A(uint8_t _dst[16*4], const uint8_t _src[8])
  1288. {
  1289. if (!BX_ENABLED(BIMG_DECODE_BC2) )
  1290. {
  1291. return;
  1292. }
  1293. for (uint32_t ii = 0, next = 0; ii < 16*4; ii += 4, next += 4)
  1294. {
  1295. uint32_t c0 = (_src[next>>3] >> (next&7) ) & 0xf;
  1296. _dst[ii] = bitRangeConvert(c0, 4, 8);
  1297. }
  1298. }
  1299. static void decodeBlockDxt45A(uint8_t _dst[16*4], const uint8_t _src[8])
  1300. {
  1301. if (!BX_ENABLED(BIMG_DECODE_BC3 || BIMG_DECODE_BC4 || BIMG_DECODE_BC5) )
  1302. {
  1303. return;
  1304. }
  1305. uint8_t alpha[8];
  1306. alpha[0] = _src[0];
  1307. alpha[1] = _src[1];
  1308. if (alpha[0] > alpha[1])
  1309. {
  1310. alpha[2] = (6*alpha[0] + 1*alpha[1]) / 7;
  1311. alpha[3] = (5*alpha[0] + 2*alpha[1]) / 7;
  1312. alpha[4] = (4*alpha[0] + 3*alpha[1]) / 7;
  1313. alpha[5] = (3*alpha[0] + 4*alpha[1]) / 7;
  1314. alpha[6] = (2*alpha[0] + 5*alpha[1]) / 7;
  1315. alpha[7] = (1*alpha[0] + 6*alpha[1]) / 7;
  1316. }
  1317. else
  1318. {
  1319. alpha[2] = (4*alpha[0] + 1*alpha[1]) / 5;
  1320. alpha[3] = (3*alpha[0] + 2*alpha[1]) / 5;
  1321. alpha[4] = (2*alpha[0] + 3*alpha[1]) / 5;
  1322. alpha[5] = (1*alpha[0] + 4*alpha[1]) / 5;
  1323. alpha[6] = 0;
  1324. alpha[7] = 255;
  1325. }
  1326. uint32_t idx0 = _src[2];
  1327. uint32_t idx1 = _src[5];
  1328. idx0 |= uint32_t(_src[3])<<8;
  1329. idx1 |= uint32_t(_src[6])<<8;
  1330. idx0 |= uint32_t(_src[4])<<16;
  1331. idx1 |= uint32_t(_src[7])<<16;
  1332. for (uint32_t ii = 0; ii < 8*4; ii += 4)
  1333. {
  1334. _dst[ii] = alpha[idx0&7];
  1335. _dst[ii+32] = alpha[idx1&7];
  1336. idx0 >>= 3;
  1337. idx1 >>= 3;
  1338. }
  1339. }
  1340. // BC6H, BC7
  1341. //
  1342. // Reference(s):
  1343. // - https://web.archive.org/web/20181126035446/https://www.khronos.org/registry/OpenGL/extensions/ARB/ARB_texture_compression_bptc.txt
  1344. // - https://web.archive.org/web/20181126035538/https://docs.microsoft.com/en-us/windows/desktop/direct3d11/bc6h-format
  1345. //
  1346. static const uint16_t s_bptcP2[] =
  1347. { // 3210 0000000000 1111111111 2222222222 3333333333
  1348. 0xcccc, // 0, 0, 1, 1, 0, 0, 1, 1, 0, 0, 1, 1, 0, 0, 1, 1
  1349. 0x8888, // 0, 0, 0, 1, 0, 0, 0, 1, 0, 0, 0, 1, 0, 0, 0, 1
  1350. 0xeeee, // 0, 1, 1, 1, 0, 1, 1, 1, 0, 1, 1, 1, 0, 1, 1, 1
  1351. 0xecc8, // 0, 0, 0, 1, 0, 0, 1, 1, 0, 0, 1, 1, 0, 1, 1, 1
  1352. 0xc880, // 0, 0, 0, 0, 0, 0, 0, 1, 0, 0, 0, 1, 0, 0, 1, 1
  1353. 0xfeec, // 0, 0, 1, 1, 0, 1, 1, 1, 0, 1, 1, 1, 1, 1, 1, 1
  1354. 0xfec8, // 0, 0, 0, 1, 0, 0, 1, 1, 0, 1, 1, 1, 1, 1, 1, 1
  1355. 0xec80, // 0, 0, 0, 0, 0, 0, 0, 1, 0, 0, 1, 1, 0, 1, 1, 1
  1356. 0xc800, // 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 0, 0, 1, 1
  1357. 0xffec, // 0, 0, 1, 1, 0, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1
  1358. 0xfe80, // 0, 0, 0, 0, 0, 0, 0, 1, 0, 1, 1, 1, 1, 1, 1, 1
  1359. 0xe800, // 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 0, 1, 1, 1
  1360. 0xffe8, // 0, 0, 0, 1, 0, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1
  1361. 0xff00, // 0, 0, 0, 0, 0, 0, 0, 0, 1, 1, 1, 1, 1, 1, 1, 1
  1362. 0xfff0, // 0, 0, 0, 0, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1
  1363. 0xf000, // 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 1, 1, 1
  1364. 0xf710, // 0, 0, 0, 0, 1, 0, 0, 0, 1, 1, 1, 0, 1, 1, 1, 1
  1365. 0x008e, // 0, 1, 1, 1, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0
  1366. 0x7100, // 0, 0, 0, 0, 0, 0, 0, 0, 1, 0, 0, 0, 1, 1, 1, 0
  1367. 0x08ce, // 0, 1, 1, 1, 0, 0, 1, 1, 0, 0, 0, 1, 0, 0, 0, 0
  1368. 0x008c, // 0, 0, 1, 1, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0
  1369. 0x7310, // 0, 0, 0, 0, 1, 0, 0, 0, 1, 1, 0, 0, 1, 1, 1, 0
  1370. 0x3100, // 0, 0, 0, 0, 0, 0, 0, 0, 1, 0, 0, 0, 1, 1, 0, 0
  1371. 0x8cce, // 0, 1, 1, 1, 0, 0, 1, 1, 0, 0, 1, 1, 0, 0, 0, 1
  1372. 0x088c, // 0, 0, 1, 1, 0, 0, 0, 1, 0, 0, 0, 1, 0, 0, 0, 0
  1373. 0x3110, // 0, 0, 0, 0, 1, 0, 0, 0, 1, 0, 0, 0, 1, 1, 0, 0
  1374. 0x6666, // 0, 1, 1, 0, 0, 1, 1, 0, 0, 1, 1, 0, 0, 1, 1, 0
  1375. 0x366c, // 0, 0, 1, 1, 0, 1, 1, 0, 0, 1, 1, 0, 1, 1, 0, 0
  1376. 0x17e8, // 0, 0, 0, 1, 0, 1, 1, 1, 1, 1, 1, 0, 1, 0, 0, 0
  1377. 0x0ff0, // 0, 0, 0, 0, 1, 1, 1, 1, 1, 1, 1, 1, 0, 0, 0, 0
  1378. 0x718e, // 0, 1, 1, 1, 0, 0, 0, 1, 1, 0, 0, 0, 1, 1, 1, 0
  1379. 0x399c, // 0, 0, 1, 1, 1, 0, 0, 1, 1, 0, 0, 1, 1, 1, 0, 0
  1380. 0xaaaa, // 0, 1, 0, 1, 0, 1, 0, 1, 0, 1, 0, 1, 0, 1, 0, 1
  1381. 0xf0f0, // 0, 0, 0, 0, 1, 1, 1, 1, 0, 0, 0, 0, 1, 1, 1, 1
  1382. 0x5a5a, // 0, 1, 0, 1, 1, 0, 1, 0, 0, 1, 0, 1, 1, 0, 1, 0
  1383. 0x33cc, // 0, 0, 1, 1, 0, 0, 1, 1, 1, 1, 0, 0, 1, 1, 0, 0
  1384. 0x3c3c, // 0, 0, 1, 1, 1, 1, 0, 0, 0, 0, 1, 1, 1, 1, 0, 0
  1385. 0x55aa, // 0, 1, 0, 1, 0, 1, 0, 1, 1, 0, 1, 0, 1, 0, 1, 0
  1386. 0x9696, // 0, 1, 1, 0, 1, 0, 0, 1, 0, 1, 1, 0, 1, 0, 0, 1
  1387. 0xa55a, // 0, 1, 0, 1, 1, 0, 1, 0, 1, 0, 1, 0, 0, 1, 0, 1
  1388. 0x73ce, // 0, 1, 1, 1, 0, 0, 1, 1, 1, 1, 0, 0, 1, 1, 1, 0
  1389. 0x13c8, // 0, 0, 0, 1, 0, 0, 1, 1, 1, 1, 0, 0, 1, 0, 0, 0
  1390. 0x324c, // 0, 0, 1, 1, 0, 0, 1, 0, 0, 1, 0, 0, 1, 1, 0, 0
  1391. 0x3bdc, // 0, 0, 1, 1, 1, 0, 1, 1, 1, 1, 0, 1, 1, 1, 0, 0
  1392. 0x6996, // 0, 1, 1, 0, 1, 0, 0, 1, 1, 0, 0, 1, 0, 1, 1, 0
  1393. 0xc33c, // 0, 0, 1, 1, 1, 1, 0, 0, 1, 1, 0, 0, 0, 0, 1, 1
  1394. 0x9966, // 0, 1, 1, 0, 0, 1, 1, 0, 1, 0, 0, 1, 1, 0, 0, 1
  1395. 0x0660, // 0, 0, 0, 0, 0, 1, 1, 0, 0, 1, 1, 0, 0, 0, 0, 0
  1396. 0x0272, // 0, 1, 0, 0, 1, 1, 1, 0, 0, 1, 0, 0, 0, 0, 0, 0
  1397. 0x04e4, // 0, 0, 1, 0, 0, 1, 1, 1, 0, 0, 1, 0, 0, 0, 0, 0
  1398. 0x4e40, // 0, 0, 0, 0, 0, 0, 1, 0, 0, 1, 1, 1, 0, 0, 1, 0
  1399. 0x2720, // 0, 0, 0, 0, 0, 1, 0, 0, 1, 1, 1, 0, 0, 1, 0, 0
  1400. 0xc936, // 0, 1, 1, 0, 1, 1, 0, 0, 1, 0, 0, 1, 0, 0, 1, 1
  1401. 0x936c, // 0, 0, 1, 1, 0, 1, 1, 0, 1, 1, 0, 0, 1, 0, 0, 1
  1402. 0x39c6, // 0, 1, 1, 0, 0, 0, 1, 1, 1, 0, 0, 1, 1, 1, 0, 0
  1403. 0x639c, // 0, 0, 1, 1, 1, 0, 0, 1, 1, 1, 0, 0, 0, 1, 1, 0
  1404. 0x9336, // 0, 1, 1, 0, 1, 1, 0, 0, 1, 1, 0, 0, 1, 0, 0, 1
  1405. 0x9cc6, // 0, 1, 1, 0, 0, 0, 1, 1, 0, 0, 1, 1, 1, 0, 0, 1
  1406. 0x817e, // 0, 1, 1, 1, 1, 1, 1, 0, 1, 0, 0, 0, 0, 0, 0, 1
  1407. 0xe718, // 0, 0, 0, 1, 1, 0, 0, 0, 1, 1, 1, 0, 0, 1, 1, 1
  1408. 0xccf0, // 0, 0, 0, 0, 1, 1, 1, 1, 0, 0, 1, 1, 0, 0, 1, 1
  1409. 0x0fcc, // 0, 0, 1, 1, 0, 0, 1, 1, 1, 1, 1, 1, 0, 0, 0, 0
  1410. 0x7744, // 0, 0, 1, 0, 0, 0, 1, 0, 1, 1, 1, 0, 1, 1, 1, 0
  1411. 0xee22, // 0, 1, 0, 0, 0, 1, 0, 0, 0, 1, 1, 1, 0, 1, 1, 1
  1412. };
  1413. static const uint32_t s_bptcP3[] =
  1414. { // 76543210 0000 1111 2222 3333 4444 5555 6666 7777
  1415. 0xaa685050, // 0, 0, 1, 1, 0, 0, 1, 1, 0, 2, 2, 1, 2, 2, 2, 2
  1416. 0x6a5a5040, // 0, 0, 0, 1, 0, 0, 1, 1, 2, 2, 1, 1, 2, 2, 2, 1
  1417. 0x5a5a4200, // 0, 0, 0, 0, 2, 0, 0, 1, 2, 2, 1, 1, 2, 2, 1, 1
  1418. 0x5450a0a8, // 0, 2, 2, 2, 0, 0, 2, 2, 0, 0, 1, 1, 0, 1, 1, 1
  1419. 0xa5a50000, // 0, 0, 0, 0, 0, 0, 0, 0, 1, 1, 2, 2, 1, 1, 2, 2
  1420. 0xa0a05050, // 0, 0, 1, 1, 0, 0, 1, 1, 0, 0, 2, 2, 0, 0, 2, 2
  1421. 0x5555a0a0, // 0, 0, 2, 2, 0, 0, 2, 2, 1, 1, 1, 1, 1, 1, 1, 1
  1422. 0x5a5a5050, // 0, 0, 1, 1, 0, 0, 1, 1, 2, 2, 1, 1, 2, 2, 1, 1
  1423. 0xaa550000, // 0, 0, 0, 0, 0, 0, 0, 0, 1, 1, 1, 1, 2, 2, 2, 2
  1424. 0xaa555500, // 0, 0, 0, 0, 1, 1, 1, 1, 1, 1, 1, 1, 2, 2, 2, 2
  1425. 0xaaaa5500, // 0, 0, 0, 0, 1, 1, 1, 1, 2, 2, 2, 2, 2, 2, 2, 2
  1426. 0x90909090, // 0, 0, 1, 2, 0, 0, 1, 2, 0, 0, 1, 2, 0, 0, 1, 2
  1427. 0x94949494, // 0, 1, 1, 2, 0, 1, 1, 2, 0, 1, 1, 2, 0, 1, 1, 2
  1428. 0xa4a4a4a4, // 0, 1, 2, 2, 0, 1, 2, 2, 0, 1, 2, 2, 0, 1, 2, 2
  1429. 0xa9a59450, // 0, 0, 1, 1, 0, 1, 1, 2, 1, 1, 2, 2, 1, 2, 2, 2
  1430. 0x2a0a4250, // 0, 0, 1, 1, 2, 0, 0, 1, 2, 2, 0, 0, 2, 2, 2, 0
  1431. 0xa5945040, // 0, 0, 0, 1, 0, 0, 1, 1, 0, 1, 1, 2, 1, 1, 2, 2
  1432. 0x0a425054, // 0, 1, 1, 1, 0, 0, 1, 1, 2, 0, 0, 1, 2, 2, 0, 0
  1433. 0xa5a5a500, // 0, 0, 0, 0, 1, 1, 2, 2, 1, 1, 2, 2, 1, 1, 2, 2
  1434. 0x55a0a0a0, // 0, 0, 2, 2, 0, 0, 2, 2, 0, 0, 2, 2, 1, 1, 1, 1
  1435. 0xa8a85454, // 0, 1, 1, 1, 0, 1, 1, 1, 0, 2, 2, 2, 0, 2, 2, 2
  1436. 0x6a6a4040, // 0, 0, 0, 1, 0, 0, 0, 1, 2, 2, 2, 1, 2, 2, 2, 1
  1437. 0xa4a45000, // 0, 0, 0, 0, 0, 0, 1, 1, 0, 1, 2, 2, 0, 1, 2, 2
  1438. 0x1a1a0500, // 0, 0, 0, 0, 1, 1, 0, 0, 2, 2, 1, 0, 2, 2, 1, 0
  1439. 0x0050a4a4, // 0, 1, 2, 2, 0, 1, 2, 2, 0, 0, 1, 1, 0, 0, 0, 0
  1440. 0xaaa59090, // 0, 0, 1, 2, 0, 0, 1, 2, 1, 1, 2, 2, 2, 2, 2, 2
  1441. 0x14696914, // 0, 1, 1, 0, 1, 2, 2, 1, 1, 2, 2, 1, 0, 1, 1, 0
  1442. 0x69691400, // 0, 0, 0, 0, 0, 1, 1, 0, 1, 2, 2, 1, 1, 2, 2, 1
  1443. 0xa08585a0, // 0, 0, 2, 2, 1, 1, 0, 2, 1, 1, 0, 2, 0, 0, 2, 2
  1444. 0xaa821414, // 0, 1, 1, 0, 0, 1, 1, 0, 2, 0, 0, 2, 2, 2, 2, 2
  1445. 0x50a4a450, // 0, 0, 1, 1, 0, 1, 2, 2, 0, 1, 2, 2, 0, 0, 1, 1
  1446. 0x6a5a0200, // 0, 0, 0, 0, 2, 0, 0, 0, 2, 2, 1, 1, 2, 2, 2, 1
  1447. 0xa9a58000, // 0, 0, 0, 0, 0, 0, 0, 2, 1, 1, 2, 2, 1, 2, 2, 2
  1448. 0x5090a0a8, // 0, 2, 2, 2, 0, 0, 2, 2, 0, 0, 1, 2, 0, 0, 1, 1
  1449. 0xa8a09050, // 0, 0, 1, 1, 0, 0, 1, 2, 0, 0, 2, 2, 0, 2, 2, 2
  1450. 0x24242424, // 0, 1, 2, 0, 0, 1, 2, 0, 0, 1, 2, 0, 0, 1, 2, 0
  1451. 0x00aa5500, // 0, 0, 0, 0, 1, 1, 1, 1, 2, 2, 2, 2, 0, 0, 0, 0
  1452. 0x24924924, // 0, 1, 2, 0, 1, 2, 0, 1, 2, 0, 1, 2, 0, 1, 2, 0
  1453. 0x24499224, // 0, 1, 2, 0, 2, 0, 1, 2, 1, 2, 0, 1, 0, 1, 2, 0
  1454. 0x50a50a50, // 0, 0, 1, 1, 2, 2, 0, 0, 1, 1, 2, 2, 0, 0, 1, 1
  1455. 0x500aa550, // 0, 0, 1, 1, 1, 1, 2, 2, 2, 2, 0, 0, 0, 0, 1, 1
  1456. 0xaaaa4444, // 0, 1, 0, 1, 0, 1, 0, 1, 2, 2, 2, 2, 2, 2, 2, 2
  1457. 0x66660000, // 0, 0, 0, 0, 0, 0, 0, 0, 2, 1, 2, 1, 2, 1, 2, 1
  1458. 0xa5a0a5a0, // 0, 0, 2, 2, 1, 1, 2, 2, 0, 0, 2, 2, 1, 1, 2, 2
  1459. 0x50a050a0, // 0, 0, 2, 2, 0, 0, 1, 1, 0, 0, 2, 2, 0, 0, 1, 1
  1460. 0x69286928, // 0, 2, 2, 0, 1, 2, 2, 1, 0, 2, 2, 0, 1, 2, 2, 1
  1461. 0x44aaaa44, // 0, 1, 0, 1, 2, 2, 2, 2, 2, 2, 2, 2, 0, 1, 0, 1
  1462. 0x66666600, // 0, 0, 0, 0, 2, 1, 2, 1, 2, 1, 2, 1, 2, 1, 2, 1
  1463. 0xaa444444, // 0, 1, 0, 1, 0, 1, 0, 1, 0, 1, 0, 1, 2, 2, 2, 2
  1464. 0x54a854a8, // 0, 2, 2, 2, 0, 1, 1, 1, 0, 2, 2, 2, 0, 1, 1, 1
  1465. 0x95809580, // 0, 0, 0, 2, 1, 1, 1, 2, 0, 0, 0, 2, 1, 1, 1, 2
  1466. 0x96969600, // 0, 0, 0, 0, 2, 1, 1, 2, 2, 1, 1, 2, 2, 1, 1, 2
  1467. 0xa85454a8, // 0, 2, 2, 2, 0, 1, 1, 1, 0, 1, 1, 1, 0, 2, 2, 2
  1468. 0x80959580, // 0, 0, 0, 2, 1, 1, 1, 2, 1, 1, 1, 2, 0, 0, 0, 2
  1469. 0xaa141414, // 0, 1, 1, 0, 0, 1, 1, 0, 0, 1, 1, 0, 2, 2, 2, 2
  1470. 0x96960000, // 0, 0, 0, 0, 0, 0, 0, 0, 2, 1, 1, 2, 2, 1, 1, 2
  1471. 0xaaaa1414, // 0, 1, 1, 0, 0, 1, 1, 0, 2, 2, 2, 2, 2, 2, 2, 2
  1472. 0xa05050a0, // 0, 0, 2, 2, 0, 0, 1, 1, 0, 0, 1, 1, 0, 0, 2, 2
  1473. 0xa0a5a5a0, // 0, 0, 2, 2, 1, 1, 2, 2, 1, 1, 2, 2, 0, 0, 2, 2
  1474. 0x96000000, // 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 2, 1, 1, 2
  1475. 0x40804080, // 0, 0, 0, 2, 0, 0, 0, 1, 0, 0, 0, 2, 0, 0, 0, 1
  1476. 0xa9a8a9a8, // 0, 2, 2, 2, 1, 2, 2, 2, 0, 2, 2, 2, 1, 2, 2, 2
  1477. 0xaaaaaa44, // 0, 1, 0, 1, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2
  1478. 0x2a4a5254, // 0, 1, 1, 1, 2, 0, 1, 1, 2, 2, 0, 1, 2, 2, 2, 0
  1479. };
  1480. static const uint8_t s_bptcA2[] =
  1481. {
  1482. 15, 15, 15, 15, 15, 15, 15, 15,
  1483. 15, 15, 15, 15, 15, 15, 15, 15,
  1484. 15, 2, 8, 2, 2, 8, 8, 15,
  1485. 2, 8, 2, 2, 8, 8, 2, 2,
  1486. 15, 15, 6, 8, 2, 8, 15, 15,
  1487. 2, 8, 2, 2, 2, 15, 15, 6,
  1488. 6, 2, 6, 8, 15, 15, 2, 2,
  1489. 15, 15, 15, 15, 15, 2, 2, 15,
  1490. };
  1491. static const uint8_t s_bptcA3[2][64] =
  1492. {
  1493. {
  1494. 3, 3, 15, 15, 8, 3, 15, 15,
  1495. 8, 8, 6, 6, 6, 5, 3, 3,
  1496. 3, 3, 8, 15, 3, 3, 6, 10,
  1497. 5, 8, 8, 6, 8, 5, 15, 15,
  1498. 8, 15, 3, 5, 6, 10, 8, 15,
  1499. 15, 3, 15, 5, 15, 15, 15, 15,
  1500. 3, 15, 5, 5, 5, 8, 5, 10,
  1501. 5, 10, 8, 13, 15, 12, 3, 3,
  1502. },
  1503. {
  1504. 15, 8, 8, 3, 15, 15, 3, 8,
  1505. 15, 15, 15, 15, 15, 15, 15, 8,
  1506. 15, 8, 15, 3, 15, 8, 15, 8,
  1507. 3, 15, 6, 10, 15, 15, 10, 8,
  1508. 15, 3, 15, 10, 10, 8, 9, 10,
  1509. 6, 15, 8, 15, 3, 6, 6, 8,
  1510. 15, 3, 15, 15, 15, 15, 15, 15,
  1511. 15, 15, 15, 15, 3, 15, 15, 8,
  1512. },
  1513. };
  1514. static const uint8_t s_bptcFactors[3][16] =
  1515. {
  1516. { 0, 21, 43, 64, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 },
  1517. { 0, 9, 18, 27, 37, 46, 55, 64, 0, 0, 0, 0, 0, 0, 0, 0 },
  1518. { 0, 4, 9, 13, 17, 21, 26, 30, 34, 38, 43, 47, 51, 55, 60, 64 },
  1519. };
  1520. struct BitReader
  1521. {
  1522. BitReader(const uint8_t* _data, uint16_t _bitPos = 0)
  1523. : m_data(_data)
  1524. , m_bitPos(_bitPos)
  1525. {
  1526. }
  1527. uint16_t read(uint8_t _numBits)
  1528. {
  1529. const uint16_t pos = m_bitPos / 8;
  1530. const uint16_t shift = m_bitPos & 7;
  1531. uint32_t data = 0;
  1532. bx::memCopy(&data, &m_data[pos], bx::min(4, 16-pos) );
  1533. m_bitPos += _numBits;
  1534. return uint16_t( (data >> shift) & ( (1 << _numBits)-1) );
  1535. }
  1536. uint16_t peek(uint16_t _offset, uint8_t _numBits)
  1537. {
  1538. const uint16_t bitPos = m_bitPos + _offset;
  1539. const uint16_t shift = bitPos & 7;
  1540. uint16_t pos = bitPos / 8;
  1541. uint32_t data = 0;
  1542. bx::memCopy(&data, &m_data[pos], bx::min(4, 16-pos) );
  1543. return uint8_t( (data >> shift) & ( (1 << _numBits)-1) );
  1544. }
  1545. const uint8_t* m_data;
  1546. uint16_t m_bitPos;
  1547. };
  1548. static uint16_t bc6hUnquantize(uint16_t _value, bool _signed, uint8_t _endpointBits)
  1549. {
  1550. const uint16_t maxValue = 1<<(_endpointBits-1);
  1551. if (_signed)
  1552. {
  1553. if (_endpointBits >= 16)
  1554. {
  1555. return _value;
  1556. }
  1557. const bool sign = !!(_value & 0x8000);
  1558. _value &= 0x7fff;
  1559. uint16_t unq;
  1560. if (0 == _value)
  1561. {
  1562. unq = 0;
  1563. }
  1564. else if (_value >= maxValue-1)
  1565. {
  1566. unq = 0x7fff;
  1567. }
  1568. else
  1569. {
  1570. unq = ( (_value<<15) + 0x4000) >> (_endpointBits-1);
  1571. }
  1572. return sign ? -unq : unq;
  1573. }
  1574. if (_endpointBits >= 15)
  1575. {
  1576. return _value;
  1577. }
  1578. if (0 == _value)
  1579. {
  1580. return 0;
  1581. }
  1582. if (_value == maxValue)
  1583. {
  1584. return UINT16_MAX;
  1585. }
  1586. return ( (_value<<15) + 0x4000) >> (_endpointBits-1);
  1587. }
  1588. static uint16_t bc6hUnquantizeFinal(uint16_t _value, bool _signed)
  1589. {
  1590. if (_signed)
  1591. {
  1592. const uint16_t sign = _value & 0x8000;
  1593. _value &= 0x7fff;
  1594. return ( (_value * 31) >> 5) | sign;
  1595. }
  1596. return (_value * 31) >> 6;
  1597. }
  1598. static uint16_t signExtend(uint16_t _value, uint8_t _numBits)
  1599. {
  1600. const uint16_t mask = 1 << (_numBits - 1);
  1601. const uint16_t result = (_value ^ mask) - mask;
  1602. return result;
  1603. }
  1604. struct Bc6hModeInfo
  1605. {
  1606. uint8_t transformed;
  1607. uint8_t partitionBits;
  1608. uint8_t endpointBits;
  1609. uint8_t deltaBits[3];
  1610. };
  1611. static const Bc6hModeInfo s_bc6hModeInfo[] =
  1612. { // +--------------------------- transformed
  1613. // | +------------------------ partition bits
  1614. // | | +--------------------- endpoint bits
  1615. // | | | +-------------- delta bits
  1616. { 1, 5, 10, { 5, 5, 5 } }, // 00 2-bits
  1617. { 1, 5, 7, { 6, 6, 6 } }, // 01
  1618. { 1, 5, 11, { 5, 4, 4 } }, // 00010 5-bits
  1619. { 0, 0, 10, { 10, 10, 10 } }, // 00011
  1620. { 0, 0, 0, { 0, 0, 0 } }, // -
  1621. { 0, 0, 0, { 0, 0, 0 } }, // -
  1622. { 1, 5, 11, { 4, 5, 4 } }, // 00110
  1623. { 1, 0, 11, { 9, 9, 9 } }, // 00010
  1624. { 0, 0, 0, { 0, 0, 0 } }, // -
  1625. { 0, 0, 0, { 0, 0, 0 } }, // -
  1626. { 1, 5, 11, { 4, 4, 5 } }, // 00010
  1627. { 1, 0, 12, { 8, 8, 8 } }, // 00010
  1628. { 0, 0, 0, { 0, 0, 0 } }, // -
  1629. { 0, 0, 0, { 0, 0, 0 } }, // -
  1630. { 1, 5, 9, { 5, 5, 5 } }, // 00010
  1631. { 1, 0, 16, { 4, 4, 4 } }, // 00010
  1632. { 0, 0, 0, { 0, 0, 0 } }, // -
  1633. { 0, 0, 0, { 0, 0, 0 } }, // -
  1634. { 1, 5, 8, { 6, 5, 5 } }, // 00010
  1635. { 0, 0, 0, { 0, 0, 0 } }, // -
  1636. { 0, 0, 0, { 0, 0, 0 } }, // -
  1637. { 0, 0, 0, { 0, 0, 0 } }, // -
  1638. { 1, 5, 8, { 5, 6, 5 } }, // 00010
  1639. { 0, 0, 0, { 0, 0, 0 } }, // -
  1640. { 0, 0, 0, { 0, 0, 0 } }, // -
  1641. { 0, 0, 0, { 0, 0, 0 } }, // -
  1642. { 1, 5, 8, { 5, 5, 6 } }, // 00010
  1643. { 0, 0, 0, { 0, 0, 0 } }, // -
  1644. { 0, 0, 0, { 0, 0, 0 } }, // -
  1645. { 0, 0, 0, { 0, 0, 0 } }, // -
  1646. { 0, 5, 6, { 6, 6, 6 } }, // 00010
  1647. { 0, 0, 0, { 0, 0, 0 } }, // -
  1648. };
  1649. static void decodeBlockBc6h(uint16_t _dst[16*3], const uint8_t _src[16], bool _signed)
  1650. {
  1651. if (!BX_ENABLED(BIMG_DECODE_BC6) )
  1652. {
  1653. return;
  1654. }
  1655. uint8_t src[16];
  1656. bx::memCopy(src, _src, 16);
  1657. BitReader bit(src);
  1658. uint8_t mode = uint8_t(bit.read(2));
  1659. uint16_t epR[4] = { /* rw, rx, ry, rz */ };
  1660. uint16_t epG[4] = { /* gw, gx, gy, gz */ };
  1661. uint16_t epB[4] = { /* bw, bx, by, bz */ };
  1662. if (mode & 2)
  1663. {
  1664. // 5-bit mode
  1665. mode |= bit.read(3) << 2;
  1666. if (0 == s_bc6hModeInfo[mode].endpointBits)
  1667. {
  1668. bx::memSet(_dst, 0, 16*3*2);
  1669. return;
  1670. }
  1671. switch (mode)
  1672. {
  1673. case 2:
  1674. epR[0] |= bit.read(10) << 0;
  1675. epG[0] |= bit.read(10) << 0;
  1676. epB[0] |= bit.read(10) << 0;
  1677. epR[1] |= bit.read( 5) << 0;
  1678. epR[0] |= bit.read( 1) << 10;
  1679. epG[2] |= bit.read( 4) << 0;
  1680. epG[1] |= bit.read( 4) << 0;
  1681. epG[0] |= bit.read( 1) << 10;
  1682. epB[3] |= bit.read( 1) << 0;
  1683. epG[3] |= bit.read( 4) << 0;
  1684. epB[1] |= bit.read( 4) << 0;
  1685. epB[0] |= bit.read( 1) << 10;
  1686. epB[3] |= bit.read( 1) << 1;
  1687. epB[2] |= bit.read( 4) << 0;
  1688. epR[2] |= bit.read( 5) << 0;
  1689. epB[3] |= bit.read( 1) << 2;
  1690. epR[3] |= bit.read( 5) << 0;
  1691. epB[3] |= bit.read( 1) << 3;
  1692. break;
  1693. case 3:
  1694. epR[0] |= bit.read(10) << 0;
  1695. epG[0] |= bit.read(10) << 0;
  1696. epB[0] |= bit.read(10) << 0;
  1697. epR[1] |= bit.read(10) << 0;
  1698. epG[1] |= bit.read(10) << 0;
  1699. epB[1] |= bit.read(10) << 0;
  1700. break;
  1701. case 6:
  1702. epR[0] |= bit.read(10) << 0;
  1703. epG[0] |= bit.read(10) << 0;
  1704. epB[0] |= bit.read(10) << 0;
  1705. epR[1] |= bit.read( 4) << 0;
  1706. epR[0] |= bit.read( 1) << 10;
  1707. epG[3] |= bit.read( 1) << 4;
  1708. epG[2] |= bit.read( 4) << 0;
  1709. epG[1] |= bit.read( 5) << 0;
  1710. epG[0] |= bit.read( 1) << 10;
  1711. epG[3] |= bit.read( 4) << 0;
  1712. epB[1] |= bit.read( 4) << 0;
  1713. epB[0] |= bit.read( 1) << 10;
  1714. epB[3] |= bit.read( 1) << 1;
  1715. epB[2] |= bit.read( 4) << 0;
  1716. epR[2] |= bit.read( 4) << 0;
  1717. epB[3] |= bit.read( 1) << 0;
  1718. epB[3] |= bit.read( 1) << 2;
  1719. epR[3] |= bit.read( 4) << 0;
  1720. epG[2] |= bit.read( 1) << 4;
  1721. epB[3] |= bit.read( 1) << 3;
  1722. break;
  1723. case 7:
  1724. epR[0] |= bit.read(10) << 0;
  1725. epG[0] |= bit.read(10) << 0;
  1726. epB[0] |= bit.read(10) << 0;
  1727. epR[1] |= bit.read( 9) << 0;
  1728. epR[0] |= bit.read( 1) << 10;
  1729. epG[1] |= bit.read( 9) << 0;
  1730. epG[0] |= bit.read( 1) << 10;
  1731. epB[1] |= bit.read( 9) << 0;
  1732. epB[0] |= bit.read( 1) << 10;
  1733. break;
  1734. case 10:
  1735. epR[0] |= bit.read(10) << 0;
  1736. epG[0] |= bit.read(10) << 0;
  1737. epB[0] |= bit.read(10) << 0;
  1738. epR[1] |= bit.read( 4) << 0;
  1739. epR[0] |= bit.read( 1) << 10;
  1740. epB[2] |= bit.read( 1) << 4;
  1741. epG[2] |= bit.read( 4) << 0;
  1742. epG[1] |= bit.read( 4) << 0;
  1743. epG[0] |= bit.read( 1) << 10;
  1744. epB[3] |= bit.read( 1) << 0;
  1745. epG[3] |= bit.read( 4) << 0;
  1746. epB[1] |= bit.read( 5) << 0;
  1747. epB[0] |= bit.read( 1) << 10;
  1748. epB[2] |= bit.read( 4) << 0;
  1749. epR[2] |= bit.read( 4) << 0;
  1750. epB[3] |= bit.read( 1) << 1;
  1751. epB[3] |= bit.read( 1) << 2;
  1752. epR[3] |= bit.read( 4) << 0;
  1753. epB[3] |= bit.read( 1) << 4;
  1754. epB[3] |= bit.read( 1) << 3;
  1755. break;
  1756. case 11:
  1757. epR[0] |= bit.read(10) << 0;
  1758. epG[0] |= bit.read(10) << 0;
  1759. epB[0] |= bit.read(10) << 0;
  1760. epR[1] |= bit.read( 8) << 0;
  1761. epR[0] |= bit.read( 1) << 11;
  1762. epR[0] |= bit.read( 1) << 10;
  1763. epG[1] |= bit.read( 8) << 0;
  1764. epG[0] |= bit.read( 1) << 11;
  1765. epG[0] |= bit.read( 1) << 10;
  1766. epB[1] |= bit.read( 8) << 0;
  1767. epB[0] |= bit.read( 1) << 11;
  1768. epB[0] |= bit.read( 1) << 10;
  1769. break;
  1770. case 14:
  1771. epR[0] |= bit.read( 9) << 0;
  1772. epB[2] |= bit.read( 1) << 4;
  1773. epG[0] |= bit.read( 9) << 0;
  1774. epG[2] |= bit.read( 1) << 4;
  1775. epB[0] |= bit.read( 9) << 0;
  1776. epB[3] |= bit.read( 1) << 4;
  1777. epR[1] |= bit.read( 5) << 0;
  1778. epG[3] |= bit.read( 1) << 4;
  1779. epG[2] |= bit.read( 4) << 0;
  1780. epG[1] |= bit.read( 5) << 0;
  1781. epB[3] |= bit.read( 1) << 0;
  1782. epG[3] |= bit.read( 4) << 0;
  1783. epB[1] |= bit.read( 5) << 0;
  1784. epB[3] |= bit.read( 1) << 1;
  1785. epB[2] |= bit.read( 4) << 0;
  1786. epR[2] |= bit.read( 5) << 0;
  1787. epB[3] |= bit.read( 1) << 2;
  1788. epR[3] |= bit.read( 5) << 0;
  1789. epB[3] |= bit.read( 1) << 3;
  1790. break;
  1791. case 15:
  1792. epR[0] |= bit.read(10) << 0;
  1793. epG[0] |= bit.read(10) << 0;
  1794. epB[0] |= bit.read(10) << 0;
  1795. epR[1] |= bit.read( 4) << 0;
  1796. epR[0] |= bit.read( 1) << 15;
  1797. epR[0] |= bit.read( 1) << 14;
  1798. epR[0] |= bit.read( 1) << 13;
  1799. epR[0] |= bit.read( 1) << 12;
  1800. epR[0] |= bit.read( 1) << 11;
  1801. epR[0] |= bit.read( 1) << 10;
  1802. epG[1] |= bit.read( 4) << 0;
  1803. epG[0] |= bit.read( 1) << 15;
  1804. epG[0] |= bit.read( 1) << 14;
  1805. epG[0] |= bit.read( 1) << 13;
  1806. epG[0] |= bit.read( 1) << 12;
  1807. epG[0] |= bit.read( 1) << 11;
  1808. epG[0] |= bit.read( 1) << 10;
  1809. epB[1] |= bit.read( 4) << 0;
  1810. epB[0] |= bit.read( 1) << 15;
  1811. epB[0] |= bit.read( 1) << 14;
  1812. epB[0] |= bit.read( 1) << 13;
  1813. epB[0] |= bit.read( 1) << 12;
  1814. epB[0] |= bit.read( 1) << 11;
  1815. epB[0] |= bit.read( 1) << 10;
  1816. break;
  1817. case 18:
  1818. epR[0] |= bit.read( 8) << 0;
  1819. epG[3] |= bit.read( 1) << 4;
  1820. epB[2] |= bit.read( 1) << 4;
  1821. epG[0] |= bit.read( 8) << 0;
  1822. epB[3] |= bit.read( 1) << 2;
  1823. epG[2] |= bit.read( 1) << 4;
  1824. epB[0] |= bit.read( 8) << 0;
  1825. epB[3] |= bit.read( 1) << 3;
  1826. epB[3] |= bit.read( 1) << 4;
  1827. epR[1] |= bit.read( 6) << 0;
  1828. epG[2] |= bit.read( 4) << 0;
  1829. epG[1] |= bit.read( 5) << 0;
  1830. epB[3] |= bit.read( 1) << 0;
  1831. epG[3] |= bit.read( 4) << 0;
  1832. epB[1] |= bit.read( 5) << 0;
  1833. epB[3] |= bit.read( 1) << 1;
  1834. epB[2] |= bit.read( 4) << 0;
  1835. epR[2] |= bit.read( 6) << 0;
  1836. epR[3] |= bit.read( 6) << 0;
  1837. break;
  1838. case 22:
  1839. epR[0] |= bit.read( 8) << 0;
  1840. epB[3] |= bit.read( 1) << 0;
  1841. epB[2] |= bit.read( 1) << 4;
  1842. epG[0] |= bit.read( 8) << 0;
  1843. epG[2] |= bit.read( 1) << 5;
  1844. epG[2] |= bit.read( 1) << 4;
  1845. epB[0] |= bit.read( 8) << 0;
  1846. epG[3] |= bit.read( 1) << 5;
  1847. epB[3] |= bit.read( 1) << 4;
  1848. epR[1] |= bit.read( 5) << 0;
  1849. epG[3] |= bit.read( 1) << 4;
  1850. epG[2] |= bit.read( 4) << 0;
  1851. epG[1] |= bit.read( 6) << 0;
  1852. epG[3] |= bit.read( 4) << 0;
  1853. epB[1] |= bit.read( 5) << 0;
  1854. epB[3] |= bit.read( 1) << 1;
  1855. epB[2] |= bit.read( 4) << 0;
  1856. epR[2] |= bit.read( 5) << 0;
  1857. epB[3] |= bit.read( 1) << 2;
  1858. epR[3] |= bit.read( 5) << 0;
  1859. epB[3] |= bit.read( 1) << 3;
  1860. break;
  1861. case 26:
  1862. epR[0] |= bit.read( 8) << 0;
  1863. epB[3] |= bit.read( 1) << 1;
  1864. epB[2] |= bit.read( 1) << 4;
  1865. epG[0] |= bit.read( 8) << 0;
  1866. epB[2] |= bit.read( 1) << 5;
  1867. epG[2] |= bit.read( 1) << 4;
  1868. epB[0] |= bit.read( 8) << 0;
  1869. epB[3] |= bit.read( 1) << 5;
  1870. epB[3] |= bit.read( 1) << 4;
  1871. epR[1] |= bit.read( 5) << 0;
  1872. epG[3] |= bit.read( 1) << 4;
  1873. epG[2] |= bit.read( 4) << 0;
  1874. epG[1] |= bit.read( 5) << 0;
  1875. epB[3] |= bit.read( 1) << 0;
  1876. epG[3] |= bit.read( 4) << 0;
  1877. epB[1] |= bit.read( 6) << 0;
  1878. epB[2] |= bit.read( 4) << 0;
  1879. epR[2] |= bit.read( 5) << 0;
  1880. epB[3] |= bit.read( 1) << 2;
  1881. epR[3] |= bit.read( 5) << 0;
  1882. epB[3] |= bit.read( 1) << 3;
  1883. break;
  1884. case 30:
  1885. epR[0] |= bit.read( 6) << 0;
  1886. epG[3] |= bit.read( 1) << 4;
  1887. epB[3] |= bit.read( 1) << 0;
  1888. epB[3] |= bit.read( 1) << 1;
  1889. epB[2] |= bit.read( 1) << 4;
  1890. epG[0] |= bit.read( 6) << 0;
  1891. epG[2] |= bit.read( 1) << 5;
  1892. epB[2] |= bit.read( 1) << 5;
  1893. epB[3] |= bit.read( 1) << 2;
  1894. epG[2] |= bit.read( 1) << 4;
  1895. epB[0] |= bit.read( 6) << 0;
  1896. epG[3] |= bit.read( 1) << 5;
  1897. epB[3] |= bit.read( 1) << 3;
  1898. epB[3] |= bit.read( 1) << 5;
  1899. epB[3] |= bit.read( 1) << 4;
  1900. epR[1] |= bit.read( 6) << 0;
  1901. epG[2] |= bit.read( 4) << 0;
  1902. epG[1] |= bit.read( 6) << 0;
  1903. epG[3] |= bit.read( 4) << 0;
  1904. epB[1] |= bit.read( 6) << 0;
  1905. epB[2] |= bit.read( 4) << 0;
  1906. epR[2] |= bit.read( 6) << 0;
  1907. epR[3] |= bit.read( 6) << 0;
  1908. break;
  1909. default:
  1910. break;
  1911. }
  1912. }
  1913. else
  1914. {
  1915. switch (mode)
  1916. {
  1917. case 0:
  1918. epG[2] |= bit.read( 1) << 4;
  1919. epB[2] |= bit.read( 1) << 4;
  1920. epB[3] |= bit.read( 1) << 4;
  1921. epR[0] |= bit.read(10) << 0;
  1922. epG[0] |= bit.read(10) << 0;
  1923. epB[0] |= bit.read(10) << 0;
  1924. epR[1] |= bit.read( 5) << 0;
  1925. epG[3] |= bit.read( 1) << 4;
  1926. epG[2] |= bit.read( 4) << 0;
  1927. epG[1] |= bit.read( 5) << 0;
  1928. epB[3] |= bit.read( 1) << 0;
  1929. epG[3] |= bit.read( 4) << 0;
  1930. epB[1] |= bit.read( 5) << 0;
  1931. epB[3] |= bit.read( 1) << 1;
  1932. epB[2] |= bit.read( 4) << 0;
  1933. epR[2] |= bit.read( 5) << 0;
  1934. epB[3] |= bit.read( 1) << 2;
  1935. epR[3] |= bit.read( 5) << 0;
  1936. epB[3] |= bit.read( 1) << 3;
  1937. break;
  1938. case 1:
  1939. epG[2] |= bit.read( 1) << 5;
  1940. epG[3] |= bit.read( 1) << 4;
  1941. epG[3] |= bit.read( 1) << 5;
  1942. epR[0] |= bit.read( 7) << 0;
  1943. epB[3] |= bit.read( 1) << 0;
  1944. epB[3] |= bit.read( 1) << 1;
  1945. epB[2] |= bit.read( 1) << 4;
  1946. epG[0] |= bit.read( 7) << 0;
  1947. epB[2] |= bit.read( 1) << 5;
  1948. epB[3] |= bit.read( 1) << 2;
  1949. epG[2] |= bit.read( 1) << 4;
  1950. epB[0] |= bit.read( 7) << 0;
  1951. epB[3] |= bit.read( 1) << 3;
  1952. epB[3] |= bit.read( 1) << 5;
  1953. epB[3] |= bit.read( 1) << 4;
  1954. epR[1] |= bit.read( 6) << 0;
  1955. epG[2] |= bit.read( 4) << 0;
  1956. epG[1] |= bit.read( 6) << 0;
  1957. epG[3] |= bit.read( 4) << 0;
  1958. epB[1] |= bit.read( 6) << 0;
  1959. epB[2] |= bit.read( 4) << 0;
  1960. epR[2] |= bit.read( 6) << 0;
  1961. epR[3] |= bit.read( 6) << 0;
  1962. break;
  1963. default:
  1964. break;
  1965. }
  1966. }
  1967. const Bc6hModeInfo mi = s_bc6hModeInfo[mode];
  1968. if (_signed)
  1969. {
  1970. epR[0] = signExtend(epR[0], mi.endpointBits);
  1971. epG[0] = signExtend(epG[0], mi.endpointBits);
  1972. epB[0] = signExtend(epB[0], mi.endpointBits);
  1973. }
  1974. const uint8_t numSubsets = !!mi.partitionBits + 1;
  1975. for (uint8_t ii = 1, num = numSubsets*2; ii < num; ++ii)
  1976. {
  1977. if (_signed
  1978. || mi.transformed)
  1979. {
  1980. epR[ii] = signExtend(epR[ii], mi.deltaBits[0]);
  1981. epG[ii] = signExtend(epG[ii], mi.deltaBits[1]);
  1982. epB[ii] = signExtend(epB[ii], mi.deltaBits[2]);
  1983. }
  1984. if (mi.transformed)
  1985. {
  1986. const uint16_t mask = (1<<mi.endpointBits) - 1;
  1987. epR[ii] = (epR[ii] + epR[0]) & mask;
  1988. epG[ii] = (epG[ii] + epG[0]) & mask;
  1989. epB[ii] = (epB[ii] + epB[0]) & mask;
  1990. if (_signed)
  1991. {
  1992. epR[ii] = signExtend(epR[ii], mi.endpointBits);
  1993. epG[ii] = signExtend(epG[ii], mi.endpointBits);
  1994. epB[ii] = signExtend(epB[ii], mi.endpointBits);
  1995. }
  1996. }
  1997. }
  1998. for (uint8_t ii = 0, num = numSubsets*2; ii < num; ++ii)
  1999. {
  2000. epR[ii] = bc6hUnquantize(epR[ii], _signed, mi.endpointBits);
  2001. epG[ii] = bc6hUnquantize(epG[ii], _signed, mi.endpointBits);
  2002. epB[ii] = bc6hUnquantize(epB[ii], _signed, mi.endpointBits);
  2003. }
  2004. const uint8_t partitionSetIdx = uint8_t(mi.partitionBits ? bit.read(5) : 0);
  2005. const uint8_t indexBits = mi.partitionBits ? 3 : 4;
  2006. const uint8_t* factors = s_bptcFactors[indexBits-2];
  2007. for (uint8_t yy = 0; yy < 4; ++yy)
  2008. {
  2009. for (uint8_t xx = 0; xx < 4; ++xx)
  2010. {
  2011. const uint8_t idx = yy*4+xx;
  2012. uint8_t subsetIndex = 0;
  2013. uint8_t indexAnchor = 0;
  2014. if (0 != mi.partitionBits)
  2015. {
  2016. subsetIndex = (s_bptcP2[partitionSetIdx] >> idx) & 1;
  2017. indexAnchor = subsetIndex ? s_bptcA2[partitionSetIdx] : 0;
  2018. }
  2019. const uint8_t anchor = idx == indexAnchor;
  2020. const uint8_t num = indexBits - anchor;
  2021. const uint8_t index = (uint8_t)bit.read(num);
  2022. const uint8_t fc = factors[index];
  2023. const uint8_t fca = 64 - fc;
  2024. const uint8_t fcb = fc;
  2025. subsetIndex *= 2;
  2026. uint16_t rr = bc6hUnquantizeFinal( (epR[subsetIndex]*fca + epR[subsetIndex + 1]*fcb + 32) >> 6, _signed);
  2027. uint16_t gg = bc6hUnquantizeFinal( (epG[subsetIndex]*fca + epG[subsetIndex + 1]*fcb + 32) >> 6, _signed);
  2028. uint16_t bb = bc6hUnquantizeFinal( (epB[subsetIndex]*fca + epB[subsetIndex + 1]*fcb + 32) >> 6, _signed);
  2029. uint16_t* rgba = &_dst[idx*3];
  2030. rgba[0] = rr;
  2031. rgba[1] = gg;
  2032. rgba[2] = bb;
  2033. }
  2034. }
  2035. }
  2036. static void decodeBlockBc6h(float _dst[16*4], const uint8_t _src[16])
  2037. {
  2038. if (!BX_ENABLED(BIMG_DECODE_BC6) )
  2039. {
  2040. return;
  2041. }
  2042. uint16_t tmp[16*3];
  2043. decodeBlockBc6h(tmp, _src, true);
  2044. for (uint32_t ii = 0; ii < 16; ++ii)
  2045. {
  2046. _dst[ii*4+0] = bx::halfToFloat(tmp[ii*3+0]);
  2047. _dst[ii*4+1] = bx::halfToFloat(tmp[ii*3+1]);
  2048. _dst[ii*4+2] = bx::halfToFloat(tmp[ii*3+2]);
  2049. _dst[ii*4+3] = 1.0f;
  2050. }
  2051. }
  2052. struct Bc7ModeInfo
  2053. {
  2054. uint8_t numSubsets;
  2055. uint8_t partitionBits;
  2056. uint8_t rotationBits;
  2057. uint8_t indexSelectionBits;
  2058. uint8_t colorBits;
  2059. uint8_t alphaBits;
  2060. uint8_t endpointPBits;
  2061. uint8_t sharedPBits;
  2062. uint8_t indexBits[2];
  2063. };
  2064. static const Bc7ModeInfo s_bp7ModeInfo[] =
  2065. { // +---------------------------- num subsets
  2066. // | +------------------------- partition bits
  2067. // | | +---------------------- rotation bits
  2068. // | | | +------------------- index selection bits
  2069. // | | | | +---------------- color bits
  2070. // | | | | | +------------- alpha bits
  2071. // | | | | | | +---------- endpoint P-bits
  2072. // | | | | | | | +------- shared P-bits
  2073. // | | | | | | | | +-- 2x index bits
  2074. { 3, 4, 0, 0, 4, 0, 1, 0, { 3, 0 } }, // 0
  2075. { 2, 6, 0, 0, 6, 0, 0, 1, { 3, 0 } }, // 1
  2076. { 3, 6, 0, 0, 5, 0, 0, 0, { 2, 0 } }, // 2
  2077. { 2, 6, 0, 0, 7, 0, 1, 0, { 2, 0 } }, // 3
  2078. { 1, 0, 2, 1, 5, 6, 0, 0, { 2, 3 } }, // 4
  2079. { 1, 0, 2, 0, 7, 8, 0, 0, { 2, 2 } }, // 5
  2080. { 1, 0, 0, 0, 7, 7, 1, 0, { 4, 0 } }, // 6
  2081. { 2, 6, 0, 0, 5, 5, 1, 0, { 2, 0 } }, // 7
  2082. };
  2083. static void decodeBlockBc7(uint8_t _dst[16*4], const uint8_t _src[16])
  2084. {
  2085. if (!BX_ENABLED(BIMG_DECODE_BC7) )
  2086. {
  2087. return;
  2088. }
  2089. BitReader bit(_src);
  2090. uint8_t mode = 0;
  2091. for (; mode < 8 && 0 == bit.read(1); ++mode)
  2092. {
  2093. }
  2094. if (mode == 8)
  2095. {
  2096. bx::memSet(_dst, 0, 16*4);
  2097. return;
  2098. }
  2099. const Bc7ModeInfo& mi = s_bp7ModeInfo[mode];
  2100. const uint8_t modePBits = 0 != mi.endpointPBits
  2101. ? mi.endpointPBits
  2102. : mi.sharedPBits
  2103. ;
  2104. const uint8_t partitionSetIdx = uint8_t(bit.read(mi.partitionBits) );
  2105. const uint8_t rotationMode = uint8_t(bit.read(mi.rotationBits) );
  2106. const uint8_t indexSelectionMode = uint8_t(bit.read(mi.indexSelectionBits) );
  2107. uint8_t epR[6];
  2108. uint8_t epG[6];
  2109. uint8_t epB[6];
  2110. uint8_t epA[6];
  2111. for (uint8_t ii = 0; ii < mi.numSubsets; ++ii)
  2112. {
  2113. epR[ii*2+0] = uint8_t(bit.read(mi.colorBits) << modePBits);
  2114. epR[ii*2+1] = uint8_t(bit.read(mi.colorBits) << modePBits);
  2115. }
  2116. for (uint8_t ii = 0; ii < mi.numSubsets; ++ii)
  2117. {
  2118. epG[ii*2+0] = uint8_t(bit.read(mi.colorBits) << modePBits);
  2119. epG[ii*2+1] = uint8_t(bit.read(mi.colorBits) << modePBits);
  2120. }
  2121. for (uint8_t ii = 0; ii < mi.numSubsets; ++ii)
  2122. {
  2123. epB[ii*2+0] = uint8_t(bit.read(mi.colorBits) << modePBits);
  2124. epB[ii*2+1] = uint8_t(bit.read(mi.colorBits) << modePBits);
  2125. }
  2126. if (mi.alphaBits)
  2127. {
  2128. for (uint8_t ii = 0; ii < mi.numSubsets; ++ii)
  2129. {
  2130. epA[ii*2+0] = uint8_t(bit.read(mi.alphaBits) << modePBits);
  2131. epA[ii*2+1] = uint8_t(bit.read(mi.alphaBits) << modePBits);
  2132. }
  2133. }
  2134. else
  2135. {
  2136. bx::memSet(epA, 0xff, 6);
  2137. }
  2138. if (0 != modePBits)
  2139. {
  2140. for (uint8_t ii = 0; ii < mi.numSubsets; ++ii)
  2141. {
  2142. const uint8_t pda = uint8_t( bit.read(modePBits) );
  2143. const uint8_t pdb = uint8_t(0 == mi.sharedPBits ? bit.read(modePBits) : pda);
  2144. epR[ii*2+0] |= pda;
  2145. epR[ii*2+1] |= pdb;
  2146. epG[ii*2+0] |= pda;
  2147. epG[ii*2+1] |= pdb;
  2148. epB[ii*2+0] |= pda;
  2149. epB[ii*2+1] |= pdb;
  2150. epA[ii*2+0] |= pda;
  2151. epA[ii*2+1] |= pdb;
  2152. }
  2153. }
  2154. const uint8_t colorBits = mi.colorBits + modePBits;
  2155. for (uint8_t ii = 0; ii < mi.numSubsets; ++ii)
  2156. {
  2157. epR[ii*2+0] = bitRangeConvert(epR[ii*2+0], colorBits, 8);
  2158. epR[ii*2+1] = bitRangeConvert(epR[ii*2+1], colorBits, 8);
  2159. epG[ii*2+0] = bitRangeConvert(epG[ii*2+0], colorBits, 8);
  2160. epG[ii*2+1] = bitRangeConvert(epG[ii*2+1], colorBits, 8);
  2161. epB[ii*2+0] = bitRangeConvert(epB[ii*2+0], colorBits, 8);
  2162. epB[ii*2+1] = bitRangeConvert(epB[ii*2+1], colorBits, 8);
  2163. }
  2164. if (mi.alphaBits)
  2165. {
  2166. const uint8_t alphaBits = mi.alphaBits + modePBits;
  2167. for (uint8_t ii = 0; ii < mi.numSubsets; ++ii)
  2168. {
  2169. epA[ii*2+0] = bitRangeConvert(epA[ii*2+0], alphaBits, 8);
  2170. epA[ii*2+1] = bitRangeConvert(epA[ii*2+1], alphaBits, 8);
  2171. }
  2172. }
  2173. const bool hasIndexBits1 = 0 != mi.indexBits[1];
  2174. const uint8_t* factors[] =
  2175. {
  2176. s_bptcFactors[mi.indexBits[0]-2],
  2177. hasIndexBits1 ? s_bptcFactors[mi.indexBits[1]-2] : factors[0],
  2178. };
  2179. uint16_t offset[2] =
  2180. {
  2181. 0,
  2182. uint16_t(mi.numSubsets*(16*mi.indexBits[0]-1) ),
  2183. };
  2184. for (uint8_t yy = 0; yy < 4; ++yy)
  2185. {
  2186. for (uint8_t xx = 0; xx < 4; ++xx)
  2187. {
  2188. const uint8_t idx = yy*4+xx;
  2189. uint8_t subsetIndex = 0;
  2190. uint8_t indexAnchor = 0;
  2191. switch (mi.numSubsets)
  2192. {
  2193. case 2:
  2194. subsetIndex = (s_bptcP2[partitionSetIdx] >> idx) & 1;
  2195. indexAnchor = 0 != subsetIndex ? s_bptcA2[partitionSetIdx] : 0;
  2196. break;
  2197. case 3:
  2198. subsetIndex = (s_bptcP3[partitionSetIdx] >> (2*idx) ) & 3;
  2199. indexAnchor = 0 != subsetIndex ? s_bptcA3[subsetIndex-1][partitionSetIdx] : 0;
  2200. break;
  2201. default:
  2202. break;
  2203. }
  2204. const uint8_t anchor = idx == indexAnchor;
  2205. const uint8_t num[2] =
  2206. {
  2207. uint8_t( mi.indexBits[0] - anchor ),
  2208. uint8_t(hasIndexBits1 ? mi.indexBits[1] - anchor : 0),
  2209. };
  2210. const uint8_t index[2] =
  2211. {
  2212. (uint8_t)bit.peek(offset[0], num[0]),
  2213. hasIndexBits1 ? (uint8_t)bit.peek(offset[1], num[1]) : index[0],
  2214. };
  2215. offset[0] += num[0];
  2216. offset[1] += num[1];
  2217. const uint8_t fc = factors[ indexSelectionMode][index[ indexSelectionMode] ];
  2218. const uint8_t fa = factors[!indexSelectionMode][index[!indexSelectionMode] ];
  2219. const uint8_t fca = 64 - fc;
  2220. const uint8_t fcb = fc;
  2221. const uint8_t faa = 64 - fa;
  2222. const uint8_t fab = fa;
  2223. subsetIndex *= 2;
  2224. uint8_t rr = uint8_t(uint16_t(epR[subsetIndex]*fca + epR[subsetIndex + 1]*fcb + 32) >> 6);
  2225. uint8_t gg = uint8_t(uint16_t(epG[subsetIndex]*fca + epG[subsetIndex + 1]*fcb + 32) >> 6);
  2226. uint8_t bb = uint8_t(uint16_t(epB[subsetIndex]*fca + epB[subsetIndex + 1]*fcb + 32) >> 6);
  2227. uint8_t aa = uint8_t(uint16_t(epA[subsetIndex]*faa + epA[subsetIndex + 1]*fab + 32) >> 6);
  2228. switch (rotationMode)
  2229. {
  2230. case 1: bx::swap(aa, rr); break;
  2231. case 2: bx::swap(aa, gg); break;
  2232. case 3: bx::swap(aa, bb); break;
  2233. default: break;
  2234. };
  2235. uint8_t* bgra = &_dst[idx*4];
  2236. bgra[0] = bb;
  2237. bgra[1] = gg;
  2238. bgra[2] = rr;
  2239. bgra[3] = aa;
  2240. }
  2241. }
  2242. }
  2243. // ATC
  2244. //
  2245. static void decodeBlockATC(uint8_t _dst[16*4], const uint8_t _src[8])
  2246. {
  2247. if (!BX_ENABLED(BIMG_DECODE_ATC) )
  2248. {
  2249. return;
  2250. }
  2251. uint8_t colors[4*4];
  2252. uint32_t c0 = _src[0] | (_src[1] << 8);
  2253. uint32_t c1 = _src[2] | (_src[3] << 8);
  2254. if (0 == (c0 & 0x8000) )
  2255. {
  2256. colors[ 0] = bitRangeConvert( (c0>> 0)&0x1f, 5, 8);
  2257. colors[ 1] = bitRangeConvert( (c0>> 5)&0x1f, 5, 8);
  2258. colors[ 2] = bitRangeConvert( (c0>>10)&0x1f, 5, 8);
  2259. colors[12] = bitRangeConvert( (c1>> 0)&0x1f, 5, 8);
  2260. colors[13] = bitRangeConvert( (c1>> 5)&0x3f, 6, 8);
  2261. colors[14] = bitRangeConvert( (c1>>11)&0x1f, 5, 8);
  2262. colors[ 4] = (2 * colors[0] + colors[12]) / 3;
  2263. colors[ 5] = (2 * colors[1] + colors[13]) / 3;
  2264. colors[ 6] = (2 * colors[2] + colors[14]) / 3;
  2265. colors[ 8] = (colors[0] + 2 * colors[12]) / 3;
  2266. colors[ 9] = (colors[1] + 2 * colors[13]) / 3;
  2267. colors[10] = (colors[2] + 2 * colors[14]) / 3;
  2268. }
  2269. else
  2270. {
  2271. colors[ 0] = 0;
  2272. colors[ 1] = 0;
  2273. colors[ 2] = 0;
  2274. colors[ 8] = bitRangeConvert( (c0>> 0)&0x1f, 5, 8);
  2275. colors[ 9] = bitRangeConvert( (c0>> 5)&0x1f, 5, 8);
  2276. colors[10] = bitRangeConvert( (c0>>10)&0x1f, 5, 8);
  2277. colors[12] = bitRangeConvert( (c1>> 0)&0x1f, 5, 8);
  2278. colors[13] = bitRangeConvert( (c1>> 5)&0x3f, 6, 8);
  2279. colors[14] = bitRangeConvert( (c1>>11)&0x1f, 5, 8);
  2280. colors[ 4] = colors[ 8] - colors[12] / 4;
  2281. colors[ 5] = colors[ 9] - colors[13] / 4;
  2282. colors[ 6] = colors[10] - colors[14] / 4;
  2283. }
  2284. for (uint32_t ii = 0, next = 8*4; ii < 16*4; ii += 4, next += 2)
  2285. {
  2286. int32_t idx = ( (_src[next>>3] >> (next & 7) ) & 3) * 4;
  2287. _dst[ii+0] = colors[idx+0];
  2288. _dst[ii+1] = colors[idx+1];
  2289. _dst[ii+2] = colors[idx+2];
  2290. _dst[ii+3] = colors[idx+3];
  2291. }
  2292. }
  2293. static const int32_t s_etc1Mod[8][4] =
  2294. {
  2295. { 2, 8, -2, -8 },
  2296. { 5, 17, -5, -17 },
  2297. { 9, 29, -9, -29 },
  2298. { 13, 42, -13, -42 },
  2299. { 18, 60, -18, -60 },
  2300. { 24, 80, -24, -80 },
  2301. { 33, 106, -33, -106 },
  2302. { 47, 183, -47, -183 },
  2303. };
  2304. static const uint8_t s_etc2Mod[] = { 3, 6, 11, 16, 23, 32, 41, 64 };
  2305. static uint8_t uint8_sat(int32_t _a)
  2306. {
  2307. using namespace bx;
  2308. const uint32_t min = uint32_imin(_a, 255);
  2309. const uint32_t result = uint32_imax(min, 0);
  2310. return (uint8_t)result;
  2311. }
  2312. static uint8_t uint8_satadd(int32_t _a, int32_t _b)
  2313. {
  2314. const int32_t add = _a + _b;
  2315. return uint8_sat(add);
  2316. }
  2317. static void decodeBlockEtc2ModeT(uint8_t _dst[16*4], const uint8_t _src[8])
  2318. {
  2319. uint8_t rgb[16];
  2320. // 0 1 2 3 4 5 6 7
  2321. // 7654321076543210765432107654321076543210765432107654321076543210
  2322. // ...rr.rrggggbbbbrrrrggggbbbbDD.Dmmmmmmmmmmmmmmmmllllllllllllllll
  2323. // ^ ^ ^ ^ ^
  2324. // +-- c0 +-- c1 | +-- msb +-- lsb
  2325. // +-- dist
  2326. rgb[ 0] = ( (_src[0] >> 1) & 0xc)
  2327. | (_src[0] & 0x3)
  2328. ;
  2329. rgb[ 1] = _src[1] >> 4;
  2330. rgb[ 2] = _src[1] & 0xf;
  2331. rgb[ 8] = _src[2] >> 4;
  2332. rgb[ 9] = _src[2] & 0xf;
  2333. rgb[10] = _src[3] >> 4;
  2334. rgb[ 0] = bitRangeConvert(rgb[ 0], 4, 8);
  2335. rgb[ 1] = bitRangeConvert(rgb[ 1], 4, 8);
  2336. rgb[ 2] = bitRangeConvert(rgb[ 2], 4, 8);
  2337. rgb[ 8] = bitRangeConvert(rgb[ 8], 4, 8);
  2338. rgb[ 9] = bitRangeConvert(rgb[ 9], 4, 8);
  2339. rgb[10] = bitRangeConvert(rgb[10], 4, 8);
  2340. uint8_t dist = ((_src[3] >> 1) & 0x6) | (_src[3] & 0x1);
  2341. int32_t mod = s_etc2Mod[dist];
  2342. rgb[ 4] = uint8_satadd(rgb[ 8], mod);
  2343. rgb[ 5] = uint8_satadd(rgb[ 9], mod);
  2344. rgb[ 6] = uint8_satadd(rgb[10], mod);
  2345. rgb[12] = uint8_satadd(rgb[ 8], -mod);
  2346. rgb[13] = uint8_satadd(rgb[ 9], -mod);
  2347. rgb[14] = uint8_satadd(rgb[10], -mod);
  2348. uint32_t indexMsb = (_src[4]<<8) | _src[5];
  2349. uint32_t indexLsb = (_src[6]<<8) | _src[7];
  2350. for (uint32_t ii = 0; ii < 16; ++ii)
  2351. {
  2352. const uint32_t idx = (ii&0xc) | ( (ii & 0x3)<<4);
  2353. const uint32_t lsbi = indexLsb & 1;
  2354. const uint32_t msbi = (indexMsb & 1)<<1;
  2355. const uint32_t pal = (lsbi | msbi)<<2;
  2356. _dst[idx + 0] = rgb[pal+2];
  2357. _dst[idx + 1] = rgb[pal+1];
  2358. _dst[idx + 2] = rgb[pal+0];
  2359. _dst[idx + 3] = 255;
  2360. indexLsb >>= 1;
  2361. indexMsb >>= 1;
  2362. }
  2363. }
  2364. static void decodeBlockEtc2ModeH(uint8_t _dst[16*4], const uint8_t _src[8])
  2365. {
  2366. uint8_t rgb[16];
  2367. // 0 1 2 3 4 5 6 7
  2368. // 7654321076543210765432107654321076543210765432107654321076543210
  2369. // .rrrrggg...gb.bbbrrrrggggbbbbD.Dmmmmmmmmmmmmmmmmllllllllllllllll
  2370. // ^ ^ ^ ^ ^
  2371. // +-- c0 +-- c1 | +-- msb +-- lsb
  2372. // +-- dist
  2373. rgb[ 0] = (_src[0] >> 3) & 0xf;
  2374. rgb[ 1] = ( (_src[0] << 1) & 0xe)
  2375. | ( (_src[1] >> 4) & 0x1)
  2376. ;
  2377. rgb[ 2] = (_src[1] & 0x8)
  2378. | ( (_src[1] << 1) & 0x6)
  2379. | (_src[2] >> 7)
  2380. ;
  2381. rgb[ 8] = (_src[2] >> 3) & 0xf;
  2382. rgb[ 9] = ( (_src[2] << 1) & 0xe)
  2383. | (_src[3] >> 7)
  2384. ;
  2385. rgb[10] = (_src[3] >> 3) & 0xf;
  2386. rgb[ 0] = bitRangeConvert(rgb[ 0], 4, 8);
  2387. rgb[ 1] = bitRangeConvert(rgb[ 1], 4, 8);
  2388. rgb[ 2] = bitRangeConvert(rgb[ 2], 4, 8);
  2389. rgb[ 8] = bitRangeConvert(rgb[ 8], 4, 8);
  2390. rgb[ 9] = bitRangeConvert(rgb[ 9], 4, 8);
  2391. rgb[10] = bitRangeConvert(rgb[10], 4, 8);
  2392. uint32_t col0 = uint32_t(rgb[0]<<16) | uint32_t(rgb[1]<<8) | uint32_t(rgb[ 2]);
  2393. uint32_t col1 = uint32_t(rgb[8]<<16) | uint32_t(rgb[9]<<8) | uint32_t(rgb[10]);
  2394. uint8_t dist = (_src[3] & 0x4) | ((_src[3]<<1)&0x2) | (col0 >= col1);
  2395. int32_t mod = s_etc2Mod[dist];
  2396. rgb[ 4] = uint8_satadd(rgb[ 0], -mod);
  2397. rgb[ 5] = uint8_satadd(rgb[ 1], -mod);
  2398. rgb[ 6] = uint8_satadd(rgb[ 2], -mod);
  2399. rgb[ 0] = uint8_satadd(rgb[ 0], mod);
  2400. rgb[ 1] = uint8_satadd(rgb[ 1], mod);
  2401. rgb[ 2] = uint8_satadd(rgb[ 2], mod);
  2402. rgb[12] = uint8_satadd(rgb[ 8], -mod);
  2403. rgb[13] = uint8_satadd(rgb[ 9], -mod);
  2404. rgb[14] = uint8_satadd(rgb[10], -mod);
  2405. rgb[ 8] = uint8_satadd(rgb[ 8], mod);
  2406. rgb[ 9] = uint8_satadd(rgb[ 9], mod);
  2407. rgb[10] = uint8_satadd(rgb[10], mod);
  2408. uint32_t indexMsb = (_src[4]<<8) | _src[5];
  2409. uint32_t indexLsb = (_src[6]<<8) | _src[7];
  2410. for (uint32_t ii = 0; ii < 16; ++ii)
  2411. {
  2412. const uint32_t idx = (ii&0xc) | ( (ii & 0x3)<<4);
  2413. const uint32_t lsbi = indexLsb & 1;
  2414. const uint32_t msbi = (indexMsb & 1)<<1;
  2415. const uint32_t pal = (lsbi | msbi)<<2;
  2416. _dst[idx + 0] = rgb[pal+2];
  2417. _dst[idx + 1] = rgb[pal+1];
  2418. _dst[idx + 2] = rgb[pal+0];
  2419. _dst[idx + 3] = 255;
  2420. indexLsb >>= 1;
  2421. indexMsb >>= 1;
  2422. }
  2423. }
  2424. static void decodeBlockEtc2ModePlanar(uint8_t _dst[16*4], const uint8_t _src[8])
  2425. {
  2426. // 0 1 2 3 4 5 6 7
  2427. // 7654321076543210765432107654321076543210765432107654321076543210
  2428. // .rrrrrrg.ggggggb...bb.bbbrrrrr.rgggggggbbbbbbrrrrrrgggggggbbbbbb
  2429. // ^ ^ ^
  2430. // +-- c0 +-- cH +-- cV
  2431. uint8_t c0[3];
  2432. uint8_t cH[3];
  2433. uint8_t cV[3];
  2434. c0[0] = (_src[0] >> 1) & 0x3f;
  2435. c0[1] = ( (_src[0] & 1) << 6)
  2436. | ( (_src[1] >> 1) & 0x3f)
  2437. ;
  2438. c0[2] = ( (_src[1] & 1) << 5)
  2439. | ( (_src[2] & 0x18) )
  2440. | ( (_src[2] << 1) & 6)
  2441. | ( (_src[3] >> 7) )
  2442. ;
  2443. cH[0] = ( (_src[3] >> 1) & 0x3e)
  2444. | (_src[3] & 1)
  2445. ;
  2446. cH[1] = _src[4] >> 1;
  2447. cH[2] = ( (_src[4] & 1) << 5)
  2448. | (_src[5] >> 3)
  2449. ;
  2450. cV[0] = ( (_src[5] & 0x7) << 3)
  2451. | (_src[6] >> 5)
  2452. ;
  2453. cV[1] = ( (_src[6] & 0x1f) << 2)
  2454. | (_src[7] >> 5)
  2455. ;
  2456. cV[2] = _src[7] & 0x3f;
  2457. c0[0] = bitRangeConvert(c0[0], 6, 8);
  2458. c0[1] = bitRangeConvert(c0[1], 7, 8);
  2459. c0[2] = bitRangeConvert(c0[2], 6, 8);
  2460. cH[0] = bitRangeConvert(cH[0], 6, 8);
  2461. cH[1] = bitRangeConvert(cH[1], 7, 8);
  2462. cH[2] = bitRangeConvert(cH[2], 6, 8);
  2463. cV[0] = bitRangeConvert(cV[0], 6, 8);
  2464. cV[1] = bitRangeConvert(cV[1], 7, 8);
  2465. cV[2] = bitRangeConvert(cV[2], 6, 8);
  2466. int16_t dy[3];
  2467. dy[0] = cV[0] - c0[0];
  2468. dy[1] = cV[1] - c0[1];
  2469. dy[2] = cV[2] - c0[2];
  2470. int16_t sx[3];
  2471. sx[0] = int16_t(c0[0])<<2;
  2472. sx[1] = int16_t(c0[1])<<2;
  2473. sx[2] = int16_t(c0[2])<<2;
  2474. int16_t ex[3];
  2475. ex[0] = int16_t(cH[0])<<2;
  2476. ex[1] = int16_t(cH[1])<<2;
  2477. ex[2] = int16_t(cH[2])<<2;
  2478. for (int32_t vv = 0; vv < 4; ++vv)
  2479. {
  2480. int16_t dx[3];
  2481. dx[0] = (ex[0] - sx[0])>>2;
  2482. dx[1] = (ex[1] - sx[1])>>2;
  2483. dx[2] = (ex[2] - sx[2])>>2;
  2484. for (int32_t hh = 0; hh < 4; ++hh)
  2485. {
  2486. const uint32_t idx = (vv<<4) + (hh<<2);
  2487. _dst[idx + 0] = uint8_sat( (sx[2] + dx[2]*hh)>>2);
  2488. _dst[idx + 1] = uint8_sat( (sx[1] + dx[1]*hh)>>2);
  2489. _dst[idx + 2] = uint8_sat( (sx[0] + dx[0]*hh)>>2);
  2490. _dst[idx + 3] = 255;
  2491. }
  2492. sx[0] += dy[0];
  2493. sx[1] += dy[1];
  2494. sx[2] += dy[2];
  2495. ex[0] += dy[0];
  2496. ex[1] += dy[1];
  2497. ex[2] += dy[2];
  2498. }
  2499. }
  2500. static void decodeBlockEtc12(uint8_t _dst[16*4], const uint8_t _src[8])
  2501. {
  2502. if (!BX_ENABLED(BIMG_DECODE_ETC1 || BIMG_DECODE_ETC2) )
  2503. {
  2504. return;
  2505. }
  2506. bool flipBit = 0 != (_src[3] & 0x1);
  2507. bool diffBit = 0 != (_src[3] & 0x2);
  2508. uint8_t rgb[8];
  2509. if (diffBit)
  2510. {
  2511. rgb[0] = _src[0] >> 3;
  2512. rgb[1] = _src[1] >> 3;
  2513. rgb[2] = _src[2] >> 3;
  2514. int8_t diff[3];
  2515. diff[0] = int8_t( (_src[0] & 0x7)<<5)>>5;
  2516. diff[1] = int8_t( (_src[1] & 0x7)<<5)>>5;
  2517. diff[2] = int8_t( (_src[2] & 0x7)<<5)>>5;
  2518. int8_t rr = rgb[0] + diff[0];
  2519. int8_t gg = rgb[1] + diff[1];
  2520. int8_t bb = rgb[2] + diff[2];
  2521. // Etc2 3-modes
  2522. if (rr < 0 || rr > 31)
  2523. {
  2524. decodeBlockEtc2ModeT(_dst, _src);
  2525. return;
  2526. }
  2527. if (gg < 0 || gg > 31)
  2528. {
  2529. decodeBlockEtc2ModeH(_dst, _src);
  2530. return;
  2531. }
  2532. if (bb < 0 || bb > 31)
  2533. {
  2534. decodeBlockEtc2ModePlanar(_dst, _src);
  2535. return;
  2536. }
  2537. // Etc1
  2538. rgb[0] = bitRangeConvert(rgb[0], 5, 8);
  2539. rgb[1] = bitRangeConvert(rgb[1], 5, 8);
  2540. rgb[2] = bitRangeConvert(rgb[2], 5, 8);
  2541. rgb[4] = bitRangeConvert(rr, 5, 8);
  2542. rgb[5] = bitRangeConvert(gg, 5, 8);
  2543. rgb[6] = bitRangeConvert(bb, 5, 8);
  2544. }
  2545. else
  2546. {
  2547. rgb[0] = _src[0] >> 4;
  2548. rgb[1] = _src[1] >> 4;
  2549. rgb[2] = _src[2] >> 4;
  2550. rgb[4] = _src[0] & 0xf;
  2551. rgb[5] = _src[1] & 0xf;
  2552. rgb[6] = _src[2] & 0xf;
  2553. rgb[0] = bitRangeConvert(rgb[0], 4, 8);
  2554. rgb[1] = bitRangeConvert(rgb[1], 4, 8);
  2555. rgb[2] = bitRangeConvert(rgb[2], 4, 8);
  2556. rgb[4] = bitRangeConvert(rgb[4], 4, 8);
  2557. rgb[5] = bitRangeConvert(rgb[5], 4, 8);
  2558. rgb[6] = bitRangeConvert(rgb[6], 4, 8);
  2559. }
  2560. uint32_t table[2];
  2561. table[0] = (_src[3] >> 5) & 0x7;
  2562. table[1] = (_src[3] >> 2) & 0x7;
  2563. uint32_t indexMsb = (_src[4]<<8) | _src[5];
  2564. uint32_t indexLsb = (_src[6]<<8) | _src[7];
  2565. if (flipBit)
  2566. {
  2567. for (uint32_t ii = 0; ii < 16; ++ii)
  2568. {
  2569. const uint32_t block = (ii>>1)&1;
  2570. const uint32_t color = block<<2;
  2571. const uint32_t idx = (ii&0xc) | ( (ii & 0x3)<<4);
  2572. const uint32_t lsbi = indexLsb & 1;
  2573. const uint32_t msbi = (indexMsb & 1)<<1;
  2574. const int32_t mod = s_etc1Mod[table[block] ][lsbi | msbi];
  2575. _dst[idx + 0] = uint8_satadd(rgb[color+2], mod);
  2576. _dst[idx + 1] = uint8_satadd(rgb[color+1], mod);
  2577. _dst[idx + 2] = uint8_satadd(rgb[color+0], mod);
  2578. _dst[idx + 3] = 255;
  2579. indexLsb >>= 1;
  2580. indexMsb >>= 1;
  2581. }
  2582. }
  2583. else
  2584. {
  2585. for (uint32_t ii = 0; ii < 16; ++ii)
  2586. {
  2587. const uint32_t block = ii>>3;
  2588. const uint32_t color = block<<2;
  2589. const uint32_t idx = (ii&0xc) | ( (ii & 0x3)<<4);
  2590. const uint32_t lsbi = indexLsb & 1;
  2591. const uint32_t msbi = (indexMsb & 1)<<1;
  2592. const int32_t mod = s_etc1Mod[table[block] ][lsbi | msbi];
  2593. _dst[idx + 0] = uint8_satadd(rgb[color+2], mod);
  2594. _dst[idx + 1] = uint8_satadd(rgb[color+1], mod);
  2595. _dst[idx + 2] = uint8_satadd(rgb[color+0], mod);
  2596. _dst[idx + 3] = 255;
  2597. indexLsb >>= 1;
  2598. indexMsb >>= 1;
  2599. }
  2600. }
  2601. }
  2602. static const int8_t s_etc2aMod[16][8] =
  2603. {
  2604. { -3, -6, -9, -15, 2, 5, 8, 14 },
  2605. { -3, -7, -10, -13, 2, 6, 9, 12 },
  2606. { -2, -5, -8, -13, 1, 4, 7, 12 },
  2607. { -2, -4, -6, -13, 1, 3, 5, 12 },
  2608. { -3, -6, -8, -12, 2, 5, 7, 11 },
  2609. { -3, -7, -9, -11, 2, 6, 8, 10 },
  2610. { -4, -7, -8, -11, 3, 6, 7, 10 },
  2611. { -3, -5, -8, -11, 2, 4, 7, 10 },
  2612. { -2, -6, -8, -10, 1, 5, 7, 9 },
  2613. { -2, -5, -8, -10, 1, 4, 7, 9 },
  2614. { -2, -4, -8, -10, 1, 3, 7, 9 },
  2615. { -2, -5, -7, -10, 1, 4, 6, 9 },
  2616. { -3, -4, -7, -10, 2, 3, 6, 9 },
  2617. { -1, -2, -3, -10, 0, 1, 2, 9 },
  2618. { -4, -6, -8, -9, 3, 5, 7, 8 },
  2619. { -3, -5, -7, -9, 2, 4, 6, 8 }
  2620. };
  2621. void decodeBlockEtc2Alpha(uint8_t _dst[16 * 4], const uint8_t _src[8])
  2622. {
  2623. if (!BX_ENABLED(BIMG_DECODE_ETC2))
  2624. {
  2625. return;
  2626. }
  2627. const int32_t bc = _src[0];
  2628. const int8_t *modTable = s_etc2aMod[_src[1] & 0x0f];
  2629. const int32_t mult = (_src[1] & 0xf0) >> 4;
  2630. const uint64_t indices = ((uint64_t)_src[2] << 40)
  2631. | ((uint64_t)_src[3] << 32)
  2632. | ((uint64_t)_src[4] << 24)
  2633. | ((uint64_t)_src[5] << 16)
  2634. | ((uint64_t)_src[6] << 8)
  2635. | _src[7];
  2636. for (int ii = 0; ii < 16; ii++) {
  2637. const uint32_t idx = (ii & 0xc) | ((ii & 0x3) << 4);
  2638. const int32_t mod = modTable[(indices >> (45 - ii * 3)) & 0x7];
  2639. _dst[idx + 3] = uint8_satadd(bc, mod*mult);
  2640. }
  2641. }
  2642. static const uint8_t s_pvrtcFactors[16][4] =
  2643. {
  2644. { 4, 4, 4, 4 },
  2645. { 2, 6, 2, 6 },
  2646. { 8, 0, 8, 0 },
  2647. { 6, 2, 6, 2 },
  2648. { 2, 2, 6, 6 },
  2649. { 1, 3, 3, 9 },
  2650. { 4, 0, 12, 0 },
  2651. { 3, 1, 9, 3 },
  2652. { 8, 8, 0, 0 },
  2653. { 4, 12, 0, 0 },
  2654. { 16, 0, 0, 0 },
  2655. { 12, 4, 0, 0 },
  2656. { 6, 6, 2, 2 },
  2657. { 3, 9, 1, 3 },
  2658. { 12, 0, 4, 0 },
  2659. { 9, 3, 3, 1 },
  2660. };
  2661. static const uint8_t s_pvrtcWeights[8][4] =
  2662. {
  2663. { 8, 0, 8, 0 },
  2664. { 5, 3, 5, 3 },
  2665. { 3, 5, 3, 5 },
  2666. { 0, 8, 0, 8 },
  2667. { 8, 0, 8, 0 },
  2668. { 4, 4, 4, 4 },
  2669. { 4, 4, 4, 4 },
  2670. { 0, 8, 0, 8 },
  2671. };
  2672. uint32_t morton2d(uint32_t _x, uint32_t _y)
  2673. {
  2674. using namespace bx;
  2675. const uint32_t tmpx = uint32_part1by1(_x);
  2676. const uint32_t xbits = uint32_sll(tmpx, 1);
  2677. const uint32_t ybits = uint32_part1by1(_y);
  2678. const uint32_t result = uint32_or(xbits, ybits);
  2679. return result;
  2680. }
  2681. uint32_t getColor(const uint8_t _src[8])
  2682. {
  2683. return 0
  2684. | _src[7]<<24
  2685. | _src[6]<<16
  2686. | _src[5]<<8
  2687. | _src[4]
  2688. ;
  2689. }
  2690. static void decodeBlockPtc14RgbAddA(uint32_t _block, uint32_t* _r, uint32_t* _g, uint32_t* _b, uint8_t _factor)
  2691. {
  2692. if (0 != (_block & (1<<15) ) )
  2693. {
  2694. *_r += bitRangeConvert( (_block >> 10) & 0x1f, 5, 8) * _factor;
  2695. *_g += bitRangeConvert( (_block >> 5) & 0x1f, 5, 8) * _factor;
  2696. *_b += bitRangeConvert( (_block >> 1) & 0x0f, 4, 8) * _factor;
  2697. }
  2698. else
  2699. {
  2700. *_r += bitRangeConvert( (_block >> 8) & 0xf, 4, 8) * _factor;
  2701. *_g += bitRangeConvert( (_block >> 4) & 0xf, 4, 8) * _factor;
  2702. *_b += bitRangeConvert( (_block >> 1) & 0x7, 3, 8) * _factor;
  2703. }
  2704. }
  2705. static void decodeBlockPtc14RgbAddB(uint32_t _block, uint32_t* _r, uint32_t* _g, uint32_t* _b, uint8_t _factor)
  2706. {
  2707. if (0 != (_block & (1<<31) ) )
  2708. {
  2709. *_r += bitRangeConvert( (_block >> 26) & 0x1f, 5, 8) * _factor;
  2710. *_g += bitRangeConvert( (_block >> 21) & 0x1f, 5, 8) * _factor;
  2711. *_b += bitRangeConvert( (_block >> 16) & 0x1f, 5, 8) * _factor;
  2712. }
  2713. else
  2714. {
  2715. *_r += bitRangeConvert( (_block >> 24) & 0xf, 4, 8) * _factor;
  2716. *_g += bitRangeConvert( (_block >> 20) & 0xf, 4, 8) * _factor;
  2717. *_b += bitRangeConvert( (_block >> 16) & 0xf, 4, 8) * _factor;
  2718. }
  2719. }
  2720. static void decodeBlockPtc14(uint8_t _dst[16*4], const uint8_t* _src, uint32_t _x, uint32_t _y, uint32_t _width, uint32_t _height)
  2721. {
  2722. // 0 1 2 3 4 5 6 7
  2723. // 7654321076543210765432107654321076543210765432107654321076543210
  2724. // mmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmyrrrrrgggggbbbbbxrrrrrgggggbbbbp
  2725. // ^ ^^ ^^ ^
  2726. // +-- modulation data |+- B color |+- A color |
  2727. // +-- B opaque +-- A opaque |
  2728. // alpha punchthrough --+
  2729. const uint8_t* bc = &_src[morton2d(_x, _y) * 8];
  2730. uint32_t mod = 0
  2731. | bc[3]<<24
  2732. | bc[2]<<16
  2733. | bc[1]<<8
  2734. | bc[0]
  2735. ;
  2736. const bool punchthrough = !!(bc[7] & 1);
  2737. const uint8_t* weightTable = s_pvrtcWeights[4 * punchthrough];
  2738. const uint8_t* factorTable = s_pvrtcFactors[0];
  2739. for (int yy = 0; yy < 4; ++yy)
  2740. {
  2741. const uint32_t yOffset = (yy < 2) ? -1 : 0;
  2742. const uint32_t y0 = (_y + yOffset) % _height;
  2743. const uint32_t y1 = (y0 + 1) % _height;
  2744. for (int xx = 0; xx < 4; ++xx)
  2745. {
  2746. const uint32_t xOffset = (xx < 2) ? -1 : 0;
  2747. const uint32_t x0 = (_x + xOffset) % _width;
  2748. const uint32_t x1 = (x0 + 1) % _width;
  2749. const uint32_t bc0 = getColor(&_src[morton2d(x0, y0) * 8]);
  2750. const uint32_t bc1 = getColor(&_src[morton2d(x1, y0) * 8]);
  2751. const uint32_t bc2 = getColor(&_src[morton2d(x0, y1) * 8]);
  2752. const uint32_t bc3 = getColor(&_src[morton2d(x1, y1) * 8]);
  2753. const uint8_t f0 = factorTable[0];
  2754. const uint8_t f1 = factorTable[1];
  2755. const uint8_t f2 = factorTable[2];
  2756. const uint8_t f3 = factorTable[3];
  2757. uint32_t ar = 0, ag = 0, ab = 0;
  2758. decodeBlockPtc14RgbAddA(bc0, &ar, &ag, &ab, f0);
  2759. decodeBlockPtc14RgbAddA(bc1, &ar, &ag, &ab, f1);
  2760. decodeBlockPtc14RgbAddA(bc2, &ar, &ag, &ab, f2);
  2761. decodeBlockPtc14RgbAddA(bc3, &ar, &ag, &ab, f3);
  2762. uint32_t br = 0, bg = 0, bb = 0;
  2763. decodeBlockPtc14RgbAddB(bc0, &br, &bg, &bb, f0);
  2764. decodeBlockPtc14RgbAddB(bc1, &br, &bg, &bb, f1);
  2765. decodeBlockPtc14RgbAddB(bc2, &br, &bg, &bb, f2);
  2766. decodeBlockPtc14RgbAddB(bc3, &br, &bg, &bb, f3);
  2767. const uint8_t* weight = &weightTable[(mod & 3)*4];
  2768. const uint8_t wa = weight[0];
  2769. const uint8_t wb = weight[1];
  2770. _dst[(yy*4 + xx)*4+0] = uint8_t( (ab * wa + bb * wb) >> 7);
  2771. _dst[(yy*4 + xx)*4+1] = uint8_t( (ag * wa + bg * wb) >> 7);
  2772. _dst[(yy*4 + xx)*4+2] = uint8_t( (ar * wa + br * wb) >> 7);
  2773. _dst[(yy*4 + xx)*4+3] = 255;
  2774. mod >>= 2;
  2775. factorTable += 4;
  2776. }
  2777. }
  2778. }
  2779. static void decodeBlockPtc14ARgbaAddA(uint32_t _block, uint32_t* _r, uint32_t* _g, uint32_t* _b, uint32_t* _a, uint8_t _factor)
  2780. {
  2781. if (0 != (_block & (1<<15) ) )
  2782. {
  2783. *_r += bitRangeConvert( (_block >> 10) & 0x1f, 5, 8) * _factor;
  2784. *_g += bitRangeConvert( (_block >> 5) & 0x1f, 5, 8) * _factor;
  2785. *_b += bitRangeConvert( (_block >> 1) & 0x0f, 4, 8) * _factor;
  2786. *_a += 255 * _factor;
  2787. }
  2788. else
  2789. {
  2790. *_r += bitRangeConvert( (_block >> 8) & 0xf, 4, 8) * _factor;
  2791. *_g += bitRangeConvert( (_block >> 4) & 0xf, 4, 8) * _factor;
  2792. *_b += bitRangeConvert( (_block >> 1) & 0x7, 3, 8) * _factor;
  2793. *_a += bitRangeConvert( (_block >> 12) & 0x7, 3, 8) * _factor;
  2794. }
  2795. }
  2796. static void decodeBlockPtc14ARgbaAddB(uint32_t _block, uint32_t* _r, uint32_t* _g, uint32_t* _b, uint32_t* _a, uint8_t _factor)
  2797. {
  2798. if (0 != (_block & (1<<31) ) )
  2799. {
  2800. *_r += bitRangeConvert( (_block >> 26) & 0x1f, 5, 8) * _factor;
  2801. *_g += bitRangeConvert( (_block >> 21) & 0x1f, 5, 8) * _factor;
  2802. *_b += bitRangeConvert( (_block >> 16) & 0x1f, 5, 8) * _factor;
  2803. *_a += 255 * _factor;
  2804. }
  2805. else
  2806. {
  2807. *_r += bitRangeConvert( (_block >> 24) & 0xf, 4, 8) * _factor;
  2808. *_g += bitRangeConvert( (_block >> 20) & 0xf, 4, 8) * _factor;
  2809. *_b += bitRangeConvert( (_block >> 16) & 0xf, 4, 8) * _factor;
  2810. *_a += bitRangeConvert( (_block >> 28) & 0x7, 3, 8) * _factor;
  2811. }
  2812. }
  2813. static void decodeBlockPtc14A(uint8_t _dst[16*4], const uint8_t* _src, uint32_t _x, uint32_t _y, uint32_t _width, uint32_t _height)
  2814. {
  2815. // 0 1 2 3 4 5 6 7
  2816. // 7654321076543210765432107654321076543210765432107654321076543210
  2817. // mmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmyrrrrrgggggbbbbbxrrrrrgggggbbbbp
  2818. // ^ ^^ ^^ ^
  2819. // +-- modulation data |+- B color |+- A color |
  2820. // +-- B opaque +-- A opaque |
  2821. // alpha punchthrough --+
  2822. const uint8_t* bc = &_src[morton2d(_x, _y) * 8];
  2823. uint32_t mod = 0
  2824. | bc[3]<<24
  2825. | bc[2]<<16
  2826. | bc[1]<<8
  2827. | bc[0]
  2828. ;
  2829. const bool punchthrough = !!(bc[7] & 1);
  2830. const uint8_t* weightTable = s_pvrtcWeights[4 * punchthrough];
  2831. const uint8_t* factorTable = s_pvrtcFactors[0];
  2832. for (int yy = 0; yy < 4; ++yy)
  2833. {
  2834. const uint32_t yOffset = (yy < 2) ? -1 : 0;
  2835. const uint32_t y0 = (_y + yOffset) % _height;
  2836. const uint32_t y1 = (y0 + 1) % _height;
  2837. for (int xx = 0; xx < 4; ++xx)
  2838. {
  2839. const uint32_t xOffset = (xx < 2) ? -1 : 0;
  2840. const uint32_t x0 = (_x + xOffset) % _width;
  2841. const uint32_t x1 = (x0 + 1) % _width;
  2842. const uint32_t bc0 = getColor(&_src[morton2d(x0, y0) * 8]);
  2843. const uint32_t bc1 = getColor(&_src[morton2d(x1, y0) * 8]);
  2844. const uint32_t bc2 = getColor(&_src[morton2d(x0, y1) * 8]);
  2845. const uint32_t bc3 = getColor(&_src[morton2d(x1, y1) * 8]);
  2846. const uint8_t f0 = factorTable[0];
  2847. const uint8_t f1 = factorTable[1];
  2848. const uint8_t f2 = factorTable[2];
  2849. const uint8_t f3 = factorTable[3];
  2850. uint32_t ar = 0, ag = 0, ab = 0, aa = 0;
  2851. decodeBlockPtc14ARgbaAddA(bc0, &ar, &ag, &ab, &aa, f0);
  2852. decodeBlockPtc14ARgbaAddA(bc1, &ar, &ag, &ab, &aa, f1);
  2853. decodeBlockPtc14ARgbaAddA(bc2, &ar, &ag, &ab, &aa, f2);
  2854. decodeBlockPtc14ARgbaAddA(bc3, &ar, &ag, &ab, &aa, f3);
  2855. uint32_t br = 0, bg = 0, bb = 0, ba = 0;
  2856. decodeBlockPtc14ARgbaAddB(bc0, &br, &bg, &bb, &ba, f0);
  2857. decodeBlockPtc14ARgbaAddB(bc1, &br, &bg, &bb, &ba, f1);
  2858. decodeBlockPtc14ARgbaAddB(bc2, &br, &bg, &bb, &ba, f2);
  2859. decodeBlockPtc14ARgbaAddB(bc3, &br, &bg, &bb, &ba, f3);
  2860. const uint8_t* weight = &weightTable[(mod & 3)*4];
  2861. const uint8_t wa = weight[0];
  2862. const uint8_t wb = weight[1];
  2863. const uint8_t wc = weight[2];
  2864. const uint8_t wd = weight[3];
  2865. _dst[(yy*4 + xx)*4+0] = uint8_t( (ab * wa + bb * wb) >> 7);
  2866. _dst[(yy*4 + xx)*4+1] = uint8_t( (ag * wa + bg * wb) >> 7);
  2867. _dst[(yy*4 + xx)*4+2] = uint8_t( (ar * wa + br * wb) >> 7);
  2868. _dst[(yy*4 + xx)*4+3] = uint8_t( (aa * wc + ba * wd) >> 7);
  2869. mod >>= 2;
  2870. factorTable += 4;
  2871. }
  2872. }
  2873. }
  2874. 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)
  2875. {
  2876. const ImageBlockInfo& blockInfo = getBlockInfo(_format);
  2877. const uint16_t blockWidth = blockInfo.blockWidth;
  2878. const uint16_t blockHeight = blockInfo.blockHeight;
  2879. const uint16_t minBlockX = blockInfo.minBlockX;
  2880. const uint16_t minBlockY = blockInfo.minBlockY;
  2881. _width = bx::max<uint16_t>(blockWidth * minBlockX, ( (_width + blockWidth - 1) / blockWidth)*blockWidth);
  2882. _height = bx::max<uint16_t>(blockHeight * minBlockY, ( (_height + blockHeight - 1) / blockHeight)*blockHeight);
  2883. _depth = bx::max<uint16_t>(1, _depth);
  2884. _numLayers = bx::max<uint16_t>(1, _numLayers);
  2885. const uint8_t numMips = _hasMips ? imageGetNumMips(_format, _width, _height, _depth) : 1;
  2886. uint32_t size = imageGetSize(NULL, _width, _height, _depth, _cubeMap, _hasMips, _numLayers, _format);
  2887. ImageContainer* imageContainer = (ImageContainer*)bx::alignedAlloc(_allocator, size + bx::alignUp(sizeof(ImageContainer), 16), 16);
  2888. imageContainer->m_allocator = _allocator;
  2889. imageContainer->m_data = bx::alignPtr(imageContainer + 1, 0, 16);
  2890. imageContainer->m_format = _format;
  2891. imageContainer->m_orientation = Orientation::R0;
  2892. imageContainer->m_size = size;
  2893. imageContainer->m_offset = 0;
  2894. imageContainer->m_width = _width;
  2895. imageContainer->m_height = _height;
  2896. imageContainer->m_depth = _depth;
  2897. imageContainer->m_numLayers = _numLayers;
  2898. imageContainer->m_numMips = numMips;
  2899. imageContainer->m_hasAlpha = false;
  2900. imageContainer->m_cubeMap = _cubeMap;
  2901. imageContainer->m_ktx = false;
  2902. imageContainer->m_pvr3 = false;
  2903. imageContainer->m_ktxLE = false;
  2904. imageContainer->m_srgb = false;
  2905. if (NULL != _data)
  2906. {
  2907. bx::memCopy(imageContainer->m_data, _data, imageContainer->m_size);
  2908. }
  2909. return imageContainer;
  2910. }
  2911. void imageFree(ImageContainer* _imageContainer)
  2912. {
  2913. bx::alignedFree(_imageContainer->m_allocator, _imageContainer, 16);
  2914. }
  2915. // DDS
  2916. #define DDS_MAGIC BX_MAKEFOURCC('D', 'D', 'S', ' ')
  2917. #define DDS_HEADER_SIZE 124
  2918. #define DDS_DXT1 BX_MAKEFOURCC('D', 'X', 'T', '1')
  2919. #define DDS_DXT2 BX_MAKEFOURCC('D', 'X', 'T', '2')
  2920. #define DDS_DXT3 BX_MAKEFOURCC('D', 'X', 'T', '3')
  2921. #define DDS_DXT4 BX_MAKEFOURCC('D', 'X', 'T', '4')
  2922. #define DDS_DXT5 BX_MAKEFOURCC('D', 'X', 'T', '5')
  2923. #define DDS_ATI1 BX_MAKEFOURCC('A', 'T', 'I', '1')
  2924. #define DDS_BC4U BX_MAKEFOURCC('B', 'C', '4', 'U')
  2925. #define DDS_ATI2 BX_MAKEFOURCC('A', 'T', 'I', '2')
  2926. #define DDS_BC5U BX_MAKEFOURCC('B', 'C', '5', 'U')
  2927. #define DDS_RXGB BX_MAKEFOURCC('R', 'X', 'G', 'B')
  2928. #define DDS_DX10 BX_MAKEFOURCC('D', 'X', '1', '0')
  2929. #define DDS_ETC1 BX_MAKEFOURCC('E', 'T', 'C', '1')
  2930. #define DDS_ETC2 BX_MAKEFOURCC('E', 'T', 'C', '2')
  2931. #define DDS_ET2A BX_MAKEFOURCC('E', 'T', '2', 'A')
  2932. #define DDS_PTC2 BX_MAKEFOURCC('P', 'T', 'C', '2')
  2933. #define DDS_PTC4 BX_MAKEFOURCC('P', 'T', 'C', '4')
  2934. #define DDS_ATC BX_MAKEFOURCC('A', 'T', 'C', ' ')
  2935. #define DDS_ATCE BX_MAKEFOURCC('A', 'T', 'C', 'E')
  2936. #define DDS_ATCI BX_MAKEFOURCC('A', 'T', 'C', 'I')
  2937. #define DDS_ASTC4x4 BX_MAKEFOURCC('A', 'S', '4', '4')
  2938. #define DDS_ASTC5x4 BX_MAKEFOURCC('A', 'S', '5', '4')
  2939. #define DDS_ASTC5x5 BX_MAKEFOURCC('A', 'S', '5', '5')
  2940. #define DDS_ASTC6x5 BX_MAKEFOURCC('A', 'S', '6', '5')
  2941. #define DDS_ASTC6x6 BX_MAKEFOURCC('A', 'S', '6', '6')
  2942. #define DDS_ASTC8x5 BX_MAKEFOURCC('A', 'S', '8', '5')
  2943. #define DDS_ASTC8x6 BX_MAKEFOURCC('A', 'S', '8', '6')
  2944. #define DDS_ASTC8x8 BX_MAKEFOURCC('A', 'S', '8', '8')
  2945. #define DDS_ASTC10x5 BX_MAKEFOURCC('A', 'S', ':', '5')
  2946. #define DDS_ASTC10x6 BX_MAKEFOURCC('A', 'S', ':', '6')
  2947. #define DDS_ASTC10x8 BX_MAKEFOURCC('A', 'S', ':', '8')
  2948. #define DDS_ASTC10x10 BX_MAKEFOURCC('A', 'S', ':', ':')
  2949. #define DDS_ASTC12x10 BX_MAKEFOURCC('A', 'S', '<', ':')
  2950. #define DDS_ASTC12x12 BX_MAKEFOURCC('A', 'S', '<', '<')
  2951. #define DDS_R8G8B8 20
  2952. #define DDS_A8R8G8B8 21
  2953. #define DDS_R5G6B5 23
  2954. #define DDS_A1R5G5B5 25
  2955. #define DDS_A4R4G4B4 26
  2956. #define DDS_A2B10G10R10 31
  2957. #define DDS_G16R16 34
  2958. #define DDS_A2R10G10B10 35
  2959. #define DDS_A16B16G16R16 36
  2960. #define DDS_A8L8 51
  2961. #define DDS_R16F 111
  2962. #define DDS_G16R16F 112
  2963. #define DDS_A16B16G16R16F 113
  2964. #define DDS_R32F 114
  2965. #define DDS_G32R32F 115
  2966. #define DDS_A32B32G32R32F 116
  2967. #define DDS_FORMAT_R32G32B32A32_FLOAT 2
  2968. #define DDS_FORMAT_R32G32B32A32_UINT 3
  2969. #define DDS_FORMAT_R16G16B16A16_FLOAT 10
  2970. #define DDS_FORMAT_R16G16B16A16_UNORM 11
  2971. #define DDS_FORMAT_R16G16B16A16_UINT 12
  2972. #define DDS_FORMAT_R32G32_FLOAT 16
  2973. #define DDS_FORMAT_R32G32_UINT 17
  2974. #define DDS_FORMAT_R10G10B10A2_UNORM 24
  2975. #define DDS_FORMAT_R11G11B10_FLOAT 26
  2976. #define DDS_FORMAT_R8G8B8A8_UNORM 28
  2977. #define DDS_FORMAT_R8G8B8A8_UNORM_SRGB 29
  2978. #define DDS_FORMAT_R16G16_FLOAT 34
  2979. #define DDS_FORMAT_R16G16_UNORM 35
  2980. #define DDS_FORMAT_R32_FLOAT 41
  2981. #define DDS_FORMAT_R32_UINT 42
  2982. #define DDS_FORMAT_R8G8_UNORM 49
  2983. #define DDS_FORMAT_R16_FLOAT 54
  2984. #define DDS_FORMAT_R16_UNORM 56
  2985. #define DDS_FORMAT_R8_UNORM 61
  2986. #define DDS_FORMAT_A8_UNORM 65
  2987. #define DDS_FORMAT_R1_UNORM 66
  2988. #define DDS_FORMAT_BC1_UNORM 71
  2989. #define DDS_FORMAT_BC1_UNORM_SRGB 72
  2990. #define DDS_FORMAT_BC2_UNORM 74
  2991. #define DDS_FORMAT_BC2_UNORM_SRGB 75
  2992. #define DDS_FORMAT_BC3_UNORM 77
  2993. #define DDS_FORMAT_BC3_UNORM_SRGB 78
  2994. #define DDS_FORMAT_BC4_UNORM 80
  2995. #define DDS_FORMAT_BC5_UNORM 83
  2996. #define DDS_FORMAT_B5G6R5_UNORM 85
  2997. #define DDS_FORMAT_B5G5R5A1_UNORM 86
  2998. #define DDS_FORMAT_B8G8R8A8_UNORM 87
  2999. #define DDS_FORMAT_B8G8R8A8_UNORM_SRGB 91
  3000. #define DDS_FORMAT_BC6H_UF16 95
  3001. #define DDS_FORMAT_BC6H_SF16 96
  3002. #define DDS_FORMAT_BC7_UNORM 98
  3003. #define DDS_FORMAT_BC7_UNORM_SRGB 99
  3004. #define DDS_FORMAT_B4G4R4A4_UNORM 115
  3005. #define DDS_FORMAT_ASTC_4X4_UNORM 134
  3006. #define DDS_FORMAT_ASTC_4X4_UNORM_SRGB 135
  3007. #define DDS_FORMAT_ASTC_5X4_UNORM 138
  3008. #define DDS_FORMAT_ASTC_5X4_UNORM_SRGB 139
  3009. #define DDS_FORMAT_ASTC_5X5_UNORM 142
  3010. #define DDS_FORMAT_ASTC_5X5_UNORM_SRGB 143
  3011. #define DDS_FORMAT_ASTC_6X5_UNORM 146
  3012. #define DDS_FORMAT_ASTC_6X5_UNORM_SRGB 147
  3013. #define DDS_FORMAT_ASTC_6X6_UNORM 150
  3014. #define DDS_FORMAT_ASTC_6X6_UNORM_SRGB 151
  3015. #define DDS_FORMAT_ASTC_8X5_UNORM 154
  3016. #define DDS_FORMAT_ASTC_8X5_UNORM_SRGB 155
  3017. #define DDS_FORMAT_ASTC_8X6_UNORM 158
  3018. #define DDS_FORMAT_ASTC_8X6_UNORM_SRGB 159
  3019. #define DDS_FORMAT_ASTC_8X8_UNORM 162
  3020. #define DDS_FORMAT_ASTC_8X8_UNORM_SRGB 163
  3021. #define DDS_FORMAT_ASTC_10X5_UNORM 166
  3022. #define DDS_FORMAT_ASTC_10X5_UNORM_SRGB 167
  3023. #define DDS_FORMAT_ASTC_10X6_UNORM 170
  3024. #define DDS_FORMAT_ASTC_10X6_UNORM_SRGB 171
  3025. #define DDS_FORMAT_ASTC_10X8_UNORM 174
  3026. #define DDS_FORMAT_ASTC_10X8_UNORM_SRGB 175
  3027. #define DDS_FORMAT_ASTC_10X10_UNORM 178
  3028. #define DDS_FORMAT_ASTC_10X10_UNORM_SRGB 179
  3029. #define DDS_FORMAT_ASTC_12X10_UNORM 182
  3030. #define DDS_FORMAT_ASTC_12X10_UNORM_SRGB 183
  3031. #define DDS_FORMAT_ASTC_12X12_UNORM 186
  3032. #define DDS_FORMAT_ASTC_12X12_UNORM_SRGB 187
  3033. #define DDS_DX10_DIMENSION_TEXTURE2D 3
  3034. #define DDS_DX10_DIMENSION_TEXTURE3D 4
  3035. #define DDS_DX10_MISC_TEXTURECUBE 4
  3036. #define DDSD_CAPS 0x00000001
  3037. #define DDSD_HEIGHT 0x00000002
  3038. #define DDSD_WIDTH 0x00000004
  3039. #define DDSD_PITCH 0x00000008
  3040. #define DDSD_PIXELFORMAT 0x00001000
  3041. #define DDSD_MIPMAPCOUNT 0x00020000
  3042. #define DDSD_LINEARSIZE 0x00080000
  3043. #define DDSD_DEPTH 0x00800000
  3044. #define DDPF_ALPHAPIXELS 0x00000001
  3045. #define DDPF_ALPHA 0x00000002
  3046. #define DDPF_FOURCC 0x00000004
  3047. #define DDPF_INDEXED 0x00000020
  3048. #define DDPF_RGB 0x00000040
  3049. #define DDPF_YUV 0x00000200
  3050. #define DDPF_LUMINANCE 0x00020000
  3051. #define DDPF_BUMPDUDV 0x00080000
  3052. #define DDSCAPS_COMPLEX 0x00000008
  3053. #define DDSCAPS_TEXTURE 0x00001000
  3054. #define DDSCAPS_MIPMAP 0x00400000
  3055. #define DDSCAPS2_VOLUME 0x00200000
  3056. #define DDSCAPS2_CUBEMAP 0x00000200
  3057. #define DDSCAPS2_CUBEMAP_POSITIVEX 0x00000400
  3058. #define DDSCAPS2_CUBEMAP_NEGATIVEX 0x00000800
  3059. #define DDSCAPS2_CUBEMAP_POSITIVEY 0x00001000
  3060. #define DDSCAPS2_CUBEMAP_NEGATIVEY 0x00002000
  3061. #define DDSCAPS2_CUBEMAP_POSITIVEZ 0x00004000
  3062. #define DDSCAPS2_CUBEMAP_NEGATIVEZ 0x00008000
  3063. #define DSCAPS2_CUBEMAP_ALLSIDES (0 \
  3064. | DDSCAPS2_CUBEMAP_POSITIVEX \
  3065. | DDSCAPS2_CUBEMAP_NEGATIVEX \
  3066. | DDSCAPS2_CUBEMAP_POSITIVEY \
  3067. | DDSCAPS2_CUBEMAP_NEGATIVEY \
  3068. | DDSCAPS2_CUBEMAP_POSITIVEZ \
  3069. | DDSCAPS2_CUBEMAP_NEGATIVEZ \
  3070. )
  3071. struct TranslateDdsFormat
  3072. {
  3073. uint32_t m_format;
  3074. TextureFormat::Enum m_textureFormat;
  3075. bool m_srgb;
  3076. };
  3077. static const TranslateDdsFormat s_translateDdsFourccFormat[] =
  3078. {
  3079. { DDS_DXT1, TextureFormat::BC1, false },
  3080. { DDS_DXT2, TextureFormat::BC2, false },
  3081. { DDS_DXT3, TextureFormat::BC2, false },
  3082. { DDS_DXT4, TextureFormat::BC3, false },
  3083. { DDS_DXT5, TextureFormat::BC3, false },
  3084. { DDS_ATI1, TextureFormat::BC4, false },
  3085. { DDS_BC4U, TextureFormat::BC4, false },
  3086. { DDS_ATI2, TextureFormat::BC5, false },
  3087. { DDS_BC5U, TextureFormat::BC5, false },
  3088. { DDS_RXGB, TextureFormat::BC5, false },
  3089. { DDS_ETC1, TextureFormat::ETC1, false },
  3090. { DDS_ETC2, TextureFormat::ETC2, false },
  3091. { DDS_ET2A, TextureFormat::ETC2A, false },
  3092. { DDS_PTC2, TextureFormat::PTC12A, false },
  3093. { DDS_PTC4, TextureFormat::PTC14A, false },
  3094. { DDS_ATC , TextureFormat::ATC, false },
  3095. { DDS_ATCE, TextureFormat::ATCE, false },
  3096. { DDS_ATCI, TextureFormat::ATCI, false },
  3097. { DDS_ASTC4x4, TextureFormat::ASTC4x4, false },
  3098. { DDS_ASTC5x4, TextureFormat::ASTC5x4, false },
  3099. { DDS_ASTC5x5, TextureFormat::ASTC5x5, false },
  3100. { DDS_ASTC6x5, TextureFormat::ASTC6x5, false },
  3101. { DDS_ASTC6x6, TextureFormat::ASTC6x6, false },
  3102. { DDS_ASTC8x5, TextureFormat::ASTC8x5, false },
  3103. { DDS_ASTC8x6, TextureFormat::ASTC8x6, false },
  3104. { DDS_ASTC8x8, TextureFormat::ASTC8x8, false },
  3105. { DDS_ASTC10x5, TextureFormat::ASTC10x5, false },
  3106. { DDS_ASTC10x6, TextureFormat::ASTC10x6, false },
  3107. { DDS_ASTC10x8, TextureFormat::ASTC10x8, false },
  3108. { DDS_ASTC10x10, TextureFormat::ASTC10x10, false },
  3109. { DDS_ASTC12x10, TextureFormat::ASTC12x10, false },
  3110. { DDS_ASTC12x12, TextureFormat::ASTC12x12, false },
  3111. { DDS_A16B16G16R16, TextureFormat::RGBA16, false },
  3112. { DDS_A16B16G16R16F, TextureFormat::RGBA16F, false },
  3113. { DDPF_RGB|DDPF_ALPHAPIXELS, TextureFormat::BGRA8, false },
  3114. { DDPF_INDEXED, TextureFormat::R8, false },
  3115. { DDPF_LUMINANCE, TextureFormat::R8, false },
  3116. { DDPF_ALPHA, TextureFormat::R8, false },
  3117. { DDS_R16F, TextureFormat::R16F, false },
  3118. { DDS_R32F, TextureFormat::R32F, false },
  3119. { DDS_A8L8, TextureFormat::RG8, false },
  3120. { DDS_G16R16, TextureFormat::RG16, false },
  3121. { DDS_G16R16F, TextureFormat::RG16F, false },
  3122. { DDS_G32R32F, TextureFormat::RG32F, false },
  3123. { DDS_R8G8B8, TextureFormat::RGB8, false },
  3124. { DDS_A8R8G8B8, TextureFormat::BGRA8, false },
  3125. { DDS_A16B16G16R16, TextureFormat::RGBA16, false },
  3126. { DDS_A16B16G16R16F, TextureFormat::RGBA16F, false },
  3127. { DDS_A32B32G32R32F, TextureFormat::RGBA32F, false },
  3128. { DDS_R5G6B5, TextureFormat::B5G6R5, false },
  3129. { DDS_R5G6B5, TextureFormat::R5G6B5, false },
  3130. { DDS_A4R4G4B4, TextureFormat::BGRA4, false },
  3131. { DDS_A4R4G4B4, TextureFormat::RGBA4, false },
  3132. { DDS_A1R5G5B5, TextureFormat::BGR5A1, false },
  3133. { DDS_A1R5G5B5, TextureFormat::RGB5A1, false },
  3134. { DDS_A2B10G10R10, TextureFormat::RGB10A2, false },
  3135. };
  3136. static const TranslateDdsFormat s_translateDxgiFormat[] =
  3137. {
  3138. { DDS_FORMAT_BC1_UNORM, TextureFormat::BC1, false },
  3139. { DDS_FORMAT_BC1_UNORM_SRGB, TextureFormat::BC1, true },
  3140. { DDS_FORMAT_BC2_UNORM, TextureFormat::BC2, false },
  3141. { DDS_FORMAT_BC2_UNORM_SRGB, TextureFormat::BC2, true },
  3142. { DDS_FORMAT_BC3_UNORM, TextureFormat::BC3, false },
  3143. { DDS_FORMAT_BC3_UNORM_SRGB, TextureFormat::BC3, true },
  3144. { DDS_FORMAT_BC4_UNORM, TextureFormat::BC4, false },
  3145. { DDS_FORMAT_BC5_UNORM, TextureFormat::BC5, false },
  3146. { DDS_FORMAT_BC6H_UF16, TextureFormat::BC6H, false },
  3147. { DDS_FORMAT_BC6H_SF16, TextureFormat::BC6H, false },
  3148. { DDS_FORMAT_BC7_UNORM, TextureFormat::BC7, false },
  3149. { DDS_FORMAT_BC7_UNORM_SRGB, TextureFormat::BC7, true },
  3150. { DDS_FORMAT_ASTC_4X4_UNORM, TextureFormat::ASTC4x4, false },
  3151. { DDS_FORMAT_ASTC_4X4_UNORM_SRGB, TextureFormat::ASTC4x4, true },
  3152. { DDS_FORMAT_ASTC_5X4_UNORM, TextureFormat::ASTC5x4, false },
  3153. { DDS_FORMAT_ASTC_5X4_UNORM_SRGB, TextureFormat::ASTC5x4, true },
  3154. { DDS_FORMAT_ASTC_5X5_UNORM, TextureFormat::ASTC5x5, false },
  3155. { DDS_FORMAT_ASTC_5X5_UNORM_SRGB, TextureFormat::ASTC5x5, true },
  3156. { DDS_FORMAT_ASTC_6X5_UNORM, TextureFormat::ASTC6x5, false },
  3157. { DDS_FORMAT_ASTC_6X5_UNORM_SRGB, TextureFormat::ASTC6x5, true },
  3158. { DDS_FORMAT_ASTC_6X6_UNORM, TextureFormat::ASTC6x6, false },
  3159. { DDS_FORMAT_ASTC_6X6_UNORM_SRGB, TextureFormat::ASTC6x6, true },
  3160. { DDS_FORMAT_ASTC_8X5_UNORM, TextureFormat::ASTC8x5, false },
  3161. { DDS_FORMAT_ASTC_8X5_UNORM_SRGB, TextureFormat::ASTC8x5, true },
  3162. { DDS_FORMAT_ASTC_8X6_UNORM, TextureFormat::ASTC8x6, false },
  3163. { DDS_FORMAT_ASTC_8X6_UNORM_SRGB, TextureFormat::ASTC8x6, true },
  3164. { DDS_FORMAT_ASTC_8X8_UNORM, TextureFormat::ASTC8x8, false },
  3165. { DDS_FORMAT_ASTC_8X8_UNORM_SRGB, TextureFormat::ASTC8x8, true },
  3166. { DDS_FORMAT_ASTC_10X5_UNORM, TextureFormat::ASTC10x5, false },
  3167. { DDS_FORMAT_ASTC_10X5_UNORM_SRGB, TextureFormat::ASTC10x5, true },
  3168. { DDS_FORMAT_ASTC_10X6_UNORM, TextureFormat::ASTC10x6, false },
  3169. { DDS_FORMAT_ASTC_10X6_UNORM_SRGB, TextureFormat::ASTC10x6, true },
  3170. { DDS_FORMAT_ASTC_10X8_UNORM, TextureFormat::ASTC10x8, false },
  3171. { DDS_FORMAT_ASTC_10X8_UNORM_SRGB, TextureFormat::ASTC10x8, true },
  3172. { DDS_FORMAT_ASTC_10X10_UNORM, TextureFormat::ASTC10x10, false },
  3173. { DDS_FORMAT_ASTC_10X10_UNORM_SRGB, TextureFormat::ASTC10x10, true },
  3174. { DDS_FORMAT_ASTC_12X10_UNORM, TextureFormat::ASTC12x10, false },
  3175. { DDS_FORMAT_ASTC_12X10_UNORM_SRGB, TextureFormat::ASTC12x10, true },
  3176. { DDS_FORMAT_ASTC_12X12_UNORM, TextureFormat::ASTC12x12, false },
  3177. { DDS_FORMAT_R1_UNORM, TextureFormat::R1, false },
  3178. { DDS_FORMAT_R8_UNORM, TextureFormat::R8, false },
  3179. { DDS_FORMAT_A8_UNORM, TextureFormat::R8, false },
  3180. { DDS_FORMAT_R16_UNORM, TextureFormat::R16, false },
  3181. { DDS_FORMAT_R16_FLOAT, TextureFormat::R16F, false },
  3182. { DDS_FORMAT_R32_UINT, TextureFormat::R32U, false },
  3183. { DDS_FORMAT_R32_FLOAT, TextureFormat::R32F, false },
  3184. { DDS_FORMAT_R8G8_UNORM, TextureFormat::RG8, false },
  3185. { DDS_FORMAT_R16G16_UNORM, TextureFormat::RG16, false },
  3186. { DDS_FORMAT_R16G16_FLOAT, TextureFormat::RG16F, false },
  3187. { DDS_FORMAT_R32G32_UINT, TextureFormat::RG32U, false },
  3188. { DDS_FORMAT_R32G32_FLOAT, TextureFormat::RG32F, false },
  3189. { DDS_FORMAT_B8G8R8A8_UNORM, TextureFormat::BGRA8, false },
  3190. { DDS_FORMAT_B8G8R8A8_UNORM_SRGB, TextureFormat::BGRA8, true },
  3191. { DDS_FORMAT_R8G8B8A8_UNORM, TextureFormat::RGBA8, false },
  3192. { DDS_FORMAT_R8G8B8A8_UNORM_SRGB, TextureFormat::RGBA8, true },
  3193. { DDS_FORMAT_R16G16B16A16_UNORM, TextureFormat::RGBA16, false },
  3194. { DDS_FORMAT_R16G16B16A16_FLOAT, TextureFormat::RGBA16F, false },
  3195. { DDS_FORMAT_R32G32B32A32_UINT, TextureFormat::RGBA32U, false },
  3196. { DDS_FORMAT_R32G32B32A32_FLOAT, TextureFormat::RGBA32F, false },
  3197. { DDS_FORMAT_B5G6R5_UNORM, TextureFormat::B5G6R5, false },
  3198. { DDS_FORMAT_B5G6R5_UNORM, TextureFormat::R5G6B5, false },
  3199. { DDS_FORMAT_B4G4R4A4_UNORM, TextureFormat::BGRA4, false },
  3200. { DDS_FORMAT_B4G4R4A4_UNORM, TextureFormat::RGBA4, false },
  3201. { DDS_FORMAT_B5G5R5A1_UNORM, TextureFormat::BGR5A1, false },
  3202. { DDS_FORMAT_B5G5R5A1_UNORM, TextureFormat::RGB5A1, false },
  3203. { DDS_FORMAT_R10G10B10A2_UNORM, TextureFormat::RGB10A2, false },
  3204. { DDS_FORMAT_R11G11B10_FLOAT, TextureFormat::RG11B10F, false },
  3205. };
  3206. struct TranslateDdsPixelFormat
  3207. {
  3208. uint32_t m_bitCount;
  3209. uint32_t m_flags;
  3210. uint32_t m_bitmask[4];
  3211. TextureFormat::Enum m_textureFormat;
  3212. };
  3213. static const TranslateDdsPixelFormat s_translateDdsPixelFormat[] =
  3214. {
  3215. { 8, DDPF_LUMINANCE, { 0x000000ff, 0x00000000, 0x00000000, 0x00000000 }, TextureFormat::R8 },
  3216. { 8, DDPF_ALPHA, { 0x00000000, 0x00000000, 0x00000000, 0x000000ff }, TextureFormat::R8 },
  3217. { 16, DDPF_BUMPDUDV, { 0x000000ff, 0x0000ff00, 0x00000000, 0x00000000 }, TextureFormat::RG8S },
  3218. { 16, DDPF_RGB, { 0x0000ffff, 0x00000000, 0x00000000, 0x00000000 }, TextureFormat::R16U },
  3219. { 16, DDPF_RGB|DDPF_ALPHAPIXELS, { 0x0000000f, 0x000000f0, 0x00000f00, 0x0000f000 }, TextureFormat::RGBA4 },
  3220. { 16, DDPF_RGB|DDPF_ALPHAPIXELS, { 0x00000f00, 0x000000f0, 0x0000000f, 0x0000f000 }, TextureFormat::BGRA4 },
  3221. { 16, DDPF_RGB, { 0x0000001f, 0x000007e0, 0x0000f800, 0x00000000 }, TextureFormat::R5G6B5 },
  3222. { 16, DDPF_RGB, { 0x0000f800, 0x000007e0, 0x0000001f, 0x00000000 }, TextureFormat::B5G6R5 },
  3223. { 16, DDPF_RGB|DDPF_ALPHAPIXELS, { 0x0000001f, 0x000003e0, 0x00007c00, 0x00008000 }, TextureFormat::BGR5A1 },
  3224. { 16, DDPF_RGB|DDPF_ALPHAPIXELS, { 0x00007c00, 0x000003e0, 0x0000001f, 0x00008000 }, TextureFormat::RGB5A1 },
  3225. { 24, DDPF_RGB, { 0x00ff0000, 0x0000ff00, 0x000000ff, 0x00000000 }, TextureFormat::RGB8 },
  3226. { 24, DDPF_RGB, { 0x000000ff, 0x0000ff00, 0x00ff0000, 0x00000000 }, TextureFormat::RGB8 },
  3227. { 32, DDPF_RGB, { 0x00ff0000, 0x0000ff00, 0x000000ff, 0x00000000 }, TextureFormat::BGRA8 },
  3228. { 32, DDPF_RGB|DDPF_ALPHAPIXELS, { 0x000000ff, 0x0000ff00, 0x00ff0000, 0xff000000 }, TextureFormat::RGBA8 },
  3229. { 32, DDPF_BUMPDUDV, { 0x000000ff, 0x0000ff00, 0x00ff0000, 0xff000000 }, TextureFormat::RGBA8S },
  3230. { 32, DDPF_RGB, { 0x00ff0000, 0x0000ff00, 0x000000ff, 0xff000000 }, TextureFormat::BGRA8 },
  3231. { 32, DDPF_RGB|DDPF_ALPHAPIXELS, { 0x00ff0000, 0x0000ff00, 0x000000ff, 0xff000000 }, TextureFormat::BGRA8 }, // D3DFMT_A8R8G8B8
  3232. { 32, DDPF_RGB|DDPF_ALPHAPIXELS, { 0x00ff0000, 0x0000ff00, 0x000000ff, 0x00000000 }, TextureFormat::BGRA8 }, // D3DFMT_X8R8G8B8
  3233. { 32, DDPF_RGB|DDPF_ALPHAPIXELS, { 0x000003ff, 0x000ffc00, 0x3ff00000, 0xc0000000 }, TextureFormat::RGB10A2 },
  3234. { 32, DDPF_RGB, { 0x0000ffff, 0xffff0000, 0x00000000, 0x00000000 }, TextureFormat::RG16 },
  3235. { 32, DDPF_BUMPDUDV, { 0x0000ffff, 0xffff0000, 0x00000000, 0x00000000 }, TextureFormat::RG16S },
  3236. { 32, DDPF_RGB, { 0xffffffff, 0x00000000, 0x00000000, 0x00000000 }, TextureFormat::R32U },
  3237. };
  3238. bool imageParseDds(ImageContainer& _imageContainer, bx::ReaderSeekerI* _reader, bx::Error* _err)
  3239. {
  3240. BX_ERROR_SCOPE(_err);
  3241. int32_t total = 0;
  3242. uint32_t headerSize;
  3243. total += bx::read(_reader, headerSize, _err);
  3244. if (!_err->isOk()
  3245. || headerSize < DDS_HEADER_SIZE)
  3246. {
  3247. BX_ERROR_SET(_err, BIMG_ERROR, "DDS: Invalid header size.");
  3248. return false;
  3249. }
  3250. uint32_t flags;
  3251. total += bx::read(_reader, flags, _err);
  3252. if (!_err->isOk() )
  3253. {
  3254. return false;
  3255. }
  3256. if ( (flags & (DDSD_HEIGHT|DDSD_WIDTH) ) != (DDSD_HEIGHT|DDSD_WIDTH) )
  3257. {
  3258. BX_ERROR_SET(_err, BIMG_ERROR, "DDS: Invalid flags.");
  3259. return false;
  3260. }
  3261. uint32_t height;
  3262. total += bx::read(_reader, height, _err);
  3263. uint32_t width;
  3264. total += bx::read(_reader, width, _err);
  3265. uint32_t pitch;
  3266. total += bx::read(_reader, pitch, _err);
  3267. uint32_t depth;
  3268. total += bx::read(_reader, depth, _err);
  3269. uint32_t mips;
  3270. total += bx::read(_reader, mips, _err);
  3271. bx::skip(_reader, 44); // reserved
  3272. total += 44;
  3273. uint32_t pixelFormatSize;
  3274. total += bx::read(_reader, pixelFormatSize, _err);
  3275. uint32_t pixelFlags;
  3276. total += bx::read(_reader, pixelFlags, _err);
  3277. uint32_t fourcc;
  3278. total += bx::read(_reader, fourcc, _err);
  3279. uint32_t bitCount;
  3280. total += bx::read(_reader, bitCount, _err);
  3281. uint32_t bitmask[4];
  3282. total += bx::read(_reader, bitmask, sizeof(bitmask), _err);
  3283. uint32_t caps[4];
  3284. total += bx::read(_reader, caps, _err);
  3285. bx::skip(_reader, 4);
  3286. total += 4; // reserved
  3287. if (!_err->isOk() )
  3288. {
  3289. return false;
  3290. }
  3291. uint32_t dxgiFormat = 0;
  3292. uint32_t arraySize = 1;
  3293. if (DDPF_FOURCC == (pixelFlags & DDPF_FOURCC)
  3294. && DDS_DX10 == fourcc)
  3295. {
  3296. total += bx::read(_reader, dxgiFormat, _err);
  3297. uint32_t dims;
  3298. total += bx::read(_reader, dims, _err);
  3299. uint32_t miscFlags;
  3300. total += bx::read(_reader, miscFlags, _err);
  3301. total += bx::read(_reader, arraySize, _err);
  3302. uint32_t miscFlags2;
  3303. total += bx::read(_reader, miscFlags2, _err);
  3304. }
  3305. BX_UNUSED(total);
  3306. if (!_err->isOk() )
  3307. {
  3308. return false;
  3309. }
  3310. if ( (caps[0] & DDSCAPS_TEXTURE) == 0)
  3311. {
  3312. BX_ERROR_SET(_err, BIMG_ERROR, "DDS: Unsupported caps.");
  3313. return false;
  3314. }
  3315. bool cubeMap = 0 != (caps[1] & DDSCAPS2_CUBEMAP);
  3316. if (cubeMap)
  3317. {
  3318. if ( (caps[1] & DSCAPS2_CUBEMAP_ALLSIDES) != DSCAPS2_CUBEMAP_ALLSIDES)
  3319. {
  3320. // partial cube map is not supported.
  3321. BX_ERROR_SET(_err, BIMG_ERROR, "DDS: Incomplete cubemap.");
  3322. return false;
  3323. }
  3324. }
  3325. TextureFormat::Enum format = TextureFormat::Unknown;
  3326. bool hasAlpha = pixelFlags & DDPF_ALPHAPIXELS;
  3327. bool srgb = false;
  3328. if (dxgiFormat == 0)
  3329. {
  3330. if (DDPF_FOURCC == (pixelFlags & DDPF_FOURCC) )
  3331. {
  3332. for (uint32_t ii = 0; ii < BX_COUNTOF(s_translateDdsFourccFormat); ++ii)
  3333. {
  3334. if (s_translateDdsFourccFormat[ii].m_format == fourcc)
  3335. {
  3336. format = s_translateDdsFourccFormat[ii].m_textureFormat;
  3337. break;
  3338. }
  3339. }
  3340. }
  3341. else
  3342. {
  3343. for (uint32_t ii = 0; ii < BX_COUNTOF(s_translateDdsPixelFormat); ++ii)
  3344. {
  3345. const TranslateDdsPixelFormat& pf = s_translateDdsPixelFormat[ii];
  3346. if (pf.m_bitCount == bitCount
  3347. && pf.m_flags == pixelFlags
  3348. && pf.m_bitmask[0] == bitmask[0]
  3349. && pf.m_bitmask[1] == bitmask[1]
  3350. && pf.m_bitmask[2] == bitmask[2]
  3351. && pf.m_bitmask[3] == bitmask[3])
  3352. {
  3353. format = pf.m_textureFormat;
  3354. break;
  3355. }
  3356. }
  3357. }
  3358. }
  3359. else
  3360. {
  3361. for (uint32_t ii = 0; ii < BX_COUNTOF(s_translateDxgiFormat); ++ii)
  3362. {
  3363. if (s_translateDxgiFormat[ii].m_format == dxgiFormat)
  3364. {
  3365. format = s_translateDxgiFormat[ii].m_textureFormat;
  3366. srgb = s_translateDxgiFormat[ii].m_srgb;
  3367. break;
  3368. }
  3369. }
  3370. }
  3371. if (TextureFormat::Unknown == format)
  3372. {
  3373. BX_ERROR_SET(_err, BIMG_ERROR, "DDS: Unknown texture format.");
  3374. return false;
  3375. }
  3376. _imageContainer.m_allocator = NULL;
  3377. _imageContainer.m_data = NULL;
  3378. _imageContainer.m_size = 0;
  3379. _imageContainer.m_offset = (uint32_t)bx::seek(_reader);
  3380. _imageContainer.m_width = width;
  3381. _imageContainer.m_height = height;
  3382. _imageContainer.m_depth = depth;
  3383. _imageContainer.m_format = format;
  3384. _imageContainer.m_orientation = Orientation::R0;
  3385. _imageContainer.m_numLayers = uint16_t(arraySize);
  3386. _imageContainer.m_numMips = uint8_t( (caps[0] & DDSCAPS_MIPMAP) ? mips : 1);
  3387. _imageContainer.m_hasAlpha = hasAlpha;
  3388. _imageContainer.m_cubeMap = cubeMap;
  3389. _imageContainer.m_ktx = false;
  3390. _imageContainer.m_ktxLE = false;
  3391. _imageContainer.m_pvr3 = false;
  3392. _imageContainer.m_srgb = srgb;
  3393. return true;
  3394. }
  3395. ImageContainer* imageParseDds(bx::AllocatorI* _allocator, const void* _src, uint32_t _size, bx::Error* _err)
  3396. {
  3397. return imageParseT<DDS_MAGIC, imageParseDds>(_allocator, _src, _size, _err);
  3398. }
  3399. // KTX
  3400. #define KTX_MAGIC BX_MAKEFOURCC(0xAB, 'K', 'T', 'X')
  3401. #define KTX_HEADER_SIZE 64
  3402. #define KTX_ETC1_RGB8_OES 0x8D64
  3403. #define KTX_COMPRESSED_R11_EAC 0x9270
  3404. #define KTX_COMPRESSED_SIGNED_R11_EAC 0x9271
  3405. #define KTX_COMPRESSED_RG11_EAC 0x9272
  3406. #define KTX_COMPRESSED_SIGNED_RG11_EAC 0x9273
  3407. #define KTX_COMPRESSED_RGB8_ETC2 0x9274
  3408. #define KTX_COMPRESSED_SRGB8_ETC2 0x9275
  3409. #define KTX_COMPRESSED_RGB8_PUNCHTHROUGH_ALPHA1_ETC2 0x9276
  3410. #define KTX_COMPRESSED_SRGB8_PUNCHTHROUGH_ALPHA1_ETC2 0x9277
  3411. #define KTX_COMPRESSED_RGBA8_ETC2_EAC 0x9278
  3412. #define KTX_COMPRESSED_SRGB8_ALPHA8_ETC2_EAC 0x9279
  3413. #define KTX_COMPRESSED_RGB_PVRTC_4BPPV1_IMG 0x8C00
  3414. #define KTX_COMPRESSED_RGB_PVRTC_2BPPV1_IMG 0x8C01
  3415. #define KTX_COMPRESSED_RGBA_PVRTC_4BPPV1_IMG 0x8C02
  3416. #define KTX_COMPRESSED_RGBA_PVRTC_2BPPV1_IMG 0x8C03
  3417. #define KTX_COMPRESSED_RGBA_PVRTC_2BPPV2_IMG 0x9137
  3418. #define KTX_COMPRESSED_RGBA_PVRTC_4BPPV2_IMG 0x9138
  3419. #define KTX_COMPRESSED_RGB_S3TC_DXT1_EXT 0x83F0
  3420. #define KTX_COMPRESSED_RGBA_S3TC_DXT1_EXT 0x83F1
  3421. #define KTX_COMPRESSED_RGBA_S3TC_DXT3_EXT 0x83F2
  3422. #define KTX_COMPRESSED_RGBA_S3TC_DXT5_EXT 0x83F3
  3423. #define KTX_COMPRESSED_SRGB_ALPHA_S3TC_DXT1_EXT 0x8C4D
  3424. #define KTX_COMPRESSED_SRGB_ALPHA_S3TC_DXT3_EXT 0x8C4E
  3425. #define KTX_COMPRESSED_SRGB_ALPHA_S3TC_DXT5_EXT 0x8C4F
  3426. #define KTX_COMPRESSED_LUMINANCE_LATC1_EXT 0x8C70
  3427. #define KTX_COMPRESSED_LUMINANCE_ALPHA_LATC2_EXT 0x8C72
  3428. #define KTX_COMPRESSED_RGBA_BPTC_UNORM_ARB 0x8E8C
  3429. #define KTX_COMPRESSED_SRGB_ALPHA_BPTC_UNORM_ARB 0x8E8D
  3430. #define KTX_COMPRESSED_RGB_BPTC_SIGNED_FLOAT_ARB 0x8E8E
  3431. #define KTX_COMPRESSED_RGB_BPTC_UNSIGNED_FLOAT_ARB 0x8E8F
  3432. #define KTX_COMPRESSED_SRGB_PVRTC_2BPPV1_EXT 0x8A54
  3433. #define KTX_COMPRESSED_SRGB_PVRTC_4BPPV1_EXT 0x8A55
  3434. #define KTX_COMPRESSED_SRGB_ALPHA_PVRTC_2BPPV1_EXT 0x8A56
  3435. #define KTX_COMPRESSED_SRGB_ALPHA_PVRTC_4BPPV1_EXT 0x8A57
  3436. #define KTX_ATC_RGB_AMD 0x8C92
  3437. #define KTX_ATC_RGBA_EXPLICIT_ALPHA_AMD 0x8C93
  3438. #define KTX_ATC_RGBA_INTERPOLATED_ALPHA_AMD 0x87EE
  3439. #define KTX_COMPRESSED_RGBA_ASTC_4x4_KHR 0x93B0
  3440. #define KTX_COMPRESSED_RGBA_ASTC_5x4_KHR 0x93B1
  3441. #define KTX_COMPRESSED_RGBA_ASTC_5x5_KHR 0x93B2
  3442. #define KTX_COMPRESSED_RGBA_ASTC_6x5_KHR 0x93B3
  3443. #define KTX_COMPRESSED_RGBA_ASTC_6x6_KHR 0x93B4
  3444. #define KTX_COMPRESSED_RGBA_ASTC_8x5_KHR 0x93B5
  3445. #define KTX_COMPRESSED_RGBA_ASTC_8x6_KHR 0x93B6
  3446. #define KTX_COMPRESSED_RGBA_ASTC_8x8_KHR 0x93B7
  3447. #define KTX_COMPRESSED_RGBA_ASTC_10x5_KHR 0x93B8
  3448. #define KTX_COMPRESSED_RGBA_ASTC_10x6_KHR 0x93B9
  3449. #define KTX_COMPRESSED_RGBA_ASTC_10x8_KHR 0x93BA
  3450. #define KTX_COMPRESSED_RGBA_ASTC_10x10_KHR 0x93BB
  3451. #define KTX_COMPRESSED_RGBA_ASTC_12x10_KHR 0x93BC
  3452. #define KTX_COMPRESSED_RGBA_ASTC_12x12_KHR 0x93BD
  3453. #define KTX_COMPRESSED_SRGB8_ALPHA8_ASTC_4x4_KHR 0x93D0
  3454. #define KTX_COMPRESSED_SRGB8_ALPHA8_ASTC_5x4_KHR 0x93D1
  3455. #define KTX_COMPRESSED_SRGB8_ALPHA8_ASTC_5x5_KHR 0x93D2
  3456. #define KTX_COMPRESSED_SRGB8_ALPHA8_ASTC_6x5_KHR 0x93D3
  3457. #define KTX_COMPRESSED_SRGB8_ALPHA8_ASTC_6x6_KHR 0x93D4
  3458. #define KTX_COMPRESSED_SRGB8_ALPHA8_ASTC_8x5_KHR 0x93D5
  3459. #define KTX_COMPRESSED_SRGB8_ALPHA8_ASTC_8x6_KHR 0x93D6
  3460. #define KTX_COMPRESSED_SRGB8_ALPHA8_ASTC_8x8_KHR 0x93D7
  3461. #define KTX_COMPRESSED_SRGB8_ALPHA8_ASTC_10x5_KHR 0x93D8
  3462. #define KTX_COMPRESSED_SRGB8_ALPHA8_ASTC_10x6_KHR 0x93D9
  3463. #define KTX_COMPRESSED_SRGB8_ALPHA8_ASTC_10x8_KHR 0x93DA
  3464. #define KTX_COMPRESSED_SRGB8_ALPHA8_ASTC_10x10_KHR 0x93DB
  3465. #define KTX_COMPRESSED_SRGB8_ALPHA8_ASTC_12x10_KHR 0x93DC
  3466. #define KTX_COMPRESSED_SRGB8_ALPHA8_ASTC_12x12_KHR 0x93DD
  3467. #define KTX_A8 0x803C
  3468. #define KTX_R8 0x8229
  3469. #define KTX_R16 0x822A
  3470. #define KTX_RG8 0x822B
  3471. #define KTX_RG16 0x822C
  3472. #define KTX_R16F 0x822D
  3473. #define KTX_R32F 0x822E
  3474. #define KTX_RG16F 0x822F
  3475. #define KTX_RG32F 0x8230
  3476. #define KTX_RGBA8 0x8058
  3477. #define KTX_RGBA16 0x805B
  3478. #define KTX_RGBA16F 0x881A
  3479. #define KTX_R32UI 0x8236
  3480. #define KTX_RG32UI 0x823C
  3481. #define KTX_RGBA32UI 0x8D70
  3482. #define KTX_RGBA32F 0x8814
  3483. #define KTX_RGB565 0x8D62
  3484. #define KTX_RGBA4 0x8056
  3485. #define KTX_RGB5_A1 0x8057
  3486. #define KTX_RGB10_A2 0x8059
  3487. #define KTX_R8I 0x8231
  3488. #define KTX_R8UI 0x8232
  3489. #define KTX_R16I 0x8233
  3490. #define KTX_R16UI 0x8234
  3491. #define KTX_R32I 0x8235
  3492. #define KTX_R32UI 0x8236
  3493. #define KTX_RG8I 0x8237
  3494. #define KTX_RG8UI 0x8238
  3495. #define KTX_RG16I 0x8239
  3496. #define KTX_RG16UI 0x823A
  3497. #define KTX_RG32I 0x823B
  3498. #define KTX_RG32UI 0x823C
  3499. #define KTX_R8_SNORM 0x8F94
  3500. #define KTX_RG8_SNORM 0x8F95
  3501. #define KTX_RGB8_SNORM 0x8F96
  3502. #define KTX_RGBA8_SNORM 0x8F97
  3503. #define KTX_R16_SNORM 0x8F98
  3504. #define KTX_RG16_SNORM 0x8F99
  3505. #define KTX_RGB16_SNORM 0x8F9A
  3506. #define KTX_RGBA16_SNORM 0x8F9B
  3507. #define KTX_SRGB8 0x8C41
  3508. #define KTX_SRGB8_ALPHA8 0x8C43
  3509. #define KTX_RGBA32UI 0x8D70
  3510. #define KTX_RGB32UI 0x8D71
  3511. #define KTX_RGBA16UI 0x8D76
  3512. #define KTX_RGB16UI 0x8D77
  3513. #define KTX_RGBA8UI 0x8D7C
  3514. #define KTX_RGB8UI 0x8D7D
  3515. #define KTX_RGBA32I 0x8D82
  3516. #define KTX_RGB32I 0x8D83
  3517. #define KTX_RGBA16I 0x8D88
  3518. #define KTX_RGB16I 0x8D89
  3519. #define KTX_RGBA8I 0x8D8E
  3520. #define KTX_RGB8 0x8051
  3521. #define KTX_RGB8I 0x8D8F
  3522. #define KTX_RGB9_E5 0x8C3D
  3523. #define KTX_R11F_G11F_B10F 0x8C3A
  3524. #define KTX_ZERO 0
  3525. #define KTX_RED 0x1903
  3526. #define KTX_ALPHA 0x1906
  3527. #define KTX_RGB 0x1907
  3528. #define KTX_RGBA 0x1908
  3529. #define KTX_BGRA 0x80E1
  3530. #define KTX_RG 0x8227
  3531. #define KTX_BYTE 0x1400
  3532. #define KTX_UNSIGNED_BYTE 0x1401
  3533. #define KTX_SHORT 0x1402
  3534. #define KTX_UNSIGNED_SHORT 0x1403
  3535. #define KTX_INT 0x1404
  3536. #define KTX_UNSIGNED_INT 0x1405
  3537. #define KTX_FLOAT 0x1406
  3538. #define KTX_HALF_FLOAT 0x140B
  3539. #define KTX_UNSIGNED_INT_5_9_9_9_REV 0x8C3E
  3540. #define KTX_UNSIGNED_SHORT_5_6_5 0x8363
  3541. #define KTX_UNSIGNED_SHORT_4_4_4_4 0x8033
  3542. #define KTX_UNSIGNED_SHORT_5_5_5_1 0x8034
  3543. #define KTX_UNSIGNED_INT_2_10_10_10_REV 0x8368
  3544. #define KTX_UNSIGNED_INT_10F_11F_11F_REV 0x8C3B
  3545. struct KtxFormatInfo
  3546. {
  3547. uint32_t m_internalFmt;
  3548. uint32_t m_internalFmtSrgb;
  3549. uint32_t m_fmt;
  3550. uint32_t m_type;
  3551. };
  3552. static const KtxFormatInfo s_translateKtxFormat[] =
  3553. {
  3554. { KTX_COMPRESSED_RGBA_S3TC_DXT1_EXT, KTX_COMPRESSED_SRGB_ALPHA_S3TC_DXT1_EXT, KTX_RGBA, KTX_ZERO, }, // BC1
  3555. { KTX_COMPRESSED_RGBA_S3TC_DXT3_EXT, KTX_COMPRESSED_SRGB_ALPHA_S3TC_DXT3_EXT, KTX_RGBA, KTX_ZERO, }, // BC2
  3556. { KTX_COMPRESSED_RGBA_S3TC_DXT5_EXT, KTX_COMPRESSED_SRGB_ALPHA_S3TC_DXT5_EXT, KTX_RGBA, KTX_ZERO, }, // BC3
  3557. { KTX_COMPRESSED_LUMINANCE_LATC1_EXT, KTX_ZERO, KTX_RED, KTX_ZERO, }, // BC4
  3558. { KTX_COMPRESSED_LUMINANCE_ALPHA_LATC2_EXT, KTX_ZERO, KTX_RG, KTX_ZERO, }, // BC5
  3559. { KTX_COMPRESSED_RGB_BPTC_SIGNED_FLOAT_ARB, KTX_ZERO, KTX_RGB, KTX_ZERO, }, // BC6H
  3560. { KTX_COMPRESSED_RGBA_BPTC_UNORM_ARB, KTX_ZERO, KTX_RGBA, KTX_ZERO, }, // BC7
  3561. { KTX_ETC1_RGB8_OES, KTX_ZERO, KTX_RGB, KTX_ZERO, }, // ETC1
  3562. { KTX_COMPRESSED_RGB8_ETC2, KTX_ZERO, KTX_RGB, KTX_ZERO, }, // ETC2
  3563. { KTX_COMPRESSED_RGBA8_ETC2_EAC, KTX_COMPRESSED_SRGB8_ETC2, KTX_RGBA, KTX_ZERO, }, // ETC2A
  3564. { KTX_COMPRESSED_RGB8_PUNCHTHROUGH_ALPHA1_ETC2, KTX_COMPRESSED_SRGB8_PUNCHTHROUGH_ALPHA1_ETC2, KTX_RGB, KTX_ZERO, }, // ETC2A1
  3565. { KTX_COMPRESSED_RGB_PVRTC_2BPPV1_IMG, KTX_COMPRESSED_SRGB_PVRTC_2BPPV1_EXT, KTX_RGB, KTX_ZERO, }, // PTC12
  3566. { KTX_COMPRESSED_RGB_PVRTC_4BPPV1_IMG, KTX_COMPRESSED_SRGB_PVRTC_4BPPV1_EXT, KTX_RGB, KTX_ZERO, }, // PTC14
  3567. { KTX_COMPRESSED_RGBA_PVRTC_2BPPV1_IMG, KTX_COMPRESSED_SRGB_ALPHA_PVRTC_2BPPV1_EXT, KTX_RGBA, KTX_ZERO, }, // PTC12A
  3568. { KTX_COMPRESSED_RGBA_PVRTC_4BPPV1_IMG, KTX_COMPRESSED_SRGB_ALPHA_PVRTC_4BPPV1_EXT, KTX_RGBA, KTX_ZERO, }, // PTC14A
  3569. { KTX_COMPRESSED_RGBA_PVRTC_2BPPV2_IMG, KTX_ZERO, KTX_RGBA, KTX_ZERO, }, // PTC22
  3570. { KTX_COMPRESSED_RGBA_PVRTC_4BPPV2_IMG, KTX_ZERO, KTX_RGBA, KTX_ZERO, }, // PTC24
  3571. { KTX_ATC_RGB_AMD, KTX_ZERO, KTX_RGB, KTX_ZERO, }, // ATC
  3572. { KTX_ATC_RGBA_EXPLICIT_ALPHA_AMD, KTX_ZERO, KTX_RGBA, KTX_ZERO, }, // ATCE
  3573. { KTX_ATC_RGBA_INTERPOLATED_ALPHA_AMD, KTX_ZERO, KTX_RGBA, KTX_ZERO, }, // ATCI
  3574. { KTX_COMPRESSED_RGBA_ASTC_4x4_KHR, KTX_COMPRESSED_SRGB8_ALPHA8_ASTC_4x4_KHR, KTX_RGBA, KTX_ZERO, }, // ASTC4x4
  3575. { KTX_COMPRESSED_RGBA_ASTC_5x4_KHR, KTX_COMPRESSED_SRGB8_ALPHA8_ASTC_5x4_KHR, KTX_RGBA, KTX_ZERO, }, // ASTC5x4
  3576. { KTX_COMPRESSED_RGBA_ASTC_5x5_KHR, KTX_COMPRESSED_SRGB8_ALPHA8_ASTC_5x5_KHR, KTX_RGBA, KTX_ZERO, }, // ASTC5x5
  3577. { KTX_COMPRESSED_RGBA_ASTC_6x5_KHR, KTX_COMPRESSED_SRGB8_ALPHA8_ASTC_6x5_KHR, KTX_RGBA, KTX_ZERO, }, // ASTC6x5
  3578. { KTX_COMPRESSED_RGBA_ASTC_6x6_KHR, KTX_COMPRESSED_SRGB8_ALPHA8_ASTC_6x6_KHR, KTX_RGBA, KTX_ZERO, }, // ASTC6x6
  3579. { KTX_COMPRESSED_RGBA_ASTC_8x5_KHR, KTX_COMPRESSED_SRGB8_ALPHA8_ASTC_8x5_KHR, KTX_RGBA, KTX_ZERO, }, // ASTC8x5
  3580. { KTX_COMPRESSED_RGBA_ASTC_8x6_KHR, KTX_COMPRESSED_SRGB8_ALPHA8_ASTC_8x6_KHR, KTX_RGBA, KTX_ZERO, }, // ASTC8x6
  3581. { KTX_COMPRESSED_RGBA_ASTC_8x8_KHR, KTX_COMPRESSED_SRGB8_ALPHA8_ASTC_8x8_KHR, KTX_RGBA, KTX_ZERO, }, // ASTC8x8
  3582. { KTX_COMPRESSED_RGBA_ASTC_10x5_KHR, KTX_COMPRESSED_SRGB8_ALPHA8_ASTC_10x5_KHR, KTX_RGBA, KTX_ZERO, }, // ASTC10x5
  3583. { KTX_COMPRESSED_RGBA_ASTC_10x6_KHR, KTX_COMPRESSED_SRGB8_ALPHA8_ASTC_10x6_KHR, KTX_RGBA, KTX_ZERO, }, // ASTC10x6
  3584. { KTX_COMPRESSED_RGBA_ASTC_10x8_KHR, KTX_COMPRESSED_SRGB8_ALPHA8_ASTC_10x8_KHR, KTX_RGBA, KTX_ZERO, }, // ASTC10x8
  3585. { KTX_COMPRESSED_RGBA_ASTC_10x10_KHR, KTX_COMPRESSED_SRGB8_ALPHA8_ASTC_10x10_KHR, KTX_RGBA, KTX_ZERO, }, // ASTC10x10
  3586. { KTX_COMPRESSED_RGBA_ASTC_12x10_KHR, KTX_COMPRESSED_SRGB8_ALPHA8_ASTC_12x10_KHR, KTX_RGBA, KTX_ZERO, }, // ASTC12x10
  3587. { KTX_COMPRESSED_RGBA_ASTC_12x12_KHR, KTX_COMPRESSED_SRGB8_ALPHA8_ASTC_12x12_KHR, KTX_RGBA, KTX_ZERO, }, // ASTC12x12
  3588. { KTX_ZERO, KTX_ZERO, KTX_ZERO, KTX_ZERO, }, // Unknown
  3589. { KTX_ZERO, KTX_ZERO, KTX_ZERO, KTX_ZERO, }, // R1
  3590. { KTX_ALPHA, KTX_ZERO, KTX_ALPHA, KTX_UNSIGNED_BYTE, }, // A8
  3591. { KTX_R8, KTX_ZERO, KTX_RED, KTX_UNSIGNED_BYTE, }, // R8
  3592. { KTX_R8I, KTX_ZERO, KTX_RED, KTX_BYTE, }, // R8S
  3593. { KTX_R8UI, KTX_ZERO, KTX_RED, KTX_UNSIGNED_BYTE, }, // R8S
  3594. { KTX_R8_SNORM, KTX_ZERO, KTX_RED, KTX_BYTE, }, // R8S
  3595. { KTX_R16, KTX_ZERO, KTX_RED, KTX_UNSIGNED_SHORT, }, // R16
  3596. { KTX_R16I, KTX_ZERO, KTX_RED, KTX_SHORT, }, // R16I
  3597. { KTX_R16UI, KTX_ZERO, KTX_RED, KTX_UNSIGNED_SHORT, }, // R16U
  3598. { KTX_R16F, KTX_ZERO, KTX_RED, KTX_HALF_FLOAT, }, // R16F
  3599. { KTX_R16_SNORM, KTX_ZERO, KTX_RED, KTX_SHORT, }, // R16S
  3600. { KTX_R32I, KTX_ZERO, KTX_RED, KTX_INT, }, // R32I
  3601. { KTX_R32UI, KTX_ZERO, KTX_RED, KTX_UNSIGNED_INT, }, // R32U
  3602. { KTX_R32F, KTX_ZERO, KTX_RED, KTX_FLOAT, }, // R32F
  3603. { KTX_RG8, KTX_ZERO, KTX_RG, KTX_UNSIGNED_BYTE, }, // RG8
  3604. { KTX_RG8I, KTX_ZERO, KTX_RG, KTX_BYTE, }, // RG8I
  3605. { KTX_RG8UI, KTX_ZERO, KTX_RG, KTX_UNSIGNED_BYTE, }, // RG8U
  3606. { KTX_RG8_SNORM, KTX_ZERO, KTX_RG, KTX_BYTE, }, // RG8S
  3607. { KTX_RG16, KTX_ZERO, KTX_RG, KTX_UNSIGNED_SHORT, }, // RG16
  3608. { KTX_RG16I, KTX_ZERO, KTX_RG, KTX_SHORT, }, // RG16
  3609. { KTX_RG16UI, KTX_ZERO, KTX_RG, KTX_UNSIGNED_SHORT, }, // RG16
  3610. { KTX_RG16F, KTX_ZERO, KTX_RG, KTX_FLOAT, }, // RG16F
  3611. { KTX_RG16_SNORM, KTX_ZERO, KTX_RG, KTX_SHORT, }, // RG16S
  3612. { KTX_RG32I, KTX_ZERO, KTX_RG, KTX_INT, }, // RG32I
  3613. { KTX_RG32UI, KTX_ZERO, KTX_RG, KTX_UNSIGNED_INT, }, // RG32U
  3614. { KTX_RG32F, KTX_ZERO, KTX_RG, KTX_FLOAT, }, // RG32F
  3615. { KTX_RGB8, KTX_SRGB8, KTX_RGB, KTX_UNSIGNED_BYTE, }, // RGB8
  3616. { KTX_RGB8I, KTX_ZERO, KTX_RGB, KTX_BYTE, }, // RGB8I
  3617. { KTX_RGB8UI, KTX_ZERO, KTX_RGB, KTX_UNSIGNED_BYTE, }, // RGB8U
  3618. { KTX_RGB8_SNORM, KTX_ZERO, KTX_RGB, KTX_BYTE, }, // RGB8S
  3619. { KTX_RGB9_E5, KTX_ZERO, KTX_RGB, KTX_UNSIGNED_INT_5_9_9_9_REV, }, // RGB9E5F
  3620. { KTX_BGRA, KTX_SRGB8_ALPHA8, KTX_BGRA, KTX_UNSIGNED_BYTE, }, // BGRA8
  3621. { KTX_RGBA8, KTX_SRGB8_ALPHA8, KTX_RGBA, KTX_UNSIGNED_BYTE, }, // RGBA8
  3622. { KTX_RGBA8I, KTX_ZERO, KTX_RGBA, KTX_BYTE, }, // RGBA8I
  3623. { KTX_RGBA8UI, KTX_ZERO, KTX_RGBA, KTX_UNSIGNED_BYTE, }, // RGBA8U
  3624. { KTX_RGBA8_SNORM, KTX_ZERO, KTX_RGBA, KTX_BYTE, }, // RGBA8S
  3625. { KTX_RGBA16, KTX_ZERO, KTX_RGBA, KTX_UNSIGNED_SHORT, }, // RGBA16
  3626. { KTX_RGBA16I, KTX_ZERO, KTX_RGBA, KTX_SHORT, }, // RGBA16I
  3627. { KTX_RGBA16UI, KTX_ZERO, KTX_RGBA, KTX_UNSIGNED_SHORT, }, // RGBA16U
  3628. { KTX_RGBA16F, KTX_ZERO, KTX_RGBA, KTX_HALF_FLOAT, }, // RGBA16F
  3629. { KTX_RGBA16_SNORM, KTX_ZERO, KTX_RGBA, KTX_SHORT, }, // RGBA16S
  3630. { KTX_RGBA32I, KTX_ZERO, KTX_RGBA, KTX_INT, }, // RGBA32I
  3631. { KTX_RGBA32UI, KTX_ZERO, KTX_RGBA, KTX_UNSIGNED_INT, }, // RGBA32U
  3632. { KTX_RGBA32F, KTX_ZERO, KTX_RGBA, KTX_FLOAT, }, // RGBA32F
  3633. { KTX_RGB565, KTX_ZERO, KTX_RGB, KTX_UNSIGNED_SHORT_5_6_5, }, // B5G6R5
  3634. { KTX_RGB565, KTX_ZERO, KTX_RGB, KTX_UNSIGNED_SHORT_5_6_5, }, // R5G6B5
  3635. { KTX_RGBA4, KTX_ZERO, KTX_BGRA, KTX_UNSIGNED_SHORT_4_4_4_4, }, // BGRA4
  3636. { KTX_RGBA4, KTX_ZERO, KTX_RGBA, KTX_UNSIGNED_SHORT_4_4_4_4, }, // RGBA4
  3637. { KTX_RGB5_A1, KTX_ZERO, KTX_BGRA, KTX_UNSIGNED_SHORT_5_5_5_1, }, // BGR5A1
  3638. { KTX_RGB5_A1, KTX_ZERO, KTX_RGBA, KTX_UNSIGNED_SHORT_5_5_5_1, }, // RGB5A1
  3639. { KTX_RGB10_A2, KTX_ZERO, KTX_RGBA, KTX_UNSIGNED_INT_2_10_10_10_REV, }, // RGB10A2
  3640. { KTX_R11F_G11F_B10F, KTX_ZERO, KTX_RGB, KTX_UNSIGNED_INT_10F_11F_11F_REV, }, // RG11B10F
  3641. };
  3642. static_assert(TextureFormat::UnknownDepth == BX_COUNTOF(s_translateKtxFormat) );
  3643. struct KtxFormatInfo2
  3644. {
  3645. uint32_t m_internalFmt;
  3646. TextureFormat::Enum m_format;
  3647. };
  3648. static const KtxFormatInfo2 s_translateKtxFormat2[] =
  3649. {
  3650. { KTX_A8, TextureFormat::A8 },
  3651. { KTX_RED, TextureFormat::R8 },
  3652. { KTX_RGB, TextureFormat::RGB8 },
  3653. { KTX_RGBA, TextureFormat::RGBA8 },
  3654. { KTX_COMPRESSED_RGB_S3TC_DXT1_EXT, TextureFormat::BC1 },
  3655. };
  3656. bool imageParseKtx(ImageContainer& _imageContainer, bx::ReaderSeekerI* _reader, bx::Error* _err)
  3657. {
  3658. BX_ERROR_SCOPE(_err);
  3659. uint8_t identifier[8];
  3660. bx::read(_reader, identifier, _err);
  3661. if (identifier[1] != '1'
  3662. && identifier[2] != '1')
  3663. {
  3664. BX_ERROR_SET(_err, BIMG_ERROR, "KTX: Unrecognized version.");
  3665. return false;
  3666. }
  3667. uint32_t endianness;
  3668. bx::read(_reader, endianness, _err);
  3669. bool fromLittleEndian = 0x04030201 == endianness;
  3670. uint32_t glType;
  3671. bx::readHE(_reader, glType, fromLittleEndian, _err);
  3672. uint32_t glTypeSize;
  3673. bx::readHE(_reader, glTypeSize, fromLittleEndian, _err);
  3674. uint32_t glFormat;
  3675. bx::readHE(_reader, glFormat, fromLittleEndian, _err);
  3676. uint32_t glInternalFormat;
  3677. bx::readHE(_reader, glInternalFormat, fromLittleEndian, _err);
  3678. uint32_t glBaseInternalFormat;
  3679. bx::readHE(_reader, glBaseInternalFormat, fromLittleEndian, _err);
  3680. uint32_t width;
  3681. bx::readHE(_reader, width, fromLittleEndian, _err);
  3682. uint32_t height;
  3683. bx::readHE(_reader, height, fromLittleEndian, _err);
  3684. uint32_t depth;
  3685. bx::readHE(_reader, depth, fromLittleEndian, _err);
  3686. uint32_t numberOfArrayElements;
  3687. bx::readHE(_reader, numberOfArrayElements, fromLittleEndian, _err);
  3688. uint32_t numFaces;
  3689. bx::readHE(_reader, numFaces, fromLittleEndian, _err);
  3690. uint32_t numMips;
  3691. bx::readHE(_reader, numMips, fromLittleEndian, _err);
  3692. uint32_t metaDataSize;
  3693. bx::readHE(_reader, metaDataSize, fromLittleEndian, _err);
  3694. if (!_err->isOk() )
  3695. {
  3696. return false;
  3697. }
  3698. // skip meta garbage...
  3699. int64_t offset = bx::skip(_reader, metaDataSize);
  3700. TextureFormat::Enum format = TextureFormat::Unknown;
  3701. bool hasAlpha = false;
  3702. bool srgb = false;
  3703. for (uint32_t ii = 0; ii < BX_COUNTOF(s_translateKtxFormat); ++ii)
  3704. {
  3705. if (s_translateKtxFormat[ii].m_internalFmt == glInternalFormat)
  3706. {
  3707. format = TextureFormat::Enum(ii);
  3708. break;
  3709. }
  3710. if (s_translateKtxFormat[ii].m_internalFmtSrgb == glInternalFormat
  3711. && s_translateKtxFormat[ii].m_fmt == glBaseInternalFormat)
  3712. {
  3713. format = TextureFormat::Enum(ii);
  3714. srgb = true;
  3715. break;
  3716. }
  3717. }
  3718. if (TextureFormat::Unknown == format)
  3719. {
  3720. for (uint32_t ii = 0; ii < BX_COUNTOF(s_translateKtxFormat2); ++ii)
  3721. {
  3722. if (s_translateKtxFormat2[ii].m_internalFmt == glInternalFormat)
  3723. {
  3724. format = s_translateKtxFormat2[ii].m_format;
  3725. break;
  3726. }
  3727. }
  3728. }
  3729. _imageContainer.m_allocator = NULL;
  3730. _imageContainer.m_data = NULL;
  3731. _imageContainer.m_size = 0;
  3732. _imageContainer.m_offset = (uint32_t)offset;
  3733. _imageContainer.m_width = width;
  3734. _imageContainer.m_height = height;
  3735. _imageContainer.m_depth = bx::max<uint32_t>(depth, 1);
  3736. _imageContainer.m_format = format;
  3737. _imageContainer.m_orientation = Orientation::R0;
  3738. _imageContainer.m_numLayers = uint16_t(bx::max<uint32_t>(numberOfArrayElements, 1) );
  3739. _imageContainer.m_numMips = uint8_t(bx::max<uint32_t>(numMips, 1) );
  3740. _imageContainer.m_hasAlpha = hasAlpha;
  3741. _imageContainer.m_cubeMap = numFaces == 6;
  3742. _imageContainer.m_ktx = true;
  3743. _imageContainer.m_ktxLE = fromLittleEndian;
  3744. _imageContainer.m_pvr3 = false;
  3745. _imageContainer.m_srgb = srgb;
  3746. if (TextureFormat::Unknown == format)
  3747. {
  3748. BX_ERROR_SET(_err, BIMG_ERROR, "KTX: Unrecognized image format.");
  3749. return false;
  3750. }
  3751. return true;
  3752. }
  3753. ImageContainer* imageParseKtx(bx::AllocatorI* _allocator, const void* _src, uint32_t _size, bx::Error* _err)
  3754. {
  3755. return imageParseT<KTX_MAGIC, imageParseKtx>(_allocator, _src, _size, _err);
  3756. }
  3757. // PVR3
  3758. #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) )
  3759. #define PVR3_MAGIC BX_MAKEFOURCC('P', 'V', 'R', 3)
  3760. #define PVR3_HEADER_SIZE 52
  3761. #define PVR3_PVRTC1_2BPP_RGB 0
  3762. #define PVR3_PVRTC1_2BPP_RGBA 1
  3763. #define PVR3_PVRTC1_4BPP_RGB 2
  3764. #define PVR3_PVRTC1_4BPP_RGBA 3
  3765. #define PVR3_PVRTC2_2BPP_RGBA 4
  3766. #define PVR3_PVRTC2_4BPP_RGBA 5
  3767. #define PVR3_ETC1 6
  3768. #define PVR3_DXT1 7
  3769. #define PVR3_DXT2 8
  3770. #define PVR3_DXT3 9
  3771. #define PVR3_DXT4 10
  3772. #define PVR3_DXT5 11
  3773. #define PVR3_BC4 12
  3774. #define PVR3_BC5 13
  3775. #define PVR3_R8 PVR3_MAKE8CC('r', 0, 0, 0, 8, 0, 0, 0)
  3776. #define PVR3_R16 PVR3_MAKE8CC('r', 0, 0, 0, 16, 0, 0, 0)
  3777. #define PVR3_R32 PVR3_MAKE8CC('r', 0, 0, 0, 32, 0, 0, 0)
  3778. #define PVR3_RG8 PVR3_MAKE8CC('r', 'g', 0, 0, 8, 8, 0, 0)
  3779. #define PVR3_RG16 PVR3_MAKE8CC('r', 'g', 0, 0, 16, 16, 0, 0)
  3780. #define PVR3_RG32 PVR3_MAKE8CC('r', 'g', 0, 0, 32, 32, 0, 0)
  3781. #define PVR3_BGRA8 PVR3_MAKE8CC('b', 'g', 'r', 'a', 8, 8, 8, 8)
  3782. #define PVR3_RGBA16 PVR3_MAKE8CC('r', 'g', 'b', 'a', 16, 16, 16, 16)
  3783. #define PVR3_RGBA32 PVR3_MAKE8CC('r', 'g', 'b', 'a', 32, 32, 32, 32)
  3784. #define PVR3_BGR565 PVR3_MAKE8CC('b', 'g', 'r', 0, 5, 6, 5, 0)
  3785. #define PVR3_RGB565 PVR3_MAKE8CC('r', 'g', 'b', 0, 5, 6, 5, 0)
  3786. #define PVR3_BGRA4 PVR3_MAKE8CC('b', 'g', 'r', 'a', 4, 4, 4, 4)
  3787. #define PVR3_RGBA4 PVR3_MAKE8CC('r', 'g', 'b', 'a', 4, 4, 4, 4)
  3788. #define PVR3_BGRA51 PVR3_MAKE8CC('b', 'g', 'r', 'a', 5, 5, 5, 1)
  3789. #define PVR3_RGBA51 PVR3_MAKE8CC('r', 'g', 'b', 'a', 5, 5, 5, 1)
  3790. #define PVR3_RGB10A2 PVR3_MAKE8CC('r', 'g', 'b', 'a', 10, 10, 10, 2)
  3791. #define PVR3_CHANNEL_TYPE_ANY UINT32_MAX
  3792. #define PVR3_CHANNEL_TYPE_FLOAT UINT32_C(12)
  3793. struct TranslatePvr3Format
  3794. {
  3795. uint64_t m_format;
  3796. uint32_t m_channelTypeMask;
  3797. TextureFormat::Enum m_textureFormat;
  3798. };
  3799. static const TranslatePvr3Format s_translatePvr3Format[] =
  3800. {
  3801. { PVR3_PVRTC1_2BPP_RGB, PVR3_CHANNEL_TYPE_ANY, TextureFormat::PTC12 },
  3802. { PVR3_PVRTC1_2BPP_RGBA, PVR3_CHANNEL_TYPE_ANY, TextureFormat::PTC12A },
  3803. { PVR3_PVRTC1_4BPP_RGB, PVR3_CHANNEL_TYPE_ANY, TextureFormat::PTC14 },
  3804. { PVR3_PVRTC1_4BPP_RGBA, PVR3_CHANNEL_TYPE_ANY, TextureFormat::PTC14A },
  3805. { PVR3_PVRTC2_2BPP_RGBA, PVR3_CHANNEL_TYPE_ANY, TextureFormat::PTC22 },
  3806. { PVR3_PVRTC2_4BPP_RGBA, PVR3_CHANNEL_TYPE_ANY, TextureFormat::PTC24 },
  3807. { PVR3_ETC1, PVR3_CHANNEL_TYPE_ANY, TextureFormat::ETC1 },
  3808. { PVR3_DXT1, PVR3_CHANNEL_TYPE_ANY, TextureFormat::BC1 },
  3809. { PVR3_DXT2, PVR3_CHANNEL_TYPE_ANY, TextureFormat::BC2 },
  3810. { PVR3_DXT3, PVR3_CHANNEL_TYPE_ANY, TextureFormat::BC2 },
  3811. { PVR3_DXT4, PVR3_CHANNEL_TYPE_ANY, TextureFormat::BC3 },
  3812. { PVR3_DXT5, PVR3_CHANNEL_TYPE_ANY, TextureFormat::BC3 },
  3813. { PVR3_BC4, PVR3_CHANNEL_TYPE_ANY, TextureFormat::BC4 },
  3814. { PVR3_BC5, PVR3_CHANNEL_TYPE_ANY, TextureFormat::BC5 },
  3815. { PVR3_R8, PVR3_CHANNEL_TYPE_ANY, TextureFormat::R8 },
  3816. { PVR3_R16, PVR3_CHANNEL_TYPE_ANY, TextureFormat::R16U },
  3817. { PVR3_R16, PVR3_CHANNEL_TYPE_FLOAT, TextureFormat::R16F },
  3818. { PVR3_R32, PVR3_CHANNEL_TYPE_ANY, TextureFormat::R32U },
  3819. { PVR3_R32, PVR3_CHANNEL_TYPE_FLOAT, TextureFormat::R32F },
  3820. { PVR3_RG8, PVR3_CHANNEL_TYPE_ANY, TextureFormat::RG8 },
  3821. { PVR3_RG16, PVR3_CHANNEL_TYPE_ANY, TextureFormat::RG16 },
  3822. { PVR3_RG16, PVR3_CHANNEL_TYPE_FLOAT, TextureFormat::RG16F },
  3823. { PVR3_RG32, PVR3_CHANNEL_TYPE_ANY, TextureFormat::RG16 },
  3824. { PVR3_RG32, PVR3_CHANNEL_TYPE_FLOAT, TextureFormat::RG32F },
  3825. { PVR3_BGRA8, PVR3_CHANNEL_TYPE_ANY, TextureFormat::BGRA8 },
  3826. { PVR3_RGBA16, PVR3_CHANNEL_TYPE_ANY, TextureFormat::RGBA16 },
  3827. { PVR3_RGBA16, PVR3_CHANNEL_TYPE_FLOAT, TextureFormat::RGBA16F },
  3828. { PVR3_RGBA32, PVR3_CHANNEL_TYPE_ANY, TextureFormat::RGBA32U },
  3829. { PVR3_RGBA32, PVR3_CHANNEL_TYPE_FLOAT, TextureFormat::RGBA32F },
  3830. { PVR3_RGB565, PVR3_CHANNEL_TYPE_ANY, TextureFormat::B5G6R5 },
  3831. { PVR3_RGB565, PVR3_CHANNEL_TYPE_ANY, TextureFormat::R5G6B5 },
  3832. { PVR3_BGRA4, PVR3_CHANNEL_TYPE_ANY, TextureFormat::BGRA4 },
  3833. { PVR3_RGBA4, PVR3_CHANNEL_TYPE_ANY, TextureFormat::RGBA4 },
  3834. { PVR3_BGRA51, PVR3_CHANNEL_TYPE_ANY, TextureFormat::BGR5A1 },
  3835. { PVR3_RGBA51, PVR3_CHANNEL_TYPE_ANY, TextureFormat::RGB5A1 },
  3836. { PVR3_RGB10A2, PVR3_CHANNEL_TYPE_ANY, TextureFormat::RGB10A2 },
  3837. };
  3838. bool imageParsePvr3(ImageContainer& _imageContainer, bx::ReaderSeekerI* _reader, bx::Error* _err)
  3839. {
  3840. BX_ERROR_SCOPE(_err);
  3841. uint32_t flags;
  3842. bx::read(_reader, flags, _err);
  3843. uint64_t pixelFormat;
  3844. bx::read(_reader, pixelFormat, _err);
  3845. uint32_t colorSpace;
  3846. bx::read(_reader, colorSpace, _err); // 0 - linearRGB, 1 - sRGB
  3847. uint32_t channelType;
  3848. bx::read(_reader, channelType, _err);
  3849. uint32_t height;
  3850. bx::read(_reader, height, _err);
  3851. uint32_t width;
  3852. bx::read(_reader, width, _err);
  3853. uint32_t depth;
  3854. bx::read(_reader, depth, _err);
  3855. uint32_t numSurfaces;
  3856. bx::read(_reader, numSurfaces, _err);
  3857. uint32_t numFaces;
  3858. bx::read(_reader, numFaces, _err);
  3859. uint32_t numMips;
  3860. bx::read(_reader, numMips, _err);
  3861. uint32_t metaDataSize;
  3862. bx::read(_reader, metaDataSize, _err);
  3863. if (!_err->isOk() )
  3864. {
  3865. return false;
  3866. }
  3867. // skip meta garbage...
  3868. int64_t offset = bx::skip(_reader, metaDataSize);
  3869. TextureFormat::Enum format = TextureFormat::Unknown;
  3870. bool hasAlpha = false;
  3871. for (uint32_t ii = 0; ii < BX_COUNTOF(s_translatePvr3Format); ++ii)
  3872. {
  3873. if (s_translatePvr3Format[ii].m_format == pixelFormat
  3874. && channelType == (s_translatePvr3Format[ii].m_channelTypeMask & channelType) )
  3875. {
  3876. format = s_translatePvr3Format[ii].m_textureFormat;
  3877. break;
  3878. }
  3879. }
  3880. _imageContainer.m_allocator = NULL;
  3881. _imageContainer.m_data = NULL;
  3882. _imageContainer.m_size = 0;
  3883. _imageContainer.m_offset = (uint32_t)offset;
  3884. _imageContainer.m_width = width;
  3885. _imageContainer.m_height = height;
  3886. _imageContainer.m_depth = depth;
  3887. _imageContainer.m_format = format;
  3888. _imageContainer.m_orientation = Orientation::R0;
  3889. _imageContainer.m_numLayers = 1;
  3890. _imageContainer.m_numMips = uint8_t(bx::max<uint32_t>(numMips, 1) );
  3891. _imageContainer.m_hasAlpha = hasAlpha;
  3892. _imageContainer.m_cubeMap = numFaces > 1;
  3893. _imageContainer.m_ktx = false;
  3894. _imageContainer.m_ktxLE = false;
  3895. _imageContainer.m_pvr3 = true;
  3896. _imageContainer.m_srgb = colorSpace > 0;
  3897. return TextureFormat::Unknown != format;
  3898. }
  3899. ImageContainer* imageParsePvr3(bx::AllocatorI* _allocator, const void* _src, uint32_t _size, bx::Error* _err)
  3900. {
  3901. return imageParseT<PVR3_MAGIC, imageParsePvr3>(_allocator, _src, _size, _err);
  3902. }
  3903. bool imageParse(ImageContainer& _imageContainer, bx::ReaderSeekerI* _reader, bx::Error* _err)
  3904. {
  3905. BX_ERROR_SCOPE(_err);
  3906. uint32_t magic;
  3907. bx::read(_reader, magic, _err);
  3908. if (DDS_MAGIC == magic)
  3909. {
  3910. return imageParseDds(_imageContainer, _reader, _err);
  3911. }
  3912. else if (KTX_MAGIC == magic)
  3913. {
  3914. return imageParseKtx(_imageContainer, _reader, _err);
  3915. }
  3916. else if (PVR3_MAGIC == magic)
  3917. {
  3918. return imageParsePvr3(_imageContainer, _reader, _err);
  3919. }
  3920. else if (BIMG_CHUNK_MAGIC_GNF == magic)
  3921. {
  3922. return imageParseGnf(_imageContainer, _reader, _err);
  3923. }
  3924. else if (BIMG_CHUNK_MAGIC_TEX == magic)
  3925. {
  3926. TextureCreate tc;
  3927. bx::read(_reader, tc, _err);
  3928. _imageContainer.m_format = tc.m_format;
  3929. _imageContainer.m_orientation = Orientation::R0;
  3930. _imageContainer.m_offset = UINT32_MAX;
  3931. _imageContainer.m_allocator = NULL;
  3932. if (NULL == tc.m_mem)
  3933. {
  3934. _imageContainer.m_data = NULL;
  3935. _imageContainer.m_size = 0;
  3936. }
  3937. else
  3938. {
  3939. _imageContainer.m_data = tc.m_mem->data;
  3940. _imageContainer.m_size = tc.m_mem->size;
  3941. }
  3942. _imageContainer.m_width = tc.m_width;
  3943. _imageContainer.m_height = tc.m_height;
  3944. _imageContainer.m_depth = tc.m_depth;
  3945. _imageContainer.m_numLayers = tc.m_numLayers;
  3946. _imageContainer.m_numMips = tc.m_numMips;
  3947. _imageContainer.m_hasAlpha = false;
  3948. _imageContainer.m_cubeMap = tc.m_cubeMap;
  3949. _imageContainer.m_ktx = false;
  3950. _imageContainer.m_ktxLE = false;
  3951. _imageContainer.m_pvr3 = false;
  3952. _imageContainer.m_srgb = false;
  3953. return _err->isOk();
  3954. }
  3955. BX_TRACE("Unrecognized image format (magic: 0x%08x)!", magic);
  3956. BX_ERROR_SET(_err, BIMG_ERROR, "Unrecognized image format.");
  3957. return false;
  3958. }
  3959. bool imageParse(ImageContainer& _imageContainer, const void* _data, uint32_t _size, bx::Error* _err)
  3960. {
  3961. BX_ERROR_SCOPE(_err);
  3962. bx::MemoryReader reader(_data, _size);
  3963. return imageParse(_imageContainer, &reader, _err);
  3964. }
  3965. 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)
  3966. {
  3967. const uint8_t* src = (const uint8_t*)_src;
  3968. uint8_t* dst = (uint8_t*)_dst;
  3969. const uint32_t srcBpp = s_imageBlockInfo[_srcFormat].bitsPerPixel;
  3970. const uint32_t srcPitch = _width*srcBpp/8;
  3971. for (uint32_t zz = 0; zz < _depth; ++zz, src += _height*srcPitch, dst += _height*_dstPitch)
  3972. {
  3973. if (isCompressed(_srcFormat))
  3974. {
  3975. uint32_t size = imageGetSize(NULL, uint16_t(_width), uint16_t(_height), 0, false, false, 1, TextureFormat::RGBA8);
  3976. void* temp = bx::alloc(_allocator, size);
  3977. imageDecodeToRgba8(_allocator, temp, _src, _width, _height, _width*4, _srcFormat);
  3978. imageConvert(_allocator, dst, TextureFormat::R8, temp, TextureFormat::RGBA8, _width, _height, 1, _width*4, _dstPitch);
  3979. bx::free(_allocator, temp);
  3980. }
  3981. else
  3982. {
  3983. imageConvert(_allocator, dst, TextureFormat::R8, src, _srcFormat, _width, _height, 1, srcPitch, _dstPitch);
  3984. }
  3985. }
  3986. }
  3987. void imageDecodeToBgra8(bx::AllocatorI* _allocator, void* _dst, const void* _src, uint32_t _width, uint32_t _height, uint32_t _dstPitch, TextureFormat::Enum _srcFormat)
  3988. {
  3989. const uint8_t* src = (const uint8_t*)_src;
  3990. uint8_t* dst = (uint8_t*)_dst;
  3991. uint32_t width = _width/4;
  3992. uint32_t height = _height/4;
  3993. uint8_t temp[16*4];
  3994. switch (_srcFormat)
  3995. {
  3996. case TextureFormat::BC1:
  3997. if (BX_ENABLED(BIMG_DECODE_BC1) )
  3998. {
  3999. for (uint32_t yy = 0; yy < height; ++yy)
  4000. {
  4001. for (uint32_t xx = 0; xx < width; ++xx)
  4002. {
  4003. decodeBlockDxt1(temp, src);
  4004. src += 8;
  4005. uint8_t* block = &dst[yy*_dstPitch*4 + xx*16];
  4006. bx::memCopy(&block[0*_dstPitch], &temp[ 0], 16);
  4007. bx::memCopy(&block[1*_dstPitch], &temp[16], 16);
  4008. bx::memCopy(&block[2*_dstPitch], &temp[32], 16);
  4009. bx::memCopy(&block[3*_dstPitch], &temp[48], 16);
  4010. }
  4011. }
  4012. }
  4013. else
  4014. {
  4015. BX_WARN(false, "BC1 decoder is disabled (BIMG_DECODE_BC1).");
  4016. imageCheckerboard(_dst, _width, _height, 16, UINT32_C(0xff000000), UINT32_C(0xff00ff00) );
  4017. }
  4018. break;
  4019. case TextureFormat::BC2:
  4020. if (BX_ENABLED(BIMG_DECODE_BC2) )
  4021. {
  4022. for (uint32_t yy = 0; yy < height; ++yy)
  4023. {
  4024. for (uint32_t xx = 0; xx < width; ++xx)
  4025. {
  4026. decodeBlockDxt23A(temp+3, src);
  4027. src += 8;
  4028. decodeBlockDxt(temp, src);
  4029. src += 8;
  4030. uint8_t* block = &dst[yy*_dstPitch*4 + xx*16];
  4031. bx::memCopy(&block[0*_dstPitch], &temp[ 0], 16);
  4032. bx::memCopy(&block[1*_dstPitch], &temp[16], 16);
  4033. bx::memCopy(&block[2*_dstPitch], &temp[32], 16);
  4034. bx::memCopy(&block[3*_dstPitch], &temp[48], 16);
  4035. }
  4036. }
  4037. }
  4038. else
  4039. {
  4040. BX_WARN(false, "BC2 decoder is disabled (BIMG_DECODE_BC2).");
  4041. imageCheckerboard(_dst, _width, _height, 16, UINT32_C(0xff000000), UINT32_C(0xff00ff00) );
  4042. }
  4043. break;
  4044. case TextureFormat::BC3:
  4045. if (BX_ENABLED(BIMG_DECODE_BC3) )
  4046. {
  4047. for (uint32_t yy = 0; yy < height; ++yy)
  4048. {
  4049. for (uint32_t xx = 0; xx < width; ++xx)
  4050. {
  4051. decodeBlockDxt45A(temp+3, src);
  4052. src += 8;
  4053. decodeBlockDxt(temp, src);
  4054. src += 8;
  4055. uint8_t* block = &dst[yy*_dstPitch*4 + xx*16];
  4056. bx::memCopy(&block[0*_dstPitch], &temp[ 0], 16);
  4057. bx::memCopy(&block[1*_dstPitch], &temp[16], 16);
  4058. bx::memCopy(&block[2*_dstPitch], &temp[32], 16);
  4059. bx::memCopy(&block[3*_dstPitch], &temp[48], 16);
  4060. }
  4061. }
  4062. }
  4063. else
  4064. {
  4065. BX_WARN(false, "BC3 decoder is disabled (BIMG_DECODE_BC3).");
  4066. imageCheckerboard(_dst, _width, _height, 16, UINT32_C(0xff000000), UINT32_C(0xff00ff00) );
  4067. }
  4068. break;
  4069. case TextureFormat::BC4:
  4070. if (BX_ENABLED(BIMG_DECODE_BC4) )
  4071. {
  4072. for (uint32_t yy = 0; yy < height; ++yy)
  4073. {
  4074. for (uint32_t xx = 0; xx < width; ++xx)
  4075. {
  4076. decodeBlockDxt45A(temp, src);
  4077. src += 8;
  4078. uint8_t* block = &dst[yy*_dstPitch*4 + xx*16];
  4079. bx::memCopy(&block[0*_dstPitch], &temp[ 0], 16);
  4080. bx::memCopy(&block[1*_dstPitch], &temp[16], 16);
  4081. bx::memCopy(&block[2*_dstPitch], &temp[32], 16);
  4082. bx::memCopy(&block[3*_dstPitch], &temp[48], 16);
  4083. }
  4084. }
  4085. }
  4086. else
  4087. {
  4088. BX_WARN(false, "BC4 decoder is disabled (BIMG_DECODE_BC4).");
  4089. imageCheckerboard(_dst, _width, _height, 16, UINT32_C(0xff000000), UINT32_C(0xff00ff00) );
  4090. }
  4091. break;
  4092. case TextureFormat::BC5:
  4093. if (BX_ENABLED(BIMG_DECODE_BC5) )
  4094. {
  4095. for (uint32_t yy = 0; yy < height; ++yy)
  4096. {
  4097. for (uint32_t xx = 0; xx < width; ++xx)
  4098. {
  4099. decodeBlockDxt45A(temp+2, src);
  4100. src += 8;
  4101. decodeBlockDxt45A(temp+1, src);
  4102. src += 8;
  4103. for (uint32_t ii = 0; ii < 16; ++ii)
  4104. {
  4105. float nx = temp[ii*4+2]*2.0f/255.0f - 1.0f;
  4106. float ny = temp[ii*4+1]*2.0f/255.0f - 1.0f;
  4107. float nz = bx::sqrt(1.0f - nx*nx - ny*ny);
  4108. temp[ii*4+0] = uint8_t( (nz + 1.0f)*255.0f/2.0f);
  4109. temp[ii*4+3] = 0;
  4110. }
  4111. uint8_t* block = &dst[yy*_dstPitch*4 + xx*16];
  4112. bx::memCopy(&block[0*_dstPitch], &temp[ 0], 16);
  4113. bx::memCopy(&block[1*_dstPitch], &temp[16], 16);
  4114. bx::memCopy(&block[2*_dstPitch], &temp[32], 16);
  4115. bx::memCopy(&block[3*_dstPitch], &temp[48], 16);
  4116. }
  4117. }
  4118. }
  4119. else
  4120. {
  4121. BX_WARN(false, "BC5 decoder is disabled (BIMG_DECODE_BC5).");
  4122. imageCheckerboard(_dst, _width, _height, 16, UINT32_C(0xff000000), UINT32_C(0xff00ff00) );
  4123. }
  4124. break;
  4125. case TextureFormat::BC6H:
  4126. if (BX_ENABLED(BIMG_DECODE_BC6) )
  4127. {
  4128. ImageContainer* rgba32f = imageAlloc(_allocator
  4129. , TextureFormat::RGBA32F
  4130. , uint16_t(_width)
  4131. , uint16_t(_height)
  4132. , uint16_t(1)
  4133. , 1
  4134. , false
  4135. , false
  4136. );
  4137. imageDecodeToRgba32f(_allocator, rgba32f->m_data, _src, _width, _height, 1, _width*16, _srcFormat);
  4138. imageConvert(_allocator, _dst, TextureFormat::BGRA8, rgba32f->m_data, TextureFormat::RGBA32F, _width, _height, 1, _width*16, _dstPitch);
  4139. imageFree(rgba32f);
  4140. }
  4141. else
  4142. {
  4143. BX_WARN(false, "BC6 decoder is disabled (BIMG_DECODE_BC6).");
  4144. imageCheckerboard(_dst, _width, _height, 16, UINT32_C(0xff000000), UINT32_C(0xff00ff00) );
  4145. }
  4146. break;
  4147. case TextureFormat::BC7:
  4148. if (BX_ENABLED(BIMG_DECODE_BC7) )
  4149. {
  4150. for (uint32_t yy = 0; yy < height; ++yy)
  4151. {
  4152. for (uint32_t xx = 0; xx < width; ++xx)
  4153. {
  4154. decodeBlockBc7(temp, src);
  4155. src += 16;
  4156. uint8_t* block = &dst[yy*_dstPitch*4 + xx*16];
  4157. bx::memCopy(&block[0*_dstPitch], &temp[ 0], 16);
  4158. bx::memCopy(&block[1*_dstPitch], &temp[16], 16);
  4159. bx::memCopy(&block[2*_dstPitch], &temp[32], 16);
  4160. bx::memCopy(&block[3*_dstPitch], &temp[48], 16);
  4161. }
  4162. }
  4163. }
  4164. else
  4165. {
  4166. BX_WARN(false, "BC7 decoder is disabled (BIMG_DECODE_BC7).");
  4167. imageCheckerboard(_dst, _width, _height, 16, UINT32_C(0xff000000), UINT32_C(0xff00ff00) );
  4168. }
  4169. break;
  4170. case TextureFormat::ETC1:
  4171. case TextureFormat::ETC2:
  4172. if (BX_ENABLED(BIMG_DECODE_ETC1 || BIMG_DECODE_ETC2) )
  4173. {
  4174. for (uint32_t yy = 0; yy < height; ++yy)
  4175. {
  4176. for (uint32_t xx = 0; xx < width; ++xx)
  4177. {
  4178. decodeBlockEtc12(temp, src);
  4179. src += 8;
  4180. uint8_t* block = &dst[yy*_dstPitch*4 + xx*16];
  4181. bx::memCopy(&block[0*_dstPitch], &temp[ 0], 16);
  4182. bx::memCopy(&block[1*_dstPitch], &temp[16], 16);
  4183. bx::memCopy(&block[2*_dstPitch], &temp[32], 16);
  4184. bx::memCopy(&block[3*_dstPitch], &temp[48], 16);
  4185. }
  4186. }
  4187. }
  4188. else
  4189. {
  4190. BX_WARN(false, "ETC1/ETC2 decoder is disabled (BIMG_DECODE_ETC1/ETC2).");
  4191. imageCheckerboard(_dst, _width, _height, 16, UINT32_C(0xff000000), UINT32_C(0xff00ff00) );
  4192. }
  4193. break;
  4194. case TextureFormat::ETC2A:
  4195. if (BX_ENABLED(BIMG_DECODE_ETC2))
  4196. {
  4197. for (uint32_t yy = 0; yy < height; ++yy)
  4198. {
  4199. for (uint32_t xx = 0; xx < width; ++xx)
  4200. {
  4201. decodeBlockEtc12(temp, src + 8);
  4202. decodeBlockEtc2Alpha(temp, src);
  4203. src += 16;
  4204. uint8_t* block = &dst[yy*_dstPitch * 4 + xx * 16];
  4205. bx::memCopy(&block[0 * _dstPitch], &temp[0], 16);
  4206. bx::memCopy(&block[1 * _dstPitch], &temp[16], 16);
  4207. bx::memCopy(&block[2 * _dstPitch], &temp[32], 16);
  4208. bx::memCopy(&block[3 * _dstPitch], &temp[48], 16);
  4209. }
  4210. }
  4211. }
  4212. else
  4213. {
  4214. BX_WARN(false, "ETC2 decoder is disabled (BIMG_DECODE_ETC2).");
  4215. imageCheckerboard(_dst, _width, _height, 16, UINT32_C(0xff000000), UINT32_C(0xff00ff00));
  4216. }
  4217. break;
  4218. case TextureFormat::ETC2A1:
  4219. BX_WARN(false, "ETC2A1 decoder is not implemented.");
  4220. imageCheckerboard(_dst, _width, _height, 16, UINT32_C(0xff000000), UINT32_C(0xffff0000) );
  4221. break;
  4222. case TextureFormat::PTC12:
  4223. BX_WARN(false, "PTC12 decoder is not implemented.");
  4224. imageCheckerboard(_dst, _width, _height, 16, UINT32_C(0xff000000), UINT32_C(0xffff00ff) );
  4225. break;
  4226. case TextureFormat::PTC12A:
  4227. BX_WARN(false, "PTC12A decoder is not implemented.");
  4228. imageCheckerboard(_dst, _width, _height, 16, UINT32_C(0xff000000), UINT32_C(0xffffff00) );
  4229. break;
  4230. case TextureFormat::PTC14:
  4231. for (uint32_t yy = 0; yy < height; ++yy)
  4232. {
  4233. for (uint32_t xx = 0; xx < width; ++xx)
  4234. {
  4235. decodeBlockPtc14(temp, src, xx, yy, width, height);
  4236. uint8_t* block = &dst[yy*_dstPitch*4 + xx*16];
  4237. bx::memCopy(&block[0*_dstPitch], &temp[ 0], 16);
  4238. bx::memCopy(&block[1*_dstPitch], &temp[16], 16);
  4239. bx::memCopy(&block[2*_dstPitch], &temp[32], 16);
  4240. bx::memCopy(&block[3*_dstPitch], &temp[48], 16);
  4241. }
  4242. }
  4243. break;
  4244. case TextureFormat::PTC14A:
  4245. for (uint32_t yy = 0; yy < height; ++yy)
  4246. {
  4247. for (uint32_t xx = 0; xx < width; ++xx)
  4248. {
  4249. decodeBlockPtc14A(temp, src, xx, yy, width, height);
  4250. uint8_t* block = &dst[yy*_dstPitch*4 + xx*16];
  4251. bx::memCopy(&block[0*_dstPitch], &temp[ 0], 16);
  4252. bx::memCopy(&block[1*_dstPitch], &temp[16], 16);
  4253. bx::memCopy(&block[2*_dstPitch], &temp[32], 16);
  4254. bx::memCopy(&block[3*_dstPitch], &temp[48], 16);
  4255. }
  4256. }
  4257. break;
  4258. case TextureFormat::PTC22:
  4259. BX_WARN(false, "PTC22 decoder is not implemented.");
  4260. imageCheckerboard(_dst, _width, _height, 16, UINT32_C(0xff00ff00), UINT32_C(0xff0000ff) );
  4261. break;
  4262. case TextureFormat::PTC24:
  4263. BX_WARN(false, "PTC24 decoder is not implemented.");
  4264. imageCheckerboard(_dst, _width, _height, 16, UINT32_C(0xff000000), UINT32_C(0xffffffff) );
  4265. break;
  4266. case TextureFormat::ATC:
  4267. for (uint32_t yy = 0; yy < height; ++yy)
  4268. {
  4269. for (uint32_t xx = 0; xx < width; ++xx)
  4270. {
  4271. decodeBlockATC(temp, src);
  4272. src += 8;
  4273. uint8_t* block = &dst[(yy*_dstPitch+xx*4)*4];
  4274. bx::memCopy(&block[0*_dstPitch], &temp[ 0], 16);
  4275. bx::memCopy(&block[1*_dstPitch], &temp[16], 16);
  4276. bx::memCopy(&block[2*_dstPitch], &temp[32], 16);
  4277. bx::memCopy(&block[3*_dstPitch], &temp[48], 16);
  4278. }
  4279. }
  4280. break;
  4281. case TextureFormat::ATCE:
  4282. for (uint32_t yy = 0; yy < height; ++yy)
  4283. {
  4284. for (uint32_t xx = 0; xx < width; ++xx)
  4285. {
  4286. decodeBlockDxt23A(temp+3, src);
  4287. src += 8;
  4288. decodeBlockATC(temp, src);
  4289. src += 8;
  4290. uint8_t* block = &dst[(yy*_dstPitch+xx*4)*4];
  4291. bx::memCopy(&block[0*_dstPitch], &temp[ 0], 16);
  4292. bx::memCopy(&block[1*_dstPitch], &temp[16], 16);
  4293. bx::memCopy(&block[2*_dstPitch], &temp[32], 16);
  4294. bx::memCopy(&block[3*_dstPitch], &temp[48], 16);
  4295. }
  4296. }
  4297. break;
  4298. case TextureFormat::ATCI:
  4299. for (uint32_t yy = 0; yy < height; ++yy)
  4300. {
  4301. for (uint32_t xx = 0; xx < width; ++xx)
  4302. {
  4303. decodeBlockDxt45A(temp+3, src);
  4304. src += 8;
  4305. decodeBlockATC(temp, src);
  4306. src += 8;
  4307. uint8_t* block = &dst[(yy*_dstPitch+xx*4)*4];
  4308. bx::memCopy(&block[0*_dstPitch], &temp[ 0], 16);
  4309. bx::memCopy(&block[1*_dstPitch], &temp[16], 16);
  4310. bx::memCopy(&block[2*_dstPitch], &temp[32], 16);
  4311. bx::memCopy(&block[3*_dstPitch], &temp[48], 16);
  4312. }
  4313. }
  4314. break;
  4315. case TextureFormat::ASTC4x4:
  4316. case TextureFormat::ASTC5x4:
  4317. case TextureFormat::ASTC5x5:
  4318. case TextureFormat::ASTC6x5:
  4319. case TextureFormat::ASTC6x6:
  4320. case TextureFormat::ASTC8x5:
  4321. case TextureFormat::ASTC8x6:
  4322. case TextureFormat::ASTC8x8:
  4323. case TextureFormat::ASTC10x5:
  4324. case TextureFormat::ASTC10x6:
  4325. case TextureFormat::ASTC10x8:
  4326. case TextureFormat::ASTC10x10:
  4327. case TextureFormat::ASTC12x10:
  4328. case TextureFormat::ASTC12x12:
  4329. imageDecodeToRgba8(_allocator, _dst, _src, _width, _height, _dstPitch, _srcFormat);
  4330. imageSwizzleBgra8(_dst, _dstPitch, _width, _height, _dst, _dstPitch);
  4331. break;
  4332. case TextureFormat::RGBA8:
  4333. {
  4334. const uint32_t srcPitch = _width * 4;
  4335. imageSwizzleBgra8(_dst, _dstPitch, _width, _height, _src, srcPitch);
  4336. }
  4337. break;
  4338. case TextureFormat::BGRA8:
  4339. {
  4340. const uint32_t srcPitch = _width * 4;
  4341. const uint32_t size = bx::uint32_min(srcPitch, _dstPitch);
  4342. bx::memCopy(_dst, _dstPitch, _src, srcPitch, size, _height);
  4343. }
  4344. break;
  4345. default:
  4346. {
  4347. const uint32_t srcBpp = s_imageBlockInfo[_srcFormat].bitsPerPixel;
  4348. const uint32_t srcPitch = _width * srcBpp / 8;
  4349. if (!imageConvert(_allocator, _dst, TextureFormat::BGRA8, _src, _srcFormat, _width, _height, 1, srcPitch, _dstPitch) )
  4350. {
  4351. // Failed to convert, just make ugly red-yellow checkerboard texture.
  4352. imageCheckerboard(_dst, _width, _height, 16, UINT32_C(0xffff0000), UINT32_C(0xffffff00) );
  4353. }
  4354. }
  4355. break;
  4356. }
  4357. }
  4358. void imageDecodeToRgba8(bx::AllocatorI* _allocator, void* _dst, const void* _src, uint32_t _width, uint32_t _height, uint32_t _dstPitch, TextureFormat::Enum _srcFormat)
  4359. {
  4360. switch (_srcFormat)
  4361. {
  4362. case TextureFormat::RGBA8:
  4363. {
  4364. const uint32_t srcPitch = _width * 4;
  4365. const uint32_t size = bx::uint32_min(srcPitch, _dstPitch);
  4366. bx::memCopy(_dst, _dstPitch, _src, srcPitch, size, _height);
  4367. }
  4368. break;
  4369. case TextureFormat::BGRA8:
  4370. {
  4371. const uint32_t srcPitch = _width * 4;
  4372. imageSwizzleBgra8(_dst, _dstPitch, _width, _height, _src, srcPitch);
  4373. }
  4374. break;
  4375. case TextureFormat::ASTC4x4:
  4376. case TextureFormat::ASTC5x4:
  4377. case TextureFormat::ASTC5x5:
  4378. case TextureFormat::ASTC6x5:
  4379. case TextureFormat::ASTC6x6:
  4380. case TextureFormat::ASTC8x5:
  4381. case TextureFormat::ASTC8x6:
  4382. case TextureFormat::ASTC8x8:
  4383. case TextureFormat::ASTC10x5:
  4384. case TextureFormat::ASTC10x6:
  4385. case TextureFormat::ASTC10x8:
  4386. case TextureFormat::ASTC10x10:
  4387. case TextureFormat::ASTC12x10:
  4388. case TextureFormat::ASTC12x12:
  4389. if (BX_ENABLED(BIMG_DECODE_ASTC) )
  4390. {
  4391. const bimg::ImageBlockInfo& astcBlockInfo = bimg::getBlockInfo(_srcFormat);
  4392. astcenc_config config{};
  4393. astcenc_error status = astcenc_config_init(
  4394. ASTCENC_PRF_LDR
  4395. , astcBlockInfo.blockWidth
  4396. , astcBlockInfo.blockHeight
  4397. , 1
  4398. , ASTCENC_PRE_MEDIUM
  4399. , ASTCENC_FLG_DECOMPRESS_ONLY
  4400. , &config
  4401. );
  4402. if (status != ASTCENC_SUCCESS)
  4403. {
  4404. BX_TRACE("astc error in config init %s", astcenc_get_error_string(status));
  4405. imageCheckerboard(_dst, _width, _height, 16, UINT32_C(0xff000000), UINT32_C(0xffffff00) );
  4406. break;
  4407. }
  4408. astcenc_context* context;
  4409. status = astcenc_context_alloc(&config, 1, &context);
  4410. if (status != ASTCENC_SUCCESS)
  4411. {
  4412. BX_TRACE("astc error in context alloc %s", astcenc_get_error_string(status));
  4413. imageCheckerboard(_dst, _width, _height, 16, UINT32_C(0xff000000), UINT32_C(0xffffff00) );
  4414. break;
  4415. }
  4416. //Put image data into an astcenc_image
  4417. astcenc_image image{};
  4418. image.dim_x = _width;
  4419. image.dim_y = _height;
  4420. image.dim_z = 1;
  4421. image.data_type = ASTCENC_TYPE_U8;
  4422. image.data = &_dst;
  4423. const uint32_t size = imageGetSize(NULL, uint16_t(_width), uint16_t(_height), 0, false, false, 1, _srcFormat);
  4424. static const astcenc_swizzle swizzle
  4425. { //0123/rgba swizzle corresponds to ASTC_RGBA
  4426. ASTCENC_SWZ_R,
  4427. ASTCENC_SWZ_G,
  4428. ASTCENC_SWZ_B,
  4429. ASTCENC_SWZ_A,
  4430. };
  4431. status = astcenc_decompress_image(
  4432. context
  4433. , (const uint8_t*)_src
  4434. , size
  4435. , &image
  4436. , &swizzle
  4437. , 0
  4438. );
  4439. if (status != ASTCENC_SUCCESS)
  4440. {
  4441. BX_TRACE("astc error in compress image %s", astcenc_get_error_string(status));
  4442. imageCheckerboard(_dst, _width, _height, 16, UINT32_C(0xff000000), UINT32_C(0xffffff00) );
  4443. astcenc_context_free(context);
  4444. break;
  4445. }
  4446. astcenc_context_free(context);
  4447. }
  4448. else
  4449. {
  4450. BX_WARN(false, "ASTC decoder is disabled (BIMG_DECODE_ASTC).");
  4451. imageCheckerboard(_dst, _width, _height, 16, UINT32_C(0xff000000), UINT32_C(0xff00ff00) );
  4452. }
  4453. break;
  4454. default:
  4455. {
  4456. const uint32_t srcPitch = _width * 4;
  4457. imageDecodeToBgra8(_allocator, _dst, _src, _width, _height, _dstPitch, _srcFormat);
  4458. imageSwizzleBgra8(_dst, _dstPitch, _width, _height, _dst, srcPitch);
  4459. }
  4460. break;
  4461. }
  4462. }
  4463. void imageRgba8ToRgba32fRef(void* _dst, uint32_t _width, uint32_t _height, uint32_t _srcPitch, const void* _src)
  4464. {
  4465. const uint32_t dstWidth = _width;
  4466. const uint32_t dstHeight = _height;
  4467. if (0 == dstWidth
  4468. || 0 == dstHeight)
  4469. {
  4470. return;
  4471. }
  4472. float* dst = (float*)_dst;
  4473. const uint8_t* src = (const uint8_t*)_src;
  4474. for (uint32_t yy = 0, ystep = _srcPitch; yy < dstHeight; ++yy, src += ystep)
  4475. {
  4476. const uint8_t* rgba = src;
  4477. for (uint32_t xx = 0; xx < dstWidth; ++xx, rgba += 4, dst += 4)
  4478. {
  4479. dst[0] = bx::toLinear(rgba[0]);
  4480. dst[1] = bx::toLinear(rgba[1]);
  4481. dst[2] = bx::toLinear(rgba[2]);
  4482. dst[3] = rgba[3];
  4483. }
  4484. }
  4485. }
  4486. void imageRgba8ToRgba32f(void* _dst, uint32_t _width, uint32_t _height, uint32_t _srcPitch, const void* _src)
  4487. {
  4488. const uint32_t dstWidth = _width;
  4489. const uint32_t dstHeight = _height;
  4490. if (0 == dstWidth
  4491. || 0 == dstHeight)
  4492. {
  4493. return;
  4494. }
  4495. float* dst = (float*)_dst;
  4496. const uint8_t* src = (const uint8_t*)_src;
  4497. using namespace bx;
  4498. 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);
  4499. const simd128_t umask = simd_ild(0xff, 0xff00, 0xff0000, 0xff000000);
  4500. const simd128_t wflip = simd_ild(0, 0, 0, 0x80000000);
  4501. const simd128_t wadd = simd_ld(0.0f, 0.0f, 0.0f, 32768.0f*65536.0f);
  4502. for (uint32_t yy = 0, ystep = _srcPitch; yy < dstHeight; ++yy, src += ystep)
  4503. {
  4504. const uint8_t* rgba = src;
  4505. for (uint32_t xx = 0; xx < dstWidth; ++xx, rgba += 4, dst += 4)
  4506. {
  4507. const simd128_t abgr0 = simd_splat(rgba);
  4508. const simd128_t abgr0m = simd_and(abgr0, umask);
  4509. const simd128_t abgr0x = simd_xor(abgr0m, wflip);
  4510. const simd128_t abgr0f = simd_itof(abgr0x);
  4511. const simd128_t abgr0c = simd_add(abgr0f, wadd);
  4512. const simd128_t abgr0n = simd_mul(abgr0c, unpack);
  4513. simd_st(dst, abgr0n);
  4514. }
  4515. }
  4516. }
  4517. 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)
  4518. {
  4519. const uint8_t* src = (const uint8_t*)_src;
  4520. uint8_t* dst = (uint8_t*)_dst;
  4521. const uint32_t srcBpp = s_imageBlockInfo[_srcFormat].bitsPerPixel;
  4522. const uint32_t srcPitch = _width*srcBpp/8;
  4523. for (uint32_t zz = 0; zz < _depth; ++zz, src += _height*srcPitch, dst += _height*_dstPitch)
  4524. {
  4525. switch (_srcFormat)
  4526. {
  4527. case TextureFormat::BC5:
  4528. {
  4529. uint32_t width = _width/4;
  4530. uint32_t height = _height/4;
  4531. const uint8_t* srcData = src;
  4532. for (uint32_t yy = 0; yy < height; ++yy)
  4533. {
  4534. for (uint32_t xx = 0; xx < width; ++xx)
  4535. {
  4536. uint8_t temp[16*4];
  4537. decodeBlockDxt45A(temp+2, srcData);
  4538. srcData += 8;
  4539. decodeBlockDxt45A(temp+1, srcData);
  4540. srcData += 8;
  4541. for (uint32_t ii = 0; ii < 16; ++ii)
  4542. {
  4543. float nx = temp[ii*4+2]*2.0f/255.0f - 1.0f;
  4544. float ny = temp[ii*4+1]*2.0f/255.0f - 1.0f;
  4545. float nz = bx::sqrt(1.0f - nx*nx - ny*ny);
  4546. const uint32_t offset = (yy*4 + ii/4)*_width*16 + (xx*4 + ii%4)*16;
  4547. float* block = (float*)&dst[offset];
  4548. block[0] = nx;
  4549. block[1] = ny;
  4550. block[2] = nz;
  4551. block[3] = 0.0f;
  4552. }
  4553. }
  4554. }
  4555. }
  4556. break;
  4557. case TextureFormat::BC6H:
  4558. {
  4559. uint32_t width = _width/4;
  4560. uint32_t height = _height/4;
  4561. const uint8_t* srcData = src;
  4562. for (uint32_t yy = 0; yy < height; ++yy)
  4563. {
  4564. for (uint32_t xx = 0; xx < width; ++xx)
  4565. {
  4566. float tmp[16*4];
  4567. decodeBlockBc6h(tmp, srcData);
  4568. srcData += 16;
  4569. uint8_t* block = (uint8_t*)&dst[yy*_dstPitch*4 + xx*64];
  4570. bx::memCopy(&block[0*_dstPitch], &tmp[ 0], 64);
  4571. bx::memCopy(&block[1*_dstPitch], &tmp[16], 64);
  4572. bx::memCopy(&block[2*_dstPitch], &tmp[32], 64);
  4573. bx::memCopy(&block[3*_dstPitch], &tmp[48], 64);
  4574. }
  4575. }
  4576. }
  4577. break;
  4578. case TextureFormat::RGBA32F:
  4579. bx::memCopy(dst, src, _dstPitch*_height);
  4580. break;
  4581. default:
  4582. if (isCompressed(_srcFormat) )
  4583. {
  4584. uint32_t size = imageGetSize(NULL, uint16_t(_width), uint16_t(_height), 0, false, false, 1, TextureFormat::RGBA8);
  4585. void* temp = bx::alloc(_allocator, size);
  4586. imageDecodeToRgba8(_allocator, temp, src, _width, _height, _width*4, _srcFormat);
  4587. imageRgba8ToRgba32f(dst, _width, _height, _width*4, temp);
  4588. bx::free(_allocator, temp);
  4589. }
  4590. else
  4591. {
  4592. imageConvert(_allocator, dst, TextureFormat::RGBA32F, src, _srcFormat, _width, _height, 1, srcPitch, _dstPitch);
  4593. }
  4594. break;
  4595. }
  4596. }
  4597. }
  4598. bool imageGetRawData(const ImageContainer& _imageContainer, uint16_t _side, uint8_t _lod, const void* _data, uint32_t _size, ImageMip& _mip)
  4599. {
  4600. uint32_t offset = _imageContainer.m_offset;
  4601. TextureFormat::Enum format = TextureFormat::Enum(_imageContainer.m_format);
  4602. bool hasAlpha = _imageContainer.m_hasAlpha;
  4603. const ImageBlockInfo& blockInfo = s_imageBlockInfo[format];
  4604. const uint8_t bpp = blockInfo.bitsPerPixel;
  4605. const uint32_t blockSize = blockInfo.blockSize;
  4606. const uint32_t blockWidth = blockInfo.blockWidth;
  4607. const uint32_t blockHeight = blockInfo.blockHeight;
  4608. const uint32_t minBlockX = blockInfo.minBlockX;
  4609. const uint32_t minBlockY = blockInfo.minBlockY;
  4610. if (UINT32_MAX == _imageContainer.m_offset)
  4611. {
  4612. if (NULL == _imageContainer.m_data)
  4613. {
  4614. return false;
  4615. }
  4616. offset = 0;
  4617. _data = _imageContainer.m_data;
  4618. _size = _imageContainer.m_size;
  4619. }
  4620. const uint8_t* data = (const uint8_t*)_data;
  4621. const uint16_t numSides = _imageContainer.m_numLayers * (_imageContainer.m_cubeMap ? 6 : 1);
  4622. if (_imageContainer.m_ktx || _imageContainer.m_pvr3)
  4623. {
  4624. uint32_t width = _imageContainer.m_width;
  4625. uint32_t height = _imageContainer.m_height;
  4626. uint32_t depth = _imageContainer.m_depth;
  4627. for (uint8_t lod = 0, num = _imageContainer.m_numMips; lod < num; ++lod)
  4628. {
  4629. width = bx::max<uint32_t>(blockWidth * minBlockX, ( (width + blockWidth - 1) / blockWidth )*blockWidth);
  4630. height = bx::max<uint32_t>(blockHeight * minBlockY, ( (height + blockHeight - 1) / blockHeight)*blockHeight);
  4631. depth = bx::max<uint32_t>(1, depth);
  4632. const uint32_t mipSize = width/blockWidth * height/blockHeight * depth * blockSize;
  4633. if (_imageContainer.m_ktx)
  4634. {
  4635. const uint32_t size = _imageContainer.m_numLayers == 1 && _imageContainer.m_cubeMap ? mipSize : mipSize * numSides;
  4636. uint32_t imageSize = bx::toHostEndian(*(const uint32_t*)&data[offset], _imageContainer.m_ktxLE);
  4637. BX_ASSERT(size == imageSize, "KTX: Image size mismatch %d (expected %d).", size, imageSize);
  4638. BX_UNUSED(size, imageSize);
  4639. offset += sizeof(uint32_t);
  4640. }
  4641. for (uint16_t side = 0; side < numSides; ++side)
  4642. {
  4643. BX_ASSERT(offset <= _size, "Reading past size of data buffer! (offset %d, size %d)", offset, _size);
  4644. if (side == _side
  4645. && lod == _lod)
  4646. {
  4647. _mip.m_width = width;
  4648. _mip.m_height = height;
  4649. _mip.m_depth = depth;
  4650. _mip.m_blockSize = blockSize;
  4651. _mip.m_size = mipSize;
  4652. _mip.m_data = &data[offset];
  4653. _mip.m_bpp = bpp;
  4654. _mip.m_format = format;
  4655. _mip.m_hasAlpha = hasAlpha;
  4656. return true;
  4657. }
  4658. offset += mipSize;
  4659. BX_UNUSED(_size);
  4660. }
  4661. width >>= 1;
  4662. height >>= 1;
  4663. depth >>= 1;
  4664. }
  4665. }
  4666. else
  4667. {
  4668. for (uint16_t side = 0; side < numSides; ++side)
  4669. {
  4670. uint32_t width = _imageContainer.m_width;
  4671. uint32_t height = _imageContainer.m_height;
  4672. uint32_t depth = _imageContainer.m_depth;
  4673. for (uint8_t lod = 0, num = _imageContainer.m_numMips; lod < num; ++lod)
  4674. {
  4675. BX_ASSERT(offset <= _size, "Reading past size of data buffer! (offset %d, size %d)", offset, _size);
  4676. width = bx::max<uint32_t>(blockWidth * minBlockX, ( (width + blockWidth - 1) / blockWidth )*blockWidth);
  4677. height = bx::max<uint32_t>(blockHeight * minBlockY, ( (height + blockHeight - 1) / blockHeight)*blockHeight);
  4678. depth = bx::max<uint32_t>(1, depth);
  4679. uint32_t mipSize = width/blockWidth * height/blockHeight * depth * blockSize;
  4680. if (side == _side
  4681. && lod == _lod)
  4682. {
  4683. _mip.m_width = width;
  4684. _mip.m_height = height;
  4685. _mip.m_depth = depth;
  4686. _mip.m_blockSize = blockSize;
  4687. _mip.m_size = mipSize;
  4688. _mip.m_data = &data[offset];
  4689. _mip.m_bpp = bpp;
  4690. _mip.m_format = format;
  4691. _mip.m_hasAlpha = hasAlpha;
  4692. return true;
  4693. }
  4694. offset += mipSize;
  4695. BX_UNUSED(_size);
  4696. width >>= 1;
  4697. height >>= 1;
  4698. depth >>= 1;
  4699. }
  4700. }
  4701. }
  4702. return false;
  4703. }
  4704. 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)
  4705. {
  4706. BX_ERROR_SCOPE(_err);
  4707. uint8_t type = _grayscale ? 3 : 2;
  4708. uint8_t bpp = _grayscale ? 8 : 32;
  4709. uint8_t header[18] = {};
  4710. header[ 2] = type;
  4711. header[12] = _width &0xff;
  4712. header[13] = (_width >>8)&0xff;
  4713. header[14] = _height &0xff;
  4714. header[15] = (_height>>8)&0xff;
  4715. header[16] = bpp;
  4716. header[17] = 32;
  4717. int32_t total = 0;
  4718. total += bx::write(_writer, header, sizeof(header), _err);
  4719. uint32_t dstPitch = _width*bpp/8;
  4720. if (_yflip)
  4721. {
  4722. const uint8_t* data = (const uint8_t*)_src + _srcPitch*_height - _srcPitch;
  4723. for (uint32_t yy = 0; yy < _height && _err->isOk(); ++yy)
  4724. {
  4725. total += bx::write(_writer, data, dstPitch, _err);
  4726. data -= _srcPitch;
  4727. }
  4728. }
  4729. else if (_srcPitch == dstPitch)
  4730. {
  4731. total += bx::write(_writer, _src, _height*_srcPitch, _err);
  4732. }
  4733. else
  4734. {
  4735. const uint8_t* data = (const uint8_t*)_src;
  4736. for (uint32_t yy = 0; yy < _height && _err->isOk(); ++yy)
  4737. {
  4738. total += bx::write(_writer, data, dstPitch, _err);
  4739. data += _srcPitch;
  4740. }
  4741. }
  4742. return total;
  4743. }
  4744. template<typename Ty>
  4745. class HashWriter : public bx::WriterI
  4746. {
  4747. public:
  4748. HashWriter(bx::WriterI* _writer)
  4749. : m_writer(_writer)
  4750. {
  4751. begin();
  4752. }
  4753. void begin()
  4754. {
  4755. m_hash.begin();
  4756. }
  4757. uint32_t end()
  4758. {
  4759. return m_hash.end();
  4760. }
  4761. virtual int32_t write(const void* _data, int32_t _size, bx::Error* _err) override
  4762. {
  4763. m_hash.add(_data, _size);
  4764. return m_writer->write(_data, _size, _err);
  4765. }
  4766. private:
  4767. Ty m_hash;
  4768. bx::WriterI* m_writer;
  4769. };
  4770. 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)
  4771. {
  4772. BX_ERROR_SCOPE(_err);
  4773. switch (_format)
  4774. {
  4775. case TextureFormat::R8:
  4776. case TextureFormat::RGBA8:
  4777. case TextureFormat::BGRA8:
  4778. break;
  4779. default:
  4780. BX_ERROR_SET(_err, BIMG_ERROR, "PNG: Unsupported texture format.");
  4781. return 0;
  4782. }
  4783. const bool grayscale = TextureFormat::R8 == _format;
  4784. const bool bgra = TextureFormat::BGRA8 == _format;
  4785. int32_t total = 0;
  4786. total += bx::write(_writer, "\x89PNG\r\n\x1a\n", _err);
  4787. total += bx::write(_writer, bx::toBigEndian<uint32_t>(13), _err);
  4788. HashWriter<bx::HashCrc32> writerC(_writer);
  4789. total += bx::write(&writerC, "IHDR", _err);
  4790. total += bx::write(&writerC, bx::toBigEndian(_width), _err);
  4791. total += bx::write(&writerC, bx::toBigEndian(_height), _err);
  4792. total += bx::write(&writerC, "\x08\x06", _err);
  4793. total += bx::writeRep(&writerC, 0, 3, _err);
  4794. total += bx::write(_writer, bx::toBigEndian(writerC.end() ), _err);
  4795. const uint32_t bpp = grayscale ? 8 : 32;
  4796. const uint32_t stride = _width*bpp/8;
  4797. const uint16_t zlen = bx::toLittleEndian<uint16_t>(uint16_t(stride + 1) );
  4798. const uint16_t zlenC = bx::toLittleEndian<uint16_t>(~zlen);
  4799. total += bx::write(_writer, bx::toBigEndian<uint32_t>(_height*(stride+6)+6), _err);
  4800. writerC.begin();
  4801. total += bx::write(&writerC, "IDAT", _err);
  4802. total += bx::write(&writerC, "\x78\x9c", _err);
  4803. const uint8_t* data = (const uint8_t*)_src;
  4804. int32_t step = int32_t(_srcPitch);
  4805. if (_yflip)
  4806. {
  4807. data += _srcPitch*_height - _srcPitch;
  4808. step = -step;
  4809. }
  4810. HashWriter<bx::HashAdler32> writerA(&writerC);
  4811. for (uint32_t ii = 0; ii < _height && _err->isOk(); ++ii)
  4812. {
  4813. total += bx::write(&writerC, uint8_t(ii == _height-1 ? 1 : 0), _err);
  4814. total += bx::write(&writerC, zlen, _err);
  4815. total += bx::write(&writerC, zlenC, _err);
  4816. total += bx::write(&writerA, uint8_t(0), _err);
  4817. if (bgra)
  4818. {
  4819. for (uint32_t xx = 0; xx < _width; ++xx)
  4820. {
  4821. const uint8_t* texel = &data[xx*4];
  4822. const uint8_t bb = texel[0];
  4823. const uint8_t gg = texel[1];
  4824. const uint8_t rr = texel[2];
  4825. const uint8_t aa = texel[3];
  4826. total += bx::write(&writerA, rr, _err);
  4827. total += bx::write(&writerA, gg, _err);
  4828. total += bx::write(&writerA, bb, _err);
  4829. total += bx::write(&writerA, aa, _err);
  4830. }
  4831. }
  4832. else
  4833. {
  4834. total += bx::write(&writerA, data, stride, _err);
  4835. }
  4836. data += step;
  4837. }
  4838. total += bx::write(&writerC, bx::toBigEndian(writerA.end() ), _err);
  4839. total += bx::write(_writer, bx::toBigEndian(writerC.end() ), _err);
  4840. total += bx::write(&writerC, uint32_t(0), _err);
  4841. writerC.begin();
  4842. total += bx::write(&writerC, "IEND", _err);
  4843. total += bx::write(_writer, bx::toBigEndian(writerC.end() ), _err);
  4844. return total;
  4845. }
  4846. 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)
  4847. {
  4848. BX_ERROR_SCOPE(_err);
  4849. const uint32_t bpp = getBitsPerPixel(_format);
  4850. uint32_t bytesPerChannel = 0;
  4851. switch (_format)
  4852. {
  4853. case TextureFormat::RGBA16F:
  4854. bytesPerChannel = 2;
  4855. break;
  4856. default:
  4857. BX_ERROR_SET(_err, BIMG_ERROR, "EXR: Unsupported texture format.");
  4858. return 0;
  4859. }
  4860. int32_t total = 0;
  4861. total += bx::write(_writer, "v/1\x01", _err);
  4862. total += bx::writeLE(_writer, uint32_t(2), _err);
  4863. total += bx::write(_writer, "channels", _err);
  4864. total += bx::write(_writer, '\0', _err);
  4865. total += bx::write(_writer, "chlist", _err);
  4866. total += bx::write(_writer, '\0', _err);
  4867. total += bx::writeLE(_writer, uint32_t(18*4+1), _err);
  4868. const uint8_t cdata[] = { 0, 1, 0, 0, 0, 0, 0, 0, 0, 1, 0, 0, 0, 1, 0, 0, 0 };
  4869. // Order is always ABGR order because Photoshop and GIMP ignore these fields and
  4870. // assume it's in ABGR order.
  4871. total += bx::write(_writer, 'A', _err);
  4872. total += bx::write(_writer, cdata, BX_COUNTOF(cdata), _err);
  4873. total += bx::write(_writer, 'B', _err);
  4874. total += bx::write(_writer, cdata, BX_COUNTOF(cdata), _err);
  4875. total += bx::write(_writer, 'G', _err);
  4876. total += bx::write(_writer, cdata, BX_COUNTOF(cdata), _err);
  4877. total += bx::write(_writer, 'R', _err);
  4878. total += bx::write(_writer, cdata, BX_COUNTOF(cdata), _err);
  4879. total += bx::write(_writer, '\0', _err);
  4880. total += bx::write(_writer, "compression", _err);
  4881. total += bx::write(_writer, '\0', _err);
  4882. total += bx::write(_writer, "compression", _err);
  4883. total += bx::write(_writer, '\0', _err);
  4884. total += bx::writeLE(_writer, uint32_t(1), _err);
  4885. total += bx::write(_writer, '\0', _err); // no compression
  4886. total += bx::write(_writer, "dataWindow", _err);
  4887. total += bx::write(_writer, '\0', _err);
  4888. total += bx::write(_writer, "box2i", _err);
  4889. total += bx::write(_writer, '\0', _err);
  4890. total += bx::writeLE(_writer, uint32_t(16), _err);
  4891. total += bx::writeRep(_writer, '\0', 8, _err);
  4892. total += bx::writeLE(_writer, _width-1, _err);
  4893. total += bx::writeLE(_writer, _height-1, _err);
  4894. total += bx::write(_writer, "displayWindow", _err);
  4895. total += bx::write(_writer, '\0', _err);
  4896. total += bx::write(_writer, "box2i", _err);
  4897. total += bx::write(_writer, '\0', _err);
  4898. total += bx::writeLE(_writer, uint32_t(16), _err);
  4899. total += bx::writeRep(_writer, '\0', 8, _err);
  4900. total += bx::writeLE(_writer, _width-1, _err);
  4901. total += bx::writeLE(_writer, _height-1, _err);
  4902. total += bx::write(_writer, "lineOrder", _err);
  4903. total += bx::write(_writer, '\0', _err);
  4904. total += bx::write(_writer, "lineOrder", _err);
  4905. total += bx::write(_writer, '\0', _err);
  4906. total += bx::writeLE(_writer, uint32_t(1), _err);
  4907. total += bx::write(_writer, _yflip, _err);
  4908. total += bx::write(_writer, "pixelAspectRatio", _err);
  4909. total += bx::write(_writer, '\0', _err);
  4910. total += bx::write(_writer, "float", _err);
  4911. total += bx::write(_writer, '\0', _err);
  4912. total += bx::writeLE(_writer, uint32_t(4), _err);
  4913. total += bx::writeLE(_writer, 1.0f, _err);
  4914. total += bx::write(_writer, "screenWindowCenter", _err);
  4915. total += bx::write(_writer, '\0', _err);
  4916. total += bx::write(_writer, "v2f", _err);
  4917. total += bx::write(_writer, '\0', _err);
  4918. total += bx::writeLE(_writer, uint32_t(8), _err);
  4919. total += bx::writeRep(_writer, '\0', 8, _err);
  4920. total += bx::write(_writer, "screenWindowWidth", _err);
  4921. total += bx::write(_writer, '\0', _err);
  4922. total += bx::write(_writer, "float", _err);
  4923. total += bx::write(_writer, '\0', _err);
  4924. total += bx::writeLE(_writer, uint32_t(4), _err);
  4925. total += bx::writeLE(_writer, 1.0f, _err);
  4926. total += bx::write(_writer, '\0', _err);
  4927. const uint32_t exrStride = _width*bpp/8;
  4928. uint64_t offset = 0;
  4929. for (uint32_t yy = 0; yy < _height && _err->isOk(); ++yy)
  4930. {
  4931. total += bx::writeLE(_writer, (offset), _err);
  4932. offset += exrStride + 8 /* offset */;
  4933. }
  4934. const uint8_t* data = (const uint8_t*)_src;
  4935. for (uint32_t yy = 0; yy < _height && _err->isOk(); ++yy)
  4936. {
  4937. total += bx::writeLE(_writer, yy, _err);
  4938. total += bx::writeLE(_writer, exrStride, _err);
  4939. for (uint32_t xx = 0; xx < _width && _err->isOk(); ++xx)
  4940. {
  4941. total += bx::write(_writer, &data[xx*bpp/8+3*bytesPerChannel], bytesPerChannel, _err);
  4942. }
  4943. for (uint32_t xx = 0; xx < _width && _err->isOk(); ++xx)
  4944. {
  4945. total += bx::write(_writer, &data[xx*bpp/8+2*bytesPerChannel], bytesPerChannel, _err);
  4946. }
  4947. for (uint32_t xx = 0; xx < _width && _err->isOk(); ++xx)
  4948. {
  4949. total += bx::write(_writer, &data[xx*bpp/8+1*bytesPerChannel], bytesPerChannel, _err);
  4950. }
  4951. for (uint32_t xx = 0; xx < _width && _err->isOk(); ++xx)
  4952. {
  4953. total += bx::write(_writer, &data[xx*bpp/8+0*bytesPerChannel], bytesPerChannel, _err);
  4954. }
  4955. data += _srcPitch;
  4956. }
  4957. return total;
  4958. }
  4959. 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)
  4960. {
  4961. BX_ERROR_SCOPE(_err);
  4962. int32_t total = 0;
  4963. total += bx::write(_writer, "#?RADIANCE\n" , _err);
  4964. total += bx::write(_writer, "FORMAT=32-bit_rle_rgbe\n" , _err);
  4965. total += bx::write(_writer, '\n' , _err);
  4966. total += bx::write(_writer, _err, "%cY %d +X %d\n", _yflip ? '+' : '-', _height, _width);
  4967. UnpackFn unpack = getUnpack(_format);
  4968. const uint32_t bpp = getBitsPerPixel(_format);
  4969. const uint8_t* data = (const uint8_t*)_src;
  4970. for (uint32_t yy = 0; yy < _height && _err->isOk(); ++yy)
  4971. {
  4972. for (uint32_t xx = 0; xx < _width && _err->isOk(); ++xx)
  4973. {
  4974. float rgba[4];
  4975. unpack(rgba, &data[xx*bpp/8]);
  4976. const float maxVal = bx::max(rgba[0], rgba[1], rgba[2]);
  4977. const float exp = bx::ceil(bx::log2(maxVal) );
  4978. const float toRgb8 = 255.0f * 1.0f/bx::ldexp(1.0f, int(exp) );
  4979. uint8_t rgbe[4];
  4980. rgbe[0] = uint8_t(rgba[0] * toRgb8);
  4981. rgbe[1] = uint8_t(rgba[1] * toRgb8);
  4982. rgbe[2] = uint8_t(rgba[2] * toRgb8);
  4983. rgbe[3] = uint8_t(exp+128.0f);
  4984. total += bx::write(_writer, rgbe, 4, _err);
  4985. }
  4986. data += _srcPitch;
  4987. }
  4988. return total;
  4989. }
  4990. 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)
  4991. {
  4992. BX_ERROR_SCOPE(_err);
  4993. uint32_t ddspf = UINT32_MAX;
  4994. uint32_t dxgiFormat = UINT32_MAX;
  4995. uint32_t fourccFormat = UINT32_MAX;
  4996. for (uint32_t ii = 0; ii < BX_COUNTOF(s_translateDdsPixelFormat); ++ii)
  4997. {
  4998. if (s_translateDdsPixelFormat[ii].m_textureFormat == _format)
  4999. {
  5000. ddspf = ii;
  5001. break;
  5002. }
  5003. }
  5004. if (UINT32_MAX == ddspf)
  5005. {
  5006. for (uint32_t ii = 0; ii < BX_COUNTOF(s_translateDxgiFormat); ++ii)
  5007. {
  5008. if (s_translateDxgiFormat[ii].m_textureFormat == _format)
  5009. {
  5010. dxgiFormat = s_translateDxgiFormat[ii].m_format;
  5011. break;
  5012. }
  5013. }
  5014. }
  5015. if (UINT32_MAX == ddspf && UINT32_MAX == dxgiFormat)
  5016. {
  5017. for (uint32_t ii = 0; ii < BX_COUNTOF(s_translateDdsFourccFormat); ++ii)
  5018. {
  5019. if (s_translateDdsFourccFormat[ii].m_textureFormat == _format)
  5020. {
  5021. fourccFormat = s_translateDdsFourccFormat[ii].m_format;
  5022. break;
  5023. }
  5024. }
  5025. }
  5026. if (UINT32_MAX == ddspf
  5027. && UINT32_MAX == dxgiFormat
  5028. && UINT32_MAX == fourccFormat)
  5029. {
  5030. BX_ERROR_SET(_err, BIMG_ERROR, "DDS: output format not supported.");
  5031. return 0;
  5032. }
  5033. const uint32_t bpp = getBitsPerPixel(_format);
  5034. uint32_t total = 0;
  5035. total += bx::write(_writer, uint32_t(DDS_MAGIC), _err);
  5036. uint32_t headerStart = total;
  5037. total += bx::write(_writer, uint32_t(DDS_HEADER_SIZE), _err);
  5038. total += bx::write(_writer, uint32_t(0
  5039. | DDSD_HEIGHT
  5040. | DDSD_WIDTH
  5041. | DDSD_PIXELFORMAT
  5042. | DDSD_CAPS
  5043. | (1 < _depth ? DDSD_DEPTH : 0)
  5044. | (1 < _numMips ? DDSD_MIPMAPCOUNT : 0)
  5045. | (isCompressed(_format) ? DDSD_LINEARSIZE : DDSD_PITCH)
  5046. )
  5047. , _err
  5048. );
  5049. const uint32_t pitchOrLinearSize = isCompressed(_format)
  5050. ? _width*_height*bpp/8
  5051. : _width*bpp/8
  5052. ;
  5053. total += bx::write(_writer, _height, _err);
  5054. total += bx::write(_writer, _width, _err);
  5055. total += bx::write(_writer, pitchOrLinearSize, _err);
  5056. total += bx::write(_writer, _depth, _err);
  5057. total += bx::write(_writer, uint32_t(_numMips), _err);
  5058. total += bx::writeRep(_writer, 0, 44, _err); // reserved1
  5059. if (UINT32_MAX != ddspf)
  5060. {
  5061. const TranslateDdsPixelFormat& pf = s_translateDdsPixelFormat[ddspf];
  5062. total += bx::write(_writer, uint32_t(8*sizeof(uint32_t) ), _err); // pixelFormatSize
  5063. total += bx::write(_writer, pf.m_flags, _err);
  5064. total += bx::write(_writer, uint32_t(0), _err);
  5065. total += bx::write(_writer, pf.m_bitCount, _err);
  5066. total += bx::write(_writer, pf.m_bitmask, _err);
  5067. }
  5068. else
  5069. {
  5070. total += bx::write(_writer, uint32_t(8*sizeof(uint32_t) ), _err); // pixelFormatSize
  5071. total += bx::write(_writer, uint32_t(DDPF_FOURCC), _err);
  5072. if (UINT32_MAX != fourccFormat)
  5073. {
  5074. total += bx::write(_writer, fourccFormat, _err);
  5075. }
  5076. else
  5077. {
  5078. total += bx::write(_writer, uint32_t(DDS_DX10), _err);
  5079. }
  5080. total += bx::write(_writer, uint32_t(0), _err); // bitCount
  5081. total += bx::writeRep(_writer, 0, 4*sizeof(uint32_t), _err); // bitmask
  5082. }
  5083. uint32_t caps[4] =
  5084. {
  5085. uint32_t(DDSCAPS_TEXTURE | (1 < _numMips ? DDSCAPS_COMPLEX|DDSCAPS_MIPMAP : 0) ),
  5086. uint32_t(_cubeMap ? DDSCAPS2_CUBEMAP|DSCAPS2_CUBEMAP_ALLSIDES : 0),
  5087. 0,
  5088. 0,
  5089. };
  5090. total += bx::write(_writer, caps, sizeof(caps), _err);
  5091. total += bx::writeRep(_writer, 0, 4, _err); // reserved2
  5092. BX_WARN(total-headerStart == DDS_HEADER_SIZE
  5093. , "DDS: Failed to write header size %d (expected: %d)."
  5094. , total-headerStart
  5095. , DDS_HEADER_SIZE
  5096. );
  5097. if (UINT32_MAX != dxgiFormat)
  5098. {
  5099. total += bx::write(_writer, dxgiFormat, _err);
  5100. total += bx::write(_writer, uint32_t(1 < _depth ? DDS_DX10_DIMENSION_TEXTURE3D : DDS_DX10_DIMENSION_TEXTURE2D), _err); // dims
  5101. total += bx::write(_writer, uint32_t(_cubeMap ? DDS_DX10_MISC_TEXTURECUBE : 0), _err); // miscFlags
  5102. total += bx::write(_writer, uint32_t(_numLayers), _err); // arraySize
  5103. total += bx::write(_writer, uint32_t(0), _err); // miscFlags2
  5104. BX_WARN(total-headerStart == DDS_HEADER_SIZE+20
  5105. , "DDS: Failed to write header size %d (expected: %d)."
  5106. , total-headerStart
  5107. , DDS_HEADER_SIZE+20
  5108. );
  5109. BX_UNUSED(headerStart);
  5110. }
  5111. return total;
  5112. }
  5113. int32_t imageWriteDds(bx::WriterI* _writer, ImageContainer& _imageContainer, const void* _data, uint32_t _size, bx::Error* _err)
  5114. {
  5115. BX_ERROR_SCOPE(_err);
  5116. int32_t total = 0;
  5117. total += imageWriteDdsHeader(_writer
  5118. , TextureFormat::Enum(_imageContainer.m_format)
  5119. , _imageContainer.m_cubeMap
  5120. , _imageContainer.m_width
  5121. , _imageContainer.m_height
  5122. , _imageContainer.m_depth
  5123. , _imageContainer.m_numMips
  5124. , _imageContainer.m_numLayers
  5125. , _err
  5126. );
  5127. if (!_err->isOk() )
  5128. {
  5129. return total;
  5130. }
  5131. for (uint8_t side = 0, numSides = uint8_t(_imageContainer.m_numLayers * (_imageContainer.m_cubeMap ? 6 : 1) ); side < numSides && _err->isOk(); ++side)
  5132. {
  5133. for (uint8_t lod = 0, num = _imageContainer.m_numMips; lod < num && _err->isOk(); ++lod)
  5134. {
  5135. ImageMip mip;
  5136. if (imageGetRawData(_imageContainer, side, lod, _data, _size, mip) )
  5137. {
  5138. total += bx::write(_writer, mip.m_data, mip.m_size, _err);
  5139. }
  5140. }
  5141. }
  5142. return total;
  5143. }
  5144. 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)
  5145. {
  5146. BX_ERROR_SCOPE(_err);
  5147. const KtxFormatInfo& tfi = s_translateKtxFormat[_format];
  5148. uint32_t internalFmt = tfi.m_internalFmt;
  5149. if (_srgb && tfi.m_internalFmtSrgb != KTX_ZERO) {
  5150. internalFmt = tfi.m_internalFmtSrgb;
  5151. }
  5152. int32_t total = 0;
  5153. total += bx::write(_writer, "\xabKTX 11\xbb\r\n\x1a\n", 12, _err);
  5154. total += bx::write(_writer, uint32_t(0x04030201), _err);
  5155. total += bx::write(_writer, uint32_t(0), _err); // glType; For compressed textures, glType must equal 0
  5156. total += bx::write(_writer, uint32_t(1), _err); // glTypeSize; For texture data which does not depend on platform endianness, including compressed texture data, glTypeSize must equal 1.
  5157. total += bx::write(_writer, uint32_t(0), _err); // glFormat; For compressed textures, glFormat must equal 0
  5158. total += bx::write(_writer, internalFmt, _err); // glInternalFormat
  5159. total += bx::write(_writer, tfi.m_fmt, _err); // glBaseInternalFormat
  5160. total += bx::write(_writer, _width, _err);
  5161. total += bx::write(_writer, _height, _err);
  5162. total += bx::write(_writer, _depth > 1 ? _depth : uint32_t(0), _err); // For 2D and cube textures pixelDepth must be 0.
  5163. total += bx::write(_writer, _numLayers > 1 ? _numLayers : uint32_t(0), _err); // numberOfArrayElements; If the texture is not an array texture, numberOfArrayElements must equal 0.
  5164. total += bx::write(_writer, _cubeMap ? uint32_t(6) : uint32_t(1), _err); // numberOfFaces; For cubemaps and cubemap arrays this should be 6. For non cubemaps this should be 1
  5165. total += bx::write(_writer, uint32_t(_numMips), _err);
  5166. total += bx::write(_writer, uint32_t(0), _err); // Meta-data size.
  5167. BX_WARN(total == 64, "KTX: Failed to write header size %d (expected: %d).", total, 64);
  5168. return total;
  5169. }
  5170. 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)
  5171. {
  5172. BX_ERROR_SCOPE(_err);
  5173. int32_t total = 0;
  5174. total += imageWriteKtxHeader(_writer, _format, _cubeMap, _width, _height, _depth, _numMips, _numLayers, _srgb, _err);
  5175. if (!_err->isOk() )
  5176. {
  5177. return total;
  5178. }
  5179. const ImageBlockInfo& blockInfo = s_imageBlockInfo[_format];
  5180. const uint32_t blockWidth = blockInfo.blockWidth;
  5181. const uint32_t blockHeight = blockInfo.blockHeight;
  5182. const uint32_t minBlockX = blockInfo.minBlockX;
  5183. const uint32_t minBlockY = blockInfo.minBlockY;
  5184. const uint8_t blockSize = blockInfo.blockSize;
  5185. const uint8_t* src = (const uint8_t*)_src;
  5186. const uint32_t numLayers = bx::max<uint32_t>(_numLayers, 1);
  5187. const uint32_t numSides = _cubeMap ? 6 : 1;
  5188. uint32_t width = _width;
  5189. uint32_t height = _height;
  5190. uint32_t depth = _depth;
  5191. for (uint8_t lod = 0; lod < _numMips && _err->isOk(); ++lod)
  5192. {
  5193. width = bx::max<uint32_t>(blockWidth * minBlockX, ( (width + blockWidth - 1) / blockWidth )*blockWidth);
  5194. height = bx::max<uint32_t>(blockHeight * minBlockY, ( (height + blockHeight - 1) / blockHeight)*blockHeight);
  5195. depth = bx::max<uint32_t>(1, depth);
  5196. const uint32_t mipSize = width/blockWidth * height/blockHeight * depth * blockSize;
  5197. const uint32_t size = numSides == 6 && numLayers == 1 ? mipSize : mipSize * numSides * numLayers;
  5198. total += bx::write(_writer, size, _err);
  5199. for (uint32_t layer = 0; layer < numLayers && _err->isOk(); ++layer)
  5200. {
  5201. for (uint8_t side = 0; side < numSides && _err->isOk(); ++side)
  5202. {
  5203. total += bx::write(_writer, src, mipSize, _err);
  5204. src += mipSize;
  5205. }
  5206. }
  5207. width >>= 1;
  5208. height >>= 1;
  5209. depth >>= 1;
  5210. }
  5211. return total;
  5212. }
  5213. int32_t imageWriteKtx(bx::WriterI* _writer, ImageContainer& _imageContainer, const void* _data, uint32_t _size, bx::Error* _err)
  5214. {
  5215. BX_ERROR_SCOPE(_err);
  5216. int32_t total = 0;
  5217. total += imageWriteKtxHeader(_writer
  5218. , TextureFormat::Enum(_imageContainer.m_format)
  5219. , _imageContainer.m_cubeMap
  5220. , _imageContainer.m_width
  5221. , _imageContainer.m_height
  5222. , _imageContainer.m_depth
  5223. , _imageContainer.m_numMips
  5224. , _imageContainer.m_numLayers
  5225. , _imageContainer.m_srgb
  5226. , _err
  5227. );
  5228. if (!_err->isOk() )
  5229. {
  5230. return total;
  5231. }
  5232. const uint32_t numMips = _imageContainer.m_numMips;
  5233. const uint32_t numLayers = bx::max<uint32_t>(_imageContainer.m_numLayers, 1);
  5234. const uint32_t numSides = _imageContainer.m_cubeMap ? 6 : 1;
  5235. for (uint8_t lod = 0; lod < numMips && _err->isOk(); ++lod)
  5236. {
  5237. ImageMip mip;
  5238. imageGetRawData(_imageContainer, 0, lod, _data, _size, mip);
  5239. const uint32_t size = numSides == 6 && numLayers == 1 ? mip.m_size : mip.m_size * numSides * numLayers;
  5240. total += bx::write(_writer, size, _err);
  5241. for (uint32_t layer = 0; layer < numLayers && _err->isOk(); ++layer)
  5242. {
  5243. for (uint8_t side = 0; side < numSides && _err->isOk(); ++side)
  5244. {
  5245. if (imageGetRawData(_imageContainer, uint16_t(layer*numSides + side), lod, _data, _size, mip) )
  5246. {
  5247. total += bx::write(_writer, mip.m_data, mip.m_size, _err);
  5248. }
  5249. }
  5250. }
  5251. }
  5252. return total;
  5253. }
  5254. } // namespace bimg