OLD | NEW |
| (Empty) |
1 <!DOCTYPE html> | |
2 <html> | |
3 <head> | |
4 <meta charset="utf-8"> | |
5 <title>WebGL Program Compiling/Linking Conformance Test</title> | |
6 <script src="../../../resources/js-test.js" type="text/javascript"></script> | |
7 <script src="resources/webgl-test.js" type="text/javascript"></script> | |
8 </head> | |
9 <body> | |
10 <div id="description"></div> | |
11 <div id="console"></div> | |
12 <canvas id="canvas" width="2" height="2"> </canvas> | |
13 <script type="text/javascript"> | |
14 function go() { | |
15 description("Tests that program compiling/linking/using works correctly."); | |
16 | |
17 debug(""); | |
18 debug("Canvas.getContext"); | |
19 | |
20 if (window.internals) | |
21 window.internals.settings.setWebGLErrorsToConsoleEnabled(false); | |
22 | |
23 var gl = create3DContext(document.getElementById("canvas")); | |
24 if (!gl) { | |
25 testFailed("context does not exist"); | |
26 return; | |
27 } | |
28 | |
29 testPassed("context exists"); | |
30 | |
31 gl.clearColor(0.0, 0.0, 0.0, 0.0); | |
32 gl.clear(gl.COLOR_BUFFER_BIT | gl.DEPTH_BUFFER_BIT); | |
33 | |
34 function doArraysHaveSameContents(a, b) { | |
35 var flags = []; | |
36 function hasUnusedValue(a, value) { | |
37 for (var ii = 0; ii < a.length; ++ii) { | |
38 if (a[ii] === value && !flags[ii]) { | |
39 flags[ii] = true; | |
40 return true; | |
41 } | |
42 } | |
43 return false; | |
44 } | |
45 | |
46 try { | |
47 if (a.length !== b.length) { | |
48 return false; | |
49 } | |
50 for (var ii = 0; ii < a.length; ii++) { | |
51 if (!hasUnusedValue(b, a[ii])) { | |
52 return false; | |
53 } | |
54 } | |
55 } catch (ex) { | |
56 return false; | |
57 } | |
58 return true; | |
59 } | |
60 | |
61 /////// Check compileShader() ///////////////////////////// | |
62 | |
63 var vs = gl.createShader(gl.VERTEX_SHADER); | |
64 gl.shaderSource(vs, "attribute vec4 aVertex; attribute vec4 aColor; varying
vec4 vColor; void main() { vColor = aColor; gl_Position = aVertex; }"); | |
65 gl.compileShader(vs); | |
66 | |
67 assertMsg(gl.getShaderParameter(vs, gl.COMPILE_STATUS) == true, | |
68 "good vertex shader should compile"); | |
69 | |
70 var vs2 = gl.createShader(gl.VERTEX_SHADER); | |
71 gl.shaderSource(vs2, "attribute vec4 aVertex; attribute vec4 aColor; varying
vec4 vColor; void main() { vColor = aColor; gl_Position = aVertex * 0.5; }"); | |
72 gl.compileShader(vs2); | |
73 | |
74 assertMsg(gl.getShaderParameter(vs2, gl.COMPILE_STATUS) == true, | |
75 "good vertex shader #2 should compile"); | |
76 | |
77 var vsBad = gl.createShader(gl.VERTEX_SHADER); | |
78 gl.shaderSource(vsBad, "WILL NOT COMPILE;"); | |
79 gl.compileShader(vsBad); | |
80 | |
81 assertMsg(gl.getShaderParameter(vsBad, gl.COMPILE_STATUS) == false, | |
82 "bad vertex shader should fail to compile"); | |
83 | |
84 var fs = gl.createShader(gl.FRAGMENT_SHADER); | |
85 gl.shaderSource(fs, "precision mediump float; varying vec4 vColor; void main
() { gl_FragColor = vColor; }"); | |
86 gl.compileShader(fs); | |
87 | |
88 assertMsg(gl.getShaderParameter(fs, gl.COMPILE_STATUS) == true, | |
89 "good fragment shader should compile"); | |
90 | |
91 var fs2 = gl.createShader(gl.FRAGMENT_SHADER); | |
92 gl.shaderSource(fs2, "precision mediump float; varying vec4 vColor; void mai
n() { gl_FragColor = vColor * 0.5; }"); | |
93 gl.compileShader(fs2); | |
94 | |
95 assertMsg(gl.getShaderParameter(fs2, gl.COMPILE_STATUS) == true, | |
96 "good fragment shader #2 should compile"); | |
97 | |
98 var fsBad = gl.createShader(gl.FRAGMENT_SHADER); | |
99 gl.shaderSource(fsBad, "WILL NOT COMPILE;"); | |
100 gl.compileShader(fsBad); | |
101 | |
102 assertMsg(gl.getShaderParameter(fsBad, gl.COMPILE_STATUS) == false, | |
103 "bad fragment shader should fail to compile"); | |
104 | |
105 glErrorShouldBe(gl, gl.NO_ERROR, "should be no errors at this point"); | |
106 | |
107 /////// Check attachShader() ///////////////////////////// | |
108 | |
109 function checkAttachShader(already_attached_shaders, shader, expected_error_
code, errmsg) { | |
110 var prog = gl.createProgram(); | |
111 for (var i = 0; i < already_attached_shaders.length; ++i) | |
112 gl.attachShader(prog, already_attached_shaders[i]); | |
113 if(gl.getError() != gl.NO_ERROR) | |
114 assertMsg(false, "unexpected error in attachShader()"); | |
115 gl.attachShader(prog, shader); | |
116 glErrorShouldBe(gl, expected_error_code, errmsg); | |
117 } | |
118 | |
119 checkAttachShader([], vs, gl.NO_ERROR, "attaching a vertex shader should suc
ceed"); | |
120 checkAttachShader([vs], vs, gl.INVALID_OPERATION, | |
121 "attaching an already attached vertex shader should genera
te INVALID_OPERATION"); | |
122 checkAttachShader([], fs, gl.NO_ERROR, "attaching a fragment shader should s
ucceed"); | |
123 checkAttachShader([fs], fs, gl.INVALID_OPERATION, | |
124 "attaching an already attached fragment shader should gene
rate INVALID_OPERATION"); | |
125 checkAttachShader([vs], vs2, gl.INVALID_OPERATION, | |
126 "attaching shaders of the same type to a program should ge
nerate INVALID_OPERATION"); | |
127 checkAttachShader([fs], fs2, gl.INVALID_OPERATION, | |
128 "attaching shaders of the same type to a program should ge
nerate INVALID_OPERATION"); | |
129 | |
130 /////// Check detachShader() ///////////////////////////// | |
131 | |
132 function checkDetachShader(already_attached_shaders, shader, expected_error_
code, errmsg) { | |
133 var prog = gl.createProgram(); | |
134 for (var i = 0; i < already_attached_shaders.length; ++i) | |
135 gl.attachShader(prog, already_attached_shaders[i]); | |
136 if(gl.getError() != gl.NO_ERROR) | |
137 assertMsg(false, "unexpected error in attachShader()"); | |
138 gl.detachShader(prog, shader); | |
139 glErrorShouldBe(gl, expected_error_code, errmsg); | |
140 } | |
141 | |
142 checkDetachShader([vs], vs, gl.NO_ERROR, "detaching a vertex shader should s
ucceed"); | |
143 checkDetachShader([fs], vs, gl.INVALID_OPERATION, | |
144 "detaching a not already attached vertex shader should gen
erate INVALID_OPERATION"); | |
145 checkDetachShader([fs], fs, gl.NO_ERROR, "detaching a fragment shader should
succeed"); | |
146 checkDetachShader([vs], fs, gl.INVALID_OPERATION, | |
147 "detaching a not already attached fragment shader should g
enerate INVALID_OPERATION"); | |
148 | |
149 /////// Check getAttachedShaders() ///////////////////////////// | |
150 | |
151 function checkGetAttachedShaders(shaders_to_attach, shaders_to_detach, expec
ted_shaders, errmsg) { | |
152 var prog = gl.createProgram(); | |
153 for (var i = 0; i < shaders_to_attach.length; ++i) | |
154 gl.attachShader(prog, shaders_to_attach[i]); | |
155 if(gl.getError() != gl.NO_ERROR) | |
156 assertMsg(false, "unexpected error in attachShader()"); | |
157 for (var i = 0; i < shaders_to_detach.length; ++i) | |
158 gl.detachShader(prog, shaders_to_detach[i]); | |
159 if(gl.getError() != gl.NO_ERROR) | |
160 assertMsg(false, "unexpected error in detachShader()"); | |
161 assertMsg(doArraysHaveSameContents(gl.getAttachedShaders(prog), expected
_shaders), errmsg); | |
162 } | |
163 checkGetAttachedShaders([], [], [], "getAttachedShaders should return an emp
ty list by default"); | |
164 checkGetAttachedShaders([fs], [], [fs], "attaching a single shader should gi
ve the expected list"); | |
165 checkGetAttachedShaders([fs, vs], [], [fs, vs], | |
166 "attaching some shaders should give the expected list"); | |
167 checkGetAttachedShaders([fs], [fs], [], "attaching a shader and detaching it
shoud leave an empty list"); | |
168 checkGetAttachedShaders([fs, vs], [fs, vs], [], | |
169 "attaching some shaders and detaching them in same order shoud leave an
empty list"); | |
170 checkGetAttachedShaders([fs, vs], [vs, fs], [], | |
171 "attaching some shaders and detaching them in random order shoud leave a
n empty list"); | |
172 checkGetAttachedShaders([fs, vs], [vs], [fs], | |
173 "attaching and detaching some shaders should leave the difference list")
; | |
174 checkGetAttachedShaders([fs, vs], [fs], [vs], | |
175 "attaching and detaching some shaders should leave the difference list")
; | |
176 checkGetAttachedShaders([fsBad], [], [fsBad], | |
177 "attaching a shader that failed to compile should still show it in the l
ist"); | |
178 checkGetAttachedShaders([fs, vsBad], [], [fs, vsBad], | |
179 "attaching shaders, including one that failed to compile, should still s
how the it in the list"); | |
180 | |
181 /////// Check linkProgram() and useProgram ///////////////////////////// | |
182 | |
183 function checkLinkAndUse(shaders, deleteShaderAfterAttach, expected_status,
errmsg) { | |
184 var prog = gl.createProgram(); | |
185 for (var i = 0; i < shaders.length; ++i) { | |
186 gl.attachShader(prog, shaders[i]); | |
187 if (deleteShaderAfterAttach) | |
188 gl.deleteShader(shaders[i]); | |
189 } | |
190 gl.bindAttribLocation(prog, 0, "aVertex"); | |
191 gl.bindAttribLocation(prog, 1, "aColor"); | |
192 gl.linkProgram(prog); | |
193 if (gl.getError() != gl.NO_ERROR) | |
194 assertMsg(false, "unexpected error in linkProgram()"); | |
195 assertMsg(gl.getProgramParameter(prog, gl.LINK_STATUS) == expected_statu
s, errmsg); | |
196 if (expected_status == true && gl.getProgramParameter(prog, gl.LINK_STAT
US) == false) | |
197 debug(gl.getProgramInfoLog(prog)); | |
198 if (gl.getError() != gl.NO_ERROR) | |
199 assertMsg(false, "unexpected error in getProgramParameter()"); | |
200 gl.useProgram(prog); | |
201 if (expected_status == true) | |
202 glErrorShouldBe(gl, gl.NO_ERROR, "using a valid program should succe
ed"); | |
203 if (expected_status == false) | |
204 glErrorShouldBe(gl, gl.INVALID_OPERATION, "using an invalid program
should generate INVALID_OPERATION"); | |
205 return prog; | |
206 } | |
207 | |
208 var progGood1 = checkLinkAndUse([vs, fs], false, true, "valid program should
link"); | |
209 var progGood2 = checkLinkAndUse([vs, fs2], false, true, "valid program #2 sh
ould link"); | |
210 var progBad1 = checkLinkAndUse([vs], false, false, "program with no fragment
shader should fail to link"); | |
211 var progBad2 = checkLinkAndUse([fs], false, false, "program with no vertex s
hader should fail to link"); | |
212 var progBad3 = checkLinkAndUse([vsBad, fs], false, false, "program with bad
vertex shader should fail to link"); | |
213 var progBad4 = checkLinkAndUse([vs, fsBad], false, false, "program with bad
fragment shader should fail to link"); | |
214 var progBad5 = checkLinkAndUse([vsBad, fsBad], false, false, "program with b
ad shaders should fail to link"); | |
215 | |
216 gl.useProgram(progGood1); | |
217 glErrorShouldBe(gl, gl.NO_ERROR, "using a valid program shouldn't generate a
GL error"); | |
218 | |
219 var vbuf = gl.createBuffer(); | |
220 gl.bindBuffer(gl.ARRAY_BUFFER, vbuf); | |
221 gl.bufferData(gl.ARRAY_BUFFER, | |
222 new Float32Array([ | |
223 0.0, 0.0, 0.0, 1.0, | |
224 1.0, 0.0, 0.0, 1.0, | |
225 1.0, 1.0, 0.0, 1.0, | |
226 0.0, 1.0, 0.0, 1.0]), | |
227 gl.STATIC_DRAW); | |
228 gl.vertexAttribPointer(0, 4, gl.FLOAT, false, 0, 0); | |
229 gl.enableVertexAttribArray(0); | |
230 gl.vertexAttrib3f(1, 1.0, 0.0, 0.0); | |
231 | |
232 glErrorShouldBe(gl, gl.NO_ERROR, "should be no errors at this point #2"); | |
233 | |
234 gl.useProgram(progGood1); | |
235 gl.drawArrays(gl.TRIANGLES, 0, 3); | |
236 glErrorShouldBe(gl, gl.NO_ERROR, "drawing with a valid program shouldn't gen
erate a GL error"); | |
237 | |
238 gl.useProgram(progBad1); | |
239 glErrorShouldBe(gl, gl.INVALID_OPERATION, "using an invalid program should g
enerate INVALID_OPERATION"); | |
240 gl.drawArrays(gl.TRIANGLES, 0, 3); | |
241 glErrorShouldBe(gl, gl.NO_ERROR, "Try to use an invalid program should not c
hange the current rendering state"); | |
242 | |
243 gl.useProgram(progGood2); | |
244 gl.drawArrays(gl.TRIANGLES, 0, 3); | |
245 glErrorShouldBe(gl, gl.NO_ERROR, "drawing with a valid program shouldn't gen
erate a GL error"); | |
246 gl.detachShader(progGood2, fs2); | |
247 gl.attachShader(progGood2, fsBad); | |
248 gl.linkProgram(progGood2); | |
249 assertMsg(gl.getProgramParameter(progGood2, gl.LINK_STATUS) == false, | |
250 "linking should fail with in-use formerly good program, with new b
ad shader attached"); | |
251 | |
252 gl.useProgram(progGood1); | |
253 gl.drawArrays(gl.TRIANGLES, 0, 4); | |
254 glErrorShouldBe(gl, gl.NO_ERROR, "drawing with a valid when last used progra
m shouldn't generate a GL error"); | |
255 | |
256 var progGood1 = checkLinkAndUse([vs, fs], true, true, "delete shaders after
attaching them and before linking program should not affect linkProgram"); | |
257 gl.useProgram(progGood1); | |
258 gl.drawArrays(gl.TRIANGLES, 0, 4); | |
259 glErrorShouldBe(gl, gl.NO_ERROR, "drawing with a valid when last used progra
m shouldn't generate a GL error"); | |
260 | |
261 /////// Check deleteProgram() and deleteShader() ///////////////////////////// | |
262 | |
263 gl.useProgram(progGood1); | |
264 gl.deleteProgram(progGood1); | |
265 gl.drawArrays(gl.TRIANGLES, 0, 4); | |
266 glErrorShouldBe(gl, gl.NO_ERROR, "delete the current program shouldn't chang
e the current rendering state"); | |
267 | |
268 gl.linkProgram(progGood1); | |
269 glErrorShouldBe(gl, gl.NO_ERROR, "The current program shouldn't be deleted")
; | |
270 | |
271 var fs3 = gl.createShader(gl.FRAGMENT_SHADER); | |
272 gl.shaderSource(fs3, "precision mediump float; varying vec4 vColor; void mai
n() { gl_FragColor = vColor; }"); | |
273 gl.compileShader(fs3); | |
274 | |
275 assertMsg(gl.getShaderParameter(fs3, gl.COMPILE_STATUS) == true, | |
276 "good fragment shader should compile"); | |
277 | |
278 gl.deleteShader(fs3); | |
279 gl.compileShader(fs3); | |
280 glErrorShouldBe(gl, gl.INVALID_VALUE, "an unattached shader should be delete
d immediately"); | |
281 | |
282 fs3 = gl.createShader(gl.FRAGMENT_SHADER); | |
283 gl.shaderSource(fs3, "precision mediump float; varying vec4 vColor; void mai
n() { gl_FragColor = vColor; }"); | |
284 gl.compileShader(fs3); | |
285 | |
286 assertMsg(gl.getShaderParameter(fs3, gl.COMPILE_STATUS) == true, | |
287 "good fragment shader should compile"); | |
288 | |
289 gl.detachShader(progGood1, fs); | |
290 gl.attachShader(progGood1, fs3); | |
291 | |
292 gl.deleteShader(fs3); | |
293 gl.compileShader(fs3); | |
294 assertMsg(gl.getShaderParameter(fs3, gl.COMPILE_STATUS) == true, | |
295 "an attached shader shouldn't be deleted"); | |
296 | |
297 gl.useProgram(null); | |
298 gl.linkProgram(progGood1); | |
299 glErrorShouldBe(gl, gl.INVALID_VALUE, "a delete-marked program should be del
eted once it's no longer the current program"); | |
300 | |
301 gl.compileShader(fs3); | |
302 glErrorShouldBe(gl, gl.INVALID_VALUE, "a delete-marked shader should be dele
ted once all its attachments are removed"); | |
303 } | |
304 | |
305 debug(""); | |
306 go(); | |
307 </script> | |
308 | |
309 </body> | |
310 </html> | |
OLD | NEW |