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