UNPKG

typegpu

Version:

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

82 lines (81 loc) 2.56 kB
import {} from "../shared/meta.js"; import { isMarkedInternal } from "../shared/symbols.js"; import { vertexFormats } from "../shared/vertexFormat.js"; import * as wgsl from "./wgslTypes.js"; /** * Runtime function to extract the inner data type from decorated types. * If the data is not decorated, returns the data as-is. */ export function undecorate(data) { if (wgsl.isDecorated(data) || isLooseDecorated(data)) { return data.inner; } return data; } export function unptr(data) { if (wgsl.isPtr(data)) { return data.inner; } return data; } const looseTypeLiterals = ['unstruct', 'disarray', 'loose-decorated', ...vertexFormats]; export function isLooseData(data) { return isMarkedInternal(data) && looseTypeLiterals.includes(data?.type); } /** * Checks whether the passed in value is a disarray schema, * as opposed to, e.g., a regular array schema. * * Array schemas can be used to describe uniform and storage buffers, * whereas disarray schemas cannot. Disarrays are useful for * defining vertex buffers instead. * * @example * isDisarray(d.arrayOf(d.u32, 4)) // false * isDisarray(d.disarrayOf(d.u32, 4)) // true * isDisarray(d.vec3f) // false */ export function isDisarray(schema) { return isMarkedInternal(schema) && schema?.type === 'disarray'; } /** * Checks whether passed in value is a unstruct schema, * as opposed to, e.g., a struct schema. * * Struct schemas can be used to describe uniform and storage buffers, * whereas unstruct schemas cannot. Unstructs are useful for * defining vertex buffers instead. * * @example * isUnstruct(d.struct({ a: d.u32 })) // false * isUnstruct(d.unstruct({ a: d.u32 })) // true * isUnstruct(d.vec3f) // false */ export function isUnstruct(schema) { return isMarkedInternal(schema) && schema?.type === 'unstruct'; } export function isLooseDecorated(value) { return isMarkedInternal(value) && value?.type === 'loose-decorated'; } export function getCustomAlignment(data) { return data.attribs?.find(wgsl.isAlignAttrib) ?.params[0]; } export function getCustomSize(data) { return data.attribs?.find(wgsl.isSizeAttrib) ?.params[0]; } export function getCustomLocation(data) { return data.attribs?.find(wgsl.isLocationAttrib) ?.params[0]; } export function isData(value) { return wgsl.isWgslData(value) || isLooseData(value); } export const UnknownData = Symbol('UNKNOWN'); export class MatrixColumnsAccess { matrix; constructor(matrix) { this.matrix = matrix; } }