UNPKG

typegpu

Version:

A thin layer between JS and WebGPU/WGSL that improves development experience and allows for faster iteration.

29 lines (28 loc) 1.09 kB
import { safeStringify } from "./stringify.js"; /** * The values of ExternalsV2 are zero-argument functions for accessing the value. * Since they would be recognized by the WgslGenerator as regular, non 'use gpu' functions, * we turn them into getters. * * @example * normalizeExternalsV2({ "ext.prop": () => ext.prop; }); // { get "ext.prop"() => ext.prop; } */ function normalizeExternalsV2(externals) { const result = {}; for (const [key, value] of Object.entries(externals)) { Object.defineProperty(result, key, { get: value, enumerable: true }); } return result; } export function normalizeMetadata(meta) { if (meta.v === 1) { const rawExternals = meta.externals; const externals = typeof rawExternals === 'function' ? rawExternals : () => rawExternals; return { ...meta, externals }; } if (meta.v === 2) { const externals = normalizeExternalsV2(meta?.externals); return { ...meta, externals: () => externals }; } throw new Error(`Unrecognized TypeGPU metadata format: ${safeStringify(meta)}`); }