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

Disable terrain render cache during style property transitions #10485

Merged
merged 4 commits into from
Mar 22, 2021

Conversation

mpulkki-mapbox
Copy link
Contributor

The terrain feature uses render cache for optimizing rendering performance by caching intermediate rendering results of tiles in cache textures when certain conditions are met. The cache is supposed to get invalidated on style changes but the code is not currently taking transitions into account. Terrain class correctly listens for style data events but those are sent only once at the beginning of transitions.

The following snippet changes water color correctly in Streets-style when the transition duration is set to zero.

map.painter.terrain.forceRenderCached = true;
map.style.stylesheet.transition = { duration: 0, delay: 0 };
map.setPaintProperty("water", "fill-color", ["rgba", 117, 207, 0, 1]);

But the render cache does not get invalidated properly when a transition duration is set.

map.painter.terrain.forceRenderCached = true;
map.style.stylesheet.transition = { duration: 500, delay: 0 };
map.setPaintProperty("water", "fill-color", ["rgba", 117, 207, 0, 1]);

not_working

This pull request fixes the cache invalidation by disabling the render cache for the duration of style property transitions. The PR does not fix cache issues caused by re-evalution of paint properties (#10302). Six new render tests have been added (most of which fails before the patch and passes after it) for validating the fix.

Launch Checklist

  • briefly describe the changes in this PR
  • include before/after visuals or gifs if this PR includes visual changes
  • write tests for all new functionality
  • manually test the debug page
  • apply changelog label ('bug', 'feature', 'docs', etc) or use the label 'skip changelog'
  • add an entry inside this element for inclusion in the mapbox-gl-js changelog: <changelog>Fix style property transitions not invalidating the terrain render cache</changelog>

const options = this.painter.options;
this.renderCached = (options.zooming || options.moving || options.rotating || !!this.forceRenderCached) && !this._invalidateRenderCache;
this.renderCached = (options.zooming || options.moving || options.rotating || !!this.forceRenderCached) && !this._invalidateRenderCache && !options.styleDirty;
Copy link
Contributor

Choose a reason for hiding this comment

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

There's similar implementation used for crossfade below:

_shouldDisableRenderCache(): boolean {
// Disable render caches on dynamic events due to fading.
const isCrossFading = id => {
const layer = this._style._layers[id];
const isHidden = !layer.isHidden(this.painter.transform.zoom);
const crossFade = layer.getCrossfadeParameters();
const isFading = !!crossFade && crossFade.t !== 1;

should we add this.style.hasTransitions() there instead?

Copy link
Contributor

Choose a reason for hiding this comment

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

Yes I agree, I also think that would be a better place to consolidate this logic.

Copy link
Contributor Author

@mpulkki-mapbox mpulkki-mapbox Mar 22, 2021

Choose a reason for hiding this comment

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

Agreed. I've updated the PR by moving the introduced logic to _shouldDisableRenderCache. I was initially concerned if some of the transition events could be missed because style.update() was called before painter.render() leading to "transition" flags being clear before rendering of the frame was done. But this works correctly as render cache contents gets updated in the very first frame after transitions have finished.

Copy link
Contributor

@karimnaaji karimnaaji left a comment

Choose a reason for hiding this comment

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

One nit to maybe consolidate logic in _shouldDisableRenderCache, otherwise LGTM.

const options = this.painter.options;
this.renderCached = (options.zooming || options.moving || options.rotating || !!this.forceRenderCached) && !this._invalidateRenderCache;
this.renderCached = (options.zooming || options.moving || options.rotating || !!this.forceRenderCached) && !this._invalidateRenderCache && !options.styleDirty;
Copy link
Contributor

Choose a reason for hiding this comment

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

Yes I agree, I also think that would be a better place to consolidate this logic.

@karimnaaji karimnaaji added this to the v2.3 milestone Mar 19, 2021
@mpulkki-mapbox mpulkki-mapbox force-pushed the mpulkki-fix-render-cache-invalidation branch from a3aac05 to 57ec76e Compare March 22, 2021 08:51
const layer = this._style._layers[id];
const isHidden = !layer.isHidden(this.painter.transform.zoom);
Copy link
Contributor Author

Choose a reason for hiding this comment

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

I noticed a possible bug where isHidden was negated twice leading to wrong result. Hidden layers are now properly skipped when determining whether caching should be disabled. @astojilj can you confirm this?

Copy link
Contributor

@astojilj astojilj Mar 22, 2021

Choose a reason for hiding this comment

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

Great find. This relates to raster fade on terrain and good to recheck the details (a192ebb) - there is render cache there that seems to be passing now, too.

Edit: please ignore this, wrong about it.

Copy link
Contributor

@astojilj astojilj left a comment

Choose a reason for hiding this comment

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

LGTM

@karimnaaji karimnaaji merged commit 6789ac8 into main Mar 22, 2021
@karimnaaji karimnaaji deleted the mpulkki-fix-render-cache-invalidation branch March 22, 2021 15:37
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

3 participants