image.cpp 123 KB

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