OLD | NEW |
(Empty) | |
| 1 <!-- |
| 2 Copyright (c) 2011 The Chromium Authors. 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 OES_standard_derivatives Conformance Tests</title> |
| 11 <link rel="stylesheet" href="../../resources/js-test-style.css"/> |
| 12 <script src="../../resources/desktop-gl-constants.js" type="text/javascript"></s
cript> |
| 13 <script src="../../resources/js-test-pre.js"></script> |
| 14 <script src="../resources/webgl-test.js"></script> |
| 15 <script src="../resources/webgl-test-utils.js"></script> |
| 16 </head> |
| 17 <body> |
| 18 <div id="description"></div> |
| 19 <canvas id="canvas" style="width: 50px; height: 50px;"> </canvas> |
| 20 <div id="console"></div> |
| 21 <!-- Shaders for testing standard derivatives --> |
| 22 |
| 23 <!-- Shader omitting the required #extension pragma --> |
| 24 <script id="missingPragmaFragmentShader" type="x-shader/x-fragment"> |
| 25 precision mediump float; |
| 26 varying vec2 texCoord; |
| 27 void main() { |
| 28 float dx = dFdx(texCoord.x); |
| 29 float dy = dFdy(texCoord.y); |
| 30 float w = fwidth(texCoord.x); |
| 31 gl_FragColor = vec4(dx, dy, w, 1.0); |
| 32 } |
| 33 </script> |
| 34 |
| 35 <!-- Shader to test macro definition --> |
| 36 <script id="macroFragmentShader" type="x-shader/x-fragment"> |
| 37 precision mediump float; |
| 38 void main() { |
| 39 #ifdef GL_OES_standard_derivatives |
| 40 gl_FragColor = vec4(0.0, 0.0, 0.0, 0.0); |
| 41 #else |
| 42 // Error expected |
| 43 #error no GL_OES_standard_derivatives; |
| 44 #endif |
| 45 } |
| 46 </script> |
| 47 |
| 48 <!-- Shader with required #extension pragma --> |
| 49 <script id="testFragmentShader" type="x-shader/x-fragment"> |
| 50 #extension GL_OES_standard_derivatives : enable |
| 51 precision mediump float; |
| 52 varying vec2 texCoord; |
| 53 void main() { |
| 54 float dx = dFdx(texCoord.x); |
| 55 float dy = dFdy(texCoord.y); |
| 56 float w = fwidth(texCoord.x); |
| 57 gl_FragColor = vec4(dx, dy, w, 1.0); |
| 58 } |
| 59 </script> |
| 60 <!-- Shaders to link with test fragment shaders --> |
| 61 <script id="goodVertexShader" type="x-shader/x-vertex"> |
| 62 attribute vec4 vPosition; |
| 63 varying vec2 texCoord; |
| 64 void main() { |
| 65 texCoord = vPosition.xy; |
| 66 gl_Position = vPosition; |
| 67 } |
| 68 </script> |
| 69 <!-- Shaders to test output --> |
| 70 <script id="outputVertexShader" type="x-shader/x-vertex"> |
| 71 attribute vec4 vPosition; |
| 72 varying vec4 position; |
| 73 void main() { |
| 74 position = vPosition; |
| 75 gl_Position = vPosition; |
| 76 } |
| 77 </script> |
| 78 <script id="outputFragmentShader" type="x-shader/x-fragment"> |
| 79 #extension GL_OES_standard_derivatives : enable |
| 80 precision mediump float; |
| 81 varying vec4 position; |
| 82 void main() { |
| 83 float dzdx = dFdx(position.z); |
| 84 float dzdy = dFdy(position.z); |
| 85 float fw = fwidth(position.z); |
| 86 gl_FragColor = vec4(abs(dzdx), abs(dzdy), fw, 1.0); |
| 87 } |
| 88 </script> |
| 89 |
| 90 <script> |
| 91 description("This test verifies the functionality of the OES_standard_derivative
s extension, if it is available."); |
| 92 |
| 93 debug(""); |
| 94 |
| 95 var wtu = WebGLTestUtils; |
| 96 var canvas = document.getElementById("canvas"); |
| 97 var gl = create3DContext(canvas); |
| 98 var ext = null; |
| 99 |
| 100 if (!gl) { |
| 101 testFailed("WebGL context does not exist"); |
| 102 } else { |
| 103 testPassed("WebGL context exists"); |
| 104 |
| 105 // Run tests with extension disabled |
| 106 runHintTestDisabled(); |
| 107 runShaderTests(false); |
| 108 |
| 109 // Query the extension and store globally so shouldBe can access it |
| 110 ext = gl.getExtension("OES_standard_derivatives"); |
| 111 if (!ext) { |
| 112 testPassed("No OES_standard_derivatives support -- this is legal"); |
| 113 |
| 114 runSupportedTest(false); |
| 115 } else { |
| 116 testPassed("Successfully enabled OES_standard_derivatives extension"); |
| 117 |
| 118 runSupportedTest(true); |
| 119 |
| 120 runHintTestEnabled(); |
| 121 runShaderTests(true); |
| 122 runOutputTests(); |
| 123 runUniqueObjectTest(); |
| 124 } |
| 125 } |
| 126 |
| 127 function runSupportedTest(extensionEnabled) { |
| 128 var supported = gl.getSupportedExtensions(); |
| 129 if (supported.indexOf("OES_standard_derivatives") >= 0) { |
| 130 if (extensionEnabled) { |
| 131 testPassed("OES_standard_derivatives listed as supported and getExte
nsion succeeded"); |
| 132 } else { |
| 133 testFailed("OES_standard_derivatives listed as supported but getExte
nsion failed"); |
| 134 } |
| 135 } else { |
| 136 if (extensionEnabled) { |
| 137 testFailed("OES_standard_derivatives not listed as supported but get
Extension succeeded"); |
| 138 } else { |
| 139 testPassed("OES_standard_derivatives not listed as supported and get
Extension failed -- this is legal"); |
| 140 } |
| 141 } |
| 142 } |
| 143 |
| 144 function runHintTestDisabled() { |
| 145 debug("Testing FRAGMENT_SHADER_DERIVATIVE_HINT_OES with extension disabled")
; |
| 146 |
| 147 // Use the constant directly as we don't have the extension |
| 148 var FRAGMENT_SHADER_DERIVATIVE_HINT_OES = 0x8B8B; |
| 149 |
| 150 gl.getParameter(FRAGMENT_SHADER_DERIVATIVE_HINT_OES); |
| 151 glErrorShouldBe(gl, gl.INVALID_ENUM, "FRAGMENT_SHADER_DERIVATIVE_HINT_OES sh
ould not be queryable if extension is disabled"); |
| 152 |
| 153 gl.hint(FRAGMENT_SHADER_DERIVATIVE_HINT_OES, gl.DONT_CARE); |
| 154 glErrorShouldBe(gl, gl.INVALID_ENUM, "hint should not accept FRAGMENT_SHADER
_DERIVATIVE_HINT_OES if extension is disabled"); |
| 155 } |
| 156 |
| 157 function runHintTestEnabled() { |
| 158 debug("Testing FRAGMENT_SHADER_DERIVATIVE_HINT_OES with extension enabled"); |
| 159 |
| 160 shouldBe("ext.FRAGMENT_SHADER_DERIVATIVE_HINT_OES", "0x8B8B"); |
| 161 |
| 162 gl.getParameter(ext.FRAGMENT_SHADER_DERIVATIVE_HINT_OES); |
| 163 glErrorShouldBe(gl, gl.NO_ERROR, "FRAGMENT_SHADER_DERIVATIVE_HINT_OES query
should succeed if extension is enabled"); |
| 164 |
| 165 // Default value is DONT_CARE |
| 166 if (gl.getParameter(ext.FRAGMENT_SHADER_DERIVATIVE_HINT_OES) == gl.DONT_CARE
) { |
| 167 testPassed("Default value of FRAGMENT_SHADER_DERIVATIVE_HINT_OES is DONT
_CARE"); |
| 168 } else { |
| 169 testFailed("Default value of FRAGMENT_SHADER_DERIVATIVE_HINT_OES is not
DONT_CARE"); |
| 170 } |
| 171 |
| 172 // Ensure that we can set the target |
| 173 gl.hint(ext.FRAGMENT_SHADER_DERIVATIVE_HINT_OES, gl.DONT_CARE); |
| 174 glErrorShouldBe(gl, gl.NO_ERROR, "hint should accept FRAGMENT_SHADER_DERIVAT
IVE_HINT_OES"); |
| 175 |
| 176 // Test all the hint modes |
| 177 var validModes = ["FASTEST", "NICEST", "DONT_CARE"]; |
| 178 var anyFailed = false; |
| 179 for (var n = 0; n < validModes.length; n++) { |
| 180 var mode = validModes[n]; |
| 181 gl.hint(ext.FRAGMENT_SHADER_DERIVATIVE_HINT_OES, gl[mode]); |
| 182 if (gl.getParameter(ext.FRAGMENT_SHADER_DERIVATIVE_HINT_OES) != gl[mode]
) { |
| 183 testFailed("Round-trip of hint()/getParameter() failed on mode " + m
ode); |
| 184 anyFailed = true; |
| 185 } |
| 186 } |
| 187 if (!anyFailed) { |
| 188 testPassed("Round-trip of hint()/getParameter() with all supported modes
"); |
| 189 } |
| 190 } |
| 191 |
| 192 function runShaderTests(extensionEnabled) { |
| 193 debug(""); |
| 194 debug("Testing various shader compiles with extension " + (extensionEnabled
? "enabled" : "disabled")); |
| 195 |
| 196 // Expect the macro shader to succeed ONLY if enabled |
| 197 var macroFragmentProgram = wtu.loadProgramFromScriptExpectError(gl, "goodVer
texShader", "macroFragmentShader"); |
| 198 if (extensionEnabled) { |
| 199 if (macroFragmentProgram) { |
| 200 // Expected result |
| 201 testPassed("GL_OES_standard_derivatives defined in shaders when exte
nsion is enabled"); |
| 202 } else { |
| 203 testFailed("GL_OES_standard_derivatives not defined in shaders when
extension is enabled"); |
| 204 } |
| 205 } else { |
| 206 if (macroFragmentProgram) { |
| 207 testFailed("GL_OES_standard_derivatives defined in shaders when exte
nsion is disabled"); |
| 208 } else { |
| 209 testPassed("GL_OES_standard_derivatives not defined in shaders when
extension disabled"); |
| 210 } |
| 211 } |
| 212 |
| 213 // Always expect the shader missing the #pragma to fail (whether enabled or
not) |
| 214 var missingPragmaFragmentProgram = wtu.loadProgramFromScriptExpectError(gl,
"goodVertexShader", "missingPragmaFragmentShader"); |
| 215 if (missingPragmaFragmentProgram) { |
| 216 testFailed("Shader built-ins allowed without #extension pragma"); |
| 217 } else { |
| 218 testPassed("Shader built-ins disallowed without #extension pragma"); |
| 219 } |
| 220 |
| 221 // Try to compile a shader using the built-ins that should only succeed if e
nabled |
| 222 var testFragmentProgram = wtu.loadProgramFromScriptExpectError(gl, "goodVert
exShader", "testFragmentShader"); |
| 223 if (extensionEnabled) { |
| 224 if (testFragmentProgram) { |
| 225 testPassed("Shader built-ins compiled successfully when extension en
abled"); |
| 226 } else { |
| 227 testFailed("Shader built-ins failed to compile when extension enable
d"); |
| 228 } |
| 229 } else { |
| 230 if (testFragmentProgram) { |
| 231 testFailed("Shader built-ins compiled successfully when extension di
sabled"); |
| 232 } else { |
| 233 testPassed("Shader built-ins failed to compile when extension disabl
ed"); |
| 234 } |
| 235 } |
| 236 } |
| 237 |
| 238 function runOutputTests() { |
| 239 // This tests does several draws with various values of z. |
| 240 // The output of the fragment shader is: |
| 241 // [dFdx(z), dFdy(z), fwidth(z), 1.0] |
| 242 // The expected math: (note the conversion to uint8) |
| 243 // canvas.width = canvas.height = 50 |
| 244 // dFdx = totalChange.x / canvas.width = 0.5 / 50.0 = 0.01 |
| 245 // dFdy = totalChange.y / canvas.height = 0.5 / 50.0 = 0.01 |
| 246 // fw = abs(dFdx + dFdy) = 0.01 + 0.01 = 0.02 |
| 247 // r = floor(dFdx * 255) = 3 |
| 248 // g = floor(dFdy * 255) = 3 |
| 249 // b = floor(fw * 255) = 5 |
| 250 |
| 251 var e = 2; // Amount of variance to allow in result pixels - may need to be
tweaked higher |
| 252 |
| 253 debug("Testing various draws for valid built-in function behavior"); |
| 254 |
| 255 canvas.width = 50; canvas.height = 50; |
| 256 gl.viewport(0, 0, canvas.width, canvas.height); |
| 257 gl.hint(ext.FRAGMENT_SHADER_DERIVATIVE_HINT_OES, gl.NICEST); |
| 258 |
| 259 var shaders = [ |
| 260 wtu.loadShaderFromScript(gl, "outputVertexShader"), |
| 261 wtu.loadShaderFromScript(gl, "outputFragmentShader") |
| 262 ]; |
| 263 var program = wtu.setupProgram(gl, shaders, ['vPosition', 'texCoord0'], [0,
1]); |
| 264 var quadParameters = wtu.setupUnitQuad(gl, 0, 1); |
| 265 |
| 266 function readLocation(x, y) { |
| 267 var pixels = new Uint8Array(1 * 1 * 4); |
| 268 var px = Math.floor(x * canvas.width); |
| 269 var py = Math.floor(y * canvas.height); |
| 270 gl.readPixels(px, py, 1, 1, gl.RGBA, gl.UNSIGNED_BYTE, pixels); |
| 271 return pixels; |
| 272 }; |
| 273 function toString(arr) { |
| 274 var s = "["; |
| 275 for (var n = 0; n < arr.length; n++) { |
| 276 s += arr[n]; |
| 277 if (n < arr.length - 1) { |
| 278 s += ", "; |
| 279 } |
| 280 } |
| 281 return s + "]"; |
| 282 }; |
| 283 function expectResult(target, successMessage, failureMessage) { |
| 284 var locations = [ |
| 285 readLocation(0.1, 0.1), |
| 286 readLocation(0.9, 0.1), |
| 287 readLocation(0.1, 0.9), |
| 288 readLocation(0.9, 0.9), |
| 289 readLocation(0.5, 0.5) |
| 290 ]; |
| 291 var anyDiffer = false; |
| 292 for (var n = 0; n < locations.length; n++) { |
| 293 var source = locations[n]; |
| 294 for (var m = 0; m < 4; m++) { |
| 295 if (Math.abs(source[m] - target[m]) > e) { |
| 296 anyDiffer = true; |
| 297 testFailed(failureMessage + "; should be " + toString(target
) + ", was " + toString(source)); |
| 298 break; |
| 299 } |
| 300 } |
| 301 } |
| 302 if (!anyDiffer) { |
| 303 testPassed(successMessage); |
| 304 } |
| 305 }; |
| 306 |
| 307 function setupBuffers(tl, tr, bl, br) { |
| 308 gl.bindBuffer(gl.ARRAY_BUFFER, quadParameters[0]); |
| 309 gl.bufferData(gl.ARRAY_BUFFER, new Float32Array([ |
| 310 1.0, 1.0, tr, |
| 311 -1.0, 1.0, tl, |
| 312 -1.0, -1.0, bl, |
| 313 1.0, 1.0, tr, |
| 314 -1.0, -1.0, bl, |
| 315 1.0, -1.0, br]), gl.STATIC_DRAW); |
| 316 }; |
| 317 |
| 318 // Draw 1: (no variation) |
| 319 setupBuffers(0.0, 0.0, 0.0, 0.0); |
| 320 wtu.drawQuad(gl); |
| 321 expectResult([0, 0, 0, 255], |
| 322 "Draw 1 (no variation) returned the correct data", |
| 323 "Draw 1 (no variation) returned incorrect data"); |
| 324 |
| 325 // Draw 2: (variation in x) |
| 326 setupBuffers(1.0, 0.0, 1.0, 0.0); |
| 327 wtu.drawQuad(gl); |
| 328 expectResult([5, 0, 5, 255], |
| 329 "Draw 2 (variation in x) returned the correct data", |
| 330 "Draw 2 (variation in x) returned incorrect data"); |
| 331 |
| 332 // Draw 3: (variation in y) |
| 333 setupBuffers(1.0, 1.0, 0.0, 0.0); |
| 334 wtu.drawQuad(gl); |
| 335 expectResult([0, 5, 5, 255], |
| 336 "Draw 3 (variation in y) returned the correct data", |
| 337 "Draw 3 (variation in y) returned incorrect data"); |
| 338 |
| 339 // Draw 4: (variation in x & y) |
| 340 setupBuffers(1.0, 0.5, 0.5, 0.0); |
| 341 wtu.drawQuad(gl); |
| 342 expectResult([3, 3, 5, 255], |
| 343 "Draw 4 (variation in x & y) returned the correct data", |
| 344 "Draw 4 (variation in x & y) returned incorrect data"); |
| 345 |
| 346 } |
| 347 |
| 348 function attemptToForceGC() |
| 349 { |
| 350 var holderArray = []; |
| 351 var tempArray; |
| 352 window.tempArray = holderArray; |
| 353 for (var i = 0; i < 12; ++i) { |
| 354 tempArray = []; |
| 355 for (var j = 0; j < 1024 * 1024; ++j) { |
| 356 tempArray.push(0); |
| 357 } |
| 358 holderArray.push(tempArray); |
| 359 } |
| 360 window.tempArray = null; |
| 361 } |
| 362 |
| 363 function runUniqueObjectTest() |
| 364 { |
| 365 debug("Testing that getExtension() returns the same object each time"); |
| 366 gl.getExtension("OES_standard_derivatives").myProperty = 2; |
| 367 if (window.GCController) { |
| 368 window.GCController.collect(); |
| 369 } else if (window.opera && window.opera.collect) { |
| 370 window.opera.collect(); |
| 371 } else { |
| 372 attemptToForceGC(); |
| 373 } |
| 374 shouldBe('gl.getExtension("OES_standard_derivatives").myProperty', '2'); |
| 375 } |
| 376 |
| 377 |
| 378 debug(""); |
| 379 successfullyParsed = true; |
| 380 </script> |
| 381 <script src="../../resources/js-test-post.js"></script> |
| 382 |
| 383 </body> |
| 384 </html> |
OLD | NEW |