BsPixelUtil.cpp 65 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145
  1. //********************************** Banshee Engine (www.banshee3d.com) **************************************************//
  2. //**************** Copyright (c) 2016 Marko Pintera ([email protected]). All rights reserved. **********************//
  3. #include "BsPixelUtil.h"
  4. #include "BsBitwise.h"
  5. #include "BsColor.h"
  6. #include "BsMath.h"
  7. #include "BsException.h"
  8. #include "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, 0, 0, 0, 0
  306. },
  307. //-----------------------------------------------------------------------
  308. {"PF_R8",
  309. /* Bytes per element */
  310. 1,
  311. /* Flags */
  312. 0,
  313. /* Component type and count */
  314. PCT_BYTE, 1,
  315. /* rbits, gbits, bbits, abits */
  316. 8, 0, 0, 0,
  317. /* Masks and shifts */
  318. 0x000000FF, 0, 0, 0,
  319. 0, 0, 0, 0
  320. },
  321. //-----------------------------------------------------------------------
  322. {"PF_R8G8",
  323. /* Bytes per element */
  324. 2,
  325. /* Flags */
  326. 0,
  327. /* Component type and count */
  328. PCT_BYTE, 2,
  329. /* rbits, gbits, bbits, abits */
  330. 8, 8, 0, 0,
  331. /* Masks and shifts */
  332. 0x000000FF, 0x0000FF00, 0, 0,
  333. 0, 8, 0, 0
  334. },
  335. //-----------------------------------------------------------------------
  336. {"PF_R8G8B8",
  337. /* Bytes per element */
  338. 3, // 24 bit integer -- special
  339. /* Flags */
  340. PFF_NATIVEENDIAN,
  341. /* Component type and count */
  342. PCT_BYTE, 3,
  343. /* rbits, gbits, bbits, abits */
  344. 8, 8, 8, 0,
  345. /* Masks and shifts */
  346. 0x000000FF, 0x0000FF00, 0x00FF0000, 0,
  347. 0, 8, 16, 0
  348. },
  349. //-----------------------------------------------------------------------
  350. {"PF_B8G8R8",
  351. /* Bytes per element */
  352. 3, // 24 bit integer -- special
  353. /* Flags */
  354. PFF_NATIVEENDIAN,
  355. /* Component type and count */
  356. PCT_BYTE, 3,
  357. /* rbits, gbits, bbits, abits */
  358. 8, 8, 8, 0,
  359. /* Masks and shifts */
  360. 0x00FF0000, 0x0000FF00, 0x000000FF, 0,
  361. 16, 8, 0, 0
  362. },
  363. //-----------------------------------------------------------------------
  364. {}, // Deleted format
  365. //-----------------------------------------------------------------------
  366. {}, // Deleted format
  367. //-----------------------------------------------------------------------
  368. {"PF_B8G8R8A8",
  369. /* Bytes per element */
  370. 4,
  371. /* Flags */
  372. PFF_HASALPHA | PFF_NATIVEENDIAN,
  373. /* Component type and count */
  374. PCT_BYTE, 4,
  375. /* rbits, gbits, bbits, abits */
  376. 8, 8, 8, 8,
  377. /* Masks and shifts */
  378. 0x00FF0000, 0x0000FF00, 0x000000FF, 0xFF000000,
  379. 16, 8, 0, 24
  380. },
  381. //-----------------------------------------------------------------------
  382. {"PF_R8G8B8A8",
  383. /* Bytes per element */
  384. 4,
  385. /* Flags */
  386. PFF_HASALPHA | PFF_NATIVEENDIAN,
  387. /* Component type and count */
  388. PCT_BYTE, 4,
  389. /* rbits, gbits, bbits, abits */
  390. 8, 8, 8, 8,
  391. /* Masks and shifts */
  392. 0x000000FF, 0x0000FF00, 0x00FF0000, 0xFF000000,
  393. 0, 8, 16, 24
  394. },
  395. //-----------------------------------------------------------------------
  396. {}, // Deleted format
  397. //-----------------------------------------------------------------------
  398. {}, // Deleted format
  399. //-----------------------------------------------------------------------
  400. {}, // Deleted format
  401. //-----------------------------------------------------------------------
  402. {}, // Deleted format
  403. //-----------------------------------------------------------------------
  404. {"PF_BC1",
  405. /* Bytes per element */
  406. 0,
  407. /* Flags */
  408. PFF_COMPRESSED | PFF_HASALPHA,
  409. /* Component type and count */
  410. PCT_BYTE, 3, // No alpha
  411. /* rbits, gbits, bbits, abits */
  412. 0, 0, 0, 0,
  413. /* Masks and shifts */
  414. 0, 0, 0, 0, 0, 0, 0, 0
  415. },
  416. //-----------------------------------------------------------------------
  417. {"PF_BC1a",
  418. /* Bytes per element */
  419. 0,
  420. /* Flags */
  421. PFF_COMPRESSED,
  422. /* Component type and count */
  423. PCT_BYTE, 3,
  424. /* rbits, gbits, bbits, abits */
  425. 0, 0, 0, 0,
  426. /* Masks and shifts */
  427. 0, 0, 0, 0, 0, 0, 0, 0
  428. },
  429. //-----------------------------------------------------------------------
  430. {"PF_BC2",
  431. /* Bytes per element */
  432. 0,
  433. /* Flags */
  434. PFF_COMPRESSED | PFF_HASALPHA,
  435. /* Component type and count */
  436. PCT_BYTE, 4,
  437. /* rbits, gbits, bbits, abits */
  438. 0, 0, 0, 0,
  439. /* Masks and shifts */
  440. 0, 0, 0, 0, 0, 0, 0, 0
  441. },
  442. //-----------------------------------------------------------------------
  443. {"PF_BC3",
  444. /* Bytes per element */
  445. 0,
  446. /* Flags */
  447. PFF_COMPRESSED | PFF_HASALPHA,
  448. /* Component type and count */
  449. PCT_BYTE, 4,
  450. /* rbits, gbits, bbits, abits */
  451. 0, 0, 0, 0,
  452. /* Masks and shifts */
  453. 0, 0, 0, 0, 0, 0, 0, 0
  454. },
  455. //-----------------------------------------------------------------------
  456. {"PF_BC4",
  457. /* Bytes per element */
  458. 0,
  459. /* Flags */
  460. PFF_COMPRESSED,
  461. /* Component type and count */
  462. PCT_BYTE, 1,
  463. /* rbits, gbits, bbits, abits */
  464. 0, 0, 0, 0,
  465. /* Masks and shifts */
  466. 0, 0, 0, 0, 0, 0, 0, 0
  467. },
  468. //-----------------------------------------------------------------------
  469. {"PF_BC5",
  470. /* Bytes per element */
  471. 0,
  472. /* Flags */
  473. PFF_COMPRESSED,
  474. /* Component type and count */
  475. PCT_BYTE, 2,
  476. /* rbits, gbits, bbits, abits */
  477. 0, 0, 0, 0,
  478. /* Masks and shifts */
  479. 0, 0, 0, 0, 0, 0, 0, 0
  480. },
  481. //-----------------------------------------------------------------------
  482. {"PF_BC6H",
  483. /* Bytes per element */
  484. 0,
  485. /* Flags */
  486. PFF_COMPRESSED,
  487. /* Component type and count */
  488. PCT_FLOAT16, 3,
  489. /* rbits, gbits, bbits, abits */
  490. 0, 0, 0, 0,
  491. /* Masks and shifts */
  492. 0, 0, 0, 0, 0, 0, 0, 0
  493. },
  494. //-----------------------------------------------------------------------
  495. {"PF_BC7",
  496. /* Bytes per element */
  497. 0,
  498. /* Flags */
  499. PFF_COMPRESSED | PFF_HASALPHA,
  500. /* Component type and count */
  501. PCT_BYTE, 4,
  502. /* rbits, gbits, bbits, abits */
  503. 0, 0, 0, 0,
  504. /* Masks and shifts */
  505. 0, 0, 0, 0, 0, 0, 0, 0
  506. },
  507. //-----------------------------------------------------------------------
  508. {"PF_FLOAT16_R",
  509. /* Bytes per element */
  510. 2,
  511. /* Flags */
  512. PFF_FLOAT,
  513. /* Component type and count */
  514. PCT_FLOAT16, 1,
  515. /* rbits, gbits, bbits, abits */
  516. 16, 0, 0, 0,
  517. /* Masks and shifts */
  518. 0, 0, 0, 0, 0, 0, 0, 0
  519. },
  520. //-----------------------------------------------------------------------
  521. {"PF_FLOAT16_RG",
  522. /* Bytes per element */
  523. 4,
  524. /* Flags */
  525. PFF_FLOAT,
  526. /* Component type and count */
  527. PCT_FLOAT16, 2,
  528. /* rbits, gbits, bbits, abits */
  529. 16, 16, 0, 0,
  530. /* Masks and shifts */
  531. 0, 0, 0, 0, 0, 0, 0, 0
  532. },
  533. //-----------------------------------------------------------------------
  534. { "PF_FLOAT16_RGB",
  535. /* Bytes per element */
  536. 6,
  537. /* Flags */
  538. PFF_FLOAT,
  539. /* Component type and count */
  540. PCT_FLOAT16, 3,
  541. /* rbits, gbits, bbits, abits */
  542. 16, 16, 16, 0,
  543. /* Masks and shifts */
  544. 0, 0, 0, 0, 0, 0, 0, 0
  545. },
  546. //-----------------------------------------------------------------------
  547. { "PF_FLOAT16_RGBA",
  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. 0, 0, 0, 0, 0, 0, 0, 0
  558. },
  559. //-----------------------------------------------------------------------
  560. {"PF_FLOAT32_R",
  561. /* Bytes per element */
  562. 4,
  563. /* Flags */
  564. PFF_FLOAT,
  565. /* Component type and count */
  566. PCT_FLOAT32, 1,
  567. /* rbits, gbits, bbits, abits */
  568. 32, 0, 0, 0,
  569. /* Masks and shifts */
  570. 0, 0, 0, 0, 0, 0, 0, 0
  571. },
  572. //-----------------------------------------------------------------------
  573. {"PF_FLOAT32_RG",
  574. /* Bytes per element */
  575. 8,
  576. /* Flags */
  577. PFF_FLOAT,
  578. /* Component type and count */
  579. PCT_FLOAT32, 2,
  580. /* rbits, gbits, bbits, abits */
  581. 32, 32, 0, 0,
  582. /* Masks and shifts */
  583. 0, 0, 0, 0, 0, 0, 0, 0
  584. },
  585. //-----------------------------------------------------------------------
  586. { "PF_FLOAT32_RGB",
  587. /* Bytes per element */
  588. 12,
  589. /* Flags */
  590. PFF_FLOAT,
  591. /* Component type and count */
  592. PCT_FLOAT32, 3,
  593. /* rbits, gbits, bbits, abits */
  594. 32, 32, 32, 0,
  595. /* Masks and shifts */
  596. 0, 0, 0, 0, 0, 0, 0, 0
  597. },
  598. //-----------------------------------------------------------------------
  599. { "PF_FLOAT32_RGBA",
  600. /* Bytes per element */
  601. 16,
  602. /* Flags */
  603. PFF_FLOAT | PFF_HASALPHA,
  604. /* Component type and count */
  605. PCT_FLOAT32, 4,
  606. /* rbits, gbits, bbits, abits */
  607. 32, 32, 32, 32,
  608. /* Masks and shifts */
  609. 0, 0, 0, 0, 0, 0, 0, 0
  610. },
  611. //-----------------------------------------------------------------------
  612. {"PF_D32_S8X24",
  613. /* Bytes per element */
  614. 8,
  615. /* Flags */
  616. PFF_DEPTH | PFF_FLOAT,
  617. /* Component type and count */
  618. PCT_FLOAT32, 2,
  619. /* rbits, gbits, bbits, abits */
  620. 0, 0, 0, 0,
  621. /* Masks and shifts */
  622. 0, 0, 0, 0, 0, 0, 0, 0
  623. },
  624. //-----------------------------------------------------------------------
  625. {"PF_D24_S8",
  626. /* Bytes per element */
  627. 4,
  628. /* Flags */
  629. PFF_DEPTH | PFF_FLOAT,
  630. /* Component type and count */
  631. PCT_FLOAT32, 1,
  632. /* rbits, gbits, bbits, abits */
  633. 0, 0, 0, 0,
  634. /* Masks and shifts */
  635. 0, 0, 0, 0, 0, 0, 0, 0
  636. },
  637. //-----------------------------------------------------------------------
  638. {"PF_D32",
  639. /* Bytes per element */
  640. 4,
  641. /* Flags */
  642. PFF_DEPTH | PFF_FLOAT,
  643. /* Component type and count */
  644. PCT_FLOAT32, 1,
  645. /* rbits, gbits, bbits, abits */
  646. 0, 0, 0, 0,
  647. /* Masks and shifts */
  648. 0, 0, 0, 0, 0, 0, 0, 0
  649. },
  650. //-----------------------------------------------------------------------
  651. {"PF_D16",
  652. /* Bytes per element */
  653. 2,
  654. /* Flags */
  655. PFF_DEPTH | PFF_FLOAT,
  656. /* Component type and count */
  657. PCT_FLOAT16, 1,
  658. /* rbits, gbits, bbits, abits */
  659. 0, 0, 0, 0,
  660. /* Masks and shifts */
  661. 0, 0, 0, 0, 0, 0, 0, 0
  662. },
  663. //-----------------------------------------------------------------------
  664. { "PF_FLOAT_R11G11B10",
  665. /* Bytes per element */
  666. 4,
  667. /* Flags */
  668. PFF_FLOAT,
  669. /* Component type and count */
  670. PCT_PACKED_R11G11B10, 1,
  671. /* rbits, gbits, bbits, abits */
  672. 11, 11, 10, 0,
  673. /* Masks and shifts */
  674. 0x000007FF, 0x003FF800, 0xFFC00000, 0,
  675. 0, 11, 22, 0
  676. },
  677. //-----------------------------------------------------------------------
  678. { "PF_UNORM_R10G10B10A2",
  679. /* Bytes per element */
  680. 4,
  681. /* Flags */
  682. PFF_FLOAT | PFF_HASALPHA,
  683. /* Component type and count */
  684. PCT_PACKED_R10G10B10A2, 1,
  685. /* rbits, gbits, bbits, abits */
  686. 10, 10, 10, 2,
  687. /* Masks and shifts */
  688. 0x000003FF, 0x000FFC00, 0x3FF00000, 0xC0000000,
  689. 0, 10, 20, 30
  690. },
  691. };
  692. static inline const PixelFormatDescription &getDescriptionFor(const PixelFormat fmt)
  693. {
  694. const int ord = (int)fmt;
  695. assert(ord >= 0 && ord < PF_COUNT);
  696. return _pixelFormats[ord];
  697. }
  698. /** Handles compression output from NVTT library for a single image. */
  699. struct NVTTCompressOutputHandler : public nvtt::OutputHandler
  700. {
  701. NVTTCompressOutputHandler(UINT8* buffer, UINT32 sizeBytes)
  702. :buffer(buffer), bufferWritePos(buffer), bufferEnd(buffer + sizeBytes)
  703. { }
  704. void beginImage(int size, int width, int height, int depth, int face, int miplevel) override
  705. { }
  706. bool writeData(const void* data, int size) override
  707. {
  708. assert((bufferWritePos + size) <= bufferEnd);
  709. memcpy(bufferWritePos, data, size);
  710. bufferWritePos += size;
  711. return true;
  712. }
  713. void endImage() override
  714. { }
  715. UINT8* buffer;
  716. UINT8* bufferWritePos;
  717. UINT8* bufferEnd;
  718. };
  719. /** Handles output from NVTT library for a mip-map chain. */
  720. struct NVTTMipmapOutputHandler : public nvtt::OutputHandler
  721. {
  722. NVTTMipmapOutputHandler(const Vector<SPtr<PixelData>>& buffers)
  723. :buffers(buffers), bufferWritePos(nullptr), bufferEnd(nullptr)
  724. { }
  725. virtual void beginImage(int size, int width, int height, int depth, int face, int miplevel)
  726. {
  727. assert(miplevel >= 0 && miplevel < (int)buffers.size());
  728. assert(size == buffers[miplevel]->getConsecutiveSize());
  729. activeBuffer = buffers[miplevel];
  730. bufferWritePos = activeBuffer->getData();
  731. bufferEnd = bufferWritePos + activeBuffer->getConsecutiveSize();
  732. }
  733. virtual bool writeData(const void* data, int size)
  734. {
  735. assert((bufferWritePos + size) <= bufferEnd);
  736. memcpy(bufferWritePos, data, size);
  737. bufferWritePos += size;
  738. return true;
  739. }
  740. void endImage() override
  741. { }
  742. Vector<SPtr<PixelData>> buffers;
  743. SPtr<PixelData> activeBuffer;
  744. UINT8* bufferWritePos;
  745. UINT8* bufferEnd;
  746. };
  747. nvtt::Format toNVTTFormat(PixelFormat format)
  748. {
  749. switch (format)
  750. {
  751. case PF_BC1:
  752. return nvtt::Format_BC1;
  753. case PF_BC1a:
  754. return nvtt::Format_BC1a;
  755. case PF_BC2:
  756. return nvtt::Format_BC2;
  757. case PF_BC3:
  758. return nvtt::Format_BC3;
  759. case PF_BC4:
  760. return nvtt::Format_BC4;
  761. case PF_BC5:
  762. return nvtt::Format_BC5;
  763. case PF_BC6H:
  764. return nvtt::Format_BC6;
  765. case PF_BC7:
  766. return nvtt::Format_BC7;
  767. default: // Unsupported format
  768. return nvtt::Format_BC3;
  769. }
  770. }
  771. nvtt::Quality toNVTTQuality(CompressionQuality quality)
  772. {
  773. switch (quality)
  774. {
  775. case CompressionQuality::Fastest:
  776. return nvtt::Quality_Fastest;
  777. case CompressionQuality::Highest:
  778. return nvtt::Quality_Highest;
  779. case CompressionQuality::Normal:
  780. return nvtt::Quality_Normal;
  781. case CompressionQuality::Production:
  782. return nvtt::Quality_Normal;
  783. }
  784. // Unknown quality level
  785. return nvtt::Quality_Normal;
  786. }
  787. nvtt::AlphaMode toNVTTAlphaMode(AlphaMode alphaMode)
  788. {
  789. switch (alphaMode)
  790. {
  791. case AlphaMode::None:
  792. return nvtt::AlphaMode_None;
  793. case AlphaMode::Premultiplied:
  794. return nvtt::AlphaMode_Premultiplied;
  795. case AlphaMode::Transparency:
  796. return nvtt::AlphaMode_Transparency;
  797. }
  798. // Unknown alpha mode
  799. return nvtt::AlphaMode_None;
  800. }
  801. nvtt::WrapMode toNVTTWrapMode(MipMapWrapMode wrapMode)
  802. {
  803. switch (wrapMode)
  804. {
  805. case MipMapWrapMode::Clamp:
  806. return nvtt::WrapMode_Clamp;
  807. case MipMapWrapMode::Mirror:
  808. return nvtt::WrapMode_Mirror;
  809. case MipMapWrapMode::Repeat:
  810. return nvtt::WrapMode_Repeat;
  811. }
  812. // Unknown alpha mode
  813. return nvtt::WrapMode_Mirror;
  814. }
  815. UINT32 PixelUtil::getNumElemBytes(PixelFormat format)
  816. {
  817. return getDescriptionFor(format).elemBytes;
  818. }
  819. UINT32 PixelUtil::getMemorySize(UINT32 width, UINT32 height, UINT32 depth, PixelFormat format)
  820. {
  821. if(isCompressed(format))
  822. {
  823. switch(format)
  824. {
  825. // BC formats work by dividing the image into 4x4 blocks, then encoding each
  826. // 4x4 block with a certain number of bytes.
  827. case PF_BC1:
  828. case PF_BC1a:
  829. case PF_BC4:
  830. return ((width+3)/4)*((height+3)/4)*8 * depth;
  831. case PF_BC2:
  832. case PF_BC3:
  833. case PF_BC5:
  834. case PF_BC6H:
  835. case PF_BC7:
  836. return ((width+3)/4)*((height+3)/4)*16 * depth;
  837. default:
  838. BS_EXCEPT(InvalidParametersException, "Invalid compressed pixel format");
  839. return 0;
  840. }
  841. }
  842. return width*height*depth*getNumElemBytes(format);
  843. }
  844. void PixelUtil::getPitch(UINT32 width, UINT32 height, UINT32 depth, PixelFormat format,
  845. UINT32& rowPitch, UINT32& depthPitch)
  846. {
  847. if (isCompressed(format))
  848. {
  849. switch (format)
  850. {
  851. // BC formats work by dividing the image into 4x4 blocks, then encoding each
  852. // 4x4 block with a certain number of bytes.
  853. case PF_BC1:
  854. case PF_BC1a:
  855. case PF_BC4:
  856. case PF_BC2:
  857. case PF_BC3:
  858. case PF_BC5:
  859. case PF_BC6H:
  860. case PF_BC7:
  861. rowPitch = div(width + 3, 4).quot * 4;
  862. depthPitch = div(height + 3, 4).quot * 4 * rowPitch;
  863. return;
  864. default:
  865. BS_EXCEPT(InvalidParametersException, "Invalid compressed pixel format");
  866. return;
  867. }
  868. }
  869. rowPitch = width;
  870. depthPitch = width * height;
  871. }
  872. void PixelUtil::getSizeForMipLevel(UINT32 width, UINT32 height, UINT32 depth, UINT32 mipLevel,
  873. UINT32& mipWidth, UINT32& mipHeight, UINT32& mipDepth)
  874. {
  875. mipWidth = width;
  876. mipHeight = height;
  877. mipDepth = depth;
  878. for (UINT32 i = 0; i < mipLevel; i++)
  879. {
  880. if (mipWidth != 1) mipWidth /= 2;
  881. if (mipHeight != 1) mipHeight /= 2;
  882. if (mipDepth != 1) mipDepth /= 2;
  883. }
  884. }
  885. UINT32 PixelUtil::getNumElemBits(PixelFormat format)
  886. {
  887. return getDescriptionFor(format).elemBytes * 8;
  888. }
  889. UINT32 PixelUtil::getFlags(PixelFormat format)
  890. {
  891. return getDescriptionFor(format).flags;
  892. }
  893. bool PixelUtil::hasAlpha(PixelFormat format)
  894. {
  895. return (PixelUtil::getFlags(format) & PFF_HASALPHA) > 0;
  896. }
  897. bool PixelUtil::isFloatingPoint(PixelFormat format)
  898. {
  899. return (PixelUtil::getFlags(format) & PFF_FLOAT) > 0;
  900. }
  901. bool PixelUtil::isCompressed(PixelFormat format)
  902. {
  903. return (PixelUtil::getFlags(format) & PFF_COMPRESSED) > 0;
  904. }
  905. bool PixelUtil::isDepth(PixelFormat format)
  906. {
  907. return (PixelUtil::getFlags(format) & PFF_DEPTH) > 0;
  908. }
  909. bool PixelUtil::isNativeEndian(PixelFormat format)
  910. {
  911. return (PixelUtil::getFlags(format) & PFF_NATIVEENDIAN) > 0;
  912. }
  913. bool PixelUtil::checkFormat(PixelFormat& format, TextureType texType, int usage)
  914. {
  915. // First check just the usage since it's the most limiting factor
  916. //// Depth-stencil only supports depth formats
  917. if ((usage & TU_DEPTHSTENCIL) != 0)
  918. {
  919. if (isDepth(format))
  920. return true;
  921. format = PF_D32_S8X24;
  922. return false;
  923. }
  924. //// Render targets support everything but compressed & depth-stencil formats
  925. if ((usage & TU_RENDERTARGET) != 0)
  926. {
  927. if (!isDepth(format) && !isCompressed(format))
  928. return true;
  929. format = PF_R8G8B8A8;
  930. return false;
  931. }
  932. //// Load-store textures support everything but compressed & depth-stencil formats
  933. if ((usage & TU_LOADSTORE) != 0)
  934. {
  935. if (!isDepth(format) && !isCompressed(format))
  936. return true;
  937. format = PF_R8G8B8A8;
  938. return false;
  939. }
  940. //// Sampled texture support depends on texture type
  941. switch (texType)
  942. {
  943. case TEX_TYPE_1D:
  944. {
  945. // 1D textures support anything but depth & compressed formats
  946. if (!isDepth(format) && !isCompressed(format))
  947. return true;
  948. format = PF_R8G8B8A8;
  949. return false;
  950. }
  951. case TEX_TYPE_3D:
  952. {
  953. // 3D textures support anything but depth & compressed formats
  954. if (!isDepth(format))
  955. return true;
  956. format = PF_R8G8B8A8;
  957. return false;
  958. }
  959. default: // 2D & cube
  960. {
  961. // 2D/cube textures support anything but depth formats
  962. if (!isDepth(format))
  963. return true;
  964. format = PF_R8G8B8A8;
  965. return false;
  966. }
  967. }
  968. }
  969. bool PixelUtil::isValidExtent(UINT32 width, UINT32 height, UINT32 depth, PixelFormat format)
  970. {
  971. if(isCompressed(format))
  972. {
  973. switch(format)
  974. {
  975. case PF_BC1:
  976. case PF_BC2:
  977. case PF_BC1a:
  978. case PF_BC3:
  979. case PF_BC4:
  980. case PF_BC5:
  981. case PF_BC6H:
  982. case PF_BC7:
  983. return ((width & 3) == 0 && (height & 3) == 0 && depth == 1);
  984. default:
  985. return true;
  986. }
  987. }
  988. else
  989. {
  990. return true;
  991. }
  992. }
  993. void PixelUtil::getBitDepths(PixelFormat format, int(&rgba)[4])
  994. {
  995. const PixelFormatDescription& des = getDescriptionFor(format);
  996. rgba[0] = des.rbits;
  997. rgba[1] = des.gbits;
  998. rgba[2] = des.bbits;
  999. rgba[3] = des.abits;
  1000. }
  1001. void PixelUtil::getBitMasks(PixelFormat format, UINT32(&rgba)[4])
  1002. {
  1003. const PixelFormatDescription& des = getDescriptionFor(format);
  1004. rgba[0] = des.rmask;
  1005. rgba[1] = des.gmask;
  1006. rgba[2] = des.bmask;
  1007. rgba[3] = des.amask;
  1008. }
  1009. void PixelUtil::getBitShifts(PixelFormat format, UINT8(&rgba)[4])
  1010. {
  1011. const PixelFormatDescription& des = getDescriptionFor(format);
  1012. rgba[0] = des.rshift;
  1013. rgba[1] = des.gshift;
  1014. rgba[2] = des.bshift;
  1015. rgba[3] = des.ashift;
  1016. }
  1017. String PixelUtil::getFormatName(PixelFormat srcformat)
  1018. {
  1019. return getDescriptionFor(srcformat).name;
  1020. }
  1021. bool PixelUtil::isAccessible(PixelFormat srcformat)
  1022. {
  1023. if (srcformat == PF_UNKNOWN)
  1024. return false;
  1025. UINT32 flags = getFlags(srcformat);
  1026. return !((flags & PFF_COMPRESSED) || (flags & PFF_DEPTH));
  1027. }
  1028. PixelComponentType PixelUtil::getElementType(PixelFormat format)
  1029. {
  1030. const PixelFormatDescription& des = getDescriptionFor(format);
  1031. return des.componentType;
  1032. }
  1033. UINT32 PixelUtil::getNumElements(PixelFormat format)
  1034. {
  1035. const PixelFormatDescription& des = getDescriptionFor(format);
  1036. return des.componentCount;
  1037. }
  1038. UINT32 PixelUtil::getMaxMipmaps(UINT32 width, UINT32 height, UINT32 depth, PixelFormat format)
  1039. {
  1040. UINT32 count = 0;
  1041. if ((width > 0) && (height > 0))
  1042. {
  1043. while (!(width == 1 && height == 1 && depth == 1))
  1044. {
  1045. if (width > 1) width = width / 2;
  1046. if (height > 1) height = height / 2;
  1047. if (depth > 1) depth = depth / 2;
  1048. count++;
  1049. }
  1050. }
  1051. return count;
  1052. }
  1053. void PixelUtil::packColor(const Color& color, PixelFormat format, void* dest)
  1054. {
  1055. packColor(color.r, color.g, color.b, color.a, format, dest);
  1056. }
  1057. void PixelUtil::packColor(UINT8 r, UINT8 g, UINT8 b, UINT8 a, PixelFormat format, void* dest)
  1058. {
  1059. const PixelFormatDescription &des = getDescriptionFor(format);
  1060. if (des.flags & PFF_NATIVEENDIAN)
  1061. {
  1062. // Shortcut for integer formats packing
  1063. UINT32 value = ((Bitwise::fixedToFixed(r, 8, des.rbits) << des.rshift) & des.rmask) |
  1064. ((Bitwise::fixedToFixed(g, 8, des.gbits) << des.gshift) & des.gmask) |
  1065. ((Bitwise::fixedToFixed(b, 8, des.bbits) << des.bshift) & des.bmask) |
  1066. ((Bitwise::fixedToFixed(a, 8, des.abits) << des.ashift) & des.amask);
  1067. // And write to memory
  1068. Bitwise::intWrite(dest, des.elemBytes, value);
  1069. }
  1070. else
  1071. {
  1072. // Convert to float
  1073. packColor((float)r / 255.0f, (float)g / 255.0f, (float)b / 255.0f, (float)a / 255.0f, format, dest);
  1074. }
  1075. }
  1076. void PixelUtil::packColor(float r, float g, float b, float a, const PixelFormat format, void* dest)
  1077. {
  1078. const PixelFormatDescription& des = getDescriptionFor(format);
  1079. if (des.flags & PFF_NATIVEENDIAN)
  1080. {
  1081. // Do the packing
  1082. const unsigned int value = ((Bitwise::floatToFixed(r, des.rbits) << des.rshift) & des.rmask) |
  1083. ((Bitwise::floatToFixed(g, des.gbits) << des.gshift) & des.gmask) |
  1084. ((Bitwise::floatToFixed(b, des.bbits) << des.bshift) & des.bmask) |
  1085. ((Bitwise::floatToFixed(a, des.abits) << des.ashift) & des.amask);
  1086. // And write to memory
  1087. Bitwise::intWrite(dest, des.elemBytes, value);
  1088. }
  1089. else
  1090. {
  1091. switch (format)
  1092. {
  1093. case PF_FLOAT32_R:
  1094. ((float*)dest)[0] = r;
  1095. break;
  1096. case PF_FLOAT32_RG:
  1097. ((float*)dest)[0] = r;
  1098. ((float*)dest)[1] = g;
  1099. break;
  1100. case PF_FLOAT32_RGB:
  1101. ((float*)dest)[0] = r;
  1102. ((float*)dest)[1] = g;
  1103. ((float*)dest)[2] = b;
  1104. break;
  1105. case PF_FLOAT32_RGBA:
  1106. ((float*)dest)[0] = r;
  1107. ((float*)dest)[1] = g;
  1108. ((float*)dest)[2] = b;
  1109. ((float*)dest)[3] = a;
  1110. break;
  1111. case PF_FLOAT16_R:
  1112. ((UINT16*)dest)[0] = Bitwise::floatToHalf(r);
  1113. break;
  1114. case PF_FLOAT16_RG:
  1115. ((UINT16*)dest)[0] = Bitwise::floatToHalf(r);
  1116. ((UINT16*)dest)[1] = Bitwise::floatToHalf(g);
  1117. break;
  1118. case PF_FLOAT16_RGB:
  1119. ((UINT16*)dest)[0] = Bitwise::floatToHalf(r);
  1120. ((UINT16*)dest)[1] = Bitwise::floatToHalf(g);
  1121. ((UINT16*)dest)[2] = Bitwise::floatToHalf(b);
  1122. break;
  1123. case PF_FLOAT16_RGBA:
  1124. ((UINT16*)dest)[0] = Bitwise::floatToHalf(r);
  1125. ((UINT16*)dest)[1] = Bitwise::floatToHalf(g);
  1126. ((UINT16*)dest)[2] = Bitwise::floatToHalf(b);
  1127. ((UINT16*)dest)[3] = Bitwise::floatToHalf(a);
  1128. break;
  1129. case PF_R8G8:
  1130. ((UINT8*)dest)[0] = (UINT8)Bitwise::floatToFixed(r, 8);
  1131. ((UINT8*)dest)[1] = (UINT8)Bitwise::floatToFixed(g, 8);
  1132. break;
  1133. case PF_R8:
  1134. ((UINT8*)dest)[0] = (UINT8)Bitwise::floatToFixed(r, 8);
  1135. break;
  1136. case PF_FLOAT_R11G11B10:
  1137. {
  1138. UINT32 value;
  1139. value = Bitwise::floatToFloat11(r);
  1140. value |= Bitwise::floatToFloat11(g) << 11;
  1141. value |= Bitwise::floatToFloat10(b) << 22;
  1142. ((UINT32*)dest)[0] = value;
  1143. }
  1144. break;
  1145. default:
  1146. LOGERR("Pack to " + getFormatName(format) + " not implemented");
  1147. break;
  1148. }
  1149. }
  1150. }
  1151. void PixelUtil::unpackColor(Color* color, PixelFormat format, const void* src)
  1152. {
  1153. unpackColor(&color->r, &color->g, &color->b, &color->a, format, src);
  1154. }
  1155. void PixelUtil::unpackColor(UINT8* r, UINT8* g, UINT8* b, UINT8* a, PixelFormat format, const void* src)
  1156. {
  1157. const PixelFormatDescription &des = getDescriptionFor(format);
  1158. if (des.flags & PFF_NATIVEENDIAN)
  1159. {
  1160. // Shortcut for integer formats unpacking
  1161. const UINT32 value = Bitwise::intRead(src, des.elemBytes);
  1162. *r = (UINT8)Bitwise::fixedToFixed((value & des.rmask) >> des.rshift, des.rbits, 8);
  1163. *g = (UINT8)Bitwise::fixedToFixed((value & des.gmask) >> des.gshift, des.gbits, 8);
  1164. *b = (UINT8)Bitwise::fixedToFixed((value & des.bmask) >> des.bshift, des.bbits, 8);
  1165. if (des.flags & PFF_HASALPHA)
  1166. {
  1167. *a = (UINT8)Bitwise::fixedToFixed((value & des.amask) >> des.ashift, des.abits, 8);
  1168. }
  1169. else
  1170. {
  1171. *a = 255; // No alpha, default a component to full
  1172. }
  1173. }
  1174. else
  1175. {
  1176. // Do the operation with the more generic floating point
  1177. float rr, gg, bb, aa;
  1178. unpackColor(&rr, &gg, &bb, &aa, format, src);
  1179. *r = (UINT8)Bitwise::floatToFixed(rr, 8);
  1180. *g = (UINT8)Bitwise::floatToFixed(gg, 8);
  1181. *b = (UINT8)Bitwise::floatToFixed(bb, 8);
  1182. *a = (UINT8)Bitwise::floatToFixed(aa, 8);
  1183. }
  1184. }
  1185. void PixelUtil::unpackColor(float* r, float* g, float* b, float* a, PixelFormat format, const void* src)
  1186. {
  1187. const PixelFormatDescription &des = getDescriptionFor(format);
  1188. if (des.flags & PFF_NATIVEENDIAN)
  1189. {
  1190. // Shortcut for integer formats unpacking
  1191. const unsigned int value = Bitwise::intRead(src, des.elemBytes);
  1192. *r = Bitwise::fixedToFloat((value & des.rmask) >> des.rshift, des.rbits);
  1193. *g = Bitwise::fixedToFloat((value & des.gmask) >> des.gshift, des.gbits);
  1194. *b = Bitwise::fixedToFloat((value & des.bmask) >> des.bshift, des.bbits);
  1195. if (des.flags & PFF_HASALPHA)
  1196. {
  1197. *a = Bitwise::fixedToFloat((value & des.amask) >> des.ashift, des.abits);
  1198. }
  1199. else
  1200. {
  1201. *a = 1.0f; // No alpha, default a component to full
  1202. }
  1203. }
  1204. else
  1205. {
  1206. switch (format)
  1207. {
  1208. case PF_FLOAT32_R:
  1209. *r = *g = *b = ((float*)src)[0];
  1210. *a = 1.0f;
  1211. break;
  1212. case PF_FLOAT32_RG:
  1213. *r = ((float*)src)[0];
  1214. *g = *b = ((float*)src)[1];
  1215. *a = 1.0f;
  1216. break;
  1217. case PF_FLOAT32_RGB:
  1218. *r = ((float*)src)[0];
  1219. *g = ((float*)src)[1];
  1220. *b = ((float*)src)[2];
  1221. *a = 1.0f;
  1222. break;
  1223. case PF_FLOAT32_RGBA:
  1224. *r = ((float*)src)[0];
  1225. *g = ((float*)src)[1];
  1226. *b = ((float*)src)[2];
  1227. *a = ((float*)src)[3];
  1228. break;
  1229. case PF_FLOAT16_R:
  1230. *r = *g = *b = Bitwise::halfToFloat(((UINT16*)src)[0]);
  1231. *a = 1.0f;
  1232. break;
  1233. case PF_FLOAT16_RG:
  1234. *r = Bitwise::halfToFloat(((UINT16*)src)[0]);
  1235. *g = *b = Bitwise::halfToFloat(((UINT16*)src)[1]);
  1236. *a = 1.0f;
  1237. break;
  1238. case PF_FLOAT16_RGB:
  1239. *r = Bitwise::halfToFloat(((UINT16*)src)[0]);
  1240. *g = Bitwise::halfToFloat(((UINT16*)src)[1]);
  1241. *b = Bitwise::halfToFloat(((UINT16*)src)[2]);
  1242. *a = 1.0f;
  1243. break;
  1244. case PF_FLOAT16_RGBA:
  1245. *r = Bitwise::halfToFloat(((UINT16*)src)[0]);
  1246. *g = Bitwise::halfToFloat(((UINT16*)src)[1]);
  1247. *b = Bitwise::halfToFloat(((UINT16*)src)[2]);
  1248. *a = Bitwise::halfToFloat(((UINT16*)src)[3]);
  1249. break;
  1250. case PF_R8G8:
  1251. *r = Bitwise::fixedToFloat(((UINT8*)src)[0], 8);
  1252. *g = Bitwise::fixedToFloat(((UINT8*)src)[1], 8);
  1253. *b = 0.0f;
  1254. *a = 1.0f;
  1255. break;
  1256. case PF_R8:
  1257. *r = Bitwise::fixedToFloat(((UINT8*)src)[0], 8);
  1258. *g = 0.0f;
  1259. *b = 0.0f;
  1260. *a = 1.0f;
  1261. break;
  1262. case PF_FLOAT_R11G11B10:
  1263. {
  1264. UINT32 value = ((UINT32*)src)[0];
  1265. *r = Bitwise::float11ToFloat(value);
  1266. *g = Bitwise::float11ToFloat(value >> 11);
  1267. *b = Bitwise::float10ToFloat(value >> 22);
  1268. }
  1269. break;
  1270. default:
  1271. LOGERR("Unpack from " + getFormatName(format) + " not implemented");
  1272. break;
  1273. }
  1274. }
  1275. }
  1276. void PixelUtil::packDepth(float depth, const PixelFormat format, void* dest)
  1277. {
  1278. if (!isDepth(format))
  1279. {
  1280. LOGERR("Cannot convert depth to " + getFormatName(format) + ": it is not a depth format");
  1281. return;
  1282. }
  1283. LOGERR("Method is not implemented");
  1284. //TODO implement depth packing
  1285. }
  1286. float PixelUtil::unpackDepth(PixelFormat format, void* src)
  1287. {
  1288. const PixelFormatDescription &des = getDescriptionFor(format);
  1289. if (!isDepth(format))
  1290. {
  1291. LOGERR("Cannot unpack from " + getFormatName(format) + ": it is not a depth format");
  1292. return 0;
  1293. }
  1294. UINT32* color = (UINT32 *)src;
  1295. switch (format)
  1296. {
  1297. case PF_D24S8:
  1298. return static_cast<float>(*color & 0x00FFFFFF) / (float)16777216;
  1299. break;
  1300. case PF_D16:
  1301. return static_cast<float>(*color & 0xFFFF) / (float)65536;
  1302. break;
  1303. case PF_D32:
  1304. return static_cast<float>(*color & 0xFFFFFFFF) / (float)4294967296;
  1305. break;
  1306. case PF_D32_S8X24:
  1307. return static_cast<float>(*color & 0xFFFFFFFF) / (float)4294967296;
  1308. break;
  1309. default:
  1310. LOGERR("Cannot unpack from " + getFormatName(format));
  1311. return 0;
  1312. break;
  1313. }
  1314. }
  1315. void PixelUtil::bulkPixelConversion(const PixelData &src, PixelData &dst)
  1316. {
  1317. assert(src.getWidth() == dst.getWidth() &&
  1318. src.getHeight() == dst.getHeight() &&
  1319. src.getDepth() == dst.getDepth());
  1320. // Check for compressed formats, we don't support decompression
  1321. if (PixelUtil::isCompressed(src.getFormat()))
  1322. {
  1323. if (src.getFormat() == dst.getFormat())
  1324. {
  1325. memcpy(dst.getData(), src.getData(), src.getConsecutiveSize());
  1326. return;
  1327. }
  1328. else
  1329. {
  1330. LOGERR("bulkPixelConversion() cannot be used to compress or decompress images");
  1331. return;
  1332. }
  1333. }
  1334. // Check for compression
  1335. if (PixelUtil::isCompressed(dst.getFormat()))
  1336. {
  1337. if (src.getFormat() == dst.getFormat())
  1338. {
  1339. memcpy(dst.getData(), src.getData(), src.getConsecutiveSize());
  1340. return;
  1341. }
  1342. else
  1343. {
  1344. CompressionOptions co;
  1345. co.format = dst.getFormat();
  1346. compress(src, dst, co);
  1347. return;
  1348. }
  1349. }
  1350. // The easy case
  1351. if (src.getFormat() == dst.getFormat())
  1352. {
  1353. // Everything consecutive?
  1354. if (src.isConsecutive() && dst.isConsecutive())
  1355. {
  1356. memcpy(dst.getData(), src.getData(), src.getConsecutiveSize());
  1357. return;
  1358. }
  1359. const UINT32 srcPixelSize = PixelUtil::getNumElemBytes(src.getFormat());
  1360. const UINT32 dstPixelSize = PixelUtil::getNumElemBytes(dst.getFormat());
  1361. UINT8 *srcptr = static_cast<UINT8*>(src.getData())
  1362. + (src.getLeft() + src.getTop() * src.getRowPitch() + src.getFront() * src.getSlicePitch()) * srcPixelSize;
  1363. UINT8 *dstptr = static_cast<UINT8*>(dst.getData())
  1364. + (dst.getLeft() + dst.getTop() * dst.getRowPitch() + dst.getFront() * dst.getSlicePitch()) * dstPixelSize;
  1365. // Calculate pitches+skips in bytes
  1366. const UINT32 srcRowPitchBytes = src.getRowPitch()*srcPixelSize;
  1367. const UINT32 srcSliceSkipBytes = src.getSliceSkip()*srcPixelSize;
  1368. const UINT32 dstRowPitchBytes = dst.getRowPitch()*dstPixelSize;
  1369. const UINT32 dstSliceSkipBytes = dst.getSliceSkip()*dstPixelSize;
  1370. // Otherwise, copy per row
  1371. const UINT32 rowSize = src.getWidth()*srcPixelSize;
  1372. for (UINT32 z = src.getFront(); z < src.getBack(); z++)
  1373. {
  1374. for (UINT32 y = src.getTop(); y < src.getBottom(); y++)
  1375. {
  1376. memcpy(dstptr, srcptr, rowSize);
  1377. srcptr += srcRowPitchBytes;
  1378. dstptr += dstRowPitchBytes;
  1379. }
  1380. srcptr += srcSliceSkipBytes;
  1381. dstptr += dstSliceSkipBytes;
  1382. }
  1383. return;
  1384. }
  1385. UINT32 srcPixelSize = PixelUtil::getNumElemBytes(src.getFormat());
  1386. UINT32 dstPixelSize = PixelUtil::getNumElemBytes(dst.getFormat());
  1387. UINT8 *srcptr = static_cast<UINT8*>(src.getData())
  1388. + (src.getLeft() + src.getTop() * src.getRowPitch() + src.getFront() * src.getSlicePitch()) * srcPixelSize;
  1389. UINT8 *dstptr = static_cast<UINT8*>(dst.getData())
  1390. + (dst.getLeft() + dst.getTop() * dst.getRowPitch() + dst.getFront() * dst.getSlicePitch()) * dstPixelSize;
  1391. // Calculate pitches+skips in bytes
  1392. UINT32 srcRowSkipBytes = src.getRowSkip()*srcPixelSize;
  1393. UINT32 srcSliceSkipBytes = src.getSliceSkip()*srcPixelSize;
  1394. UINT32 dstRowSkipBytes = dst.getRowSkip()*dstPixelSize;
  1395. UINT32 dstSliceSkipBytes = dst.getSliceSkip()*dstPixelSize;
  1396. // The brute force fallback
  1397. float r, g, b, a;
  1398. for (UINT32 z = src.getFront(); z < src.getBack(); z++)
  1399. {
  1400. for (UINT32 y = src.getTop(); y < src.getBottom(); y++)
  1401. {
  1402. for (UINT32 x = src.getLeft(); x < src.getRight(); x++)
  1403. {
  1404. unpackColor(&r, &g, &b, &a, src.getFormat(), srcptr);
  1405. packColor(r, g, b, a, dst.getFormat(), dstptr);
  1406. srcptr += srcPixelSize;
  1407. dstptr += dstPixelSize;
  1408. }
  1409. srcptr += srcRowSkipBytes;
  1410. dstptr += dstRowSkipBytes;
  1411. }
  1412. srcptr += srcSliceSkipBytes;
  1413. dstptr += dstSliceSkipBytes;
  1414. }
  1415. }
  1416. void PixelUtil::flipComponentOrder(PixelData& data)
  1417. {
  1418. if (isCompressed(data.getFormat()))
  1419. {
  1420. LOGERR("flipComponentOrder() not supported on compressed images.");
  1421. return;
  1422. }
  1423. const PixelFormatDescription& pfd = getDescriptionFor(data.getFormat());
  1424. if (pfd.componentCount <= 1) // Nothing to flip
  1425. return;
  1426. bool bitCountMismatch = false;
  1427. if (pfd.rbits != pfd.gbits)
  1428. bitCountMismatch = true;
  1429. if(pfd.componentCount > 2 && pfd.rbits != pfd.bbits)
  1430. bitCountMismatch = true;
  1431. if (pfd.componentCount > 3 && pfd.rbits != pfd.abits)
  1432. bitCountMismatch = true;
  1433. if(bitCountMismatch)
  1434. {
  1435. LOGERR("flipComponentOrder() not supported for formats that don't have the same number of bytes for all components.");
  1436. return;
  1437. }
  1438. struct CompData
  1439. {
  1440. UINT32 mask;
  1441. UINT8 shift;
  1442. };
  1443. std::array<CompData, 4> compData =
  1444. {{
  1445. { pfd.rmask, pfd.rshift },
  1446. { pfd.gmask, pfd.gshift },
  1447. { pfd.bmask, pfd.bshift },
  1448. { pfd.amask, pfd.ashift }
  1449. }};
  1450. // Ensure unused components are at the end, after sort
  1451. if (pfd.componentCount < 4)
  1452. compData[4].shift = 0xFF;
  1453. if (pfd.componentCount < 3)
  1454. compData[3].shift = 0xFF;
  1455. std::sort(compData.begin(), compData.end(),
  1456. [&](const CompData& lhs, const CompData& rhs) { return lhs.shift < rhs.shift; }
  1457. );
  1458. UINT8* dataPtr = data.getData();
  1459. UINT32 pixelSize = pfd.elemBytes;
  1460. UINT32 rowSkipBytes = data.getRowSkip()*pixelSize;
  1461. UINT32 sliceSkipBytes = data.getSliceSkip()*pixelSize;
  1462. for (UINT32 z = 0; z < data.getDepth(); z++)
  1463. {
  1464. for (UINT32 y = 0; y < data.getHeight(); y++)
  1465. {
  1466. for (UINT32 x = 0; x < data.getWidth(); x++)
  1467. {
  1468. if(pfd.componentCount == 2)
  1469. {
  1470. UINT64 pixelData = 0;
  1471. memcpy(&pixelData, dataPtr, pixelSize);
  1472. UINT64 output = 0;
  1473. output |= (pixelData & compData[1].mask) >> compData[1].shift;
  1474. output |= (pixelData & compData[0].mask) << compData[1].shift;
  1475. memcpy(dataPtr, &output, pixelSize);
  1476. }
  1477. else if(pfd.componentCount == 3)
  1478. {
  1479. UINT64 pixelData = 0;
  1480. memcpy(&pixelData, dataPtr, pixelSize);
  1481. UINT64 output = 0;
  1482. output |= (pixelData & compData[2].mask) >> compData[2].shift;
  1483. output |= (pixelData & compData[0].mask) << compData[2].shift;
  1484. memcpy(dataPtr, &output, pixelSize);
  1485. }
  1486. else if(pfd.componentCount == 4)
  1487. {
  1488. UINT64 pixelData = 0;
  1489. memcpy(&pixelData, dataPtr, pixelSize);
  1490. UINT64 output = 0;
  1491. output |= (pixelData & compData[3].mask) >> compData[3].shift;
  1492. output |= (pixelData & compData[0].mask) << compData[3].shift;
  1493. output |= (pixelData & compData[2].mask) >> (compData[2].shift - compData[1].shift);
  1494. output |= (pixelData & compData[1].mask) << (compData[2].shift - compData[1].shift);
  1495. memcpy(dataPtr, &output, pixelSize);
  1496. }
  1497. dataPtr += pixelSize;
  1498. }
  1499. dataPtr += rowSkipBytes;
  1500. }
  1501. dataPtr += sliceSkipBytes;
  1502. }
  1503. }
  1504. void PixelUtil::scale(const PixelData& src, PixelData& scaled, Filter filter)
  1505. {
  1506. assert(PixelUtil::isAccessible(src.getFormat()));
  1507. assert(PixelUtil::isAccessible(scaled.getFormat()));
  1508. PixelData temp;
  1509. switch (filter)
  1510. {
  1511. default:
  1512. case FILTER_NEAREST:
  1513. if(src.getFormat() == scaled.getFormat())
  1514. {
  1515. // No intermediate buffer needed
  1516. temp = scaled;
  1517. }
  1518. else
  1519. {
  1520. // Allocate temporary buffer of destination size in source format
  1521. temp = PixelData(scaled.getWidth(), scaled.getHeight(), scaled.getDepth(), src.getFormat());
  1522. temp.allocateInternalBuffer();
  1523. }
  1524. // No conversion
  1525. switch (PixelUtil::getNumElemBytes(src.getFormat()))
  1526. {
  1527. case 1: NearestResampler<1>::scale(src, temp); break;
  1528. case 2: NearestResampler<2>::scale(src, temp); break;
  1529. case 3: NearestResampler<3>::scale(src, temp); break;
  1530. case 4: NearestResampler<4>::scale(src, temp); break;
  1531. case 6: NearestResampler<6>::scale(src, temp); break;
  1532. case 8: NearestResampler<8>::scale(src, temp); break;
  1533. case 12: NearestResampler<12>::scale(src, temp); break;
  1534. case 16: NearestResampler<16>::scale(src, temp); break;
  1535. default:
  1536. // Never reached
  1537. assert(false);
  1538. }
  1539. if(temp.getData() != scaled.getData())
  1540. {
  1541. // Blit temp buffer
  1542. PixelUtil::bulkPixelConversion(temp, scaled);
  1543. temp.freeInternalBuffer();
  1544. }
  1545. break;
  1546. case FILTER_LINEAR:
  1547. switch (src.getFormat())
  1548. {
  1549. case PF_R8G8:
  1550. case PF_R8G8B8: case PF_B8G8R8:
  1551. case PF_R8G8B8A8: case PF_B8G8R8A8:
  1552. if(src.getFormat() == scaled.getFormat())
  1553. {
  1554. // No intermediate buffer needed
  1555. temp = scaled;
  1556. }
  1557. else
  1558. {
  1559. // Allocate temp buffer of destination size in source format
  1560. temp = PixelData(scaled.getWidth(), scaled.getHeight(), scaled.getDepth(), src.getFormat());
  1561. temp.allocateInternalBuffer();
  1562. }
  1563. // No conversion
  1564. switch (PixelUtil::getNumElemBytes(src.getFormat()))
  1565. {
  1566. case 1: LinearResampler_Byte<1>::scale(src, temp); break;
  1567. case 2: LinearResampler_Byte<2>::scale(src, temp); break;
  1568. case 3: LinearResampler_Byte<3>::scale(src, temp); break;
  1569. case 4: LinearResampler_Byte<4>::scale(src, temp); break;
  1570. default:
  1571. // Never reached
  1572. assert(false);
  1573. }
  1574. if(temp.getData() != scaled.getData())
  1575. {
  1576. // Blit temp buffer
  1577. PixelUtil::bulkPixelConversion(temp, scaled);
  1578. temp.freeInternalBuffer();
  1579. }
  1580. break;
  1581. case PF_FLOAT32_RGB:
  1582. case PF_FLOAT32_RGBA:
  1583. if (scaled.getFormat() == PF_FLOAT32_RGB || scaled.getFormat() == PF_FLOAT32_RGBA)
  1584. {
  1585. // float32 to float32, avoid unpack/repack overhead
  1586. LinearResampler_Float32::scale(src, scaled);
  1587. break;
  1588. }
  1589. // Else, fall through
  1590. default:
  1591. // Fallback case, slow but works
  1592. LinearResampler::scale(src, scaled);
  1593. }
  1594. break;
  1595. }
  1596. }
  1597. void PixelUtil::copy(const PixelData& src, PixelData& dst, UINT32 offsetX, UINT32 offsetY, UINT32 offsetZ)
  1598. {
  1599. if(src.getFormat() != dst.getFormat())
  1600. {
  1601. LOGERR("Source format is different from destination format for copy(). This operation cannot be used for "
  1602. "a format conversion. Aborting copy.");
  1603. return;
  1604. }
  1605. UINT32 right = offsetX + dst.getWidth();
  1606. UINT32 bottom = offsetY + dst.getHeight();
  1607. UINT32 back = offsetZ + dst.getDepth();
  1608. if(right > src.getWidth() || bottom > src.getHeight() || back > src.getDepth())
  1609. {
  1610. LOGERR("Provided offset or destination size is too large and is referencing pixels that are out of bounds"
  1611. " on the source texture. Aborting copy().");
  1612. return;
  1613. }
  1614. UINT8* srcPtr = (UINT8*)src.getData() + offsetZ * src.getSlicePitch();
  1615. UINT8* dstPtr = (UINT8*)dst.getData();
  1616. UINT32 elemSize = getNumElemBytes(dst.getFormat());
  1617. UINT32 rowSize = dst.getWidth() * elemSize;
  1618. for(UINT32 z = 0; z < dst.getDepth(); z++)
  1619. {
  1620. UINT8* srcRowPtr = srcPtr + offsetY * src.getRowPitch() * elemSize;
  1621. UINT8* dstRowPtr = dstPtr;
  1622. for(UINT32 y = 0; y < dst.getHeight(); y++)
  1623. {
  1624. memcpy(dstRowPtr, srcRowPtr + offsetX * elemSize, rowSize);
  1625. srcRowPtr += src.getRowPitch() * elemSize;
  1626. dstRowPtr += dst.getRowPitch() * elemSize;
  1627. }
  1628. srcPtr += src.getSlicePitch() * elemSize;
  1629. dstPtr += dst.getSlicePitch() * elemSize;
  1630. }
  1631. }
  1632. void PixelUtil::mirror(PixelData& pixelData, MirrorMode mode)
  1633. {
  1634. UINT32 width = pixelData.getWidth();
  1635. UINT32 height = pixelData.getHeight();
  1636. UINT32 depth = pixelData.getDepth();
  1637. UINT32 elemSize = getNumElemBytes(pixelData.getFormat());
  1638. if (mode.isSet(MirrorModeBits::Z))
  1639. {
  1640. UINT32 sliceSize = width * height * elemSize;
  1641. UINT8* sliceTemp = bs_stack_alloc<UINT8>(sliceSize);
  1642. UINT8* dataPtr = pixelData.getData();
  1643. UINT32 halfDepth = depth / 2;
  1644. for (UINT32 z = 0; z < halfDepth; z++)
  1645. {
  1646. UINT32 srcZ = z * sliceSize;
  1647. UINT32 dstZ = (depth - z - 1) * sliceSize;
  1648. memcpy(sliceTemp, &dataPtr[dstZ], sliceSize);
  1649. memcpy(&dataPtr[srcZ], &dataPtr[srcZ], sliceSize);
  1650. memcpy(&dataPtr[dstZ], sliceTemp, sliceSize);
  1651. }
  1652. // Note: If flipping Y or X as well I could do it here without an extra set of memcpys
  1653. bs_stack_free(sliceTemp);
  1654. }
  1655. if(mode.isSet(MirrorModeBits::Y))
  1656. {
  1657. UINT32 rowSize = width * elemSize;
  1658. UINT8* rowTemp = bs_stack_alloc<UINT8>(rowSize);
  1659. UINT8* slicePtr = pixelData.getData();
  1660. for (UINT32 z = 0; z < depth; z++)
  1661. {
  1662. UINT32 halfHeight = height / 2;
  1663. for (UINT32 y = 0; y < halfHeight; y++)
  1664. {
  1665. UINT32 srcY = y * rowSize;
  1666. UINT32 dstY = (height - y - 1) * rowSize;
  1667. memcpy(rowTemp, &slicePtr[dstY], rowSize);
  1668. memcpy(&slicePtr[dstY], &slicePtr[srcY], rowSize);
  1669. memcpy(&slicePtr[srcY], rowTemp, rowSize);
  1670. }
  1671. // Note: If flipping X as well I could do it here without an extra set of memcpys
  1672. slicePtr += pixelData.getSlicePitch() * elemSize;
  1673. }
  1674. bs_stack_free(rowTemp);
  1675. }
  1676. if (mode.isSet(MirrorModeBits::X))
  1677. {
  1678. UINT8* elemTemp = bs_stack_alloc<UINT8>(elemSize);
  1679. UINT8* slicePtr = pixelData.getData();
  1680. for (UINT32 z = 0; z < depth; z++)
  1681. {
  1682. UINT8* rowPtr = slicePtr;
  1683. for (UINT32 y = 0; y < height; y++)
  1684. {
  1685. UINT32 halfWidth = width / 2;
  1686. for (UINT32 x = 0; x < halfWidth; x++)
  1687. {
  1688. UINT32 srcX = x * elemSize;
  1689. UINT32 dstX = (width - x - 1) * elemSize;
  1690. memcpy(elemTemp, &rowPtr[dstX], elemSize);
  1691. memcpy(&rowPtr[dstX], &rowPtr[srcX], elemSize);
  1692. memcpy(&rowPtr[srcX], elemTemp, elemSize);
  1693. }
  1694. rowPtr += pixelData.getRowPitch() * elemSize;
  1695. }
  1696. slicePtr += pixelData.getSlicePitch() * elemSize;
  1697. }
  1698. bs_stack_free(elemTemp);
  1699. }
  1700. }
  1701. void PixelUtil::applyGamma(UINT8* buffer, float gamma, UINT32 size, UINT8 bpp)
  1702. {
  1703. if(gamma == 1.0f)
  1704. return;
  1705. UINT32 stride = bpp >> 3;
  1706. for(size_t i = 0, j = size / stride; i < j; i++, buffer += stride)
  1707. {
  1708. float r = (float)buffer[0];
  1709. float g = (float)buffer[1];
  1710. float b = (float)buffer[2];
  1711. r = r * gamma;
  1712. g = g * gamma;
  1713. b = b * gamma;
  1714. float scale = 1.0f;
  1715. float tmp = 0.0f;
  1716. if(r > 255.0f && (tmp=(255.0f/r)) < scale)
  1717. scale = tmp;
  1718. if(g > 255.0f && (tmp=(255.0f/g)) < scale)
  1719. scale = tmp;
  1720. if(b > 255.0f && (tmp=(255.0f/b)) < scale)
  1721. scale = tmp;
  1722. r *= scale;
  1723. g *= scale;
  1724. b *= scale;
  1725. buffer[0] = (UINT8)r;
  1726. buffer[1] = (UINT8)g;
  1727. buffer[2] = (UINT8)b;
  1728. }
  1729. }
  1730. void PixelUtil::compress(const PixelData& src, PixelData& dst, const CompressionOptions& options)
  1731. {
  1732. if (!isCompressed(options.format))
  1733. {
  1734. LOGERR("Compression failed. Destination format is not a valid compressed format.")
  1735. return;
  1736. }
  1737. if (src.getDepth() != 1)
  1738. {
  1739. LOGERR("Compression failed. 3D texture compression not supported.")
  1740. return;
  1741. }
  1742. if (isCompressed(src.getFormat()))
  1743. {
  1744. LOGERR("Compression failed. Source data cannot be compressed.");
  1745. return;
  1746. }
  1747. PixelFormat interimFormat = options.format == PF_BC6H ? PF_FLOAT32_RGBA : PF_B8G8R8A8;
  1748. PixelData interimData(src.getWidth(), src.getHeight(), 1, interimFormat);
  1749. interimData.allocateInternalBuffer();
  1750. bulkPixelConversion(src, interimData);
  1751. if(interimFormat != PF_FLOAT32_RGBA)
  1752. flipComponentOrder(interimData);
  1753. nvtt::InputOptions io;
  1754. io.setTextureLayout(nvtt::TextureType_2D, src.getWidth(), src.getHeight());
  1755. io.setMipmapGeneration(false);
  1756. io.setAlphaMode(toNVTTAlphaMode(options.alphaMode));
  1757. io.setNormalMap(options.isNormalMap);
  1758. if (interimFormat == PF_FLOAT32_RGBA)
  1759. io.setFormat(nvtt::InputFormat_RGBA_32F);
  1760. else
  1761. io.setFormat(nvtt::InputFormat_BGRA_8UB);
  1762. if (options.isSRGB)
  1763. io.setGamma(2.2f, 2.2f);
  1764. else
  1765. io.setGamma(1.0f, 1.0f);
  1766. io.setMipmapData(interimData.getData(), src.getWidth(), src.getHeight());
  1767. nvtt::CompressionOptions co;
  1768. co.setFormat(toNVTTFormat(options.format));
  1769. co.setQuality(toNVTTQuality(options.quality));
  1770. NVTTCompressOutputHandler outputHandler(dst.getData(), dst.getConsecutiveSize());
  1771. nvtt::OutputOptions oo;
  1772. oo.setOutputHeader(false);
  1773. oo.setOutputHandler(&outputHandler);
  1774. nvtt::Compressor compressor;
  1775. if (!compressor.process(io, co, oo))
  1776. {
  1777. LOGERR("Compression failed. Internal error.");
  1778. return;
  1779. }
  1780. }
  1781. Vector<SPtr<PixelData>> PixelUtil::genMipmaps(const PixelData& src, const MipMapGenOptions& options)
  1782. {
  1783. Vector<SPtr<PixelData>> outputMipBuffers;
  1784. if (src.getDepth() != 1)
  1785. {
  1786. LOGERR("Mipmap generation failed. 3D texture formats not supported.")
  1787. return outputMipBuffers;
  1788. }
  1789. if (isCompressed(src.getFormat()))
  1790. {
  1791. LOGERR("Mipmap generation failed. Source data cannot be compressed.")
  1792. return outputMipBuffers;
  1793. }
  1794. if (!Bitwise::isPow2(src.getWidth()) || !Bitwise::isPow2(src.getHeight()))
  1795. {
  1796. LOGERR("Mipmap generation failed. Texture width & height must be powers of 2.");
  1797. return outputMipBuffers;
  1798. }
  1799. PixelFormat interimFormat = isFloatingPoint(src.getFormat()) ? PF_FLOAT32_RGBA : PF_B8G8R8A8;
  1800. PixelData interimData(src.getWidth(), src.getHeight(), 1, interimFormat);
  1801. interimData.allocateInternalBuffer();
  1802. bulkPixelConversion(src, interimData);
  1803. if (interimFormat != PF_FLOAT32_RGBA)
  1804. flipComponentOrder(interimData);
  1805. nvtt::InputOptions io;
  1806. io.setTextureLayout(nvtt::TextureType_2D, src.getWidth(), src.getHeight());
  1807. io.setMipmapGeneration(true);
  1808. io.setNormalMap(options.isNormalMap);
  1809. io.setNormalizeMipmaps(options.normalizeMipmaps);
  1810. io.setWrapMode(toNVTTWrapMode(options.wrapMode));
  1811. if (interimFormat == PF_FLOAT32_RGBA)
  1812. io.setFormat(nvtt::InputFormat_RGBA_32F);
  1813. else
  1814. io.setFormat(nvtt::InputFormat_BGRA_8UB);
  1815. if (options.isSRGB)
  1816. io.setGamma(2.2f, 2.2f);
  1817. else
  1818. io.setGamma(1.0f, 1.0f);
  1819. io.setMipmapData(interimData.getData(), src.getWidth(), src.getHeight());
  1820. nvtt::CompressionOptions co;
  1821. co.setFormat(nvtt::Format_RGBA);
  1822. if (interimFormat == PF_FLOAT32_RGBA)
  1823. {
  1824. co.setPixelType(nvtt::PixelType_Float);
  1825. co.setPixelFormat(32, 32, 32, 32);
  1826. }
  1827. else
  1828. {
  1829. co.setPixelType(nvtt::PixelType_UnsignedNorm);
  1830. co.setPixelFormat(32, 0x0000FF00, 0x00FF0000, 0xFF000000, 0x000000FF);
  1831. }
  1832. UINT32 numMips = getMaxMipmaps(src.getWidth(), src.getHeight(), 1, src.getFormat());
  1833. Vector<SPtr<PixelData>> rgbaMipBuffers;
  1834. // Note: This can be done more effectively without creating so many temp buffers
  1835. // and working with the original formats directly, but it would complicate the code
  1836. // too much at the moment.
  1837. UINT32 curWidth = src.getWidth();
  1838. UINT32 curHeight = src.getHeight();
  1839. for (UINT32 i = 0; i < numMips; i++)
  1840. {
  1841. rgbaMipBuffers.push_back(bs_shared_ptr_new<PixelData>(curWidth, curHeight, 1, interimFormat));
  1842. rgbaMipBuffers.back()->allocateInternalBuffer();
  1843. if (curWidth > 1)
  1844. curWidth = curWidth / 2;
  1845. if (curHeight > 1)
  1846. curHeight = curHeight / 2;
  1847. }
  1848. rgbaMipBuffers.push_back(bs_shared_ptr_new<PixelData>(curWidth, curHeight, 1, interimFormat));
  1849. rgbaMipBuffers.back()->allocateInternalBuffer();
  1850. NVTTMipmapOutputHandler outputHandler(rgbaMipBuffers);
  1851. nvtt::OutputOptions oo;
  1852. oo.setOutputHeader(false);
  1853. oo.setOutputHandler(&outputHandler);
  1854. nvtt::Compressor compressor;
  1855. if (!compressor.process(io, co, oo))
  1856. {
  1857. LOGERR("Mipmap generation failed. Internal error.");
  1858. return outputMipBuffers;
  1859. }
  1860. interimData.freeInternalBuffer();
  1861. for (UINT32 i = 0; i < (UINT32)rgbaMipBuffers.size(); i++)
  1862. {
  1863. SPtr<PixelData> argbBuffer = rgbaMipBuffers[i];
  1864. SPtr<PixelData> outputBuffer = bs_shared_ptr_new<PixelData>(argbBuffer->getWidth(), argbBuffer->getHeight(), 1, src.getFormat());
  1865. outputBuffer->allocateInternalBuffer();
  1866. bulkPixelConversion(*argbBuffer, *outputBuffer);
  1867. argbBuffer->freeInternalBuffer();
  1868. outputMipBuffers.push_back(outputBuffer);
  1869. }
  1870. return outputMipBuffers;
  1871. }
  1872. }