@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/dist/index.cjs
CHANGED
|
@@ -2,328 +2,181 @@
|
|
|
2
2
|
|
|
3
3
|
var shaderLibrary = require('@vcollab/shader-library');
|
|
4
4
|
|
|
5
|
-
// src/
|
|
6
|
-
var UNIFORM_BLOCK_NAME = "GlobalParams_std140";
|
|
7
|
-
var UNIFORM_BLOCK_BINDING = 0;
|
|
5
|
+
// src/errors.ts
|
|
8
6
|
var GLPipelineError = class extends Error {
|
|
9
7
|
name = "GLPipelineError";
|
|
10
8
|
};
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
}
|
|
20
|
-
function compile(gl, type, source, stage) {
|
|
21
|
-
const shader = gl.createShader(type);
|
|
22
|
-
if (shader === null) throw new GLPipelineError(`could not create a ${stage} shader`);
|
|
23
|
-
gl.shaderSource(shader, source);
|
|
24
|
-
gl.compileShader(shader);
|
|
25
|
-
if (!gl.getShaderParameter(shader, gl.COMPILE_STATUS)) {
|
|
26
|
-
const log = gl.getShaderInfoLog(shader);
|
|
27
|
-
gl.deleteShader(shader);
|
|
28
|
-
throw new GLPipelineError(`the ${stage} stage did not compile on this driver:
|
|
29
|
-
${log}`);
|
|
30
|
-
}
|
|
31
|
-
return shader;
|
|
32
|
-
}
|
|
33
|
-
function linkProgram(gl, vertex, fragment) {
|
|
34
|
-
const vertexShader = compile(gl, gl.VERTEX_SHADER, vertex, "vertex");
|
|
35
|
-
let fragmentShader;
|
|
36
|
-
try {
|
|
37
|
-
fragmentShader = compile(gl, gl.FRAGMENT_SHADER, fragment, "fragment");
|
|
38
|
-
} catch (error) {
|
|
39
|
-
gl.deleteShader(vertexShader);
|
|
40
|
-
throw error;
|
|
41
|
-
}
|
|
42
|
-
const program = gl.createProgram();
|
|
43
|
-
if (program === null) {
|
|
44
|
-
gl.deleteShader(vertexShader);
|
|
45
|
-
gl.deleteShader(fragmentShader);
|
|
46
|
-
throw new GLPipelineError("could not create a program");
|
|
47
|
-
}
|
|
48
|
-
gl.attachShader(program, vertexShader);
|
|
49
|
-
gl.attachShader(program, fragmentShader);
|
|
50
|
-
gl.linkProgram(program);
|
|
51
|
-
gl.deleteShader(vertexShader);
|
|
52
|
-
gl.deleteShader(fragmentShader);
|
|
53
|
-
if (!gl.getProgramParameter(program, gl.LINK_STATUS)) {
|
|
54
|
-
const log = gl.getProgramInfoLog(program);
|
|
55
|
-
gl.deleteProgram(program);
|
|
56
|
-
throw new GLPipelineError(`the stage pair did not link on this driver:
|
|
57
|
-
${log}`);
|
|
58
|
-
}
|
|
59
|
-
const blockIndex = gl.getUniformBlockIndex(program, UNIFORM_BLOCK_NAME);
|
|
60
|
-
if (blockIndex !== gl.INVALID_INDEX) {
|
|
61
|
-
gl.uniformBlockBinding(program, blockIndex, UNIFORM_BLOCK_BINDING);
|
|
9
|
+
|
|
10
|
+
// src/geometry.ts
|
|
11
|
+
var listeners = /* @__PURE__ */ new WeakMap();
|
|
12
|
+
function onDispose(geometry, release) {
|
|
13
|
+
let set = listeners.get(geometry);
|
|
14
|
+
if (set === void 0) {
|
|
15
|
+
set = /* @__PURE__ */ new Set();
|
|
16
|
+
listeners.set(geometry, set);
|
|
62
17
|
}
|
|
63
|
-
|
|
18
|
+
set.add(release);
|
|
19
|
+
return () => set.delete(release);
|
|
64
20
|
}
|
|
65
|
-
var
|
|
21
|
+
var GLGeometry = class {
|
|
66
22
|
#gl;
|
|
67
|
-
#
|
|
68
|
-
|
|
69
|
-
#
|
|
70
|
-
|
|
71
|
-
#
|
|
72
|
-
|
|
73
|
-
|
|
74
|
-
|
|
75
|
-
/**
|
|
76
|
-
|
|
77
|
-
/** A byte view of `uniforms.buffer`, made once. */
|
|
78
|
-
#uniformBytes;
|
|
79
|
-
#indexCount = 0;
|
|
80
|
-
/** The envelope this was built from. */
|
|
81
|
-
envelope;
|
|
82
|
-
/** Its language block: layout, bindings and sources. */
|
|
83
|
-
block;
|
|
84
|
-
/** Its sampler bindings, by semantic. */
|
|
85
|
-
bindings;
|
|
86
|
-
/**
|
|
87
|
-
* The uniform block to write into. The caller sets whatever the pipeline
|
|
88
|
-
* declares; {@link draw} uploads it.
|
|
89
|
-
*/
|
|
90
|
-
uniforms;
|
|
23
|
+
#attributes = /* @__PURE__ */ new Map();
|
|
24
|
+
/** The buffers this geometry uploaded, and so deletes. */
|
|
25
|
+
#owned = [];
|
|
26
|
+
#indexBuffer;
|
|
27
|
+
#disposed = false;
|
|
28
|
+
#stopSharing = null;
|
|
29
|
+
/** Vertices per attribute: every attribute, its own or shared, holds this many. */
|
|
30
|
+
vertexCount;
|
|
31
|
+
/** How many indices a draw reads. */
|
|
32
|
+
indexCount;
|
|
91
33
|
/**
|
|
92
|
-
*
|
|
93
|
-
*
|
|
94
|
-
*
|
|
95
|
-
*
|
|
34
|
+
* Upload the attributes, and the indices unless shared.
|
|
35
|
+
*
|
|
36
|
+
* @param shares a geometry whose buffers this one reads for every semantic
|
|
37
|
+
* it does not supply itself, and whose indices it draws with. It must
|
|
38
|
+
* outlive this one; disposing it disposes this.
|
|
39
|
+
* @throws {GLPipelineError} if an attribute's `components` is not a positive
|
|
40
|
+
* integer, its data is not a whole number of vertices, two attributes
|
|
41
|
+
* disagree on the vertex count, there are no indices to draw with, the
|
|
42
|
+
* shared geometry is disposed, or a buffer cannot be created. Nothing is
|
|
43
|
+
* left allocated.
|
|
96
44
|
*/
|
|
97
|
-
constructor(gl,
|
|
45
|
+
constructor(gl, source, shares) {
|
|
98
46
|
this.#gl = gl;
|
|
99
|
-
|
|
100
|
-
|
|
101
|
-
const stages = this.block.stages;
|
|
102
|
-
if (stages.vertex === void 0 || stages.fragment === void 0) {
|
|
103
|
-
throw new GLPipelineError(`${envelope.pipeline.id} does not carry both stages`);
|
|
47
|
+
if (shares?.disposed) {
|
|
48
|
+
throw new GLPipelineError("cannot share the buffers of a disposed geometry");
|
|
104
49
|
}
|
|
105
|
-
|
|
106
|
-
|
|
107
|
-
|
|
108
|
-
|
|
109
|
-
|
|
110
|
-
|
|
111
|
-
|
|
112
|
-
|
|
113
|
-
|
|
114
|
-
const size = gl.getActiveUniformBlockParameter(
|
|
115
|
-
program,
|
|
116
|
-
blockIndex,
|
|
117
|
-
gl.UNIFORM_BLOCK_DATA_SIZE
|
|
50
|
+
let vertexCount = shares?.vertexCount;
|
|
51
|
+
for (const [semantic, attribute] of source.attributes) {
|
|
52
|
+
const { data, components } = attribute;
|
|
53
|
+
if (!(data instanceof Float32Array || data instanceof Int32Array)) {
|
|
54
|
+
throw new GLPipelineError(`${semantic}: data must be a Float32Array or an Int32Array`);
|
|
55
|
+
}
|
|
56
|
+
if (!Number.isInteger(components) || components < 1) {
|
|
57
|
+
throw new GLPipelineError(
|
|
58
|
+
`${semantic}: components is ${String(components)}, not a positive integer`
|
|
118
59
|
);
|
|
119
|
-
|
|
120
|
-
|
|
121
|
-
|
|
60
|
+
}
|
|
61
|
+
if (data.length % components !== 0) {
|
|
62
|
+
throw new GLPipelineError(
|
|
63
|
+
`${semantic}: ${data.length} values is not a whole number of ${components}-value vertices`
|
|
122
64
|
);
|
|
123
|
-
if (typeof size !== "number" || size < end) {
|
|
124
|
-
throw new GLPipelineError(
|
|
125
|
-
`${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}`
|
|
126
|
-
);
|
|
127
|
-
}
|
|
128
|
-
bufferSize = Math.max(bufferSize, size);
|
|
129
65
|
}
|
|
130
|
-
|
|
131
|
-
if (
|
|
132
|
-
|
|
133
|
-
|
|
134
|
-
|
|
135
|
-
if (vao === null) throw new GLPipelineError("could not create a vertex array");
|
|
136
|
-
this.#samplerLocations = this.bindings.all.map(
|
|
137
|
-
(record) => gl.getUniformLocation(program, record.uniform)
|
|
138
|
-
);
|
|
139
|
-
this.#program = program;
|
|
140
|
-
this.#ubo = ubo;
|
|
141
|
-
this.#vao = vao;
|
|
142
|
-
} catch (error) {
|
|
143
|
-
if (ubo !== null) gl.deleteBuffer(ubo);
|
|
144
|
-
gl.deleteProgram(program);
|
|
145
|
-
throw error;
|
|
146
|
-
}
|
|
147
|
-
}
|
|
148
|
-
/** The linked program, for a caller that needs it directly. */
|
|
149
|
-
get program() {
|
|
150
|
-
return this.#program;
|
|
151
|
-
}
|
|
152
|
-
/**
|
|
153
|
-
* Upload the geometry, matching the contract's attributes by semantic.
|
|
154
|
-
*
|
|
155
|
-
* An attribute the caller cannot supply is left as a **disabled array with a
|
|
156
|
-
* constant**, which is what a viewer does for data a draw does not have —
|
|
157
|
-
* the same "off state is data" the always-present features rely on. The
|
|
158
|
-
* constant is context state rather than vertex-array state, so {@link draw}
|
|
159
|
-
* sets it again each time. The contract's declared width wins over the
|
|
160
|
-
* source's, so a `vec3` read from a wider field takes the first three values
|
|
161
|
-
* of each vertex.
|
|
162
|
-
*
|
|
163
|
-
* Calling it again replaces the previous geometry and frees its buffers.
|
|
164
|
-
*
|
|
165
|
-
* @throws {GLPipelineError} if a source's data type or `integer` flag does
|
|
166
|
-
* not match the declared type, its `components` is narrower than the
|
|
167
|
-
* declared width, or a buffer cannot be created. A refused source changes
|
|
168
|
-
* nothing; when buffer creation fails the pipeline is left with no geometry.
|
|
169
|
-
*/
|
|
170
|
-
setGeometry(geometry) {
|
|
171
|
-
const gl = this.#gl;
|
|
172
|
-
const plan = this.envelope.pipeline.inputs.map((input) => {
|
|
173
|
-
const shape = attributeShape(input);
|
|
174
|
-
const source = geometry.attributes.get(input.semantic);
|
|
175
|
-
if (source !== void 0) {
|
|
176
|
-
const label = `${input.semantic} (${input.name}, ${input.type})`;
|
|
177
|
-
if (source.integer !== shape.integer) {
|
|
178
|
-
throw new GLPipelineError(
|
|
179
|
-
`${label} is ${shape.integer ? "an integer" : "a float"} attribute; the source says integer: ${source.integer}`
|
|
180
|
-
);
|
|
181
|
-
}
|
|
182
|
-
const wanted = shape.integer ? Int32Array : Float32Array;
|
|
183
|
-
if (!(source.data instanceof wanted)) {
|
|
184
|
-
throw new GLPipelineError(`${label} needs ${wanted.name} data`);
|
|
185
|
-
}
|
|
186
|
-
if (!Number.isInteger(source.components) || source.components < shape.width) {
|
|
187
|
-
throw new GLPipelineError(
|
|
188
|
-
`${label} reads ${shape.width} values per vertex; the source carries ${source.components}`
|
|
189
|
-
);
|
|
190
|
-
}
|
|
66
|
+
const count = data.length / components;
|
|
67
|
+
if (vertexCount !== void 0 && count !== vertexCount) {
|
|
68
|
+
throw new GLPipelineError(
|
|
69
|
+
`${semantic} holds ${count} vertices; the geometry has ${vertexCount}`
|
|
70
|
+
);
|
|
191
71
|
}
|
|
192
|
-
|
|
193
|
-
}
|
|
194
|
-
|
|
195
|
-
|
|
196
|
-
|
|
197
|
-
|
|
198
|
-
|
|
199
|
-
|
|
200
|
-
|
|
72
|
+
vertexCount = count;
|
|
73
|
+
}
|
|
74
|
+
const indices = source.indices;
|
|
75
|
+
if (indices === void 0 && shares === void 0) {
|
|
76
|
+
throw new GLPipelineError("a geometry needs indices, or another geometry to share");
|
|
77
|
+
}
|
|
78
|
+
this.vertexCount = vertexCount ?? 0;
|
|
79
|
+
this.indexCount = indices?.length ?? shares.indexCount;
|
|
80
|
+
const owned = [];
|
|
81
|
+
const savedVertexArray = gl.getParameter(
|
|
82
|
+
gl.VERTEX_ARRAY_BINDING
|
|
83
|
+
);
|
|
84
|
+
const savedArrayBuffer = gl.getParameter(gl.ARRAY_BUFFER_BINDING);
|
|
201
85
|
try {
|
|
202
|
-
for (const
|
|
203
|
-
if (location < 0) continue;
|
|
204
|
-
if (source === void 0) {
|
|
205
|
-
gl.disableVertexAttribArray(location);
|
|
206
|
-
absent.push({ location, integer: shape.integer });
|
|
207
|
-
continue;
|
|
208
|
-
}
|
|
86
|
+
for (const [semantic, attribute] of source.attributes) {
|
|
209
87
|
const buffer = gl.createBuffer();
|
|
210
88
|
if (buffer === null)
|
|
211
|
-
throw new GLPipelineError(`could not create a buffer for ${
|
|
212
|
-
|
|
89
|
+
throw new GLPipelineError(`could not create a buffer for ${semantic}`);
|
|
90
|
+
owned.push(buffer);
|
|
213
91
|
gl.bindBuffer(gl.ARRAY_BUFFER, buffer);
|
|
214
|
-
gl.bufferData(gl.ARRAY_BUFFER,
|
|
215
|
-
|
|
216
|
-
|
|
217
|
-
|
|
218
|
-
|
|
92
|
+
gl.bufferData(gl.ARRAY_BUFFER, attribute.data, gl.STATIC_DRAW);
|
|
93
|
+
this.#attributes.set(semantic, {
|
|
94
|
+
buffer,
|
|
95
|
+
components: attribute.components,
|
|
96
|
+
integer: attribute.integer,
|
|
97
|
+
type: attribute.data instanceof Int32Array ? "int" : "float"
|
|
98
|
+
});
|
|
99
|
+
}
|
|
100
|
+
if (indices !== void 0) {
|
|
101
|
+
const buffer = gl.createBuffer();
|
|
102
|
+
if (buffer === null) throw new GLPipelineError("could not create an index buffer");
|
|
103
|
+
owned.push(buffer);
|
|
104
|
+
gl.bindVertexArray(null);
|
|
105
|
+
gl.bindBuffer(gl.ELEMENT_ARRAY_BUFFER, buffer);
|
|
106
|
+
gl.bufferData(gl.ELEMENT_ARRAY_BUFFER, indices, gl.STATIC_DRAW);
|
|
107
|
+
gl.bindBuffer(gl.ELEMENT_ARRAY_BUFFER, null);
|
|
108
|
+
this.#indexBuffer = buffer;
|
|
109
|
+
} else {
|
|
110
|
+
this.#indexBuffer = null;
|
|
219
111
|
}
|
|
220
|
-
const elements = gl.createBuffer();
|
|
221
|
-
if (elements === null) throw new GLPipelineError("could not create an index buffer");
|
|
222
|
-
buffers.push(elements);
|
|
223
|
-
gl.bindBuffer(gl.ELEMENT_ARRAY_BUFFER, elements);
|
|
224
|
-
gl.bufferData(gl.ELEMENT_ARRAY_BUFFER, geometry.indices, gl.STATIC_DRAW);
|
|
225
112
|
} catch (error) {
|
|
226
|
-
for (const buffer of
|
|
113
|
+
for (const buffer of owned) gl.deleteBuffer(buffer);
|
|
227
114
|
throw error;
|
|
228
115
|
} finally {
|
|
229
|
-
gl.
|
|
116
|
+
gl.bindBuffer(gl.ARRAY_BUFFER, savedArrayBuffer);
|
|
117
|
+
gl.bindVertexArray(savedVertexArray);
|
|
118
|
+
}
|
|
119
|
+
this.#owned = owned;
|
|
120
|
+
if (shares !== void 0) {
|
|
121
|
+
for (const semantic of shares.semantics) {
|
|
122
|
+
if (!this.#attributes.has(semantic)) {
|
|
123
|
+
this.#attributes.set(semantic, shares.attribute(semantic));
|
|
124
|
+
}
|
|
125
|
+
}
|
|
126
|
+
if (this.#indexBuffer === null) this.#indexBuffer = shares.indexBuffer;
|
|
127
|
+
this.#stopSharing = onDispose(shares, () => this.dispose());
|
|
230
128
|
}
|
|
231
|
-
this.#buffers = buffers;
|
|
232
|
-
this.#absent = absent;
|
|
233
|
-
this.#indexCount = geometry.indices.length;
|
|
234
129
|
}
|
|
235
|
-
/**
|
|
236
|
-
|
|
237
|
-
|
|
238
|
-
* By semantic, because the name a table or texture is declared under never
|
|
239
|
-
* reaches the linked program: GLSL ES 3.00 has no binding qualifier for
|
|
240
|
-
* samplers, so the cross-compiler pairs each texture with a sampler and names
|
|
241
|
-
* the combination itself. The envelope's record is the only route across.
|
|
242
|
-
*
|
|
243
|
-
* An integer table (`usampler2D`) must use `NEAREST` filtering and no
|
|
244
|
-
* mipmaps, or the texture is incomplete and every fetch returns zero.
|
|
245
|
-
*/
|
|
246
|
-
setTexture(semantic, texture) {
|
|
247
|
-
if (texture === null) this.#textures.delete(semantic);
|
|
248
|
-
else this.#textures.set(semantic, texture);
|
|
130
|
+
/** Every semantic this geometry can feed, its own and shared. */
|
|
131
|
+
get semantics() {
|
|
132
|
+
return [...this.#attributes.keys()];
|
|
249
133
|
}
|
|
250
|
-
/**
|
|
251
|
-
|
|
252
|
-
return this.
|
|
134
|
+
/** One semantic's buffer and layout, or undefined where it has none. */
|
|
135
|
+
attribute(semantic) {
|
|
136
|
+
return this.#attributes.get(semantic);
|
|
137
|
+
}
|
|
138
|
+
/** The index buffer a draw reads, its own or shared. */
|
|
139
|
+
get indexBuffer() {
|
|
140
|
+
return this.#indexBuffer;
|
|
141
|
+
}
|
|
142
|
+
/** Whether {@link dispose} has been called, here or on a geometry this shares. */
|
|
143
|
+
get disposed() {
|
|
144
|
+
return this.#disposed;
|
|
253
145
|
}
|
|
254
146
|
/**
|
|
255
|
-
*
|
|
256
|
-
*
|
|
257
|
-
*
|
|
258
|
-
* unit and the texture bound on each of the pipeline's units changed.
|
|
259
|
-
*
|
|
260
|
-
* @throws {GLPipelineError} if any semantic in {@link requiredSemantics} has
|
|
261
|
-
* no texture; the message names every one missing.
|
|
147
|
+
* Delete the buffers this geometry uploaded, and tell every pipeline that
|
|
148
|
+
* drew it, so each drops the vertex array it built here. Geometries sharing
|
|
149
|
+
* this one are disposed too. Calling it again does nothing.
|
|
262
150
|
*/
|
|
263
|
-
draw() {
|
|
264
|
-
const gl = this.#gl;
|
|
265
|
-
if (this.#indexCount === 0) return;
|
|
266
|
-
const missing = this.requiredSemantics.filter((semantic) => !this.#textures.has(semantic));
|
|
267
|
-
if (missing.length > 0) {
|
|
268
|
-
throw new GLPipelineError(
|
|
269
|
-
`${this.envelope.pipeline.id} has no texture for: ${missing.join(", ")}`
|
|
270
|
-
);
|
|
271
|
-
}
|
|
272
|
-
gl.useProgram(this.#program);
|
|
273
|
-
gl.bindBuffer(gl.UNIFORM_BUFFER, this.#ubo);
|
|
274
|
-
gl.bufferSubData(gl.UNIFORM_BUFFER, 0, this.#uniformBytes);
|
|
275
|
-
gl.bindBufferBase(gl.UNIFORM_BUFFER, UNIFORM_BLOCK_BINDING, this.#ubo);
|
|
276
|
-
this.bindings.all.forEach((record, index) => {
|
|
277
|
-
gl.activeTexture(gl.TEXTURE0 + record.unit);
|
|
278
|
-
gl.bindTexture(gl.TEXTURE_2D, this.#textures.get(record.semantic));
|
|
279
|
-
const location = this.#samplerLocations[index];
|
|
280
|
-
if (location !== null) gl.uniform1i(location, record.unit);
|
|
281
|
-
});
|
|
282
|
-
for (const { location, integer } of this.#absent) {
|
|
283
|
-
if (integer) gl.vertexAttribI4i(location, 0, 0, 0, 0);
|
|
284
|
-
else gl.vertexAttrib4f(location, 0, 0, 0, 0);
|
|
285
|
-
}
|
|
286
|
-
gl.bindVertexArray(this.#vao);
|
|
287
|
-
gl.drawElements(gl.TRIANGLES, this.#indexCount, gl.UNSIGNED_INT, 0);
|
|
288
|
-
gl.bindVertexArray(null);
|
|
289
|
-
}
|
|
290
|
-
/** Release the GL objects this owns. Textures given to it are the caller's. */
|
|
291
151
|
dispose() {
|
|
292
|
-
|
|
293
|
-
|
|
294
|
-
|
|
295
|
-
|
|
296
|
-
|
|
297
|
-
this
|
|
298
|
-
|
|
299
|
-
this.#
|
|
300
|
-
this.#
|
|
152
|
+
if (this.#disposed) return;
|
|
153
|
+
this.#disposed = true;
|
|
154
|
+
this.#stopSharing?.();
|
|
155
|
+
this.#stopSharing = null;
|
|
156
|
+
const waiting = listeners.get(this);
|
|
157
|
+
listeners.delete(this);
|
|
158
|
+
for (const release of waiting ?? []) release();
|
|
159
|
+
for (const buffer of this.#owned) this.#gl.deleteBuffer(buffer);
|
|
160
|
+
this.#owned = [];
|
|
161
|
+
this.#attributes.clear();
|
|
301
162
|
}
|
|
302
163
|
};
|
|
303
|
-
|
|
304
|
-
// src/pick.ts
|
|
305
164
|
var EMPTY_COLOR = new Uint32Array([shaderLibrary.PICK_EMPTY, shaderLibrary.PICK_EMPTY, shaderLibrary.PICK_EMPTY, shaderLibrary.PICK_EMPTY]);
|
|
306
165
|
var FAR_DEPTH = new Float32Array([1]);
|
|
307
166
|
var PickPass = class {
|
|
308
167
|
#gl;
|
|
309
|
-
#pipeline;
|
|
310
168
|
#framebuffer;
|
|
311
169
|
#target = null;
|
|
312
170
|
#depth = null;
|
|
313
171
|
#width = 0;
|
|
314
172
|
#height = 0;
|
|
315
173
|
#readback = new Uint32Array(4);
|
|
316
|
-
constructor(gl
|
|
174
|
+
constructor(gl) {
|
|
317
175
|
this.#gl = gl;
|
|
318
|
-
this.#pipeline = pipeline;
|
|
319
176
|
const framebuffer = gl.createFramebuffer();
|
|
320
177
|
if (framebuffer === null) throw new GLPipelineError("could not create a pick framebuffer");
|
|
321
178
|
this.#framebuffer = framebuffer;
|
|
322
179
|
}
|
|
323
|
-
/** The pipeline drawing into the target, for setting its geometry and textures. */
|
|
324
|
-
get pipeline() {
|
|
325
|
-
return this.#pipeline;
|
|
326
|
-
}
|
|
327
180
|
/**
|
|
328
181
|
* Match the target to the viewport.
|
|
329
182
|
*
|
|
@@ -394,33 +247,41 @@ var PickPass = class {
|
|
|
394
247
|
}
|
|
395
248
|
}
|
|
396
249
|
/**
|
|
397
|
-
* Draw the pick pass and read one pixel.
|
|
250
|
+
* Draw the scene's pick pass and read one pixel.
|
|
251
|
+
*
|
|
252
|
+
* The target is cleared, then `drawScene` is called once with a {@link PickDraw}
|
|
253
|
+
* and issues every draw the pick should see: for each occurrence, set its
|
|
254
|
+
* pick pipeline's uniforms and textures as the color pass had them, then
|
|
255
|
+
* `draw(pipeline, geometry)`. Nearest wins, as on screen.
|
|
398
256
|
*
|
|
399
|
-
*
|
|
400
|
-
*
|
|
401
|
-
*
|
|
402
|
-
*
|
|
403
|
-
*
|
|
404
|
-
*
|
|
257
|
+
* Only the pixel asked for is drawn: after the clear the scissor is set to
|
|
258
|
+
* that one pixel, so a scene of thousands of draws shades one fragment each.
|
|
259
|
+
* The pass sets the state it depends on — every color channel and depth
|
|
260
|
+
* writable, `LESS` depth testing, no blending — and afterwards puts back
|
|
261
|
+
* each of those, the scissor test and box, the viewport and the caller's
|
|
262
|
+
* framebuffers, even if a draw throws. Face culling is left as the caller set
|
|
263
|
+
* it, so the pick honors winding exactly as the color pass does. The draws
|
|
264
|
+
* also change the current program and texture bindings (see
|
|
405
265
|
* {@link GLPipeline.draw}).
|
|
406
266
|
*
|
|
407
267
|
* @param x horizontal position in framebuffer pixels, from the left.
|
|
408
268
|
* @param y vertical position in framebuffer pixels, from the **bottom** —
|
|
409
269
|
* GL's origin, not the DOM's. {@link fromCanvasPoint} converts.
|
|
410
|
-
* @param
|
|
411
|
-
*
|
|
412
|
-
*
|
|
413
|
-
* @throws {GLPipelineError} before {@link resize} has been called,
|
|
414
|
-
* pipeline
|
|
270
|
+
* @param drawScene issues the draws, through the `draw` it is given.
|
|
271
|
+
* @returns the hit, or `null` where nothing was drawn or the point is
|
|
272
|
+
* outside — in which case `drawScene` is not called.
|
|
273
|
+
* @throws {GLPipelineError} before {@link resize} has been called, if a draw
|
|
274
|
+
* names a pipeline that is not a pick pipeline (one writing a color into
|
|
275
|
+
* this integer target would read back as garbage), or if a pipeline
|
|
276
|
+
* refuses to draw. Anything `drawScene` throws is rethrown.
|
|
415
277
|
*/
|
|
416
|
-
pick(x, y,
|
|
278
|
+
pick(x, y, drawScene) {
|
|
417
279
|
const gl = this.#gl;
|
|
418
280
|
if (this.#target === null)
|
|
419
281
|
throw new GLPipelineError("the pick pass has no target; call resize first");
|
|
420
282
|
const px = Math.floor(x);
|
|
421
283
|
const py = Math.floor(y);
|
|
422
284
|
if (px < 0 || py < 0 || px >= this.#width || py >= this.#height) return null;
|
|
423
|
-
writeUniforms(this.#pipeline.uniforms);
|
|
424
285
|
const saved = {
|
|
425
286
|
draw: gl.getParameter(gl.DRAW_FRAMEBUFFER_BINDING),
|
|
426
287
|
read: gl.getParameter(gl.READ_FRAMEBUFFER_BINDING),
|
|
@@ -428,6 +289,7 @@ var PickPass = class {
|
|
|
428
289
|
blend: gl.isEnabled(gl.BLEND),
|
|
429
290
|
depthTest: gl.isEnabled(gl.DEPTH_TEST),
|
|
430
291
|
scissorTest: gl.isEnabled(gl.SCISSOR_TEST),
|
|
292
|
+
scissorBox: gl.getParameter(gl.SCISSOR_BOX),
|
|
431
293
|
depthMask: gl.getParameter(gl.DEPTH_WRITEMASK),
|
|
432
294
|
colorMask: gl.getParameter(gl.COLOR_WRITEMASK),
|
|
433
295
|
depthFunc: gl.getParameter(gl.DEPTH_FUNC)
|
|
@@ -444,7 +306,16 @@ var PickPass = class {
|
|
|
444
306
|
gl.enable(gl.DEPTH_TEST);
|
|
445
307
|
gl.depthFunc(gl.LESS);
|
|
446
308
|
gl.disable(gl.BLEND);
|
|
447
|
-
|
|
309
|
+
gl.enable(gl.SCISSOR_TEST);
|
|
310
|
+
gl.scissor(px, py, 1, 1);
|
|
311
|
+
drawScene((pipeline, geometry) => {
|
|
312
|
+
if (!pipeline.envelope.pipeline.capabilities.includes(shaderLibrary.PICK_CAPABILITY)) {
|
|
313
|
+
throw new GLPipelineError(
|
|
314
|
+
`${pipeline.envelope.pipeline.id} is not a pick pipeline; the pick target holds identifiers, and a color written into it reads back as garbage`
|
|
315
|
+
);
|
|
316
|
+
}
|
|
317
|
+
pipeline.draw(geometry);
|
|
318
|
+
});
|
|
448
319
|
gl.readPixels(px, py, 1, 1, gl.RGBA_INTEGER, gl.UNSIGNED_INT, this.#readback);
|
|
449
320
|
} finally {
|
|
450
321
|
gl.bindFramebuffer(gl.DRAW_FRAMEBUFFER, saved.draw);
|
|
@@ -455,6 +326,8 @@ var PickPass = class {
|
|
|
455
326
|
toggle(gl.BLEND, saved.blend);
|
|
456
327
|
toggle(gl.DEPTH_TEST, saved.depthTest);
|
|
457
328
|
toggle(gl.SCISSOR_TEST, saved.scissorTest);
|
|
329
|
+
const [sx, sy, sw, sh] = saved.scissorBox;
|
|
330
|
+
gl.scissor(sx, sy, sw, sh);
|
|
458
331
|
gl.depthMask(saved.depthMask);
|
|
459
332
|
const [r, g, b, a] = saved.colorMask;
|
|
460
333
|
gl.colorMask(r, g, b, a);
|
|
@@ -496,10 +369,333 @@ var PickPass = class {
|
|
|
496
369
|
this.#height = 0;
|
|
497
370
|
}
|
|
498
371
|
};
|
|
372
|
+
var UNIFORM_BLOCK_NAME = "GlobalParams_std140";
|
|
373
|
+
var UNIFORM_BLOCK_BINDING = 0;
|
|
374
|
+
function attributeShape(input) {
|
|
375
|
+
const match = /^(i?)vec([234])$/.exec(input.type);
|
|
376
|
+
if (match !== null) return { integer: match[1] === "i", width: Number(match[2]) };
|
|
377
|
+
if (input.type === "float") return { integer: false, width: 1 };
|
|
378
|
+
if (input.type === "int") return { integer: true, width: 1 };
|
|
379
|
+
throw new GLPipelineError(
|
|
380
|
+
`${input.name} is declared ${input.type}; only float, vecN, int and ivecN attributes are supported`
|
|
381
|
+
);
|
|
382
|
+
}
|
|
383
|
+
function compile(gl, type, source, stage) {
|
|
384
|
+
const shader = gl.createShader(type);
|
|
385
|
+
if (shader === null) throw new GLPipelineError(`could not create a ${stage} shader`);
|
|
386
|
+
gl.shaderSource(shader, source);
|
|
387
|
+
gl.compileShader(shader);
|
|
388
|
+
if (!gl.getShaderParameter(shader, gl.COMPILE_STATUS)) {
|
|
389
|
+
const log = gl.getShaderInfoLog(shader);
|
|
390
|
+
gl.deleteShader(shader);
|
|
391
|
+
throw new GLPipelineError(`the ${stage} stage did not compile on this driver:
|
|
392
|
+
${log}`);
|
|
393
|
+
}
|
|
394
|
+
return shader;
|
|
395
|
+
}
|
|
396
|
+
function linkProgram(gl, vertex, fragment) {
|
|
397
|
+
const vertexShader = compile(gl, gl.VERTEX_SHADER, vertex, "vertex");
|
|
398
|
+
let fragmentShader;
|
|
399
|
+
try {
|
|
400
|
+
fragmentShader = compile(gl, gl.FRAGMENT_SHADER, fragment, "fragment");
|
|
401
|
+
} catch (error) {
|
|
402
|
+
gl.deleteShader(vertexShader);
|
|
403
|
+
throw error;
|
|
404
|
+
}
|
|
405
|
+
const program = gl.createProgram();
|
|
406
|
+
if (program === null) {
|
|
407
|
+
gl.deleteShader(vertexShader);
|
|
408
|
+
gl.deleteShader(fragmentShader);
|
|
409
|
+
throw new GLPipelineError("could not create a program");
|
|
410
|
+
}
|
|
411
|
+
gl.attachShader(program, vertexShader);
|
|
412
|
+
gl.attachShader(program, fragmentShader);
|
|
413
|
+
gl.linkProgram(program);
|
|
414
|
+
gl.deleteShader(vertexShader);
|
|
415
|
+
gl.deleteShader(fragmentShader);
|
|
416
|
+
if (!gl.getProgramParameter(program, gl.LINK_STATUS)) {
|
|
417
|
+
const log = gl.getProgramInfoLog(program);
|
|
418
|
+
gl.deleteProgram(program);
|
|
419
|
+
throw new GLPipelineError(`the stage pair did not link on this driver:
|
|
420
|
+
${log}`);
|
|
421
|
+
}
|
|
422
|
+
const blockIndex = gl.getUniformBlockIndex(program, UNIFORM_BLOCK_NAME);
|
|
423
|
+
if (blockIndex !== gl.INVALID_INDEX) {
|
|
424
|
+
gl.uniformBlockBinding(program, blockIndex, UNIFORM_BLOCK_BINDING);
|
|
425
|
+
}
|
|
426
|
+
return program;
|
|
427
|
+
}
|
|
428
|
+
var GLPipeline = class {
|
|
429
|
+
#gl;
|
|
430
|
+
#program;
|
|
431
|
+
#ubo;
|
|
432
|
+
/** The vertex array built over each geometry this has drawn. */
|
|
433
|
+
#bindings = /* @__PURE__ */ new Map();
|
|
434
|
+
/** Each contract input's shape and its location in this program, read once. */
|
|
435
|
+
#inputs;
|
|
436
|
+
#textures = /* @__PURE__ */ new Map();
|
|
437
|
+
/** Each binding's sampler uniform location, in `bindings.all` order. */
|
|
438
|
+
#samplerLocations;
|
|
439
|
+
/** A byte view of `uniforms.buffer`, made once. */
|
|
440
|
+
#uniformBytes;
|
|
441
|
+
#disposed = false;
|
|
442
|
+
/** The envelope this was built from. */
|
|
443
|
+
envelope;
|
|
444
|
+
/** Its language block: layout, bindings and sources. */
|
|
445
|
+
block;
|
|
446
|
+
/** Its sampler bindings, by semantic. */
|
|
447
|
+
bindings;
|
|
448
|
+
/**
|
|
449
|
+
* The uniform block to write into. The caller sets whatever the pipeline
|
|
450
|
+
* declares before each draw that needs different values; {@link draw}
|
|
451
|
+
* uploads it.
|
|
452
|
+
*/
|
|
453
|
+
uniforms;
|
|
454
|
+
/**
|
|
455
|
+
* @throws {GLPipelineError} if the envelope lacks a stage, declares an
|
|
456
|
+
* attribute type this cannot feed, the driver rejects the text, a GL
|
|
457
|
+
* object cannot be created, or the driver sizes the uniform block too
|
|
458
|
+
* small for the layout the build reflected. Anything created before the
|
|
459
|
+
* failure is freed.
|
|
460
|
+
*/
|
|
461
|
+
constructor(gl, envelope, language = "glsl") {
|
|
462
|
+
this.#gl = gl;
|
|
463
|
+
this.envelope = envelope;
|
|
464
|
+
this.block = shaderLibrary.ShaderSet.language(envelope, language);
|
|
465
|
+
const stages = this.block.stages;
|
|
466
|
+
if (stages.vertex === void 0 || stages.fragment === void 0) {
|
|
467
|
+
throw new GLPipelineError(`${envelope.pipeline.id} does not carry both stages`);
|
|
468
|
+
}
|
|
469
|
+
const shapes = envelope.pipeline.inputs.map((input) => ({
|
|
470
|
+
input,
|
|
471
|
+
shape: attributeShape(input)
|
|
472
|
+
}));
|
|
473
|
+
const program = linkProgram(gl, stages.vertex.source, stages.fragment.source);
|
|
474
|
+
try {
|
|
475
|
+
this.bindings = new shaderLibrary.ResourceBindings(this.block);
|
|
476
|
+
this.uniforms = new shaderLibrary.UniformBlock(this.block);
|
|
477
|
+
this.#uniformBytes = new Uint8Array(this.uniforms.buffer);
|
|
478
|
+
let bufferSize = this.block.blockSize;
|
|
479
|
+
const blockIndex = gl.getUniformBlockIndex(program, UNIFORM_BLOCK_NAME);
|
|
480
|
+
if (blockIndex !== gl.INVALID_INDEX) {
|
|
481
|
+
const size = gl.getActiveUniformBlockParameter(
|
|
482
|
+
program,
|
|
483
|
+
blockIndex,
|
|
484
|
+
gl.UNIFORM_BLOCK_DATA_SIZE
|
|
485
|
+
);
|
|
486
|
+
const end = Math.max(
|
|
487
|
+
0,
|
|
488
|
+
...Object.values(this.block.layout).map((member) => member.offset + member.size)
|
|
489
|
+
);
|
|
490
|
+
if (typeof size !== "number" || size < end) {
|
|
491
|
+
throw new GLPipelineError(
|
|
492
|
+
`${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}`
|
|
493
|
+
);
|
|
494
|
+
}
|
|
495
|
+
bufferSize = Math.max(bufferSize, size);
|
|
496
|
+
}
|
|
497
|
+
this.#samplerLocations = this.bindings.all.map(
|
|
498
|
+
(record) => gl.getUniformLocation(program, record.uniform)
|
|
499
|
+
);
|
|
500
|
+
this.#inputs = shapes.map(({ input, shape }) => ({
|
|
501
|
+
input,
|
|
502
|
+
shape,
|
|
503
|
+
location: gl.getAttribLocation(program, input.name)
|
|
504
|
+
}));
|
|
505
|
+
const ubo = gl.createBuffer();
|
|
506
|
+
if (ubo === null) throw new GLPipelineError("could not create a uniform buffer");
|
|
507
|
+
gl.bindBuffer(gl.UNIFORM_BUFFER, ubo);
|
|
508
|
+
gl.bufferData(gl.UNIFORM_BUFFER, bufferSize, gl.DYNAMIC_DRAW);
|
|
509
|
+
this.#program = program;
|
|
510
|
+
this.#ubo = ubo;
|
|
511
|
+
} catch (error) {
|
|
512
|
+
gl.deleteProgram(program);
|
|
513
|
+
throw error;
|
|
514
|
+
}
|
|
515
|
+
}
|
|
516
|
+
/** The linked program, for a caller that needs it directly. */
|
|
517
|
+
get program() {
|
|
518
|
+
return this.#program;
|
|
519
|
+
}
|
|
520
|
+
/**
|
|
521
|
+
* Give a semantic its texture.
|
|
522
|
+
*
|
|
523
|
+
* By semantic, because the name a table or texture is declared under never
|
|
524
|
+
* reaches the linked program: GLSL ES 3.00 has no binding qualifier for
|
|
525
|
+
* samplers, so the cross-compiler pairs each texture with a sampler and names
|
|
526
|
+
* the combination itself. The envelope's record is the only route across.
|
|
527
|
+
*
|
|
528
|
+
* The texture is used by every draw until it is replaced, so a caller giving
|
|
529
|
+
* each occurrence a table of its own sets it before that occurrence's draw.
|
|
530
|
+
* An integer table (`usampler2D`) must use `NEAREST` filtering and no
|
|
531
|
+
* mipmaps, or the texture is incomplete and every fetch returns zero.
|
|
532
|
+
*/
|
|
533
|
+
setTexture(semantic, texture) {
|
|
534
|
+
if (texture === null) this.#textures.delete(semantic);
|
|
535
|
+
else this.#textures.set(semantic, texture);
|
|
536
|
+
}
|
|
537
|
+
/** Every semantic this pipeline expects a texture for. */
|
|
538
|
+
get requiredSemantics() {
|
|
539
|
+
return this.bindings.semantics;
|
|
540
|
+
}
|
|
541
|
+
/**
|
|
542
|
+
* Check a geometry against this pipeline's contract and build the vertex
|
|
543
|
+
* array that reads it, without drawing.
|
|
544
|
+
*
|
|
545
|
+
* {@link draw} does this on first use; calling it ahead moves a refusal to
|
|
546
|
+
* where the geometry is made rather than the first frame. An attribute the
|
|
547
|
+
* geometry does not supply is left as a **disabled array with a constant**,
|
|
548
|
+
* which is what a viewer does for data a draw does not have — the same "off
|
|
549
|
+
* state is data" the always-present features rely on. The contract's
|
|
550
|
+
* declared width wins over the source's, so a `vec3` read from a wider field
|
|
551
|
+
* takes the first three values of each vertex.
|
|
552
|
+
*
|
|
553
|
+
* @throws {GLPipelineError} if the geometry or this pipeline is disposed, a
|
|
554
|
+
* source's element type or `integer` flag does not match the declared
|
|
555
|
+
* type, its `components` is narrower than the declared width, or the
|
|
556
|
+
* vertex array cannot be created. Nothing is kept for the geometry then.
|
|
557
|
+
*/
|
|
558
|
+
prepare(geometry) {
|
|
559
|
+
this.#binding(geometry);
|
|
560
|
+
}
|
|
561
|
+
/**
|
|
562
|
+
* Forget the vertex array built over one geometry, as when it will not be
|
|
563
|
+
* drawn by this pipeline again. Disposing the geometry does this for every
|
|
564
|
+
* pipeline that drew it.
|
|
565
|
+
*/
|
|
566
|
+
release(geometry) {
|
|
567
|
+
const binding = this.#bindings.get(geometry);
|
|
568
|
+
if (binding === void 0) return;
|
|
569
|
+
this.#bindings.delete(geometry);
|
|
570
|
+
binding.forget();
|
|
571
|
+
this.#gl.deleteVertexArray(binding.vao);
|
|
572
|
+
}
|
|
573
|
+
/**
|
|
574
|
+
* Draw a geometry, after uploading the uniform block and binding every
|
|
575
|
+
* resource.
|
|
576
|
+
*
|
|
577
|
+
* The uniforms and textures are whatever was last set, so drawing one
|
|
578
|
+
* geometry at several places — the occurrences of one mesh — is a loop of
|
|
579
|
+
* "set this occurrence's values, draw". A geometry with no indices draws
|
|
580
|
+
* nothing. Leaves the current program, the active texture unit, the texture
|
|
581
|
+
* bound on each of the pipeline's units and the array buffer binding
|
|
582
|
+
* changed, and no vertex array bound.
|
|
583
|
+
*
|
|
584
|
+
* @throws {GLPipelineError} if any semantic in {@link requiredSemantics} has
|
|
585
|
+
* no texture (the message names every one missing), or for anything
|
|
586
|
+
* {@link prepare} refuses.
|
|
587
|
+
*/
|
|
588
|
+
draw(geometry) {
|
|
589
|
+
const gl = this.#gl;
|
|
590
|
+
if (this.#disposed) throw new GLPipelineError(`${this.envelope.pipeline.id} is disposed`);
|
|
591
|
+
const missing = this.requiredSemantics.filter((semantic) => !this.#textures.has(semantic));
|
|
592
|
+
if (missing.length > 0) {
|
|
593
|
+
throw new GLPipelineError(
|
|
594
|
+
`${this.envelope.pipeline.id} has no texture for: ${missing.join(", ")}`
|
|
595
|
+
);
|
|
596
|
+
}
|
|
597
|
+
const binding = this.#binding(geometry);
|
|
598
|
+
if (geometry.indexCount === 0) return;
|
|
599
|
+
gl.useProgram(this.#program);
|
|
600
|
+
gl.bindBuffer(gl.UNIFORM_BUFFER, this.#ubo);
|
|
601
|
+
gl.bufferSubData(gl.UNIFORM_BUFFER, 0, this.#uniformBytes);
|
|
602
|
+
gl.bindBufferBase(gl.UNIFORM_BUFFER, UNIFORM_BLOCK_BINDING, this.#ubo);
|
|
603
|
+
this.bindings.all.forEach((record, index) => {
|
|
604
|
+
gl.activeTexture(gl.TEXTURE0 + record.unit);
|
|
605
|
+
gl.bindTexture(gl.TEXTURE_2D, this.#textures.get(record.semantic));
|
|
606
|
+
const location = this.#samplerLocations[index];
|
|
607
|
+
if (location !== null) gl.uniform1i(location, record.unit);
|
|
608
|
+
});
|
|
609
|
+
for (const { location, integer } of binding.absent) {
|
|
610
|
+
if (integer) gl.vertexAttribI4i(location, 0, 0, 0, 0);
|
|
611
|
+
else gl.vertexAttrib4f(location, 0, 0, 0, 0);
|
|
612
|
+
}
|
|
613
|
+
gl.bindVertexArray(binding.vao);
|
|
614
|
+
gl.drawElements(gl.TRIANGLES, geometry.indexCount, gl.UNSIGNED_INT, 0);
|
|
615
|
+
gl.bindVertexArray(null);
|
|
616
|
+
}
|
|
617
|
+
/** Whether {@link dispose} has been called. */
|
|
618
|
+
get disposed() {
|
|
619
|
+
return this.#disposed;
|
|
620
|
+
}
|
|
621
|
+
/**
|
|
622
|
+
* Release the GL objects this owns: the program, the uniform buffer and
|
|
623
|
+
* every vertex array it built. Geometries and textures given to it are the
|
|
624
|
+
* caller's and are left alone. Calling it again does nothing.
|
|
625
|
+
*/
|
|
626
|
+
dispose() {
|
|
627
|
+
if (this.#disposed) return;
|
|
628
|
+
this.#disposed = true;
|
|
629
|
+
for (const geometry of [...this.#bindings.keys()]) this.release(geometry);
|
|
630
|
+
const gl = this.#gl;
|
|
631
|
+
gl.deleteProgram(this.#program);
|
|
632
|
+
gl.deleteBuffer(this.#ubo);
|
|
633
|
+
this.#textures.clear();
|
|
634
|
+
}
|
|
635
|
+
/** The vertex array over a geometry: the one built before, or a new one. */
|
|
636
|
+
#binding(geometry) {
|
|
637
|
+
if (this.#disposed) {
|
|
638
|
+
throw new GLPipelineError(`${this.envelope.pipeline.id} is disposed`);
|
|
639
|
+
}
|
|
640
|
+
if (geometry.disposed) throw new GLPipelineError("the geometry is disposed");
|
|
641
|
+
const known = this.#bindings.get(geometry);
|
|
642
|
+
if (known !== void 0) return known;
|
|
643
|
+
const gl = this.#gl;
|
|
644
|
+
const plan = this.#inputs.map(({ input, shape, location }) => {
|
|
645
|
+
const source = geometry.attribute(input.semantic);
|
|
646
|
+
if (source !== void 0) {
|
|
647
|
+
const label = `${input.semantic} (${input.name}, ${input.type})`;
|
|
648
|
+
if (source.integer !== shape.integer) {
|
|
649
|
+
throw new GLPipelineError(
|
|
650
|
+
`${label} is ${shape.integer ? "an integer" : "a float"} attribute; the source says integer: ${source.integer}`
|
|
651
|
+
);
|
|
652
|
+
}
|
|
653
|
+
if (source.type !== (shape.integer ? "int" : "float")) {
|
|
654
|
+
throw new GLPipelineError(
|
|
655
|
+
`${label} needs ${shape.integer ? "Int32Array" : "Float32Array"} data`
|
|
656
|
+
);
|
|
657
|
+
}
|
|
658
|
+
if (source.components < shape.width) {
|
|
659
|
+
throw new GLPipelineError(
|
|
660
|
+
`${label} reads ${shape.width} values per vertex; the source carries ${source.components}`
|
|
661
|
+
);
|
|
662
|
+
}
|
|
663
|
+
}
|
|
664
|
+
return { shape, source, location };
|
|
665
|
+
});
|
|
666
|
+
const vao = gl.createVertexArray();
|
|
667
|
+
if (vao === null) throw new GLPipelineError("could not create a vertex array");
|
|
668
|
+
const absent = [];
|
|
669
|
+
gl.bindVertexArray(vao);
|
|
670
|
+
for (const { shape, source, location } of plan) {
|
|
671
|
+
if (location < 0) continue;
|
|
672
|
+
if (source === void 0) {
|
|
673
|
+
gl.disableVertexAttribArray(location);
|
|
674
|
+
absent.push({ location, integer: shape.integer });
|
|
675
|
+
continue;
|
|
676
|
+
}
|
|
677
|
+
gl.bindBuffer(gl.ARRAY_BUFFER, source.buffer);
|
|
678
|
+
gl.enableVertexAttribArray(location);
|
|
679
|
+
const stride = source.components * 4;
|
|
680
|
+
if (shape.integer) gl.vertexAttribIPointer(location, shape.width, gl.INT, stride, 0);
|
|
681
|
+
else gl.vertexAttribPointer(location, shape.width, gl.FLOAT, false, stride, 0);
|
|
682
|
+
}
|
|
683
|
+
gl.bindBuffer(gl.ELEMENT_ARRAY_BUFFER, geometry.indexBuffer);
|
|
684
|
+
gl.bindVertexArray(null);
|
|
685
|
+
const binding = {
|
|
686
|
+
vao,
|
|
687
|
+
absent,
|
|
688
|
+
forget: onDispose(geometry, () => this.release(geometry))
|
|
689
|
+
};
|
|
690
|
+
this.#bindings.set(geometry, binding);
|
|
691
|
+
return binding;
|
|
692
|
+
}
|
|
693
|
+
};
|
|
499
694
|
|
|
500
695
|
// src/version.ts
|
|
501
|
-
var VERSION = "
|
|
696
|
+
var VERSION = "2.0.0";
|
|
502
697
|
|
|
698
|
+
exports.GLGeometry = GLGeometry;
|
|
503
699
|
exports.GLPipeline = GLPipeline;
|
|
504
700
|
exports.GLPipelineError = GLPipelineError;
|
|
505
701
|
exports.PickPass = PickPass;
|