@vcollab/shader-webgl2 0.0.0-stage → 2.0.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/CHANGELOG.md ADDED
@@ -0,0 +1,93 @@
1
+ # Changelog
2
+
3
+ All notable changes to `@vcollab/shader-webgl2` are documented here.
4
+
5
+ The format follows [Keep a Changelog](https://keepachangelog.com/en/1.1.0/) and
6
+ this package adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0.html).
7
+
8
+ ## [2.0.0] — 2026-12-06
9
+
10
+ Buffers and programs are separate objects. A slab's buffers are uploaded once
11
+ and drawn by any pipeline; switching pipelines uploads nothing, and one
12
+ geometry can be drawn at several places with different uniforms.
13
+
14
+ ### Added
15
+
16
+ - `GLGeometry`: one slab's attribute and index buffers, independent of any
17
+ program, with `GeometrySource` and `GeometryAttribute`. Validates the source
18
+ before allocating anything (element type, `components` a positive integer,
19
+ whole vertices, one vertex count), and binds the index buffer as
20
+ `ELEMENT_ARRAY_BUFFER` with no vertex array current. A geometry can share
21
+ another's buffers and indices and add its own; it is disposed with the one
22
+ it shares. Disposing a geometry releases every vertex array built over it.
23
+ - `GLPipeline.draw(geometry)`, `prepare(geometry)` and `release(geometry)`.
24
+ A pipeline builds one vertex array per geometry on first use and keeps it:
25
+ attribute locations differ between envelopes, so it cannot be shared
26
+ between pipelines.
27
+ - `GLPipeline.disposed`; `dispose` is idempotent, and a disposed pipeline
28
+ refuses to draw.
29
+ - `PickDraw`, the function `PickPass.pick` hands its callback.
30
+
31
+ ### Changed
32
+
33
+ - **Breaking:** `GLPipeline.setGeometry` and the `Geometry` type are removed;
34
+ build a `GLGeometry` and pass it to `draw`. `draw()` without an argument
35
+ is gone, and a geometry with no indices draws nothing.
36
+ - **Breaking:** `new PickPass(gl)` owns no pipeline and `PickPass.pipeline` is
37
+ removed. `pick(x, y, drawScene)` calls `drawScene(draw)` once, and the
38
+ callback issues every draw — `draw(pipeline, geometry)` — through any
39
+ number of pick pipelines, writing each one's uniforms itself. `draw` refuses
40
+ a pipeline without the `pick` capability. `drawScene` is not called for a
41
+ point outside the target.
42
+ - The pick draws are scissored to the pixel read back; the scissor test and
43
+ box are put back afterwards.
44
+ - The constructor refuses an attribute type it cannot feed before compiling,
45
+ and a contract mismatch in a geometry is reported at its first draw (or
46
+ `prepare`) rather than when it is set.
47
+ - `PickHit` carries `groupId`, from `decodePick`.
48
+ - Takes `@vcollab/shader-library` 2.0.0.
49
+
50
+ ## [1.0.0] — 2026-12-03
51
+
52
+ First release. Extracted from the playground's renderer, which had already
53
+ written the binding layer generically; this package is that code with the
54
+ playground's policy removed.
55
+
56
+ ### Added
57
+
58
+ - `GLPipeline`: one envelope bound to a WebGL2 context — compiles and links the
59
+ stage pair, binds the emitted uniform block, matches attributes and resources
60
+ by semantic, uploads the block and draws. It decides no values.
61
+ - The uniform buffer is sized at the larger of the envelope's `blockSize` and
62
+ the driver's `UNIFORM_BLOCK_DATA_SIZE`; a driver block too small for the
63
+ reflected layout is refused.
64
+ - `setGeometry` checks each source against the contract: `int`/`ivecN`
65
+ inputs take `Int32Array` data, `float`/`vecN` inputs `Float32Array`,
66
+ `components` is at least the declared width, and
67
+ `AttributeSource.integer` agrees with the declared type. Calling it again
68
+ frees the previous geometry's buffers.
69
+ - An attribute the caller did not supply reads a zero constant, set at every
70
+ draw because it is context state rather than vertex-array state.
71
+ - `draw` throws `GLPipelineError` naming every semantic in
72
+ `requiredSemantics` that has no texture.
73
+ - Every GL object made before a failed step is freed before the error is
74
+ rethrown.
75
+ - `PickPass`: the pick pipeline drawn into an offscreen `rgba32ui` target with
76
+ a depth buffer, one pixel read back and decoded. Hit-testing is a second draw
77
+ rather than a second attachment on the color pipeline — see
78
+ [docs/design.md](docs/design.md) §4 for why.
79
+ - `pick` sets the state the pass depends on (scissor test off, every color
80
+ and depth write on, depth testing on with `LESS`, blending off), resets
81
+ the readback to `PICK_EMPTY` before reading, and afterwards restores that
82
+ state, the viewport and the caller's draw and read framebuffers, even if
83
+ the draw throws. Face culling is left as the caller set it.
84
+ - `fromCanvasPoint` converts a CSS-pixel offset to the integer device pixel
85
+ GL reads: `x = floor(offsetX * scaleX)`,
86
+ `y = height - 1 - floor(offsetY * scaleY)`.
87
+ - `resize` rebuilds only when the floored size changes, checks every object
88
+ and the framebuffer's completeness, and puts back the caller's bindings.
89
+ - `linkProgram`, `UNIFORM_BLOCK_NAME` and `UNIFORM_BLOCK_BINDING` for a caller
90
+ managing its own program.
91
+ - Documented: context loss is not handled (rebuild every object after
92
+ `webglcontextrestored`), and an integer table (`usampler2D`) needs `NEAREST`
93
+ filtering and no mipmaps.
package/LICENSE ADDED
@@ -0,0 +1,8 @@
1
+ Copyright (c) 2018-2026 Visual Collaboration Technologies Inc.
2
+ All Rights Reserved.
3
+
4
+ This software is proprietary and confidential. Unauthorized copying,
5
+ distribution, or use of this software, via any medium, is strictly
6
+ prohibited. Access is granted only to authorized VCollab developers
7
+ and individuals explicitly authorized by Visual Collaboration
8
+ Technologies Inc.
package/README.md CHANGED
@@ -1,3 +1,199 @@
1
- # Temporary Holding Version
1
+ # @vcollab/shader-webgl2
2
2
 
