BsPixelUtil.cpp 59 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790
  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. return 0;
  922. }
  923. void PixelUtil::getSizeForMipLevel(UINT32 width, UINT32 height, UINT32 depth, UINT32 mipLevel,
  924. UINT32& mipWidth, UINT32& mipHeight, UINT32& mipDepth)
  925. {
  926. mipWidth = width;
  927. mipHeight = height;
  928. mipDepth = depth;
  929. for (UINT32 i = 0; i < mipLevel; i++)
  930. {
  931. if (mipWidth != 1) mipWidth /= 2;
  932. if (mipHeight != 1) mipHeight /= 2;
  933. if (mipDepth != 1) mipDepth /= 2;
  934. }
  935. }
  936. UINT32 PixelUtil::getNumElemBits(PixelFormat format)
  937. {
  938. return getDescriptionFor(format).elemBytes * 8;
  939. }
  940. UINT32 PixelUtil::getFlags(PixelFormat format)
  941. {
  942. return getDescriptionFor(format).flags;
  943. }
  944. bool PixelUtil::hasAlpha(PixelFormat format)
  945. {
  946. return (PixelUtil::getFlags(format) & PFF_HASALPHA) > 0;
  947. }
  948. bool PixelUtil::isFloatingPoint(PixelFormat format)
  949. {
  950. return (PixelUtil::getFlags(format) & PFF_FLOAT) > 0;
  951. }
  952. bool PixelUtil::isCompressed(PixelFormat format)
  953. {
  954. return (PixelUtil::getFlags(format) & PFF_COMPRESSED) > 0;
  955. }
  956. bool PixelUtil::isDepth(PixelFormat format)
  957. {
  958. return (PixelUtil::getFlags(format) & PFF_DEPTH) > 0;
  959. }
  960. bool PixelUtil::isNativeEndian(PixelFormat format)
  961. {
  962. return (PixelUtil::getFlags(format) & PFF_NATIVEENDIAN) > 0;
  963. }
  964. bool PixelUtil::isValidExtent(UINT32 width, UINT32 height, UINT32 depth, PixelFormat format)
  965. {
  966. if(isCompressed(format))
  967. {
  968. switch(format)
  969. {
  970. case PF_BC1:
  971. case PF_BC2:
  972. case PF_BC1a:
  973. case PF_BC3:
  974. case PF_BC4:
  975. case PF_BC5:
  976. case PF_BC6H:
  977. case PF_BC7:
  978. return ((width & 3) == 0 && (height & 3) == 0 && depth == 1);
  979. default:
  980. return true;
  981. }
  982. }
  983. else
  984. {
  985. return true;
  986. }
  987. }
  988. void PixelUtil::getBitDepths(PixelFormat format, int rgba[4])
  989. {
  990. const PixelFormatDescription& des = getDescriptionFor(format);
  991. rgba[0] = des.rbits;
  992. rgba[1] = des.gbits;
  993. rgba[2] = des.bbits;
  994. rgba[3] = des.abits;
  995. }
  996. void PixelUtil::getBitMasks(PixelFormat format, UINT32 rgba[4])
  997. {
  998. const PixelFormatDescription& des = getDescriptionFor(format);
  999. rgba[0] = des.rmask;
  1000. rgba[1] = des.gmask;
  1001. rgba[2] = des.bmask;
  1002. rgba[3] = des.amask;
  1003. }
  1004. void PixelUtil::getBitShifts(PixelFormat format, UINT8 rgba[4])
  1005. {
  1006. const PixelFormatDescription& des = getDescriptionFor(format);
  1007. rgba[0] = des.rshift;
  1008. rgba[1] = des.gshift;
  1009. rgba[2] = des.bshift;
  1010. rgba[3] = des.ashift;
  1011. }
  1012. String PixelUtil::getFormatName(PixelFormat srcformat)
  1013. {
  1014. return getDescriptionFor(srcformat).name;
  1015. }
  1016. bool PixelUtil::isAccessible(PixelFormat srcformat)
  1017. {
  1018. if (srcformat == PF_UNKNOWN)
  1019. return false;
  1020. UINT32 flags = getFlags(srcformat);
  1021. return !((flags & PFF_COMPRESSED) || (flags & PFF_DEPTH));
  1022. }
  1023. PixelComponentType PixelUtil::getElementType(PixelFormat format)
  1024. {
  1025. const PixelFormatDescription& des = getDescriptionFor(format);
  1026. return des.componentType;
  1027. }
  1028. UINT32 PixelUtil::getNumElements(PixelFormat format)
  1029. {
  1030. const PixelFormatDescription& des = getDescriptionFor(format);
  1031. return des.componentCount;
  1032. }
  1033. UINT32 PixelUtil::getMaxMipmaps(UINT32 width, UINT32 height, UINT32 depth, PixelFormat format)
  1034. {
  1035. UINT32 count = 0;
  1036. if((width > 0) && (height > 0))
  1037. {
  1038. do {
  1039. if(width>1) width = width/2;
  1040. if(height>1) height = height/2;
  1041. if(depth>1) depth = depth/2;
  1042. count ++;
  1043. } while(!(width == 1 && height == 1 && depth == 1));
  1044. }
  1045. return count;
  1046. }
  1047. void PixelUtil::packColor(const Color& color, PixelFormat format, void* dest)
  1048. {
  1049. packColor(color.r, color.g, color.b, color.a, format, dest);
  1050. }
  1051. void PixelUtil::packColor(UINT8 r, UINT8 g, UINT8 b, UINT8 a, PixelFormat format, void* dest)
  1052. {
  1053. const PixelFormatDescription &des = getDescriptionFor(format);
  1054. if(des.flags & PFF_NATIVEENDIAN)
  1055. {
  1056. // Shortcut for integer formats packing
  1057. UINT32 value = ((Bitwise::fixedToFixed(r, 8, des.rbits)<<des.rshift) & des.rmask) |
  1058. ((Bitwise::fixedToFixed(g, 8, des.gbits)<<des.gshift) & des.gmask) |
  1059. ((Bitwise::fixedToFixed(b, 8, des.bbits)<<des.bshift) & des.bmask) |
  1060. ((Bitwise::fixedToFixed(a, 8, des.abits)<<des.ashift) & des.amask);
  1061. // And write to memory
  1062. Bitwise::intWrite(dest, des.elemBytes, value);
  1063. }
  1064. else
  1065. {
  1066. // Convert to float
  1067. packColor((float)r/255.0f,(float)g/255.0f,(float)b/255.0f,(float)a/255.0f, format, dest);
  1068. }
  1069. }
  1070. void PixelUtil::packColor(float r, float g, float b, float a, const PixelFormat format, void* dest)
  1071. {
  1072. const PixelFormatDescription& des = getDescriptionFor(format);
  1073. if(des.flags & PFF_NATIVEENDIAN)
  1074. {
  1075. // Do the packing
  1076. const unsigned int value = ((Bitwise::floatToFixed(r, des.rbits)<<des.rshift) & des.rmask) |
  1077. ((Bitwise::floatToFixed(g, des.gbits)<<des.gshift) & des.gmask) |
  1078. ((Bitwise::floatToFixed(b, des.bbits)<<des.bshift) & des.bmask) |
  1079. ((Bitwise::floatToFixed(a, des.abits)<<des.ashift) & des.amask);
  1080. // And write to memory
  1081. Bitwise::intWrite(dest, des.elemBytes, value);
  1082. }
  1083. else
  1084. {
  1085. switch(format)
  1086. {
  1087. case PF_FLOAT32_R:
  1088. ((float*)dest)[0] = r;
  1089. break;
  1090. case PF_FLOAT32_RG:
  1091. ((float*)dest)[0] = r;
  1092. ((float*)dest)[1] = g;
  1093. break;
  1094. case PF_FLOAT32_RGB:
  1095. ((float*)dest)[0] = r;
  1096. ((float*)dest)[1] = g;
  1097. ((float*)dest)[2] = b;
  1098. break;
  1099. case PF_FLOAT32_RGBA:
  1100. ((float*)dest)[0] = r;
  1101. ((float*)dest)[1] = g;
  1102. ((float*)dest)[2] = b;
  1103. ((float*)dest)[3] = a;
  1104. break;
  1105. case PF_FLOAT16_R:
  1106. ((UINT16*)dest)[0] = Bitwise::floatToHalf(r);
  1107. break;
  1108. case PF_FLOAT16_RG:
  1109. ((UINT16*)dest)[0] = Bitwise::floatToHalf(r);
  1110. ((UINT16*)dest)[1] = Bitwise::floatToHalf(g);
  1111. break;
  1112. case PF_FLOAT16_RGB:
  1113. ((UINT16*)dest)[0] = Bitwise::floatToHalf(r);
  1114. ((UINT16*)dest)[1] = Bitwise::floatToHalf(g);
  1115. ((UINT16*)dest)[2] = Bitwise::floatToHalf(b);
  1116. break;
  1117. case PF_FLOAT16_RGBA:
  1118. ((UINT16*)dest)[0] = Bitwise::floatToHalf(r);
  1119. ((UINT16*)dest)[1] = Bitwise::floatToHalf(g);
  1120. ((UINT16*)dest)[2] = Bitwise::floatToHalf(b);
  1121. ((UINT16*)dest)[3] = Bitwise::floatToHalf(a);
  1122. break;
  1123. case PF_R8G8:
  1124. ((UINT8*)dest)[0] = (UINT8)Bitwise::floatToFixed(r, 8);
  1125. ((UINT8*)dest)[1] = (UINT8)Bitwise::floatToFixed(g, 8);
  1126. break;
  1127. case PF_R8:
  1128. ((UINT8*)dest)[0] = (UINT8)Bitwise::floatToFixed(r, 8);
  1129. break;
  1130. default:
  1131. BS_EXCEPT(NotImplementedException, "Pack to " + getFormatName(format) + " not implemented");
  1132. break;
  1133. }
  1134. }
  1135. }
  1136. void PixelUtil::unpackColor(Color* color, PixelFormat format, const void* src)
  1137. {
  1138. unpackColor(&color->r, &color->g, &color->b, &color->a, format, src);
  1139. }
  1140. void PixelUtil::unpackColor(UINT8* r, UINT8* g, UINT8* b, UINT8* a, PixelFormat format, const void* src)
  1141. {
  1142. const PixelFormatDescription &des = getDescriptionFor(format);
  1143. if(des.flags & PFF_NATIVEENDIAN)
  1144. {
  1145. // Shortcut for integer formats unpacking
  1146. const UINT32 value = Bitwise::intRead(src, des.elemBytes);
  1147. *r = (UINT8)Bitwise::fixedToFixed((value & des.rmask)>>des.rshift, des.rbits, 8);
  1148. *g = (UINT8)Bitwise::fixedToFixed((value & des.gmask)>>des.gshift, des.gbits, 8);
  1149. *b = (UINT8)Bitwise::fixedToFixed((value & des.bmask)>>des.bshift, des.bbits, 8);
  1150. if(des.flags & PFF_HASALPHA)
  1151. {
  1152. *a = (UINT8)Bitwise::fixedToFixed((value & des.amask)>>des.ashift, des.abits, 8);
  1153. }
  1154. else
  1155. {
  1156. *a = 255; // No alpha, default a component to full
  1157. }
  1158. }
  1159. else
  1160. {
  1161. // Do the operation with the more generic floating point
  1162. float rr, gg, bb, aa;
  1163. unpackColor(&rr,&gg,&bb,&aa, format, src);
  1164. *r = (UINT8)Bitwise::floatToFixed(rr, 8);
  1165. *g = (UINT8)Bitwise::floatToFixed(gg, 8);
  1166. *b = (UINT8)Bitwise::floatToFixed(bb, 8);
  1167. *a = (UINT8)Bitwise::floatToFixed(aa, 8);
  1168. }
  1169. }
  1170. void PixelUtil::unpackColor(float* r, float* g, float* b, float* a, PixelFormat format, const void* src)
  1171. {
  1172. const PixelFormatDescription &des = getDescriptionFor(format);
  1173. if(des.flags & PFF_NATIVEENDIAN)
  1174. {
  1175. // Shortcut for integer formats unpacking
  1176. const unsigned int value = Bitwise::intRead(src, des.elemBytes);
  1177. *r = Bitwise::fixedToFloat((value & des.rmask)>>des.rshift, des.rbits);
  1178. *g = Bitwise::fixedToFloat((value & des.gmask)>>des.gshift, des.gbits);
  1179. *b = Bitwise::fixedToFloat((value & des.bmask)>>des.bshift, des.bbits);
  1180. if(des.flags & PFF_HASALPHA)
  1181. {
  1182. *a = Bitwise::fixedToFloat((value & des.amask)>>des.ashift, des.abits);
  1183. }
  1184. else
  1185. {
  1186. *a = 1.0f; // No alpha, default a component to full
  1187. }
  1188. }
  1189. else
  1190. {
  1191. switch(format)
  1192. {
  1193. case PF_FLOAT32_R:
  1194. *r = *g = *b = ((float*)src)[0];
  1195. *a = 1.0f;
  1196. break;
  1197. case PF_FLOAT32_RG:
  1198. *r = ((float*)src)[0];
  1199. *g = *b = ((float*)src)[1];
  1200. *a = 1.0f;
  1201. break;
  1202. case PF_FLOAT32_RGB:
  1203. *r = ((float*)src)[0];
  1204. *g = ((float*)src)[1];
  1205. *b = ((float*)src)[2];
  1206. *a = 1.0f;
  1207. break;
  1208. case PF_FLOAT32_RGBA:
  1209. *r = ((float*)src)[0];
  1210. *g = ((float*)src)[1];
  1211. *b = ((float*)src)[2];
  1212. *a = ((float*)src)[3];
  1213. break;
  1214. case PF_FLOAT16_R:
  1215. *r = *g = *b = Bitwise::halfToFloat(((UINT16*)src)[0]);
  1216. *a = 1.0f;
  1217. break;
  1218. case PF_FLOAT16_RG:
  1219. *r = Bitwise::halfToFloat(((UINT16*)src)[0]);
  1220. *g = *b = Bitwise::halfToFloat(((UINT16*)src)[1]);
  1221. *a = 1.0f;
  1222. break;
  1223. case PF_FLOAT16_RGB:
  1224. *r = Bitwise::halfToFloat(((UINT16*)src)[0]);
  1225. *g = Bitwise::halfToFloat(((UINT16*)src)[1]);
  1226. *b = Bitwise::halfToFloat(((UINT16*)src)[2]);
  1227. *a = 1.0f;
  1228. break;
  1229. case PF_FLOAT16_RGBA:
  1230. *r = Bitwise::halfToFloat(((UINT16*)src)[0]);
  1231. *g = Bitwise::halfToFloat(((UINT16*)src)[1]);
  1232. *b = Bitwise::halfToFloat(((UINT16*)src)[2]);
  1233. *a = Bitwise::halfToFloat(((UINT16*)src)[3]);
  1234. break;
  1235. case PF_R8G8:
  1236. *r = Bitwise::fixedToFloat(((UINT8*)src)[0], 8);
  1237. *g = Bitwise::fixedToFloat(((UINT8*)src)[1], 8);
  1238. *b = 0.0f;
  1239. *a = 1.0f;
  1240. break;
  1241. case PF_R8:
  1242. *r = Bitwise::fixedToFloat(((UINT8*)src)[0], 8);
  1243. *g = 0.0f;
  1244. *b = 0.0f;
  1245. *a = 1.0f;
  1246. break;
  1247. default:
  1248. BS_EXCEPT(NotImplementedException, "Unpack from " + getFormatName(format) + " not implemented");
  1249. break;
  1250. }
  1251. }
  1252. }
  1253. void PixelUtil::bulkPixelConversion(const PixelData &src, PixelData &dst)
  1254. {
  1255. assert(src.getWidth() == dst.getWidth() &&
  1256. src.getHeight() == dst.getHeight() &&
  1257. src.getDepth() == dst.getDepth());
  1258. // Check for compressed formats, we don't support decompression
  1259. if(PixelUtil::isCompressed(src.getFormat()))
  1260. {
  1261. if(src.getFormat() == dst.getFormat())
  1262. {
  1263. memcpy(dst.getData(), src.getData(), src.getConsecutiveSize());
  1264. return;
  1265. }
  1266. else
  1267. {
  1268. BS_EXCEPT(NotImplementedException, "This method can not be used to compress or decompress images");
  1269. }
  1270. }
  1271. // Check for compression
  1272. if (PixelUtil::isCompressed(dst.getFormat()))
  1273. {
  1274. if (src.getFormat() == dst.getFormat())
  1275. {
  1276. memcpy(dst.getData(), src.getData(), src.getConsecutiveSize());
  1277. return;
  1278. }
  1279. else
  1280. {
  1281. CompressionOptions co;
  1282. co.format = dst.getFormat();
  1283. compress(src, dst, co);
  1284. return;
  1285. }
  1286. }
  1287. // The easy case
  1288. if(src.getFormat() == dst.getFormat())
  1289. {
  1290. // Everything consecutive?
  1291. if(src.isConsecutive() && dst.isConsecutive())
  1292. {
  1293. memcpy(dst.getData(), src.getData(), src.getConsecutiveSize());
  1294. return;
  1295. }
  1296. const UINT32 srcPixelSize = PixelUtil::getNumElemBytes(src.getFormat());
  1297. const UINT32 dstPixelSize = PixelUtil::getNumElemBytes(dst.getFormat());
  1298. UINT8 *srcptr = static_cast<UINT8*>(src.getData())
  1299. + (src.getLeft() + src.getTop() * src.getRowPitch() + src.getFront() * src.getSlicePitch()) * srcPixelSize;
  1300. UINT8 *dstptr = static_cast<UINT8*>(dst.getData())
  1301. + (dst.getLeft() + dst.getTop() * dst.getRowPitch() + dst.getFront() * dst.getSlicePitch()) * dstPixelSize;
  1302. // Calculate pitches+skips in bytes
  1303. const UINT32 srcRowPitchBytes = src.getRowPitch()*srcPixelSize;
  1304. const UINT32 srcSliceSkipBytes = src.getSliceSkip()*srcPixelSize;
  1305. const UINT32 dstRowPitchBytes = dst.getRowPitch()*dstPixelSize;
  1306. const UINT32 dstSliceSkipBytes = dst.getSliceSkip()*dstPixelSize;
  1307. // Otherwise, copy per row
  1308. const UINT32 rowSize = src.getWidth()*srcPixelSize;
  1309. for (UINT32 z = src.getFront(); z < src.getBack(); z++)
  1310. {
  1311. for(UINT32 y = src.getTop(); y < src.getBottom(); y++)
  1312. {
  1313. memcpy(dstptr, srcptr, rowSize);
  1314. srcptr += srcRowPitchBytes;
  1315. dstptr += dstRowPitchBytes;
  1316. }
  1317. srcptr += srcSliceSkipBytes;
  1318. dstptr += dstSliceSkipBytes;
  1319. }
  1320. return;
  1321. }
  1322. // Converting to PF_X8R8G8B8 is exactly the same as converting to
  1323. // PF_A8R8G8B8. (same with PF_X8B8G8R8 and PF_A8B8G8R8)
  1324. if(dst.getFormat() == PF_X8R8G8B8 || dst.getFormat() == PF_X8B8G8R8)
  1325. {
  1326. // Do the same conversion, with PF_A8R8G8B8, which has a lot of
  1327. // optimized conversions
  1328. PixelFormat tempFormat = dst.getFormat() == PF_X8R8G8B8?PF_A8R8G8B8:PF_A8B8G8R8;
  1329. PixelData tempdst(dst.getWidth(), dst.getHeight(), dst.getDepth(), tempFormat);
  1330. bulkPixelConversion(src, tempdst);
  1331. return;
  1332. }
  1333. // Converting from PF_X8R8G8B8 is exactly the same as converting from
  1334. // PF_A8R8G8B8, given that the destination format does not have alpha.
  1335. if((src.getFormat() == PF_X8R8G8B8 || src.getFormat() == PF_X8B8G8R8) && !hasAlpha(dst.getFormat()))
  1336. {
  1337. // Do the same conversion, with PF_A8R8G8B8, which has a lot of
  1338. // optimized conversions
  1339. PixelFormat tempFormat = src.getFormat()==PF_X8R8G8B8?PF_A8R8G8B8:PF_A8B8G8R8;
  1340. PixelData tempsrc(src.getWidth(), src.getHeight(), src.getDepth(), tempFormat);
  1341. tempsrc.setExternalBuffer(src.getData());
  1342. bulkPixelConversion(tempsrc, dst);
  1343. return;
  1344. }
  1345. const UINT32 srcPixelSize = PixelUtil::getNumElemBytes(src.getFormat());
  1346. const UINT32 dstPixelSize = PixelUtil::getNumElemBytes(dst.getFormat());
  1347. UINT8 *srcptr = static_cast<UINT8*>(src.getData())
  1348. + (src.getLeft() + src.getTop() * src.getRowPitch() + src.getFront() * src.getSlicePitch()) * srcPixelSize;
  1349. UINT8 *dstptr = static_cast<UINT8*>(dst.getData())
  1350. + (dst.getLeft() + dst.getTop() * dst.getRowPitch() + dst.getFront() * dst.getSlicePitch()) * dstPixelSize;
  1351. // Calculate pitches+skips in bytes
  1352. const UINT32 srcRowSkipBytes = src.getRowSkip()*srcPixelSize;
  1353. const UINT32 srcSliceSkipBytes = src.getSliceSkip()*srcPixelSize;
  1354. const UINT32 dstRowSkipBytes = dst.getRowSkip()*dstPixelSize;
  1355. const UINT32 dstSliceSkipBytes = dst.getSliceSkip()*dstPixelSize;
  1356. // The brute force fallback
  1357. float r,g,b,a;
  1358. for (UINT32 z = src.getFront(); z<src.getBack(); z++)
  1359. {
  1360. for (UINT32 y = src.getTop(); y < src.getBottom(); y++)
  1361. {
  1362. for (UINT32 x = src.getLeft(); x<src.getRight(); x++)
  1363. {
  1364. unpackColor(&r, &g, &b, &a, src.getFormat(), srcptr);
  1365. packColor(r, g, b, a, dst.getFormat(), dstptr);
  1366. srcptr += srcPixelSize;
  1367. dstptr += dstPixelSize;
  1368. }
  1369. srcptr += srcRowSkipBytes;
  1370. dstptr += dstRowSkipBytes;
  1371. }
  1372. srcptr += srcSliceSkipBytes;
  1373. dstptr += dstSliceSkipBytes;
  1374. }
  1375. }
  1376. void PixelUtil::scale(const PixelData& src, PixelData& scaled, Filter filter)
  1377. {
  1378. assert(PixelUtil::isAccessible(src.getFormat()));
  1379. assert(PixelUtil::isAccessible(scaled.getFormat()));
  1380. PixelData temp;
  1381. switch (filter)
  1382. {
  1383. default:
  1384. case FILTER_NEAREST:
  1385. if(src.getFormat() == scaled.getFormat())
  1386. {
  1387. // No intermediate buffer needed
  1388. temp = scaled;
  1389. }
  1390. else
  1391. {
  1392. // Allocate temporary buffer of destination size in source format
  1393. temp = PixelData(scaled.getWidth(), scaled.getHeight(), scaled.getDepth(), src.getFormat());
  1394. temp.allocateInternalBuffer();
  1395. }
  1396. // No conversion
  1397. switch (PixelUtil::getNumElemBytes(src.getFormat()))
  1398. {
  1399. case 1: NearestResampler<1>::scale(src, temp); break;
  1400. case 2: NearestResampler<2>::scale(src, temp); break;
  1401. case 3: NearestResampler<3>::scale(src, temp); break;
  1402. case 4: NearestResampler<4>::scale(src, temp); break;
  1403. case 6: NearestResampler<6>::scale(src, temp); break;
  1404. case 8: NearestResampler<8>::scale(src, temp); break;
  1405. case 12: NearestResampler<12>::scale(src, temp); break;
  1406. case 16: NearestResampler<16>::scale(src, temp); break;
  1407. default:
  1408. // Never reached
  1409. assert(false);
  1410. }
  1411. if(temp.getData() != scaled.getData())
  1412. {
  1413. // Blit temp buffer
  1414. PixelUtil::bulkPixelConversion(temp, scaled);
  1415. temp.freeInternalBuffer();
  1416. }
  1417. break;
  1418. case FILTER_LINEAR:
  1419. switch (src.getFormat())
  1420. {
  1421. case PF_R8G8:
  1422. case PF_R8G8B8: case PF_B8G8R8:
  1423. case PF_R8G8B8A8: case PF_B8G8R8A8:
  1424. case PF_A8B8G8R8: case PF_A8R8G8B8:
  1425. case PF_X8B8G8R8: case PF_X8R8G8B8:
  1426. if(src.getFormat() == scaled.getFormat())
  1427. {
  1428. // No intermediate buffer needed
  1429. temp = scaled;
  1430. }
  1431. else
  1432. {
  1433. // Allocate temp buffer of destination size in source format
  1434. temp = PixelData(scaled.getWidth(), scaled.getHeight(), scaled.getDepth(), src.getFormat());
  1435. temp.allocateInternalBuffer();
  1436. }
  1437. // No conversion
  1438. switch (PixelUtil::getNumElemBytes(src.getFormat()))
  1439. {
  1440. case 1: LinearResampler_Byte<1>::scale(src, temp); break;
  1441. case 2: LinearResampler_Byte<2>::scale(src, temp); break;
  1442. case 3: LinearResampler_Byte<3>::scale(src, temp); break;
  1443. case 4: LinearResampler_Byte<4>::scale(src, temp); break;
  1444. default:
  1445. // Never reached
  1446. assert(false);
  1447. }
  1448. if(temp.getData() != scaled.getData())
  1449. {
  1450. // Blit temp buffer
  1451. PixelUtil::bulkPixelConversion(temp, scaled);
  1452. temp.freeInternalBuffer();
  1453. }
  1454. break;
  1455. case PF_FLOAT32_RGB:
  1456. case PF_FLOAT32_RGBA:
  1457. if (scaled.getFormat() == PF_FLOAT32_RGB || scaled.getFormat() == PF_FLOAT32_RGBA)
  1458. {
  1459. // float32 to float32, avoid unpack/repack overhead
  1460. LinearResampler_Float32::scale(src, scaled);
  1461. break;
  1462. }
  1463. // Else, fall through
  1464. default:
  1465. // Fallback case, slow but works
  1466. LinearResampler::scale(src, scaled);
  1467. }
  1468. break;
  1469. }
  1470. }
  1471. void PixelUtil::applyGamma(UINT8* buffer, float gamma, UINT32 size, UINT8 bpp)
  1472. {
  1473. if(gamma == 1.0f)
  1474. return;
  1475. UINT32 stride = bpp >> 3;
  1476. for(size_t i = 0, j = size / stride; i < j; i++, buffer += stride)
  1477. {
  1478. float r = (float)buffer[0];
  1479. float g = (float)buffer[1];
  1480. float b = (float)buffer[2];
  1481. r = r * gamma;
  1482. g = g * gamma;
  1483. b = b * gamma;
  1484. float scale = 1.0f;
  1485. float tmp = 0.0f;
  1486. if(r > 255.0f && (tmp=(255.0f/r)) < scale)
  1487. scale = tmp;
  1488. if(g > 255.0f && (tmp=(255.0f/g)) < scale)
  1489. scale = tmp;
  1490. if(b > 255.0f && (tmp=(255.0f/b)) < scale)
  1491. scale = tmp;
  1492. r *= scale;
  1493. g *= scale;
  1494. b *= scale;
  1495. buffer[0] = (UINT8)r;
  1496. buffer[1] = (UINT8)g;
  1497. buffer[2] = (UINT8)b;
  1498. }
  1499. }
  1500. void PixelUtil::compress(const PixelData& src, PixelData& dst, const CompressionOptions& options)
  1501. {
  1502. if (!isCompressed(options.format))
  1503. BS_EXCEPT(InvalidParametersException, "Wanted format is not a compressed format.");
  1504. // Note: NVTT site has implementations for these two formats for when I decide to add them
  1505. if (options.format == PF_BC6H || options.format == PF_BC7)
  1506. BS_EXCEPT(InvalidParametersException, "Specified formats are not yet supported.");
  1507. if (src.getDepth() != 1)
  1508. BS_EXCEPT(InvalidParametersException, "3D textures are not supported.");
  1509. PixelFormat pf = options.format;
  1510. if (isCompressed(src.getFormat()))
  1511. BS_EXCEPT(InvalidParametersException, "Source data cannot be compressed.");
  1512. PixelData bgraData(src.getWidth(), src.getHeight(), 1, PF_B8G8R8A8);
  1513. bgraData.allocateInternalBuffer();
  1514. bulkPixelConversion(src, bgraData);
  1515. nvtt::InputOptions io;
  1516. io.setTextureLayout(nvtt::TextureType_2D, src.getWidth(), src.getHeight());
  1517. io.setMipmapData(bgraData.getData(), src.getWidth(), src.getHeight());
  1518. io.setMipmapGeneration(false);
  1519. io.setAlphaMode(toNVTTAlphaMode(options.alphaMode));
  1520. io.setNormalMap(options.isNormalMap);
  1521. if (options.isSRGB)
  1522. io.setGamma(2.2f, 2.2f);
  1523. else
  1524. io.setGamma(1.0f, 1.0f);
  1525. nvtt::CompressionOptions co;
  1526. co.setFormat(toNVTTFormat(options.format));
  1527. co.setQuality(toNVTTQuality(options.quality));
  1528. NVTTCompressOutputHandler outputHandler(dst.getData(), dst.getConsecutiveSize());
  1529. nvtt::OutputOptions oo;
  1530. oo.setOutputHeader(false);
  1531. oo.setOutputHandler(&outputHandler);
  1532. nvtt::Compressor compressor;
  1533. if (!compressor.process(io, co, oo))
  1534. BS_EXCEPT(InternalErrorException, "Compressing failed.");
  1535. }
  1536. Vector<PixelDataPtr> PixelUtil::genMipmaps(const PixelData& src, const MipMapGenOptions& options)
  1537. {
  1538. if (src.getDepth() != 1)
  1539. BS_EXCEPT(InvalidParametersException, "3D textures are not supported.");
  1540. // Note: Add support for floating point mips, no reason they shouldn't be supported other than
  1541. // nvtt doesn't support them natively
  1542. if (isCompressed(src.getFormat()) || isFloatingPoint(src.getFormat()))
  1543. BS_EXCEPT(InvalidParametersException, "Source data cannot be compressed or in floating point format.");
  1544. if (!Math::isPow2(src.getWidth()) || !Math::isPow2(src.getHeight()))
  1545. BS_EXCEPT(InvalidParametersException, "Texture width & height must be powers of 2.");
  1546. PixelData argbData(src.getWidth(), src.getHeight(), 1, PF_A8R8G8B8);
  1547. argbData.allocateInternalBuffer();
  1548. bulkPixelConversion(src, argbData);
  1549. nvtt::InputOptions io;
  1550. io.setTextureLayout(nvtt::TextureType_2D, src.getWidth(), src.getHeight());
  1551. io.setMipmapData(argbData.getData(), src.getWidth(), src.getHeight());
  1552. io.setMipmapGeneration(true);
  1553. io.setNormalMap(options.isNormalMap);
  1554. io.setNormalizeMipmaps(options.normalizeMipmaps);
  1555. io.setWrapMode(toNVTTWrapMode(options.wrapMode));
  1556. nvtt::CompressionOptions co;
  1557. co.setFormat(nvtt::Format_RGBA);
  1558. UINT32 numMips = getMaxMipmaps(src.getWidth(), src.getHeight(), 1, src.getFormat());
  1559. Vector<PixelDataPtr> argbMipBuffers;
  1560. // Note: This can be done more effectively without creating so many temp buffers
  1561. // and working with the original formats directly, but it would complicate the code
  1562. // too much at the moment.
  1563. UINT32 curWidth = src.getWidth();
  1564. UINT32 curHeight = src.getHeight();
  1565. for (UINT32 i = 0; i < numMips; i++)
  1566. {
  1567. argbMipBuffers.push_back(bs_shared_ptr_new<PixelData>(curWidth, curHeight, 1, PF_A8R8G8B8));
  1568. argbMipBuffers.back()->allocateInternalBuffer();
  1569. if (curWidth > 1)
  1570. curWidth = curWidth / 2;
  1571. if (curHeight > 1)
  1572. curHeight = curHeight / 2;
  1573. }
  1574. argbMipBuffers.push_back(bs_shared_ptr_new<PixelData>(curWidth, curHeight, 1, PF_A8R8G8B8));
  1575. argbMipBuffers.back()->allocateInternalBuffer();
  1576. NVTTMipmapOutputHandler outputHandler(argbMipBuffers);
  1577. nvtt::OutputOptions oo;
  1578. oo.setOutputHeader(false);
  1579. oo.setOutputHandler(&outputHandler);
  1580. nvtt::Compressor compressor;
  1581. if (!compressor.process(io, co, oo))
  1582. BS_EXCEPT(InternalErrorException, "Mipmap generation failed.");
  1583. argbData.freeInternalBuffer();
  1584. Vector<PixelDataPtr> outputMipBuffers;
  1585. for (UINT32 i = 0; i < (UINT32)argbMipBuffers.size(); i++)
  1586. {
  1587. PixelDataPtr argbBuffer = argbMipBuffers[i];
  1588. PixelDataPtr outputBuffer = bs_shared_ptr_new<PixelData>(argbBuffer->getWidth(), argbBuffer->getHeight(), 1, src.getFormat());
  1589. outputBuffer->allocateInternalBuffer();
  1590. bulkPixelConversion(*argbBuffer, *outputBuffer);
  1591. argbBuffer->freeInternalBuffer();
  1592. outputMipBuffers.push_back(outputBuffer);
  1593. }
  1594. return outputMipBuffers;
  1595. }
  1596. }