image.cpp 135 KB

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