typegpu
Version:
A thin layer between JS and WebGPU/WGSL that improves development experience and allows for faster iteration.
56 lines (55 loc) • 2.13 kB
JavaScript
import { snip } from "../../data/snippet.js";
import {} from "../../data/wgslTypes.js";
import { makeDereferenceable } from "../../tgsl/makeDereferenceable.js";
import { makeResolvable } from "../../tgsl/makeResolvable.js";
import { getName, setName } from "../../shared/meta.js";
import { $gpuValueOf, $internal, $repr, $resolve } from "../../shared/symbols.js";
/**
* A class representing a buffer accessed via a BindGroupLayout.
* Compared to a regular buffer, it's missing read/write methods,
* but it holds a membership.
*/
export class TgpuLaidOutBufferImpl {
[$internal];
usage;
dataType;
#membership;
static {
TgpuLaidOutBufferImpl.prototype.resourceType = 'laid-out-buffer';
makeDereferenceable(makeResolvable(TgpuLaidOutBufferImpl.prototype, {
asString() {
return `${this.usage}:${getName(this) ?? '<unnamed>'}`;
},
resolve(ctx) {
const id = ctx.makeUniqueIdentifier(getName(this), 'global');
const group = ctx.allocateLayoutEntry(this.#membership.layout);
return ctx.gen.declareGlobalVar({
group,
binding: this.#membership.idx,
scope: this.usage,
id,
dataType: this.dataType,
init: undefined,
});
},
}), {
codegenMode: {
getBaseSnippet(trackingProxy) {
return snip(trackingProxy, this.dataType, this.usage, /* possibleSideEffects */ false);
},
},
normalMode: {
get() {
throw new Error('Direct access to buffer values is possible only as part of a compute dispatch or draw call. Try .read() or .write() instead');
},
},
});
}
constructor(usage, dataType, membership) {
this[$internal] = { dataType };
this.usage = usage;
this.dataType = dataType;
this.#membership = membership;
setName(this, membership.key);
}
}