summaryrefslogtreecommitdiffstats
path: root/devtools/client/shadereditor/test/doc_shader-order.html
blob: a7cec53aab00d18532d42feff7cd074ac62ac6c3 (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
<!-- Any copyright is dedicated to the Public Domain.
     http://creativecommons.org/publicdomain/zero/1.0/ -->
<!doctype html>

<html>
  <head>
    <meta charset="utf-8"/>
    <title>WebGL editor test page</title>

    <script id="shader-vs" type="x-shader/x-vertex">
      precision lowp float;

      void main(void) {
        gl_Position = vec4(0, 0, 0, 1); // I'm a vertex shader!
      }
    </script>

    <script id="shader-fs" type="x-shader/x-fragment">
      precision lowp float;
      varying vec3 vFragmentColor;

      void main(void) {
        gl_FragColor = vec4(1, 0, 0, 1); // I'm a fragment shader!
      }
    </script>
  </head>

  <body>
    <canvas width="512" height="512"></canvas>

    <script type="text/javascript;version=1.8">
      "use strict";

      let canvas, gl;

      window.onload = function() {
        canvas = document.querySelector("canvas");
        gl = canvas.getContext("webgl", { preserveDrawingBuffer: true });

        let shaderProgram = gl.createProgram();
        let vertexShader, fragmentShader;

        // Compile and attach the shaders in a random order. The test will
        // ensure that the correct vertex and fragment source is retrieved
        // regardless of this crazyness.
        if (Math.random() > 0.5) {
          vertexShader = getShader(gl, "shader-vs");
          fragmentShader = getShader(gl, "shader-fs");
        } else {
          fragmentShader = getShader(gl, "shader-fs");
          vertexShader = getShader(gl, "shader-vs");
        }
        if (Math.random() > 0.5) {
          gl.attachShader(shaderProgram, vertexShader);
          gl.attachShader(shaderProgram, fragmentShader);
        } else {
          gl.attachShader(shaderProgram, fragmentShader);
          gl.attachShader(shaderProgram, vertexShader);
        }

        gl.linkProgram(shaderProgram);
      }

      function getShader(gl, id) {
        let script = document.getElementById(id);
        let source = script.textContent;
        let shader;

        if (script.type == "x-shader/x-fragment") {
          shader = gl.createShader(gl.FRAGMENT_SHADER);
        } else if (script.type == "x-shader/x-vertex") {
          shader = gl.createShader(gl.VERTEX_SHADER);
        }

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

        return shader;
      }
    </script>
  </body>

</html>