image.cpp 129 KB

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