Skip to content

Commit

Permalink
Modify the webgl line feature shaders.
Browse files Browse the repository at this point in the history
Chrome 65 and 66 when running in xvfb with openmesa do not compute
atan(0, <negative>) correctly.  Also, there are occasional int() casts
that round down even when they are small integers.  These changes make
image comparisons more robust by working around the atan issue (this may
be the issue detailed in
chromium.googlesource.com/angle/angle/commit/da9fb09).  I have submitted
a new issue to chromium:
https://bugs.chromium.org/p/chromium/issues/detail?id=841296 to track
this problem.

With this commit, the baselines were regenerated with Chrome 64, and the
test strictness requirements changed in PR #792 have been reverted.
There are subtly differences between Chrome 64 and 66 (font aliasing is
slightly different, and I think the other changes are because the shader
float precision is subtly different, though I'm not sure of that).
  • Loading branch information
manthey committed May 9, 2018
1 parent 193c14d commit 0de44b0
Show file tree
Hide file tree
Showing 6 changed files with 27 additions and 22 deletions.
33 changes: 22 additions & 11 deletions src/gl/lineFeature.js
Original file line number Diff line number Diff line change
Expand Up @@ -109,17 +109,24 @@ var gl_lineFeature = function (arg) {
'const float PI = 3.14159265358979323846264;',

'vec4 viewCoord(vec3 c) {',
' vec4 result = projectionMatrix * modelViewMatrix * vec4(c.xyz, 1);',
' vec4 result = projectionMatrix * modelViewMatrix * vec4(c.xyz, 1.0);',
' if (result.w != 0.0) result = result / result.w;',
' return result;',
'}',

'float atan2(float y, float x) {',
' if (x > 0.0) return atan(y / x);',
' if (x < 0.0 && y >= 0.0) return atan(y / x) + PI;',
' if (x < 0.0) return atan(y / x) - PI;',
' return sign(y) * 0.5 * PI;',
'}',

'void main(void)',
'{',
/* If any vertex has been deliberately set to a negative opacity,
* skip doing computations on it. */
' if (strokeOpacity < 0.0) {',
' gl_Position = vec4(2, 2, 0, 1);',
' gl_Position = vec4(2.0, 2.0, 0.0, 1.0);',
' return;',
' }',
/* convert coordinates. We have four values, since we need to
Expand All @@ -133,10 +140,10 @@ var gl_lineFeature = function (arg) {
// calculate line segment vector and angle
' vec2 deltaCB = C.xy - B.xy;',
' if (deltaCB == vec2(0.0, 0.0)) {',
' gl_Position = vec4(2, 2, 0, 1);',
' gl_Position = vec4(2.0, 2.0, 0.0, 1.0);',
' return;',
' }',
' float angleCB = atan(deltaCB.y / aspect, deltaCB.x);',
' float angleCB = atan2(deltaCB.y / aspect, deltaCB.x);',
// values we need to pass along
' strokeColorVar = vec4(strokeColor, strokeOpacity);',
// extract values from our flags field
Expand Down Expand Up @@ -169,10 +176,10 @@ var gl_lineFeature = function (arg) {
// offset, then the functional join angle is not simply half the
// angle between the two lines, but rather half the angle of the
// inside edge of the the two lines.
' float cosABC, sinABC, cosBCD, sinBCD;', // of half angles
' float cosABC = 1.0, sinABC = 0.0, cosBCD = 1.0, sinBCD = 0.0;', // of half angles
// handle near end
' if (nearMode >= 4) {',
' float angleBA = atan((B.y - A.y) / aspect, B.x - A.x);',
' float angleBA = atan2((B.y - A.y) / aspect, B.x - A.x);',
' if (A.xy == B.xy) angleBA = angleCB;',
' float angleABC = angleCB - angleBA;',
// ensure angle is in the range [-PI, PI], then take the half angle
Expand Down Expand Up @@ -205,7 +212,7 @@ var gl_lineFeature = function (arg) {

// handle far end
' if (farMode >= 4) {',
' float angleDC = atan((D.y - C.y) / aspect, D.x - C.x);',
' float angleDC = atan2((D.y - C.y) / aspect, D.x - C.x);',
' if (D.xy == C.xy) angleDC = angleCB;',
' float angleBCD = angleDC - angleCB;',
// ensure angle is in the range [-PI, PI], then take the half angle
Expand All @@ -231,7 +238,7 @@ var gl_lineFeature = function (arg) {
' gl_Position = vec4(',
' B.x + (xOffset * cos(angleCB) - yOffset * sin(angleCB)) * pixelWidth,',
' B.y + (xOffset * sin(angleCB) + yOffset * cos(angleCB)) * pixelWidth * aspect,',
' B.z, 1);',
' B.z, 1.0);',
// store other values needed to determine which pixels to plot.
' float lineLength = length(vec2(deltaCB.x, deltaCB.y / aspect)) / pixelWidth;',

Expand Down Expand Up @@ -262,7 +269,11 @@ var gl_lineFeature = function (arg) {
function createFragmentShader(allowDebug) {
var fragmentShaderSource = [
'#ifdef GL_ES',
' precision highp float;',
' #ifdef GL_FRAGMENT_PRECISION_HIGH',
' precision highp float;',
' #else',
' precision mediump float;',
' #endif',
'#endif',
'varying vec4 strokeColorVar;',
'varying vec4 subpos;',
Expand All @@ -275,8 +286,8 @@ var gl_lineFeature = function (arg) {
' vec4 color = strokeColorVar;',
allowDebug ? ' bool debug = bool(mod(fixedFlags, 2.0));' : '',
' float opacity = 1.0;',
' int nearMode = int(info.x);',
' int farMode = int(info.y);',
' int nearMode = int(floor(info.x + 0.5));',
' int farMode = int(floor(info.y + 0.5));',
' float cosABC = angles.x;',
' float sinABC = angles.y;',
' float cosBCD = angles.z;',
Expand Down
2 changes: 1 addition & 1 deletion testing/test-data/base-images.tgz.md5
Original file line number Diff line number Diff line change
@@ -1 +1 @@
2333a69e96ce063d10862bd8d563eb3d
24a2ffe0807138fe13c33ddeccb2b426
2 changes: 1 addition & 1 deletion testing/test-data/base-images.tgz.url
Original file line number Diff line number Diff line change
@@ -1 +1 @@
https://data.kitware.com/api/v1/file/5a9ed5838d777f0685786207/download
https://data.kitware.com/api/v1/file/5af1fcff8d777f0685797d19/download
4 changes: 1 addition & 3 deletions tests/gl-cases/glLines.js

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

4 changes: 1 addition & 3 deletions tests/gl-cases/glPolygons.js
Original file line number Diff line number Diff line change
Expand Up @@ -2446,9 +2446,7 @@ describe('glPolygons', function () {
layer: layer
}).read(JSON.stringify(data), function () {
myMap.draw();
// maximum error is 0.0030 because of a change in Chrome 65. When new
// versions are released, see if this can be moved back to 0.0015
imageTest.imageTest('glMultiPolygons', null, 0.0030, done, myMap.onIdle, 0, 2);
imageTest.imageTest('glMultiPolygons', null, 0.0015, done, myMap.onIdle, 0, 2);
});
});

Expand Down
4 changes: 1 addition & 3 deletions tests/headed-cases/lines.js
Original file line number Diff line number Diff line change
Expand Up @@ -16,9 +16,7 @@ describe('lines example', function () {
it('more lines', function (done) {
base$ = $('iframe#map')[0].contentWindow.jQuery;
base$('#lines').val(100000).trigger('change');
// maximum error is 0.0030 because of a change in Chrome 65. When new
// versions are released, see if this can be moved back to 0.0015
imageTest.imageTest('exampleLines100k', '#map', 0.0030, done, null, 0, 2, '#map.ready[segments="100000"]');
imageTest.imageTest('exampleLines100k', '#map', 0.0015, done, null, 0, 2, '#map.ready[segments="100000"]');
}, 10000);
it('thin preset', function (done) {
base$('button.preset').eq(1).trigger('click');
Expand Down

0 comments on commit 0de44b0

Please sign in to comment.