tor-browser

The Tor Browser
git clone https://git.dasho.dev/tor-browser.git
Log | Files | Refs | README | LICENSE

glsl-function-nodes.html (3723B)


      1 <!--
      2 Copyright (c) 2019 The Khronos Group Inc.
      3 Use of this source code is governed by an MIT-style license that can be
      4 found in the LICENSE.txt file.
      5 -->
      6 
      7 <!DOCTYPE html>
      8 <html>
      9 <head>
     10 <meta charset="utf-8">
     11 <title>GLSL function nodes Test</title>
     12 <link rel="stylesheet" href="../../../resources/js-test-style.css"/>
     13 <link rel="stylesheet" href="../../../resources/glsl-feature-tests.css"/>
     14 <script src="../../../js/js-test-pre.js"></script>
     15 <script src="../../../js/webgl-test-utils.js"> </script>
     16 
     17 <script id="vshaderFunction" type="x-shader/x-vertex">
     18 attribute vec4 aPosition;
     19 varying vec4 vColor;
     20 
     21 float sign_emu(float value) {
     22  if (value == 0.0) return 0.0;
     23  return value > 0.0 ? 1.0 : -1.0;
     24 }
     25 
     26 void main()
     27 {
     28   gl_Position = aPosition;
     29   vec2 texcoord = vec2(aPosition.xy * 0.5 + vec2(0.5, 0.5));
     30   vec4 color = vec4(
     31       texcoord,
     32       texcoord.x * texcoord.y,
     33       (1.0 - texcoord.x) * texcoord.y * 0.5 + 0.5);
     34   vColor = vec4(
     35    sign_emu(color.x * 2.0 - 1.0) * 0.5 + 0.5,
     36    sign_emu(color.y * 2.0 - 1.0) * 0.5 + 0.5,
     37    0,
     38    1);
     39 }
     40 </script>
     41 
     42 <script id="vshaderMacro" type="x-shader/x-vertex">
     43 attribute vec4 aPosition;
     44 varying vec4 vColor;
     45 
     46 #define sign_emu(value) ((value) == 0.0 ? 0.0 : ((value) > 0.0 ? 1.0 : -1.0))
     47 
     48 void main()
     49 {
     50   gl_Position = aPosition;
     51   vec2 texcoord = vec2(aPosition.xy * 0.5 + vec2(0.5, 0.5));
     52   vec4 color = vec4(
     53       texcoord,
     54       texcoord.x * texcoord.y,
     55       (1.0 - texcoord.x) * texcoord.y * 0.5 + 0.5);
     56   vColor = vec4(
     57    sign_emu(color.x * 2.0 - 1.0) * 0.5 + 0.5,
     58    sign_emu(color.y * 2.0 - 1.0) * 0.5 + 0.5,
     59    0,
     60    1);
     61 }
     62 </script>
     63 
     64 <script id="fshader" type="x-shader/x-fragment">
     65 precision mediump float;
     66 varying vec4 vColor;
     67 void main()
     68 {
     69   gl_FragColor = vColor;
     70 }
     71 </script>
     72 </head>
     73 <body>
     74 <canvas id="canvasFunction" width="50" height="50"></canvas>
     75 <canvas id="canvasMacro" width="50" height="50"></canvas>
     76 <div id="description">This tests against a Mac driver bug related to function calls.</div>
     77 <div id="console"></div>
     78 <script>
     79 "use strict";
     80 var width = 50;
     81 var height = 50;
     82 var wtu = WebGLTestUtils;
     83 
     84 function drawAndRead(canvasID, vshaderID, buffer)
     85 {
     86    var gl = wtu.create3DContext(canvasID);
     87    var program = wtu.setupProgram(gl, [vshaderID, "fshader"], ["aPosition"]);
     88    var vertexObject = gl.createBuffer();
     89    gl.bindBuffer(gl.ARRAY_BUFFER, vertexObject);
     90    gl.bufferData(gl.ARRAY_BUFFER, new Float32Array([ 0,0.5,0, -0.5,-0.5,0, 0.5,-0.5,0 ]), gl.STATIC_DRAW);
     91    gl.enableVertexAttribArray(0);
     92    gl.vertexAttribPointer(0, 3, gl.FLOAT, false, 0, 0);
     93 
     94    gl.clear(gl.COLOR_BUFFER_BIT | gl.DEPTH_BUFFER_BIT);
     95    gl.drawArrays(gl.TRIANGLES, 0, 3);
     96    gl.readPixels(0, 0, width, height, gl.RGBA, gl.UNSIGNED_BYTE, buffer);
     97    if (gl.getError() != gl.NO_ERROR)
     98        return false;
     99    return true;
    100 }
    101 
    102 function compareRendering(buffer1, buffer2, tol)
    103 {
    104    for (var i = 0; i < width * height * 4; ++i) {
    105        if (Math.abs(buffer1[i] - buffer2[i]) > tol)
    106            return false;
    107    }
    108    return true;
    109 }
    110 
    111 function init()
    112 {
    113    description("tests function nodes");
    114 
    115    var bufFunction = new Uint8Array(width * height * 4);
    116    var bufMacro = new Uint8Array(width * height * 4);
    117 
    118    if (drawAndRead("canvasFunction", "vshaderFunction", bufFunction) == false ||
    119        drawAndRead("canvasMacro", "vshaderMacro", bufMacro) == false) {
    120        testFailed("Setup failed");
    121    } else {
    122        if (compareRendering(bufFunction, bufMacro, 4) == false)
    123            testFailed("Rendering results are different");
    124        else
    125            testPassed("Rendering results are the same");
    126    }
    127 }
    128 
    129 init();
    130 var successfullyParsed = true;
    131 </script>
    132 <script src="../../../js/js-test-post.js"></script>
    133 </body>
    134 </html>