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("test: " + passMsg); |
| 66 |
| 67 var console = document.getElementById("console"); |
| 68 |
| 69 if (info.vShaderSource === undefined) { |
| 70 if (info.vShaderId) { |
| 71 info.vShaderSource = document.getElementById(info.vShaderId).text; |
| 72 } else { |
| 73 info.vShader = 'defaultVertexShader'; |
| 74 info.vShaderSource = defaultVertexShader; |
| 75 } |
| 76 } |
| 77 if (info.fShaderSource === undefined) { |
| 78 if (info.fShaderId) { |
| 79 info.fShaderSource = document.getElementById(info.fShaderId).text; |
| 80 } else { |
| 81 info.fShader = 'defaultFragmentShader'; |
| 82 info.fShaderSource = defaultFragmentShader; |
| 83 } |
| 84 } |
| 85 |
| 86 var vLabel = (info.vShaderSource == defaultVertexShader ? "default" : "test")
+ " vertex shader"; |
| 87 var fLabel = (info.fShaderSource == defaultFragmentShader ? "default" : "test"
) + " fragment shader"; |
| 88 |
| 89 var vSource = info.vShaderPrep ? info.vShaderPrep(info.vShaderSource) : |
| 90 info.vShaderSource; |
| 91 |
| 92 wtu.addShaderSource(console, vLabel, vSource); |
| 93 |
| 94 // Reuse identical shaders so we test shared shader. |
| 95 var vShader = vShaderDB[vSource]; |
| 96 if (!vShader) { |
| 97 vShader = wtu.loadShader(gl, vSource, gl.VERTEX_SHADER); |
| 98 if (info.vShaderTest) { |
| 99 if (!info.vShaderTest(vShader)) { |
| 100 testFailed("[vertex shader test] " + passMsg); |
| 101 return; |
| 102 } |
| 103 } |
| 104 // As per GLSL 1.0.17 10.27 we can only check for success on |
| 105 // compileShader, not failure. |
| 106 if (info.vShaderSuccess && !vShader) { |
| 107 testFailed("[unexpected vertex shader compile status] (expected: " + |
| 108 info.vShaderSuccess + ") " + passMsg); |
| 109 } |
| 110 // Save the shaders so we test shared shader. |
| 111 if (vShader) { |
| 112 vShaderDB[vSource] = vShader; |
| 113 } |
| 114 } |
| 115 |
| 116 var fSource = info.fShaderPrep ? info.fShaderPrep(info.fShaderSource) : |
| 117 info.fShaderSource; |
| 118 |
| 119 wtu.addShaderSource(console, fLabel, fSource); |
| 120 |
| 121 // Reuse identical shaders so we test shared shader. |
| 122 var fShader = fShaderDB[fSource]; |
| 123 if (!fShader) { |
| 124 fShader = wtu.loadShader(gl, fSource, gl.FRAGMENT_SHADER); |
| 125 if (info.fShaderTest) { |
| 126 if (!info.fShaderTest(fShader)) { |
| 127 testFailed("[fragment shdaer test] " + passMsg); |
| 128 return; |
| 129 } |
| 130 } |
| 131 //debug(fShader == null ? "fail" : "succeed"); |
| 132 // As per GLSL 1.0.17 10.27 we can only check for success on |
| 133 // compileShader, not failure. |
| 134 if (info.fShaderSuccess && !fShader) { |
| 135 testFailed("[unexpected fragment shader compile status] (expected: " + |
| 136 info.fShaderSuccess + ") " + passMsg); |
| 137 return; |
| 138 } |
| 139 // Safe the shaders so we test shared shader. |
| 140 if (fShader) { |
| 141 fShaderDB[fSource] = fShader; |
| 142 } |
| 143 } |
| 144 |
| 145 if (vShader && fShader) { |
| 146 var program = gl.createProgram(); |
| 147 gl.attachShader(program, vShader); |
| 148 gl.attachShader(program, fShader); |
| 149 gl.bindAttribLocation(program, 0, "vPosition"); |
| 150 gl.bindAttribLocation(program, 1, "texCoord0"); |
| 151 gl.linkProgram(program); |
| 152 var linked = (gl.getProgramParameter(program, gl.LINK_STATUS) != 0); |
| 153 if (!linked) { |
| 154 var error = gl.getProgramInfoLog(program); |
| 155 log("*** Error linking program '"+program+"':"+error); |
| 156 } |
| 157 if (linked != info.linkSuccess) { |
| 158 testFailed("[unexpected link status] " + passMsg); |
| 159 return; |
| 160 } |
| 161 } else { |
| 162 if (info.linkSuccess) { |
| 163 testFailed("[link failed] " + passMsg); |
| 164 return; |
| 165 } |
| 166 } |
| 167 |
| 168 if (!info.render) { |
| 169 testPassed(passMsg); |
| 170 return; |
| 171 } |
| 172 |
| 173 gl.useProgram(program); |
| 174 wtu.setupUnitQuad(gl); |
| 175 wtu.drawQuad(gl); |
| 176 |
| 177 var div = document.createElement("div"); |
| 178 div.className = "testimages"; |
| 179 wtu.insertImage(div, "result", wtu.makeImage(gl.canvas)); |
| 180 div.appendChild(document.createElement('br')); |
| 181 console.appendChild(div); |
| 182 wtu.checkCanvas(gl, [0, 255, 0, 255], "should be green", 0); |
| 183 } |
| 184 |
| 185 function runTests(shaderInfos) { |
| 186 var wtu = WebGLTestUtils; |
| 187 var canvas = document.createElement('canvas'); |
| 188 canvas.width = 32; |
| 189 canvas.height = 32; |
| 190 var gl = wtu.create3DContext(canvas); |
| 191 if (!gl) { |
| 192 testFailed("context does not exist"); |
| 193 finishTest(); |
| 194 return; |
| 195 } |
| 196 |
| 197 for (var ii = 0; ii < shaderInfos.length; ++ii) { |
| 198 runOneTest(gl, shaderInfos[ii]); |
| 199 } |
| 200 |
| 201 finishTest(); |
| 202 }; |
| 203 |
| 204 function loadExternalShaders(filename, passMsg) { |
| 205 var shaderInfos = []; |
| 206 var lines = wtu.readFileList(filename); |
| 207 for (var ii = 0; ii < lines.length; ++ii) { |
| 208 var info = { |
| 209 vShaderSource: defaultVertexShader, |
| 210 vShaderSuccess: true, |
| 211 fShaderSource: defaultFragmentShader, |
| 212 fShaderSuccess: true, |
| 213 linkSuccess: true, |
| 214 }; |
| 215 |
| 216 var line = lines[ii]; |
| 217 var files = line.split(/ +/); |
| 218 var passMsg = ""; |
| 219 for (var jj = 0; jj < files.length; ++jj) { |
| 220 var file = files[jj]; |
| 221 var shaderSource = wtu.readFile(file); |
| 222 var firstLine = shaderSource.split("\n")[0]; |
| 223 var success = undefined; |
| 224 if (firstLine.indexOf("fail") >= 0) { |
| 225 success = false; |
| 226 } else if (firstLine.indexOf("succeed") >= 0) { |
| 227 success = true; |
| 228 } |
| 229 if (success === undefined) { |
| 230 testFailed("bad first line in " + file + ":" + firstLine); |
| 231 continue; |
| 232 } |
| 233 if (!wtu.startsWith(firstLine, "// ")) { |
| 234 testFailed("bad first line in " + file + ":" + firstLine); |
| 235 continue; |
| 236 } |
| 237 passMsg = passMsg + (passMsg.length ? ", " : "") + firstLine.substr(3); |
| 238 if (wtu.endsWith(file, ".vert")) { |
| 239 info.vShaderSource = shaderSource; |
| 240 info.vShaderSuccess = success; |
| 241 } else if (wtu.endsWith(file, ".frag")) { |
| 242 info.fShaderSource = shaderSource; |
| 243 info.fShaderSuccess = success; |
| 244 } |
| 245 } |
| 246 info.linkSuccess = info.vShaderSuccess && info.fShaderSuccess; |
| 247 info.passMsg = passMsg; |
| 248 shaderInfos.push(info); |
| 249 } |
| 250 return shaderInfos; |
| 251 } |
| 252 |
| 253 function getSource(elem) { |
| 254 var str = elem.text; |
| 255 return str.replace(/^\s*/, '').replace(/\s*$/, ''); |
| 256 } |
| 257 |
| 258 function getPassMessage(source) { |
| 259 var lines = source.split('\n'); |
| 260 return lines[0].substring(3); |
| 261 } |
| 262 |
| 263 function getSuccess(msg) { |
| 264 if (msg.indexOf("fail") >= 0) { |
| 265 return false; |
| 266 } |
| 267 if (msg.indexOf("succeed") >= 0) { |
| 268 return true; |
| 269 } |
| 270 testFailed("bad test description. Must have 'fail' or 'success'"); |
| 271 } |
| 272 |
| 273 function setupTest() { |
| 274 var vShaderElem = document.getElementById('vertexShader'); |
| 275 var vShaderSource = defaultVertexShader; |
| 276 var vShaderSuccess = true; |
| 277 |
| 278 var fShaderElem = document.getElementById('fragmentShader'); |
| 279 var fShaderSource = defaultFragmentShader; |
| 280 var fShaderSuccess = true; |
| 281 |
| 282 var passMsg = undefined; |
| 283 |
| 284 if (vShaderElem) { |
| 285 vShaderSource = getSource(vShaderElem); |
| 286 passMsg = getPassMessage(vShaderSource); |
| 287 vShaderSuccess = getSuccess(passMsg); |
| 288 } |
| 289 |
| 290 if (fShaderElem) { |
| 291 fShaderSource = getSource(fShaderElem); |
| 292 passMsg = getPassMessage(fShaderSource); |
| 293 fShaderSuccess = getSuccess(passMsg); |
| 294 } |
| 295 |
| 296 var linkSuccess = vShaderSuccess && fShaderSuccess; |
| 297 |
| 298 if (passMsg === undefined) { |
| 299 testFailed("no test shader found."); |
| 300 finishTest(); |
| 301 return; |
| 302 } |
| 303 |
| 304 var info = { |
| 305 vShaderSource: vShaderSource, |
| 306 vShaderSuccess: vShaderSuccess, |
| 307 fShaderSource: fShaderSource, |
| 308 fShaderSuccess: fShaderSuccess, |
| 309 linkSuccess: linkSuccess, |
| 310 passMsg: passMsg |
| 311 }; |
| 312 |
| 313 return info; |
| 314 } |
| 315 |
| 316 function runTest() { |
| 317 var info = setupTest(); |
| 318 description(info.passMsg); |
| 319 runTests([info]); |
| 320 } |
| 321 |
| 322 function runRenderTests(tests) { |
| 323 for (var ii = 0; ii < tests.length; ++ii) { |
| 324 tests[ii].render = true |
| 325 } |
| 326 runTests(tests); |
| 327 } |
| 328 |
| 329 function runRenderTest() { |
| 330 var info = setupTest(); |
| 331 description(info.passMsg); |
| 332 runRenderTests([info]); |
| 333 } |
| 334 |
| 335 return { |
| 336 runTest: runTest, |
| 337 runTests: runTests, |
| 338 runRenderTest: runRenderTest, |
| 339 runRenderTests: runRenderTests, |
| 340 loadExternalShaders: loadExternalShaders, |
| 341 |
| 342 none: false, |
| 343 }; |
| 344 }()); |
OLD | NEW |