content/canvas/test/webgl-conformance/conformance/programs/gl-bind-attrib-location-test.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/programs/gl-bind-attrib-location-test.html	Wed Dec 31 06:09:35 2014 +0100
     1.3 @@ -0,0 +1,201 @@
     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 +<title>WebGL BindAttribLocation Conformance Tests</title>
    1.14 +<link rel="stylesheet" href="../../resources/js-test-style.css"/>
    1.15 +<script src="../../resources/js-test-pre.js"></script>
    1.16 +<script src="../resources/webgl-test.js"></script>
    1.17 +</head>
    1.18 +<body>
    1.19 +<div id="description"></div>
    1.20 +<div id="console"></div>
    1.21 +<canvas style="border: 1px solid black;" id="canvas" width="50" height="50"></canvas>
    1.22 +<script id="vshader" type="text/something-not-javascript">
    1.23 +attribute vec4 vPosition;
    1.24 +attribute vec4 vColor;
    1.25 +varying vec4 color;
    1.26 +void main()
    1.27 +{
    1.28 +  gl_Position = vPosition;
    1.29 +  color = vColor;
    1.30 +}
    1.31 +</script>
    1.32 +<script id="fshader" type="text/something-not-javascript">
    1.33 +precision mediump float;
    1.34 +
    1.35 +varying vec4 color;
    1.36 +void main()
    1.37 +{
    1.38 +  gl_FragColor = color;
    1.39 +}
    1.40 +</script>
    1.41 +<script>
    1.42 +description("This test ensures WebGL implementations don't allow names that start with 'gl_' when calling bindAttribLocation.");
    1.43 +
    1.44 +debug("");
    1.45 +debug("Canvas.getContext");
    1.46 +
    1.47 +var gl = create3DContext(document.getElementById("canvas"));
    1.48 +shouldBeNonNull("gl");
    1.49 +
    1.50 +function fail(x,y, buf, shouldBe)
    1.51 +{
    1.52 +  var i = (y*50+x) * 4;
    1.53 +  var reason = "pixel at ("+x+","+y+") is ("+buf[i]+","+buf[i+1]+","+buf[i+2]+","+buf[i+3]+"), should be "+shouldBe;
    1.54 +  testFailed(reason);
    1.55 +}
    1.56 +
    1.57 +function pass()
    1.58 +{
    1.59 +  testPassed("drawing is correct");
    1.60 +}
    1.61 +
    1.62 +function loadShader(shaderType, shaderId) {
    1.63 +  // Get the shader source.
    1.64 +  var shaderSource = document.getElementById(shaderId).text;
    1.65 +
    1.66 +  // Create the shader object
    1.67 +  var shader = gl.createShader(shaderType);
    1.68 +  if (shader == null) {
    1.69 +    debug("*** Error: unable to create shader '"+shaderId+"'");
    1.70 +    return null;
    1.71 +  }
    1.72 +
    1.73 +  // Load the shader source
    1.74 +  gl.shaderSource(shader, shaderSource);
    1.75 +
    1.76 +  // Compile the shader
    1.77 +  gl.compileShader(shader);
    1.78 +
    1.79 +  // Check the compile status
    1.80 +  var compiled = gl.getShaderParameter(shader, gl.COMPILE_STATUS);
    1.81 +  if (!compiled) {
    1.82 +    // Something went wrong during compilation; get the error
    1.83 +    var error = gl.getShaderInfoLog(shader);
    1.84 +    debug("*** Error compiling shader '"+shader+"':"+error);
    1.85 +    gl.deleteShader(shader);
    1.86 +    return null;
    1.87 +  }
    1.88 +  return shader;
    1.89 +}
    1.90 +
    1.91 +debug("");
    1.92 +debug("Checking gl.bindAttribLocation.");
    1.93 +
    1.94 +var program = gl.createProgram();
    1.95 +gl.bindAttribLocation(program, 0, "gl_foo");
    1.96 +glErrorShouldBe(gl, gl.INVALID_OPERATION,
    1.97 +    "bindAttribLocation should return INVALID_OPERATION if name starts with 'gl_'");
    1.98 +gl.bindAttribLocation(program, 0, "gl_TexCoord0");
    1.99 +glErrorShouldBe(gl, gl.INVALID_OPERATION,
   1.100 +    "bindAttribLocation should return INVALID_OPERATION if name starts with 'gl_'");
   1.101 +
   1.102 +var vs = loadShader(gl.VERTEX_SHADER, "vshader");
   1.103 +var fs = loadShader(gl.FRAGMENT_SHADER, "fshader");
   1.104 +gl.attachShader(program, vs);
   1.105 +gl.attachShader(program, fs);
   1.106 +
   1.107 +var positions = gl.createBuffer();
   1.108 +gl.bindBuffer(gl.ARRAY_BUFFER, positions);
   1.109 +gl.bufferData(gl.ARRAY_BUFFER, new Float32Array([ 0,0.5,0, -0.5,-0.5,0, 0.5,-0.5,0 ]), gl.STATIC_DRAW);
   1.110 +
   1.111 +var colors = gl.createBuffer();
   1.112 +gl.bindBuffer(gl.ARRAY_BUFFER, colors);
   1.113 +gl.bufferData(gl.ARRAY_BUFFER, new Float32Array([
   1.114 +    0,1,0,1,
   1.115 +    0,1,0,1,
   1.116 +    0,1,0,1]), gl.STATIC_DRAW);
   1.117 +
   1.118 +function setBindLocations(colorLocation, positionLocation) {
   1.119 +  gl.bindAttribLocation(program, positionLocation, "vPosition");
   1.120 +  gl.bindAttribLocation(program, colorLocation, "vColor");
   1.121 +  gl.linkProgram(program);
   1.122 +  gl.useProgram(program);
   1.123 +  var linked = (gl.getProgramParameter(program, gl.LINK_STATUS) != 0);
   1.124 +  assertMsg(linked, "program linked successfully");
   1.125 +
   1.126 +  debug("vPosition:" + gl.getAttribLocation(program, "vPosition"))
   1.127 +  debug("vColor   :" + gl.getAttribLocation(program, "vColor"))
   1.128 +  assertMsg(gl.getAttribLocation(program, "vPosition") == positionLocation,
   1.129 +            "location of vPosition should be " + positionLocation);
   1.130 +  assertMsg(gl.getAttribLocation(program, "vColor") == colorLocation,
   1.131 +            "location of vColor should be " + colorLocation);
   1.132 +
   1.133 +  var ploc = gl.getAttribLocation(program, "vPosition");
   1.134 +  var cloc = gl.getAttribLocation(program, "vColor");
   1.135 +  gl.bindBuffer(gl.ARRAY_BUFFER, positions);
   1.136 +  gl.enableVertexAttribArray(positionLocation);
   1.137 +  gl.vertexAttribPointer(positionLocation, 3, gl.FLOAT, false, 0, 0);
   1.138 +  gl.bindBuffer(gl.ARRAY_BUFFER, colors);
   1.139 +  gl.enableVertexAttribArray(colorLocation);
   1.140 +  gl.vertexAttribPointer(colorLocation, 4, gl.FLOAT, false, 0, 0);
   1.141 +}
   1.142 +
   1.143 +function checkDraw(colorLocation, positionLocation, r, g, b, a) {
   1.144 +  gl.clearColor(0, 0, 0, 1);
   1.145 +  gl.clear(gl.COLOR_BUFFER_BIT | gl.DEPTH_BUFFER_BIT);
   1.146 +  gl.drawArrays(gl.TRIANGLES, 0, 3);
   1.147 +
   1.148 +  var width = 50;
   1.149 +  var height = 50;
   1.150 +  var buf = new Uint8Array(width * height * 4);
   1.151 +  gl.readPixels(0, 0, width, height, gl.RGBA, gl.UNSIGNED_BYTE, buf);
   1.152 +
   1.153 +  function checkPixel(x, y, r, g, b, a) {
   1.154 +    var offset = (y * width + x) * 4;
   1.155 +    if (buf[offset + 0] != r ||
   1.156 +        buf[offset + 1] != g ||
   1.157 +        buf[offset + 2] != b ||
   1.158 +        buf[offset + 3] != a) {
   1.159 +        fail(x, y, buf, "(" + r + "," + g + "," + b + "," + a + ")");
   1.160 +        return false;
   1.161 +    }
   1.162 +    return true;
   1.163 +  }
   1.164 +
   1.165 +  // Test several locations
   1.166 +  // First line should be all black
   1.167 +  var success = true;
   1.168 +  for (var i = 0; i < 50; ++i)
   1.169 +    success = success && checkPixel(i, 0, 0, 0, 0, 255);
   1.170 +
   1.171 +  // Line 15 should be red for at least 10 rgba pixels starting 20 pixels in
   1.172 +  var offset = (15 * 50 + 20) * 4;
   1.173 +  for (var i = 0; i < 10; ++i)
   1.174 +    success = success && checkPixel(20 + i, 15, r, g, b, a);
   1.175 +
   1.176 +  // Last line should be all black
   1.177 +  for (var i = 0; i < 50; ++i)
   1.178 +    success = success && checkPixel(i, 49, 0, 0, 0, 255);
   1.179 +
   1.180 +  if (success)
   1.181 +    pass();
   1.182 +
   1.183 +  gl.disableVertexAttribArray(positionLocation);
   1.184 +  gl.disableVertexAttribArray(colorLocation);
   1.185 +}
   1.186 +
   1.187 +setBindLocations(2, 3);
   1.188 +checkDraw(2, 3, 0, 255, 0, 255);
   1.189 +
   1.190 +setBindLocations(0, 3);
   1.191 +gl.disableVertexAttribArray(0);
   1.192 +gl.vertexAttrib4f(0, 1, 0, 0, 1);
   1.193 +checkDraw(0, 3, 255, 0, 0, 255);
   1.194 +
   1.195 +glErrorShouldBe(gl, gl.NO_ERROR);
   1.196 +
   1.197 +debug("");
   1.198 +successfullyParsed = true;
   1.199 +
   1.200 +</script>
   1.201 +<script>finishTest();</script>
   1.202 +
   1.203 +</body>
   1.204 +</html>

mercurial