ext-depth-clamp.html (5225B)
1 <!-- 2 Copyright (c) 2023 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 7 <!DOCTYPE html> 8 <html> 9 <head> 10 <meta charset="utf-8"> 11 <title>WebGL EXT_depth_clamp 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 <canvas width="32" height="32" id="c"></canvas> 18 <div id="description"></div> 19 <div id="console"></div> 20 <script> 21 "use strict"; 22 description("This test verifies the functionality of the EXT_depth_clamp extension, if it is available."); 23 24 debug(""); 25 26 var wtu = WebGLTestUtils; 27 var gl = wtu.create3DContext("c"); 28 var ext; 29 const w = gl.drawingBufferWidth; 30 const h = gl.drawingBufferHeight; 31 32 function runTestNoExtension() { 33 debug(""); 34 debug("Check the parameter without the extension"); 35 shouldBeNull("gl.getParameter(0x864F /* DEPTH_CLAMP_EXT */)"); 36 wtu.glErrorShouldBe(gl, gl.INVALID_ENUM, "parameter unknown without enabling the extension"); 37 wtu.glErrorShouldBe(gl, gl.NO_ERROR, "should be no errors"); 38 39 debug("Check the cap without the extension"); 40 gl.disable(0x864F /* DEPTH_CLAMP_EXT */); 41 wtu.glErrorShouldBe(gl, gl.INVALID_ENUM, "cap unknown without enabling the extension"); 42 wtu.glErrorShouldBe(gl, gl.NO_ERROR, "should be no errors"); 43 44 gl.enable(0x864F /* DEPTH_CLAMP_EXT */); 45 wtu.glErrorShouldBe(gl, gl.INVALID_ENUM, "cap unknown without enabling the extension"); 46 wtu.glErrorShouldBe(gl, gl.NO_ERROR, "should be no errors"); 47 48 shouldBeFalse("gl.isEnabled(0x864F /* DEPTH_CLAMP_EXT */)"); 49 wtu.glErrorShouldBe(gl, gl.INVALID_ENUM, "cap unknown without enabling the extension"); 50 wtu.glErrorShouldBe(gl, gl.NO_ERROR, "should be no errors"); 51 } 52 53 function checkEnums() { 54 debug(""); 55 debug("Check enums"); 56 shouldBe("ext.DEPTH_CLAMP_EXT", "0x864F"); 57 } 58 59 function checkQueries() { 60 debug(""); 61 debug("Check default state"); 62 shouldBeFalse('gl.isEnabled(ext.DEPTH_CLAMP_EXT)'); 63 shouldBeFalse('gl.getParameter(ext.DEPTH_CLAMP_EXT)'); 64 wtu.glErrorShouldBe(gl, gl.NO_ERROR, "should be no errors"); 65 66 debug(""); 67 debug("Check state update using the new capability"); 68 gl.enable(ext.DEPTH_CLAMP_EXT); 69 wtu.glErrorShouldBe(gl, gl.NO_ERROR, "should be no errors"); 70 shouldBeTrue('gl.isEnabled(ext.DEPTH_CLAMP_EXT)'); 71 shouldBeTrue('gl.getParameter(ext.DEPTH_CLAMP_EXT)'); 72 wtu.glErrorShouldBe(gl, gl.NO_ERROR, "should be no errors"); 73 74 gl.disable(ext.DEPTH_CLAMP_EXT); 75 wtu.glErrorShouldBe(gl, gl.NO_ERROR, "should be no errors"); 76 shouldBeFalse('gl.isEnabled(ext.DEPTH_CLAMP_EXT)'); 77 shouldBeFalse('gl.getParameter(ext.DEPTH_CLAMP_EXT)'); 78 wtu.glErrorShouldBe(gl, gl.NO_ERROR, "should be no errors"); 79 } 80 81 function checkClamping() { 82 debug(""); 83 debug("Check depth clamp operation"); 84 85 // Draw a red quad located beyond the clipping volume. 86 // When depth clamping is enabled, it must be drawn. 87 const vertexShader = ` 88 attribute vec2 vPosition; 89 uniform float u_depth; 90 void main() { 91 gl_Position = vec4(vPosition, u_depth, 1.0); 92 }`; 93 const program = wtu.setupProgram(gl, [vertexShader, 94 wtu.simpleColorFragmentShader]); 95 gl.useProgram(program); 96 const colorLoc = gl.getUniformLocation(program, "u_color"); 97 const depthLoc = gl.getUniformLocation(program, "u_depth"); 98 99 wtu.setupUnitQuad(gl); 100 wtu.glErrorShouldBe(gl, gl.NO_ERROR, "should be no errors"); 101 102 // Red beyond the far plane 103 gl.uniform4f(colorLoc, 1, 0, 0, 1); 104 gl.uniform1f(depthLoc, +2); 105 106 // Clipped 107 wtu.clearAndDrawUnitQuad(gl); 108 wtu.checkCanvasRect(gl, 0, 0, w, h, [255, 255, 255, 255]); 109 110 // Enable depth clamping, disable depth clipping 111 gl.enable(ext.DEPTH_CLAMP_EXT); 112 113 // Not clipped 114 wtu.clearAndDrawUnitQuad(gl); 115 wtu.checkCanvasRect(gl, 0, 0, w, h, [255, 0, 0, 255]); 116 117 // Green beyond the near plane 118 gl.uniform4f(colorLoc, 0, 1, 0, 1); 119 gl.uniform1f(depthLoc, -2); 120 121 // Not clipped 122 wtu.clearAndDrawUnitQuad(gl); 123 wtu.checkCanvasRect(gl, 0, 0, w, h, [0, 255, 0, 255]); 124 125 // Blue beyond the near plane with clamping disabled 126 gl.disable(ext.DEPTH_CLAMP_EXT); 127 gl.uniform4f(colorLoc, 0, 0, 1, 1); 128 129 // Clipped 130 wtu.clearAndDrawUnitQuad(gl); 131 wtu.checkCanvasRect(gl, 0, 0, w, h, [255, 255, 255, 255]); 132 133 wtu.glErrorShouldBe(gl, gl.NO_ERROR, "should be no errors"); 134 } 135 136 function runTestExtension() { 137 checkEnums(); 138 checkQueries(); 139 checkClamping(); 140 } 141 142 function runTest() { 143 if (!gl) { 144 testFailed("WebGL context does not exist"); 145 return; 146 } 147 testPassed("WebGL context exists"); 148 149 runTestNoExtension(); 150 151 ext = gl.getExtension("EXT_depth_clamp"); 152 153 wtu.runExtensionSupportedTest(gl, "EXT_depth_clamp", ext !== null); 154 155 if (ext !== null) { 156 runTestExtension(); 157 } else { 158 testPassed("No EXT_depth_clamp support -- this is legal"); 159 } 160 } 161 162 runTest(); 163 164 var successfullyParsed = true; 165 </script> 166 <script src="../../js/js-test-post.js"></script> 167 </body> 168 </html>