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 <meta charset=
"utf-8">
11 <title>WebGL EXT_frag_depth Conformance Tests
</title>
12 <link rel=
"stylesheet" href=
"../../resources/js-test-style.css"/>
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: 50px; height: 50px;"> </canvas>
19 <div id=
"console"></div>
20 <!-- Shaders for testing fragment depth writing -->
22 <!-- Shader omitting the required #extension pragma -->
23 <script id=
"missingPragmaFragmentShader" type=
"x-shader/x-fragment">
24 precision mediump float;
26 gl_FragColor = vec4(
1.0,
0.0,
0.0,
1.0);
27 gl_FragDepthEXT =
1.0;
31 <!-- Shader to test macro definition -->
32 <script id=
"macroFragmentShader" type=
"x-shader/x-fragment">
33 precision mediump float;
35 #ifdef GL_EXT_frag_depth
36 gl_FragColor = vec4(
1.0,
0.0,
0.0,
1.0);
39 #error no GL_EXT_frag_depth;
44 <!-- Shader with required #extension pragma -->
45 <script id=
"testFragmentShader" type=
"x-shader/x-fragment">
46 #extension GL_EXT_frag_depth : enable
47 precision mediump float;
49 gl_FragColor = vec4(
1.0,
0.0,
0.0,
1.0);
50 gl_FragDepthEXT =
1.0;
53 <!-- Shaders to link with test fragment shaders -->
54 <script id=
"goodVertexShader" type=
"x-shader/x-vertex">
55 attribute vec4 vPosition;
57 gl_Position = vPosition;
60 <!-- Shaders to test output -->
61 <script id=
"outputVertexShader" type=
"x-shader/x-vertex">
62 attribute vec4 vPosition;
64 gl_Position = vPosition;
67 <script id=
"outputFragmentShader" type=
"x-shader/x-fragment">
68 #extension GL_EXT_frag_depth : enable
69 precision mediump float;
72 gl_FragColor = vec4(
1.0,
0.0,
0.0,
1.0);
73 gl_FragDepthEXT = uDepth;
79 description("This test verifies the functionality of the EXT_frag_depth extension, if it is available.");
83 var wtu
= WebGLTestUtils
;
84 var canvas
= document
.getElementById("canvas");
85 var gl
= wtu
.create3DContext(canvas
);
88 // Run all tests once.
91 // Run all tests against with a new context to test for any cache issues.
93 debug("Testing new context to catch cache errors");
94 gl
= wtu
.create3DContext();
98 function runAllTests() {
100 testFailed("WebGL context does not exist");
102 testPassed("WebGL context exists");
104 runShaderTests(false);
106 // Query the extension and store globally so shouldBe can access it
107 ext
= wtu
.getExtensionWithKnownPrefixes(gl
, "EXT_frag_depth");
109 testPassed("No EXT_frag_depth support -- this is legal");
111 runSupportedTest(false);
113 testPassed("Successfully enabled EXT_frag_depth extension");
115 runSupportedTest(true);
117 runShaderTests(true);
119 runUniqueObjectTest();
121 // Run deferred link tests.
122 runDeferredLinkTests();
128 function runSupportedTest(extensionEnabled
) {
129 var supported
= gl
.getSupportedExtensions();
130 if (supported
.indexOf("EXT_frag_depth") >= 0) {
131 if (extensionEnabled
) {
132 testPassed("EXT_frag_depth listed as supported and getExtension succeeded");
134 testFailed("EXT_frag_depth listed as supported but getExtension failed");
137 if (extensionEnabled
) {
138 testFailed("EXT_frag_depth not listed as supported but getExtension succeeded");
140 testPassed("EXT_frag_depth not listed as supported and getExtension failed -- this is legal");
145 function runShaderTests(extensionEnabled
) {
147 debug("Testing various shader compiles with extension " + (extensionEnabled
? "enabled" : "disabled"));
149 // Expect the macro shader to succeed ONLY if enabled
150 var macroFragmentProgram
= wtu
.loadProgramFromScriptExpectError(gl
, "goodVertexShader", "macroFragmentShader");
151 if (extensionEnabled
) {
152 if (macroFragmentProgram
) {
154 testPassed("GL_EXT_frag_depth defined in shaders when extension is enabled");
156 testFailed("GL_EXT_frag_depth not defined in shaders when extension is enabled");
159 if (macroFragmentProgram
) {
160 testFailed("GL_EXT_frag_depth defined in shaders when extension is disabled");
162 testPassed("GL_EXT_frag_depth not defined in shaders when extension disabled");
166 // Always expect the shader missing the #pragma to fail (whether enabled or not)
167 var missingPragmaFragmentProgram
= wtu
.loadProgramFromScriptExpectError(gl
, "goodVertexShader", "missingPragmaFragmentShader");
168 if (missingPragmaFragmentProgram
) {
169 testFailed("Shader built-ins allowed without #extension pragma");
171 testPassed("Shader built-ins disallowed without #extension pragma");
174 // Try to compile a shader using the built-ins that should only succeed if enabled
175 var testFragmentProgram
= wtu
.loadProgramFromScriptExpectError(gl
, "goodVertexShader", "testFragmentShader");
176 if (extensionEnabled
) {
177 if (testFragmentProgram
) {
178 testPassed("Shader built-ins compiled successfully when extension enabled");
180 testFailed("Shader built-ins failed to compile when extension enabled");
183 if (testFragmentProgram
) {
184 testFailed("Shader built-ins compiled successfully when extension disabled");
186 testPassed("Shader built-ins failed to compile when extension disabled");
191 function runOutputTests() {
192 var e
= 2; // Amount of variance to allow in result pixels - may need to be tweaked higher
194 debug("Testing various draws for valid built-in function behavior");
196 canvas
.width
= 50; canvas
.height
= 50;
197 gl
.viewport(0, 0, canvas
.width
, canvas
.height
);
199 // Enable depth testing with a clearDepth of 0.5
200 // This makes it so that fragments are only rendered when
201 // gl_fragDepthEXT is < 0.5
203 gl
.enable(gl
.DEPTH_TEST
);
207 var program
= wtu
.setupProgram(gl
, ["outputVertexShader", "outputFragmentShader"], ['vPosition'], [0]);
208 var quadParameters
= wtu
.setupUnitQuad(gl
, 0, 1);
209 var depthUniform
= gl
.getUniformLocation(program
, "uDepth");
211 // Draw 1: Greater than clear depth
212 gl
.uniform1f(depthUniform
, 1.0);
213 wtu
.clearAndDrawUnitQuad(gl
);
214 wtu
.checkCanvasRect(gl
, 0, 0, canvas
.width
, canvas
.height
, [255, 255, 255, 255]);
216 // Draw 2: Less than clear depth
217 gl
.uniform1f(depthUniform
, 0.0);
218 wtu
.clearAndDrawUnitQuad(gl
);
219 wtu
.checkCanvasRect(gl
, 0, 0, canvas
.width
, canvas
.height
, [255, 0, 0, 255]);
222 function runUniqueObjectTest()
224 debug("Testing that getExtension() returns the same object each time");
226 gl
.getExtension("EXT_frag_depth").myProperty
= 2;
227 webglHarnessCollectGarbage();
228 shouldBe('gl.getExtension("EXT_frag_depth").myProperty', '2');
231 function runDeferredLinkTests() {
233 debug("Testing deferred shader compilation tests.");
235 // Test for compilation failures that are caused by missing extensions
236 // do not succeed if extensions are enabled during linking. This would
237 // only happen for deferred shader compilations.
239 // First test if link succeeds with extension enabled.
240 var glEnabled
= wtu
.create3DContext();
241 var extEnabled
= glEnabled
.getExtension("EXT_frag_depth");
243 testFailed("Deferred link test expects the extension to be supported");
246 var vertexShader
= wtu
.loadShaderFromScript(glEnabled
, "goodVertexShader");
247 var fragmentShader
= wtu
.loadShaderFromScript(glEnabled
, "macroFragmentShader");
249 if (!vertexShader
|| !fragmentShader
) {
250 testFailed("Could not create good shaders.");
254 var program
= wtu
.setupProgram(glEnabled
, [vertexShader
, fragmentShader
]);
257 testFailed("Compilation with extension enabled failed.");
261 // Create new context to test link failure without extension enabled.
262 var glDeferred
= wtu
.create3DContext();
264 var vertexShader
= wtu
.loadShaderFromScript(glDeferred
, "goodVertexShader", glDeferred
.VERTEX_SHADER
, undefined, undefined, true);
265 var fragmentShader
= wtu
.loadShaderFromScript(glDeferred
, "macroFragmentShader", glDeferred
.FRAGMENT_SHADER
, undefined, undefined, true);
267 if (vertexShader
== null || fragmentShader
== null) {
268 testFailed("Could not create shaders.");
272 // Shader compilations should have failed due to extensions not enabled.
273 glDeferred
.getExtension("EXT_frag_depth");
274 var program
= wtu
.setupProgram(glDeferred
, [vertexShader
, fragmentShader
]);
276 testFailed("Compilation with extension disabled then linking with extension enabled should have failed.");
280 testPassed("Compilation with extension disabled then linking with extension enabled.");
284 var successfullyParsed
= true;
286 <script src=
"../../js/js-test-post.js"></script>