Depth effects

This commit is contained in:
James Babcock committed 2026-09-22 10:59:51 -07:00
1 parent 4320021222
commit 392554dd37
26 files changed
+623 -136

No files matched your search

+56 -19
View File
@@ -1,4 +1,4 @@
import { GrayImage, type DeferredDraw } from "./image";
import { GrayImage, type DeferredDraw, type PlacedImage } from "./image";
/**
* The unit apps (and the shell) submit to the compositor: instead of one
@@ -8,20 +8,17 @@ import { GrayImage, type DeferredDraw } from "./image";
* transparent, 1 is black (the two quantize to the same 4bpp shade, so
* reserving 0 costs nothing visible).
*
* This representation exists to carry structure the wire pipeline will later
* exploit: a plane's glyph list can become on-glasses display-list references
* (texture cache), and the offset can diverge per lens for stereo depth. For
* now the compositor boundary just flattens (flattenPlanes) and feeds the
* existing incremental-update and screen-preview pipeline.
* Static planes form the incremental screen buffer. Planes with stereo depth
* and retained presentation draws replay from resources in the root display list.
*/
export type Plane = {
shellKey?: number;
depth?: number;
dimUnderneath?: number;
image: GrayImage;
/**
* Offset of the plane's top-left within the submitted frame. Applied
* identically to both lenses for now; a per-lens divergence (stereo depth
* for floating overlays) is the planned extension.
* before any per-lens horizontal offset from depth.
*/
x: number;
y: number;
@@ -72,8 +69,9 @@ export function flattenPlanes(
/**
* Flatten planes and also return the frame's deferred draws (glyphs and
* images), translated into frame coordinates and in bake order (plane order,
* then each image's draw order). The flattened image has every draw baked in,
* exactly as flattenPlanes produces; the list preserves the draws' identity
* then each image's draw order). Regular draws are baked into the image;
* selected-menu presentation records remain unbaked for root-list replay.
* The list preserves the draws' identity
* so the texture-cache pipeline can replay them as on-glasses cached draws
* (see graphics/glyph-wire.ts).
*/
@@ -81,15 +79,54 @@ export function flattenPlanesWithDraws(
planes: readonly Plane[],
size?: { width: number; height: number },
): { image: GrayImage; draws: DeferredDraw[] } {
const image = flattenPlanes(planes, size);
const rasters = planes.map(p => p.image.withDrawsBaked(false));
const image = flattenPlanes(planes.map((plane, i) => ({ ...plane, image: plane.depth ? new GrayImage(plane.image.width, plane.image.height) : rasters[i]! })), size);
const draws: DeferredDraw[] = [];
for (const plane of planes) {
for (const placed of plane.image.draws) {
draws.push(
plane.x === 0 && plane.y === 0
? placed
: { ...placed, x: placed.x + plane.x, y: placed.y + plane.y },
);
for (const [index, plane] of planes.entries()) {
const retained: DeferredDraw[] = [];
if (plane.depth) {
// Keep the whole menu off the screen buffer, including its black background.
const raster = rasters[index]!;
let left = raster.width, top = raster.height, right = -1, bottom = -1;
for (let y = 0; y < raster.height; y++) for (let x = 0; x < raster.width; x++) if (raster.pixels[y*raster.width+x]) {
left = Math.min(left,x); top = Math.min(top,y); right = Math.max(right,x); bottom = Math.max(bottom,y);
}
if (right >= left) {
const crop = new GrayImage(right-left+1,bottom-top+1);
for(let y=0;y<crop.height;y++) crop.pixels.set(raster.pixels.subarray((y+top)*raster.width+left,(y+top)*raster.width+right+1),y*crop.width);
retained.push({ kind: "image", source: crop, x: left, y: top,
presentation: { mode: "masked-image", radius: 0, background: 0, border: 0, depth: plane.depth } });
}
}
const planeDraws = plane.depth ? [...retained, ...plane.image.draws.filter(d => d.kind === "image" && d.presentation).map(d => {
const draw = d as PlacedImage;
return { ...draw, presentation: { ...draw.presentation!, depth: draw.presentation!.depth + plane.depth! } };
})] : plane.image.draws;
for (const placed of planeDraws) {
const translated = { ...placed, x: placed.x + plane.x, y: placed.y + plane.y };
if (translated.kind === "image" && translated.presentation) {
// Restore opaque pixels from later planes after replaying this row. The screen
// already contains those pixels; rect copies preserve rounded/irregular occlusion.
const occlusions: { x: number; y: number; width: number; height: number }[] = [];
const depth = translated.presentation.depth;
const leftShift = Math.floor(depth / 2), rightShift = -Math.floor((depth + 1) / 2);
const left = Math.max(0, translated.x + Math.min(leftShift, rightShift));
const right = Math.min(image.width, translated.x + translated.source.width + Math.max(leftShift, rightShift));
for (let y = Math.max(0, translated.y); y < Math.min(image.height, translated.y + translated.source.height); y++) {
let start = -1;
for (let x = left; x <= right; x++) {
const covered = x < right && planes.some((p, i) => i > index && !p.depth && x >= p.x && y >= p.y && x < p.x + p.image.width && y < p.y + p.image.height && rasters[i]!.pixels[(y-p.y)*p.image.width+x-p.x] !== 0);
if (covered && start < 0) start = x;
if (!covered && start >= 0) {
const previous = occlusions.find(r => r.x === start && r.width === x - start && r.y + r.height === y);
if (previous) previous.height++; else occlusions.push({ x: start, y, width: x-start, height: 1 });
start = -1;
}
}
}
translated.presentation = { ...translated.presentation, occlusions };
}
draws.push(translated);
}
}
return { image, draws };
@@ -102,6 +139,6 @@ export function flattenPlanesWithDraws(
*/
export function planesFingerprint(planes: readonly Plane[]): string {
return planes
.map((plane) => `${plane.x},${plane.y}+${plane.image.width}x${plane.image.height}:${plane.image.fingerprint()}`)
.map((plane) => `${plane.depth ?? 0}:${plane.x},${plane.y}+${plane.image.width}x${plane.image.height}:${plane.image.fingerprint()}`)
.join("|");
}