BsPixelUtil.cpp 72 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304230523062307230823092310231123122313231423152316231723182319232023212322232323242325232623272328232923302331233223332334233523362337233823392340234123422343234423452346234723482349235023512352235323542355235623572358235923602361236223632364236523662367236823692370237123722373237423752376237723782379238023812382238323842385238623872388238923902391239223932394239523962397239823992400240124022403240424052406240724082409241024112412241324142415241624172418241924202421242224232424242524262427242824292430243124322433243424352436243724382439244024412442244324442445244624472448244924502451245224532454245524562457245824592460246124622463246424652466246724682469247024712472247324742475247624772478247924802481248224832484248524862487248824892490249124922493249424952496249724982499250025012502250325042505250625072508250925102511251225132514251525162517251825192520252125222523252425252526252725282529253025312532253325342535253625372538253925402541254225432544254525462547254825492550255125522553255425552556255725582559256025612562
  1. //********************************** Banshee Engine (www.banshee3d.com) **************************************************//
  2. //**************** Copyright (c) 2016 Marko Pintera ([email protected]). All rights reserved. **********************//
  3. #include "Image/BsPixelUtil.h"
  4. #include "Utility/BsBitwise.h"
  5. #include "Image/BsColor.h"
  6. #include "Math/BsMath.h"
  7. #include "Error/BsException.h"
  8. #include "Image/BsTexture.h"
  9. #include <nvtt.h>
  10. namespace bs
  11. {
  12. /**
  13. * Performs pixel data resampling using the point filter (nearest neighbor). Does not perform format conversions.
  14. *
  15. * @tparam elementSize Size of a single pixel in bytes.
  16. */
  17. template<UINT32 elementSize> struct NearestResampler
  18. {
  19. static void scale(const PixelData& source, const PixelData& dest)
  20. {
  21. UINT8* sourceData = source.getData();
  22. UINT8* destPtr = dest.getData();
  23. // Get steps for traversing source data in 16/48 fixed point format
  24. UINT64 stepX = ((UINT64)source.getWidth() << 48) / dest.getWidth();
  25. UINT64 stepY = ((UINT64)source.getHeight() << 48) / dest.getHeight();
  26. UINT64 stepZ = ((UINT64)source.getDepth() << 48) / dest.getDepth();
  27. UINT64 curZ = (stepZ >> 1) - 1; // Offset half a pixel to start at pixel center
  28. for (UINT32 z = dest.getFront(); z < dest.getBack(); z++, curZ += stepZ)
  29. {
  30. UINT32 offsetZ = (UINT32)(curZ >> 48) * source.getSlicePitch();
  31. UINT64 curY = (stepY >> 1) - 1; // Offset half a pixel to start at pixel center
  32. for (UINT32 y = dest.getTop(); y < dest.getBottom(); y++, curY += stepY)
  33. {
  34. UINT32 offsetY = (UINT32)(curY >> 48) * source.getRowPitch();
  35. UINT64 curX = (stepX >> 1) - 1; // Offset half a pixel to start at pixel center
  36. for (UINT32 x = dest.getLeft(); x < dest.getRight(); x++, curX += stepX)
  37. {
  38. UINT32 offsetX = (UINT32)(curX >> 48);
  39. UINT32 offsetBytes = elementSize*(offsetX + offsetY + offsetZ);
  40. UINT8* curSourcePtr = sourceData + offsetBytes;
  41. memcpy(destPtr, curSourcePtr, elementSize);
  42. destPtr += elementSize;
  43. }
  44. destPtr += elementSize*dest.getRowSkip();
  45. }
  46. destPtr += elementSize*dest.getSliceSkip();
  47. }
  48. }
  49. };
  50. /** Performs pixel data resampling using the box filter (linear). Performs format conversions. */
  51. struct LinearResampler
  52. {
  53. static void scale(const PixelData& source, const PixelData& dest)
  54. {
  55. UINT32 sourceElemSize = PixelUtil::getNumElemBytes(source.getFormat());
  56. UINT32 destElemSize = PixelUtil::getNumElemBytes(dest.getFormat());
  57. UINT8* sourceData = source.getData();
  58. UINT8* destPtr = dest.getData();
  59. // Get steps for traversing source data in 16/48 fixed point precision format
  60. UINT64 stepX = ((UINT64)source.getWidth() << 48) / dest.getWidth();
  61. UINT64 stepY = ((UINT64)source.getHeight() << 48) / dest.getHeight();
  62. UINT64 stepZ = ((UINT64)source.getDepth() << 48) / dest.getDepth();
  63. // Contains 16/16 fixed point precision format. Most significant
  64. // 16 bits will contain the coordinate in the source image, and the
  65. // least significant 16 bits will contain the fractional part of the coordinate
  66. // that will be used for determining the blend amount.
  67. UINT32 temp = 0;
  68. UINT64 curZ = (stepZ >> 1) - 1; // Offset half a pixel to start at pixel center
  69. for (UINT32 z = dest.getFront(); z < dest.getBack(); z++, curZ += stepZ)
  70. {
  71. temp = UINT32(curZ >> 32);
  72. temp = (temp > 0x8000)? temp - 0x8000 : 0;
  73. UINT32 sampleCoordZ1 = temp >> 16;
  74. UINT32 sampleCoordZ2 = std::min(sampleCoordZ1 + 1, (UINT32)source.getDepth() - 1);
  75. float sampleWeightZ = (temp & 0xFFFF) / 65536.0f;
  76. UINT64 curY = (stepY >> 1) - 1; // Offset half a pixel to start at pixel center
  77. for (UINT32 y = dest.getTop(); y < dest.getBottom(); y++, curY += stepY)
  78. {
  79. temp = (UINT32)(curY >> 32);
  80. temp = (temp > 0x8000)? temp - 0x8000 : 0;
  81. UINT32 sampleCoordY1 = temp >> 16;
  82. UINT32 sampleCoordY2 = std::min(sampleCoordY1 + 1, (UINT32)source.getHeight() - 1);
  83. float sampleWeightY = (temp & 0xFFFF) / 65536.0f;
  84. UINT64 curX = (stepX >> 1) - 1; // Offset half a pixel to start at pixel center
  85. for (UINT32 x = dest.getLeft(); x < dest.getRight(); x++, curX += stepX)
  86. {
  87. temp = (UINT32)(curX >> 32);
  88. temp = (temp > 0x8000)? temp - 0x8000 : 0;
  89. UINT32 sampleCoordX1 = temp >> 16;
  90. UINT32 sampleCoordX2 = std::min(sampleCoordX1 + 1, (UINT32)source.getWidth() - 1);
  91. float sampleWeightX = (temp & 0xFFFF) / 65536.0f;
  92. Color x1y1z1, x2y1z1, x1y2z1, x2y2z1;
  93. Color x1y1z2, x2y1z2, x1y2z2, x2y2z2;
  94. #define GETSOURCEDATA(x, y, z) sourceData + sourceElemSize*((x)+(y)*source.getRowPitch() + (z)*source.getSlicePitch())
  95. PixelUtil::unpackColor(&x1y1z1, source.getFormat(), GETSOURCEDATA(sampleCoordX1, sampleCoordY1, sampleCoordZ1));
  96. PixelUtil::unpackColor(&x2y1z1, source.getFormat(), GETSOURCEDATA(sampleCoordX2, sampleCoordY1, sampleCoordZ1));
  97. PixelUtil::unpackColor(&x1y2z1, source.getFormat(), GETSOURCEDATA(sampleCoordX1, sampleCoordY2, sampleCoordZ1));
  98. PixelUtil::unpackColor(&x2y2z1, source.getFormat(), GETSOURCEDATA(sampleCoordX2, sampleCoordY2, sampleCoordZ1));
  99. PixelUtil::unpackColor(&x1y1z2, source.getFormat(), GETSOURCEDATA(sampleCoordX1, sampleCoordY1, sampleCoordZ2));
  100. PixelUtil::unpackColor(&x2y1z2, source.getFormat(), GETSOURCEDATA(sampleCoordX2, sampleCoordY1, sampleCoordZ2));
  101. PixelUtil::unpackColor(&x1y2z2, source.getFormat(), GETSOURCEDATA(sampleCoordX1, sampleCoordY2, sampleCoordZ2));
  102. PixelUtil::unpackColor(&x2y2z2, source.getFormat(), GETSOURCEDATA(sampleCoordX2, sampleCoordY2, sampleCoordZ2));
  103. #undef GETSOURCEDATA
  104. Color accum =
  105. x1y1z1 * ((1.0f - sampleWeightX)*(1.0f - sampleWeightY)*(1.0f - sampleWeightZ)) +
  106. x2y1z1 * ( sampleWeightX *(1.0f - sampleWeightY)*(1.0f - sampleWeightZ)) +
  107. x1y2z1 * ((1.0f - sampleWeightX)* sampleWeightY *(1.0f - sampleWeightZ)) +
  108. x2y2z1 * ( sampleWeightX * sampleWeightY *(1.0f - sampleWeightZ)) +
  109. x1y1z2 * ((1.0f - sampleWeightX)*(1.0f - sampleWeightY)* sampleWeightZ ) +
  110. x2y1z2 * ( sampleWeightX *(1.0f - sampleWeightY)* sampleWeightZ ) +
  111. x1y2z2 * ((1.0f - sampleWeightX)* sampleWeightY * sampleWeightZ ) +
  112. x2y2z2 * ( sampleWeightX * sampleWeightY * sampleWeightZ );
  113. PixelUtil::packColor(accum, dest.getFormat(), destPtr);
  114. destPtr += destElemSize;
  115. }
  116. destPtr += destElemSize * dest.getRowSkip();
  117. }
  118. destPtr += destElemSize * dest.getSliceSkip();
  119. }
  120. }
  121. };
  122. /**
  123. * Performs pixel data resampling using the box filter (linear). Only handles float RGB or RGBA pixel data (32 bits per
  124. * channel).
  125. */
  126. struct LinearResampler_Float32
  127. {
  128. static void scale(const PixelData& source, const PixelData& dest)
  129. {
  130. UINT32 numSourceChannels = PixelUtil::getNumElemBytes(source.getFormat()) / sizeof(float);
  131. UINT32 numDestChannels = PixelUtil::getNumElemBytes(dest.getFormat()) / sizeof(float);
  132. float* sourceData = (float*)source.getData();
  133. float* destPtr = (float*)dest.getData();
  134. // Get steps for traversing source data in 16/48 fixed point precision format
  135. UINT64 stepX = ((UINT64)source.getWidth() << 48) / dest.getWidth();
  136. UINT64 stepY = ((UINT64)source.getHeight() << 48) / dest.getHeight();
  137. UINT64 stepZ = ((UINT64)source.getDepth() << 48) / dest.getDepth();
  138. // Contains 16/16 fixed point precision format. Most significant
  139. // 16 bits will contain the coordinate in the source image, and the
  140. // least significant 16 bits will contain the fractional part of the coordinate
  141. // that will be used for determining the blend amount.
  142. UINT32 temp = 0;
  143. UINT64 curZ = (stepZ >> 1) - 1; // Offset half a pixel to start at pixel center
  144. for (UINT32 z = dest.getFront(); z < dest.getBack(); z++, curZ += stepZ)
  145. {
  146. temp = (UINT32)(curZ >> 32);
  147. temp = (temp > 0x8000)? temp - 0x8000 : 0;
  148. UINT32 sampleCoordZ1 = temp >> 16;
  149. UINT32 sampleCoordZ2 = std::min(sampleCoordZ1 + 1, (UINT32)source.getDepth() - 1);
  150. float sampleWeightZ = (temp & 0xFFFF) / 65536.0f;
  151. UINT64 curY = (stepY >> 1) - 1; // Offset half a pixel to start at pixel center
  152. for (UINT32 y = dest.getTop(); y < dest.getBottom(); y++, curY += stepY)
  153. {
  154. temp = (UINT32)(curY >> 32);
  155. temp = (temp > 0x8000)? temp - 0x8000 : 0;
  156. UINT32 sampleCoordY1 = temp >> 16;
  157. UINT32 sampleCoordY2 = std::min(sampleCoordY1 + 1, (UINT32)source.getHeight() - 1);
  158. float sampleWeightY = (temp & 0xFFFF) / 65536.0f;
  159. UINT64 curX = (stepX >> 1) - 1; // Offset half a pixel to start at pixel center
  160. for (UINT32 x = dest.getLeft(); x < dest.getRight(); x++, curX += stepX)
  161. {
  162. temp = (UINT32)(curX >> 32);
  163. temp = (temp > 0x8000)? temp - 0x8000 : 0;
  164. UINT32 sampleCoordX1 = temp >> 16;
  165. UINT32 sampleCoordX2 = std::min(sampleCoordX1 + 1, (UINT32)source.getWidth() - 1);
  166. float sampleWeightX = (temp & 0xFFFF) / 65536.0f;
  167. // process R,G,B,A simultaneously for cache coherence?
  168. float accum[4] = { 0.0f, 0.0f, 0.0f, 0.0f };
  169. #define ACCUM3(x,y,z,factor) \
  170. { float f = factor; \
  171. UINT32 offset = (x + y*source.getRowPitch() + z*source.getSlicePitch())*numSourceChannels; \
  172. accum[0] += sourceData[offset + 0] * f; accum[1] += sourceData[offset + 1] * f; \
  173. accum[2] += sourceData[offset + 2] * f; }
  174. #define ACCUM4(x,y,z,factor) \
  175. { float f = factor; \
  176. UINT32 offset = (x + y*source.getRowPitch() + z*source.getSlicePitch())*numSourceChannels; \
  177. accum[0] += sourceData[offset + 0] * f; accum[1] += sourceData[offset + 1] * f; \
  178. accum[2] += sourceData[offset + 2] * f; accum[3] += sourceData[offset + 3] * f; }
  179. if (numSourceChannels == 3 || numDestChannels == 3)
  180. {
  181. // RGB
  182. ACCUM3(sampleCoordX1, sampleCoordY1, sampleCoordZ1, (1.0f - sampleWeightX) * (1.0f - sampleWeightY) * (1.0f - sampleWeightZ));
  183. ACCUM3(sampleCoordX2, sampleCoordY1, sampleCoordZ1, sampleWeightX * (1.0f - sampleWeightY) * (1.0f - sampleWeightZ));
  184. ACCUM3(sampleCoordX1, sampleCoordY2, sampleCoordZ1, (1.0f - sampleWeightX) * sampleWeightY * (1.0f - sampleWeightZ));
  185. ACCUM3(sampleCoordX2, sampleCoordY2, sampleCoordZ1, sampleWeightX * sampleWeightY * (1.0f - sampleWeightZ));
  186. ACCUM3(sampleCoordX1, sampleCoordY1, sampleCoordZ2, (1.0f - sampleWeightX) * (1.0f - sampleWeightY) * sampleWeightZ);
  187. ACCUM3(sampleCoordX2, sampleCoordY1, sampleCoordZ2, sampleWeightX * (1.0f - sampleWeightY) * sampleWeightZ);
  188. ACCUM3(sampleCoordX1, sampleCoordY2, sampleCoordZ2, (1.0f - sampleWeightX) * sampleWeightY * sampleWeightZ);
  189. ACCUM3(sampleCoordX2, sampleCoordY2, sampleCoordZ2, sampleWeightX * sampleWeightY * sampleWeightZ);
  190. accum[3] = 1.0f;
  191. }
  192. else
  193. {
  194. // RGBA
  195. ACCUM4(sampleCoordX1, sampleCoordY1, sampleCoordZ1, (1.0f - sampleWeightX) * (1.0f - sampleWeightY) * (1.0f - sampleWeightZ));
  196. ACCUM4(sampleCoordX2, sampleCoordY1, sampleCoordZ1, sampleWeightX * (1.0f - sampleWeightY) * (1.0f - sampleWeightZ));
  197. ACCUM4(sampleCoordX1, sampleCoordY2, sampleCoordZ1, (1.0f - sampleWeightX) * sampleWeightY * (1.0f - sampleWeightZ));
  198. ACCUM4(sampleCoordX2, sampleCoordY2, sampleCoordZ1, sampleWeightX * sampleWeightY * (1.0f - sampleWeightZ));
  199. ACCUM4(sampleCoordX1, sampleCoordY1, sampleCoordZ2, (1.0f - sampleWeightX) * (1.0f - sampleWeightY) * sampleWeightZ);
  200. ACCUM4(sampleCoordX2, sampleCoordY1, sampleCoordZ2, sampleWeightX * (1.0f - sampleWeightY) * sampleWeightZ);
  201. ACCUM4(sampleCoordX1, sampleCoordY2, sampleCoordZ2, (1.0f - sampleWeightX) * sampleWeightY * sampleWeightZ);
  202. ACCUM4(sampleCoordX2, sampleCoordY2, sampleCoordZ2, sampleWeightX * sampleWeightY * sampleWeightZ);
  203. }
  204. memcpy(destPtr, accum, sizeof(float)*numDestChannels);
  205. #undef ACCUM3
  206. #undef ACCUM4
  207. destPtr += numDestChannels;
  208. }
  209. destPtr += numDestChannels*dest.getRowSkip();
  210. }
  211. destPtr += numDestChannels*dest.getSliceSkip();
  212. }
  213. }
  214. };
  215. // byte linear resampler, does not do any format conversions.
  216. // only handles pixel formats that use 1 byte per color channel.
  217. // 2D only; punts 3D pixelboxes to default LinearResampler (slow).
  218. // templated on bytes-per-pixel to allow compiler optimizations, such
  219. // as unrolling loops and replacing multiplies with bitshifts
  220. /**
  221. * Performs pixel data resampling using the box filter (linear). Only handles pixel formats with one byte per channel.
  222. * Does not perform format conversion.
  223. *
  224. * @tparam channels Number of channels in the pixel format.
  225. */
  226. template<UINT32 channels> struct LinearResampler_Byte
  227. {
  228. static void scale(const PixelData& source, const PixelData& dest)
  229. {
  230. // Only optimized for 2D
  231. if (source.getDepth() > 1 || dest.getDepth() > 1)
  232. {
  233. LinearResampler::scale(source, dest);
  234. return;
  235. }
  236. UINT8* sourceData = (UINT8*)source.getData();
  237. UINT8* destPtr = (UINT8*)dest.getData();
  238. // Get steps for traversing source data in 16/48 fixed point precision format
  239. UINT64 stepX = ((UINT64)source.getWidth() << 48) / dest.getWidth();
  240. UINT64 stepY = ((UINT64)source.getHeight() << 48) / dest.getHeight();
  241. // Contains 16/16 fixed point precision format. Most significant
  242. // 16 bits will contain the coordinate in the source image, and the
  243. // least significant 16 bits will contain the fractional part of the coordinate
  244. // that will be used for determining the blend amount.
  245. UINT32 temp;
  246. UINT64 curY = (stepY >> 1) - 1; // Offset half a pixel to start at pixel center
  247. for (UINT32 y = dest.getTop(); y < dest.getBottom(); y++, curY += stepY)
  248. {
  249. temp = (UINT32)(curY >> 36);
  250. temp = (temp > 0x800)? temp - 0x800: 0;
  251. UINT32 sampleWeightY = temp & 0xFFF;
  252. UINT32 sampleCoordY1 = temp >> 12;
  253. UINT32 sampleCoordY2 = std::min(sampleCoordY1 + 1, (UINT32)source.getBottom() - source.getTop() - 1);
  254. UINT32 sampleY1Offset = sampleCoordY1 * source.getRowPitch();
  255. UINT32 sampleY2Offset = sampleCoordY2 * source.getRowPitch();
  256. UINT64 curX = (stepX >> 1) - 1; // Offset half a pixel to start at pixel center
  257. for (UINT32 x = dest.getLeft(); x < dest.getRight(); x++, curX += stepX)
  258. {
  259. temp = (UINT32)(curX >> 36);
  260. temp = (temp > 0x800)? temp - 0x800 : 0;
  261. UINT32 sampleWeightX = temp & 0xFFF;
  262. UINT32 sampleCoordX1 = temp >> 12;
  263. UINT32 sampleCoordX2 = std::min(sampleCoordX1 + 1, (UINT32)source.getRight() - source.getLeft() - 1);
  264. UINT32 sxfsyf = sampleWeightX*sampleWeightY;
  265. for (UINT32 k = 0; k < channels; k++)
  266. {
  267. UINT32 accum =
  268. sourceData[(sampleCoordX1 + sampleY1Offset)*channels+k]*(0x1000000-(sampleWeightX<<12)-(sampleWeightY<<12)+sxfsyf) +
  269. sourceData[(sampleCoordX2 + sampleY1Offset)*channels+k]*((sampleWeightX<<12)-sxfsyf) +
  270. sourceData[(sampleCoordX1 + sampleY2Offset)*channels+k]*((sampleWeightY<<12)-sxfsyf) +
  271. sourceData[(sampleCoordX2 + sampleY2Offset)*channels+k]*sxfsyf;
  272. // Round up to byte size
  273. *destPtr = (UINT8)((accum + 0x800000) >> 24);
  274. destPtr++;
  275. }
  276. }
  277. destPtr += channels*dest.getRowSkip();
  278. }
  279. }
  280. };
  281. /** Data describing a pixel format. */
  282. struct PixelFormatDescription
  283. {
  284. const char* name; /**< Name of the format. */
  285. UINT8 elemBytes; /**< Number of bytes one element (color value) uses. */
  286. UINT32 flags; /**< PixelFormatFlags set by the pixel format. */
  287. PixelComponentType componentType; /**< Data type of a single element of the format. */
  288. UINT8 componentCount; /**< Number of elements in the format. */
  289. UINT8 rbits, gbits, bbits, abits; /**< Number of bits per element in the format. */
  290. UINT32 rmask, gmask, bmask, amask; /**< Masks used by packers/unpackers. */
  291. UINT8 rshift, gshift, bshift, ashift; /**< Shifts used by packers/unpackers. */
  292. };
  293. /** A list of all available pixel formats. */
  294. PixelFormatDescription _pixelFormats[PF_COUNT] = {
  295. {"PF_UNKNOWN",
  296. /* Bytes per element */
  297. 0,
  298. /* Flags */
  299. 0,
  300. /* Component type and count */
  301. PCT_BYTE, 0,
  302. /* rbits, gbits, bbits, abits */
  303. 0, 0, 0, 0,
  304. /* Masks and shifts */
  305. 0, 0, 0, 0,
  306. 0, 0, 0, 0,
  307. },
  308. //-----------------------------------------------------------------------
  309. {"PF_R8",
  310. /* Bytes per element */
  311. 1,
  312. /* Flags */
  313. PFF_INTEGER | PFF_NORMALIZED,
  314. /* Component type and count */
  315. PCT_BYTE, 1,
  316. /* rbits, gbits, bbits, abits */
  317. 8, 0, 0, 0,
  318. /* Masks and shifts */
  319. 0x000000FF, 0, 0, 0,
  320. 0, 0, 0, 0,
  321. },
  322. //-----------------------------------------------------------------------
  323. {"PF_RG8",
  324. /* Bytes per element */
  325. 2,
  326. /* Flags */
  327. PFF_INTEGER | PFF_NORMALIZED,
  328. /* Component type and count */
  329. PCT_BYTE, 2,
  330. /* rbits, gbits, bbits, abits */
  331. 8, 8, 0, 0,
  332. /* Masks and shifts */
  333. 0x000000FF, 0x0000FF00, 0, 0,
  334. 0, 8, 0, 0,
  335. },
  336. //-----------------------------------------------------------------------
  337. {"PF_RGB8",
  338. /* Bytes per element */
  339. 4, // 4th byte is unused
  340. /* Flags */
  341. PFF_INTEGER | PFF_NORMALIZED,
  342. /* Component type and count */
  343. PCT_BYTE, 3,
  344. /* rbits, gbits, bbits, abits */
  345. 8, 8, 8, 0,
  346. /* Masks and shifts */
  347. 0x000000FF, 0x0000FF00, 0x00FF0000, 0,
  348. 0, 8, 16, 0,
  349. },
  350. //-----------------------------------------------------------------------
  351. {"PF_BGR8",
  352. /* Bytes per element */
  353. 4, // 4th byte is unused
  354. /* Flags */
  355. PFF_INTEGER | PFF_NORMALIZED,
  356. /* Component type and count */
  357. PCT_BYTE, 3,
  358. /* rbits, gbits, bbits, abits */
  359. 8, 8, 8, 0,
  360. /* Masks and shifts */
  361. 0x00FF0000, 0x0000FF00, 0x000000FF, 0,
  362. 16, 8, 0, 0,
  363. },
  364. //-----------------------------------------------------------------------
  365. {}, // Deleted format
  366. //-----------------------------------------------------------------------
  367. {}, // Deleted format
  368. //-----------------------------------------------------------------------
  369. {"PF_BGRA8",
  370. /* Bytes per element */
  371. 4,
  372. /* Flags */
  373. PFF_HASALPHA | PFF_INTEGER | PFF_NORMALIZED,
  374. /* Component type and count */
  375. PCT_BYTE, 4,
  376. /* rbits, gbits, bbits, abits */
  377. 8, 8, 8, 8,
  378. /* Masks and shifts */
  379. 0x00FF0000, 0x0000FF00, 0x000000FF, 0xFF000000,
  380. 16, 8, 0, 24,
  381. },
  382. //-----------------------------------------------------------------------
  383. {"PF_RGBA8",
  384. /* Bytes per element */
  385. 4,
  386. /* Flags */
  387. PFF_HASALPHA | PFF_INTEGER | PFF_NORMALIZED,
  388. /* Component type and count */
  389. PCT_BYTE, 4,
  390. /* rbits, gbits, bbits, abits */
  391. 8, 8, 8, 8,
  392. /* Masks and shifts */
  393. 0x000000FF, 0x0000FF00, 0x00FF0000, 0xFF000000,
  394. 0, 8, 16, 24,
  395. },
  396. //-----------------------------------------------------------------------
  397. {}, // Deleted format
  398. //-----------------------------------------------------------------------
  399. {}, // Deleted format
  400. //-----------------------------------------------------------------------
  401. {}, // Deleted format
  402. //-----------------------------------------------------------------------
  403. {}, // Deleted format
  404. //-----------------------------------------------------------------------
  405. { "PF_BC1",
  406. /* Bytes per element */
  407. 0,
  408. /* Flags */
  409. PFF_COMPRESSED | PFF_HASALPHA,
  410. /* Component type and count */
  411. PCT_BYTE, 3, // No alpha
  412. /* rbits, gbits, bbits, abits */
  413. 0, 0, 0, 0,
  414. /* Masks and shifts */
  415. 0, 0, 0, 0,
  416. 0, 0, 0, 0,
  417. },
  418. //-----------------------------------------------------------------------
  419. { "PF_BC1a",
  420. /* Bytes per element */
  421. 0,
  422. /* Flags */
  423. PFF_COMPRESSED,
  424. /* Component type and count */
  425. PCT_BYTE, 3,
  426. /* rbits, gbits, bbits, abits */
  427. 0, 0, 0, 0,
  428. /* Masks and shifts */
  429. 0, 0, 0, 0,
  430. 0, 0, 0, 0,
  431. },
  432. //-----------------------------------------------------------------------
  433. { "PF_BC2",
  434. /* Bytes per element */
  435. 0,
  436. /* Flags */
  437. PFF_COMPRESSED | PFF_HASALPHA,
  438. /* Component type and count */
  439. PCT_BYTE, 4,
  440. /* rbits, gbits, bbits, abits */
  441. 0, 0, 0, 0,
  442. /* Masks and shifts */
  443. 0, 0, 0, 0,
  444. 0, 0, 0, 0,
  445. },
  446. //-----------------------------------------------------------------------
  447. { "PF_BC3",
  448. /* Bytes per element */
  449. 0,
  450. /* Flags */
  451. PFF_COMPRESSED | PFF_HASALPHA,
  452. /* Component type and count */
  453. PCT_BYTE, 4,
  454. /* rbits, gbits, bbits, abits */
  455. 0, 0, 0, 0,
  456. /* Masks and shifts */
  457. 0, 0, 0, 0,
  458. 0, 0, 0, 0,
  459. },
  460. //-----------------------------------------------------------------------
  461. { "PF_BC4",
  462. /* Bytes per element */
  463. 0,
  464. /* Flags */
  465. PFF_COMPRESSED,
  466. /* Component type and count */
  467. PCT_BYTE, 1,
  468. /* rbits, gbits, bbits, abits */
  469. 0, 0, 0, 0,
  470. /* Masks and shifts */
  471. 0, 0, 0, 0,
  472. 0, 0, 0, 0,
  473. },
  474. //-----------------------------------------------------------------------
  475. { "PF_BC5",
  476. /* Bytes per element */
  477. 0,
  478. /* Flags */
  479. PFF_COMPRESSED,
  480. /* Component type and count */
  481. PCT_BYTE, 2,
  482. /* rbits, gbits, bbits, abits */
  483. 0, 0, 0, 0,
  484. /* Masks and shifts */
  485. 0, 0, 0, 0,
  486. 0, 0, 0, 0,
  487. },
  488. //-----------------------------------------------------------------------
  489. { "PF_BC6H",
  490. /* Bytes per element */
  491. 0,
  492. /* Flags */
  493. PFF_COMPRESSED,
  494. /* Component type and count */
  495. PCT_FLOAT16, 3,
  496. /* rbits, gbits, bbits, abits */
  497. 0, 0, 0, 0,
  498. /* Masks and shifts */
  499. 0, 0, 0, 0,
  500. 0, 0, 0, 0,
  501. },
  502. //-----------------------------------------------------------------------
  503. { "PF_BC7",
  504. /* Bytes per element */
  505. 0,
  506. /* Flags */
  507. PFF_COMPRESSED | PFF_HASALPHA,
  508. /* Component type and count */
  509. PCT_BYTE, 4,
  510. /* rbits, gbits, bbits, abits */
  511. 0, 0, 0, 0,
  512. /* Masks and shifts */
  513. 0, 0, 0, 0,
  514. 0, 0, 0, 0,
  515. },
  516. //-----------------------------------------------------------------------
  517. {"PF_R16F",
  518. /* Bytes per element */
  519. 2,
  520. /* Flags */
  521. PFF_FLOAT,
  522. /* Component type and count */
  523. PCT_FLOAT16, 1,
  524. /* rbits, gbits, bbits, abits */
  525. 16, 0, 0, 0,
  526. /* Masks and shifts */
  527. 0x0000FFFF, 0, 0, 0,
  528. 0, 0, 0, 0,
  529. },
  530. //-----------------------------------------------------------------------
  531. {"PF_RG16F",
  532. /* Bytes per element */
  533. 4,
  534. /* Flags */
  535. PFF_FLOAT,
  536. /* Component type and count */
  537. PCT_FLOAT16, 2,
  538. /* rbits, gbits, bbits, abits */
  539. 16, 16, 0, 0,
  540. /* Masks and shifts */
  541. 0x0000FFFF, 0xFFFF0000, 0, 0,
  542. 0, 16, 0, 0,
  543. },
  544. //-----------------------------------------------------------------------
  545. { }, // Deleted format
  546. //-----------------------------------------------------------------------
  547. { "PF_RGBA16F",
  548. /* Bytes per element */
  549. 8,
  550. /* Flags */
  551. PFF_FLOAT | PFF_HASALPHA,
  552. /* Component type and count */
  553. PCT_FLOAT16, 4,
  554. /* rbits, gbits, bbits, abits */
  555. 16, 16, 16, 16,
  556. /* Masks and shifts */
  557. 0x0000FFFF, 0xFFFF0000, 0x0000FFFF, 0xFFFF0000,
  558. 0, 16, 0, 16,
  559. },
  560. //-----------------------------------------------------------------------
  561. {"PF_R32F",
  562. /* Bytes per element */
  563. 4,
  564. /* Flags */
  565. PFF_FLOAT,
  566. /* Component type and count */
  567. PCT_FLOAT32, 1,
  568. /* rbits, gbits, bbits, abits */
  569. 32, 0, 0, 0,
  570. /* Masks and shifts */
  571. 0xFFFFFFFF, 0, 0, 0,
  572. 0, 0, 0, 0,
  573. },
  574. //-----------------------------------------------------------------------
  575. {"PF_RG32F",
  576. /* Bytes per element */
  577. 8,
  578. /* Flags */
  579. PFF_FLOAT,
  580. /* Component type and count */
  581. PCT_FLOAT32, 2,
  582. /* rbits, gbits, bbits, abits */
  583. 32, 32, 0, 0,
  584. /* Masks and shifts */
  585. 0xFFFFFFFF, 0xFFFFFFFF, 0, 0,
  586. 0, 0, 0, 0,
  587. },
  588. //-----------------------------------------------------------------------
  589. { "PF_RGB32F",
  590. /* Bytes per element */
  591. 12,
  592. /* Flags */
  593. PFF_FLOAT,
  594. /* Component type and count */
  595. PCT_FLOAT32, 3,
  596. /* rbits, gbits, bbits, abits */
  597. 32, 32, 32, 0,
  598. /* Masks and shifts */
  599. 0xFFFFFFFF, 0xFFFFFFFF, 0xFFFFFFFF, 0,
  600. 0, 0, 0, 0,
  601. },
  602. //-----------------------------------------------------------------------
  603. { "PF_RGBA32F",
  604. /* Bytes per element */
  605. 16,
  606. /* Flags */
  607. PFF_FLOAT | PFF_HASALPHA,
  608. /* Component type and count */
  609. PCT_FLOAT32, 4,
  610. /* rbits, gbits, bbits, abits */
  611. 32, 32, 32, 32,
  612. /* Masks and shifts */
  613. 0xFFFFFFFF, 0xFFFFFFFF, 0xFFFFFFFF, 0xFFFFFFFF,
  614. 0, 0, 0, 0,
  615. },
  616. //-----------------------------------------------------------------------
  617. {"PF_D32_S8X24",
  618. /* Bytes per element */
  619. 8,
  620. /* Flags */
  621. PFF_DEPTH | PFF_NORMALIZED,
  622. /* Component type and count */
  623. PCT_FLOAT32, 2,
  624. /* rbits, gbits, bbits, abits */
  625. 32, 8, 0, 0,
  626. /* Masks and shifts */
  627. 0xFFFFFFFF, 0x000000FF, 0x00000000, 0x00000000,
  628. 0, 0, 0, 0,
  629. },
  630. //-----------------------------------------------------------------------
  631. {"PF_D24_S8",
  632. /* Bytes per element */
  633. 4,
  634. /* Flags */
  635. PFF_DEPTH | PFF_INTEGER | PFF_NORMALIZED,
  636. /* Component type and count */
  637. PCT_INT, 2,
  638. /* rbits, gbits, bbits, abits */
  639. 24, 8, 0, 0,
  640. /* Masks and shifts */
  641. 0x00FFFFFF, 0x0FF0000, 0x00000000, 0x00000000,
  642. 0, 24, 0, 0,
  643. },
  644. //-----------------------------------------------------------------------
  645. {"PF_D32",
  646. /* Bytes per element */
  647. 4,
  648. /* Flags */
  649. PFF_DEPTH | PFF_FLOAT,
  650. /* Component type and count */
  651. PCT_FLOAT32, 1,
  652. /* rbits, gbits, bbits, abits */
  653. 32, 0, 0, 0,
  654. /* Masks and shifts */
  655. 0xFFFFFFFF, 0x00000000, 0x00000000, 0x00000000,
  656. 0, 0, 0, 0,
  657. },
  658. //-----------------------------------------------------------------------
  659. {"PF_D16",
  660. /* Bytes per element */
  661. 2,
  662. /* Flags */
  663. PFF_DEPTH | PFF_INTEGER | PFF_NORMALIZED,
  664. /* Component type and count */
  665. PCT_SHORT, 1,
  666. /* rbits, gbits, bbits, abits */
  667. 16, 0, 0, 0,
  668. /* Masks and shifts */
  669. 0x0000FFFF, 0x00000000, 0x00000000, 0x00000000,
  670. 0, 0, 0, 0,
  671. },
  672. //-----------------------------------------------------------------------
  673. { "PF_RG11B10F",
  674. /* Bytes per element */
  675. 4,
  676. /* Flags */
  677. PFF_FLOAT,
  678. /* Component type and count */
  679. PCT_PACKED_R11G11B10, 1,
  680. /* rbits, gbits, bbits, abits */
  681. 11, 11, 10, 0,
  682. /* Masks and shifts */
  683. 0x000007FF, 0x003FF800, 0xFFC00000, 0,
  684. 0, 11, 22, 0,
  685. },
  686. //-----------------------------------------------------------------------
  687. { "PF_RGB10A2",
  688. /* Bytes per element */
  689. 4,
  690. /* Flags */
  691. PFF_INTEGER | PFF_NORMALIZED | PFF_HASALPHA,
  692. /* Component type and count */
  693. PCT_PACKED_R10G10B10A2, 1,
  694. /* rbits, gbits, bbits, abits */
  695. 10, 10, 10, 2,
  696. /* Masks and shifts */
  697. 0x000003FF, 0x000FFC00, 0x3FF00000, 0xC0000000,
  698. 0, 10, 20, 30,
  699. },
  700. //-----------------------------------------------------------------------
  701. { "PF_R8I",
  702. /* Bytes per element */
  703. 1,
  704. /* Flags */
  705. PFF_INTEGER | PFF_SIGNED,
  706. /* Component type and count */
  707. PCT_BYTE, 1,
  708. /* rbits, gbits, bbits, abits */
  709. 8, 0, 0, 0,
  710. /* Masks and shifts */
  711. 0x000000FF, 0, 0, 0,
  712. 0, 0, 0, 0,
  713. },
  714. //-----------------------------------------------------------------------
  715. { "PF_RG8I",
  716. /* Bytes per element */
  717. 2,
  718. /* Flags */
  719. PFF_INTEGER | PFF_SIGNED,
  720. /* Component type and count */
  721. PCT_BYTE, 2,
  722. /* rbits, gbits, bbits, abits */
  723. 8, 8, 0, 0,
  724. /* Masks and shifts */
  725. 0x000000FF, 0x0000FF00, 0, 0,
  726. 0, 8, 0, 0,
  727. },
  728. //-----------------------------------------------------------------------
  729. { "PF_RGBA8I",
  730. /* Bytes per element */
  731. 4,
  732. /* Flags */
  733. PFF_INTEGER | PFF_SIGNED | PFF_HASALPHA,
  734. /* Component type and count */
  735. PCT_BYTE, 4,
  736. /* rbits, gbits, bbits, abits */
  737. 8, 8, 8, 8,
  738. /* Masks and shifts */
  739. 0x000000FF, 0x0000FF00, 0x00FF0000, 0xFF000000,
  740. 0, 8, 16, 24,
  741. },
  742. //-----------------------------------------------------------------------
  743. { "PF_R8U",
  744. /* Bytes per element */
  745. 1,
  746. /* Flags */
  747. PFF_INTEGER,
  748. /* Component type and count */
  749. PCT_BYTE, 1,
  750. /* rbits, gbits, bbits, abits */
  751. 8, 0, 0, 0,
  752. /* Masks and shifts */
  753. 0x000000FF, 0, 0, 0,
  754. 0, 0, 0, 0,
  755. },
  756. //-----------------------------------------------------------------------
  757. { "PF_RG8U",
  758. /* Bytes per element */
  759. 2,
  760. /* Flags */
  761. PFF_INTEGER,
  762. /* Component type and count */
  763. PCT_BYTE, 2,
  764. /* rbits, gbits, bbits, abits */
  765. 8, 8, 0, 0,
  766. /* Masks and shifts */
  767. 0x000000FF, 0x0000FF00, 0, 0,
  768. 0, 8, 0, 0,
  769. },
  770. //-----------------------------------------------------------------------
  771. { "PF_RGBA8U",
  772. /* Bytes per element */
  773. 4,
  774. /* Flags */
  775. PFF_INTEGER | PFF_HASALPHA,
  776. /* Component type and count */
  777. PCT_BYTE, 4,
  778. /* rbits, gbits, bbits, abits */
  779. 8, 8, 8, 8,
  780. /* Masks and shifts */
  781. 0x000000FF, 0x0000FF00, 0x00FF0000, 0xFF000000,
  782. 0, 8, 16, 24,
  783. },
  784. //-----------------------------------------------------------------------
  785. { "PF_R8S",
  786. /* Bytes per element */
  787. 1,
  788. /* Flags */
  789. PFF_INTEGER | PFF_NORMALIZED | PFF_SIGNED,
  790. /* Component type and count */
  791. PCT_BYTE, 1,
  792. /* rbits, gbits, bbits, abits */
  793. 8, 0, 0, 0,
  794. /* Masks and shifts */
  795. 0x000000FF, 0, 0, 0,
  796. 0, 0, 0, 0,
  797. },
  798. //-----------------------------------------------------------------------
  799. { "PF_RG8S",
  800. /* Bytes per element */
  801. 2,
  802. /* Flags */
  803. PFF_INTEGER | PFF_NORMALIZED | PFF_SIGNED,
  804. /* Component type and count */
  805. PCT_BYTE, 2,
  806. /* rbits, gbits, bbits, abits */
  807. 8, 8, 0, 0,
  808. /* Masks and shifts */
  809. 0x000000FF, 0x0000FF00, 0, 0,
  810. 0, 8, 0, 0,
  811. },
  812. //-----------------------------------------------------------------------
  813. { "PF_RGBA8S",
  814. /* Bytes per element */
  815. 4,
  816. /* Flags */
  817. PFF_INTEGER | PFF_NORMALIZED | PFF_SIGNED | PFF_HASALPHA,
  818. /* Component type and count */
  819. PCT_BYTE, 4,
  820. /* rbits, gbits, bbits, abits */
  821. 8, 8, 8, 8,
  822. /* Masks and shifts */
  823. 0x000000FF, 0x0000FF00, 0x00FF0000, 0xFF000000,
  824. 0, 8, 16, 24,
  825. },
  826. //-----------------------------------------------------------------------
  827. { "PF_R16I",
  828. /* Bytes per element */
  829. 2,
  830. /* Flags */
  831. PFF_INTEGER | PFF_SIGNED,
  832. /* Component type and count */
  833. PCT_SHORT, 1,
  834. /* rbits, gbits, bbits, abits */
  835. 16, 0, 0, 0,
  836. /* Masks and shifts */
  837. 0x0000FFFF, 0, 0, 0,
  838. 0, 0, 0, 0,
  839. },
  840. //-----------------------------------------------------------------------
  841. { "PF_RG16I",
  842. /* Bytes per element */
  843. 4,
  844. /* Flags */
  845. PFF_INTEGER | PFF_SIGNED,
  846. /* Component type and count */
  847. PCT_SHORT, 2,
  848. /* rbits, gbits, bbits, abits */
  849. 16, 16, 0, 0,
  850. /* Masks and shifts */
  851. 0x0000FFFF, 0xFFFF0000, 0, 0,
  852. 0, 16, 0, 0,
  853. },
  854. //-----------------------------------------------------------------------
  855. { "PF_RGBA16I",
  856. /* Bytes per element */
  857. 8,
  858. /* Flags */
  859. PFF_INTEGER | PFF_SIGNED | PFF_HASALPHA,
  860. /* Component type and count */
  861. PCT_SHORT, 4,
  862. /* rbits, gbits, bbits, abits */
  863. 16, 16, 16, 16,
  864. /* Masks and shifts */
  865. 0x0000FFFF, 0xFFFF0000, 0x0000FFFF, 0xFFFF0000,
  866. 0, 16, 0, 16,
  867. },
  868. //-----------------------------------------------------------------------
  869. { "PF_R16U",
  870. /* Bytes per element */
  871. 2,
  872. /* Flags */
  873. PFF_INTEGER,
  874. /* Component type and count */
  875. PCT_SHORT, 1,
  876. /* rbits, gbits, bbits, abits */
  877. 16, 0, 0, 0,
  878. /* Masks and shifts */
  879. 0x0000FFFF, 0, 0, 0,
  880. 0, 0, 0, 0,
  881. },
  882. //-----------------------------------------------------------------------
  883. { "PF_RG16U",
  884. /* Bytes per element */
  885. 4,
  886. /* Flags */
  887. PFF_INTEGER,
  888. /* Component type and count */
  889. PCT_SHORT, 2,
  890. /* rbits, gbits, bbits, abits */
  891. 16, 16, 0, 0,
  892. /* Masks and shifts */
  893. 0x0000FFFF, 0xFFFF0000, 0, 0,
  894. 0, 16, 0, 0,
  895. },
  896. //-----------------------------------------------------------------------
  897. { "PF_RGBA16U",
  898. /* Bytes per element */
  899. 8,
  900. /* Flags */
  901. PFF_INTEGER | PFF_HASALPHA,
  902. /* Component type and count */
  903. PCT_SHORT, 4,
  904. /* rbits, gbits, bbits, abits */
  905. 16, 16, 16, 16,
  906. /* Masks and shifts */
  907. 0x0000FFFF, 0xFFFF0000, 0x0000FFFF, 0xFFFF0000,
  908. 0, 16, 0, 16,
  909. },
  910. //-----------------------------------------------------------------------
  911. { "PF_R32I",
  912. /* Bytes per element */
  913. 4,
  914. /* Flags */
  915. PFF_INTEGER,
  916. /* Component type and count */
  917. PCT_INT, 1,
  918. /* rbits, gbits, bbits, abits */
  919. 32, 0, 0, 0,
  920. /* Masks and shifts */
  921. 0xFFFFFFFF, 0, 0, 0,
  922. 0, 0, 0, 0,
  923. },
  924. //-----------------------------------------------------------------------
  925. { "PF_RG32I",
  926. /* Bytes per element */
  927. 8,
  928. /* Flags */
  929. PFF_INTEGER | PFF_SIGNED,
  930. /* Component type and count */
  931. PCT_INT, 2,
  932. /* rbits, gbits, bbits, abits */
  933. 32, 32, 0, 0,
  934. /* Masks and shifts */
  935. 0xFFFFFFFF, 0xFFFFFFFF, 0, 0,
  936. 0, 0, 0, 0,
  937. },
  938. //-----------------------------------------------------------------------
  939. { "PF_RGB32I",
  940. /* Bytes per element */
  941. 12,
  942. /* Flags */
  943. PFF_INTEGER | PFF_SIGNED,
  944. /* Component type and count */
  945. PCT_INT, 3,
  946. /* rbits, gbits, bbits, abits */
  947. 32, 32, 32, 0,
  948. /* Masks and shifts */
  949. 0xFFFFFFFF, 0xFFFFFFFF, 0xFFFFFFFF, 0,
  950. 0, 0, 0, 0,
  951. },
  952. //-----------------------------------------------------------------------
  953. { "PF_RGBA32I",
  954. /* Bytes per element */
  955. 16,
  956. /* Flags */
  957. PFF_INTEGER | PFF_SIGNED | PFF_HASALPHA,
  958. /* Component type and count */
  959. PCT_INT, 4,
  960. /* rbits, gbits, bbits, abits */
  961. 32, 32, 32, 32,
  962. /* Masks and shifts */
  963. 0xFFFFFFFF, 0xFFFFFFFF, 0xFFFFFFFF, 0xFFFFFFFF,
  964. 0, 0, 0, 0
  965. },
  966. //-----------------------------------------------------------------------
  967. { "PF_R32U",
  968. /* Bytes per element */
  969. 4,
  970. /* Flags */
  971. PFF_INTEGER,
  972. /* Component type and count */
  973. PCT_INT, 1,
  974. /* rbits, gbits, bbits, abits */
  975. 32, 0, 0, 0,
  976. /* Masks and shifts */
  977. 0xFFFFFFFF, 0, 0, 0,
  978. 0, 0, 0, 0,
  979. },
  980. //-----------------------------------------------------------------------
  981. { "PF_RG32U",
  982. /* Bytes per element */
  983. 8,
  984. /* Flags */
  985. PFF_INTEGER,
  986. /* Component type and count */
  987. PCT_INT, 2,
  988. /* rbits, gbits, bbits, abits */
  989. 32, 32, 0, 0,
  990. /* Masks and shifts */
  991. 0xFFFFFFFF, 0xFFFFFFFF, 0, 0,
  992. 0, 0, 0, 0,
  993. },
  994. //-----------------------------------------------------------------------
  995. { "PF_RGB32U",
  996. /* Bytes per element */
  997. 12,
  998. /* Flags */
  999. PFF_INTEGER,
  1000. /* Component type and count */
  1001. PCT_INT, 3,
  1002. /* rbits, gbits, bbits, abits */
  1003. 32, 32, 32, 0,
  1004. /* Masks and shifts */
  1005. 0xFFFFFFFF, 0xFFFFFFFF, 0xFFFFFFFF, 0,
  1006. 0, 0, 0, 0,
  1007. },
  1008. //-----------------------------------------------------------------------
  1009. { "PF_RGBA32U",
  1010. /* Bytes per element */
  1011. 16,
  1012. /* Flags */
  1013. PFF_INTEGER | PFF_HASALPHA,
  1014. /* Component type and count */
  1015. PCT_INT, 4,
  1016. /* rbits, gbits, bbits, abits */
  1017. 32, 32, 32, 32,
  1018. /* Masks and shifts */
  1019. 0xFFFFFFFF, 0xFFFFFFFF, 0xFFFFFFFF, 0xFFFFFFFF,
  1020. 0, 0, 0, 0
  1021. },
  1022. //-----------------------------------------------------------------------
  1023. { "PF_R16S",
  1024. /* Bytes per element */
  1025. 2,
  1026. /* Flags */
  1027. PFF_INTEGER | PFF_NORMALIZED | PFF_SIGNED,
  1028. /* Component type and count */
  1029. PCT_SHORT, 1,
  1030. /* rbits, gbits, bbits, abits */
  1031. 16, 0, 0, 0,
  1032. /* Masks and shifts */
  1033. 0x0000FFFF, 0, 0, 0,
  1034. 0, 0, 0, 0,
  1035. },
  1036. //-----------------------------------------------------------------------
  1037. { "PF_RG16S",
  1038. /* Bytes per element */
  1039. 4,
  1040. /* Flags */
  1041. PFF_INTEGER | PFF_NORMALIZED | PFF_SIGNED,
  1042. /* Component type and count */
  1043. PCT_SHORT, 2,
  1044. /* rbits, gbits, bbits, abits */
  1045. 16, 16, 0, 0,
  1046. /* Masks and shifts */
  1047. 0x0000FFFF, 0xFFFF0000, 0, 0,
  1048. 0, 16, 0, 0,
  1049. },
  1050. //-----------------------------------------------------------------------
  1051. { "PF_RGBA16S",
  1052. /* Bytes per element */
  1053. 8,
  1054. /* Flags */
  1055. PFF_INTEGER | PFF_NORMALIZED | PFF_SIGNED | PFF_HASALPHA,
  1056. /* Component type and count */
  1057. PCT_SHORT, 4,
  1058. /* rbits, gbits, bbits, abits */
  1059. 16, 16, 16, 16,
  1060. /* Masks and shifts */
  1061. 0x0000FFFF, 0xFFFF0000, 0x0000FFFF, 0xFFFF0000,
  1062. 0, 16, 0, 16,
  1063. },
  1064. //-----------------------------------------------------------------------
  1065. { "PF_R16",
  1066. /* Bytes per element */
  1067. 2,
  1068. /* Flags */
  1069. PFF_INTEGER | PFF_NORMALIZED,
  1070. /* Component type and count */
  1071. PCT_SHORT, 1,
  1072. /* rbits, gbits, bbits, abits */
  1073. 16, 0, 0, 0,
  1074. /* Masks and shifts */
  1075. 0x0000FFFF, 0, 0, 0,
  1076. 0, 0, 0, 0
  1077. },
  1078. //-----------------------------------------------------------------------
  1079. { "PF_RG16",
  1080. /* Bytes per element */
  1081. 4,
  1082. /* Flags */
  1083. PFF_INTEGER | PFF_NORMALIZED,
  1084. /* Component type and count */
  1085. PCT_SHORT, 2,
  1086. /* rbits, gbits, bbits, abits */
  1087. 16, 16, 0, 0,
  1088. /* Masks and shifts */
  1089. 0x0000FFFF, 0xFFFF0000, 0, 0,
  1090. 0, 16, 0, 0
  1091. },
  1092. //-----------------------------------------------------------------------
  1093. { "PF_RGBA16",
  1094. /* Bytes per element */
  1095. 8,
  1096. /* Flags */
  1097. PFF_INTEGER | PFF_NORMALIZED | PFF_HASALPHA,
  1098. /* Component type and count */
  1099. PCT_SHORT, 4,
  1100. /* rbits, gbits, bbits, abits */
  1101. 16, 16, 16, 16,
  1102. /* Masks and shifts */
  1103. 0x0000FFFF, 0xFFFF0000, 0x0000FFFF, 0xFFFF0000,
  1104. 0, 16, 0, 16
  1105. },
  1106. };
  1107. static inline const PixelFormatDescription &getDescriptionFor(const PixelFormat fmt)
  1108. {
  1109. const int ord = (int)fmt;
  1110. assert(ord >= 0 && ord < PF_COUNT);
  1111. return _pixelFormats[ord];
  1112. }
  1113. /** Handles compression output from NVTT library for a single image. */
  1114. struct NVTTCompressOutputHandler : public nvtt::OutputHandler
  1115. {
  1116. NVTTCompressOutputHandler(UINT8* buffer, UINT32 sizeBytes)
  1117. :buffer(buffer), bufferWritePos(buffer), bufferEnd(buffer + sizeBytes)
  1118. { }
  1119. void beginImage(int size, int width, int height, int depth, int face, int miplevel) override
  1120. { }
  1121. bool writeData(const void* data, int size) override
  1122. {
  1123. assert((bufferWritePos + size) <= bufferEnd);
  1124. memcpy(bufferWritePos, data, size);
  1125. bufferWritePos += size;
  1126. return true;
  1127. }
  1128. void endImage() override
  1129. { }
  1130. UINT8* buffer;
  1131. UINT8* bufferWritePos;
  1132. UINT8* bufferEnd;
  1133. };
  1134. /** Handles output from NVTT library for a mip-map chain. */
  1135. struct NVTTMipmapOutputHandler : public nvtt::OutputHandler
  1136. {
  1137. NVTTMipmapOutputHandler(const Vector<SPtr<PixelData>>& buffers)
  1138. :buffers(buffers), bufferWritePos(nullptr), bufferEnd(nullptr)
  1139. { }
  1140. virtual void beginImage(int size, int width, int height, int depth, int face, int miplevel)
  1141. {
  1142. assert(miplevel >= 0 && miplevel < (int)buffers.size());
  1143. assert(size == buffers[miplevel]->getConsecutiveSize());
  1144. activeBuffer = buffers[miplevel];
  1145. bufferWritePos = activeBuffer->getData();
  1146. bufferEnd = bufferWritePos + activeBuffer->getConsecutiveSize();
  1147. }
  1148. virtual bool writeData(const void* data, int size)
  1149. {
  1150. assert((bufferWritePos + size) <= bufferEnd);
  1151. memcpy(bufferWritePos, data, size);
  1152. bufferWritePos += size;
  1153. return true;
  1154. }
  1155. void endImage() override
  1156. { }
  1157. Vector<SPtr<PixelData>> buffers;
  1158. SPtr<PixelData> activeBuffer;
  1159. UINT8* bufferWritePos;
  1160. UINT8* bufferEnd;
  1161. };
  1162. nvtt::Format toNVTTFormat(PixelFormat format)
  1163. {
  1164. switch (format)
  1165. {
  1166. case PF_BC1:
  1167. return nvtt::Format_BC1;
  1168. case PF_BC1a:
  1169. return nvtt::Format_BC1a;
  1170. case PF_BC2:
  1171. return nvtt::Format_BC2;
  1172. case PF_BC3:
  1173. return nvtt::Format_BC3;
  1174. case PF_BC4:
  1175. return nvtt::Format_BC4;
  1176. case PF_BC5:
  1177. return nvtt::Format_BC5;
  1178. case PF_BC6H:
  1179. return nvtt::Format_BC6;
  1180. case PF_BC7:
  1181. return nvtt::Format_BC7;
  1182. default: // Unsupported format
  1183. return nvtt::Format_BC3;
  1184. }
  1185. }
  1186. nvtt::Quality toNVTTQuality(CompressionQuality quality)
  1187. {
  1188. switch (quality)
  1189. {
  1190. case CompressionQuality::Fastest:
  1191. return nvtt::Quality_Fastest;
  1192. case CompressionQuality::Highest:
  1193. return nvtt::Quality_Highest;
  1194. case CompressionQuality::Normal:
  1195. return nvtt::Quality_Normal;
  1196. case CompressionQuality::Production:
  1197. return nvtt::Quality_Normal;
  1198. }
  1199. // Unknown quality level
  1200. return nvtt::Quality_Normal;
  1201. }
  1202. nvtt::AlphaMode toNVTTAlphaMode(AlphaMode alphaMode)
  1203. {
  1204. switch (alphaMode)
  1205. {
  1206. case AlphaMode::None:
  1207. return nvtt::AlphaMode_None;
  1208. case AlphaMode::Premultiplied:
  1209. return nvtt::AlphaMode_Premultiplied;
  1210. case AlphaMode::Transparency:
  1211. return nvtt::AlphaMode_Transparency;
  1212. }
  1213. // Unknown alpha mode
  1214. return nvtt::AlphaMode_None;
  1215. }
  1216. nvtt::WrapMode toNVTTWrapMode(MipMapWrapMode wrapMode)
  1217. {
  1218. switch (wrapMode)
  1219. {
  1220. case MipMapWrapMode::Clamp:
  1221. return nvtt::WrapMode_Clamp;
  1222. case MipMapWrapMode::Mirror:
  1223. return nvtt::WrapMode_Mirror;
  1224. case MipMapWrapMode::Repeat:
  1225. return nvtt::WrapMode_Repeat;
  1226. }
  1227. // Unknown alpha mode
  1228. return nvtt::WrapMode_Mirror;
  1229. }
  1230. UINT32 PixelUtil::getNumElemBytes(PixelFormat format)
  1231. {
  1232. return getDescriptionFor(format).elemBytes;
  1233. }
  1234. UINT32 PixelUtil::getMemorySize(UINT32 width, UINT32 height, UINT32 depth, PixelFormat format)
  1235. {
  1236. if(isCompressed(format))
  1237. {
  1238. switch(format)
  1239. {
  1240. // BC formats work by dividing the image into 4x4 blocks, then encoding each
  1241. // 4x4 block with a certain number of bytes.
  1242. case PF_BC1:
  1243. case PF_BC1a:
  1244. case PF_BC4:
  1245. return ((width+3)/4)*((height+3)/4)*8 * depth;
  1246. case PF_BC2:
  1247. case PF_BC3:
  1248. case PF_BC5:
  1249. case PF_BC6H:
  1250. case PF_BC7:
  1251. return ((width+3)/4)*((height+3)/4)*16 * depth;
  1252. default:
  1253. BS_EXCEPT(InvalidParametersException, "Invalid compressed pixel format");
  1254. return 0;
  1255. }
  1256. }
  1257. return width*height*depth*getNumElemBytes(format);
  1258. }
  1259. void PixelUtil::getPitch(UINT32 width, UINT32 height, UINT32 depth, PixelFormat format,
  1260. UINT32& rowPitch, UINT32& depthPitch)
  1261. {
  1262. if (isCompressed(format))
  1263. {
  1264. switch (format)
  1265. {
  1266. // BC formats work by dividing the image into 4x4 blocks, then encoding each
  1267. // 4x4 block with a certain number of bytes.
  1268. case PF_BC1:
  1269. case PF_BC1a:
  1270. case PF_BC4:
  1271. case PF_BC2:
  1272. case PF_BC3:
  1273. case PF_BC5:
  1274. case PF_BC6H:
  1275. case PF_BC7:
  1276. rowPitch = div(width + 3, 4).quot * 4;
  1277. depthPitch = div(height + 3, 4).quot * 4 * rowPitch;
  1278. return;
  1279. default:
  1280. BS_EXCEPT(InvalidParametersException, "Invalid compressed pixel format");
  1281. return;
  1282. }
  1283. }
  1284. rowPitch = width;
  1285. depthPitch = width * height;
  1286. }
  1287. void PixelUtil::getSizeForMipLevel(UINT32 width, UINT32 height, UINT32 depth, UINT32 mipLevel,
  1288. UINT32& mipWidth, UINT32& mipHeight, UINT32& mipDepth)
  1289. {
  1290. mipWidth = width;
  1291. mipHeight = height;
  1292. mipDepth = depth;
  1293. for (UINT32 i = 0; i < mipLevel; i++)
  1294. {
  1295. if (mipWidth != 1) mipWidth /= 2;
  1296. if (mipHeight != 1) mipHeight /= 2;
  1297. if (mipDepth != 1) mipDepth /= 2;
  1298. }
  1299. }
  1300. UINT32 PixelUtil::getNumElemBits(PixelFormat format)
  1301. {
  1302. return getDescriptionFor(format).elemBytes * 8;
  1303. }
  1304. UINT32 PixelUtil::getFlags(PixelFormat format)
  1305. {
  1306. return getDescriptionFor(format).flags;
  1307. }
  1308. bool PixelUtil::hasAlpha(PixelFormat format)
  1309. {
  1310. return (PixelUtil::getFlags(format) & PFF_HASALPHA) > 0;
  1311. }
  1312. bool PixelUtil::isFloatingPoint(PixelFormat format)
  1313. {
  1314. return (PixelUtil::getFlags(format) & PFF_FLOAT) > 0;
  1315. }
  1316. bool PixelUtil::isCompressed(PixelFormat format)
  1317. {
  1318. return (PixelUtil::getFlags(format) & PFF_COMPRESSED) > 0;
  1319. }
  1320. bool PixelUtil::isNormalized(PixelFormat format)
  1321. {
  1322. return (PixelUtil::getFlags(format) & PFF_NORMALIZED) > 0;
  1323. }
  1324. bool PixelUtil::isDepth(PixelFormat format)
  1325. {
  1326. return (PixelUtil::getFlags(format) & PFF_DEPTH) > 0;
  1327. }
  1328. bool PixelUtil::checkFormat(PixelFormat& format, TextureType texType, int usage)
  1329. {
  1330. // First check just the usage since it's the most limiting factor
  1331. //// Depth-stencil only supports depth formats
  1332. if ((usage & TU_DEPTHSTENCIL) != 0)
  1333. {
  1334. if (isDepth(format))
  1335. return true;
  1336. format = PF_D32_S8X24;
  1337. return false;
  1338. }
  1339. //// Render targets support everything but compressed & depth-stencil formats
  1340. if ((usage & TU_RENDERTARGET) != 0)
  1341. {
  1342. if (!isDepth(format) && !isCompressed(format))
  1343. return true;
  1344. format = PF_RGBA8;
  1345. return false;
  1346. }
  1347. //// Load-store textures support everything but compressed & depth-stencil formats
  1348. if ((usage & TU_LOADSTORE) != 0)
  1349. {
  1350. if (!isDepth(format) && !isCompressed(format))
  1351. return true;
  1352. format = PF_RGBA8;
  1353. return false;
  1354. }
  1355. //// Sampled texture support depends on texture type
  1356. switch (texType)
  1357. {
  1358. case TEX_TYPE_1D:
  1359. {
  1360. // 1D textures support anything but depth & compressed formats
  1361. if (!isDepth(format) && !isCompressed(format))
  1362. return true;
  1363. format = PF_RGBA8;
  1364. return false;
  1365. }
  1366. case TEX_TYPE_3D:
  1367. {
  1368. // 3D textures support anything but depth & compressed formats
  1369. if (!isDepth(format))
  1370. return true;
  1371. format = PF_RGBA8;
  1372. return false;
  1373. }
  1374. default: // 2D & cube
  1375. {
  1376. // 2D/cube textures support anything but depth formats
  1377. if (!isDepth(format))
  1378. return true;
  1379. format = PF_RGBA8;
  1380. return false;
  1381. }
  1382. }
  1383. }
  1384. bool PixelUtil::isValidExtent(UINT32 width, UINT32 height, UINT32 depth, PixelFormat format)
  1385. {
  1386. if(isCompressed(format))
  1387. {
  1388. switch(format)
  1389. {
  1390. case PF_BC1:
  1391. case PF_BC2:
  1392. case PF_BC1a:
  1393. case PF_BC3:
  1394. case PF_BC4:
  1395. case PF_BC5:
  1396. case PF_BC6H:
  1397. case PF_BC7:
  1398. return ((width & 3) == 0 && (height & 3) == 0 && depth == 1);
  1399. default:
  1400. return true;
  1401. }
  1402. }
  1403. else
  1404. {
  1405. return true;
  1406. }
  1407. }
  1408. void PixelUtil::getBitDepths(PixelFormat format, int(&rgba)[4])
  1409. {
  1410. const PixelFormatDescription& des = getDescriptionFor(format);
  1411. rgba[0] = des.rbits;
  1412. rgba[1] = des.gbits;
  1413. rgba[2] = des.bbits;
  1414. rgba[3] = des.abits;
  1415. }
  1416. void PixelUtil::getBitMasks(PixelFormat format, UINT32(&rgba)[4])
  1417. {
  1418. const PixelFormatDescription& des = getDescriptionFor(format);
  1419. rgba[0] = des.rmask;
  1420. rgba[1] = des.gmask;
  1421. rgba[2] = des.bmask;
  1422. rgba[3] = des.amask;
  1423. }
  1424. void PixelUtil::getBitShifts(PixelFormat format, UINT8(&rgba)[4])
  1425. {
  1426. const PixelFormatDescription& des = getDescriptionFor(format);
  1427. rgba[0] = des.rshift;
  1428. rgba[1] = des.gshift;
  1429. rgba[2] = des.bshift;
  1430. rgba[3] = des.ashift;
  1431. }
  1432. String PixelUtil::getFormatName(PixelFormat srcformat)
  1433. {
  1434. return getDescriptionFor(srcformat).name;
  1435. }
  1436. bool PixelUtil::isAccessible(PixelFormat srcformat)
  1437. {
  1438. if (srcformat == PF_UNKNOWN)
  1439. return false;
  1440. UINT32 flags = getFlags(srcformat);
  1441. return !((flags & PFF_COMPRESSED) || (flags & PFF_DEPTH));
  1442. }
  1443. PixelComponentType PixelUtil::getElementType(PixelFormat format)
  1444. {
  1445. const PixelFormatDescription& des = getDescriptionFor(format);
  1446. return des.componentType;
  1447. }
  1448. UINT32 PixelUtil::getNumElements(PixelFormat format)
  1449. {
  1450. const PixelFormatDescription& des = getDescriptionFor(format);
  1451. return des.componentCount;
  1452. }
  1453. UINT32 PixelUtil::getMaxMipmaps(UINT32 width, UINT32 height, UINT32 depth, PixelFormat format)
  1454. {
  1455. UINT32 count = 0;
  1456. if ((width > 0) && (height > 0))
  1457. {
  1458. while (!(width == 1 && height == 1 && depth == 1))
  1459. {
  1460. if (width > 1) width = width / 2;
  1461. if (height > 1) height = height / 2;
  1462. if (depth > 1) depth = depth / 2;
  1463. count++;
  1464. }
  1465. }
  1466. return count;
  1467. }
  1468. void PixelUtil::packColor(const Color& color, PixelFormat format, void* dest)
  1469. {
  1470. packColor(color.r, color.g, color.b, color.a, format, dest);
  1471. }
  1472. void PixelUtil::packColor(UINT8 r, UINT8 g, UINT8 b, UINT8 a, PixelFormat format, void* dest)
  1473. {
  1474. const PixelFormatDescription &des = getDescriptionFor(format);
  1475. if (des.flags & PFF_INTEGER)
  1476. {
  1477. // Shortcut for integer formats packing
  1478. UINT32 value = ((Bitwise::fixedToFixed(r, 8, des.rbits) << des.rshift) & des.rmask) |
  1479. ((Bitwise::fixedToFixed(g, 8, des.gbits) << des.gshift) & des.gmask) |
  1480. ((Bitwise::fixedToFixed(b, 8, des.bbits) << des.bshift) & des.bmask) |
  1481. ((Bitwise::fixedToFixed(a, 8, des.abits) << des.ashift) & des.amask);
  1482. // And write to memory
  1483. Bitwise::intWrite(dest, des.elemBytes, value);
  1484. }
  1485. else
  1486. {
  1487. // Convert to float
  1488. packColor((float)r / 255.0f, (float)g / 255.0f, (float)b / 255.0f, (float)a / 255.0f, format, dest);
  1489. }
  1490. }
  1491. void PixelUtil::packColor(float r, float g, float b, float a, const PixelFormat format, void* dest)
  1492. {
  1493. // Special cases
  1494. if (format == PF_RG11B10F)
  1495. {
  1496. UINT32 value;
  1497. value = Bitwise::floatToFloat11(r);
  1498. value |= Bitwise::floatToFloat11(g) << 11;
  1499. value |= Bitwise::floatToFloat10(b) << 22;
  1500. ((UINT32*)dest)[0] = value;
  1501. return;
  1502. }
  1503. if (format == PF_RGB10A2)
  1504. {
  1505. LOGERR("packColor() not implemented for format \"" + getFormatName(PF_RGB10A2) + "\".");
  1506. return;
  1507. }
  1508. // All other formats handled in a generic way
  1509. const PixelFormatDescription& des = getDescriptionFor(format);
  1510. assert(des.componentCount <= 4);
  1511. float inputs[] = { r, g, b, a };
  1512. UINT8 bits[] = { des.rbits, des.gbits, des.bbits, des.abits };
  1513. UINT32 masks[] = { des.rmask, des.gmask, des.bmask, des.amask };
  1514. UINT8 shifts[] = { des.rshift, des.gshift, des.bshift, des.ashift };
  1515. memset(dest, 0, des.elemBytes);
  1516. UINT32 curBit = 0;
  1517. UINT32 prevDword = 0;
  1518. UINT32 dwordValue = 0;
  1519. for (UINT32 i = 0; i < des.componentCount; i++)
  1520. {
  1521. UINT32 curDword = curBit / 32;
  1522. // New dword reached, write current one and advance
  1523. if(curDword > prevDword)
  1524. {
  1525. UINT32* curDst = ((UINT32*)dest) + prevDword;
  1526. Bitwise::intWrite(curDst, 4, dwordValue);
  1527. dwordValue = 0;
  1528. prevDword = curDword;
  1529. }
  1530. if (des.flags & PFF_INTEGER)
  1531. {
  1532. if (des.flags & PFF_NORMALIZED)
  1533. {
  1534. if (des.flags & PFF_SIGNED)
  1535. dwordValue |= (Bitwise::snormToUint(inputs[i], bits[i]) << shifts[i]) & masks[i];
  1536. else
  1537. dwordValue |= (Bitwise::unormToUint(inputs[i], bits[i]) << shifts[i]) & masks[i];
  1538. }
  1539. else
  1540. {
  1541. // Note: Casting integer to float. A better option would be to have a separate unpackColor that has
  1542. // integer output parameters.
  1543. dwordValue |= (((UINT32)inputs[i]) << shifts[i]) & masks[i];
  1544. }
  1545. }
  1546. else if (des.flags & PFF_FLOAT)
  1547. {
  1548. // Note: Not handling unsigned floats
  1549. if (des.componentType == PCT_FLOAT16)
  1550. dwordValue |= (Bitwise::floatToHalf(inputs[i]) << shifts[i]) & masks[i];
  1551. else
  1552. dwordValue |= *(UINT32*)&inputs[i];
  1553. }
  1554. else
  1555. {
  1556. LOGERR("packColor() not implemented for format \"" + getFormatName(format) + "\".");
  1557. return;
  1558. }
  1559. curBit += bits[i];
  1560. }
  1561. // Write last dword
  1562. UINT32 numBytes = std::min((prevDword + 1) * 4, (UINT32)des.elemBytes) - (prevDword * 4);
  1563. UINT32* curDst = ((UINT32*)dest) + prevDword;
  1564. Bitwise::intWrite(curDst, numBytes, dwordValue);
  1565. }
  1566. void PixelUtil::unpackColor(Color* color, PixelFormat format, const void* src)
  1567. {
  1568. unpackColor(&color->r, &color->g, &color->b, &color->a, format, src);
  1569. }
  1570. void PixelUtil::unpackColor(UINT8* r, UINT8* g, UINT8* b, UINT8* a, PixelFormat format, const void* src)
  1571. {
  1572. const PixelFormatDescription &des = getDescriptionFor(format);
  1573. if (des.flags & PFF_INTEGER)
  1574. {
  1575. // Shortcut for integer formats unpacking
  1576. const UINT32 value = Bitwise::intRead(src, des.elemBytes);
  1577. *r = (UINT8)Bitwise::fixedToFixed((value & des.rmask) >> des.rshift, des.rbits, 8);
  1578. *g = (UINT8)Bitwise::fixedToFixed((value & des.gmask) >> des.gshift, des.gbits, 8);
  1579. *b = (UINT8)Bitwise::fixedToFixed((value & des.bmask) >> des.bshift, des.bbits, 8);
  1580. if (des.flags & PFF_HASALPHA)
  1581. {
  1582. *a = (UINT8)Bitwise::fixedToFixed((value & des.amask) >> des.ashift, des.abits, 8);
  1583. }
  1584. else
  1585. {
  1586. *a = 255; // No alpha, default a component to full
  1587. }
  1588. }
  1589. else
  1590. {
  1591. // Do the operation with the more generic floating point
  1592. float rr, gg, bb, aa;
  1593. unpackColor(&rr, &gg, &bb, &aa, format, src);
  1594. *r = (UINT8)Bitwise::unormToUint(rr, 8);
  1595. *g = (UINT8)Bitwise::unormToUint(gg, 8);
  1596. *b = (UINT8)Bitwise::unormToUint(bb, 8);
  1597. *a = (UINT8)Bitwise::unormToUint(aa, 8);
  1598. }
  1599. }
  1600. void PixelUtil::unpackColor(float* r, float* g, float* b, float* a, PixelFormat format, const void* src)
  1601. {
  1602. // Special cases
  1603. if(format == PF_RG11B10F)
  1604. {
  1605. UINT32 value = ((UINT32*)src)[0];
  1606. *r = Bitwise::float11ToFloat(value);
  1607. *g = Bitwise::float11ToFloat(value >> 11);
  1608. *b = Bitwise::float10ToFloat(value >> 22);
  1609. return;
  1610. }
  1611. if(format == PF_RGB10A2)
  1612. {
  1613. LOGERR("unpackColor() not implemented for format \"" + getFormatName(PF_RGB10A2) + "\".");
  1614. return;
  1615. }
  1616. // All other formats handled in a generic way
  1617. const PixelFormatDescription& des = getDescriptionFor(format);
  1618. assert(des.componentCount <= 4);
  1619. float* outputs[] = { r, g, b, a };
  1620. UINT8 bits[] = { des.rbits, des.gbits, des.bbits, des.abits };
  1621. UINT32 masks[] = { des.rmask, des.gmask, des.bmask, des.amask };
  1622. UINT8 shifts[] = { des.rshift, des.gshift, des.bshift, des.ashift };
  1623. UINT32 curBit = 0;
  1624. for(UINT32 i = 0; i < des.componentCount; i++)
  1625. {
  1626. UINT32 curDword = curBit / 32;
  1627. UINT32 numBytes = std::min((curDword + 1) * 4, (UINT32)des.elemBytes) - (curDword * 4);
  1628. UINT32* curSrc = ((UINT32*)src) + curDword;
  1629. UINT32 value = Bitwise::intRead(curSrc, numBytes);
  1630. if(des.flags & PFF_INTEGER)
  1631. {
  1632. if(des.flags & PFF_NORMALIZED)
  1633. {
  1634. if (des.flags & PFF_SIGNED)
  1635. *outputs[i] = Bitwise::uintToSnorm((value & masks[i]) >> shifts[i], bits[i]);
  1636. else
  1637. *outputs[i] = Bitwise::uintToUnorm((value & masks[i]) >> shifts[i], bits[i]);
  1638. }
  1639. else
  1640. {
  1641. // Note: Casting integer to float. A better option would be to have a separate unpackColor that has
  1642. // integer output parameters.
  1643. *outputs[i] = (float)((value & masks[i]) >> shifts[i]);
  1644. }
  1645. }
  1646. else if(des.flags & PFF_FLOAT)
  1647. {
  1648. // Note: Not handling unsigned floats
  1649. if (des.componentType == PCT_FLOAT16)
  1650. *outputs[i] = Bitwise::halfToFloat((UINT16)((value & masks[i]) >> shifts[i]));
  1651. else
  1652. *outputs[i] = *(float*)&value;
  1653. }
  1654. else
  1655. {
  1656. LOGERR("unpackColor() not implemented for format \"" + getFormatName(format) + "\".");
  1657. return;
  1658. }
  1659. curBit += bits[i];
  1660. }
  1661. // Fill empty components
  1662. for (UINT32 i = des.componentCount; i < 3; i++)
  1663. *outputs[i] = 0.0f;
  1664. if (des.componentCount < 4)
  1665. *outputs[3] = 1.0f;
  1666. }
  1667. void PixelUtil::packDepth(float depth, const PixelFormat format, void* dest)
  1668. {
  1669. if (!isDepth(format))
  1670. {
  1671. LOGERR("Cannot convert depth to " + getFormatName(format) + ": it is not a depth format");
  1672. return;
  1673. }
  1674. LOGERR("Method is not implemented");
  1675. //TODO implement depth packing
  1676. }
  1677. float PixelUtil::unpackDepth(PixelFormat format, void* src)
  1678. {
  1679. const PixelFormatDescription &des = getDescriptionFor(format);
  1680. if (!isDepth(format))
  1681. {
  1682. LOGERR("Cannot unpack from " + getFormatName(format) + ": it is not a depth format");
  1683. return 0;
  1684. }
  1685. UINT32* color = (UINT32 *)src;
  1686. UINT32 masked = 0;
  1687. switch (format)
  1688. {
  1689. case PF_D24S8:
  1690. return static_cast<float>(*color & 0x00FFFFFF) / (float)16777216;
  1691. break;
  1692. case PF_D16:
  1693. return static_cast<float>(*color & 0xFFFF) / (float)65536;
  1694. break;
  1695. case PF_D32:
  1696. masked = *color & 0xFFFFFFFF;
  1697. return *((float*)&masked);
  1698. break;
  1699. case PF_D32_S8X24:
  1700. masked = *color & 0xFFFFFFFF;
  1701. return *( (float*) &masked );
  1702. break;
  1703. default:
  1704. LOGERR("Cannot unpack from " + getFormatName(format));
  1705. return 0;
  1706. break;
  1707. }
  1708. }
  1709. void PixelUtil::bulkPixelConversion(const PixelData &src, PixelData &dst)
  1710. {
  1711. assert(src.getWidth() == dst.getWidth() &&
  1712. src.getHeight() == dst.getHeight() &&
  1713. src.getDepth() == dst.getDepth());
  1714. // Check for compressed formats, we don't support decompression
  1715. if (PixelUtil::isCompressed(src.getFormat()))
  1716. {
  1717. if (src.getFormat() == dst.getFormat())
  1718. {
  1719. memcpy(dst.getData(), src.getData(), src.getConsecutiveSize());
  1720. return;
  1721. }
  1722. else
  1723. {
  1724. LOGERR("bulkPixelConversion() cannot be used to compress or decompress images");
  1725. return;
  1726. }
  1727. }
  1728. // Check for compression
  1729. if (PixelUtil::isCompressed(dst.getFormat()))
  1730. {
  1731. if (src.getFormat() == dst.getFormat())
  1732. {
  1733. memcpy(dst.getData(), src.getData(), src.getConsecutiveSize());
  1734. return;
  1735. }
  1736. else
  1737. {
  1738. CompressionOptions co;
  1739. co.format = dst.getFormat();
  1740. compress(src, dst, co);
  1741. return;
  1742. }
  1743. }
  1744. // The easy case
  1745. if (src.getFormat() == dst.getFormat())
  1746. {
  1747. // Everything consecutive?
  1748. if (src.isConsecutive() && dst.isConsecutive())
  1749. {
  1750. memcpy(dst.getData(), src.getData(), src.getConsecutiveSize());
  1751. return;
  1752. }
  1753. const UINT32 srcPixelSize = PixelUtil::getNumElemBytes(src.getFormat());
  1754. const UINT32 dstPixelSize = PixelUtil::getNumElemBytes(dst.getFormat());
  1755. UINT8 *srcptr = static_cast<UINT8*>(src.getData())
  1756. + (src.getLeft() + src.getTop() * src.getRowPitch() + src.getFront() * src.getSlicePitch()) * srcPixelSize;
  1757. UINT8 *dstptr = static_cast<UINT8*>(dst.getData())
  1758. + (dst.getLeft() + dst.getTop() * dst.getRowPitch() + dst.getFront() * dst.getSlicePitch()) * dstPixelSize;
  1759. // Calculate pitches+skips in bytes
  1760. const UINT32 srcRowPitchBytes = src.getRowPitch()*srcPixelSize;
  1761. const UINT32 srcSliceSkipBytes = src.getSliceSkip()*srcPixelSize;
  1762. const UINT32 dstRowPitchBytes = dst.getRowPitch()*dstPixelSize;
  1763. const UINT32 dstSliceSkipBytes = dst.getSliceSkip()*dstPixelSize;
  1764. // Otherwise, copy per row
  1765. const UINT32 rowSize = src.getWidth()*srcPixelSize;
  1766. for (UINT32 z = src.getFront(); z < src.getBack(); z++)
  1767. {
  1768. for (UINT32 y = src.getTop(); y < src.getBottom(); y++)
  1769. {
  1770. memcpy(dstptr, srcptr, rowSize);
  1771. srcptr += srcRowPitchBytes;
  1772. dstptr += dstRowPitchBytes;
  1773. }
  1774. srcptr += srcSliceSkipBytes;
  1775. dstptr += dstSliceSkipBytes;
  1776. }
  1777. return;
  1778. }
  1779. UINT32 srcPixelSize = PixelUtil::getNumElemBytes(src.getFormat());
  1780. UINT32 dstPixelSize = PixelUtil::getNumElemBytes(dst.getFormat());
  1781. UINT8 *srcptr = static_cast<UINT8*>(src.getData())
  1782. + (src.getLeft() + src.getTop() * src.getRowPitch() + src.getFront() * src.getSlicePitch()) * srcPixelSize;
  1783. UINT8 *dstptr = static_cast<UINT8*>(dst.getData())
  1784. + (dst.getLeft() + dst.getTop() * dst.getRowPitch() + dst.getFront() * dst.getSlicePitch()) * dstPixelSize;
  1785. // Calculate pitches+skips in bytes
  1786. UINT32 srcRowSkipBytes = src.getRowSkip()*srcPixelSize;
  1787. UINT32 srcSliceSkipBytes = src.getSliceSkip()*srcPixelSize;
  1788. UINT32 dstRowSkipBytes = dst.getRowSkip()*dstPixelSize;
  1789. UINT32 dstSliceSkipBytes = dst.getSliceSkip()*dstPixelSize;
  1790. // The brute force fallback
  1791. float r, g, b, a;
  1792. for (UINT32 z = src.getFront(); z < src.getBack(); z++)
  1793. {
  1794. for (UINT32 y = src.getTop(); y < src.getBottom(); y++)
  1795. {
  1796. for (UINT32 x = src.getLeft(); x < src.getRight(); x++)
  1797. {
  1798. unpackColor(&r, &g, &b, &a, src.getFormat(), srcptr);
  1799. packColor(r, g, b, a, dst.getFormat(), dstptr);
  1800. srcptr += srcPixelSize;
  1801. dstptr += dstPixelSize;
  1802. }
  1803. srcptr += srcRowSkipBytes;
  1804. dstptr += dstRowSkipBytes;
  1805. }
  1806. srcptr += srcSliceSkipBytes;
  1807. dstptr += dstSliceSkipBytes;
  1808. }
  1809. }
  1810. void PixelUtil::flipComponentOrder(PixelData& data)
  1811. {
  1812. if (isCompressed(data.getFormat()))
  1813. {
  1814. LOGERR("flipComponentOrder() not supported on compressed images.");
  1815. return;
  1816. }
  1817. const PixelFormatDescription& pfd = getDescriptionFor(data.getFormat());
  1818. if(pfd.elemBytes > 4)
  1819. {
  1820. LOGERR("flipComponentOrder() only supported on 4 byte or smaller pixel formats.");
  1821. return;
  1822. }
  1823. if (pfd.componentCount <= 1) // Nothing to flip
  1824. return;
  1825. bool bitCountMismatch = false;
  1826. if (pfd.rbits != pfd.gbits)
  1827. bitCountMismatch = true;
  1828. if(pfd.componentCount > 2 && pfd.rbits != pfd.bbits)
  1829. bitCountMismatch = true;
  1830. if (pfd.componentCount > 3 && pfd.rbits != pfd.abits)
  1831. bitCountMismatch = true;
  1832. if(bitCountMismatch)
  1833. {
  1834. LOGERR("flipComponentOrder() not supported for formats that don't have the same number of bytes for all components.");
  1835. return;
  1836. }
  1837. struct CompData
  1838. {
  1839. UINT32 mask;
  1840. UINT8 shift;
  1841. };
  1842. std::array<CompData, 4> compData =
  1843. {{
  1844. { pfd.rmask, pfd.rshift },
  1845. { pfd.gmask, pfd.gshift },
  1846. { pfd.bmask, pfd.bshift },
  1847. { pfd.amask, pfd.ashift }
  1848. }};
  1849. // Ensure unused components are at the end, after sort
  1850. if (pfd.componentCount < 4)
  1851. compData[4].shift = 0xFF;
  1852. if (pfd.componentCount < 3)
  1853. compData[3].shift = 0xFF;
  1854. std::sort(compData.begin(), compData.end(),
  1855. [&](const CompData& lhs, const CompData& rhs) { return lhs.shift < rhs.shift; }
  1856. );
  1857. UINT8* dataPtr = data.getData();
  1858. UINT32 pixelSize = pfd.elemBytes;
  1859. UINT32 rowSkipBytes = data.getRowSkip()*pixelSize;
  1860. UINT32 sliceSkipBytes = data.getSliceSkip()*pixelSize;
  1861. for (UINT32 z = 0; z < data.getDepth(); z++)
  1862. {
  1863. for (UINT32 y = 0; y < data.getHeight(); y++)
  1864. {
  1865. for (UINT32 x = 0; x < data.getWidth(); x++)
  1866. {
  1867. if(pfd.componentCount == 2)
  1868. {
  1869. UINT64 pixelData = 0;
  1870. memcpy(&pixelData, dataPtr, pixelSize);
  1871. UINT64 output = 0;
  1872. output |= (pixelData & compData[1].mask) >> compData[1].shift;
  1873. output |= (pixelData & compData[0].mask) << compData[1].shift;
  1874. memcpy(dataPtr, &output, pixelSize);
  1875. }
  1876. else if(pfd.componentCount == 3)
  1877. {
  1878. UINT64 pixelData = 0;
  1879. memcpy(&pixelData, dataPtr, pixelSize);
  1880. UINT64 output = 0;
  1881. output |= (pixelData & compData[2].mask) >> compData[2].shift;
  1882. output |= (pixelData & compData[0].mask) << compData[2].shift;
  1883. memcpy(dataPtr, &output, pixelSize);
  1884. }
  1885. else if(pfd.componentCount == 4)
  1886. {
  1887. UINT64 pixelData = 0;
  1888. memcpy(&pixelData, dataPtr, pixelSize);
  1889. UINT64 output = 0;
  1890. output |= (pixelData & compData[3].mask) >> compData[3].shift;
  1891. output |= (pixelData & compData[0].mask) << compData[3].shift;
  1892. output |= (pixelData & compData[2].mask) >> (compData[2].shift - compData[1].shift);
  1893. output |= (pixelData & compData[1].mask) << (compData[2].shift - compData[1].shift);
  1894. memcpy(dataPtr, &output, pixelSize);
  1895. }
  1896. dataPtr += pixelSize;
  1897. }
  1898. dataPtr += rowSkipBytes;
  1899. }
  1900. dataPtr += sliceSkipBytes;
  1901. }
  1902. }
  1903. void PixelUtil::scale(const PixelData& src, PixelData& scaled, Filter filter)
  1904. {
  1905. assert(PixelUtil::isAccessible(src.getFormat()));
  1906. assert(PixelUtil::isAccessible(scaled.getFormat()));
  1907. PixelData temp;
  1908. switch (filter)
  1909. {
  1910. default:
  1911. case FILTER_NEAREST:
  1912. if(src.getFormat() == scaled.getFormat())
  1913. {
  1914. // No intermediate buffer needed
  1915. temp = scaled;
  1916. }
  1917. else
  1918. {
  1919. // Allocate temporary buffer of destination size in source format
  1920. temp = PixelData(scaled.getWidth(), scaled.getHeight(), scaled.getDepth(), src.getFormat());
  1921. temp.allocateInternalBuffer();
  1922. }
  1923. // No conversion
  1924. switch (PixelUtil::getNumElemBytes(src.getFormat()))
  1925. {
  1926. case 1: NearestResampler<1>::scale(src, temp); break;
  1927. case 2: NearestResampler<2>::scale(src, temp); break;
  1928. case 3: NearestResampler<3>::scale(src, temp); break;
  1929. case 4: NearestResampler<4>::scale(src, temp); break;
  1930. case 6: NearestResampler<6>::scale(src, temp); break;
  1931. case 8: NearestResampler<8>::scale(src, temp); break;
  1932. case 12: NearestResampler<12>::scale(src, temp); break;
  1933. case 16: NearestResampler<16>::scale(src, temp); break;
  1934. default:
  1935. // Never reached
  1936. assert(false);
  1937. }
  1938. if(temp.getData() != scaled.getData())
  1939. {
  1940. // Blit temp buffer
  1941. PixelUtil::bulkPixelConversion(temp, scaled);
  1942. temp.freeInternalBuffer();
  1943. }
  1944. break;
  1945. case FILTER_LINEAR:
  1946. switch (src.getFormat())
  1947. {
  1948. case PF_RG8:
  1949. case PF_RGB8: case PF_BGR8:
  1950. case PF_RGBA8: case PF_BGRA8:
  1951. if(src.getFormat() == scaled.getFormat())
  1952. {
  1953. // No intermediate buffer needed
  1954. temp = scaled;
  1955. }
  1956. else
  1957. {
  1958. // Allocate temp buffer of destination size in source format
  1959. temp = PixelData(scaled.getWidth(), scaled.getHeight(), scaled.getDepth(), src.getFormat());
  1960. temp.allocateInternalBuffer();
  1961. }
  1962. // No conversion
  1963. switch (PixelUtil::getNumElemBytes(src.getFormat()))
  1964. {
  1965. case 1: LinearResampler_Byte<1>::scale(src, temp); break;
  1966. case 2: LinearResampler_Byte<2>::scale(src, temp); break;
  1967. case 3: LinearResampler_Byte<3>::scale(src, temp); break;
  1968. case 4: LinearResampler_Byte<4>::scale(src, temp); break;
  1969. default:
  1970. // Never reached
  1971. assert(false);
  1972. }
  1973. if(temp.getData() != scaled.getData())
  1974. {
  1975. // Blit temp buffer
  1976. PixelUtil::bulkPixelConversion(temp, scaled);
  1977. temp.freeInternalBuffer();
  1978. }
  1979. break;
  1980. case PF_RGB32F:
  1981. case PF_RGBA32F:
  1982. if (scaled.getFormat() == PF_RGB32F || scaled.getFormat() == PF_RGBA32F)
  1983. {
  1984. // float32 to float32, avoid unpack/repack overhead
  1985. LinearResampler_Float32::scale(src, scaled);
  1986. break;
  1987. }
  1988. // Else, fall through
  1989. default:
  1990. // Fallback case, slow but works
  1991. LinearResampler::scale(src, scaled);
  1992. }
  1993. break;
  1994. }
  1995. }
  1996. void PixelUtil::copy(const PixelData& src, PixelData& dst, UINT32 offsetX, UINT32 offsetY, UINT32 offsetZ)
  1997. {
  1998. if(src.getFormat() != dst.getFormat())
  1999. {
  2000. LOGERR("Source format is different from destination format for copy(). This operation cannot be used for "
  2001. "a format conversion. Aborting copy.");
  2002. return;
  2003. }
  2004. UINT32 right = offsetX + dst.getWidth();
  2005. UINT32 bottom = offsetY + dst.getHeight();
  2006. UINT32 back = offsetZ + dst.getDepth();
  2007. if(right > src.getWidth() || bottom > src.getHeight() || back > src.getDepth())
  2008. {
  2009. LOGERR("Provided offset or destination size is too large and is referencing pixels that are out of bounds"
  2010. " on the source texture. Aborting copy().");
  2011. return;
  2012. }
  2013. UINT8* srcPtr = (UINT8*)src.getData() + offsetZ * src.getSlicePitch();
  2014. UINT8* dstPtr = (UINT8*)dst.getData();
  2015. UINT32 elemSize = getNumElemBytes(dst.getFormat());
  2016. UINT32 rowSize = dst.getWidth() * elemSize;
  2017. for(UINT32 z = 0; z < dst.getDepth(); z++)
  2018. {
  2019. UINT8* srcRowPtr = srcPtr + offsetY * src.getRowPitch() * elemSize;
  2020. UINT8* dstRowPtr = dstPtr;
  2021. for(UINT32 y = 0; y < dst.getHeight(); y++)
  2022. {
  2023. memcpy(dstRowPtr, srcRowPtr + offsetX * elemSize, rowSize);
  2024. srcRowPtr += src.getRowPitch() * elemSize;
  2025. dstRowPtr += dst.getRowPitch() * elemSize;
  2026. }
  2027. srcPtr += src.getSlicePitch() * elemSize;
  2028. dstPtr += dst.getSlicePitch() * elemSize;
  2029. }
  2030. }
  2031. void PixelUtil::mirror(PixelData& pixelData, MirrorMode mode)
  2032. {
  2033. UINT32 width = pixelData.getWidth();
  2034. UINT32 height = pixelData.getHeight();
  2035. UINT32 depth = pixelData.getDepth();
  2036. UINT32 elemSize = getNumElemBytes(pixelData.getFormat());
  2037. if (mode.isSet(MirrorModeBits::Z))
  2038. {
  2039. UINT32 sliceSize = width * height * elemSize;
  2040. UINT8* sliceTemp = bs_stack_alloc<UINT8>(sliceSize);
  2041. UINT8* dataPtr = pixelData.getData();
  2042. UINT32 halfDepth = depth / 2;
  2043. for (UINT32 z = 0; z < halfDepth; z++)
  2044. {
  2045. UINT32 srcZ = z * sliceSize;
  2046. UINT32 dstZ = (depth - z - 1) * sliceSize;
  2047. memcpy(sliceTemp, &dataPtr[dstZ], sliceSize);
  2048. memcpy(&dataPtr[srcZ], &dataPtr[srcZ], sliceSize);
  2049. memcpy(&dataPtr[dstZ], sliceTemp, sliceSize);
  2050. }
  2051. // Note: If flipping Y or X as well I could do it here without an extra set of memcpys
  2052. bs_stack_free(sliceTemp);
  2053. }
  2054. if(mode.isSet(MirrorModeBits::Y))
  2055. {
  2056. UINT32 rowSize = width * elemSize;
  2057. UINT8* rowTemp = bs_stack_alloc<UINT8>(rowSize);
  2058. UINT8* slicePtr = pixelData.getData();
  2059. for (UINT32 z = 0; z < depth; z++)
  2060. {
  2061. UINT32 halfHeight = height / 2;
  2062. for (UINT32 y = 0; y < halfHeight; y++)
  2063. {
  2064. UINT32 srcY = y * rowSize;
  2065. UINT32 dstY = (height - y - 1) * rowSize;
  2066. memcpy(rowTemp, &slicePtr[dstY], rowSize);
  2067. memcpy(&slicePtr[dstY], &slicePtr[srcY], rowSize);
  2068. memcpy(&slicePtr[srcY], rowTemp, rowSize);
  2069. }
  2070. // Note: If flipping X as well I could do it here without an extra set of memcpys
  2071. slicePtr += pixelData.getSlicePitch() * elemSize;
  2072. }
  2073. bs_stack_free(rowTemp);
  2074. }
  2075. if (mode.isSet(MirrorModeBits::X))
  2076. {
  2077. UINT8* elemTemp = bs_stack_alloc<UINT8>(elemSize);
  2078. UINT8* slicePtr = pixelData.getData();
  2079. for (UINT32 z = 0; z < depth; z++)
  2080. {
  2081. UINT8* rowPtr = slicePtr;
  2082. for (UINT32 y = 0; y < height; y++)
  2083. {
  2084. UINT32 halfWidth = width / 2;
  2085. for (UINT32 x = 0; x < halfWidth; x++)
  2086. {
  2087. UINT32 srcX = x * elemSize;
  2088. UINT32 dstX = (width - x - 1) * elemSize;
  2089. memcpy(elemTemp, &rowPtr[dstX], elemSize);
  2090. memcpy(&rowPtr[dstX], &rowPtr[srcX], elemSize);
  2091. memcpy(&rowPtr[srcX], elemTemp, elemSize);
  2092. }
  2093. rowPtr += pixelData.getRowPitch() * elemSize;
  2094. }
  2095. slicePtr += pixelData.getSlicePitch() * elemSize;
  2096. }
  2097. bs_stack_free(elemTemp);
  2098. }
  2099. }
  2100. void PixelUtil::applyGamma(UINT8* buffer, float gamma, UINT32 size, UINT8 bpp)
  2101. {
  2102. if(gamma == 1.0f)
  2103. return;
  2104. UINT32 stride = bpp >> 3;
  2105. for(size_t i = 0, j = size / stride; i < j; i++, buffer += stride)
  2106. {
  2107. float r = (float)buffer[0];
  2108. float g = (float)buffer[1];
  2109. float b = (float)buffer[2];
  2110. r = r * gamma;
  2111. g = g * gamma;
  2112. b = b * gamma;
  2113. float scale = 1.0f;
  2114. float tmp = 0.0f;
  2115. if(r > 255.0f && (tmp=(255.0f/r)) < scale)
  2116. scale = tmp;
  2117. if(g > 255.0f && (tmp=(255.0f/g)) < scale)
  2118. scale = tmp;
  2119. if(b > 255.0f && (tmp=(255.0f/b)) < scale)
  2120. scale = tmp;
  2121. r *= scale;
  2122. g *= scale;
  2123. b *= scale;
  2124. buffer[0] = (UINT8)r;
  2125. buffer[1] = (UINT8)g;
  2126. buffer[2] = (UINT8)b;
  2127. }
  2128. }
  2129. void PixelUtil::compress(const PixelData& src, PixelData& dst, const CompressionOptions& options)
  2130. {
  2131. if (!isCompressed(options.format))
  2132. {
  2133. LOGERR("Compression failed. Destination format is not a valid compressed format.")
  2134. return;
  2135. }
  2136. if (src.getDepth() != 1)
  2137. {
  2138. LOGERR("Compression failed. 3D texture compression not supported.")
  2139. return;
  2140. }
  2141. if (isCompressed(src.getFormat()))
  2142. {
  2143. LOGERR("Compression failed. Source data cannot be compressed.");
  2144. return;
  2145. }
  2146. PixelFormat interimFormat = options.format == PF_BC6H ? PF_RGBA32F : PF_BGRA8;
  2147. PixelData interimData(src.getWidth(), src.getHeight(), 1, interimFormat);
  2148. interimData.allocateInternalBuffer();
  2149. bulkPixelConversion(src, interimData);
  2150. if(interimFormat != PF_RGBA32F)
  2151. flipComponentOrder(interimData);
  2152. nvtt::InputOptions io;
  2153. io.setTextureLayout(nvtt::TextureType_2D, src.getWidth(), src.getHeight());
  2154. io.setMipmapGeneration(false);
  2155. io.setAlphaMode(toNVTTAlphaMode(options.alphaMode));
  2156. io.setNormalMap(options.isNormalMap);
  2157. if (interimFormat == PF_RGBA32F)
  2158. io.setFormat(nvtt::InputFormat_RGBA_32F);
  2159. else
  2160. io.setFormat(nvtt::InputFormat_BGRA_8UB);
  2161. if (options.isSRGB)
  2162. io.setGamma(2.2f, 2.2f);
  2163. else
  2164. io.setGamma(1.0f, 1.0f);
  2165. io.setMipmapData(interimData.getData(), src.getWidth(), src.getHeight());
  2166. nvtt::CompressionOptions co;
  2167. co.setFormat(toNVTTFormat(options.format));
  2168. co.setQuality(toNVTTQuality(options.quality));
  2169. NVTTCompressOutputHandler outputHandler(dst.getData(), dst.getConsecutiveSize());
  2170. nvtt::OutputOptions oo;
  2171. oo.setOutputHeader(false);
  2172. oo.setOutputHandler(&outputHandler);
  2173. nvtt::Compressor compressor;
  2174. if (!compressor.process(io, co, oo))
  2175. {
  2176. LOGERR("Compression failed. Internal error.");
  2177. return;
  2178. }
  2179. }
  2180. Vector<SPtr<PixelData>> PixelUtil::genMipmaps(const PixelData& src, const MipMapGenOptions& options)
  2181. {
  2182. Vector<SPtr<PixelData>> outputMipBuffers;
  2183. if (src.getDepth() != 1)
  2184. {
  2185. LOGERR("Mipmap generation failed. 3D texture formats not supported.")
  2186. return outputMipBuffers;
  2187. }
  2188. if (isCompressed(src.getFormat()))
  2189. {
  2190. LOGERR("Mipmap generation failed. Source data cannot be compressed.")
  2191. return outputMipBuffers;
  2192. }
  2193. if (!Bitwise::isPow2(src.getWidth()) || !Bitwise::isPow2(src.getHeight()))
  2194. {
  2195. LOGERR("Mipmap generation failed. Texture width & height must be powers of 2.");
  2196. return outputMipBuffers;
  2197. }
  2198. PixelFormat interimFormat = isFloatingPoint(src.getFormat()) ? PF_RGBA32F : PF_BGRA8;
  2199. PixelData interimData(src.getWidth(), src.getHeight(), 1, interimFormat);
  2200. interimData.allocateInternalBuffer();
  2201. bulkPixelConversion(src, interimData);
  2202. if (interimFormat != PF_RGBA32F)
  2203. flipComponentOrder(interimData);
  2204. nvtt::InputOptions io;
  2205. io.setTextureLayout(nvtt::TextureType_2D, src.getWidth(), src.getHeight());
  2206. io.setMipmapGeneration(true);
  2207. io.setNormalMap(options.isNormalMap);
  2208. io.setNormalizeMipmaps(options.normalizeMipmaps);
  2209. io.setWrapMode(toNVTTWrapMode(options.wrapMode));
  2210. if (interimFormat == PF_RGBA32F)
  2211. io.setFormat(nvtt::InputFormat_RGBA_32F);
  2212. else
  2213. io.setFormat(nvtt::InputFormat_BGRA_8UB);
  2214. if (options.isSRGB)
  2215. io.setGamma(2.2f, 2.2f);
  2216. else
  2217. io.setGamma(1.0f, 1.0f);
  2218. io.setMipmapData(interimData.getData(), src.getWidth(), src.getHeight());
  2219. nvtt::CompressionOptions co;
  2220. co.setFormat(nvtt::Format_RGBA);
  2221. if (interimFormat == PF_RGBA32F)
  2222. {
  2223. co.setPixelType(nvtt::PixelType_Float);
  2224. co.setPixelFormat(32, 32, 32, 32);
  2225. }
  2226. else
  2227. {
  2228. co.setPixelType(nvtt::PixelType_UnsignedNorm);
  2229. co.setPixelFormat(32, 0x0000FF00, 0x00FF0000, 0xFF000000, 0x000000FF);
  2230. }
  2231. UINT32 numMips = getMaxMipmaps(src.getWidth(), src.getHeight(), 1, src.getFormat());
  2232. Vector<SPtr<PixelData>> rgbaMipBuffers;
  2233. // Note: This can be done more effectively without creating so many temp buffers
  2234. // and working with the original formats directly, but it would complicate the code
  2235. // too much at the moment.
  2236. UINT32 curWidth = src.getWidth();
  2237. UINT32 curHeight = src.getHeight();
  2238. for (UINT32 i = 0; i < numMips; i++)
  2239. {
  2240. rgbaMipBuffers.push_back(bs_shared_ptr_new<PixelData>(curWidth, curHeight, 1, interimFormat));
  2241. rgbaMipBuffers.back()->allocateInternalBuffer();
  2242. if (curWidth > 1)
  2243. curWidth = curWidth / 2;
  2244. if (curHeight > 1)
  2245. curHeight = curHeight / 2;
  2246. }
  2247. rgbaMipBuffers.push_back(bs_shared_ptr_new<PixelData>(curWidth, curHeight, 1, interimFormat));
  2248. rgbaMipBuffers.back()->allocateInternalBuffer();
  2249. NVTTMipmapOutputHandler outputHandler(rgbaMipBuffers);
  2250. nvtt::OutputOptions oo;
  2251. oo.setOutputHeader(false);
  2252. oo.setOutputHandler(&outputHandler);
  2253. nvtt::Compressor compressor;
  2254. if (!compressor.process(io, co, oo))
  2255. {
  2256. LOGERR("Mipmap generation failed. Internal error.");
  2257. return outputMipBuffers;
  2258. }
  2259. interimData.freeInternalBuffer();
  2260. for (UINT32 i = 0; i < (UINT32)rgbaMipBuffers.size(); i++)
  2261. {
  2262. SPtr<PixelData> argbBuffer = rgbaMipBuffers[i];
  2263. SPtr<PixelData> outputBuffer = bs_shared_ptr_new<PixelData>(argbBuffer->getWidth(), argbBuffer->getHeight(), 1, src.getFormat());
  2264. outputBuffer->allocateInternalBuffer();
  2265. bulkPixelConversion(*argbBuffer, *outputBuffer);
  2266. argbBuffer->freeInternalBuffer();
  2267. outputMipBuffers.push_back(outputBuffer);
  2268. }
  2269. return outputMipBuffers;
  2270. }
  2271. }