summaryrefslogtreecommitdiffstats
path: root/dom/canvas/test/webgl-conf/checkout/conformance2/extensions/webgl-clip-cull-distance.html
blob: cb2253c326c8056ef0bf4b99a3fe2caf78477c2f (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
<!--
Copyright (c) 2022 The Khronos Group Inc.
Use of this source code is governed by an MIT-style license that can be
found in the LICENSE.txt file.
-->

<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<title>WebGL WEBGL_clip_cull_distance Conformance Tests</title>
<LINK rel="stylesheet" href="../../resources/js-test-style.css"/>
<script src="../../js/js-test-pre.js"></script>
<script src="../../js/webgl-test-utils.js"></script>
</head>
<body>
<canvas width="32" height="32" id="c"></canvas>
<div id="description"></div>
<div id="console"></div>
<script>
"use strict";
description("This test verifies the functionality of the WEBGL_clip_cull_distance extension, if it is available.");

debug("");

var wtu = WebGLTestUtils;
var gl = wtu.create3DContext("c", null, 2);
var ext;
const w = gl.drawingBufferWidth;
const h = gl.drawingBufferHeight;

function runTestNoExtension() {
  debug("");
  debug("Check parameters and capabilities without the extension");

  shouldBeNull("gl.getParameter(0x0D32 /* MAX_CLIP_DISTANCES_WEBGL */)");
  wtu.glErrorShouldBe(gl, gl.INVALID_ENUM, "parameter unknown without enabling the extension");
  shouldBeNull("gl.getParameter(0x82F9 /* MAX_CULL_DISTANCES_WEBGL */)");
  wtu.glErrorShouldBe(gl, gl.INVALID_ENUM, "parameter unknown without enabling the extension");
  shouldBeNull("gl.getParameter(0x82FA /* MAX_COMBINED_CLIP_AND_CULL_DISTANCES_WEBGL */)");
  wtu.glErrorShouldBe(gl, gl.INVALID_ENUM, "parameter unknown without enabling the extension");

  wtu.glErrorShouldBe(gl, gl.NO_ERROR, "should be no errors");

  const assertState = (i) => {
    shouldBeFalse(`gl.isEnabled(${0x3000 + i} /* CLIP_DISTANCE${i}_WEBGL */)`);
    wtu.glErrorShouldBe(gl, gl.INVALID_ENUM, "parameter unknown without enabling the extension");

    shouldBeNull(`gl.getParameter(${0x3000 + i} /* CLIP_DISTANCE${i}_WEBGL */)`);
    wtu.glErrorShouldBe(gl, gl.INVALID_ENUM, "parameter unknown without enabling the extension");
  };

  for (let i = 0; i < 8; i++) {
    assertState(i);

    gl.enable(0x3000 + i /* CLIP_DISTANCEi_WEBGL */);
    wtu.glErrorShouldBe(gl, gl.INVALID_ENUM, "capability unknown without enabling the extension");

    assertState(i);

    gl.disable(0x3000 + i /* CLIP_DISTANCEi_WEBGL */);
    wtu.glErrorShouldBe(gl, gl.INVALID_ENUM, "capability unknown without enabling the extension");

    assertState(i);
  }

  wtu.glErrorShouldBe(gl, gl.NO_ERROR, "should be no errors");
  debug("");
}

function checkEnums() {
  debug("");
  debug("Check enums");
  shouldBe("ext.MAX_CLIP_DISTANCES_WEBGL", "0x0D32");
  shouldBe("ext.MAX_CULL_DISTANCES_WEBGL", "0x82F9");
  shouldBe("ext.MAX_COMBINED_CLIP_AND_CULL_DISTANCES_WEBGL", "0x82FA");
  shouldBe("ext.CLIP_DISTANCE0_WEBGL", "0x3000");
  shouldBe("ext.CLIP_DISTANCE1_WEBGL", "0x3001");
  shouldBe("ext.CLIP_DISTANCE2_WEBGL", "0x3002");
  shouldBe("ext.CLIP_DISTANCE3_WEBGL", "0x3003");
  shouldBe("ext.CLIP_DISTANCE4_WEBGL", "0x3004");
  shouldBe("ext.CLIP_DISTANCE5_WEBGL", "0x3005");
  shouldBe("ext.CLIP_DISTANCE6_WEBGL", "0x3006");
  shouldBe("ext.CLIP_DISTANCE7_WEBGL", "0x3007");
}

function checkQueries() {
  debug("");
  debug("Check parameters");
  shouldBeGreaterThanOrEqual('gl.getParameter(ext.MAX_CLIP_DISTANCES_WEBGL)', '8');
  wtu.glErrorShouldBe(gl, gl.NO_ERROR, "should be no errors");

  const maxCullDistances = gl.getParameter(ext.MAX_CULL_DISTANCES_WEBGL);
  wtu.glErrorShouldBe(gl, gl.NO_ERROR, "should be no errors");

  if (maxCullDistances == 0) {
    testPassed("No cull distance support");
    shouldBe("gl.getParameter(ext.MAX_COMBINED_CLIP_AND_CULL_DISTANCES_WEBGL)", "0");
    wtu.glErrorShouldBe(gl, gl.NO_ERROR, "should be no errors");
  } else if (maxCullDistances >= 8) {
    testPassed("Optional cull distance support");
    shouldBeGreaterThanOrEqual("gl.getParameter(ext.MAX_COMBINED_CLIP_AND_CULL_DISTANCES_WEBGL)", "8");
    wtu.glErrorShouldBe(gl, gl.NO_ERROR, "should be no errors");
  } else {
    testFailed("Invalid number of supported cull distances");
  }

  debug("");
  debug("Check clip distance capabilities");

  const assertState = (i, s) => {
    shouldBe(`gl.isEnabled(${0x3000 + i} /* CLIP_DISTANCE${i}_WEBGL */)`, s ? "true" : "false");
    wtu.glErrorShouldBe(gl, gl.NO_ERROR, "should be no errors");

    shouldBe(`gl.getParameter(${0x3000 + i} /* CLIP_DISTANCE${i}_WEBGL */)`, s ? "true" : "false");
    wtu.glErrorShouldBe(gl, gl.NO_ERROR, "should be no errors");
  };

  for (let i = 0; i < 8; i++) {
    assertState(i, false);

    gl.enable(ext.CLIP_DISTANCE0_WEBGL + i);
    wtu.glErrorShouldBe(gl, gl.NO_ERROR, "should be no errors");

    assertState(i, true);

    gl.disable(ext.CLIP_DISTANCE0_WEBGL + i);
    wtu.glErrorShouldBe(gl, gl.NO_ERROR, "should be no errors");

    assertState(i, false);
  }
}

function checkClipDistance() {
  debug("");
  debug("Check clip distance operation");

  const vs = `#version 300 es
#extension GL_ANGLE_clip_cull_distance : require

uniform vec4 u_plane;
in vec2 a_position;
void main()
{
    gl_Position = vec4(a_position, 0.0, 1.0);
    gl_ClipDistance[0] = dot(gl_Position, u_plane);
}`;

  const program = wtu.setupProgram(gl, [vs, wtu.simpleColorFragmentShaderESSL300]);
  gl.useProgram(program);
  gl.uniform4fv(gl.getUniformLocation(program, 'u_color'), [1.0, 0.0, 0.0, 1.0]);

  gl.enable(ext.CLIP_DISTANCE0_WEBGL);

  // Clear to blue
  gl.clearColor(0, 0, 1, 1);
  gl.clear(gl.COLOR_BUFFER_BIT);

  wtu.setupUnitQuad(gl);

  // Draw full screen quad with color red
  gl.uniform4f(gl.getUniformLocation(program, "u_plane"), 1, 0, 0, 0.5);
  wtu.drawUnitQuad(gl);
  wtu.glErrorShouldBe(gl, gl.NO_ERROR, "should be no errors");

  // All pixels on the left of the plane x = -0.5 must be blue
  let x      = 0;
  let y      = 0;
  let width  = w / 4 - 1;
  let height = h;
  wtu.checkCanvasRect(gl, x, y, width, height,
                      [0, 0, 255, 255], "should be blue");

  // All pixels on the right of the plane x = -0.5 must be red
  x      = w / 4 + 2;
  y      = 0;
  width  = w - x;
  height = h;
  wtu.checkCanvasRect(gl, x, y, width, height,
                      [255, 0, 0, 255], "should be red");

  // Clear to green
  gl.clearColor(0, 1, 0, 1);
  gl.clear(gl.COLOR_BUFFER_BIT);

  // Draw full screen quad with color red
  gl.uniform4f(gl.getUniformLocation(program, "u_plane"), -1, 0, 0, -0.5);
  wtu.drawUnitQuad(gl);
  wtu.glErrorShouldBe(gl, gl.NO_ERROR, "should be no errors");

  // All pixels on the left of the plane x = -0.5 must be red
  x      = 0;
  y      = 0;
  width  = w / 4 - 1;
  height = h;
  wtu.checkCanvasRect(gl, x, y, width, height,
                      [255, 0, 0, 255], "should be red");

  // All pixels on the right of the plane x = -0.5 must be green
  x      = w / 4 + 2;
  y      = 0;
  width  = w - x;
  height = h;
  wtu.checkCanvasRect(gl, x, y, width, height,
                      [0, 255, 0, 255], "should be green");

  // Disable CLIP_DISTANCE0 and draw again
  gl.disable(ext.CLIP_DISTANCE0_WEBGL);
  wtu.drawUnitQuad(gl);

  // All pixels must be red
  wtu.checkCanvas(gl, [255, 0, 0, 255], "should be red");
}

function checkClipDistanceInterpolation() {
  debug("");
  debug("Check clip distance interpolation");

  const vs = `#version 300 es
#extension GL_ANGLE_clip_cull_distance : require
in vec2 a_position;
void main()
{
  gl_Position = vec4(a_position, 0.0, 1.0);
  gl_ClipDistance[0] = dot(gl_Position, vec4( 1,  0, 0, 0.5));
  gl_ClipDistance[1] = dot(gl_Position, vec4(-1,  0, 0, 0.5));
  gl_ClipDistance[2] = dot(gl_Position, vec4( 0,  1, 0, 0.5));
  gl_ClipDistance[3] = dot(gl_Position, vec4( 0, -1, 0, 0.5));
  gl_ClipDistance[4] = gl_ClipDistance[0];
  gl_ClipDistance[5] = gl_ClipDistance[1];
  gl_ClipDistance[6] = gl_ClipDistance[2];
  gl_ClipDistance[7] = gl_ClipDistance[3];
}`;

  const fs = `#version 300 es
#extension GL_ANGLE_clip_cull_distance : require
precision highp float;
out vec4 my_FragColor;
void main()
{
  float r = gl_ClipDistance[0] + gl_ClipDistance[1];
  float g = gl_ClipDistance[2] + gl_ClipDistance[3];
  float b = gl_ClipDistance[4] + gl_ClipDistance[5];
  float a = gl_ClipDistance[6] + gl_ClipDistance[7];
  my_FragColor = vec4(r, g, b, a) * 0.5;
}`;

  const program = wtu.setupProgram(gl, [vs, fs]);
  gl.useProgram(program);

  gl.enable(ext.CLIP_DISTANCE0_WEBGL);
  gl.enable(ext.CLIP_DISTANCE1_WEBGL);
  gl.enable(ext.CLIP_DISTANCE2_WEBGL);
  gl.enable(ext.CLIP_DISTANCE3_WEBGL);
  gl.enable(ext.CLIP_DISTANCE4_WEBGL);
  gl.enable(ext.CLIP_DISTANCE5_WEBGL);
  gl.enable(ext.CLIP_DISTANCE6_WEBGL);
  gl.enable(ext.CLIP_DISTANCE7_WEBGL);

  wtu.setupUnitQuad(gl);

  // Clear to blue
  gl.clearColor(0, 0, 1, 1);
  gl.clear(gl.COLOR_BUFFER_BIT);

  // Draw full screen quad with color gray
  wtu.drawUnitQuad(gl);
  wtu.glErrorShouldBe(gl, gl.NO_ERROR, "should be no errors");

  const data = new Uint8Array(w * h * 4);
  gl.readPixels(0, 0, w, h, gl.RGBA, gl.UNSIGNED_BYTE, data);
  let passed = true;
  for (let x = 0; x < w; x++) {
    for (let y = 0; y < h; y++) {
      const currentPosition = (y * h + x) * 4;
      const inside = (x >= w / 4 && x < w * 3 / 4 && y >= h / 4 && y < h * 3 / 4);
      const expected = inside ? [127, 127, 127, 127] : [0, 0, 255, 255];
      const actual = data.slice(currentPosition, currentPosition + 4);
      if (Math.abs(actual[0] - expected[0]) > 1 ||
          Math.abs(actual[1] - expected[1]) > 1 ||
          Math.abs(actual[2] - expected[2]) > 1 ||
          Math.abs(actual[3] - expected[3]) > 1) {
        passed = false;
      }
    }
  }
  if (passed) {
    testPassed("Correct clip distance interpolation");
  } else {
    testFailed("Incorrect clip distance interpolation");
  }
}

function checkCullDistance() {
  debug("");
  debug("Check cull distance operation");

  if (gl.getParameter(ext.MAX_CULL_DISTANCES_WEBGL) == 0) {
    testPassed("No cull distance support");
    return;
  }

  const vs = `#version 300 es
#extension GL_ANGLE_clip_cull_distance : require

uniform vec4 u_plane;
in vec2 a_position;
void main()
{
    gl_Position = vec4(a_position, 0.0, 1.0);
    gl_CullDistance[0] = dot(gl_Position, u_plane);
}`;

  const program = wtu.setupProgram(gl, [vs, wtu.simpleColorFragmentShaderESSL300]);
  gl.useProgram(program);
  gl.uniform4fv(gl.getUniformLocation(program, 'u_color'), [1.0, 0.0, 0.0, 1.0]);

  // Clear to blue
  gl.clearColor(0, 0, 1, 1);
  gl.clear(gl.COLOR_BUFFER_BIT);

  wtu.setupUnitQuad(gl);

  // Draw full screen quad with color red
  gl.uniform4f(gl.getUniformLocation(program, "u_plane"), 1, 0, 0, 0.5);
  wtu.drawUnitQuad(gl);
  wtu.glErrorShouldBe(gl, gl.NO_ERROR, "should be no errors");

  // All pixels must be red
  wtu.checkCanvas(gl, [255, 0, 0, 255], "should be red");

  // Clear to green
  gl.clearColor(0, 1, 0, 1);
  gl.clear(gl.COLOR_BUFFER_BIT);

  // Draw full screen quad with color red
  gl.uniform4f(gl.getUniformLocation(program, "u_plane"), -1, 1, 0, -0.5);
  wtu.drawUnitQuad(gl);
  wtu.glErrorShouldBe(gl, gl.NO_ERROR, "should be no errors");

  // All pixels above the y > x line must be red
  const data = new Uint8Array(w * h * 4);
  gl.readPixels(0, 0, w, h, gl.RGBA, gl.UNSIGNED_BYTE, data);
  let passed = true;
  for (let x = 0; x < w; ++x) {
    for (let y = 0; y < h; ++y) {
      if (y <= x + 2 && y >= x - 2) continue; // skip the edge
      const currentPosition = (y * h + x) * 4;
      const actual = data.slice(currentPosition, currentPosition + 2);
      const expected = (y > x) ? [255, 0] : [0, 255];
      if (actual[0] != expected[0] || actual[1] != expected[1]) {
        passed = false;
      }
    }
  }
  if (passed) {
    testPassed("Correct cull distance operation");
  } else {
    testFailed("Incorrect cull distance operation");
  }
}

function checkCullDistanceInterpolation() {
  debug("");
  debug("Check cull distance interpolation");

  if (gl.getParameter(ext.MAX_CULL_DISTANCES_WEBGL) == 0) {
    testPassed("No cull distance support");
    return;
  }

  const vs = `#version 300 es
#extension GL_ANGLE_clip_cull_distance : require
in vec2 a_position;
void main()
{
  gl_Position = vec4(a_position, 0.0, 1.0);
  gl_CullDistance[0] = dot(gl_Position, vec4( 1,  0, 0, 1));
  gl_CullDistance[1] = dot(gl_Position, vec4(-1,  0, 0, 1));
  gl_CullDistance[2] = dot(gl_Position, vec4( 0,  1, 0, 1));
  gl_CullDistance[3] = dot(gl_Position, vec4( 0, -1, 0, 1));
  gl_CullDistance[4] = gl_CullDistance[0];
  gl_CullDistance[5] = gl_CullDistance[1];
  gl_CullDistance[6] = gl_CullDistance[2];
  gl_CullDistance[7] = gl_CullDistance[3];
}`;

  const fs = `#version 300 es
#extension GL_ANGLE_clip_cull_distance : require
precision highp float;
out vec4 my_FragColor;
void main()
{
  float r = gl_CullDistance[0] + gl_CullDistance[1];
  float g = gl_CullDistance[2] + gl_CullDistance[3];
  float b = gl_CullDistance[4] + gl_CullDistance[5];
  float a = gl_CullDistance[6] + gl_CullDistance[7];
  my_FragColor = vec4(r, g, b, a) * 0.25;
}`;

  const program = wtu.setupProgram(gl, [vs, fs]);
  gl.useProgram(program);

  // Clear to blue
  gl.clearColor(0, 0, 1, 1);
  gl.clear(gl.COLOR_BUFFER_BIT);

  wtu.setupQuad(gl, {scale: 0.5});

  // Draw a small quad with color gray
  wtu.drawUnitQuad(gl);
  wtu.glErrorShouldBe(gl, gl.NO_ERROR, "should be no errors");

  const data = new Uint8Array(w * h * 4);
  gl.readPixels(0, 0, w, h, gl.RGBA, gl.UNSIGNED_BYTE, data);
  let passed = true;
  for (let x = 0; x < w; x++) {
    for (let y = 0; y < h; y++) {
      const currentPosition = (y * h + x) * 4;
      const inside = (x >= w / 4 && x < w * 3 / 4 && y >= h / 4 && y < h * 3 / 4);
      const expected = inside ? [127, 127, 127, 127] : [0, 0, 255, 255];
      const actual = data.slice(currentPosition, currentPosition + 4);
      if (Math.abs(actual[0] - expected[0]) > 1 ||
          Math.abs(actual[1] - expected[1]) > 1 ||
          Math.abs(actual[2] - expected[2]) > 1 ||
          Math.abs(actual[3] - expected[3]) > 1) {
        passed = false;
      }
    }
  }
  if (passed) {
    testPassed("Correct cull distance interpolation");
  } else {
    testFailed("Incorrect cull distance interpolation");
  }
}

function runTestExtension() {
  checkEnums();
  checkQueries();

  checkClipDistance();
  checkClipDistanceInterpolation();

  checkCullDistance();
  checkCullDistanceInterpolation();
}

function runTest() {
  if (!gl) {
    testFailed("context does not exist");
  } else {
    testPassed("context exists");

    runTestNoExtension();

    ext = gl.getExtension("WEBGL_clip_cull_distance");

    wtu.runExtensionSupportedTest(gl, "WEBGL_clip_cull_distance", ext !== null);

    if (ext !== null) {
      runTestExtension();
    } else {
      testPassed("No WEBGL_clip_cull_distance support -- this is legal");
    }
  }
}

runTest();

var successfullyParsed = true;
</script>
<script src="../../js/js-test-post.js"></script>
</body>
</html>