mirror of
https://github.com/Rezmason/matrix.git
synced 2026-04-14 12:29:30 -07:00
Renamed gpu-uniforms to gpu-buffer, and messed around with its API.
I believe all the align, size, stride and byteOffset values are now in the proper units, ie. bytes.
This commit is contained in:
@@ -12,50 +12,49 @@
|
||||
*
|
||||
**/
|
||||
|
||||
const BYTES_PER_ELEMENT = 4;
|
||||
const zero = () => 0;
|
||||
const array = (n) => () => Array(n).fill(0);
|
||||
|
||||
const simpleTypes = {
|
||||
["i32"]: [1, 1, "i32", zero],
|
||||
["u32"]: [1, 1, "u32", zero],
|
||||
["f32"]: [1, 1, "f32", zero],
|
||||
["i32"]: [4, 4, "i32", zero],
|
||||
["u32"]: [4, 4, "u32", zero],
|
||||
["f32"]: [4, 4, "f32", zero],
|
||||
|
||||
["atomic<i32>"]: [1, 1, "i32", zero],
|
||||
["atomic<u32>"]: [1, 1, "u32", zero],
|
||||
["atomic<f32>"]: [1, 1, "f32", zero],
|
||||
["atomic<i32>"]: [4, 4, "i32", zero],
|
||||
["atomic<u32>"]: [4, 4, "u32", zero],
|
||||
["atomic<f32>"]: [4, 4, "f32", zero],
|
||||
|
||||
["vec2<i32>"]: [2, 2, "i32", array(2)],
|
||||
["vec2<u32>"]: [2, 2, "u32", array(2)],
|
||||
["vec2<f32>"]: [2, 2, "f32", array(2)],
|
||||
["vec2<i32>"]: [8, 8, "i32", array(2)],
|
||||
["vec2<u32>"]: [8, 8, "u32", array(2)],
|
||||
["vec2<f32>"]: [8, 8, "f32", array(2)],
|
||||
|
||||
["vec3<i32>"]: [4, 3, "i32", array(3)],
|
||||
["vec3<u32>"]: [4, 3, "u32", array(3)],
|
||||
["vec3<f32>"]: [4, 3, "f32", array(3)],
|
||||
["vec3<i32>"]: [16, 12, "i32", array(3)],
|
||||
["vec3<u32>"]: [16, 12, "u32", array(3)],
|
||||
["vec3<f32>"]: [16, 12, "f32", array(3)],
|
||||
|
||||
["vec4<i32>"]: [4, 4, "i32", array(4)],
|
||||
["vec4<u32>"]: [4, 4, "u32", array(4)],
|
||||
["vec4<f32>"]: [4, 4, "f32", array(4)],
|
||||
["vec4<i32>"]: [16, 16, "i32", array(4)],
|
||||
["vec4<u32>"]: [16, 16, "u32", array(4)],
|
||||
["vec4<f32>"]: [16, 16, "f32", array(4)],
|
||||
|
||||
["mat2x2<f32>"]: [2, 4, "f32", array(2 * 2)],
|
||||
["mat3x2<f32>"]: [2, 6, "f32", array(3 * 2)],
|
||||
["mat4x2<f32>"]: [2, 8, "f32", array(4 * 2)],
|
||||
["mat2x3<f32>"]: [4, 8, "f32", array(2 * 3)],
|
||||
["mat3x3<f32>"]: [4, 12, "f32", array(3 * 3)],
|
||||
["mat4x3<f32>"]: [4, 16, "f32", array(4 * 3)],
|
||||
["mat2x4<f32>"]: [4, 8, "f32", array(2 * 4)],
|
||||
["mat3x4<f32>"]: [4, 12, "f32", array(3 * 4)],
|
||||
["mat4x4<f32>"]: [4, 16, "f32", array(4 * 4)],
|
||||
["mat2x2<f32>"]: [8, 16, "f32", array(2 * 2)],
|
||||
["mat3x2<f32>"]: [8, 24, "f32", array(3 * 2)],
|
||||
["mat4x2<f32>"]: [8, 32, "f32", array(4 * 2)],
|
||||
["mat2x3<f32>"]: [16, 32, "f32", array(2 * 3)],
|
||||
["mat3x3<f32>"]: [16, 48, "f32", array(3 * 3)],
|
||||
["mat4x3<f32>"]: [16, 64, "f32", array(4 * 3)],
|
||||
["mat2x4<f32>"]: [16, 32, "f32", array(2 * 4)],
|
||||
["mat3x4<f32>"]: [16, 48, "f32", array(3 * 4)],
|
||||
["mat4x4<f32>"]: [16, 64, "f32", array(4 * 4)],
|
||||
};
|
||||
|
||||
const getTypeData = (type, attributes, otherStructLayouts) => {
|
||||
if (simpleTypes[type] != null) {
|
||||
let [align, size, baseType, defaultValue] = simpleTypes[type];
|
||||
if (attributes.align != null) {
|
||||
align = parseInt(attributes.align) / 4;
|
||||
align = parseInt(attributes.align);
|
||||
}
|
||||
if (attributes.size != null) {
|
||||
size = parseInt(attributes.size) / 4;
|
||||
size = parseInt(attributes.size);
|
||||
}
|
||||
return {
|
||||
baseType,
|
||||
@@ -67,10 +66,10 @@ const getTypeData = (type, attributes, otherStructLayouts) => {
|
||||
const innerLayout = otherStructLayouts[type];
|
||||
let { align, size } = innerLayout;
|
||||
if (attributes.align != null) {
|
||||
align = parseInt(attributes.align) / 4;
|
||||
align = parseInt(attributes.align);
|
||||
}
|
||||
if (attributes.size != null) {
|
||||
size = parseInt(attributes.size) / 4;
|
||||
size = parseInt(attributes.size);
|
||||
}
|
||||
return {
|
||||
isStruct: true,
|
||||
@@ -153,7 +152,7 @@ const parseStruct = (str, structLayouts) => {
|
||||
byteOffset += typeData.size;
|
||||
}
|
||||
|
||||
const minSizeInBytes = byteOffset * BYTES_PER_ELEMENT;
|
||||
const minSizeInBytes = byteOffset;
|
||||
const align = Math.max(...fields.map((field) => field.align));
|
||||
const size = Math.ceil(minSizeInBytes / align) * align;
|
||||
return { name, fields, size, align };
|
||||
@@ -199,7 +198,7 @@ const writeField = (allLayouts, field, value, views, byteOffset, warnMissingFiel
|
||||
} else {
|
||||
const view = views[field.baseType];
|
||||
const array = value[Symbol.iterator] == null ? [Number(value)] : value;
|
||||
view.set(array, byteOffset + field.byteOffset);
|
||||
view.set(array, (byteOffset + field.byteOffset) / 4);
|
||||
}
|
||||
};
|
||||
|
||||
@@ -208,7 +207,7 @@ const makeGenerator = (layout, structLayouts) => {
|
||||
return Object.freeze({
|
||||
minSize,
|
||||
create: () => makeDataForLayout(structLayouts, layout),
|
||||
write: (object, destination, warnMissingFields = false) => {
|
||||
toBuffer: (object, destination, warnMissingFields = false) => {
|
||||
if (destination == null) {
|
||||
let size = layout.size;
|
||||
const lastField = layout.fields[layout.fields.length - 1];
|
||||
@@ -233,15 +232,16 @@ const makeGenerator = (layout, structLayouts) => {
|
||||
});
|
||||
};
|
||||
|
||||
const api = Object.freeze({
|
||||
read: (wgsl) => {
|
||||
const byteSizeOf = (simpleType) => simpleTypes[simpleType]?.[1];
|
||||
|
||||
const structs = Object.freeze({
|
||||
from: (wgsl) => {
|
||||
if (typeof wgsl !== "string") {
|
||||
throw new Error("Input is not a string.");
|
||||
}
|
||||
const structLayouts = parseStructLayoutsFromShader(wgsl);
|
||||
return Object.fromEntries(Object.entries(structLayouts).map(([name, layout]) => [name, makeGenerator(layout, structLayouts)]));
|
||||
},
|
||||
byteSizeOf: (simpleType) => simpleTypes[simpleType][1] * BYTES_PER_ELEMENT,
|
||||
});
|
||||
|
||||
export default api;
|
||||
export { structs, byteSizeOf };
|
||||
Reference in New Issue
Block a user