summaryrefslogtreecommitdiffstats
path: root/dom/canvas/test/webgl-conf/checkout/conformance/glsl/variables/gl-pointcoord.html
blob: f87078a6ff1fc3982a7a2ca44617be8e9d2d5328 (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
<!--
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">
<title>gl-pointcoord Test</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 id="example" width="256" height="256">
</canvas>
<div id="description"></div>
<div id="console"></div>
<script id="vshader" type="x-shader/x-vertex">
attribute vec4 vPosition;
uniform float uPointSize;
void main()
{
  gl_PointSize = uPointSize;
  gl_Position = vPosition;
}
</script>

<script id="fshader" type="x-shader/x-fragment">
precision mediump float;
void main()
{
  gl_FragColor = vec4(
      gl_PointCoord.x,
      gl_PointCoord.y,
      0,
      1);
}
</script>

<script>
"use strict";
description("Checks gl_PointCoord and gl_PointSize");
debug("");

// NOTE: I'm not 100% confident in this test. I think it is correct.

var wtu = WebGLTestUtils;
var gl = wtu.create3DContext("example");
shouldBeNonNull("gl");
var program = wtu.setupProgram(gl, ["vshader", "fshader"], ["vPosition"]);
shouldBe("gl.getError()", "gl.NO_ERROR");

var canvas = gl.canvas;
var width = canvas.width;
var height = canvas.height;
shouldBe("width", "height");

var maxPointSize = gl.getParameter(gl.ALIASED_POINT_SIZE_RANGE)[1];
shouldBeTrue("maxPointSize >= 1");
// The minimum and maximum point sizes may be floating-point numbers.
shouldBeTrue("Math.floor(maxPointSize) >= 1");
maxPointSize = Math.floor(maxPointSize);
shouldBeTrue("maxPointSize % 1 == 0");

maxPointSize = Math.min(maxPointSize, 64);
var pointWidth = maxPointSize / width;
var pointStep = Math.floor(maxPointSize / 4);
var pointStep = Math.max(1, pointStep);

var pointSizeLoc = gl.getUniformLocation(program, "uPointSize");
gl.uniform1f(pointSizeLoc, maxPointSize);

var pixelOffset = (maxPointSize % 2) ? (1 / width) : 0;
var vertexObject = gl.createBuffer();
gl.bindBuffer(gl.ARRAY_BUFFER, vertexObject);
gl.bufferData(
    gl.ARRAY_BUFFER,
    new Float32Array(
        [-0.5 + pixelOffset, -0.5 + pixelOffset,
          0.5 + pixelOffset, -0.5 + pixelOffset,
         -0.5 + pixelOffset,  0.5 + pixelOffset,
          0.5 + pixelOffset,  0.5 + pixelOffset]),
    gl.STATIC_DRAW);
gl.enableVertexAttribArray(0);
gl.vertexAttribPointer(0, 2, gl.FLOAT, false, 0, 0);

gl.clear(gl.COLOR_BUFFER_BIT | gl.DEPTH_BUFFER_BIT);

gl.drawArrays(gl.POINTS, 0, 4);
shouldBe("gl.getError()", "gl.NO_ERROR");

function s2p(s) {
  return (s + 1.0) * 0.5 * width;
}

//function print(x, y) {
//  var b = new Uint8Array(4);
//  gl.readPixels(x, y, 1, 1, gl.RGBA, gl.UNSIGNED_BYTE, b);
//  debug("" + x + "," + y + ": " + b[0] + "," + b[1] + "," + b[2]);
//}
//
//for (var ii = 0; ii < 100; ++ii) {
//  print(ii, ii);
//}

for (var py = 0; py < 2; ++py) {
  for (var px = 0; px < 2; ++px) {
    debug("");
    var pointX = -0.5 + px + pixelOffset;
    var pointY = -0.5 + py + pixelOffset;
    for (var yy = 0; yy < maxPointSize; yy += pointStep) {
      for (var xx = 0; xx < maxPointSize; xx += pointStep) {
        // formula for s and t from OpenGL ES 2.0 spec section 3.3
        var xw = s2p(pointX);
        var yw = s2p(pointY);
        //debug("xw: " + xw + " yw: " + yw);
        var u = xx / maxPointSize * 2 - 1;
        var v = yy / maxPointSize * 2 - 1;
        var xf = Math.floor(s2p(pointX + u * pointWidth));
        var yf = Math.floor(s2p(pointY + v * pointWidth));
        //debug("xf: " + xf + " yf: " + yf);
        var s = 0.5 + (xf + 0.5 - xw) / maxPointSize;
        var t = 0.5 + (yf + 0.5 - yw) / maxPointSize;
        //debug("s: " + s + " t: " + t);
        var color = [Math.floor(s * 255), Math.floor((1 - t) * 255), 0];
        var msg = "pixel " + xf + "," + yf + " should be " + color;
        wtu.checkCanvasRect(gl, xf, yf, 1, 1, color, msg, 4);
      }
    }
  }
}

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

</body>
</html>