image.cpp 194 KB

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