-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathroundoffVisualization.html
214 lines (194 loc) · 7.09 KB
/
roundoffVisualization.html
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
<html>
<head>
<title>Curious case of disappearing roundoff errors</title>
</head>
<script type="x-shader/x-vertex" id="passThruVS">
attribute vec2 coord;
varying vec2 pos;
void main(void) {
pos = coord;
gl_Position = vec4(coord, 0, 1.0);
gl_PointSize = 1.0;
}
</script>
<script type="x-shader/x-fragment" id="renderFrac">
precision highp float;
varying vec2 pos;
vec2 twoSum(float a, float b) {
float s = a + b;
float av = s - b;
float bv = s - a;
float e = (a - av)+(b - bv);
return vec2(s, e);
}
void main(void) {
vec2 sum = twoSum(pos.x, pos.y * 2e-8);
gl_FragColor = vec4(0, fract(sum.y*1e8), fract(abs(sum.x)), 1);
}
</script>
<script type="x-shader/x-fragment" id="renderFracONE">
precision highp float;
varying vec2 pos;
uniform vec2 ONE;
vec2 twoSum(float a, float b) {
float s = a + b;
float av = s * ONE.x - b * ONE.y;
float bv = s * ONE.y - a * ONE.x;
float e = (a - av) + (b - bv);
return vec2(s, e);
}
void main(void) {
vec2 sum = twoSum(pos.x, pos.y * 2e-8);
gl_FragColor = vec4(0, fract(sum.y*1e8), fract(abs(sum.x)), 1);
}
</script>
<script type="text/javascript">
class GLContext {
constructor(canvas) {
if (canvas == null)
throw 'Can not find canvas';
const ctx = canvas.getContext('webgl');
if (ctx == null)
throw 'Can not get WebGL context...';
ctx.viewport(0, 0, canvas.clientWidth, canvas.clientHeight);
ctx.clearColor(0, 0, 0, 1.0);
ctx.clear(ctx.COLOR_BUFFER_BIT);
this._ctx = ctx;
this.PRIMITIVES = {
POINTS: ctx.POINTS,
LINE_STRIP: ctx.LINE_STRIP,
LINES: ctx.LINES,
TRIANGLE_STRIP: ctx.TRIANGLE_STRIP,
TRIANGLES: ctx.TRIANGLES,
};
}
getGLContext() {
if (!this._ctx) throw 'Missing context';
return this._ctx;
}
compileShader(type, source) {
const glCtx = this.getGLContext();
var rc = glCtx.createShader(type);
glCtx.shaderSource(rc, source);
glCtx.compileShader(rc);
if (!glCtx.getShaderParameter(rc, glCtx.COMPILE_STATUS)) {
throw glCtx.getShaderInfoLog(rc) + ' in ' + source;
}
return rc;
}
compileShaderById(id) {
const glCtx = this.getGLContext();
const element = document.getElementById(id);
if (element == null) {
throw 'Can not find shader by element id ' + id;
}
const source = element.innerHTML;
if (element.type === 'x-shader/x-vertex') {
return this.compileShader(glCtx.VERTEX_SHADER, source);
}
if (element.type === 'x-shader/x-fragment') {
return this.compileShader(glCtx.FRAGMENT_SHADER, source);
}
throw 'Unknown shader type ' + element.type;
}
linkProgram(shaders) {
const glCtx = this.getGLContext();
const rc = glCtx.createProgram();
for(var cnt = 0; cnt < shaders.length; cnt++) {
if (!shaders[cnt]) {
throw 'Can not link program with null shaders';
}
glCtx.attachShader(rc, shaders[cnt]);
}
glCtx.linkProgram(rc);
if (!glCtx.getProgramParameter(rc, glCtx.LINK_STATUS)) {
throw glCtx.getProgramInfoLog(rc);
}
return rc;
}
setUniform2f(name, a, b) {
const glCtx = this.getGLContext();
const prog = glCtx.getParameter(glCtx.CURRENT_PROGRAM);
const idx = glCtx.getUniformLocation(prog, name);
glCtx.uniform2f(idx, a, b);
}
draw2DArray(type, name, arr) {
const glCtx = this.getGLContext();
const prog = glCtx.getParameter(glCtx.CURRENT_PROGRAM);
const idx = glCtx.getAttribLocation(prog, name);
var buf = glCtx.createBuffer();
glCtx.bindBuffer(glCtx.ARRAY_BUFFER, buf);
glCtx.bufferData(glCtx.ARRAY_BUFFER, new Float32Array(arr), glCtx.STATIC_DRAW);
glCtx.enableVertexAttribArray(idx);
glCtx.vertexAttribPointer(idx, 2, glCtx.FLOAT, false, 0, 0);
glCtx.drawArrays(type, 0, arr.length/2);
glCtx.deleteBuffer(buf);
}
}
function printMessage(elementName, msg) {
document.getElementById(elementName).innerHTML=
'<div style="color:#FFFFFF;'+
'font-size:120%;margin-top:40%;'+
'margin-left:20%;margin-right:20%">' +
msg + '</div>';
}
function visualizeRoundoffs(canvasName, shaderName, elementName) {
try {
printMessage(elementName, 'Initializing WebGL context...');
const canvas = document.getElementById(canvasName);
const ctx = new GLContext(canvas);
const prog = ctx.linkProgram(['passThruVS', shaderName].map(_ => ctx.compileShaderById(_)));
const gl = ctx.getGLContext();
gl.useProgram(prog);
ctx.setUniform2f('ONE', 1.0, 1.0);
ctx.draw2DArray(gl.TRIANGLE_STRIP, 'coord', [-1, -1, 1, -1, -1, 1, 1, 1]);
const pixelCount = 100;
const buf = new Uint8Array(4 * pixelCount);
gl.readPixels(0, 0, pixelCount, 1, gl.RGBA, gl.UNSIGNED_BYTE, buf);
if (gl.getError())
throw "Failed to read pixels";
// Check for non-zero values in green channel
for(var cnt = 0; cnt < pixelCount; ++cnt) {
if (buf[cnt*4+1] != 0) {
printMessage(elementName, '👍Shader compiler is aware of rounding errors.');
return;
}
}
printMessage(elementName, '💔Shader compiler lives in the world of the infinitely precision floats.');
} catch (e) { printMessage(elementName, 'WebGL context creation failed'); alert(e); }
}
</script>
<body onLoad="visualizeRoundoffs('canvas1', 'renderFrac', 'floatingMsg1');visualizeRoundoffs('canvas2', 'renderFracONE', 'floatingMsg2')">
<div width="1024px">
<div style="position:relative;float:left">
<canvas id="canvas1" width="512px" height="512px"></canvas>
<div id="floatingMsg1" style="position:absolute;top:0px; left:0px; width:512px;height:512px"></div>
</div>
<div style="position:relative;margin-left:512px;">
<canvas id="canvas2" width="512px" height="512px"></canvas>
<div id="floatingMsg2" style="position:absolute;top:0px; left:0px; width:512px;height:512px"></div>
</div>
</div>
<br/>
If all you see in the above left image is a dull black stripe on a blue background, then WebGL fragment shader compiler decided that following two functions are identical:
<div>
<pre style="display:inline-block;padding:10px; border: 1px solid #777; background-color:#eee;vertical-align: middle;">
vec2 sum(float a, float b) {
float s = a + b;
float e = (b - (s-a)) + (a - (s-b));
return vec2(s, e);
}
</pre>
<div style="display:inline;font-size:200%;vertical-align: middle;">↔</div>
<pre style="display:inline-block;padding:10px; border: 1px solid #777; background-color:#eee;vertical-align: middle;">
vec2 sum(float a, float b) {
return vec2(a + b, 0.0);
}
</pre>
</div>
which is clearly not the case, when non-overlapping floats are added together (for example 1.0 and pow(2,-24)).<br>
Image on the right is an attempt to workaround the limitations by using uniform that shader compilers can not optimize away, see <a href="https://github.com/visgl/luma.gl/blame/7aa4f21b956a3b16b1035e71915727bce71071a8/modules/shadertools/src/modules/fp64/fp64-arithmetic.glsl.ts#L7">this</a> explanation from luma.gl.<br/>
When compiler does not assume rounding errors to be zero, resulting picture looks as follows:<br>
<img src="roundoffErrors.png" width="512px" height="512px" />
</body>
</html>