tor-browser

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

complex-glsl-does-not-crash.html (7436B)


      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>Driver Bug - complex glsl should not crash</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 <script src="../../../js/glsl-conformance-test.js"></script>
     17 </head>
     18 <body>
     19 <div id="description"></div>
     20 <div id="console"></div>
     21 <canvas id="example" width="2" height="2"> </canvas>
     22 <script id="vshader" type="x-shader/x-vertex">
     23 attribute vec4 a_position;
     24 void main()
     25 {
     26    gl_Position = a_position;
     27 }
     28 </script>
     29 <script id="fshader" type="x-shader/x-vertex">
     30 precision mediump float;
     31 varying vec4 v_varying;
     32 void main()
     33 {
     34    gl_FragColor = v_varying;
     35 }
     36 </script>
     37 <script id="vshaderArrayTest" type="x-shader/x-vertex">
     38 attribute vec4 a_position;
     39 varying vec4 v_varying;
     40 uniform $(type) u_uniform[$(numTestType)];
     41 void main()
     42 {
     43    v_varying = $(code);
     44    gl_Position = a_position;
     45 }
     46 </script>
     47 <script id="fshaderArrayTest" type="x-shader/x-fragment">
     48 precision mediump float;
     49 uniform $(type) u_uniform[$(numTestType)];
     50 void main()
     51 {
     52    gl_FragColor = $(code);
     53 }
     54 </script>
     55 <script id="vshaderUniformTest" type="x-shader/x-fragment">
     56 attribute vec4 a_position;
     57 varying vec4 v_varying;
     58 $(uniforms)
     59 void main()
     60 {
     61    v_varying = $(code);
     62    gl_Position = a_position;
     63 }
     64 </script>
     65 <script id="fshaderUniformTest" type="x-shader/x-fragment">
     66 precision mediump float;
     67 $(uniforms)
     68 void main()
     69 {
     70    gl_FragColor = $(code);
     71 }
     72 </script>
     73 <script>
     74 "use strict";
     75 description();
     76 debug("");
     77 var wtu = WebGLTestUtils;
     78 var gl = wtu.create3DContext("example");
     79 
     80 var uniformTypes = [
     81  { type: "bool",        componentsPerRow: 1, rows: 1, code: "vec4(u_uniform$(id)$(index), 0, 0, 0)", },
     82  { type: "float",       componentsPerRow: 1, rows: 1, code: "vec4(u_uniform$(id)$(index), 0, 0, 0)", },
     83  { type: "int",         componentsPerRow: 1, rows: 1, code: "vec4(u_uniform$(id)$(index), 0, 0, 0)", },
     84  { type: "vec2",        componentsPerRow: 2, rows: 1, code: "vec4(u_uniform$(id)$(index), 0, 0)", },
     85  { type: "ivec2",       componentsPerRow: 2, rows: 1, code: "vec4(u_uniform$(id)$(index), 0, 0)", },
     86  { type: "bvec2",       componentsPerRow: 2, rows: 1, code: "vec4(u_uniform$(id)$(index), 0, 0)", },
     87  { type: "vec3",        componentsPerRow: 3, rows: 1, code: "vec4(u_uniform$(id)$(index), 0)", },
     88  { type: "ivec3",       componentsPerRow: 3, rows: 1, code: "vec4(u_uniform$(id)$(index), 0)", },
     89  { type: "bvec3",       componentsPerRow: 3, rows: 1, code: "vec4(u_uniform$(id)$(index), 0)", },
     90  { type: "vec4",        componentsPerRow: 4, rows: 1, code: "vec4(u_uniform$(id)$(index))", },
     91  { type: "ivec4",       componentsPerRow: 4, rows: 1, code: "vec4(u_uniform$(id)$(index))", },
     92  { type: "bvec4",       componentsPerRow: 4, rows: 1, code: "vec4(u_uniform$(id)$(index))", },
     93 // Yes, the spec says mat2 takes 4 columns, 2 rows.
     94  { type: "mat2",        componentsPerRow: 4, rows: 2, code: "vec4(u_uniform$(id)$(index)[0], 0, 0)", },
     95  { type: "mat3",        componentsPerRow: 3, rows: 3, code: "vec4(u_uniform$(id)$(index)[0], 0)", },
     96  { type: "mat4",        componentsPerRow: 4, rows: 4, code: "vec4(u_uniform$(id)$(index)[0])", },
     97 // Samplers generally have more restictive limits.
     98 //  { type: "sampler2D",   componentsPerRow: 1, rows: 1, code: "vec4(texture2D(u_uniform[$(index)], vec2(0, 0)))", },
     99 //  { type: "samplerCube", componentsPerRow: 1, rows: 1, code: "vec4(textureCube(u_uniform[$(index)], vec3(0, 0, 0)))", },
    100 ];
    101 
    102 var vBaseSource = wtu.getScript("vshader");
    103 var fBaseSource = wtu.getScript("fshader");
    104 var vArrayTestSource = wtu.getScript("vshaderArrayTest");
    105 var fArrayTestSource = wtu.getScript("fshaderArrayTest");
    106 var vUniformTestSource = wtu.getScript("vshaderUniformTest");
    107 var fUniformTestSource = wtu.getScript("fshaderUniformTest");
    108 
    109 var tests = [];
    110 var shaderTypes = [
    111  { type: "vertex",
    112    // For tests that expect failure which shader might fail.
    113    vertExpectation: false,
    114    fragExpectation: true,
    115    vertArrayTest: vArrayTestSource,
    116    fragArrayTest: fBaseSource,
    117    vertUniformTest: vUniformTestSource,
    118    fragUniformTest: fBaseSource,
    119    maxVectors: gl.getParameter(gl.MAX_VERTEX_UNIFORM_VECTORS),
    120    minVectors: 128,  // GLSL ES 1.0.17 Appendix A.7,
    121  },
    122  { type: "fragment",
    123    // For tests that expect failure which shader might fail.
    124    vertExpectation: true,
    125    fragExpectation: false,
    126    vertArrayTest: vBaseSource,
    127    fragArrayTest: fArrayTestSource,
    128    vertUniformTest: vBaseSource,
    129    fragUniformTest: fUniformTestSource,
    130    maxVectors: gl.getParameter(gl.MAX_FRAGMENT_UNIFORM_VECTORS),
    131    minVectors: 16,  // GLSL ES 1.0.17 Appendix A.7,
    132  },
    133 ];
    134 for (var ss = 0; ss < shaderTypes.length; ++ss) {
    135  var shaderType = shaderTypes[ss];
    136  debug("max " + shaderType.type + ": " + shaderType.maxVectors);
    137  for (var ii = 0; ii < uniformTypes.length; ++ii) {
    138    var info = uniformTypes[ii];
    139    wtu.log("checking: " + info.type);
    140    // Compute the maximum amount of this type allowed in a single array.
    141    var numVars = Math.floor(shaderType.maxVectors / info.rows);
    142    // Compute the minimum required to work in a single array.
    143    var minVars = Math.floor(shaderType.minVectors / info.rows);
    144    // Compute the maximum allowed as single elements
    145    var numPerRow = Math.floor(4 / info.componentsPerRow);
    146    var numMax = Math.floor(shaderType.maxVectors * numPerRow / info.rows);
    147 
    148    // Test array[max] of the type
    149    // Note: We can't test for success or failer as actual GL drivers are only required to be able to
    150    // do the minimum number. After that it can fail for any reason.
    151    var code = wtu.replaceParams(info.code, {id: "", index: "[" + (numVars - 1) + "]"});
    152    tests.push({
    153      vShaderSource: wtu.replaceParams(shaderType.vertArrayTest, {numTestType: numVars, code: code}, info),
    154      vShaderSuccess: true,
    155      fShaderSource: wtu.replaceParams(shaderType.fragArrayTest, {numTestType: numVars, code: code}, info),
    156      fShaderSuccess: true,
    157      linkSuccess: true,
    158      ignoreResults: true,
    159      passMsg: shaderType.type + " shader with uniform array of " + info.type + " with " + numVars + " elements (the maximum)",
    160    });
    161 
    162    var generateCode = function(numVars) {
    163      var uniforms = [];
    164      var codes = [];
    165      for (var uu = 0; uu < numVars; ++uu) {
    166        uniforms.push("    uniform " + info.type + " u_uniform" + uu + ";");
    167        codes.push(wtu.replaceParams(info.code, {id: uu, index: ""}));
    168      }
    169      return {
    170        uniforms: uniforms.join("\n"),
    171        code: codes.join(" + \n            "),
    172      };
    173    };
    174 
    175    // Test max uniforms of type.
    176    tests.push({
    177      vShaderSource: wtu.replaceParams(shaderType.vertUniformTest, generateCode(numMax), info),
    178      vShaderSuccess: shaderType.vertExpectation,
    179      fShaderSource: wtu.replaceParams(shaderType.fragUniformTest, generateCode(numMax), info),
    180      fShaderSuccess: shaderType.fragExpectation,
    181      linkSuccess: true,
    182      ignoreResults: true,
    183      passMsg: shaderType.type + " shader with " + (numMax) + " uniforms of " + info.type,
    184    });
    185  }
    186 }
    187 GLSLConformanceTester.runTests(tests);
    188 var successfullyParsed = true;
    189 </script>
    190 </body>
    191 </html>