content/canvas/test/webgl-conformance/conformance/programs/gl-bind-attrib-location-test.html

Thu, 15 Jan 2015 21:03:48 +0100

author
Michael Schloh von Bennewitz <michael@schloh.com>
date
Thu, 15 Jan 2015 21:03:48 +0100
branch
TOR_BUG_9701
changeset 11
deefc01c0e14
permissions
-rw-r--r--

Integrate friendly tips from Tor colleagues to make (or not) 4.5 alpha 3;
This includes removal of overloaded (but unused) methods, and addition of
a overlooked call to DataStruct::SetData(nsISupports, uint32_t, bool.)

     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 BindAttribLocation Conformance Tests</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 </head>
    15 <body>
    16 <div id="description"></div>
    17 <div id="console"></div>
    18 <canvas style="border: 1px solid black;" id="canvas" width="50" height="50"></canvas>
    19 <script id="vshader" type="text/something-not-javascript">
    20 attribute vec4 vPosition;
    21 attribute vec4 vColor;
    22 varying vec4 color;
    23 void main()
    24 {
    25   gl_Position = vPosition;
    26   color = vColor;
    27 }
    28 </script>
    29 <script id="fshader" type="text/something-not-javascript">
    30 precision mediump float;
    32 varying vec4 color;
    33 void main()
    34 {
    35   gl_FragColor = color;
    36 }
    37 </script>
    38 <script>
    39 description("This test ensures WebGL implementations don't allow names that start with 'gl_' when calling bindAttribLocation.");
    41 debug("");
    42 debug("Canvas.getContext");
    44 var gl = create3DContext(document.getElementById("canvas"));
    45 shouldBeNonNull("gl");
    47 function fail(x,y, buf, shouldBe)
    48 {
    49   var i = (y*50+x) * 4;
    50   var reason = "pixel at ("+x+","+y+") is ("+buf[i]+","+buf[i+1]+","+buf[i+2]+","+buf[i+3]+"), should be "+shouldBe;
    51   testFailed(reason);
    52 }
    54 function pass()
    55 {
    56   testPassed("drawing is correct");
    57 }
    59 function loadShader(shaderType, shaderId) {
    60   // Get the shader source.
    61   var shaderSource = document.getElementById(shaderId).text;
    63   // Create the shader object
    64   var shader = gl.createShader(shaderType);
    65   if (shader == null) {
    66     debug("*** Error: unable to create shader '"+shaderId+"'");
    67     return null;
    68   }
    70   // Load the shader source
    71   gl.shaderSource(shader, shaderSource);
    73   // Compile the shader
    74   gl.compileShader(shader);
    76   // Check the compile status
    77   var compiled = gl.getShaderParameter(shader, gl.COMPILE_STATUS);
    78   if (!compiled) {
    79     // Something went wrong during compilation; get the error
    80     var error = gl.getShaderInfoLog(shader);
    81     debug("*** Error compiling shader '"+shader+"':"+error);
    82     gl.deleteShader(shader);
    83     return null;
    84   }
    85   return shader;
    86 }
    88 debug("");
    89 debug("Checking gl.bindAttribLocation.");
    91 var program = gl.createProgram();
    92 gl.bindAttribLocation(program, 0, "gl_foo");
    93 glErrorShouldBe(gl, gl.INVALID_OPERATION,
    94     "bindAttribLocation should return INVALID_OPERATION if name starts with 'gl_'");
    95 gl.bindAttribLocation(program, 0, "gl_TexCoord0");
    96 glErrorShouldBe(gl, gl.INVALID_OPERATION,
    97     "bindAttribLocation should return INVALID_OPERATION if name starts with 'gl_'");
    99 var vs = loadShader(gl.VERTEX_SHADER, "vshader");
   100 var fs = loadShader(gl.FRAGMENT_SHADER, "fshader");
   101 gl.attachShader(program, vs);
   102 gl.attachShader(program, fs);
   104 var positions = gl.createBuffer();
   105 gl.bindBuffer(gl.ARRAY_BUFFER, positions);
   106 gl.bufferData(gl.ARRAY_BUFFER, new Float32Array([ 0,0.5,0, -0.5,-0.5,0, 0.5,-0.5,0 ]), gl.STATIC_DRAW);
   108 var colors = gl.createBuffer();
   109 gl.bindBuffer(gl.ARRAY_BUFFER, colors);
   110 gl.bufferData(gl.ARRAY_BUFFER, new Float32Array([
   111     0,1,0,1,
   112     0,1,0,1,
   113     0,1,0,1]), gl.STATIC_DRAW);
   115 function setBindLocations(colorLocation, positionLocation) {
   116   gl.bindAttribLocation(program, positionLocation, "vPosition");
   117   gl.bindAttribLocation(program, colorLocation, "vColor");
   118   gl.linkProgram(program);
   119   gl.useProgram(program);
   120   var linked = (gl.getProgramParameter(program, gl.LINK_STATUS) != 0);
   121   assertMsg(linked, "program linked successfully");
   123   debug("vPosition:" + gl.getAttribLocation(program, "vPosition"))
   124   debug("vColor   :" + gl.getAttribLocation(program, "vColor"))
   125   assertMsg(gl.getAttribLocation(program, "vPosition") == positionLocation,
   126             "location of vPosition should be " + positionLocation);
   127   assertMsg(gl.getAttribLocation(program, "vColor") == colorLocation,
   128             "location of vColor should be " + colorLocation);
   130   var ploc = gl.getAttribLocation(program, "vPosition");
   131   var cloc = gl.getAttribLocation(program, "vColor");
   132   gl.bindBuffer(gl.ARRAY_BUFFER, positions);
   133   gl.enableVertexAttribArray(positionLocation);
   134   gl.vertexAttribPointer(positionLocation, 3, gl.FLOAT, false, 0, 0);
   135   gl.bindBuffer(gl.ARRAY_BUFFER, colors);
   136   gl.enableVertexAttribArray(colorLocation);
   137   gl.vertexAttribPointer(colorLocation, 4, gl.FLOAT, false, 0, 0);
   138 }
   140 function checkDraw(colorLocation, positionLocation, r, g, b, a) {
   141   gl.clearColor(0, 0, 0, 1);
   142   gl.clear(gl.COLOR_BUFFER_BIT | gl.DEPTH_BUFFER_BIT);
   143   gl.drawArrays(gl.TRIANGLES, 0, 3);
   145   var width = 50;
   146   var height = 50;
   147   var buf = new Uint8Array(width * height * 4);
   148   gl.readPixels(0, 0, width, height, gl.RGBA, gl.UNSIGNED_BYTE, buf);
   150   function checkPixel(x, y, r, g, b, a) {
   151     var offset = (y * width + x) * 4;
   152     if (buf[offset + 0] != r ||
   153         buf[offset + 1] != g ||
   154         buf[offset + 2] != b ||
   155         buf[offset + 3] != a) {
   156         fail(x, y, buf, "(" + r + "," + g + "," + b + "," + a + ")");
   157         return false;
   158     }
   159     return true;
   160   }
   162   // Test several locations
   163   // First line should be all black
   164   var success = true;
   165   for (var i = 0; i < 50; ++i)
   166     success = success && checkPixel(i, 0, 0, 0, 0, 255);
   168   // Line 15 should be red for at least 10 rgba pixels starting 20 pixels in
   169   var offset = (15 * 50 + 20) * 4;
   170   for (var i = 0; i < 10; ++i)
   171     success = success && checkPixel(20 + i, 15, r, g, b, a);
   173   // Last line should be all black
   174   for (var i = 0; i < 50; ++i)
   175     success = success && checkPixel(i, 49, 0, 0, 0, 255);
   177   if (success)
   178     pass();
   180   gl.disableVertexAttribArray(positionLocation);
   181   gl.disableVertexAttribArray(colorLocation);
   182 }
   184 setBindLocations(2, 3);
   185 checkDraw(2, 3, 0, 255, 0, 255);
   187 setBindLocations(0, 3);
   188 gl.disableVertexAttribArray(0);
   189 gl.vertexAttrib4f(0, 1, 0, 0, 1);
   190 checkDraw(0, 3, 255, 0, 0, 255);
   192 glErrorShouldBe(gl, gl.NO_ERROR);
   194 debug("");
   195 successfullyParsed = true;
   197 </script>
   198 <script>finishTest();</script>
   200 </body>
   201 </html>

mercurial