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.
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>
17 <script id=
"vshader" type=
"x-shader/x-vertex">
18 attribute vec4 a_position;
24 gl_Position = a_position;
27 <script id=
"fshader" type=
"x-shader/x-fragment">
28 precision mediump float;
32 gl_FragColor = v_color;
35 <script id=
"fshader-not-link" type=
"x-shader/x-fragment">
36 precision mediump float;
44 <canvas id=
"canvas" width=
"16" height=
"16"> </canvas>
45 <div id=
"description"></div>
46 <div id=
"console"></div>
51 function compile(gl
, shader
, src
) {
52 var shaderSource
= document
.getElementById(src
).text
;
53 gl
.shaderSource(shader
, shaderSource
);
54 gl
.compileShader(shader
);
58 debug("Canvas.getContext");
60 var wtu
= WebGLTestUtils
;
61 var gl
= wtu
.create3DContext("canvas");
63 testFailed("context does not exist");
66 function testProgramInvalidation() {
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
);
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]);
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");
86 gl
.uniform4fv(colorLoc
, [1, 127/255, 0, 1]);
88 wtu
.checkCanvas(gl
, [255, 127, 0, 255], "should be orange");
89 wtu
.glErrorShouldBe(gl
, gl
.NO_ERROR
, "should be no errors before relink");
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");
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() {
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
);
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
);
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;
140 <script src=
"../../js/js-test-post.js"></script>