-
Notifications
You must be signed in to change notification settings - Fork 12
/
utils.js
418 lines (388 loc) · 13 KB
/
utils.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
// Debug
const NAMESPACE = "pex-context";
const checkProps = (allowedProps, obj) =>
Object.keys(obj).forEach((prop) => {
if (!allowedProps.includes(prop)) throw new Error(`Unknown prop "${prop}"`);
});
const isWebGL2 = (gl) =>
typeof WebGL2RenderingContext !== "undefined" &&
gl instanceof WebGL2RenderingContext;
// State and gl
function compareFBOAttachments(framebuffer, passOpts) {
const fboDepthAttachment = framebuffer.depth?.texture;
const passDepthAttachment = passOpts.depth?.texture || passOpts.depth;
if (fboDepthAttachment != passDepthAttachment) return false;
if (framebuffer.color.length != passOpts.color.length) return false;
for (let i = 0; i < framebuffer.color.length; i++) {
const fboColorAttachment = framebuffer.color[i]?.texture;
const passColorAttachment = passOpts.color[i]?.texture || passOpts.color[i];
if (fboColorAttachment != passColorAttachment) return false;
}
return true;
}
function enableVertexData(ctx, vertexLayout, cmd, updateState) {
const gl = ctx.gl;
const { attributes = {}, indices } = cmd;
for (let i = 0; i < ctx.capabilities.maxVertexAttribs; i++) {
ctx.state.activeAttributes[i] = null;
gl.disableVertexAttribArray(i);
}
for (let i = 0; i < vertexLayout.length; i++) {
const [name, location, size] = vertexLayout[i];
// TODO: is attributes array valid?
const attrib = attributes[i] || attributes[name];
if (!attrib) {
console.debug(
NAMESPACE,
"invalid command",
cmd,
"doesn't satisfy vertex layout",
vertexLayout,
);
throw new Error(
`Command is missing attribute "${name}" at location ${location} with ${attrib}`,
);
}
let buffer = attrib.buffer;
if (!buffer && attrib.class === "vertexBuffer") {
buffer = attrib;
}
if (!buffer || !buffer.target) {
throw new Error(
`Trying to draw arrays with invalid buffer for attribute : ${name}`,
);
}
gl.bindBuffer(buffer.target, buffer.handle);
if (size === 16) {
gl.enableVertexAttribArray(location + 0);
gl.enableVertexAttribArray(location + 1);
gl.enableVertexAttribArray(location + 2);
gl.enableVertexAttribArray(location + 3);
if (updateState) {
ctx.state.activeAttributes[location + 0] = buffer;
ctx.state.activeAttributes[location + 1] = buffer;
ctx.state.activeAttributes[location + 2] = buffer;
ctx.state.activeAttributes[location + 3] = buffer;
}
// TODO: is this still valid?
// we still check for buffer type because while e.g. pex-renderer would copy buffer type to attrib, a raw pex-context example probably would not
gl.vertexAttribPointer(
location,
4,
attrib.type || buffer.type,
attrib.normalized || false,
attrib.stride || 64,
attrib.offset || 0,
);
gl.vertexAttribPointer(
location + 1,
4,
attrib.type || buffer.type,
attrib.normalized || false,
attrib.stride || 64,
attrib.offset || 16,
);
gl.vertexAttribPointer(
location + 2,
4,
attrib.type || buffer.type,
attrib.normalized || false,
attrib.stride || 64,
attrib.offset || 32,
);
gl.vertexAttribPointer(
location + 3,
4,
attrib.type || buffer.type,
attrib.normalized || false,
attrib.stride || 64,
attrib.offset || 48,
);
const divisor = attrib.divisor || 0;
gl.vertexAttribDivisor(location + 0, divisor);
gl.vertexAttribDivisor(location + 1, divisor);
gl.vertexAttribDivisor(location + 2, divisor);
gl.vertexAttribDivisor(location + 3, divisor);
} else {
gl.enableVertexAttribArray(location);
if (updateState) ctx.state.activeAttributes[location] = buffer;
let offset = attrib.offset || 0;
const type = attrib.type || buffer.type;
// Instanced Base fallback
if (
!ctx.capabilities.drawInstancedBase &&
(cmd.baseInstance || cmd.baseVertex)
) {
if (indices) {
offset +=
(attrib.divisor ? cmd.baseInstance : cmd.baseVertex) *
size *
ctx.DataTypeConstructor[type].BYTES_PER_ELEMENT;
} else {
if (attrib.divisor) offset += (cmd.count * cmd.baseInstance) / size;
}
}
gl.vertexAttribPointer(
location,
size,
type,
attrib.normalized || false,
attrib.stride || 0,
offset,
);
gl.vertexAttribDivisor(location, attrib.divisor || 0);
}
// TODO: how to match index with vertexLayout location?
}
if (indices) {
let indexBuffer = indices.buffer;
if (!indexBuffer && indices.class === "indexBuffer") {
indexBuffer = indices;
}
if (!indexBuffer || !indexBuffer.target) {
console.debug(NAMESPACE, "invalid command", cmd, "buffer", indexBuffer);
throw new Error(`Trying to draw arrays with invalid buffer for elements`);
}
if (updateState) ctx.state.indexBuffer = indexBuffer;
gl.bindBuffer(indexBuffer.target, indexBuffer.handle);
}
}
function drawElements(ctx, cmd, instanced, primitive) {
const gl = ctx.gl;
// TODO: is that always correct
const count = cmd.count || ctx.state.indexBuffer.length;
const offset = cmd.indices?.offset || cmd.vertexArray?.indices?.offset || 0;
const type =
cmd.indices?.type ||
cmd.vertexArray?.indices?.offset ||
ctx.state.indexBuffer.type;
// Instanced drawing
if (instanced) {
if (cmd.multiDraw) {
if (ctx.capabilities.multiDraw) {
// Multidraw elements instanced base
if (
ctx.capabilities.multiDrawInstancedBase &&
(cmd.multiDraw.baseVertices || cmd.multiDraw.baseInstances)
) {
const ext = gl.getExtension(
"WEBGL_multi_draw_instanced_base_vertex_base_instance",
);
ext.multiDrawElementsInstancedBaseVertexBaseInstanceWEBGL(
primitive,
cmd.multiDraw.counts,
cmd.multiDraw.countsOffset || 0,
type,
cmd.multiDraw.offsets,
cmd.multiDraw.offsetsOffset || 0,
cmd.multiDraw.instanceCounts,
cmd.multiDraw.instanceCountsOffset || 0,
cmd.multiDraw.baseVertices || [],
cmd.multiDraw.baseVerticesOffset || 0,
cmd.multiDraw.baseInstances || [],
cmd.multiDraw.baseInstancesOffset || 0,
cmd.multiDraw.drawCount || cmd.multiDraw.counts.length,
);
} else {
// Multidraw elements instanced (or multidraw base fallback with offset from enableVertexData)
const ext = gl.getExtension("WEBGL_multi_draw");
// TODO: fallback
if (cmd.multiDraw.baseVertices || cmd.multiDraw.baseInstances) {
console.error("Unsupported multiDrawInstancedBase. No fallback.");
}
ext.multiDrawElementsInstancedWEBGL(
primitive,
cmd.multiDraw.counts,
cmd.multiDraw.countsOffset || 0,
type,
cmd.multiDraw.offsets,
cmd.multiDraw.offsetsOffset || 0,
cmd.multiDraw.instanceCounts,
cmd.multiDraw.instanceCountsOffset || 0,
cmd.multiDraw.drawCount || cmd.multiDraw.counts.length,
);
}
} else {
// Multi draw elements instanced fallback
// TODO: fallback
console.error("Unsupported multidraw. No fallback.");
}
} else {
// Non-multi drawing
if (
ctx.capabilities.drawInstancedBase &&
(Number.isFinite(cmd.baseVertex) || Number.isFinite(cmd.baseInstance))
) {
// Draw elements instanced base
const ext = gl.getExtension(
"WEBGL_draw_instanced_base_vertex_base_instance",
);
ext.drawElementsInstancedBaseVertexBaseInstanceWEBGL(
primitive,
count,
type,
offset,
cmd.instances,
cmd.baseVertex || 0,
cmd.baseInstance || 0,
);
} else {
// Draw elements instanced (or base fallback with offset from enableVertexData)
gl.drawElementsInstanced(primitive, count, type, offset, cmd.instances);
}
}
} else {
// Non instanced drawing
if (cmd.multiDraw) {
if (ctx.capabilities.multiDraw) {
// Multidraw elements
const ext = gl.getExtension("WEBGL_multi_draw");
ext.multiDrawElementsWEBGL(
primitive,
cmd.multiDraw.counts,
cmd.multiDraw.countsOffset || 0,
type,
cmd.multiDraw.offsets,
cmd.multiDraw.offsetsOffset || 0,
cmd.multiDraw.drawCount || cmd.multiDraw.counts.length,
);
} else {
// Multidraw elements fallback
const countsOffset = cmd.multiDraw.countsOffset || 0;
const offsetsOffset = cmd.multiDraw.offsetsOffset || 0;
const drawCount =
cmd.multiDraw.drawCount || cmd.multiDraw.counts.length;
for (let i = 0; i < drawCount; i++) {
gl.drawElements(
primitive,
cmd.multiDraw.counts[i + countsOffset],
type,
cmd.multiDraw.offsets[i + offsetsOffset],
);
}
}
} else {
// Draw elements
gl.drawElements(primitive, count, type, offset);
}
}
}
function drawArrays(ctx, cmd, instanced, primitive) {
const gl = ctx.gl;
if (instanced) {
if (cmd.multiDraw && ctx.capabilities.multiDraw) {
if (
cmd.multiDraw.baseInstances &&
ctx.capabilities.multiDrawInstancedBase
) {
const ext = gl.getExtension(
"WEBGL_multi_draw_instanced_base_vertex_base_instance",
);
ext.multiDrawArraysInstancedBaseInstanceWEBGL(
primitive,
cmd.multiDraw.firsts,
cmd.multiDraw.firstsOffset || 0,
cmd.multiDraw.counts,
cmd.multiDraw.countsOffset || 0,
cmd.multiDraw.instanceCounts,
cmd.multiDraw.instanceCountsOffset || 0,
cmd.multiDraw.baseInstances,
cmd.multiDraw.baseInstancesOffset || 0,
cmd.multiDraw.drawCount || cmd.multiDraw.firsts.length,
);
} else {
const ext = gl.getExtension("WEBGL_multi_draw");
ext.multiDrawArraysInstancedWEBGL(
primitive,
cmd.multiDraw.firsts,
cmd.multiDraw.firstsOffset || 0,
cmd.multiDraw.counts,
cmd.multiDraw.countsOffset || 0,
cmd.multiDraw.instanceCounts,
cmd.multiDraw.instanceCountsOffset || 0,
cmd.multiDraw.drawCount || cmd.multiDraw.firsts.length,
);
}
} else {
// Non-multi drawing
if (
ctx.capabilities.drawInstancedBase &&
Number.isFinite(cmd.baseInstance)
) {
// Draw arrays instanced with base instance
const ext = gl.getExtension(
"WEBGL_draw_instanced_base_vertex_base_instance",
);
ext.drawArraysInstancedBaseInstanceWEBGL(
primitive,
cmd.first || 0,
cmd.count,
cmd.instances,
cmd.baseInstance,
);
} else {
// Draw arrays instanced (or base instance fallback)
gl.drawArraysInstanced(
primitive,
cmd.first || 0,
cmd.count,
cmd.instances,
);
}
}
} else {
// Non instanced drawing
if (cmd.multiDraw) {
// Multidraw arrays
if (ctx.capabilities.multiDraw) {
const ext = gl.getExtension("WEBGL_multi_draw");
ext.multiDrawArraysWEBGL(
primitive,
cmd.multiDraw.firsts,
cmd.multiDraw.firstsOffset || 0,
cmd.multiDraw.counts,
cmd.multiDraw.countsOffset || 0,
cmd.multiDraw.drawCount || cmd.multiDraw.firsts.length,
);
} else {
// Multidraw arrays fallback
const firstsOffset = cmd.multiDraw.firstsOffset || 0;
const countsOffset = cmd.multiDraw.countsOffset || 0;
const drawCount =
cmd.multiDraw.drawCount || cmd.multiDraw.firsts.length;
for (let i = 0; i < drawCount; i++) {
gl.drawArrays(
primitive,
cmd.multiDraw.firsts[i + firstsOffset],
cmd.multiDraw.counts[i + countsOffset],
);
}
}
} else {
// Draw arrays
gl.drawArrays(primitive, cmd.first || 0, cmd.count);
}
}
}
function draw(ctx, cmd) {
const instanced = Object.values(
cmd.attributes || cmd.vertexArray.attributes,
).some((attrib) => attrib.divisor);
const primitive = cmd.pipeline.primitive;
// Draw elements/arrays: instanced, base, multi-draw
if (cmd.indices || cmd.vertexArray?.indices) {
drawElements(ctx, cmd, instanced, primitive);
} else if (cmd.count || cmd.multiDraw?.counts) {
drawArrays(ctx, cmd, instanced, primitive);
} else {
throw new Error("Vertex arrays requires elements, count or multiDraw");
}
}
export {
NAMESPACE,
isWebGL2,
checkProps,
compareFBOAttachments,
enableVertexData,
draw,
};