@luma.gl/core 9.3.0-alpha.8 → 9.3.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/dist/adapter/device.d.ts +1 -1
- package/dist/adapter/device.d.ts.map +1 -1
- package/dist/adapter/device.js +3 -2
- package/dist/adapter/device.js.map +1 -1
- package/dist/adapter/luma.d.ts.map +1 -1
- package/dist/adapter/luma.js +2 -1
- package/dist/adapter/luma.js.map +1 -1
- package/dist/dist.dev.js +328 -173
- package/dist/dist.min.js +5 -5
- package/dist/factories/bind-group-factory.d.ts.map +1 -1
- package/dist/factories/bind-group-factory.js +14 -5
- package/dist/factories/bind-group-factory.js.map +1 -1
- package/dist/index.cjs +317 -173
- package/dist/index.cjs.map +4 -4
- package/dist/index.d.ts +2 -1
- package/dist/index.d.ts.map +1 -1
- package/dist/index.js +2 -1
- package/dist/index.js.map +1 -1
- package/dist/portable/shader-block-writer.d.ts +51 -0
- package/dist/portable/shader-block-writer.d.ts.map +1 -0
- package/dist/portable/shader-block-writer.js +185 -0
- package/dist/portable/shader-block-writer.js.map +1 -0
- package/dist/portable/uniform-store.d.ts +52 -20
- package/dist/portable/uniform-store.d.ts.map +1 -1
- package/dist/portable/uniform-store.js +71 -26
- package/dist/portable/uniform-store.js.map +1 -1
- package/dist/shadertypes/data-types/data-type-decoder.js +2 -2
- package/dist/shadertypes/data-types/data-type-decoder.js.map +1 -1
- package/dist/shadertypes/data-types/decode-data-types.js +2 -2
- package/dist/shadertypes/data-types/decode-data-types.js.map +1 -1
- package/dist/shadertypes/shader-types/shader-block-layout.d.ts +72 -0
- package/dist/shadertypes/shader-types/shader-block-layout.d.ts.map +1 -0
- package/dist/shadertypes/shader-types/shader-block-layout.js +209 -0
- package/dist/shadertypes/shader-types/shader-block-layout.js.map +1 -0
- package/dist/shadertypes/texture-types/texture-format-decoder.js +1 -1
- package/dist/shadertypes/texture-types/texture-format-decoder.js.map +1 -1
- package/dist/shadertypes/texture-types/texture-format-table.js +2 -2
- package/dist/shadertypes/texture-types/texture-format-table.js.map +1 -1
- package/dist/shadertypes/vertex-types/vertex-format-decoder.d.ts.map +1 -1
- package/dist/shadertypes/vertex-types/vertex-format-decoder.js +41 -3
- package/dist/shadertypes/vertex-types/vertex-format-decoder.js.map +1 -1
- package/dist/shadertypes/vertex-types/vertex-formats.d.ts +6 -6
- package/dist/shadertypes/vertex-types/vertex-formats.d.ts.map +1 -1
- package/package.json +2 -2
- package/src/adapter/device.ts +4 -2
- package/src/adapter/luma.ts +1 -0
- package/src/factories/bind-group-factory.ts +23 -5
- package/src/index.ts +7 -1
- package/src/portable/shader-block-writer.ts +254 -0
- package/src/portable/uniform-store.ts +92 -37
- package/src/shadertypes/data-types/data-type-decoder.ts +2 -2
- package/src/shadertypes/data-types/decode-data-types.ts +2 -2
- package/src/shadertypes/shader-types/shader-block-layout.ts +340 -0
- package/src/shadertypes/shader-types/shader-types.ts +5 -5
- package/src/shadertypes/texture-types/texture-format-decoder.ts +1 -1
- package/src/shadertypes/texture-types/texture-format-table.ts +2 -2
- package/src/shadertypes/vertex-types/vertex-format-decoder.ts +47 -3
- package/src/shadertypes/vertex-types/vertex-formats.ts +18 -5
- package/dist/portable/uniform-buffer-layout.d.ts +0 -42
- package/dist/portable/uniform-buffer-layout.d.ts.map +0 -1
- package/dist/portable/uniform-buffer-layout.js +0 -274
- package/dist/portable/uniform-buffer-layout.js.map +0 -1
- package/src/portable/uniform-buffer-layout.ts +0 -384
|
@@ -1,274 +0,0 @@
|
|
|
1
|
-
// luma.gl
|
|
2
|
-
// SPDX-License-Identifier: MIT
|
|
3
|
-
// Copyright (c) vis.gl contributors
|
|
4
|
-
import { alignTo } from "../shadertypes/data-types/decode-data-types.js";
|
|
5
|
-
import { getVariableShaderTypeInfo, resolveVariableShaderTypeAlias } from "../shadertypes/shader-types/shader-type-decoder.js";
|
|
6
|
-
import { getScratchArrayBuffer } from "../utils/array-utils-flat.js";
|
|
7
|
-
import { isNumberArray } from "../utils/is-array.js";
|
|
8
|
-
import { log } from "../utils/log.js";
|
|
9
|
-
/**
|
|
10
|
-
* Smallest buffer size that can be used for uniform buffers.
|
|
11
|
-
* TODO - does this depend on device?
|
|
12
|
-
*/
|
|
13
|
-
const minBufferSize = 1024;
|
|
14
|
-
/**
|
|
15
|
-
* Std140 layout for uniform buffers
|
|
16
|
-
* Supports manual listing of uniforms
|
|
17
|
-
*/
|
|
18
|
-
export class UniformBufferLayout {
|
|
19
|
-
layout = {};
|
|
20
|
-
uniformTypes;
|
|
21
|
-
/** number of bytes needed for buffer allocation */
|
|
22
|
-
byteLength;
|
|
23
|
-
/** Create a new UniformBufferLayout given a map of attributes. */
|
|
24
|
-
constructor(uniformTypes) {
|
|
25
|
-
this.uniformTypes = { ...uniformTypes };
|
|
26
|
-
let size = 0;
|
|
27
|
-
for (const [key, uniformType] of Object.entries(this.uniformTypes)) {
|
|
28
|
-
size = this._addToLayout(key, uniformType, size);
|
|
29
|
-
}
|
|
30
|
-
size = alignTo(size, 4);
|
|
31
|
-
this.byteLength = Math.max(size * 4, minBufferSize);
|
|
32
|
-
}
|
|
33
|
-
/** Does this layout have a field with specified name */
|
|
34
|
-
has(name) {
|
|
35
|
-
return Boolean(this.layout[name]);
|
|
36
|
-
}
|
|
37
|
-
/** Get offset and size for a field with specified name */
|
|
38
|
-
get(name) {
|
|
39
|
-
const layout = this.layout[name];
|
|
40
|
-
return layout;
|
|
41
|
-
}
|
|
42
|
-
/** Flatten nested uniform values into leaf-path values understood by UniformBlock. */
|
|
43
|
-
getFlatUniformValues(uniformValues) {
|
|
44
|
-
const flattenedUniformValues = {};
|
|
45
|
-
for (const [name, value] of Object.entries(uniformValues)) {
|
|
46
|
-
const uniformType = this.uniformTypes[name];
|
|
47
|
-
if (uniformType) {
|
|
48
|
-
this._flattenCompositeValue(flattenedUniformValues, name, uniformType, value);
|
|
49
|
-
}
|
|
50
|
-
else if (this.layout[name]) {
|
|
51
|
-
flattenedUniformValues[name] = value;
|
|
52
|
-
}
|
|
53
|
-
}
|
|
54
|
-
return flattenedUniformValues;
|
|
55
|
-
}
|
|
56
|
-
/** Get the data for the complete buffer */
|
|
57
|
-
getData(uniformValues) {
|
|
58
|
-
const buffer = getScratchArrayBuffer(this.byteLength);
|
|
59
|
-
new Uint8Array(buffer, 0, this.byteLength).fill(0);
|
|
60
|
-
const typedArrays = {
|
|
61
|
-
i32: new Int32Array(buffer),
|
|
62
|
-
u32: new Uint32Array(buffer),
|
|
63
|
-
f32: new Float32Array(buffer),
|
|
64
|
-
f16: new Uint16Array(buffer)
|
|
65
|
-
};
|
|
66
|
-
const flattenedUniformValues = this.getFlatUniformValues(uniformValues);
|
|
67
|
-
for (const [name, value] of Object.entries(flattenedUniformValues)) {
|
|
68
|
-
this._writeLeafValue(typedArrays, name, value);
|
|
69
|
-
}
|
|
70
|
-
return new Uint8Array(buffer, 0, this.byteLength);
|
|
71
|
-
}
|
|
72
|
-
// Recursively add a uniform to the layout
|
|
73
|
-
_addToLayout(name, type, offset) {
|
|
74
|
-
if (typeof type === 'string') {
|
|
75
|
-
const info = getLeafLayoutInfo(type);
|
|
76
|
-
const alignedOffset = alignTo(offset, info.alignment);
|
|
77
|
-
this.layout[name] = {
|
|
78
|
-
offset: alignedOffset,
|
|
79
|
-
...info
|
|
80
|
-
};
|
|
81
|
-
return alignedOffset + info.size;
|
|
82
|
-
}
|
|
83
|
-
if (Array.isArray(type)) {
|
|
84
|
-
if (Array.isArray(type[0])) {
|
|
85
|
-
throw new Error(`Nested arrays are not supported for ${name}`);
|
|
86
|
-
}
|
|
87
|
-
const elementType = type[0];
|
|
88
|
-
const length = type[1];
|
|
89
|
-
const stride = alignTo(getTypeSize(elementType), 4);
|
|
90
|
-
const arrayOffset = alignTo(offset, 4);
|
|
91
|
-
for (let i = 0; i < length; i++) {
|
|
92
|
-
this._addToLayout(`${name}[${i}]`, elementType, arrayOffset + i * stride);
|
|
93
|
-
}
|
|
94
|
-
return arrayOffset + stride * length;
|
|
95
|
-
}
|
|
96
|
-
if (isCompositeShaderTypeStruct(type)) {
|
|
97
|
-
let structOffset = alignTo(offset, 4);
|
|
98
|
-
for (const [memberName, memberType] of Object.entries(type)) {
|
|
99
|
-
structOffset = this._addToLayout(`${name}.${memberName}`, memberType, structOffset);
|
|
100
|
-
}
|
|
101
|
-
return alignTo(structOffset, 4);
|
|
102
|
-
}
|
|
103
|
-
throw new Error(`Unsupported CompositeShaderType for ${name}`);
|
|
104
|
-
}
|
|
105
|
-
_flattenCompositeValue(flattenedUniformValues, baseName, uniformType, value) {
|
|
106
|
-
if (value === undefined) {
|
|
107
|
-
return;
|
|
108
|
-
}
|
|
109
|
-
if (typeof uniformType === 'string' || this.layout[baseName]) {
|
|
110
|
-
flattenedUniformValues[baseName] = value;
|
|
111
|
-
return;
|
|
112
|
-
}
|
|
113
|
-
if (Array.isArray(uniformType)) {
|
|
114
|
-
const elementType = uniformType[0];
|
|
115
|
-
const length = uniformType[1];
|
|
116
|
-
if (Array.isArray(elementType)) {
|
|
117
|
-
throw new Error(`Nested arrays are not supported for ${baseName}`);
|
|
118
|
-
}
|
|
119
|
-
if (typeof elementType === 'string' && isNumberArray(value)) {
|
|
120
|
-
this._flattenPackedArray(flattenedUniformValues, baseName, elementType, length, value);
|
|
121
|
-
return;
|
|
122
|
-
}
|
|
123
|
-
if (!Array.isArray(value)) {
|
|
124
|
-
log.warn(`Unsupported uniform array value for ${baseName}:`, value)();
|
|
125
|
-
return;
|
|
126
|
-
}
|
|
127
|
-
for (let index = 0; index < Math.min(value.length, length); index++) {
|
|
128
|
-
const elementValue = value[index];
|
|
129
|
-
if (elementValue === undefined) {
|
|
130
|
-
continue;
|
|
131
|
-
}
|
|
132
|
-
this._flattenCompositeValue(flattenedUniformValues, `${baseName}[${index}]`, elementType, elementValue);
|
|
133
|
-
}
|
|
134
|
-
return;
|
|
135
|
-
}
|
|
136
|
-
if (isCompositeShaderTypeStruct(uniformType) && isCompositeUniformObject(value)) {
|
|
137
|
-
for (const [key, subValue] of Object.entries(value)) {
|
|
138
|
-
if (subValue === undefined) {
|
|
139
|
-
continue;
|
|
140
|
-
}
|
|
141
|
-
const nestedName = `${baseName}.${key}`;
|
|
142
|
-
this._flattenCompositeValue(flattenedUniformValues, nestedName, uniformType[key], subValue);
|
|
143
|
-
}
|
|
144
|
-
return;
|
|
145
|
-
}
|
|
146
|
-
log.warn(`Unsupported uniform value for ${baseName}:`, value)();
|
|
147
|
-
}
|
|
148
|
-
_flattenPackedArray(flattenedUniformValues, baseName, elementType, length, value) {
|
|
149
|
-
const numericValue = value;
|
|
150
|
-
const elementLayout = getLeafLayoutInfo(elementType);
|
|
151
|
-
const packedElementLength = elementLayout.components;
|
|
152
|
-
for (let index = 0; index < length; index++) {
|
|
153
|
-
const start = index * packedElementLength;
|
|
154
|
-
if (start >= numericValue.length) {
|
|
155
|
-
break;
|
|
156
|
-
}
|
|
157
|
-
if (packedElementLength === 1) {
|
|
158
|
-
flattenedUniformValues[`${baseName}[${index}]`] = Number(numericValue[start]);
|
|
159
|
-
}
|
|
160
|
-
else {
|
|
161
|
-
flattenedUniformValues[`${baseName}[${index}]`] = sliceNumericArray(value, start, start + packedElementLength);
|
|
162
|
-
}
|
|
163
|
-
}
|
|
164
|
-
}
|
|
165
|
-
_writeLeafValue(typedArrays, name, value) {
|
|
166
|
-
const layout = this.layout[name];
|
|
167
|
-
if (!layout) {
|
|
168
|
-
log.warn(`Uniform ${name} not found in layout`)();
|
|
169
|
-
return;
|
|
170
|
-
}
|
|
171
|
-
const { type, components, columns, rows, offset } = layout;
|
|
172
|
-
const array = typedArrays[type];
|
|
173
|
-
if (components === 1) {
|
|
174
|
-
array[offset] = Number(value);
|
|
175
|
-
return;
|
|
176
|
-
}
|
|
177
|
-
const sourceValue = value;
|
|
178
|
-
if (columns === 1) {
|
|
179
|
-
for (let componentIndex = 0; componentIndex < components; componentIndex++) {
|
|
180
|
-
array[offset + componentIndex] = Number(sourceValue[componentIndex] ?? 0);
|
|
181
|
-
}
|
|
182
|
-
return;
|
|
183
|
-
}
|
|
184
|
-
let sourceIndex = 0;
|
|
185
|
-
for (let columnIndex = 0; columnIndex < columns; columnIndex++) {
|
|
186
|
-
const columnOffset = offset + columnIndex * 4;
|
|
187
|
-
for (let rowIndex = 0; rowIndex < rows; rowIndex++) {
|
|
188
|
-
array[columnOffset + rowIndex] = Number(sourceValue[sourceIndex++] ?? 0);
|
|
189
|
-
}
|
|
190
|
-
}
|
|
191
|
-
}
|
|
192
|
-
}
|
|
193
|
-
function getTypeSize(type) {
|
|
194
|
-
if (typeof type === 'string') {
|
|
195
|
-
return getLeafLayoutInfo(type).size;
|
|
196
|
-
}
|
|
197
|
-
if (Array.isArray(type)) {
|
|
198
|
-
const elementType = type[0];
|
|
199
|
-
const length = type[1];
|
|
200
|
-
if (Array.isArray(elementType)) {
|
|
201
|
-
throw new Error('Nested arrays are not supported');
|
|
202
|
-
}
|
|
203
|
-
return alignTo(getTypeSize(elementType), 4) * length;
|
|
204
|
-
}
|
|
205
|
-
let size = 0;
|
|
206
|
-
for (const memberType of Object.values(type)) {
|
|
207
|
-
const compositeMemberType = memberType;
|
|
208
|
-
size = alignTo(size, getTypeAlignment(compositeMemberType));
|
|
209
|
-
size += getTypeSize(compositeMemberType);
|
|
210
|
-
}
|
|
211
|
-
return alignTo(size, 4);
|
|
212
|
-
}
|
|
213
|
-
function getTypeAlignment(type) {
|
|
214
|
-
if (typeof type === 'string') {
|
|
215
|
-
return getLeafLayoutInfo(type).alignment;
|
|
216
|
-
}
|
|
217
|
-
if (Array.isArray(type)) {
|
|
218
|
-
return 4;
|
|
219
|
-
}
|
|
220
|
-
return 4;
|
|
221
|
-
}
|
|
222
|
-
function getLeafLayoutInfo(type) {
|
|
223
|
-
const resolvedType = resolveVariableShaderTypeAlias(type);
|
|
224
|
-
const decodedType = getVariableShaderTypeInfo(resolvedType);
|
|
225
|
-
const matrixMatch = /^mat(\d)x(\d)<.+>$/.exec(resolvedType);
|
|
226
|
-
if (matrixMatch) {
|
|
227
|
-
const columns = Number(matrixMatch[1]);
|
|
228
|
-
const rows = Number(matrixMatch[2]);
|
|
229
|
-
return {
|
|
230
|
-
alignment: 4,
|
|
231
|
-
size: columns * 4,
|
|
232
|
-
components: columns * rows,
|
|
233
|
-
columns,
|
|
234
|
-
rows,
|
|
235
|
-
shaderType: resolvedType,
|
|
236
|
-
type: decodedType.type
|
|
237
|
-
};
|
|
238
|
-
}
|
|
239
|
-
const vectorMatch = /^vec(\d)<.+>$/.exec(resolvedType);
|
|
240
|
-
if (vectorMatch) {
|
|
241
|
-
const components = Number(vectorMatch[1]);
|
|
242
|
-
return {
|
|
243
|
-
alignment: components === 2 ? 2 : 4,
|
|
244
|
-
size: components === 3 ? 4 : components,
|
|
245
|
-
components,
|
|
246
|
-
columns: 1,
|
|
247
|
-
rows: components,
|
|
248
|
-
shaderType: resolvedType,
|
|
249
|
-
type: decodedType.type
|
|
250
|
-
};
|
|
251
|
-
}
|
|
252
|
-
return {
|
|
253
|
-
alignment: 1,
|
|
254
|
-
size: 1,
|
|
255
|
-
components: 1,
|
|
256
|
-
columns: 1,
|
|
257
|
-
rows: 1,
|
|
258
|
-
shaderType: resolvedType,
|
|
259
|
-
type: decodedType.type
|
|
260
|
-
};
|
|
261
|
-
}
|
|
262
|
-
function isCompositeShaderTypeStruct(value) {
|
|
263
|
-
return Boolean(value) && typeof value === 'object' && !Array.isArray(value);
|
|
264
|
-
}
|
|
265
|
-
function isCompositeUniformObject(value) {
|
|
266
|
-
return (Boolean(value) &&
|
|
267
|
-
typeof value === 'object' &&
|
|
268
|
-
!Array.isArray(value) &&
|
|
269
|
-
!ArrayBuffer.isView(value));
|
|
270
|
-
}
|
|
271
|
-
function sliceNumericArray(value, start, end) {
|
|
272
|
-
return Array.prototype.slice.call(value, start, end);
|
|
273
|
-
}
|
|
274
|
-
//# sourceMappingURL=uniform-buffer-layout.js.map
|
|
@@ -1 +0,0 @@
|
|
|
1
|
-
{"version":3,"file":"uniform-bu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;QACzB,CAAC,KAAK,CAAC,OAAO,CAAC,KAAK,CAAC;QACrB,CAAC,WAAW,CAAC,MAAM,CAAC,KAAK,CAAC,CAC3B,CAAC;AACJ,CAAC;AAED,SAAS,iBAAiB,CAAC,KAAmB,EAAE,KAAa,EAAE,GAAW;IACxE,OAAO,KAAK,CAAC,SAAS,CAAC,KAAK,CAAC,IAAI,CAAC,KAAK,EAAE,KAAK,EAAE,GAAG,CAAa,CAAC;AACnE,CAAC"}
|
|
@@ -1,384 +0,0 @@
|
|
|
1
|
-
// luma.gl
|
|
2
|
-
// SPDX-License-Identifier: MIT
|
|
3
|
-
// Copyright (c) vis.gl contributors
|
|
4
|
-
|
|
5
|
-
import type {PrimitiveDataType} from '../shadertypes/data-types/data-types';
|
|
6
|
-
import type {
|
|
7
|
-
CompositeShaderType,
|
|
8
|
-
VariableShaderType
|
|
9
|
-
} from '../shadertypes/shader-types/shader-types';
|
|
10
|
-
import {alignTo} from '../shadertypes/data-types/decode-data-types';
|
|
11
|
-
import {
|
|
12
|
-
getVariableShaderTypeInfo,
|
|
13
|
-
resolveVariableShaderTypeAlias
|
|
14
|
-
} from '../shadertypes/shader-types/shader-type-decoder';
|
|
15
|
-
|
|
16
|
-
import type {CompositeUniformValue, UniformValue} from '../adapter/types/uniforms';
|
|
17
|
-
import {getScratchArrayBuffer} from '../utils/array-utils-flat';
|
|
18
|
-
import {isNumberArray} from '../utils/is-array';
|
|
19
|
-
import {log} from '../utils/log';
|
|
20
|
-
|
|
21
|
-
type UniformLayoutEntry = {
|
|
22
|
-
offset: number;
|
|
23
|
-
size: number;
|
|
24
|
-
components: number;
|
|
25
|
-
columns: number;
|
|
26
|
-
rows: number;
|
|
27
|
-
shaderType: VariableShaderType;
|
|
28
|
-
type: PrimitiveDataType;
|
|
29
|
-
};
|
|
30
|
-
|
|
31
|
-
export type UniformValues = Record<string, CompositeUniformValue>;
|
|
32
|
-
|
|
33
|
-
/**
|
|
34
|
-
* Smallest buffer size that can be used for uniform buffers.
|
|
35
|
-
* TODO - does this depend on device?
|
|
36
|
-
*/
|
|
37
|
-
const minBufferSize: number = 1024;
|
|
38
|
-
|
|
39
|
-
/**
|
|
40
|
-
* Std140 layout for uniform buffers
|
|
41
|
-
* Supports manual listing of uniforms
|
|
42
|
-
*/
|
|
43
|
-
export class UniformBufferLayout {
|
|
44
|
-
readonly layout: Record<string, UniformLayoutEntry> = {};
|
|
45
|
-
readonly uniformTypes: Record<string, CompositeShaderType>;
|
|
46
|
-
|
|
47
|
-
/** number of bytes needed for buffer allocation */
|
|
48
|
-
readonly byteLength: number;
|
|
49
|
-
|
|
50
|
-
/** Create a new UniformBufferLayout given a map of attributes. */
|
|
51
|
-
constructor(uniformTypes: Readonly<Record<string, CompositeShaderType>>) {
|
|
52
|
-
this.uniformTypes = {...uniformTypes};
|
|
53
|
-
|
|
54
|
-
let size = 0;
|
|
55
|
-
|
|
56
|
-
for (const [key, uniformType] of Object.entries(this.uniformTypes)) {
|
|
57
|
-
size = this._addToLayout(key, uniformType, size);
|
|
58
|
-
}
|
|
59
|
-
|
|
60
|
-
size = alignTo(size, 4);
|
|
61
|
-
this.byteLength = Math.max(size * 4, minBufferSize);
|
|
62
|
-
}
|
|
63
|
-
|
|
64
|
-
/** Does this layout have a field with specified name */
|
|
65
|
-
has(name: string) {
|
|
66
|
-
return Boolean(this.layout[name]);
|
|
67
|
-
}
|
|
68
|
-
|
|
69
|
-
/** Get offset and size for a field with specified name */
|
|
70
|
-
get(name: string): {offset: number; size: number} | undefined {
|
|
71
|
-
const layout = this.layout[name];
|
|
72
|
-
return layout;
|
|
73
|
-
}
|
|
74
|
-
|
|
75
|
-
/** Flatten nested uniform values into leaf-path values understood by UniformBlock. */
|
|
76
|
-
getFlatUniformValues(uniformValues: Readonly<UniformValues>): Record<string, UniformValue> {
|
|
77
|
-
const flattenedUniformValues: Record<string, UniformValue> = {};
|
|
78
|
-
|
|
79
|
-
for (const [name, value] of Object.entries(uniformValues)) {
|
|
80
|
-
const uniformType = this.uniformTypes[name];
|
|
81
|
-
if (uniformType) {
|
|
82
|
-
this._flattenCompositeValue(flattenedUniformValues, name, uniformType, value);
|
|
83
|
-
} else if (this.layout[name]) {
|
|
84
|
-
flattenedUniformValues[name] = value as UniformValue;
|
|
85
|
-
}
|
|
86
|
-
}
|
|
87
|
-
|
|
88
|
-
return flattenedUniformValues;
|
|
89
|
-
}
|
|
90
|
-
|
|
91
|
-
/** Get the data for the complete buffer */
|
|
92
|
-
getData(uniformValues: Readonly<UniformValues>): Uint8Array {
|
|
93
|
-
const buffer = getScratchArrayBuffer(this.byteLength);
|
|
94
|
-
new Uint8Array(buffer, 0, this.byteLength).fill(0);
|
|
95
|
-
const typedArrays = {
|
|
96
|
-
i32: new Int32Array(buffer),
|
|
97
|
-
u32: new Uint32Array(buffer),
|
|
98
|
-
f32: new Float32Array(buffer),
|
|
99
|
-
f16: new Uint16Array(buffer)
|
|
100
|
-
};
|
|
101
|
-
|
|
102
|
-
const flattenedUniformValues = this.getFlatUniformValues(uniformValues);
|
|
103
|
-
for (const [name, value] of Object.entries(flattenedUniformValues)) {
|
|
104
|
-
this._writeLeafValue(typedArrays, name, value);
|
|
105
|
-
}
|
|
106
|
-
|
|
107
|
-
return new Uint8Array(buffer, 0, this.byteLength);
|
|
108
|
-
}
|
|
109
|
-
|
|
110
|
-
// Recursively add a uniform to the layout
|
|
111
|
-
private _addToLayout(name: string, type: CompositeShaderType, offset: number): number {
|
|
112
|
-
if (typeof type === 'string') {
|
|
113
|
-
const info = getLeafLayoutInfo(type);
|
|
114
|
-
const alignedOffset = alignTo(offset, info.alignment);
|
|
115
|
-
this.layout[name] = {
|
|
116
|
-
offset: alignedOffset,
|
|
117
|
-
...info
|
|
118
|
-
};
|
|
119
|
-
return alignedOffset + info.size;
|
|
120
|
-
}
|
|
121
|
-
|
|
122
|
-
if (Array.isArray(type)) {
|
|
123
|
-
if (Array.isArray(type[0])) {
|
|
124
|
-
throw new Error(`Nested arrays are not supported for ${name}`);
|
|
125
|
-
}
|
|
126
|
-
|
|
127
|
-
const elementType = type[0] as CompositeShaderType;
|
|
128
|
-
const length = type[1] as number;
|
|
129
|
-
const stride = alignTo(getTypeSize(elementType), 4);
|
|
130
|
-
const arrayOffset = alignTo(offset, 4);
|
|
131
|
-
|
|
132
|
-
for (let i = 0; i < length; i++) {
|
|
133
|
-
this._addToLayout(`${name}[${i}]`, elementType, arrayOffset + i * stride);
|
|
134
|
-
}
|
|
135
|
-
return arrayOffset + stride * length;
|
|
136
|
-
}
|
|
137
|
-
|
|
138
|
-
if (isCompositeShaderTypeStruct(type)) {
|
|
139
|
-
let structOffset = alignTo(offset, 4);
|
|
140
|
-
for (const [memberName, memberType] of Object.entries(type)) {
|
|
141
|
-
structOffset = this._addToLayout(`${name}.${memberName}`, memberType, structOffset);
|
|
142
|
-
}
|
|
143
|
-
return alignTo(structOffset, 4);
|
|
144
|
-
}
|
|
145
|
-
|
|
146
|
-
throw new Error(`Unsupported CompositeShaderType for ${name}`);
|
|
147
|
-
}
|
|
148
|
-
|
|
149
|
-
private _flattenCompositeValue(
|
|
150
|
-
flattenedUniformValues: Record<string, UniformValue>,
|
|
151
|
-
baseName: string,
|
|
152
|
-
uniformType: CompositeShaderType,
|
|
153
|
-
value: CompositeUniformValue | undefined
|
|
154
|
-
): void {
|
|
155
|
-
if (value === undefined) {
|
|
156
|
-
return;
|
|
157
|
-
}
|
|
158
|
-
|
|
159
|
-
if (typeof uniformType === 'string' || this.layout[baseName]) {
|
|
160
|
-
flattenedUniformValues[baseName] = value as UniformValue;
|
|
161
|
-
return;
|
|
162
|
-
}
|
|
163
|
-
|
|
164
|
-
if (Array.isArray(uniformType)) {
|
|
165
|
-
const elementType = uniformType[0] as CompositeShaderType;
|
|
166
|
-
const length = uniformType[1] as number;
|
|
167
|
-
|
|
168
|
-
if (Array.isArray(elementType)) {
|
|
169
|
-
throw new Error(`Nested arrays are not supported for ${baseName}`);
|
|
170
|
-
}
|
|
171
|
-
|
|
172
|
-
if (typeof elementType === 'string' && isNumberArray(value)) {
|
|
173
|
-
this._flattenPackedArray(flattenedUniformValues, baseName, elementType, length, value);
|
|
174
|
-
return;
|
|
175
|
-
}
|
|
176
|
-
|
|
177
|
-
if (!Array.isArray(value)) {
|
|
178
|
-
log.warn(`Unsupported uniform array value for ${baseName}:`, value)();
|
|
179
|
-
return;
|
|
180
|
-
}
|
|
181
|
-
|
|
182
|
-
for (let index = 0; index < Math.min(value.length, length); index++) {
|
|
183
|
-
const elementValue = value[index];
|
|
184
|
-
if (elementValue === undefined) {
|
|
185
|
-
continue;
|
|
186
|
-
}
|
|
187
|
-
|
|
188
|
-
this._flattenCompositeValue(
|
|
189
|
-
flattenedUniformValues,
|
|
190
|
-
`${baseName}[${index}]`,
|
|
191
|
-
elementType,
|
|
192
|
-
elementValue
|
|
193
|
-
);
|
|
194
|
-
}
|
|
195
|
-
return;
|
|
196
|
-
}
|
|
197
|
-
|
|
198
|
-
if (isCompositeShaderTypeStruct(uniformType) && isCompositeUniformObject(value)) {
|
|
199
|
-
for (const [key, subValue] of Object.entries(value)) {
|
|
200
|
-
if (subValue === undefined) {
|
|
201
|
-
continue;
|
|
202
|
-
}
|
|
203
|
-
|
|
204
|
-
const nestedName = `${baseName}.${key}`;
|
|
205
|
-
this._flattenCompositeValue(flattenedUniformValues, nestedName, uniformType[key], subValue);
|
|
206
|
-
}
|
|
207
|
-
return;
|
|
208
|
-
}
|
|
209
|
-
|
|
210
|
-
log.warn(`Unsupported uniform value for ${baseName}:`, value)();
|
|
211
|
-
}
|
|
212
|
-
|
|
213
|
-
private _flattenPackedArray(
|
|
214
|
-
flattenedUniformValues: Record<string, UniformValue>,
|
|
215
|
-
baseName: string,
|
|
216
|
-
elementType: VariableShaderType,
|
|
217
|
-
length: number,
|
|
218
|
-
value: UniformValue
|
|
219
|
-
): void {
|
|
220
|
-
const numericValue = value as Readonly<ArrayLike<number>>;
|
|
221
|
-
const elementLayout = getLeafLayoutInfo(elementType);
|
|
222
|
-
const packedElementLength = elementLayout.components;
|
|
223
|
-
|
|
224
|
-
for (let index = 0; index < length; index++) {
|
|
225
|
-
const start = index * packedElementLength;
|
|
226
|
-
if (start >= numericValue.length) {
|
|
227
|
-
break;
|
|
228
|
-
}
|
|
229
|
-
|
|
230
|
-
if (packedElementLength === 1) {
|
|
231
|
-
flattenedUniformValues[`${baseName}[${index}]`] = Number(numericValue[start]);
|
|
232
|
-
} else {
|
|
233
|
-
flattenedUniformValues[`${baseName}[${index}]`] = sliceNumericArray(
|
|
234
|
-
value,
|
|
235
|
-
start,
|
|
236
|
-
start + packedElementLength
|
|
237
|
-
) as UniformValue;
|
|
238
|
-
}
|
|
239
|
-
}
|
|
240
|
-
}
|
|
241
|
-
|
|
242
|
-
private _writeLeafValue(
|
|
243
|
-
typedArrays: Record<string, any>,
|
|
244
|
-
name: string,
|
|
245
|
-
value: UniformValue
|
|
246
|
-
): void {
|
|
247
|
-
const layout = this.layout[name];
|
|
248
|
-
if (!layout) {
|
|
249
|
-
log.warn(`Uniform ${name} not found in layout`)();
|
|
250
|
-
return;
|
|
251
|
-
}
|
|
252
|
-
|
|
253
|
-
const {type, components, columns, rows, offset} = layout;
|
|
254
|
-
const array = typedArrays[type];
|
|
255
|
-
|
|
256
|
-
if (components === 1) {
|
|
257
|
-
array[offset] = Number(value);
|
|
258
|
-
return;
|
|
259
|
-
}
|
|
260
|
-
|
|
261
|
-
const sourceValue = value as Readonly<ArrayLike<number>>;
|
|
262
|
-
|
|
263
|
-
if (columns === 1) {
|
|
264
|
-
for (let componentIndex = 0; componentIndex < components; componentIndex++) {
|
|
265
|
-
array[offset + componentIndex] = Number(sourceValue[componentIndex] ?? 0);
|
|
266
|
-
}
|
|
267
|
-
return;
|
|
268
|
-
}
|
|
269
|
-
|
|
270
|
-
let sourceIndex = 0;
|
|
271
|
-
for (let columnIndex = 0; columnIndex < columns; columnIndex++) {
|
|
272
|
-
const columnOffset = offset + columnIndex * 4;
|
|
273
|
-
for (let rowIndex = 0; rowIndex < rows; rowIndex++) {
|
|
274
|
-
array[columnOffset + rowIndex] = Number(sourceValue[sourceIndex++] ?? 0);
|
|
275
|
-
}
|
|
276
|
-
}
|
|
277
|
-
}
|
|
278
|
-
}
|
|
279
|
-
|
|
280
|
-
function getTypeSize(type: CompositeShaderType): number {
|
|
281
|
-
if (typeof type === 'string') {
|
|
282
|
-
return getLeafLayoutInfo(type).size;
|
|
283
|
-
}
|
|
284
|
-
|
|
285
|
-
if (Array.isArray(type)) {
|
|
286
|
-
const elementType = type[0] as CompositeShaderType;
|
|
287
|
-
const length = type[1] as number;
|
|
288
|
-
|
|
289
|
-
if (Array.isArray(elementType)) {
|
|
290
|
-
throw new Error('Nested arrays are not supported');
|
|
291
|
-
}
|
|
292
|
-
|
|
293
|
-
return alignTo(getTypeSize(elementType), 4) * length;
|
|
294
|
-
}
|
|
295
|
-
|
|
296
|
-
let size = 0;
|
|
297
|
-
for (const memberType of Object.values(type)) {
|
|
298
|
-
const compositeMemberType = memberType as CompositeShaderType;
|
|
299
|
-
size = alignTo(size, getTypeAlignment(compositeMemberType));
|
|
300
|
-
size += getTypeSize(compositeMemberType);
|
|
301
|
-
}
|
|
302
|
-
|
|
303
|
-
return alignTo(size, 4);
|
|
304
|
-
}
|
|
305
|
-
|
|
306
|
-
function getTypeAlignment(type: CompositeShaderType): 1 | 2 | 4 {
|
|
307
|
-
if (typeof type === 'string') {
|
|
308
|
-
return getLeafLayoutInfo(type).alignment;
|
|
309
|
-
}
|
|
310
|
-
|
|
311
|
-
if (Array.isArray(type)) {
|
|
312
|
-
return 4;
|
|
313
|
-
}
|
|
314
|
-
|
|
315
|
-
return 4;
|
|
316
|
-
}
|
|
317
|
-
|
|
318
|
-
function getLeafLayoutInfo(
|
|
319
|
-
type: VariableShaderType
|
|
320
|
-
): Omit<UniformLayoutEntry, 'offset'> & {alignment: 1 | 2 | 4} {
|
|
321
|
-
const resolvedType = resolveVariableShaderTypeAlias(type);
|
|
322
|
-
const decodedType = getVariableShaderTypeInfo(resolvedType);
|
|
323
|
-
const matrixMatch = /^mat(\d)x(\d)<.+>$/.exec(resolvedType);
|
|
324
|
-
|
|
325
|
-
if (matrixMatch) {
|
|
326
|
-
const columns = Number(matrixMatch[1]);
|
|
327
|
-
const rows = Number(matrixMatch[2]);
|
|
328
|
-
|
|
329
|
-
return {
|
|
330
|
-
alignment: 4,
|
|
331
|
-
size: columns * 4,
|
|
332
|
-
components: columns * rows,
|
|
333
|
-
columns,
|
|
334
|
-
rows,
|
|
335
|
-
shaderType: resolvedType,
|
|
336
|
-
type: decodedType.type
|
|
337
|
-
};
|
|
338
|
-
}
|
|
339
|
-
|
|
340
|
-
const vectorMatch = /^vec(\d)<.+>$/.exec(resolvedType);
|
|
341
|
-
if (vectorMatch) {
|
|
342
|
-
const components = Number(vectorMatch[1]) as 2 | 3 | 4;
|
|
343
|
-
return {
|
|
344
|
-
alignment: components === 2 ? 2 : 4,
|
|
345
|
-
size: components === 3 ? 4 : components,
|
|
346
|
-
components,
|
|
347
|
-
columns: 1,
|
|
348
|
-
rows: components,
|
|
349
|
-
shaderType: resolvedType,
|
|
350
|
-
type: decodedType.type
|
|
351
|
-
};
|
|
352
|
-
}
|
|
353
|
-
|
|
354
|
-
return {
|
|
355
|
-
alignment: 1,
|
|
356
|
-
size: 1,
|
|
357
|
-
components: 1,
|
|
358
|
-
columns: 1,
|
|
359
|
-
rows: 1,
|
|
360
|
-
shaderType: resolvedType,
|
|
361
|
-
type: decodedType.type
|
|
362
|
-
};
|
|
363
|
-
}
|
|
364
|
-
|
|
365
|
-
function isCompositeShaderTypeStruct(
|
|
366
|
-
value: CompositeShaderType
|
|
367
|
-
): value is Record<string, CompositeShaderType> {
|
|
368
|
-
return Boolean(value) && typeof value === 'object' && !Array.isArray(value);
|
|
369
|
-
}
|
|
370
|
-
|
|
371
|
-
function isCompositeUniformObject(
|
|
372
|
-
value: CompositeUniformValue
|
|
373
|
-
): value is Record<string, CompositeUniformValue | undefined> {
|
|
374
|
-
return (
|
|
375
|
-
Boolean(value) &&
|
|
376
|
-
typeof value === 'object' &&
|
|
377
|
-
!Array.isArray(value) &&
|
|
378
|
-
!ArrayBuffer.isView(value)
|
|
379
|
-
);
|
|
380
|
-
}
|
|
381
|
-
|
|
382
|
-
function sliceNumericArray(value: UniformValue, start: number, end: number): number[] {
|
|
383
|
-
return Array.prototype.slice.call(value, start, end) as number[];
|
|
384
|
-
}
|