summaryrefslogtreecommitdiffstats
path: root/dom/canvas/test/webgl-conf/checkout/conformance/glsl/bugs/floor-div-cos-should-not-truncate.html
blob: 69a019aa1be9cf54a67dae750b95618c4a12f4fd (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
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<title>Floor + divide + cosine should not truncate intermediate results.</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="256" height="256"> </canvas>
<div id="description"></div>
<div id="console"></div>

<script id="vshader" type="x-shader/x-vertex">
precision highp float;

attribute vec3 pos;

// This divisor must be greater than the 32-bit floating point
// representation of 1e6 / (2 * pi) to repro.
const float magic = 159154.953125;

void main(void) {
  // This floor must be present to repro.
  float x = floor(pos.x);

  // This divide and cosine must be present to repro.
  x = cos(x / magic);

  // If the GPU truncated 'x / magic' to 0, then 'cos(x / magic)' will produce
  // 1.0, the green square will be moved offscreen, and the red background
  // will be visible.
  gl_Position.x = pos.y + x * 2.0;
  gl_Position.y = pos.z;
  gl_Position.z = 0.0;
  gl_Position.w = 1.0;
}
</script>

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

void main(void) {
  gl_FragColor = vec4(0.0, 1.0, 0.0, 1.0);
}
</script>

<script type="application/javascript">
"use strict";
description("Flooring a number, then dividing by a large number, then computing the cosine of that should not truncate the intermediate values.");
debug("Regression test for <a href='https://code.google.com/p/angleproject/issues/detail?id=1179'>https://code.google.com/p/angleproject/issues/detail?id=1179</a>");
var wtu = WebGLTestUtils;
var gl = wtu.create3DContext("canvas");
var program = wtu.setupProgram(gl, ['vshader', 'fshader'], ['pos'], undefined, true);

gl.clearColor(1, 0, 0, 1);
gl.clear(gl.COLOR_BUFFER_BIT);

var magic = 159154.953125;
var x = (Math.PI / 2.0) * magic;
var data = [
  x, -1, -1,
  x, 1, -1,
  x, 1, 1,
  x, -1, -1,
  x, 1, 1,
  x, -1, 1
];

gl.bindBuffer(gl.ARRAY_BUFFER, gl.createBuffer());
gl.bufferData(gl.ARRAY_BUFFER, new Float32Array(data), gl.STATIC_DRAW);
gl.enableVertexAttribArray(0);
gl.vertexAttribPointer(0, 3, gl.FLOAT, false, 12, 0);

gl.drawArrays(gl.TRIANGLES, 0, 6);

wtu.checkCanvas(gl, [0,255,0,255], "should be 0,255,0,255");
finishTest();
</script>