summaryrefslogtreecommitdiffstats
path: root/dom/canvas/test/webgl-conf/checkout/conformance/glsl/variables/gl-fragcoord-xy-values.html
blob: 161a58bfc6fd147bf2799d34a3d072781498bae6 (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
<!--
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-fragcoord 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="canvas" width="32" height="32">
</canvas>
<div id="description"></div>
<div id="console"></div>
<script id="vshader" type="x-shader/x-vertex">
// Inputs
attribute vec4 aPosInfo;

// Outputs
varying vec2 vTargetPixelCoord;

void main()
{
  vTargetPixelCoord = aPosInfo.zw;

  gl_PointSize = 1.0;
  gl_Position  = vec4(aPosInfo.xy, 0.0, 1.0);
}
</script>

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

// Inputs
varying vec2 vTargetPixelCoord;

// Colors used to signal correctness
const vec4 red   = vec4(1.0, 0.0, 0.0, 1.0);
const vec4 green = vec4(0.0, 1.0, 0.0, 1.0);

void main()
{
  // Check pixel index
  bool pixelIxValid = (floor(gl_FragCoord.xy) == vTargetPixelCoord);

  // Check fractional part of coordinates
  bool fracCoordValid = all(lessThan(abs(fract(gl_FragCoord.xy) - vec2(0.5)), vec2(0.0001)));

  gl_FragColor = (pixelIxValid && fracCoordValid) ? green : red;
}
</script>

<script id="test_fshader" type="x-shader/x-fragment">
// Shader to test if the frame buffer positions within the output pixel are different for the five render passes
// Pass on frame buffer position in varying, change in vertex shader : vTargetPixelCoord = aPosInfo.xy;
// Set test_fshader in setupProgram()

precision mediump float;

// Inputs
varying vec2 vTargetPixelCoord;

const vec2 pixSize = vec2(2.0/32.0, 2.0/32.0);

void main()
{
  // Coordinates within a framebuffer pixel [0, 1>
  vec2 inPixelCoord = fract(vTargetPixelCoord / pixSize);

  // Create different color dependent on the position inside the framebuffer pixel
  float r = (inPixelCoord.x < 0.4) ? 0.2 : (inPixelCoord.x > 0.6) ? 0.8 : 0.5;
  float g = (inPixelCoord.y < 0.4) ? 0.2 : (inPixelCoord.y > 0.6) ? 0.8 : 0.5;

  gl_FragColor = vec4(r, g, 0.0, 1.0);
}
</script>

<script>
"use strict";

// Test if gl_FragCoord.xy values are always of the form :
// (first framebuffer pixel index + 0.5, second framebuffer pixel index + 0.5)
// (if no multisampling)

// This is done by rendering a set of points which targets either the center of the
// output pixel or the center of one of the quadrants

// Constants
var floatsPerAttribute = 4;

// Globals
var wtu;
var gl;
var program;
var vxBuffer;

// Set data for one attribute (framebuffer.xy, pixel_index.xy)
function setPixelData(data, dIx, xx, yy, xSize, ySize, xOffset, yOffset)
{
  // Frame buffer first coordinate [-1, 1]
  data[dIx++] = (xx + 0.5) * xSize + xOffset - 1;

  // Frame buffer second coordinate [-1, 1]
  data[dIx++] = (yy + 0.5) * ySize + yOffset - 1;

  // Frame buffer pixel first index
  data[dIx++] = xx;

  // Frame buffer pixel second index
  data[dIx++] = yy;

  return dIx;
}

// Create attribute data
function createAttributeData(xOffset, yOffset)
{
  // Retrieve realised dimensions of viewport
  var widthPx    = gl.drawingBufferWidth;
  var heightPx   = gl.drawingBufferHeight;
  var pixelCount = widthPx * heightPx;

  // Pixel size in framebuffer coordinates
  var pWidth  = 2 / widthPx;
  var pHeight = 2 / heightPx;
  var data = new Float32Array(pixelCount * floatsPerAttribute);
  var dIx = 0;
  for (var yy = 0; yy < heightPx; ++yy)
    for (var xx = 0; xx < widthPx; ++xx)
      dIx = setPixelData(data, dIx, xx, yy, pWidth, pHeight,  xOffset * pWidth,  yOffset * pHeight);

  if (dIx !== data.length)
    wtu.error("gl-fragcoord-xy-values.html, createAttributeData(), index not correct at end");

  return data;
}

// Initialize test
function init()
{
  description("tests gl_FragCoord.xy values");

  wtu = WebGLTestUtils;
  gl = wtu.create3DContext("canvas", { antialias: false });
  program = wtu.setupProgram(gl, ["vshader", "fshader"], ["aPosInfo"]);
  vxBuffer = gl.createBuffer();

  gl.bindBuffer(gl.ARRAY_BUFFER, vxBuffer);
  gl.enableVertexAttribArray(0);
  gl.vertexAttribPointer(0, floatsPerAttribute, gl.FLOAT, false, 0, 0);
}

// Render data
function render(xOffset, yOffset, passMsg)
{
  // Set attribute data
  var data = createAttributeData(xOffset, yOffset);
  gl.bufferData(gl.ARRAY_BUFFER, data, gl.DYNAMIC_DRAW);

  gl.clear(gl.COLOR_BUFFER_BIT);
  gl.drawArrays(gl.POINTS, 0, data.length / floatsPerAttribute);
  wtu.glErrorShouldBe(gl, gl.NO_ERROR, "no errors from draw");
  var green = [0, 255, 0, 255];
  wtu.checkCanvas(gl, green, passMsg);
}

// Run tests
init();
render(0, 0, "green : sampling at center of output pixel is correct");
render(0.25, 0.25, "green : sampling in top right quadrant of output pixel is correct");
render(-0.25, 0.25, "green : sampling in top left quadrant of output pixel is correct");
render( 0.25, -0.25, "green : sampling in bottom right quadrant of output pixel is correct");
render(-0.25, -0.25, "green : sampling in bottom left quadrant of output pixel is correct");
var successfullyParsed = true;
</script>
<script src="../../../js/js-test-post.js"></script>

</body>
</html>