BsPixelUtil.cpp 72 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191219221932194219521962197219821992200220122022203220422052206220722082209221022112212221322142215221622172218221922202221222222232224222522262227222822292230223122322233223422352236223722382239224022412242224322442245224622472248224922502251225222532254225522562257225822592260226122622263226422652266226722682269227022712272227322742275227622772278227922802281228222832284228522862287228822892290229122922293229422952296229722982299230023012302230323042305230623072308230923102311231223132314231523162317231823192320232123222323232423252326232723282329233023312332233323342335233623372338233923402341234223432344234523462347234823492350235123522353235423552356235723582359236023612362236323642365236623672368236923702371237223732374237523762377237823792380238123822383238423852386238723882389239023912392239323942395239623972398239924002401240224032404240524062407240824092410241124122413241424152416241724182419242024212422242324242425242624272428242924302431243224332434243524362437243824392440244124422443244424452446244724482449245024512452245324542455245624572458245924602461246224632464246524662467246824692470247124722473247424752476247724782479248024812482248324842485248624872488248924902491249224932494249524962497249824992500250125022503250425052506250725082509251025112512251325142515251625172518251925202521252225232524252525262527252825292530253125322533253425352536253725382539254025412542254325442545254625472548254925502551255225532554255525562557
  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. virtual void beginImage(int size, int width, int height, int depth, int face, int miplevel)
  1141. {
  1142. assert(miplevel >= 0 && miplevel < (int)buffers.size());
  1143. assert(size == buffers[miplevel]->getConsecutiveSize());
  1144. activeBuffer = buffers[miplevel];
  1145. bufferWritePos = activeBuffer->getData();
  1146. bufferEnd = bufferWritePos + activeBuffer->getConsecutiveSize();
  1147. }
  1148. virtual bool writeData(const void* data, int size)
  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::isDepth(PixelFormat format)
  1321. {
  1322. return (PixelUtil::getFlags(format) & PFF_DEPTH) > 0;
  1323. }
  1324. bool PixelUtil::checkFormat(PixelFormat& format, TextureType texType, int usage)
  1325. {
  1326. // First check just the usage since it's the most limiting factor
  1327. //// Depth-stencil only supports depth formats
  1328. if ((usage & TU_DEPTHSTENCIL) != 0)
  1329. {
  1330. if (isDepth(format))
  1331. return true;
  1332. format = PF_D32_S8X24;
  1333. return false;
  1334. }
  1335. //// Render targets support everything but compressed & depth-stencil formats
  1336. if ((usage & TU_RENDERTARGET) != 0)
  1337. {
  1338. if (!isDepth(format) && !isCompressed(format))
  1339. return true;
  1340. format = PF_RGBA8;
  1341. return false;
  1342. }
  1343. //// Load-store textures support everything but compressed & depth-stencil formats
  1344. if ((usage & TU_LOADSTORE) != 0)
  1345. {
  1346. if (!isDepth(format) && !isCompressed(format))
  1347. return true;
  1348. format = PF_RGBA8;
  1349. return false;
  1350. }
  1351. //// Sampled texture support depends on texture type
  1352. switch (texType)
  1353. {
  1354. case TEX_TYPE_1D:
  1355. {
  1356. // 1D textures support anything but depth & compressed formats
  1357. if (!isDepth(format) && !isCompressed(format))
  1358. return true;
  1359. format = PF_RGBA8;
  1360. return false;
  1361. }
  1362. case TEX_TYPE_3D:
  1363. {
  1364. // 3D textures support anything but depth & compressed formats
  1365. if (!isDepth(format))
  1366. return true;
  1367. format = PF_RGBA8;
  1368. return false;
  1369. }
  1370. default: // 2D & cube
  1371. {
  1372. // 2D/cube textures support anything but depth formats
  1373. if (!isDepth(format))
  1374. return true;
  1375. format = PF_RGBA8;
  1376. return false;
  1377. }
  1378. }
  1379. }
  1380. bool PixelUtil::isValidExtent(UINT32 width, UINT32 height, UINT32 depth, PixelFormat format)
  1381. {
  1382. if(isCompressed(format))
  1383. {
  1384. switch(format)
  1385. {
  1386. case PF_BC1:
  1387. case PF_BC2:
  1388. case PF_BC1a:
  1389. case PF_BC3:
  1390. case PF_BC4:
  1391. case PF_BC5:
  1392. case PF_BC6H:
  1393. case PF_BC7:
  1394. return ((width & 3) == 0 && (height & 3) == 0 && depth == 1);
  1395. default:
  1396. return true;
  1397. }
  1398. }
  1399. else
  1400. {
  1401. return true;
  1402. }
  1403. }
  1404. void PixelUtil::getBitDepths(PixelFormat format, int(&rgba)[4])
  1405. {
  1406. const PixelFormatDescription& des = getDescriptionFor(format);
  1407. rgba[0] = des.rbits;
  1408. rgba[1] = des.gbits;
  1409. rgba[2] = des.bbits;
  1410. rgba[3] = des.abits;
  1411. }
  1412. void PixelUtil::getBitMasks(PixelFormat format, UINT32(&rgba)[4])
  1413. {
  1414. const PixelFormatDescription& des = getDescriptionFor(format);
  1415. rgba[0] = des.rmask;
  1416. rgba[1] = des.gmask;
  1417. rgba[2] = des.bmask;
  1418. rgba[3] = des.amask;
  1419. }
  1420. void PixelUtil::getBitShifts(PixelFormat format, UINT8(&rgba)[4])
  1421. {
  1422. const PixelFormatDescription& des = getDescriptionFor(format);
  1423. rgba[0] = des.rshift;
  1424. rgba[1] = des.gshift;
  1425. rgba[2] = des.bshift;
  1426. rgba[3] = des.ashift;
  1427. }
  1428. String PixelUtil::getFormatName(PixelFormat srcformat)
  1429. {
  1430. return getDescriptionFor(srcformat).name;
  1431. }
  1432. bool PixelUtil::isAccessible(PixelFormat srcformat)
  1433. {
  1434. if (srcformat == PF_UNKNOWN)
  1435. return false;
  1436. UINT32 flags = getFlags(srcformat);
  1437. return !((flags & PFF_COMPRESSED) || (flags & PFF_DEPTH));
  1438. }
  1439. PixelComponentType PixelUtil::getElementType(PixelFormat format)
  1440. {
  1441. const PixelFormatDescription& des = getDescriptionFor(format);
  1442. return des.componentType;
  1443. }
  1444. UINT32 PixelUtil::getNumElements(PixelFormat format)
  1445. {
  1446. const PixelFormatDescription& des = getDescriptionFor(format);
  1447. return des.componentCount;
  1448. }
  1449. UINT32 PixelUtil::getMaxMipmaps(UINT32 width, UINT32 height, UINT32 depth, PixelFormat format)
  1450. {
  1451. UINT32 count = 0;
  1452. if ((width > 0) && (height > 0))
  1453. {
  1454. while (!(width == 1 && height == 1 && depth == 1))
  1455. {
  1456. if (width > 1) width = width / 2;
  1457. if (height > 1) height = height / 2;
  1458. if (depth > 1) depth = depth / 2;
  1459. count++;
  1460. }
  1461. }
  1462. return count;
  1463. }
  1464. void PixelUtil::packColor(const Color& color, PixelFormat format, void* dest)
  1465. {
  1466. packColor(color.r, color.g, color.b, color.a, format, dest);
  1467. }
  1468. void PixelUtil::packColor(UINT8 r, UINT8 g, UINT8 b, UINT8 a, PixelFormat format, void* dest)
  1469. {
  1470. const PixelFormatDescription &des = getDescriptionFor(format);
  1471. if (des.flags & PFF_INTEGER)
  1472. {
  1473. // Shortcut for integer formats packing
  1474. UINT32 value = ((Bitwise::fixedToFixed(r, 8, des.rbits) << des.rshift) & des.rmask) |
  1475. ((Bitwise::fixedToFixed(g, 8, des.gbits) << des.gshift) & des.gmask) |
  1476. ((Bitwise::fixedToFixed(b, 8, des.bbits) << des.bshift) & des.bmask) |
  1477. ((Bitwise::fixedToFixed(a, 8, des.abits) << des.ashift) & des.amask);
  1478. // And write to memory
  1479. Bitwise::intWrite(dest, des.elemBytes, value);
  1480. }
  1481. else
  1482. {
  1483. // Convert to float
  1484. packColor((float)r / 255.0f, (float)g / 255.0f, (float)b / 255.0f, (float)a / 255.0f, format, dest);
  1485. }
  1486. }
  1487. void PixelUtil::packColor(float r, float g, float b, float a, const PixelFormat format, void* dest)
  1488. {
  1489. // Special cases
  1490. if (format == PF_RG11B10F)
  1491. {
  1492. UINT32 value;
  1493. value = Bitwise::floatToFloat11(r);
  1494. value |= Bitwise::floatToFloat11(g) << 11;
  1495. value |= Bitwise::floatToFloat10(b) << 22;
  1496. ((UINT32*)dest)[0] = value;
  1497. return;
  1498. }
  1499. if (format == PF_RGB10A2)
  1500. {
  1501. LOGERR("packColor() not implemented for format \"" + getFormatName(PF_RGB10A2) + "\".");
  1502. return;
  1503. }
  1504. // All other formats handled in a generic way
  1505. const PixelFormatDescription& des = getDescriptionFor(format);
  1506. assert(des.componentCount <= 4);
  1507. float inputs[] = { r, g, b, a };
  1508. UINT8 bits[] = { des.rbits, des.gbits, des.bbits, des.abits };
  1509. UINT32 masks[] = { des.rmask, des.gmask, des.bmask, des.amask };
  1510. UINT8 shifts[] = { des.rshift, des.gshift, des.bshift, des.ashift };
  1511. memset(dest, 0, des.elemBytes);
  1512. UINT32 curBit = 0;
  1513. UINT32 prevDword = 0;
  1514. UINT32 dwordValue = 0;
  1515. for (UINT32 i = 0; i < des.componentCount; i++)
  1516. {
  1517. UINT32 curDword = curBit / 32;
  1518. // New dword reached, write current one and advance
  1519. if(curDword > prevDword)
  1520. {
  1521. UINT32* curDst = ((UINT32*)dest) + prevDword;
  1522. Bitwise::intWrite(curDst, 4, dwordValue);
  1523. dwordValue = 0;
  1524. prevDword = curDword;
  1525. }
  1526. if (des.flags & PFF_INTEGER)
  1527. {
  1528. if (des.flags & PFF_NORMALIZED)
  1529. {
  1530. if (des.flags & PFF_SIGNED)
  1531. dwordValue |= (Bitwise::snormToUint(inputs[i], bits[i]) << shifts[i]) & masks[i];
  1532. else
  1533. dwordValue |= (Bitwise::unormToUint(inputs[i], bits[i]) << shifts[i]) & masks[i];
  1534. }
  1535. else
  1536. {
  1537. // Note: Casting integer to float. A better option would be to have a separate unpackColor that has
  1538. // integer output parameters.
  1539. dwordValue |= (((UINT32)inputs[i]) << shifts[i]) & masks[i];
  1540. }
  1541. }
  1542. else if (des.flags & PFF_FLOAT)
  1543. {
  1544. // Note: Not handling unsigned floats
  1545. if (des.componentType == PCT_FLOAT16)
  1546. dwordValue |= (Bitwise::floatToHalf(inputs[i]) << shifts[i]) & masks[i];
  1547. else
  1548. dwordValue |= *(UINT32*)&inputs[i];
  1549. }
  1550. else
  1551. {
  1552. LOGERR("packColor() not implemented for format \"" + getFormatName(format) + "\".");
  1553. return;
  1554. }
  1555. curBit += bits[i];
  1556. }
  1557. // Write last dword
  1558. UINT32 numBytes = std::min((prevDword + 1) * 4, (UINT32)des.elemBytes) - (prevDword * 4);
  1559. UINT32* curDst = ((UINT32*)dest) + prevDword;
  1560. Bitwise::intWrite(curDst, numBytes, dwordValue);
  1561. }
  1562. void PixelUtil::unpackColor(Color* color, PixelFormat format, const void* src)
  1563. {
  1564. unpackColor(&color->r, &color->g, &color->b, &color->a, format, src);
  1565. }
  1566. void PixelUtil::unpackColor(UINT8* r, UINT8* g, UINT8* b, UINT8* a, PixelFormat format, const void* src)
  1567. {
  1568. const PixelFormatDescription &des = getDescriptionFor(format);
  1569. if (des.flags & PFF_INTEGER)
  1570. {
  1571. // Shortcut for integer formats unpacking
  1572. const UINT32 value = Bitwise::intRead(src, des.elemBytes);
  1573. *r = (UINT8)Bitwise::fixedToFixed((value & des.rmask) >> des.rshift, des.rbits, 8);
  1574. *g = (UINT8)Bitwise::fixedToFixed((value & des.gmask) >> des.gshift, des.gbits, 8);
  1575. *b = (UINT8)Bitwise::fixedToFixed((value & des.bmask) >> des.bshift, des.bbits, 8);
  1576. if (des.flags & PFF_HASALPHA)
  1577. {
  1578. *a = (UINT8)Bitwise::fixedToFixed((value & des.amask) >> des.ashift, des.abits, 8);
  1579. }
  1580. else
  1581. {
  1582. *a = 255; // No alpha, default a component to full
  1583. }
  1584. }
  1585. else
  1586. {
  1587. // Do the operation with the more generic floating point
  1588. float rr, gg, bb, aa;
  1589. unpackColor(&rr, &gg, &bb, &aa, format, src);
  1590. *r = (UINT8)Bitwise::unormToUint(rr, 8);
  1591. *g = (UINT8)Bitwise::unormToUint(gg, 8);
  1592. *b = (UINT8)Bitwise::unormToUint(bb, 8);
  1593. *a = (UINT8)Bitwise::unormToUint(aa, 8);
  1594. }
  1595. }
  1596. void PixelUtil::unpackColor(float* r, float* g, float* b, float* a, PixelFormat format, const void* src)
  1597. {
  1598. // Special cases
  1599. if(format == PF_RG11B10F)
  1600. {
  1601. UINT32 value = ((UINT32*)src)[0];
  1602. *r = Bitwise::float11ToFloat(value);
  1603. *g = Bitwise::float11ToFloat(value >> 11);
  1604. *b = Bitwise::float10ToFloat(value >> 22);
  1605. return;
  1606. }
  1607. if(format == PF_RGB10A2)
  1608. {
  1609. LOGERR("unpackColor() not implemented for format \"" + getFormatName(PF_RGB10A2) + "\".");
  1610. return;
  1611. }
  1612. // All other formats handled in a generic way
  1613. const PixelFormatDescription& des = getDescriptionFor(format);
  1614. assert(des.componentCount <= 4);
  1615. float* outputs[] = { r, g, b, a };
  1616. UINT8 bits[] = { des.rbits, des.gbits, des.bbits, des.abits };
  1617. UINT32 masks[] = { des.rmask, des.gmask, des.bmask, des.amask };
  1618. UINT8 shifts[] = { des.rshift, des.gshift, des.bshift, des.ashift };
  1619. UINT32 curBit = 0;
  1620. for(UINT32 i = 0; i < des.componentCount; i++)
  1621. {
  1622. UINT32 curDword = curBit / 32;
  1623. UINT32 numBytes = std::min((curDword + 1) * 4, (UINT32)des.elemBytes) - (curDword * 4);
  1624. UINT32* curSrc = ((UINT32*)src) + curDword;
  1625. UINT32 value = Bitwise::intRead(curSrc, numBytes);
  1626. if(des.flags & PFF_INTEGER)
  1627. {
  1628. if(des.flags & PFF_NORMALIZED)
  1629. {
  1630. if (des.flags & PFF_SIGNED)
  1631. *outputs[i] = Bitwise::uintToSnorm((value & masks[i]) >> shifts[i], bits[i]);
  1632. else
  1633. *outputs[i] = Bitwise::uintToUnorm((value & masks[i]) >> shifts[i], bits[i]);
  1634. }
  1635. else
  1636. {
  1637. // Note: Casting integer to float. A better option would be to have a separate unpackColor that has
  1638. // integer output parameters.
  1639. *outputs[i] = (float)((value & masks[i]) >> shifts[i]);
  1640. }
  1641. }
  1642. else if(des.flags & PFF_FLOAT)
  1643. {
  1644. // Note: Not handling unsigned floats
  1645. if (des.componentType == PCT_FLOAT16)
  1646. *outputs[i] = Bitwise::halfToFloat((UINT16)((value & masks[i]) >> shifts[i]));
  1647. else
  1648. *outputs[i] = *(float*)&value;
  1649. }
  1650. else
  1651. {
  1652. LOGERR("unpackColor() not implemented for format \"" + getFormatName(format) + "\".");
  1653. return;
  1654. }
  1655. curBit += bits[i];
  1656. }
  1657. // Fill empty components
  1658. for (UINT32 i = des.componentCount; i < 3; i++)
  1659. *outputs[i] = 0.0f;
  1660. if (des.componentCount < 4)
  1661. *outputs[3] = 1.0f;
  1662. }
  1663. void PixelUtil::packDepth(float depth, const PixelFormat format, void* dest)
  1664. {
  1665. if (!isDepth(format))
  1666. {
  1667. LOGERR("Cannot convert depth to " + getFormatName(format) + ": it is not a depth format");
  1668. return;
  1669. }
  1670. LOGERR("Method is not implemented");
  1671. //TODO implement depth packing
  1672. }
  1673. float PixelUtil::unpackDepth(PixelFormat format, void* src)
  1674. {
  1675. const PixelFormatDescription &des = getDescriptionFor(format);
  1676. if (!isDepth(format))
  1677. {
  1678. LOGERR("Cannot unpack from " + getFormatName(format) + ": it is not a depth format");
  1679. return 0;
  1680. }
  1681. UINT32* color = (UINT32 *)src;
  1682. UINT32 masked = 0;
  1683. switch (format)
  1684. {
  1685. case PF_D24S8:
  1686. return static_cast<float>(*color & 0x00FFFFFF) / (float)16777216;
  1687. break;
  1688. case PF_D16:
  1689. return static_cast<float>(*color & 0xFFFF) / (float)65536;
  1690. break;
  1691. case PF_D32:
  1692. masked = *color & 0xFFFFFFFF;
  1693. return *((float*)&masked);
  1694. break;
  1695. case PF_D32_S8X24:
  1696. masked = *color & 0xFFFFFFFF;
  1697. return *( (float*) &masked );
  1698. break;
  1699. default:
  1700. LOGERR("Cannot unpack from " + getFormatName(format));
  1701. return 0;
  1702. break;
  1703. }
  1704. }
  1705. void PixelUtil::bulkPixelConversion(const PixelData &src, PixelData &dst)
  1706. {
  1707. assert(src.getWidth() == dst.getWidth() &&
  1708. src.getHeight() == dst.getHeight() &&
  1709. src.getDepth() == dst.getDepth());
  1710. // Check for compressed formats, we don't support decompression
  1711. if (PixelUtil::isCompressed(src.getFormat()))
  1712. {
  1713. if (src.getFormat() == dst.getFormat())
  1714. {
  1715. memcpy(dst.getData(), src.getData(), src.getConsecutiveSize());
  1716. return;
  1717. }
  1718. else
  1719. {
  1720. LOGERR("bulkPixelConversion() cannot be used to compress or decompress images");
  1721. return;
  1722. }
  1723. }
  1724. // Check for compression
  1725. if (PixelUtil::isCompressed(dst.getFormat()))
  1726. {
  1727. if (src.getFormat() == dst.getFormat())
  1728. {
  1729. memcpy(dst.getData(), src.getData(), src.getConsecutiveSize());
  1730. return;
  1731. }
  1732. else
  1733. {
  1734. CompressionOptions co;
  1735. co.format = dst.getFormat();
  1736. compress(src, dst, co);
  1737. return;
  1738. }
  1739. }
  1740. // The easy case
  1741. if (src.getFormat() == dst.getFormat())
  1742. {
  1743. // Everything consecutive?
  1744. if (src.isConsecutive() && dst.isConsecutive())
  1745. {
  1746. memcpy(dst.getData(), src.getData(), src.getConsecutiveSize());
  1747. return;
  1748. }
  1749. const UINT32 srcPixelSize = PixelUtil::getNumElemBytes(src.getFormat());
  1750. const UINT32 dstPixelSize = PixelUtil::getNumElemBytes(dst.getFormat());
  1751. UINT8 *srcptr = static_cast<UINT8*>(src.getData())
  1752. + (src.getLeft() + src.getTop() * src.getRowPitch() + src.getFront() * src.getSlicePitch()) * srcPixelSize;
  1753. UINT8 *dstptr = static_cast<UINT8*>(dst.getData())
  1754. + (dst.getLeft() + dst.getTop() * dst.getRowPitch() + dst.getFront() * dst.getSlicePitch()) * dstPixelSize;
  1755. // Calculate pitches+skips in bytes
  1756. const UINT32 srcRowPitchBytes = src.getRowPitch()*srcPixelSize;
  1757. const UINT32 srcSliceSkipBytes = src.getSliceSkip()*srcPixelSize;
  1758. const UINT32 dstRowPitchBytes = dst.getRowPitch()*dstPixelSize;
  1759. const UINT32 dstSliceSkipBytes = dst.getSliceSkip()*dstPixelSize;
  1760. // Otherwise, copy per row
  1761. const UINT32 rowSize = src.getWidth()*srcPixelSize;
  1762. for (UINT32 z = src.getFront(); z < src.getBack(); z++)
  1763. {
  1764. for (UINT32 y = src.getTop(); y < src.getBottom(); y++)
  1765. {
  1766. memcpy(dstptr, srcptr, rowSize);
  1767. srcptr += srcRowPitchBytes;
  1768. dstptr += dstRowPitchBytes;
  1769. }
  1770. srcptr += srcSliceSkipBytes;
  1771. dstptr += dstSliceSkipBytes;
  1772. }
  1773. return;
  1774. }
  1775. UINT32 srcPixelSize = PixelUtil::getNumElemBytes(src.getFormat());
  1776. UINT32 dstPixelSize = PixelUtil::getNumElemBytes(dst.getFormat());
  1777. UINT8 *srcptr = static_cast<UINT8*>(src.getData())
  1778. + (src.getLeft() + src.getTop() * src.getRowPitch() + src.getFront() * src.getSlicePitch()) * srcPixelSize;
  1779. UINT8 *dstptr = static_cast<UINT8*>(dst.getData())
  1780. + (dst.getLeft() + dst.getTop() * dst.getRowPitch() + dst.getFront() * dst.getSlicePitch()) * dstPixelSize;
  1781. // Calculate pitches+skips in bytes
  1782. UINT32 srcRowSkipBytes = src.getRowSkip()*srcPixelSize;
  1783. UINT32 srcSliceSkipBytes = src.getSliceSkip()*srcPixelSize;
  1784. UINT32 dstRowSkipBytes = dst.getRowSkip()*dstPixelSize;
  1785. UINT32 dstSliceSkipBytes = dst.getSliceSkip()*dstPixelSize;
  1786. // The brute force fallback
  1787. float r, g, b, a;
  1788. for (UINT32 z = src.getFront(); z < src.getBack(); z++)
  1789. {
  1790. for (UINT32 y = src.getTop(); y < src.getBottom(); y++)
  1791. {
  1792. for (UINT32 x = src.getLeft(); x < src.getRight(); x++)
  1793. {
  1794. unpackColor(&r, &g, &b, &a, src.getFormat(), srcptr);
  1795. packColor(r, g, b, a, dst.getFormat(), dstptr);
  1796. srcptr += srcPixelSize;
  1797. dstptr += dstPixelSize;
  1798. }
  1799. srcptr += srcRowSkipBytes;
  1800. dstptr += dstRowSkipBytes;
  1801. }
  1802. srcptr += srcSliceSkipBytes;
  1803. dstptr += dstSliceSkipBytes;
  1804. }
  1805. }
  1806. void PixelUtil::flipComponentOrder(PixelData& data)
  1807. {
  1808. if (isCompressed(data.getFormat()))
  1809. {
  1810. LOGERR("flipComponentOrder() not supported on compressed images.");
  1811. return;
  1812. }
  1813. const PixelFormatDescription& pfd = getDescriptionFor(data.getFormat());
  1814. if(pfd.elemBytes > 4)
  1815. {
  1816. LOGERR("flipComponentOrder() only supported on 4 byte or smaller pixel formats.");
  1817. return;
  1818. }
  1819. if (pfd.componentCount <= 1) // Nothing to flip
  1820. return;
  1821. bool bitCountMismatch = false;
  1822. if (pfd.rbits != pfd.gbits)
  1823. bitCountMismatch = true;
  1824. if(pfd.componentCount > 2 && pfd.rbits != pfd.bbits)
  1825. bitCountMismatch = true;
  1826. if (pfd.componentCount > 3 && pfd.rbits != pfd.abits)
  1827. bitCountMismatch = true;
  1828. if(bitCountMismatch)
  1829. {
  1830. LOGERR("flipComponentOrder() not supported for formats that don't have the same number of bytes for all components.");
  1831. return;
  1832. }
  1833. struct CompData
  1834. {
  1835. UINT32 mask;
  1836. UINT8 shift;
  1837. };
  1838. std::array<CompData, 4> compData =
  1839. {{
  1840. { pfd.rmask, pfd.rshift },
  1841. { pfd.gmask, pfd.gshift },
  1842. { pfd.bmask, pfd.bshift },
  1843. { pfd.amask, pfd.ashift }
  1844. }};
  1845. // Ensure unused components are at the end, after sort
  1846. if (pfd.componentCount < 4)
  1847. compData[4].shift = 0xFF;
  1848. if (pfd.componentCount < 3)
  1849. compData[3].shift = 0xFF;
  1850. std::sort(compData.begin(), compData.end(),
  1851. [&](const CompData& lhs, const CompData& rhs) { return lhs.shift < rhs.shift; }
  1852. );
  1853. UINT8* dataPtr = data.getData();
  1854. UINT32 pixelSize = pfd.elemBytes;
  1855. UINT32 rowSkipBytes = data.getRowSkip()*pixelSize;
  1856. UINT32 sliceSkipBytes = data.getSliceSkip()*pixelSize;
  1857. for (UINT32 z = 0; z < data.getDepth(); z++)
  1858. {
  1859. for (UINT32 y = 0; y < data.getHeight(); y++)
  1860. {
  1861. for (UINT32 x = 0; x < data.getWidth(); x++)
  1862. {
  1863. if(pfd.componentCount == 2)
  1864. {
  1865. UINT64 pixelData = 0;
  1866. memcpy(&pixelData, dataPtr, pixelSize);
  1867. UINT64 output = 0;
  1868. output |= (pixelData & compData[1].mask) >> compData[1].shift;
  1869. output |= (pixelData & compData[0].mask) << compData[1].shift;
  1870. memcpy(dataPtr, &output, pixelSize);
  1871. }
  1872. else if(pfd.componentCount == 3)
  1873. {
  1874. UINT64 pixelData = 0;
  1875. memcpy(&pixelData, dataPtr, pixelSize);
  1876. UINT64 output = 0;
  1877. output |= (pixelData & compData[2].mask) >> compData[2].shift;
  1878. output |= (pixelData & compData[0].mask) << compData[2].shift;
  1879. memcpy(dataPtr, &output, pixelSize);
  1880. }
  1881. else if(pfd.componentCount == 4)
  1882. {
  1883. UINT64 pixelData = 0;
  1884. memcpy(&pixelData, dataPtr, pixelSize);
  1885. UINT64 output = 0;
  1886. output |= (pixelData & compData[3].mask) >> compData[3].shift;
  1887. output |= (pixelData & compData[0].mask) << compData[3].shift;
  1888. output |= (pixelData & compData[2].mask) >> (compData[2].shift - compData[1].shift);
  1889. output |= (pixelData & compData[1].mask) << (compData[2].shift - compData[1].shift);
  1890. memcpy(dataPtr, &output, pixelSize);
  1891. }
  1892. dataPtr += pixelSize;
  1893. }
  1894. dataPtr += rowSkipBytes;
  1895. }
  1896. dataPtr += sliceSkipBytes;
  1897. }
  1898. }
  1899. void PixelUtil::scale(const PixelData& src, PixelData& scaled, Filter filter)
  1900. {
  1901. assert(PixelUtil::isAccessible(src.getFormat()));
  1902. assert(PixelUtil::isAccessible(scaled.getFormat()));
  1903. PixelData temp;
  1904. switch (filter)
  1905. {
  1906. default:
  1907. case FILTER_NEAREST:
  1908. if(src.getFormat() == scaled.getFormat())
  1909. {
  1910. // No intermediate buffer needed
  1911. temp = scaled;
  1912. }
  1913. else
  1914. {
  1915. // Allocate temporary buffer of destination size in source format
  1916. temp = PixelData(scaled.getWidth(), scaled.getHeight(), scaled.getDepth(), src.getFormat());
  1917. temp.allocateInternalBuffer();
  1918. }
  1919. // No conversion
  1920. switch (PixelUtil::getNumElemBytes(src.getFormat()))
  1921. {
  1922. case 1: NearestResampler<1>::scale(src, temp); break;
  1923. case 2: NearestResampler<2>::scale(src, temp); break;
  1924. case 3: NearestResampler<3>::scale(src, temp); break;
  1925. case 4: NearestResampler<4>::scale(src, temp); break;
  1926. case 6: NearestResampler<6>::scale(src, temp); break;
  1927. case 8: NearestResampler<8>::scale(src, temp); break;
  1928. case 12: NearestResampler<12>::scale(src, temp); break;
  1929. case 16: NearestResampler<16>::scale(src, temp); break;
  1930. default:
  1931. // Never reached
  1932. assert(false);
  1933. }
  1934. if(temp.getData() != scaled.getData())
  1935. {
  1936. // Blit temp buffer
  1937. PixelUtil::bulkPixelConversion(temp, scaled);
  1938. temp.freeInternalBuffer();
  1939. }
  1940. break;
  1941. case FILTER_LINEAR:
  1942. switch (src.getFormat())
  1943. {
  1944. case PF_RG8:
  1945. case PF_RGB8: case PF_BGR8:
  1946. case PF_RGBA8: case PF_BGRA8:
  1947. if(src.getFormat() == scaled.getFormat())
  1948. {
  1949. // No intermediate buffer needed
  1950. temp = scaled;
  1951. }
  1952. else
  1953. {
  1954. // Allocate temp buffer of destination size in source format
  1955. temp = PixelData(scaled.getWidth(), scaled.getHeight(), scaled.getDepth(), src.getFormat());
  1956. temp.allocateInternalBuffer();
  1957. }
  1958. // No conversion
  1959. switch (PixelUtil::getNumElemBytes(src.getFormat()))
  1960. {
  1961. case 1: LinearResampler_Byte<1>::scale(src, temp); break;
  1962. case 2: LinearResampler_Byte<2>::scale(src, temp); break;
  1963. case 3: LinearResampler_Byte<3>::scale(src, temp); break;
  1964. case 4: LinearResampler_Byte<4>::scale(src, temp); break;
  1965. default:
  1966. // Never reached
  1967. assert(false);
  1968. }
  1969. if(temp.getData() != scaled.getData())
  1970. {
  1971. // Blit temp buffer
  1972. PixelUtil::bulkPixelConversion(temp, scaled);
  1973. temp.freeInternalBuffer();
  1974. }
  1975. break;
  1976. case PF_RGB32F:
  1977. case PF_RGBA32F:
  1978. if (scaled.getFormat() == PF_RGB32F || scaled.getFormat() == PF_RGBA32F)
  1979. {
  1980. // float32 to float32, avoid unpack/repack overhead
  1981. LinearResampler_Float32::scale(src, scaled);
  1982. break;
  1983. }
  1984. // Else, fall through
  1985. default:
  1986. // Fallback case, slow but works
  1987. LinearResampler::scale(src, scaled);
  1988. }
  1989. break;
  1990. }
  1991. }
  1992. void PixelUtil::copy(const PixelData& src, PixelData& dst, UINT32 offsetX, UINT32 offsetY, UINT32 offsetZ)
  1993. {
  1994. if(src.getFormat() != dst.getFormat())
  1995. {
  1996. LOGERR("Source format is different from destination format for copy(). This operation cannot be used for "
  1997. "a format conversion. Aborting copy.");
  1998. return;
  1999. }
  2000. UINT32 right = offsetX + dst.getWidth();
  2001. UINT32 bottom = offsetY + dst.getHeight();
  2002. UINT32 back = offsetZ + dst.getDepth();
  2003. if(right > src.getWidth() || bottom > src.getHeight() || back > src.getDepth())
  2004. {
  2005. LOGERR("Provided offset or destination size is too large and is referencing pixels that are out of bounds"
  2006. " on the source texture. Aborting copy().");
  2007. return;
  2008. }
  2009. UINT8* srcPtr = (UINT8*)src.getData() + offsetZ * src.getSlicePitch();
  2010. UINT8* dstPtr = (UINT8*)dst.getData();
  2011. UINT32 elemSize = getNumElemBytes(dst.getFormat());
  2012. UINT32 rowSize = dst.getWidth() * elemSize;
  2013. for(UINT32 z = 0; z < dst.getDepth(); z++)
  2014. {
  2015. UINT8* srcRowPtr = srcPtr + offsetY * src.getRowPitch() * elemSize;
  2016. UINT8* dstRowPtr = dstPtr;
  2017. for(UINT32 y = 0; y < dst.getHeight(); y++)
  2018. {
  2019. memcpy(dstRowPtr, srcRowPtr + offsetX * elemSize, rowSize);
  2020. srcRowPtr += src.getRowPitch() * elemSize;
  2021. dstRowPtr += dst.getRowPitch() * elemSize;
  2022. }
  2023. srcPtr += src.getSlicePitch() * elemSize;
  2024. dstPtr += dst.getSlicePitch() * elemSize;
  2025. }
  2026. }
  2027. void PixelUtil::mirror(PixelData& pixelData, MirrorMode mode)
  2028. {
  2029. UINT32 width = pixelData.getWidth();
  2030. UINT32 height = pixelData.getHeight();
  2031. UINT32 depth = pixelData.getDepth();
  2032. UINT32 elemSize = getNumElemBytes(pixelData.getFormat());
  2033. if (mode.isSet(MirrorModeBits::Z))
  2034. {
  2035. UINT32 sliceSize = width * height * elemSize;
  2036. UINT8* sliceTemp = bs_stack_alloc<UINT8>(sliceSize);
  2037. UINT8* dataPtr = pixelData.getData();
  2038. UINT32 halfDepth = depth / 2;
  2039. for (UINT32 z = 0; z < halfDepth; z++)
  2040. {
  2041. UINT32 srcZ = z * sliceSize;
  2042. UINT32 dstZ = (depth - z - 1) * sliceSize;
  2043. memcpy(sliceTemp, &dataPtr[dstZ], sliceSize);
  2044. memcpy(&dataPtr[srcZ], &dataPtr[srcZ], sliceSize);
  2045. memcpy(&dataPtr[dstZ], sliceTemp, sliceSize);
  2046. }
  2047. // Note: If flipping Y or X as well I could do it here without an extra set of memcpys
  2048. bs_stack_free(sliceTemp);
  2049. }
  2050. if(mode.isSet(MirrorModeBits::Y))
  2051. {
  2052. UINT32 rowSize = width * elemSize;
  2053. UINT8* rowTemp = bs_stack_alloc<UINT8>(rowSize);
  2054. UINT8* slicePtr = pixelData.getData();
  2055. for (UINT32 z = 0; z < depth; z++)
  2056. {
  2057. UINT32 halfHeight = height / 2;
  2058. for (UINT32 y = 0; y < halfHeight; y++)
  2059. {
  2060. UINT32 srcY = y * rowSize;
  2061. UINT32 dstY = (height - y - 1) * rowSize;
  2062. memcpy(rowTemp, &slicePtr[dstY], rowSize);
  2063. memcpy(&slicePtr[dstY], &slicePtr[srcY], rowSize);
  2064. memcpy(&slicePtr[srcY], rowTemp, rowSize);
  2065. }
  2066. // Note: If flipping X as well I could do it here without an extra set of memcpys
  2067. slicePtr += pixelData.getSlicePitch() * elemSize;
  2068. }
  2069. bs_stack_free(rowTemp);
  2070. }
  2071. if (mode.isSet(MirrorModeBits::X))
  2072. {
  2073. UINT8* elemTemp = bs_stack_alloc<UINT8>(elemSize);
  2074. UINT8* slicePtr = pixelData.getData();
  2075. for (UINT32 z = 0; z < depth; z++)
  2076. {
  2077. UINT8* rowPtr = slicePtr;
  2078. for (UINT32 y = 0; y < height; y++)
  2079. {
  2080. UINT32 halfWidth = width / 2;
  2081. for (UINT32 x = 0; x < halfWidth; x++)
  2082. {
  2083. UINT32 srcX = x * elemSize;
  2084. UINT32 dstX = (width - x - 1) * elemSize;
  2085. memcpy(elemTemp, &rowPtr[dstX], elemSize);
  2086. memcpy(&rowPtr[dstX], &rowPtr[srcX], elemSize);
  2087. memcpy(&rowPtr[srcX], elemTemp, elemSize);
  2088. }
  2089. rowPtr += pixelData.getRowPitch() * elemSize;
  2090. }
  2091. slicePtr += pixelData.getSlicePitch() * elemSize;
  2092. }
  2093. bs_stack_free(elemTemp);
  2094. }
  2095. }
  2096. void PixelUtil::applyGamma(UINT8* buffer, float gamma, UINT32 size, UINT8 bpp)
  2097. {
  2098. if(gamma == 1.0f)
  2099. return;
  2100. UINT32 stride = bpp >> 3;
  2101. for(size_t i = 0, j = size / stride; i < j; i++, buffer += stride)
  2102. {
  2103. float r = (float)buffer[0];
  2104. float g = (float)buffer[1];
  2105. float b = (float)buffer[2];
  2106. r = r * gamma;
  2107. g = g * gamma;
  2108. b = b * gamma;
  2109. float scale = 1.0f;
  2110. float tmp = 0.0f;
  2111. if(r > 255.0f && (tmp=(255.0f/r)) < scale)
  2112. scale = tmp;
  2113. if(g > 255.0f && (tmp=(255.0f/g)) < scale)
  2114. scale = tmp;
  2115. if(b > 255.0f && (tmp=(255.0f/b)) < scale)
  2116. scale = tmp;
  2117. r *= scale;
  2118. g *= scale;
  2119. b *= scale;
  2120. buffer[0] = (UINT8)r;
  2121. buffer[1] = (UINT8)g;
  2122. buffer[2] = (UINT8)b;
  2123. }
  2124. }
  2125. void PixelUtil::compress(const PixelData& src, PixelData& dst, const CompressionOptions& options)
  2126. {
  2127. if (!isCompressed(options.format))
  2128. {
  2129. LOGERR("Compression failed. Destination format is not a valid compressed format.")
  2130. return;
  2131. }
  2132. if (src.getDepth() != 1)
  2133. {
  2134. LOGERR("Compression failed. 3D texture compression not supported.")
  2135. return;
  2136. }
  2137. if (isCompressed(src.getFormat()))
  2138. {
  2139. LOGERR("Compression failed. Source data cannot be compressed.");
  2140. return;
  2141. }
  2142. PixelFormat interimFormat = options.format == PF_BC6H ? PF_RGBA32F : PF_BGRA8;
  2143. PixelData interimData(src.getWidth(), src.getHeight(), 1, interimFormat);
  2144. interimData.allocateInternalBuffer();
  2145. bulkPixelConversion(src, interimData);
  2146. if(interimFormat != PF_RGBA32F)
  2147. flipComponentOrder(interimData);
  2148. nvtt::InputOptions io;
  2149. io.setTextureLayout(nvtt::TextureType_2D, src.getWidth(), src.getHeight());
  2150. io.setMipmapGeneration(false);
  2151. io.setAlphaMode(toNVTTAlphaMode(options.alphaMode));
  2152. io.setNormalMap(options.isNormalMap);
  2153. if (interimFormat == PF_RGBA32F)
  2154. io.setFormat(nvtt::InputFormat_RGBA_32F);
  2155. else
  2156. io.setFormat(nvtt::InputFormat_BGRA_8UB);
  2157. if (options.isSRGB)
  2158. io.setGamma(2.2f, 2.2f);
  2159. else
  2160. io.setGamma(1.0f, 1.0f);
  2161. io.setMipmapData(interimData.getData(), src.getWidth(), src.getHeight());
  2162. nvtt::CompressionOptions co;
  2163. co.setFormat(toNVTTFormat(options.format));
  2164. co.setQuality(toNVTTQuality(options.quality));
  2165. NVTTCompressOutputHandler outputHandler(dst.getData(), dst.getConsecutiveSize());
  2166. nvtt::OutputOptions oo;
  2167. oo.setOutputHeader(false);
  2168. oo.setOutputHandler(&outputHandler);
  2169. nvtt::Compressor compressor;
  2170. if (!compressor.process(io, co, oo))
  2171. {
  2172. LOGERR("Compression failed. Internal error.");
  2173. return;
  2174. }
  2175. }
  2176. Vector<SPtr<PixelData>> PixelUtil::genMipmaps(const PixelData& src, const MipMapGenOptions& options)
  2177. {
  2178. Vector<SPtr<PixelData>> outputMipBuffers;
  2179. if (src.getDepth() != 1)
  2180. {
  2181. LOGERR("Mipmap generation failed. 3D texture formats not supported.")
  2182. return outputMipBuffers;
  2183. }
  2184. if (isCompressed(src.getFormat()))
  2185. {
  2186. LOGERR("Mipmap generation failed. Source data cannot be compressed.")
  2187. return outputMipBuffers;
  2188. }
  2189. if (!Bitwise::isPow2(src.getWidth()) || !Bitwise::isPow2(src.getHeight()))
  2190. {
  2191. LOGERR("Mipmap generation failed. Texture width & height must be powers of 2.");
  2192. return outputMipBuffers;
  2193. }
  2194. PixelFormat interimFormat = isFloatingPoint(src.getFormat()) ? PF_RGBA32F : PF_BGRA8;
  2195. PixelData interimData(src.getWidth(), src.getHeight(), 1, interimFormat);
  2196. interimData.allocateInternalBuffer();
  2197. bulkPixelConversion(src, interimData);
  2198. if (interimFormat != PF_RGBA32F)
  2199. flipComponentOrder(interimData);
  2200. nvtt::InputOptions io;
  2201. io.setTextureLayout(nvtt::TextureType_2D, src.getWidth(), src.getHeight());
  2202. io.setMipmapGeneration(true);
  2203. io.setNormalMap(options.isNormalMap);
  2204. io.setNormalizeMipmaps(options.normalizeMipmaps);
  2205. io.setWrapMode(toNVTTWrapMode(options.wrapMode));
  2206. if (interimFormat == PF_RGBA32F)
  2207. io.setFormat(nvtt::InputFormat_RGBA_32F);
  2208. else
  2209. io.setFormat(nvtt::InputFormat_BGRA_8UB);
  2210. if (options.isSRGB)
  2211. io.setGamma(2.2f, 2.2f);
  2212. else
  2213. io.setGamma(1.0f, 1.0f);
  2214. io.setMipmapData(interimData.getData(), src.getWidth(), src.getHeight());
  2215. nvtt::CompressionOptions co;
  2216. co.setFormat(nvtt::Format_RGBA);
  2217. if (interimFormat == PF_RGBA32F)
  2218. {
  2219. co.setPixelType(nvtt::PixelType_Float);
  2220. co.setPixelFormat(32, 32, 32, 32);
  2221. }
  2222. else
  2223. {
  2224. co.setPixelType(nvtt::PixelType_UnsignedNorm);
  2225. co.setPixelFormat(32, 0x0000FF00, 0x00FF0000, 0xFF000000, 0x000000FF);
  2226. }
  2227. UINT32 numMips = getMaxMipmaps(src.getWidth(), src.getHeight(), 1, src.getFormat());
  2228. Vector<SPtr<PixelData>> rgbaMipBuffers;
  2229. // Note: This can be done more effectively without creating so many temp buffers
  2230. // and working with the original formats directly, but it would complicate the code
  2231. // too much at the moment.
  2232. UINT32 curWidth = src.getWidth();
  2233. UINT32 curHeight = src.getHeight();
  2234. for (UINT32 i = 0; i < numMips; i++)
  2235. {
  2236. rgbaMipBuffers.push_back(bs_shared_ptr_new<PixelData>(curWidth, curHeight, 1, interimFormat));
  2237. rgbaMipBuffers.back()->allocateInternalBuffer();
  2238. if (curWidth > 1)
  2239. curWidth = curWidth / 2;
  2240. if (curHeight > 1)
  2241. curHeight = curHeight / 2;
  2242. }
  2243. rgbaMipBuffers.push_back(bs_shared_ptr_new<PixelData>(curWidth, curHeight, 1, interimFormat));
  2244. rgbaMipBuffers.back()->allocateInternalBuffer();
  2245. NVTTMipmapOutputHandler outputHandler(rgbaMipBuffers);
  2246. nvtt::OutputOptions oo;
  2247. oo.setOutputHeader(false);
  2248. oo.setOutputHandler(&outputHandler);
  2249. nvtt::Compressor compressor;
  2250. if (!compressor.process(io, co, oo))
  2251. {
  2252. LOGERR("Mipmap generation failed. Internal error.");
  2253. return outputMipBuffers;
  2254. }
  2255. interimData.freeInternalBuffer();
  2256. for (UINT32 i = 0; i < (UINT32)rgbaMipBuffers.size(); i++)
  2257. {
  2258. SPtr<PixelData> argbBuffer = rgbaMipBuffers[i];
  2259. SPtr<PixelData> outputBuffer = bs_shared_ptr_new<PixelData>(argbBuffer->getWidth(), argbBuffer->getHeight(), 1, src.getFormat());
  2260. outputBuffer->allocateInternalBuffer();
  2261. bulkPixelConversion(*argbBuffer, *outputBuffer);
  2262. argbBuffer->freeInternalBuffer();
  2263. outputMipBuffers.push_back(outputBuffer);
  2264. }
  2265. return outputMipBuffers;
  2266. }
  2267. }