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