BsPixelUtil.cpp 57 KB

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