3
- This version is a temporary placeholder for this package. An operational version to replace this has been submitted for review and is awaiting a staged release.
3
+ Binds a VCollab shader pipeline envelope to a WebGL2 context: geometry,
4
+ programs, uniform blocks, resources, and the pick pass.
5
+
6
+ ## Overview
7
+
8
+ The shaders are built offline and shipped as an artifact set.
9
+ [`@vcollab/shader-library`](../shader-library/README.md) reads that set and
10
+ tells a renderer what to bind and where; this is the renderer half for WebGL2.
11
+
12
+ Everything it does is driven by the envelope and nothing by knowledge of a
13
+ particular shader: the contract says which attributes to supply, the layout
14
+ says where each uniform sits, and the bindings say which sampler uniform and
15
+ texture unit each resource takes. A pipeline this package has never heard of
16
+ binds correctly as long as the set describes it.
17
+
18
+ **It decides no values.** Which uniforms to write, what to put in them, and
19
+ what a table or image holds are the caller's. That split is what keeps a
20
+ viewer's policy out of the binding layer, and the binding layer out of every
21
+ viewer.
22
+
23
+ ## Installation
24
+
25
+ ```bash
26
+ pnpm add @vcollab/shader-webgl2 @vcollab/shader-library
27
+ ```
28
+
29
+ ## Quick Start
30
+
31
+ ```ts
32
+ import { openBundledSet } from "@vcollab/shader-library/bundled";
33
+ import { GLGeometry, GLPipeline } from "@vcollab/shader-webgl2";
34
+
35
+ const set = await openBundledSet();
36
+
37
+ // The slab's buffers, uploaded once. Attributes are matched by the contract's
38
+ // semantic, never by declared name. A float/vecN input takes a Float32Array,
39
+ // an int/ivecN input an Int32Array.
40
+ const geometry = new GLGeometry(gl, {
41
+ attributes: new Map([
42
+ ["coordinates", { data: positions, components: 3, integer: false }], // Float32Array
43
+ ["atom-id", { data: atomIds, components: 1, integer: true }], // Int32Array
44
+ ]),
45
+ indices, // Uint32Array
46
+ });
47
+
48
+ // A compiled program for one combination of features. It draws any geometry.
49
+ const pipeline = new GLPipeline(gl, await set.envelope("deform3.symtensor"));
50
+
51
+ // Resources by semantic too — the declared name does not reach the program.
52
+ // Every semantic the pipeline binds needs a texture, or draw() throws.
53
+ for (const semantic of pipeline.requiredSemantics) {
54
+ const texture = textures.get(semantic);
55
+ if (texture === undefined) throw new Error(`no texture for ${semantic}`);
56
+ pipeline.setTexture(semantic, texture);
57
+ }
58
+
59
+ function frame(camera) {
60
+ pipeline.uniforms.set("u_mvpMatrix", camera.mvp); // column-major in
61
+ pipeline.uniforms.set("u_colorSource", 2);
62
+ pipeline.draw(geometry);
63
+ }
64
+ ```
65
+
66
+ Buffers and programs change at different rates, so they are separate objects.
67
+ A slab's buffers live as long as the model; the program changes whenever the
68
+ result, the fringe, the deformation or capping is switched, and a pick draws
69
+ the same slab through a program of its own. Switching pipelines uploads
70
+ nothing: each pipeline builds a vertex array over a geometry the first time it
71
+ draws it, and keeps it.
72
+
73
+ An attribute you do not supply is left as a disabled array with a constant,
74
+ which is how a viewer expresses "this draw has no such data" — the same
75
+ "off state is data" the always-present features rely on.
76
+
77
+ ### One mesh, several places
78
+
79
+ The uniforms and textures are whatever was last set, so drawing one geometry
80
+ at several places is a loop. Give each occurrence its own block of the scene
81
+ mask — `TableBlocks` in `@vcollab/shader-library` hands them out — and set its
82
+ base before its draw:
83
+
84
+ ```ts
85
+ for (const occurrence of occurrences) {
86
+ pipeline.uniforms.set("u_mvpMatrix", occurrence.mvp);
87
+ pipeline.uniforms.set("u_maskLutBase", occurrence.maskBase);
88
+ pipeline.draw(geometry);
89
+ }
90
+ ```
91
+
92
+ A geometry can also **share** another's buffers and add its own — an
93
+ occurrence with a result field of its own over the mesh's positions, ids and
94
+ indices: `new GLGeometry(gl, { attributes: new Map([["result.0", field]]) }, mesh)`.
95
+ It owns only what it uploaded, and is disposed with the geometry it shares.
96
+
97
+ ### What it refuses
98
+
99
+ Input it cannot use is refused with a `GLPipelineError` rather than skipped,
100
+ because a skipped binding draws a plausible wrong picture:
101
+
102
+ - `GLGeometry` refuses data that is not a `Float32Array` or `Int32Array`, a
103
+ `components` that is not a positive integer, data that is not a whole number
104
+ of vertices, attributes that disagree on the vertex count, and a geometry
105
+ with no indices and nothing to share. Nothing is allocated then.
106
+ - A pipeline refuses, at its first draw of a geometry (or at `prepare`), a
107
+ source whose data type or `integer` flag does not match the declared type,
108
+ or whose `components` is narrower than the declared width.
109
+ - `draw` refuses to run while any semantic in `requiredSemantics` has no
110
+ texture, and names every one missing.
111
+ - The constructor refuses an attribute type it cannot feed, and a driver whose
112
+ size for the uniform block is too small for the layout the build reflected.
113
+
114
+ ### What stays with the caller
115
+
116
+ - **Context loss is not handled.** After `webglcontextrestored`, every
117
+ `GLGeometry`, `GLPipeline` and `PickPass` from the lost context is dead:
118
+ build new ones and set their textures again.
119
+ - **Disposal.** Disposing a geometry releases the vertex arrays every pipeline
120
+ built over it; disposing a pipeline releases its own. Textures are never
121
+ deleted here.
122
+ - **Integer tables** (`usampler2D`) must use `NEAREST` filtering and no
123
+ mipmaps; anything else makes the texture incomplete and every fetch reads
124
+ zero.
125
+ - **`draw` leaves GL state changed**: the current program, the active texture
126
+ unit, the textures bound on the pipeline's units and the array buffer
127
+ binding. It leaves no vertex array bound.
128
+
129
+ ## Picking
130
+
131
+ The pick shape writes identifiers *instead of* a color, so hit-testing is a
132
+ second draw of the same geometry rather than a feature added to the color
133
+ pipeline. That is much the cheaper arrangement: the extra draw happens when
134
+ someone clicks, where making the color pipeline write a second attachment
135
+ would cost bandwidth on every frame, force every consumer off the default
136
+ framebuffer, and give up multisampling — an integer attachment cannot be
137
+ multisampled, and every attachment of a framebuffer shares its sample count.
138
+
139
+ ```ts
140
+ import { pickPipelineFor } from "@vcollab/shader-library";
141
+ import { GLPipeline, PickPass } from "@vcollab/shader-webgl2";
142
+
143
+ // The pick pipeline whose vertex stage matches the color one, so deformed
144
+ // geometry is picked where it is drawn.
145
+ const entry = pickPipelineFor(set.registry, set.entry("deform3.symtensor"));
146
+ const pickPipeline = new GLPipeline(gl, await set.envelope(entry.id));
147
+ // deform3.pick binds the mask and the transform tables; set whatever it lists.
148
+ for (const semantic of pickPipeline.requiredSemantics) {
149
+ pickPipeline.setTexture(semantic, textures.get(semantic));
150
+ }
151
+ const pass = new PickPass(gl);
152
+
153
+ canvas.addEventListener("pointerdown", (event) => {
154
+ pass.resize(canvas.width, canvas.height);
155
+ const { x, y } = PickPass.fromCanvasPoint(canvas, event.offsetX, event.offsetY);
156
+ // Issue the scene's draws with the same state the color pass used, or the
157
+ // pick names something the viewer cannot see.
158
+ const hit = pass.pick(x, y, (draw) => {
159
+ for (const occurrence of occurrences) {
160
+ writeUniforms(pickPipeline.uniforms, occurrence, currentState);
161
+ draw(pickPipeline, geometry);
162
+ }
163
+ });
164
+ // The group id is a_atomId + u_maskLutBase: the block it falls in names the
165
+ // occurrence, and the offset within it the atom.
166
+ if (hit !== null) select(blocks.locate(hit.groupId), hit.depth);
167
+ });
168
+ ```
169
+
170
+ The pass owns no pipeline: one pick can draw through several — a scene mixes
171
+ deformed and undeformed slabs — and `draw` refuses one that is not a pick
172
+ pipeline, since a color written into the integer target reads back as garbage.
173
+ `pick` returns `null` where nothing was drawn, and does not call the scene
174
+ for a point outside the target. The target is cleared to `PICK_EMPTY` and the
175
+ texel decoded by `@vcollab/shader-library` — the depth channel holds the
176
+ *bits* of a float, so it cannot be read as a number.
177
+
178
+ `fromCanvasPoint` returns the integer device pixel under the point, counted
179
+ from the bottom row: the top CSS row is row `canvas.height - 1`.
180
+
181
+ The pass draws into its own framebuffer, and only the pixel it reads: after
182
+ the clear the scissor is set to that one pixel, so a scene of thousands of
183
+ draws shades one fragment each. For the draws it turns blending off, depth
184
+ testing on with `LESS`, and every color and depth write on; afterwards it puts
185
+ back each of those, the scissor test and box, the viewport and the caller's
186
+ draw and read framebuffers, even if a draw throws. Face culling is left as the
187
+ caller set it, so the pick honours winding exactly as the color pass does.
188
+
189
+ ## Dependencies
190
+
191
+ `@vcollab/shader-library` is a normal dependency. There are no peers, and the
192
+ package touches nothing but the `WebGL2RenderingContext` it is handed.
193
+
194
+ ## Documentation
195
+
196
+ | If you want to… | Read |
197
+ |---|---|
198
+ | Understand the design and what this package does not do | [docs/design.md](docs/design.md) |
199
+ | Look up an export | [docs/api.md](docs/api.md) |
@@ -0,0 +1,4 @@
1
+ /** Raised for anything this context would not accept. */
2
+ export declare class GLPipelineError extends Error {
3
+ readonly name = "GLPipelineError";
4
+ }
@@ -0,0 +1,81 @@
1
+ /** One attribute's data, as the caller holds it. */
2
+ export interface AttributeSource {
3
+ /**
4
+ * Tightly packed, `components` values per vertex. `Int32Array` for an
5
+ * attribute the contract declares `int`/`ivecN`, `Float32Array` for
6
+ * `float`/`vecN`.
7
+ */
8
+ readonly data: Float32Array | Int32Array;
9
+ /** How many values one vertex carries in `data`; at least the declared width. */
10
+ readonly components: number;
11
+ /**
12
+ * Whether the shader reads it as an integer attribute. Checked against the
13
+ * contract when a pipeline first draws the geometry: a source that disagrees
14
+ * is refused there.
15
+ */
16
+ readonly integer: boolean;
17
+ }
18
+ /** The data a geometry is made from. */
19
+ export interface GeometrySource {
20
+ /** Attribute data by the contract's **semantic**, not by its declared name. */
21
+ readonly attributes: ReadonlyMap<string, AttributeSource>;
22
+ /**
23
+ * Triangle indices. Required unless the geometry shares another's, whose
24
+ * indices it then uses.
25
+ */
26
+ readonly indices?: Uint32Array;
27
+ }
28
+ /** One uploaded attribute, as a pipeline binds it. */
29
+ export interface GeometryAttribute {
30
+ readonly buffer: WebGLBuffer;
31
+ readonly components: number;
32
+ readonly integer: boolean;
33
+ /** The element type the buffer holds. */
34
+ readonly type: "float" | "int";
35
+ }
36
+ /** Called when a geometry is disposed, so whoever built on it can let go. */
37
+ type Release = () => void;
38
+ /**
39
+ * Be told when a geometry is disposed. Returns the call that stops it.
40
+ *
41
+ * Not exported from the package: a pipeline uses it to drop the vertex array
42
+ * it built over a geometry's buffers, so disposing a geometry never leaves a
43
+ * pipeline holding one that points at deleted buffers.
44
+ */
45
+ export declare function onDispose(geometry: GLGeometry, release: Release): () => void;
46
+ /** A slab's buffers on the GPU, independent of any program. */
47
+ export declare class GLGeometry {
48
+ #private;
49
+ /** Vertices per attribute: every attribute, its own or shared, holds this many. */
50
+ readonly vertexCount: number;
51
+ /** How many indices a draw reads. */
52
+ readonly indexCount: number;
53
+ /**
54
+ * Upload the attributes, and the indices unless shared.
55
+ *
56
+ * @param shares a geometry whose buffers this one reads for every semantic
57
+ * it does not supply itself, and whose indices it draws with. It must
58
+ * outlive this one; disposing it disposes this.
59
+ * @throws {GLPipelineError} if an attribute's `components` is not a positive
60
+ * integer, its data is not a whole number of vertices, two attributes
61
+ * disagree on the vertex count, there are no indices to draw with, the
62
+ * shared geometry is disposed, or a buffer cannot be created. Nothing is
63
+ * left allocated.
64
+ */
65
+ constructor(gl: WebGL2RenderingContext, source: GeometrySource, shares?: GLGeometry);
66
+ /** Every semantic this geometry can feed, its own and shared. */
67
+ get semantics(): string[];
68
+ /** One semantic's buffer and layout, or undefined where it has none. */
69
+ attribute(semantic: string): GeometryAttribute | undefined;
70
+ /** The index buffer a draw reads, its own or shared. */
71
+ get indexBuffer(): WebGLBuffer;
72
+ /** Whether {@link dispose} has been called, here or on a geometry this shares. */
73
+ get disposed(): boolean;
74
+ /**
75
+ * Delete the buffers this geometry uploaded, and tell every pipeline that
76
+ * drew it, so each drops the vertex array it built here. Geometries sharing
77
+ * this one are disposed too. Calling it again does nothing.
78
+ */
79
+ dispose(): void;
80
+ }
81
+ export {};