mirror of
https://github.com/Rezmason/matrix.git
synced 2026-04-14 12:29:30 -07:00
More std140 tweaks
This commit is contained in:
74
js/std140.js
74
js/std140.js
@@ -1,62 +1,62 @@
|
||||
const supportedLayoutTypes = {
|
||||
["i32"]: { alignAtByte: 1, sizeInBytes: 1, baseType: "i32" },
|
||||
["u32"]: { alignAtByte: 1, sizeInBytes: 1, baseType: "u32" },
|
||||
["f32"]: { alignAtByte: 1, sizeInBytes: 1, baseType: "f32" },
|
||||
const supportedTypes = {
|
||||
["i32"]: [1, 1, "i32"],
|
||||
["u32"]: [1, 1, "u32"],
|
||||
["f32"]: [1, 1, "f32"],
|
||||
|
||||
["atomic<i32>"]: { alignAtByte: 1, sizeInBytes: 1, baseType: "i32" },
|
||||
["vec2<i32>"]: { alignAtByte: 2, sizeInBytes: 2, baseType: "i32" },
|
||||
["vec3<i32>"]: { alignAtByte: 4, sizeInBytes: 3, baseType: "i32" },
|
||||
["vec4<i32>"]: { alignAtByte: 4, sizeInBytes: 4, baseType: "i32" },
|
||||
["atomic<i32>"]: [1, 1, "i32"],
|
||||
["vec2<i32>"]: [2, 2, "i32"],
|
||||
["vec3<i32>"]: [4, 3, "i32"],
|
||||
["vec4<i32>"]: [4, 4, "i32"],
|
||||
|
||||
["atomic<u32>"]: { alignAtByte: 1, sizeInBytes: 1, baseType: "u32" },
|
||||
["vec2<u32>"]: { alignAtByte: 2, sizeInBytes: 2, baseType: "u32" },
|
||||
["vec3<u32>"]: { alignAtByte: 4, sizeInBytes: 3, baseType: "u32" },
|
||||
["vec4<u32>"]: { alignAtByte: 4, sizeInBytes: 4, baseType: "u32" },
|
||||
["atomic<u32>"]: [1, 1, "u32"],
|
||||
["vec2<u32>"]: [2, 2, "u32"],
|
||||
["vec3<u32>"]: [4, 3, "u32"],
|
||||
["vec4<u32>"]: [4, 4, "u32"],
|
||||
|
||||
["atomic<f32>"]: { alignAtByte: 1, sizeInBytes: 1, baseType: "f32" },
|
||||
["vec2<f32>"]: { alignAtByte: 2, sizeInBytes: 2, baseType: "f32" },
|
||||
["vec3<f32>"]: { alignAtByte: 4, sizeInBytes: 3, baseType: "f32" },
|
||||
["vec4<f32>"]: { alignAtByte: 4, sizeInBytes: 4, baseType: "f32" },
|
||||
["atomic<f32>"]: [1, 1, "f32"],
|
||||
["vec2<f32>"]: [2, 2, "f32"],
|
||||
["vec3<f32>"]: [4, 3, "f32"],
|
||||
["vec4<f32>"]: [4, 4, "f32"],
|
||||
|
||||
["mat2x2<f32>"]: { alignAtByte: 2, sizeInBytes: 4, baseType: "f32" },
|
||||
["mat3x2<f32>"]: { alignAtByte: 2, sizeInBytes: 6, baseType: "f32" },
|
||||
["mat4x2<f32>"]: { alignAtByte: 2, sizeInBytes: 8, baseType: "f32" },
|
||||
["mat2x3<f32>"]: { alignAtByte: 4, sizeInBytes: 8, baseType: "f32" },
|
||||
["mat3x3<f32>"]: { alignAtByte: 4, sizeInBytes: 12, baseType: "f32" },
|
||||
["mat4x3<f32>"]: { alignAtByte: 4, sizeInBytes: 16, baseType: "f32" },
|
||||
["mat2x4<f32>"]: { alignAtByte: 4, sizeInBytes: 8, baseType: "f32" },
|
||||
["mat3x4<f32>"]: { alignAtByte: 4, sizeInBytes: 12, baseType: "f32" },
|
||||
["mat4x4<f32>"]: { alignAtByte: 4, sizeInBytes: 16, baseType: "f32" },
|
||||
["mat2x2<f32>"]: [2, 4, "f32"],
|
||||
["mat3x2<f32>"]: [2, 6, "f32"],
|
||||
["mat4x2<f32>"]: [2, 8, "f32"],
|
||||
["mat2x3<f32>"]: [4, 8, "f32"],
|
||||
["mat3x3<f32>"]: [4, 12, "f32"],
|
||||
["mat4x3<f32>"]: [4, 16, "f32"],
|
||||
["mat2x4<f32>"]: [4, 8, "f32"],
|
||||
["mat3x4<f32>"]: [4, 12, "f32"],
|
||||
["mat4x4<f32>"]: [4, 16, "f32"],
|
||||
};
|
||||
|
||||
const computeStructLayout = (types) => {
|
||||
const entries = [];
|
||||
const fields = [];
|
||||
let byteOffset = 0;
|
||||
for (const type of types) {
|
||||
if (supportedLayoutTypes[type] == null) {
|
||||
if (supportedTypes[type] == null) {
|
||||
throw new Error(`Unsupported type: ${type}`);
|
||||
}
|
||||
const { alignAtByte, sizeInBytes, baseType } = supportedLayoutTypes[type];
|
||||
const [alignAtByte, sizeInBytes, baseType] = supportedTypes[type];
|
||||
byteOffset = Math.ceil(byteOffset / alignAtByte) * alignAtByte;
|
||||
entries.push({ baseType, byteOffset });
|
||||
fields.push({ baseType, byteOffset });
|
||||
byteOffset += sizeInBytes;
|
||||
}
|
||||
|
||||
// console.log(types);
|
||||
// console.log(entries);
|
||||
// console.log(fields);
|
||||
|
||||
const size = byteOffset * Float32Array.BYTES_PER_ELEMENT;
|
||||
|
||||
return {
|
||||
entries,
|
||||
fields,
|
||||
size,
|
||||
build: (values, buffer = null) => buildStruct(entries, values, buffer ?? new ArrayBuffer(size)),
|
||||
build: (values, buffer = null) => buildStruct(fields, values, buffer ?? new ArrayBuffer(size)),
|
||||
};
|
||||
};
|
||||
|
||||
const buildStruct = (entries, values, buffer) => {
|
||||
if (values.length !== entries.length) {
|
||||
throw new Error(`This struct contains ${entries.length} values, and you supplied ${values.length}.`);
|
||||
const buildStruct = (fields, values, buffer) => {
|
||||
if (values.length !== fields.length) {
|
||||
throw new Error(`This struct contains ${fields.length} values, and you supplied ${values.length}.`);
|
||||
}
|
||||
|
||||
const views = {
|
||||
@@ -66,10 +66,10 @@ const buildStruct = (entries, values, buffer) => {
|
||||
};
|
||||
|
||||
for (let i = 0; i < values.length; i++) {
|
||||
const view = views[entries[i].baseType];
|
||||
const view = views[fields[i].baseType];
|
||||
const value = values[i];
|
||||
const array = value[Symbol.iterator] == null ? [value] : value;
|
||||
view.set(array, entries[i].byteOffset);
|
||||
view.set(array, fields[i].byteOffset);
|
||||
}
|
||||
return buffer;
|
||||
};
|
||||
|
||||
Reference in New Issue
Block a user