@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/CHANGELOG.md +51 -0
- package/LICENSE +8 -0
- package/README.md +154 -2
- package/dist/index.cjs +511 -0
- package/dist/index.cjs.map +1 -0
- package/dist/index.d.ts +21 -0
- package/dist/index.js +503 -0
- package/dist/index.js.map +1 -0
- package/dist/pick.d.ts +96 -0
- package/dist/pipeline.d.ts +130 -0
- package/dist/version.d.ts +2 -0
- package/package.json +60 -3
package/dist/index.js
ADDED
|
@@ -0,0 +1,503 @@
|
|
|
1
|
+
import { ShaderSet, ResourceBindings, UniformBlock, PICK_EMPTY, decodePick } from '@vcollab/shader-library';
|
|
2
|
+
|
|
3
|
+
// src/pick.ts
|
|
4
|
+
var UNIFORM_BLOCK_NAME = "GlobalParams_std140";
|
|
5
|
+
var UNIFORM_BLOCK_BINDING = 0;
|
|
6
|
+
var GLPipelineError = class extends Error {
|
|
7
|
+
name = "GLPipelineError";
|
|
8
|
+
};
|
|
9
|
+
function attributeShape(input) {
|
|
10
|
+
const match = /^(i?)vec([234])$/.exec(input.type);
|
|
11
|
+
if (match !== null) return { integer: match[1] === "i", width: Number(match[2]) };
|
|
12
|
+
if (input.type === "float") return { integer: false, width: 1 };
|
|
13
|
+
if (input.type === "int") return { integer: true, width: 1 };
|
|
14
|
+
throw new GLPipelineError(
|
|
15
|
+
`${input.name} is declared ${input.type}; only float, vecN, int and ivecN attributes are supported`
|
|
16
|
+
);
|
|
17
|
+
}
|
|
18
|
+
function compile(gl, type, source, stage) {
|
|
19
|
+
const shader = gl.createShader(type);
|
|
20
|
+
if (shader === null) throw new GLPipelineError(`could not create a ${stage} shader`);
|
|
21
|
+
gl.shaderSource(shader, source);
|
|
22
|
+
gl.compileShader(shader);
|
|
23
|
+
if (!gl.getShaderParameter(shader, gl.COMPILE_STATUS)) {
|
|
24
|
+
const log = gl.getShaderInfoLog(shader);
|
|
25
|
+
gl.deleteShader(shader);
|
|
26
|
+
throw new GLPipelineError(`the ${stage} stage did not compile on this driver:
|
|
27
|
+
${log}`);
|
|
28
|
+
}
|
|
29
|
+
return shader;
|
|
30
|
+
}
|
|
31
|
+
function linkProgram(gl, vertex, fragment) {
|
|
32
|
+
const vertexShader = compile(gl, gl.VERTEX_SHADER, vertex, "vertex");
|
|
33
|
+
let fragmentShader;
|
|
34
|
+
try {
|
|
35
|
+
fragmentShader = compile(gl, gl.FRAGMENT_SHADER, fragment, "fragment");
|
|
36
|
+
} catch (error) {
|
|
37
|
+
gl.deleteShader(vertexShader);
|
|
38
|
+
throw error;
|
|
39
|
+
}
|
|
40
|
+
const program = gl.createProgram();
|
|
41
|
+
if (program === null) {
|
|
42
|
+
gl.deleteShader(vertexShader);
|
|
43
|
+
gl.deleteShader(fragmentShader);
|
|
44
|
+
throw new GLPipelineError("could not create a program");
|
|
45
|
+
}
|
|
46
|
+
gl.attachShader(program, vertexShader);
|
|
47
|
+
gl.attachShader(program, fragmentShader);
|
|
48
|
+
gl.linkProgram(program);
|
|
49
|
+
gl.deleteShader(vertexShader);
|
|
50
|
+
gl.deleteShader(fragmentShader);
|
|
51
|
+
if (!gl.getProgramParameter(program, gl.LINK_STATUS)) {
|
|
52
|
+
const log = gl.getProgramInfoLog(program);
|
|
53
|
+
gl.deleteProgram(program);
|
|
54
|
+
throw new GLPipelineError(`the stage pair did not link on this driver:
|
|
55
|
+
${log}`);
|
|
56
|
+
}
|
|
57
|
+
const blockIndex = gl.getUniformBlockIndex(program, UNIFORM_BLOCK_NAME);
|
|
58
|
+
if (blockIndex !== gl.INVALID_INDEX) {
|
|
59
|
+
gl.uniformBlockBinding(program, blockIndex, UNIFORM_BLOCK_BINDING);
|
|
60
|
+
}
|
|
61
|
+
return program;
|
|
62
|
+
}
|
|
63
|
+
var GLPipeline = class {
|
|
64
|
+
#gl;
|
|
65
|
+
#program;
|
|
66
|
+
#ubo;
|
|
67
|
+
#vao;
|
|
68
|
+
/** The current geometry's vertex and index buffers; replaced by each setGeometry. */
|
|
69
|
+
#buffers = [];
|
|
70
|
+
/** Attributes held at a constant, set again every draw (the constant is not VAO state). */
|
|
71
|
+
#absent = [];
|
|
72
|
+
#textures = /* @__PURE__ */ new Map();
|
|
73
|
+
/** Each binding's sampler uniform location, in `bindings.all` order. */
|
|
74
|
+
#samplerLocations;
|
|
75
|
+
/** A byte view of `uniforms.buffer`, made once. */
|
|
76
|
+
#uniformBytes;
|
|
77
|
+
#indexCount = 0;
|
|
78
|
+
/** The envelope this was built from. */
|
|
79
|
+
envelope;
|
|
80
|
+
/** Its language block: layout, bindings and sources. */
|
|
81
|
+
block;
|
|
82
|
+
/** Its sampler bindings, by semantic. */
|
|
83
|
+
bindings;
|
|
84
|
+
/**
|
|
85
|
+
* The uniform block to write into. The caller sets whatever the pipeline
|
|
86
|
+
* declares; {@link draw} uploads it.
|
|
87
|
+
*/
|
|
88
|
+
uniforms;
|
|
89
|
+
/**
|
|
90
|
+
* @throws {GLPipelineError} if the envelope lacks a stage, the driver rejects
|
|
91
|
+
* the text, a GL object cannot be created, or the driver's size for the
|
|
92
|
+
* uniform block differs from the size the build reflected. Anything created
|
|
93
|
+
* before the failure is freed.
|
|
94
|
+
*/
|
|
95
|
+
constructor(gl, envelope, language = "glsl") {
|
|
96
|
+
this.#gl = gl;
|
|
97
|
+
this.envelope = envelope;
|
|
98
|
+
this.block = ShaderSet.language(envelope, language);
|
|
99
|
+
const stages = this.block.stages;
|
|
100
|
+
if (stages.vertex === void 0 || stages.fragment === void 0) {
|
|
101
|
+
throw new GLPipelineError(`${envelope.pipeline.id} does not carry both stages`);
|
|
102
|
+
}
|
|
103
|
+
const program = linkProgram(gl, stages.vertex.source, stages.fragment.source);
|
|
104
|
+
let ubo = null;
|
|
105
|
+
try {
|
|
106
|
+
this.bindings = new ResourceBindings(this.block);
|
|
107
|
+
this.uniforms = new UniformBlock(this.block);
|
|
108
|
+
this.#uniformBytes = new Uint8Array(this.uniforms.buffer);
|
|
109
|
+
let bufferSize = this.block.blockSize;
|
|
110
|
+
const blockIndex = gl.getUniformBlockIndex(program, UNIFORM_BLOCK_NAME);
|
|
111
|
+
if (blockIndex !== gl.INVALID_INDEX) {
|
|
112
|
+
const size = gl.getActiveUniformBlockParameter(
|
|
113
|
+
program,
|
|
114
|
+
blockIndex,
|
|
115
|
+
gl.UNIFORM_BLOCK_DATA_SIZE
|
|
116
|
+
);
|
|
117
|
+
const end = Math.max(
|
|
118
|
+
0,
|
|
119
|
+
...Object.values(this.block.layout).map((member) => member.offset + member.size)
|
|
120
|
+
);
|
|
121
|
+
if (typeof size !== "number" || size < end) {
|
|
122
|
+
throw new GLPipelineError(
|
|
123
|
+
`${envelope.pipeline.id}: the driver sizes ${UNIFORM_BLOCK_NAME} at ${String(size)} bytes, too small for the layout the build reflected, which ends at byte ${end}`
|
|
124
|
+
);
|
|
125
|
+
}
|
|
126
|
+
bufferSize = Math.max(bufferSize, size);
|
|
127
|
+
}
|
|
128
|
+
ubo = gl.createBuffer();
|
|
129
|
+
if (ubo === null) throw new GLPipelineError("could not create a uniform buffer");
|
|
130
|
+
gl.bindBuffer(gl.UNIFORM_BUFFER, ubo);
|
|
131
|
+
gl.bufferData(gl.UNIFORM_BUFFER, bufferSize, gl.DYNAMIC_DRAW);
|
|
132
|
+
const vao = gl.createVertexArray();
|
|
133
|
+
if (vao === null) throw new GLPipelineError("could not create a vertex array");
|
|
134
|
+
this.#samplerLocations = this.bindings.all.map(
|
|
135
|
+
(record) => gl.getUniformLocation(program, record.uniform)
|
|
136
|
+
);
|
|
137
|
+
this.#program = program;
|
|
138
|
+
this.#ubo = ubo;
|
|
139
|
+
this.#vao = vao;
|
|
140
|
+
} catch (error) {
|
|
141
|
+
if (ubo !== null) gl.deleteBuffer(ubo);
|
|
142
|
+
gl.deleteProgram(program);
|
|
143
|
+
throw error;
|
|
144
|
+
}
|
|
145
|
+
}
|
|
146
|
+
/** The linked program, for a caller that needs it directly. */
|
|
147
|
+
get program() {
|
|
148
|
+
return this.#program;
|
|
149
|
+
}
|
|
150
|
+
/**
|
|
151
|
+
* Upload the geometry, matching the contract's attributes by semantic.
|
|
152
|
+
*
|
|
153
|
+
* An attribute the caller cannot supply is left as a **disabled array with a
|
|
154
|
+
* constant**, which is what a viewer does for data a draw does not have —
|
|
155
|
+
* the same "off state is data" the always-present features rely on. The
|
|
156
|
+
* constant is context state rather than vertex-array state, so {@link draw}
|
|
157
|
+
* sets it again each time. The contract's declared width wins over the
|
|
158
|
+
* source's, so a `vec3` read from a wider field takes the first three values
|
|
159
|
+
* of each vertex.
|
|
160
|
+
*
|
|
161
|
+
* Calling it again replaces the previous geometry and frees its buffers.
|
|
162
|
+
*
|
|
163
|
+
* @throws {GLPipelineError} if a source's data type or `integer` flag does
|
|
164
|
+
* not match the declared type, its `components` is narrower than the
|
|
165
|
+
* declared width, or a buffer cannot be created. A refused source changes
|
|
166
|
+
* nothing; when buffer creation fails the pipeline is left with no geometry.
|
|
167
|
+
*/
|
|
168
|
+
setGeometry(geometry) {
|
|
169
|
+
const gl = this.#gl;
|
|
170
|
+
const plan = this.envelope.pipeline.inputs.map((input) => {
|
|
171
|
+
const shape = attributeShape(input);
|
|
172
|
+
const source = geometry.attributes.get(input.semantic);
|
|
173
|
+
if (source !== void 0) {
|
|
174
|
+
const label = `${input.semantic} (${input.name}, ${input.type})`;
|
|
175
|
+
if (source.integer !== shape.integer) {
|
|
176
|
+
throw new GLPipelineError(
|
|
177
|
+
`${label} is ${shape.integer ? "an integer" : "a float"} attribute; the source says integer: ${source.integer}`
|
|
178
|
+
);
|
|
179
|
+
}
|
|
180
|
+
const wanted = shape.integer ? Int32Array : Float32Array;
|
|
181
|
+
if (!(source.data instanceof wanted)) {
|
|
182
|
+
throw new GLPipelineError(`${label} needs ${wanted.name} data`);
|
|
183
|
+
}
|
|
184
|
+
if (!Number.isInteger(source.components) || source.components < shape.width) {
|
|
185
|
+
throw new GLPipelineError(
|
|
186
|
+
`${label} reads ${shape.width} values per vertex; the source carries ${source.components}`
|
|
187
|
+
);
|
|
188
|
+
}
|
|
189
|
+
}
|
|
190
|
+
return { input, shape, source, location: gl.getAttribLocation(this.#program, input.name) };
|
|
191
|
+
});
|
|
192
|
+
for (const buffer of this.#buffers) gl.deleteBuffer(buffer);
|
|
193
|
+
this.#buffers = [];
|
|
194
|
+
this.#absent = [];
|
|
195
|
+
this.#indexCount = 0;
|
|
196
|
+
const buffers = [];
|
|
197
|
+
const absent = [];
|
|
198
|
+
gl.bindVertexArray(this.#vao);
|
|
199
|
+
try {
|
|
200
|
+
for (const { input, shape, source, location } of plan) {
|
|
201
|
+
if (location < 0) continue;
|
|
202
|
+
if (source === void 0) {
|
|
203
|
+
gl.disableVertexAttribArray(location);
|
|
204
|
+
absent.push({ location, integer: shape.integer });
|
|
205
|
+
continue;
|
|
206
|
+
}
|
|
207
|
+
const buffer = gl.createBuffer();
|
|
208
|
+
if (buffer === null)
|
|
209
|
+
throw new GLPipelineError(`could not create a buffer for ${input.name}`);
|
|
210
|
+
buffers.push(buffer);
|
|
211
|
+
gl.bindBuffer(gl.ARRAY_BUFFER, buffer);
|
|
212
|
+
gl.bufferData(gl.ARRAY_BUFFER, source.data, gl.STATIC_DRAW);
|
|
213
|
+
gl.enableVertexAttribArray(location);
|
|
214
|
+
const stride = source.components * 4;
|
|
215
|
+
if (shape.integer) gl.vertexAttribIPointer(location, shape.width, gl.INT, stride, 0);
|
|
216
|
+
else gl.vertexAttribPointer(location, shape.width, gl.FLOAT, false, stride, 0);
|
|
217
|
+
}
|
|
218
|
+
const elements = gl.createBuffer();
|
|
219
|
+
if (elements === null) throw new GLPipelineError("could not create an index buffer");
|
|
220
|
+
buffers.push(elements);
|
|
221
|
+
gl.bindBuffer(gl.ELEMENT_ARRAY_BUFFER, elements);
|
|
222
|
+
gl.bufferData(gl.ELEMENT_ARRAY_BUFFER, geometry.indices, gl.STATIC_DRAW);
|
|
223
|
+
} catch (error) {
|
|
224
|
+
for (const buffer of buffers) gl.deleteBuffer(buffer);
|
|
225
|
+
throw error;
|
|
226
|
+
} finally {
|
|
227
|
+
gl.bindVertexArray(null);
|
|
228
|
+
}
|
|
229
|
+
this.#buffers = buffers;
|
|
230
|
+
this.#absent = absent;
|
|
231
|
+
this.#indexCount = geometry.indices.length;
|
|
232
|
+
}
|
|
233
|
+
/**
|
|
234
|
+
* Give a semantic its texture.
|
|
235
|
+
*
|
|
236
|
+
* By semantic, because the name a table or texture is declared under never
|
|
237
|
+
* reaches the linked program: GLSL ES 3.00 has no binding qualifier for
|
|
238
|
+
* samplers, so the cross-compiler pairs each texture with a sampler and names
|
|
239
|
+
* the combination itself. The envelope's record is the only route across.
|
|
240
|
+
*
|
|
241
|
+
* An integer table (`usampler2D`) must use `NEAREST` filtering and no
|
|
242
|
+
* mipmaps, or the texture is incomplete and every fetch returns zero.
|
|
243
|
+
*/
|
|
244
|
+
setTexture(semantic, texture) {
|
|
245
|
+
if (texture === null) this.#textures.delete(semantic);
|
|
246
|
+
else this.#textures.set(semantic, texture);
|
|
247
|
+
}
|
|
248
|
+
/** Every semantic this pipeline expects a texture for. */
|
|
249
|
+
get requiredSemantics() {
|
|
250
|
+
return this.bindings.semantics;
|
|
251
|
+
}
|
|
252
|
+
/**
|
|
253
|
+
* Draw, after uploading the uniform block and binding every resource.
|
|
254
|
+
*
|
|
255
|
+
* A no-op without geometry. Leaves the current program, the active texture
|
|
256
|
+
* unit and the texture bound on each of the pipeline's units changed.
|
|
257
|
+
*
|
|
258
|
+
* @throws {GLPipelineError} if any semantic in {@link requiredSemantics} has
|
|
259
|
+
* no texture; the message names every one missing.
|
|
260
|
+
*/
|
|
261
|
+
draw() {
|
|
262
|
+
const gl = this.#gl;
|
|
263
|
+
if (this.#indexCount === 0) return;
|
|
264
|
+
const missing = this.requiredSemantics.filter((semantic) => !this.#textures.has(semantic));
|
|
265
|
+
if (missing.length > 0) {
|
|
266
|
+
throw new GLPipelineError(
|
|
267
|
+
`${this.envelope.pipeline.id} has no texture for: ${missing.join(", ")}`
|
|
268
|
+
);
|
|
269
|
+
}
|
|
270
|
+
gl.useProgram(this.#program);
|
|
271
|
+
gl.bindBuffer(gl.UNIFORM_BUFFER, this.#ubo);
|
|
272
|
+
gl.bufferSubData(gl.UNIFORM_BUFFER, 0, this.#uniformBytes);
|
|
273
|
+
gl.bindBufferBase(gl.UNIFORM_BUFFER, UNIFORM_BLOCK_BINDING, this.#ubo);
|
|
274
|
+
this.bindings.all.forEach((record, index) => {
|
|
275
|
+
gl.activeTexture(gl.TEXTURE0 + record.unit);
|
|
276
|
+
gl.bindTexture(gl.TEXTURE_2D, this.#textures.get(record.semantic));
|
|
277
|
+
const location = this.#samplerLocations[index];
|
|
278
|
+
if (location !== null) gl.uniform1i(location, record.unit);
|
|
279
|
+
});
|
|
280
|
+
for (const { location, integer } of this.#absent) {
|
|
281
|
+
if (integer) gl.vertexAttribI4i(location, 0, 0, 0, 0);
|
|
282
|
+
else gl.vertexAttrib4f(location, 0, 0, 0, 0);
|
|
283
|
+
}
|
|
284
|
+
gl.bindVertexArray(this.#vao);
|
|
285
|
+
gl.drawElements(gl.TRIANGLES, this.#indexCount, gl.UNSIGNED_INT, 0);
|
|
286
|
+
gl.bindVertexArray(null);
|
|
287
|
+
}
|
|
288
|
+
/** Release the GL objects this owns. Textures given to it are the caller's. */
|
|
289
|
+
dispose() {
|
|
290
|
+
const gl = this.#gl;
|
|
291
|
+
gl.deleteProgram(this.#program);
|
|
292
|
+
gl.deleteBuffer(this.#ubo);
|
|
293
|
+
gl.deleteVertexArray(this.#vao);
|
|
294
|
+
for (const buffer of this.#buffers) gl.deleteBuffer(buffer);
|
|
295
|
+
this.#buffers = [];
|
|
296
|
+
this.#absent = [];
|
|
297
|
+
this.#indexCount = 0;
|
|
298
|
+
this.#textures.clear();
|
|
299
|
+
}
|
|
300
|
+
};
|
|
301
|
+
|
|
302
|
+
// src/pick.ts
|
|
303
|
+
var EMPTY_COLOR = new Uint32Array([PICK_EMPTY, PICK_EMPTY, PICK_EMPTY, PICK_EMPTY]);
|
|
304
|
+
var FAR_DEPTH = new Float32Array([1]);
|
|
305
|
+
var PickPass = class {
|
|
306
|
+
#gl;
|
|
307
|
+
#pipeline;
|
|
308
|
+
#framebuffer;
|
|
309
|
+
#target = null;
|
|
310
|
+
#depth = null;
|
|
311
|
+
#width = 0;
|
|
312
|
+
#height = 0;
|
|
313
|
+
#readback = new Uint32Array(4);
|
|
314
|
+
constructor(gl, pipeline) {
|
|
315
|
+
this.#gl = gl;
|
|
316
|
+
this.#pipeline = pipeline;
|
|
317
|
+
const framebuffer = gl.createFramebuffer();
|
|
318
|
+
if (framebuffer === null) throw new GLPipelineError("could not create a pick framebuffer");
|
|
319
|
+
this.#framebuffer = framebuffer;
|
|
320
|
+
}
|
|
321
|
+
/** The pipeline drawing into the target, for setting its geometry and textures. */
|
|
322
|
+
get pipeline() {
|
|
323
|
+
return this.#pipeline;
|
|
324
|
+
}
|
|
325
|
+
/**
|
|
326
|
+
* Match the target to the viewport.
|
|
327
|
+
*
|
|
328
|
+
* `rgba32ui` for the attachment, with a depth buffer beside it so the
|
|
329
|
+
* nearest primitive wins exactly as it does on screen. Neither is
|
|
330
|
+
* multisampled: an integer attachment cannot be. Sizes are floored, and a
|
|
331
|
+
* call at the current floored size does nothing. The caller's texture,
|
|
332
|
+
* renderbuffer and framebuffer bindings are put back afterwards.
|
|
333
|
+
*
|
|
334
|
+
* @throws {GLPipelineError} if the texture or renderbuffer cannot be created
|
|
335
|
+
* or the framebuffer is not complete; the pass is then left with no target.
|
|
336
|
+
*/
|
|
337
|
+
resize(width, height) {
|
|
338
|
+
const gl = this.#gl;
|
|
339
|
+
const w = Math.max(1, Math.floor(width));
|
|
340
|
+
const h = Math.max(1, Math.floor(height));
|
|
341
|
+
if (this.#target !== null && w === this.#width && h === this.#height) return;
|
|
342
|
+
this.#release();
|
|
343
|
+
const savedTexture = gl.getParameter(gl.TEXTURE_BINDING_2D);
|
|
344
|
+
const savedRenderbuffer = gl.getParameter(gl.RENDERBUFFER_BINDING);
|
|
345
|
+
const savedDraw = gl.getParameter(gl.DRAW_FRAMEBUFFER_BINDING);
|
|
346
|
+
const savedRead = gl.getParameter(gl.READ_FRAMEBUFFER_BINDING);
|
|
347
|
+
let target = null;
|
|
348
|
+
let depth = null;
|
|
349
|
+
try {
|
|
350
|
+
target = gl.createTexture();
|
|
351
|
+
if (target === null) throw new GLPipelineError("could not create a pick target texture");
|
|
352
|
+
gl.bindTexture(gl.TEXTURE_2D, target);
|
|
353
|
+
gl.texImage2D(
|
|
354
|
+
gl.TEXTURE_2D,
|
|
355
|
+
0,
|
|
356
|
+
gl.RGBA32UI,
|
|
357
|
+
w,
|
|
358
|
+
h,
|
|
359
|
+
0,
|
|
360
|
+
gl.RGBA_INTEGER,
|
|
361
|
+
gl.UNSIGNED_INT,
|
|
362
|
+
null
|
|
363
|
+
);
|
|
364
|
+
gl.texParameteri(gl.TEXTURE_2D, gl.TEXTURE_MIN_FILTER, gl.NEAREST);
|
|
365
|
+
gl.texParameteri(gl.TEXTURE_2D, gl.TEXTURE_MAG_FILTER, gl.NEAREST);
|
|
366
|
+
depth = gl.createRenderbuffer();
|
|
367
|
+
if (depth === null) throw new GLPipelineError("could not create a pick depth buffer");
|
|
368
|
+
gl.bindRenderbuffer(gl.RENDERBUFFER, depth);
|
|
369
|
+
gl.renderbufferStorage(gl.RENDERBUFFER, gl.DEPTH_COMPONENT24, w, h);
|
|
370
|
+
gl.bindFramebuffer(gl.FRAMEBUFFER, this.#framebuffer);
|
|
371
|
+
gl.framebufferTexture2D(gl.FRAMEBUFFER, gl.COLOR_ATTACHMENT0, gl.TEXTURE_2D, target, 0);
|
|
372
|
+
gl.framebufferRenderbuffer(gl.FRAMEBUFFER, gl.DEPTH_ATTACHMENT, gl.RENDERBUFFER, depth);
|
|
373
|
+
const status = gl.checkFramebufferStatus(gl.FRAMEBUFFER);
|
|
374
|
+
if (status !== gl.FRAMEBUFFER_COMPLETE) {
|
|
375
|
+
throw new GLPipelineError(
|
|
376
|
+
`the pick framebuffer is not complete: 0x${status.toString(16)}`
|
|
377
|
+
);
|
|
378
|
+
}
|
|
379
|
+
this.#target = target;
|
|
380
|
+
this.#depth = depth;
|
|
381
|
+
this.#width = w;
|
|
382
|
+
this.#height = h;
|
|
383
|
+
target = null;
|
|
384
|
+
depth = null;
|
|
385
|
+
} finally {
|
|
386
|
+
if (target !== null) gl.deleteTexture(target);
|
|
387
|
+
if (depth !== null) gl.deleteRenderbuffer(depth);
|
|
388
|
+
gl.bindTexture(gl.TEXTURE_2D, savedTexture);
|
|
389
|
+
gl.bindRenderbuffer(gl.RENDERBUFFER, savedRenderbuffer);
|
|
390
|
+
gl.bindFramebuffer(gl.DRAW_FRAMEBUFFER, savedDraw);
|
|
391
|
+
gl.bindFramebuffer(gl.READ_FRAMEBUFFER, savedRead);
|
|
392
|
+
}
|
|
393
|
+
}
|
|
394
|
+
/**
|
|
395
|
+
* Draw the pick pass and read one pixel.
|
|
396
|
+
*
|
|
397
|
+
* The pass sets the state it depends on — no scissor, every color channel
|
|
398
|
+
* and depth writable, `LESS` depth testing, no blending — and afterwards
|
|
399
|
+
* puts back each of those, the viewport and the caller's framebuffers, even
|
|
400
|
+
* if the draw throws. Face culling is left as the caller set it, so the pick
|
|
401
|
+
* honours winding exactly as the color pass does. The pipeline's draw also
|
|
402
|
+
* changes the current program and texture bindings (see
|
|
403
|
+
* {@link GLPipeline.draw}).
|
|
404
|
+
*
|
|
405
|
+
* @param x horizontal position in framebuffer pixels, from the left.
|
|
406
|
+
* @param y vertical position in framebuffer pixels, from the **bottom** —
|
|
407
|
+
* GL's origin, not the DOM's. {@link fromCanvasPoint} converts.
|
|
408
|
+
* @param writeUniforms fills the pipeline's block from the same state the
|
|
409
|
+
* color pass used.
|
|
410
|
+
* @returns the hit, or `null` where nothing was drawn or the point is outside.
|
|
411
|
+
* @throws {GLPipelineError} before {@link resize} has been called, or if the
|
|
412
|
+
* pipeline refuses to draw (a texture missing).
|
|
413
|
+
*/
|
|
414
|
+
pick(x, y, writeUniforms) {
|
|
415
|
+
const gl = this.#gl;
|
|
416
|
+
if (this.#target === null)
|
|
417
|
+
throw new GLPipelineError("the pick pass has no target; call resize first");
|
|
418
|
+
const px = Math.floor(x);
|
|
419
|
+
const py = Math.floor(y);
|
|
420
|
+
if (px < 0 || py < 0 || px >= this.#width || py >= this.#height) return null;
|
|
421
|
+
writeUniforms(this.#pipeline.uniforms);
|
|
422
|
+
const saved = {
|
|
423
|
+
draw: gl.getParameter(gl.DRAW_FRAMEBUFFER_BINDING),
|
|
424
|
+
read: gl.getParameter(gl.READ_FRAMEBUFFER_BINDING),
|
|
425
|
+
viewport: gl.getParameter(gl.VIEWPORT),
|
|
426
|
+
blend: gl.isEnabled(gl.BLEND),
|
|
427
|
+
depthTest: gl.isEnabled(gl.DEPTH_TEST),
|
|
428
|
+
scissorTest: gl.isEnabled(gl.SCISSOR_TEST),
|
|
429
|
+
depthMask: gl.getParameter(gl.DEPTH_WRITEMASK),
|
|
430
|
+
colorMask: gl.getParameter(gl.COLOR_WRITEMASK),
|
|
431
|
+
depthFunc: gl.getParameter(gl.DEPTH_FUNC)
|
|
432
|
+
};
|
|
433
|
+
this.#readback.fill(PICK_EMPTY);
|
|
434
|
+
try {
|
|
435
|
+
gl.bindFramebuffer(gl.FRAMEBUFFER, this.#framebuffer);
|
|
436
|
+
gl.viewport(0, 0, this.#width, this.#height);
|
|
437
|
+
gl.disable(gl.SCISSOR_TEST);
|
|
438
|
+
gl.colorMask(true, true, true, true);
|
|
439
|
+
gl.depthMask(true);
|
|
440
|
+
gl.clearBufferuiv(gl.COLOR, 0, EMPTY_COLOR);
|
|
441
|
+
gl.clearBufferfv(gl.DEPTH, 0, FAR_DEPTH);
|
|
442
|
+
gl.enable(gl.DEPTH_TEST);
|
|
443
|
+
gl.depthFunc(gl.LESS);
|
|
444
|
+
gl.disable(gl.BLEND);
|
|
445
|
+
this.#pipeline.draw();
|
|
446
|
+
gl.readPixels(px, py, 1, 1, gl.RGBA_INTEGER, gl.UNSIGNED_INT, this.#readback);
|
|
447
|
+
} finally {
|
|
448
|
+
gl.bindFramebuffer(gl.DRAW_FRAMEBUFFER, saved.draw);
|
|
449
|
+
gl.bindFramebuffer(gl.READ_FRAMEBUFFER, saved.read);
|
|
450
|
+
const [vx, vy, vw, vh] = saved.viewport;
|
|
451
|
+
gl.viewport(vx, vy, vw, vh);
|
|
452
|
+
const toggle = (cap, on) => on ? gl.enable(cap) : gl.disable(cap);
|
|
453
|
+
toggle(gl.BLEND, saved.blend);
|
|
454
|
+
toggle(gl.DEPTH_TEST, saved.depthTest);
|
|
455
|
+
toggle(gl.SCISSOR_TEST, saved.scissorTest);
|
|
456
|
+
gl.depthMask(saved.depthMask);
|
|
457
|
+
const [r, g, b, a] = saved.colorMask;
|
|
458
|
+
gl.colorMask(r, g, b, a);
|
|
459
|
+
gl.depthFunc(saved.depthFunc);
|
|
460
|
+
}
|
|
461
|
+
const hit = decodePick(this.#readback);
|
|
462
|
+
return hit === null ? null : { ...hit, x: px, y: py };
|
|
463
|
+
}
|
|
464
|
+
/**
|
|
465
|
+
* A canvas point as {@link pick} wants it: the integer device pixel under
|
|
466
|
+
* the point, counted from the bottom-left.
|
|
467
|
+
*
|
|
468
|
+
* A DOM event gives CSS pixels from the top-left of the element; GL reads
|
|
469
|
+
* device pixels from the bottom-left. Getting this wrong picks a mirrored
|
|
470
|
+
* point, which looks plausible near the middle and wrong everywhere else.
|
|
471
|
+
* The top CSS row maps to row `height - 1`; a point on or past an edge maps
|
|
472
|
+
* outside the target, where {@link pick} returns `null`.
|
|
473
|
+
*/
|
|
474
|
+
static fromCanvasPoint(canvas, offsetX, offsetY) {
|
|
475
|
+
const scaleX = canvas.width / Math.max(1, canvas.clientWidth);
|
|
476
|
+
const scaleY = canvas.height / Math.max(1, canvas.clientHeight);
|
|
477
|
+
return {
|
|
478
|
+
x: Math.floor(offsetX * scaleX),
|
|
479
|
+
y: canvas.height - 1 - Math.floor(offsetY * scaleY)
|
|
480
|
+
};
|
|
481
|
+
}
|
|
482
|
+
/** Release the target, its depth buffer and the framebuffer. */
|
|
483
|
+
dispose() {
|
|
484
|
+
this.#gl.deleteFramebuffer(this.#framebuffer);
|
|
485
|
+
this.#release();
|
|
486
|
+
}
|
|
487
|
+
#release() {
|
|
488
|
+
const gl = this.#gl;
|
|
489
|
+
if (this.#target !== null) gl.deleteTexture(this.#target);
|
|
490
|
+
if (this.#depth !== null) gl.deleteRenderbuffer(this.#depth);
|
|
491
|
+
this.#target = null;
|
|
492
|
+
this.#depth = null;
|
|
493
|
+
this.#width = 0;
|
|
494
|
+
this.#height = 0;
|
|
495
|
+
}
|
|
496
|
+
};
|
|
497
|
+
|
|
498
|
+
// src/version.ts
|
|
499
|
+
var VERSION = "1.0.0";
|
|
500
|
+
|
|
501
|
+
export { GLPipeline, GLPipelineError, PickPass, UNIFORM_BLOCK_BINDING, UNIFORM_BLOCK_NAME, VERSION, linkProgram };
|
|
502
|
+
//# sourceMappingURL=index.js.map
|
|
503
|
+
//# sourceMappingURL=index.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"sources":["../src/pipeline.ts","../src/pick.ts","../src/version.ts"],"names":[],"mappings":";;;AAiCO,IAAM,kBAAA,GAAqB;AAG3B,IAAM,qBAAA,GAAwB;AA2B9B,IAAM,eAAA,GAAN,cAA8B,KAAA,CAAM;AAAA,EACvB,IAAA,GAAO,iBAAA;AAC3B;AASA,SAAS,eAAe,KAAA,EAA2D;AACjF,EAAA,MAAM,KAAA,GAAQ,kBAAA,CAAmB,IAAA,CAAK,KAAA,CAAM,IAAI,CAAA;AAChD,EAAA,IAAI,KAAA,KAAU,IAAA,EAAM,OAAO,EAAE,SAAS,KAAA,CAAM,CAAC,CAAA,KAAM,GAAA,EAAK,KAAA,EAAO,MAAA,CAAO,KAAA,CAAM,CAAC,CAAC,CAAA,EAAE;AAChF,EAAA,IAAI,KAAA,CAA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AS,MAAA,CAAO,KAAA,GAAQ,KAAK,GAAA,CAAI,CAAA,EAAG,OAAO,WAAW,CAAA;AAC5D,IAAA,MAAM,SAAS,MAAA,CAAO,MAAA,GAAS,KAAK,GAAA,CAAI,CAAA,EAAG,OAAO,YAAY,CAAA;AAC9D,IAAA,OAAO;AAAA,MACL,CAAA,EAAG,IAAA,CAAK,KAAA,CAAM,OAAA,GAAU,MAAM,CAAA;AAAA,MAC9B,GAAG,MAAA,CAAO,MAAA,GAAS,IAAI,IAAA,CAAK,KAAA,CAAM,UAAU,MAAM;AAAA,KACpD;AAAA,EACF;AAAA;AAAA,EAGA,OAAA,GAAgB;AACd,IAAA,IAAA,CAAK,GAAA,CAAI,iBAAA,CAAkB,IAAA,CAAK,YAAY,CAAA;AAC5C,IAAA,IAAA,CAAK,QAAA,EAAS;AAAA,EAChB;AAAA,EAEA,QAAA,GAAiB;AACf,IAAA,MAAM,KAAK,IAAA,CAAK,GAAA;AAChB,IAAA,IAAI,KAAK,OAAA,KAAY,IAAA,EAAM,EAAA,CAAG,aAAA,CAAc,KAAK,OAAO,CAAA;AACxD,IAAA,IAAI,KAAK,MAAA,KAAW,IAAA,EAAM,EAAA,CAAG,kBAAA,CAAmB,KAAK,MAAM,CAAA;AAC3D,IAAA,IAAA,CAAK,OAAA,GAAU,IAAA;AACf,IAAA,IAAA,CAAK,MAAA,GAAS,IAAA;AACd,IAAA,IAAA,CAAK,MAAA,GAAS,CAAA;AACd,IAAA,IAAA,CAAK,OAAA,GAAU,CAAA;AAAA,EACjB;AACF;;;ACnQO,IAAM,OAAA,GAAkB","file":"index.js","sourcesContent":["// Copyright Visual Collaboration Technologies Inc. All Rights Reserved.\n// See LICENSE for details.\n\n/**\n * One pipeline envelope, bound to a WebGL2 context.\n *\n * Everything here is driven by the envelope and nothing by knowledge of a\n * particular shader: the contract says which attributes to supply and which\n * uniforms exist, the layout says where each uniform sits, and the bindings\n * say which sampler uniform and texture unit each resource takes. A pipeline\n * this file has never heard of binds correctly as long as the set describes it.\n *\n * **What it deliberately does not do is decide values.** Which uniforms to\n * write, what to put in them, and what a table or image contains are the\n * caller's; this owns the GL objects and the wiring between them. That split is\n * what keeps a viewer's policy out of the binding layer and the binding layer\n * out of every viewer.\n *\n * Input it cannot use is refused with a {@link GLPipelineError} rather than\n * skipped: a draw that leaves a sampler unbound or reads an attribute with the\n * wrong type shows a plausible wrong picture, which is harder to find than an\n * exception.\n */\n\nimport type {\n AttributeSpec,\n Envelope,\n LanguageBlock,\n ShaderLanguage,\n} from \"@vcollab/shader-library\";\nimport { ResourceBindings, ShaderSet, UniformBlock } from \"@vcollab/shader-library\";\n\n/** The std140 block every emitted stage declares. */\nexport const UNIFORM_BLOCK_NAME = \"GlobalParams_std140\";\n\n/** The binding point the block is bound to. */\nexport const UNIFORM_BLOCK_BINDING = 0;\n\n/** One attribute's data, as the caller holds it. */\nexport interface AttributeSource {\n /**\n * Tightly packed, `components` values per vertex. `Int32Array` for an\n * attribute the contract declares `int`/`ivecN`, `Float32Array` for\n * `float`/`vecN`.\n */\n readonly data: Float32Array | Int32Array;\n /** How many values one vertex carries in `data`; at least the declared width. */\n readonly components: number;\n /**\n * Whether the shader reads it as an integer attribute. Checked against the\n * contract: a source that disagrees is refused.\n */\n readonly integer: boolean;\n}\n\n/** The geometry a pipeline draws. */\nexport interface Geometry {\n /** Attribute data by the contract's **semantic**, not by its declared name. */\n readonly attributes: ReadonlyMap<string, AttributeSource>;\n readonly indices: Uint32Array;\n}\n\n/** Raised for anything this context would not accept. */\nexport class GLPipelineError extends Error {\n override readonly name = \"GLPipelineError\";\n}\n\n/** An attribute the caller did not supply, held at a constant. */\ninterface AbsentAttribute {\n readonly location: number;\n readonly integer: boolean;\n}\n\n/** How a contract attribute type is fed: integer or float, and how wide. */\nfunction attributeShape(input: AttributeSpec): { integer: boolean; width: number } {\n const match = /^(i?)vec([234])$/.exec(input.type);\n if (match !== null) return { integer: match[1] === \"i\", width: Number(match[2]) };\n if (input.type === \"float\") return { integer: false, width: 1 };\n if (input.type === \"int\") return { integer: true, width: 1 };\n throw new GLPipelineError(\n `${input.name} is declared ${input.type}; only float, vecN, int and ivecN attributes ` +\n \"are supported\",\n );\n}\n\nfunction compile(\n gl: WebGL2RenderingContext,\n type: number,\n source: string,\n stage: string,\n): WebGLShader {\n const shader = gl.createShader(type);\n if (shader === null) throw new GLPipelineError(`could not create a ${stage} shader`);\n gl.shaderSource(shader, source);\n gl.compileShader(shader);\n if (!gl.getShaderParameter(shader, gl.COMPILE_STATUS)) {\n const log = gl.getShaderInfoLog(shader);\n gl.deleteShader(shader);\n throw new GLPipelineError(`the ${stage} stage did not compile on this driver:\\n${log}`);\n }\n return shader;\n}\n\n/**\n * Compile and link one pipeline's two stages.\n *\n * The text is the build's output and is handed to the driver as it stands —\n * nothing is patched on the way. The only thing added is the report when a\n * driver disagrees, which is the one failure offline validation cannot rule\n * out. Every GL object made on the way is freed if a later step fails.\n */\nexport function linkProgram(\n gl: WebGL2RenderingContext,\n vertex: string,\n fragment: string,\n): WebGLProgram {\n const vertexShader = compile(gl, gl.VERTEX_SHADER, vertex, \"vertex\");\n let fragmentShader: WebGLShader;\n try {\n fragmentShader = compile(gl, gl.FRAGMENT_SHADER, fragment, \"fragment\");\n } catch (error) {\n gl.deleteShader(vertexShader);\n throw error;\n }\n const program = gl.createProgram();\n if (program === null) {\n gl.deleteShader(vertexShader);\n gl.deleteShader(fragmentShader);\n throw new GLPipelineError(\"could not create a program\");\n }\n gl.attachShader(program, vertexShader);\n gl.attachShader(program, fragmentShader);\n gl.linkProgram(program);\n // The shaders belong to the program once attached; deleting the handles here\n // frees them when the program goes.\n gl.deleteShader(vertexShader);\n gl.deleteShader(fragmentShader);\n if (!gl.getProgramParameter(program, gl.LINK_STATUS)) {\n const log = gl.getProgramInfoLog(program);\n gl.deleteProgram(program);\n throw new GLPipelineError(`the stage pair did not link on this driver:\\n${log}`);\n }\n\n const blockIndex = gl.getUniformBlockIndex(program, UNIFORM_BLOCK_NAME);\n if (blockIndex !== gl.INVALID_INDEX) {\n gl.uniformBlockBinding(program, blockIndex, UNIFORM_BLOCK_BINDING);\n }\n return program;\n}\n\n/** A pipeline with its program, geometry, uniform buffer and resources. */\nexport class GLPipeline {\n readonly #gl: WebGL2RenderingContext;\n readonly #program: WebGLProgram;\n readonly #ubo: WebGLBuffer;\n readonly #vao: WebGLVertexArrayObject;\n /** The current geometry's vertex and index buffers; replaced by each setGeometry. */\n #buffers: WebGLBuffer[] = [];\n /** Attributes held at a constant, set again every draw (the constant is not VAO state). */\n #absent: AbsentAttribute[] = [];\n readonly #textures = new Map<string, WebGLTexture>();\n /** Each binding's sampler uniform location, in `bindings.all` order. */\n readonly #samplerLocations: (WebGLUniformLocation | null)[];\n /** A byte view of `uniforms.buffer`, made once. */\n readonly #uniformBytes: Uint8Array;\n #indexCount = 0;\n\n /** The envelope this was built from. */\n readonly envelope: Envelope;\n /** Its language block: layout, bindings and sources. */\n readonly block: LanguageBlock;\n /** Its sampler bindings, by semantic. */\n readonly bindings: ResourceBindings;\n /**\n * The uniform block to write into. The caller sets whatever the pipeline\n * declares; {@link draw} uploads it.\n */\n readonly uniforms: UniformBlock;\n\n /**\n * @throws {GLPipelineError} if the envelope lacks a stage, the driver rejects\n * the text, a GL object cannot be created, or the driver's size for the\n * uniform block differs from the size the build reflected. Anything created\n * before the failure is freed.\n */\n constructor(gl: WebGL2RenderingContext, envelope: Envelope, language: ShaderLanguage = \"glsl\") {\n this.#gl = gl;\n this.envelope = envelope;\n this.block = ShaderSet.language(envelope, language);\n const stages = this.block.stages;\n if (stages.vertex === undefined || stages.fragment === undefined) {\n throw new GLPipelineError(`${envelope.pipeline.id} does not carry both stages`);\n }\n const program = linkProgram(gl, stages.vertex.source, stages.fragment.source);\n let ubo: WebGLBuffer | null = null;\n try {\n this.bindings = new ResourceBindings(this.block);\n this.uniforms = new UniformBlock(this.block);\n this.#uniformBytes = new Uint8Array(this.uniforms.buffer);\n\n // The buffer bound to a block must be at least the size the driver\n // reports for it. std140 fixes every member's offset, so a driver may\n // report more than the reflected blockSize (trailing padding) and the\n // buffer simply grows to match. A driver block too small to hold the\n // last member the envelope records is a different layout, and refused.\n let bufferSize = this.block.blockSize;\n const blockIndex = gl.getUniformBlockIndex(program, UNIFORM_BLOCK_NAME);\n if (blockIndex !== gl.INVALID_INDEX) {\n const size: unknown = gl.getActiveUniformBlockParameter(\n program,\n blockIndex,\n gl.UNIFORM_BLOCK_DATA_SIZE,\n );\n const end = Math.max(\n 0,\n ...Object.values(this.block.layout).map((member) => member.offset + member.size),\n );\n if (typeof size !== \"number\" || size < end) {\n throw new GLPipelineError(\n `${envelope.pipeline.id}: the driver sizes ${UNIFORM_BLOCK_NAME} at ${String(size)} ` +\n `bytes, too small for the layout the build reflected, which ends at byte ${end}`,\n );\n }\n bufferSize = Math.max(bufferSize, size);\n }\n\n ubo = gl.createBuffer();\n if (ubo === null) throw new GLPipelineError(\"could not create a uniform buffer\");\n gl.bindBuffer(gl.UNIFORM_BUFFER, ubo);\n gl.bufferData(gl.UNIFORM_BUFFER, bufferSize, gl.DYNAMIC_DRAW);\n\n const vao = gl.createVertexArray();\n if (vao === null) throw new GLPipelineError(\"could not create a vertex array\");\n\n this.#samplerLocations = this.bindings.all.map((record) =>\n gl.getUniformLocation(program, record.uniform),\n );\n this.#program = program;\n this.#ubo = ubo;\n this.#vao = vao;\n } catch (error) {\n if (ubo !== null) gl.deleteBuffer(ubo);\n gl.deleteProgram(program);\n throw error;\n }\n }\n\n /** The linked program, for a caller that needs it directly. */\n get program(): WebGLProgram {\n return this.#program;\n }\n\n /**\n * Upload the geometry, matching the contract's attributes by semantic.\n *\n * An attribute the caller cannot supply is left as a **disabled array with a\n * constant**, which is what a viewer does for data a draw does not have —\n * the same \"off state is data\" the always-present features rely on. The\n * constant is context state rather than vertex-array state, so {@link draw}\n * sets it again each time. The contract's declared width wins over the\n * source's, so a `vec3` read from a wider field takes the first three values\n * of each vertex.\n *\n * Calling it again replaces the previous geometry and frees its buffers.\n *\n * @throws {GLPipelineError} if a source's data type or `integer` flag does\n * not match the declared type, its `components` is narrower than the\n * declared width, or a buffer cannot be created. A refused source changes\n * nothing; when buffer creation fails the pipeline is left with no geometry.\n */\n setGeometry(geometry: Geometry): void {\n const gl = this.#gl;\n\n // Check everything before touching GL, so refused input changes nothing.\n const plan = this.envelope.pipeline.inputs.map((input) => {\n const shape = attributeShape(input);\n const source = geometry.attributes.get(input.semantic);\n if (source !== undefined) {\n const label = `${input.semantic} (${input.name}, ${input.type})`;\n if (source.integer !== shape.integer) {\n throw new GLPipelineError(\n `${label} is ${shape.integer ? \"an integer\" : \"a float\"} attribute; the source ` +\n `says integer: ${source.integer}`,\n );\n }\n const wanted = shape.integer ? Int32Array : Float32Array;\n if (!(source.data instanceof wanted)) {\n throw new GLPipelineError(`${label} needs ${wanted.name} data`);\n }\n if (!Number.isInteger(source.components) || source.components < shape.width) {\n throw new GLPipelineError(\n `${label} reads ${shape.width} values per vertex; the source carries ` +\n `${source.components}`,\n );\n }\n }\n return { input, shape, source, location: gl.getAttribLocation(this.#program, input.name) };\n });\n\n for (const buffer of this.#buffers) gl.deleteBuffer(buffer);\n this.#buffers = [];\n this.#absent = [];\n this.#indexCount = 0;\n\n const buffers: WebGLBuffer[] = [];\n const absent: AbsentAttribute[] = [];\n gl.bindVertexArray(this.#vao);\n try {\n for (const { input, shape, source, location } of plan) {\n if (location < 0) continue;\n if (source === undefined) {\n gl.disableVertexAttribArray(location);\n absent.push({ location, integer: shape.integer });\n continue;\n }\n const buffer = gl.createBuffer();\n if (buffer === null)\n throw new GLPipelineError(`could not create a buffer for ${input.name}`);\n buffers.push(buffer);\n gl.bindBuffer(gl.ARRAY_BUFFER, buffer);\n gl.bufferData(gl.ARRAY_BUFFER, source.data, gl.STATIC_DRAW);\n gl.enableVertexAttribArray(location);\n const stride = source.components * 4;\n if (shape.integer) gl.vertexAttribIPointer(location, shape.width, gl.INT, stride, 0);\n else gl.vertexAttribPointer(location, shape.width, gl.FLOAT, false, stride, 0);\n }\n\n const elements = gl.createBuffer();\n if (elements === null) throw new GLPipelineError(\"could not create an index buffer\");\n buffers.push(elements);\n gl.bindBuffer(gl.ELEMENT_ARRAY_BUFFER, elements);\n gl.bufferData(gl.ELEMENT_ARRAY_BUFFER, geometry.indices, gl.STATIC_DRAW);\n } catch (error) {\n for (const buffer of buffers) gl.deleteBuffer(buffer);\n throw error;\n } finally {\n gl.bindVertexArray(null);\n }\n this.#buffers = buffers;\n this.#absent = absent;\n this.#indexCount = geometry.indices.length;\n }\n\n /**\n * Give a semantic its texture.\n *\n * By semantic, because the name a table or texture is declared under never\n * reaches the linked program: GLSL ES 3.00 has no binding qualifier for\n * samplers, so the cross-compiler pairs each texture with a sampler and names\n * the combination itself. The envelope's record is the only route across.\n *\n * An integer table (`usampler2D`) must use `NEAREST` filtering and no\n * mipmaps, or the texture is incomplete and every fetch returns zero.\n */\n setTexture(semantic: string, texture: WebGLTexture | null): void {\n if (texture === null) this.#textures.delete(semantic);\n else this.#textures.set(semantic, texture);\n }\n\n /** Every semantic this pipeline expects a texture for. */\n get requiredSemantics(): string[] {\n return this.bindings.semantics;\n }\n\n /**\n * Draw, after uploading the uniform block and binding every resource.\n *\n * A no-op without geometry. Leaves the current program, the active texture\n * unit and the texture bound on each of the pipeline's units changed.\n *\n * @throws {GLPipelineError} if any semantic in {@link requiredSemantics} has\n * no texture; the message names every one missing.\n */\n draw(): void {\n const gl = this.#gl;\n if (this.#indexCount === 0) return;\n\n const missing = this.requiredSemantics.filter((semantic) => !this.#textures.has(semantic));\n if (missing.length > 0) {\n throw new GLPipelineError(\n `${this.envelope.pipeline.id} has no texture for: ${missing.join(\", \")}`,\n );\n }\n\n gl.useProgram(this.#program);\n gl.bindBuffer(gl.UNIFORM_BUFFER, this.#ubo);\n gl.bufferSubData(gl.UNIFORM_BUFFER, 0, this.#uniformBytes);\n gl.bindBufferBase(gl.UNIFORM_BUFFER, UNIFORM_BLOCK_BINDING, this.#ubo);\n\n this.bindings.all.forEach((record, index) => {\n gl.activeTexture(gl.TEXTURE0 + record.unit);\n gl.bindTexture(gl.TEXTURE_2D, this.#textures.get(record.semantic) as WebGLTexture);\n const location = this.#samplerLocations[index] as WebGLUniformLocation | null;\n if (location !== null) gl.uniform1i(location, record.unit);\n });\n\n // The constant for a disabled array is context state, not VAO state, and\n // the same location can be a vec3 in one pipeline and an int in another.\n for (const { location, integer } of this.#absent) {\n if (integer) gl.vertexAttribI4i(location, 0, 0, 0, 0);\n else gl.vertexAttrib4f(location, 0, 0, 0, 0);\n }\n\n gl.bindVertexArray(this.#vao);\n gl.drawElements(gl.TRIANGLES, this.#indexCount, gl.UNSIGNED_INT, 0);\n gl.bindVertexArray(null);\n }\n\n /** Release the GL objects this owns. Textures given to it are the caller's. */\n dispose(): void {\n const gl = this.#gl;\n gl.deleteProgram(this.#program);\n gl.deleteBuffer(this.#ubo);\n gl.deleteVertexArray(this.#vao);\n for (const buffer of this.#buffers) gl.deleteBuffer(buffer);\n this.#buffers = [];\n this.#absent = [];\n this.#indexCount = 0;\n this.#textures.clear();\n }\n}\n","// Copyright Visual Collaboration Technologies Inc. All Rights Reserved.\n// See LICENSE for details.\n\n/**\n * Hit-testing: the pick pipeline drawn into an offscreen integer target.\n *\n * The pick shape is exclusive — it writes identifiers *instead of* a color —\n * so picking is a second draw of the same geometry, not a feature added to the\n * color pipeline. That is the cheaper arrangement by some distance: the extra\n * draw happens when someone clicks, where making the color pipeline write a\n * second attachment would cost bandwidth on every frame, force every consumer\n * off the default framebuffer, and give up multisampling — an integer\n * attachment cannot be multisampled, and every attachment of a framebuffer\n * shares its sample count.\n *\n * Keeping the two passes in agreement is the price. The camera, the mask, the\n * placements and the deform scale must match what is on screen, or the pick\n * names something the viewer cannot see. {@link PickPass.pick} takes a callback\n * to write the uniforms for exactly that reason: the caller writes them from\n * the same state it used for the color pass, rather than this holding a\n * second copy that could drift.\n */\n\nimport type { Pick, UniformBlock } from \"@vcollab/shader-library\";\nimport { decodePick, PICK_EMPTY } from \"@vcollab/shader-library\";\nimport type { GLPipeline } from \"./pipeline.ts\";\nimport { GLPipelineError } from \"./pipeline.ts\";\n\n/** A hit, with the pixel it was found at. */\nexport interface PickHit extends Pick {\n /** Where the pick was taken, in framebuffer pixels. */\n readonly x: number;\n readonly y: number;\n}\n\nconst EMPTY_COLOR = new Uint32Array([PICK_EMPTY, PICK_EMPTY, PICK_EMPTY, PICK_EMPTY]);\nconst FAR_DEPTH = new Float32Array([1]);\n\n/**\n * An offscreen integer target and the pipeline that draws into it.\n *\n * The pass owns its framebuffer and resizes it with the viewport; the pipeline\n * is the caller's, built from a `pick` envelope whose vertex variant matches\n * the color pipeline's — `deform3.pick` for a `deform3` color pass, so that\n * deformed geometry is picked where it is drawn.\n */\nexport class PickPass {\n readonly #gl: WebGL2RenderingContext;\n readonly #pipeline: GLPipeline;\n readonly #framebuffer: WebGLFramebuffer;\n #target: WebGLTexture | null = null;\n #depth: WebGLRenderbuffer | null = null;\n #width = 0;\n #height = 0;\n readonly #readback = new Uint32Array(4);\n\n constructor(gl: WebGL2RenderingContext, pipeline: GLPipeline) {\n this.#gl = gl;\n this.#pipeline = pipeline;\n const framebuffer = gl.createFramebuffer();\n if (framebuffer === null) throw new GLPipelineError(\"could not create a pick framebuffer\");\n this.#framebuffer = framebuffer;\n }\n\n /** The pipeline drawing into the target, for setting its geometry and textures. */\n get pipeline(): GLPipeline {\n return this.#pipeline;\n }\n\n /**\n * Match the target to the viewport.\n *\n * `rgba32ui` for the attachment, with a depth buffer beside it so the\n * nearest primitive wins exactly as it does on screen. Neither is\n * multisampled: an integer attachment cannot be. Sizes are floored, and a\n * call at the current floored size does nothing. The caller's texture,\n * renderbuffer and framebuffer bindings are put back afterwards.\n *\n * @throws {GLPipelineError} if the texture or renderbuffer cannot be created\n * or the framebuffer is not complete; the pass is then left with no target.\n */\n resize(width: number, height: number): void {\n const gl = this.#gl;\n const w = Math.max(1, Math.floor(width));\n const h = Math.max(1, Math.floor(height));\n if (this.#target !== null && w === this.#width && h === this.#height) return;\n this.#release();\n\n const savedTexture = gl.getParameter(gl.TEXTURE_BINDING_2D) as WebGLTexture | null;\n const savedRenderbuffer = gl.getParameter(gl.RENDERBUFFER_BINDING) as WebGLRenderbuffer | null;\n const savedDraw = gl.getParameter(gl.DRAW_FRAMEBUFFER_BINDING) as WebGLFramebuffer | null;\n const savedRead = gl.getParameter(gl.READ_FRAMEBUFFER_BINDING) as WebGLFramebuffer | null;\n let target: WebGLTexture | null = null;\n let depth: WebGLRenderbuffer | null = null;\n try {\n target = gl.createTexture();\n if (target === null) throw new GLPipelineError(\"could not create a pick target texture\");\n gl.bindTexture(gl.TEXTURE_2D, target);\n gl.texImage2D(\n gl.TEXTURE_2D,\n 0,\n gl.RGBA32UI,\n w,\n h,\n 0,\n gl.RGBA_INTEGER,\n gl.UNSIGNED_INT,\n null,\n );\n // An integer texture can only be fetched, never filtered.\n gl.texParameteri(gl.TEXTURE_2D, gl.TEXTURE_MIN_FILTER, gl.NEAREST);\n gl.texParameteri(gl.TEXTURE_2D, gl.TEXTURE_MAG_FILTER, gl.NEAREST);\n\n depth = gl.createRenderbuffer();\n if (depth === null) throw new GLPipelineError(\"could not create a pick depth buffer\");\n gl.bindRenderbuffer(gl.RENDERBUFFER, depth);\n gl.renderbufferStorage(gl.RENDERBUFFER, gl.DEPTH_COMPONENT24, w, h);\n\n gl.bindFramebuffer(gl.FRAMEBUFFER, this.#framebuffer);\n gl.framebufferTexture2D(gl.FRAMEBUFFER, gl.COLOR_ATTACHMENT0, gl.TEXTURE_2D, target, 0);\n gl.framebufferRenderbuffer(gl.FRAMEBUFFER, gl.DEPTH_ATTACHMENT, gl.RENDERBUFFER, depth);\n const status = gl.checkFramebufferStatus(gl.FRAMEBUFFER);\n if (status !== gl.FRAMEBUFFER_COMPLETE) {\n throw new GLPipelineError(\n `the pick framebuffer is not complete: 0x${status.toString(16)}`,\n );\n }\n\n this.#target = target;\n this.#depth = depth;\n this.#width = w;\n this.#height = h;\n target = null;\n depth = null;\n } finally {\n // Still set only if something above threw: free the half-built target.\n if (target !== null) gl.deleteTexture(target);\n if (depth !== null) gl.deleteRenderbuffer(depth);\n gl.bindTexture(gl.TEXTURE_2D, savedTexture);\n gl.bindRenderbuffer(gl.RENDERBUFFER, savedRenderbuffer);\n gl.bindFramebuffer(gl.DRAW_FRAMEBUFFER, savedDraw);\n gl.bindFramebuffer(gl.READ_FRAMEBUFFER, savedRead);\n }\n }\n\n /**\n * Draw the pick pass and read one pixel.\n *\n * The pass sets the state it depends on — no scissor, every color channel\n * and depth writable, `LESS` depth testing, no blending — and afterwards\n * puts back each of those, the viewport and the caller's framebuffers, even\n * if the draw throws. Face culling is left as the caller set it, so the pick\n * honours winding exactly as the color pass does. The pipeline's draw also\n * changes the current program and texture bindings (see\n * {@link GLPipeline.draw}).\n *\n * @param x horizontal position in framebuffer pixels, from the left.\n * @param y vertical position in framebuffer pixels, from the **bottom** —\n * GL's origin, not the DOM's. {@link fromCanvasPoint} converts.\n * @param writeUniforms fills the pipeline's block from the same state the\n * color pass used.\n * @returns the hit, or `null` where nothing was drawn or the point is outside.\n * @throws {GLPipelineError} before {@link resize} has been called, or if the\n * pipeline refuses to draw (a texture missing).\n */\n pick(x: number, y: number, writeUniforms: (uniforms: UniformBlock) => void): PickHit | null {\n const gl = this.#gl;\n if (this.#target === null)\n throw new GLPipelineError(\"the pick pass has no target; call resize first\");\n\n const px = Math.floor(x);\n const py = Math.floor(y);\n if (px < 0 || py < 0 || px >= this.#width || py >= this.#height) return null;\n\n writeUniforms(this.#pipeline.uniforms);\n\n const saved = {\n draw: gl.getParameter(gl.DRAW_FRAMEBUFFER_BINDING) as WebGLFramebuffer | null,\n read: gl.getParameter(gl.READ_FRAMEBUFFER_BINDING) as WebGLFramebuffer | null,\n viewport: gl.getParameter(gl.VIEWPORT) as Int32Array,\n blend: gl.isEnabled(gl.BLEND),\n depthTest: gl.isEnabled(gl.DEPTH_TEST),\n scissorTest: gl.isEnabled(gl.SCISSOR_TEST),\n depthMask: gl.getParameter(gl.DEPTH_WRITEMASK) as boolean,\n colorMask: gl.getParameter(gl.COLOR_WRITEMASK) as boolean[],\n depthFunc: gl.getParameter(gl.DEPTH_FUNC) as number,\n };\n\n // Failed or skipped reads must not return the previous hit.\n this.#readback.fill(PICK_EMPTY);\n try {\n gl.bindFramebuffer(gl.FRAMEBUFFER, this.#framebuffer);\n gl.viewport(0, 0, this.#width, this.#height);\n // The clears honour the scissor and the write masks, so set them first.\n gl.disable(gl.SCISSOR_TEST);\n gl.colorMask(true, true, true, true);\n gl.depthMask(true);\n // An integer attachment is not cleared by clearColor: that path is for\n // fixed- and floating-point buffers and leaves an integer target\n // untouched. Clearing to zero instead of PICK_EMPTY would make every\n // empty pixel decode as primitive 0.\n gl.clearBufferuiv(gl.COLOR, 0, EMPTY_COLOR);\n gl.clearBufferfv(gl.DEPTH, 0, FAR_DEPTH);\n gl.enable(gl.DEPTH_TEST);\n gl.depthFunc(gl.LESS);\n // Blending is ignored for an integer attachment, but a caller that left\n // it on for the color pass should not have to know that.\n gl.disable(gl.BLEND);\n\n this.#pipeline.draw();\n\n gl.readPixels(px, py, 1, 1, gl.RGBA_INTEGER, gl.UNSIGNED_INT, this.#readback);\n } finally {\n gl.bindFramebuffer(gl.DRAW_FRAMEBUFFER, saved.draw);\n gl.bindFramebuffer(gl.READ_FRAMEBUFFER, saved.read);\n const [vx, vy, vw, vh] = saved.viewport as unknown as [number, number, number, number];\n gl.viewport(vx, vy, vw, vh);\n const toggle = (cap: number, on: boolean) => (on ? gl.enable(cap) : gl.disable(cap));\n toggle(gl.BLEND, saved.blend);\n toggle(gl.DEPTH_TEST, saved.depthTest);\n toggle(gl.SCISSOR_TEST, saved.scissorTest);\n gl.depthMask(saved.depthMask);\n const [r, g, b, a] = saved.colorMask as [boolean, boolean, boolean, boolean];\n gl.colorMask(r, g, b, a);\n gl.depthFunc(saved.depthFunc);\n }\n\n const hit = decodePick(this.#readback);\n return hit === null ? null : { ...hit, x: px, y: py };\n }\n\n /**\n * A canvas point as {@link pick} wants it: the integer device pixel under\n * the point, counted from the bottom-left.\n *\n * A DOM event gives CSS pixels from the top-left of the element; GL reads\n * device pixels from the bottom-left. Getting this wrong picks a mirrored\n * point, which looks plausible near the middle and wrong everywhere else.\n * The top CSS row maps to row `height - 1`; a point on or past an edge maps\n * outside the target, where {@link pick} returns `null`.\n */\n static fromCanvasPoint(\n canvas: { clientWidth: number; clientHeight: number; width: number; height: number },\n offsetX: number,\n offsetY: number,\n ): { x: number; y: number } {\n const scaleX = canvas.width / Math.max(1, canvas.clientWidth);\n const scaleY = canvas.height / Math.max(1, canvas.clientHeight);\n return {\n x: Math.floor(offsetX * scaleX),\n y: canvas.height - 1 - Math.floor(offsetY * scaleY),\n };\n }\n\n /** Release the target, its depth buffer and the framebuffer. */\n dispose(): void {\n this.#gl.deleteFramebuffer(this.#framebuffer);\n this.#release();\n }\n\n #release(): void {\n const gl = this.#gl;\n if (this.#target !== null) gl.deleteTexture(this.#target);\n if (this.#depth !== null) gl.deleteRenderbuffer(this.#depth);\n this.#target = null;\n this.#depth = null;\n this.#width = 0;\n this.#height = 0;\n }\n}\n","// Copyright Visual Collaboration Technologies Inc. All Rights Reserved.\n// See LICENSE for details.\n\n// __PKG_VERSION__ is replaced with the package.json version at build time by\n// tsup (see tsup.config.ts) and by vitest during tests (see vitest.config.ts).\n// package.json remains the single source of truth; it stays out of the TS\n// program so tsc can emit declarations cleanly (see tsconfig.build.json).\ndeclare const __PKG_VERSION__: string;\n\n/** The package version, injected from package.json at build time. */\nexport const VERSION: string = __PKG_VERSION__;\n"]}
|