typegpu
Version:
A thin layer between JS and WebGPU/WGSL that improves development experience and allows for faster iteration.
30 lines (29 loc) • 1.03 kB
JavaScript
import { isBuiltin } from "../../data/attributes.js";
import { isVoid, isWgslStruct } from "../../data/wgslTypes.js";
function isColorAttachment(value) {
return !!value?.view;
}
export function connectAttachmentToShader(fragmentOut, attachment) {
if (isVoid(fragmentOut) || isBuiltin(fragmentOut)) {
return [];
}
if (isWgslStruct(fragmentOut)) {
const result = [];
for (const key of Object.keys(fragmentOut.propTypes)) {
const outputValue = fragmentOut.propTypes[key];
if (isBuiltin(outputValue)) {
continue;
}
const matching = attachment[key];
if (!matching) {
throw new Error(`A color attachment by the name of '${key}' was not provided to the shader.`);
}
result.push(matching);
}
return result;
}
if (!isColorAttachment(attachment)) {
throw new Error('Expected a single color attachment, not a record.');
}
return [attachment];
}