image.cpp 198 KB

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