Bug 1426289 - Add test for vertexAttrib4f updates. a=test-only
authorJeff Gilbert <jgilbert@mozilla.com>
Tue, 19 Dec 2017 22:54:41 -0800
changeset 445415 a9eef658495137795554a5a2e14e867a02517bab
parent 445414 4aaa982ec14eee08d9fb59dca66aab2153d387c5
child 445416 f681d554e292cc3ce7b5f22be7c96e7464094211
push id1618
push userCallek@gmail.com
push dateThu, 11 Jan 2018 17:45:48 +0000
treeherdermozilla-release@882ca853e05a [default view] [failures only]
perfherder[talos] [build metrics] [platform microbench] (compared to previous push)
reviewerstest-only
bugs1426289
milestone58.0
Bug 1426289 - Add test for vertexAttrib4f updates. a=test-only MozReview-Commit-ID: E2BqeuyafUg
dom/canvas/test/webgl-mochitest/mochitest.ini
dom/canvas/test/webgl-mochitest/test_vertexattrib4f_update.html
dom/canvas/test/webgl-mochitest/webgl-util.js
--- a/dom/canvas/test/webgl-mochitest/mochitest.ini
+++ b/dom/canvas/test/webgl-mochitest/mochitest.ini
@@ -103,8 +103,9 @@ skip-if = toolkit == 'android' #bug 8654
 [test_video_fastpath_mp4.html]
 [test_video_fastpath_theora.html]
 [test_video_fastpath_vp8.html]
 [test_video_fastpath_vp9.html]
 [test_webglcontextcreationerror.html]
 [test_webgl_fingerprinting_resistance.html]
 fail-if = (os == 'mac') # on try server, LOCAL_GL_MAX_CUBE_MAP_TEXTURE_SIZE = 512 on mac
 [test_without_index_validation.html]
+[test_vertexattrib4f_update.html]
new file mode 100644
--- /dev/null
+++ b/dom/canvas/test/webgl-mochitest/test_vertexattrib4f_update.html
@@ -0,0 +1,62 @@
+<!DOCTYPE html>
+<html>
+  <head>
+    <meta charset='utf-8'>
+    <title>Bug 1426289 - vertexAttrib4f should actually update.</title>
+    <script src='/tests/SimpleTest/SimpleTest.js'></script>
+    <link rel='stylesheet' href='/tests/SimpleTest/test.css'>
+    <script src='webgl-util.js'></script>
+  </head>
+  <body>
+    <script id='eVertSource' type='none'>
+attribute vec4 aColor;
+varying vec4 vColor;
+
+void main() {
+  gl_PointSize = 64.0;
+  gl_Position = vec4(vec3(0.0), 1.0);
+  vColor = aColor;
+}
+    </script>
+    <script id='eFragSource' type='none'>
+precision mediump float;
+varying vec4 vColor;
+
+void main() {
+  gl_FragColor = vColor;
+}
+    </script>
+    <script>
+const canvas = document.createElement('canvas');
+canvas.width = 1;
+canvas.height = 1;
+const gl = canvas.getContext('webgl');
+
+const prog = WebGLUtil.linkProgramByIds(gl, eVertSource, eFragSource);
+gl.useProgram(prog);
+
+function getRgb() {
+  const data = new Uint32Array(1);
+  gl.readPixels(0, 0, 1, 1, gl.RGBA, gl.UNSIGNED_BYTE, new Uint8Array(data.buffer));
+  return data[0] & 0xffffff;
+}
+
+gl.clearColor(0.0, 0.0, 0.0, 1.0);
+gl.clear(gl.COLOR_BUFFER_BIT);
+let was = getRgb();
+ok(was == 0x000000, '0x'+was.toString(16));
+
+gl.disableVertexAttribArray(prog.aColor);
+
+gl.vertexAttrib4f(prog.aColor, 1, 0, 0, 1);
+gl.drawArrays(gl.POINTS, 0, 1);
+was = getRgb();
+ok(was == 0x0000ff, '0x'+was.toString(16));
+
+gl.vertexAttrib4f(prog.aColor, 0, 1, 0, 1);
+gl.drawArrays(gl.POINTS, 0, 1);
+was = getRgb();
+ok(was == 0x00ff00, '0x'+was.toString(16));
+    </script>
+  </body>
+</html>
--- a/dom/canvas/test/webgl-mochitest/webgl-util.js
+++ b/dom/canvas/test/webgl-mochitest/webgl-util.js
@@ -161,10 +161,49 @@ WebGLUtil = (function() {
   return {
     setErrorFunc: setErrorFunc,
     setWarningFunc: setWarningFunc,
 
     getWebGL: getWebGL,
     withWebGL2: withWebGL2,
     createShaderById: createShaderById,
     createProgramByIds: createProgramByIds,
+
+
+    linkProgramByIds: function(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;
+    },
   };
 })();