@vcollab/shader-webgl2 1.0.0 → 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 +42 -0
- package/README.md +86 -42
- package/dist/errors.d.ts +4 -0
- package/dist/geometry.d.ts +81 -0
- package/dist/index.cjs +488 -292
- package/dist/index.cjs.map +1 -1
- package/dist/index.d.ts +12 -7
- package/dist/index.js +489 -294
- package/dist/index.js.map +1 -1
- package/dist/pick.d.ts +44 -27
- package/dist/pipeline.d.ts +60 -57
- package/package.json +3 -3
package/CHANGELOG.md
CHANGED
|
@@ -5,6 +5,48 @@ All notable changes to `@vcollab/shader-webgl2` are documented here.
|
|
|
5
5
|
The format follows [Keep a Changelog](https://keepachangelog.com/en/1.1.0/) and
|
|
6
6
|
this package adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0.html).
|
|
7
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
|
+
|
|
8
50
|
## [1.0.0] — 2026-12-03
|
|
9
51
|
|
|
10
52
|
First release. Extracted from the playground's renderer, which had already
|
package/README.md
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
# @vcollab/shader-webgl2
|
|
2
2
|
|
|
3
|
-
Binds a VCollab shader pipeline envelope to a WebGL2 context:
|
|
4
|
-
|
|
3
|
+
Binds a VCollab shader pipeline envelope to a WebGL2 context: geometry,
|
|
4
|
+
programs, uniform blocks, resources, and the pick pass.
|
|
5
5
|
|
|
6
6
|
## Overview
|
|
7
7
|
|
|
@@ -30,28 +30,26 @@ pnpm add @vcollab/shader-webgl2 @vcollab/shader-library
|
|
|
30
30
|
|
|
31
31
|
```ts
|
|
32
32
|
import { openBundledSet } from "@vcollab/shader-library/bundled";
|
|
33
|
-
import { GLPipeline } from "@vcollab/shader-webgl2";
|
|
33
|
+
import { GLGeometry, GLPipeline } from "@vcollab/shader-webgl2";
|
|
34
34
|
|
|
35
35
|
const set = await openBundledSet();
|
|
36
|
-
const pipeline = new GLPipeline(gl, await set.envelope("deform3.symtensor"));
|
|
37
36
|
|
|
38
|
-
// Attributes are matched by the contract's
|
|
39
|
-
// A float/vecN input takes a Float32Array,
|
|
40
|
-
|
|
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
41
|
attributes: new Map([
|
|
42
42
|
["coordinates", { data: positions, components: 3, integer: false }], // Float32Array
|
|
43
|
-
["
|
|
43
|
+
["atom-id", { data: atomIds, components: 1, integer: true }], // Int32Array
|
|
44
44
|
]),
|
|
45
45
|
indices, // Uint32Array
|
|
46
46
|
});
|
|
47
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
|
+
|
|
48
51
|
// Resources by semantic too — the declared name does not reach the program.
|
|
49
52
|
// Every semantic the pipeline binds needs a texture, or draw() throws.
|
|
50
|
-
const textures = new Map<string, WebGLTexture>([
|
|
51
|
-
["mask", maskTable],
|
|
52
|
-
["transform", transformTable],
|
|
53
|
-
["material", materialTable],
|
|
54
|
-
]);
|
|
55
53
|
for (const semantic of pipeline.requiredSemantics) {
|
|
56
54
|
const texture = textures.get(semantic);
|
|
57
55
|
if (texture === undefined) throw new Error(`no texture for ${semantic}`);
|
|
@@ -59,39 +57,74 @@ for (const semantic of pipeline.requiredSemantics) {
|
|
|
59
57
|
}
|
|
60
58
|
|
|
61
59
|
function frame(camera) {
|
|
62
|
-
pipeline.uniforms.set("u_mvpMatrix", camera.mvp);
|
|
60
|
+
pipeline.uniforms.set("u_mvpMatrix", camera.mvp); // column-major in
|
|
63
61
|
pipeline.uniforms.set("u_colorSource", 2);
|
|
64
|
-
pipeline.draw();
|
|
62
|
+
pipeline.draw(geometry);
|
|
65
63
|
}
|
|
66
64
|
```
|
|
67
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
|
+
|
|
68
73
|
An attribute you do not supply is left as a disabled array with a constant,
|
|
69
74
|
which is how a viewer expresses "this draw has no such data" — the same
|
|
70
75
|
"off state is data" the always-present features rely on.
|
|
71
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
|
+
|
|
72
97
|
### What it refuses
|
|
73
98
|
|
|
74
99
|
Input it cannot use is refused with a `GLPipelineError` rather than skipped,
|
|
75
100
|
because a skipped binding draws a plausible wrong picture:
|
|
76
101
|
|
|
77
|
-
- `
|
|
78
|
-
|
|
79
|
-
|
|
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.
|
|
80
109
|
- `draw` refuses to run while any semantic in `requiredSemantics` has no
|
|
81
110
|
texture, and names every one missing.
|
|
82
|
-
- The constructor refuses
|
|
83
|
-
|
|
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.
|
|
84
113
|
|
|
85
114
|
### What stays with the caller
|
|
86
115
|
|
|
87
116
|
- **Context loss is not handled.** After `webglcontextrestored`, every
|
|
88
|
-
`GLPipeline` and `PickPass` from the lost context is dead:
|
|
89
|
-
|
|
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.
|
|
90
122
|
- **Integer tables** (`usampler2D`) must use `NEAREST` filtering and no
|
|
91
123
|
mipmaps; anything else makes the texture incomplete and every fetch reads
|
|
92
124
|
zero.
|
|
93
125
|
- **`draw` leaves GL state changed**: the current program, the active texture
|
|
94
|
-
unit
|
|
126
|
+
unit, the textures bound on the pipeline's units and the array buffer
|
|
127
|
+
binding. It leaves no vertex array bound.
|
|
95
128
|
|
|
96
129
|
## Picking
|
|
97
130
|
|
|
@@ -110,37 +143,48 @@ import { GLPipeline, PickPass } from "@vcollab/shader-webgl2";
|
|
|
110
143
|
// The pick pipeline whose vertex stage matches the color one, so deformed
|
|
111
144
|
// geometry is picked where it is drawn.
|
|
112
145
|
const entry = pickPipelineFor(set.registry, set.entry("deform3.symtensor"));
|
|
113
|
-
const
|
|
114
|
-
|
|
115
|
-
|
|
116
|
-
|
|
117
|
-
const texture = textures.get(semantic);
|
|
118
|
-
if (texture === undefined) throw new Error(`no texture for ${semantic}`);
|
|
119
|
-
pass.pipeline.setTexture(semantic, texture);
|
|
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));
|
|
120
150
|
}
|
|
151
|
+
const pass = new PickPass(gl);
|
|
121
152
|
|
|
122
153
|
canvas.addEventListener("pointerdown", (event) => {
|
|
123
154
|
pass.resize(canvas.width, canvas.height);
|
|
124
155
|
const { x, y } = PickPass.fromCanvasPoint(canvas, event.offsetX, event.offsetY);
|
|
125
|
-
//
|
|
126
|
-
// names something the viewer cannot see.
|
|
127
|
-
const hit = pass.pick(x, y, (
|
|
128
|
-
|
|
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);
|
|
129
167
|
});
|
|
130
168
|
```
|
|
131
169
|
|
|
132
|
-
|
|
133
|
-
|
|
134
|
-
|
|
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.
|
|
135
177
|
|
|
136
178
|
`fromCanvasPoint` returns the integer device pixel under the point, counted
|
|
137
179
|
from the bottom row: the top CSS row is row `canvas.height - 1`.
|
|
138
180
|
|
|
139
|
-
The pass draws into its own framebuffer
|
|
140
|
-
|
|
141
|
-
|
|
142
|
-
|
|
143
|
-
|
|
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.
|
|
144
188
|
|
|
145
189
|
## Dependencies
|
|
146
190
|
|
package/dist/errors.d.ts
ADDED
|
@@ -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 {};
|