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.
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>
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;
28 uniform sampler2D tex;
30 vec4 color = texture2DLodEXT(tex, texCoord0v, lod);
35 <!-- Shader to test macro definition -->
36 <script id=
"macroFragmentShader" type=
"x-shader/x-fragment">
37 precision mediump float;
39 #ifdef GL_EXT_shader_texture_lod
40 gl_FragColor = vec4(
0.0,
0.0,
0.0,
0.0);
43 #error no GL_EXT_shader_texture_lod;
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;
54 uniform sampler2D tex;
56 vec4 color = texture2DLodEXT(tex, texCoord0v, lod);
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;
67 texCoord0v = texCoord0;
68 gl_Position = vPosition;
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;
78 texCoord0v = texCoord0;
79 gl_Position = vPosition;
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;
87 uniform sampler2D tex;
89 vec4 color = texture2DLodEXT(tex, texCoord0v, lod);
95 description("This test verifies the functionality of the EXT_shader_texture_lod extension, if it is available.");
99 var wtu
= WebGLTestUtils
;
100 var canvas
= document
.getElementById("canvas");
101 var gl
= wtu
.create3DContext(canvas
);
104 // Run all tests once.
107 // Run all tests against with a new context to test for any cache issues.
109 debug("Testing new context to catch cache errors");
110 var canvas2
= document
.getElementById("canvas2");
111 gl
= wtu
.create3DContext(canvas2
);
115 function runAllTests() {
117 testFailed("WebGL context does not exist");
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");
127 testPassed("No EXT_shader_texture_lod support -- this is legal");
129 runSupportedTest(false);
131 testPassed("Successfully enabled EXT_shader_texture_lod extension");
133 runSupportedTest(true);
135 runShaderTests(true);
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");
153 testFailed("EXT_shader_texture_lod listed as supported but getExtension failed");
156 if (extensionEnabled
) {
157 testFailed("EXT_shader_texture_lod not listed as supported but getExtension succeeded");
159 testPassed("EXT_shader_texture_lod not listed as supported and getExtension failed -- this is legal");
164 function runShaderTests(extensionEnabled
) {
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
) {
173 testPassed("GL_EXT_shader_texture_lod defined in shaders when extension is enabled");
175 testFailed("GL_EXT_shader_texture_lod not defined in shaders when extension is enabled");
178 if (macroFragmentProgram
) {
179 testFailed("GL_EXT_shader_texture_lod defined in shaders when extension is disabled");
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");
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");
199 testFailed("Shader built-ins failed to compile when extension enabled");
202 if (testFragmentProgram
) {
203 testFailed("Shader built-ins compiled successfully when extension disabled");
205 testPassed("Shader built-ins failed to compile when extension disabled");
210 function runOutputTests() {
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);
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
);
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()
261 debug("Testing that getExtension() returns the same object each time");
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.
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
279 // create a self-cycle on the extension object
283 function runDeferredLinkTests() {
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");
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.");
306 var program
= wtu
.setupProgram(glEnabled
, [vertexShader
, fragmentShader
]);
309 testFailed("Compilation with extension enabled failed.");
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.");
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
]);
328 testFailed("Compilation with extension disabled then linking with extension enabled should have failed.");
332 testPassed("Compilation with extension disabled then linking with extension enabled.");
336 successfullyParsed
= true;
338 <script src=
"../../js/js-test-post.js"></script>