content/canvas/test/webgl-conformance/conformance/textures/tex-image-and-sub-image-2d-with-array-buffer-view.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/textures/tex-image-and-sub-image-2d-with-array-buffer-view.html	Wed Dec 31 06:09:35 2014 +0100
     1.3 @@ -0,0 +1,193 @@
     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 src="../resources/webgl-test-utils.js"></script>
    1.17 +</head>
    1.18 +<body>
    1.19 +<canvas id="example" width="1px" height="2px"></canvas>
    1.20 +<div id="description"></div>
    1.21 +<div id="console"></div>
    1.22 +<script>
    1.23 +description('Verifies texImage2D and texSubImage2D code paths taking ArrayBufferView');
    1.24 +
    1.25 +var wtu = WebGLTestUtils;
    1.26 +var gl = null;
    1.27 +var textureLoc = null;
    1.28 +var successfullyParsed = false;
    1.29 +
    1.30 +// These two declarations need to be global for "shouldBe" to see them
    1.31 +var buf = null;
    1.32 +var idx = 0;
    1.33 +var pixel = [0, 0, 0, 1];
    1.34 +var correctColor = null;
    1.35 +
    1.36 +function generateRGBAData(type, unpackAlignment)
    1.37 +{
    1.38 +    var sourceData = [ 255,   0,   0, 255,
    1.39 +                       0,   255,   0,   0 ];
    1.40 +    switch (type) {
    1.41 +    case gl.UNSIGNED_BYTE: {
    1.42 +        var rowWidth = Math.max(unpackAlignment, 4);
    1.43 +        var data = new Uint8Array(2 * rowWidth);
    1.44 +        for (var y = 0; y < 2; ++y) {
    1.45 +            var index = y * rowWidth;
    1.46 +            for (var element = 0; element < 4; ++element) {
    1.47 +                data[index + element] = sourceData[4 * y + element];
    1.48 +            }
    1.49 +        }
    1.50 +        return data;
    1.51 +    }
    1.52 +    case gl.UNSIGNED_SHORT_4_4_4_4: {
    1.53 +        var rowWidth = Math.max(unpackAlignment, 2) / 2;
    1.54 +        var data = new Uint16Array(2 * rowWidth);
    1.55 +        for (var y = 0; y < 2; ++y) {
    1.56 +            var index = y * rowWidth;
    1.57 +            data[index] = (((sourceData[4 * y] & 0xF0) << 8)
    1.58 +                           | ((sourceData[4 * y + 1] & 0xF0) << 4)
    1.59 +                           | (sourceData[4 * y + 2] & 0xF0)
    1.60 +                           | (sourceData[4 * y + 3] >> 4));
    1.61 +        }
    1.62 +        return data;
    1.63 +    }
    1.64 +    case gl.UNSIGNED_SHORT_5_5_5_1: {
    1.65 +        var rowWidth = Math.max(unpackAlignment, 2) / 2;
    1.66 +        var data = new Uint16Array(2 * rowWidth);
    1.67 +        for (var y = 0; y < 2; ++y) {
    1.68 +            var index = y * rowWidth;
    1.69 +            data[index] = (((sourceData[4 * y] & 0xF8) << 8)
    1.70 +                           | ((sourceData[4 * y + 1] & 0xF8) << 3)
    1.71 +                           | ((sourceData[4 * y + 2] & 0xF8) >> 2)
    1.72 +                           | (sourceData[4 * y + 3] >> 7));
    1.73 +        }
    1.74 +        return data;
    1.75 +    }
    1.76 +    }
    1.77 +}
    1.78 +
    1.79 +function typeToString(type)
    1.80 +{
    1.81 +    switch (type) {
    1.82 +    case gl.UNSIGNED_BYTE:  return 'UNSIGNED_BYTE';
    1.83 +    case gl.UNSIGNED_SHORT_5_5_5_1:  return 'UNSIGNED_SHORT_5_5_5_1';
    1.84 +    case gl.UNSIGNED_SHORT_4_4_4_4:  return 'UNSIGNED_SHORT_4_4_4_4';
    1.85 +    }
    1.86 +    return 'Unknown type ' + type;
    1.87 +}
    1.88 +
    1.89 +function runOneIteration(useTexSubImage2D, type, unpackAlignment, flipY, premultiplyAlpha, topColor, bottomColor)
    1.90 +{
    1.91 +    debug('Testing ' + (useTexSubImage2D ? 'texSubImage2D' : 'texImage2D') +
    1.92 +          ' with type=' + typeToString(type) +
    1.93 +          ', unpackAlignment=' + unpackAlignment +
    1.94 +          ', flipY=' + flipY + ', premultiplyAlpha=' + premultiplyAlpha);
    1.95 +    gl.colorMask(true, true, true, true);
    1.96 +    gl.clearColor(0, 0, 0, 1.0);
    1.97 +    gl.clear(gl.COLOR_BUFFER_BIT | gl.DEPTH_BUFFER_BIT);
    1.98 +    // Enable writes to the RGB channels
    1.99 +    gl.colorMask(true, true, true, false);
   1.100 +    var texture = gl.createTexture();
   1.101 +    // Bind the texture to texture unit 0
   1.102 +    gl.bindTexture(gl.TEXTURE_2D, texture);
   1.103 +    // Set up texture parameters
   1.104 +    gl.texParameteri(gl.TEXTURE_2D, gl.TEXTURE_MIN_FILTER, gl.NEAREST);
   1.105 +    gl.texParameteri(gl.TEXTURE_2D, gl.TEXTURE_MAG_FILTER, gl.NEAREST);
   1.106 +    // Set up pixel store parameters
   1.107 +    gl.pixelStorei(gl.UNPACK_ALIGNMENT, unpackAlignment);
   1.108 +    gl.pixelStorei(gl.UNPACK_FLIP_Y_WEBGL, flipY);
   1.109 +    gl.pixelStorei(gl.UNPACK_PREMULTIPLY_ALPHA_WEBGL, premultiplyAlpha);
   1.110 +    // Generate the data
   1.111 +    var data = generateRGBAData(type, unpackAlignment);
   1.112 +    if (gl.getError() != gl.NO_ERROR)
   1.113 +        testFailed("GL error before texture upload");
   1.114 +    // Upload the image into the texture
   1.115 +    if (useTexSubImage2D) {
   1.116 +        // Initialize the texture to black first
   1.117 +        gl.texImage2D(gl.TEXTURE_2D, 0, gl.RGBA, 1, 2, 0,
   1.118 +                      gl.RGBA, type, null);
   1.119 +        if (gl.getError() != gl.NO_ERROR)
   1.120 +            testFailed("GL error after texImage2D(null)");
   1.121 +        gl.texSubImage2D(gl.TEXTURE_2D, 0, 0, 0, 1, 2, gl.RGBA, type, data);
   1.122 +        if (gl.getError() != gl.NO_ERROR)
   1.123 +            testFailed("GL error after texSubImage2D");
   1.124 +    } else {
   1.125 +        gl.texImage2D(gl.TEXTURE_2D, 0, gl.RGBA, 1, 2, 0, gl.RGBA, type, data);
   1.126 +        if (gl.getError() != gl.NO_ERROR)
   1.127 +            testFailed("GL error after texImage2D");
   1.128 +    }
   1.129 +
   1.130 +    // Point the uniform sampler to texture unit 0
   1.131 +    gl.uniform1i(textureLoc, 0);
   1.132 +    // Draw the triangles
   1.133 +    wtu.drawQuad(gl, [0, 0, 0, 255]);
   1.134 +
   1.135 +    // Read back the rendering results
   1.136 +    buf = new Uint8Array(1 * 2 * 4);
   1.137 +    gl.readPixels(0, 0, 1, 2, gl.RGBA, gl.UNSIGNED_BYTE, buf);
   1.138 +    // Check the top pixel and bottom pixel and make sure they have
   1.139 +    // the right color.
   1.140 +    debug("Checking bottom pixel");
   1.141 +    wtu.checkCanvasRect(gl, 0, 0, 1, 1, bottomColor, "shouldBe " + bottomColor);
   1.142 +    debug("Checking top pixel");
   1.143 +    wtu.checkCanvasRect(gl, 0, 1, 1, 1, topColor, "shouldBe " + topColor);
   1.144 +}
   1.145 +
   1.146 +function runTest()
   1.147 +{
   1.148 +    var red = [255, 0, 0, 255];
   1.149 +    var green = [0, 255, 0, 255];
   1.150 +    var redPremultiplyAlpha = [255, 0, 0, 255];
   1.151 +    var greenPremultiplyAlpha = [0, 0, 0, 255];
   1.152 +
   1.153 +    var types = [ gl.UNSIGNED_BYTE, gl.UNSIGNED_SHORT_5_5_5_1, gl.UNSIGNED_SHORT_4_4_4_4 ];
   1.154 +    var unpackAlignments = [ 1, 2, 4, 8 ];
   1.155 +
   1.156 +    for (var i = 0; i < types.length; ++i) {
   1.157 +        var type = types[i];
   1.158 +        for (var j = 0; j < unpackAlignments.length; ++j) {
   1.159 +            var unpackAlignment = unpackAlignments[j];
   1.160 +            runOneIteration(false, type, unpackAlignment, true, false,
   1.161 +                            red, green);
   1.162 +            runOneIteration(false, type, unpackAlignment, false, false,
   1.163 +                            green, red);
   1.164 +            runOneIteration(false, type, unpackAlignment, true, true,
   1.165 +                            redPremultiplyAlpha, greenPremultiplyAlpha);
   1.166 +            runOneIteration(false, type, unpackAlignment, false, true,
   1.167 +                            greenPremultiplyAlpha, redPremultiplyAlpha);
   1.168 +            runOneIteration(true, type, unpackAlignment, true, false,
   1.169 +                            red, green);
   1.170 +            runOneIteration(true, type, unpackAlignment, false, false,
   1.171 +                            green, red);
   1.172 +            runOneIteration(true, type, unpackAlignment, true, true,
   1.173 +                            redPremultiplyAlpha, greenPremultiplyAlpha);
   1.174 +            runOneIteration(true, type, unpackAlignment, false, true,
   1.175 +                            greenPremultiplyAlpha, redPremultiplyAlpha);
   1.176 +        }
   1.177 +    }
   1.178 +
   1.179 +}
   1.180 +
   1.181 +gl = wtu.create3DContext("example");
   1.182 +var program = wtu.setupTexturedQuad(gl);
   1.183 +gl.disable(gl.BLEND);
   1.184 +
   1.185 +gl.clearColor(0,0,0,1);
   1.186 +gl.clearDepth(1);
   1.187 +
   1.188 +textureLoc = gl.getUniformLocation(program, "tex");
   1.189 +
   1.190 +runTest();
   1.191 +glErrorShouldBe(gl, gl.NO_ERROR, "should be no errors");
   1.192 +successfullyParsed = true;
   1.193 +</script>
   1.194 +<script>finishTest();</script>
   1.195 +</body>
   1.196 +</html>

mercurial