diff --git a/.github/workflows/build-pull-request.yml b/.github/workflows/build-pull-request.yml index 4ff842ed..964add54 100644 --- a/.github/workflows/build-pull-request.yml +++ b/.github/workflows/build-pull-request.yml @@ -4,6 +4,10 @@ on: pull_request: types: ['opened', 'synchronize'] +concurrency: + group: ${{ github.workflow }}-pr-${{ github.event.pull_request.number }} + cancel-in-progress: true + jobs: build-pull-request: name: Build pull request diff --git a/.github/workflows/docker-pr.yml b/.github/workflows/docker-pr.yml index 997aba6d..24b9389c 100644 --- a/.github/workflows/docker-pr.yml +++ b/.github/workflows/docker-pr.yml @@ -6,6 +6,10 @@ on: - 'Dockerfile' - '.github/workflows/docker-pr.yml' +concurrency: + group: ${{ github.workflow }}-pr-${{ github.event.pull_request.number }} + cancel-in-progress: true + jobs: docker-build: runs-on: ubuntu-latest diff --git a/.github/workflows/lockfile.yml b/.github/workflows/lockfile.yml index a9cc1a34..e96d2818 100644 --- a/.github/workflows/lockfile.yml +++ b/.github/workflows/lockfile.yml @@ -5,6 +5,10 @@ on: paths: - 'package-lock.json' +concurrency: + group: ${{ github.workflow }}-pr-${{ github.event.pull_request.number }} + cancel-in-progress: true + jobs: lockfile_changes: runs-on: ubuntu-latest diff --git a/.github/workflows/rat-dev-preview.yml b/.github/workflows/rat-dev-preview.yml index 45da8bc4..47f22b7e 100644 --- a/.github/workflows/rat-dev-preview.yml +++ b/.github/workflows/rat-dev-preview.yml @@ -4,6 +4,13 @@ on: push: branches: ['dev'] +# One run per ref wins: a newer push to dev cancels the older queued or running one, so the run +# that builds rat-dev-dist is always the newest. Pull-request runs are other workflows and other +# groups; they never cancel it. +concurrency: + group: rat-dev-preview + cancel-in-progress: true + jobs: build: name: Build dev preview diff --git a/src/app/features/scene/skyBand.ts b/src/app/features/scene/skyBand.ts index ae6d5791..b6b39315 100644 --- a/src/app/features/scene/skyBand.ts +++ b/src/app/features/scene/skyBand.ts @@ -28,20 +28,19 @@ export function hexToLinear(hex: string): Linear { return [ch(1), ch(3), ch(5)]; } -/** How a displayed colour is read: as encoded, or as the 8-bit screen can show it, the sky pass dithers by half a step either way ('low', 'high'). */ +/** How a displayed colour is read: as encoded, or moved one 8-bit step down or up in every channel ('low', 'high'). */ export type Reading = 'exact' | 'low' | 'high'; /** * Relative luminance of what the terrain shaders put on screen for a linear colour: they encode with - * pow(1/2.2), not the sRGB curve, so the screen colour is read back through the sRGB curve. 'low' and 'high' read - * it after 8-bit quantisation with the dither at its worst, so the bounds they give hold on the screen. + * pow(1/2.2), not the sRGB curve, so the screen colour is read back through the sRGB curve. 'low' and 'high' move every + * encoded channel one 8-bit step down or up first: rounding to 8 bits (half a step) plus the sky pass's dither (half a + * step) cannot move a channel further, so they bound what the screen shows. A smooth bound, so a search can climb it. */ export function displayedLuminance(c: Linear, reading: Reading = 'exact'): number { + const shift = { exact: 0, low: -1 / 255, high: 1 / 255 }[reading]; const ch = (v: number) => { - let e = Math.min(1, Math.max(0, v)) ** (1 / 2.2); - if (reading !== 'exact') { - e = Math.min(255, Math.max(0, Math.round(e * 255 + (reading === 'low' ? -0.5 : 0.5)))) / 255; - } + const e = Math.min(1, Math.max(0, Math.min(1, Math.max(0, v)) ** (1 / 2.2) + shift)); return e <= 0.04045 ? e / 12.92 : ((e + 0.055) / 1.055) ** 2.4; }; return luma([ch(c[0]), ch(c[1]), ch(c[2])]); @@ -59,7 +58,7 @@ export interface SkyBand { /** * The shaping targets, in linear luma. The floor sits 0.019 above the text bound (0.2983): the screen encoding is * gamma 2.2, which reads about 0.006 darker through the sRGB curve at this level, and 8-bit rounding with the sky - * pass's dither at its worst takes about 0.006 more. + * pass's dither at its worst (one step in every channel) takes about 0.007 more. */ export const SKY_BAND: Record = { day: { floor: 0.317, ceiling: 1, lift: hexToLinear('#e2f4ff') }, diff --git a/src/app/features/scene/skyForState.test.ts b/src/app/features/scene/skyForState.test.ts index 116e7f9f..2ad3b5f6 100644 --- a/src/app/features/scene/skyForState.test.ts +++ b/src/app/features/scene/skyForState.test.ts @@ -230,23 +230,77 @@ describe('the terrain sky lies in the band its text ink needs', () => { }); describe('skyBackdrop holds the union of what the scenes draw', () => { + // An independent extremum of the screen reading ('low' for the darkest, 'high' for the lightest: one 8-bit step each way) over the terrain camera's reach: not terrainDrawn's grid and compass search but alternating golden-section + // line searches in elevation and azimuth from 60 starts (a golden-angle spiral and scatter about the sun), the bracket + // shrinking to 1e-3 degrees. A plateau (the shader clamps) cannot stall it: each line search always + // moves to the better end of its bracket. + const extremes = (state: FixedSkyState) => { + const sky = chooseSky(terrainSkyFor(state)); + const rad = Math.PI / 180; + const f = (el: number, az: number, reading: 'low' | 'high') => { + const e = Math.min(TERRAIN_REACH_DEG, Math.max(0, el)) * rad; + const a = az * rad; + return displayedLuminance( + skyColour(sky, [Math.cos(e) * Math.sin(a), Math.sin(e), -Math.cos(e) * Math.cos(a)]), + reading + ); + }; + const P = 0.6180339887; + const line = (g: (x: number) => number, mid: number, w: number) => { + let a = mid - w; + let b = mid + w; + for (let k = 0; k < 40; k += 1) { + const c = b - P * (b - a); + const d = a + P * (b - a); + if (g(c) > g(d)) b = d; + else a = c; + } + return (a + b) / 2; + }; + const hunt = (sign: 1 | -1) => { + const reading = sign === 1 ? 'high' : 'low'; + let best = -Infinity; + for (let i = 0; i < 60; i += 1) { + let el = + i % 2 + ? sky.elevation + (((i * 7919) % 1600) - 800) / 100 + : Math.asin(((i + 0.5) / 60) * Math.sin(TERRAIN_REACH_DEG * rad)) / rad; + let az = + i % 2 ? sky.azimuth + (((i * 104729) % 1600) - 800) / 100 : (i * 137.50776405) % 360; + for (let w = 8; w > 1e-3; w /= 2) { + const [el0, az0] = [el, az]; + el = line((x) => sign * f(Math.max(0, x), az0, reading), el0, w); + const el1 = el; + az = line((x) => sign * f(el1, x, reading), az0, w); + best = Math.max(best, sign * f(el, az, reading)); + } + } + return sign * best; + }; + return { darkest: hunt(-1), lightest: hunt(1) }; + }; + const computed = (state: FixedSkyState) => { const r = [ridgeDrawn(state), terrainDrawn(state)]; + const ind = extremes(state); return { - darkest: Math.min(...r.map((x) => x.darkest)), - lightest: Math.max(...r.map((x) => x.lightest)), + darkest: Math.min(...r.map((x) => x.darkest), ind.darkest), + lightest: Math.max(...r.map((x) => x.lightest), ind.lightest), }; }; + // The constants are the swept extremes rounded outward to six places, so each lies in [extreme, extreme + 1e-6): + // a constant 1e-6 low is under the extreme, one 2e-5 high is over the bound. SEARCH is the searches' own error. + const SEARCH = 1e-8; it.each(STATES)( - '%s: the constant bounds the computed range and sits within 1e-5 of it', + '%s: the constant is the swept extreme rounded outward, within 1e-6 above it', (state) => { const c = computed(state); const b = skyBackdrop(state); - expect(b.darkest).toBeLessThanOrEqual(c.darkest + EPS); - expect(b.lightest).toBeGreaterThanOrEqual(c.lightest - EPS); - expect(c.darkest - b.darkest).toBeLessThan(1e-5); - expect(b.lightest - c.lightest).toBeLessThan(1e-5); + expect(b.darkest).toBeLessThanOrEqual(c.darkest); + expect(b.lightest).toBeGreaterThanOrEqual(c.lightest); + expect(c.darkest - b.darkest).toBeLessThan(1e-6 + SEARCH); + expect(b.lightest - c.lightest).toBeLessThan(1e-6 + SEARCH); } ); diff --git a/src/app/features/scene/skyForState.ts b/src/app/features/scene/skyForState.ts index a40317a2..7da17476 100644 --- a/src/app/features/scene/skyForState.ts +++ b/src/app/features/scene/skyForState.ts @@ -107,77 +107,72 @@ export function ridgeDrawn(state: SkyState): Range { export const TERRAIN_REACH_DEG = Math.ceil((CAMERA.fov / 2 - CAMERA.pitch) * (180 / Math.PI) + 1.5); const SAMPLE_DEG = 3; -const SUN_DEG = 0.25; +const SUN_DEG = 0.1; const SUN_SPAN_DEG = 8; const HORIZON_DEG = 0.5; const REFINE_FROM = 6; /** - * The terrain sky's range, as displayed (`displayedLuminance`): `skyColour` (the CPU twin of its sky shader) over the part of the sky the camera reaches (0 to TERRAIN_REACH_DEG, every azimuth: it turns), for the sky the scene picks for this state. A 3 degree grid, a 0.5 degree ring of the horizon, the - * top of the reach and a 0.25 degree patch around the sun find where the extremes are; each of the best few is then refined by a - * compass search whose step halves from 1 degree to under 0.001, because the sun's glow peaks (and the sky's darkest - * patch sits) between any fixed grid's samples. + * The terrain sky's range as the 8-bit screen shows it (`displayedLuminance`, read 'low' for the darkest and 'high' for the + * lightest: one 8-bit step each way, rounding and dither at their worst): `skyColour` (the CPU twin of its sky shader) over the part of the sky the camera reaches + * (0 to TERRAIN_REACH_DEG, every azimuth: it turns), for the sky the scene picks for this state. A 3 degree grid, a 0.5 degree + * ring of the horizon, the top of the reach and a 0.25 degree patch around the sun find where the extremes are; each of the + * best few is then refined by a compass search whose step halves from 1 degree to under 5e-7, because the sun's glow peaks + * (and the sky's darkest patch sits) between any fixed grid's samples. */ export function terrainDrawn(state: SkyState): Range { const sky = chooseSky(terrainSkyFor(state)); const rad = Math.PI / 180; - // Every point looked at is also read as the 8-bit screen would show it, dither at its worst, so the range bounds that too. - let screenDarkest = Infinity; - let screenLightest = -Infinity; - const lum = (el: number, az: number) => { - const e = Math.min(TERRAIN_REACH_DEG, Math.max(0, el)) * rad; - const a = az * rad; - const c = skyColour(sky, [Math.cos(e) * Math.sin(a), Math.sin(e), -Math.cos(e) * Math.cos(a)]); - screenDarkest = Math.min(screenDarkest, displayedLuminance(c, 'low')); - screenLightest = Math.max(screenLightest, displayedLuminance(c, 'high')); - return displayedLuminance(c); - }; - const seen: { el: number; az: number; l: number }[] = []; - const at = (el: number, az: number) => seen.push({ el, az, l: lum(el, az) }); - for (let el = 0; el <= TERRAIN_REACH_DEG; el += SAMPLE_DEG) { - for (let az = 0; az < 360; az += SAMPLE_DEG) at(el, az); - } - for (let az = 0; az < 360; az += HORIZON_DEG) at(0, az); - at(TERRAIN_REACH_DEG, 0); - for (let dEl = -SUN_SPAN_DEG; dEl <= SUN_SPAN_DEG; dEl += SUN_DEG) { - for (let dAz = -SUN_SPAN_DEG; dAz <= SUN_SPAN_DEG; dAz += SUN_DEG) { - at(sky.elevation + dEl, sky.azimuth + dAz); + type Point = { el: number; az: number; read: number; score: number }; + const extreme = (reading: 'low' | 'high', sign: 1 | -1): number => { + const point = (el: number, az: number): Point => { + const e = Math.min(TERRAIN_REACH_DEG, Math.max(0, el)) * rad; + const a = az * rad; + const c = skyColour(sky, [ + Math.cos(e) * Math.sin(a), + Math.sin(e), + -Math.cos(e) * Math.cos(a), + ]); + const read = displayedLuminance(c, reading); + return { el, az, read, score: read }; + }; + const seen: Point[] = []; + const at = (el: number, az: number) => seen.push(point(el, az)); + for (let el = 0; el <= TERRAIN_REACH_DEG; el += SAMPLE_DEG) { + for (let az = 0; az < 360; az += SAMPLE_DEG) at(el, az); } - } - type Point = { el: number; az: number; l: number }; - const around = (p: Point, step: number): Point[] => - [ - [step, 0], - [-step, 0], - [0, step], - [0, -step], - ].map(([dEl, dAz]) => { - const el = Math.min(TERRAIN_REACH_DEG, Math.max(0, p.el + dEl)); - return { el, az: p.az + dAz, l: lum(el, p.az + dAz) }; - }); - const refine = (from: Point, sign: 1 | -1) => { - let best = from; - for (let step = 1; step > 0.0005; step /= 2) { - let improved = true; - while (improved) { - const cur = best; - best = around(cur, step).reduce((b, c) => (sign * c.l > sign * b.l ? c : b), cur); - improved = best !== cur; + for (let az = 0; az < 360; az += HORIZON_DEG) at(0, az); + at(TERRAIN_REACH_DEG, 0); + for (let dEl = -SUN_SPAN_DEG; dEl <= SUN_SPAN_DEG; dEl += SUN_DEG) { + for (let dAz = -SUN_SPAN_DEG; dAz <= SUN_SPAN_DEG; dAz += SUN_DEG) { + at(sky.elevation + dEl, sky.azimuth + dAz); } } - return best.l; - }; - const byLight = [...seen].sort((p, q) => q.l - p.l); - const lightest = Math.max( - byLight[0].l, - ...byLight.slice(0, REFINE_FROM).map((p) => refine(p, 1)) - ); - const darkest = Math.min( - byLight[byLight.length - 1].l, - ...byLight.slice(-REFINE_FROM).map((p) => refine(p, -1)) - ); - return { - darkest: Math.min(darkest, screenDarkest), - lightest: Math.max(lightest, screenLightest), + const around = (p: Point, step: number): Point[] => + [ + [step, 0], + [-step, 0], + [0, step], + [0, -step], + ].map(([dEl, dAz]) => + point(Math.min(TERRAIN_REACH_DEG, Math.max(0, p.el + dEl)), p.az + dAz) + ); + const refine = (from: Point) => { + let best = from; + for (let step = 1; step > 5e-7; step /= 2) { + let improved = true; + while (improved) { + const cur = best; + best = around(cur, step).reduce((b, c) => (sign * c.score > sign * b.score ? c : b), cur); + improved = best !== cur; + } + } + return best.read; + }; + const ranked = [...seen].sort((p, q) => sign * (q.score - p.score)); + return sign === 1 + ? Math.max(ranked[0].read, ...ranked.slice(0, REFINE_FROM).map(refine)) + : Math.min(ranked[0].read, ...ranked.slice(0, REFINE_FROM).map(refine)); }; + return { darkest: extreme('low', -1), lightest: extreme('high', 1) }; } diff --git a/src/app/features/scene/skyState.ts b/src/app/features/scene/skyState.ts index a79ed9c6..666e83d6 100644 --- a/src/app/features/scene/skyState.ts +++ b/src/app/features/scene/skyState.ts @@ -127,11 +127,11 @@ export interface SkyBackdrop { // this module, which the main chunk loads, does not pull in the terrain sky and the baked scattering // tables with it; skyForState.test.ts fails when they drift from the computed union. The ridge sky counts // over its whole range, the terrain sky over what its frozen camera reaches (TERRAIN_REACH_DEG, skyForState.ts). -// Bounds what the 8-bit screen shows too (dither at its worst), rounded outward to six places. +// Bounds what the 8-bit screen shows too (one step each way for rounding and dither), rounded outward to six places. const BACKDROPS: Record = { - day: { darkest: 0.463644, lightest: 0.898318 }, - dusk: { darkest: 0.306293, lightest: 0.968113 }, - night: { darkest: 0.05987, lightest: 0.081133 }, + day: { darkest: 0.461063, lightest: 0.901108 }, + dusk: { darkest: 0.30619, lightest: 0.968744 }, + night: { darkest: 0.05987, lightest: 0.081705 }, }; export const skyBackdrop = (state: SkyState): SkyBackdrop => BACKDROPS[resolveSkyState(state)]; diff --git a/src/app/features/scene/skyTheme.test.ts b/src/app/features/scene/skyTheme.test.ts index 704ff15d..f6873466 100644 --- a/src/app/features/scene/skyTheme.test.ts +++ b/src/app/features/scene/skyTheme.test.ts @@ -63,9 +63,9 @@ describe('ink of the theme a state picks, on bare sky', () => { // numbers are recorded as they are measured (Night's #609385 would clear 4.5 at a luminance of about 0.53 or more). // 'auto' resolves from the clock to one of these three and has the same numbers. const cells: [FixedSkyState, string, string, string, string, string][] = [ - ['day', 'noonday-sky', '#342d25', '6.64', '#2a4f27', '4.57'], + ['day', 'noonday-sky', '#342d25', '6.60', '#2a4f27', '4.55'], ['dusk', 'noonday-sky', '#342d25', '4.60', '#2a4f27', '3.17'], - ['night', 'midnight-sky', '#aeccff', '4.91', '#609385', '2.29'], + ['night', 'midnight-sky', '#aeccff', '4.89', '#609385', '2.28'], ]; it.each(cells)(