summaryrefslogtreecommitdiffstats
path: root/dom/canvas/test/webgl-mochitest/webgl-util.js
blob: ca4837860a68bcfab2980ecdef669682ac07ab3a (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
WebGLUtil = (function () {
  // ---------------------------------------------------------------------------
  // WebGL helpers

  function withWebGL2(canvasId, callback, onFinished) {
    var run = function () {
      var canvas = document.getElementById(canvasId);

      var gl = null;
      try {
        gl = canvas.getContext("webgl2");
      } catch (e) {}

      if (!gl) {
        todo(false, "WebGL2 is not supported");
        onFinished();
        return;
      }

      callback(gl);
      onFinished();
    };

    try {
      var prefArrArr = [
        ["webgl.force-enabled", true],
        ["webgl.enable-webgl2", true],
      ];
      var prefEnv = { set: prefArrArr };
      SpecialPowers.pushPrefEnv(prefEnv, run);
    } catch (e) {
      warning("No SpecialPowers, but trying WebGL2 anyway...");
      run();
    }
  }

  // Returns a valid shader, or null on errors.
  function createShaderById(gl, id) {
    var elem = document.getElementById(id);
    if (!elem) {
      throw new Error(
        "Failed to create shader from non-existent id '" + id + "'."
      );
    }

    var src = elem.innerHTML.trim();

    var shader;
    if (elem.type == "x-shader/x-fragment") {
      shader = gl.createShader(gl.FRAGMENT_SHADER);
    } else if (elem.type == "x-shader/x-vertex") {
      shader = gl.createShader(gl.VERTEX_SHADER);
    } else {
      throw new Error(
        "Bad MIME type for shader '" + id + "': " + elem.type + "."
      );
    }

    gl.shaderSource(shader, src);
    gl.compileShader(shader);

    return shader;
  }

  function createProgramByIds(gl, vsId, fsId) {
    var vs = createShaderById(gl, vsId);
    var fs = createShaderById(gl, fsId);
    if (!vs || !fs) {
      return null;
    }

    var prog = gl.createProgram();
    gl.attachShader(prog, vs);
    gl.attachShader(prog, fs);
    gl.linkProgram(prog);

    if (!gl.getProgramParameter(prog, gl.LINK_STATUS)) {
      var str = "Shader program linking failed:";
      str += "\nShader program info log:\n" + gl.getProgramInfoLog(prog);
      str += "\n\nVert shader log:\n" + gl.getShaderInfoLog(vs);
      str += "\n\nFrag shader log:\n" + gl.getShaderInfoLog(fs);
      console.error(str);
      return null;
    }

    return prog;
  }

  return {
    withWebGL2,

    createShaderById,
    createProgramByIds,

    linkProgramByIds(gl, vertSrcElem, fragSrcElem) {
      const prog = gl.createProgram();

      function attachShaderById(type, srcElem) {
        const shader = gl.createShader(type);
        gl.shaderSource(shader, srcElem.innerHTML.trim() + "\n");
        gl.compileShader(shader);
        gl.attachShader(prog, shader);
        prog[type] = shader;
      }
      attachShaderById(gl.VERTEX_SHADER, vertSrcElem);
      attachShaderById(gl.FRAGMENT_SHADER, fragSrcElem);

      gl.linkProgram(prog);
      const success = gl.getProgramParameter(prog, gl.LINK_STATUS);
      if (!success) {
        console.error("Error linking program:");
        console.error("\nLink log: " + gl.getProgramInfoLog(prog));
        console.error(
          "\nVert shader log: " + gl.getShaderInfoLog(prog[gl.VERTEX_SHADER])
        );
        console.error(
          "\nFrag shader log: " + gl.getShaderInfoLog(prog[gl.FRAGMENT_SHADER])
        );
        return null;
      }
      gl.deleteShader(prog[gl.VERTEX_SHADER]);
      gl.deleteShader(prog[gl.FRAGMENT_SHADER]);

      let count = gl.getProgramParameter(prog, gl.ACTIVE_ATTRIBUTES);
      for (let i = 0; i < count; i++) {
        const info = gl.getActiveAttrib(prog, i);
        prog[info.name] = gl.getAttribLocation(prog, info.name);
      }
      count = gl.getProgramParameter(prog, gl.ACTIVE_UNIFORMS);
      for (let i = 0; i < count; i++) {
        const info = gl.getActiveUniform(prog, i);
        prog[info.name] = gl.getUniformLocation(prog, info.name);
      }
      return prog;
    },
  };
})();