content/canvas/test/webgl-mochitest/test_draw.html

Wed, 31 Dec 2014 06:09:35 +0100

author
Michael Schloh von Bennewitz <michael@schloh.com>
date
Wed, 31 Dec 2014 06:09:35 +0100
changeset 0
6474c204b198
permissions
-rw-r--r--

Cloned upstream origin tor-browser at tor-browser-31.3.0esr-4.5-1-build1
revision ID fc1c9ff7c1b2defdbc039f12214767608f46423f for hacking purpose.

     1 <!DOCTYPE HTML>
     2 <meta http-equiv="content-type" content="text/html; charset=utf-8" />
     4 <title>WebGL test: Basic drawing</title>
     6 <script src="/tests/SimpleTest/SimpleTest.js"></script>
     7 <link rel="stylesheet" href="/tests/SimpleTest/test.css">
     8 <script src="driver-info.js"></script>
     9 <script src="webgl-util.js"></script>
    12 <script id="vs" type="x-shader/x-vertex">
    14 attribute vec2 aVertCoord;
    16 void main(void) {
    17   gl_Position = vec4(aVertCoord, 0.0, 1.0);
    18 }
    20 </script>
    21 <script id="fs" type="x-shader/x-fragment">
    23 precision mediump float;
    25 void main(void) {
    26   gl_FragColor = vec4(0.0, 1.0, 0.0, 1.0);
    27 }
    29 </script>
    30 <body>
    31 <canvas id="c" width="64" height="64"></canvas>
    32 <script>
    34 // Give ourselves a scope to return early from:
    35 (function() {
    36   var gl = WebGLUtil.getWebGL('c');
    37   if (!gl) {
    38     todo(false, 'WebGL is unavailable.');
    39     return;
    40   }
    42   function errorFunc(str) {
    43     ok(false, 'Error: ' + str);
    44   }
    45   WebGLUtil.setErrorFunc(errorFunc);
    46   WebGLUtil.setWarningFunc(errorFunc);
    48   gl.disable(gl.DEPTH_TEST);
    50   var prog   = WebGLUtil.createProgramByIds(gl, 'vs', 'fs');
    51   if (!prog) {
    52     ok(false, 'Program linking should succeed.');
    53     return;
    54   }
    56   prog.aVertCoord = gl.getAttribLocation(prog, "aVertCoord");
    57   ok(prog.aVertCoord >= 0, '`aVertCoord` should be valid.');
    59   function checkGLError(func, info, refValue) {
    60     if (!refValue)
    61       refValue = 0;
    63     var error = gl.getError();
    64     func(error == refValue,
    65          '[' + info + '] gl.getError should be 0x' + refValue.toString(16) +
    66                                         ', was 0x' + error.toString(16) + '.');
    67   }
    69   var vertCoordArr = new Float32Array([
    70     -1, -1,
    71      1, -1,
    72     -1,  1,
    73      1,  1,
    74   ]);
    75   var vertCoordBuff = gl.createBuffer();
    76   gl.bindBuffer(gl.ARRAY_BUFFER, vertCoordBuff);
    77   gl.bufferData(gl.ARRAY_BUFFER, vertCoordArr, gl.STATIC_DRAW);
    79   var indexArr = new Uint16Array([
    80     0, 1, 2,
    81     3,
    82   ]);
    83   var indexBuff = gl.createBuffer();
    84   gl.bindBuffer(gl.ELEMENT_ARRAY_BUFFER, indexBuff);
    85   gl.bufferData(gl.ELEMENT_ARRAY_BUFFER, indexArr, gl.STATIC_DRAW);
    88   function testPixel(x, y, refData, func, infoString) {
    89     var pixel = new Uint8Array(4);
    90     gl.readPixels(x, y, 1, 1, gl.RGBA, gl.UNSIGNED_BYTE, pixel);
    92     var pixelMatches = pixel[0] == refData[0] &&
    93                        pixel[1] == refData[1] &&
    94                        pixel[2] == refData[2] &&
    95                        pixel[3] == refData[3];
    96     func(pixelMatches, infoString);
    97   }
    99   function preDraw(info) {
   100     gl.clearColor(1.0, 0.0, 0.0, 1.0);
   101     gl.clear(gl.COLOR_BUFFER_BIT);
   103     testPixel(0, 0, [255, 0, 0, 255], ok, '[' + info + '] Should be red before drawing.');
   104   }
   106   function postDraw(info) {
   107     testPixel(0, 0, [0, 255, 0, 255], ok, '[' + info + '] Should be green before drawing.');
   108   }
   110   gl.useProgram(prog);
   111   gl.enableVertexAttribArray(prog.aVertCoord);
   112   gl.vertexAttribPointer(prog.aVertCoord, 2, gl.FLOAT, false, 0, 0);
   114   // Start drawing
   115   checkGLError(ok, 'after setup');
   117   preDraw('DrawArrays');
   118   gl.drawArrays(gl.TRIANGLE_STRIP, 0, 4);
   119   postDraw('DrawArrays');
   120   checkGLError(ok, 'after DrawArrays');
   122   preDraw('DrawElements');
   123   gl.drawElements(gl.TRIANGLE_STRIP, 4, gl.UNSIGNED_SHORT, 0);
   124   postDraw('DrawElements');
   125   checkGLError(ok, 'after DrawElements');
   127   ok(true, 'Test complete.');
   128 })();
   130 </script>

mercurial