@vcollab/shader-webgl2 0.0.0-stage → 1.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/dist/pick.d.ts ADDED
@@ -0,0 +1,96 @@
1
+ /**
2
+ * Hit-testing: the pick pipeline drawn into an offscreen integer target.
3
+ *
4
+ * The pick shape is exclusive — it writes identifiers *instead of* a color —
5
+ * so picking is a second draw of the same geometry, not a feature added to the
6
+ * color pipeline. That is the cheaper arrangement by some distance: the extra
7
+ * draw happens when someone clicks, where making the color pipeline write a
8
+ * second attachment would cost bandwidth on every frame, force every consumer
9
+ * off the default framebuffer, and give up multisampling — an integer
10
+ * attachment cannot be multisampled, and every attachment of a framebuffer
11
+ * shares its sample count.
12
+ *
13
+ * Keeping the two passes in agreement is the price. The camera, the mask, the
14
+ * placements and the deform scale must match what is on screen, or the pick
15
+ * names something the viewer cannot see. {@link PickPass.pick} takes a callback
16
+ * to write the uniforms for exactly that reason: the caller writes them from
17
+ * the same state it used for the color pass, rather than this holding a
18
+ * second copy that could drift.
19
+ */
20
+ import type { Pick, UniformBlock } from "@vcollab/shader-library";
21
+ import type { GLPipeline } from "./pipeline.ts";
22
+ /** A hit, with the pixel it was found at. */
23
+ export interface PickHit extends Pick {
24
+ /** Where the pick was taken, in framebuffer pixels. */
25
+ readonly x: number;
26
+ readonly y: number;
27
+ }
28
+ /**
29
+ * An offscreen integer target and the pipeline that draws into it.
30
+ *
31
+ * The pass owns its framebuffer and resizes it with the viewport; the pipeline
32
+ * is the caller's, built from a `pick` envelope whose vertex variant matches
33
+ * the color pipeline's — `deform3.pick` for a `deform3` color pass, so that
34
+ * deformed geometry is picked where it is drawn.
35
+ */
36
+ export declare class PickPass {
37
+ #private;
38
+ constructor(gl: WebGL2RenderingContext, pipeline: GLPipeline);
39
+ /** The pipeline drawing into the target, for setting its geometry and textures. */
40
+ get pipeline(): GLPipeline;
41
+ /**
42
+ * Match the target to the viewport.
43
+ *
44
+ * `rgba32ui` for the attachment, with a depth buffer beside it so the
45
+ * nearest primitive wins exactly as it does on screen. Neither is
46
+ * multisampled: an integer attachment cannot be. Sizes are floored, and a
47
+ * call at the current floored size does nothing. The caller's texture,
48
+ * renderbuffer and framebuffer bindings are put back afterwards.
49
+ *
50
+ * @throws {GLPipelineError} if the texture or renderbuffer cannot be created
51
+ * or the framebuffer is not complete; the pass is then left with no target.
52
+ */
53
+ resize(width: number, height: number): void;
54
+ /**
55
+ * Draw the pick pass and read one pixel.
56
+ *
57
+ * The pass sets the state it depends on — no scissor, every color channel
58
+ * and depth writable, `LESS` depth testing, no blending — and afterwards
59
+ * puts back each of those, the viewport and the caller's framebuffers, even
60
+ * if the draw throws. Face culling is left as the caller set it, so the pick
61
+ * honours winding exactly as the color pass does. The pipeline's draw also
62
+ * changes the current program and texture bindings (see
63
+ * {@link GLPipeline.draw}).
64
+ *
65
+ * @param x horizontal position in framebuffer pixels, from the left.
66
+ * @param y vertical position in framebuffer pixels, from the **bottom** —
67
+ * GL's origin, not the DOM's. {@link fromCanvasPoint} converts.
68
+ * @param writeUniforms fills the pipeline's block from the same state the
69
+ * color pass used.
70
+ * @returns the hit, or `null` where nothing was drawn or the point is outside.
71
+ * @throws {GLPipelineError} before {@link resize} has been called, or if the
72
+ * pipeline refuses to draw (a texture missing).
73
+ */
74
+ pick(x: number, y: number, writeUniforms: (uniforms: UniformBlock) => void): PickHit | null;
75
+ /**
76
+ * A canvas point as {@link pick} wants it: the integer device pixel under
77
+ * the point, counted from the bottom-left.
78
+ *
79
+ * A DOM event gives CSS pixels from the top-left of the element; GL reads
80
+ * device pixels from the bottom-left. Getting this wrong picks a mirrored
81
+ * point, which looks plausible near the middle and wrong everywhere else.
82
+ * The top CSS row maps to row `height - 1`; a point on or past an edge maps
83
+ * outside the target, where {@link pick} returns `null`.
84
+ */
85
+ static fromCanvasPoint(canvas: {
86
+ clientWidth: number;
87
+ clientHeight: number;
88
+ width: number;
89
+ height: number;
90
+ }, offsetX: number, offsetY: number): {
91
+ x: number;
92
+ y: number;
93
+ };
94
+ /** Release the target, its depth buffer and the framebuffer. */
95
+ dispose(): void;
96
+ }
@@ -0,0 +1,130 @@
1
+ /**
2
+ * One pipeline envelope, bound to a WebGL2 context.
3
+ *
4
+ * Everything here is driven by the envelope and nothing by knowledge of a
5
+ * particular shader: the contract says which attributes to supply and which
6
+ * uniforms exist, the layout says where each uniform sits, and the bindings
7
+ * say which sampler uniform and texture unit each resource takes. A pipeline
8
+ * this file has never heard of binds correctly as long as the set describes it.
9
+ *
10
+ * **What it deliberately does not do is decide values.** Which uniforms to
11
+ * write, what to put in them, and what a table or image contains are the
12
+ * caller's; this owns the GL objects and the wiring between them. That split is
13
+ * what keeps a viewer's policy out of the binding layer and the binding layer
14
+ * out of every viewer.
15
+ *
16
+ * Input it cannot use is refused with a {@link GLPipelineError} rather than
17
+ * skipped: a draw that leaves a sampler unbound or reads an attribute with the
18
+ * wrong type shows a plausible wrong picture, which is harder to find than an
19
+ * exception.
20
+ */
21
+ import type { Envelope, LanguageBlock, ShaderLanguage } from "@vcollab/shader-library";
22
+ import { ResourceBindings, UniformBlock } from "@vcollab/shader-library";
23
+ /** The std140 block every emitted stage declares. */
24
+ export declare const UNIFORM_BLOCK_NAME = "GlobalParams_std140";
25
+ /** The binding point the block is bound to. */
26
+ export declare const UNIFORM_BLOCK_BINDING = 0;
27
+ /** One attribute's data, as the caller holds it. */
28
+ export interface AttributeSource {
29
+ /**
30
+ * Tightly packed, `components` values per vertex. `Int32Array` for an
31
+ * attribute the contract declares `int`/`ivecN`, `Float32Array` for
32
+ * `float`/`vecN`.
33
+ */
34
+ readonly data: Float32Array | Int32Array;
35
+ /** How many values one vertex carries in `data`; at least the declared width. */
36
+ readonly components: number;
37
+ /**
38
+ * Whether the shader reads it as an integer attribute. Checked against the
39
+ * contract: a source that disagrees is refused.
40
+ */
41
+ readonly integer: boolean;
42
+ }
43
+ /** The geometry a pipeline draws. */
44
+ export interface Geometry {
45
+ /** Attribute data by the contract's **semantic**, not by its declared name. */
46
+ readonly attributes: ReadonlyMap<string, AttributeSource>;
47
+ readonly indices: Uint32Array;
48
+ }
49
+ /** Raised for anything this context would not accept. */
50
+ export declare class GLPipelineError extends Error {
51
+ readonly name = "GLPipelineError";
52
+ }
53
+ /**
54
+ * Compile and link one pipeline's two stages.
55
+ *
56
+ * The text is the build's output and is handed to the driver as it stands —
57
+ * nothing is patched on the way. The only thing added is the report when a
58
+ * driver disagrees, which is the one failure offline validation cannot rule
59
+ * out. Every GL object made on the way is freed if a later step fails.
60
+ */
61
+ export declare function linkProgram(gl: WebGL2RenderingContext, vertex: string, fragment: string): WebGLProgram;
62
+ /** A pipeline with its program, geometry, uniform buffer and resources. */
63
+ export declare class GLPipeline {
64
+ #private;
65
+ /** The envelope this was built from. */
66
+ readonly envelope: Envelope;
67
+ /** Its language block: layout, bindings and sources. */
68
+ readonly block: LanguageBlock;
69
+ /** Its sampler bindings, by semantic. */
70
+ readonly bindings: ResourceBindings;
71
+ /**
72
+ * The uniform block to write into. The caller sets whatever the pipeline
73
+ * declares; {@link draw} uploads it.
74
+ */
75
+ readonly uniforms: UniformBlock;
76
+ /**
77
+ * @throws {GLPipelineError} if the envelope lacks a stage, the driver rejects
78
+ * the text, a GL object cannot be created, or the driver's size for the
79
+ * uniform block differs from the size the build reflected. Anything created
80
+ * before the failure is freed.
81
+ */
82
+ constructor(gl: WebGL2RenderingContext, envelope: Envelope, language?: ShaderLanguage);
83
+ /** The linked program, for a caller that needs it directly. */
84
+ get program(): WebGLProgram;
85
+ /**
86
+ * Upload the geometry, matching the contract's attributes by semantic.
87
+ *
88
+ * An attribute the caller cannot supply is left as a **disabled array with a
89
+ * constant**, which is what a viewer does for data a draw does not have —
90
+ * the same "off state is data" the always-present features rely on. The
91
+ * constant is context state rather than vertex-array state, so {@link draw}
92
+ * sets it again each time. The contract's declared width wins over the
93
+ * source's, so a `vec3` read from a wider field takes the first three values
94
+ * of each vertex.
95
+ *
96
+ * Calling it again replaces the previous geometry and frees its buffers.
97
+ *
98
+ * @throws {GLPipelineError} if a source's data type or `integer` flag does
99
+ * not match the declared type, its `components` is narrower than the
100
+ * declared width, or a buffer cannot be created. A refused source changes
101
+ * nothing; when buffer creation fails the pipeline is left with no geometry.
102
+ */
103
+ setGeometry(geometry: Geometry): void;
104
+ /**
105
+ * Give a semantic its texture.
106
+ *
107
+ * By semantic, because the name a table or texture is declared under never
108
+ * reaches the linked program: GLSL ES 3.00 has no binding qualifier for
109
+ * samplers, so the cross-compiler pairs each texture with a sampler and names
110
+ * the combination itself. The envelope's record is the only route across.
111
+ *
112
+ * An integer table (`usampler2D`) must use `NEAREST` filtering and no
113
+ * mipmaps, or the texture is incomplete and every fetch returns zero.
114
+ */
115
+ setTexture(semantic: string, texture: WebGLTexture | null): void;
116
+ /** Every semantic this pipeline expects a texture for. */
117
+ get requiredSemantics(): string[];
118
+ /**
119
+ * Draw, after uploading the uniform block and binding every resource.
120
+ *
121
+ * A no-op without geometry. Leaves the current program, the active texture
122
+ * unit and the texture bound on each of the pipeline's units changed.
123
+ *
124
+ * @throws {GLPipelineError} if any semantic in {@link requiredSemantics} has
125
+ * no texture; the message names every one missing.
126
+ */
127
+ draw(): void;
128
+ /** Release the GL objects this owns. Textures given to it are the caller's. */
129
+ dispose(): void;
130
+ }
@@ -0,0 +1,2 @@
1
+ /** The package version, injected from package.json at build time. */
2
+ export declare const VERSION: string;
package/package.json CHANGED
@@ -1,6 +1,63 @@
1
1
  {
2
2
  "name": "@vcollab/shader-webgl2",
3
- "version": "0.0.0-stage",
4
- "stub": true,
5
- "description": "Temporary package placeholder for staged publishing"
3
+ "version": "1.0.0",
4
+ "description": "Binds a VCollab shader pipeline envelope to a WebGL2 context: programs, attributes, uniform blocks, resources and the pick pass",
5
+ "author": "Visual Collaboration Technologies Inc.",
6
+ "license": "SEE LICENSE IN LICENSE",
7
+ "type": "module",
8
+ "sideEffects": false,
9
+ "main": "./dist/index.cjs",
10
+ "module": "./dist/index.js",
11
+ "types": "./dist/index.d.ts",
12
+ "exports": {
13
+ ".": {
14
+ "types": "./dist/index.d.ts",
15
+ "import": "./dist/index.js",
16
+ "require": "./dist/index.cjs"
17
+ },
18
+ "./package.json": "./package.json"
19
+ },
20
+ "files": [
21
+ "dist",
22
+ "README.md",
23
+ "CHANGELOG.md",
24
+ "LICENSE"
25
+ ],
26
+ "engines": {
27
+ "node": ">=22"
28
+ },
29
+ "publishConfig": {
30
+ "access": "public",
31
+ "provenance": false
32
+ },
33
+ "keywords": [
34
+ "shader",
35
+ "webgl2",
36
+ "glsl",
37
+ "picking",
38
+ "vcollab"
39
+ ],
40
+ "dependencies": {
41
+ "@vcollab/shader-library": "^1.0.0"
42
+ },
43
+ "devDependencies": {
44
+ "@types/node": "^26.6.1",
45
+ "@vitest/coverage-v8": "^5.0.1",
46
+ "rimraf": "^6.0.0",
47
+ "tsup": "^8.3.6",
48
+ "typescript": "^7.0.2",
49
+ "vitest": "^5.0.1",
50
+ "@vcollab/shader-artifacts": "1.0.0"
51
+ },
52
+ "scripts": {
53
+ "build": "tsup && tsc -p tsconfig.build.json",
54
+ "typecheck": "tsc --noEmit",
55
+ "lint": "biome check .",
56
+ "lint:fix": "biome check --write .",
57
+ "format": "biome format --write .",
58
+ "test": "vitest run",
59
+ "test:watch": "vitest",
60
+ "coverage": "vitest run --coverage",
61
+ "clean": "rimraf dist"
62
+ }
6
63
  }