typegpu
Version:
A thin layer between JS and WebGPU/WGSL that improves development experience and allows for faster iteration.
38 lines (37 loc) • 1.47 kB
JavaScript
import { $cast, $gpuCallable } from "../shared/symbols.js";
import { hasCast, isGPUCallable } from "../types.js";
/**
* A wrapper for `schema(item)` or `schema()` call on JS side.
* If the schema is a pointer, returns the value pointed to without copying.
* If the schema is a TgpuVertexFormatData, calls the corresponding constructible schema instead.
* If the schema is not callable, throws an error.
* Otherwise, returns `schema(item)` or `schema()`.
*/
export function schemaCallWrapper(schema, item) {
const callSchema = schema;
if (hasCast(callSchema)) {
return callSchema[$cast](item);
}
if (typeof callSchema !== 'function') {
// Not callable
return item;
}
return item === undefined ? callSchema() : callSchema(item);
}
/**
* A wrapper for `schema(item)` or `schema()` call on the GPU side.
* If the schema is a pointer, returns the value pointed to without copying.
* If the schema is a TgpuVertexFormatData, calls the corresponding constructible schema instead.
* If the schema is not callable, throws an error.
* Otherwise, returns `schema(item)` or `schema()`.
*/
export function schemaCallWrapperGPU(ctx, schema, item) {
if (!isGPUCallable(schema)) {
// Not callable
return item;
}
const callSchema = schema;
return item === undefined || item.value === undefined
? callSchema[$gpuCallable].call(ctx, [])
: callSchema[$gpuCallable].call(ctx, [item]);
}