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.
10 <meta charset=
"utf-8">
11 <title>WebGL WEBGL_polygon_mode 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 <canvas width=
"32" height=
"32" id=
"c"></canvas>
18 <div id=
"description"></div>
19 <div id=
"console"></div>
22 description("This test verifies the functionality of the WEBGL_polygon_mode extension, if it is available.");
26 var wtu
= WebGLTestUtils
;
27 var gl
= wtu
.create3DContext("c");
29 const w
= gl
.drawingBufferWidth
;
30 const h
= gl
.drawingBufferHeight
;
32 function runTestNoExtension() {
34 debug("Check the parameters without the extension");
35 shouldBeNull("gl.getParameter(0x0B40 /* POLYGON_MODE_WEBGL */)");
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 shouldBeNull("gl.getParameter(0x2A02 /* POLYGON_OFFSET_LINE_WEBGL */)");
39 wtu
.glErrorShouldBe(gl
, gl
.INVALID_ENUM
, "parameter unknown without enabling the extension");
40 wtu
.glErrorShouldBe(gl
, gl
.NO_ERROR
, "should be no errors");
42 debug("Check the cap without the extension");
43 gl
.disable(0x2A02 /* POLYGON_OFFSET_LINE_WEBGL */);
44 wtu
.glErrorShouldBe(gl
, gl
.INVALID_ENUM
, "cap unknown without enabling the extension");
45 wtu
.glErrorShouldBe(gl
, gl
.NO_ERROR
, "should be no errors");
47 gl
.enable(0x2A02 /* POLYGON_OFFSET_LINE_WEBGL */);
48 wtu
.glErrorShouldBe(gl
, gl
.INVALID_ENUM
, "cap unknown without enabling the extension");
49 wtu
.glErrorShouldBe(gl
, gl
.NO_ERROR
, "should be no errors");
51 shouldBeFalse("gl.isEnabled(0x2A02 /* POLYGON_OFFSET_LINE_WEBGL */)");
52 wtu
.glErrorShouldBe(gl
, gl
.INVALID_ENUM
, "cap unknown without enabling the extension");
53 wtu
.glErrorShouldBe(gl
, gl
.NO_ERROR
, "should be no errors");
56 function checkEnums() {
59 shouldBe("ext.POLYGON_MODE_WEBGL", "0x0B40");
60 shouldBe("ext.POLYGON_OFFSET_LINE_WEBGL", "0x2A02");
61 shouldBe("ext.LINE_WEBGL", "0x1B01");
62 shouldBe("ext.FILL_WEBGL", "0x1B02");
65 function checkQueries() {
67 debug("Check default state");
68 shouldBe('gl.getParameter(ext.POLYGON_MODE_WEBGL)', 'ext.FILL_WEBGL');
69 shouldBeFalse('gl.getParameter(ext.POLYGON_OFFSET_LINE_WEBGL)');
70 shouldBeFalse('gl.isEnabled(ext.POLYGON_OFFSET_LINE_WEBGL)');
72 debug("Check state updates");
73 ext
.polygonModeWEBGL(gl
.FRONT_AND_BACK
, ext
.LINE_WEBGL
);
74 shouldBe('gl.getParameter(ext.POLYGON_MODE_WEBGL)', 'ext.LINE_WEBGL');
75 ext
.polygonModeWEBGL(gl
.FRONT_AND_BACK
, ext
.FILL_WEBGL
);
76 shouldBe('gl.getParameter(ext.POLYGON_MODE_WEBGL)', 'ext.FILL_WEBGL');
78 debug("Check errors");
79 ext
.polygonModeWEBGL(gl
.FRONT
, ext
.LINE_WEBGL
);
80 wtu
.glErrorShouldBe(gl
, gl
.INVALID_ENUM
, "invalid face");
81 ext
.polygonModeWEBGL(gl
.FRONT_AND_BACK
, 0);
82 wtu
.glErrorShouldBe(gl
, gl
.INVALID_ENUM
, "invalid mode");
83 shouldBe('gl.getParameter(ext.POLYGON_MODE_WEBGL)', 'ext.FILL_WEBGL');
85 debug("Check cap updates");
86 gl
.enable(ext
.POLYGON_OFFSET_LINE_WEBGL
);
87 shouldBeTrue('gl.getParameter(ext.POLYGON_OFFSET_LINE_WEBGL)');
88 shouldBeTrue('gl.isEnabled(ext.POLYGON_OFFSET_LINE_WEBGL)');
89 gl
.disable(ext
.POLYGON_OFFSET_LINE_WEBGL
);
90 shouldBeFalse('gl.getParameter(ext.POLYGON_OFFSET_LINE_WEBGL)');
91 shouldBeFalse('gl.isEnabled(ext.POLYGON_OFFSET_LINE_WEBGL)');
92 wtu
.glErrorShouldBe(gl
, gl
.NO_ERROR
, "should be no errors");
95 function checkDiagonal(r
, g
, b
) {
96 const pixels
= new Uint8Array(w
* h
* 4);
97 gl
.readPixels(0, 0, w
, h
, gl
.RGBA
, gl
.UNSIGNED_BYTE
, pixels
);
98 for (let i
= 0; i
< w
; i
++)
100 const baseOffset
= (i
* w
+ i
) * 4;
101 if (pixels
[baseOffset
+ 0] != r
||
102 pixels
[baseOffset
+ 1] != g
||
103 pixels
[baseOffset
+ 2] != b
) {
104 testFailed(`Unexpected diagonal color at (${i}, ${i})`);
108 testPassed("Expected diagonal color");
111 function checkLineMode() {
113 debug("Check line polygon mode");
115 gl
.enable(gl
.DEPTH_TEST
);
117 const program
= wtu
.setupProgram(gl
, [wtu
.simpleVertexShader
,
118 wtu
.simpleColorFragmentShader
]);
119 gl
.useProgram(program
);
120 const colorLoc
= gl
.getUniformLocation(program
, "u_color");
122 wtu
.setupUnitQuad(gl
);
124 // Draw red quad with lines
125 gl
.uniform4f(colorLoc
, 1, 0, 0, 1);
126 ext
.polygonModeWEBGL(gl
.FRONT_AND_BACK
, ext
.LINE_WEBGL
);
127 wtu
.clearAndDrawUnitQuad(gl
);
129 // Nothing is drawn inside triangles
130 wtu
.checkCanvasRect(gl
, 2, 17, 13, 13, [255, 255, 255, 255]);
131 wtu
.checkCanvasRect(gl
, 17, 2, 13, 13, [255, 255, 255, 255]);
133 // Main diagonal is drawn
134 checkDiagonal(255, 0, 0);
136 // Test polygon offset
137 gl
.polygonOffset(0, -2);
138 gl
.enable(gl
.POLYGON_OFFSET_FILL
);
140 // Depth test must fail because line mode uses its own polygon offset toggle
141 gl
.uniform4f(colorLoc
, 0, 1, 0, 1);
142 wtu
.drawUnitQuad(gl
);
143 checkDiagonal(255, 0, 0);
145 // Depth test must pass
146 gl
.enable(ext
.POLYGON_OFFSET_LINE_WEBGL
)
147 wtu
.drawUnitQuad(gl
);
148 checkDiagonal(0, 255, 0);
150 wtu
.glErrorShouldBe(gl
, gl
.NO_ERROR
, "should be no errors");
153 function runTestExtension() {
161 testFailed("WebGL context does not exist");
164 testPassed("WebGL context exists");
166 runTestNoExtension();
168 ext
= gl
.getExtension("WEBGL_polygon_mode");
170 wtu
.runExtensionSupportedTest(gl
, "WEBGL_polygon_mode", ext
!== null);
175 testPassed("No WEBGL_polygon_mode support -- this is legal");
181 var successfullyParsed
= true;
183 <script src=
"../../js/js-test-post.js"></script>