BsPixelUtil.cpp 59 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788
  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 "nvtt/nvtt.h"
  9. namespace BansheeEngine
  10. {
  11. /**
  12. * @brief Performs pixel data resampling using the point filter (nearest neighbor).
  13. * 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. /**
  51. * @brief Performs pixel data resampling using the box filter (linear).
  52. * Performs format conversions.
  53. */
  54. struct LinearResampler
  55. {
  56. static void scale(const PixelData& source, const PixelData& dest)
  57. {
  58. UINT32 sourceElemSize = PixelUtil::getNumElemBytes(source.getFormat());
  59. UINT32 destElemSize = PixelUtil::getNumElemBytes(dest.getFormat());
  60. UINT8* sourceData = source.getData();
  61. UINT8* destPtr = dest.getData();
  62. // Get steps for traversing source data in 16/48 fixed point precision format
  63. UINT64 stepX = ((UINT64)source.getWidth() << 48) / dest.getWidth();
  64. UINT64 stepY = ((UINT64)source.getHeight() << 48) / dest.getHeight();
  65. UINT64 stepZ = ((UINT64)source.getDepth() << 48) / dest.getDepth();
  66. // Contains 16/16 fixed point precision format. Most significant
  67. // 16 bits will contain the coordinate in the source image, and the
  68. // least significant 16 bits will contain the fractional part of the coordinate
  69. // that will be used for determining the blend amount.
  70. UINT32 temp = 0;
  71. UINT64 curZ = (stepZ >> 1) - 1; // Offset half a pixel to start at pixel center
  72. for (UINT32 z = dest.getFront(); z < dest.getBack(); z++, curZ += stepZ)
  73. {
  74. temp = UINT32(curZ >> 32);
  75. temp = (temp > 0x8000)? temp - 0x8000 : 0;
  76. UINT32 sampleCoordZ1 = temp >> 16;
  77. UINT32 sampleCoordZ2 = std::min(sampleCoordZ1 + 1, (UINT32)source.getDepth() - 1);
  78. float sampleWeightZ = (temp & 0xFFFF) / 65536.0f;
  79. UINT64 curY = (stepY >> 1) - 1; // Offset half a pixel to start at pixel center
  80. for (UINT32 y = dest.getTop(); y < dest.getBottom(); y++, curY += stepY)
  81. {
  82. temp = (UINT32)(curY >> 32);
  83. temp = (temp > 0x8000)? temp - 0x8000 : 0;
  84. UINT32 sampleCoordY1 = temp >> 16;
  85. UINT32 sampleCoordY2 = std::min(sampleCoordY1 + 1, (UINT32)source.getHeight() - 1);
  86. float sampleWeightY = (temp & 0xFFFF) / 65536.0f;
  87. UINT64 curX = (stepX >> 1) - 1; // Offset half a pixel to start at pixel center
  88. for (UINT32 x = dest.getLeft(); x < dest.getRight(); x++, curX += stepX)
  89. {
  90. temp = (UINT32)(curX >> 32);
  91. temp = (temp > 0x8000)? temp - 0x8000 : 0;
  92. UINT32 sampleCoordX1 = temp >> 16;
  93. UINT32 sampleCoordX2 = std::min(sampleCoordX1 + 1, (UINT32)source.getWidth() - 1);
  94. float sampleWeightX = (temp & 0xFFFF) / 65536.0f;
  95. Color x1y1z1, x2y1z1, x1y2z1, x2y2z1;
  96. Color x1y1z2, x2y1z2, x1y2z2, x2y2z2;
  97. #define GETSOURCEDATA(x, y, z) sourceData + sourceElemSize*((x)+(y)*source.getRowPitch() + (z)*source.getSlicePitch())
  98. PixelUtil::unpackColor(&x1y1z1, source.getFormat(), GETSOURCEDATA(sampleCoordX1, sampleCoordY1, sampleCoordZ1));
  99. PixelUtil::unpackColor(&x2y1z1, source.getFormat(), GETSOURCEDATA(sampleCoordX2, sampleCoordY1, sampleCoordZ1));
  100. PixelUtil::unpackColor(&x1y2z1, source.getFormat(), GETSOURCEDATA(sampleCoordX1, sampleCoordY2, sampleCoordZ1));
  101. PixelUtil::unpackColor(&x2y2z1, source.getFormat(), GETSOURCEDATA(sampleCoordX2, sampleCoordY2, sampleCoordZ1));
  102. PixelUtil::unpackColor(&x1y1z2, source.getFormat(), GETSOURCEDATA(sampleCoordX1, sampleCoordY1, sampleCoordZ2));
  103. PixelUtil::unpackColor(&x2y1z2, source.getFormat(), GETSOURCEDATA(sampleCoordX2, sampleCoordY1, sampleCoordZ2));
  104. PixelUtil::unpackColor(&x1y2z2, source.getFormat(), GETSOURCEDATA(sampleCoordX1, sampleCoordY2, sampleCoordZ2));
  105. PixelUtil::unpackColor(&x2y2z2, source.getFormat(), GETSOURCEDATA(sampleCoordX2, sampleCoordY2, sampleCoordZ2));
  106. #undef GETSOURCEDATA
  107. Color accum =
  108. x1y1z1 * ((1.0f - sampleWeightX)*(1.0f - sampleWeightY)*(1.0f - sampleWeightZ)) +
  109. x2y1z1 * ( sampleWeightX *(1.0f - sampleWeightY)*(1.0f - sampleWeightZ)) +
  110. x1y2z1 * ((1.0f - sampleWeightX)* sampleWeightY *(1.0f - sampleWeightZ)) +
  111. x2y2z1 * ( sampleWeightX * sampleWeightY *(1.0f - sampleWeightZ)) +
  112. x1y1z2 * ((1.0f - sampleWeightX)*(1.0f - sampleWeightY)* sampleWeightZ ) +
  113. x2y1z2 * ( sampleWeightX *(1.0f - sampleWeightY)* sampleWeightZ ) +
  114. x1y2z2 * ((1.0f - sampleWeightX)* sampleWeightY * sampleWeightZ ) +
  115. x2y2z2 * ( sampleWeightX * sampleWeightY * sampleWeightZ );
  116. PixelUtil::packColor(accum, dest.getFormat(), destPtr);
  117. destPtr += destElemSize;
  118. }
  119. destPtr += destElemSize * dest.getRowSkip();
  120. }
  121. destPtr += destElemSize * dest.getSliceSkip();
  122. }
  123. }
  124. };
  125. /**
  126. * @brief Performs pixel data resampling using the box filter (linear).
  127. * Only handles float RGB or RGBA pixel data (32 bits per channel).
  128. */
  129. struct LinearResampler_Float32
  130. {
  131. static void scale(const PixelData& source, const PixelData& dest)
  132. {
  133. UINT32 numSourceChannels = PixelUtil::getNumElemBytes(source.getFormat()) / sizeof(float);
  134. UINT32 numDestChannels = PixelUtil::getNumElemBytes(dest.getFormat()) / sizeof(float);
  135. float* sourceData = (float*)source.getData();
  136. float* destPtr = (float*)dest.getData();
  137. // Get steps for traversing source data in 16/48 fixed point precision format
  138. UINT64 stepX = ((UINT64)source.getWidth() << 48) / dest.getWidth();
  139. UINT64 stepY = ((UINT64)source.getHeight() << 48) / dest.getHeight();
  140. UINT64 stepZ = ((UINT64)source.getDepth() << 48) / dest.getDepth();
  141. // Contains 16/16 fixed point precision format. Most significant
  142. // 16 bits will contain the coordinate in the source image, and the
  143. // least significant 16 bits will contain the fractional part of the coordinate
  144. // that will be used for determining the blend amount.
  145. UINT32 temp = 0;
  146. UINT64 curZ = (stepZ >> 1) - 1; // Offset half a pixel to start at pixel center
  147. for (UINT32 z = dest.getFront(); z < dest.getBack(); z++, curZ += stepZ)
  148. {
  149. temp = (UINT32)(curZ >> 32);
  150. temp = (temp > 0x8000)? temp - 0x8000 : 0;
  151. UINT32 sampleCoordZ1 = temp >> 16;
  152. UINT32 sampleCoordZ2 = std::min(sampleCoordZ1 + 1, (UINT32)source.getDepth() - 1);
  153. float sampleWeightZ = (temp & 0xFFFF) / 65536.0f;
  154. UINT64 curY = (stepY >> 1) - 1; // Offset half a pixel to start at pixel center
  155. for (UINT32 y = dest.getTop(); y < dest.getBottom(); y++, curY += stepY)
  156. {
  157. temp = (UINT32)(curY >> 32);
  158. temp = (temp > 0x8000)? temp - 0x8000 : 0;
  159. UINT32 sampleCoordY1 = temp >> 16;
  160. UINT32 sampleCoordY2 = std::min(sampleCoordY1 + 1, (UINT32)source.getHeight() - 1);
  161. float sampleWeightY = (temp & 0xFFFF) / 65536.0f;
  162. UINT64 curX = (stepX >> 1) - 1; // Offset half a pixel to start at pixel center
  163. for (UINT32 x = dest.getLeft(); x < dest.getRight(); x++, curX += stepX)
  164. {
  165. temp = (UINT32)(curX >> 32);
  166. temp = (temp > 0x8000)? temp - 0x8000 : 0;
  167. UINT32 sampleCoordX1 = temp >> 16;
  168. UINT32 sampleCoordX2 = std::min(sampleCoordX1 + 1, (UINT32)source.getWidth() - 1);
  169. float sampleWeightX = (temp & 0xFFFF) / 65536.0f;
  170. // process R,G,B,A simultaneously for cache coherence?
  171. float accum[4] = { 0.0f, 0.0f, 0.0f, 0.0f };
  172. #define ACCUM3(x,y,z,factor) \
  173. { float f = factor; \
  174. UINT32 offset = (x + y*source.getRowPitch() + z*source.getSlicePitch())*numSourceChannels; \
  175. accum[0] += sourceData[offset + 0] * f; accum[1] += sourceData[offset + 1] * f; \
  176. accum[2] += sourceData[offset + 2] * f; }
  177. #define ACCUM4(x,y,z,factor) \
  178. { float f = factor; \
  179. UINT32 offset = (x + y*source.getRowPitch() + z*source.getSlicePitch())*numSourceChannels; \
  180. accum[0] += sourceData[offset + 0] * f; accum[1] += sourceData[offset + 1] * f; \
  181. accum[2] += sourceData[offset + 2] * f; accum[3] += sourceData[offset + 3] * f; }
  182. if (numSourceChannels == 3 || numDestChannels == 3)
  183. {
  184. // RGB
  185. ACCUM3(sampleCoordX1, sampleCoordY1, sampleCoordZ1, (1.0f - sampleWeightX) * (1.0f - sampleWeightY) * (1.0f - sampleWeightZ));
  186. ACCUM3(sampleCoordX2, sampleCoordY1, sampleCoordZ1, sampleWeightX * (1.0f - sampleWeightY) * (1.0f - sampleWeightZ));
  187. ACCUM3(sampleCoordX1, sampleCoordY2, sampleCoordZ1, (1.0f - sampleWeightX) * sampleWeightY * (1.0f - sampleWeightZ));
  188. ACCUM3(sampleCoordX2, sampleCoordY2, sampleCoordZ1, sampleWeightX * sampleWeightY * (1.0f - sampleWeightZ));
  189. ACCUM3(sampleCoordX1, sampleCoordY1, sampleCoordZ2, (1.0f - sampleWeightX) * (1.0f - sampleWeightY) * sampleWeightZ);
  190. ACCUM3(sampleCoordX2, sampleCoordY1, sampleCoordZ2, sampleWeightX * (1.0f - sampleWeightY) * sampleWeightZ);
  191. ACCUM3(sampleCoordX1, sampleCoordY2, sampleCoordZ2, (1.0f - sampleWeightX) * sampleWeightY * sampleWeightZ);
  192. ACCUM3(sampleCoordX2, sampleCoordY2, sampleCoordZ2, sampleWeightX * sampleWeightY * sampleWeightZ);
  193. accum[3] = 1.0f;
  194. }
  195. else
  196. {
  197. // RGBA
  198. ACCUM4(sampleCoordX1, sampleCoordY1, sampleCoordZ1, (1.0f - sampleWeightX) * (1.0f - sampleWeightY) * (1.0f - sampleWeightZ));
  199. ACCUM4(sampleCoordX2, sampleCoordY1, sampleCoordZ1, sampleWeightX * (1.0f - sampleWeightY) * (1.0f - sampleWeightZ));
  200. ACCUM4(sampleCoordX1, sampleCoordY2, sampleCoordZ1, (1.0f - sampleWeightX) * sampleWeightY * (1.0f - sampleWeightZ));
  201. ACCUM4(sampleCoordX2, sampleCoordY2, sampleCoordZ1, sampleWeightX * sampleWeightY * (1.0f - sampleWeightZ));
  202. ACCUM4(sampleCoordX1, sampleCoordY1, sampleCoordZ2, (1.0f - sampleWeightX) * (1.0f - sampleWeightY) * sampleWeightZ);
  203. ACCUM4(sampleCoordX2, sampleCoordY1, sampleCoordZ2, sampleWeightX * (1.0f - sampleWeightY) * sampleWeightZ);
  204. ACCUM4(sampleCoordX1, sampleCoordY2, sampleCoordZ2, (1.0f - sampleWeightX) * sampleWeightY * sampleWeightZ);
  205. ACCUM4(sampleCoordX2, sampleCoordY2, sampleCoordZ2, sampleWeightX * sampleWeightY * sampleWeightZ);
  206. }
  207. memcpy(destPtr, accum, sizeof(float)*numDestChannels);
  208. #undef ACCUM3
  209. #undef ACCUM4
  210. destPtr += numDestChannels;
  211. }
  212. destPtr += numDestChannels*dest.getRowSkip();
  213. }
  214. destPtr += numDestChannels*dest.getSliceSkip();
  215. }
  216. }
  217. };
  218. // byte linear resampler, does not do any format conversions.
  219. // only handles pixel formats that use 1 byte per color channel.
  220. // 2D only; punts 3D pixelboxes to default LinearResampler (slow).
  221. // templated on bytes-per-pixel to allow compiler optimizations, such
  222. // as unrolling loops and replacing multiplies with bitshifts
  223. /**
  224. * @brief Performs pixel data resampling using the box filter (linear).
  225. * Only handles pixel formats with one byte per channel. Does
  226. * not perform format conversion.
  227. *
  228. * @tparam channels Number of channels in the pixel format.
  229. */
  230. template<UINT32 channels> struct LinearResampler_Byte
  231. {
  232. static void scale(const PixelData& source, const PixelData& dest)
  233. {
  234. // Only optimized for 2D
  235. if (source.getDepth() > 1 || dest.getDepth() > 1)
  236. {
  237. LinearResampler::scale(source, dest);
  238. return;
  239. }
  240. UINT8* sourceData = (UINT8*)source.getData();
  241. UINT8* destPtr = (UINT8*)dest.getData();
  242. // Get steps for traversing source data in 16/48 fixed point precision format
  243. UINT64 stepX = ((UINT64)source.getWidth() << 48) / dest.getWidth();
  244. UINT64 stepY = ((UINT64)source.getHeight() << 48) / dest.getHeight();
  245. // Contains 16/16 fixed point precision format. Most significant
  246. // 16 bits will contain the coordinate in the source image, and the
  247. // least significant 16 bits will contain the fractional part of the coordinate
  248. // that will be used for determining the blend amount.
  249. UINT32 temp;
  250. UINT64 curY = (stepY >> 1) - 1; // Offset half a pixel to start at pixel center
  251. for (UINT32 y = dest.getTop(); y < dest.getBottom(); y++, curY += stepY)
  252. {
  253. temp = (UINT32)(curY >> 36);
  254. temp = (temp > 0x800)? temp - 0x800: 0;
  255. UINT32 sampleWeightY = temp & 0xFFF;
  256. UINT32 sampleCoordY1 = temp >> 12;
  257. UINT32 sampleCoordY2 = std::min(sampleCoordY1 + 1, (UINT32)source.getBottom() - source.getTop() - 1);
  258. UINT32 sampleY1Offset = sampleCoordY1 * source.getRowPitch();
  259. UINT32 sampleY2Offset = sampleCoordY2 * source.getRowPitch();
  260. UINT64 curX = (stepX >> 1) - 1; // Offset half a pixel to start at pixel center
  261. for (UINT32 x = dest.getLeft(); x < dest.getRight(); x++, curX += stepX)
  262. {
  263. temp = (UINT32)(curX >> 36);
  264. temp = (temp > 0x800)? temp - 0x800 : 0;
  265. UINT32 sampleWeightX = temp & 0xFFF;
  266. UINT32 sampleCoordX1 = temp >> 12;
  267. UINT32 sampleCoordX2 = std::min(sampleCoordX1 + 1, (UINT32)source.getRight() - source.getLeft() - 1);
  268. UINT32 sxfsyf = sampleWeightX*sampleWeightY;
  269. for (UINT32 k = 0; k < channels; k++)
  270. {
  271. UINT32 accum =
  272. sourceData[(sampleCoordX1 + sampleY1Offset)*channels+k]*(0x1000000-(sampleWeightX<<12)-(sampleWeightY<<12)+sxfsyf) +
  273. sourceData[(sampleCoordX2 + sampleY1Offset)*channels+k]*((sampleWeightX<<12)-sxfsyf) +
  274. sourceData[(sampleCoordX1 + sampleY2Offset)*channels+k]*((sampleWeightY<<12)-sxfsyf) +
  275. sourceData[(sampleCoordX2 + sampleY2Offset)*channels+k]*sxfsyf;
  276. // Round up to byte size
  277. *destPtr = (UINT8)((accum + 0x800000) >> 24);
  278. destPtr++;
  279. }
  280. }
  281. destPtr += channels*dest.getRowSkip();
  282. }
  283. }
  284. };
  285. /**
  286. * @brief Data describing a pixel format.
  287. */
  288. struct PixelFormatDescription
  289. {
  290. const char* name; /**< Name of the format. */
  291. UINT8 elemBytes; /**< Number of bytes one element (color value) uses. */
  292. UINT32 flags; /**< PixelFormatFlags set by the pixel format. */
  293. PixelComponentType componentType; /**< Data type of a single element of the format. */
  294. UINT8 componentCount; /**< Number of elements in the format. */
  295. UINT8 rbits, gbits, bbits, abits; /**< Number of bits per element in the format. */
  296. UINT32 rmask, gmask, bmask, amask; /**< Masks used by packers/unpackers. */
  297. UINT8 rshift, gshift, bshift, ashift; /**< Shifts used by packers/unpackers. */
  298. };
  299. /**
  300. * @brief A list of all available pixel formats.
  301. */
  302. PixelFormatDescription _pixelFormats[PF_COUNT] = {
  303. {"PF_UNKNOWN",
  304. /* Bytes per element */
  305. 0,
  306. /* Flags */
  307. 0,
  308. /* Component type and count */
  309. PCT_BYTE, 0,
  310. /* rbits, gbits, bbits, abits */
  311. 0, 0, 0, 0,
  312. /* Masks and shifts */
  313. 0, 0, 0, 0, 0, 0, 0, 0
  314. },
  315. //-----------------------------------------------------------------------
  316. {"PF_R8",
  317. /* Bytes per element */
  318. 1,
  319. /* Flags */
  320. 0,
  321. /* Component type and count */
  322. PCT_BYTE, 1,
  323. /* rbits, gbits, bbits, abits */
  324. 8, 0, 0, 0,
  325. /* Masks and shifts */
  326. 0x000000FF, 0, 0, 0,
  327. 0, 0, 0, 0
  328. },
  329. //-----------------------------------------------------------------------
  330. {"PF_R8G8",
  331. /* Bytes per element */
  332. 2,
  333. /* Flags */
  334. 0,
  335. /* Component type and count */
  336. PCT_BYTE, 2,
  337. /* rbits, gbits, bbits, abits */
  338. 8, 8, 0, 0,
  339. /* Masks and shifts */
  340. 0x000000FF, 0x0000FF00, 0, 0,
  341. 0, 8, 0, 0
  342. },
  343. //-----------------------------------------------------------------------
  344. {"PF_R8G8B8",
  345. /* Bytes per element */
  346. 3, // 24 bit integer -- special
  347. /* Flags */
  348. PFF_NATIVEENDIAN,
  349. /* Component type and count */
  350. PCT_BYTE, 3,
  351. /* rbits, gbits, bbits, abits */
  352. 8, 8, 8, 0,
  353. /* Masks and shifts */
  354. 0x000000FF, 0x0000FF00, 0x00FF0000, 0,
  355. 0, 8, 16, 0
  356. },
  357. //-----------------------------------------------------------------------
  358. {"PF_B8G8R8",
  359. /* Bytes per element */
  360. 3, // 24 bit integer -- special
  361. /* Flags */
  362. PFF_NATIVEENDIAN,
  363. /* Component type and count */
  364. PCT_BYTE, 3,
  365. /* rbits, gbits, bbits, abits */
  366. 8, 8, 8, 0,
  367. /* Masks and shifts */
  368. 0x00FF0000, 0x0000FF00, 0x000000FF, 0,
  369. 16, 8, 0, 0
  370. },
  371. //-----------------------------------------------------------------------
  372. {"PF_A8R8G8B8",
  373. /* Bytes per element */
  374. 4,
  375. /* Flags */
  376. PFF_HASALPHA | PFF_NATIVEENDIAN,
  377. /* Component type and count */
  378. PCT_BYTE, 4,
  379. /* rbits, gbits, bbits, abits */
  380. 8, 8, 8, 8,
  381. /* Masks and shifts */
  382. 0x0000FF00, 0x00FF0000, 0xFF000000, 0x000000FF,
  383. 8, 16, 24, 0
  384. },
  385. //-----------------------------------------------------------------------
  386. {"PF_A8B8G8R8",
  387. /* Bytes per element */
  388. 4,
  389. /* Flags */
  390. PFF_HASALPHA | PFF_NATIVEENDIAN,
  391. /* Component type and count */
  392. PCT_BYTE, 4,
  393. /* rbits, gbits, bbits, abits */
  394. 8, 8, 8, 8,
  395. /* Masks and shifts */
  396. 0xFF000000, 0x00FF0000, 0x0000FF00, 0x000000FF,
  397. 24, 16, 8, 0,
  398. },
  399. //-----------------------------------------------------------------------
  400. {"PF_B8G8R8A8",
  401. /* Bytes per element */
  402. 4,
  403. /* Flags */
  404. PFF_HASALPHA | PFF_NATIVEENDIAN,
  405. /* Component type and count */
  406. PCT_BYTE, 4,
  407. /* rbits, gbits, bbits, abits */
  408. 8, 8, 8, 8,
  409. /* Masks and shifts */
  410. 0x00FF0000, 0x0000FF00, 0x000000FF, 0xFF000000,
  411. 16, 8, 0, 24
  412. },
  413. //-----------------------------------------------------------------------
  414. {"PF_R8G8B8A8",
  415. /* Bytes per element */
  416. 4,
  417. /* Flags */
  418. PFF_HASALPHA | PFF_NATIVEENDIAN,
  419. /* Component type and count */
  420. PCT_BYTE, 4,
  421. /* rbits, gbits, bbits, abits */
  422. 8, 8, 8, 8,
  423. /* Masks and shifts */
  424. 0x000000FF, 0x0000FF00, 0x00FF0000, 0xFF000000,
  425. 0, 8, 16, 24
  426. },
  427. //-----------------------------------------------------------------------
  428. {"PF_X8R8G8B8",
  429. /* Bytes per element */
  430. 4,
  431. /* Flags */
  432. PFF_NATIVEENDIAN,
  433. /* Component type and count */
  434. PCT_BYTE, 3,
  435. /* rbits, gbits, bbits, abits */
  436. 8, 8, 8, 0,
  437. /* Masks and shifts */
  438. 0x0000FF00, 0x00FF0000, 0xFF000000, 0x000000FF,
  439. 8, 16, 24, 0
  440. },
  441. //-----------------------------------------------------------------------
  442. {"PF_X8B8G8R8",
  443. /* Bytes per element */
  444. 4,
  445. /* Flags */
  446. PFF_NATIVEENDIAN,
  447. /* Component type and count */
  448. PCT_BYTE, 3,
  449. /* rbits, gbits, bbits, abits */
  450. 8, 8, 8, 0,
  451. /* Masks and shifts */
  452. 0xFF000000, 0x00FF0000, 0x0000FF00, 0x000000FF,
  453. 24, 16, 8, 0
  454. },
  455. //-----------------------------------------------------------------------
  456. {"PF_R8G8B8X8",
  457. /* Bytes per element */
  458. 4,
  459. /* Flags */
  460. PFF_HASALPHA | PFF_NATIVEENDIAN,
  461. /* Component type and count */
  462. PCT_BYTE, 3,
  463. /* rbits, gbits, bbits, abits */
  464. 8, 8, 8, 0,
  465. /* Masks and shifts */
  466. 0x000000FF, 0x0000FF00, 0x00FF0000, 0xFF000000,
  467. 0, 8, 16, 0
  468. },
  469. //-----------------------------------------------------------------------
  470. {"PF_B8G8R8X8",
  471. /* Bytes per element */
  472. 4,
  473. /* Flags */
  474. PFF_HASALPHA | PFF_NATIVEENDIAN,
  475. /* Component type and count */
  476. PCT_BYTE, 3,
  477. /* rbits, gbits, bbits, abits */
  478. 8, 8, 8, 0,
  479. /* Masks and shifts */
  480. 0x00FF0000, 0x0000FF00, 0x000000FF, 0xFF000000,
  481. 16, 8, 0, 0
  482. },
  483. //-----------------------------------------------------------------------
  484. {"PF_BC1",
  485. /* Bytes per element */
  486. 0,
  487. /* Flags */
  488. PFF_COMPRESSED | PFF_HASALPHA,
  489. /* Component type and count */
  490. PCT_BYTE, 3, // No alpha
  491. /* rbits, gbits, bbits, abits */
  492. 0, 0, 0, 0,
  493. /* Masks and shifts */
  494. 0, 0, 0, 0, 0, 0, 0, 0
  495. },
  496. //-----------------------------------------------------------------------
  497. {"PF_BC1a",
  498. /* Bytes per element */
  499. 0,
  500. /* Flags */
  501. PFF_COMPRESSED,
  502. /* Component type and count */
  503. PCT_BYTE, 3,
  504. /* rbits, gbits, bbits, abits */
  505. 0, 0, 0, 0,
  506. /* Masks and shifts */
  507. 0, 0, 0, 0, 0, 0, 0, 0
  508. },
  509. //-----------------------------------------------------------------------
  510. {"PF_BC2",
  511. /* Bytes per element */
  512. 0,
  513. /* Flags */
  514. PFF_COMPRESSED | PFF_HASALPHA,
  515. /* Component type and count */
  516. PCT_BYTE, 4,
  517. /* rbits, gbits, bbits, abits */
  518. 0, 0, 0, 0,
  519. /* Masks and shifts */
  520. 0, 0, 0, 0, 0, 0, 0, 0
  521. },
  522. //-----------------------------------------------------------------------
  523. {"PF_BC3",
  524. /* Bytes per element */
  525. 0,
  526. /* Flags */
  527. PFF_COMPRESSED | PFF_HASALPHA,
  528. /* Component type and count */
  529. PCT_BYTE, 4,
  530. /* rbits, gbits, bbits, abits */
  531. 0, 0, 0, 0,
  532. /* Masks and shifts */
  533. 0, 0, 0, 0, 0, 0, 0, 0
  534. },
  535. //-----------------------------------------------------------------------
  536. {"PF_BC4",
  537. /* Bytes per element */
  538. 0,
  539. /* Flags */
  540. PFF_COMPRESSED,
  541. /* Component type and count */
  542. PCT_BYTE, 1,
  543. /* rbits, gbits, bbits, abits */
  544. 0, 0, 0, 0,
  545. /* Masks and shifts */
  546. 0, 0, 0, 0, 0, 0, 0, 0
  547. },
  548. //-----------------------------------------------------------------------
  549. {"PF_BC5",
  550. /* Bytes per element */
  551. 0,
  552. /* Flags */
  553. PFF_COMPRESSED,
  554. /* Component type and count */
  555. PCT_BYTE, 2,
  556. /* rbits, gbits, bbits, abits */
  557. 0, 0, 0, 0,
  558. /* Masks and shifts */
  559. 0, 0, 0, 0, 0, 0, 0, 0
  560. },
  561. //-----------------------------------------------------------------------
  562. {"PF_BC6H",
  563. /* Bytes per element */
  564. 0,
  565. /* Flags */
  566. PFF_COMPRESSED,
  567. /* Component type and count */
  568. PCT_FLOAT16, 3,
  569. /* rbits, gbits, bbits, abits */
  570. 0, 0, 0, 0,
  571. /* Masks and shifts */
  572. 0, 0, 0, 0, 0, 0, 0, 0
  573. },
  574. //-----------------------------------------------------------------------
  575. {"PF_BC7",
  576. /* Bytes per element */
  577. 0,
  578. /* Flags */
  579. PFF_COMPRESSED | PFF_HASALPHA,
  580. /* Component type and count */
  581. PCT_BYTE, 4,
  582. /* rbits, gbits, bbits, abits */
  583. 0, 0, 0, 0,
  584. /* Masks and shifts */
  585. 0, 0, 0, 0, 0, 0, 0, 0
  586. },
  587. //-----------------------------------------------------------------------
  588. {"PF_FLOAT16_R",
  589. /* Bytes per element */
  590. 2,
  591. /* Flags */
  592. PFF_FLOAT,
  593. /* Component type and count */
  594. PCT_FLOAT16, 1,
  595. /* rbits, gbits, bbits, abits */
  596. 16, 0, 0, 0,
  597. /* Masks and shifts */
  598. 0, 0, 0, 0, 0, 0, 0, 0
  599. },
  600. //-----------------------------------------------------------------------
  601. {"PF_FLOAT16_RG",
  602. /* Bytes per element */
  603. 4,
  604. /* Flags */
  605. PFF_FLOAT,
  606. /* Component type and count */
  607. PCT_FLOAT16, 2,
  608. /* rbits, gbits, bbits, abits */
  609. 16, 16, 0, 0,
  610. /* Masks and shifts */
  611. 0, 0, 0, 0, 0, 0, 0, 0
  612. },
  613. //-----------------------------------------------------------------------
  614. {"PF_FLOAT16_RGB",
  615. /* Bytes per element */
  616. 6,
  617. /* Flags */
  618. PFF_FLOAT,
  619. /* Component type and count */
  620. PCT_FLOAT16, 3,
  621. /* rbits, gbits, bbits, abits */
  622. 16, 16, 16, 0,
  623. /* Masks and shifts */
  624. 0, 0, 0, 0, 0, 0, 0, 0
  625. },
  626. //-----------------------------------------------------------------------
  627. {"PF_FLOAT16_RGBA",
  628. /* Bytes per element */
  629. 8,
  630. /* Flags */
  631. PFF_FLOAT | PFF_HASALPHA,
  632. /* Component type and count */
  633. PCT_FLOAT16, 4,
  634. /* rbits, gbits, bbits, abits */
  635. 16, 16, 16, 16,
  636. /* Masks and shifts */
  637. 0, 0, 0, 0, 0, 0, 0, 0
  638. },
  639. //-----------------------------------------------------------------------
  640. {"PF_FLOAT32_R",
  641. /* Bytes per element */
  642. 4,
  643. /* Flags */
  644. PFF_FLOAT,
  645. /* Component type and count */
  646. PCT_FLOAT32, 1,
  647. /* rbits, gbits, bbits, abits */
  648. 32, 0, 0, 0,
  649. /* Masks and shifts */
  650. 0, 0, 0, 0, 0, 0, 0, 0
  651. },
  652. //-----------------------------------------------------------------------
  653. {"PF_FLOAT32_RG",
  654. /* Bytes per element */
  655. 8,
  656. /* Flags */
  657. PFF_FLOAT,
  658. /* Component type and count */
  659. PCT_FLOAT32, 2,
  660. /* rbits, gbits, bbits, abits */
  661. 32, 32, 0, 0,
  662. /* Masks and shifts */
  663. 0, 0, 0, 0, 0, 0, 0, 0
  664. },
  665. //-----------------------------------------------------------------------
  666. {"PF_FLOAT32_RGB",
  667. /* Bytes per element */
  668. 12,
  669. /* Flags */
  670. PFF_FLOAT,
  671. /* Component type and count */
  672. PCT_FLOAT32, 3,
  673. /* rbits, gbits, bbits, abits */
  674. 32, 32, 32, 0,
  675. /* Masks and shifts */
  676. 0, 0, 0, 0, 0, 0, 0, 0
  677. },
  678. //-----------------------------------------------------------------------
  679. {"PF_FLOAT32_RGBA",
  680. /* Bytes per element */
  681. 16,
  682. /* Flags */
  683. PFF_FLOAT | PFF_HASALPHA,
  684. /* Component type and count */
  685. PCT_FLOAT32, 4,
  686. /* rbits, gbits, bbits, abits */
  687. 32, 32, 32, 32,
  688. /* Masks and shifts */
  689. 0, 0, 0, 0, 0, 0, 0, 0
  690. },
  691. //-----------------------------------------------------------------------
  692. {"PF_D32_S8X24",
  693. /* Bytes per element */
  694. 4,
  695. /* Flags */
  696. PFF_DEPTH | PFF_FLOAT,
  697. /* Component type and count */
  698. PCT_FLOAT32, 1,
  699. /* rbits, gbits, bbits, abits */
  700. 0, 0, 0, 0,
  701. /* Masks and shifts */
  702. 0, 0, 0, 0, 0, 0, 0, 0
  703. },
  704. //-----------------------------------------------------------------------
  705. {"PF_D24_S8",
  706. /* Bytes per element */
  707. 8,
  708. /* Flags */
  709. PFF_DEPTH | PFF_FLOAT,
  710. /* Component type and count */
  711. PCT_FLOAT32, 2,
  712. /* rbits, gbits, bbits, abits */
  713. 0, 0, 0, 0,
  714. /* Masks and shifts */
  715. 0, 0, 0, 0, 0, 0, 0, 0
  716. },
  717. //-----------------------------------------------------------------------
  718. {"PF_D32",
  719. /* Bytes per element */
  720. 4,
  721. /* Flags */
  722. PFF_DEPTH | PFF_FLOAT,
  723. /* Component type and count */
  724. PCT_FLOAT32, 1,
  725. /* rbits, gbits, bbits, abits */
  726. 0, 0, 0, 0,
  727. /* Masks and shifts */
  728. 0, 0, 0, 0, 0, 0, 0, 0
  729. },
  730. //-----------------------------------------------------------------------
  731. {"PF_D16",
  732. /* Bytes per element */
  733. 2,
  734. /* Flags */
  735. PFF_DEPTH | PFF_FLOAT,
  736. /* Component type and count */
  737. PCT_FLOAT16, 1,
  738. /* rbits, gbits, bbits, abits */
  739. 0, 0, 0, 0,
  740. /* Masks and shifts */
  741. 0, 0, 0, 0, 0, 0, 0, 0
  742. },
  743. //-----------------------------------------------------------------------
  744. { "PF_FLOAT_R11G11B10",
  745. /* Bytes per element */
  746. 4,
  747. /* Flags */
  748. PFF_FLOAT,
  749. /* Component type and count */
  750. PCT_PACKED_R11G11B10, 1,
  751. /* rbits, gbits, bbits, abits */
  752. 11, 11, 10, 0,
  753. /* Masks and shifts */
  754. 0x000007FF, 0x003FF800, 0xFFC00000, 0,
  755. 0, 11, 22, 0
  756. },
  757. //-----------------------------------------------------------------------
  758. { "PF_UNORM_R10G10B10A2",
  759. /* Bytes per element */
  760. 4,
  761. /* Flags */
  762. PFF_FLOAT | PFF_HASALPHA,
  763. /* Component type and count */
  764. PCT_PACKED_R10G10B10A2, 1,
  765. /* rbits, gbits, bbits, abits */
  766. 10, 10, 10, 2,
  767. /* Masks and shifts */
  768. 0x000003FF, 0x000FFC00, 0x3FF00000, 0xC0000000,
  769. 0, 10, 20, 30
  770. },
  771. };
  772. static inline const PixelFormatDescription &getDescriptionFor(const PixelFormat fmt)
  773. {
  774. const int ord = (int)fmt;
  775. assert(ord>=0 && ord<PF_COUNT);
  776. return _pixelFormats[ord];
  777. }
  778. /**
  779. * @brief Handles compression output from NVTT library for a single image.
  780. */
  781. struct NVTTCompressOutputHandler : public nvtt::OutputHandler
  782. {
  783. NVTTCompressOutputHandler(UINT8* buffer, UINT32 sizeBytes)
  784. :buffer(buffer), bufferWritePos(buffer), bufferEnd(buffer + sizeBytes)
  785. { }
  786. virtual void beginImage(int size, int width, int height, int depth, int face, int miplevel) override
  787. { }
  788. virtual bool writeData(const void* data, int size) override
  789. {
  790. assert((bufferWritePos + size) <= bufferEnd);
  791. memcpy(bufferWritePos, data, size);
  792. bufferWritePos += size;
  793. return true;
  794. }
  795. UINT8* buffer;
  796. UINT8* bufferWritePos;
  797. UINT8* bufferEnd;
  798. };
  799. /**
  800. * @brief Handles output from NVTT library for a mip-map chain.
  801. */
  802. struct NVTTMipmapOutputHandler : public nvtt::OutputHandler
  803. {
  804. NVTTMipmapOutputHandler(const Vector<PixelDataPtr>& buffers)
  805. :buffers(buffers), bufferWritePos(nullptr), bufferEnd(nullptr)
  806. { }
  807. virtual void beginImage(int size, int width, int height, int depth, int face, int miplevel)
  808. {
  809. assert(miplevel >= 0 && miplevel < (int)buffers.size());
  810. assert(size == buffers[miplevel]->getConsecutiveSize());
  811. activeBuffer = buffers[miplevel];
  812. bufferWritePos = activeBuffer->getData();
  813. bufferEnd = bufferWritePos + activeBuffer->getConsecutiveSize();
  814. }
  815. virtual bool writeData(const void* data, int size)
  816. {
  817. assert((bufferWritePos + size) <= bufferEnd);
  818. memcpy(bufferWritePos, data, size);
  819. bufferWritePos += size;
  820. return true;
  821. }
  822. Vector<PixelDataPtr> buffers;
  823. PixelDataPtr activeBuffer;
  824. UINT8* bufferWritePos;
  825. UINT8* bufferEnd;
  826. };
  827. nvtt::Format toNVTTFormat(PixelFormat format)
  828. {
  829. switch (format)
  830. {
  831. case PF_BC1:
  832. return nvtt::Format_BC1;
  833. case PF_BC1a:
  834. return nvtt::Format_BC1a;
  835. case PF_BC2:
  836. return nvtt::Format_BC2;
  837. case PF_BC3:
  838. return nvtt::Format_BC3;
  839. case PF_BC4:
  840. return nvtt::Format_BC4;
  841. case PF_BC5:
  842. return nvtt::Format_BC5;
  843. }
  844. // Unsupported format
  845. return nvtt::Format_BC3;
  846. }
  847. nvtt::Quality toNVTTQuality(CompressionQuality quality)
  848. {
  849. switch (quality)
  850. {
  851. case CompressionQuality::Fastest:
  852. return nvtt::Quality_Fastest;
  853. case CompressionQuality::Highest:
  854. return nvtt::Quality_Highest;
  855. case CompressionQuality::Normal:
  856. return nvtt::Quality_Normal;
  857. case CompressionQuality::Production:
  858. return nvtt::Quality_Normal;
  859. }
  860. // Unknown quality level
  861. return nvtt::Quality_Normal;
  862. }
  863. nvtt::AlphaMode toNVTTAlphaMode(AlphaMode alphaMode)
  864. {
  865. switch (alphaMode)
  866. {
  867. case AlphaMode::None:
  868. return nvtt::AlphaMode_None;
  869. case AlphaMode::Premultiplied:
  870. return nvtt::AlphaMode_Premultiplied;
  871. case AlphaMode::Transparency:
  872. return nvtt::AlphaMode_Transparency;
  873. }
  874. // Unknown alpha mode
  875. return nvtt::AlphaMode_None;
  876. }
  877. nvtt::WrapMode toNVTTWrapMode(MipMapWrapMode wrapMode)
  878. {
  879. switch (wrapMode)
  880. {
  881. case MipMapWrapMode::Clamp:
  882. return nvtt::WrapMode_Clamp;
  883. case MipMapWrapMode::Mirror:
  884. return nvtt::WrapMode_Mirror;
  885. case MipMapWrapMode::Repeat:
  886. return nvtt::WrapMode_Repeat;
  887. }
  888. // Unknown alpha mode
  889. return nvtt::WrapMode_Mirror;
  890. }
  891. UINT32 PixelUtil::getNumElemBytes(PixelFormat format)
  892. {
  893. return getDescriptionFor(format).elemBytes;
  894. }
  895. UINT32 PixelUtil::getMemorySize(UINT32 width, UINT32 height, UINT32 depth, PixelFormat format)
  896. {
  897. if(isCompressed(format))
  898. {
  899. switch(format)
  900. {
  901. // BC formats work by dividing the image into 4x4 blocks, then encoding each
  902. // 4x4 block with a certain number of bytes.
  903. case PF_BC1:
  904. case PF_BC1a:
  905. case PF_BC4:
  906. return ((width+3)/4)*((height+3)/4)*8 * depth;
  907. case PF_BC2:
  908. case PF_BC3:
  909. case PF_BC5:
  910. case PF_BC6H:
  911. case PF_BC7:
  912. return ((width+3)/4)*((height+3)/4)*16 * depth;
  913. default:
  914. BS_EXCEPT(InvalidParametersException, "Invalid compressed pixel format");
  915. }
  916. }
  917. else
  918. {
  919. return width*height*depth*getNumElemBytes(format);
  920. }
  921. }
  922. void PixelUtil::getSizeForMipLevel(UINT32 width, UINT32 height, UINT32 depth, UINT32 mipLevel,
  923. UINT32& mipWidth, UINT32& mipHeight, UINT32& mipDepth)
  924. {
  925. mipWidth = width;
  926. mipHeight = height;
  927. mipDepth = depth;
  928. for (UINT32 i = 0; i < mipLevel; i++)
  929. {
  930. if (mipWidth != 1) mipWidth /= 2;
  931. if (mipHeight != 1) mipHeight /= 2;
  932. if (mipDepth != 1) mipDepth /= 2;
  933. }
  934. }
  935. UINT32 PixelUtil::getNumElemBits(PixelFormat format)
  936. {
  937. return getDescriptionFor(format).elemBytes * 8;
  938. }
  939. UINT32 PixelUtil::getFlags(PixelFormat format)
  940. {
  941. return getDescriptionFor(format).flags;
  942. }
  943. bool PixelUtil::hasAlpha(PixelFormat format)
  944. {
  945. return (PixelUtil::getFlags(format) & PFF_HASALPHA) > 0;
  946. }
  947. bool PixelUtil::isFloatingPoint(PixelFormat format)
  948. {
  949. return (PixelUtil::getFlags(format) & PFF_FLOAT) > 0;
  950. }
  951. bool PixelUtil::isCompressed(PixelFormat format)
  952. {
  953. return (PixelUtil::getFlags(format) & PFF_COMPRESSED) > 0;
  954. }
  955. bool PixelUtil::isDepth(PixelFormat format)
  956. {
  957. return (PixelUtil::getFlags(format) & PFF_DEPTH) > 0;
  958. }
  959. bool PixelUtil::isNativeEndian(PixelFormat format)
  960. {
  961. return (PixelUtil::getFlags(format) & PFF_NATIVEENDIAN) > 0;
  962. }
  963. bool PixelUtil::isValidExtent(UINT32 width, UINT32 height, UINT32 depth, PixelFormat format)
  964. {
  965. if(isCompressed(format))
  966. {
  967. switch(format)
  968. {
  969. case PF_BC1:
  970. case PF_BC2:
  971. case PF_BC1a:
  972. case PF_BC3:
  973. case PF_BC4:
  974. case PF_BC5:
  975. case PF_BC6H:
  976. case PF_BC7:
  977. return ((width & 3) == 0 && (height & 3) == 0 && depth == 1);
  978. default:
  979. return true;
  980. }
  981. }
  982. else
  983. {
  984. return true;
  985. }
  986. }
  987. void PixelUtil::getBitDepths(PixelFormat format, int rgba[4])
  988. {
  989. const PixelFormatDescription& des = getDescriptionFor(format);
  990. rgba[0] = des.rbits;
  991. rgba[1] = des.gbits;
  992. rgba[2] = des.bbits;
  993. rgba[3] = des.abits;
  994. }
  995. void PixelUtil::getBitMasks(PixelFormat format, UINT32 rgba[4])
  996. {
  997. const PixelFormatDescription& des = getDescriptionFor(format);
  998. rgba[0] = des.rmask;
  999. rgba[1] = des.gmask;
  1000. rgba[2] = des.bmask;
  1001. rgba[3] = des.amask;
  1002. }
  1003. void PixelUtil::getBitShifts(PixelFormat format, UINT8 rgba[4])
  1004. {
  1005. const PixelFormatDescription& des = getDescriptionFor(format);
  1006. rgba[0] = des.rshift;
  1007. rgba[1] = des.gshift;
  1008. rgba[2] = des.bshift;
  1009. rgba[3] = des.ashift;
  1010. }
  1011. String PixelUtil::getFormatName(PixelFormat srcformat)
  1012. {
  1013. return getDescriptionFor(srcformat).name;
  1014. }
  1015. bool PixelUtil::isAccessible(PixelFormat srcformat)
  1016. {
  1017. if (srcformat == PF_UNKNOWN)
  1018. return false;
  1019. UINT32 flags = getFlags(srcformat);
  1020. return !((flags & PFF_COMPRESSED) || (flags & PFF_DEPTH));
  1021. }
  1022. PixelComponentType PixelUtil::getElementType(PixelFormat format)
  1023. {
  1024. const PixelFormatDescription& des = getDescriptionFor(format);
  1025. return des.componentType;
  1026. }
  1027. UINT32 PixelUtil::getNumElements(PixelFormat format)
  1028. {
  1029. const PixelFormatDescription& des = getDescriptionFor(format);
  1030. return des.componentCount;
  1031. }
  1032. UINT32 PixelUtil::getMaxMipmaps(UINT32 width, UINT32 height, UINT32 depth, PixelFormat format)
  1033. {
  1034. UINT32 count = 0;
  1035. if((width > 0) && (height > 0))
  1036. {
  1037. do {
  1038. if(width>1) width = width/2;
  1039. if(height>1) height = height/2;
  1040. if(depth>1) depth = depth/2;
  1041. count ++;
  1042. } while(!(width == 1 && height == 1 && depth == 1));
  1043. }
  1044. return count;
  1045. }
  1046. void PixelUtil::packColor(const Color& color, PixelFormat format, void* dest)
  1047. {
  1048. packColor(color.r, color.g, color.b, color.a, format, dest);
  1049. }
  1050. void PixelUtil::packColor(UINT8 r, UINT8 g, UINT8 b, UINT8 a, PixelFormat format, void* dest)
  1051. {
  1052. const PixelFormatDescription &des = getDescriptionFor(format);
  1053. if(des.flags & PFF_NATIVEENDIAN)
  1054. {
  1055. // Shortcut for integer formats packing
  1056. UINT32 value = ((Bitwise::fixedToFixed(r, 8, des.rbits)<<des.rshift) & des.rmask) |
  1057. ((Bitwise::fixedToFixed(g, 8, des.gbits)<<des.gshift) & des.gmask) |
  1058. ((Bitwise::fixedToFixed(b, 8, des.bbits)<<des.bshift) & des.bmask) |
  1059. ((Bitwise::fixedToFixed(a, 8, des.abits)<<des.ashift) & des.amask);
  1060. // And write to memory
  1061. Bitwise::intWrite(dest, des.elemBytes, value);
  1062. }
  1063. else
  1064. {
  1065. // Convert to float
  1066. packColor((float)r/255.0f,(float)g/255.0f,(float)b/255.0f,(float)a/255.0f, format, dest);
  1067. }
  1068. }
  1069. void PixelUtil::packColor(float r, float g, float b, float a, const PixelFormat format, void* dest)
  1070. {
  1071. const PixelFormatDescription& des = getDescriptionFor(format);
  1072. if(des.flags & PFF_NATIVEENDIAN)
  1073. {
  1074. // Do the packing
  1075. const unsigned int value = ((Bitwise::floatToFixed(r, des.rbits)<<des.rshift) & des.rmask) |
  1076. ((Bitwise::floatToFixed(g, des.gbits)<<des.gshift) & des.gmask) |
  1077. ((Bitwise::floatToFixed(b, des.bbits)<<des.bshift) & des.bmask) |
  1078. ((Bitwise::floatToFixed(a, des.abits)<<des.ashift) & des.amask);
  1079. // And write to memory
  1080. Bitwise::intWrite(dest, des.elemBytes, value);
  1081. }
  1082. else
  1083. {
  1084. switch(format)
  1085. {
  1086. case PF_FLOAT32_R:
  1087. ((float*)dest)[0] = r;
  1088. break;
  1089. case PF_FLOAT32_RG:
  1090. ((float*)dest)[0] = r;
  1091. ((float*)dest)[1] = g;
  1092. break;
  1093. case PF_FLOAT32_RGB:
  1094. ((float*)dest)[0] = r;
  1095. ((float*)dest)[1] = g;
  1096. ((float*)dest)[2] = b;
  1097. break;
  1098. case PF_FLOAT32_RGBA:
  1099. ((float*)dest)[0] = r;
  1100. ((float*)dest)[1] = g;
  1101. ((float*)dest)[2] = b;
  1102. ((float*)dest)[3] = a;
  1103. break;
  1104. case PF_FLOAT16_R:
  1105. ((UINT16*)dest)[0] = Bitwise::floatToHalf(r);
  1106. break;
  1107. case PF_FLOAT16_RG:
  1108. ((UINT16*)dest)[0] = Bitwise::floatToHalf(r);
  1109. ((UINT16*)dest)[1] = Bitwise::floatToHalf(g);
  1110. break;
  1111. case PF_FLOAT16_RGB:
  1112. ((UINT16*)dest)[0] = Bitwise::floatToHalf(r);
  1113. ((UINT16*)dest)[1] = Bitwise::floatToHalf(g);
  1114. ((UINT16*)dest)[2] = Bitwise::floatToHalf(b);
  1115. break;
  1116. case PF_FLOAT16_RGBA:
  1117. ((UINT16*)dest)[0] = Bitwise::floatToHalf(r);
  1118. ((UINT16*)dest)[1] = Bitwise::floatToHalf(g);
  1119. ((UINT16*)dest)[2] = Bitwise::floatToHalf(b);
  1120. ((UINT16*)dest)[3] = Bitwise::floatToHalf(a);
  1121. break;
  1122. case PF_R8G8:
  1123. ((UINT8*)dest)[0] = (UINT8)Bitwise::floatToFixed(r, 8);
  1124. ((UINT8*)dest)[1] = (UINT8)Bitwise::floatToFixed(g, 8);
  1125. break;
  1126. case PF_R8:
  1127. ((UINT8*)dest)[0] = (UINT8)Bitwise::floatToFixed(r, 8);
  1128. break;
  1129. default:
  1130. BS_EXCEPT(NotImplementedException, "Pack to " + getFormatName(format) + " not implemented");
  1131. break;
  1132. }
  1133. }
  1134. }
  1135. void PixelUtil::unpackColor(Color* color, PixelFormat format, const void* src)
  1136. {
  1137. unpackColor(&color->r, &color->g, &color->b, &color->a, format, src);
  1138. }
  1139. void PixelUtil::unpackColor(UINT8* r, UINT8* g, UINT8* b, UINT8* a, PixelFormat format, const void* src)
  1140. {
  1141. const PixelFormatDescription &des = getDescriptionFor(format);
  1142. if(des.flags & PFF_NATIVEENDIAN)
  1143. {
  1144. // Shortcut for integer formats unpacking
  1145. const UINT32 value = Bitwise::intRead(src, des.elemBytes);
  1146. *r = (UINT8)Bitwise::fixedToFixed((value & des.rmask)>>des.rshift, des.rbits, 8);
  1147. *g = (UINT8)Bitwise::fixedToFixed((value & des.gmask)>>des.gshift, des.gbits, 8);
  1148. *b = (UINT8)Bitwise::fixedToFixed((value & des.bmask)>>des.bshift, des.bbits, 8);
  1149. if(des.flags & PFF_HASALPHA)
  1150. {
  1151. *a = (UINT8)Bitwise::fixedToFixed((value & des.amask)>>des.ashift, des.abits, 8);
  1152. }
  1153. else
  1154. {
  1155. *a = 255; // No alpha, default a component to full
  1156. }
  1157. }
  1158. else
  1159. {
  1160. // Do the operation with the more generic floating point
  1161. float rr, gg, bb, aa;
  1162. unpackColor(&rr,&gg,&bb,&aa, format, src);
  1163. *r = (UINT8)Bitwise::floatToFixed(rr, 8);
  1164. *g = (UINT8)Bitwise::floatToFixed(gg, 8);
  1165. *b = (UINT8)Bitwise::floatToFixed(bb, 8);
  1166. *a = (UINT8)Bitwise::floatToFixed(aa, 8);
  1167. }
  1168. }
  1169. void PixelUtil::unpackColor(float* r, float* g, float* b, float* a, PixelFormat format, const void* src)
  1170. {
  1171. const PixelFormatDescription &des = getDescriptionFor(format);
  1172. if(des.flags & PFF_NATIVEENDIAN)
  1173. {
  1174. // Shortcut for integer formats unpacking
  1175. const unsigned int value = Bitwise::intRead(src, des.elemBytes);
  1176. *r = Bitwise::fixedToFloat((value & des.rmask)>>des.rshift, des.rbits);
  1177. *g = Bitwise::fixedToFloat((value & des.gmask)>>des.gshift, des.gbits);
  1178. *b = Bitwise::fixedToFloat((value & des.bmask)>>des.bshift, des.bbits);
  1179. if(des.flags & PFF_HASALPHA)
  1180. {
  1181. *a = Bitwise::fixedToFloat((value & des.amask)>>des.ashift, des.abits);
  1182. }
  1183. else
  1184. {
  1185. *a = 1.0f; // No alpha, default a component to full
  1186. }
  1187. }
  1188. else
  1189. {
  1190. switch(format)
  1191. {
  1192. case PF_FLOAT32_R:
  1193. *r = *g = *b = ((float*)src)[0];
  1194. *a = 1.0f;
  1195. break;
  1196. case PF_FLOAT32_RG:
  1197. *r = ((float*)src)[0];
  1198. *g = *b = ((float*)src)[1];
  1199. *a = 1.0f;
  1200. break;
  1201. case PF_FLOAT32_RGB:
  1202. *r = ((float*)src)[0];
  1203. *g = ((float*)src)[1];
  1204. *b = ((float*)src)[2];
  1205. *a = 1.0f;
  1206. break;
  1207. case PF_FLOAT32_RGBA:
  1208. *r = ((float*)src)[0];
  1209. *g = ((float*)src)[1];
  1210. *b = ((float*)src)[2];
  1211. *a = ((float*)src)[3];
  1212. break;
  1213. case PF_FLOAT16_R:
  1214. *r = *g = *b = Bitwise::halfToFloat(((UINT16*)src)[0]);
  1215. *a = 1.0f;
  1216. break;
  1217. case PF_FLOAT16_RG:
  1218. *r = Bitwise::halfToFloat(((UINT16*)src)[0]);
  1219. *g = *b = Bitwise::halfToFloat(((UINT16*)src)[1]);
  1220. *a = 1.0f;
  1221. break;
  1222. case PF_FLOAT16_RGB:
  1223. *r = Bitwise::halfToFloat(((UINT16*)src)[0]);
  1224. *g = Bitwise::halfToFloat(((UINT16*)src)[1]);
  1225. *b = Bitwise::halfToFloat(((UINT16*)src)[2]);
  1226. *a = 1.0f;
  1227. break;
  1228. case PF_FLOAT16_RGBA:
  1229. *r = Bitwise::halfToFloat(((UINT16*)src)[0]);
  1230. *g = Bitwise::halfToFloat(((UINT16*)src)[1]);
  1231. *b = Bitwise::halfToFloat(((UINT16*)src)[2]);
  1232. *a = Bitwise::halfToFloat(((UINT16*)src)[3]);
  1233. break;
  1234. case PF_R8G8:
  1235. *r = Bitwise::fixedToFloat(((UINT8*)src)[0], 8);
  1236. *g = Bitwise::fixedToFloat(((UINT8*)src)[1], 8);
  1237. *b = 0.0f;
  1238. *a = 1.0f;
  1239. break;
  1240. case PF_R8:
  1241. *r = Bitwise::fixedToFloat(((UINT8*)src)[0], 8);
  1242. *g = 0.0f;
  1243. *b = 0.0f;
  1244. *a = 1.0f;
  1245. break;
  1246. default:
  1247. BS_EXCEPT(NotImplementedException, "Unpack from " + getFormatName(format) + " not implemented");
  1248. break;
  1249. }
  1250. }
  1251. }
  1252. void PixelUtil::bulkPixelConversion(const PixelData &src, PixelData &dst)
  1253. {
  1254. assert(src.getWidth() == dst.getWidth() &&
  1255. src.getHeight() == dst.getHeight() &&
  1256. src.getDepth() == dst.getDepth());
  1257. // Check for compressed formats, we don't support decompression
  1258. if(PixelUtil::isCompressed(src.getFormat()))
  1259. {
  1260. if(src.getFormat() == dst.getFormat())
  1261. {
  1262. memcpy(dst.getData(), src.getData(), src.getConsecutiveSize());
  1263. return;
  1264. }
  1265. else
  1266. {
  1267. BS_EXCEPT(NotImplementedException, "This method can not be used to compress or decompress images");
  1268. }
  1269. }
  1270. // Check for compression
  1271. if (PixelUtil::isCompressed(dst.getFormat()))
  1272. {
  1273. if (src.getFormat() == dst.getFormat())
  1274. {
  1275. memcpy(dst.getData(), src.getData(), src.getConsecutiveSize());
  1276. return;
  1277. }
  1278. else
  1279. {
  1280. CompressionOptions co;
  1281. co.format = dst.getFormat();
  1282. compress(src, dst, co);
  1283. return;
  1284. }
  1285. }
  1286. // The easy case
  1287. if(src.getFormat() == dst.getFormat())
  1288. {
  1289. // Everything consecutive?
  1290. if(src.isConsecutive() && dst.isConsecutive())
  1291. {
  1292. memcpy(dst.getData(), src.getData(), src.getConsecutiveSize());
  1293. return;
  1294. }
  1295. const UINT32 srcPixelSize = PixelUtil::getNumElemBytes(src.getFormat());
  1296. const UINT32 dstPixelSize = PixelUtil::getNumElemBytes(dst.getFormat());
  1297. UINT8 *srcptr = static_cast<UINT8*>(src.getData())
  1298. + (src.getLeft() + src.getTop() * src.getRowPitch() + src.getFront() * src.getSlicePitch()) * srcPixelSize;
  1299. UINT8 *dstptr = static_cast<UINT8*>(dst.getData())
  1300. + (dst.getLeft() + dst.getTop() * dst.getRowPitch() + dst.getFront() * dst.getSlicePitch()) * dstPixelSize;
  1301. // Calculate pitches+skips in bytes
  1302. const UINT32 srcRowPitchBytes = src.getRowPitch()*srcPixelSize;
  1303. const UINT32 srcSliceSkipBytes = src.getSliceSkip()*srcPixelSize;
  1304. const UINT32 dstRowPitchBytes = dst.getRowPitch()*dstPixelSize;
  1305. const UINT32 dstSliceSkipBytes = dst.getSliceSkip()*dstPixelSize;
  1306. // Otherwise, copy per row
  1307. const UINT32 rowSize = src.getWidth()*srcPixelSize;
  1308. for (UINT32 z = src.getFront(); z < src.getBack(); z++)
  1309. {
  1310. for(UINT32 y = src.getTop(); y < src.getBottom(); y++)
  1311. {
  1312. memcpy(dstptr, srcptr, rowSize);
  1313. srcptr += srcRowPitchBytes;
  1314. dstptr += dstRowPitchBytes;
  1315. }
  1316. srcptr += srcSliceSkipBytes;
  1317. dstptr += dstSliceSkipBytes;
  1318. }
  1319. return;
  1320. }
  1321. // Converting to PF_X8R8G8B8 is exactly the same as converting to
  1322. // PF_A8R8G8B8. (same with PF_X8B8G8R8 and PF_A8B8G8R8)
  1323. if(dst.getFormat() == PF_X8R8G8B8 || dst.getFormat() == PF_X8B8G8R8)
  1324. {
  1325. // Do the same conversion, with PF_A8R8G8B8, which has a lot of
  1326. // optimized conversions
  1327. PixelFormat tempFormat = dst.getFormat() == PF_X8R8G8B8?PF_A8R8G8B8:PF_A8B8G8R8;
  1328. PixelData tempdst(dst.getWidth(), dst.getHeight(), dst.getDepth(), tempFormat);
  1329. bulkPixelConversion(src, tempdst);
  1330. return;
  1331. }
  1332. // Converting from PF_X8R8G8B8 is exactly the same as converting from
  1333. // PF_A8R8G8B8, given that the destination format does not have alpha.
  1334. if((src.getFormat() == PF_X8R8G8B8 || src.getFormat() == PF_X8B8G8R8) && !hasAlpha(dst.getFormat()))
  1335. {
  1336. // Do the same conversion, with PF_A8R8G8B8, which has a lot of
  1337. // optimized conversions
  1338. PixelFormat tempFormat = src.getFormat()==PF_X8R8G8B8?PF_A8R8G8B8:PF_A8B8G8R8;
  1339. PixelData tempsrc(src.getWidth(), src.getHeight(), src.getDepth(), tempFormat);
  1340. tempsrc.setExternalBuffer(src.getData());
  1341. bulkPixelConversion(tempsrc, dst);
  1342. return;
  1343. }
  1344. const UINT32 srcPixelSize = PixelUtil::getNumElemBytes(src.getFormat());
  1345. const UINT32 dstPixelSize = PixelUtil::getNumElemBytes(dst.getFormat());
  1346. UINT8 *srcptr = static_cast<UINT8*>(src.getData())
  1347. + (src.getLeft() + src.getTop() * src.getRowPitch() + src.getFront() * src.getSlicePitch()) * srcPixelSize;
  1348. UINT8 *dstptr = static_cast<UINT8*>(dst.getData())
  1349. + (dst.getLeft() + dst.getTop() * dst.getRowPitch() + dst.getFront() * dst.getSlicePitch()) * dstPixelSize;
  1350. // Calculate pitches+skips in bytes
  1351. const UINT32 srcRowSkipBytes = src.getRowSkip()*srcPixelSize;
  1352. const UINT32 srcSliceSkipBytes = src.getSliceSkip()*srcPixelSize;
  1353. const UINT32 dstRowSkipBytes = dst.getRowSkip()*dstPixelSize;
  1354. const UINT32 dstSliceSkipBytes = dst.getSliceSkip()*dstPixelSize;
  1355. // The brute force fallback
  1356. float r,g,b,a;
  1357. for (UINT32 z = src.getFront(); z<src.getBack(); z++)
  1358. {
  1359. for (UINT32 y = src.getTop(); y < src.getBottom(); y++)
  1360. {
  1361. for (UINT32 x = src.getLeft(); x<src.getRight(); x++)
  1362. {
  1363. unpackColor(&r, &g, &b, &a, src.getFormat(), srcptr);
  1364. packColor(r, g, b, a, dst.getFormat(), dstptr);
  1365. srcptr += srcPixelSize;
  1366. dstptr += dstPixelSize;
  1367. }
  1368. srcptr += srcRowSkipBytes;
  1369. dstptr += dstRowSkipBytes;
  1370. }
  1371. srcptr += srcSliceSkipBytes;
  1372. dstptr += dstSliceSkipBytes;
  1373. }
  1374. }
  1375. void PixelUtil::scale(const PixelData& src, PixelData& scaled, Filter filter)
  1376. {
  1377. assert(PixelUtil::isAccessible(src.getFormat()));
  1378. assert(PixelUtil::isAccessible(scaled.getFormat()));
  1379. PixelData temp;
  1380. switch (filter)
  1381. {
  1382. default:
  1383. case FILTER_NEAREST:
  1384. if(src.getFormat() == scaled.getFormat())
  1385. {
  1386. // No intermediate buffer needed
  1387. temp = scaled;
  1388. }
  1389. else
  1390. {
  1391. // Allocate temporary buffer of destination size in source format
  1392. temp = PixelData(scaled.getWidth(), scaled.getHeight(), scaled.getDepth(), src.getFormat());
  1393. temp.allocateInternalBuffer();
  1394. }
  1395. // No conversion
  1396. switch (PixelUtil::getNumElemBytes(src.getFormat()))
  1397. {
  1398. case 1: NearestResampler<1>::scale(src, temp); break;
  1399. case 2: NearestResampler<2>::scale(src, temp); break;
  1400. case 3: NearestResampler<3>::scale(src, temp); break;
  1401. case 4: NearestResampler<4>::scale(src, temp); break;
  1402. case 6: NearestResampler<6>::scale(src, temp); break;
  1403. case 8: NearestResampler<8>::scale(src, temp); break;
  1404. case 12: NearestResampler<12>::scale(src, temp); break;
  1405. case 16: NearestResampler<16>::scale(src, temp); break;
  1406. default:
  1407. // Never reached
  1408. assert(false);
  1409. }
  1410. if(temp.getData() != scaled.getData())
  1411. {
  1412. // Blit temp buffer
  1413. PixelUtil::bulkPixelConversion(temp, scaled);
  1414. temp.freeInternalBuffer();
  1415. }
  1416. break;
  1417. case FILTER_LINEAR:
  1418. switch (src.getFormat())
  1419. {
  1420. case PF_R8G8:
  1421. case PF_R8G8B8: case PF_B8G8R8:
  1422. case PF_R8G8B8A8: case PF_B8G8R8A8:
  1423. case PF_A8B8G8R8: case PF_A8R8G8B8:
  1424. case PF_X8B8G8R8: case PF_X8R8G8B8:
  1425. if(src.getFormat() == scaled.getFormat())
  1426. {
  1427. // No intermediate buffer needed
  1428. temp = scaled;
  1429. }
  1430. else
  1431. {
  1432. // Allocate temp buffer of destination size in source format
  1433. temp = PixelData(scaled.getWidth(), scaled.getHeight(), scaled.getDepth(), src.getFormat());
  1434. temp.allocateInternalBuffer();
  1435. }
  1436. // No conversion
  1437. switch (PixelUtil::getNumElemBytes(src.getFormat()))
  1438. {
  1439. case 1: LinearResampler_Byte<1>::scale(src, temp); break;
  1440. case 2: LinearResampler_Byte<2>::scale(src, temp); break;
  1441. case 3: LinearResampler_Byte<3>::scale(src, temp); break;
  1442. case 4: LinearResampler_Byte<4>::scale(src, temp); break;
  1443. default:
  1444. // Never reached
  1445. assert(false);
  1446. }
  1447. if(temp.getData() != scaled.getData())
  1448. {
  1449. // Blit temp buffer
  1450. PixelUtil::bulkPixelConversion(temp, scaled);
  1451. temp.freeInternalBuffer();
  1452. }
  1453. break;
  1454. case PF_FLOAT32_RGB:
  1455. case PF_FLOAT32_RGBA:
  1456. if (scaled.getFormat() == PF_FLOAT32_RGB || scaled.getFormat() == PF_FLOAT32_RGBA)
  1457. {
  1458. // float32 to float32, avoid unpack/repack overhead
  1459. LinearResampler_Float32::scale(src, scaled);
  1460. break;
  1461. }
  1462. // Else, fall through
  1463. default:
  1464. // Fallback case, slow but works
  1465. LinearResampler::scale(src, scaled);
  1466. }
  1467. break;
  1468. }
  1469. }
  1470. void PixelUtil::applyGamma(UINT8* buffer, float gamma, UINT32 size, UINT8 bpp)
  1471. {
  1472. if(gamma == 1.0f)
  1473. return;
  1474. UINT32 stride = bpp >> 3;
  1475. for(size_t i = 0, j = size / stride; i < j; i++, buffer += stride)
  1476. {
  1477. float r = (float)buffer[0];
  1478. float g = (float)buffer[1];
  1479. float b = (float)buffer[2];
  1480. r = r * gamma;
  1481. g = g * gamma;
  1482. b = b * gamma;
  1483. float scale = 1.0f;
  1484. float tmp = 0.0f;
  1485. if(r > 255.0f && (tmp=(255.0f/r)) < scale)
  1486. scale = tmp;
  1487. if(g > 255.0f && (tmp=(255.0f/g)) < scale)
  1488. scale = tmp;
  1489. if(b > 255.0f && (tmp=(255.0f/b)) < scale)
  1490. scale = tmp;
  1491. r *= scale;
  1492. g *= scale;
  1493. b *= scale;
  1494. buffer[0] = (UINT8)r;
  1495. buffer[1] = (UINT8)g;
  1496. buffer[2] = (UINT8)b;
  1497. }
  1498. }
  1499. void PixelUtil::compress(const PixelData& src, PixelData& dst, const CompressionOptions& options)
  1500. {
  1501. if (!isCompressed(options.format))
  1502. BS_EXCEPT(InvalidParametersException, "Wanted format is not a compressed format.");
  1503. // Note: NVTT site has implementations for these two formats for when I decide to add them
  1504. if (options.format == PF_BC6H || options.format == PF_BC7)
  1505. BS_EXCEPT(InvalidParametersException, "Specified formats are not yet supported.");
  1506. if (src.getDepth() != 1)
  1507. BS_EXCEPT(InvalidParametersException, "3D textures are not supported.");
  1508. PixelFormat pf = options.format;
  1509. if (isCompressed(src.getFormat()))
  1510. BS_EXCEPT(InvalidParametersException, "Source data cannot be compressed.");
  1511. PixelData bgraData(src.getWidth(), src.getHeight(), 1, PF_B8G8R8A8);
  1512. bgraData.allocateInternalBuffer();
  1513. bulkPixelConversion(src, bgraData);
  1514. nvtt::InputOptions io;
  1515. io.setTextureLayout(nvtt::TextureType_2D, src.getWidth(), src.getHeight());
  1516. io.setMipmapData(bgraData.getData(), src.getWidth(), src.getHeight());
  1517. io.setMipmapGeneration(false);
  1518. io.setAlphaMode(toNVTTAlphaMode(options.alphaMode));
  1519. io.setNormalMap(options.isNormalMap);
  1520. if (options.isSRGB)
  1521. io.setGamma(2.2f, 2.2f);
  1522. else
  1523. io.setGamma(1.0f, 1.0f);
  1524. nvtt::CompressionOptions co;
  1525. co.setFormat(toNVTTFormat(options.format));
  1526. co.setQuality(toNVTTQuality(options.quality));
  1527. NVTTCompressOutputHandler outputHandler(dst.getData(), dst.getConsecutiveSize());
  1528. nvtt::OutputOptions oo;
  1529. oo.setOutputHeader(false);
  1530. oo.setOutputHandler(&outputHandler);
  1531. nvtt::Compressor compressor;
  1532. if (!compressor.process(io, co, oo))
  1533. BS_EXCEPT(InternalErrorException, "Compressing failed.");
  1534. }
  1535. Vector<PixelDataPtr> PixelUtil::genMipmaps(const PixelData& src, const MipMapGenOptions& options)
  1536. {
  1537. if (src.getDepth() != 1)
  1538. BS_EXCEPT(InvalidParametersException, "3D textures are not supported.");
  1539. // Note: Add support for floating point mips, no reason they shouldn't be supported other than
  1540. // nvtt doesn't support them natively
  1541. if (isCompressed(src.getFormat()) || isFloatingPoint(src.getFormat()))
  1542. BS_EXCEPT(InvalidParametersException, "Source data cannot be compressed or in floating point format.");
  1543. if (!Math::isPow2(src.getWidth()) || !Math::isPow2(src.getHeight()))
  1544. BS_EXCEPT(InvalidParametersException, "Texture width & height must be powers of 2.");
  1545. PixelData argbData(src.getWidth(), src.getHeight(), 1, PF_A8R8G8B8);
  1546. argbData.allocateInternalBuffer();
  1547. bulkPixelConversion(src, argbData);
  1548. nvtt::InputOptions io;
  1549. io.setTextureLayout(nvtt::TextureType_2D, src.getWidth(), src.getHeight());
  1550. io.setMipmapData(argbData.getData(), src.getWidth(), src.getHeight());
  1551. io.setMipmapGeneration(true);
  1552. io.setNormalMap(options.isNormalMap);
  1553. io.setNormalizeMipmaps(options.normalizeMipmaps);
  1554. io.setWrapMode(toNVTTWrapMode(options.wrapMode));
  1555. nvtt::CompressionOptions co;
  1556. co.setFormat(nvtt::Format_RGBA);
  1557. UINT32 numMips = getMaxMipmaps(src.getWidth(), src.getHeight(), 1, src.getFormat());
  1558. Vector<PixelDataPtr> argbMipBuffers;
  1559. // Note: This can be done more effectively without creating so many temp buffers
  1560. // and working with the original formats directly, but it would complicate the code
  1561. // too much at the moment.
  1562. UINT32 curWidth = src.getWidth();
  1563. UINT32 curHeight = src.getHeight();
  1564. for (UINT32 i = 0; i < numMips; i++)
  1565. {
  1566. argbMipBuffers.push_back(bs_shared_ptr_new<PixelData>(curWidth, curHeight, 1, PF_A8R8G8B8));
  1567. argbMipBuffers.back()->allocateInternalBuffer();
  1568. if (curWidth > 1)
  1569. curWidth = curWidth / 2;
  1570. if (curHeight > 1)
  1571. curHeight = curHeight / 2;
  1572. }
  1573. argbMipBuffers.push_back(bs_shared_ptr_new<PixelData>(curWidth, curHeight, 1, PF_A8R8G8B8));
  1574. argbMipBuffers.back()->allocateInternalBuffer();
  1575. NVTTMipmapOutputHandler outputHandler(argbMipBuffers);
  1576. nvtt::OutputOptions oo;
  1577. oo.setOutputHeader(false);
  1578. oo.setOutputHandler(&outputHandler);
  1579. nvtt::Compressor compressor;
  1580. if (!compressor.process(io, co, oo))
  1581. BS_EXCEPT(InternalErrorException, "Mipmap generation failed.");
  1582. argbData.freeInternalBuffer();
  1583. Vector<PixelDataPtr> outputMipBuffers;
  1584. for (UINT32 i = 0; i < (UINT32)argbMipBuffers.size(); i++)
  1585. {
  1586. PixelDataPtr argbBuffer = argbMipBuffers[i];
  1587. PixelDataPtr outputBuffer = bs_shared_ptr_new<PixelData>(argbBuffer->getWidth(), argbBuffer->getHeight(), 1, src.getFormat());
  1588. outputBuffer->allocateInternalBuffer();
  1589. bulkPixelConversion(*argbBuffer, *outputBuffer);
  1590. argbBuffer->freeInternalBuffer();
  1591. outputMipBuffers.push_back(outputBuffer);
  1592. }
  1593. return outputMipBuffers;
  1594. }
  1595. }