typegpu
Version:
A thin layer between JS and WebGPU/WGSL that improves development experience and allows for faster iteration.
50 lines (49 loc) • 1.9 kB
JavaScript
import { $gpuValueOf, $internal, $ownSnippet, $resolve } from "../../shared/symbols.js";
import { getName, setName } from "../../shared/meta.js";
import { textureExternal } from "../../data/texture.js";
import { valueProxyHandler } from "../valueProxyUtils.js";
import { inCodegenMode } from "../../execMode.js";
import { snip } from "../../data/snippet.js";
export function isExternalTexture(value) {
return value?.resourceType === 'external-texture';
}
// --------------
// Implementation
// --------------
export class TgpuExternalTextureImpl {
resourceType = 'external-texture';
[$internal] = true;
schema;
#membership;
constructor(schema, membership) {
this.schema = schema;
this.#membership = membership;
setName(this, membership.key);
}
[$resolve](ctx) {
const id = ctx.makeUniqueIdentifier(getName(this), 'global');
const group = ctx.allocateLayoutEntry(this.#membership.layout);
ctx.addDeclaration(` var ${id}: ${ctx.resolve(this.schema).value};`, id);
return snip(id, textureExternal(), 'handle');
}
get [$gpuValueOf]() {
const schema = this.schema;
return new Proxy({
[$internal]: true,
get [$ownSnippet]() {
return snip(this, schema, 'handle', false);
},
[$resolve]: (ctx) => ctx.resolve(this),
toString: () => `textureExternal:${getName(this) ?? '<unnamed>'}.$`,
}, valueProxyHandler);
}
get $() {
if (inCodegenMode()) {
return this[$gpuValueOf];
}
throw new Error('Direct access to texture views values is possible only as part of a compute dispatch or draw call. Try .read() or .write() instead');
}
toString() {
return `textureExternal:${getName(this) ?? '<unnamed>'}`;
}
}