image.cpp 158 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243224422452246224722482249225022512252225322542255225622572258225922602261226222632264226522662267226822692270227122722273227422752276227722782279228022812282228322842285228622872288228922902291229222932294229522962297229822992300230123022303230423052306230723082309231023112312231323142315231623172318231923202321232223232324232523262327232823292330233123322333233423352336233723382339234023412342234323442345234623472348234923502351235223532354235523562357235823592360236123622363236423652366236723682369237023712372237323742375237623772378237923802381238223832384238523862387238823892390239123922393239423952396239723982399240024012402240324042405240624072408240924102411241224132414241524162417241824192420242124222423242424252426242724282429243024312432243324342435243624372438243924402441244224432444244524462447244824492450245124522453245424552456245724582459246024612462246324642465246624672468246924702471247224732474247524762477247824792480248124822483248424852486248724882489249024912492249324942495249624972498249925002501250225032504250525062507250825092510251125122513251425152516251725182519252025212522252325242525252625272528252925302531253225332534253525362537253825392540254125422543254425452546254725482549255025512552255325542555255625572558255925602561256225632564256525662567256825692570257125722573257425752576257725782579258025812582258325842585258625872588258925902591259225932594259525962597259825992600260126022603260426052606260726082609261026112612261326142615261626172618261926202621262226232624262526262627262826292630263126322633263426352636263726382639264026412642264326442645264626472648264926502651265226532654265526562657265826592660266126622663266426652666266726682669267026712672267326742675267626772678267926802681268226832684268526862687268826892690269126922693269426952696269726982699270027012702270327042705270627072708270927102711271227132714271527162717271827192720272127222723272427252726272727282729273027312732273327342735273627372738273927402741274227432744274527462747274827492750275127522753275427552756275727582759276027612762276327642765276627672768276927702771277227732774277527762777277827792780278127822783278427852786278727882789279027912792279327942795279627972798279928002801280228032804280528062807280828092810281128122813281428152816281728182819282028212822282328242825282628272828282928302831283228332834283528362837283828392840284128422843284428452846284728482849285028512852285328542855285628572858285928602861286228632864286528662867286828692870287128722873287428752876287728782879288028812882288328842885288628872888288928902891289228932894289528962897289828992900290129022903290429052906290729082909291029112912291329142915291629172918291929202921292229232924292529262927292829292930293129322933293429352936293729382939294029412942294329442945294629472948294929502951295229532954295529562957295829592960296129622963296429652966296729682969297029712972297329742975297629772978297929802981298229832984298529862987298829892990299129922993299429952996299729982999300030013002300330043005300630073008300930103011301230133014301530163017301830193020302130223023302430253026302730283029303030313032303330343035303630373038303930403041304230433044304530463047304830493050305130523053305430553056305730583059306030613062306330643065306630673068306930703071307230733074307530763077307830793080308130823083308430853086308730883089309030913092309330943095309630973098309931003101310231033104310531063107310831093110311131123113311431153116311731183119312031213122312331243125312631273128312931303131313231333134313531363137313831393140314131423143314431453146314731483149315031513152315331543155315631573158315931603161316231633164316531663167316831693170317131723173317431753176317731783179318031813182318331843185318631873188318931903191319231933194319531963197319831993200320132023203320432053206320732083209321032113212321332143215321632173218321932203221322232233224322532263227322832293230323132323233323432353236323732383239324032413242324332443245324632473248324932503251325232533254325532563257325832593260326132623263326432653266326732683269327032713272327332743275327632773278327932803281328232833284328532863287328832893290329132923293329432953296329732983299330033013302330333043305330633073308330933103311331233133314331533163317331833193320332133223323332433253326332733283329333033313332333333343335333633373338333933403341334233433344334533463347334833493350335133523353335433553356335733583359336033613362336333643365336633673368336933703371337233733374337533763377337833793380338133823383338433853386338733883389339033913392339333943395339633973398339934003401340234033404340534063407340834093410341134123413341434153416341734183419342034213422342334243425342634273428342934303431343234333434343534363437343834393440344134423443344434453446344734483449345034513452345334543455345634573458345934603461346234633464346534663467346834693470347134723473347434753476347734783479348034813482348334843485348634873488348934903491349234933494349534963497349834993500350135023503350435053506350735083509351035113512351335143515351635173518351935203521352235233524352535263527352835293530353135323533353435353536353735383539354035413542354335443545354635473548354935503551355235533554355535563557355835593560356135623563356435653566356735683569357035713572357335743575357635773578357935803581358235833584358535863587358835893590359135923593359435953596359735983599360036013602360336043605360636073608360936103611361236133614361536163617361836193620362136223623362436253626362736283629363036313632363336343635363636373638363936403641364236433644364536463647364836493650365136523653365436553656365736583659366036613662366336643665366636673668366936703671367236733674367536763677367836793680368136823683368436853686368736883689369036913692369336943695369636973698369937003701370237033704370537063707370837093710371137123713371437153716371737183719372037213722372337243725372637273728372937303731373237333734373537363737373837393740374137423743374437453746374737483749375037513752375337543755375637573758375937603761376237633764376537663767376837693770377137723773377437753776377737783779378037813782378337843785378637873788378937903791379237933794379537963797379837993800380138023803380438053806380738083809381038113812381338143815381638173818381938203821382238233824382538263827382838293830383138323833383438353836383738383839384038413842384338443845384638473848384938503851385238533854385538563857385838593860386138623863386438653866386738683869387038713872387338743875387638773878387938803881388238833884388538863887388838893890389138923893389438953896389738983899390039013902390339043905390639073908390939103911391239133914391539163917391839193920392139223923392439253926392739283929393039313932393339343935393639373938393939403941394239433944394539463947394839493950395139523953395439553956395739583959396039613962396339643965396639673968396939703971397239733974397539763977397839793980398139823983398439853986398739883989399039913992399339943995399639973998399940004001400240034004400540064007400840094010401140124013401440154016401740184019402040214022402340244025402640274028402940304031403240334034403540364037403840394040404140424043404440454046404740484049405040514052405340544055405640574058405940604061406240634064406540664067406840694070407140724073407440754076407740784079408040814082408340844085408640874088408940904091409240934094409540964097409840994100410141024103410441054106410741084109411041114112411341144115411641174118411941204121412241234124412541264127412841294130413141324133413441354136413741384139414041414142414341444145414641474148414941504151415241534154415541564157415841594160416141624163416441654166416741684169417041714172417341744175417641774178417941804181418241834184418541864187418841894190419141924193419441954196419741984199420042014202420342044205420642074208420942104211421242134214421542164217421842194220422142224223422442254226422742284229423042314232423342344235423642374238423942404241424242434244424542464247424842494250425142524253425442554256425742584259426042614262426342644265426642674268426942704271427242734274427542764277427842794280428142824283428442854286428742884289429042914292429342944295429642974298429943004301430243034304430543064307430843094310431143124313431443154316431743184319432043214322432343244325432643274328432943304331433243334334433543364337433843394340434143424343434443454346434743484349435043514352435343544355435643574358435943604361436243634364436543664367436843694370437143724373437443754376437743784379438043814382438343844385438643874388438943904391439243934394439543964397439843994400440144024403440444054406
  1. /*
  2. * Copyright 2011-2018 Branimir Karadzic. All rights reserved.
  3. * License: https://github.com/bkaradzic/bimg#license-bsd-2-clause
  4. */
  5. #include "bimg_p.h"
  6. #include <bx/hash.h>
  7. namespace bimg
  8. {
  9. static const ImageBlockInfo s_imageBlockInfo[] =
  10. {
  11. // +-------------------------------------------- bits per pixel
  12. // | +----------------------------------------- block width
  13. // | | +-------------------------------------- block height
  14. // | | | +---------------------------------- block size
  15. // | | | | +------------------------------- min blocks x
  16. // | | | | | +---------------------------- min blocks y
  17. // | | | | | | +------------------------ depth bits
  18. // | | | | | | | +--------------------- stencil bits
  19. // | | | | | | | | +---+---+---+----- r, g, b, a bits
  20. // | | | | | | | | r g b a +-- encoding type
  21. // | | | | | | | | | | | | |
  22. { 4, 4, 4, 8, 1, 1, 0, 0, 0, 0, 0, 0, uint8_t(bx::EncodingType::Unorm) }, // BC1
  23. { 8, 4, 4, 16, 1, 1, 0, 0, 0, 0, 0, 0, uint8_t(bx::EncodingType::Unorm) }, // BC2
  24. { 8, 4, 4, 16, 1, 1, 0, 0, 0, 0, 0, 0, uint8_t(bx::EncodingType::Unorm) }, // BC3
  25. { 4, 4, 4, 8, 1, 1, 0, 0, 0, 0, 0, 0, uint8_t(bx::EncodingType::Unorm) }, // BC4
  26. { 8, 4, 4, 16, 1, 1, 0, 0, 0, 0, 0, 0, uint8_t(bx::EncodingType::Unorm) }, // BC5
  27. { 8, 4, 4, 16, 1, 1, 0, 0, 0, 0, 0, 0, uint8_t(bx::EncodingType::Float) }, // BC6H
  28. { 8, 4, 4, 16, 1, 1, 0, 0, 0, 0, 0, 0, uint8_t(bx::EncodingType::Unorm) }, // BC7
  29. { 4, 4, 4, 8, 1, 1, 0, 0, 0, 0, 0, 0, uint8_t(bx::EncodingType::Unorm) }, // ETC1
  30. { 4, 4, 4, 8, 1, 1, 0, 0, 0, 0, 0, 0, uint8_t(bx::EncodingType::Unorm) }, // ETC2
  31. { 8, 4, 4, 16, 1, 1, 0, 0, 0, 0, 0, 0, uint8_t(bx::EncodingType::Unorm) }, // ETC2A
  32. { 4, 4, 4, 8, 1, 1, 0, 0, 0, 0, 0, 0, uint8_t(bx::EncodingType::Unorm) }, // ETC2A1
  33. { 2, 8, 4, 8, 2, 2, 0, 0, 0, 0, 0, 0, uint8_t(bx::EncodingType::Unorm) }, // PTC12
  34. { 4, 4, 4, 8, 2, 2, 0, 0, 0, 0, 0, 0, uint8_t(bx::EncodingType::Unorm) }, // PTC14
  35. { 2, 8, 4, 8, 2, 2, 0, 0, 0, 0, 0, 0, uint8_t(bx::EncodingType::Unorm) }, // PTC12A
  36. { 4, 4, 4, 8, 2, 2, 0, 0, 0, 0, 0, 0, uint8_t(bx::EncodingType::Unorm) }, // PTC14A
  37. { 2, 8, 4, 8, 2, 2, 0, 0, 0, 0, 0, 0, uint8_t(bx::EncodingType::Unorm) }, // PTC22
  38. { 4, 4, 4, 8, 2, 2, 0, 0, 0, 0, 0, 0, uint8_t(bx::EncodingType::Unorm) }, // PTC24
  39. { 4, 4, 4, 8, 1, 1, 0, 0, 0, 0, 0, 0, uint8_t(bx::EncodingType::Unorm) }, // ATC
  40. { 8, 4, 4, 16, 1, 1, 0, 0, 0, 0, 0, 0, uint8_t(bx::EncodingType::Unorm) }, // ATCE
  41. { 8, 4, 4, 16, 1, 1, 0, 0, 0, 0, 0, 0, uint8_t(bx::EncodingType::Unorm) }, // ATCI
  42. { 8, 4, 4, 16, 1, 1, 0, 0, 0, 0, 0, 0, uint8_t(bx::EncodingType::Unorm) }, // ASTC4x4
  43. { 6, 5, 5, 16, 1, 1, 0, 0, 0, 0, 0, 0, uint8_t(bx::EncodingType::Unorm) }, // ASTC5x5
  44. { 4, 6, 6, 16, 1, 1, 0, 0, 0, 0, 0, 0, uint8_t(bx::EncodingType::Unorm) }, // ASTC6x6
  45. { 4, 8, 5, 16, 1, 1, 0, 0, 0, 0, 0, 0, uint8_t(bx::EncodingType::Unorm) }, // ASTC8x5
  46. { 3, 8, 6, 16, 1, 1, 0, 0, 0, 0, 0, 0, uint8_t(bx::EncodingType::Unorm) }, // ASTC8x6
  47. { 3, 10, 5, 16, 1, 1, 0, 0, 0, 0, 0, 0, uint8_t(bx::EncodingType::Unorm) }, // ASTC10x5
  48. { 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, uint8_t(bx::EncodingType::Count) }, // Unknown
  49. { 1, 8, 1, 1, 1, 1, 0, 0, 1, 0, 0, 0, uint8_t(bx::EncodingType::Unorm) }, // R1
  50. { 8, 1, 1, 1, 1, 1, 0, 0, 0, 0, 0, 8, uint8_t(bx::EncodingType::Unorm) }, // A8
  51. { 8, 1, 1, 1, 1, 1, 0, 0, 8, 0, 0, 0, uint8_t(bx::EncodingType::Unorm) }, // R8
  52. { 8, 1, 1, 1, 1, 1, 0, 0, 8, 0, 0, 0, uint8_t(bx::EncodingType::Int ) }, // R8I
  53. { 8, 1, 1, 1, 1, 1, 0, 0, 8, 0, 0, 0, uint8_t(bx::EncodingType::Uint ) }, // R8U
  54. { 8, 1, 1, 1, 1, 1, 0, 0, 8, 0, 0, 0, uint8_t(bx::EncodingType::Snorm) }, // R8S
  55. { 16, 1, 1, 2, 1, 1, 0, 0, 16, 0, 0, 0, uint8_t(bx::EncodingType::Unorm) }, // R16
  56. { 16, 1, 1, 2, 1, 1, 0, 0, 16, 0, 0, 0, uint8_t(bx::EncodingType::Int ) }, // R16I
  57. { 16, 1, 1, 2, 1, 1, 0, 0, 16, 0, 0, 0, uint8_t(bx::EncodingType::Uint ) }, // R16U
  58. { 16, 1, 1, 2, 1, 1, 0, 0, 16, 0, 0, 0, uint8_t(bx::EncodingType::Float) }, // R16F
  59. { 16, 1, 1, 2, 1, 1, 0, 0, 16, 0, 0, 0, uint8_t(bx::EncodingType::Snorm) }, // R16S
  60. { 32, 1, 1, 4, 1, 1, 0, 0, 32, 0, 0, 0, uint8_t(bx::EncodingType::Int ) }, // R32I
  61. { 32, 1, 1, 4, 1, 1, 0, 0, 32, 0, 0, 0, uint8_t(bx::EncodingType::Uint ) }, // R32U
  62. { 32, 1, 1, 4, 1, 1, 0, 0, 32, 0, 0, 0, uint8_t(bx::EncodingType::Float) }, // R32F
  63. { 16, 1, 1, 2, 1, 1, 0, 0, 8, 8, 0, 0, uint8_t(bx::EncodingType::Unorm) }, // RG8
  64. { 16, 1, 1, 2, 1, 1, 0, 0, 8, 8, 0, 0, uint8_t(bx::EncodingType::Int ) }, // RG8I
  65. { 16, 1, 1, 2, 1, 1, 0, 0, 8, 8, 0, 0, uint8_t(bx::EncodingType::Uint ) }, // RG8U
  66. { 16, 1, 1, 2, 1, 1, 0, 0, 8, 8, 0, 0, uint8_t(bx::EncodingType::Snorm) }, // RG8S
  67. { 32, 1, 1, 4, 1, 1, 0, 0, 16, 16, 0, 0, uint8_t(bx::EncodingType::Unorm) }, // RG16
  68. { 32, 1, 1, 4, 1, 1, 0, 0, 16, 16, 0, 0, uint8_t(bx::EncodingType::Int ) }, // RG16I
  69. { 32, 1, 1, 4, 1, 1, 0, 0, 16, 16, 0, 0, uint8_t(bx::EncodingType::Uint ) }, // RG16U
  70. { 32, 1, 1, 4, 1, 1, 0, 0, 16, 16, 0, 0, uint8_t(bx::EncodingType::Float) }, // RG16F
  71. { 32, 1, 1, 4, 1, 1, 0, 0, 16, 16, 0, 0, uint8_t(bx::EncodingType::Snorm) }, // RG16S
  72. { 64, 1, 1, 8, 1, 1, 0, 0, 32, 32, 0, 0, uint8_t(bx::EncodingType::Int ) }, // RG32I
  73. { 64, 1, 1, 8, 1, 1, 0, 0, 32, 32, 0, 0, uint8_t(bx::EncodingType::Uint ) }, // RG32U
  74. { 64, 1, 1, 8, 1, 1, 0, 0, 32, 32, 0, 0, uint8_t(bx::EncodingType::Float) }, // RG32F
  75. { 24, 1, 1, 3, 1, 1, 0, 0, 8, 8, 8, 0, uint8_t(bx::EncodingType::Unorm) }, // RGB8
  76. { 24, 1, 1, 3, 1, 1, 0, 0, 8, 8, 8, 0, uint8_t(bx::EncodingType::Int ) }, // RGB8I
  77. { 24, 1, 1, 3, 1, 1, 0, 0, 8, 8, 8, 0, uint8_t(bx::EncodingType::Uint ) }, // RGB8U
  78. { 24, 1, 1, 3, 1, 1, 0, 0, 8, 8, 8, 0, uint8_t(bx::EncodingType::Snorm) }, // RGB8S
  79. { 32, 1, 1, 4, 1, 1, 0, 0, 9, 9, 9, 5, uint8_t(bx::EncodingType::Float) }, // RGB9E5F
  80. { 32, 1, 1, 4, 1, 1, 0, 0, 8, 8, 8, 8, uint8_t(bx::EncodingType::Unorm) }, // BGRA8
  81. { 32, 1, 1, 4, 1, 1, 0, 0, 8, 8, 8, 8, uint8_t(bx::EncodingType::Unorm) }, // RGBA8
  82. { 32, 1, 1, 4, 1, 1, 0, 0, 8, 8, 8, 8, uint8_t(bx::EncodingType::Int ) }, // RGBA8I
  83. { 32, 1, 1, 4, 1, 1, 0, 0, 8, 8, 8, 8, uint8_t(bx::EncodingType::Uint ) }, // RGBA8U
  84. { 32, 1, 1, 4, 1, 1, 0, 0, 8, 8, 8, 8, uint8_t(bx::EncodingType::Snorm) }, // RGBA8S
  85. { 64, 1, 1, 8, 1, 1, 0, 0, 16, 16, 16, 16, uint8_t(bx::EncodingType::Unorm) }, // RGBA16
  86. { 64, 1, 1, 8, 1, 1, 0, 0, 16, 16, 16, 16, uint8_t(bx::EncodingType::Int ) }, // RGBA16I
  87. { 64, 1, 1, 8, 1, 1, 0, 0, 16, 16, 16, 16, uint8_t(bx::EncodingType::Uint ) }, // RGBA16U
  88. { 64, 1, 1, 8, 1, 1, 0, 0, 16, 16, 16, 16, uint8_t(bx::EncodingType::Float) }, // RGBA16F
  89. { 64, 1, 1, 8, 1, 1, 0, 0, 16, 16, 16, 16, uint8_t(bx::EncodingType::Snorm) }, // RGBA16S
  90. { 128, 1, 1, 16, 1, 1, 0, 0, 32, 32, 32, 32, uint8_t(bx::EncodingType::Int ) }, // RGBA32I
  91. { 128, 1, 1, 16, 1, 1, 0, 0, 32, 32, 32, 32, uint8_t(bx::EncodingType::Uint ) }, // RGBA32U
  92. { 128, 1, 1, 16, 1, 1, 0, 0, 32, 32, 32, 32, uint8_t(bx::EncodingType::Float) }, // RGBA32F
  93. { 16, 1, 1, 2, 1, 1, 0, 0, 5, 6, 5, 0, uint8_t(bx::EncodingType::Unorm) }, // R5G6B5
  94. { 16, 1, 1, 2, 1, 1, 0, 0, 4, 4, 4, 4, uint8_t(bx::EncodingType::Unorm) }, // RGBA4
  95. { 16, 1, 1, 2, 1, 1, 0, 0, 5, 5, 5, 1, uint8_t(bx::EncodingType::Unorm) }, // RGB5A1
  96. { 32, 1, 1, 4, 1, 1, 0, 0, 10, 10, 10, 2, uint8_t(bx::EncodingType::Unorm) }, // RGB10A2
  97. { 32, 1, 1, 4, 1, 1, 0, 0, 11, 11, 10, 0, uint8_t(bx::EncodingType::Unorm) }, // RG11B10F
  98. { 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, uint8_t(bx::EncodingType::Count) }, // UnknownDepth
  99. { 16, 1, 1, 2, 1, 1, 16, 0, 0, 0, 0, 0, uint8_t(bx::EncodingType::Unorm) }, // D16
  100. { 24, 1, 1, 3, 1, 1, 24, 0, 0, 0, 0, 0, uint8_t(bx::EncodingType::Unorm) }, // D24
  101. { 32, 1, 1, 4, 1, 1, 24, 8, 0, 0, 0, 0, uint8_t(bx::EncodingType::Unorm) }, // D24S8
  102. { 32, 1, 1, 4, 1, 1, 32, 0, 0, 0, 0, 0, uint8_t(bx::EncodingType::Unorm) }, // D32
  103. { 16, 1, 1, 2, 1, 1, 16, 0, 0, 0, 0, 0, uint8_t(bx::EncodingType::Float) }, // D16F
  104. { 24, 1, 1, 3, 1, 1, 24, 0, 0, 0, 0, 0, uint8_t(bx::EncodingType::Float) }, // D24F
  105. { 32, 1, 1, 4, 1, 1, 32, 0, 0, 0, 0, 0, uint8_t(bx::EncodingType::Float) }, // D32F
  106. { 8, 1, 1, 1, 1, 1, 0, 8, 0, 0, 0, 0, uint8_t(bx::EncodingType::Unorm) }, // D0S8
  107. };
  108. BX_STATIC_ASSERT(TextureFormat::Count == BX_COUNTOF(s_imageBlockInfo) );
  109. static const char* s_textureFormatName[] =
  110. {
  111. "BC1", // BC1
  112. "BC2", // BC2
  113. "BC3", // BC3
  114. "BC4", // BC4
  115. "BC5", // BC5
  116. "BC6H", // BC6H
  117. "BC7", // BC7
  118. "ETC1", // ETC1
  119. "ETC2", // ETC2
  120. "ETC2A", // ETC2A
  121. "ETC2A1", // ETC2A1
  122. "PTC12", // PTC12
  123. "PTC14", // PTC14
  124. "PTC12A", // PTC12A
  125. "PTC14A", // PTC14A
  126. "PTC22", // PTC22
  127. "PTC24", // PTC24
  128. "ATC", // ATC
  129. "ATCE", // ATCE
  130. "ATCI", // ATCI
  131. "ASTC4x4", // ASTC4x4
  132. "ASTC5x5", // ASTC5x5
  133. "ASTC6x6", // ASTC6x6
  134. "ASTC8x5", // ASTC8x5
  135. "ASTC8x6", // ASTC8x6
  136. "ASTC10x5", // ASTC10x5
  137. "<unknown>", // Unknown
  138. "R1", // R1
  139. "A8", // A8
  140. "R8", // R8
  141. "R8I", // R8I
  142. "R8U", // R8U
  143. "R8S", // R8S
  144. "R16", // R16
  145. "R16I", // R16I
  146. "R16U", // R16U
  147. "R16F", // R16F
  148. "R16S", // R16S
  149. "R32I", // R32I
  150. "R32U", // R32U
  151. "R32F", // R32F
  152. "RG8", // RG8
  153. "RG8I", // RG8I
  154. "RG8U", // RG8U
  155. "RG8S", // RG8S
  156. "RG16", // RG16
  157. "RG16I", // RG16I
  158. "RG16U", // RG16U
  159. "RG16F", // RG16F
  160. "RG16S", // RG16S
  161. "RG32I", // RG32I
  162. "RG32U", // RG32U
  163. "RG32F", // RG32F
  164. "RGB8", // RGB8
  165. "RGB8I", // RGB8I
  166. "RGB8U", // RGB8U
  167. "RGB8S", // RGB8S
  168. "RGB9E5", // RGB9E5F
  169. "BGRA8", // BGRA8
  170. "RGBA8", // RGBA8
  171. "RGBA8I", // RGBA8I
  172. "RGBA8U", // RGBA8U
  173. "RGBA8S", // RGBA8S
  174. "RGBA16", // RGBA16
  175. "RGBA16I", // RGBA16I
  176. "RGBA16U", // RGBA16U
  177. "RGBA16F", // RGBA16F
  178. "RGBA16S", // RGBA16S
  179. "RGBA32I", // RGBA32I
  180. "RGBA32U", // RGBA32U
  181. "RGBA32F", // RGBA32F
  182. "R5G6B5", // R5G6B5
  183. "RGBA4", // RGBA4
  184. "RGB5A1", // RGB5A1
  185. "RGB10A2", // RGB10A2
  186. "RG11B10F", // RG11B10F
  187. "<unknown>", // UnknownDepth
  188. "D16", // D16
  189. "D24", // D24
  190. "D24S8", // D24S8
  191. "D32", // D32
  192. "D16F", // D16F
  193. "D24F", // D24F
  194. "D32F", // D32F
  195. "D0S8", // D0S8
  196. };
  197. BX_STATIC_ASSERT(TextureFormat::Count == BX_COUNTOF(s_textureFormatName) );
  198. bool isCompressed(TextureFormat::Enum _format)
  199. {
  200. return _format < TextureFormat::Unknown;
  201. }
  202. bool isColor(TextureFormat::Enum _format)
  203. {
  204. return _format > TextureFormat::Unknown
  205. && _format < TextureFormat::UnknownDepth
  206. ;
  207. }
  208. bool isDepth(TextureFormat::Enum _format)
  209. {
  210. return _format > TextureFormat::UnknownDepth
  211. && _format < TextureFormat::Count
  212. ;
  213. }
  214. bool isValid(TextureFormat::Enum _format)
  215. {
  216. return _format != TextureFormat::Unknown
  217. && _format != TextureFormat::UnknownDepth
  218. && _format != TextureFormat::Count
  219. ;
  220. }
  221. bool isFloat(TextureFormat::Enum _format)
  222. {
  223. return uint8_t(bx::EncodingType::Float) == s_imageBlockInfo[_format].encoding;
  224. }
  225. uint8_t getBitsPerPixel(TextureFormat::Enum _format)
  226. {
  227. return s_imageBlockInfo[_format].bitsPerPixel;
  228. }
  229. const ImageBlockInfo& getBlockInfo(TextureFormat::Enum _format)
  230. {
  231. return s_imageBlockInfo[_format];
  232. }
  233. uint8_t getBlockSize(TextureFormat::Enum _format)
  234. {
  235. return s_imageBlockInfo[_format].blockSize;
  236. }
  237. const char* getName(TextureFormat::Enum _format)
  238. {
  239. return s_textureFormatName[_format];
  240. }
  241. TextureFormat::Enum getFormat(const char* _name)
  242. {
  243. for (uint32_t ii = 0; ii < TextureFormat::Count; ++ii)
  244. {
  245. const TextureFormat::Enum fmt = TextureFormat::Enum(ii);
  246. if (isValid(fmt) )
  247. {
  248. if (0 == bx::strCmpI(s_textureFormatName[ii], _name) )
  249. {
  250. return fmt;
  251. }
  252. }
  253. }
  254. return TextureFormat::Unknown;
  255. }
  256. uint8_t imageGetNumMips(TextureFormat::Enum _format, uint16_t _width, uint16_t _height, uint16_t _depth)
  257. {
  258. const ImageBlockInfo& blockInfo = getBlockInfo(_format);
  259. const uint16_t blockWidth = blockInfo.blockWidth;
  260. const uint16_t blockHeight = blockInfo.blockHeight;
  261. const uint16_t minBlockX = blockInfo.minBlockX;
  262. const uint16_t minBlockY = blockInfo.minBlockY;
  263. _width = bx::uint16_max(blockWidth * minBlockX, ( (_width + blockWidth - 1) / blockWidth )*blockWidth);
  264. _height = bx::uint16_max(blockHeight * minBlockY, ( (_height + blockHeight - 1) / blockHeight)*blockHeight);
  265. _depth = bx::uint16_max(1, _depth);
  266. uint8_t numMips = calcNumMips(true, _width, _height, _depth);
  267. return numMips;
  268. }
  269. uint32_t imageGetSize(TextureInfo* _info, uint16_t _width, uint16_t _height, uint16_t _depth, bool _cubeMap, bool _hasMips, uint16_t _numLayers, TextureFormat::Enum _format)
  270. {
  271. const ImageBlockInfo& blockInfo = getBlockInfo(_format);
  272. const uint8_t bpp = blockInfo.bitsPerPixel;
  273. const uint16_t blockSize = blockInfo.blockSize;
  274. const uint16_t blockWidth = blockInfo.blockWidth;
  275. const uint16_t blockHeight = blockInfo.blockHeight;
  276. const uint16_t minBlockX = blockInfo.minBlockX;
  277. const uint16_t minBlockY = blockInfo.minBlockY;
  278. const uint8_t numMips = calcNumMips(_hasMips, _width, _height, _depth);
  279. const uint32_t sides = _cubeMap ? 6 : 1;
  280. uint32_t width = _width;
  281. uint32_t height = _height;
  282. uint32_t depth = _depth;
  283. uint32_t size = 0;
  284. if (_format != TextureFormat::Unknown)
  285. {
  286. for (uint32_t lod = 0; lod < numMips; ++lod)
  287. {
  288. depth = bx::uint32_max(1, depth);
  289. uint16_t blocksX = bx::uint32_max(minBlockX, ((width + blockWidth - 1) / blockWidth ));
  290. uint16_t blocksY = bx::uint32_max(minBlockY, ((height + blockHeight - 1) / blockHeight));
  291. size += blocksX * blocksY * blockSize * depth * sides;
  292. width >>= 1;
  293. height >>= 1;
  294. depth >>= 1;
  295. }
  296. }
  297. size *= _numLayers;
  298. if (NULL != _info)
  299. {
  300. _info->format = _format;
  301. _info->width = _width;
  302. _info->height = _height;
  303. _info->depth = _depth;
  304. _info->numMips = numMips;
  305. _info->numLayers = _numLayers;
  306. _info->cubeMap = _cubeMap;
  307. _info->storageSize = size;
  308. _info->bitsPerPixel = bpp;
  309. }
  310. return size;
  311. }
  312. void imageSolid(void* _dst, uint32_t _width, uint32_t _height, uint32_t _solid)
  313. {
  314. uint32_t* dst = (uint32_t*)_dst;
  315. for (uint32_t ii = 0, num = _width*_height; ii < num; ++ii)
  316. {
  317. *dst++ = _solid;
  318. }
  319. }
  320. void imageCheckerboard(void* _dst, uint32_t _width, uint32_t _height, uint32_t _step, uint32_t _0, uint32_t _1)
  321. {
  322. uint32_t* dst = (uint32_t*)_dst;
  323. for (uint32_t yy = 0; yy < _height; ++yy)
  324. {
  325. for (uint32_t xx = 0; xx < _width; ++xx)
  326. {
  327. uint32_t abgr = ( (xx/_step)&1) ^ ( (yy/_step)&1) ? _1 : _0;
  328. *dst++ = abgr;
  329. }
  330. }
  331. }
  332. void imageRgba8Downsample2x2Ref(void* _dst, uint32_t _width, uint32_t _height, uint32_t _depth, uint32_t _srcPitch, uint32_t _dstPitch, const void* _src)
  333. {
  334. const uint32_t dstWidth = _width/2;
  335. const uint32_t dstHeight = _height/2;
  336. if (0 == dstWidth
  337. || 0 == dstHeight)
  338. {
  339. return;
  340. }
  341. const uint8_t* src = (const uint8_t*)_src;
  342. for (uint32_t zz = 0; zz < _depth; ++zz)
  343. {
  344. for (uint32_t yy = 0, ystep = _srcPitch*2; yy < dstHeight; ++yy, src += ystep)
  345. {
  346. uint8_t* dst = (uint8_t*)_dst + _dstPitch*yy;
  347. const uint8_t* rgba = src;
  348. for (uint32_t xx = 0; xx < dstWidth; ++xx, rgba += 8, dst += 4)
  349. {
  350. float rr = bx::toLinear(rgba[ 0]);
  351. float gg = bx::toLinear(rgba[ 1]);
  352. float bb = bx::toLinear(rgba[ 2]);
  353. float aa = rgba[ 3];
  354. rr += bx::toLinear(rgba[ 4]);
  355. gg += bx::toLinear(rgba[ 5]);
  356. bb += bx::toLinear(rgba[ 6]);
  357. aa += rgba[ 7];
  358. rr += bx::toLinear(rgba[_srcPitch+0]);
  359. gg += bx::toLinear(rgba[_srcPitch+1]);
  360. bb += bx::toLinear(rgba[_srcPitch+2]);
  361. aa += rgba[_srcPitch+3];
  362. rr += bx::toLinear(rgba[_srcPitch+4]);
  363. gg += bx::toLinear(rgba[_srcPitch+5]);
  364. bb += bx::toLinear(rgba[_srcPitch+6]);
  365. aa += rgba[_srcPitch+7];
  366. rr *= 0.25f;
  367. gg *= 0.25f;
  368. bb *= 0.25f;
  369. aa *= 0.25f;
  370. rr = bx::toGamma(rr);
  371. gg = bx::toGamma(gg);
  372. bb = bx::toGamma(bb);
  373. dst[0] = (uint8_t)rr;
  374. dst[1] = (uint8_t)gg;
  375. dst[2] = (uint8_t)bb;
  376. dst[3] = (uint8_t)aa;
  377. }
  378. }
  379. }
  380. }
  381. BX_SIMD_INLINE bx::simd128_t simd_to_linear(bx::simd128_t _a)
  382. {
  383. using namespace bx;
  384. const simd128_t f12_92 = simd_ld(12.92f, 12.92f, 12.92f, 1.0f);
  385. const simd128_t f0_055 = simd_ld(0.055f, 0.055f, 0.055f, 0.0f);
  386. const simd128_t f1_055 = simd_ld(1.055f, 1.055f, 1.055f, 1.0f);
  387. const simd128_t f2_4 = simd_ld(2.4f, 2.4f, 2.4f, 1.0f);
  388. const simd128_t f0_04045 = simd_ld(0.04045f, 0.04045f, 0.04045f, 0.0f);
  389. const simd128_t lo = simd_div(_a, f12_92);
  390. const simd128_t tmp0 = simd_add(_a, f0_055);
  391. const simd128_t tmp1 = simd_div(tmp0, f1_055);
  392. const simd128_t hi = simd_pow(tmp1, f2_4);
  393. const simd128_t mask = simd_cmple(_a, f0_04045);
  394. const simd128_t result = simd_selb(mask, hi, lo);
  395. return result;
  396. }
  397. BX_SIMD_INLINE bx::simd128_t simd_to_gamma(bx::simd128_t _a)
  398. {
  399. using namespace bx;
  400. const simd128_t f12_92 = simd_ld(12.92f, 12.92f, 12.92f, 1.0f);
  401. const simd128_t f0_055 = simd_ld(0.055f, 0.055f, 0.055f, 0.0f);
  402. const simd128_t f1_055 = simd_ld(1.055f, 1.055f, 1.055f, 1.0f);
  403. const simd128_t f1o2_4 = simd_ld(1.0f/2.4f, 1.0f/2.4f, 1.0f/2.4f, 1.0f);
  404. const simd128_t f0_0031308 = simd_ld(0.0031308f, 0.0031308f, 0.0031308f, 0.0f);
  405. const simd128_t lo = simd_mul(_a, f12_92);
  406. const simd128_t absa = simd_abs(_a);
  407. const simd128_t tmp0 = simd_pow(absa, f1o2_4);
  408. const simd128_t tmp1 = simd_mul(tmp0, f1_055);
  409. const simd128_t hi = simd_sub(tmp1, f0_055);
  410. const simd128_t mask = simd_cmple(_a, f0_0031308);
  411. const simd128_t result = simd_selb(mask, hi, lo);
  412. return result;
  413. }
  414. void imageRgba8Downsample2x2(void* _dst, uint32_t _width, uint32_t _height, uint32_t _depth, uint32_t _srcPitch, uint32_t _dstPitch, const void* _src)
  415. {
  416. const uint32_t dstWidth = _width/2;
  417. const uint32_t dstHeight = _height/2;
  418. if (0 == dstWidth
  419. || 0 == dstHeight)
  420. {
  421. return;
  422. }
  423. const uint8_t* src = (const uint8_t*)_src;
  424. using namespace bx;
  425. const simd128_t unpack = simd_ld(1.0f, 1.0f/256.0f, 1.0f/65536.0f, 1.0f/16777216.0f);
  426. const simd128_t pack = simd_ld(1.0f, 256.0f*0.5f, 65536.0f, 16777216.0f*0.5f);
  427. const simd128_t umask = simd_ild(0xff, 0xff00, 0xff0000, 0xff000000);
  428. const simd128_t pmask = simd_ild(0xff, 0x7f80, 0xff0000, 0x7f800000);
  429. const simd128_t wflip = simd_ild(0, 0, 0, 0x80000000);
  430. const simd128_t wadd = simd_ld(0.0f, 0.0f, 0.0f, 32768.0f*65536.0f);
  431. const simd128_t quater = simd_splat(0.25f);
  432. for (uint32_t zz = 0; zz < _depth; ++zz)
  433. {
  434. for (uint32_t yy = 0, ystep = _srcPitch*2; yy < dstHeight; ++yy, src += ystep)
  435. {
  436. uint8_t* dst = (uint8_t*)_dst + _dstPitch*yy;
  437. const uint8_t* rgba = src;
  438. for (uint32_t xx = 0; xx < dstWidth; ++xx, rgba += 8, dst += 4)
  439. {
  440. const simd128_t abgr0 = simd_splat(rgba);
  441. const simd128_t abgr1 = simd_splat(rgba+4);
  442. const simd128_t abgr2 = simd_splat(rgba+_srcPitch);
  443. const simd128_t abgr3 = simd_splat(rgba+_srcPitch+4);
  444. const simd128_t abgr0m = simd_and(abgr0, umask);
  445. const simd128_t abgr1m = simd_and(abgr1, umask);
  446. const simd128_t abgr2m = simd_and(abgr2, umask);
  447. const simd128_t abgr3m = simd_and(abgr3, umask);
  448. const simd128_t abgr0x = simd_xor(abgr0m, wflip);
  449. const simd128_t abgr1x = simd_xor(abgr1m, wflip);
  450. const simd128_t abgr2x = simd_xor(abgr2m, wflip);
  451. const simd128_t abgr3x = simd_xor(abgr3m, wflip);
  452. const simd128_t abgr0f = simd_itof(abgr0x);
  453. const simd128_t abgr1f = simd_itof(abgr1x);
  454. const simd128_t abgr2f = simd_itof(abgr2x);
  455. const simd128_t abgr3f = simd_itof(abgr3x);
  456. const simd128_t abgr0c = simd_add(abgr0f, wadd);
  457. const simd128_t abgr1c = simd_add(abgr1f, wadd);
  458. const simd128_t abgr2c = simd_add(abgr2f, wadd);
  459. const simd128_t abgr3c = simd_add(abgr3f, wadd);
  460. const simd128_t abgr0n = simd_mul(abgr0c, unpack);
  461. const simd128_t abgr1n = simd_mul(abgr1c, unpack);
  462. const simd128_t abgr2n = simd_mul(abgr2c, unpack);
  463. const simd128_t abgr3n = simd_mul(abgr3c, unpack);
  464. const simd128_t abgr0l = simd_to_linear(abgr0n);
  465. const simd128_t abgr1l = simd_to_linear(abgr1n);
  466. const simd128_t abgr2l = simd_to_linear(abgr2n);
  467. const simd128_t abgr3l = simd_to_linear(abgr3n);
  468. const simd128_t sum0 = simd_add(abgr0l, abgr1l);
  469. const simd128_t sum1 = simd_add(abgr2l, abgr3l);
  470. const simd128_t sum2 = simd_add(sum0, sum1);
  471. const simd128_t avg0 = simd_mul(sum2, quater);
  472. const simd128_t avg1 = simd_to_gamma(avg0);
  473. const simd128_t avg2 = simd_mul(avg1, pack);
  474. const simd128_t ftoi0 = simd_ftoi(avg2);
  475. const simd128_t ftoi1 = simd_and(ftoi0, pmask);
  476. const simd128_t zwxy = simd_swiz_zwxy(ftoi1);
  477. const simd128_t tmp0 = simd_or(ftoi1, zwxy);
  478. const simd128_t yyyy = simd_swiz_yyyy(tmp0);
  479. const simd128_t tmp1 = simd_iadd(yyyy, yyyy);
  480. const simd128_t result = simd_or(tmp0, tmp1);
  481. simd_stx(dst, result);
  482. }
  483. }
  484. }
  485. }
  486. void imageRgba32fToLinear(void* _dst, uint32_t _width, uint32_t _height, uint32_t _depth, uint32_t _srcPitch, const void* _src)
  487. {
  488. uint8_t* dst = ( uint8_t*)_dst;
  489. const uint8_t* src = (const uint8_t*)_src;
  490. for (uint32_t zz = 0; zz < _depth; ++zz)
  491. {
  492. for (uint32_t yy = 0; yy < _height; ++yy, src += _srcPitch, dst += _width*16)
  493. {
  494. for (uint32_t xx = 0; xx < _width; ++xx)
  495. {
  496. const uint32_t offset = xx * 16;
  497. float* fd = ( float*)(dst + offset);
  498. const float* fs = (const float*)(src + offset);
  499. fd[0] = bx::toLinear(fs[0]);
  500. fd[1] = bx::toLinear(fs[1]);
  501. fd[2] = bx::toLinear(fs[2]);
  502. fd[3] = fs[3];
  503. }
  504. }
  505. }
  506. }
  507. void imageRgba32fToGamma(void* _dst, uint32_t _width, uint32_t _height, uint32_t _depth, uint32_t _srcPitch, const void* _src)
  508. {
  509. uint8_t* dst = ( uint8_t*)_dst;
  510. const uint8_t* src = (const uint8_t*)_src;
  511. for (uint32_t zz = 0; zz < _depth; ++zz)
  512. {
  513. for (uint32_t yy = 0; yy < _height; ++yy, src += _srcPitch, dst += _width*16)
  514. {
  515. for (uint32_t xx = 0; xx < _width; ++xx)
  516. {
  517. const uint32_t offset = xx * 16;
  518. float* fd = ( float*)(dst + offset);
  519. const float* fs = (const float*)(src + offset);
  520. fd[0] = bx::toGamma(fs[0]);
  521. fd[1] = bx::toGamma(fs[1]);
  522. fd[2] = bx::toGamma(fs[2]);
  523. fd[3] = fs[3];
  524. }
  525. }
  526. }
  527. }
  528. void imageRgba32fLinearDownsample2x2Ref(void* _dst, uint32_t _width, uint32_t _height, uint32_t _depth, uint32_t _srcPitch, const void* _src)
  529. {
  530. const uint32_t dstWidth = _width/2;
  531. const uint32_t dstHeight = _height/2;
  532. const uint32_t dstDepth = _depth/2;
  533. if (0 == dstWidth
  534. || 0 == dstHeight)
  535. {
  536. return;
  537. }
  538. const uint8_t* src = (const uint8_t*)_src;
  539. uint8_t* dst = (uint8_t*)_dst;
  540. if (0 == dstDepth)
  541. {
  542. for (uint32_t yy = 0, ystep = _srcPitch*2; yy < dstHeight; ++yy, src += ystep)
  543. {
  544. const float* rgba0 = (const float*)&src[0];
  545. const float* rgba1 = (const float*)&src[_srcPitch];
  546. for (uint32_t xx = 0; xx < dstWidth; ++xx, rgba0 += 8, rgba1 += 8, dst += 16)
  547. {
  548. float xyz[4];
  549. xyz[0] = rgba0[0];
  550. xyz[1] = rgba0[1];
  551. xyz[2] = rgba0[2];
  552. xyz[3] = rgba0[3];
  553. xyz[0] += rgba0[4];
  554. xyz[1] += rgba0[5];
  555. xyz[2] += rgba0[6];
  556. xyz[3] += rgba0[7];
  557. xyz[0] += rgba1[0];
  558. xyz[1] += rgba1[1];
  559. xyz[2] += rgba1[2];
  560. xyz[3] += rgba1[3];
  561. xyz[0] += rgba1[4];
  562. xyz[1] += rgba1[5];
  563. xyz[2] += rgba1[6];
  564. xyz[3] += rgba1[7];
  565. xyz[0] *= 1.0f/4.0f;
  566. xyz[1] *= 1.0f/4.0f;
  567. xyz[2] *= 1.0f/4.0f;
  568. xyz[3] *= 1.0f/4.0f;
  569. bx::packRgba32F(dst, xyz);
  570. }
  571. }
  572. }
  573. else
  574. {
  575. const uint32_t slicePitch = _srcPitch*_height;
  576. for (uint32_t zz = 0; zz < dstDepth; ++zz, src += slicePitch)
  577. {
  578. for (uint32_t yy = 0, ystep = _srcPitch*2; yy < dstHeight; ++yy, src += ystep)
  579. {
  580. const float* rgba0 = (const float*)&src[0];
  581. const float* rgba1 = (const float*)&src[_srcPitch];
  582. const float* rgba2 = (const float*)&src[slicePitch];
  583. const float* rgba3 = (const float*)&src[slicePitch+_srcPitch];
  584. for (uint32_t xx = 0
  585. ; xx < dstWidth
  586. ; ++xx, rgba0 += 8, rgba1 += 8, rgba2 += 8, rgba3 += 8, dst += 16
  587. )
  588. {
  589. float xyz[4];
  590. xyz[0] = rgba0[0];
  591. xyz[1] = rgba0[1];
  592. xyz[2] = rgba0[2];
  593. xyz[3] = rgba0[3];
  594. xyz[0] += rgba0[4];
  595. xyz[1] += rgba0[5];
  596. xyz[2] += rgba0[6];
  597. xyz[3] += rgba0[7];
  598. xyz[0] += rgba1[0];
  599. xyz[1] += rgba1[1];
  600. xyz[2] += rgba1[2];
  601. xyz[3] += rgba1[3];
  602. xyz[0] += rgba1[4];
  603. xyz[1] += rgba1[5];
  604. xyz[2] += rgba1[6];
  605. xyz[3] += rgba1[7];
  606. xyz[0] += rgba2[0];
  607. xyz[1] += rgba2[1];
  608. xyz[2] += rgba2[2];
  609. xyz[3] += rgba2[3];
  610. xyz[0] += rgba2[4];
  611. xyz[1] += rgba2[5];
  612. xyz[2] += rgba2[6];
  613. xyz[3] += rgba2[7];
  614. xyz[0] += rgba3[0];
  615. xyz[1] += rgba3[1];
  616. xyz[2] += rgba3[2];
  617. xyz[3] += rgba3[3];
  618. xyz[0] += rgba3[4];
  619. xyz[1] += rgba3[5];
  620. xyz[2] += rgba3[6];
  621. xyz[3] += rgba3[7];
  622. xyz[0] *= 1.0f/8.0f;
  623. xyz[1] *= 1.0f/8.0f;
  624. xyz[2] *= 1.0f/8.0f;
  625. xyz[3] *= 1.0f/8.0f;
  626. bx::packRgba32F(dst, xyz);
  627. }
  628. }
  629. }
  630. }
  631. }
  632. void imageRgba32fLinearDownsample2x2(void* _dst, uint32_t _width, uint32_t _height, uint32_t _depth, uint32_t _srcPitch, const void* _src)
  633. {
  634. imageRgba32fLinearDownsample2x2Ref(_dst, _width, _height, _depth, _srcPitch, _src);
  635. }
  636. void imageRgba32fDownsample2x2NormalMapRef(void* _dst, uint32_t _width, uint32_t _height, uint32_t _srcPitch, uint32_t _dstPitch, const void* _src)
  637. {
  638. const uint32_t dstWidth = _width/2;
  639. const uint32_t dstHeight = _height/2;
  640. if (0 == dstWidth
  641. || 0 == dstHeight)
  642. {
  643. return;
  644. }
  645. const uint8_t* src = (const uint8_t*)_src;
  646. for (uint32_t yy = 0, ystep = _srcPitch*2; yy < dstHeight; ++yy, src += ystep)
  647. {
  648. const float* rgba0 = (const float*)&src[0];
  649. const float* rgba1 = (const float*)&src[_srcPitch];
  650. uint8_t* dst = (uint8_t*)_dst + _dstPitch*yy;
  651. for (uint32_t xx = 0; xx < dstWidth; ++xx, rgba0 += 8, rgba1 += 8, dst += 16)
  652. {
  653. float xyz[3];
  654. xyz[0] = rgba0[0];
  655. xyz[1] = rgba0[1];
  656. xyz[2] = rgba0[2];
  657. xyz[0] += rgba0[4];
  658. xyz[1] += rgba0[5];
  659. xyz[2] += rgba0[6];
  660. xyz[0] += rgba1[0];
  661. xyz[1] += rgba1[1];
  662. xyz[2] += rgba1[2];
  663. xyz[0] += rgba1[4];
  664. xyz[1] += rgba1[5];
  665. xyz[2] += rgba1[6];
  666. bx::vec3Norm( (float*)dst, xyz);
  667. }
  668. }
  669. }
  670. void imageRgba32fDownsample2x2NormalMap(void* _dst, uint32_t _width, uint32_t _height, uint32_t _srcPitch, uint32_t _dstPitch, const void* _src)
  671. {
  672. imageRgba32fDownsample2x2NormalMapRef(_dst, _width, _height, _srcPitch, _dstPitch, _src);
  673. }
  674. void imageSwizzleBgra8Ref(void* _dst, uint32_t _dstPitch, uint32_t _width, uint32_t _height, const void* _src, uint32_t _srcPitch)
  675. {
  676. const uint8_t* srcData = (uint8_t*) _src;
  677. uint8_t* dstData = (uint8_t*)_dst;
  678. for (uint32_t yy = 0; yy < _height; ++yy, srcData += _srcPitch, dstData += _dstPitch)
  679. {
  680. const uint8_t* src = srcData;
  681. uint8_t* dst = dstData;
  682. for (uint32_t xx = 0; xx < _width; ++xx, src += 4, dst += 4)
  683. {
  684. uint8_t rr = src[0];
  685. uint8_t gg = src[1];
  686. uint8_t bb = src[2];
  687. uint8_t aa = src[3];
  688. dst[0] = bb;
  689. dst[1] = gg;
  690. dst[2] = rr;
  691. dst[3] = aa;
  692. }
  693. }
  694. }
  695. void imageSwizzleBgra8(void* _dst, uint32_t _dstPitch, uint32_t _width, uint32_t _height, const void* _src, uint32_t _srcPitch)
  696. {
  697. // Test can we do four 4-byte pixels at the time.
  698. if (0 != (_width&0x3)
  699. || _width < 4
  700. || !bx::isAligned(_src, 16)
  701. || !bx::isAligned(_dst, 16) )
  702. {
  703. BX_WARN(false, "Image swizzle is taking slow path.");
  704. BX_WARN(bx::isAligned(_src, 16), "Source %p is not 16-byte aligned.", _src);
  705. BX_WARN(bx::isAligned(_dst, 16), "Destination %p is not 16-byte aligned.", _dst);
  706. BX_WARN(_width < 4, "Image width must be multiple of 4 (width %d).", _width);
  707. imageSwizzleBgra8Ref(_dst, _dstPitch, _width, _height, _src, _srcPitch);
  708. return;
  709. }
  710. using namespace bx;
  711. const simd128_t mf0f0 = simd_isplat(0xff00ff00);
  712. const simd128_t m0f0f = simd_isplat(0x00ff00ff);
  713. const uint32_t width = _width/4;
  714. const uint8_t* srcData = (uint8_t*) _src;
  715. uint8_t* dstData = (uint8_t*)_dst;
  716. for (uint32_t yy = 0; yy < _height; ++yy, srcData += _srcPitch, dstData += _dstPitch)
  717. {
  718. const uint8_t* src = srcData;
  719. uint8_t* dst = dstData;
  720. for (uint32_t xx = 0; xx < width; ++xx, src += 16, dst += 16)
  721. {
  722. const simd128_t tabgr = simd_ld(src);
  723. const simd128_t t00ab = simd_srl(tabgr, 16);
  724. const simd128_t tgr00 = simd_sll(tabgr, 16);
  725. const simd128_t tgrab = simd_or(t00ab, tgr00);
  726. const simd128_t ta0g0 = simd_and(tabgr, mf0f0);
  727. const simd128_t t0r0b = simd_and(tgrab, m0f0f);
  728. const simd128_t targb = simd_or(ta0g0, t0r0b);
  729. simd_st(dst, targb);
  730. }
  731. }
  732. }
  733. void imageCopy(void* _dst, uint32_t _height, uint32_t _srcPitch, uint32_t _depth, const void* _src, uint32_t _dstPitch)
  734. {
  735. const uint32_t pitch = bx::uint32_min(_srcPitch, _dstPitch);
  736. const uint8_t* src = (uint8_t*)_src;
  737. uint8_t* dst = (uint8_t*)_dst;
  738. for (uint32_t zz = 0; zz < _depth; ++zz, src += _srcPitch*_height, dst += _dstPitch*_height)
  739. {
  740. bx::memCopy(dst, src, pitch, _height, _srcPitch, _dstPitch);
  741. }
  742. }
  743. void imageCopy(void* _dst, uint32_t _width, uint32_t _height, uint32_t _depth, uint32_t _bpp, uint32_t _srcPitch, const void* _src)
  744. {
  745. const uint32_t dstPitch = _width*_bpp/8;
  746. imageCopy(_dst, _height, _srcPitch, _depth, _src, dstPitch);
  747. }
  748. struct PackUnpack
  749. {
  750. PackFn pack;
  751. UnpackFn unpack;
  752. };
  753. static const PackUnpack s_packUnpack[] =
  754. {
  755. { NULL, NULL }, // BC1
  756. { NULL, NULL }, // BC2
  757. { NULL, NULL }, // BC3
  758. { NULL, NULL }, // BC4
  759. { NULL, NULL }, // BC5
  760. { NULL, NULL }, // BC6H
  761. { NULL, NULL }, // BC7
  762. { NULL, NULL }, // ETC1
  763. { NULL, NULL }, // ETC2
  764. { NULL, NULL }, // ETC2A
  765. { NULL, NULL }, // ETC2A1
  766. { NULL, NULL }, // PTC12
  767. { NULL, NULL }, // PTC14
  768. { NULL, NULL }, // PTC12A
  769. { NULL, NULL }, // PTC14A
  770. { NULL, NULL }, // PTC22
  771. { NULL, NULL }, // PTC24
  772. { NULL, NULL }, // ATC
  773. { NULL, NULL }, // ATCE
  774. { NULL, NULL }, // ATCI
  775. { NULL, NULL }, // ASTC4x4
  776. { NULL, NULL }, // ASTC5x5
  777. { NULL, NULL }, // ASTC6x6
  778. { NULL, NULL }, // ASTC8x5
  779. { NULL, NULL }, // ASTC8x6
  780. { NULL, NULL }, // ASTC10x5
  781. { NULL, NULL }, // Unknown
  782. { NULL, NULL }, // R1
  783. { bx::packR8, bx::unpackR8 }, // A8
  784. { bx::packR8, bx::unpackR8 }, // R8
  785. { bx::packR8I, bx::unpackR8I }, // R8I
  786. { bx::packR8U, bx::unpackR8U }, // R8U
  787. { bx::packR8S, bx::unpackR8S }, // R8S
  788. { bx::packR16, bx::unpackR16 }, // R16
  789. { bx::packR16I, bx::unpackR16I }, // R16I
  790. { bx::packR16U, bx::unpackR16U }, // R16U
  791. { bx::packR16F, bx::unpackR16F }, // R16F
  792. { bx::packR16S, bx::unpackR16S }, // R16S
  793. { bx::packR32I, bx::unpackR32I }, // R32I
  794. { bx::packR32U, bx::unpackR32U }, // R32U
  795. { bx::packR32F, bx::unpackR32F }, // R32F
  796. { bx::packRg8, bx::unpackRg8 }, // RG8
  797. { bx::packRg8I, bx::unpackRg8I }, // RG8I
  798. { bx::packRg8U, bx::unpackRg8U }, // RG8U
  799. { bx::packRg8S, bx::unpackRg8S }, // RG8S
  800. { bx::packRg16, bx::unpackRg16 }, // RG16
  801. { bx::packRg16I, bx::unpackRg16I }, // RG16I
  802. { bx::packRg16U, bx::unpackRg16U }, // RG16U
  803. { bx::packRg16F, bx::unpackRg16F }, // RG16F
  804. { bx::packRg16S, bx::unpackRg16S }, // RG16S
  805. { bx::packRg32I, bx::unpackRg32I }, // RG32I
  806. { bx::packRg32U, bx::unpackRg32U }, // RG32U
  807. { bx::packRg32F, bx::unpackRg32F }, // RG32F
  808. { bx::packRgb8, bx::unpackRgb8 }, // RGB8
  809. { bx::packRgb8S, bx::unpackRgb8S }, // RGB8S
  810. { bx::packRgb8I, bx::unpackRgb8I }, // RGB8I
  811. { bx::packRgb8U, bx::unpackRgb8U }, // RGB8U
  812. { bx::packRgb9E5F, bx::unpackRgb9E5F }, // RGB9E5F
  813. { bx::packBgra8, bx::unpackBgra8 }, // BGRA8
  814. { bx::packRgba8, bx::unpackRgba8 }, // RGBA8
  815. { bx::packRgba8I, bx::unpackRgba8I }, // RGBA8I
  816. { bx::packRgba8U, bx::unpackRgba8U }, // RGBA8U
  817. { bx::packRgba8S, bx::unpackRgba8S }, // RGBA8S
  818. { bx::packRgba16, bx::unpackRgba16 }, // RGBA16
  819. { bx::packRgba16I, bx::unpackRgba16I }, // RGBA16I
  820. { bx::packRgba16U, bx::unpackRgba16U }, // RGBA16U
  821. { bx::packRgba16F, bx::unpackRgba16F }, // RGBA16F
  822. { bx::packRgba16S, bx::unpackRgba16S }, // RGBA16S
  823. { bx::packRgba32I, bx::unpackRgba32I }, // RGBA32I
  824. { bx::packRgba32U, bx::unpackRgba32U }, // RGBA32U
  825. { bx::packRgba32F, bx::unpackRgba32F }, // RGBA32F
  826. { bx::packR5G6B5, bx::unpackR5G6B5 }, // R5G6B5
  827. { bx::packRgba4, bx::unpackRgba4 }, // RGBA4
  828. { bx::packRgb5a1, bx::unpackRgb5a1 }, // RGB5A1
  829. { bx::packRgb10A2, bx::unpackRgb10A2 }, // RGB10A2
  830. { bx::packRG11B10F, bx::unpackRG11B10F }, // RG11B10F
  831. { NULL, NULL }, // UnknownDepth
  832. { bx::packR16, bx::unpackR16 }, // D16
  833. { bx::packR24, bx::unpackR24 }, // D24
  834. { bx::packR24G8, bx::unpackR24G8 }, // D24S8
  835. { NULL, NULL }, // D32
  836. { bx::packR16F, bx::unpackR16F }, // D16F
  837. { NULL, NULL }, // D24F
  838. { bx::packR32F, bx::unpackR32F }, // D32F
  839. { bx::packR8, bx::unpackR8 }, // D0S8
  840. };
  841. BX_STATIC_ASSERT(TextureFormat::Count == BX_COUNTOF(s_packUnpack) );
  842. PackFn getPack(TextureFormat::Enum _format)
  843. {
  844. return s_packUnpack[_format].pack;
  845. }
  846. UnpackFn getUnpack(TextureFormat::Enum _format)
  847. {
  848. return s_packUnpack[_format].unpack;
  849. }
  850. bool imageConvert(TextureFormat::Enum _dstFormat, TextureFormat::Enum _srcFormat)
  851. {
  852. UnpackFn unpack = s_packUnpack[_srcFormat].unpack;
  853. PackFn pack = s_packUnpack[_dstFormat].pack;
  854. return NULL != pack
  855. && NULL != unpack
  856. ;
  857. }
  858. void imageConvert(void* _dst, uint32_t _bpp, PackFn _pack, const void* _src, UnpackFn _unpack, uint32_t _size)
  859. {
  860. const uint8_t* src = (uint8_t*)_src;
  861. uint8_t* dst = (uint8_t*)_dst;
  862. const uint32_t size = _size * 8 / _bpp;
  863. for (uint32_t ii = 0; ii < size; ++ii)
  864. {
  865. float rgba[4];
  866. _unpack(rgba, &src[ii*_bpp/8]);
  867. _pack(&dst[ii*_bpp/8], rgba);
  868. }
  869. }
  870. 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)
  871. {
  872. const uint8_t* src = (uint8_t*)_src;
  873. uint8_t* dst = (uint8_t*)_dst;
  874. const uint32_t dstPitch = _width * _dstBpp / 8;
  875. for (uint32_t zz = 0; zz < _depth; ++zz)
  876. {
  877. for (uint32_t yy = 0; yy < _height; ++yy, src += _srcPitch, dst += dstPitch)
  878. {
  879. for (uint32_t xx = 0; xx < _width; ++xx)
  880. {
  881. float rgba[4];
  882. _unpack(rgba, &src[xx*_srcBpp/8]);
  883. _pack(&dst[xx*_dstBpp/8], rgba);
  884. }
  885. }
  886. }
  887. }
  888. bool imageConvert(void* _dst, TextureFormat::Enum _dstFormat, const void* _src, TextureFormat::Enum _srcFormat, uint32_t _width, uint32_t _height, uint32_t _depth, uint32_t _srcPitch)
  889. {
  890. UnpackFn unpack = s_packUnpack[_srcFormat].unpack;
  891. PackFn pack = s_packUnpack[_dstFormat].pack;
  892. if (NULL == pack
  893. || NULL == unpack)
  894. {
  895. return false;
  896. }
  897. const uint32_t srcBpp = s_imageBlockInfo[_srcFormat].bitsPerPixel;
  898. const uint32_t dstBpp = s_imageBlockInfo[_dstFormat].bitsPerPixel;
  899. imageConvert(_dst, dstBpp, pack, _src, srcBpp, unpack, _width, _height, _depth, _srcPitch);
  900. return true;
  901. }
  902. bool imageConvert(void* _dst, TextureFormat::Enum _dstFormat, const void* _src, TextureFormat::Enum _srcFormat, uint32_t _width, uint32_t _height, uint32_t _depth)
  903. {
  904. const uint32_t srcBpp = s_imageBlockInfo[_srcFormat].bitsPerPixel;
  905. if (_dstFormat == _srcFormat)
  906. {
  907. bx::memCopy(_dst, _src, _width*_height*_depth*srcBpp/8);
  908. return true;
  909. }
  910. return imageConvert(_dst, _dstFormat, _src, _srcFormat, _width, _height, _depth, _width*srcBpp/8);
  911. }
  912. ImageContainer* imageConvert(bx::AllocatorI* _allocator, TextureFormat::Enum _dstFormat, const ImageContainer& _input)
  913. {
  914. ImageContainer* output = imageAlloc(_allocator
  915. , _dstFormat
  916. , uint16_t(_input.m_width)
  917. , uint16_t(_input.m_height)
  918. , uint16_t(_input.m_depth)
  919. , _input.m_numLayers
  920. , _input.m_cubeMap
  921. , 1 < _input.m_numMips
  922. );
  923. const uint16_t numSides = _input.m_numLayers * (_input.m_cubeMap ? 6 : 1);
  924. for (uint16_t side = 0; side < numSides; ++side)
  925. {
  926. for (uint8_t lod = 0, num = _input.m_numMips; lod < num; ++lod)
  927. {
  928. ImageMip mip;
  929. if (imageGetRawData(_input, side, lod, _input.m_data, _input.m_size, mip) )
  930. {
  931. ImageMip dstMip;
  932. imageGetRawData(*output, side, lod, output->m_data, output->m_size, dstMip);
  933. uint8_t* dstData = const_cast<uint8_t*>(dstMip.m_data);
  934. bool ok = imageConvert(dstData
  935. , _dstFormat
  936. , mip.m_data
  937. , mip.m_format
  938. , mip.m_width
  939. , mip.m_height
  940. , mip.m_depth
  941. );
  942. BX_CHECK(ok, "Conversion from %s to %s failed!"
  943. , getName(_input.m_format)
  944. , getName(output->m_format)
  945. );
  946. BX_UNUSED(ok);
  947. }
  948. }
  949. }
  950. return output;
  951. }
  952. typedef bool (*ParseFn)(ImageContainer&, bx::ReaderSeekerI*, bx::Error*);
  953. template<uint32_t magicT, ParseFn parseFnT>
  954. ImageContainer* imageParseT(bx::AllocatorI* _allocator, const void* _src, uint32_t _size, bx::Error* _err)
  955. {
  956. bx::MemoryReader reader(_src, _size);
  957. uint32_t magic;
  958. bx::read(&reader, magic);
  959. ImageContainer imageContainer;
  960. if (magicT != magic
  961. || !parseFnT(imageContainer, &reader, _err) )
  962. {
  963. return NULL;
  964. }
  965. ImageContainer* output = imageAlloc(_allocator
  966. , imageContainer.m_format
  967. , uint16_t(imageContainer.m_width)
  968. , uint16_t(imageContainer.m_height)
  969. , uint16_t(imageContainer.m_depth)
  970. , imageContainer.m_numLayers
  971. , imageContainer.m_cubeMap
  972. , 1 < imageContainer.m_numMips
  973. );
  974. const uint16_t numSides = imageContainer.m_numLayers * (imageContainer.m_cubeMap ? 6 : 1);
  975. for (uint16_t side = 0; side < numSides; ++side)
  976. {
  977. for (uint8_t lod = 0, num = imageContainer.m_numMips; lod < num; ++lod)
  978. {
  979. ImageMip dstMip;
  980. if (imageGetRawData(*output, side, lod, output->m_data, output->m_size, dstMip) )
  981. {
  982. ImageMip mip;
  983. if (imageGetRawData(imageContainer, side, lod, _src, _size, mip) )
  984. {
  985. uint8_t* dstData = const_cast<uint8_t*>(dstMip.m_data);
  986. bx::memCopy(dstData, mip.m_data, mip.m_size);
  987. }
  988. }
  989. }
  990. }
  991. return output;
  992. }
  993. uint8_t bitRangeConvert(uint32_t _in, uint32_t _from, uint32_t _to)
  994. {
  995. using namespace bx;
  996. uint32_t tmp0 = uint32_sll(1, _to);
  997. uint32_t tmp1 = uint32_sll(1, _from);
  998. uint32_t tmp2 = uint32_dec(tmp0);
  999. uint32_t tmp3 = uint32_dec(tmp1);
  1000. uint32_t tmp4 = uint32_mul(_in, tmp2);
  1001. uint32_t tmp5 = uint32_add(tmp3, tmp4);
  1002. uint32_t tmp6 = uint32_srl(tmp5, _from);
  1003. uint32_t tmp7 = uint32_add(tmp5, tmp6);
  1004. uint32_t result = uint32_srl(tmp7, _from);
  1005. return uint8_t(result);
  1006. }
  1007. void decodeBlockDxt(uint8_t _dst[16*4], const uint8_t _src[8])
  1008. {
  1009. uint8_t colors[4*3];
  1010. uint32_t c0 = _src[0] | (_src[1] << 8);
  1011. colors[0] = bitRangeConvert( (c0>> 0)&0x1f, 5, 8);
  1012. colors[1] = bitRangeConvert( (c0>> 5)&0x3f, 6, 8);
  1013. colors[2] = bitRangeConvert( (c0>>11)&0x1f, 5, 8);
  1014. uint32_t c1 = _src[2] | (_src[3] << 8);
  1015. colors[3] = bitRangeConvert( (c1>> 0)&0x1f, 5, 8);
  1016. colors[4] = bitRangeConvert( (c1>> 5)&0x3f, 6, 8);
  1017. colors[5] = bitRangeConvert( (c1>>11)&0x1f, 5, 8);
  1018. colors[6] = (2*colors[0] + colors[3]) / 3;
  1019. colors[7] = (2*colors[1] + colors[4]) / 3;
  1020. colors[8] = (2*colors[2] + colors[5]) / 3;
  1021. colors[ 9] = (colors[0] + 2*colors[3]) / 3;
  1022. colors[10] = (colors[1] + 2*colors[4]) / 3;
  1023. colors[11] = (colors[2] + 2*colors[5]) / 3;
  1024. for (uint32_t ii = 0, next = 8*4; ii < 16*4; ii += 4, next += 2)
  1025. {
  1026. int idx = ( (_src[next>>3] >> (next & 7) ) & 3) * 3;
  1027. _dst[ii+0] = colors[idx+0];
  1028. _dst[ii+1] = colors[idx+1];
  1029. _dst[ii+2] = colors[idx+2];
  1030. }
  1031. }
  1032. void decodeBlockDxt1(uint8_t _dst[16*4], const uint8_t _src[8])
  1033. {
  1034. uint8_t colors[4*4];
  1035. uint32_t c0 = _src[0] | (_src[1] << 8);
  1036. colors[0] = bitRangeConvert( (c0>> 0)&0x1f, 5, 8);
  1037. colors[1] = bitRangeConvert( (c0>> 5)&0x3f, 6, 8);
  1038. colors[2] = bitRangeConvert( (c0>>11)&0x1f, 5, 8);
  1039. colors[3] = 255;
  1040. uint32_t c1 = _src[2] | (_src[3] << 8);
  1041. colors[4] = bitRangeConvert( (c1>> 0)&0x1f, 5, 8);
  1042. colors[5] = bitRangeConvert( (c1>> 5)&0x3f, 6, 8);
  1043. colors[6] = bitRangeConvert( (c1>>11)&0x1f, 5, 8);
  1044. colors[7] = 255;
  1045. if (c0 > c1)
  1046. {
  1047. colors[ 8] = (2*colors[0] + colors[4]) / 3;
  1048. colors[ 9] = (2*colors[1] + colors[5]) / 3;
  1049. colors[10] = (2*colors[2] + colors[6]) / 3;
  1050. colors[11] = 255;
  1051. colors[12] = (colors[0] + 2*colors[4]) / 3;
  1052. colors[13] = (colors[1] + 2*colors[5]) / 3;
  1053. colors[14] = (colors[2] + 2*colors[6]) / 3;
  1054. colors[15] = 255;
  1055. }
  1056. else
  1057. {
  1058. colors[ 8] = (colors[0] + colors[4]) / 2;
  1059. colors[ 9] = (colors[1] + colors[5]) / 2;
  1060. colors[10] = (colors[2] + colors[6]) / 2;
  1061. colors[11] = 255;
  1062. colors[12] = 0;
  1063. colors[13] = 0;
  1064. colors[14] = 0;
  1065. colors[15] = 0;
  1066. }
  1067. for (uint32_t ii = 0, next = 8*4; ii < 16*4; ii += 4, next += 2)
  1068. {
  1069. int idx = ( (_src[next>>3] >> (next & 7) ) & 3) * 4;
  1070. _dst[ii+0] = colors[idx+0];
  1071. _dst[ii+1] = colors[idx+1];
  1072. _dst[ii+2] = colors[idx+2];
  1073. _dst[ii+3] = colors[idx+3];
  1074. }
  1075. }
  1076. void decodeBlockDxt23A(uint8_t _dst[16*4], const uint8_t _src[8])
  1077. {
  1078. for (uint32_t ii = 0, next = 0; ii < 16*4; ii += 4, next += 4)
  1079. {
  1080. uint32_t c0 = (_src[next>>3] >> (next&7) ) & 0xf;
  1081. _dst[ii] = bitRangeConvert(c0, 4, 8);
  1082. }
  1083. }
  1084. void decodeBlockDxt45A(uint8_t _dst[16*4], const uint8_t _src[8])
  1085. {
  1086. uint8_t alpha[8];
  1087. alpha[0] = _src[0];
  1088. alpha[1] = _src[1];
  1089. if (alpha[0] > alpha[1])
  1090. {
  1091. alpha[2] = (6*alpha[0] + 1*alpha[1]) / 7;
  1092. alpha[3] = (5*alpha[0] + 2*alpha[1]) / 7;
  1093. alpha[4] = (4*alpha[0] + 3*alpha[1]) / 7;
  1094. alpha[5] = (3*alpha[0] + 4*alpha[1]) / 7;
  1095. alpha[6] = (2*alpha[0] + 5*alpha[1]) / 7;
  1096. alpha[7] = (1*alpha[0] + 6*alpha[1]) / 7;
  1097. }
  1098. else
  1099. {
  1100. alpha[2] = (4*alpha[0] + 1*alpha[1]) / 5;
  1101. alpha[3] = (3*alpha[0] + 2*alpha[1]) / 5;
  1102. alpha[4] = (2*alpha[0] + 3*alpha[1]) / 5;
  1103. alpha[5] = (1*alpha[0] + 4*alpha[1]) / 5;
  1104. alpha[6] = 0;
  1105. alpha[7] = 255;
  1106. }
  1107. uint32_t idx0 = _src[2];
  1108. uint32_t idx1 = _src[5];
  1109. idx0 |= uint32_t(_src[3])<<8;
  1110. idx1 |= uint32_t(_src[6])<<8;
  1111. idx0 |= uint32_t(_src[4])<<16;
  1112. idx1 |= uint32_t(_src[7])<<16;
  1113. for (uint32_t ii = 0; ii < 8*4; ii += 4)
  1114. {
  1115. _dst[ii] = alpha[idx0&7];
  1116. _dst[ii+32] = alpha[idx1&7];
  1117. idx0 >>= 3;
  1118. idx1 >>= 3;
  1119. }
  1120. }
  1121. void decodeBlockATC(uint8_t _dst[16*4], const uint8_t _src[8])
  1122. {
  1123. uint8_t colors[4*4]; // You can see from comparison with decodeBlockDXT just how little sense the ATI patent-avoiding(?) modification makes
  1124. uint32_t c0 = _src[0] | (_src[1] << 8);
  1125. uint32_t c1 = _src[2] | (_src[3] << 8);
  1126. if ((c0 & 0x8000) == 0)
  1127. {
  1128. colors[0] = bitRangeConvert( (c0>> 0)&0x1f, 5, 8);
  1129. colors[1] = bitRangeConvert( (c0>> 5)&0x1f, 5, 8);
  1130. colors[2] = bitRangeConvert( (c0>>10)&0x1f, 5, 8);
  1131. colors[12] = bitRangeConvert( (c1>> 0)&0x1f, 5, 8);
  1132. colors[13] = bitRangeConvert( (c1>> 5)&0x3f, 6, 8);
  1133. colors[14] = bitRangeConvert( (c1>>11)&0x1f, 5, 8);
  1134. colors[ 4] = (2 * colors[0] + colors[12]) / 3;
  1135. colors[ 5] = (2 * colors[1] + colors[13]) / 3;
  1136. colors[ 6] = (2 * colors[2] + colors[14]) / 3;
  1137. colors[ 8] = (colors[0] + 2 * colors[12]) / 3;
  1138. colors[ 9] = (colors[1] + 2 * colors[13]) / 3;
  1139. colors[10] = (colors[2] + 2 * colors[14]) / 3;
  1140. }
  1141. else
  1142. {
  1143. colors[ 0] = 0;
  1144. colors[ 1] = 0;
  1145. colors[ 2] = 0;
  1146. colors[ 8] = bitRangeConvert( (c0>> 0)&0x1f, 5, 8);
  1147. colors[ 9] = bitRangeConvert( (c0>> 5)&0x1f, 5, 8);
  1148. colors[10] = bitRangeConvert( (c0>>10)&0x1f, 5, 8);
  1149. colors[12] = bitRangeConvert( (c1>> 0)&0x1f, 5, 8);
  1150. colors[13] = bitRangeConvert( (c1>> 5)&0x3f, 6, 8);
  1151. colors[14] = bitRangeConvert( (c1>>11)&0x1f, 5, 8);
  1152. colors[ 4] = colors[ 8] - colors[12] / 4;
  1153. colors[ 5] = colors[ 9] - colors[13] / 4;
  1154. colors[ 6] = colors[10] - colors[14] / 4;
  1155. }
  1156. for (uint32_t ii = 0, next = 8*4; ii < 16*4; ii += 4, next += 2)
  1157. {
  1158. int idx = ( (_src[next>>3] >> (next & 7) ) & 3) * 4;
  1159. _dst[ii+0] = colors[idx+0];
  1160. _dst[ii+1] = colors[idx+1];
  1161. _dst[ii+2] = colors[idx+2];
  1162. _dst[ii+3] = colors[idx+3];
  1163. }
  1164. }
  1165. static const int32_t s_etc1Mod[8][4] =
  1166. {
  1167. { 2, 8, -2, -8},
  1168. { 5, 17, -5, -17},
  1169. { 9, 29, -9, -29},
  1170. { 13, 42, -13, -42},
  1171. { 18, 60, -18, -60},
  1172. { 24, 80, -24, -80},
  1173. { 33, 106, -33, -106},
  1174. { 47, 183, -47, -183},
  1175. };
  1176. static const uint8_t s_etc2Mod[8] = { 3, 6, 11, 16, 23, 32, 41, 64 };
  1177. uint8_t uint8_sat(int32_t _a)
  1178. {
  1179. using namespace bx;
  1180. const uint32_t min = uint32_imin(_a, 255);
  1181. const uint32_t result = uint32_imax(min, 0);
  1182. return (uint8_t)result;
  1183. }
  1184. uint8_t uint8_satadd(int32_t _a, int32_t _b)
  1185. {
  1186. const int32_t add = _a + _b;
  1187. return uint8_sat(add);
  1188. }
  1189. void decodeBlockEtc2ModeT(uint8_t _dst[16*4], const uint8_t _src[8])
  1190. {
  1191. uint8_t rgb[16];
  1192. // 0 1 2 3 4 5 6 7
  1193. // 7654321076543210765432107654321076543210765432107654321076543210
  1194. // ...rr.rrggggbbbbrrrrggggbbbbDDD.mmmmmmmmmmmmmmmmllllllllllllllll
  1195. // ^ ^ ^ ^ ^
  1196. // +-- c0 +-- c1 | +-- msb +-- lsb
  1197. // +-- dist
  1198. rgb[ 0] = ( (_src[0] >> 1) & 0xc)
  1199. | (_src[0] & 0x3)
  1200. ;
  1201. rgb[ 1] = _src[1] >> 4;
  1202. rgb[ 2] = _src[1] & 0xf;
  1203. rgb[ 8] = _src[2] >> 4;
  1204. rgb[ 9] = _src[2] & 0xf;
  1205. rgb[10] = _src[3] >> 4;
  1206. rgb[ 0] = bitRangeConvert(rgb[ 0], 4, 8);
  1207. rgb[ 1] = bitRangeConvert(rgb[ 1], 4, 8);
  1208. rgb[ 2] = bitRangeConvert(rgb[ 2], 4, 8);
  1209. rgb[ 8] = bitRangeConvert(rgb[ 8], 4, 8);
  1210. rgb[ 9] = bitRangeConvert(rgb[ 9], 4, 8);
  1211. rgb[10] = bitRangeConvert(rgb[10], 4, 8);
  1212. uint8_t dist = (_src[3] >> 1) & 0x7;
  1213. int32_t mod = s_etc2Mod[dist];
  1214. rgb[ 4] = uint8_satadd(rgb[ 8], mod);
  1215. rgb[ 5] = uint8_satadd(rgb[ 9], mod);
  1216. rgb[ 6] = uint8_satadd(rgb[10], mod);
  1217. rgb[12] = uint8_satadd(rgb[ 8], -mod);
  1218. rgb[13] = uint8_satadd(rgb[ 9], -mod);
  1219. rgb[14] = uint8_satadd(rgb[10], -mod);
  1220. uint32_t indexMsb = (_src[4]<<8) | _src[5];
  1221. uint32_t indexLsb = (_src[6]<<8) | _src[7];
  1222. for (uint32_t ii = 0; ii < 16; ++ii)
  1223. {
  1224. const uint32_t idx = (ii&0xc) | ( (ii & 0x3)<<4);
  1225. const uint32_t lsbi = indexLsb & 1;
  1226. const uint32_t msbi = (indexMsb & 1)<<1;
  1227. const uint32_t pal = (lsbi | msbi)<<2;
  1228. _dst[idx + 0] = rgb[pal+2];
  1229. _dst[idx + 1] = rgb[pal+1];
  1230. _dst[idx + 2] = rgb[pal+0];
  1231. _dst[idx + 3] = 255;
  1232. indexLsb >>= 1;
  1233. indexMsb >>= 1;
  1234. }
  1235. }
  1236. void decodeBlockEtc2ModeH(uint8_t _dst[16*4], const uint8_t _src[8])
  1237. {
  1238. uint8_t rgb[16];
  1239. // 0 1 2 3 4 5 6 7
  1240. // 7654321076543210765432107654321076543210765432107654321076543210
  1241. // .rrrrggg...gb.bbbrrrrggggbbbbDD.mmmmmmmmmmmmmmmmllllllllllllllll
  1242. // ^ ^ ^ ^ ^
  1243. // +-- c0 +-- c1 | +-- msb +-- lsb
  1244. // +-- dist
  1245. rgb[ 0] = (_src[0] >> 3) & 0xf;
  1246. rgb[ 1] = ( (_src[0] << 1) & 0xe)
  1247. | ( (_src[1] >> 4) & 0x1)
  1248. ;
  1249. rgb[ 2] = (_src[1] & 0x8)
  1250. | ( (_src[1] << 1) & 0x6)
  1251. | (_src[2] >> 7)
  1252. ;
  1253. rgb[ 8] = (_src[2] >> 3) & 0xf;
  1254. rgb[ 9] = ( (_src[2] << 1) & 0xe)
  1255. | (_src[3] >> 7)
  1256. ;
  1257. rgb[10] = (_src[2] >> 3) & 0xf;
  1258. rgb[ 0] = bitRangeConvert(rgb[ 0], 4, 8);
  1259. rgb[ 1] = bitRangeConvert(rgb[ 1], 4, 8);
  1260. rgb[ 2] = bitRangeConvert(rgb[ 2], 4, 8);
  1261. rgb[ 8] = bitRangeConvert(rgb[ 8], 4, 8);
  1262. rgb[ 9] = bitRangeConvert(rgb[ 9], 4, 8);
  1263. rgb[10] = bitRangeConvert(rgb[10], 4, 8);
  1264. uint32_t col0 = uint32_t(rgb[0]<<16) | uint32_t(rgb[1]<<8) | uint32_t(rgb[ 2]);
  1265. uint32_t col1 = uint32_t(rgb[8]<<16) | uint32_t(rgb[9]<<8) | uint32_t(rgb[10]);
  1266. uint8_t dist = (_src[3] & 0x6) | (col0 >= col1);
  1267. int32_t mod = s_etc2Mod[dist];
  1268. rgb[ 4] = uint8_satadd(rgb[ 0], -mod);
  1269. rgb[ 5] = uint8_satadd(rgb[ 1], -mod);
  1270. rgb[ 6] = uint8_satadd(rgb[ 2], -mod);
  1271. rgb[ 0] = uint8_satadd(rgb[ 0], mod);
  1272. rgb[ 1] = uint8_satadd(rgb[ 1], mod);
  1273. rgb[ 2] = uint8_satadd(rgb[ 2], mod);
  1274. rgb[12] = uint8_satadd(rgb[ 8], -mod);
  1275. rgb[13] = uint8_satadd(rgb[ 9], -mod);
  1276. rgb[14] = uint8_satadd(rgb[10], -mod);
  1277. rgb[ 8] = uint8_satadd(rgb[ 8], mod);
  1278. rgb[ 9] = uint8_satadd(rgb[ 9], mod);
  1279. rgb[10] = uint8_satadd(rgb[10], mod);
  1280. uint32_t indexMsb = (_src[4]<<8) | _src[5];
  1281. uint32_t indexLsb = (_src[6]<<8) | _src[7];
  1282. for (uint32_t ii = 0; ii < 16; ++ii)
  1283. {
  1284. const uint32_t idx = (ii&0xc) | ( (ii & 0x3)<<4);
  1285. const uint32_t lsbi = indexLsb & 1;
  1286. const uint32_t msbi = (indexMsb & 1)<<1;
  1287. const uint32_t pal = (lsbi | msbi)<<2;
  1288. _dst[idx + 0] = rgb[pal+2];
  1289. _dst[idx + 1] = rgb[pal+1];
  1290. _dst[idx + 2] = rgb[pal+0];
  1291. _dst[idx + 3] = 255;
  1292. indexLsb >>= 1;
  1293. indexMsb >>= 1;
  1294. }
  1295. }
  1296. void decodeBlockEtc2ModePlanar(uint8_t _dst[16*4], const uint8_t _src[8])
  1297. {
  1298. // 0 1 2 3 4 5 6 7
  1299. // 7654321076543210765432107654321076543210765432107654321076543210
  1300. // .rrrrrrg.ggggggb...bb.bbbrrrrr.rgggggggbbbbbbrrrrrrgggggggbbbbbb
  1301. // ^ ^ ^
  1302. // +-- c0 +-- cH +-- cV
  1303. uint8_t c0[3];
  1304. uint8_t cH[3];
  1305. uint8_t cV[3];
  1306. c0[0] = (_src[0] >> 1) & 0x3f;
  1307. c0[1] = ( (_src[0] & 1) << 6)
  1308. | ( (_src[1] >> 1) & 0x3f)
  1309. ;
  1310. c0[2] = ( (_src[1] & 1) << 5)
  1311. | ( (_src[2] & 0x18) )
  1312. | ( (_src[2] << 1) & 6)
  1313. | ( (_src[3] >> 7) )
  1314. ;
  1315. cH[0] = ( (_src[3] >> 1) & 0x3e)
  1316. | (_src[3] & 1)
  1317. ;
  1318. cH[1] = _src[4] >> 1;
  1319. cH[2] = ( (_src[4] & 1) << 5)
  1320. | (_src[5] >> 3)
  1321. ;
  1322. cV[0] = ( (_src[5] & 0x7) << 3)
  1323. | (_src[6] >> 5)
  1324. ;
  1325. cV[1] = ( (_src[6] & 0x1f) << 2)
  1326. | (_src[7] >> 5)
  1327. ;
  1328. cV[2] = _src[7] & 0x3f;
  1329. c0[0] = bitRangeConvert(c0[0], 6, 8);
  1330. c0[1] = bitRangeConvert(c0[1], 7, 8);
  1331. c0[2] = bitRangeConvert(c0[2], 6, 8);
  1332. cH[0] = bitRangeConvert(cH[0], 6, 8);
  1333. cH[1] = bitRangeConvert(cH[1], 7, 8);
  1334. cH[2] = bitRangeConvert(cH[2], 6, 8);
  1335. cV[0] = bitRangeConvert(cV[0], 6, 8);
  1336. cV[1] = bitRangeConvert(cV[1], 7, 8);
  1337. cV[2] = bitRangeConvert(cV[2], 6, 8);
  1338. int16_t dy[3];
  1339. dy[0] = cV[0] - c0[0];
  1340. dy[1] = cV[1] - c0[1];
  1341. dy[2] = cV[2] - c0[2];
  1342. int16_t sx[3];
  1343. sx[0] = int16_t(c0[0])<<2;
  1344. sx[1] = int16_t(c0[1])<<2;
  1345. sx[2] = int16_t(c0[2])<<2;
  1346. int16_t ex[3];
  1347. ex[0] = int16_t(cH[0])<<2;
  1348. ex[1] = int16_t(cH[1])<<2;
  1349. ex[2] = int16_t(cH[2])<<2;
  1350. for (int32_t vv = 0; vv < 4; ++vv)
  1351. {
  1352. int16_t dx[3];
  1353. dx[0] = (ex[0] - sx[0])>>2;
  1354. dx[1] = (ex[1] - sx[1])>>2;
  1355. dx[2] = (ex[2] - sx[2])>>2;
  1356. for (int32_t hh = 0; hh < 4; ++hh)
  1357. {
  1358. const uint32_t idx = (vv<<4) + (hh<<2);
  1359. _dst[idx + 0] = uint8_sat( (sx[2] + dx[2]*hh)>>2);
  1360. _dst[idx + 1] = uint8_sat( (sx[1] + dx[1]*hh)>>2);
  1361. _dst[idx + 2] = uint8_sat( (sx[0] + dx[0]*hh)>>2);
  1362. _dst[idx + 3] = 255;
  1363. }
  1364. sx[0] += dy[0];
  1365. sx[1] += dy[1];
  1366. sx[2] += dy[2];
  1367. ex[0] += dy[0];
  1368. ex[1] += dy[1];
  1369. ex[2] += dy[2];
  1370. }
  1371. }
  1372. void decodeBlockEtc12(uint8_t _dst[16*4], const uint8_t _src[8])
  1373. {
  1374. bool flipBit = 0 != (_src[3] & 0x1);
  1375. bool diffBit = 0 != (_src[3] & 0x2);
  1376. uint8_t rgb[8];
  1377. if (diffBit)
  1378. {
  1379. rgb[0] = _src[0] >> 3;
  1380. rgb[1] = _src[1] >> 3;
  1381. rgb[2] = _src[2] >> 3;
  1382. int8_t diff[3];
  1383. diff[0] = int8_t( (_src[0] & 0x7)<<5)>>5;
  1384. diff[1] = int8_t( (_src[1] & 0x7)<<5)>>5;
  1385. diff[2] = int8_t( (_src[2] & 0x7)<<5)>>5;
  1386. int8_t rr = rgb[0] + diff[0];
  1387. int8_t gg = rgb[1] + diff[1];
  1388. int8_t bb = rgb[2] + diff[2];
  1389. // Etc2 3-modes
  1390. if (rr < 0 || rr > 31)
  1391. {
  1392. decodeBlockEtc2ModeT(_dst, _src);
  1393. return;
  1394. }
  1395. if (gg < 0 || gg > 31)
  1396. {
  1397. decodeBlockEtc2ModeH(_dst, _src);
  1398. return;
  1399. }
  1400. if (bb < 0 || bb > 31)
  1401. {
  1402. decodeBlockEtc2ModePlanar(_dst, _src);
  1403. return;
  1404. }
  1405. // Etc1
  1406. rgb[0] = bitRangeConvert(rgb[0], 5, 8);
  1407. rgb[1] = bitRangeConvert(rgb[1], 5, 8);
  1408. rgb[2] = bitRangeConvert(rgb[2], 5, 8);
  1409. rgb[4] = bitRangeConvert(rr, 5, 8);
  1410. rgb[5] = bitRangeConvert(gg, 5, 8);
  1411. rgb[6] = bitRangeConvert(bb, 5, 8);
  1412. }
  1413. else
  1414. {
  1415. rgb[0] = _src[0] >> 4;
  1416. rgb[1] = _src[1] >> 4;
  1417. rgb[2] = _src[2] >> 4;
  1418. rgb[4] = _src[0] & 0xf;
  1419. rgb[5] = _src[1] & 0xf;
  1420. rgb[6] = _src[2] & 0xf;
  1421. rgb[0] = bitRangeConvert(rgb[0], 4, 8);
  1422. rgb[1] = bitRangeConvert(rgb[1], 4, 8);
  1423. rgb[2] = bitRangeConvert(rgb[2], 4, 8);
  1424. rgb[4] = bitRangeConvert(rgb[4], 4, 8);
  1425. rgb[5] = bitRangeConvert(rgb[5], 4, 8);
  1426. rgb[6] = bitRangeConvert(rgb[6], 4, 8);
  1427. }
  1428. uint32_t table[2];
  1429. table[0] = (_src[3] >> 5) & 0x7;
  1430. table[1] = (_src[3] >> 2) & 0x7;
  1431. uint32_t indexMsb = (_src[4]<<8) | _src[5];
  1432. uint32_t indexLsb = (_src[6]<<8) | _src[7];
  1433. if (flipBit)
  1434. {
  1435. for (uint32_t ii = 0; ii < 16; ++ii)
  1436. {
  1437. const uint32_t block = (ii>>1)&1;
  1438. const uint32_t color = block<<2;
  1439. const uint32_t idx = (ii&0xc) | ( (ii & 0x3)<<4);
  1440. const uint32_t lsbi = indexLsb & 1;
  1441. const uint32_t msbi = (indexMsb & 1)<<1;
  1442. const int32_t mod = s_etc1Mod[table[block] ][lsbi | msbi];
  1443. _dst[idx + 0] = uint8_satadd(rgb[color+2], mod);
  1444. _dst[idx + 1] = uint8_satadd(rgb[color+1], mod);
  1445. _dst[idx + 2] = uint8_satadd(rgb[color+0], mod);
  1446. _dst[idx + 3] = 255;
  1447. indexLsb >>= 1;
  1448. indexMsb >>= 1;
  1449. }
  1450. }
  1451. else
  1452. {
  1453. for (uint32_t ii = 0; ii < 16; ++ii)
  1454. {
  1455. const uint32_t block = ii>>3;
  1456. const uint32_t color = block<<2;
  1457. const uint32_t idx = (ii&0xc) | ( (ii & 0x3)<<4);
  1458. const uint32_t lsbi = indexLsb & 1;
  1459. const uint32_t msbi = (indexMsb & 1)<<1;
  1460. const int32_t mod = s_etc1Mod[table[block] ][lsbi | msbi];
  1461. _dst[idx + 0] = uint8_satadd(rgb[color+2], mod);
  1462. _dst[idx + 1] = uint8_satadd(rgb[color+1], mod);
  1463. _dst[idx + 2] = uint8_satadd(rgb[color+0], mod);
  1464. _dst[idx + 3] = 255;
  1465. indexLsb >>= 1;
  1466. indexMsb >>= 1;
  1467. }
  1468. }
  1469. }
  1470. static const uint8_t s_pvrtcFactors[16][4] =
  1471. {
  1472. { 4, 4, 4, 4 },
  1473. { 2, 6, 2, 6 },
  1474. { 8, 0, 8, 0 },
  1475. { 6, 2, 6, 2 },
  1476. { 2, 2, 6, 6 },
  1477. { 1, 3, 3, 9 },
  1478. { 4, 0, 12, 0 },
  1479. { 3, 1, 9, 3 },
  1480. { 8, 8, 0, 0 },
  1481. { 4, 12, 0, 0 },
  1482. { 16, 0, 0, 0 },
  1483. { 12, 4, 0, 0 },
  1484. { 6, 6, 2, 2 },
  1485. { 3, 9, 1, 3 },
  1486. { 12, 0, 4, 0 },
  1487. { 9, 3, 3, 1 },
  1488. };
  1489. static const uint8_t s_pvrtcWeights[8][4] =
  1490. {
  1491. { 8, 0, 8, 0 },
  1492. { 5, 3, 5, 3 },
  1493. { 3, 5, 3, 5 },
  1494. { 0, 8, 0, 8 },
  1495. { 8, 0, 8, 0 },
  1496. { 4, 4, 4, 4 },
  1497. { 4, 4, 4, 4 },
  1498. { 0, 8, 0, 8 },
  1499. };
  1500. uint32_t morton2d(uint32_t _x, uint32_t _y)
  1501. {
  1502. using namespace bx;
  1503. const uint32_t tmpx = uint32_part1by1(_x);
  1504. const uint32_t xbits = uint32_sll(tmpx, 1);
  1505. const uint32_t ybits = uint32_part1by1(_y);
  1506. const uint32_t result = uint32_or(xbits, ybits);
  1507. return result;
  1508. }
  1509. uint32_t getColor(const uint8_t _src[8])
  1510. {
  1511. return 0
  1512. | _src[7]<<24
  1513. | _src[6]<<16
  1514. | _src[5]<<8
  1515. | _src[4]
  1516. ;
  1517. }
  1518. void decodeBlockPtc14RgbAddA(uint32_t _block, uint32_t* _r, uint32_t* _g, uint32_t* _b, uint8_t _factor)
  1519. {
  1520. if (0 != (_block & (1<<15) ) )
  1521. {
  1522. *_r += bitRangeConvert( (_block >> 10) & 0x1f, 5, 8) * _factor;
  1523. *_g += bitRangeConvert( (_block >> 5) & 0x1f, 5, 8) * _factor;
  1524. *_b += bitRangeConvert( (_block >> 1) & 0x0f, 4, 8) * _factor;
  1525. }
  1526. else
  1527. {
  1528. *_r += bitRangeConvert( (_block >> 8) & 0xf, 4, 8) * _factor;
  1529. *_g += bitRangeConvert( (_block >> 4) & 0xf, 4, 8) * _factor;
  1530. *_b += bitRangeConvert( (_block >> 1) & 0x7, 3, 8) * _factor;
  1531. }
  1532. }
  1533. void decodeBlockPtc14RgbAddB(uint32_t _block, uint32_t* _r, uint32_t* _g, uint32_t* _b, uint8_t _factor)
  1534. {
  1535. if (0 != (_block & (1<<31) ) )
  1536. {
  1537. *_r += bitRangeConvert( (_block >> 26) & 0x1f, 5, 8) * _factor;
  1538. *_g += bitRangeConvert( (_block >> 21) & 0x1f, 5, 8) * _factor;
  1539. *_b += bitRangeConvert( (_block >> 16) & 0x1f, 5, 8) * _factor;
  1540. }
  1541. else
  1542. {
  1543. *_r += bitRangeConvert( (_block >> 24) & 0xf, 4, 8) * _factor;
  1544. *_g += bitRangeConvert( (_block >> 20) & 0xf, 4, 8) * _factor;
  1545. *_b += bitRangeConvert( (_block >> 16) & 0xf, 4, 8) * _factor;
  1546. }
  1547. }
  1548. void decodeBlockPtc14(uint8_t _dst[16*4], const uint8_t* _src, uint32_t _x, uint32_t _y, uint32_t _width, uint32_t _height)
  1549. {
  1550. // 0 1 2 3 4 5 6 7
  1551. // 7654321076543210765432107654321076543210765432107654321076543210
  1552. // mmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmyrrrrrgggggbbbbbxrrrrrgggggbbbbp
  1553. // ^ ^^ ^^ ^
  1554. // +-- modulation data |+- B color |+- A color |
  1555. // +-- B opaque +-- A opaque |
  1556. // alpha punchthrough --+
  1557. const uint8_t* bc = &_src[morton2d(_x, _y) * 8];
  1558. uint32_t mod = 0
  1559. | bc[3]<<24
  1560. | bc[2]<<16
  1561. | bc[1]<<8
  1562. | bc[0]
  1563. ;
  1564. const bool punchthrough = !!(bc[7] & 1);
  1565. const uint8_t* weightTable = s_pvrtcWeights[4 * punchthrough];
  1566. const uint8_t* factorTable = s_pvrtcFactors[0];
  1567. for (int yy = 0; yy < 4; ++yy)
  1568. {
  1569. const uint32_t yOffset = (yy < 2) ? -1 : 0;
  1570. const uint32_t y0 = (_y + yOffset) % _height;
  1571. const uint32_t y1 = (y0 + 1) % _height;
  1572. for (int xx = 0; xx < 4; ++xx)
  1573. {
  1574. const uint32_t xOffset = (xx < 2) ? -1 : 0;
  1575. const uint32_t x0 = (_x + xOffset) % _width;
  1576. const uint32_t x1 = (x0 + 1) % _width;
  1577. const uint32_t bc0 = getColor(&_src[morton2d(x0, y0) * 8]);
  1578. const uint32_t bc1 = getColor(&_src[morton2d(x1, y0) * 8]);
  1579. const uint32_t bc2 = getColor(&_src[morton2d(x0, y1) * 8]);
  1580. const uint32_t bc3 = getColor(&_src[morton2d(x1, y1) * 8]);
  1581. const uint8_t f0 = factorTable[0];
  1582. const uint8_t f1 = factorTable[1];
  1583. const uint8_t f2 = factorTable[2];
  1584. const uint8_t f3 = factorTable[3];
  1585. uint32_t ar = 0, ag = 0, ab = 0;
  1586. decodeBlockPtc14RgbAddA(bc0, &ar, &ag, &ab, f0);
  1587. decodeBlockPtc14RgbAddA(bc1, &ar, &ag, &ab, f1);
  1588. decodeBlockPtc14RgbAddA(bc2, &ar, &ag, &ab, f2);
  1589. decodeBlockPtc14RgbAddA(bc3, &ar, &ag, &ab, f3);
  1590. uint32_t br = 0, bg = 0, bb = 0;
  1591. decodeBlockPtc14RgbAddB(bc0, &br, &bg, &bb, f0);
  1592. decodeBlockPtc14RgbAddB(bc1, &br, &bg, &bb, f1);
  1593. decodeBlockPtc14RgbAddB(bc2, &br, &bg, &bb, f2);
  1594. decodeBlockPtc14RgbAddB(bc3, &br, &bg, &bb, f3);
  1595. const uint8_t* weight = &weightTable[(mod & 3)*4];
  1596. const uint8_t wa = weight[0];
  1597. const uint8_t wb = weight[1];
  1598. _dst[(yy*4 + xx)*4+0] = uint8_t( (ab * wa + bb * wb) >> 7);
  1599. _dst[(yy*4 + xx)*4+1] = uint8_t( (ag * wa + bg * wb) >> 7);
  1600. _dst[(yy*4 + xx)*4+2] = uint8_t( (ar * wa + br * wb) >> 7);
  1601. _dst[(yy*4 + xx)*4+3] = 255;
  1602. mod >>= 2;
  1603. factorTable += 4;
  1604. }
  1605. }
  1606. }
  1607. void decodeBlockPtc14ARgbaAddA(uint32_t _block, uint32_t* _r, uint32_t* _g, uint32_t* _b, uint32_t* _a, uint8_t _factor)
  1608. {
  1609. if (0 != (_block & (1<<15) ) )
  1610. {
  1611. *_r += bitRangeConvert( (_block >> 10) & 0x1f, 5, 8) * _factor;
  1612. *_g += bitRangeConvert( (_block >> 5) & 0x1f, 5, 8) * _factor;
  1613. *_b += bitRangeConvert( (_block >> 1) & 0x0f, 4, 8) * _factor;
  1614. *_a += 255 * _factor;
  1615. }
  1616. else
  1617. {
  1618. *_r += bitRangeConvert( (_block >> 8) & 0xf, 4, 8) * _factor;
  1619. *_g += bitRangeConvert( (_block >> 4) & 0xf, 4, 8) * _factor;
  1620. *_b += bitRangeConvert( (_block >> 1) & 0x7, 3, 8) * _factor;
  1621. *_a += bitRangeConvert( (_block >> 12) & 0x7, 3, 8) * _factor;
  1622. }
  1623. }
  1624. void decodeBlockPtc14ARgbaAddB(uint32_t _block, uint32_t* _r, uint32_t* _g, uint32_t* _b, uint32_t* _a, uint8_t _factor)
  1625. {
  1626. if (0 != (_block & (1<<31) ) )
  1627. {
  1628. *_r += bitRangeConvert( (_block >> 26) & 0x1f, 5, 8) * _factor;
  1629. *_g += bitRangeConvert( (_block >> 21) & 0x1f, 5, 8) * _factor;
  1630. *_b += bitRangeConvert( (_block >> 16) & 0x1f, 5, 8) * _factor;
  1631. *_a += 255 * _factor;
  1632. }
  1633. else
  1634. {
  1635. *_r += bitRangeConvert( (_block >> 24) & 0xf, 4, 8) * _factor;
  1636. *_g += bitRangeConvert( (_block >> 20) & 0xf, 4, 8) * _factor;
  1637. *_b += bitRangeConvert( (_block >> 16) & 0xf, 4, 8) * _factor;
  1638. *_a += bitRangeConvert( (_block >> 28) & 0x7, 3, 8) * _factor;
  1639. }
  1640. }
  1641. void decodeBlockPtc14A(uint8_t _dst[16*4], const uint8_t* _src, uint32_t _x, uint32_t _y, uint32_t _width, uint32_t _height)
  1642. {
  1643. // 0 1 2 3 4 5 6 7
  1644. // 7654321076543210765432107654321076543210765432107654321076543210
  1645. // mmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmyrrrrrgggggbbbbbxrrrrrgggggbbbbp
  1646. // ^ ^^ ^^ ^
  1647. // +-- modulation data |+- B color |+- A color |
  1648. // +-- B opaque +-- A opaque |
  1649. // alpha punchthrough --+
  1650. const uint8_t* bc = &_src[morton2d(_x, _y) * 8];
  1651. uint32_t mod = 0
  1652. | bc[3]<<24
  1653. | bc[2]<<16
  1654. | bc[1]<<8
  1655. | bc[0]
  1656. ;
  1657. const bool punchthrough = !!(bc[7] & 1);
  1658. const uint8_t* weightTable = s_pvrtcWeights[4 * punchthrough];
  1659. const uint8_t* factorTable = s_pvrtcFactors[0];
  1660. for (int yy = 0; yy < 4; ++yy)
  1661. {
  1662. const uint32_t yOffset = (yy < 2) ? -1 : 0;
  1663. const uint32_t y0 = (_y + yOffset) % _height;
  1664. const uint32_t y1 = (y0 + 1) % _height;
  1665. for (int xx = 0; xx < 4; ++xx)
  1666. {
  1667. const uint32_t xOffset = (xx < 2) ? -1 : 0;
  1668. const uint32_t x0 = (_x + xOffset) % _width;
  1669. const uint32_t x1 = (x0 + 1) % _width;
  1670. const uint32_t bc0 = getColor(&_src[morton2d(x0, y0) * 8]);
  1671. const uint32_t bc1 = getColor(&_src[morton2d(x1, y0) * 8]);
  1672. const uint32_t bc2 = getColor(&_src[morton2d(x0, y1) * 8]);
  1673. const uint32_t bc3 = getColor(&_src[morton2d(x1, y1) * 8]);
  1674. const uint8_t f0 = factorTable[0];
  1675. const uint8_t f1 = factorTable[1];
  1676. const uint8_t f2 = factorTable[2];
  1677. const uint8_t f3 = factorTable[3];
  1678. uint32_t ar = 0, ag = 0, ab = 0, aa = 0;
  1679. decodeBlockPtc14ARgbaAddA(bc0, &ar, &ag, &ab, &aa, f0);
  1680. decodeBlockPtc14ARgbaAddA(bc1, &ar, &ag, &ab, &aa, f1);
  1681. decodeBlockPtc14ARgbaAddA(bc2, &ar, &ag, &ab, &aa, f2);
  1682. decodeBlockPtc14ARgbaAddA(bc3, &ar, &ag, &ab, &aa, f3);
  1683. uint32_t br = 0, bg = 0, bb = 0, ba = 0;
  1684. decodeBlockPtc14ARgbaAddB(bc0, &br, &bg, &bb, &ba, f0);
  1685. decodeBlockPtc14ARgbaAddB(bc1, &br, &bg, &bb, &ba, f1);
  1686. decodeBlockPtc14ARgbaAddB(bc2, &br, &bg, &bb, &ba, f2);
  1687. decodeBlockPtc14ARgbaAddB(bc3, &br, &bg, &bb, &ba, f3);
  1688. const uint8_t* weight = &weightTable[(mod & 3)*4];
  1689. const uint8_t wa = weight[0];
  1690. const uint8_t wb = weight[1];
  1691. const uint8_t wc = weight[2];
  1692. const uint8_t wd = weight[3];
  1693. _dst[(yy*4 + xx)*4+0] = uint8_t( (ab * wa + bb * wb) >> 7);
  1694. _dst[(yy*4 + xx)*4+1] = uint8_t( (ag * wa + bg * wb) >> 7);
  1695. _dst[(yy*4 + xx)*4+2] = uint8_t( (ar * wa + br * wb) >> 7);
  1696. _dst[(yy*4 + xx)*4+3] = uint8_t( (aa * wc + ba * wd) >> 7);
  1697. mod >>= 2;
  1698. factorTable += 4;
  1699. }
  1700. }
  1701. }
  1702. 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)
  1703. {
  1704. const ImageBlockInfo& blockInfo = getBlockInfo(_format);
  1705. const uint16_t blockWidth = blockInfo.blockWidth;
  1706. const uint16_t blockHeight = blockInfo.blockHeight;
  1707. const uint16_t minBlockX = blockInfo.minBlockX;
  1708. const uint16_t minBlockY = blockInfo.minBlockY;
  1709. _width = bx::uint16_max(blockWidth * minBlockX, ( (_width + blockWidth - 1) / blockWidth)*blockWidth);
  1710. _height = bx::uint16_max(blockHeight * minBlockY, ( (_height + blockHeight - 1) / blockHeight)*blockHeight);
  1711. _depth = bx::uint16_max(1, _depth);
  1712. _numLayers = bx::uint16_max(1, _numLayers);
  1713. const uint8_t numMips = _hasMips ? imageGetNumMips(_format, _width, _height, _depth) : 1;
  1714. uint32_t size = imageGetSize(NULL, _width, _height, _depth, _cubeMap, _hasMips, _numLayers, _format);
  1715. ImageContainer* imageContainer = (ImageContainer*)BX_ALLOC(_allocator, size + sizeof(ImageContainer) );
  1716. imageContainer->m_allocator = _allocator;
  1717. imageContainer->m_data = imageContainer + 1;
  1718. imageContainer->m_format = _format;
  1719. imageContainer->m_orientation = Orientation::R0;
  1720. imageContainer->m_size = size;
  1721. imageContainer->m_offset = 0;
  1722. imageContainer->m_width = _width;
  1723. imageContainer->m_height = _height;
  1724. imageContainer->m_depth = _depth;
  1725. imageContainer->m_numLayers = _numLayers;
  1726. imageContainer->m_numMips = numMips;
  1727. imageContainer->m_hasAlpha = false;
  1728. imageContainer->m_cubeMap = _cubeMap;
  1729. imageContainer->m_ktx = false;
  1730. imageContainer->m_ktxLE = false;
  1731. imageContainer->m_srgb = false;
  1732. if (NULL != _data)
  1733. {
  1734. bx::memCopy(imageContainer->m_data, _data, imageContainer->m_size);
  1735. }
  1736. return imageContainer;
  1737. }
  1738. void imageFree(ImageContainer* _imageContainer)
  1739. {
  1740. BX_FREE(_imageContainer->m_allocator, _imageContainer);
  1741. }
  1742. // DDS
  1743. #define DDS_MAGIC BX_MAKEFOURCC('D', 'D', 'S', ' ')
  1744. #define DDS_HEADER_SIZE 124
  1745. #define DDS_DXT1 BX_MAKEFOURCC('D', 'X', 'T', '1')
  1746. #define DDS_DXT2 BX_MAKEFOURCC('D', 'X', 'T', '2')
  1747. #define DDS_DXT3 BX_MAKEFOURCC('D', 'X', 'T', '3')
  1748. #define DDS_DXT4 BX_MAKEFOURCC('D', 'X', 'T', '4')
  1749. #define DDS_DXT5 BX_MAKEFOURCC('D', 'X', 'T', '5')
  1750. #define DDS_ATI1 BX_MAKEFOURCC('A', 'T', 'I', '1')
  1751. #define DDS_BC4U BX_MAKEFOURCC('B', 'C', '4', 'U')
  1752. #define DDS_ATI2 BX_MAKEFOURCC('A', 'T', 'I', '2')
  1753. #define DDS_BC5U BX_MAKEFOURCC('B', 'C', '5', 'U')
  1754. #define DDS_DX10 BX_MAKEFOURCC('D', 'X', '1', '0')
  1755. #define DDS_ETC1 BX_MAKEFOURCC('E', 'T', 'C', '1')
  1756. #define DDS_ETC2 BX_MAKEFOURCC('E', 'T', 'C', '2')
  1757. #define DDS_ET2A BX_MAKEFOURCC('E', 'T', '2', 'A')
  1758. #define DDS_PTC2 BX_MAKEFOURCC('P', 'T', 'C', '2')
  1759. #define DDS_PTC4 BX_MAKEFOURCC('P', 'T', 'C', '4')
  1760. #define DDS_ATC BX_MAKEFOURCC('A', 'T', 'C', ' ')
  1761. #define DDS_ATCE BX_MAKEFOURCC('A', 'T', 'C', 'E')
  1762. #define DDS_ATCI BX_MAKEFOURCC('A', 'T', 'C', 'I')
  1763. #define DDS_ASTC4x4 BX_MAKEFOURCC('A', 'S', '4', '4')
  1764. #define DDS_ASTC5x5 BX_MAKEFOURCC('A', 'S', '5', '5')
  1765. #define DDS_ASTC6x6 BX_MAKEFOURCC('A', 'S', '6', '6')
  1766. #define DDS_ASTC8x5 BX_MAKEFOURCC('A', 'S', '8', '5')
  1767. #define DDS_ASTC8x6 BX_MAKEFOURCC('A', 'S', '8', '6')
  1768. #define DDS_ASTC10x5 BX_MAKEFOURCC('A', 'S', ':', '5')
  1769. #define DDS_R8G8B8 20
  1770. #define DDS_A8R8G8B8 21
  1771. #define DDS_R5G6B5 23
  1772. #define DDS_A1R5G5B5 25
  1773. #define DDS_A4R4G4B4 26
  1774. #define DDS_A2B10G10R10 31
  1775. #define DDS_G16R16 34
  1776. #define DDS_A2R10G10B10 35
  1777. #define DDS_A16B16G16R16 36
  1778. #define DDS_A8L8 51
  1779. #define DDS_R16F 111
  1780. #define DDS_G16R16F 112
  1781. #define DDS_A16B16G16R16F 113
  1782. #define DDS_R32F 114
  1783. #define DDS_G32R32F 115
  1784. #define DDS_A32B32G32R32F 116
  1785. #define DDS_FORMAT_R32G32B32A32_FLOAT 2
  1786. #define DDS_FORMAT_R32G32B32A32_UINT 3
  1787. #define DDS_FORMAT_R16G16B16A16_FLOAT 10
  1788. #define DDS_FORMAT_R16G16B16A16_UNORM 11
  1789. #define DDS_FORMAT_R16G16B16A16_UINT 12
  1790. #define DDS_FORMAT_R32G32_FLOAT 16
  1791. #define DDS_FORMAT_R32G32_UINT 17
  1792. #define DDS_FORMAT_R10G10B10A2_UNORM 24
  1793. #define DDS_FORMAT_R11G11B10_FLOAT 26
  1794. #define DDS_FORMAT_R8G8B8A8_UNORM 28
  1795. #define DDS_FORMAT_R8G8B8A8_UNORM_SRGB 29
  1796. #define DDS_FORMAT_R16G16_FLOAT 34
  1797. #define DDS_FORMAT_R16G16_UNORM 35
  1798. #define DDS_FORMAT_R32_FLOAT 41
  1799. #define DDS_FORMAT_R32_UINT 42
  1800. #define DDS_FORMAT_R8G8_UNORM 49
  1801. #define DDS_FORMAT_R16_FLOAT 54
  1802. #define DDS_FORMAT_R16_UNORM 56
  1803. #define DDS_FORMAT_R8_UNORM 61
  1804. #define DDS_FORMAT_R1_UNORM 66
  1805. #define DDS_FORMAT_BC1_UNORM 71
  1806. #define DDS_FORMAT_BC1_UNORM_SRGB 72
  1807. #define DDS_FORMAT_BC2_UNORM 74
  1808. #define DDS_FORMAT_BC2_UNORM_SRGB 75
  1809. #define DDS_FORMAT_BC3_UNORM 77
  1810. #define DDS_FORMAT_BC3_UNORM_SRGB 78
  1811. #define DDS_FORMAT_BC4_UNORM 80
  1812. #define DDS_FORMAT_BC5_UNORM 83
  1813. #define DDS_FORMAT_B5G6R5_UNORM 85
  1814. #define DDS_FORMAT_B5G5R5A1_UNORM 86
  1815. #define DDS_FORMAT_B8G8R8A8_UNORM 87
  1816. #define DDS_FORMAT_B8G8R8A8_UNORM_SRGB 91
  1817. #define DDS_FORMAT_BC6H_SF16 96
  1818. #define DDS_FORMAT_BC7_UNORM 98
  1819. #define DDS_FORMAT_BC7_UNORM_SRGB 99
  1820. #define DDS_FORMAT_B4G4R4A4_UNORM 115
  1821. #define DDS_DX10_DIMENSION_TEXTURE2D 3
  1822. #define DDS_DX10_DIMENSION_TEXTURE3D 4
  1823. #define DDS_DX10_MISC_TEXTURECUBE 4
  1824. #define DDSD_CAPS 0x00000001
  1825. #define DDSD_HEIGHT 0x00000002
  1826. #define DDSD_WIDTH 0x00000004
  1827. #define DDSD_PITCH 0x00000008
  1828. #define DDSD_PIXELFORMAT 0x00001000
  1829. #define DDSD_MIPMAPCOUNT 0x00020000
  1830. #define DDSD_LINEARSIZE 0x00080000
  1831. #define DDSD_DEPTH 0x00800000
  1832. #define DDPF_ALPHAPIXELS 0x00000001
  1833. #define DDPF_ALPHA 0x00000002
  1834. #define DDPF_FOURCC 0x00000004
  1835. #define DDPF_INDEXED 0x00000020
  1836. #define DDPF_RGB 0x00000040
  1837. #define DDPF_YUV 0x00000200
  1838. #define DDPF_LUMINANCE 0x00020000
  1839. #define DDPF_BUMPDUDV 0x00080000
  1840. #define DDSCAPS_COMPLEX 0x00000008
  1841. #define DDSCAPS_TEXTURE 0x00001000
  1842. #define DDSCAPS_MIPMAP 0x00400000
  1843. #define DDSCAPS2_VOLUME 0x00200000
  1844. #define DDSCAPS2_CUBEMAP 0x00000200
  1845. #define DDSCAPS2_CUBEMAP_POSITIVEX 0x00000400
  1846. #define DDSCAPS2_CUBEMAP_NEGATIVEX 0x00000800
  1847. #define DDSCAPS2_CUBEMAP_POSITIVEY 0x00001000
  1848. #define DDSCAPS2_CUBEMAP_NEGATIVEY 0x00002000
  1849. #define DDSCAPS2_CUBEMAP_POSITIVEZ 0x00004000
  1850. #define DDSCAPS2_CUBEMAP_NEGATIVEZ 0x00008000
  1851. #define DSCAPS2_CUBEMAP_ALLSIDES (0 \
  1852. | DDSCAPS2_CUBEMAP_POSITIVEX \
  1853. | DDSCAPS2_CUBEMAP_NEGATIVEX \
  1854. | DDSCAPS2_CUBEMAP_POSITIVEY \
  1855. | DDSCAPS2_CUBEMAP_NEGATIVEY \
  1856. | DDSCAPS2_CUBEMAP_POSITIVEZ \
  1857. | DDSCAPS2_CUBEMAP_NEGATIVEZ \
  1858. )
  1859. struct TranslateDdsFormat
  1860. {
  1861. uint32_t m_format;
  1862. TextureFormat::Enum m_textureFormat;
  1863. bool m_srgb;
  1864. };
  1865. static const TranslateDdsFormat s_translateDdsFourccFormat[] =
  1866. {
  1867. { DDS_DXT1, TextureFormat::BC1, false },
  1868. { DDS_DXT2, TextureFormat::BC2, false },
  1869. { DDS_DXT3, TextureFormat::BC2, false },
  1870. { DDS_DXT4, TextureFormat::BC3, false },
  1871. { DDS_DXT5, TextureFormat::BC3, false },
  1872. { DDS_ATI1, TextureFormat::BC4, false },
  1873. { DDS_BC4U, TextureFormat::BC4, false },
  1874. { DDS_ATI2, TextureFormat::BC5, false },
  1875. { DDS_BC5U, TextureFormat::BC5, false },
  1876. { DDS_ETC1, TextureFormat::ETC1, false },
  1877. { DDS_ETC2, TextureFormat::ETC2, false },
  1878. { DDS_ET2A, TextureFormat::ETC2A, false },
  1879. { DDS_PTC2, TextureFormat::PTC12A, false },
  1880. { DDS_PTC4, TextureFormat::PTC14A, false },
  1881. { DDS_ATC , TextureFormat::ATC, false },
  1882. { DDS_ATCE, TextureFormat::ATCE, false },
  1883. { DDS_ATCI, TextureFormat::ATCI, false },
  1884. { DDS_ASTC4x4, TextureFormat::ASTC4x4, false },
  1885. { DDS_ASTC5x5, TextureFormat::ASTC5x5, false },
  1886. { DDS_ASTC6x6, TextureFormat::ASTC6x6, false },
  1887. { DDS_ASTC8x5, TextureFormat::ASTC8x5, false },
  1888. { DDS_ASTC8x6, TextureFormat::ASTC8x6, false },
  1889. { DDS_ASTC10x5, TextureFormat::ASTC10x5, false },
  1890. { DDS_A16B16G16R16, TextureFormat::RGBA16, false },
  1891. { DDS_A16B16G16R16F, TextureFormat::RGBA16F, false },
  1892. { DDPF_RGB|DDPF_ALPHAPIXELS, TextureFormat::BGRA8, false },
  1893. { DDPF_INDEXED, TextureFormat::R8, false },
  1894. { DDPF_LUMINANCE, TextureFormat::R8, false },
  1895. { DDPF_ALPHA, TextureFormat::R8, false },
  1896. { DDS_R16F, TextureFormat::R16F, false },
  1897. { DDS_R32F, TextureFormat::R32F, false },
  1898. { DDS_A8L8, TextureFormat::RG8, false },
  1899. { DDS_G16R16, TextureFormat::RG16, false },
  1900. { DDS_G16R16F, TextureFormat::RG16F, false },
  1901. { DDS_G32R32F, TextureFormat::RG32F, false },
  1902. { DDS_R8G8B8, TextureFormat::RGB8, false },
  1903. { DDS_A8R8G8B8, TextureFormat::BGRA8, false },
  1904. { DDS_A16B16G16R16, TextureFormat::RGBA16, false },
  1905. { DDS_A16B16G16R16F, TextureFormat::RGBA16F, false },
  1906. { DDS_A32B32G32R32F, TextureFormat::RGBA32F, false },
  1907. { DDS_R5G6B5, TextureFormat::R5G6B5, false },
  1908. { DDS_A4R4G4B4, TextureFormat::RGBA4, false },
  1909. { DDS_A1R5G5B5, TextureFormat::RGB5A1, false },
  1910. { DDS_A2B10G10R10, TextureFormat::RGB10A2, false },
  1911. };
  1912. static const TranslateDdsFormat s_translateDxgiFormat[] =
  1913. {
  1914. { DDS_FORMAT_BC1_UNORM, TextureFormat::BC1, false },
  1915. { DDS_FORMAT_BC1_UNORM_SRGB, TextureFormat::BC1, true },
  1916. { DDS_FORMAT_BC2_UNORM, TextureFormat::BC2, false },
  1917. { DDS_FORMAT_BC2_UNORM_SRGB, TextureFormat::BC2, true },
  1918. { DDS_FORMAT_BC3_UNORM, TextureFormat::BC3, false },
  1919. { DDS_FORMAT_BC3_UNORM_SRGB, TextureFormat::BC3, true },
  1920. { DDS_FORMAT_BC4_UNORM, TextureFormat::BC4, false },
  1921. { DDS_FORMAT_BC5_UNORM, TextureFormat::BC5, false },
  1922. { DDS_FORMAT_BC6H_SF16, TextureFormat::BC6H, false },
  1923. { DDS_FORMAT_BC7_UNORM, TextureFormat::BC7, false },
  1924. { DDS_FORMAT_BC7_UNORM_SRGB, TextureFormat::BC7, true },
  1925. { DDS_FORMAT_R1_UNORM, TextureFormat::R1, false },
  1926. { DDS_FORMAT_R8_UNORM, TextureFormat::R8, false },
  1927. { DDS_FORMAT_R16_UNORM, TextureFormat::R16, false },
  1928. { DDS_FORMAT_R16_FLOAT, TextureFormat::R16F, false },
  1929. { DDS_FORMAT_R32_UINT, TextureFormat::R32U, false },
  1930. { DDS_FORMAT_R32_FLOAT, TextureFormat::R32F, false },
  1931. { DDS_FORMAT_R8G8_UNORM, TextureFormat::RG8, false },
  1932. { DDS_FORMAT_R16G16_UNORM, TextureFormat::RG16, false },
  1933. { DDS_FORMAT_R16G16_FLOAT, TextureFormat::RG16F, false },
  1934. { DDS_FORMAT_R32G32_UINT, TextureFormat::RG32U, false },
  1935. { DDS_FORMAT_R32G32_FLOAT, TextureFormat::RG32F, false },
  1936. { DDS_FORMAT_B8G8R8A8_UNORM, TextureFormat::BGRA8, false },
  1937. { DDS_FORMAT_B8G8R8A8_UNORM_SRGB, TextureFormat::BGRA8, true },
  1938. { DDS_FORMAT_R8G8B8A8_UNORM, TextureFormat::RGBA8, false },
  1939. { DDS_FORMAT_R8G8B8A8_UNORM_SRGB, TextureFormat::RGBA8, true },
  1940. { DDS_FORMAT_R16G16B16A16_UNORM, TextureFormat::RGBA16, false },
  1941. { DDS_FORMAT_R16G16B16A16_FLOAT, TextureFormat::RGBA16F, false },
  1942. { DDS_FORMAT_R32G32B32A32_UINT, TextureFormat::RGBA32U, false },
  1943. { DDS_FORMAT_R32G32B32A32_FLOAT, TextureFormat::RGBA32F, false },
  1944. { DDS_FORMAT_B5G6R5_UNORM, TextureFormat::R5G6B5, false },
  1945. { DDS_FORMAT_B4G4R4A4_UNORM, TextureFormat::RGBA4, false },
  1946. { DDS_FORMAT_B5G5R5A1_UNORM, TextureFormat::RGB5A1, false },
  1947. { DDS_FORMAT_R10G10B10A2_UNORM, TextureFormat::RGB10A2, false },
  1948. { DDS_FORMAT_R11G11B10_FLOAT, TextureFormat::RG11B10F, false },
  1949. };
  1950. struct TranslateDdsPixelFormat
  1951. {
  1952. uint32_t m_bitCount;
  1953. uint32_t m_flags;
  1954. uint32_t m_bitmask[4];
  1955. TextureFormat::Enum m_textureFormat;
  1956. };
  1957. static const TranslateDdsPixelFormat s_translateDdsPixelFormat[] =
  1958. {
  1959. { 8, DDPF_LUMINANCE, { 0x000000ff, 0x00000000, 0x00000000, 0x00000000 }, TextureFormat::R8 },
  1960. { 16, DDPF_BUMPDUDV, { 0x000000ff, 0x0000ff00, 0x00000000, 0x00000000 }, TextureFormat::RG8S },
  1961. { 16, DDPF_RGB, { 0x0000ffff, 0x00000000, 0x00000000, 0x00000000 }, TextureFormat::R16U },
  1962. { 16, DDPF_RGB|DDPF_ALPHAPIXELS, { 0x00000f00, 0x000000f0, 0x0000000f, 0x0000f000 }, TextureFormat::RGBA4 },
  1963. { 16, DDPF_RGB, { 0x0000f800, 0x000007e0, 0x0000001f, 0x00000000 }, TextureFormat::R5G6B5 },
  1964. { 16, DDPF_RGB, { 0x00007c00, 0x000003e0, 0x0000001f, 0x00008000 }, TextureFormat::RGB5A1 },
  1965. { 24, DDPF_RGB, { 0x00ff0000, 0x0000ff00, 0x000000ff, 0x00000000 }, TextureFormat::RGB8 },
  1966. { 24, DDPF_RGB, { 0x000000ff, 0x0000ff00, 0x00ff0000, 0x00000000 }, TextureFormat::RGB8 },
  1967. { 32, DDPF_RGB, { 0x00ff0000, 0x0000ff00, 0x000000ff, 0x00000000 }, TextureFormat::BGRA8 },
  1968. { 32, DDPF_RGB|DDPF_ALPHAPIXELS, { 0x000000ff, 0x0000ff00, 0x00ff0000, 0xff000000 }, TextureFormat::RGBA8 },
  1969. { 32, DDPF_BUMPDUDV, { 0x000000ff, 0x0000ff00, 0x00ff0000, 0xff000000 }, TextureFormat::RGBA8S },
  1970. { 32, DDPF_RGB, { 0x00ff0000, 0x0000ff00, 0x000000ff, 0xff000000 }, TextureFormat::BGRA8 },
  1971. { 32, DDPF_RGB|DDPF_ALPHAPIXELS, { 0x00ff0000, 0x0000ff00, 0x000000ff, 0xff000000 }, TextureFormat::BGRA8 }, // D3DFMT_A8R8G8B8
  1972. { 32, DDPF_RGB|DDPF_ALPHAPIXELS, { 0x00ff0000, 0x0000ff00, 0x000000ff, 0x00000000 }, TextureFormat::BGRA8 }, // D3DFMT_X8R8G8B8
  1973. { 32, DDPF_RGB|DDPF_ALPHAPIXELS, { 0x000003ff, 0x000ffc00, 0x3ff00000, 0xc0000000 }, TextureFormat::RGB10A2 },
  1974. { 32, DDPF_RGB, { 0x0000ffff, 0xffff0000, 0x00000000, 0x00000000 }, TextureFormat::RG16 },
  1975. { 32, DDPF_BUMPDUDV, { 0x0000ffff, 0xffff0000, 0x00000000, 0x00000000 }, TextureFormat::RG16S },
  1976. { 32, DDPF_RGB, { 0xffffffff, 0x00000000, 0x00000000, 0x00000000 }, TextureFormat::R32U },
  1977. };
  1978. bool imageParseDds(ImageContainer& _imageContainer, bx::ReaderSeekerI* _reader, bx::Error* _err)
  1979. {
  1980. BX_ERROR_SCOPE(_err);
  1981. int32_t total = 0;
  1982. uint32_t headerSize;
  1983. total += bx::read(_reader, headerSize, _err);
  1984. if (!_err->isOk()
  1985. || headerSize < DDS_HEADER_SIZE)
  1986. {
  1987. return false;
  1988. }
  1989. uint32_t flags;
  1990. total += bx::read(_reader, flags, _err);
  1991. if (!_err->isOk() )
  1992. {
  1993. return false;
  1994. }
  1995. if ( (flags & (DDSD_CAPS|DDSD_HEIGHT|DDSD_WIDTH|DDSD_PIXELFORMAT) ) != (DDSD_CAPS|DDSD_HEIGHT|DDSD_WIDTH|DDSD_PIXELFORMAT) )
  1996. {
  1997. BX_ERROR_SET(_err, BIMG_ERROR, "DDS: Invalid flags.");
  1998. return false;
  1999. }
  2000. uint32_t height;
  2001. total += bx::read(_reader, height, _err);
  2002. uint32_t width;
  2003. total += bx::read(_reader, width, _err);
  2004. uint32_t pitch;
  2005. total += bx::read(_reader, pitch, _err);
  2006. uint32_t depth;
  2007. total += bx::read(_reader, depth, _err);
  2008. uint32_t mips;
  2009. total += bx::read(_reader, mips, _err);
  2010. bx::skip(_reader, 44); // reserved
  2011. total += 44;
  2012. uint32_t pixelFormatSize;
  2013. total += bx::read(_reader, pixelFormatSize, _err);
  2014. uint32_t pixelFlags;
  2015. total += bx::read(_reader, pixelFlags, _err);
  2016. uint32_t fourcc;
  2017. total += bx::read(_reader, fourcc, _err);
  2018. uint32_t bitCount;
  2019. total += bx::read(_reader, bitCount, _err);
  2020. uint32_t bitmask[4];
  2021. total += bx::read(_reader, bitmask, sizeof(bitmask), _err);
  2022. uint32_t caps[4];
  2023. total += bx::read(_reader, caps, _err);
  2024. bx::skip(_reader, 4);
  2025. total += 4; // reserved
  2026. if (!_err->isOk() )
  2027. {
  2028. return false;
  2029. }
  2030. uint32_t dxgiFormat = 0;
  2031. uint32_t arraySize = 1;
  2032. if (DDPF_FOURCC == (pixelFlags & DDPF_FOURCC)
  2033. && DDS_DX10 == fourcc)
  2034. {
  2035. total += bx::read(_reader, dxgiFormat, _err);
  2036. uint32_t dims;
  2037. total += bx::read(_reader, dims, _err);
  2038. uint32_t miscFlags;
  2039. total += bx::read(_reader, miscFlags, _err);
  2040. total += bx::read(_reader, arraySize, _err);
  2041. uint32_t miscFlags2;
  2042. total += bx::read(_reader, miscFlags2, _err);
  2043. }
  2044. if (!_err->isOk() )
  2045. {
  2046. return false;
  2047. }
  2048. if ( (caps[0] & DDSCAPS_TEXTURE) == 0)
  2049. {
  2050. BX_ERROR_SET(_err, BIMG_ERROR, "DDS: Unsupported caps.");
  2051. return false;
  2052. }
  2053. bool cubeMap = 0 != (caps[1] & DDSCAPS2_CUBEMAP);
  2054. if (cubeMap)
  2055. {
  2056. if ( (caps[1] & DSCAPS2_CUBEMAP_ALLSIDES) != DSCAPS2_CUBEMAP_ALLSIDES)
  2057. {
  2058. // partial cube map is not supported.
  2059. BX_ERROR_SET(_err, BIMG_ERROR, "DDS: Incomplete cubemap.");
  2060. return false;
  2061. }
  2062. }
  2063. TextureFormat::Enum format = TextureFormat::Unknown;
  2064. bool hasAlpha = pixelFlags & DDPF_ALPHAPIXELS;
  2065. bool srgb = false;
  2066. if (dxgiFormat == 0)
  2067. {
  2068. if (DDPF_FOURCC == (pixelFlags & DDPF_FOURCC) )
  2069. {
  2070. for (uint32_t ii = 0; ii < BX_COUNTOF(s_translateDdsFourccFormat); ++ii)
  2071. {
  2072. if (s_translateDdsFourccFormat[ii].m_format == fourcc)
  2073. {
  2074. format = s_translateDdsFourccFormat[ii].m_textureFormat;
  2075. break;
  2076. }
  2077. }
  2078. }
  2079. else
  2080. {
  2081. for (uint32_t ii = 0; ii < BX_COUNTOF(s_translateDdsPixelFormat); ++ii)
  2082. {
  2083. const TranslateDdsPixelFormat& pf = s_translateDdsPixelFormat[ii];
  2084. if (pf.m_bitCount == bitCount
  2085. && pf.m_flags == pixelFlags
  2086. && pf.m_bitmask[0] == bitmask[0]
  2087. && pf.m_bitmask[1] == bitmask[1]
  2088. && pf.m_bitmask[2] == bitmask[2]
  2089. && pf.m_bitmask[3] == bitmask[3])
  2090. {
  2091. format = pf.m_textureFormat;
  2092. break;
  2093. }
  2094. }
  2095. }
  2096. }
  2097. else
  2098. {
  2099. for (uint32_t ii = 0; ii < BX_COUNTOF(s_translateDxgiFormat); ++ii)
  2100. {
  2101. if (s_translateDxgiFormat[ii].m_format == dxgiFormat)
  2102. {
  2103. format = s_translateDxgiFormat[ii].m_textureFormat;
  2104. srgb = s_translateDxgiFormat[ii].m_srgb;
  2105. break;
  2106. }
  2107. }
  2108. }
  2109. if (TextureFormat::Unknown == format)
  2110. {
  2111. BX_ERROR_SET(_err, BIMG_ERROR, "DDS: Unknown texture format.");
  2112. return false;
  2113. }
  2114. _imageContainer.m_allocator = NULL;
  2115. _imageContainer.m_data = NULL;
  2116. _imageContainer.m_size = 0;
  2117. _imageContainer.m_offset = (uint32_t)bx::seek(_reader);
  2118. _imageContainer.m_width = width;
  2119. _imageContainer.m_height = height;
  2120. _imageContainer.m_depth = depth;
  2121. _imageContainer.m_format = format;
  2122. _imageContainer.m_orientation = Orientation::R0;
  2123. _imageContainer.m_numLayers = uint16_t(arraySize);
  2124. _imageContainer.m_numMips = uint8_t( (caps[0] & DDSCAPS_MIPMAP) ? mips : 1);
  2125. _imageContainer.m_hasAlpha = hasAlpha;
  2126. _imageContainer.m_cubeMap = cubeMap;
  2127. _imageContainer.m_ktx = false;
  2128. _imageContainer.m_ktxLE = false;
  2129. _imageContainer.m_srgb = srgb;
  2130. return true;
  2131. }
  2132. ImageContainer* imageParseDds(bx::AllocatorI* _allocator, const void* _src, uint32_t _size, bx::Error* _err)
  2133. {
  2134. return imageParseT<DDS_MAGIC, imageParseDds>(_allocator, _src, _size, _err);
  2135. }
  2136. // KTX
  2137. #define KTX_MAGIC BX_MAKEFOURCC(0xAB, 'K', 'T', 'X')
  2138. #define KTX_HEADER_SIZE 64
  2139. #define KTX_ETC1_RGB8_OES 0x8D64
  2140. #define KTX_COMPRESSED_R11_EAC 0x9270
  2141. #define KTX_COMPRESSED_SIGNED_R11_EAC 0x9271
  2142. #define KTX_COMPRESSED_RG11_EAC 0x9272
  2143. #define KTX_COMPRESSED_SIGNED_RG11_EAC 0x9273
  2144. #define KTX_COMPRESSED_RGB8_ETC2 0x9274
  2145. #define KTX_COMPRESSED_SRGB8_ETC2 0x9275
  2146. #define KTX_COMPRESSED_RGB8_PUNCHTHROUGH_ALPHA1_ETC2 0x9276
  2147. #define KTX_COMPRESSED_SRGB8_PUNCHTHROUGH_ALPHA1_ETC2 0x9277
  2148. #define KTX_COMPRESSED_RGBA8_ETC2_EAC 0x9278
  2149. #define KTX_COMPRESSED_SRGB8_ALPHA8_ETC2_EAC 0x9279
  2150. #define KTX_COMPRESSED_RGB_PVRTC_4BPPV1_IMG 0x8C00
  2151. #define KTX_COMPRESSED_RGB_PVRTC_2BPPV1_IMG 0x8C01
  2152. #define KTX_COMPRESSED_RGBA_PVRTC_4BPPV1_IMG 0x8C02
  2153. #define KTX_COMPRESSED_RGBA_PVRTC_2BPPV1_IMG 0x8C03
  2154. #define KTX_COMPRESSED_RGBA_PVRTC_2BPPV2_IMG 0x9137
  2155. #define KTX_COMPRESSED_RGBA_PVRTC_4BPPV2_IMG 0x9138
  2156. #define KTX_COMPRESSED_RGB_S3TC_DXT1_EXT 0x83F0
  2157. #define KTX_COMPRESSED_RGBA_S3TC_DXT1_EXT 0x83F1
  2158. #define KTX_COMPRESSED_RGBA_S3TC_DXT3_EXT 0x83F2
  2159. #define KTX_COMPRESSED_RGBA_S3TC_DXT5_EXT 0x83F3
  2160. #define KTX_COMPRESSED_SRGB_ALPHA_S3TC_DXT1_EXT 0x8C4D
  2161. #define KTX_COMPRESSED_SRGB_ALPHA_S3TC_DXT3_EXT 0x8C4E
  2162. #define KTX_COMPRESSED_SRGB_ALPHA_S3TC_DXT5_EXT 0x8C4F
  2163. #define KTX_COMPRESSED_LUMINANCE_LATC1_EXT 0x8C70
  2164. #define KTX_COMPRESSED_LUMINANCE_ALPHA_LATC2_EXT 0x8C72
  2165. #define KTX_COMPRESSED_RGBA_BPTC_UNORM_ARB 0x8E8C
  2166. #define KTX_COMPRESSED_SRGB_ALPHA_BPTC_UNORM_ARB 0x8E8D
  2167. #define KTX_COMPRESSED_RGB_BPTC_SIGNED_FLOAT_ARB 0x8E8E
  2168. #define KTX_COMPRESSED_RGB_BPTC_UNSIGNED_FLOAT_ARB 0x8E8F
  2169. #define KTX_COMPRESSED_SRGB_PVRTC_2BPPV1_EXT 0x8A54
  2170. #define KTX_COMPRESSED_SRGB_PVRTC_4BPPV1_EXT 0x8A55
  2171. #define KTX_COMPRESSED_SRGB_ALPHA_PVRTC_2BPPV1_EXT 0x8A56
  2172. #define KTX_COMPRESSED_SRGB_ALPHA_PVRTC_4BPPV1_EXT 0x8A57
  2173. #define KTX_ATC_RGB_AMD 0x8C92
  2174. #define KTX_ATC_RGBA_EXPLICIT_ALPHA_AMD 0x8C93
  2175. #define KTX_ATC_RGBA_INTERPOLATED_ALPHA_AMD 0x87EE
  2176. #define KTX_COMPRESSED_RGBA_ASTC_4x4_KHR 0x93B0
  2177. #define KTX_COMPRESSED_RGBA_ASTC_5x5_KHR 0x93B2
  2178. #define KTX_COMPRESSED_RGBA_ASTC_6x6_KHR 0x93B4
  2179. #define KTX_COMPRESSED_RGBA_ASTC_8x5_KHR 0x93B5
  2180. #define KTX_COMPRESSED_RGBA_ASTC_8x6_KHR 0x93B6
  2181. #define KTX_COMPRESSED_RGBA_ASTC_10x5_KHR 0x93B8
  2182. #define KTX_COMPRESSED_SRGB8_ALPHA8_ASTC_4x4_KHR 0x93D0
  2183. #define KTX_COMPRESSED_SRGB8_ALPHA8_ASTC_5x5_KHR 0x93D2
  2184. #define KTX_COMPRESSED_SRGB8_ALPHA8_ASTC_6x6_KHR 0x93D4
  2185. #define KTX_COMPRESSED_SRGB8_ALPHA8_ASTC_8x5_KHR 0x93D5
  2186. #define KTX_COMPRESSED_SRGB8_ALPHA8_ASTC_8x6_KHR 0x93D6
  2187. #define KTX_COMPRESSED_SRGB8_ALPHA8_ASTC_10x5_KHR 0x93D8
  2188. #define KTX_A8 0x803C
  2189. #define KTX_R8 0x8229
  2190. #define KTX_R16 0x822A
  2191. #define KTX_RG8 0x822B
  2192. #define KTX_RG16 0x822C
  2193. #define KTX_R16F 0x822D
  2194. #define KTX_R32F 0x822E
  2195. #define KTX_RG16F 0x822F
  2196. #define KTX_RG32F 0x8230
  2197. #define KTX_RGBA8 0x8058
  2198. #define KTX_RGBA16 0x805B
  2199. #define KTX_RGBA16F 0x881A
  2200. #define KTX_R32UI 0x8236
  2201. #define KTX_RG32UI 0x823C
  2202. #define KTX_RGBA32UI 0x8D70
  2203. #define KTX_RGBA32F 0x8814
  2204. #define KTX_RGB565 0x8D62
  2205. #define KTX_RGBA4 0x8056
  2206. #define KTX_RGB5_A1 0x8057
  2207. #define KTX_RGB10_A2 0x8059
  2208. #define KTX_R8I 0x8231
  2209. #define KTX_R8UI 0x8232
  2210. #define KTX_R16I 0x8233
  2211. #define KTX_R16UI 0x8234
  2212. #define KTX_R32I 0x8235
  2213. #define KTX_R32UI 0x8236
  2214. #define KTX_RG8I 0x8237
  2215. #define KTX_RG8UI 0x8238
  2216. #define KTX_RG16I 0x8239
  2217. #define KTX_RG16UI 0x823A
  2218. #define KTX_RG32I 0x823B
  2219. #define KTX_RG32UI 0x823C
  2220. #define KTX_R8_SNORM 0x8F94
  2221. #define KTX_RG8_SNORM 0x8F95
  2222. #define KTX_RGB8_SNORM 0x8F96
  2223. #define KTX_RGBA8_SNORM 0x8F97
  2224. #define KTX_R16_SNORM 0x8F98
  2225. #define KTX_RG16_SNORM 0x8F99
  2226. #define KTX_RGB16_SNORM 0x8F9A
  2227. #define KTX_RGBA16_SNORM 0x8F9B
  2228. #define KTX_SRGB8 0x8C41
  2229. #define KTX_SRGB8_ALPHA8 0x8C43
  2230. #define KTX_RGBA32UI 0x8D70
  2231. #define KTX_RGB32UI 0x8D71
  2232. #define KTX_RGBA16UI 0x8D76
  2233. #define KTX_RGB16UI 0x8D77
  2234. #define KTX_RGBA8UI 0x8D7C
  2235. #define KTX_RGB8UI 0x8D7D
  2236. #define KTX_RGBA32I 0x8D82
  2237. #define KTX_RGB32I 0x8D83
  2238. #define KTX_RGBA16I 0x8D88
  2239. #define KTX_RGB16I 0x8D89
  2240. #define KTX_RGBA8I 0x8D8E
  2241. #define KTX_RGB8 0x8051
  2242. #define KTX_RGB8I 0x8D8F
  2243. #define KTX_RGB9_E5 0x8C3D
  2244. #define KTX_R11F_G11F_B10F 0x8C3A
  2245. #define KTX_ZERO 0
  2246. #define KTX_RED 0x1903
  2247. #define KTX_ALPHA 0x1906
  2248. #define KTX_RGB 0x1907
  2249. #define KTX_RGBA 0x1908
  2250. #define KTX_BGRA 0x80E1
  2251. #define KTX_RG 0x8227
  2252. #define KTX_BYTE 0x1400
  2253. #define KTX_UNSIGNED_BYTE 0x1401
  2254. #define KTX_SHORT 0x1402
  2255. #define KTX_UNSIGNED_SHORT 0x1403
  2256. #define KTX_INT 0x1404
  2257. #define KTX_UNSIGNED_INT 0x1405
  2258. #define KTX_FLOAT 0x1406
  2259. #define KTX_HALF_FLOAT 0x140B
  2260. #define KTX_UNSIGNED_INT_5_9_9_9_REV 0x8C3E
  2261. #define KTX_UNSIGNED_SHORT_5_6_5 0x8363
  2262. #define KTX_UNSIGNED_SHORT_4_4_4_4 0x8033
  2263. #define KTX_UNSIGNED_SHORT_5_5_5_1 0x8034
  2264. #define KTX_UNSIGNED_INT_2_10_10_10_REV 0x8368
  2265. #define KTX_UNSIGNED_INT_10F_11F_11F_REV 0x8C3B
  2266. struct KtxFormatInfo
  2267. {
  2268. uint32_t m_internalFmt;
  2269. uint32_t m_internalFmtSrgb;
  2270. uint32_t m_fmt;
  2271. uint32_t m_type;
  2272. };
  2273. static const KtxFormatInfo s_translateKtxFormat[] =
  2274. {
  2275. { KTX_COMPRESSED_RGBA_S3TC_DXT1_EXT, KTX_COMPRESSED_SRGB_ALPHA_S3TC_DXT1_EXT, KTX_COMPRESSED_RGBA_S3TC_DXT1_EXT, KTX_ZERO, }, // BC1
  2276. { KTX_COMPRESSED_RGBA_S3TC_DXT3_EXT, KTX_COMPRESSED_SRGB_ALPHA_S3TC_DXT3_EXT, KTX_COMPRESSED_RGBA_S3TC_DXT3_EXT, KTX_ZERO, }, // BC2
  2277. { KTX_COMPRESSED_RGBA_S3TC_DXT5_EXT, KTX_COMPRESSED_SRGB_ALPHA_S3TC_DXT5_EXT, KTX_COMPRESSED_RGBA_S3TC_DXT5_EXT, KTX_ZERO, }, // BC3
  2278. { KTX_COMPRESSED_LUMINANCE_LATC1_EXT, KTX_ZERO, KTX_COMPRESSED_LUMINANCE_LATC1_EXT, KTX_ZERO, }, // BC4
  2279. { KTX_COMPRESSED_LUMINANCE_ALPHA_LATC2_EXT, KTX_ZERO, KTX_COMPRESSED_LUMINANCE_ALPHA_LATC2_EXT, KTX_ZERO, }, // BC5
  2280. { KTX_COMPRESSED_RGB_BPTC_SIGNED_FLOAT_ARB, KTX_ZERO, KTX_COMPRESSED_RGB_BPTC_SIGNED_FLOAT_ARB, KTX_ZERO, }, // BC6H
  2281. { KTX_COMPRESSED_RGBA_BPTC_UNORM_ARB, KTX_ZERO, KTX_COMPRESSED_RGBA_BPTC_UNORM_ARB, KTX_ZERO, }, // BC7
  2282. { KTX_ETC1_RGB8_OES, KTX_ZERO, KTX_ETC1_RGB8_OES, KTX_ZERO, }, // ETC1
  2283. { KTX_COMPRESSED_RGB8_ETC2, KTX_ZERO, KTX_COMPRESSED_RGB8_ETC2, KTX_ZERO, }, // ETC2
  2284. { KTX_COMPRESSED_RGBA8_ETC2_EAC, KTX_COMPRESSED_SRGB8_ETC2, KTX_COMPRESSED_RGBA8_ETC2_EAC, KTX_ZERO, }, // ETC2A
  2285. { KTX_COMPRESSED_RGB8_PUNCHTHROUGH_ALPHA1_ETC2, KTX_COMPRESSED_SRGB8_PUNCHTHROUGH_ALPHA1_ETC2, KTX_COMPRESSED_RGB8_PUNCHTHROUGH_ALPHA1_ETC2, KTX_ZERO, }, // ETC2A1
  2286. { KTX_COMPRESSED_RGB_PVRTC_2BPPV1_IMG, KTX_COMPRESSED_SRGB_PVRTC_2BPPV1_EXT, KTX_COMPRESSED_RGB_PVRTC_2BPPV1_IMG, KTX_ZERO, }, // PTC12
  2287. { KTX_COMPRESSED_RGB_PVRTC_4BPPV1_IMG, KTX_COMPRESSED_SRGB_PVRTC_4BPPV1_EXT, KTX_COMPRESSED_RGB_PVRTC_4BPPV1_IMG, KTX_ZERO, }, // PTC14
  2288. { KTX_COMPRESSED_RGBA_PVRTC_2BPPV1_IMG, KTX_COMPRESSED_SRGB_ALPHA_PVRTC_2BPPV1_EXT, KTX_COMPRESSED_RGBA_PVRTC_2BPPV1_IMG, KTX_ZERO, }, // PTC12A
  2289. { KTX_COMPRESSED_RGBA_PVRTC_4BPPV1_IMG, KTX_COMPRESSED_SRGB_ALPHA_PVRTC_4BPPV1_EXT, KTX_COMPRESSED_RGBA_PVRTC_4BPPV1_IMG, KTX_ZERO, }, // PTC14A
  2290. { KTX_COMPRESSED_RGBA_PVRTC_2BPPV2_IMG, KTX_ZERO, KTX_COMPRESSED_RGBA_PVRTC_2BPPV2_IMG, KTX_ZERO, }, // PTC22
  2291. { KTX_COMPRESSED_RGBA_PVRTC_4BPPV2_IMG, KTX_ZERO, KTX_COMPRESSED_RGBA_PVRTC_4BPPV2_IMG, KTX_ZERO, }, // PTC24
  2292. { KTX_ATC_RGB_AMD, KTX_ZERO, KTX_ATC_RGB_AMD, KTX_ZERO, }, // ATC
  2293. { KTX_ATC_RGBA_EXPLICIT_ALPHA_AMD, KTX_ZERO, KTX_ATC_RGBA_EXPLICIT_ALPHA_AMD, KTX_ZERO, }, // ATCE
  2294. { KTX_ATC_RGBA_INTERPOLATED_ALPHA_AMD, KTX_ZERO, KTX_ATC_RGBA_INTERPOLATED_ALPHA_AMD, KTX_ZERO, }, // ATCI
  2295. { KTX_COMPRESSED_RGBA_ASTC_4x4_KHR, KTX_COMPRESSED_SRGB8_ALPHA8_ASTC_4x4_KHR, KTX_COMPRESSED_RGBA_ASTC_4x4_KHR, KTX_ZERO, }, // ASTC4x4
  2296. { KTX_COMPRESSED_RGBA_ASTC_5x5_KHR, KTX_COMPRESSED_SRGB8_ALPHA8_ASTC_5x5_KHR, KTX_COMPRESSED_RGBA_ASTC_5x5_KHR, KTX_ZERO, }, // ASTC5x5
  2297. { KTX_COMPRESSED_RGBA_ASTC_6x6_KHR, KTX_COMPRESSED_SRGB8_ALPHA8_ASTC_6x6_KHR, KTX_COMPRESSED_RGBA_ASTC_6x6_KHR, KTX_ZERO, }, // ASTC6x6
  2298. { KTX_COMPRESSED_RGBA_ASTC_8x5_KHR, KTX_COMPRESSED_SRGB8_ALPHA8_ASTC_8x5_KHR, KTX_COMPRESSED_RGBA_ASTC_8x5_KHR, KTX_ZERO, }, // ASTC8x5
  2299. { KTX_COMPRESSED_RGBA_ASTC_8x6_KHR, KTX_COMPRESSED_SRGB8_ALPHA8_ASTC_8x6_KHR, KTX_COMPRESSED_RGBA_ASTC_8x6_KHR, KTX_ZERO, }, // ASTC8x6
  2300. { KTX_COMPRESSED_RGBA_ASTC_10x5_KHR, KTX_COMPRESSED_SRGB8_ALPHA8_ASTC_10x5_KHR, KTX_COMPRESSED_RGBA_ASTC_10x5_KHR, KTX_ZERO, }, // ASTC10x5
  2301. { KTX_ZERO, KTX_ZERO, KTX_ZERO, KTX_ZERO, }, // Unknown
  2302. { KTX_ZERO, KTX_ZERO, KTX_ZERO, KTX_ZERO, }, // R1
  2303. { KTX_ALPHA, KTX_ZERO, KTX_ALPHA, KTX_UNSIGNED_BYTE, }, // A8
  2304. { KTX_R8, KTX_ZERO, KTX_RED, KTX_UNSIGNED_BYTE, }, // R8
  2305. { KTX_R8I, KTX_ZERO, KTX_RED, KTX_BYTE, }, // R8S
  2306. { KTX_R8UI, KTX_ZERO, KTX_RED, KTX_UNSIGNED_BYTE, }, // R8S
  2307. { KTX_R8_SNORM, KTX_ZERO, KTX_RED, KTX_BYTE, }, // R8S
  2308. { KTX_R16, KTX_ZERO, KTX_RED, KTX_UNSIGNED_SHORT, }, // R16
  2309. { KTX_R16I, KTX_ZERO, KTX_RED, KTX_SHORT, }, // R16I
  2310. { KTX_R16UI, KTX_ZERO, KTX_RED, KTX_UNSIGNED_SHORT, }, // R16U
  2311. { KTX_R16F, KTX_ZERO, KTX_RED, KTX_HALF_FLOAT, }, // R16F
  2312. { KTX_R16_SNORM, KTX_ZERO, KTX_RED, KTX_SHORT, }, // R16S
  2313. { KTX_R32I, KTX_ZERO, KTX_RED, KTX_INT, }, // R32I
  2314. { KTX_R32UI, KTX_ZERO, KTX_RED, KTX_UNSIGNED_INT, }, // R32U
  2315. { KTX_R32F, KTX_ZERO, KTX_RED, KTX_FLOAT, }, // R32F
  2316. { KTX_RG8, KTX_ZERO, KTX_RG, KTX_UNSIGNED_BYTE, }, // RG8
  2317. { KTX_RG8I, KTX_ZERO, KTX_RG, KTX_BYTE, }, // RG8I
  2318. { KTX_RG8UI, KTX_ZERO, KTX_RG, KTX_UNSIGNED_BYTE, }, // RG8U
  2319. { KTX_RG8_SNORM, KTX_ZERO, KTX_RG, KTX_BYTE, }, // RG8S
  2320. { KTX_RG16, KTX_ZERO, KTX_RG, KTX_UNSIGNED_SHORT, }, // RG16
  2321. { KTX_RG16I, KTX_ZERO, KTX_RG, KTX_SHORT, }, // RG16
  2322. { KTX_RG16UI, KTX_ZERO, KTX_RG, KTX_UNSIGNED_SHORT, }, // RG16
  2323. { KTX_RG16F, KTX_ZERO, KTX_RG, KTX_FLOAT, }, // RG16F
  2324. { KTX_RG16_SNORM, KTX_ZERO, KTX_RG, KTX_SHORT, }, // RG16S
  2325. { KTX_RG32I, KTX_ZERO, KTX_RG, KTX_INT, }, // RG32I
  2326. { KTX_RG32UI, KTX_ZERO, KTX_RG, KTX_UNSIGNED_INT, }, // RG32U
  2327. { KTX_RG32F, KTX_ZERO, KTX_RG, KTX_FLOAT, }, // RG32F
  2328. { KTX_RGB8, KTX_SRGB8, KTX_RGB, KTX_UNSIGNED_BYTE, }, // RGB8
  2329. { KTX_RGB8I, KTX_ZERO, KTX_RGB, KTX_BYTE, }, // RGB8I
  2330. { KTX_RGB8UI, KTX_ZERO, KTX_RGB, KTX_UNSIGNED_BYTE, }, // RGB8U
  2331. { KTX_RGB8_SNORM, KTX_ZERO, KTX_RGB, KTX_BYTE, }, // RGB8S
  2332. { KTX_RGB9_E5, KTX_ZERO, KTX_RGB, KTX_UNSIGNED_INT_5_9_9_9_REV, }, // RGB9E5F
  2333. { KTX_BGRA, KTX_SRGB8_ALPHA8, KTX_BGRA, KTX_UNSIGNED_BYTE, }, // BGRA8
  2334. { KTX_RGBA8, KTX_SRGB8_ALPHA8, KTX_RGBA, KTX_UNSIGNED_BYTE, }, // RGBA8
  2335. { KTX_RGBA8I, KTX_ZERO, KTX_RGBA, KTX_BYTE, }, // RGBA8I
  2336. { KTX_RGBA8UI, KTX_ZERO, KTX_RGBA, KTX_UNSIGNED_BYTE, }, // RGBA8U
  2337. { KTX_RGBA8_SNORM, KTX_ZERO, KTX_RGBA, KTX_BYTE, }, // RGBA8S
  2338. { KTX_RGBA16, KTX_ZERO, KTX_RGBA, KTX_UNSIGNED_SHORT, }, // RGBA16
  2339. { KTX_RGBA16I, KTX_ZERO, KTX_RGBA, KTX_SHORT, }, // RGBA16I
  2340. { KTX_RGBA16UI, KTX_ZERO, KTX_RGBA, KTX_UNSIGNED_SHORT, }, // RGBA16U
  2341. { KTX_RGBA16F, KTX_ZERO, KTX_RGBA, KTX_HALF_FLOAT, }, // RGBA16F
  2342. { KTX_RGBA16_SNORM, KTX_ZERO, KTX_RGBA, KTX_SHORT, }, // RGBA16S
  2343. { KTX_RGBA32I, KTX_ZERO, KTX_RGBA, KTX_INT, }, // RGBA32I
  2344. { KTX_RGBA32UI, KTX_ZERO, KTX_RGBA, KTX_UNSIGNED_INT, }, // RGBA32U
  2345. { KTX_RGBA32F, KTX_ZERO, KTX_RGBA, KTX_FLOAT, }, // RGBA32F
  2346. { KTX_RGB565, KTX_ZERO, KTX_RGB, KTX_UNSIGNED_SHORT_5_6_5, }, // R5G6B5
  2347. { KTX_RGBA4, KTX_ZERO, KTX_RGBA, KTX_UNSIGNED_SHORT_4_4_4_4, }, // RGBA4
  2348. { KTX_RGB5_A1, KTX_ZERO, KTX_RGBA, KTX_UNSIGNED_SHORT_5_5_5_1, }, // RGB5A1
  2349. { KTX_RGB10_A2, KTX_ZERO, KTX_RGBA, KTX_UNSIGNED_INT_2_10_10_10_REV, }, // RGB10A2
  2350. { KTX_R11F_G11F_B10F, KTX_ZERO, KTX_RGB, KTX_UNSIGNED_INT_10F_11F_11F_REV, }, // RG11B10F
  2351. };
  2352. BX_STATIC_ASSERT(TextureFormat::UnknownDepth == BX_COUNTOF(s_translateKtxFormat) );
  2353. struct KtxFormatInfo2
  2354. {
  2355. uint32_t m_internalFmt;
  2356. TextureFormat::Enum m_format;
  2357. };
  2358. static const KtxFormatInfo2 s_translateKtxFormat2[] =
  2359. {
  2360. { KTX_A8, TextureFormat::A8 },
  2361. { KTX_RED, TextureFormat::R8 },
  2362. { KTX_RGB, TextureFormat::RGB8 },
  2363. { KTX_RGBA, TextureFormat::RGBA8 },
  2364. { KTX_COMPRESSED_RGB_S3TC_DXT1_EXT, TextureFormat::BC1 },
  2365. };
  2366. bool imageParseKtx(ImageContainer& _imageContainer, bx::ReaderSeekerI* _reader, bx::Error* _err)
  2367. {
  2368. BX_ERROR_SCOPE(_err);
  2369. uint8_t identifier[8];
  2370. bx::read(_reader, identifier);
  2371. if (identifier[1] != '1'
  2372. && identifier[2] != '1')
  2373. {
  2374. return false;
  2375. }
  2376. uint32_t endianness;
  2377. bx::read(_reader, endianness);
  2378. bool fromLittleEndian = 0x04030201 == endianness;
  2379. uint32_t glType;
  2380. bx::readHE(_reader, glType, fromLittleEndian);
  2381. uint32_t glTypeSize;
  2382. bx::readHE(_reader, glTypeSize, fromLittleEndian);
  2383. uint32_t glFormat;
  2384. bx::readHE(_reader, glFormat, fromLittleEndian);
  2385. uint32_t glInternalFormat;
  2386. bx::readHE(_reader, glInternalFormat, fromLittleEndian);
  2387. uint32_t glBaseInternalFormat;
  2388. bx::readHE(_reader, glBaseInternalFormat, fromLittleEndian);
  2389. uint32_t width;
  2390. bx::readHE(_reader, width, fromLittleEndian);
  2391. uint32_t height;
  2392. bx::readHE(_reader, height, fromLittleEndian);
  2393. uint32_t depth;
  2394. bx::readHE(_reader, depth, fromLittleEndian);
  2395. uint32_t numberOfArrayElements;
  2396. bx::readHE(_reader, numberOfArrayElements, fromLittleEndian);
  2397. uint32_t numFaces;
  2398. bx::readHE(_reader, numFaces, fromLittleEndian);
  2399. uint32_t numMips;
  2400. bx::readHE(_reader, numMips, fromLittleEndian);
  2401. uint32_t metaDataSize;
  2402. bx::readHE(_reader, metaDataSize, fromLittleEndian);
  2403. // skip meta garbage...
  2404. int64_t offset = bx::skip(_reader, metaDataSize);
  2405. TextureFormat::Enum format = TextureFormat::Unknown;
  2406. bool hasAlpha = false;
  2407. for (uint32_t ii = 0; ii < BX_COUNTOF(s_translateKtxFormat); ++ii)
  2408. {
  2409. if (s_translateKtxFormat[ii].m_internalFmt == glInternalFormat)
  2410. {
  2411. format = TextureFormat::Enum(ii);
  2412. break;
  2413. }
  2414. }
  2415. if (TextureFormat::Unknown == format)
  2416. {
  2417. for (uint32_t ii = 0; ii < BX_COUNTOF(s_translateKtxFormat2); ++ii)
  2418. {
  2419. if (s_translateKtxFormat2[ii].m_internalFmt == glInternalFormat)
  2420. {
  2421. format = s_translateKtxFormat2[ii].m_format;
  2422. break;
  2423. }
  2424. }
  2425. }
  2426. _imageContainer.m_allocator = NULL;
  2427. _imageContainer.m_data = NULL;
  2428. _imageContainer.m_size = 0;
  2429. _imageContainer.m_offset = (uint32_t)offset;
  2430. _imageContainer.m_width = width;
  2431. _imageContainer.m_height = height;
  2432. _imageContainer.m_depth = depth;
  2433. _imageContainer.m_format = format;
  2434. _imageContainer.m_orientation = Orientation::R0;
  2435. _imageContainer.m_numLayers = uint16_t(bx::uint32_max(numberOfArrayElements, 1) );
  2436. _imageContainer.m_numMips = uint8_t(bx::uint32_max(numMips, 1) );
  2437. _imageContainer.m_hasAlpha = hasAlpha;
  2438. _imageContainer.m_cubeMap = numFaces > 1;
  2439. _imageContainer.m_ktx = true;
  2440. _imageContainer.m_ktxLE = fromLittleEndian;
  2441. _imageContainer.m_srgb = false;
  2442. if (TextureFormat::Unknown == format)
  2443. {
  2444. BX_ERROR_SET(_err, BIMG_ERROR, "Unrecognized image format.");
  2445. return false;
  2446. }
  2447. return true;
  2448. }
  2449. ImageContainer* imageParseKtx(bx::AllocatorI* _allocator, const void* _src, uint32_t _size, bx::Error* _err)
  2450. {
  2451. return imageParseT<KTX_MAGIC, imageParseKtx>(_allocator, _src, _size, _err);
  2452. }
  2453. // PVR3
  2454. #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) )
  2455. #define PVR3_MAGIC BX_MAKEFOURCC('P', 'V', 'R', 3)
  2456. #define PVR3_HEADER_SIZE 52
  2457. #define PVR3_PVRTC1_2BPP_RGB 0
  2458. #define PVR3_PVRTC1_2BPP_RGBA 1
  2459. #define PVR3_PVRTC1_4BPP_RGB 2
  2460. #define PVR3_PVRTC1_4BPP_RGBA 3
  2461. #define PVR3_PVRTC2_2BPP_RGBA 4
  2462. #define PVR3_PVRTC2_4BPP_RGBA 5
  2463. #define PVR3_ETC1 6
  2464. #define PVR3_DXT1 7
  2465. #define PVR3_DXT2 8
  2466. #define PVR3_DXT3 9
  2467. #define PVR3_DXT4 10
  2468. #define PVR3_DXT5 11
  2469. #define PVR3_BC4 12
  2470. #define PVR3_BC5 13
  2471. #define PVR3_R8 PVR3_MAKE8CC('r', 0, 0, 0, 8, 0, 0, 0)
  2472. #define PVR3_R16 PVR3_MAKE8CC('r', 0, 0, 0, 16, 0, 0, 0)
  2473. #define PVR3_R32 PVR3_MAKE8CC('r', 0, 0, 0, 32, 0, 0, 0)
  2474. #define PVR3_RG8 PVR3_MAKE8CC('r', 'g', 0, 0, 8, 8, 0, 0)
  2475. #define PVR3_RG16 PVR3_MAKE8CC('r', 'g', 0, 0, 16, 16, 0, 0)
  2476. #define PVR3_RG32 PVR3_MAKE8CC('r', 'g', 0, 0, 32, 32, 0, 0)
  2477. #define PVR3_BGRA8 PVR3_MAKE8CC('b', 'g', 'r', 'a', 8, 8, 8, 8)
  2478. #define PVR3_RGBA16 PVR3_MAKE8CC('r', 'g', 'b', 'a', 16, 16, 16, 16)
  2479. #define PVR3_RGBA32 PVR3_MAKE8CC('r', 'g', 'b', 'a', 32, 32, 32, 32)
  2480. #define PVR3_RGB565 PVR3_MAKE8CC('r', 'g', 'b', 0, 5, 6, 5, 0)
  2481. #define PVR3_RGBA4 PVR3_MAKE8CC('r', 'g', 'b', 'a', 4, 4, 4, 4)
  2482. #define PVR3_RGBA51 PVR3_MAKE8CC('r', 'g', 'b', 'a', 5, 5, 5, 1)
  2483. #define PVR3_RGB10A2 PVR3_MAKE8CC('r', 'g', 'b', 'a', 10, 10, 10, 2)
  2484. #define PVR3_CHANNEL_TYPE_ANY UINT32_MAX
  2485. #define PVR3_CHANNEL_TYPE_FLOAT UINT32_C(12)
  2486. struct TranslatePvr3Format
  2487. {
  2488. uint64_t m_format;
  2489. uint32_t m_channelTypeMask;
  2490. TextureFormat::Enum m_textureFormat;
  2491. };
  2492. static const TranslatePvr3Format s_translatePvr3Format[] =
  2493. {
  2494. { PVR3_PVRTC1_2BPP_RGB, PVR3_CHANNEL_TYPE_ANY, TextureFormat::PTC12 },
  2495. { PVR3_PVRTC1_2BPP_RGBA, PVR3_CHANNEL_TYPE_ANY, TextureFormat::PTC12A },
  2496. { PVR3_PVRTC1_4BPP_RGB, PVR3_CHANNEL_TYPE_ANY, TextureFormat::PTC14 },
  2497. { PVR3_PVRTC1_4BPP_RGBA, PVR3_CHANNEL_TYPE_ANY, TextureFormat::PTC14A },
  2498. { PVR3_PVRTC2_2BPP_RGBA, PVR3_CHANNEL_TYPE_ANY, TextureFormat::PTC22 },
  2499. { PVR3_PVRTC2_4BPP_RGBA, PVR3_CHANNEL_TYPE_ANY, TextureFormat::PTC24 },
  2500. { PVR3_ETC1, PVR3_CHANNEL_TYPE_ANY, TextureFormat::ETC1 },
  2501. { PVR3_DXT1, PVR3_CHANNEL_TYPE_ANY, TextureFormat::BC1 },
  2502. { PVR3_DXT2, PVR3_CHANNEL_TYPE_ANY, TextureFormat::BC2 },
  2503. { PVR3_DXT3, PVR3_CHANNEL_TYPE_ANY, TextureFormat::BC2 },
  2504. { PVR3_DXT4, PVR3_CHANNEL_TYPE_ANY, TextureFormat::BC3 },
  2505. { PVR3_DXT5, PVR3_CHANNEL_TYPE_ANY, TextureFormat::BC3 },
  2506. { PVR3_BC4, PVR3_CHANNEL_TYPE_ANY, TextureFormat::BC4 },
  2507. { PVR3_BC5, PVR3_CHANNEL_TYPE_ANY, TextureFormat::BC5 },
  2508. { PVR3_R8, PVR3_CHANNEL_TYPE_ANY, TextureFormat::R8 },
  2509. { PVR3_R16, PVR3_CHANNEL_TYPE_ANY, TextureFormat::R16U },
  2510. { PVR3_R16, PVR3_CHANNEL_TYPE_FLOAT, TextureFormat::R16F },
  2511. { PVR3_R32, PVR3_CHANNEL_TYPE_ANY, TextureFormat::R32U },
  2512. { PVR3_R32, PVR3_CHANNEL_TYPE_FLOAT, TextureFormat::R32F },
  2513. { PVR3_RG8, PVR3_CHANNEL_TYPE_ANY, TextureFormat::RG8 },
  2514. { PVR3_RG16, PVR3_CHANNEL_TYPE_ANY, TextureFormat::RG16 },
  2515. { PVR3_RG16, PVR3_CHANNEL_TYPE_FLOAT, TextureFormat::RG16F },
  2516. { PVR3_RG32, PVR3_CHANNEL_TYPE_ANY, TextureFormat::RG16 },
  2517. { PVR3_RG32, PVR3_CHANNEL_TYPE_FLOAT, TextureFormat::RG32F },
  2518. { PVR3_BGRA8, PVR3_CHANNEL_TYPE_ANY, TextureFormat::BGRA8 },
  2519. { PVR3_RGBA16, PVR3_CHANNEL_TYPE_ANY, TextureFormat::RGBA16 },
  2520. { PVR3_RGBA16, PVR3_CHANNEL_TYPE_FLOAT, TextureFormat::RGBA16F },
  2521. { PVR3_RGBA32, PVR3_CHANNEL_TYPE_ANY, TextureFormat::RGBA32U },
  2522. { PVR3_RGBA32, PVR3_CHANNEL_TYPE_FLOAT, TextureFormat::RGBA32F },
  2523. { PVR3_RGB565, PVR3_CHANNEL_TYPE_ANY, TextureFormat::R5G6B5 },
  2524. { PVR3_RGBA4, PVR3_CHANNEL_TYPE_ANY, TextureFormat::RGBA4 },
  2525. { PVR3_RGBA51, PVR3_CHANNEL_TYPE_ANY, TextureFormat::RGB5A1 },
  2526. { PVR3_RGB10A2, PVR3_CHANNEL_TYPE_ANY, TextureFormat::RGB10A2 },
  2527. };
  2528. bool imageParsePvr3(ImageContainer& _imageContainer, bx::ReaderSeekerI* _reader, bx::Error* _err)
  2529. {
  2530. BX_ERROR_SCOPE(_err);
  2531. uint32_t flags;
  2532. bx::read(_reader, flags);
  2533. uint64_t pixelFormat;
  2534. bx::read(_reader, pixelFormat);
  2535. uint32_t colorSpace;
  2536. bx::read(_reader, colorSpace); // 0 - linearRGB, 1 - sRGB
  2537. uint32_t channelType;
  2538. bx::read(_reader, channelType);
  2539. uint32_t height;
  2540. bx::read(_reader, height);
  2541. uint32_t width;
  2542. bx::read(_reader, width);
  2543. uint32_t depth;
  2544. bx::read(_reader, depth);
  2545. uint32_t numSurfaces;
  2546. bx::read(_reader, numSurfaces);
  2547. uint32_t numFaces;
  2548. bx::read(_reader, numFaces);
  2549. uint32_t numMips;
  2550. bx::read(_reader, numMips);
  2551. uint32_t metaDataSize;
  2552. bx::read(_reader, metaDataSize);
  2553. // skip meta garbage...
  2554. int64_t offset = bx::skip(_reader, metaDataSize);
  2555. TextureFormat::Enum format = TextureFormat::Unknown;
  2556. bool hasAlpha = false;
  2557. for (uint32_t ii = 0; ii < BX_COUNTOF(s_translatePvr3Format); ++ii)
  2558. {
  2559. if (s_translatePvr3Format[ii].m_format == pixelFormat
  2560. && channelType == (s_translatePvr3Format[ii].m_channelTypeMask & channelType) )
  2561. {
  2562. format = s_translatePvr3Format[ii].m_textureFormat;
  2563. break;
  2564. }
  2565. }
  2566. _imageContainer.m_allocator = NULL;
  2567. _imageContainer.m_data = NULL;
  2568. _imageContainer.m_size = 0;
  2569. _imageContainer.m_offset = (uint32_t)offset;
  2570. _imageContainer.m_width = width;
  2571. _imageContainer.m_height = height;
  2572. _imageContainer.m_depth = depth;
  2573. _imageContainer.m_format = format;
  2574. _imageContainer.m_orientation = Orientation::R0;
  2575. _imageContainer.m_numLayers = 1;
  2576. _imageContainer.m_numMips = uint8_t(bx::uint32_max(numMips, 1) );
  2577. _imageContainer.m_hasAlpha = hasAlpha;
  2578. _imageContainer.m_cubeMap = numFaces > 1;
  2579. _imageContainer.m_ktx = false;
  2580. _imageContainer.m_ktxLE = false;
  2581. _imageContainer.m_srgb = colorSpace > 0;
  2582. return TextureFormat::Unknown != format;
  2583. }
  2584. ImageContainer* imageParsePvr3(bx::AllocatorI* _allocator, const void* _src, uint32_t _size, bx::Error* _err)
  2585. {
  2586. return imageParseT<PVR3_MAGIC, imageParsePvr3>(_allocator, _src, _size, _err);
  2587. }
  2588. bool imageParse(ImageContainer& _imageContainer, bx::ReaderSeekerI* _reader, bx::Error* _err)
  2589. {
  2590. BX_ERROR_SCOPE(_err);
  2591. uint32_t magic;
  2592. bx::read(_reader, magic, _err);
  2593. if (DDS_MAGIC == magic)
  2594. {
  2595. return imageParseDds(_imageContainer, _reader, _err);
  2596. }
  2597. else if (KTX_MAGIC == magic)
  2598. {
  2599. return imageParseKtx(_imageContainer, _reader, _err);
  2600. }
  2601. else if (PVR3_MAGIC == magic)
  2602. {
  2603. return imageParsePvr3(_imageContainer, _reader, _err);
  2604. }
  2605. else if (BIMG_CHUNK_MAGIC_GNF == magic)
  2606. {
  2607. return imageParseGnf(_imageContainer, _reader, _err);
  2608. }
  2609. else if (BIMG_CHUNK_MAGIC_TEX == magic)
  2610. {
  2611. TextureCreate tc;
  2612. bx::read(_reader, tc);
  2613. _imageContainer.m_format = tc.m_format;
  2614. _imageContainer.m_orientation = Orientation::R0;
  2615. _imageContainer.m_offset = UINT32_MAX;
  2616. _imageContainer.m_allocator = NULL;
  2617. if (NULL == tc.m_mem)
  2618. {
  2619. _imageContainer.m_data = NULL;
  2620. _imageContainer.m_size = 0;
  2621. }
  2622. else
  2623. {
  2624. _imageContainer.m_data = tc.m_mem->data;
  2625. _imageContainer.m_size = tc.m_mem->size;
  2626. }
  2627. _imageContainer.m_width = tc.m_width;
  2628. _imageContainer.m_height = tc.m_height;
  2629. _imageContainer.m_depth = tc.m_depth;
  2630. _imageContainer.m_numLayers = tc.m_numLayers;
  2631. _imageContainer.m_numMips = tc.m_numMips;
  2632. _imageContainer.m_hasAlpha = false;
  2633. _imageContainer.m_cubeMap = tc.m_cubeMap;
  2634. _imageContainer.m_ktx = false;
  2635. _imageContainer.m_ktxLE = false;
  2636. _imageContainer.m_srgb = false;
  2637. return _err->isOk();
  2638. }
  2639. BX_TRACE("Unrecognized image format (magic: 0x%08x)!", magic);
  2640. BX_ERROR_SET(_err, BIMG_ERROR, "Unrecognized image format.");
  2641. return false;
  2642. }
  2643. bool imageParse(ImageContainer& _imageContainer, const void* _data, uint32_t _size, bx::Error* _err)
  2644. {
  2645. BX_ERROR_SCOPE(_err);
  2646. bx::MemoryReader reader(_data, _size);
  2647. return imageParse(_imageContainer, &reader, _err);
  2648. }
  2649. 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)
  2650. {
  2651. const uint8_t* src = (const uint8_t*)_src;
  2652. uint8_t* dst = (uint8_t*)_dst;
  2653. const uint32_t srcBpp = s_imageBlockInfo[_srcFormat].bitsPerPixel;
  2654. const uint32_t srcPitch = _width*srcBpp/8;
  2655. for (uint32_t zz = 0; zz < _depth; ++zz, src += _height*srcPitch, dst += _height*_dstPitch)
  2656. {
  2657. if (isCompressed(_srcFormat))
  2658. {
  2659. uint32_t size = imageGetSize(NULL, uint16_t(_width), uint16_t(_height), 0, false, false, 1, TextureFormat::RGBA8);
  2660. void* temp = BX_ALLOC(_allocator, size);
  2661. imageDecodeToRgba8(temp, _src, _width, _height, _width*4, _srcFormat);
  2662. imageConvert(dst, TextureFormat::R8, temp, TextureFormat::RGBA8, _width, _height, 1, _width*4);
  2663. BX_FREE(_allocator, temp);
  2664. }
  2665. else
  2666. {
  2667. imageConvert(dst, TextureFormat::R8, src, _srcFormat, _width, _height, 1, srcPitch);
  2668. }
  2669. }
  2670. }
  2671. void imageDecodeToBgra8(void* _dst, const void* _src, uint32_t _width, uint32_t _height, uint32_t _dstPitch, TextureFormat::Enum _srcFormat)
  2672. {
  2673. const uint8_t* src = (const uint8_t*)_src;
  2674. uint8_t* dst = (uint8_t*)_dst;
  2675. uint32_t width = _width/4;
  2676. uint32_t height = _height/4;
  2677. uint8_t temp[16*4];
  2678. switch (_srcFormat)
  2679. {
  2680. case TextureFormat::BC1:
  2681. for (uint32_t yy = 0; yy < height; ++yy)
  2682. {
  2683. for (uint32_t xx = 0; xx < width; ++xx)
  2684. {
  2685. decodeBlockDxt1(temp, src);
  2686. src += 8;
  2687. uint8_t* block = &dst[yy*_dstPitch*4 + xx*16];
  2688. bx::memCopy(&block[0*_dstPitch], &temp[ 0], 16);
  2689. bx::memCopy(&block[1*_dstPitch], &temp[16], 16);
  2690. bx::memCopy(&block[2*_dstPitch], &temp[32], 16);
  2691. bx::memCopy(&block[3*_dstPitch], &temp[48], 16);
  2692. }
  2693. }
  2694. break;
  2695. case TextureFormat::BC2:
  2696. for (uint32_t yy = 0; yy < height; ++yy)
  2697. {
  2698. for (uint32_t xx = 0; xx < width; ++xx)
  2699. {
  2700. decodeBlockDxt23A(temp+3, src);
  2701. src += 8;
  2702. decodeBlockDxt(temp, src);
  2703. src += 8;
  2704. uint8_t* block = &dst[yy*_dstPitch*4 + xx*16];
  2705. bx::memCopy(&block[0*_dstPitch], &temp[ 0], 16);
  2706. bx::memCopy(&block[1*_dstPitch], &temp[16], 16);
  2707. bx::memCopy(&block[2*_dstPitch], &temp[32], 16);
  2708. bx::memCopy(&block[3*_dstPitch], &temp[48], 16);
  2709. }
  2710. }
  2711. break;
  2712. case TextureFormat::BC3:
  2713. for (uint32_t yy = 0; yy < height; ++yy)
  2714. {
  2715. for (uint32_t xx = 0; xx < width; ++xx)
  2716. {
  2717. decodeBlockDxt45A(temp+3, src);
  2718. src += 8;
  2719. decodeBlockDxt(temp, src);
  2720. src += 8;
  2721. uint8_t* block = &dst[yy*_dstPitch*4 + xx*16];
  2722. bx::memCopy(&block[0*_dstPitch], &temp[ 0], 16);
  2723. bx::memCopy(&block[1*_dstPitch], &temp[16], 16);
  2724. bx::memCopy(&block[2*_dstPitch], &temp[32], 16);
  2725. bx::memCopy(&block[3*_dstPitch], &temp[48], 16);
  2726. }
  2727. }
  2728. break;
  2729. case TextureFormat::BC4:
  2730. for (uint32_t yy = 0; yy < height; ++yy)
  2731. {
  2732. for (uint32_t xx = 0; xx < width; ++xx)
  2733. {
  2734. decodeBlockDxt45A(temp, src);
  2735. src += 8;
  2736. uint8_t* block = &dst[yy*_dstPitch*4 + xx*16];
  2737. bx::memCopy(&block[0*_dstPitch], &temp[ 0], 16);
  2738. bx::memCopy(&block[1*_dstPitch], &temp[16], 16);
  2739. bx::memCopy(&block[2*_dstPitch], &temp[32], 16);
  2740. bx::memCopy(&block[3*_dstPitch], &temp[48], 16);
  2741. }
  2742. }
  2743. break;
  2744. case TextureFormat::BC5:
  2745. for (uint32_t yy = 0; yy < height; ++yy)
  2746. {
  2747. for (uint32_t xx = 0; xx < width; ++xx)
  2748. {
  2749. decodeBlockDxt45A(temp+2, src);
  2750. src += 8;
  2751. decodeBlockDxt45A(temp+1, src);
  2752. src += 8;
  2753. for (uint32_t ii = 0; ii < 16; ++ii)
  2754. {
  2755. float nx = temp[ii*4+2]*2.0f/255.0f - 1.0f;
  2756. float ny = temp[ii*4+1]*2.0f/255.0f - 1.0f;
  2757. float nz = bx::sqrt(1.0f - nx*nx - ny*ny);
  2758. temp[ii*4+0] = uint8_t( (nz + 1.0f)*255.0f/2.0f);
  2759. temp[ii*4+3] = 0;
  2760. }
  2761. uint8_t* block = &dst[yy*_dstPitch*4 + xx*16];
  2762. bx::memCopy(&block[0*_dstPitch], &temp[ 0], 16);
  2763. bx::memCopy(&block[1*_dstPitch], &temp[16], 16);
  2764. bx::memCopy(&block[2*_dstPitch], &temp[32], 16);
  2765. bx::memCopy(&block[3*_dstPitch], &temp[48], 16);
  2766. }
  2767. }
  2768. break;
  2769. case TextureFormat::ETC1:
  2770. case TextureFormat::ETC2:
  2771. for (uint32_t yy = 0; yy < height; ++yy)
  2772. {
  2773. for (uint32_t xx = 0; xx < width; ++xx)
  2774. {
  2775. decodeBlockEtc12(temp, src);
  2776. src += 8;
  2777. uint8_t* block = &dst[yy*_dstPitch*4 + xx*16];
  2778. bx::memCopy(&block[0*_dstPitch], &temp[ 0], 16);
  2779. bx::memCopy(&block[1*_dstPitch], &temp[16], 16);
  2780. bx::memCopy(&block[2*_dstPitch], &temp[32], 16);
  2781. bx::memCopy(&block[3*_dstPitch], &temp[48], 16);
  2782. }
  2783. }
  2784. break;
  2785. case TextureFormat::ETC2A:
  2786. BX_WARN(false, "ETC2A decoder is not implemented.");
  2787. imageCheckerboard(_dst, _width, _height, 16, UINT32_C(0xff000000), UINT32_C(0xff00ff00) );
  2788. break;
  2789. case TextureFormat::ETC2A1:
  2790. BX_WARN(false, "ETC2A1 decoder is not implemented.");
  2791. imageCheckerboard(_dst, _width, _height, 16, UINT32_C(0xff000000), UINT32_C(0xffff0000) );
  2792. break;
  2793. case TextureFormat::PTC12:
  2794. BX_WARN(false, "PTC12 decoder is not implemented.");
  2795. imageCheckerboard(_dst, _width, _height, 16, UINT32_C(0xff000000), UINT32_C(0xffff00ff) );
  2796. break;
  2797. case TextureFormat::PTC12A:
  2798. BX_WARN(false, "PTC12A decoder is not implemented.");
  2799. imageCheckerboard(_dst, _width, _height, 16, UINT32_C(0xff000000), UINT32_C(0xffffff00) );
  2800. break;
  2801. case TextureFormat::PTC14:
  2802. for (uint32_t yy = 0; yy < height; ++yy)
  2803. {
  2804. for (uint32_t xx = 0; xx < width; ++xx)
  2805. {
  2806. decodeBlockPtc14(temp, src, xx, yy, width, height);
  2807. uint8_t* block = &dst[yy*_dstPitch*4 + xx*16];
  2808. bx::memCopy(&block[0*_dstPitch], &temp[ 0], 16);
  2809. bx::memCopy(&block[1*_dstPitch], &temp[16], 16);
  2810. bx::memCopy(&block[2*_dstPitch], &temp[32], 16);
  2811. bx::memCopy(&block[3*_dstPitch], &temp[48], 16);
  2812. }
  2813. }
  2814. break;
  2815. case TextureFormat::PTC14A:
  2816. for (uint32_t yy = 0; yy < height; ++yy)
  2817. {
  2818. for (uint32_t xx = 0; xx < width; ++xx)
  2819. {
  2820. decodeBlockPtc14A(temp, src, xx, yy, width, height);
  2821. uint8_t* block = &dst[yy*_dstPitch*4 + xx*16];
  2822. bx::memCopy(&block[0*_dstPitch], &temp[ 0], 16);
  2823. bx::memCopy(&block[1*_dstPitch], &temp[16], 16);
  2824. bx::memCopy(&block[2*_dstPitch], &temp[32], 16);
  2825. bx::memCopy(&block[3*_dstPitch], &temp[48], 16);
  2826. }
  2827. }
  2828. break;
  2829. case TextureFormat::PTC22:
  2830. BX_WARN(false, "PTC22 decoder is not implemented.");
  2831. imageCheckerboard(_dst, _width, _height, 16, UINT32_C(0xff00ff00), UINT32_C(0xff0000ff) );
  2832. break;
  2833. case TextureFormat::PTC24:
  2834. BX_WARN(false, "PTC24 decoder is not implemented.");
  2835. imageCheckerboard(_dst, _width, _height, 16, UINT32_C(0xff000000), UINT32_C(0xffffffff) );
  2836. break;
  2837. case TextureFormat::ATC:
  2838. for (uint32_t yy = 0; yy < height; ++yy)
  2839. {
  2840. for (uint32_t xx = 0; xx < width; ++xx)
  2841. {
  2842. decodeBlockATC(temp, src);
  2843. src += 8;
  2844. uint8_t* block = &dst[(yy*_dstPitch+xx*4)*4];
  2845. bx::memCopy(&block[0*_dstPitch], &temp[ 0], 16);
  2846. bx::memCopy(&block[1*_dstPitch], &temp[16], 16);
  2847. bx::memCopy(&block[2*_dstPitch], &temp[32], 16);
  2848. bx::memCopy(&block[3*_dstPitch], &temp[48], 16);
  2849. }
  2850. }
  2851. break;
  2852. case TextureFormat::ATCE:
  2853. for (uint32_t yy = 0; yy < height; ++yy)
  2854. {
  2855. for (uint32_t xx = 0; xx < width; ++xx)
  2856. {
  2857. decodeBlockDxt23A(temp+3, src);
  2858. src += 8;
  2859. decodeBlockATC(temp, src);
  2860. src += 8;
  2861. uint8_t* block = &dst[(yy*_dstPitch+xx*4)*4];
  2862. bx::memCopy(&block[0*_dstPitch], &temp[ 0], 16);
  2863. bx::memCopy(&block[1*_dstPitch], &temp[16], 16);
  2864. bx::memCopy(&block[2*_dstPitch], &temp[32], 16);
  2865. bx::memCopy(&block[3*_dstPitch], &temp[48], 16);
  2866. }
  2867. }
  2868. break;
  2869. case TextureFormat::ATCI:
  2870. for (uint32_t yy = 0; yy < height; ++yy)
  2871. {
  2872. for (uint32_t xx = 0; xx < width; ++xx)
  2873. {
  2874. decodeBlockDxt45A(temp+3, src);
  2875. src += 8;
  2876. decodeBlockATC(temp, src);
  2877. src += 8;
  2878. uint8_t* block = &dst[(yy*_dstPitch+xx*4)*4];
  2879. bx::memCopy(&block[0*_dstPitch], &temp[ 0], 16);
  2880. bx::memCopy(&block[1*_dstPitch], &temp[16], 16);
  2881. bx::memCopy(&block[2*_dstPitch], &temp[32], 16);
  2882. bx::memCopy(&block[3*_dstPitch], &temp[48], 16);
  2883. }
  2884. }
  2885. break;
  2886. case TextureFormat::ASTC4x4:
  2887. case TextureFormat::ASTC5x5:
  2888. case TextureFormat::ASTC6x6:
  2889. case TextureFormat::ASTC8x5:
  2890. case TextureFormat::ASTC8x6:
  2891. case TextureFormat::ASTC10x5:
  2892. BX_WARN(false, "ASTC decoder is not implemented.");
  2893. imageCheckerboard(_dst, _width, _height, 16, UINT32_C(0xff000000), UINT32_C(0xffffff00) );
  2894. break;
  2895. case TextureFormat::RGBA8:
  2896. {
  2897. const uint32_t srcPitch = _width * 4;
  2898. imageSwizzleBgra8(_dst, _dstPitch, _width, _height, _src, srcPitch);
  2899. }
  2900. break;
  2901. case TextureFormat::BGRA8:
  2902. {
  2903. const uint32_t srcPitch = _width * 4;
  2904. const uint32_t size = bx::uint32_min(srcPitch, _dstPitch);
  2905. bx::memCopy(_dst, _src, size, _height, srcPitch, _dstPitch);
  2906. }
  2907. break;
  2908. default:
  2909. {
  2910. const uint32_t srcBpp = s_imageBlockInfo[_srcFormat].bitsPerPixel;
  2911. const uint32_t srcPitch = _width * srcBpp / 8;
  2912. if (!imageConvert(_dst, TextureFormat::BGRA8, _src, _srcFormat, _width, _height, 1, srcPitch) )
  2913. {
  2914. // Failed to convert, just make ugly red-yellow checkerboard texture.
  2915. imageCheckerboard(_dst, _width, _height, 16, UINT32_C(0xffff0000), UINT32_C(0xffffff00) );
  2916. }
  2917. }
  2918. break;
  2919. }
  2920. }
  2921. void imageDecodeToRgba8(void* _dst, const void* _src, uint32_t _width, uint32_t _height, uint32_t _dstPitch, TextureFormat::Enum _srcFormat)
  2922. {
  2923. switch (_srcFormat)
  2924. {
  2925. case TextureFormat::RGBA8:
  2926. {
  2927. const uint32_t srcPitch = _width * 4;
  2928. const uint32_t size = bx::uint32_min(srcPitch, _dstPitch);
  2929. bx::memCopy(_dst, _src, size, _height, srcPitch, _dstPitch);
  2930. }
  2931. break;
  2932. case TextureFormat::BGRA8:
  2933. {
  2934. const uint32_t srcPitch = _width * 4;
  2935. imageSwizzleBgra8(_dst, _dstPitch, _width, _height, _src, srcPitch);
  2936. }
  2937. break;
  2938. default:
  2939. {
  2940. const uint32_t srcPitch = _width * 4;
  2941. imageDecodeToBgra8(_dst, _src, _width, _height, _dstPitch, _srcFormat);
  2942. imageSwizzleBgra8(_dst, _dstPitch, _width, _height, _dst, srcPitch);
  2943. }
  2944. break;
  2945. }
  2946. }
  2947. void imageRgba8ToRgba32fRef(void* _dst, uint32_t _width, uint32_t _height, uint32_t _srcPitch, const void* _src)
  2948. {
  2949. const uint32_t dstWidth = _width;
  2950. const uint32_t dstHeight = _height;
  2951. if (0 == dstWidth
  2952. || 0 == dstHeight)
  2953. {
  2954. return;
  2955. }
  2956. float* dst = (float*)_dst;
  2957. const uint8_t* src = (const uint8_t*)_src;
  2958. for (uint32_t yy = 0, ystep = _srcPitch; yy < dstHeight; ++yy, src += ystep)
  2959. {
  2960. const uint8_t* rgba = src;
  2961. for (uint32_t xx = 0; xx < dstWidth; ++xx, rgba += 4, dst += 4)
  2962. {
  2963. dst[0] = bx::toLinear(rgba[0]);
  2964. dst[1] = bx::toLinear(rgba[1]);
  2965. dst[2] = bx::toLinear(rgba[2]);
  2966. dst[3] = rgba[3];
  2967. }
  2968. }
  2969. }
  2970. void imageRgba8ToRgba32f(void* _dst, uint32_t _width, uint32_t _height, uint32_t _srcPitch, const void* _src)
  2971. {
  2972. const uint32_t dstWidth = _width;
  2973. const uint32_t dstHeight = _height;
  2974. if (0 == dstWidth
  2975. || 0 == dstHeight)
  2976. {
  2977. return;
  2978. }
  2979. float* dst = (float*)_dst;
  2980. const uint8_t* src = (const uint8_t*)_src;
  2981. using namespace bx;
  2982. const simd128_t unpack = simd_ld(1.0f, 1.0f/256.0f, 1.0f/65536.0f, 1.0f/16777216.0f);
  2983. const simd128_t umask = simd_ild(0xff, 0xff00, 0xff0000, 0xff000000);
  2984. const simd128_t wflip = simd_ild(0, 0, 0, 0x80000000);
  2985. const simd128_t wadd = simd_ld(0.0f, 0.0f, 0.0f, 32768.0f*65536.0f);
  2986. for (uint32_t yy = 0, ystep = _srcPitch; yy < dstHeight; ++yy, src += ystep)
  2987. {
  2988. const uint8_t* rgba = src;
  2989. for (uint32_t xx = 0; xx < dstWidth; ++xx, rgba += 4, dst += 4)
  2990. {
  2991. const simd128_t abgr0 = simd_splat(rgba);
  2992. const simd128_t abgr0m = simd_and(abgr0, umask);
  2993. const simd128_t abgr0x = simd_xor(abgr0m, wflip);
  2994. const simd128_t abgr0f = simd_itof(abgr0x);
  2995. const simd128_t abgr0c = simd_add(abgr0f, wadd);
  2996. const simd128_t abgr0n = simd_mul(abgr0c, unpack);
  2997. simd_st(dst, abgr0n);
  2998. }
  2999. }
  3000. }
  3001. 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)
  3002. {
  3003. const uint8_t* src = (const uint8_t*)_src;
  3004. uint8_t* dst = (uint8_t*)_dst;
  3005. const uint32_t srcBpp = s_imageBlockInfo[_srcFormat].bitsPerPixel;
  3006. const uint32_t srcPitch = _width*srcBpp/8;
  3007. for (uint32_t zz = 0; zz < _depth; ++zz, src += _height*srcPitch, dst += _height*_dstPitch)
  3008. {
  3009. switch (_srcFormat)
  3010. {
  3011. case TextureFormat::BC5:
  3012. {
  3013. uint32_t width = _width/4;
  3014. uint32_t height = _height/4;
  3015. const uint8_t* srcData = src;
  3016. for (uint32_t yy = 0; yy < height; ++yy)
  3017. {
  3018. for (uint32_t xx = 0; xx < width; ++xx)
  3019. {
  3020. uint8_t temp[16*4];
  3021. decodeBlockDxt45A(temp+2, srcData);
  3022. srcData += 8;
  3023. decodeBlockDxt45A(temp+1, srcData);
  3024. srcData += 8;
  3025. for (uint32_t ii = 0; ii < 16; ++ii)
  3026. {
  3027. float nx = temp[ii*4+2]*2.0f/255.0f - 1.0f;
  3028. float ny = temp[ii*4+1]*2.0f/255.0f - 1.0f;
  3029. float nz = bx::sqrt(1.0f - nx*nx - ny*ny);
  3030. const uint32_t offset = (yy*4 + ii/4)*_width*16 + (xx*4 + ii%4)*16;
  3031. float* block = (float*)&dst[offset];
  3032. block[0] = nx;
  3033. block[1] = ny;
  3034. block[2] = nz;
  3035. block[3] = 0.0f;
  3036. }
  3037. }
  3038. }
  3039. }
  3040. break;
  3041. case TextureFormat::RGBA32F:
  3042. bx::memCopy(dst, src, _dstPitch*_height);
  3043. break;
  3044. default:
  3045. if (isCompressed(_srcFormat) )
  3046. {
  3047. uint32_t size = imageGetSize(NULL, uint16_t(_width), uint16_t(_height), 0, false, false, 1, TextureFormat::RGBA8);
  3048. void* temp = BX_ALLOC(_allocator, size);
  3049. imageDecodeToRgba8(temp, src, _width, _height, _width*4, _srcFormat);
  3050. imageRgba8ToRgba32f(dst, _width, _height, _width*4, temp);
  3051. BX_FREE(_allocator, temp);
  3052. }
  3053. else
  3054. {
  3055. imageConvert(dst, TextureFormat::RGBA32F, src, _srcFormat, _width, _height, 1, srcPitch);
  3056. }
  3057. break;
  3058. }
  3059. }
  3060. }
  3061. bool imageGetRawData(const ImageContainer& _imageContainer, uint16_t _side, uint8_t _lod, const void* _data, uint32_t _size, ImageMip& _mip)
  3062. {
  3063. uint32_t offset = _imageContainer.m_offset;
  3064. TextureFormat::Enum format = TextureFormat::Enum(_imageContainer.m_format);
  3065. bool hasAlpha = _imageContainer.m_hasAlpha;
  3066. const ImageBlockInfo& blockInfo = s_imageBlockInfo[format];
  3067. const uint8_t bpp = blockInfo.bitsPerPixel;
  3068. const uint32_t blockSize = blockInfo.blockSize;
  3069. const uint32_t blockWidth = blockInfo.blockWidth;
  3070. const uint32_t blockHeight = blockInfo.blockHeight;
  3071. const uint32_t minBlockX = blockInfo.minBlockX;
  3072. const uint32_t minBlockY = blockInfo.minBlockY;
  3073. if (UINT32_MAX == _imageContainer.m_offset)
  3074. {
  3075. if (NULL == _imageContainer.m_data)
  3076. {
  3077. return false;
  3078. }
  3079. offset = 0;
  3080. _data = _imageContainer.m_data;
  3081. _size = _imageContainer.m_size;
  3082. }
  3083. const uint8_t* data = (const uint8_t*)_data;
  3084. const uint16_t numSides = _imageContainer.m_numLayers * (_imageContainer.m_cubeMap ? 6 : 1);
  3085. if (_imageContainer.m_ktx)
  3086. {
  3087. uint32_t width = _imageContainer.m_width;
  3088. uint32_t height = _imageContainer.m_height;
  3089. uint32_t depth = _imageContainer.m_depth;
  3090. for (uint8_t lod = 0, num = _imageContainer.m_numMips; lod < num; ++lod)
  3091. {
  3092. uint32_t sourceSize = bx::toHostEndian(*(const uint32_t*)&data[offset], _imageContainer.m_ktxLE);
  3093. offset += sizeof(uint32_t);
  3094. depth = bx::uint32_max(1, depth);
  3095. uint32_t blocksX = bx::uint32_max(minBlockX, ((width + blockWidth - 1) / blockWidth ));
  3096. uint32_t blocksY = bx::uint32_max(minBlockY, ((height + blockHeight - 1) / blockHeight));
  3097. uint32_t destSize = blocksX * blocksY * blockSize * depth;
  3098. BX_CHECK(sourceSize == destSize, "KTX: Image size mismatch %d (expected %d).", sourceSize, destSize);
  3099. for (uint16_t side = 0; side < numSides; ++side)
  3100. {
  3101. if (side == _side
  3102. && lod == _lod)
  3103. {
  3104. _mip.m_width = blocksX * blockWidth;
  3105. _mip.m_height = blocksY * blockHeight;
  3106. _mip.m_depth = depth;
  3107. _mip.m_blockSize = blockSize;
  3108. _mip.m_size = destSize;
  3109. _mip.m_data = &data[offset];
  3110. _mip.m_bpp = bpp;
  3111. _mip.m_format = format;
  3112. _mip.m_hasAlpha = hasAlpha;
  3113. return true;
  3114. }
  3115. offset += sourceSize;
  3116. BX_CHECK(offset <= _size, "Reading past size of data buffer! (offset %d, size %d)", offset, _size);
  3117. BX_UNUSED(_size);
  3118. }
  3119. width >>= 1;
  3120. height >>= 1;
  3121. depth >>= 1;
  3122. }
  3123. }
  3124. else
  3125. {
  3126. for (uint16_t side = 0; side < numSides; ++side)
  3127. {
  3128. uint32_t width = _imageContainer.m_width;
  3129. uint32_t height = _imageContainer.m_height;
  3130. uint32_t depth = _imageContainer.m_depth;
  3131. for (uint8_t lod = 0, num = _imageContainer.m_numMips; lod < num; ++lod)
  3132. {
  3133. depth = bx::uint32_max(1, depth);
  3134. uint32_t blocksX = bx::uint32_max(minBlockX, ((width + blockWidth - 1) / blockWidth ));
  3135. uint32_t blocksY = bx::uint32_max(minBlockY, ((height + blockHeight - 1) / blockHeight));
  3136. uint32_t size = blocksX * blocksY * blockSize * depth;
  3137. if (side == _side
  3138. && lod == _lod)
  3139. {
  3140. _mip.m_width = blocksX * blockWidth;
  3141. _mip.m_height = blocksY * blockHeight;
  3142. _mip.m_depth = depth;
  3143. _mip.m_blockSize = blockSize;
  3144. _mip.m_size = size;
  3145. _mip.m_data = &data[offset];
  3146. _mip.m_bpp = bpp;
  3147. _mip.m_format = format;
  3148. _mip.m_hasAlpha = hasAlpha;
  3149. return true;
  3150. }
  3151. offset += size;
  3152. BX_CHECK(offset <= _size, "Reading past size of data buffer! (offset %d, size %d)", offset, _size);
  3153. BX_UNUSED(_size);
  3154. width >>= 1;
  3155. height >>= 1;
  3156. depth >>= 1;
  3157. }
  3158. }
  3159. }
  3160. return false;
  3161. }
  3162. 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)
  3163. {
  3164. BX_ERROR_SCOPE(_err);
  3165. uint8_t type = _grayscale ? 3 : 2;
  3166. uint8_t bpp = _grayscale ? 8 : 32;
  3167. uint8_t header[18] = {};
  3168. header[ 2] = type;
  3169. header[12] = _width &0xff;
  3170. header[13] = (_width >>8)&0xff;
  3171. header[14] = _height &0xff;
  3172. header[15] = (_height>>8)&0xff;
  3173. header[16] = bpp;
  3174. header[17] = 32;
  3175. int32_t total = 0;
  3176. total += bx::write(_writer, header, sizeof(header), _err);
  3177. uint32_t dstPitch = _width*bpp/8;
  3178. if (_yflip)
  3179. {
  3180. const uint8_t* data = (const uint8_t*)_src + _srcPitch*_height - _srcPitch;
  3181. for (uint32_t yy = 0; yy < _height && _err->isOk(); ++yy)
  3182. {
  3183. total += bx::write(_writer, data, dstPitch, _err);
  3184. data -= _srcPitch;
  3185. }
  3186. }
  3187. else if (_srcPitch == dstPitch)
  3188. {
  3189. total += bx::write(_writer, _src, _height*_srcPitch, _err);
  3190. }
  3191. else
  3192. {
  3193. const uint8_t* data = (const uint8_t*)_src;
  3194. for (uint32_t yy = 0; yy < _height && _err->isOk(); ++yy)
  3195. {
  3196. total += bx::write(_writer, data, dstPitch, _err);
  3197. data += _srcPitch;
  3198. }
  3199. }
  3200. return total;
  3201. }
  3202. template<typename Ty>
  3203. class HashWriter : public bx::WriterI
  3204. {
  3205. public:
  3206. HashWriter(bx::WriterI* _writer)
  3207. : m_writer(_writer)
  3208. {
  3209. begin();
  3210. }
  3211. void begin()
  3212. {
  3213. m_hash.begin();
  3214. }
  3215. uint32_t end()
  3216. {
  3217. return m_hash.end();
  3218. }
  3219. virtual int32_t write(const void* _data, int32_t _size, bx::Error* _err) override
  3220. {
  3221. m_hash.add(_data, _size);
  3222. return m_writer->write(_data, _size, _err);
  3223. }
  3224. private:
  3225. Ty m_hash;
  3226. bx::WriterI* m_writer;
  3227. };
  3228. 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)
  3229. {
  3230. BX_ERROR_SCOPE(_err);
  3231. switch (_format)
  3232. {
  3233. case TextureFormat::R8:
  3234. case TextureFormat::RGBA8:
  3235. case TextureFormat::BGRA8:
  3236. break;
  3237. default:
  3238. BX_ERROR_SET(_err, BIMG_ERROR, "PNG: Unsupported texture format.");
  3239. return 0;
  3240. }
  3241. const bool grayscale = TextureFormat::R8 == _format;
  3242. const bool bgra = TextureFormat::BGRA8 == _format;
  3243. int32_t total = 0;
  3244. total += bx::write(_writer, "\x89PNG\r\n\x1a\n", _err);
  3245. total += bx::write(_writer, bx::toBigEndian<uint32_t>(13), _err);
  3246. HashWriter<bx::HashCrc32> writerC(_writer);
  3247. total += bx::write(&writerC, "IHDR", _err);
  3248. total += bx::write(&writerC, bx::toBigEndian(_width), _err);
  3249. total += bx::write(&writerC, bx::toBigEndian(_height), _err);
  3250. total += bx::write(&writerC, "\x08\x06", _err);
  3251. total += bx::writeRep(&writerC, 0, 3, _err);
  3252. total += bx::write(_writer, bx::toBigEndian(writerC.end() ), _err);
  3253. const uint32_t bpp = grayscale ? 8 : 32;
  3254. const uint32_t stride = _width*bpp/8;
  3255. const uint16_t zlen = bx::toLittleEndian<uint16_t>(uint16_t(stride + 1) );
  3256. const uint16_t zlenC = bx::toLittleEndian<uint16_t>(~zlen);
  3257. total += bx::write(_writer, bx::toBigEndian<uint32_t>(_height*(stride+6)+6), _err);
  3258. writerC.begin();
  3259. total += bx::write(&writerC, "IDAT", _err);
  3260. total += bx::write(&writerC, "\x78\x9c", _err);
  3261. const uint8_t* data = (const uint8_t*)_src;
  3262. int32_t step = int32_t(_srcPitch);
  3263. if (_yflip)
  3264. {
  3265. data += _srcPitch*_height - _srcPitch;
  3266. step = -step;
  3267. }
  3268. HashWriter<bx::HashAdler32> writerA(&writerC);
  3269. for (uint32_t ii = 0; ii < _height && _err->isOk(); ++ii)
  3270. {
  3271. total += bx::write(&writerC, uint8_t(ii == _height-1 ? 1 : 0), _err);
  3272. total += bx::write(&writerC, zlen, _err);
  3273. total += bx::write(&writerC, zlenC, _err);
  3274. total += bx::write(&writerA, uint8_t(0), _err);
  3275. if (bgra)
  3276. {
  3277. for (uint32_t xx = 0; xx < _width; ++xx)
  3278. {
  3279. const uint8_t* texel = &data[xx*4];
  3280. const uint8_t bb = texel[0];
  3281. const uint8_t gg = texel[1];
  3282. const uint8_t rr = texel[2];
  3283. const uint8_t aa = texel[3];
  3284. total += bx::write(&writerA, rr, _err);
  3285. total += bx::write(&writerA, gg, _err);
  3286. total += bx::write(&writerA, bb, _err);
  3287. total += bx::write(&writerA, aa, _err);
  3288. }
  3289. }
  3290. else
  3291. {
  3292. total += bx::write(&writerA, data, stride, _err);
  3293. }
  3294. data += step;
  3295. }
  3296. total += bx::write(&writerC, bx::toBigEndian(writerA.end() ), _err);
  3297. total += bx::write(_writer, bx::toBigEndian(writerC.end() ), _err);
  3298. total += bx::write(&writerC, uint32_t(0), _err);
  3299. writerC.begin();
  3300. total += bx::write(&writerC, "IEND", _err);
  3301. total += bx::write(_writer, bx::toBigEndian(writerC.end() ), _err);
  3302. return total;
  3303. }
  3304. 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)
  3305. {
  3306. BX_ERROR_SCOPE(_err);
  3307. const uint32_t bpp = getBitsPerPixel(_format);
  3308. uint32_t bytesPerChannel = 0;
  3309. switch (_format)
  3310. {
  3311. case TextureFormat::RGBA16F:
  3312. bytesPerChannel = 2;
  3313. break;
  3314. default:
  3315. BX_ERROR_SET(_err, BIMG_ERROR, "EXR: Unsupported texture format.");
  3316. return 0;
  3317. }
  3318. int32_t total = 0;
  3319. total += bx::write(_writer, "v/1\x01", _err);
  3320. total += bx::writeLE(_writer, uint32_t(2), _err);
  3321. total += bx::write(_writer, "channels", _err);
  3322. total += bx::write(_writer, '\0', _err);
  3323. total += bx::write(_writer, "chlist", _err);
  3324. total += bx::write(_writer, '\0', _err);
  3325. total += bx::writeLE(_writer, uint32_t(18*4+1), _err);
  3326. const uint8_t cdata[] = { 0, 1, 0, 0, 0, 0, 0, 0, 0, 1, 0, 0, 0, 1, 0, 0, 0 };
  3327. total += bx::write(_writer, 'R', _err);
  3328. total += bx::write(_writer, cdata, BX_COUNTOF(cdata), _err);
  3329. total += bx::write(_writer, 'G', _err);
  3330. total += bx::write(_writer, cdata, BX_COUNTOF(cdata), _err);
  3331. total += bx::write(_writer, 'B', _err);
  3332. total += bx::write(_writer, cdata, BX_COUNTOF(cdata), _err);
  3333. total += bx::write(_writer, 'A', _err);
  3334. total += bx::write(_writer, cdata, BX_COUNTOF(cdata), _err);
  3335. total += bx::write(_writer, '\0', _err);
  3336. total += bx::write(_writer, "compression", _err);
  3337. total += bx::write(_writer, '\0', _err);
  3338. total += bx::write(_writer, "compression", _err);
  3339. total += bx::write(_writer, '\0', _err);
  3340. total += bx::writeLE(_writer, uint32_t(1), _err);
  3341. total += bx::write(_writer, '\0', _err); // no compression
  3342. total += bx::write(_writer, "dataWindow", _err);
  3343. total += bx::write(_writer, '\0', _err);
  3344. total += bx::write(_writer, "box2i", _err);
  3345. total += bx::write(_writer, '\0', _err);
  3346. total += bx::writeLE(_writer, uint32_t(16), _err);
  3347. total += bx::writeRep(_writer, '\0', 8, _err);
  3348. total += bx::writeLE(_writer, _width-1, _err);
  3349. total += bx::writeLE(_writer, _height-1, _err);
  3350. total += bx::write(_writer, "displayWindow", _err);
  3351. total += bx::write(_writer, '\0', _err);
  3352. total += bx::write(_writer, "box2i", _err);
  3353. total += bx::write(_writer, '\0', _err);
  3354. total += bx::writeLE(_writer, uint32_t(16), _err);
  3355. total += bx::writeRep(_writer, '\0', 8, _err);
  3356. total += bx::writeLE(_writer, _width-1, _err);
  3357. total += bx::writeLE(_writer, _height-1, _err);
  3358. total += bx::write(_writer, "lineOrder", _err);
  3359. total += bx::write(_writer, '\0', _err);
  3360. total += bx::write(_writer, "lineOrder", _err);
  3361. total += bx::write(_writer, '\0', _err);
  3362. total += bx::writeLE(_writer, uint32_t(1), _err);
  3363. total += bx::write(_writer, _yflip, _err);
  3364. total += bx::write(_writer, "pixelAspectRatio", _err);
  3365. total += bx::write(_writer, '\0', _err);
  3366. total += bx::write(_writer, "float", _err);
  3367. total += bx::write(_writer, '\0', _err);
  3368. total += bx::writeLE(_writer, uint32_t(4), _err);
  3369. total += bx::writeLE(_writer, 1.0f, _err);
  3370. total += bx::write(_writer, "screenWindowCenter", _err);
  3371. total += bx::write(_writer, '\0', _err);
  3372. total += bx::write(_writer, "v2f", _err);
  3373. total += bx::write(_writer, '\0', _err);
  3374. total += bx::writeLE(_writer, uint32_t(8), _err);
  3375. total += bx::writeRep(_writer, '\0', 8, _err);
  3376. total += bx::write(_writer, "screenWindowWidth", _err);
  3377. total += bx::write(_writer, '\0', _err);
  3378. total += bx::write(_writer, "float", _err);
  3379. total += bx::write(_writer, '\0', _err);
  3380. total += bx::writeLE(_writer, uint32_t(4), _err);
  3381. total += bx::writeLE(_writer, 1.0f, _err);
  3382. total += bx::write(_writer, '\0', _err);
  3383. const uint32_t exrStride = _width*bpp/8;
  3384. uint64_t offset = 0;
  3385. for (uint32_t yy = 0; yy < _height && _err->isOk(); ++yy)
  3386. {
  3387. total += bx::writeLE(_writer, (offset), _err);
  3388. offset += exrStride + 8 /* offset */;
  3389. }
  3390. const uint8_t* data = (const uint8_t*)_src;
  3391. for (uint32_t yy = 0; yy < _height && _err->isOk(); ++yy)
  3392. {
  3393. total += bx::writeLE(_writer, yy, _err);
  3394. total += bx::writeLE(_writer, exrStride, _err);
  3395. for (uint32_t xx = 0; xx < _width && _err->isOk(); ++xx)
  3396. {
  3397. total += bx::write(_writer, &data[xx*bpp/8+0*bytesPerChannel], bytesPerChannel, _err);
  3398. }
  3399. for (uint32_t xx = 0; xx < _width && _err->isOk(); ++xx)
  3400. {
  3401. total += bx::write(_writer, &data[xx*bpp/8+1*bytesPerChannel], bytesPerChannel, _err);
  3402. }
  3403. for (uint32_t xx = 0; xx < _width && _err->isOk(); ++xx)
  3404. {
  3405. total += bx::write(_writer, &data[xx*bpp/8+2*bytesPerChannel], bytesPerChannel, _err);
  3406. }
  3407. for (uint32_t xx = 0; xx < _width && _err->isOk(); ++xx)
  3408. {
  3409. total += bx::write(_writer, &data[xx*bpp/8+3*bytesPerChannel], bytesPerChannel, _err);
  3410. }
  3411. data += _srcPitch;
  3412. }
  3413. return total;
  3414. }
  3415. 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)
  3416. {
  3417. BX_ERROR_SCOPE(_err);
  3418. uint32_t ddspf = UINT32_MAX;
  3419. uint32_t dxgiFormat = UINT32_MAX;
  3420. for (uint32_t ii = 0; ii < BX_COUNTOF(s_translateDdsPixelFormat); ++ii)
  3421. {
  3422. if (s_translateDdsPixelFormat[ii].m_textureFormat == _format)
  3423. {
  3424. ddspf = ii;
  3425. break;
  3426. }
  3427. }
  3428. if (UINT32_MAX == ddspf)
  3429. {
  3430. for (uint32_t ii = 0; ii < BX_COUNTOF(s_translateDxgiFormat); ++ii)
  3431. {
  3432. if (s_translateDxgiFormat[ii].m_textureFormat == _format)
  3433. {
  3434. dxgiFormat = s_translateDxgiFormat[ii].m_format;
  3435. break;
  3436. }
  3437. }
  3438. if (UINT32_MAX == dxgiFormat)
  3439. {
  3440. BX_ERROR_SET(_err, BIMG_ERROR, "DDS: DXGI format not supported.");
  3441. return 0;
  3442. }
  3443. }
  3444. const uint32_t bpp = getBitsPerPixel(_format);
  3445. uint32_t total = 0;
  3446. total += bx::write(_writer, uint32_t(DDS_MAGIC), _err);
  3447. uint32_t headerStart = total;
  3448. total += bx::write(_writer, uint32_t(DDS_HEADER_SIZE), _err);
  3449. total += bx::write(_writer, uint32_t(0
  3450. | DDSD_HEIGHT
  3451. | DDSD_WIDTH
  3452. | DDSD_PIXELFORMAT
  3453. | DDSD_CAPS
  3454. | (1 < _depth ? DDSD_DEPTH : 0)
  3455. | (1 < _numMips ? DDSD_MIPMAPCOUNT : 0)
  3456. | (isCompressed(_format) ? DDSD_LINEARSIZE : DDSD_PITCH)
  3457. )
  3458. , _err
  3459. );
  3460. const uint32_t pitchOrLinearSize = isCompressed(_format)
  3461. ? _width*_height*bpp/8
  3462. : _width*bpp/8
  3463. ;
  3464. total += bx::write(_writer, _height, _err);
  3465. total += bx::write(_writer, _width, _err);
  3466. total += bx::write(_writer, pitchOrLinearSize, _err);
  3467. total += bx::write(_writer, _depth, _err);
  3468. total += bx::write(_writer, uint32_t(_numMips), _err);
  3469. total += bx::writeRep(_writer, 0, 44, _err); // reserved1
  3470. if (UINT32_MAX != ddspf)
  3471. {
  3472. const TranslateDdsPixelFormat& pf = s_translateDdsPixelFormat[ddspf];
  3473. total += bx::write(_writer, uint32_t(8*sizeof(uint32_t) ), _err); // pixelFormatSize
  3474. total += bx::write(_writer, pf.m_flags, _err);
  3475. total += bx::write(_writer, uint32_t(0), _err);
  3476. total += bx::write(_writer, pf.m_bitCount, _err);
  3477. total += bx::write(_writer, pf.m_bitmask, _err);
  3478. }
  3479. else
  3480. {
  3481. total += bx::write(_writer, uint32_t(8*sizeof(uint32_t) ), _err); // pixelFormatSize
  3482. total += bx::write(_writer, uint32_t(DDPF_FOURCC), _err);
  3483. total += bx::write(_writer, uint32_t(DDS_DX10), _err);
  3484. total += bx::write(_writer, uint32_t(0), _err); // bitCount
  3485. total += bx::writeRep(_writer, 0, 4*sizeof(uint32_t), _err); // bitmask
  3486. }
  3487. uint32_t caps[4] =
  3488. {
  3489. uint32_t(DDSCAPS_TEXTURE | (1 < _numMips ? DDSCAPS_COMPLEX|DDSCAPS_MIPMAP : 0) ),
  3490. uint32_t(_cubeMap ? DDSCAPS2_CUBEMAP|DSCAPS2_CUBEMAP_ALLSIDES : 0),
  3491. 0,
  3492. 0,
  3493. };
  3494. total += bx::write(_writer, caps, sizeof(caps) );
  3495. total += bx::writeRep(_writer, 0, 4, _err); // reserved2
  3496. BX_WARN(total-headerStart == DDS_HEADER_SIZE
  3497. , "DDS: Failed to write header size %d (expected: %d)."
  3498. , total-headerStart
  3499. , DDS_HEADER_SIZE
  3500. );
  3501. if (UINT32_MAX != dxgiFormat)
  3502. {
  3503. total += bx::write(_writer, dxgiFormat);
  3504. total += bx::write(_writer, uint32_t(1 < _depth ? DDS_DX10_DIMENSION_TEXTURE3D : DDS_DX10_DIMENSION_TEXTURE2D), _err); // dims
  3505. total += bx::write(_writer, uint32_t(_cubeMap ? DDS_DX10_MISC_TEXTURECUBE : 0), _err); // miscFlags
  3506. total += bx::write(_writer, uint32_t(1), _err); // arraySize
  3507. total += bx::write(_writer, uint32_t(0), _err); // miscFlags2
  3508. BX_WARN(total-headerStart == DDS_HEADER_SIZE+20
  3509. , "DDS: Failed to write header size %d (expected: %d)."
  3510. , total-headerStart
  3511. , DDS_HEADER_SIZE+20
  3512. );
  3513. BX_UNUSED(headerStart);
  3514. }
  3515. return total;
  3516. }
  3517. int32_t imageWriteDds(bx::WriterI* _writer, ImageContainer& _imageContainer, const void* _data, uint32_t _size, bx::Error* _err)
  3518. {
  3519. BX_ERROR_SCOPE(_err);
  3520. int32_t total = 0;
  3521. total += imageWriteDdsHeader(_writer
  3522. , TextureFormat::Enum(_imageContainer.m_format)
  3523. , _imageContainer.m_cubeMap
  3524. , _imageContainer.m_width
  3525. , _imageContainer.m_height
  3526. , _imageContainer.m_depth
  3527. , _imageContainer.m_numMips
  3528. , _err
  3529. );
  3530. if (!_err->isOk() )
  3531. {
  3532. return total;
  3533. }
  3534. for (uint8_t side = 0, numSides = _imageContainer.m_cubeMap ? 6 : 1; side < numSides && _err->isOk(); ++side)
  3535. {
  3536. for (uint8_t lod = 0, num = _imageContainer.m_numMips; lod < num && _err->isOk(); ++lod)
  3537. {
  3538. ImageMip mip;
  3539. if (imageGetRawData(_imageContainer, side, lod, _data, _size, mip) )
  3540. {
  3541. total += bx::write(_writer, mip.m_data, mip.m_size, _err);
  3542. }
  3543. }
  3544. }
  3545. return total;
  3546. }
  3547. 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)
  3548. {
  3549. BX_ERROR_SCOPE(_err);
  3550. const KtxFormatInfo& tfi = s_translateKtxFormat[_format];
  3551. int32_t total = 0;
  3552. total += bx::write(_writer, "\xabKTX 11\xbb\r\n\x1a\n", 12, _err);
  3553. total += bx::write(_writer, uint32_t(0x04030201), _err);
  3554. total += bx::write(_writer, uint32_t(0), _err); // glType
  3555. total += bx::write(_writer, uint32_t(1), _err); // glTypeSize
  3556. total += bx::write(_writer, uint32_t(0), _err); // glFormat
  3557. total += bx::write(_writer, tfi.m_internalFmt, _err); // glInternalFormat
  3558. total += bx::write(_writer, tfi.m_fmt, _err); // glBaseInternalFormat
  3559. total += bx::write(_writer, _width, _err);
  3560. total += bx::write(_writer, _height, _err);
  3561. total += bx::write(_writer, _depth, _err);
  3562. total += bx::write(_writer, _numLayers, _err); // numberOfArrayElements
  3563. total += bx::write(_writer, _cubeMap ? uint32_t(6) : uint32_t(0), _err);
  3564. total += bx::write(_writer, uint32_t(_numMips), _err);
  3565. total += bx::write(_writer, uint32_t(0), _err); // Meta-data size.
  3566. BX_WARN(total == 64, "KTX: Failed to write header size %d (expected: %d).", total, 64);
  3567. return total;
  3568. }
  3569. 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)
  3570. {
  3571. BX_ERROR_SCOPE(_err);
  3572. int32_t total = 0;
  3573. total += imageWriteKtxHeader(_writer, _format, _cubeMap, _width, _height, _depth, _numMips, _numLayers, _err);
  3574. if (!_err->isOk() )
  3575. {
  3576. return total;
  3577. }
  3578. const ImageBlockInfo& blockInfo = s_imageBlockInfo[_format];
  3579. const uint8_t bpp = blockInfo.bitsPerPixel;
  3580. const uint32_t blockWidth = blockInfo.blockWidth;
  3581. const uint32_t blockHeight = blockInfo.blockHeight;
  3582. const uint32_t minBlockX = blockInfo.minBlockX;
  3583. const uint32_t minBlockY = blockInfo.minBlockY;
  3584. const uint8_t* src = (const uint8_t*)_src;
  3585. const uint32_t numLayers = bx::uint32_max(_numLayers, 1);
  3586. const uint32_t numSides = _cubeMap ? 6 : 1;
  3587. uint32_t width = _width;
  3588. uint32_t height = _height;
  3589. uint32_t depth = _depth;
  3590. for (uint8_t lod = 0; lod < _numMips && _err->isOk(); ++lod)
  3591. {
  3592. width = bx::uint32_max(blockWidth * minBlockX, ( (width + blockWidth - 1) / blockWidth )*blockWidth);
  3593. height = bx::uint32_max(blockHeight * minBlockY, ( (height + blockHeight - 1) / blockHeight)*blockHeight);
  3594. depth = bx::uint32_max(1, depth);
  3595. const uint32_t mipSize = width*height*depth*bpp/8;
  3596. const uint32_t size = mipSize*numLayers*numSides;
  3597. total += bx::write(_writer, size, _err);
  3598. for (uint32_t layer = 0; layer < numLayers && _err->isOk(); ++layer)
  3599. {
  3600. for (uint8_t side = 0; side < numSides && _err->isOk(); ++side)
  3601. {
  3602. total += bx::write(_writer, src, size, _err);
  3603. src += size;
  3604. }
  3605. }
  3606. width >>= 1;
  3607. height >>= 1;
  3608. depth >>= 1;
  3609. }
  3610. return total;
  3611. }
  3612. int32_t imageWriteKtx(bx::WriterI* _writer, ImageContainer& _imageContainer, const void* _data, uint32_t _size, bx::Error* _err)
  3613. {
  3614. BX_ERROR_SCOPE(_err);
  3615. int32_t total = 0;
  3616. total += imageWriteKtxHeader(_writer
  3617. , TextureFormat::Enum(_imageContainer.m_format)
  3618. , _imageContainer.m_cubeMap
  3619. , _imageContainer.m_width
  3620. , _imageContainer.m_height
  3621. , _imageContainer.m_depth
  3622. , _imageContainer.m_numMips
  3623. , _imageContainer.m_numLayers
  3624. , _err
  3625. );
  3626. if (!_err->isOk() )
  3627. {
  3628. return total;
  3629. }
  3630. const uint32_t numMips = _imageContainer.m_numMips;
  3631. const uint32_t numLayers = bx::uint32_max(_imageContainer.m_numLayers, 1);
  3632. const uint32_t numSides = _imageContainer.m_cubeMap ? 6 : 1;
  3633. for (uint8_t lod = 0; lod < numMips && _err->isOk(); ++lod)
  3634. {
  3635. ImageMip mip;
  3636. imageGetRawData(_imageContainer, 0, lod, _data, _size, mip);
  3637. const uint32_t size = mip.m_size*numSides*numLayers;
  3638. total += bx::write(_writer, size, _err);
  3639. for (uint32_t layer = 0; layer < numLayers && _err->isOk(); ++layer)
  3640. {
  3641. for (uint8_t side = 0; side < numSides && _err->isOk(); ++side)
  3642. {
  3643. if (imageGetRawData(_imageContainer, uint16_t(layer*numSides + side), lod, _data, _size, mip) )
  3644. {
  3645. total += bx::write(_writer, mip.m_data, mip.m_size, _err);
  3646. }
  3647. }
  3648. }
  3649. }
  3650. return total;
  3651. }
  3652. // +----------+
  3653. // |-z 2|
  3654. // | ^ +y |
  3655. // | | |
  3656. // | +---->+x |
  3657. // +----------+----------+----------+----------+
  3658. // |+y 1|+y 4|+y 0|+y 5|
  3659. // | ^ -x | ^ +z | ^ +x | ^ -z |
  3660. // | | | | | | | | |
  3661. // | +---->+z | +---->+x | +---->-z | +---->-x |
  3662. // +----------+----------+----------+----------+
  3663. // |+z 3|
  3664. // | ^ -y |
  3665. // | | |
  3666. // | +---->+x |
  3667. // +----------+
  3668. //
  3669. struct CubeMapFace
  3670. {
  3671. float uv[3][3];
  3672. };
  3673. static const CubeMapFace s_cubeMapFace[] =
  3674. {
  3675. {{ // +x face
  3676. { 0.0f, 0.0f, -1.0f }, // u -> -z
  3677. { 0.0f, -1.0f, 0.0f }, // v -> -y
  3678. { 1.0f, 0.0f, 0.0f }, // +x face
  3679. }},
  3680. {{ // -x face
  3681. { 0.0f, 0.0f, 1.0f }, // u -> +z
  3682. { 0.0f, -1.0f, 0.0f }, // v -> -y
  3683. { -1.0f, 0.0f, 0.0f }, // -x face
  3684. }},
  3685. {{ // +y face
  3686. { 1.0f, 0.0f, 0.0f }, // u -> +x
  3687. { 0.0f, 0.0f, 1.0f }, // v -> +z
  3688. { 0.0f, 1.0f, 0.0f }, // +y face
  3689. }},
  3690. {{ // -y face
  3691. { 1.0f, 0.0f, 0.0f }, // u -> +x
  3692. { 0.0f, 0.0f, -1.0f }, // v -> -z
  3693. { 0.0f, -1.0f, 0.0f }, // -y face
  3694. }},
  3695. {{ // +z face
  3696. { 1.0f, 0.0f, 0.0f }, // u -> +x
  3697. { 0.0f, -1.0f, 0.0f }, // v -> -y
  3698. { 0.0f, 0.0f, 1.0f }, // +z face
  3699. }},
  3700. {{ // -z face
  3701. { -1.0f, 0.0f, 0.0f }, // u -> -x
  3702. { 0.0f, -1.0f, 0.0f }, // v -> -y
  3703. { 0.0f, 0.0f, -1.0f }, // -z face
  3704. }},
  3705. };
  3706. /// _u and _v should be center addressing and in [-1.0+invSize..1.0-invSize] range.
  3707. void texelUvToDir(float* _result, uint8_t _side, float _u, float _v)
  3708. {
  3709. const CubeMapFace& face = s_cubeMapFace[_side];
  3710. float tmp[3];
  3711. tmp[0] = face.uv[0][0] * _u + face.uv[1][0] * _v + face.uv[2][0];
  3712. tmp[1] = face.uv[0][1] * _u + face.uv[1][1] * _v + face.uv[2][1];
  3713. tmp[2] = face.uv[0][2] * _u + face.uv[1][2] * _v + face.uv[2][2];
  3714. bx::vec3Norm(_result, tmp);
  3715. }
  3716. ImageContainer* imageCubemapFromLatLongRgba32F(bx::AllocatorI* _allocator, const ImageContainer& _input, bool _useBilinearInterpolation, bx::Error* _err)
  3717. {
  3718. BX_ERROR_SCOPE(_err);
  3719. if (_input.m_depth != 1
  3720. && _input.m_numLayers != 1
  3721. && _input.m_format != TextureFormat::RGBA32F
  3722. && _input.m_width/2 != _input.m_height)
  3723. {
  3724. BX_ERROR_SET(_err, BIMG_ERROR, "Input image format is not equirectangular projection.");
  3725. return NULL;
  3726. }
  3727. const uint32_t srcWidthMinusOne = _input.m_width-1;
  3728. const uint32_t srcHeightMinusOne = _input.m_height-1;
  3729. const uint32_t srcPitch = _input.m_width*16;
  3730. const uint32_t dstWidth = _input.m_height/2;
  3731. const uint32_t dstPitch = dstWidth*16;
  3732. const float invDstWidth = 1.0f / float(dstWidth);
  3733. ImageContainer* output = imageAlloc(_allocator
  3734. , _input.m_format
  3735. , uint16_t(dstWidth)
  3736. , uint16_t(dstWidth)
  3737. , uint16_t(1)
  3738. , 1
  3739. , true
  3740. , false
  3741. );
  3742. const uint8_t* srcData = (const uint8_t*)_input.m_data;
  3743. for (uint8_t side = 0; side < 6 && _err->isOk(); ++side)
  3744. {
  3745. ImageMip mip;
  3746. imageGetRawData(*output, side, 0, output->m_data, output->m_size, mip);
  3747. for (uint32_t yy = 0; yy < dstWidth; ++yy)
  3748. {
  3749. for (uint32_t xx = 0; xx < dstWidth; ++xx)
  3750. {
  3751. float* dstData = (float*)&mip.m_data[yy*dstPitch+xx*16];
  3752. const float uu = 2.0f*xx*invDstWidth - 1.0f;
  3753. const float vv = 2.0f*yy*invDstWidth - 1.0f;
  3754. float dir[3];
  3755. texelUvToDir(dir, side, uu, vv);
  3756. float srcU, srcV;
  3757. bx::vec3ToLatLong(&srcU, &srcV, dir);
  3758. srcU *= srcWidthMinusOne;
  3759. srcV *= srcHeightMinusOne;
  3760. if (_useBilinearInterpolation)
  3761. {
  3762. const uint32_t x0 = uint32_t(srcU);
  3763. const uint32_t y0 = uint32_t(srcV);
  3764. const uint32_t x1 = bx::min(x0 + 1, srcWidthMinusOne);
  3765. const uint32_t y1 = bx::min(y0 + 1, srcHeightMinusOne);
  3766. const float* src0 = (const float*)&srcData[y0*srcPitch + x0*16];
  3767. const float* src1 = (const float*)&srcData[y0*srcPitch + x1*16];
  3768. const float* src2 = (const float*)&srcData[y1*srcPitch + x0*16];
  3769. const float* src3 = (const float*)&srcData[y1*srcPitch + x1*16];
  3770. const float tx = srcU - float(int32_t(x0) );
  3771. const float ty = srcV - float(int32_t(y0) );
  3772. const float omtx = 1.0f - tx;
  3773. const float omty = 1.0f - ty;
  3774. float p0[4];
  3775. bx::vec4Mul(p0, src0, omtx*omty);
  3776. float p1[4];
  3777. bx::vec4Mul(p1, src1, tx*omty);
  3778. float p2[4];
  3779. bx::vec4Mul(p2, src2, omtx*ty);
  3780. float p3[4];
  3781. bx::vec4Mul(p3, src3, tx*ty);
  3782. const float rr = p0[0] + p1[0] + p2[0] + p3[0];
  3783. const float gg = p0[1] + p1[1] + p2[1] + p3[1];
  3784. const float bb = p0[2] + p1[2] + p2[2] + p3[2];
  3785. const float aa = p0[3] + p1[3] + p2[3] + p3[3];
  3786. dstData[0] = rr;
  3787. dstData[1] = gg;
  3788. dstData[2] = bb;
  3789. dstData[3] = aa;
  3790. }
  3791. else
  3792. {
  3793. const uint32_t x0 = uint32_t(srcU);
  3794. const uint32_t y0 = uint32_t(srcV);
  3795. const float* src0 = (const float*)&srcData[y0*srcPitch + x0*16];
  3796. dstData[0] = src0[0];
  3797. dstData[1] = src0[1];
  3798. dstData[2] = src0[2];
  3799. dstData[3] = src0[3];
  3800. }
  3801. }
  3802. }
  3803. }
  3804. return output;
  3805. }
  3806. } // namespace bimg