content/canvas/test/webgl-conformance/conformance/reading/read-pixels-pack-alignment.html

changeset 0
6474c204b198
     1.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     1.2 +++ b/content/canvas/test/webgl-conformance/conformance/reading/read-pixels-pack-alignment.html	Wed Dec 31 06:09:35 2014 +0100
     1.3 @@ -0,0 +1,240 @@
     1.4 +<!--
     1.5 +Copyright (c) 2011 The Chromium Authors. All rights reserved.
     1.6 +Use of this source code is governed by a BSD-style license that can be
     1.7 +found in the LICENSE file.
     1.8 + -->
     1.9 +<!DOCTYPE html>
    1.10 +<html>
    1.11 +<head>
    1.12 +<meta charset="utf-8">
    1.13 +<link rel="stylesheet" href="../../resources/js-test-style.css"/>
    1.14 +<script src="../../resources/js-test-pre.js"></script>
    1.15 +<script src="../resources/webgl-test.js"></script>
    1.16 +<script id="vshader" type="x-shader/x-vertex">
    1.17 +attribute vec3 pos;
    1.18 +attribute vec4 colorIn;
    1.19 +varying vec4 color;
    1.20 +
    1.21 +void main()
    1.22 +{
    1.23 +    color = colorIn;
    1.24 +    gl_Position = vec4(pos.xyz, 1.0);
    1.25 +}
    1.26 +</script>
    1.27 +
    1.28 +<script id="fshader" type="x-shader/x-fragment">
    1.29 +precision mediump float;
    1.30 +varying vec4 color;
    1.31 +
    1.32 +void main()
    1.33 +{
    1.34 +    gl_FragColor = color;
    1.35 +}
    1.36 +</script>
    1.37 +</head>
    1.38 +<body>
    1.39 +<canvas id="example" width="32px" height="32px"></canvas>
    1.40 +<div id="description"></div>
    1.41 +<div id="console"></div>
    1.42 +<script>
    1.43 +var successfullyParsed = false;
    1.44 +
    1.45 +// The below declarations need to be global for "shouldBe" to see them
    1.46 +var gl = null;
    1.47 +var array = null;
    1.48 +var pixel = [ 0, 0, 0, 0 ];
    1.49 +var expectedColor = [ 0, 0, 0, 0 ];
    1.50 +
    1.51 +function calculatePixelBytes(format, type)
    1.52 +{
    1.53 +    var size = 0;
    1.54 +    switch (format) {
    1.55 +    case gl.ALPHA:
    1.56 +        size = 1;
    1.57 +        break;
    1.58 +    case gl.RGB:
    1.59 +        size = 3;
    1.60 +        break;
    1.61 +    case gl.RGBA:
    1.62 +        size = 4;
    1.63 +        break;
    1.64 +    default:
    1.65 +        return -1;
    1.66 +    }
    1.67 +    switch (type) {
    1.68 +    case gl.UNSIGNED_BYTE:
    1.69 +        break;
    1.70 +    case gl.UNSIGNED_SHORT_5_6_5:
    1.71 +        if (format != gl.RGB)
    1.72 +            return -1;
    1.73 +        size = 2;
    1.74 +        break;
    1.75 +    case gl.UNSIGNED_SHORT_4_4_4_4:
    1.76 +    case gl.UNSIGNED_SHORT_5_5_5_1:
    1.77 +        if (format != gl.RGBA)
    1.78 +            return -1;
    1.79 +        size = 2;
    1.80 +        break;
    1.81 +    default:
    1.82 +        return -1;
    1.83 +    }
    1.84 +    return size;
    1.85 +}
    1.86 +
    1.87 +function calculatePaddingBytes(bytesPerPixel, packAlignment, width)
    1.88 +{
    1.89 +    var padding = 0;
    1.90 +    switch (packAlignment) {
    1.91 +    case 1:
    1.92 +    case 2:
    1.93 +    case 4:
    1.94 +    case 8:
    1.95 +        padding = (bytesPerPixel * width) % packAlignment;
    1.96 +        if (padding > 0)
    1.97 +            padding = packAlignment - padding;
    1.98 +        break;
    1.99 +    default:
   1.100 +        return -1;
   1.101 +    }
   1.102 +    return padding;
   1.103 +}
   1.104 +
   1.105 +function packColor(format, type, r, g, b, a)
   1.106 +{
   1.107 +    // FIXME: not sure if the color packing is correct for UNSIGNED_SHORT_*.
   1.108 +    var color = [ 0, 0, 0, 0 ];
   1.109 +    switch (type) {
   1.110 +    case gl.UNSIGNED_BYTE:
   1.111 +        switch (format) {
   1.112 +        case gl.ALPHA:
   1.113 +            color[0] = a;
   1.114 +            break;
   1.115 +        case gl.RGB:
   1.116 +            color[0] = r;
   1.117 +            color[1] = g;
   1.118 +            color[2] = b;
   1.119 +            break;
   1.120 +        case gl.RGBA:
   1.121 +            color[0] = r;
   1.122 +            color[1] = g;
   1.123 +            color[2] = b;
   1.124 +            color[3] = a;
   1.125 +            break;
   1.126 +        default:
   1.127 +            return null;
   1.128 +        }
   1.129 +        break;
   1.130 +    case gl.UNSIGNED_SHORT_5_6_5:
   1.131 +        if (format != gl.RGB)
   1.132 +            return null;
   1.133 +        r >>= 3;
   1.134 +        g >>= 2;
   1.135 +        b >>= 3;
   1.136 +        color[0] = (r << 11) + (g << 5) + b;
   1.137 +        break;
   1.138 +    case gl.UNSIGNED_SHORT_4_4_4_4:
   1.139 +        if (format != gl.RGBA)
   1.140 +            return null;
   1.141 +        r >>= 4;
   1.142 +        g >>= 4;
   1.143 +        b >>= 4;
   1.144 +        a >>= 4;
   1.145 +        color[0] = (r << 12) + (g << 8) + (b << 4) + a;
   1.146 +        break;
   1.147 +    case gl.UNSIGNED_SHORT_5_5_5_1:
   1.148 +        if (format != gl.RGBA)
   1.149 +            return null;
   1.150 +        r >>= 3;
   1.151 +        g >>= 3;
   1.152 +        b >>= 3;
   1.153 +        a >>= 7;
   1.154 +        color[0] = (r << 11) + (g << 6) + (b << 1) + a;
   1.155 +        break;
   1.156 +    Default:
   1.157 +        return null;
   1.158 +    }
   1.159 +    return color;
   1.160 +}
   1.161 +
   1.162 +function runTestIteration(format, type, packAlignment, width, height)
   1.163 +{
   1.164 +    debug("Testing PACK_ALIGNMENT = " + packAlignment + ", width = " + width + ", height = " + height);
   1.165 +    gl.clearColor(1, 0.4, 0, 1);
   1.166 +    gl.clear(gl.COLOR_BUFFER_BIT);
   1.167 +    gl.pixelStorei(gl.PACK_ALIGNMENT, packAlignment);
   1.168 +    glErrorShouldBe(gl, gl.NO_ERROR);
   1.169 +    var bytesPerPixel = calculatePixelBytes(format, type);
   1.170 +    var padding = calculatePaddingBytes(bytesPerPixel, packAlignment, width);
   1.171 +    var size = bytesPerPixel * width * height + padding * (height - 1);
   1.172 +    if (type != gl.UNSIGNED_BYTE) {
   1.173 +        throw "test error: only UNSIGNED_BYTE is valid to ReadPixels";
   1.174 +    }
   1.175 +    if (size < 0)
   1.176 +        size = 0;
   1.177 +    array = new Uint8Array(size);
   1.178 +    gl.readPixels(0, 0, width, height, format, type, array);
   1.179 +    if (width < 0 || height < 0) {
   1.180 +        glErrorShouldBe(gl, gl.INVALID_VALUE);
   1.181 +        return;
   1.182 +    }
   1.183 +
   1.184 +    glErrorShouldBe(gl, gl.NO_ERROR);
   1.185 +    if (!array.length)
   1.186 +        return;
   1.187 +
   1.188 +    // Check the last pixel of the last row.
   1.189 +    var bytesPerRow = width * bytesPerPixel + padding;
   1.190 +    var pos = bytesPerRow * (height - 1) + (width - 1) * bytesPerPixel;
   1.191 +    var numComponents = bytesPerPixel;
   1.192 +    for (var i = 0; i < numComponents; ++i)
   1.193 +        pixel[i] = array[pos + i];
   1.194 +    for (var i = numComponents; i < 4; ++i)
   1.195 +        pixel[i] = 0;
   1.196 +    expectedColor = packColor(format, type, 255, 102, 0, 255);
   1.197 +    shouldBeNonNull("expectedColor");
   1.198 +    shouldBe("pixel", "expectedColor");
   1.199 +}
   1.200 +
   1.201 +description('Verify readPixels() works fine with various PACK_ALIGNMENT values.');
   1.202 +
   1.203 +shouldBeNonNull("gl = initWebGL('example', 'vshader', 'fshader', [ 'pos', 'colorIn' ], [ 0, 0, 0, 1 ], 1)");
   1.204 +gl.disable(gl.BLEND);
   1.205 +
   1.206 +var formats = [ gl.RGBA ];
   1.207 +var formatNames = [ "RGBA" ];
   1.208 +
   1.209 +for (var i = 0; i < formats.length; ++i) {
   1.210 +    var format = formats[i];
   1.211 +
   1.212 +    debug("Testing format = " + formatNames[i] + " and type = UNSIGNED_BYTE");
   1.213 +    runTestIteration(format, gl.UNSIGNED_BYTE, 1, 1, 2);
   1.214 +    runTestIteration(format, gl.UNSIGNED_BYTE, 2, 1, 2);
   1.215 +    runTestIteration(format, gl.UNSIGNED_BYTE, 4, 1, 2);
   1.216 +    runTestIteration(format, gl.UNSIGNED_BYTE, 8, 1, 2);
   1.217 +    runTestIteration(format, gl.UNSIGNED_BYTE, 4, 2, 2);
   1.218 +    runTestIteration(format, gl.UNSIGNED_BYTE, 8, 2, 2);
   1.219 +    runTestIteration(format, gl.UNSIGNED_BYTE, 4, 3, 2);
   1.220 +    runTestIteration(format, gl.UNSIGNED_BYTE, 8, 3, 2);
   1.221 +    runTestIteration(format, gl.UNSIGNED_BYTE, 4, 4, 2);
   1.222 +    runTestIteration(format, gl.UNSIGNED_BYTE, 8, 4, 2);
   1.223 +    runTestIteration(format, gl.UNSIGNED_BYTE, 8, 5, 1);
   1.224 +    runTestIteration(format, gl.UNSIGNED_BYTE, 4, 5, 2);
   1.225 +    runTestIteration(format, gl.UNSIGNED_BYTE, 8, 5, 2);
   1.226 +    runTestIteration(format, gl.UNSIGNED_BYTE, 8, 6, 2);
   1.227 +    runTestIteration(format, gl.UNSIGNED_BYTE, 8, 7, 2);
   1.228 +    runTestIteration(format, gl.UNSIGNED_BYTE, 8, 8, 2);
   1.229 +    runTestIteration(format, gl.UNSIGNED_BYTE, 1, 0, 0);
   1.230 +    runTestIteration(format, gl.UNSIGNED_BYTE, 2, 0, 0);
   1.231 +    runTestIteration(format, gl.UNSIGNED_BYTE, 4, 0, 0);
   1.232 +    runTestIteration(format, gl.UNSIGNED_BYTE, 8, 0, 0);
   1.233 +    runTestIteration(format, gl.UNSIGNED_BYTE, 1, -1, 1);
   1.234 +    runTestIteration(format, gl.UNSIGNED_BYTE, 2, 1, -1);
   1.235 +    runTestIteration(format, gl.UNSIGNED_BYTE, 4, 0, -1);
   1.236 +    runTestIteration(format, gl.UNSIGNED_BYTE, 8, -1, -1);
   1.237 +}
   1.238 +    
   1.239 +successfullyParsed = true;
   1.240 +</script>
   1.241 +<script>finishTest();</script>
   1.242 +</body>
   1.243 +</html>

mercurial