Skip to content

Commit

Permalink
Fix gradient texture invalidation across zoom levels
Browse files Browse the repository at this point in the history
Use a generation identifier instead of a single boolean to prevent stale data.
  • Loading branch information
karimnaaji committed Jun 1, 2020
1 parent d753cf0 commit 754be74
Show file tree
Hide file tree
Showing 4 changed files with 10 additions and 6 deletions.
1 change: 1 addition & 0 deletions src/data/bucket/line_bucket.js
Original file line number Diff line number Diff line change
Expand Up @@ -104,6 +104,7 @@ class LineBucket implements Bucket {
layoutVertexBuffer2: VertexBuffer;
gradientTexture: ?Texture;
gradient: ?RGBAImage;
gradientGeneration: number;

indexArray: TriangleIndexArray;
indexBuffer: IndexBuffer;
Expand Down
5 changes: 2 additions & 3 deletions src/render/draw_line.js
Original file line number Diff line number Diff line change
Expand Up @@ -81,7 +81,7 @@ export default function drawLine(painter: Painter, sourceCache: SourceCache, lay
painter.lineAtlas.bind(context);
} else if (gradient) {
let gradientTexture = bucket.gradientTexture;
if (!gradientTexture || layer.gradientTextureInvalidated) {
if (!gradientTexture || (gradientTexture && layer.gradientGeneration !== bucket.gradientGeneration)) {
const sourceMaxZoom = sourceCache.getSource().maxzoom;
const potentialOverzoom = coord.canonical.z === sourceMaxZoom ?
Math.ceil(1 << (painter.transform.maxZoom - coord.canonical.z)) : 1;
Expand All @@ -103,6 +103,7 @@ export default function drawLine(painter: Painter, sourceCache: SourceCache, lay
}
context.activeTexture.set(gl.TEXTURE0);
gradientTexture.bind(layer.stepInterpolant ? gl.NEAREST : gl.LINEAR, gl.CLAMP_TO_EDGE);
bucket.gradientGeneration = layer.gradientGeneration;
}

program.draw(context, gl.TRIANGLES, depthMode,
Expand All @@ -113,6 +114,4 @@ export default function drawLine(painter: Painter, sourceCache: SourceCache, lay
firstTile = false;
// once refactored so that bound texture state is managed, we'll also be able to remove this firstTile/programChanged logic
}

layer.gradientTextureInvalidated = false;
}
7 changes: 4 additions & 3 deletions src/style/style_layer/line_style_layer.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ import LineBucket from '../../data/bucket/line_bucket';
import {polygonIntersectsBufferedMultiLine} from '../../util/intersection_tests';
import {getMaximumPaintValue, translateDistance, translate} from '../query_utils';
import properties from './line_style_layer_properties';
import {extend} from '../../util/util';
import {extend, MAX_SAFE_INTEGER} from '../../util/util';
import EvaluationParameters from '../evaluation_parameters';
import {Transitionable, Transitioning, Layout, PossiblyEvaluated, DataDrivenProperty} from '../properties';

Expand Down Expand Up @@ -44,7 +44,7 @@ class LineStyleLayer extends StyleLayer {
_unevaluatedLayout: Layout<LayoutProps>;
layout: PossiblyEvaluated<LayoutProps>;

gradientTextureInvalidated: boolean;
gradientGeneration: number;
stepInterpolant: boolean;

_transitionablePaint: Transitionable<PaintProps>;
Expand All @@ -53,13 +53,14 @@ class LineStyleLayer extends StyleLayer {

constructor(layer: LayerSpecification) {
super(layer, properties);
this.gradientGeneration = 0;
}

_handleSpecialPaintPropertyUpdate(name: string) {
if (name === 'line-gradient') {
const expression: ZoomConstantExpression<'source'> = ((this._transitionablePaint._values['line-gradient'].value.expression): any);
this.stepInterpolant = expression._styleExpression.expression instanceof Step;
this.gradientTextureInvalidated = true;
this.gradientGeneration = (this.gradientGeneration + 1) % MAX_SAFE_INTEGER;
}
}
Expand Down
3 changes: 3 additions & 0 deletions src/util/util.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,9 @@ import window from './window';

import type {Callback} from '../types/callback';

// Number.MAX_SAFE_INTEGER not available in IE
export const MAX_SAFE_INTEGER = Math.pow(2, 53) - 1;

/**
* @module util
* @private
Expand Down

0 comments on commit 754be74

Please sign in to comment.