image.cpp 124 KB

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