Backed out changeset b462e7b742d8 (bug 1908261) for causing multiple reftest failures...
[gecko.git] / dom / canvas / test / webgl-conf / checkout / conformance / programs / program-handling.html
blob54138becf1c79da6e9926c379e894038b6d33aa2
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 <!DOCTYPE html>
7 <html>
8 <head>
9 <meta charset="utf-8">
10 <title>WebGL Program Handling Conformance Test</title>
11 <link rel="stylesheet" href="../../resources/js-test-style.css"/>
12 <script src="../../js/js-test-pre.js"></script>
13 <script src="../../js/desktop-gl-constants.js"></script>
14 <script src="../../js/webgl-test-utils.js"></script>
15 </head>
16 <body>
17 <script id="vshader" type="x-shader/x-vertex">
18 attribute vec4 a_position;
19 uniform vec4 u_color;
20 varying vec4 v_color;
21 void main()
23 v_color = u_color;
24 gl_Position = a_position;
26 </script>
27 <script id="fshader" type="x-shader/x-fragment">
28 precision mediump float;
29 varying vec4 v_color;
30 void main()
32 gl_FragColor = v_color;
34 </script>
35 <script id="fshader-not-link" type="x-shader/x-fragment">
36 precision mediump float;
37 varying vec4 foo;
38 void main()
40 gl_FragColor = foo;
42 </script>
44 <canvas id="canvas" width="16" height="16"> </canvas>
45 <div id="description"></div>
46 <div id="console"></div>
48 <script>
49 "use strict";
51 function compile(gl, shader, src) {
52 var shaderSource = document.getElementById(src).text;
53 gl.shaderSource(shader, shaderSource);
54 gl.compileShader(shader);
57 debug("");
58 debug("Canvas.getContext");
60 var wtu = WebGLTestUtils;
61 var gl = wtu.create3DContext("canvas");
62 if (!gl) {
63 testFailed("context does not exist");
66 function testProgramInvalidation() {
67 debug('');
68 debug('== Testing invalidation of the current program ==');
69 var vs = wtu.loadShaderFromScript(gl, "vshader");
70 var fs = wtu.loadShaderFromScript(gl, "fshader");
71 var prg = wtu.createProgram(gl, vs, fs);
72 gl.useProgram(prg);
73 const positionLoc = gl.getAttribLocation(prg, 'a_position');
74 const colorLoc = gl.getUniformLocation(prg, 'u_color');
76 wtu.setupUnitQuad(gl, positionLoc);
78 debug("Draw red with valid program");
79 gl.uniform4fv(colorLoc, [1, 0, 0, 1]);
80 wtu.drawUnitQuad(gl);
81 wtu.checkCanvas(gl, [255, 0, 0, 255], "should be red");
83 debug("Change fragment shader to one that will not link");
84 compile(gl, fs, "fshader-not-link");
85 debug("Draw orange");
86 gl.uniform4fv(colorLoc, [1, 127/255, 0, 1]);
87 wtu.drawUnitQuad(gl);
88 wtu.checkCanvas(gl, [255, 127, 0, 255], "should be orange");
89 wtu.glErrorShouldBe(gl, gl.NO_ERROR, "should be no errors before relink");
91 debug("Try linking");
92 gl.linkProgram(prg);
93 assertMsg(gl.getProgramParameter(prg, gl.LINK_STATUS) == false, "link should fail");
94 wtu.glErrorShouldBe(gl, gl.NO_ERROR, "should be no errors after linkProgram");
95 debug("Attempt to draw green; because link failed, in WebGL, the draw should generate INVALID_OPERATION");
96 gl.uniform4fv(colorLoc, [0, 1, 0, 1]);
97 wtu.glErrorShouldBe(gl, gl.INVALID_OPERATION, "because the current program has been invalidated, uniform* calls generate INVALID_OPERATION");
98 wtu.glErrorShouldBe(gl, gl.NO_ERROR, "should be no errors before draw");
99 wtu.drawUnitQuad(gl);
100 wtu.glErrorShouldBe(gl, gl.INVALID_OPERATION, "draw with invalidated program should fail");
101 wtu.checkCanvas(gl, [255, 127, 0, 255], "should still be orange");
104 function testProgramShaderRemoval() {
105 debug('');
106 debug('== Testing removal of shaders from the current program ==');
107 var vs = wtu.loadShaderFromScript(gl, "vshader");
108 var fs = wtu.loadShaderFromScript(gl, "fshader");
109 var prg = wtu.createProgram(gl, vs, fs);
110 gl.useProgram(prg);
111 const positionLoc = gl.getAttribLocation(prg, 'a_position');
112 const colorLoc = gl.getUniformLocation(prg, 'u_color');
114 wtu.setupUnitQuad(gl, positionLoc);
116 debug("Draw red with valid program");
117 gl.uniform4fv(colorLoc, [1, 0, 0, 1]);
118 wtu.drawUnitQuad(gl);
119 wtu.checkCanvas(gl, [255, 0, 0, 255], "should be red");
121 debug("Detach and delete shaders");
122 gl.detachShader(prg, vs);
123 gl.detachShader(prg, fs);
124 gl.deleteShader(vs);
125 gl.deleteShader(fs);
127 debug("Draw blue to show even though shaders are gone program is still valid");
128 gl.uniform4fv(colorLoc, [0, 0, 1, 1]);
129 wtu.drawUnitQuad(gl);
130 wtu.checkCanvas(gl, [0, 0, 255, 255], "should be blue");
131 wtu.glErrorShouldBe(gl, gl.NO_ERROR, "should be no errors");
134 testProgramInvalidation();
135 testProgramShaderRemoval();
137 var successfullyParsed = true;
138 </script>
140 <script src="../../js/js-test-post.js"></script>
141 </body>
142 </html>