BsPixelUtil.cpp 72 KB

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