typegpu
Version:
A thin layer between JS and WebGPU/WGSL that improves development experience and allows for faster iteration.
49 lines (48 loc) • 1.26 kB
JavaScript
import { getResolutionCtx } from "../../execMode.js";
import { getName, setName } from "../../shared/meta.js";
import { $gpuValueOf, $internal, $soul } from "../../shared/symbols.js";
import { getGpuValueRecursively } from "../valueProxyUtils.js";
// ----------
// Public API
// ----------
export function slot(defaultValue) {
return new TgpuSlotImpl(defaultValue);
}
// --------------
// Implementation
// --------------
class TgpuSlotImpl {
[$internal] = true;
[$soul];
resourceType = 'slot';
constructor(defaultValue = undefined) {
this[$soul] = {
type: 'slot',
defaultValue,
label: undefined,
};
}
get defaultValue() {
return this[$soul].defaultValue;
}
$name(label) {
setName(this, label);
return this;
}
areEqual(a, b) {
return Object.is(a, b);
}
toString() {
return `slot:${getName(this) ?? '<unnamed>'}`;
}
get [$gpuValueOf]() {
const ctx = getResolutionCtx();
if (!ctx) {
throw new Error(`Cannot access tgpu.slot's value outside of resolution.`);
}
return getGpuValueRecursively(ctx.unwrap(this));
}
get $() {
return this[$gpuValueOf];
}
}