image.cpp 191 KB

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