OLD | NEW |
(Empty) | |
| 1 /* |
| 2 ** Copyright (c) 2012 The Khronos Group Inc. |
| 3 ** |
| 4 ** Permission is hereby granted, free of charge, to any person obtaining a |
| 5 ** copy of this software and/or associated documentation files (the |
| 6 ** "Materials"), to deal in the Materials without restriction, including |
| 7 ** without limitation the rights to use, copy, modify, merge, publish, |
| 8 ** distribute, sublicense, and/or sell copies of the Materials, and to |
| 9 ** permit persons to whom the Materials are furnished to do so, subject to |
| 10 ** the following conditions: |
| 11 ** |
| 12 ** The above copyright notice and this permission notice shall be included |
| 13 ** in all copies or substantial portions of the Materials. |
| 14 ** |
| 15 ** THE MATERIALS ARE PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, |
| 16 ** EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF |
| 17 ** MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. |
| 18 ** IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY |
| 19 ** CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, |
| 20 ** TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE |
| 21 ** MATERIALS OR THE USE OR OTHER DEALINGS IN THE MATERIALS. |
| 22 */ |
| 23 GLSLConformanceTester = (function(){ |
| 24 |
| 25 var wtu = WebGLTestUtils; |
| 26 var defaultVertexShader = [ |
| 27 "attribute vec4 vPosition;", |
| 28 "void main()", |
| 29 "{", |
| 30 " gl_Position = vPosition;", |
| 31 "}" |
| 32 ].join('\n'); |
| 33 |
| 34 var defaultFragmentShader = [ |
| 35 "precision mediump float;", |
| 36 "void main()", |
| 37 "{", |
| 38 " gl_FragColor = vec4(1.0,0.0,0.0,1.0);", |
| 39 "}" |
| 40 ].join('\n'); |
| 41 |
| 42 function log(msg) { |
| 43 if (window.console && window.console.log) { |
| 44 window.console.log(msg); |
| 45 } |
| 46 } |
| 47 |
| 48 var vShaderDB = {}; |
| 49 var fShaderDB = {}; |
| 50 |
| 51 /** |
| 52 * vShaderSource: the source code for vertex shader |
| 53 * vShaderSuccess: true if vertex shader compiliation should |
| 54 * succeed. |
| 55 * fShaderSource: the source code for fragment shader |
| 56 * fShaderSuccess: true if fragment shader compiliation should |
| 57 * succeed. |
| 58 * linkSuccess: true of link should succeed |
| 59 * passMsg: msg to describe success condition. |
| 60 * render: if true render to unit quad. Green = success |
| 61 * |
| 62 */ |
| 63 function runOneTest(gl, info) { |
| 64 var passMsg = info.passMsg |
| 65 debug(""); |
| 66 debug("test: " + passMsg); |
| 67 |
| 68 var console = document.getElementById("console"); |
| 69 |
| 70 if (info.vShaderSource === undefined) { |
| 71 if (info.vShaderId) { |
| 72 info.vShaderSource = document.getElementById(info.vShaderId).text; |
| 73 } else { |
| 74 info.vShader = 'defaultVertexShader'; |
| 75 info.vShaderSource = defaultVertexShader; |
| 76 } |
| 77 } |
| 78 if (info.fShaderSource === undefined) { |
| 79 if (info.fShaderId) { |
| 80 info.fShaderSource = document.getElementById(info.fShaderId).text; |
| 81 } else { |
| 82 info.fShader = 'defaultFragmentShader'; |
| 83 info.fShaderSource = defaultFragmentShader; |
| 84 } |
| 85 } |
| 86 |
| 87 var vLabel = (info.vShaderSource == defaultVertexShader ? "default" : "test")
+ " vertex shader"; |
| 88 var fLabel = (info.fShaderSource == defaultFragmentShader ? "default" : "test"
) + " fragment shader"; |
| 89 |
| 90 var vSource = info.vShaderPrep ? info.vShaderPrep(info.vShaderSource) : |
| 91 info.vShaderSource; |
| 92 |
| 93 wtu.addShaderSource(console, vLabel, vSource); |
| 94 |
| 95 // Reuse identical shaders so we test shared shader. |
| 96 var vShader = vShaderDB[vSource]; |
| 97 if (!vShader) { |
| 98 vShader = wtu.loadShader(gl, vSource, gl.VERTEX_SHADER); |
| 99 if (info.vShaderTest) { |
| 100 if (!info.vShaderTest(vShader)) { |
| 101 testFailed("[vertex shader test] " + passMsg); |
| 102 return; |
| 103 } |
| 104 } |
| 105 // As per GLSL 1.0.17 10.27 we can only check for success on |
| 106 // compileShader, not failure. |
| 107 if (!info.ignoreResults && info.vShaderSuccess && !vShader) { |
| 108 testFailed("[unexpected vertex shader compile status] (expected: " + |
| 109 info.vShaderSuccess + ") " + passMsg); |
| 110 } |
| 111 // Save the shaders so we test shared shader. |
| 112 if (vShader) { |
| 113 vShaderDB[vSource] = vShader; |
| 114 } |
| 115 } |
| 116 |
| 117 var fSource = info.fShaderPrep ? info.fShaderPrep(info.fShaderSource) : |
| 118 info.fShaderSource; |
| 119 |
| 120 wtu.addShaderSource(console, fLabel, fSource); |
| 121 |
| 122 // Reuse identical shaders so we test shared shader. |
| 123 var fShader = fShaderDB[fSource]; |
| 124 if (!fShader) { |
| 125 fShader = wtu.loadShader(gl, fSource, gl.FRAGMENT_SHADER); |
| 126 if (info.fShaderTest) { |
| 127 if (!info.fShaderTest(fShader)) { |
| 128 testFailed("[fragment shdaer test] " + passMsg); |
| 129 return; |
| 130 } |
| 131 } |
| 132 //debug(fShader == null ? "fail" : "succeed"); |
| 133 // As per GLSL 1.0.17 10.27 we can only check for success on |
| 134 // compileShader, not failure. |
| 135 if (!info.ignoreResults && info.fShaderSuccess && !fShader) { |
| 136 testFailed("[unexpected fragment shader compile status] (expected: " + |
| 137 info.fShaderSuccess + ") " + passMsg); |
| 138 return; |
| 139 } |
| 140 // Safe the shaders so we test shared shader. |
| 141 if (fShader) { |
| 142 fShaderDB[fSource] = fShader; |
| 143 } |
| 144 } |
| 145 |
| 146 if (vShader && fShader) { |
| 147 var program = gl.createProgram(); |
| 148 gl.attachShader(program, vShader); |
| 149 gl.attachShader(program, fShader); |
| 150 |
| 151 if (vSource.indexOf("vPosition") >= 0) { |
| 152 gl.bindAttribLocation(program, 0, "vPosition"); |
| 153 } |
| 154 if (vSource.indexOf("texCoord0") >= 0) { |
| 155 gl.bindAttribLocation(program, 1, "texCoord0"); |
| 156 } |
| 157 gl.linkProgram(program); |
| 158 var linked = (gl.getProgramParameter(program, gl.LINK_STATUS) != 0); |
| 159 if (!linked) { |
| 160 var error = gl.getProgramInfoLog(program); |
| 161 log("*** Error linking program '"+program+"':"+error); |
| 162 } |
| 163 if (!info.ignoreResults && linked != info.linkSuccess) { |
| 164 testFailed("[unexpected link status] " + passMsg); |
| 165 return; |
| 166 } |
| 167 } else { |
| 168 if (!info.ignoreResults && info.linkSuccess) { |
| 169 testFailed("[link failed] " + passMsg); |
| 170 return; |
| 171 } |
| 172 } |
| 173 |
| 174 if (!info.render) { |
| 175 testPassed(passMsg); |
| 176 return; |
| 177 } |
| 178 |
| 179 gl.useProgram(program); |
| 180 wtu.setupUnitQuad(gl); |
| 181 wtu.clearAndDrawUnitQuad(gl); |
| 182 |
| 183 var div = document.createElement("div"); |
| 184 div.className = "testimages"; |
| 185 wtu.insertImage(div, "result", wtu.makeImage(gl.canvas)); |
| 186 div.appendChild(document.createElement('br')); |
| 187 console.appendChild(div); |
| 188 wtu.checkCanvas(gl, [0, 255, 0, 255], "should be green", 0); |
| 189 } |
| 190 |
| 191 function runTests(shaderInfos) { |
| 192 var wtu = WebGLTestUtils; |
| 193 var canvas = document.createElement('canvas'); |
| 194 canvas.width = 32; |
| 195 canvas.height = 32; |
| 196 var gl = wtu.create3DContext(canvas); |
| 197 if (!gl) { |
| 198 testFailed("context does not exist"); |
| 199 finishTest(); |
| 200 return; |
| 201 } |
| 202 |
| 203 var testIndex = 0; |
| 204 var runNextTest = function() { |
| 205 if (testIndex == shaderInfos.length) { |
| 206 finishTest(); |
| 207 return; |
| 208 } |
| 209 |
| 210 runOneTest(gl, shaderInfos[testIndex++]); |
| 211 setTimeout(runNextTest, 1); |
| 212 } |
| 213 runNextTest(); |
| 214 }; |
| 215 |
| 216 function loadExternalShaders(filename, passMsg) { |
| 217 var shaderInfos = []; |
| 218 var lines = wtu.readFileList(filename); |
| 219 for (var ii = 0; ii < lines.length; ++ii) { |
| 220 var info = { |
| 221 vShaderSource: defaultVertexShader, |
| 222 vShaderSuccess: true, |
| 223 fShaderSource: defaultFragmentShader, |
| 224 fShaderSuccess: true, |
| 225 linkSuccess: true, |
| 226 }; |
| 227 |
| 228 var line = lines[ii]; |
| 229 var files = line.split(/ +/); |
| 230 var passMsg = ""; |
| 231 for (var jj = 0; jj < files.length; ++jj) { |
| 232 var file = files[jj]; |
| 233 var shaderSource = wtu.readFile(file); |
| 234 var firstLine = shaderSource.split("\n")[0]; |
| 235 var success = undefined; |
| 236 if (firstLine.indexOf("fail") >= 0) { |
| 237 success = false; |
| 238 } else if (firstLine.indexOf("succeed") >= 0) { |
| 239 success = true; |
| 240 } |
| 241 if (success === undefined) { |
| 242 testFailed("bad first line in " + file + ":" + firstLine); |
| 243 continue; |
| 244 } |
| 245 if (!wtu.startsWith(firstLine, "// ")) { |
| 246 testFailed("bad first line in " + file + ":" + firstLine); |
| 247 continue; |
| 248 } |
| 249 passMsg = passMsg + (passMsg.length ? ", " : "") + firstLine.substr(3); |
| 250 if (wtu.endsWith(file, ".vert")) { |
| 251 info.vShaderSource = shaderSource; |
| 252 info.vShaderSuccess = success; |
| 253 } else if (wtu.endsWith(file, ".frag")) { |
| 254 info.fShaderSource = shaderSource; |
| 255 info.fShaderSuccess = success; |
| 256 } |
| 257 } |
| 258 info.linkSuccess = info.vShaderSuccess && info.fShaderSuccess; |
| 259 info.passMsg = passMsg; |
| 260 shaderInfos.push(info); |
| 261 } |
| 262 return shaderInfos; |
| 263 } |
| 264 |
| 265 function getSource(elem) { |
| 266 var str = elem.text; |
| 267 return str.replace(/^\s*/, '').replace(/\s*$/, ''); |
| 268 } |
| 269 |
| 270 function getPassMessage(source) { |
| 271 var lines = source.split('\n'); |
| 272 return lines[0].substring(3); |
| 273 } |
| 274 |
| 275 function getSuccess(msg) { |
| 276 if (msg.indexOf("fail") >= 0) { |
| 277 return false; |
| 278 } |
| 279 if (msg.indexOf("succeed") >= 0) { |
| 280 return true; |
| 281 } |
| 282 testFailed("bad test description. Must have 'fail' or 'success'"); |
| 283 } |
| 284 |
| 285 function setupTest() { |
| 286 var vShaderElem = document.getElementById('vertexShader'); |
| 287 var vShaderSource = defaultVertexShader; |
| 288 var vShaderSuccess = true; |
| 289 |
| 290 var fShaderElem = document.getElementById('fragmentShader'); |
| 291 var fShaderSource = defaultFragmentShader; |
| 292 var fShaderSuccess = true; |
| 293 |
| 294 var passMsg = undefined; |
| 295 |
| 296 if (vShaderElem) { |
| 297 vShaderSource = getSource(vShaderElem); |
| 298 passMsg = getPassMessage(vShaderSource); |
| 299 vShaderSuccess = getSuccess(passMsg); |
| 300 } |
| 301 |
| 302 if (fShaderElem) { |
| 303 fShaderSource = getSource(fShaderElem); |
| 304 passMsg = getPassMessage(fShaderSource); |
| 305 fShaderSuccess = getSuccess(passMsg); |
| 306 } |
| 307 |
| 308 var linkSuccess = vShaderSuccess && fShaderSuccess; |
| 309 |
| 310 if (passMsg === undefined) { |
| 311 testFailed("no test shader found."); |
| 312 finishTest(); |
| 313 return; |
| 314 } |
| 315 |
| 316 var info = { |
| 317 vShaderSource: vShaderSource, |
| 318 vShaderSuccess: vShaderSuccess, |
| 319 fShaderSource: fShaderSource, |
| 320 fShaderSuccess: fShaderSuccess, |
| 321 linkSuccess: linkSuccess, |
| 322 passMsg: passMsg |
| 323 }; |
| 324 |
| 325 return info; |
| 326 } |
| 327 |
| 328 function runTest() { |
| 329 var info = setupTest(); |
| 330 description(info.passMsg); |
| 331 runTests([info]); |
| 332 } |
| 333 |
| 334 function runRenderTests(tests) { |
| 335 for (var ii = 0; ii < tests.length; ++ii) { |
| 336 tests[ii].render = true |
| 337 } |
| 338 runTests(tests); |
| 339 } |
| 340 |
| 341 function runRenderTest() { |
| 342 var info = setupTest(); |
| 343 description(info.passMsg); |
| 344 runRenderTests([info]); |
| 345 } |
| 346 |
| 347 return { |
| 348 runTest: runTest, |
| 349 runTests: runTests, |
| 350 runRenderTest: runRenderTest, |
| 351 runRenderTests: runRenderTests, |
| 352 loadExternalShaders: loadExternalShaders, |
| 353 |
| 354 none: false, |
| 355 }; |
| 356 }()); |
OLD | NEW |