content/canvas/src/WebGLTexelConversions.h

changeset 0
6474c204b198
     1.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     1.2 +++ b/content/canvas/src/WebGLTexelConversions.h	Wed Dec 31 06:09:35 2014 +0100
     1.3 @@ -0,0 +1,986 @@
     1.4 +/*
     1.5 + * Copyright (C) 2010 Apple Inc. All rights reserved.
     1.6 + * Copyright (C) 2010 Google Inc. All rights reserved.
     1.7 + * Copyright (C) 2010 Mozilla Corporation. All rights reserved.
     1.8 + *
     1.9 + * Redistribution and use in source and binary forms, with or without
    1.10 + * modification, are permitted provided that the following conditions
    1.11 + * are met:
    1.12 + * 1. Redistributions of source code must retain the above copyright
    1.13 + *    notice, this list of conditions and the following disclaimer.
    1.14 + * 2. Redistributions in binary form must reproduce the above copyright
    1.15 + *    notice, this list of conditions and the following disclaimer in the
    1.16 + *    documentation and/or other materials provided with the distribution.
    1.17 + *
    1.18 + * THIS SOFTWARE IS PROVIDED BY APPLE COMPUTER, INC. ``AS IS'' AND ANY
    1.19 + * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
    1.20 + * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
    1.21 + * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL APPLE COMPUTER, INC. OR
    1.22 + * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
    1.23 + * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
    1.24 + * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
    1.25 + * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY
    1.26 + * OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
    1.27 + * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
    1.28 + * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
    1.29 + */
    1.30 +
    1.31 +#ifndef WEBGLTEXELCONVERSIONS_H_
    1.32 +#define WEBGLTEXELCONVERSIONS_H_
    1.33 +
    1.34 +#ifdef __SUNPRO_CC
    1.35 +#define __restrict
    1.36 +#endif
    1.37 +
    1.38 +#include "WebGLTypes.h"
    1.39 +#include <stdint.h>
    1.40 +#include "mozilla/Attributes.h"
    1.41 +
    1.42 +namespace mozilla {
    1.43 +
    1.44 +// single precision float
    1.45 +// seeeeeeeemmmmmmmmmmmmmmmmmmmmmmm
    1.46 +
    1.47 +// half precision float
    1.48 +// seeeeemmmmmmmmmm
    1.49 +
    1.50 +// IEEE 16bits floating point:
    1.51 +const uint16_t kFloat16Value_Zero     = 0x0000; // = 0000000000000000b
    1.52 +const uint16_t kFloat16Value_One      = 0x3C00; // = 0011110000000000b
    1.53 +const uint16_t kFloat16Value_Infinity = 0x7C00; // = 0111110000000000b
    1.54 +const uint16_t kFloat16Value_NaN      = 0x7FFF; // = 011111yyyyyyyyyyb (nonzero y)
    1.55 +
    1.56 +MOZ_ALWAYS_INLINE uint16_t
    1.57 +packToFloat16(float v)
    1.58 +{
    1.59 +    union {
    1.60 +        float f32Value;
    1.61 +        uint32_t f32Bits;
    1.62 +    };
    1.63 +
    1.64 +    f32Value = v;
    1.65 +
    1.66 +    // pull the sign from v into f16bits
    1.67 +    uint16_t f16Bits = uint16_t(f32Bits >> 16) & 0x8000;
    1.68 +
    1.69 +    // handle +/- 0
    1.70 +    if ((f32Bits & 0x7FFFFFFF) == 0x00000000) {
    1.71 +        return f16Bits;
    1.72 +    }
    1.73 +
    1.74 +    // handle NaN
    1.75 +    if (f32Value != f32Value) {
    1.76 +        return f16Bits | kFloat16Value_NaN;
    1.77 +    }
    1.78 +
    1.79 +    int32_t exp = int32_t(f32Bits >> 23) - 127;
    1.80 +
    1.81 +    // too small, we clamp it to -0 or +0
    1.82 +    if (exp < -14) {
    1.83 +        return f16Bits;
    1.84 +    }
    1.85 +
    1.86 +    // too big, we clamp it to -inf/+inf
    1.87 +    if (exp > 15) {
    1.88 +        return f16Bits | kFloat16Value_Infinity;
    1.89 +    }
    1.90 +
    1.91 +    f16Bits |= uint16_t(exp + 15) << 10;
    1.92 +    f16Bits |= uint16_t(f32Bits >> 13) & 0x03FF;
    1.93 +
    1.94 +    return f16Bits;
    1.95 +}
    1.96 +
    1.97 +MOZ_ALWAYS_INLINE float
    1.98 +unpackFromFloat16(uint16_t v)
    1.99 +{
   1.100 +    union
   1.101 +    {
   1.102 +        float f32Value;
   1.103 +        uint32_t f32Bits;
   1.104 +    };
   1.105 +
   1.106 +    // grab sign bit
   1.107 +    f32Bits = uint32_t(v & 0x8000) << 16;
   1.108 +
   1.109 +    if ((v & 0x7FFF) == 0x0000) {
   1.110 +        // +0 or -0
   1.111 +        return f32Value;
   1.112 +    }
   1.113 +
   1.114 +    uint16_t exp = (v >> 10) & 0x001F;
   1.115 +    if (exp == 0x001F) {
   1.116 +        if (v & 0x03FF) {
   1.117 +            // this is a NaN
   1.118 +            f32Bits |= 0x7FFFFFFF;
   1.119 +        } else {
   1.120 +            // this is -inf or +inf
   1.121 +            f32Bits |= 0x7F800000;
   1.122 +        }
   1.123 +        return f32Value;
   1.124 +    }
   1.125 +
   1.126 +    f32Bits |= uint32_t(exp + (-15 + 127)) << 10;
   1.127 +    f32Bits |= uint32_t(v & 0x03FF) << 13;
   1.128 +
   1.129 +    return f32Value;
   1.130 +}
   1.131 +
   1.132 +MOZ_BEGIN_ENUM_CLASS(WebGLTexelPremultiplicationOp, int)
   1.133 +    None,
   1.134 +    Premultiply,
   1.135 +    Unpremultiply
   1.136 +MOZ_END_ENUM_CLASS(WebGLTexelPremultiplicationOp)
   1.137 +
   1.138 +namespace WebGLTexelConversions {
   1.139 +
   1.140 +template<MOZ_ENUM_CLASS_ENUM_TYPE(WebGLTexelFormat) Format>
   1.141 +struct IsFloatFormat
   1.142 +{
   1.143 +    static const bool Value =
   1.144 +        Format == WebGLTexelFormat::RGBA32F ||
   1.145 +        Format == WebGLTexelFormat::RGB32F ||
   1.146 +        Format == WebGLTexelFormat::RA32F ||
   1.147 +        Format == WebGLTexelFormat::R32F ||
   1.148 +        Format == WebGLTexelFormat::A32F;
   1.149 +};
   1.150 +
   1.151 +template<MOZ_ENUM_CLASS_ENUM_TYPE(WebGLTexelFormat) Format>
   1.152 +struct IsHalfFloatFormat
   1.153 +{
   1.154 +    static const bool Value =
   1.155 +        Format == WebGLTexelFormat::RGBA16F ||
   1.156 +        Format == WebGLTexelFormat::RGB16F ||
   1.157 +        Format == WebGLTexelFormat::RA16F ||
   1.158 +        Format == WebGLTexelFormat::R16F ||
   1.159 +        Format == WebGLTexelFormat::A16F;
   1.160 +};
   1.161 +
   1.162 +template<MOZ_ENUM_CLASS_ENUM_TYPE(WebGLTexelFormat) Format>
   1.163 +struct Is16bppFormat
   1.164 +{
   1.165 +    static const bool Value =
   1.166 +        Format == WebGLTexelFormat::RGBA4444 ||
   1.167 +        Format == WebGLTexelFormat::RGBA5551 ||
   1.168 +        Format == WebGLTexelFormat::RGB565;
   1.169 +};
   1.170 +
   1.171 +template<MOZ_ENUM_CLASS_ENUM_TYPE(WebGLTexelFormat) Format,
   1.172 +         bool IsFloat = IsFloatFormat<Format>::Value,
   1.173 +         bool Is16bpp = Is16bppFormat<Format>::Value,
   1.174 +         bool IsHalfFloat = IsHalfFloatFormat<Format>::Value>
   1.175 +struct DataTypeForFormat
   1.176 +{
   1.177 +    typedef uint8_t Type;
   1.178 +};
   1.179 +
   1.180 +template<MOZ_ENUM_CLASS_ENUM_TYPE(WebGLTexelFormat) Format>
   1.181 +struct DataTypeForFormat<Format, true, false, false>
   1.182 +{
   1.183 +    typedef float Type;
   1.184 +};
   1.185 +
   1.186 +template<MOZ_ENUM_CLASS_ENUM_TYPE(WebGLTexelFormat) Format>
   1.187 +struct DataTypeForFormat<Format, false, true, false>
   1.188 +{
   1.189 +    typedef uint16_t Type;
   1.190 +};
   1.191 +
   1.192 +template<MOZ_ENUM_CLASS_ENUM_TYPE(WebGLTexelFormat) Format>
   1.193 +struct DataTypeForFormat<Format, false, false, true>
   1.194 +{
   1.195 +    typedef uint16_t Type;
   1.196 +};
   1.197 +
   1.198 +template<MOZ_ENUM_CLASS_ENUM_TYPE(WebGLTexelFormat) Format>
   1.199 +struct IntermediateFormat
   1.200 +{
   1.201 +    static const MOZ_ENUM_CLASS_ENUM_TYPE(WebGLTexelFormat) Value
   1.202 +        = IsFloatFormat<Format>::Value
   1.203 +          ? WebGLTexelFormat::RGBA32F
   1.204 +          : IsHalfFloatFormat<Format>::Value ? WebGLTexelFormat::RGBA16F
   1.205 +                                             : WebGLTexelFormat::RGBA8;
   1.206 +};
   1.207 +
   1.208 +inline GLenum
   1.209 +GLFormatForTexelFormat(WebGLTexelFormat format) {
   1.210 +    switch (format) {
   1.211 +        case WebGLTexelFormat::R8:          return LOCAL_GL_LUMINANCE;
   1.212 +        case WebGLTexelFormat::A8:          return LOCAL_GL_ALPHA;
   1.213 +        case WebGLTexelFormat::RA8:         return LOCAL_GL_LUMINANCE_ALPHA;
   1.214 +        case WebGLTexelFormat::RGBA5551:    return LOCAL_GL_RGBA;
   1.215 +        case WebGLTexelFormat::RGBA4444:    return LOCAL_GL_RGBA;
   1.216 +        case WebGLTexelFormat::RGB565:      return LOCAL_GL_RGB;
   1.217 +        case WebGLTexelFormat::D16:         return LOCAL_GL_DEPTH_COMPONENT;
   1.218 +        case WebGLTexelFormat::RGB8:        return LOCAL_GL_RGB;
   1.219 +        case WebGLTexelFormat::RGBA8:       return LOCAL_GL_RGBA;
   1.220 +        case WebGLTexelFormat::BGRA8:       return LOCAL_GL_BGRA;
   1.221 +        case WebGLTexelFormat::BGRX8:       return LOCAL_GL_BGR;
   1.222 +        case WebGLTexelFormat::R32F:        return LOCAL_GL_LUMINANCE;
   1.223 +        case WebGLTexelFormat::A32F:        return LOCAL_GL_ALPHA;
   1.224 +        case WebGLTexelFormat::D32:         return LOCAL_GL_DEPTH_COMPONENT;
   1.225 +        case WebGLTexelFormat::D24S8:       return LOCAL_GL_DEPTH_STENCIL;
   1.226 +        case WebGLTexelFormat::RA32F:       return LOCAL_GL_LUMINANCE_ALPHA;
   1.227 +        case WebGLTexelFormat::RGB32F:      return LOCAL_GL_RGB;
   1.228 +        case WebGLTexelFormat::RGBA32F:     return LOCAL_GL_RGBA;
   1.229 +        case WebGLTexelFormat::R16F:        return LOCAL_GL_LUMINANCE;
   1.230 +        case WebGLTexelFormat::A16F:        return LOCAL_GL_ALPHA;
   1.231 +        case WebGLTexelFormat::RA16F:       return LOCAL_GL_LUMINANCE_ALPHA;
   1.232 +        case WebGLTexelFormat::RGB16F:      return LOCAL_GL_RGB;
   1.233 +        case WebGLTexelFormat::RGBA16F:     return LOCAL_GL_RGBA;
   1.234 +        default:
   1.235 +            MOZ_CRASH("Unknown texel format. Coding mistake?");
   1.236 +            return LOCAL_GL_INVALID_ENUM;
   1.237 +    }
   1.238 +}
   1.239 +
   1.240 +inline size_t TexelBytesForFormat(WebGLTexelFormat format) {
   1.241 +    switch (format) {
   1.242 +        case WebGLTexelFormat::R8:
   1.243 +        case WebGLTexelFormat::A8:
   1.244 +            return 1;
   1.245 +        case WebGLTexelFormat::RA8:
   1.246 +        case WebGLTexelFormat::RGBA5551:
   1.247 +        case WebGLTexelFormat::RGBA4444:
   1.248 +        case WebGLTexelFormat::RGB565:
   1.249 +        case WebGLTexelFormat::R16F:
   1.250 +        case WebGLTexelFormat::A16F:
   1.251 +        case WebGLTexelFormat::D16:
   1.252 +            return 2;
   1.253 +        case WebGLTexelFormat::RGB8:
   1.254 +            return 3;
   1.255 +        case WebGLTexelFormat::RGBA8:
   1.256 +        case WebGLTexelFormat::BGRA8:
   1.257 +        case WebGLTexelFormat::BGRX8:
   1.258 +        case WebGLTexelFormat::R32F:
   1.259 +        case WebGLTexelFormat::A32F:
   1.260 +        case WebGLTexelFormat::D32:
   1.261 +        case WebGLTexelFormat::D24S8:
   1.262 +        case WebGLTexelFormat::RA16F:
   1.263 +            return 4;
   1.264 +        case WebGLTexelFormat::RGB16F:
   1.265 +            return 6;
   1.266 +        case WebGLTexelFormat::RGBA16F:
   1.267 +        case WebGLTexelFormat::RA32F:
   1.268 +            return 8;
   1.269 +        case WebGLTexelFormat::RGB32F:
   1.270 +            return 12;
   1.271 +        case WebGLTexelFormat::RGBA32F:
   1.272 +            return 16;
   1.273 +        default:
   1.274 +            MOZ_ASSERT(false, "Unknown texel format. Coding mistake?");
   1.275 +            return 0;
   1.276 +    }
   1.277 +}
   1.278 +
   1.279 +MOZ_ALWAYS_INLINE bool HasAlpha(WebGLTexelFormat format) {
   1.280 +    return format == WebGLTexelFormat::A8 ||
   1.281 +           format == WebGLTexelFormat::A16F ||
   1.282 +           format == WebGLTexelFormat::A32F ||
   1.283 +           format == WebGLTexelFormat::RA8 ||
   1.284 +           format == WebGLTexelFormat::RA16F ||
   1.285 +           format == WebGLTexelFormat::RA32F ||
   1.286 +           format == WebGLTexelFormat::RGBA8 ||
   1.287 +           format == WebGLTexelFormat::BGRA8 ||
   1.288 +           format == WebGLTexelFormat::RGBA16F ||
   1.289 +           format == WebGLTexelFormat::RGBA32F ||
   1.290 +           format == WebGLTexelFormat::RGBA4444 ||
   1.291 +           format == WebGLTexelFormat::RGBA5551;
   1.292 +}
   1.293 +
   1.294 +MOZ_ALWAYS_INLINE bool HasColor(WebGLTexelFormat format) {
   1.295 +    return format == WebGLTexelFormat::R8 ||
   1.296 +           format == WebGLTexelFormat::R16F ||
   1.297 +           format == WebGLTexelFormat::R32F ||
   1.298 +           format == WebGLTexelFormat::RA8 ||
   1.299 +           format == WebGLTexelFormat::RA16F ||
   1.300 +           format == WebGLTexelFormat::RA32F ||
   1.301 +           format == WebGLTexelFormat::RGB8 ||
   1.302 +           format == WebGLTexelFormat::BGRX8 ||
   1.303 +           format == WebGLTexelFormat::RGB565 ||
   1.304 +           format == WebGLTexelFormat::RGB16F ||
   1.305 +           format == WebGLTexelFormat::RGB32F ||
   1.306 +           format == WebGLTexelFormat::RGBA8 ||
   1.307 +           format == WebGLTexelFormat::BGRA8 ||
   1.308 +           format == WebGLTexelFormat::RGBA16F ||
   1.309 +           format == WebGLTexelFormat::RGBA32F ||
   1.310 +           format == WebGLTexelFormat::RGBA4444 ||
   1.311 +           format == WebGLTexelFormat::RGBA5551;
   1.312 +}
   1.313 +
   1.314 +
   1.315 +/****** BEGIN CODE SHARED WITH WEBKIT ******/
   1.316 +
   1.317 +// the pack/unpack functions here are originally from this file:
   1.318 +//   http://trac.webkit.org/browser/trunk/WebCore/platform/graphics/GraphicsContext3D.cpp
   1.319 +
   1.320 +//----------------------------------------------------------------------
   1.321 +// Pixel unpacking routines.
   1.322 +
   1.323 +template<MOZ_ENUM_CLASS_ENUM_TYPE(WebGLTexelFormat) Format, typename SrcType, typename DstType>
   1.324 +MOZ_ALWAYS_INLINE void
   1.325 +unpack(const SrcType* __restrict src,
   1.326 +       DstType* __restrict dst)
   1.327 +{
   1.328 +    MOZ_ASSERT(false, "Unimplemented texture format conversion");
   1.329 +}
   1.330 +
   1.331 +template<> MOZ_ALWAYS_INLINE void
   1.332 +unpack<WebGLTexelFormat::RGBA8, uint8_t, uint8_t>(const uint8_t* __restrict src, uint8_t* __restrict dst)
   1.333 +{
   1.334 +    dst[0] = src[0];
   1.335 +    dst[1] = src[1];
   1.336 +    dst[2] = src[2];
   1.337 +    dst[3] = src[3];
   1.338 +}
   1.339 +
   1.340 +template<> MOZ_ALWAYS_INLINE void
   1.341 +unpack<WebGLTexelFormat::RGB8, uint8_t, uint8_t>(const uint8_t* __restrict src, uint8_t* __restrict dst)
   1.342 +{
   1.343 +    dst[0] = src[0];
   1.344 +    dst[1] = src[1];
   1.345 +    dst[2] = src[2];
   1.346 +    dst[3] = 0xFF;
   1.347 +}
   1.348 +
   1.349 +template<> MOZ_ALWAYS_INLINE void
   1.350 +unpack<WebGLTexelFormat::BGRA8, uint8_t, uint8_t>(const uint8_t* __restrict src, uint8_t* __restrict dst)
   1.351 +{
   1.352 +    dst[0] = src[2];
   1.353 +    dst[1] = src[1];
   1.354 +    dst[2] = src[0];
   1.355 +    dst[3] = src[3];
   1.356 +}
   1.357 +
   1.358 +template<> MOZ_ALWAYS_INLINE void
   1.359 +unpack<WebGLTexelFormat::BGRX8, uint8_t, uint8_t>(const uint8_t* __restrict src, uint8_t* __restrict dst)
   1.360 +{
   1.361 +    dst[0] = src[2];
   1.362 +    dst[1] = src[1];
   1.363 +    dst[2] = src[0];
   1.364 +    dst[3] = 0xFF;
   1.365 +}
   1.366 +
   1.367 +template<> MOZ_ALWAYS_INLINE void
   1.368 +unpack<WebGLTexelFormat::RGBA5551, uint16_t, uint8_t>(const uint16_t* __restrict src, uint8_t* __restrict dst)
   1.369 +{
   1.370 +    uint16_t packedValue = src[0];
   1.371 +    uint8_t r = (packedValue >> 11) & 0x1F;
   1.372 +    uint8_t g = (packedValue >> 6) & 0x1F;
   1.373 +    uint8_t b = (packedValue >> 1) & 0x1F;
   1.374 +    dst[0] = (r << 3) | (r & 0x7);
   1.375 +    dst[1] = (g << 3) | (g & 0x7);
   1.376 +    dst[2] = (b << 3) | (b & 0x7);
   1.377 +    dst[3] = (packedValue & 0x1) ? 0xFF : 0;
   1.378 +}
   1.379 +
   1.380 +template<> MOZ_ALWAYS_INLINE void
   1.381 +unpack<WebGLTexelFormat::RGBA4444, uint16_t, uint8_t>(const uint16_t* __restrict src, uint8_t* __restrict dst)
   1.382 +{
   1.383 +    uint16_t packedValue = src[0];
   1.384 +    uint8_t r = (packedValue >> 12) & 0x0F;
   1.385 +    uint8_t g = (packedValue >> 8) & 0x0F;
   1.386 +    uint8_t b = (packedValue >> 4) & 0x0F;
   1.387 +    uint8_t a = packedValue & 0x0F;
   1.388 +    dst[0] = (r << 4) | r;
   1.389 +    dst[1] = (g << 4) | g;
   1.390 +    dst[2] = (b << 4) | b;
   1.391 +    dst[3] = (a << 4) | a;
   1.392 +}
   1.393 +
   1.394 +template<> MOZ_ALWAYS_INLINE void
   1.395 +unpack<WebGLTexelFormat::RGB565, uint16_t, uint8_t>(const uint16_t* __restrict src, uint8_t* __restrict dst)
   1.396 +{
   1.397 +    uint16_t packedValue = src[0];
   1.398 +    uint8_t r = (packedValue >> 11) & 0x1F;
   1.399 +    uint8_t g = (packedValue >> 5) & 0x3F;
   1.400 +    uint8_t b = packedValue & 0x1F;
   1.401 +    dst[0] = (r << 3) | (r & 0x7);
   1.402 +    dst[1] = (g << 2) | (g & 0x3);
   1.403 +    dst[2] = (b << 3) | (b & 0x7);
   1.404 +    dst[3] = 0xFF;
   1.405 +}
   1.406 +
   1.407 +template<> MOZ_ALWAYS_INLINE void
   1.408 +unpack<WebGLTexelFormat::R8, uint8_t, uint8_t>(const uint8_t* __restrict src, uint8_t* __restrict dst)
   1.409 +{
   1.410 +    dst[0] = src[0];
   1.411 +    dst[1] = src[0];
   1.412 +    dst[2] = src[0];
   1.413 +    dst[3] = 0xFF;
   1.414 +}
   1.415 +
   1.416 +template<> MOZ_ALWAYS_INLINE void
   1.417 +unpack<WebGLTexelFormat::RA8, uint8_t, uint8_t>(const uint8_t* __restrict src, uint8_t* __restrict dst)
   1.418 +{
   1.419 +    dst[0] = src[0];
   1.420 +    dst[1] = src[0];
   1.421 +    dst[2] = src[0];
   1.422 +    dst[3] = src[1];
   1.423 +}
   1.424 +
   1.425 +template<> MOZ_ALWAYS_INLINE void
   1.426 +unpack<WebGLTexelFormat::A8, uint8_t, uint8_t>(const uint8_t* __restrict src, uint8_t* __restrict dst)
   1.427 +{
   1.428 +    dst[0] = 0;
   1.429 +    dst[1] = 0;
   1.430 +    dst[2] = 0;
   1.431 +    dst[3] = src[0];
   1.432 +}
   1.433 +
   1.434 +template<> MOZ_ALWAYS_INLINE void
   1.435 +unpack<WebGLTexelFormat::RGBA32F, float, float>(const float* __restrict src, float* __restrict dst)
   1.436 +{
   1.437 +    dst[0] = src[0];
   1.438 +    dst[1] = src[1];
   1.439 +    dst[2] = src[2];
   1.440 +    dst[3] = src[3];
   1.441 +}
   1.442 +
   1.443 +template<> MOZ_ALWAYS_INLINE void
   1.444 +unpack<WebGLTexelFormat::RGB32F, float, float>(const float* __restrict src, float* __restrict dst)
   1.445 +{
   1.446 +    dst[0] = src[0];
   1.447 +    dst[1] = src[1];
   1.448 +    dst[2] = src[2];
   1.449 +    dst[3] = 1.0f;
   1.450 +}
   1.451 +
   1.452 +template<> MOZ_ALWAYS_INLINE void
   1.453 +unpack<WebGLTexelFormat::R32F, float, float>(const float* __restrict src, float* __restrict dst)
   1.454 +{
   1.455 +    dst[0] = src[0];
   1.456 +    dst[1] = src[0];
   1.457 +    dst[2] = src[0];
   1.458 +    dst[3] = 1.0f;
   1.459 +}
   1.460 +
   1.461 +template<> MOZ_ALWAYS_INLINE void
   1.462 +unpack<WebGLTexelFormat::RA32F, float, float>(const float* __restrict src, float* __restrict dst)
   1.463 +{
   1.464 +    dst[0] = src[0];
   1.465 +    dst[1] = src[0];
   1.466 +    dst[2] = src[0];
   1.467 +    dst[3] = src[1];
   1.468 +}
   1.469 +
   1.470 +template<> MOZ_ALWAYS_INLINE void
   1.471 +unpack<WebGLTexelFormat::A32F, float, float>(const float* __restrict src, float* __restrict dst)
   1.472 +{
   1.473 +    dst[0] = 0;
   1.474 +    dst[1] = 0;
   1.475 +    dst[2] = 0;
   1.476 +    dst[3] = src[0];
   1.477 +}
   1.478 +
   1.479 +template<> MOZ_ALWAYS_INLINE void
   1.480 +unpack<WebGLTexelFormat::RGBA16F, uint16_t, uint16_t>(const uint16_t* __restrict src, uint16_t* __restrict dst)
   1.481 +{
   1.482 +    dst[0] = src[0];
   1.483 +    dst[1] = src[1];
   1.484 +    dst[2] = src[2];
   1.485 +    dst[3] = src[3];
   1.486 +}
   1.487 +
   1.488 +template<> MOZ_ALWAYS_INLINE void
   1.489 +unpack<WebGLTexelFormat::RGB16F, uint16_t, uint16_t>(const uint16_t* __restrict src, uint16_t* __restrict dst)
   1.490 +{
   1.491 +    dst[0] = src[0];
   1.492 +    dst[1] = src[1];
   1.493 +    dst[2] = src[2];
   1.494 +    dst[3] = kFloat16Value_One;
   1.495 +}
   1.496 +
   1.497 +template<> MOZ_ALWAYS_INLINE void
   1.498 +unpack<WebGLTexelFormat::R16F, uint16_t, uint16_t>(const uint16_t* __restrict src, uint16_t* __restrict dst)
   1.499 +{
   1.500 +    dst[0] = src[0];
   1.501 +    dst[1] = src[0];
   1.502 +    dst[2] = src[0];
   1.503 +    dst[3] = kFloat16Value_One;
   1.504 +}
   1.505 +
   1.506 +template<> MOZ_ALWAYS_INLINE void
   1.507 +unpack<WebGLTexelFormat::RA16F, uint16_t, uint16_t>(const uint16_t* __restrict src, uint16_t* __restrict dst)
   1.508 +{
   1.509 +    dst[0] = src[0];
   1.510 +    dst[1] = src[0];
   1.511 +    dst[2] = src[0];
   1.512 +    dst[3] = src[1];
   1.513 +}
   1.514 +
   1.515 +template<> MOZ_ALWAYS_INLINE void
   1.516 +unpack<WebGLTexelFormat::A16F, uint16_t, uint16_t>(const uint16_t* __restrict src, uint16_t* __restrict dst)
   1.517 +{
   1.518 +    dst[0] = kFloat16Value_Zero;
   1.519 +    dst[1] = kFloat16Value_Zero;
   1.520 +    dst[2] = kFloat16Value_Zero;
   1.521 +    dst[3] = src[0];
   1.522 +}
   1.523 +
   1.524 +//----------------------------------------------------------------------
   1.525 +// Pixel packing routines.
   1.526 +//
   1.527 +
   1.528 +template<MOZ_ENUM_CLASS_ENUM_TYPE(WebGLTexelFormat) Format,
   1.529 +         MOZ_ENUM_CLASS_ENUM_TYPE(WebGLTexelPremultiplicationOp) PremultiplicationOp,
   1.530 +         typename SrcType,
   1.531 +         typename DstType>
   1.532 +MOZ_ALWAYS_INLINE void
   1.533 +pack(const SrcType* __restrict src,
   1.534 +     DstType* __restrict dst)
   1.535 +{
   1.536 +    MOZ_ASSERT(false, "Unimplemented texture format conversion");
   1.537 +}
   1.538 +
   1.539 +template<> MOZ_ALWAYS_INLINE void
   1.540 +pack<WebGLTexelFormat::A8, WebGLTexelPremultiplicationOp::None, uint8_t, uint8_t>(const uint8_t* __restrict src, uint8_t* __restrict dst)
   1.541 +{
   1.542 +    dst[0] = src[3];
   1.543 +}
   1.544 +
   1.545 +template<> MOZ_ALWAYS_INLINE void
   1.546 +pack<WebGLTexelFormat::A8, WebGLTexelPremultiplicationOp::Premultiply, uint8_t, uint8_t>(const uint8_t* __restrict src, uint8_t* __restrict dst)
   1.547 +{
   1.548 +    dst[0] = src[3];
   1.549 +}
   1.550 +
   1.551 +template<> MOZ_ALWAYS_INLINE void
   1.552 +pack<WebGLTexelFormat::A8, WebGLTexelPremultiplicationOp::Unpremultiply, uint8_t, uint8_t>(const uint8_t* __restrict src, uint8_t* __restrict dst)
   1.553 +{
   1.554 +    dst[0] = src[3];
   1.555 +}
   1.556 +
   1.557 +template<> MOZ_ALWAYS_INLINE void
   1.558 +pack<WebGLTexelFormat::R8, WebGLTexelPremultiplicationOp::None, uint8_t, uint8_t>(const uint8_t* __restrict src, uint8_t* __restrict dst)
   1.559 +{
   1.560 +    dst[0] = src[0];
   1.561 +}
   1.562 +
   1.563 +template<> MOZ_ALWAYS_INLINE void
   1.564 +pack<WebGLTexelFormat::R8, WebGLTexelPremultiplicationOp::Premultiply, uint8_t, uint8_t>(const uint8_t* __restrict src, uint8_t* __restrict dst)
   1.565 +{
   1.566 +    float scaleFactor = src[3] / 255.0f;
   1.567 +    uint8_t srcR = static_cast<uint8_t>(src[0] * scaleFactor);
   1.568 +    dst[0] = srcR;
   1.569 +}
   1.570 +
   1.571 +template<> MOZ_ALWAYS_INLINE void
   1.572 +pack<WebGLTexelFormat::R8, WebGLTexelPremultiplicationOp::Unpremultiply, uint8_t, uint8_t>(const uint8_t* __restrict src, uint8_t* __restrict dst)
   1.573 +{
   1.574 +    float scaleFactor = src[3] ? 255.0f / src[3] : 1.0f;
   1.575 +    uint8_t srcR = static_cast<uint8_t>(src[0] * scaleFactor);
   1.576 +    dst[0] = srcR;
   1.577 +}
   1.578 +
   1.579 +template<> MOZ_ALWAYS_INLINE void
   1.580 +pack<WebGLTexelFormat::RA8, WebGLTexelPremultiplicationOp::None, uint8_t, uint8_t>(const uint8_t* __restrict src, uint8_t* __restrict dst)
   1.581 +{
   1.582 +    dst[0] = src[0];
   1.583 +    dst[1] = src[3];
   1.584 +}
   1.585 +
   1.586 +template<> MOZ_ALWAYS_INLINE void
   1.587 +pack<WebGLTexelFormat::RA8, WebGLTexelPremultiplicationOp::Premultiply, uint8_t, uint8_t>(const uint8_t* __restrict src, uint8_t* __restrict dst)
   1.588 +{
   1.589 +    float scaleFactor = src[3] / 255.0f;
   1.590 +    uint8_t srcR = static_cast<uint8_t>(src[0] * scaleFactor);
   1.591 +    dst[0] = srcR;
   1.592 +    dst[1] = src[3];
   1.593 +}
   1.594 +
   1.595 +// FIXME: this routine is lossy and must be removed.
   1.596 +template<> MOZ_ALWAYS_INLINE void
   1.597 +pack<WebGLTexelFormat::RA8, WebGLTexelPremultiplicationOp::Unpremultiply, uint8_t, uint8_t>(const uint8_t* __restrict src, uint8_t* __restrict dst)
   1.598 +{
   1.599 +    float scaleFactor = src[3] ? 255.0f / src[3] : 1.0f;
   1.600 +    uint8_t srcR = static_cast<uint8_t>(src[0] * scaleFactor);
   1.601 +    dst[0] = srcR;
   1.602 +    dst[1] = src[3];
   1.603 +}
   1.604 +
   1.605 +template<> MOZ_ALWAYS_INLINE void
   1.606 +pack<WebGLTexelFormat::RGB8, WebGLTexelPremultiplicationOp::None, uint8_t, uint8_t>(const uint8_t* __restrict src, uint8_t* __restrict dst)
   1.607 +{
   1.608 +    dst[0] = src[0];
   1.609 +    dst[1] = src[1];
   1.610 +    dst[2] = src[2];
   1.611 +}
   1.612 +
   1.613 +template<> MOZ_ALWAYS_INLINE void
   1.614 +pack<WebGLTexelFormat::RGB8, WebGLTexelPremultiplicationOp::Premultiply, uint8_t, uint8_t>(const uint8_t* __restrict src, uint8_t* __restrict dst)
   1.615 +{
   1.616 +    float scaleFactor = src[3] / 255.0f;
   1.617 +    uint8_t srcR = static_cast<uint8_t>(src[0] * scaleFactor);
   1.618 +    uint8_t srcG = static_cast<uint8_t>(src[1] * scaleFactor);
   1.619 +    uint8_t srcB = static_cast<uint8_t>(src[2] * scaleFactor);
   1.620 +    dst[0] = srcR;
   1.621 +    dst[1] = srcG;
   1.622 +    dst[2] = srcB;
   1.623 +}
   1.624 +
   1.625 +template<> MOZ_ALWAYS_INLINE void
   1.626 +pack<WebGLTexelFormat::RGB8, WebGLTexelPremultiplicationOp::Unpremultiply, uint8_t, uint8_t>(const uint8_t* __restrict src, uint8_t* __restrict dst)
   1.627 +{
   1.628 +    float scaleFactor = src[3] ? 255.0f / src[3] : 1.0f;
   1.629 +    uint8_t srcR = static_cast<uint8_t>(src[0] * scaleFactor);
   1.630 +    uint8_t srcG = static_cast<uint8_t>(src[1] * scaleFactor);
   1.631 +    uint8_t srcB = static_cast<uint8_t>(src[2] * scaleFactor);
   1.632 +    dst[0] = srcR;
   1.633 +    dst[1] = srcG;
   1.634 +    dst[2] = srcB;
   1.635 +}
   1.636 +
   1.637 +template<> MOZ_ALWAYS_INLINE void
   1.638 +pack<WebGLTexelFormat::RGBA8, WebGLTexelPremultiplicationOp::None, uint8_t, uint8_t>(const uint8_t* __restrict src, uint8_t* __restrict dst)
   1.639 +{
   1.640 +    dst[0] = src[0];
   1.641 +    dst[1] = src[1];
   1.642 +    dst[2] = src[2];
   1.643 +    dst[3] = src[3];
   1.644 +}
   1.645 +
   1.646 +template<> MOZ_ALWAYS_INLINE void
   1.647 +pack<WebGLTexelFormat::RGBA8, WebGLTexelPremultiplicationOp::Premultiply, uint8_t, uint8_t>(const uint8_t* __restrict src, uint8_t* __restrict dst)
   1.648 +{
   1.649 +    float scaleFactor = src[3] / 255.0f;
   1.650 +    uint8_t srcR = static_cast<uint8_t>(src[0] * scaleFactor);
   1.651 +    uint8_t srcG = static_cast<uint8_t>(src[1] * scaleFactor);
   1.652 +    uint8_t srcB = static_cast<uint8_t>(src[2] * scaleFactor);
   1.653 +    dst[0] = srcR;
   1.654 +    dst[1] = srcG;
   1.655 +    dst[2] = srcB;
   1.656 +    dst[3] = src[3];
   1.657 +}
   1.658 +
   1.659 +// FIXME: this routine is lossy and must be removed.
   1.660 +template<> MOZ_ALWAYS_INLINE void
   1.661 +pack<WebGLTexelFormat::RGBA8, WebGLTexelPremultiplicationOp::Unpremultiply, uint8_t, uint8_t>(const uint8_t* __restrict src, uint8_t* __restrict dst)
   1.662 +{
   1.663 +    float scaleFactor = src[3] ? 255.0f / src[3] : 1.0f;
   1.664 +    uint8_t srcR = static_cast<uint8_t>(src[0] * scaleFactor);
   1.665 +    uint8_t srcG = static_cast<uint8_t>(src[1] * scaleFactor);
   1.666 +    uint8_t srcB = static_cast<uint8_t>(src[2] * scaleFactor);
   1.667 +    dst[0] = srcR;
   1.668 +    dst[1] = srcG;
   1.669 +    dst[2] = srcB;
   1.670 +    dst[3] = src[3];
   1.671 +}
   1.672 +
   1.673 +template<> MOZ_ALWAYS_INLINE void
   1.674 +pack<WebGLTexelFormat::RGBA4444, WebGLTexelPremultiplicationOp::None, uint8_t, uint16_t>(const uint8_t* __restrict src, uint16_t* __restrict dst)
   1.675 +{
   1.676 +    *dst = ( ((src[0] & 0xF0) << 8)
   1.677 +           | ((src[1] & 0xF0) << 4)
   1.678 +           | (src[2] & 0xF0)
   1.679 +           | (src[3] >> 4) );
   1.680 +}
   1.681 +
   1.682 +template<> MOZ_ALWAYS_INLINE void
   1.683 +pack<WebGLTexelFormat::RGBA4444, WebGLTexelPremultiplicationOp::Premultiply, uint8_t, uint16_t>(const uint8_t* __restrict src, uint16_t* __restrict dst)
   1.684 +{
   1.685 +    float scaleFactor = src[3] / 255.0f;
   1.686 +    uint8_t srcR = static_cast<uint8_t>(src[0] * scaleFactor);
   1.687 +    uint8_t srcG = static_cast<uint8_t>(src[1] * scaleFactor);
   1.688 +    uint8_t srcB = static_cast<uint8_t>(src[2] * scaleFactor);
   1.689 +    *dst = ( ((srcR & 0xF0) << 8)
   1.690 +           | ((srcG & 0xF0) << 4)
   1.691 +           | (srcB & 0xF0)
   1.692 +           | (src[3] >> 4));
   1.693 +}
   1.694 +
   1.695 +// FIXME: this routine is lossy and must be removed.
   1.696 +template<> MOZ_ALWAYS_INLINE void
   1.697 +pack<WebGLTexelFormat::RGBA4444, WebGLTexelPremultiplicationOp::Unpremultiply, uint8_t, uint16_t>(const uint8_t* __restrict src, uint16_t* __restrict dst)
   1.698 +{
   1.699 +    float scaleFactor = src[3] ? 255.0f / src[3] : 1.0f;
   1.700 +    uint8_t srcR = static_cast<uint8_t>(src[0] * scaleFactor);
   1.701 +    uint8_t srcG = static_cast<uint8_t>(src[1] * scaleFactor);
   1.702 +    uint8_t srcB = static_cast<uint8_t>(src[2] * scaleFactor);
   1.703 +    *dst = ( ((srcR & 0xF0) << 8)
   1.704 +           | ((srcG & 0xF0) << 4)
   1.705 +           | (srcB & 0xF0)
   1.706 +           | (src[3] >> 4));
   1.707 +}
   1.708 +
   1.709 +template<> MOZ_ALWAYS_INLINE void
   1.710 +pack<WebGLTexelFormat::RGBA5551, WebGLTexelPremultiplicationOp::None, uint8_t, uint16_t>(const uint8_t* __restrict src, uint16_t* __restrict dst)
   1.711 +{
   1.712 +    *dst = ( ((src[0] & 0xF8) << 8)
   1.713 +           | ((src[1] & 0xF8) << 3)
   1.714 +           | ((src[2] & 0xF8) >> 2)
   1.715 +           | (src[3] >> 7));
   1.716 +}
   1.717 +
   1.718 +template<> MOZ_ALWAYS_INLINE void
   1.719 +pack<WebGLTexelFormat::RGBA5551, WebGLTexelPremultiplicationOp::Premultiply, uint8_t, uint16_t>(const uint8_t* __restrict src, uint16_t* __restrict dst)
   1.720 +{
   1.721 +    float scaleFactor = src[3] / 255.0f;
   1.722 +    uint8_t srcR = static_cast<uint8_t>(src[0] * scaleFactor);
   1.723 +    uint8_t srcG = static_cast<uint8_t>(src[1] * scaleFactor);
   1.724 +    uint8_t srcB = static_cast<uint8_t>(src[2] * scaleFactor);
   1.725 +    *dst = ( ((srcR & 0xF8) << 8)
   1.726 +           | ((srcG & 0xF8) << 3)
   1.727 +           | ((srcB & 0xF8) >> 2)
   1.728 +           | (src[3] >> 7));
   1.729 +}
   1.730 +
   1.731 +// FIXME: this routine is lossy and must be removed.
   1.732 +template<> MOZ_ALWAYS_INLINE void
   1.733 +pack<WebGLTexelFormat::RGBA5551, WebGLTexelPremultiplicationOp::Unpremultiply, uint8_t, uint16_t>(const uint8_t* __restrict src, uint16_t* __restrict dst)
   1.734 +{
   1.735 +    float scaleFactor = src[3] ? 255.0f / src[3] : 1.0f;
   1.736 +    uint8_t srcR = static_cast<uint8_t>(src[0] * scaleFactor);
   1.737 +    uint8_t srcG = static_cast<uint8_t>(src[1] * scaleFactor);
   1.738 +    uint8_t srcB = static_cast<uint8_t>(src[2] * scaleFactor);
   1.739 +    *dst = ( ((srcR & 0xF8) << 8)
   1.740 +           | ((srcG & 0xF8) << 3)
   1.741 +           | ((srcB & 0xF8) >> 2)
   1.742 +           | (src[3] >> 7));
   1.743 +}
   1.744 +
   1.745 +template<> MOZ_ALWAYS_INLINE void
   1.746 +pack<WebGLTexelFormat::RGB565, WebGLTexelPremultiplicationOp::None, uint8_t, uint16_t>(const uint8_t* __restrict src, uint16_t* __restrict dst)
   1.747 +{
   1.748 +    *dst = ( ((src[0] & 0xF8) << 8)
   1.749 +           | ((src[1] & 0xFC) << 3)
   1.750 +           | ((src[2] & 0xF8) >> 3));
   1.751 +}
   1.752 +
   1.753 +template<> MOZ_ALWAYS_INLINE void
   1.754 +pack<WebGLTexelFormat::RGB565, WebGLTexelPremultiplicationOp::Premultiply, uint8_t, uint16_t>(const uint8_t* __restrict src, uint16_t* __restrict dst)
   1.755 +{
   1.756 +    float scaleFactor = src[3] / 255.0f;
   1.757 +    uint8_t srcR = static_cast<uint8_t>(src[0] * scaleFactor);
   1.758 +    uint8_t srcG = static_cast<uint8_t>(src[1] * scaleFactor);
   1.759 +    uint8_t srcB = static_cast<uint8_t>(src[2] * scaleFactor);
   1.760 +    *dst = ( ((srcR & 0xF8) << 8)
   1.761 +           | ((srcG & 0xFC) << 3)
   1.762 +           | ((srcB & 0xF8) >> 3));
   1.763 +}
   1.764 +
   1.765 +// FIXME: this routine is lossy and must be removed.
   1.766 +template<> MOZ_ALWAYS_INLINE void
   1.767 +pack<WebGLTexelFormat::RGB565, WebGLTexelPremultiplicationOp::Unpremultiply, uint8_t, uint16_t>(const uint8_t* __restrict src, uint16_t* __restrict dst)
   1.768 +{
   1.769 +    float scaleFactor = src[3] ? 255.0f / src[3] : 1.0f;
   1.770 +    uint8_t srcR = static_cast<uint8_t>(src[0] * scaleFactor);
   1.771 +    uint8_t srcG = static_cast<uint8_t>(src[1] * scaleFactor);
   1.772 +    uint8_t srcB = static_cast<uint8_t>(src[2] * scaleFactor);
   1.773 +    *dst = ( ((srcR & 0xF8) << 8)
   1.774 +           | ((srcG & 0xFC) << 3)
   1.775 +           | ((srcB & 0xF8) >> 3));
   1.776 +}
   1.777 +
   1.778 +template<> MOZ_ALWAYS_INLINE void
   1.779 +pack<WebGLTexelFormat::RGB32F, WebGLTexelPremultiplicationOp::None, float, float>(const float* __restrict src, float* __restrict dst)
   1.780 +{
   1.781 +    dst[0] = src[0];
   1.782 +    dst[1] = src[1];
   1.783 +    dst[2] = src[2];
   1.784 +}
   1.785 +
   1.786 +template<> MOZ_ALWAYS_INLINE void
   1.787 +pack<WebGLTexelFormat::RGB32F, WebGLTexelPremultiplicationOp::Premultiply, float, float>(const float* __restrict src, float* __restrict dst)
   1.788 +{
   1.789 +    float scaleFactor = src[3];
   1.790 +    dst[0] = src[0] * scaleFactor;
   1.791 +    dst[1] = src[1] * scaleFactor;
   1.792 +    dst[2] = src[2] * scaleFactor;
   1.793 +}
   1.794 +
   1.795 +template<> MOZ_ALWAYS_INLINE void
   1.796 +pack<WebGLTexelFormat::RGBA32F, WebGLTexelPremultiplicationOp::None, float, float>(const float* __restrict src, float* __restrict dst)
   1.797 +{
   1.798 +    dst[0] = src[0];
   1.799 +    dst[1] = src[1];
   1.800 +    dst[2] = src[2];
   1.801 +    dst[3] = src[3];
   1.802 +}
   1.803 +
   1.804 +template<> MOZ_ALWAYS_INLINE void
   1.805 +pack<WebGLTexelFormat::RGBA32F, WebGLTexelPremultiplicationOp::Premultiply, float, float>(const float* __restrict src, float* __restrict dst)
   1.806 +{
   1.807 +    float scaleFactor = src[3];
   1.808 +    dst[0] = src[0] * scaleFactor;
   1.809 +    dst[1] = src[1] * scaleFactor;
   1.810 +    dst[2] = src[2] * scaleFactor;
   1.811 +    dst[3] = src[3];
   1.812 +}
   1.813 +
   1.814 +template<> MOZ_ALWAYS_INLINE void
   1.815 +pack<WebGLTexelFormat::A32F, WebGLTexelPremultiplicationOp::None, float, float>(const float* __restrict src, float* __restrict dst)
   1.816 +{
   1.817 +    dst[0] = src[3];
   1.818 +}
   1.819 +
   1.820 +template<> MOZ_ALWAYS_INLINE void
   1.821 +pack<WebGLTexelFormat::A32F, WebGLTexelPremultiplicationOp::Premultiply, float, float>(const float* __restrict src, float* __restrict dst)
   1.822 +{
   1.823 +    dst[0] = src[3];
   1.824 +}
   1.825 +
   1.826 +template<> MOZ_ALWAYS_INLINE void
   1.827 +pack<WebGLTexelFormat::R32F, WebGLTexelPremultiplicationOp::None, float, float>(const float* __restrict src, float* __restrict dst)
   1.828 +{
   1.829 +    dst[0] = src[0];
   1.830 +}
   1.831 +
   1.832 +template<> MOZ_ALWAYS_INLINE void
   1.833 +pack<WebGLTexelFormat::R32F, WebGLTexelPremultiplicationOp::Premultiply, float, float>(const float* __restrict src, float* __restrict dst)
   1.834 +{
   1.835 +    float scaleFactor = src[3];
   1.836 +    dst[0] = src[0] * scaleFactor;
   1.837 +}
   1.838 +
   1.839 +template<> MOZ_ALWAYS_INLINE void
   1.840 +pack<WebGLTexelFormat::RA32F, WebGLTexelPremultiplicationOp::None, float, float>(const float* __restrict src, float* __restrict dst)
   1.841 +{
   1.842 +    dst[0] = src[0];
   1.843 +    dst[1] = src[3];
   1.844 +}
   1.845 +
   1.846 +template<> MOZ_ALWAYS_INLINE void
   1.847 +pack<WebGLTexelFormat::RA32F, WebGLTexelPremultiplicationOp::Premultiply, float, float>(const float* __restrict src, float* __restrict dst)
   1.848 +{
   1.849 +    float scaleFactor = src[3];
   1.850 +    dst[0] = src[0] * scaleFactor;
   1.851 +    dst[1] = scaleFactor;
   1.852 +}
   1.853 +
   1.854 +template<> MOZ_ALWAYS_INLINE void
   1.855 +pack<WebGLTexelFormat::RGB16F, WebGLTexelPremultiplicationOp::None, uint16_t, uint16_t>(const uint16_t* __restrict src, uint16_t* __restrict dst)
   1.856 +{
   1.857 +    dst[0] = src[0];
   1.858 +    dst[1] = src[1];
   1.859 +    dst[2] = src[2];
   1.860 +}
   1.861 +
   1.862 +template<> MOZ_ALWAYS_INLINE void
   1.863 +pack<WebGLTexelFormat::RGB16F, WebGLTexelPremultiplicationOp::Premultiply, uint16_t, uint16_t>(const uint16_t* __restrict src, uint16_t* __restrict dst)
   1.864 +{
   1.865 +    float scaleFactor = unpackFromFloat16(src[3]);
   1.866 +    dst[0] = packToFloat16(unpackFromFloat16(src[0]) * scaleFactor);
   1.867 +    dst[1] = packToFloat16(unpackFromFloat16(src[1]) * scaleFactor);
   1.868 +    dst[2] = packToFloat16(unpackFromFloat16(src[2]) * scaleFactor);
   1.869 +}
   1.870 +
   1.871 +template<> MOZ_ALWAYS_INLINE void
   1.872 +pack<WebGLTexelFormat::RGBA16F, WebGLTexelPremultiplicationOp::None, uint16_t, uint16_t>(const uint16_t* __restrict src, uint16_t* __restrict dst)
   1.873 +{
   1.874 +    dst[0] = src[0];
   1.875 +    dst[1] = src[1];
   1.876 +    dst[2] = src[2];
   1.877 +    dst[3] = src[3];
   1.878 +}
   1.879 +
   1.880 +template<> MOZ_ALWAYS_INLINE void
   1.881 +pack<WebGLTexelFormat::RGBA16F, WebGLTexelPremultiplicationOp::Premultiply, uint16_t, uint16_t>(const uint16_t* __restrict src, uint16_t* __restrict dst)
   1.882 +{
   1.883 +    float scaleFactor = unpackFromFloat16(src[3]);
   1.884 +    dst[0] = packToFloat16(unpackFromFloat16(src[0]) * scaleFactor);
   1.885 +    dst[1] = packToFloat16(unpackFromFloat16(src[1]) * scaleFactor);
   1.886 +    dst[2] = packToFloat16(unpackFromFloat16(src[2]) * scaleFactor);
   1.887 +    dst[3] = src[3];
   1.888 +}
   1.889 +
   1.890 +template<> MOZ_ALWAYS_INLINE void
   1.891 +pack<WebGLTexelFormat::A16F, WebGLTexelPremultiplicationOp::None, uint16_t, uint16_t>(const uint16_t* __restrict src, uint16_t* __restrict dst)
   1.892 +{
   1.893 +    dst[0] = src[3];
   1.894 +}
   1.895 +
   1.896 +template<> MOZ_ALWAYS_INLINE void
   1.897 +pack<WebGLTexelFormat::A16F, WebGLTexelPremultiplicationOp::Premultiply, uint16_t, uint16_t>(const uint16_t* __restrict src, uint16_t* __restrict dst)
   1.898 +{
   1.899 +    dst[0] = src[3];
   1.900 +}
   1.901 +
   1.902 +template<> MOZ_ALWAYS_INLINE void
   1.903 +pack<WebGLTexelFormat::R16F, WebGLTexelPremultiplicationOp::None, uint16_t, uint16_t>(const uint16_t* __restrict src, uint16_t* __restrict dst)
   1.904 +{
   1.905 +    dst[0] = src[0];
   1.906 +}
   1.907 +
   1.908 +template<> MOZ_ALWAYS_INLINE void
   1.909 +pack<WebGLTexelFormat::R16F, WebGLTexelPremultiplicationOp::Premultiply, uint16_t, uint16_t>(const uint16_t* __restrict src, uint16_t* __restrict dst)
   1.910 +{
   1.911 +    float scaleFactor = unpackFromFloat16(src[3]);
   1.912 +    dst[0] = packToFloat16(unpackFromFloat16(src[0]) * scaleFactor);
   1.913 +}
   1.914 +
   1.915 +template<> MOZ_ALWAYS_INLINE void
   1.916 +pack<WebGLTexelFormat::RA16F, WebGLTexelPremultiplicationOp::None, uint16_t, uint16_t>(const uint16_t* __restrict src, uint16_t* __restrict dst)
   1.917 +{
   1.918 +    dst[0] = src[0];
   1.919 +    dst[1] = src[3];
   1.920 +}
   1.921 +
   1.922 +template<> MOZ_ALWAYS_INLINE void
   1.923 +pack<WebGLTexelFormat::RA16F, WebGLTexelPremultiplicationOp::Premultiply, uint16_t, uint16_t>(const uint16_t* __restrict src, uint16_t* __restrict dst)
   1.924 +{
   1.925 +    float scaleFactor = unpackFromFloat16(src[3]);
   1.926 +    dst[0] = packToFloat16(unpackFromFloat16(src[0]) * scaleFactor);
   1.927 +    dst[1] = scaleFactor;
   1.928 +}
   1.929 +
   1.930 +/****** END CODE SHARED WITH WEBKIT ******/
   1.931 +
   1.932 +template<typename SrcType, typename DstType> MOZ_ALWAYS_INLINE void
   1.933 +convertType(const SrcType* __restrict src, DstType* __restrict dst)
   1.934 +{
   1.935 +    MOZ_ASSERT(false, "Unimplemented texture format conversion");
   1.936 +}
   1.937 +
   1.938 +template<> MOZ_ALWAYS_INLINE void
   1.939 +convertType<uint8_t, uint8_t>(const uint8_t* __restrict src, uint8_t* __restrict dst)
   1.940 +{
   1.941 +    dst[0] = src[0];
   1.942 +    dst[1] = src[1];
   1.943 +    dst[2] = src[2];
   1.944 +    dst[3] = src[3];
   1.945 +}
   1.946 +
   1.947 +template<> MOZ_ALWAYS_INLINE void
   1.948 +convertType<uint16_t, uint16_t>(const uint16_t* __restrict src, uint16_t* __restrict dst)
   1.949 +{
   1.950 +    dst[0] = src[0];
   1.951 +    dst[1] = src[1];
   1.952 +    dst[2] = src[2];
   1.953 +    dst[3] = src[3];
   1.954 +}
   1.955 +
   1.956 +template<> MOZ_ALWAYS_INLINE void
   1.957 +convertType<float, float>(const float* __restrict src, float* __restrict dst)
   1.958 +{
   1.959 +    dst[0] = src[0];
   1.960 +    dst[1] = src[1];
   1.961 +    dst[2] = src[2];
   1.962 +    dst[3] = src[3];
   1.963 +}
   1.964 +
   1.965 +template<> MOZ_ALWAYS_INLINE void
   1.966 +convertType<uint8_t, float>(const uint8_t* __restrict src, float* __restrict dst)
   1.967 +{
   1.968 +    const float scaleFactor = 1.f / 255.0f;
   1.969 +    dst[0] = src[0] * scaleFactor;
   1.970 +    dst[1] = src[1] * scaleFactor;
   1.971 +    dst[2] = src[2] * scaleFactor;
   1.972 +    dst[3] = src[3] * scaleFactor;
   1.973 +}
   1.974 +
   1.975 +template<> MOZ_ALWAYS_INLINE void
   1.976 +convertType<uint8_t, uint16_t>(const uint8_t* __restrict src, uint16_t* __restrict dst)
   1.977 +{
   1.978 +    const float scaleFactor = 1.f / 255.0f;
   1.979 +    dst[0] = packToFloat16(src[0] * scaleFactor);
   1.980 +    dst[1] = packToFloat16(src[1] * scaleFactor);
   1.981 +    dst[2] = packToFloat16(src[2] * scaleFactor);
   1.982 +    dst[3] = packToFloat16(src[3] * scaleFactor);
   1.983 +}
   1.984 +
   1.985 +} // end namespace WebGLTexelConversions
   1.986 +
   1.987 +} // end namespace mozilla
   1.988 +
   1.989 +#endif // WEBGLTEXELCONVERSIONS_H_

mercurial