WebGLTexelConversions.h 46 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354
  1. /*
  2. * Copyright (C) 2010 Apple Inc. All rights reserved.
  3. * Copyright (C) 2010 Google Inc. All rights reserved.
  4. * Copyright (C) 2010 Mozilla Corporation. All rights reserved.
  5. *
  6. * Redistribution and use in source and binary forms, with or without
  7. * modification, are permitted provided that the following conditions
  8. * are met:
  9. * 1. Redistributions of source code must retain the above copyright
  10. * notice, this list of conditions and the following disclaimer.
  11. * 2. Redistributions in binary form must reproduce the above copyright
  12. * notice, this list of conditions and the following disclaimer in the
  13. * documentation and/or other materials provided with the distribution.
  14. *
  15. * THIS SOFTWARE IS PROVIDED BY APPLE COMPUTER, INC. ``AS IS'' AND ANY
  16. * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
  17. * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
  18. * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL APPLE COMPUTER, INC. OR
  19. * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
  20. * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
  21. * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
  22. * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY
  23. * OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
  24. * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
  25. * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  26. */
  27. #ifndef WEBGLTEXELCONVERSIONS_H_
  28. #define WEBGLTEXELCONVERSIONS_H_
  29. #ifdef __SUNPRO_CC
  30. #define __restrict
  31. #endif
  32. #include "WebGLTypes.h"
  33. #include <stdint.h>
  34. #include "mozilla/Attributes.h"
  35. #include "mozilla/Casting.h"
  36. namespace mozilla {
  37. // Added from gfx/gl/GLContextTypes.h
  38. namespace gl {
  39. enum class OriginPos : uint8_t {
  40. TopLeft,
  41. BottomLeft
  42. };
  43. }
  44. //
  45. bool ConvertImage(size_t width, size_t height,
  46. const void* srcBegin, size_t srcStride, gl::OriginPos srcOrigin,
  47. WebGLTexelFormat srcFormat, bool srcPremultiplied,
  48. void* dstBegin, size_t dstStride, gl::OriginPos dstOrigin,
  49. WebGLTexelFormat dstFormat, bool dstPremultiplied,
  50. bool* out_wasTrivial);
  51. //////////////////////////////////////////////////////////////////////////////////////////
  52. // single precision float
  53. // seeeeeeeemmmmmmmmmmmmmmmmmmmmmmm
  54. // half precision float
  55. // seeeeemmmmmmmmmm
  56. // IEEE 16bits floating point:
  57. const uint16_t kFloat16Value_Zero = 0x0000; // = 0000000000000000b
  58. const uint16_t kFloat16Value_One = 0x3C00; // = 0011110000000000b
  59. const uint16_t kFloat16Value_Infinity = 0x7C00; // = 0111110000000000b
  60. const uint16_t kFloat16Value_NaN = 0x7FFF; // = 011111yyyyyyyyyyb (nonzero y)
  61. MOZ_ALWAYS_INLINE uint16_t
  62. packToFloat16(float v)
  63. {
  64. union {
  65. float f32Value;
  66. uint32_t f32Bits;
  67. };
  68. f32Value = v;
  69. // pull the sign from v into f16bits
  70. uint16_t f16Bits = uint16_t(f32Bits >> 16) & 0x8000;
  71. const uint32_t mantissa = f32Bits & 0x7FFFFF;
  72. const uint32_t exp = (f32Bits >> 23) & 0xFF;
  73. // Adapted from: OpenGL ES 2.0 Programming Guide Appx.
  74. // Converting Float to Half-Float
  75. // 143 = 255 - 127 + 15
  76. // = sp_max - sp_bias + hp_bias
  77. if (exp >= 143) {
  78. if (mantissa && exp == 0xFF) {
  79. // Single precision was NaN
  80. return f16Bits | kFloat16Value_NaN;
  81. } else {
  82. // Outside range, store as infinity
  83. return f16Bits | kFloat16Value_Infinity;
  84. }
  85. }
  86. // too small, try to make a denormalized number
  87. // 112 = 255 - 127 - (15 + 1)
  88. // = sp_max - sp_bias - (hp_bias + 1)
  89. if (exp <= 112) {
  90. return f16Bits | uint16_t(mantissa >> (14 + 112 - exp));
  91. }
  92. f16Bits |= uint16_t(exp - 112) << 10;
  93. f16Bits |= uint16_t(mantissa >> 13) & 0x03FF;
  94. return f16Bits;
  95. }
  96. MOZ_ALWAYS_INLINE float
  97. unpackFromFloat16(uint16_t v)
  98. {
  99. union {
  100. float f32Value;
  101. uint32_t f32Bits;
  102. };
  103. // grab sign bit
  104. f32Bits = uint32_t(v & 0x8000) << 16;
  105. uint16_t exp = (v >> 10) & 0x001F;
  106. uint16_t mantissa = v & 0x03FF;
  107. if (!exp) {
  108. // Handle denormalized numbers
  109. // Adapted from: OpenGL ES 2.0 Programming Guide Appx.
  110. // Converting Float to Half-Float
  111. if (mantissa) {
  112. exp = 112; // See packToFloat16
  113. mantissa <<= 1;
  114. // For every leading zero, decrement the exponent
  115. // and shift the mantissa to the left
  116. while ((mantissa & (1 << 10)) == 0) {
  117. mantissa <<= 1;
  118. --exp;
  119. }
  120. mantissa &= 0x03FF;
  121. f32Bits |= (exp << 23) | (mantissa << 13);
  122. // Denormalized number
  123. return f32Value;
  124. }
  125. // +/- zero
  126. return f32Value;
  127. }
  128. if (exp == 0x001F) {
  129. if (v & 0x03FF) {
  130. // this is a NaN
  131. f32Bits |= 0x7FFFFFFF;
  132. } else {
  133. // this is -inf or +inf
  134. f32Bits |= 0x7F800000;
  135. }
  136. return f32Value;
  137. }
  138. f32Bits |= uint32_t(exp + (-15 + 127)) << 23;
  139. f32Bits |= uint32_t(v & 0x03FF) << 13;
  140. return f32Value;
  141. }
  142. // These routines come from angle/common/mathutil.h
  143. // They are copied here to remove the dependency on ANGLE headers
  144. // included from mathutil.h
  145. MOZ_ALWAYS_INLINE uint16_t
  146. packToFloat11(float fp32)
  147. {
  148. const unsigned int float32MantissaMask = 0x7FFFFF;
  149. const unsigned int float32ExponentMask = 0x7F800000;
  150. const unsigned int float32SignMask = 0x80000000;
  151. const unsigned int float32ValueMask = ~float32SignMask;
  152. const unsigned int float32ExponentFirstBit = 23;
  153. const unsigned int float32ExponentBias = 127;
  154. const unsigned short float11Max = 0x7BF;
  155. const unsigned short float11MantissaMask = 0x3F;
  156. const unsigned short float11ExponentMask = 0x7C0;
  157. const unsigned short float11BitMask = 0x7FF;
  158. const unsigned int float11ExponentBias = 14;
  159. const unsigned int float32Maxfloat11 = 0x477E0000;
  160. const unsigned int float32Minfloat11 = 0x38800000;
  161. const unsigned int float32Bits = BitwiseCast<unsigned int>(fp32);
  162. const bool float32Sign = (float32Bits & float32SignMask) == float32SignMask;
  163. unsigned int float32Val = float32Bits & float32ValueMask;
  164. if ((float32Val & float32ExponentMask) == float32ExponentMask)
  165. {
  166. // INF or NAN
  167. if ((float32Val & float32MantissaMask) != 0)
  168. {
  169. return float11ExponentMask | (((float32Val >> 17) | (float32Val >> 11) | (float32Val >> 6) | (float32Val)) & float11MantissaMask);
  170. }
  171. else if (float32Sign)
  172. {
  173. // -INF is clamped to 0 since float11 is positive only
  174. return 0;
  175. }
  176. else
  177. {
  178. return float11ExponentMask;
  179. }
  180. }
  181. else if (float32Sign)
  182. {
  183. // float11 is positive only, so clamp to zero
  184. return 0;
  185. }
  186. else if (float32Val > float32Maxfloat11)
  187. {
  188. // The number is too large to be represented as a float11, set to max
  189. return float11Max;
  190. }
  191. else
  192. {
  193. if (float32Val < float32Minfloat11)
  194. {
  195. // The number is too small to be represented as a normalized float11
  196. // Convert it to a denormalized value.
  197. const unsigned int shift = (float32ExponentBias - float11ExponentBias) - (float32Val >> float32ExponentFirstBit);
  198. float32Val = ((1 << float32ExponentFirstBit) | (float32Val & float32MantissaMask)) >> shift;
  199. }
  200. else
  201. {
  202. // Rebias the exponent to represent the value as a normalized float11
  203. float32Val += 0xC8000000;
  204. }
  205. return ((float32Val + 0xFFFF + ((float32Val >> 17) & 1)) >> 17) & float11BitMask;
  206. }
  207. }
  208. MOZ_ALWAYS_INLINE uint16_t
  209. packToFloat10(float fp32)
  210. {
  211. const unsigned int float32MantissaMask = 0x7FFFFF;
  212. const unsigned int float32ExponentMask = 0x7F800000;
  213. const unsigned int float32SignMask = 0x80000000;
  214. const unsigned int float32ValueMask = ~float32SignMask;
  215. const unsigned int float32ExponentFirstBit = 23;
  216. const unsigned int float32ExponentBias = 127;
  217. const unsigned short float10Max = 0x3DF;
  218. const unsigned short float10MantissaMask = 0x1F;
  219. const unsigned short float10ExponentMask = 0x3E0;
  220. const unsigned short float10BitMask = 0x3FF;
  221. const unsigned int float10ExponentBias = 14;
  222. const unsigned int float32Maxfloat10 = 0x477C0000;
  223. const unsigned int float32Minfloat10 = 0x38800000;
  224. const unsigned int float32Bits = BitwiseCast<unsigned int>(fp32);
  225. const bool float32Sign = (float32Bits & float32SignMask) == float32SignMask;
  226. unsigned int float32Val = float32Bits & float32ValueMask;
  227. if ((float32Val & float32ExponentMask) == float32ExponentMask)
  228. {
  229. // INF or NAN
  230. if ((float32Val & float32MantissaMask) != 0)
  231. {
  232. return float10ExponentMask | (((float32Val >> 18) | (float32Val >> 13) | (float32Val >> 3) | (float32Val)) & float10MantissaMask);
  233. }
  234. else if (float32Sign)
  235. {
  236. // -INF is clamped to 0 since float11 is positive only
  237. return 0;
  238. }
  239. else
  240. {
  241. return float10ExponentMask;
  242. }
  243. }
  244. else if (float32Sign)
  245. {
  246. // float10 is positive only, so clamp to zero
  247. return 0;
  248. }
  249. else if (float32Val > float32Maxfloat10)
  250. {
  251. // The number is too large to be represented as a float11, set to max
  252. return float10Max;
  253. }
  254. else
  255. {
  256. if (float32Val < float32Minfloat10)
  257. {
  258. // The number is too small to be represented as a normalized float11
  259. // Convert it to a denormalized value.
  260. const unsigned int shift = (float32ExponentBias - float10ExponentBias) - (float32Val >> float32ExponentFirstBit);
  261. float32Val = ((1 << float32ExponentFirstBit) | (float32Val & float32MantissaMask)) >> shift;
  262. }
  263. else
  264. {
  265. // Rebias the exponent to represent the value as a normalized float11
  266. float32Val += 0xC8000000;
  267. }
  268. return ((float32Val + 0x1FFFF + ((float32Val >> 18) & 1)) >> 18) & float10BitMask;
  269. }
  270. }
  271. enum class WebGLTexelPremultiplicationOp : int {
  272. None,
  273. Premultiply,
  274. Unpremultiply
  275. };
  276. namespace WebGLTexelConversions {
  277. template<WebGLTexelFormat Format>
  278. struct IsFloatFormat
  279. {
  280. static const bool Value =
  281. Format == WebGLTexelFormat::A32F ||
  282. Format == WebGLTexelFormat::R32F ||
  283. Format == WebGLTexelFormat::RA32F ||
  284. Format == WebGLTexelFormat::RG32F ||
  285. Format == WebGLTexelFormat::RGB11F11F10F ||
  286. Format == WebGLTexelFormat::RGB32F ||
  287. Format == WebGLTexelFormat::RGBA32F;
  288. };
  289. template<WebGLTexelFormat Format>
  290. struct IsHalfFloatFormat
  291. {
  292. static const bool Value =
  293. Format == WebGLTexelFormat::A16F ||
  294. Format == WebGLTexelFormat::R16F ||
  295. Format == WebGLTexelFormat::RA16F ||
  296. Format == WebGLTexelFormat::RG16F ||
  297. Format == WebGLTexelFormat::RGB16F ||
  298. Format == WebGLTexelFormat::RGBA16F;
  299. };
  300. template<WebGLTexelFormat Format>
  301. struct Is16bppFormat
  302. {
  303. static const bool Value =
  304. Format == WebGLTexelFormat::RGB565 ||
  305. Format == WebGLTexelFormat::RGBA4444 ||
  306. Format == WebGLTexelFormat::RGBA5551;
  307. };
  308. template<WebGLTexelFormat Format,
  309. bool IsFloat = IsFloatFormat<Format>::Value,
  310. bool Is16bpp = Is16bppFormat<Format>::Value,
  311. bool IsHalfFloat = IsHalfFloatFormat<Format>::Value>
  312. struct DataTypeForFormat
  313. {
  314. typedef uint8_t Type;
  315. };
  316. template<WebGLTexelFormat Format>
  317. struct DataTypeForFormat<Format, true, false, false>
  318. {
  319. typedef float Type;
  320. };
  321. template<WebGLTexelFormat Format>
  322. struct DataTypeForFormat<Format, false, true, false>
  323. {
  324. typedef uint16_t Type;
  325. };
  326. template<WebGLTexelFormat Format>
  327. struct DataTypeForFormat<Format, false, false, true>
  328. {
  329. typedef uint16_t Type;
  330. };
  331. template<>
  332. struct DataTypeForFormat<WebGLTexelFormat::RGB11F11F10F, true, false, false>
  333. {
  334. typedef uint32_t Type;
  335. };
  336. template<WebGLTexelFormat Format>
  337. struct IntermediateFormat
  338. {
  339. static const WebGLTexelFormat Value
  340. = IsFloatFormat<Format>::Value
  341. ? WebGLTexelFormat::RGBA32F
  342. : IsHalfFloatFormat<Format>::Value ? WebGLTexelFormat::RGBA16F
  343. : WebGLTexelFormat::RGBA8;
  344. };
  345. inline size_t TexelBytesForFormat(WebGLTexelFormat format) {
  346. switch (format) {
  347. case WebGLTexelFormat::A8:
  348. case WebGLTexelFormat::R8:
  349. return 1;
  350. case WebGLTexelFormat::A16F:
  351. case WebGLTexelFormat::R16F:
  352. case WebGLTexelFormat::RA8:
  353. case WebGLTexelFormat::RG8:
  354. case WebGLTexelFormat::RGB565:
  355. case WebGLTexelFormat::RGBA4444:
  356. case WebGLTexelFormat::RGBA5551:
  357. return 2;
  358. case WebGLTexelFormat::RGB8:
  359. return 3;
  360. case WebGLTexelFormat::A32F:
  361. case WebGLTexelFormat::R32F:
  362. case WebGLTexelFormat::RA16F:
  363. case WebGLTexelFormat::RG16F:
  364. case WebGLTexelFormat::RGB11F11F10F:
  365. case WebGLTexelFormat::RGBA8:
  366. case WebGLTexelFormat::BGRX8:
  367. case WebGLTexelFormat::BGRA8:
  368. return 4;
  369. case WebGLTexelFormat::RGB16F:
  370. return 6;
  371. case WebGLTexelFormat::RA32F:
  372. case WebGLTexelFormat::RG32F:
  373. case WebGLTexelFormat::RGBA16F:
  374. return 8;
  375. case WebGLTexelFormat::RGB32F:
  376. return 12;
  377. case WebGLTexelFormat::RGBA32F:
  378. return 16;
  379. default:
  380. MOZ_ASSERT(false, "Unknown texel format. Coding mistake?");
  381. return 0;
  382. }
  383. }
  384. MOZ_ALWAYS_INLINE bool HasAlpha(WebGLTexelFormat format) {
  385. return (format == WebGLTexelFormat::A8 ||
  386. format == WebGLTexelFormat::A16F ||
  387. format == WebGLTexelFormat::A32F ||
  388. format == WebGLTexelFormat::RA8 ||
  389. format == WebGLTexelFormat::RA16F ||
  390. format == WebGLTexelFormat::RA32F ||
  391. format == WebGLTexelFormat::RGBA4444 ||
  392. format == WebGLTexelFormat::RGBA5551 ||
  393. format == WebGLTexelFormat::RGBA8 ||
  394. format == WebGLTexelFormat::RGBA16F ||
  395. format == WebGLTexelFormat::RGBA32F ||
  396. format == WebGLTexelFormat::BGRA8);
  397. }
  398. MOZ_ALWAYS_INLINE bool HasColor(WebGLTexelFormat format) {
  399. return (format == WebGLTexelFormat::R8 ||
  400. format == WebGLTexelFormat::R16F ||
  401. format == WebGLTexelFormat::R32F ||
  402. format == WebGLTexelFormat::RA8 ||
  403. format == WebGLTexelFormat::RA16F ||
  404. format == WebGLTexelFormat::RA32F ||
  405. format == WebGLTexelFormat::RG8 ||
  406. format == WebGLTexelFormat::RG16F ||
  407. format == WebGLTexelFormat::RG32F ||
  408. format == WebGLTexelFormat::RGB565 ||
  409. format == WebGLTexelFormat::RGB8 ||
  410. format == WebGLTexelFormat::RGB11F11F10F ||
  411. format == WebGLTexelFormat::RGB16F ||
  412. format == WebGLTexelFormat::RGB32F ||
  413. format == WebGLTexelFormat::RGBA4444 ||
  414. format == WebGLTexelFormat::RGBA5551 ||
  415. format == WebGLTexelFormat::RGBA8 ||
  416. format == WebGLTexelFormat::RGBA16F ||
  417. format == WebGLTexelFormat::RGBA32F ||
  418. format == WebGLTexelFormat::BGRX8 ||
  419. format == WebGLTexelFormat::BGRA8);
  420. }
  421. /****** BEGIN CODE SHARED WITH WEBKIT ******/
  422. // the pack/unpack functions here are originally from this file:
  423. // http://trac.webkit.org/browser/trunk/WebCore/platform/graphics/GraphicsContext3D.cpp
  424. //----------------------------------------------------------------------
  425. // Pixel unpacking routines.
  426. template<WebGLTexelFormat Format, typename SrcType, typename DstType>
  427. MOZ_ALWAYS_INLINE void
  428. unpack(const SrcType* __restrict src,
  429. DstType* __restrict dst)
  430. {
  431. MOZ_ASSERT(false, "Unimplemented texture format conversion");
  432. }
  433. ////////////////////////////////////////////////////////////////////////////////
  434. // 1-channel formats
  435. template<> MOZ_ALWAYS_INLINE void
  436. unpack<WebGLTexelFormat::A8, uint8_t, uint8_t>(const uint8_t* __restrict src, uint8_t* __restrict dst)
  437. {
  438. dst[0] = 0;
  439. dst[1] = 0;
  440. dst[2] = 0;
  441. dst[3] = src[0];
  442. }
  443. template<> MOZ_ALWAYS_INLINE void
  444. unpack<WebGLTexelFormat::A16F, uint16_t, uint16_t>(const uint16_t* __restrict src, uint16_t* __restrict dst)
  445. {
  446. dst[0] = kFloat16Value_Zero;
  447. dst[1] = kFloat16Value_Zero;
  448. dst[2] = kFloat16Value_Zero;
  449. dst[3] = src[0];
  450. }
  451. template<> MOZ_ALWAYS_INLINE void
  452. unpack<WebGLTexelFormat::A32F, float, float>(const float* __restrict src, float* __restrict dst)
  453. {
  454. dst[0] = 0;
  455. dst[1] = 0;
  456. dst[2] = 0;
  457. dst[3] = src[0];
  458. }
  459. template<> MOZ_ALWAYS_INLINE void
  460. unpack<WebGLTexelFormat::R8, uint8_t, uint8_t>(const uint8_t* __restrict src, uint8_t* __restrict dst)
  461. {
  462. dst[0] = src[0];
  463. dst[1] = src[0];
  464. dst[2] = src[0];
  465. dst[3] = 0xFF;
  466. }
  467. template<> MOZ_ALWAYS_INLINE void
  468. unpack<WebGLTexelFormat::R16F, uint16_t, uint16_t>(const uint16_t* __restrict src, uint16_t* __restrict dst)
  469. {
  470. dst[0] = src[0];
  471. dst[1] = src[0];
  472. dst[2] = src[0];
  473. dst[3] = kFloat16Value_One;
  474. }
  475. template<> MOZ_ALWAYS_INLINE void
  476. unpack<WebGLTexelFormat::R32F, float, float>(const float* __restrict src, float* __restrict dst)
  477. {
  478. dst[0] = src[0];
  479. dst[1] = src[0];
  480. dst[2] = src[0];
  481. dst[3] = 1.0f;
  482. }
  483. ////////////////////////////////////////////////////////////////////////////////
  484. // 2-channel formats
  485. template<> MOZ_ALWAYS_INLINE void
  486. unpack<WebGLTexelFormat::RA8, uint8_t, uint8_t>(const uint8_t* __restrict src, uint8_t* __restrict dst)
  487. {
  488. dst[0] = src[0];
  489. dst[1] = src[0];
  490. dst[2] = src[0];
  491. dst[3] = src[1];
  492. }
  493. template<> MOZ_ALWAYS_INLINE void
  494. unpack<WebGLTexelFormat::RA16F, uint16_t, uint16_t>(const uint16_t* __restrict src, uint16_t* __restrict dst)
  495. {
  496. dst[0] = src[0];
  497. dst[1] = src[0];
  498. dst[2] = src[0];
  499. dst[3] = src[1];
  500. }
  501. template<> MOZ_ALWAYS_INLINE void
  502. unpack<WebGLTexelFormat::RA32F, float, float>(const float* __restrict src, float* __restrict dst)
  503. {
  504. dst[0] = src[0];
  505. dst[1] = src[0];
  506. dst[2] = src[0];
  507. dst[3] = src[1];
  508. }
  509. ////////////////////////////////////////////////////////////////////////////////
  510. // 3-channel formats
  511. template<> MOZ_ALWAYS_INLINE void
  512. unpack<WebGLTexelFormat::RGB565, uint16_t, uint8_t>(const uint16_t* __restrict src, uint8_t* __restrict dst)
  513. {
  514. uint16_t packedValue = src[0];
  515. uint8_t r = (packedValue >> 11) & 0x1F;
  516. uint8_t g = (packedValue >> 5) & 0x3F;
  517. uint8_t b = packedValue & 0x1F;
  518. dst[0] = (r << 3) | (r & 0x7);
  519. dst[1] = (g << 2) | (g & 0x3);
  520. dst[2] = (b << 3) | (b & 0x7);
  521. dst[3] = 0xFF;
  522. }
  523. template<> MOZ_ALWAYS_INLINE void
  524. unpack<WebGLTexelFormat::RGB8, uint8_t, uint8_t>(const uint8_t* __restrict src, uint8_t* __restrict dst)
  525. {
  526. dst[0] = src[0];
  527. dst[1] = src[1];
  528. dst[2] = src[2];
  529. dst[3] = 0xFF;
  530. }
  531. template<> MOZ_ALWAYS_INLINE void
  532. unpack<WebGLTexelFormat::RGB16F, uint16_t, uint16_t>(const uint16_t* __restrict src, uint16_t* __restrict dst)
  533. {
  534. dst[0] = src[0];
  535. dst[1] = src[1];
  536. dst[2] = src[2];
  537. dst[3] = kFloat16Value_One;
  538. }
  539. template<> MOZ_ALWAYS_INLINE void
  540. unpack<WebGLTexelFormat::RGB32F, float, float>(const float* __restrict src, float* __restrict dst)
  541. {
  542. dst[0] = src[0];
  543. dst[1] = src[1];
  544. dst[2] = src[2];
  545. dst[3] = 1.0f;
  546. }
  547. ////////////////////////////////////////////////////////////////////////////////
  548. // 4-channel formats
  549. template<> MOZ_ALWAYS_INLINE void
  550. unpack<WebGLTexelFormat::RGBA4444, uint16_t, uint8_t>(const uint16_t* __restrict src, uint8_t* __restrict dst)
  551. {
  552. uint16_t packedValue = src[0];
  553. uint8_t r = (packedValue >> 12) & 0x0F;
  554. uint8_t g = (packedValue >> 8) & 0x0F;
  555. uint8_t b = (packedValue >> 4) & 0x0F;
  556. uint8_t a = packedValue & 0x0F;
  557. dst[0] = (r << 4) | r;
  558. dst[1] = (g << 4) | g;
  559. dst[2] = (b << 4) | b;
  560. dst[3] = (a << 4) | a;
  561. }
  562. template<> MOZ_ALWAYS_INLINE void
  563. unpack<WebGLTexelFormat::RGBA5551, uint16_t, uint8_t>(const uint16_t* __restrict src, uint8_t* __restrict dst)
  564. {
  565. uint16_t packedValue = src[0];
  566. uint8_t r = (packedValue >> 11) & 0x1F;
  567. uint8_t g = (packedValue >> 6) & 0x1F;
  568. uint8_t b = (packedValue >> 1) & 0x1F;
  569. dst[0] = (r << 3) | (r & 0x7);
  570. dst[1] = (g << 3) | (g & 0x7);
  571. dst[2] = (b << 3) | (b & 0x7);
  572. dst[3] = (packedValue & 0x1) ? 0xFF : 0;
  573. }
  574. template<> MOZ_ALWAYS_INLINE void
  575. unpack<WebGLTexelFormat::RGBA8, uint8_t, uint8_t>(const uint8_t* __restrict src, uint8_t* __restrict dst)
  576. {
  577. dst[0] = src[0];
  578. dst[1] = src[1];
  579. dst[2] = src[2];
  580. dst[3] = src[3];
  581. }
  582. template<> MOZ_ALWAYS_INLINE void
  583. unpack<WebGLTexelFormat::RGBA16F, uint16_t, uint16_t>(const uint16_t* __restrict src, uint16_t* __restrict dst)
  584. {
  585. dst[0] = src[0];
  586. dst[1] = src[1];
  587. dst[2] = src[2];
  588. dst[3] = src[3];
  589. }
  590. template<> MOZ_ALWAYS_INLINE void
  591. unpack<WebGLTexelFormat::RGBA32F, float, float>(const float* __restrict src, float* __restrict dst)
  592. {
  593. dst[0] = src[0];
  594. dst[1] = src[1];
  595. dst[2] = src[2];
  596. dst[3] = src[3];
  597. }
  598. ////////////////////////////////////////////////////////////////////////////////
  599. // DOM element formats
  600. template<> MOZ_ALWAYS_INLINE void
  601. unpack<WebGLTexelFormat::BGRX8, uint8_t, uint8_t>(const uint8_t* __restrict src, uint8_t* __restrict dst)
  602. {
  603. dst[0] = src[2];
  604. dst[1] = src[1];
  605. dst[2] = src[0];
  606. dst[3] = 0xFF;
  607. }
  608. template<> MOZ_ALWAYS_INLINE void
  609. unpack<WebGLTexelFormat::BGRA8, uint8_t, uint8_t>(const uint8_t* __restrict src, uint8_t* __restrict dst)
  610. {
  611. dst[0] = src[2];
  612. dst[1] = src[1];
  613. dst[2] = src[0];
  614. dst[3] = src[3];
  615. }
  616. //----------------------------------------------------------------------
  617. // Pixel packing routines.
  618. //
  619. template<WebGLTexelFormat Format,
  620. WebGLTexelPremultiplicationOp PremultiplicationOp,
  621. typename SrcType,
  622. typename DstType>
  623. MOZ_ALWAYS_INLINE void
  624. pack(const SrcType* __restrict src,
  625. DstType* __restrict dst)
  626. {
  627. MOZ_CRASH("GFX: Unimplemented texture format conversion");
  628. }
  629. ////////////////////////////////////////////////////////////////////////////////
  630. // 1-channel formats
  631. template<> MOZ_ALWAYS_INLINE void
  632. pack<WebGLTexelFormat::A8, WebGLTexelPremultiplicationOp::None, uint8_t, uint8_t>(const uint8_t* __restrict src, uint8_t* __restrict dst)
  633. {
  634. dst[0] = src[3];
  635. }
  636. template<> MOZ_ALWAYS_INLINE void
  637. pack<WebGLTexelFormat::A8, WebGLTexelPremultiplicationOp::Premultiply, uint8_t, uint8_t>(const uint8_t* __restrict src, uint8_t* __restrict dst)
  638. {
  639. dst[0] = src[3];
  640. }
  641. template<> MOZ_ALWAYS_INLINE void
  642. pack<WebGLTexelFormat::A8, WebGLTexelPremultiplicationOp::Unpremultiply, uint8_t, uint8_t>(const uint8_t* __restrict src, uint8_t* __restrict dst)
  643. {
  644. dst[0] = src[3];
  645. }
  646. template<> MOZ_ALWAYS_INLINE void
  647. pack<WebGLTexelFormat::A16F, WebGLTexelPremultiplicationOp::None, uint16_t, uint16_t>(const uint16_t* __restrict src, uint16_t* __restrict dst)
  648. {
  649. dst[0] = src[3];
  650. }
  651. template<> MOZ_ALWAYS_INLINE void
  652. pack<WebGLTexelFormat::A16F, WebGLTexelPremultiplicationOp::Premultiply, uint16_t, uint16_t>(const uint16_t* __restrict src, uint16_t* __restrict dst)
  653. {
  654. dst[0] = src[3];
  655. }
  656. template<> MOZ_ALWAYS_INLINE void
  657. pack<WebGLTexelFormat::A16F, WebGLTexelPremultiplicationOp::Unpremultiply, uint16_t, uint16_t>(const uint16_t* __restrict src, uint16_t* __restrict dst)
  658. {
  659. dst[0] = src[3];
  660. }
  661. template<> MOZ_ALWAYS_INLINE void
  662. pack<WebGLTexelFormat::A32F, WebGLTexelPremultiplicationOp::None, float, float>(const float* __restrict src, float* __restrict dst)
  663. {
  664. dst[0] = src[3];
  665. }
  666. template<> MOZ_ALWAYS_INLINE void
  667. pack<WebGLTexelFormat::A32F, WebGLTexelPremultiplicationOp::Premultiply, float, float>(const float* __restrict src, float* __restrict dst)
  668. {
  669. dst[0] = src[3];
  670. }
  671. template<> MOZ_ALWAYS_INLINE void
  672. pack<WebGLTexelFormat::A32F, WebGLTexelPremultiplicationOp::Unpremultiply, float, float>(const float* __restrict src, float* __restrict dst)
  673. {
  674. dst[0] = src[3];
  675. }
  676. template<> MOZ_ALWAYS_INLINE void
  677. pack<WebGLTexelFormat::R8, WebGLTexelPremultiplicationOp::None, uint8_t, uint8_t>(const uint8_t* __restrict src, uint8_t* __restrict dst)
  678. {
  679. dst[0] = src[0];
  680. }
  681. template<> MOZ_ALWAYS_INLINE void
  682. pack<WebGLTexelFormat::R8, WebGLTexelPremultiplicationOp::Premultiply, uint8_t, uint8_t>(const uint8_t* __restrict src, uint8_t* __restrict dst)
  683. {
  684. float scaleFactor = src[3] / 255.0f;
  685. uint8_t srcR = static_cast<uint8_t>(src[0] * scaleFactor);
  686. dst[0] = srcR;
  687. }
  688. template<> MOZ_ALWAYS_INLINE void
  689. pack<WebGLTexelFormat::R8, WebGLTexelPremultiplicationOp::Unpremultiply, uint8_t, uint8_t>(const uint8_t* __restrict src, uint8_t* __restrict dst)
  690. {
  691. float scaleFactor = src[3] ? 255.0f / src[3] : 1.0f;
  692. uint8_t srcR = static_cast<uint8_t>(src[0] * scaleFactor);
  693. dst[0] = srcR;
  694. }
  695. template<> MOZ_ALWAYS_INLINE void
  696. pack<WebGLTexelFormat::R16F, WebGLTexelPremultiplicationOp::None, uint16_t, uint16_t>(const uint16_t* __restrict src, uint16_t* __restrict dst)
  697. {
  698. dst[0] = src[0];
  699. }
  700. template<> MOZ_ALWAYS_INLINE void
  701. pack<WebGLTexelFormat::R16F, WebGLTexelPremultiplicationOp::Premultiply, uint16_t, uint16_t>(const uint16_t* __restrict src, uint16_t* __restrict dst)
  702. {
  703. float scaleFactor = unpackFromFloat16(src[3]);
  704. dst[0] = packToFloat16(unpackFromFloat16(src[0]) * scaleFactor);
  705. }
  706. template<> MOZ_ALWAYS_INLINE void
  707. pack<WebGLTexelFormat::R16F, WebGLTexelPremultiplicationOp::Unpremultiply, uint16_t, uint16_t>(const uint16_t* __restrict src, uint16_t* __restrict dst)
  708. {
  709. float unpackedAlpha = unpackFromFloat16(src[3]);
  710. float scaleFactor = unpackedAlpha ? 1.0f / unpackedAlpha : 1.0f;
  711. dst[0] = packToFloat16(unpackFromFloat16(src[0]) * scaleFactor);
  712. }
  713. template<> MOZ_ALWAYS_INLINE void
  714. pack<WebGLTexelFormat::R32F, WebGLTexelPremultiplicationOp::None, float, float>(const float* __restrict src, float* __restrict dst)
  715. {
  716. dst[0] = src[0];
  717. }
  718. template<> MOZ_ALWAYS_INLINE void
  719. pack<WebGLTexelFormat::R32F, WebGLTexelPremultiplicationOp::Premultiply, float, float>(const float* __restrict src, float* __restrict dst)
  720. {
  721. float scaleFactor = src[3];
  722. dst[0] = src[0] * scaleFactor;
  723. }
  724. template<> MOZ_ALWAYS_INLINE void
  725. pack<WebGLTexelFormat::R32F, WebGLTexelPremultiplicationOp::Unpremultiply, float, float>(const float* __restrict src, float* __restrict dst)
  726. {
  727. float scaleFactor = src[3] ? 1.0f / src[3] : 1.0f;
  728. dst[0] = src[0] * scaleFactor;
  729. }
  730. ////////////////////////////////////////////////////////////////////////////////
  731. // 2-channel formats
  732. template<> MOZ_ALWAYS_INLINE void
  733. pack<WebGLTexelFormat::RA8, WebGLTexelPremultiplicationOp::None, uint8_t, uint8_t>(const uint8_t* __restrict src, uint8_t* __restrict dst)
  734. {
  735. dst[0] = src[0];
  736. dst[1] = src[3];
  737. }
  738. template<> MOZ_ALWAYS_INLINE void
  739. pack<WebGLTexelFormat::RA8, WebGLTexelPremultiplicationOp::Premultiply, uint8_t, uint8_t>(const uint8_t* __restrict src, uint8_t* __restrict dst)
  740. {
  741. float scaleFactor = src[3] / 255.0f;
  742. uint8_t srcR = static_cast<uint8_t>(src[0] * scaleFactor);
  743. dst[0] = srcR;
  744. dst[1] = src[3];
  745. }
  746. // FIXME: this routine is lossy and must be removed.
  747. template<> MOZ_ALWAYS_INLINE void
  748. pack<WebGLTexelFormat::RA8, WebGLTexelPremultiplicationOp::Unpremultiply, uint8_t, uint8_t>(const uint8_t* __restrict src, uint8_t* __restrict dst)
  749. {
  750. float scaleFactor = src[3] ? 255.0f / src[3] : 1.0f;
  751. uint8_t srcR = static_cast<uint8_t>(src[0] * scaleFactor);
  752. dst[0] = srcR;
  753. dst[1] = src[3];
  754. }
  755. template<> MOZ_ALWAYS_INLINE void
  756. pack<WebGLTexelFormat::RA16F, WebGLTexelPremultiplicationOp::None, uint16_t, uint16_t>(const uint16_t* __restrict src, uint16_t* __restrict dst)
  757. {
  758. dst[0] = src[0];
  759. dst[1] = src[3];
  760. }
  761. template<> MOZ_ALWAYS_INLINE void
  762. pack<WebGLTexelFormat::RA16F, WebGLTexelPremultiplicationOp::Premultiply, uint16_t, uint16_t>(const uint16_t* __restrict src, uint16_t* __restrict dst)
  763. {
  764. float scaleFactor = unpackFromFloat16(src[3]);
  765. dst[0] = packToFloat16(unpackFromFloat16(src[0]) * scaleFactor);
  766. dst[1] = src[3];
  767. }
  768. template<> MOZ_ALWAYS_INLINE void
  769. pack<WebGLTexelFormat::RA16F, WebGLTexelPremultiplicationOp::Unpremultiply, uint16_t, uint16_t>(const uint16_t* __restrict src, uint16_t* __restrict dst)
  770. {
  771. float unpackedAlpha = unpackFromFloat16(src[3]);
  772. float scaleFactor = unpackedAlpha ? 1.0f / unpackedAlpha : 1.0f;
  773. dst[0] = packToFloat16(unpackFromFloat16(src[0]) * scaleFactor);
  774. dst[1] = src[3];
  775. }
  776. template<> MOZ_ALWAYS_INLINE void
  777. pack<WebGLTexelFormat::RA32F, WebGLTexelPremultiplicationOp::None, float, float>(const float* __restrict src, float* __restrict dst)
  778. {
  779. dst[0] = src[0];
  780. dst[1] = src[3];
  781. }
  782. template<> MOZ_ALWAYS_INLINE void
  783. pack<WebGLTexelFormat::RA32F, WebGLTexelPremultiplicationOp::Premultiply, float, float>(const float* __restrict src, float* __restrict dst)
  784. {
  785. float scaleFactor = src[3];
  786. dst[0] = src[0] * scaleFactor;
  787. dst[1] = src[3];
  788. }
  789. template<> MOZ_ALWAYS_INLINE void
  790. pack<WebGLTexelFormat::RA32F, WebGLTexelPremultiplicationOp::Unpremultiply, float, float>(const float* __restrict src, float* __restrict dst)
  791. {
  792. float scaleFactor = src[3] ? 1.0f / src[3] : 1.0f;
  793. dst[0] = src[0] * scaleFactor;
  794. dst[1] = src[3];
  795. }
  796. template<> MOZ_ALWAYS_INLINE void
  797. pack<WebGLTexelFormat::RG8, WebGLTexelPremultiplicationOp::None, uint8_t, uint8_t>(const uint8_t* __restrict src, uint8_t* __restrict dst)
  798. {
  799. dst[0] = src[0];
  800. dst[1] = src[1];
  801. }
  802. template<> MOZ_ALWAYS_INLINE void
  803. pack<WebGLTexelFormat::RG8, WebGLTexelPremultiplicationOp::Premultiply, uint8_t, uint8_t>(const uint8_t* __restrict src, uint8_t* __restrict dst)
  804. {
  805. float scaleFactor = src[3] / 255.0f;
  806. uint8_t srcR = static_cast<uint8_t>(src[0] * scaleFactor);
  807. uint8_t srcG = static_cast<uint8_t>(src[1] * scaleFactor);
  808. dst[0] = srcR;
  809. dst[1] = srcG;
  810. }
  811. // FIXME: this routine is lossy and must be removed.
  812. template<> MOZ_ALWAYS_INLINE void
  813. pack<WebGLTexelFormat::RG8, WebGLTexelPremultiplicationOp::Unpremultiply, uint8_t, uint8_t>(const uint8_t* __restrict src, uint8_t* __restrict dst)
  814. {
  815. float scaleFactor = src[3] ? 255.0f / src[3] : 1.0f;
  816. uint8_t srcR = static_cast<uint8_t>(src[0] * scaleFactor);
  817. uint8_t srcG = static_cast<uint8_t>(src[1] * scaleFactor);
  818. dst[0] = srcR;
  819. dst[1] = srcG;
  820. }
  821. template<> MOZ_ALWAYS_INLINE void
  822. pack<WebGLTexelFormat::RG16F, WebGLTexelPremultiplicationOp::None, uint16_t, uint16_t>(const uint16_t* __restrict src, uint16_t* __restrict dst)
  823. {
  824. dst[0] = src[0];
  825. dst[1] = src[1];
  826. }
  827. template<> MOZ_ALWAYS_INLINE void
  828. pack<WebGLTexelFormat::RG16F, WebGLTexelPremultiplicationOp::Premultiply, uint16_t, uint16_t>(const uint16_t* __restrict src, uint16_t* __restrict dst)
  829. {
  830. float scaleFactor = unpackFromFloat16(src[3]);
  831. dst[0] = packToFloat16(unpackFromFloat16(src[0]) * scaleFactor);
  832. dst[1] = packToFloat16(unpackFromFloat16(src[1]) * scaleFactor);
  833. }
  834. template<> MOZ_ALWAYS_INLINE void
  835. pack<WebGLTexelFormat::RG16F, WebGLTexelPremultiplicationOp::Unpremultiply, uint16_t, uint16_t>(const uint16_t* __restrict src, uint16_t* __restrict dst)
  836. {
  837. float unpackedAlpha = unpackFromFloat16(src[3]);
  838. float scaleFactor = unpackedAlpha ? 1.0f / unpackedAlpha : 1.0f;
  839. dst[0] = packToFloat16(unpackFromFloat16(src[0]) * scaleFactor);
  840. dst[1] = packToFloat16(unpackFromFloat16(src[1]) * scaleFactor);
  841. }
  842. template<> MOZ_ALWAYS_INLINE void
  843. pack<WebGLTexelFormat::RG32F, WebGLTexelPremultiplicationOp::None, float, float>(const float* __restrict src, float* __restrict dst)
  844. {
  845. dst[0] = src[0];
  846. dst[1] = src[1];
  847. }
  848. template<> MOZ_ALWAYS_INLINE void
  849. pack<WebGLTexelFormat::RG32F, WebGLTexelPremultiplicationOp::Premultiply, float, float>(const float* __restrict src, float* __restrict dst)
  850. {
  851. float scaleFactor = src[3];
  852. dst[0] = src[0] * scaleFactor;
  853. dst[1] = src[1] * scaleFactor;
  854. }
  855. template<> MOZ_ALWAYS_INLINE void
  856. pack<WebGLTexelFormat::RG32F, WebGLTexelPremultiplicationOp::Unpremultiply, float, float>(const float* __restrict src, float* __restrict dst)
  857. {
  858. float scaleFactor = src[3] ? 1.0f / src[3] : 1.0f;
  859. dst[0] = src[0] * scaleFactor;
  860. dst[1] = src[1] * scaleFactor;
  861. }
  862. ////////////////////////////////////////////////////////////////////////////////
  863. // 3-channel formats
  864. template<> MOZ_ALWAYS_INLINE void
  865. pack<WebGLTexelFormat::RGB565, WebGLTexelPremultiplicationOp::None, uint8_t, uint16_t>(const uint8_t* __restrict src, uint16_t* __restrict dst)
  866. {
  867. *dst = ( ((src[0] & 0xF8) << 8)
  868. | ((src[1] & 0xFC) << 3)
  869. | ((src[2] & 0xF8) >> 3));
  870. }
  871. template<> MOZ_ALWAYS_INLINE void
  872. pack<WebGLTexelFormat::RGB565, WebGLTexelPremultiplicationOp::Premultiply, uint8_t, uint16_t>(const uint8_t* __restrict src, uint16_t* __restrict dst)
  873. {
  874. float scaleFactor = src[3] / 255.0f;
  875. uint8_t srcR = static_cast<uint8_t>(src[0] * scaleFactor);
  876. uint8_t srcG = static_cast<uint8_t>(src[1] * scaleFactor);
  877. uint8_t srcB = static_cast<uint8_t>(src[2] * scaleFactor);
  878. *dst = ( ((srcR & 0xF8) << 8)
  879. | ((srcG & 0xFC) << 3)
  880. | ((srcB & 0xF8) >> 3));
  881. }
  882. // FIXME: this routine is lossy and must be removed.
  883. template<> MOZ_ALWAYS_INLINE void
  884. pack<WebGLTexelFormat::RGB565, WebGLTexelPremultiplicationOp::Unpremultiply, uint8_t, uint16_t>(const uint8_t* __restrict src, uint16_t* __restrict dst)
  885. {
  886. float scaleFactor = src[3] ? 255.0f / src[3] : 1.0f;
  887. uint8_t srcR = static_cast<uint8_t>(src[0] * scaleFactor);
  888. uint8_t srcG = static_cast<uint8_t>(src[1] * scaleFactor);
  889. uint8_t srcB = static_cast<uint8_t>(src[2] * scaleFactor);
  890. *dst = ( ((srcR & 0xF8) << 8)
  891. | ((srcG & 0xFC) << 3)
  892. | ((srcB & 0xF8) >> 3));
  893. }
  894. template<> MOZ_ALWAYS_INLINE void
  895. pack<WebGLTexelFormat::RGB8, WebGLTexelPremultiplicationOp::None, uint8_t, uint8_t>(const uint8_t* __restrict src, uint8_t* __restrict dst)
  896. {
  897. dst[0] = src[0];
  898. dst[1] = src[1];
  899. dst[2] = src[2];
  900. }
  901. template<> MOZ_ALWAYS_INLINE void
  902. pack<WebGLTexelFormat::RGB8, WebGLTexelPremultiplicationOp::Premultiply, uint8_t, uint8_t>(const uint8_t* __restrict src, uint8_t* __restrict dst)
  903. {
  904. float scaleFactor = src[3] / 255.0f;
  905. uint8_t srcR = static_cast<uint8_t>(src[0] * scaleFactor);
  906. uint8_t srcG = static_cast<uint8_t>(src[1] * scaleFactor);
  907. uint8_t srcB = static_cast<uint8_t>(src[2] * scaleFactor);
  908. dst[0] = srcR;
  909. dst[1] = srcG;
  910. dst[2] = srcB;
  911. }
  912. template<> MOZ_ALWAYS_INLINE void
  913. pack<WebGLTexelFormat::RGB8, WebGLTexelPremultiplicationOp::Unpremultiply, uint8_t, uint8_t>(const uint8_t* __restrict src, uint8_t* __restrict dst)
  914. {
  915. float scaleFactor = src[3] ? 255.0f / src[3] : 1.0f;
  916. uint8_t srcR = static_cast<uint8_t>(src[0] * scaleFactor);
  917. uint8_t srcG = static_cast<uint8_t>(src[1] * scaleFactor);
  918. uint8_t srcB = static_cast<uint8_t>(src[2] * scaleFactor);
  919. dst[0] = srcR;
  920. dst[1] = srcG;
  921. dst[2] = srcB;
  922. }
  923. template<> MOZ_ALWAYS_INLINE void
  924. pack<WebGLTexelFormat::RGB11F11F10F, WebGLTexelPremultiplicationOp::None, float, uint32_t>(const float* __restrict src, uint32_t* __restrict dst)
  925. {
  926. dst[0] = ((packToFloat11(src[0]) << 0) |
  927. (packToFloat11(src[1]) << 11) |
  928. (packToFloat10(src[2]) << 22));
  929. }
  930. template<> MOZ_ALWAYS_INLINE void
  931. pack<WebGLTexelFormat::RGB11F11F10F, WebGLTexelPremultiplicationOp::Premultiply, float, uint32_t>(const float* __restrict src, uint32_t* __restrict dst)
  932. {
  933. float scaleFactor = src[3];
  934. dst[0] = ((packToFloat11(src[0] * scaleFactor) << 0) |
  935. (packToFloat11(src[1] * scaleFactor) << 11) |
  936. (packToFloat10(src[2] * scaleFactor) << 22));
  937. }
  938. template<> MOZ_ALWAYS_INLINE void
  939. pack<WebGLTexelFormat::RGB11F11F10F, WebGLTexelPremultiplicationOp::Unpremultiply, float, uint32_t>(const float* __restrict src, uint32_t* __restrict dst)
  940. {
  941. float scaleFactor = src[3] ? 1.0f / src[3] : 1.0f;
  942. dst[0] = ((packToFloat11(src[0] * scaleFactor) << 0) |
  943. (packToFloat11(src[1] * scaleFactor) << 11) |
  944. (packToFloat10(src[2] * scaleFactor) << 22));
  945. }
  946. template<> MOZ_ALWAYS_INLINE void
  947. pack<WebGLTexelFormat::RGB16F, WebGLTexelPremultiplicationOp::None, uint16_t, uint16_t>(const uint16_t* __restrict src, uint16_t* __restrict dst)
  948. {
  949. dst[0] = src[0];
  950. dst[1] = src[1];
  951. dst[2] = src[2];
  952. }
  953. template<> MOZ_ALWAYS_INLINE void
  954. pack<WebGLTexelFormat::RGB16F, WebGLTexelPremultiplicationOp::Premultiply, uint16_t, uint16_t>(const uint16_t* __restrict src, uint16_t* __restrict dst)
  955. {
  956. float scaleFactor = unpackFromFloat16(src[3]);
  957. dst[0] = packToFloat16(unpackFromFloat16(src[0]) * scaleFactor);
  958. dst[1] = packToFloat16(unpackFromFloat16(src[1]) * scaleFactor);
  959. dst[2] = packToFloat16(unpackFromFloat16(src[2]) * scaleFactor);
  960. }
  961. template<> MOZ_ALWAYS_INLINE void
  962. pack<WebGLTexelFormat::RGB16F, WebGLTexelPremultiplicationOp::Unpremultiply, uint16_t, uint16_t>(const uint16_t* __restrict src, uint16_t* __restrict dst)
  963. {
  964. float unpackedAlpha = unpackFromFloat16(src[3]);
  965. float scaleFactor = unpackedAlpha ? 1.0f / unpackedAlpha : 1.0f;
  966. dst[0] = packToFloat16(unpackFromFloat16(src[0]) * scaleFactor);
  967. dst[1] = packToFloat16(unpackFromFloat16(src[1]) * scaleFactor);
  968. dst[2] = packToFloat16(unpackFromFloat16(src[2]) * scaleFactor);
  969. }
  970. template<> MOZ_ALWAYS_INLINE void
  971. pack<WebGLTexelFormat::RGB32F, WebGLTexelPremultiplicationOp::None, float, float>(const float* __restrict src, float* __restrict dst)
  972. {
  973. dst[0] = src[0];
  974. dst[1] = src[1];
  975. dst[2] = src[2];
  976. }
  977. template<> MOZ_ALWAYS_INLINE void
  978. pack<WebGLTexelFormat::RGB32F, WebGLTexelPremultiplicationOp::Premultiply, float, float>(const float* __restrict src, float* __restrict dst)
  979. {
  980. float scaleFactor = src[3];
  981. dst[0] = src[0] * scaleFactor;
  982. dst[1] = src[1] * scaleFactor;
  983. dst[2] = src[2] * scaleFactor;
  984. }
  985. template<> MOZ_ALWAYS_INLINE void
  986. pack<WebGLTexelFormat::RGB32F, WebGLTexelPremultiplicationOp::Unpremultiply, float, float>(const float* __restrict src, float* __restrict dst)
  987. {
  988. float scaleFactor = src[3] ? 1.0f / src[3] : 1.0f;
  989. dst[0] = src[0] * scaleFactor;
  990. dst[1] = src[1] * scaleFactor;
  991. dst[2] = src[2] * scaleFactor;
  992. }
  993. ////////////////////////////////////////////////////////////////////////////////
  994. // 4-channel formats
  995. template<> MOZ_ALWAYS_INLINE void
  996. pack<WebGLTexelFormat::RGBA4444, WebGLTexelPremultiplicationOp::None, uint8_t, uint16_t>(const uint8_t* __restrict src, uint16_t* __restrict dst)
  997. {
  998. *dst = ( ((src[0] & 0xF0) << 8)
  999. | ((src[1] & 0xF0) << 4)
  1000. | (src[2] & 0xF0)
  1001. | (src[3] >> 4) );
  1002. }
  1003. template<> MOZ_ALWAYS_INLINE void
  1004. pack<WebGLTexelFormat::RGBA4444, WebGLTexelPremultiplicationOp::Premultiply, uint8_t, uint16_t>(const uint8_t* __restrict src, uint16_t* __restrict dst)
  1005. {
  1006. float scaleFactor = src[3] / 255.0f;
  1007. uint8_t srcR = static_cast<uint8_t>(src[0] * scaleFactor);
  1008. uint8_t srcG = static_cast<uint8_t>(src[1] * scaleFactor);
  1009. uint8_t srcB = static_cast<uint8_t>(src[2] * scaleFactor);
  1010. *dst = ( ((srcR & 0xF0) << 8)
  1011. | ((srcG & 0xF0) << 4)
  1012. | (srcB & 0xF0)
  1013. | (src[3] >> 4));
  1014. }
  1015. // FIXME: this routine is lossy and must be removed.
  1016. template<> MOZ_ALWAYS_INLINE void
  1017. pack<WebGLTexelFormat::RGBA4444, WebGLTexelPremultiplicationOp::Unpremultiply, uint8_t, uint16_t>(const uint8_t* __restrict src, uint16_t* __restrict dst)
  1018. {
  1019. float scaleFactor = src[3] ? 255.0f / src[3] : 1.0f;
  1020. uint8_t srcR = static_cast<uint8_t>(src[0] * scaleFactor);
  1021. uint8_t srcG = static_cast<uint8_t>(src[1] * scaleFactor);
  1022. uint8_t srcB = static_cast<uint8_t>(src[2] * scaleFactor);
  1023. *dst = ( ((srcR & 0xF0) << 8)
  1024. | ((srcG & 0xF0) << 4)
  1025. | (srcB & 0xF0)
  1026. | (src[3] >> 4));
  1027. }
  1028. template<> MOZ_ALWAYS_INLINE void
  1029. pack<WebGLTexelFormat::RGBA5551, WebGLTexelPremultiplicationOp::None, uint8_t, uint16_t>(const uint8_t* __restrict src, uint16_t* __restrict dst)
  1030. {
  1031. *dst = ( ((src[0] & 0xF8) << 8)
  1032. | ((src[1] & 0xF8) << 3)
  1033. | ((src[2] & 0xF8) >> 2)
  1034. | (src[3] >> 7));
  1035. }
  1036. template<> MOZ_ALWAYS_INLINE void
  1037. pack<WebGLTexelFormat::RGBA5551, WebGLTexelPremultiplicationOp::Premultiply, uint8_t, uint16_t>(const uint8_t* __restrict src, uint16_t* __restrict dst)
  1038. {
  1039. float scaleFactor = src[3] / 255.0f;
  1040. uint8_t srcR = static_cast<uint8_t>(src[0] * scaleFactor);
  1041. uint8_t srcG = static_cast<uint8_t>(src[1] * scaleFactor);
  1042. uint8_t srcB = static_cast<uint8_t>(src[2] * scaleFactor);
  1043. *dst = ( ((srcR & 0xF8) << 8)
  1044. | ((srcG & 0xF8) << 3)
  1045. | ((srcB & 0xF8) >> 2)
  1046. | (src[3] >> 7));
  1047. }
  1048. // FIXME: this routine is lossy and must be removed.
  1049. template<> MOZ_ALWAYS_INLINE void
  1050. pack<WebGLTexelFormat::RGBA5551, WebGLTexelPremultiplicationOp::Unpremultiply, uint8_t, uint16_t>(const uint8_t* __restrict src, uint16_t* __restrict dst)
  1051. {
  1052. float scaleFactor = src[3] ? 255.0f / src[3] : 1.0f;
  1053. uint8_t srcR = static_cast<uint8_t>(src[0] * scaleFactor);
  1054. uint8_t srcG = static_cast<uint8_t>(src[1] * scaleFactor);
  1055. uint8_t srcB = static_cast<uint8_t>(src[2] * scaleFactor);
  1056. *dst = ( ((srcR & 0xF8) << 8)
  1057. | ((srcG & 0xF8) << 3)
  1058. | ((srcB & 0xF8) >> 2)
  1059. | (src[3] >> 7));
  1060. }
  1061. template<> MOZ_ALWAYS_INLINE void
  1062. pack<WebGLTexelFormat::RGBA8, WebGLTexelPremultiplicationOp::None, uint8_t, uint8_t>(const uint8_t* __restrict src, uint8_t* __restrict dst)
  1063. {
  1064. dst[0] = src[0];
  1065. dst[1] = src[1];
  1066. dst[2] = src[2];
  1067. dst[3] = src[3];
  1068. }
  1069. template<> MOZ_ALWAYS_INLINE void
  1070. pack<WebGLTexelFormat::RGBA8, WebGLTexelPremultiplicationOp::Premultiply, uint8_t, uint8_t>(const uint8_t* __restrict src, uint8_t* __restrict dst)
  1071. {
  1072. float scaleFactor = src[3] / 255.0f;
  1073. uint8_t srcR = static_cast<uint8_t>(src[0] * scaleFactor);
  1074. uint8_t srcG = static_cast<uint8_t>(src[1] * scaleFactor);
  1075. uint8_t srcB = static_cast<uint8_t>(src[2] * scaleFactor);
  1076. dst[0] = srcR;
  1077. dst[1] = srcG;
  1078. dst[2] = srcB;
  1079. dst[3] = src[3];
  1080. }
  1081. // FIXME: this routine is lossy and must be removed.
  1082. template<> MOZ_ALWAYS_INLINE void
  1083. pack<WebGLTexelFormat::RGBA8, WebGLTexelPremultiplicationOp::Unpremultiply, uint8_t, uint8_t>(const uint8_t* __restrict src, uint8_t* __restrict dst)
  1084. {
  1085. float scaleFactor = src[3] ? 255.0f / src[3] : 1.0f;
  1086. uint8_t srcR = static_cast<uint8_t>(src[0] * scaleFactor);
  1087. uint8_t srcG = static_cast<uint8_t>(src[1] * scaleFactor);
  1088. uint8_t srcB = static_cast<uint8_t>(src[2] * scaleFactor);
  1089. dst[0] = srcR;
  1090. dst[1] = srcG;
  1091. dst[2] = srcB;
  1092. dst[3] = src[3];
  1093. }
  1094. template<> MOZ_ALWAYS_INLINE void
  1095. pack<WebGLTexelFormat::RGBA16F, WebGLTexelPremultiplicationOp::None, uint16_t, uint16_t>(const uint16_t* __restrict src, uint16_t* __restrict dst)
  1096. {
  1097. dst[0] = src[0];
  1098. dst[1] = src[1];
  1099. dst[2] = src[2];
  1100. dst[3] = src[3];
  1101. }
  1102. template<> MOZ_ALWAYS_INLINE void
  1103. pack<WebGLTexelFormat::RGBA16F, WebGLTexelPremultiplicationOp::Premultiply, uint16_t, uint16_t>(const uint16_t* __restrict src, uint16_t* __restrict dst)
  1104. {
  1105. float scaleFactor = unpackFromFloat16(src[3]);
  1106. dst[0] = packToFloat16(unpackFromFloat16(src[0]) * scaleFactor);
  1107. dst[1] = packToFloat16(unpackFromFloat16(src[1]) * scaleFactor);
  1108. dst[2] = packToFloat16(unpackFromFloat16(src[2]) * scaleFactor);
  1109. dst[3] = src[3];
  1110. }
  1111. template<> MOZ_ALWAYS_INLINE void
  1112. pack<WebGLTexelFormat::RGBA16F, WebGLTexelPremultiplicationOp::Unpremultiply, uint16_t, uint16_t>(const uint16_t* __restrict src, uint16_t* __restrict dst)
  1113. {
  1114. float unpackedAlpha = unpackFromFloat16(src[3]);
  1115. float scaleFactor = unpackedAlpha ? 1.0f / unpackedAlpha : 1.0f;
  1116. dst[0] = packToFloat16(unpackFromFloat16(src[0]) * scaleFactor);
  1117. dst[1] = packToFloat16(unpackFromFloat16(src[1]) * scaleFactor);
  1118. dst[2] = packToFloat16(unpackFromFloat16(src[2]) * scaleFactor);
  1119. dst[3] = src[3];
  1120. }
  1121. template<> MOZ_ALWAYS_INLINE void
  1122. pack<WebGLTexelFormat::RGBA32F, WebGLTexelPremultiplicationOp::None, float, float>(const float* __restrict src, float* __restrict dst)
  1123. {
  1124. dst[0] = src[0];
  1125. dst[1] = src[1];
  1126. dst[2] = src[2];
  1127. dst[3] = src[3];
  1128. }
  1129. template<> MOZ_ALWAYS_INLINE void
  1130. pack<WebGLTexelFormat::RGBA32F, WebGLTexelPremultiplicationOp::Premultiply, float, float>(const float* __restrict src, float* __restrict dst)
  1131. {
  1132. float scaleFactor = src[3];
  1133. dst[0] = src[0] * scaleFactor;
  1134. dst[1] = src[1] * scaleFactor;
  1135. dst[2] = src[2] * scaleFactor;
  1136. dst[3] = src[3];
  1137. }
  1138. template<> MOZ_ALWAYS_INLINE void
  1139. pack<WebGLTexelFormat::RGBA32F, WebGLTexelPremultiplicationOp::Unpremultiply, float, float>(const float* __restrict src, float* __restrict dst)
  1140. {
  1141. float scaleFactor = src[3] ? 1.0f / src[3] : 1.0f;
  1142. dst[0] = src[0] * scaleFactor;
  1143. dst[1] = src[1] * scaleFactor;
  1144. dst[2] = src[2] * scaleFactor;
  1145. dst[3] = src[3];
  1146. }
  1147. /****** END CODE SHARED WITH WEBKIT ******/
  1148. template<typename SrcType, typename DstType> MOZ_ALWAYS_INLINE void
  1149. convertType(const SrcType* __restrict src, DstType* __restrict dst)
  1150. {
  1151. MOZ_ASSERT(false, "Unimplemented texture format conversion");
  1152. }
  1153. template<> MOZ_ALWAYS_INLINE void
  1154. convertType<uint8_t, uint8_t>(const uint8_t* __restrict src, uint8_t* __restrict dst)
  1155. {
  1156. dst[0] = src[0];
  1157. dst[1] = src[1];
  1158. dst[2] = src[2];
  1159. dst[3] = src[3];
  1160. }
  1161. template<> MOZ_ALWAYS_INLINE void
  1162. convertType<uint16_t, uint16_t>(const uint16_t* __restrict src, uint16_t* __restrict dst)
  1163. {
  1164. dst[0] = src[0];
  1165. dst[1] = src[1];
  1166. dst[2] = src[2];
  1167. dst[3] = src[3];
  1168. }
  1169. template<> MOZ_ALWAYS_INLINE void
  1170. convertType<float, float>(const float* __restrict src, float* __restrict dst)
  1171. {
  1172. dst[0] = src[0];
  1173. dst[1] = src[1];
  1174. dst[2] = src[2];
  1175. dst[3] = src[3];
  1176. }
  1177. template<> MOZ_ALWAYS_INLINE void
  1178. convertType<uint8_t, float>(const uint8_t* __restrict src, float* __restrict dst)
  1179. {
  1180. const float scaleFactor = 1.f / 255.0f;
  1181. dst[0] = src[0] * scaleFactor;
  1182. dst[1] = src[1] * scaleFactor;
  1183. dst[2] = src[2] * scaleFactor;
  1184. dst[3] = src[3] * scaleFactor;
  1185. }
  1186. template<> MOZ_ALWAYS_INLINE void
  1187. convertType<uint8_t, uint16_t>(const uint8_t* __restrict src, uint16_t* __restrict dst)
  1188. {
  1189. const float scaleFactor = 1.f / 255.0f;
  1190. dst[0] = packToFloat16(src[0] * scaleFactor);
  1191. dst[1] = packToFloat16(src[1] * scaleFactor);
  1192. dst[2] = packToFloat16(src[2] * scaleFactor);
  1193. dst[3] = packToFloat16(src[3] * scaleFactor);
  1194. }
  1195. } // end namespace WebGLTexelConversions
  1196. } // end namespace mozilla
  1197. #endif // WEBGLTEXELCONVERSIONS_H_