Backed out changeset b462e7b742d8 (bug 1908261) for causing multiple reftest failures...
[gecko.git] / dom / canvas / test / webgl-conf / checkout / conformance / extensions / ext-frag-depth.html
blobee2a6df0cd707d049429aaf67a088fe7cc4b5fe2
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 -->
7 <!DOCTYPE html>
8 <html>
9 <head>
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>
15 </head>
16 <body>
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;
25 void main() {
26 gl_FragColor = vec4(1.0, 0.0, 0.0, 1.0);
27 gl_FragDepthEXT = 1.0;
29 </script>
31 <!-- Shader to test macro definition -->
32 <script id="macroFragmentShader" type="x-shader/x-fragment">
33 precision mediump float;
34 void main() {
35 #ifdef GL_EXT_frag_depth
36 gl_FragColor = vec4(1.0, 0.0, 0.0, 1.0);
37 #else
38 // Error expected
39 #error no GL_EXT_frag_depth;
40 #endif
42 </script>
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;
48 void main() {
49 gl_FragColor = vec4(1.0, 0.0, 0.0, 1.0);
50 gl_FragDepthEXT = 1.0;
52 </script>
53 <!-- Shaders to link with test fragment shaders -->
54 <script id="goodVertexShader" type="x-shader/x-vertex">
55 attribute vec4 vPosition;
56 void main() {
57 gl_Position = vPosition;
59 </script>
60 <!-- Shaders to test output -->
61 <script id="outputVertexShader" type="x-shader/x-vertex">
62 attribute vec4 vPosition;
63 void main() {
64 gl_Position = vPosition;
66 </script>
67 <script id="outputFragmentShader" type="x-shader/x-fragment">
68 #extension GL_EXT_frag_depth : enable
69 precision mediump float;
70 uniform float uDepth;
71 void main() {
72 gl_FragColor = vec4(1.0, 0.0, 0.0, 1.0);
73 gl_FragDepthEXT = uDepth;
75 </script>
77 <script>
78 "use strict";
79 description("This test verifies the functionality of the EXT_frag_depth extension, if it is available.");
81 debug("");
83 var wtu = WebGLTestUtils;
84 var canvas = document.getElementById("canvas");
85 var gl = wtu.create3DContext(canvas);
86 var ext = null;
88 // Run all tests once.
89 runAllTests();
91 // Run all tests against with a new context to test for any cache issues.
92 debug("");
93 debug("Testing new context to catch cache errors");
94 gl = wtu.create3DContext();
95 ext = null;
96 runAllTests();
98 function runAllTests() {
99 if (!gl) {
100 testFailed("WebGL context does not exist");
101 } else {
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");
108 if (!ext) {
109 testPassed("No EXT_frag_depth support -- this is legal");
111 runSupportedTest(false);
112 } else {
113 testPassed("Successfully enabled EXT_frag_depth extension");
115 runSupportedTest(true);
117 runShaderTests(true);
118 runOutputTests();
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");
133 } else {
134 testFailed("EXT_frag_depth listed as supported but getExtension failed");
136 } else {
137 if (extensionEnabled) {
138 testFailed("EXT_frag_depth not listed as supported but getExtension succeeded");
139 } else {
140 testPassed("EXT_frag_depth not listed as supported and getExtension failed -- this is legal");
145 function runShaderTests(extensionEnabled) {
146 debug("");
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) {
153 // Expected result
154 testPassed("GL_EXT_frag_depth defined in shaders when extension is enabled");
155 } else {
156 testFailed("GL_EXT_frag_depth not defined in shaders when extension is enabled");
158 } else {
159 if (macroFragmentProgram) {
160 testFailed("GL_EXT_frag_depth defined in shaders when extension is disabled");
161 } else {
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");
170 } else {
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");
179 } else {
180 testFailed("Shader built-ins failed to compile when extension enabled");
182 } else {
183 if (testFragmentProgram) {
184 testFailed("Shader built-ins compiled successfully when extension disabled");
185 } else {
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
202 gl.clearDepth(0.5);
203 gl.enable(gl.DEPTH_TEST);
205 var positionLoc = 0;
206 var texcoordLoc = 1;
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");
225 ext = null;
226 gl.getExtension("EXT_frag_depth").myProperty = 2;
227 webglHarnessCollectGarbage();
228 shouldBe('gl.getExtension("EXT_frag_depth").myProperty', '2');
231 function runDeferredLinkTests() {
232 debug("");
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");
242 if (!extEnabled) {
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.");
251 return;
254 var program = wtu.setupProgram(glEnabled, [vertexShader, fragmentShader]);
256 if (!program) {
257 testFailed("Compilation with extension enabled failed.");
258 return;
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.");
269 return;
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]);
275 if (program) {
276 testFailed("Compilation with extension disabled then linking with extension enabled should have failed.");
277 return;
280 testPassed("Compilation with extension disabled then linking with extension enabled.");
283 debug("");
284 var successfullyParsed = true;
285 </script>
286 <script src="../../js/js-test-post.js"></script>
288 </body>
289 </html>