image.cpp 140 KB

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