content/canvas/test/webgl-conformance/conformance/textures/texture-active-bind-2.html

Thu, 22 Jan 2015 13:21:57 +0100

author
Michael Schloh von Bennewitz <michael@schloh.com>
date
Thu, 22 Jan 2015 13:21:57 +0100
branch
TOR_BUG_9701
changeset 15
b8a032363ba2
permissions
-rw-r--r--

Incorporate requested changes from Mozilla in review:
https://bugzilla.mozilla.org/show_bug.cgi?id=1123480#c6

     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 ActiveTexture BindTexture conformance test #2</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 <canvas id="example" width="2" height="2" style="width: 40px; height: 40px;"></canvas>
    17 <canvas id="canvas2d" width="1" height="1" style="width: 40px; height: 40px;"></canvas>
    18 <div id="description"></div>
    19 <div id="console"></div>
    20 <script id="vshader" type="x-shader/x-vertex">
    21 uniform mat4 world;
    22 attribute vec3 vPosition;
    23 attribute vec2 texCoord0;
    24 varying vec2 texCoord;
    25 void main()
    26 {
    27   gl_Position = world * vec4(vPosition, 1);
    28   texCoord = texCoord0;
    29 }
    30 </script>
    31 <script id="fshader2d" type="x-shader/x-fragment">
    32 precision mediump float;
    34 uniform sampler2D tex2d;
    35 varying vec2 texCoord;
    36 void main()
    37 {
    38   gl_FragColor = texture2D(tex2d, texCoord);
    39 }
    40 </script>
    41 <script id="fshaderCube" type="x-shader/x-fragment">
    42 precision mediump float;
    44 uniform samplerCube texCube;
    45 void main()
    46 {
    47   gl_FragColor = textureCube(texCube, vec3(0,1,0));
    48 }
    49 </script>
    51 <script>
    52 function init()
    53 {
    54   if (window.initNonKhronosFramework) {
    55     window.initNonKhronosFramework(false);
    56   }
    58   description(
    59       "Tests that binding both TEXTURE_2D and TEXTURE_CUBE_MAP to the same" +
    60       "active texture unit works as long as they are not used" +
    61       "simultaneously in the same shader program.");
    63   var canvas2d = document.getElementById("canvas2d");
    64   var ctx2d = canvas2d.getContext("2d");
    65   ctx2d.globalCompositeOperation = "copy";
    67   gl = initWebGL("example", "vshader", "fshader2d", ["vPosition", "texCoord0"],
    68                  [ 0, 0, 0, 1 ], 1);
    70   var program2d = gl.program;
    71   var programCube = createProgram(
    72       gl, "vshader", "fshaderCube", ["vPosition", "texCoord0"]);
    74   gl.disable(gl.DEPTH_TEST);
    75   gl.disable(gl.BLEND);
    77   var vertexObject = gl.createBuffer();
    78   gl.bindBuffer(gl.ARRAY_BUFFER, vertexObject);
    79   gl.bufferData(
    80       gl.ARRAY_BUFFER,
    81       new Float32Array([-1, 1,0, 1,1,0, -1,-1,0,
    82                         -1,-1,0, 1,1,0,  1,-1,0]),
    83       gl.STATIC_DRAW);
    84   gl.enableVertexAttribArray(0);
    85   gl.vertexAttribPointer(0, 3, gl.FLOAT, false, 0, 0);
    87   var vertexObject = gl.createBuffer();
    88   gl.bindBuffer(gl.ARRAY_BUFFER, vertexObject);
    89   gl.bufferData(
    90       gl.ARRAY_BUFFER,
    91       new Float32Array([ 0,0, 1,0, 0,1,
    92                          0,1, 1,0, 1,1]),
    93       gl.STATIC_DRAW);
    94   gl.enableVertexAttribArray(1);
    95   gl.vertexAttribPointer(1, 2, gl.FLOAT, false, 0, 0);
    97   // Make texture unit 1 active.
    98   gl.activeTexture(gl.TEXTURE1);
   100   // Make a 2d texture
   101   var tex2d = gl.createTexture();
   102   gl.bindTexture(gl.TEXTURE_2D, tex2d);
   103   ctx2d.fillStyle = "rgba(0, 0, 255, 255)";
   104   ctx2d.fillRect(0, 0, 1, 1);
   105   gl.texImage2D(gl.TEXTURE_2D, 0, gl.RGBA, gl.RGBA, gl.UNSIGNED_BYTE, canvas2d);
   107   // make a cube texture
   108   var texCube = gl.createTexture();
   109   gl.bindTexture(gl.TEXTURE_CUBE_MAP, texCube);
   110   ctx2d.fillStyle = "rgba(255, 0, 255, 255)";
   111   ctx2d.fillRect(0, 0, 1, 1);
   112   var targets = [
   113     gl.TEXTURE_CUBE_MAP_POSITIVE_X,
   114     gl.TEXTURE_CUBE_MAP_NEGATIVE_X,
   115     gl.TEXTURE_CUBE_MAP_POSITIVE_Y,
   116     gl.TEXTURE_CUBE_MAP_NEGATIVE_Y,
   117     gl.TEXTURE_CUBE_MAP_POSITIVE_Z,
   118     gl.TEXTURE_CUBE_MAP_NEGATIVE_Z];
   119   for (var ii = 0; ii < targets.length; ++ii) {
   120     gl.texImage2D(targets[ii], 0, gl.RGBA, gl.RGBA, gl.UNSIGNED_BYTE, canvas2d);
   121   }
   123   // Setup program2d and programCube
   124   var tex2dLoc = gl.getUniformLocation(program2d, "tex2d");
   125   var world2dLoc = gl.getUniformLocation(program2d, "world");
   126   var texCubeLoc = gl.getUniformLocation(programCube, "texCube");
   127   var worldCubeLoc = gl.getUniformLocation(programCube, "world");
   129   gl.useProgram(program2d);
   130   gl.uniform1i(tex2dLoc, 1);
   131   gl.useProgram(programCube);
   132   gl.uniform1i(texCubeLoc, 1);
   134   gl.clearColor(1,0,0,1);
   135   gl.clear(gl.COLOR_BUFFER_BIT | gl.DEPTH_BUFFER_BIT);
   137   var programs = [program2d, programCube];
   138   var worldLocs = [world2dLoc, worldCubeLoc];
   139   for (var ii = 0; ii < 4; ++ii) {
   140     var x = ii % 2;
   141     var y = Math.floor(ii / 2);
   142     gl.useProgram(programs[x]);
   143     gl.uniformMatrix4fv(
   144         worldLocs[x], false,
   145         [0.5, 0, 0, 0,
   146          0, 0.5, 0, 0,
   147          0, 0, 1, 0,
   148          -0.5 + x, -0.5 + y, 0, 1]);
   149     gl.drawArrays(gl.TRIANGLES, 0, 6);
   150   }
   152   var colors = [
   153       [0,0,255,255],
   154       [255,0,255,255],
   155       [0,0,255,255],
   156       [255,0,255,255]];
   158   for (var ii = 0; ii < colors.length; ++ii) {
   159     var c = colors[ii];
   160     var x = ii % 2;
   161     var y = Math.floor(ii / 2);
   162     var buf = new Uint8Array(4);
   163     gl.readPixels(x, y, 1, 1, gl.RGBA, gl.UNSIGNED_BYTE, buf);
   164     var msg = 'expected:' +
   165         c[0] + ', ' + c[1] + ', ' + c[2] + ', ' + c[3] + ' found: ' +
   166         buf[0] + ', ' +
   167         buf[1] + ', ' +
   168         buf[2] + ', ' +
   169         buf[3];
   170     if (buf[0] != c[0] ||
   171         buf[1] != c[1] ||
   172         buf[2] != c[2] ||
   173         buf[3] != c[3]) {
   174       testFailed(msg);
   175       return;
   176     }
   178     testPassed(msg);
   179   }
   180 }
   182 init();
   183 successfullyParsed = true;
   184 </script>
   185 <script>finishTest();</script>
   187 </body>
   188 </html>

mercurial