image.cpp 136 KB

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