Backed out changeset b462e7b742d8 (bug 1908261) for causing multiple reftest failures...
[gecko.git] / dom / canvas / test / webgl-conf / checkout / conformance / extensions / ext-shader-texture-lod.html
blob30176bd59037a6de140e1bbe1d67ccafe905b285
1 <!--
2 Copyright (c) 2019 The Khronos Group Inc.
3 Use of this source code is governed by an MIT-style license that can be
4 found in the LICENSE.txt file.
5 -->
6 <!DOCTYPE html>
7 <html>
8 <head>
9 <meta charset="utf-8">
10 <title>WebGL EXT_shader_texture_lod Conformance Tests</title>
11 <link rel="stylesheet" href="../../resources/js-test-style.css"/>
12 <script src="../../js/desktop-gl-constants.js"></script>
13 <script src="../../js/js-test-pre.js"></script>
14 <script src="../../js/webgl-test-utils.js"></script>
15 </head>
16 <body>
17 <div id="description"></div>
18 <canvas id="canvas" style="width: 256px; height: 256px;"> </canvas>
19 <canvas id="canvas2" style="width: 256px; height: 256px;"> </canvas>
20 <div id="console"></div>
21 <!-- Shaders for testing texture LOD functions -->
23 <!-- Shader omitting the required #extension pragma -->
24 <script id="missingPragmaFragmentShader" type="x-shader/x-fragment">
25 precision mediump float;
26 varying vec2 texCoord0v;
27 uniform float lod;
28 uniform sampler2D tex;
29 void main() {
30 vec4 color = texture2DLodEXT(tex, texCoord0v, lod);
31 gl_FragColor = color;
33 </script>
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_EXT_shader_texture_lod
40 gl_FragColor = vec4(0.0, 0.0, 0.0, 0.0);
41 #else
42 // Error expected
43 #error no GL_EXT_shader_texture_lod;
44 #endif
46 </script>
48 <!-- Shader with required #extension pragma -->
49 <script id="testFragmentShader" type="x-shader/x-fragment">
50 #extension GL_EXT_shader_texture_lod : enable
51 precision mediump float;
52 varying vec2 texCoord0v;
53 uniform float lod;
54 uniform sampler2D tex;
55 void main() {
56 vec4 color = texture2DLodEXT(tex, texCoord0v, lod);
57 gl_FragColor = color;
59 </script>
61 <!-- Shaders to link with test fragment shaders -->
62 <script id="goodVertexShader" type="x-shader/x-vertex">
63 attribute vec4 vPosition;
64 attribute vec2 texCoord0;
65 varying vec2 texCoord0v;
66 void main() {
67 texCoord0v = texCoord0;
68 gl_Position = vPosition;
70 </script>
72 <!-- Shaders to test output -->
73 <script id="outputVertexShader" type="x-shader/x-vertex">
74 attribute vec4 vPosition;
75 attribute vec2 texCoord0;
76 varying vec2 texCoord0v;
77 void main() {
78 texCoord0v = texCoord0;
79 gl_Position = vPosition;
81 </script>
82 <script id="outputFragmentShader" type="x-shader/x-fragment">
83 #extension GL_EXT_shader_texture_lod : require
84 precision mediump float;
85 varying vec2 texCoord0v;
86 uniform float lod;
87 uniform sampler2D tex;
88 void main() {
89 vec4 color = texture2DLodEXT(tex, texCoord0v, lod);
90 gl_FragColor = color;
92 </script>
94 <script>
95 description("This test verifies the functionality of the EXT_shader_texture_lod extension, if it is available.");
97 debug("");
99 var wtu = WebGLTestUtils;
100 var canvas = document.getElementById("canvas");
101 var gl = wtu.create3DContext(canvas);
102 var ext = null;
104 // Run all tests once.
105 runAllTests();
107 // Run all tests against with a new context to test for any cache issues.
108 debug("");
109 debug("Testing new context to catch cache errors");
110 var canvas2 = document.getElementById("canvas2");
111 gl = wtu.create3DContext(canvas2);
112 ext = null;
113 runAllTests();
115 function runAllTests() {
116 if (!gl) {
117 testFailed("WebGL context does not exist");
118 } else {
119 testPassed("WebGL context exists");
121 // Run tests with extension disabled
122 runShaderTests(false);
124 // Query the extension and store globally so shouldBe can access it
125 ext = gl.getExtension("EXT_shader_texture_lod");
126 if (!ext) {
127 testPassed("No EXT_shader_texture_lod support -- this is legal");
129 runSupportedTest(false);
130 } else {
131 testPassed("Successfully enabled EXT_shader_texture_lod extension");
133 runSupportedTest(true);
135 runShaderTests(true);
136 runOutputTests();
137 runUniqueObjectTest();
138 runReferenceCycleTest();
140 // Run deferred link tests.
141 runDeferredLinkTests();
147 function runSupportedTest(extensionEnabled) {
148 var supported = gl.getSupportedExtensions();
149 if (supported.indexOf("EXT_shader_texture_lod") >= 0) {
150 if (extensionEnabled) {
151 testPassed("EXT_shader_texture_lod listed as supported and getExtension succeeded");
152 } else {
153 testFailed("EXT_shader_texture_lod listed as supported but getExtension failed");
155 } else {
156 if (extensionEnabled) {
157 testFailed("EXT_shader_texture_lod not listed as supported but getExtension succeeded");
158 } else {
159 testPassed("EXT_shader_texture_lod not listed as supported and getExtension failed -- this is legal");
164 function runShaderTests(extensionEnabled) {
165 debug("");
166 debug("Testing various shader compiles with extension " + (extensionEnabled ? "enabled" : "disabled"));
168 // Expect the macro shader to succeed ONLY if enabled
169 var macroFragmentProgram = wtu.loadProgramFromScriptExpectError(gl, "goodVertexShader", "macroFragmentShader");
170 if (extensionEnabled) {
171 if (macroFragmentProgram) {
172 // Expected result
173 testPassed("GL_EXT_shader_texture_lod defined in shaders when extension is enabled");
174 } else {
175 testFailed("GL_EXT_shader_texture_lod not defined in shaders when extension is enabled");
177 } else {
178 if (macroFragmentProgram) {
179 testFailed("GL_EXT_shader_texture_lod defined in shaders when extension is disabled");
180 } else {
181 testPassed("GL_EXT_shader_texture_lod not defined in shaders when extension disabled");
185 // Always expect the shader missing the #pragma to fail (whether enabled or not)
186 var missingPragmaFragmentProgram = wtu.loadProgramFromScriptExpectError(gl, "goodVertexShader", "missingPragmaFragmentShader");
187 if (missingPragmaFragmentProgram) {
188 testFailed("Shader built-ins allowed without #extension pragma");
189 } else {
190 testPassed("Shader built-ins disallowed without #extension pragma");
193 // Try to compile a shader using the built-ins that should only succeed if enabled
194 var testFragmentProgram = wtu.loadProgramFromScriptExpectError(gl, "goodVertexShader", "testFragmentShader");
195 if (extensionEnabled) {
196 if (testFragmentProgram) {
197 testPassed("Shader built-ins compiled successfully when extension enabled");
198 } else {
199 testFailed("Shader built-ins failed to compile when extension enabled");
201 } else {
202 if (testFragmentProgram) {
203 testFailed("Shader built-ins compiled successfully when extension disabled");
204 } else {
205 testPassed("Shader built-ins failed to compile when extension disabled");
210 function runOutputTests() {
211 debug("");
212 debug("Testing various draws for valid built-in function behavior");
213 gl.viewport(0, 0, canvas.width, canvas.height);
215 var program = wtu.setupProgram(gl, ["outputVertexShader", "outputFragmentShader"], ['vPosition', 'texCoord0'], [0, 1]);
216 var quadParameters = wtu.setupUnitQuad(gl, 0, 1);
218 var colors = [
219 {name: 'red', color:[255, 0, 0, 255]},
220 {name: 'green', color:[0, 255, 0, 255]},
221 {name: 'blue', color:[0, 0, 255, 255]},
222 {name: 'yellow', color:[255, 255, 0, 255]},
223 {name: 'magenta', color:[255, 0, 255, 255]},
224 {name: 'cyan', color:[0, 255, 255, 255]},
225 {name: 'pink', color:[255, 128, 128, 255]},
226 {name: 'gray', color:[128, 128, 128, 255]},
227 {name: 'light green', color:[128, 255, 128, 255]},
230 var tex = gl.createTexture();
231 gl.bindTexture(gl.TEXTURE_2D, tex);
232 gl.texParameteri(gl.TEXTURE_2D, gl.TEXTURE_MIN_FILTER, gl.NEAREST_MIPMAP_LINEAR);
233 gl.texParameteri(gl.TEXTURE_2D, gl.TEXTURE_MAG_FILTER, gl.LINEAR);
234 gl.texParameteri(gl.TEXTURE_2D, gl.TEXTURE_WRAP_S, gl.REPEAT);
235 gl.texParameteri(gl.TEXTURE_2D, gl.TEXTURE_WRAP_T, gl.REPEAT);
237 for (var ii = 0; ii < colors.length; ++ii) {
238 var color = colors[ii];
239 var size = Math.pow(2, colors.length - ii - 1);
240 wtu.fillTexture(gl, tex, size, size, color.color, ii);
243 var loc = gl.getUniformLocation(program, "lod");
245 for (var ii = 0; ii < colors.length; ++ii) {
246 gl.uniform1f(loc, ii);
247 var color = colors[ii];
248 wtu.drawUnitQuad(gl);
249 wtu.checkCanvas(
250 gl, color.color,
251 "256x256 texture drawn to 256x256 dest with lod = " + ii +
252 " should be " + color.name);
255 wtu.glErrorShouldBe(gl, gl.NO_ERROR);
258 function runUniqueObjectTest()
260 debug("");
261 debug("Testing that getExtension() returns the same object each time");
262 ext = null;
263 gl.getExtension("EXT_shader_texture_lod").myProperty = 2;
264 webglHarnessCollectGarbage();
265 shouldBe('gl.getExtension("EXT_shader_texture_lod").myProperty', '2');
268 function runReferenceCycleTest()
270 // create some reference cycles. The goal is to see if they cause leaks. The point is that
271 // some browser test runners have instrumentation to detect leaked refcounted objects.
272 debug("");
273 debug("Testing reference cycles between context and extension objects");
274 var ext = gl.getExtension("EXT_shader_texture_lod");
276 // create cycle between extension and context, since the context has to hold a reference to the extension
277 ext.context = gl;
279 // create a self-cycle on the extension object
280 ext.ext = ext;
283 function runDeferredLinkTests() {
284 debug("");
285 debug("Testing deferred shader compilation tests.");
287 // Test for compilation failures that are caused by missing extensions
288 // do not succeed if extensions are enabled during linking. This would
289 // only happen for deferred shader compilations.
291 // First test if link succeeds with extension enabled.
292 var glEnabled = wtu.create3DContext();
293 var extEnabled = glEnabled.getExtension("EXT_shader_texture_lod");
294 if (!extEnabled) {
295 testFailed("Deferred link test expects the extension to be supported");
298 var vertexShader = wtu.loadShaderFromScript(glEnabled, "goodVertexShader");
299 var fragmentShader = wtu.loadShaderFromScript(glEnabled, "macroFragmentShader");
301 if (!vertexShader || !fragmentShader) {
302 testFailed("Could not create good shaders.");
303 return;
306 var program = wtu.setupProgram(glEnabled, [vertexShader, fragmentShader]);
308 if (!program) {
309 testFailed("Compilation with extension enabled failed.");
310 return;
313 // Create new context to test link failure without extension enabled.
314 var glDeferred = wtu.create3DContext();
316 var vertexShader = wtu.loadShaderFromScript(glDeferred, "goodVertexShader", glDeferred.VERTEX_SHADER, undefined, undefined, true);
317 var fragmentShader = wtu.loadShaderFromScript(glDeferred, "macroFragmentShader", glDeferred.FRAGMENT_SHADER, undefined, undefined, true);
319 if (vertexShader == null || fragmentShader == null) {
320 testFailed("Could not create shaders.");
321 return;
324 // Shader compilations should have failed due to extensions not enabled.
325 glDeferred.getExtension("EXT_shader_texture_lod");
326 var program = wtu.setupProgram(glDeferred, [vertexShader, fragmentShader]);
327 if (program) {
328 testFailed("Compilation with extension disabled then linking with extension enabled should have failed.");
329 return;
332 testPassed("Compilation with extension disabled then linking with extension enabled.");
335 debug("");
336 successfullyParsed = true;
337 </script>
338 <script src="../../js/js-test-post.js"></script>
340 </body>
341 </html>