devtools/client/shadereditor/test/doc_multiple-contexts.html
author Mark Banner <standard8@mozilla.com>
Fri, 19 Oct 2018 12:55:39 +0000
changeset 490493 edc4ae8f78e2fb02647d7cd85cb19830a17dcd1d
parent 469167 0add1bf599e5509ba474eaa1adc2fce14342e9b7
permissions -rw-r--r--
Bug 1486741 - Enable ESLint rule comma-dangle for all of mozilla-central (automatic fixes). r=mossop Differential Revision: https://phabricator.services.mozilla.com/D8389

<!-- 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;
      attribute vec3 aVertexPosition;

      void main(void) {
        gl_Position = vec4(aVertexPosition, 1);
      }
    </script>

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

      void main(void) {
        gl_FragColor = vec4(uColor, 1);
      }
    </script>
  </head>

  <body>
    <canvas id="canvas1" width="128" height="128"></canvas>
    <canvas id="canvas2" width="128" height="128"></canvas>

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

      const canvas = [], gl = [];
      const program = [];
      const squareVerticesPositionBuffer = [];
      const vertexPositionAttribute = [];
      const colorUniform = [];

      window.onload = function() {
        for (let i = 0; i < 2; i++) {
          canvas[i] = document.querySelector("#canvas" + (i + 1));
          gl[i] = canvas[i].getContext("webgl", { preserveDrawingBuffer: true });
          gl[i].clearColor(0.0, 0.0, 0.0, 1.0);

          initProgram(i);
          initBuffers(i);
          drawScene(i);
        }
      };

      function initProgram(i) {
        const vertexShader = getShader(gl[i], "shader-vs");
        const fragmentShader = getShader(gl[i], "shader-fs");

        program[i] = gl[i].createProgram();
        gl[i].attachShader(program[i], vertexShader);
        gl[i].attachShader(program[i], fragmentShader);
        gl[i].linkProgram(program[i]);

        vertexPositionAttribute[i] = gl[i].getAttribLocation(program[i], "aVertexPosition");
        gl[i].enableVertexAttribArray(vertexPositionAttribute[i]);

        colorUniform[i] = gl[i].getUniformLocation(program[i], "uColor");
      }

      function getShader(gl, id) {
        const script = document.getElementById(id);
        const 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;
      }

      function initBuffers(i) {
        squareVerticesPositionBuffer[i] = gl[i].createBuffer();
        gl[i].bindBuffer(gl[i].ARRAY_BUFFER, squareVerticesPositionBuffer[i]);
        gl[i].bufferData(gl[i].ARRAY_BUFFER, new Float32Array([
           1.0,  1.0, 0.0,
           -1.0,  1.0, 0.0,
           1.0, -1.0, 0.0,
           -1.0, -1.0, 0.0,
        ]), gl[i].STATIC_DRAW);
      }

      function drawScene(i) {
        gl[i].clear(gl[i].COLOR_BUFFER_BIT);

        gl[i].bindBuffer(gl[i].ARRAY_BUFFER, squareVerticesPositionBuffer[i]);
        gl[i].vertexAttribPointer(vertexPositionAttribute[i], 3, gl[i].FLOAT, false, 0, 0);

        gl[i].useProgram(program[i]);
        gl[i].uniform3fv(colorUniform[i], i == 0 ? [1, 1, 0] : [0, 1, 1]);
        gl[i].drawArrays(gl[i].TRIANGLE_STRIP, 0, 4);

        window.requestAnimationFrame(() => drawScene(i));
      }
    </script>
  </body>

</html>