Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Upload one raster tile texture per render frame #7592

Closed
wants to merge 7 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
30 changes: 18 additions & 12 deletions src/source/raster_dem_tile_source.js
Original file line number Diff line number Diff line change
Expand Up @@ -19,13 +19,15 @@ import type {RasterDEMSourceSpecification} from '../style-spec/types';

class RasterDEMTileSource extends RasterTileSource implements Source {
encoding: "mapbox" | "terrarium";
_textureQueue: Array<Object>;

constructor(id: string, options: RasterDEMSourceSpecification, dispatcher: Dispatcher, eventedParent: Evented) {
super(id, options, dispatcher, eventedParent);
this.type = 'raster-dem';
this.maxzoom = 22;
this._options = extend({}, options);
this.encoding = options.encoding || "mapbox";
this._textureQueue = [];
}

serialize() {
Expand Down Expand Up @@ -56,19 +58,23 @@ class RasterDEMTileSource extends RasterTileSource implements Source {
if (this.map._refreshExpiredTiles) tile.setExpiryData(img);
delete (img: any).cacheControl;
delete (img: any).expires;
this._textureQueue.push({tileKey: tile.tileID.key, img, callback: textureCallback.bind(this)});
callback(null);
}
}

const rawImageData = browser.getImageData(img);
const params = {
uid: tile.uid,
coord: tile.tileID,
source: this.id,
rawImageData,
encoding: this.encoding
};

if (!tile.workerID || tile.state === 'expired') {
tile.workerID = this.dispatcher.send('loadDEMTile', params, done.bind(this));
}
function textureCallback(tile, img) {
const rawImageData = browser.getImageData(img);
const params = {
uid: tile.uid,
coord: tile.tileID,
source: this.id,
rawImageData,
encoding: this.encoding
};

if (!tile.workerID || tile.state === 'expired') {
tile.workerID = this.dispatcher.send('loadDEMTile', params, done.bind(this));
}
}

Expand Down
10 changes: 8 additions & 2 deletions src/source/raster_tile_source.js
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@ class RasterTileSource extends Evented implements Source {
dispatcher: Dispatcher;
map: Map;
tiles: Array<string>;
_textureQueue: Array<Object>;

_loaded: boolean;
_options: RasterSourceSpecification | RasterDEMSourceSpecification;
Expand All @@ -54,6 +55,7 @@ class RasterTileSource extends Evented implements Source {
this.scheme = 'xyz';
this.tileSize = 512;
this._loaded = false;
this._textureQueue = [];

this._options = extend({}, options);
extend(this, pick(options, ['url', 'scheme', 'tileSize']));
Expand Down Expand Up @@ -103,6 +105,7 @@ class RasterTileSource extends Evented implements Source {

loadTile(tile: Tile, callback: Callback<void>) {
const url = normalizeURL(tile.tileID.canonical.url(this.tiles, this.scheme), this.url, this.tileSize);

tile.request = getImage(this.map._transformRequest(url, ResourceType.Tile), (err, img) => {
delete tile.request;

Expand All @@ -117,6 +120,11 @@ class RasterTileSource extends Evented implements Source {
delete (img: any).cacheControl;
delete (img: any).expires;

this._textureQueue.push({tileKey: tile.tileID.key, img, callback: textureCallback.bind(this)});
callback(null);
}

function textureCallback (tile, img) {
const context = this.map.painter.context;
const gl = context.gl;
tile.texture = this.map.painter.getTileTexture(img.width);
Expand All @@ -132,8 +140,6 @@ class RasterTileSource extends Evented implements Source {
}

tile.state = 'loaded';

callback(null);
}
});
}
Expand Down
1 change: 1 addition & 0 deletions src/source/source.js
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,7 @@ export interface Source {
tileID?: CanonicalTileID;
reparseOverscaled?: boolean,
vectorLayerIds?: Array<string>,
_textureQueue?: Array<Object>;

hasTransition(): boolean;

Expand Down
30 changes: 29 additions & 1 deletion src/ui/map.js
Original file line number Diff line number Diff line change
Expand Up @@ -1682,6 +1682,34 @@ class Map extends Camera {

this._placementDirty = this.style && this.style._updatePlacement(this.painter.transform, this.showCollisionBoxes, this._fadeDuration, this._crossSourceCollisions);

// For performance reasons, we limit texture uploading to the GPU to
// a max of two uploads per animation frame
let continueRenderingTextures = false;
const sources = this.style && this.style.sourceCaches;
for (const id in sources) {
const cache = sources[id];
const source = cache._source;
if ((source.type === 'raster' || source.type === 'raster-dem')) {
const textureQueue = Array.isArray(source._textureQueue) ? source._textureQueue : [];
const max = Math.min(2, textureQueue.length);
for (let i = 0; i < max; i++) {
const queued = textureQueue.shift();
if (queued) {
const {tileKey, img, callback} = queued;
const tile = cache.getTileByID(tileKey);
// do not upload aborted tiles to the GPU
if (tile && !tile.aborted) {
console.log('create texture', textureQueue.length);
callback(tile, img);
}
if (textureQueue.length > 0) {
continueRenderingTextures = true;
}
}
}
}
}

// Actually draw
this.painter.render(this.style, {
showTileBoundaries: this.showTileBoundaries,
Expand Down Expand Up @@ -1715,7 +1743,7 @@ class Map extends Camera {
// Even though `_styleDirty` and `_sourcesDirty` are reset in this
// method, synchronous events fired during Style#update or
// Style#_updateSources could have caused them to be set again.
if (this._sourcesDirty || this._repaint || this._styleDirty || this._placementDirty) {
if (this._sourcesDirty || this._repaint || this._styleDirty || this._placementDirty || continueRenderingTextures) {
this.triggerRepaint();
} else if (!this.isMoving() && this.loaded()) {
this.fire(new Event('idle'));
Expand Down