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