content/canvas/test/webgl-conformance/conformance/textures/gl-pixelstorei.html

Wed, 31 Dec 2014 13:27:57 +0100

author
Michael Schloh von Bennewitz <michael@schloh.com>
date
Wed, 31 Dec 2014 13:27:57 +0100
branch
TOR_BUG_3246
changeset 6
8bccb770b82d
permissions
-rw-r--r--

Ignore runtime configuration files generated during quality assurance.

     1 <!--
     2 Copyright (c) 2011 The Chromium Authors. All rights reserved.
     3 Use of this source code is governed by a BSD-style license that can be
     4 found in the LICENSE file.
     5 -->
     6 <!DOCTYPE html>
     7 <html>
     8 <head>
     9 <meta charset="utf-8">
    10 <title>WebGL pixelStorei Test</title>
    11 <link rel="stylesheet" href="../../resources/js-test-style.css"/>
    12 <script src="../../resources/js-test-pre.js"></script>
    13 <script src="../resources/webgl-test.js"> </script>
    14 <script src="../../resources/desktop-gl-constants.js" type="text/javascript"></script>
    15 </head>
    16 <body>
    17 <canvas id="example" width="50" height="50"></canvas>
    18 <canvas id="2d00" width="50" height="50"></canvas>
    19 <canvas id="2d01" width="50" height="50"></canvas>
    20 <canvas id="2d02" width="50" height="50"></canvas>
    21 <canvas id="2d03" width="50" height="50"></canvas>
    22 <div id="description"></div>
    23 <div id="console"></div>
    24 <script id="vshader" type="x-shader/x-vertex">
    25 attribute vec4 vPosition;
    26 void main() {
    27   gl_Position = vPosition;
    28 }
    29 </script>
    31 <script id="fshader" type="x-shader/x-fragment">
    32 void main() {
    33   gl_FragColor = vec4(1.0,0.0,0.0,1.0);
    34 }
    35 </script>
    37 <script>
    38 function fail(x,y, name, buf, shouldBe) {
    39   var i = (y*50+x) * 4;
    40   var reason = "pixel in "+name+" at ("+x+","+y+") is ("+buf[i]+","+buf[i+1]+","+buf[i+2]+","+buf[i+3]+"), should be "+shouldBe;
    41   testFailed(reason);
    42 }
    44 function pass(name) {
    45   testPassed("drawing is correct in " + name);
    46 }
    48 function init() {
    49   description("This test checks that drawImage and readPixels are not effected by gl.Pixelstorei(gl.PACK_ALIGNMENT) and visa versa");
    51   debug("There should be 5 red triangles on 5 black squares above");
    52   debug("");
    54   var canvas3d = document.getElementById("example");
    55   gl = initWebGL("example", "vshader", "fshader", [ "vPosition"], [ 0, 0, 0, 1 ], 1);
    57   var vertexObject = gl.createBuffer();
    58   gl.bindBuffer(gl.ARRAY_BUFFER, vertexObject);
    59   gl.bufferData(gl.ARRAY_BUFFER, new Float32Array([ 0,0.5,0, -0.5,-0.5,0, 0.5,-0.5,0 ]), gl.STATIC_DRAW);
    60   gl.enableVertexAttribArray(0);
    61   gl.vertexAttribPointer(0, 3, gl.FLOAT, false, 0, 0);
    63   gl.clear(gl.COLOR_BUFFER_BIT | gl.DEPTH_BUFFER_BIT);
    64   gl.drawArrays(gl.TRIANGLES, 0, 3);
    67   function checkData(buf, name) {
    68     // Test several locations
    69     // First line should be all black
    70     for (var i = 0; i < 50; ++i) {
    71       if (buf[i*4] != 0 || buf[i*4+1] != 0 || buf[i*4+2] != 0 || buf[i*4+3] != 255) {
    72         fail(i, 0, name, buf, "(0,0,0,255)");
    73         return;
    74       }
    75     }
    77     // Line 25 should be red for at least 6 red pixels starting 22 pixels in
    78     var offset = (25*50+22) * 4;
    79     for (var i = 0; i < 6; ++i) {
    80       if (buf[offset+i*4] != 255 || buf[offset+i*4+1] != 0 || buf[offset+i*4+2] != 0 || buf[offset+i*4+3] != 255) {
    81         fail(22 + i, 25, name, buf, "(255,0,0,255)");
    82         return;
    83       }
    84     }
    86     // Last line should be all black
    87     offset = (49*50) * 4;
    88     for (var i = 0; i < 50; ++i) {
    89       if (buf[offset+i*4] != 0 || buf[offset+i*4+1] != 0 || buf[offset+i*4+2] != 0 || buf[offset+i*4+3] != 255) {
    90         fail(i, 49, name, buf, "(0,0,0,255)");
    91         return;
    92       }
    93     }
    95     pass(name);
    96   }
    98   function checkColors() {
    99     var buf = new Uint8Array(50 * 50 * 4);
   100     gl.readPixels(0, 0, 50, 50, gl.RGBA, gl.UNSIGNED_BYTE, buf);
   101     checkData(buf, "3d context");
   102     var imgData = ctx2d.getImageData(0, 0, 50, 50);
   103     checkData(imgData.data, "2d context");
   104   }
   106   var table = [1, 2, 4, 8];
   107   for (var ii = 0; ii < table.length; ++ii) {
   108     gl.pixelStorei(gl.PACK_ALIGNMENT, table[ii]);
   109     ctx2d = document.getElementById("2d0" + ii).getContext("2d");
   110     ctx2d.globalCompositeOperation = 'copy';
   111     ctx2d.drawImage(canvas3d, 0, 0);
   112     checkColors();
   113     assertMsg(gl.getParameter(gl.PACK_ALIGNMENT) == table[ii],
   114         "PACK_ALIGNMENT is " + table[ii]);
   115   }
   116 }
   118 init();
   119 successfullyParsed = true;
   120 </script>
   121 <script>finishTest();</script>
   123 </body>
   124 </html>

mercurial