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

Reuse one renderbuffer for all draping. #10611

Merged
merged 2 commits into from
Apr 29, 2021
Merged
Changes from 1 commit
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
36 changes: 20 additions & 16 deletions src/terrain/terrain.js
Original file line number Diff line number Diff line change
Expand Up @@ -161,7 +161,7 @@ class ProxiedTileID extends OverscaledTileID {
}

type OverlapStencilType = false | 'Clip' | 'Mask';
type FBO = {fb: Framebuffer, tex: Texture, dirty: boolean, ref: number};
type FBO = {fb: Framebuffer, tex: Texture, dirty: boolean};

export class Terrain extends Elevation {
terrainTileForTile: {[number | string]: Tile};
Expand Down Expand Up @@ -190,6 +190,7 @@ export class Terrain extends Elevation {
_sourceTilesOverlap: {[string]: boolean};
_overlapStencilMode: StencilMode;
_overlapStencilType: OverlapStencilType;
_stencilRef: number;

_exaggeration: number;
_depthFBO: Framebuffer;
Expand Down Expand Up @@ -438,10 +439,10 @@ export class Terrain extends Elevation {
this._assignTerrainTiles(coords);
this._prepareDEMTextures();
this._setupDrapedRenderBatches();
this._initFBOPool();
this._setupRenderCache(previousProxyToSource);

this.renderingToTexture = false;
this._initFBOPool();
this._updateTimestamp = browser.now();

// Gather all dem tiles that are assigned to proxy tiles
Expand Down Expand Up @@ -800,7 +801,7 @@ export class Terrain extends Elevation {
return null;
}

_createFBO(): FBO {
_createFBO(depthAttachment?: WebGLRenderbuffer): FBO {
const painter = this.painter;
const context = painter.context;
const gl = context.gl;
Expand All @@ -811,18 +812,26 @@ export class Terrain extends Elevation {
const fb = context.createFramebuffer(bufferSize[0], bufferSize[1], false);
fb.colorAttachment.set(tex.texture);

const renderbuffer = depthAttachment ? depthAttachment : context.createRenderbuffer(context.gl.DEPTH_STENCIL, fb.width, fb.height);
fb.depthAttachment = new DepthStencilAttachment(context, fb.framebuffer);
fb.depthAttachment.set(renderbuffer);
if (!depthAttachment) {
context.clear({stencil: 0});
this._stencilRef = 0;
}

if (context.extTextureFilterAnisotropic && !context.extTextureFilterAnisotropicForceOff) {
gl.texParameterf(gl.TEXTURE_2D,
context.extTextureFilterAnisotropic.TEXTURE_MAX_ANISOTROPY_EXT,
context.extTextureFilterAnisotropicMax);
}

return {fb, tex, dirty: false, ref: 1};
return {fb, tex, dirty: false};
}

_initFBOPool() {
while (this.pool.length < Math.min(FBO_POOL_SIZE, this.proxyCoords.length)) {
this.pool.push(this._createFBO());
this.pool.push(this._createFBO(this.pool.length > 0 ? this.pool[0].fb.depthAttachment.current : null));
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pushed a small simplification to make it more explicit that we're sharing the depth stencil between these framebuffers with this change in e9768c5 (with this.pool.length > 0 ? this.pool[0].fb.depthAttachment.current : null) here and this.pool[index % FBO_POOL_SIZE].fb.depthAttachment.current line 1001, I didn't find it immediately obvious where the depth attachment was coming from at framebuffer creation time).

}
}

Expand Down Expand Up @@ -989,7 +998,8 @@ export class Terrain extends Elevation {
let index = psc.renderCachePool.pop();
if (index === undefined && psc.renderCache.length < RENDER_CACHE_MAX_SIZE) {
index = psc.renderCache.length;
psc.renderCache.push(this._createFBO());
const depthAttachment = this.pool[index % FBO_POOL_SIZE].fb.depthAttachment.current;
psc.renderCache.push(this._createFBO(depthAttachment));
// assert(psc.renderCache.length <= coords.length);
}
if (index !== undefined) {
Expand Down Expand Up @@ -1031,18 +1041,12 @@ export class Terrain extends Elevation {
this._overlapStencilType = false;
return;
}
if (!fb.depthAttachment) {
const renderbuffer = context.createRenderbuffer(context.gl.DEPTH_STENCIL, fb.width, fb.height);
fb.depthAttachment = new DepthStencilAttachment(context, fb.framebuffer);
fb.depthAttachment.set(renderbuffer);
context.clear({stencil: 0});
}
if (fbo.ref + stencilRange > 255) {
if (this._stencilRef + stencilRange > 255) {
context.clear({stencil: 0});
fbo.ref = 0;
this._stencilRef = 0;
}
fbo.ref += stencilRange;
this._overlapStencilMode.ref = fbo.ref;
this._stencilRef += stencilRange;
this._overlapStencilMode.ref = this._stencilRef;
if (layer.isTileClipped()) {
this._renderTileClippingMasks(proxiedCoords, this._overlapStencilMode.ref);
}
Expand Down