in src/webgpu/shader/execution/expression/expression.ts [261:306]
function toStorage(ty: Type, expr: string, helpers: TypeConversionHelpers): string {
if (ty instanceof ScalarType) {
assert(
ty.kind !== 'abstract-int',
`'abstract-int' values have custom code for writing to storage`
);
assert(
ty.kind !== 'abstract-float',
`'abstract-float' values have custom code for writing to storage`
);
assert(ty.kind !== 'f64', `No storage type defined for 'f64' values`);
if (ty.kind === 'bool') {
return `select(0u, 1u, ${expr})`;
}
}
if (ty instanceof VectorType) {
assert(
ty.elementType.kind !== 'abstract-int',
`'abstract-int' values have custom code for writing to storage`
);
assert(
ty.elementType.kind !== 'abstract-float',
`'abstract-float' values have custom code for writing to storage`
);
assert(ty.elementType.kind !== 'f64', `'No storage type defined for 'f64' values`);
if (ty.elementType.kind === 'bool') {
return `select(vec${ty.width}<u32>(0u), vec${ty.width}<u32>(1u), ${expr})`;
}
}
if (ty instanceof ArrayType && elementTypeOf(ty) === Type.bool) {
// array<bool, N> -> array<u32, N>
const conv = helpers.uniqueID();
const outTy = Type.array(ty.count, Type.u32);
helpers.wgsl += `
fn ${conv}(in : ${ty}) -> ${outTy} {
var out : ${outTy};
for (var i = 0; i < ${ty.count}; i++) {
out[i] = select(0u, 1u, in[i]);
}
return out;
}
`;
return `${conv}(${expr})`;
}
return expr;
}