UNPKG

typegpu

Version:

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

138 lines (137 loc) 7.25 kB
import { type TgpuNamable } from '../shared/meta.ts'; import type { Infer, InferGPURecord, InferInput, InferInputRecord, InferPatch, InferPatchRecord, InferPartial, InferPartialRecord, InferRecord, IsValidVertexSchema, MemIdentityRecord } from '../shared/repr.ts'; import type { $gpuRepr, $inRepr, $invalidSchemaReason, $memIdent, $repr, $reprPartial, $reprPatch, $validVertexSchema } from '../shared/symbols.ts'; import type { Prettify } from '../shared/utilityTypes.ts'; import type { WgslExternalTexture, WgslStorageTexture, WgslTexture } from './texture.ts'; import type { Snippet } from './snippet.ts'; import type { PackedData } from './vertexFormatData.ts'; import * as wgsl from './wgslTypes.ts'; import type { WgslComparisonSampler, WgslSampler } from './sampler.ts'; import type { BaseData } from './wgslTypes.ts'; /** * Array schema constructed via `d.disarrayOf` function. * * Useful for defining vertex buffers. * Elements in the schema are not aligned in respect to their `byteAlignment`, * unless they are explicitly decorated with the custom align attribute * via `d.align` function. */ export interface Disarray<out TElement extends wgsl.BaseData = wgsl.BaseData> extends wgsl.BaseData { <T extends TElement>(elements: Infer<T>[]): Infer<T>[]; (): Infer<TElement>[]; readonly type: 'disarray'; readonly elementCount: number; readonly elementType: TElement; readonly [$repr]: Infer<TElement>[]; readonly [$inRepr]: readonly InferInput<TElement>[] | wgsl.TypedArrayFor<TElement>; readonly [$reprPartial]: { idx: number; value: InferPartial<TElement>; }[] | undefined; readonly [$reprPatch]: Record<number, InferPatch<TElement>> | InferInput<TElement>[] | wgsl.TypedArrayFor<TElement> | undefined; readonly [$validVertexSchema]: IsValidVertexSchema<TElement>; readonly [$invalidSchemaReason]: 'Disarrays are not host-shareable, use arrays instead'; } /** * Struct schema constructed via `d.unstruct` function. * * Useful for defining vertex buffers, as the standard layout restrictions do not apply. * Members are not aligned in respect to their `byteAlignment`, * unless they are explicitly decorated with the custom align attribute * via `d.align` function. */ export interface Unstruct<out TProps extends Record<string, wgsl.BaseData> = Record<string, wgsl.BaseData>> extends wgsl.BaseData, TgpuNamable { (props: Prettify<InferRecord<TProps>>): Prettify<InferRecord<TProps>>; (): Prettify<InferRecord<TProps>>; readonly type: 'unstruct'; readonly propTypes: TProps; readonly [$repr]: Prettify<InferRecord<TProps>>; readonly [$inRepr]: Prettify<InferInputRecord<TProps>>; readonly [$gpuRepr]: Prettify<InferGPURecord<TProps>>; readonly [$memIdent]: Unstruct<Prettify<MemIdentityRecord<TProps>>>; readonly [$reprPartial]: Prettify<Partial<InferPartialRecord<TProps>>> | undefined; readonly [$reprPatch]: Prettify<Partial<InferPatchRecord<TProps>>> | undefined; readonly [$validVertexSchema]: { [K in keyof TProps]: IsValidVertexSchema<TProps[K]>; }[keyof TProps] extends true ? true : false; readonly [$invalidSchemaReason]: 'Unstructs are not host-shareable, use structs instead'; } /** @deprecated Just use `Unstruct` without any type parameters */ export type AnyUnstruct = Unstruct; export interface LooseDecorated<out TInner extends wgsl.BaseData = wgsl.BaseData, out TAttribs extends unknown[] = unknown[]> extends wgsl.BaseData { readonly type: 'loose-decorated'; readonly inner: TInner; readonly attribs: TAttribs; readonly [$repr]: Infer<TInner>; readonly [$invalidSchemaReason]: 'Loosely decorated schemas are not host-shareable'; readonly [$validVertexSchema]: IsValidVertexSchema<TInner>; } /** * Type utility to extract the inner type from decorated types. */ export type Undecorate<T> = T extends { readonly type: 'decorated' | 'loose-decorated'; readonly inner: infer TInner; } ? TInner : T; /** * Type utility to undecorate all values in a record. */ export type UndecorateRecord<T extends Record<string, unknown>> = { [Key in keyof T]: Undecorate<T[Key]>; }; /** * Runtime function to extract the inner data type from decorated types. * If the data is not decorated, returns the data as-is. */ export declare function undecorate(data: BaseData): BaseData; export declare function unptr(data: BaseData): BaseData; export declare function unptr(data: BaseData | UnknownData): BaseData | UnknownData; declare const looseTypeLiterals: readonly ["unstruct", "disarray", "loose-decorated", "uint8", "uint8x2", "uint8x4", "sint8", "sint8x2", "sint8x4", "unorm8", "unorm8x2", "unorm8x4", "snorm8", "snorm8x2", "snorm8x4", "uint16", "uint16x2", "uint16x4", "sint16", "sint16x2", "sint16x4", "unorm16", "unorm16x2", "unorm16x4", "snorm16", "snorm16x2", "snorm16x4", "float16", "float16x2", "float16x4", "float32", "float32x2", "float32x3", "float32x4", "uint32", "uint32x2", "uint32x3", "uint32x4", "sint32", "sint32x2", "sint32x3", "sint32x4", "unorm10-10-10-2", "unorm8x4-bgra"]; export type LooseTypeLiteral = (typeof looseTypeLiterals)[number]; export type IsLooseData<T> = T extends { readonly type: LooseTypeLiteral; } ? true : false; export type AnyLooseData = Disarray | Unstruct | LooseDecorated | PackedData; export declare function isLooseData(data: unknown): data is AnyLooseData; /** * 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 declare function isDisarray(schema: unknown): schema is 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 declare function isUnstruct(schema: unknown): schema is Unstruct; export declare function isLooseDecorated(value: unknown): value is LooseDecorated; export declare function getCustomAlignment(data: wgsl.BaseData): number | undefined; export declare function getCustomSize(data: wgsl.BaseData): number | undefined; export declare function getCustomLocation(data: wgsl.BaseData): number | undefined; export declare function isData(value: unknown): value is AnyData; export type AnyData = wgsl.AnyWgslData | AnyLooseData; export type AnyConcreteData = Exclude<AnyData, wgsl.AbstractInt | wgsl.AbstractFloat | wgsl.Void | WgslTexture | WgslStorageTexture | WgslExternalTexture | WgslSampler | WgslComparisonSampler>; export declare const UnknownData: unique symbol; export type UnknownData = typeof UnknownData; export declare class MatrixColumnsAccess { readonly matrix: Snippet; constructor(matrix: Snippet); } export {};