Skip to content

Commit c9947fd

Browse files
prozessor13HarelM
andauthored
Memory usage in 3d and in general, references #1291 (#1302)
Fixes #1291 * remove deprecated tiles from TerrainSourceCache * Add changelog comment Co-authored-by: HarelM <harel.mazor@gmail.com>
1 parent f014642 commit c9947fd

5 files changed

Lines changed: 36 additions & 50 deletions

File tree

CHANGELOG.md

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,8 @@
88

99
- *...Add new stuff here...*
1010
- Handle maxBounds which cross the meridian at longitude ±180° (#1298, #1299)
11-
- Hide arrow displayed in default `summary` styles on the attribution control ([#1258](https://github.com/maplibre/maplibre-gl-js/pull/1258))
11+
- Hide arrow displayed in default `summary` styles on the attribution control (#1258)
12+
- Fix memory usage in terrain 3D (#1291, #1302)
1213

1314
## 2.2.0-pre.2
1415

src/render/draw_symbol.test.ts

Lines changed: 2 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -14,9 +14,6 @@ import Transform from '../geo/transform';
1414
import type EvaluationParameters from '../style/evaluation_parameters';
1515
import type {SymbolLayerSpecification} from '../style-spec/types.g';
1616
import Style from '../style/style';
17-
import TerrainSourceCache from '../source/terrain_source_cache';
18-
import {Evented} from '../util/evented';
19-
import {RequestManager} from '../util/request_manager';
2017

2118
jest.mock('./painter');
2219
jest.mock('./program');
@@ -25,22 +22,6 @@ jest.mock('../source/tile');
2522
jest.mock('../data/bucket/symbol_bucket');
2623
jest.mock('../symbol/projection');
2724

28-
class StubMap extends Evented {
29-
transform: Transform;
30-
painter: Painter;
31-
_requestManager: RequestManager;
32-
33-
constructor() {
34-
super();
35-
this.transform = new Transform();
36-
this._requestManager = {
37-
transformRequest: (url) => {
38-
return {url};
39-
}
40-
} as any as RequestManager;
41-
}
42-
}
43-
4425
describe('drawSymbol', () => {
4526
test('should not do anything', () => {
4627
const mockPainter = new Painter(null, null);
@@ -63,7 +44,7 @@ describe('drawSymbol', () => {
6344
painterMock.renderPass = 'translucent';
6445
painterMock.transform = {pitch: 0, labelPlaneMatrix: mat4.create()} as any as Transform;
6546
painterMock.options = {} as any;
66-
painterMock.style = {terrainSourceCache: {getTerrain: () => null}} as any as Style;
47+
painterMock.style = {} as any as Style;
6748

6849
const layerSpec = {
6950
id: 'mock-layer',
@@ -164,9 +145,7 @@ describe('drawSymbol', () => {
164145
const sourceCacheMock = new SourceCache(null, null, null);
165146
(sourceCacheMock.getTile as jest.Mock).mockReturnValue(tile);
166147
sourceCacheMock.map = {showCollisionBoxes: false} as any as Map;
167-
painterMock.style = {
168-
terrainSourceCache: new TerrainSourceCache(new Style(new StubMap() as any as Map))
169-
} as any as Style;
148+
painterMock.style = {} as any as Style;
170149

171150
const spy = jest.spyOn(symbolProjection, 'updateLineLabels');
172151
drawSymbol(painterMock, sourceCacheMock, layer, [tileId], null);

src/render/draw_terrain.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -107,7 +107,7 @@ function prepareTerrain(painter: Painter, terrain: Terrain, tile: Tile, stack: n
107107
if (!tile.textures[stack]) {
108108
tile.textures[stack] = painter.getTileTexture(size) || new Texture(context, {width: size, height: size, data: null}, context.gl.RGBA);
109109
tile.textures[stack].bind(context.gl.LINEAR, context.gl.CLAMP_TO_EDGE);
110-
if (stack === 0) terrain.sourceCache.renderHistory.push(tile.tileID.key);
110+
if (stack === 0) terrain.sourceCache.renderHistory.unshift(tile.tileID.key);
111111
}
112112
const fb = terrain.getRTTFramebuffer();
113113
fb.colorAttachment.set(tile.textures[stack].texture);

src/render/terrain.test.ts

Lines changed: 19 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -6,8 +6,9 @@ import {RGBAImage} from '../util/image';
66
import Texture from './texture';
77
import type Style from '../style/style';
88
import type SourceCache from '../source/source_cache';
9-
import type TerrainSourceCache from '../source/terrain_source_cache';
109
import type {TerrainSpecification} from '../style-spec/types.g';
10+
import TileCache from '../source/tile_cache';
11+
import Tile from '../source/tile';
1112

1213
describe('Terrain', () => {
1314
test('pointCoordiate should not return null', () => {
@@ -21,23 +22,24 @@ describe('Terrain', () => {
2122
}
2223
} as any as Style;
2324
const sourceCache = {
24-
getTileByID: (tileID) => {
25-
if (tileID !== 'abcd') {
26-
return null;
27-
}
28-
return {
29-
tileID: {
30-
canonical: {
31-
x: 0,
32-
y: 0,
33-
z: 0
34-
}
35-
}
36-
};
25+
_cache: {max: 100} as TileCache
26+
} as SourceCache;
27+
const getTileByID = (tileID) : Tile => {
28+
if (tileID !== 'abcd') {
29+
return null;
3730
}
38-
} as any as TerrainSourceCache;
39-
const terrain = new Terrain(style, {} as any as SourceCache, {} as any as TerrainSpecification);
40-
terrain.sourceCache = sourceCache;
31+
return {
32+
tileID: {
33+
canonical: {
34+
x: 0,
35+
y: 0,
36+
z: 0
37+
}
38+
}
39+
};
40+
};
41+
const terrain = new Terrain(style, sourceCache, {} as any as TerrainSpecification);
42+
terrain.sourceCache.getTileByID = getTileByID;
4143
const context = style.map.painter.context as Context;
4244
const pixels = new Uint8Array([0, 0, 255, 255]);
4345
const image = new RGBAImage({width: 1, height: 1}, pixels);

src/source/terrain_source_cache.ts

Lines changed: 12 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -50,7 +50,7 @@ export default class TerrainSourceCache extends Evented {
5050
this.maxzoom = 22;
5151
this.tileSize = 512;
5252
this.deltaZoom = 1;
53-
this.renderHistorySize = 150;
53+
this.renderHistorySize = sourceCache._cache.max;
5454
sourceCache.usedForTerrain = true;
5555
sourceCache.tileSize = this.tileSize * 2 ** this.deltaZoom;
5656
}
@@ -98,15 +98,19 @@ export default class TerrainSourceCache extends Evented {
9898
removeOutdated(painter: Painter) {
9999
// create lookuptable for actual needed tiles
100100
const tileIDs = {};
101+
// remove duplicates from renderHistory and chop to renderHistorySize
102+
this.renderHistory = this.renderHistory.filter((i, p) => {
103+
return this.renderHistory.indexOf(i) === p;
104+
}).slice(0, this.renderHistorySize);
105+
// fill lookuptable with current rendered tiles
101106
for (const key of this._renderableTilesKeys) tileIDs[key] = true;
102-
// remove duplicates from renderHistory
103-
this.renderHistory = this.renderHistory.filter((i, p) => this.renderHistory.indexOf(i) === p);
107+
// fill lookuptable with most recent rendered tiles outside the viewport
108+
for (const key of this.renderHistory) tileIDs[key] = true;
104109
// free (GPU) memory from previously rendered not needed tiles
105-
while (this.renderHistory.length > this.renderHistorySize) {
106-
const tile = this.sourceCache._tiles[this.renderHistory.shift()];
107-
if (tile && !tileIDs[tile.tileID.key]) {
108-
tile.clearTextures(painter);
109-
delete this.sourceCache._tiles[tile.tileID.key];
110+
for (const key in this._tiles) {
111+
if (!tileIDs[key]) {
112+
this._tiles[key].clearTextures(painter);
113+
delete this._tiles[key];
110114
}
111115
}
112116
}

0 commit comments

Comments
 (0)