typegpu
Version:
A thin layer between JS and WebGPU/WGSL that improves development experience and allows for faster iteration.
83 lines (82 loc) • 2.94 kB
JavaScript
import { builtin } from "../../builtin.js";
import { AutoStruct } from "../../data/autoStruct.js";
import { vec4f } from "../../data/vector.js";
import { getName, setName } from "../../shared/meta.js";
import { $internal, $resolve } from "../../shared/symbols.js";
import { shaderStageSlot } from "../slot/internalSlots.js";
import { createFnCore } from "./fnCore.js";
const builtinVertexIn = {
$vertexIndex: builtin.vertexIndex,
$instanceIndex: builtin.instanceIndex,
};
const builtinVertexOut = {
$clipDistances: builtin.clipDistances,
$position: builtin.position,
};
const builtinFragmentIn = {
$position: builtin.position,
$frontFacing: builtin.frontFacing,
$primitiveIndex: builtin.primitiveIndex,
$sampleIndex: builtin.sampleIndex,
$sampleMask: builtin.sampleMask,
$subgroupInvocationId: builtin.subgroupInvocationId,
$subgroupSize: builtin.subgroupSize,
};
const builtinFragmentOut = {
$fragDepth: builtin.fragDepth,
$sampleMask: builtin.sampleMask,
};
/**
* Only used internally. Meant to be recreated on every resolution.
*/
export class AutoFragmentFn {
#core;
autoIn;
autoOut;
constructor(impl, varyings, locations) {
// If the implementation is not named, we can fallback to "fragmentFn"
if (!getName(impl)) {
setName(impl, 'fragmentFn');
}
this.#core = createFnCore(impl, 'fragment');
this.autoIn = new AutoStruct({ ...builtinFragmentIn, ...varyings }, undefined, locations);
setName(this.autoIn, 'FragmentIn');
this.autoOut = new AutoStruct(builtinFragmentOut, vec4f);
setName(this.autoOut, 'FragmentOut');
}
toString() {
return 'autoFragmentFn';
}
[$resolve](ctx) {
return ctx.withSlots([[shaderStageSlot, 'fragment']], () => this.#core.resolve(ctx, [this.autoIn], this.autoOut));
}
}
AutoFragmentFn.prototype[$internal] = true;
AutoFragmentFn.prototype.resourceType = 'auto-fragment-fn';
/**
* Only used internally. Meant to be recreated on every resolution.
*/
export class AutoVertexFn {
#core;
autoIn;
autoOut;
constructor(impl, attribs, locations) {
// If the implementation is not named, we can fallback to "vertexFn"
if (!getName(impl)) {
setName(impl, 'vertexFn');
}
this.#core = createFnCore(impl, 'vertex');
this.autoIn = new AutoStruct({ ...builtinVertexIn, ...attribs }, undefined, locations);
setName(this.autoIn, 'VertexIn');
this.autoOut = new AutoStruct(builtinVertexOut, undefined);
setName(this.autoOut, 'VertexOut');
}
toString() {
return 'autoVertexFn';
}
[$resolve](ctx) {
return ctx.withSlots([[shaderStageSlot, 'vertex']], () => this.#core.resolve(ctx, [this.autoIn], this.autoOut));
}
}
AutoVertexFn.prototype[$internal] = true;
AutoVertexFn.prototype.resourceType = 'auto-vertex-fn';