/** * @file renderer.ts * @brief WebGL2 implementation of the public renderer API. */ import { type BackdropPixels, type CornerKind, type RowOrder, type Scheme, type Stacking } from "../layout.js"; import { type Result } from "../result.js"; import { LAST_ERROR } from "./shared.js"; import { Frame } from "./frame.js"; import { type DisplayBrightness } from "../hdr.js"; export { LAST_ERROR }; /** * @brief Glass renderer bound to one WebGL2 context. * @details Shapes live in numbered slots; every call takes plain numbers so the per-frame path * stays allocation free. Create it with `createRenderer`. */ export declare class Renderer extends Frame { private constructor(); /** * @brief Build a renderer: compiles the pass programs and creates the shared GPU objects. * @returns The renderer, or the reason the context cannot be used. */ static create(gl: WebGL2RenderingContext): Result; /** * @brief Make every GPU object again after the context was lost and restored (the * `webglcontextrestored` event): pass and glass programs, the backdrop copy, mask fields and * blur pyramids. * @details Shapes, handles and materials stay as they were. A backdrop texture given with * `setBackdropTexture` belongs to the caller: give it again afterwards. * @returns 0, or why the context cannot be used yet. */ restore(): Result; /** @brief Set the target size in device pixels and the device pixels per point. */ resize(width: number, height: number, scale: number): void; /** * @brief Draw in extended range: a half-float drawing buffer whose values may pass 1 (the glass * is computed in extended range; the face limit and the rim light limit stay). * @details The context must have been created with `alpha: true` (browsers give a half-float * buffer only then; the renderer keeps it opaque), and the page must let the canvas show such * values, for example with `configureHighDynamicRange({ mode: "extended" })` where the browser * has it. * @param on Extended range on or off. * @param headroom Current brightness multiplier supplied by the host, finite and at least 1; * from 1.2 up the built-in materials drop the flat shadow part and the darker rim band * of standard output. * @returns True when the drawing buffer holds extended-range values now. */ setExtendedRange(on: boolean, headroom: number): boolean; /** * @brief Resolve supplied brightness state without changing the requested output range. * @returns The display multiplier, or an error that preserves the current configuration. */ setDisplayBrightness(state: DisplayBrightness): Result; /** * @brief Supply explicit color scales from an external rendering surface. * @details These are independent of display headroom. Identity (1, 1, 1) is the default; the * browser does not discover these surface coefficients. * @param contextScale Positive context color multiplier. * @param factor Surface EDR factor; 0 selects the default 1, negative values become 0 and * values above 1 become 1. * @param stateFactor Optional nonnegative state multiplier applied to the surface factor * before EDR resolve (default 1). It does not select context-scale normalization. * @returns Whether scale normalization is active now, or an error for nonfinite/invalid inputs. */ setSurfaceScale(contextScale: number, factor: number, stateFactor?: number): Result; /** * @brief Upload an image, canvas or video frame as the backdrop. * @details It must have the target size; it is drawn as is and sampled by the glass. The * renderer keeps its own copy: call again when the source changes. */ setBackdropSource(src: TexImageSource): void; /** * @brief Upload target-sized encoded extended-sRGB premultiplied pixels without an 8-bit copy. * @details RGB values must fit finite binary16; alpha is 0..1 and zero alpha has zero RGB. * Retains a snapshot for context restoration. Give new target-sized pixels after resizing. * Row order defaults to top down; no implicit color conversion or premultiplication occurs. * @returns 0 on upload, or a validation/context error. Invalid data preserves the prior source. */ setBackdropPixels(pixels: BackdropPixels): Result; /** @brief Remove the source and restore the empty backdrop, including after context loss. */ clearBackdrop(): void; private uploadBackdropPixels; /** * @brief Use a target-sized texture owned by the caller as the backdrop. * @details The renderer reads it whenever it has to rebuild something; call `markBackdropDirty` * after each change of its content. * @param rows Row order of the texture: `ROWS_TOP_DOWN` for DOM uploads, `ROWS_BOTTOM_UP` for * framebuffer renders. */ setBackdropTexture(texture: WebGLTexture, rows: RowOrder): void; /** * @brief Tell the renderer the content of the backdrop texture changed, so the blur pyramids * are rebuilt on the next render. */ markBackdropDirty(): void; /** * @brief Add a shape with the size-dependent standard material. * @param x Left edge in points. * @param y Top edge in points (y down). * @param w Width. * @param h Height. * @param radius Corner radius in points (clamped to half the short side). * @param corner Corner construction. * @param scheme Standard material for dark or light surroundings. * @returns Handle of the shape. It stops working once the shape is removed: calls with it are * ignored and never reach a shape added later. Later shapes are drawn over earlier ones and see * them through the glass. */ addShape(x: number, y: number, w: number, h: number, radius: number, corner: CornerKind, scheme: Scheme): number; /** * @brief Move or resize a shape; a built-in material follows the new short side. * @details Corner radii become equal again. */ setShape(handle: number, x: number, y: number, w: number, h: number, radius: number): void; /** * @brief Give each corner of a shape its own radius, in points. * @details The radii shrink together when two of them do not fit on an edge. */ setCorners(handle: number, topLeft: number, topRight: number, bottomRight: number, bottomLeft: number): void; /** * @brief Change how the corners of a shape are built (for a union: of every member with * corners). */ setCorner(handle: number, corner: CornerKind): void; /** @brief Give a shape the standard material for dark or light surroundings. */ setStandard(handle: number, scheme: Scheme): void; /** * @brief Give a shape the clear material: light blur, strong refraction, no shadow, backdrop * captured at half resolution. * @details The scheme selects the rim light colors for dark or light surroundings. */ setClear(handle: number, scheme: Scheme): void; /** * @brief Give a shape a preset: one of the built-in materials (`PRESET_*`), each with its own * laws over the size, the surroundings and, for small glass, the backdrop luminance. * @details An unknown number is ignored. */ setPreset(handle: number, preset: number, scheme: Scheme): void; /** @brief Preset number of a shape, or -1 for a dead handle. */ getPreset(handle: number): number; /** * @brief Lay a color over the glass of a shape: a gradient over the glass luminance through two * anchors of the tint hue. * @details Materials use the rule of their current scheme. A strength of 0 removes the layer. * @param r Red, 0..1. * @param g Green, 0..1. * @param b Blue, 0..1. * @param strength Strength, 0..1. */ setTint(handle: number, r: number, g: number, b: number, strength: number): void; /** * @brief Change the default color-separation multiplier for shapes that inherit it. * @details Clamps finite values to 0..1; nonfinite values restore the built-in default. */ setDefaultChromaticAberration(strength: number): void; /** @brief Current default color-separation multiplier. */ getDefaultChromaticAberration(): number; /** * @brief Override one shape's color separation without changing its other optical effects. * @details 0 disables separation, 1 preserves the preset distance, and null inherits the default. * Finite numbers are clamped to 0..1; nonfinite numbers use the built-in default. */ setChromaticAberration(handle: number, strength: number | null): void; /** @brief Effective color-separation multiplier, or -1 for a dead handle. */ getChromaticAberration(handle: number): number; /** * @brief Draw a shape with its shadow at an opacity of 0..1 over what lies below it (1 by * default). */ setOpacity(handle: number, opacity: number): void; /** * @brief Lay a flat grey over the finished glass inside the shape, above the rim lights: the * mark of a pressed control (white at a strength of 0.08 on dark glass, black at 0.08 on light * glass). * @details A strength of 0 removes it. * @param level Grey level, 0 black to 1 white. * @param strength Strength, 0..1. */ setVeil(handle: number, level: number, strength: number): void; /** * @brief Set how far a shape has materialised, at once: 1 is the shape as described, 0 hides it * (it keeps its slot and its GPU objects and is not drawn). * @details In between, the material is blended in from plain glass (no blur, lens, tone, glow * or shadow; the rim lights stay) and the frame is larger by 8 points times (1 - presence) on * every side (a mask shape keeps its frame). */ setPresence(handle: number, presence: number): void; /** * @brief Let a shape appear (presence 1) or disappear (0) over time: the presence follows a * critically damped spring of half a second and `render` reports true until it has settled. */ animatePresence(handle: number, presence: number): void; /** * @brief Add a union: several rounded rectangles drawn as one piece of glass whose outlines * flow into each other. * @details It is a shape (material, tint and removal work as for any shape); it shows once it * has a member. * @param spacing Distance in points over which neighboring members merge. * @param scheme Standard material for dark or light surroundings. * @returns Handle of the union. */ addUnion(spacing: number, scheme: Scheme): number; /** * @brief Add a rounded rectangle to a union (at most 16 members). * @returns Index of the member inside the union, or -1 when the handle is not a live union or * it is full. */ addMember(handle: number, x: number, y: number, w: number, h: number, radius: number, corner: CornerKind): number; /** * @brief Add a member of any outline to a union from a coverage mask (alpha channel, stretched * over the frame); at most 4 per union. * @details It merges with its neighbours like any member. * @returns Index of the member inside the union, or why it cannot be added. */ addMemberMask(handle: number, source: TexImageSource, x: number, y: number, w: number, h: number): Result; /** @brief Replace the mask of a member added with `addMemberMask`. */ setMemberMask(handle: number, index: number, source: TexImageSource): void; /** * @brief Move or resize a member of a union. * @details A member with corners of its own gets one radius again; a mask member keeps its mask * (the radius is not used). */ setMember(handle: number, index: number, x: number, y: number, w: number, h: number, radius: number): void; /** * @brief Give each corner of a union member its own radius, in points (radii that do not fit on * an edge shrink together). * @details Mask members are left as they are. */ setMemberCorners(handle: number, index: number, topLeft: number, topRight: number, bottomRight: number, bottomLeft: number): void; /** @brief Remove a member of a union; the last member takes its index. */ removeMember(handle: number, index: number): void; /** @brief Set the merge distance of a union in points. */ setSpacing(handle: number, spacing: number): void; /** * @brief Add a shape of any outline from a coverage mask (for example a canvas with the outline * filled). * @details The alpha channel of the mask is the coverage; the mask is stretched over the frame. * The distance field of the outline is rebuilt when the mask or the size of the frame changes, * not when the shape moves. * @param source Mask image. * @param x Left edge of the frame in points. * @param y Top edge in points. * @param w Width. * @param h Height. * @param scheme Standard material for dark or light surroundings. * @returns Handle of the shape, or why masks cannot be used on this context. */ addMask(source: TexImageSource, x: number, y: number, w: number, h: number, scheme: Scheme): Result; /** @brief Replace the mask of a shape added with `addMask`. */ setMask(handle: number, source: TexImageSource): void; /** * @brief Choose how shapes that lie over other shapes are drawn. * @param mode `STACKING_OVERLAP` (default): a shape refracts the lower glass and shadows its body * covers, but not glass its blur or refraction only reaches, so glass laid side by side is * captured in one batch. `STACKING_EXACT`: a shape refracts all the glass below it that its * lookups reach; every layer of that costs one more group of capture passes. `STACKING_FLAT`: * every shape refracts the backdrop only and all shapes are drawn in one batch. */ setStacking(mode: Stacking): void; /** * @brief Resolve built-in materials again with the supplied `ENV_*` flags. * @details Inactive windows flatten the material; contrast uses outline rings. Reduced motion * removes interior refraction and increases blur. */ setEnvironment(environment: number): void; /** @brief The `ENV_*` bits set with `setEnvironment`. */ getEnvironment(): number; /** * @brief Let a small built-in glass follow the backdrop luminance (default) or keep the fixed * tone of its scheme, as glass of a fixed size does. */ setAdaptive(handle: number, adaptive: boolean): void; /** * @brief Scheme a built-in glass shows now: 0 dark, 1 light, between the two while an adaptive * glass changes over; -1 for a dead handle. * @details Content on the glass can follow it. */ getScheme(handle: number): number; /** * @brief Current presence of a shape (0 hidden .. * @details 1 fully there), or -1 when its handle is dead. */ getPresence(handle: number): number; /** @brief True while the shape of a handle exists (it has not been removed). */ isAlive(handle: number): boolean; /** * @brief Turn every rim light, by `radians` clockwise from where its material puts it (0 by * default). * @details It follows a moving light source, the way a device's tilt turns the highlights of * the glass on it. */ setLightAngle(radians: number): void; /** @brief Draw a shape last, over every other shape. */ raise(handle: number): void; /** * @brief Remove a shape. * @details Its handle is dead from now on; the slot behind it may serve a shape added later. */ removeShape(handle: number): void; /** @brief Release every GPU object the renderer created. */ dispose(): void; } /** * @brief Create a renderer for a WebGL2 context. * @returns The renderer, or the reason the context cannot be used. */ export declare function createRenderer(gl: WebGL2RenderingContext): Result;