I am migrating my previous BabylonJS project to Babylon Lite. And my shader material was using uniform array<vec4<f32>, 8> . And Lite doesn’t support this type.
So the modification is:
export type ShaderUniformType = "f32" | "u32" | "i32" | "vec2<f32>" | "vec3<f32>" | "vec4<f32>" | "mat4x4<f32>" | `array<vec4<f32>, ${number}>`;
export type WgslScalarType =
| "f32"
| "u32"
| "i32"
| "vec2<f32>"
| "vec3<f32>"
| "vec4<f32>"
| "vec4<u32>"
| "mat4x4<f32>"
| `array<vec4<u32>, ${number}>`
| `array<vec4<f32>, ${number}>`;
function isUniformType(type: string): type is ShaderUniformType {
return (
type === "f32" ||
type === "u32" ||
type === "i32" ||
type === "vec2<f32>" ||
type === "vec3<f32>" ||
type === "vec4<f32>" ||
type === "mat4x4<f32>" ||
/^array<vec4<f32>,\s*\d+>$/.test(type)
);
}
function elementCount(type: ShaderUniformType): number {
switch (type) {
case "f32":
case "u32":
case "i32":
return 1;
case "vec2<f32>":
return 2;
case "vec3<f32>":
return 3;
case "vec4<f32>":
return 4;
case "mat4x4<f32>":
return 16;
default: {
const m = /^array<vec4<f32>,\s*(\d+)>$/.exec(type);
if (m) {
return Number(m[1]) * 4;
}
throw new Error(`ShaderMaterial: unsupported uniform type "${String(type)}".`);
}
}
}
I’ve finished the feature(including doc modification and tests) but I want to know your opinion on contributing to Babylon Lite, before creating a PR, especially on this kind of small enhancements because it doesn’t have a direct downstream consumer in Lite. Another thing is it seems this is a spec driven project, and what kind of constraints needed to express this(and future extensions) so that agents won’t drift away is not yet thoroughly considered.
And while writing the post I realise that there could be many basic combinations of uniform types if keeping on the current path… And not to mention the user defined struct. A proper recursive compute layout function is probably needed.
I’d like to know your opinions!
Edit: I was uploading f16 vertex attributes to GPU without conversion. That is another type to add in towards combinatoric explosion. And I can’t find the document that restraints agents to only use f32 vertex attributes? And since the minimum node version stays 18, I guess we can’t have native f16 conversion in Lite as well. Previous feature PR for BabylonJS: Support half-float (float16) vertex attribute type ( float16x2/float16x4 on WebGPU)