summaryrefslogtreecommitdiffstats
path: root/dom/canvas/test/webgl-conf/checkout/conformance/rendering/point-size.html
blob: d458428221ebe31f0635dd44ad8a81af40f03d77 (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
<!--
Copyright (c) 2019 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">
<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>
<script id="vshader" type="x-shader/x-vertex">
attribute vec3 pos;
attribute vec4 colorIn;
uniform float pointSize;
varying vec4 color;

void main()
{
    gl_PointSize = pointSize;
    color = colorIn;
    gl_Position = vec4(pos, 1.0);
}
</script>

<script id="fshader" type="x-shader/x-fragment">
precision mediump float;
varying vec4 color;

void main()
{
    gl_FragColor = color;
}
</script>
</head>
<body>
<canvas id="testbed" width="2" height="2"></canvas>
<div id="description"></div>
<div id="console"></div>
<script>
"use strict";
description('Verify GL_VERTEX_PROGRAM_POINT_SIZE is enabled in WebGL');

var wtu = WebGLTestUtils;
var gl = wtu.create3DContext('testbed', { antialias: false });
shouldBeNonNull("gl");

gl.disable(gl.BLEND);

// The choice of (0.4, 0.4) ensures that the centers of the surrounding
// pixels are not contained within the point when it is of size 1, but
// that they definitely are when it is of size 2.
var vertices = new Float32Array([
    0.4, 0.4, 0.0]);
var colors = new Uint8Array([
    255, 0, 0, 255]);

var colorOffset = vertices.byteLength;

var buf = new Uint8Array(2 * 2 * 4);
var index = 0;

var vbo = gl.createBuffer();
gl.bindBuffer(gl.ARRAY_BUFFER, vbo);
gl.bufferData(gl.ARRAY_BUFFER, colorOffset + colors.byteLength, gl.STATIC_DRAW);
gl.bufferSubData(gl.ARRAY_BUFFER, 0, vertices);
gl.bufferSubData(gl.ARRAY_BUFFER, colorOffset, colors);

function test(program) {
    gl.clear(gl.COLOR_BUFFER_BIT | gl.DEPTH_BUFFER_BIT);

    gl.vertexAttribPointer(0, 3, gl.FLOAT, false, 0, 0);
    gl.enableVertexAttribArray(0);
    gl.vertexAttribPointer(1, 4, gl.UNSIGNED_BYTE, true, 0, colorOffset);
    gl.enableVertexAttribArray(1);

    var locPointSize = gl.getUniformLocation(program, 'pointSize');

    shouldBe('gl.getError()', 'gl.NO_ERROR');

    debug('Draw a point of size 1 and verify it does not touch any other pixels.');

    gl.uniform1f(locPointSize, 1.0);
    gl.drawArrays(gl.POINTS, 0, vertices.length / 3);

    shouldBe('gl.getError()', 'gl.NO_ERROR');

    for (var y = 0; y < 2; ++y) {
        for (var x = 0; x < 2; ++x) {
            var correctColor = (x == 1 && y == 1) ? [255, 0, 0] : [0, 0, 0];
            wtu.checkCanvasRect(gl, x, y, 1, 1, correctColor);
        }
    }
    gl.clear(gl.COLOR_BUFFER_BIT | gl.DEPTH_BUFFER_BIT);

    debug('Draw a point of size 2 and verify it fills the appropriate region.');

    var pointSizeRange = gl.getParameter(gl.ALIASED_POINT_SIZE_RANGE);
    if (pointSizeRange[1] >= 2.0) {
        gl.uniform1f(locPointSize, 2.0);
        gl.drawArrays(gl.POINTS, 0, vertices.length / 3);
        shouldBe('gl.getError()', 'gl.NO_ERROR');
        wtu.checkCanvasRect(gl, 0, 0, 2, 2, [255, 0, 0]);
    }
}

debug('');
debug('Pass 1');
var program1 = wtu.setupProgram(gl, ['vshader', 'fshader'], ['pos', 'colorIn']);
shouldBe('gl.getError()', 'gl.NO_ERROR');
test(program1);

// Under some versions of ANGLE point sprite shader programs were
// incorrectly reloaded from cache. Rebuilding the shader program and
// repeating the test simulates the conditions that caused it to fail
debug('');
debug('Pass 2');
var program2 = wtu.setupProgram(gl, ['vshader', 'fshader'], ['pos', 'colorIn']);
shouldBe('gl.getError()', 'gl.NO_ERROR');
test(program2);

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

</body>
</html>