mirror of
https://github.com/bilawalsidhu/gods-eye-view.git
synced 2026-11-20 10:12:18 +00:00
Add a Navara HD renderer choice, release a parked engine after three minutes, and cut Navara's memory
Navara now draws at CSS pixels without MSAA, like Cesium; Navara HD keeps the device pixel ratio and MSAA 4. Only one Navara variant is ever kept. A parked engine is released after three minutes and its WebGL context is lost. Three leaks that kept a released Navara alive are fixed, the canvas context drops its unused antialias buffer, cloud shadows use two 256 px cascades, and the tile cache is budgeted from the screen buffers.
This commit is contained in:
1 parent
a6c64713ee
commit
9825b02aab
22 files changed
+657
-58
No files matched your search
@@ -13,6 +13,7 @@ import ThreeView, {
|
||||
} from '@navaramap/three';
|
||||
import * as geo from '../../geo/index.js';
|
||||
import { DefaultPlugin } from '@navaramap/three-default-plugin';
|
||||
import { Pass } from 'postprocessing';
|
||||
import { createNavaraEngine } from './createNavaraEngine.js';
|
||||
import {
|
||||
forEachLuminanceUniform,
|
||||
@@ -43,6 +44,10 @@ import {
|
||||
resolveNavaraLook,
|
||||
} from './viewOptions.js';
|
||||
|
||||
// Room for tiles in Navara's tile cache, over the screen-sized render
|
||||
// targets that its budget also counts (fixedGpuBytes).
|
||||
const TILE_CACHE_ROOM = 768 * 1024 * 1024;
|
||||
|
||||
// Navara 0.2.1 has no preserveDrawingBuffer option but takes a canvas whose
|
||||
// WebGL2 context is already created; three then uses that context.
|
||||
function preservedCanvas(container) {
|
||||
@@ -108,12 +113,14 @@ function groundInView(engine) {
|
||||
* @param {{
|
||||
* container: HTMLElement,
|
||||
* search?: string,
|
||||
* hd?: boolean,
|
||||
* }} options `search`, the page's query string, picks the look from
|
||||
* `?look=` (cinematic by default). The sun follows the engine clock (the
|
||||
* `?look=` (cinematic by default); `hd` draws at the device pixel ratio
|
||||
* with MSAA (Navara HD), else at CSS pixels without it. The sun follows the engine clock (the
|
||||
* page's app clock): the look reads its frame time.
|
||||
* @returns {Promise<{ view: object, engine: import('../contract.js').GlobeEngine, destroy: () => void }>}
|
||||
*/
|
||||
export async function createNavaraGlobe({ container, search }) {
|
||||
export async function createNavaraGlobe({ container, search, hd = false }) {
|
||||
if (!container) throw new TypeError('createNavaraGlobe needs a container');
|
||||
const canvas = preservedCanvas(container);
|
||||
let view;
|
||||
@@ -125,7 +132,7 @@ export async function createNavaraGlobe({ container, search }) {
|
||||
// Parked for a switch back: its render loop is off (setRenderLoop).
|
||||
let parked = false;
|
||||
try {
|
||||
view = new ThreeView(navaraViewOptions({ container, canvas }));
|
||||
view = new ThreeView(navaraViewOptions({ container, canvas, hd }));
|
||||
const plugin = new DefaultPlugin();
|
||||
view.addPlugin(plugin);
|
||||
await view.init();
|
||||
@@ -212,6 +219,24 @@ export async function createNavaraGlobe({ container, search }) {
|
||||
engine.render.request('navara-look');
|
||||
}),
|
||||
];
|
||||
// The tile cache keeps tiles that left the view until it reaches its
|
||||
// budget, by default a quarter of device memory up to 2 GiB: a long
|
||||
// session grew to about 1.4 GiB of GPU memory. The budget counts the
|
||||
// screen-sized targets too, which Navara reports after it draws them, so
|
||||
// it follows them (a fixed total coarsened the view on a 2x screen).
|
||||
let fixedGpuBytes = 0;
|
||||
let budgetedAt = -Infinity;
|
||||
removers.push(
|
||||
engine.frames.postRender.addEventListener(() => {
|
||||
const now = performance.now();
|
||||
if (now - budgetedAt < 1000) return;
|
||||
budgetedAt = now;
|
||||
const fixed = view.memoryStats()?.fixedGpuBytes ?? 0;
|
||||
if (fixed <= 0 || fixed === fixedGpuBytes) return;
|
||||
fixedGpuBytes = fixed;
|
||||
view.cacheBytes = fixed + TILE_CACHE_ROOM;
|
||||
}),
|
||||
);
|
||||
removeLook = () => removers.forEach((remove) => remove());
|
||||
} catch (error) {
|
||||
removeLook?.();
|
||||
@@ -251,6 +276,11 @@ export async function createNavaraGlobe({ container, search }) {
|
||||
engine.destroy();
|
||||
overlay.destroy();
|
||||
view.dispose();
|
||||
// postprocessing's fullscreen triangle is one module-level geometry:
|
||||
// every renderer that drew it left a 'dispose' listener holding its GL
|
||||
// state, which pins this canvas and the whole view. Disposing it drops
|
||||
// those listeners; the next view uploads it again.
|
||||
Pass.fullscreenGeometry.dispose();
|
||||
canvas.remove();
|
||||
},
|
||||
};
|
||||
|
||||
@@ -1,16 +1,24 @@
|
||||
// How the product builds its Navara view: render on demand (the engine's
|
||||
// render.setContinuous turns animation on), MSAA 4 as Cesium's msaaSamples,
|
||||
// the device pixel ratio, a canvas whose WebGL2 context keeps its drawing
|
||||
// render.setContinuous turns animation on), at CSS pixels without MSAA as
|
||||
// Cesium draws, or in HD at the device pixel ratio with MSAA 4 as Cesium's
|
||||
// msaaSamples, a canvas whose WebGL2 context keeps its drawing
|
||||
// buffer (Cesium's contextOptions.webgl.preserveDrawingBuffer, for captures),
|
||||
// and a look: `cinematic` by default (navaraScenePlan below), Navara's own
|
||||
// `photoreal` scene (aerial perspective, tone mapping and SMAA, about 5 ms a
|
||||
// frame) with `?look=photoreal`, and `minimal` (sky, stars, sky light and sun
|
||||
// without post effects) for benches.
|
||||
|
||||
/** WebGL2 attributes Navara 0.2.1 asks for, plus preserveDrawingBuffer. */
|
||||
import { Vector2 } from 'three';
|
||||
|
||||
/**
|
||||
* WebGL2 attributes Navara 0.2.1 asks for, plus preserveDrawingBuffer, but
|
||||
* without antialias: Navara draws into its own (MSAA) targets and only copies
|
||||
* the result to the canvas, so a multisampled canvas costs about 36 MiB for
|
||||
* nothing (pixel-identical frames).
|
||||
*/
|
||||
export const NAVARA_CONTEXT_ATTRIBUTES = Object.freeze({
|
||||
alpha: true,
|
||||
antialias: true,
|
||||
antialias: false,
|
||||
depth: true,
|
||||
stencil: true,
|
||||
premultipliedAlpha: true,
|
||||
@@ -18,15 +26,19 @@ export const NAVARA_CONTEXT_ATTRIBUTES = Object.freeze({
|
||||
powerPreference: 'default',
|
||||
});
|
||||
|
||||
const MSAA_SAMPLES = 4;
|
||||
const HD_MSAA_SAMPLES = 4;
|
||||
|
||||
/** ThreeView options. */
|
||||
export function navaraViewOptions({ container, canvas } = {}) {
|
||||
/**
|
||||
* ThreeView options. Without `hd` the view draws at CSS pixels without MSAA
|
||||
* (on a 2x screen about 1 GiB less than HD; SMAA still smooths edges).
|
||||
*/
|
||||
export function navaraViewOptions({ container, canvas, hd = false } = {}) {
|
||||
return {
|
||||
container,
|
||||
...(canvas ? { canvas } : {}),
|
||||
animation: false,
|
||||
multisampling: MSAA_SAMPLES,
|
||||
...(hd ? {} : { pixelRatio: 1 }),
|
||||
multisampling: hd ? HD_MSAA_SAMPLES : 0,
|
||||
};
|
||||
}
|
||||
|
||||
@@ -175,6 +187,11 @@ const cinematic = (mobile) => ({
|
||||
// No cloud shadows on the ground: dark patches over the map
|
||||
// read as features (and cost nothing measurable either way).
|
||||
shadows: false,
|
||||
// The clouds still shade themselves from these maps. The low
|
||||
// preset's 256² and 2 cascades, which Navara 0.2.1 overwrites
|
||||
// with its 512² and 3 (4 MiB instead of 24).
|
||||
shadowMapSize: new Vector2(256, 256),
|
||||
shadowCascadeCount: 2,
|
||||
// The two cumulus layers start at 1,500 m instead of 750 and
|
||||
// 1,000 m, above the city views (Austin's 600 m, Manhattan's
|
||||
// 900 m): clouds fill the sky without hiding the streets.
|
||||
|
||||
@@ -23,10 +23,17 @@ import {
|
||||
cloudsSunlitAt,
|
||||
} from './viewOptions.js';
|
||||
|
||||
test('the product view renders on demand with MSAA 4 at the native pixel ratio', () => {
|
||||
test('Navara renders on demand at CSS pixels without MSAA; Navara HD at the device pixel ratio with MSAA 4', () => {
|
||||
const container = { id: 'globe' };
|
||||
const canvas = { id: 'canvas' };
|
||||
assert.deepEqual(navaraViewOptions({ container, canvas }), {
|
||||
container,
|
||||
canvas,
|
||||
animation: false,
|
||||
pixelRatio: 1,
|
||||
multisampling: 0,
|
||||
});
|
||||
assert.deepEqual(navaraViewOptions({ container, canvas, hd: true }), {
|
||||
container,
|
||||
canvas,
|
||||
animation: false,
|
||||
@@ -34,10 +41,10 @@ test('the product view renders on demand with MSAA 4 at the native pixel ratio',
|
||||
});
|
||||
});
|
||||
|
||||
test('the canvas context keeps its drawing buffer, with the attributes Navara asks for', () => {
|
||||
test('the canvas context keeps its drawing buffer, with the attributes Navara asks for but no antialias', () => {
|
||||
assert.deepEqual(NAVARA_CONTEXT_ATTRIBUTES, {
|
||||
alpha: true,
|
||||
antialias: true,
|
||||
antialias: false,
|
||||
depth: true,
|
||||
stencil: true,
|
||||
premultipliedAlpha: true,
|
||||
@@ -99,6 +106,14 @@ test('the cinematic look: photoreal plus clouds and ACES, no sun shadow maps, no
|
||||
});
|
||||
assert.equal(clouds.clouds.coverage, 0.36);
|
||||
assert.equal(clouds.clouds.shadows, false, 'no cloud shadow maps');
|
||||
assert.deepEqual(
|
||||
[
|
||||
clouds.clouds.shadowMapSize.x,
|
||||
clouds.clouds.shadowMapSize.y,
|
||||
clouds.clouds.shadowCascadeCount,
|
||||
],
|
||||
[256, 256, 2],
|
||||
);
|
||||
assert.ok(
|
||||
clouds.clouds.cloudLayers.every(({ altitude }) => altitude >= 1000),
|
||||
'cloud layers sit above the city views',
|
||||
|
||||
Reference in new issue
Block a user