UNPKG

typegpu

Version:

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

56 lines (55 loc) 2.5 kB
import type { AnyData } from '../../data/dataTypes.ts'; import { type Origin } from '../../data/snippet.ts'; import type { BaseData } from '../../data/wgslTypes.ts'; import type { InferGPU } from '../../shared/repr.ts'; import { $gpuValueOf } from '../../shared/symbols.ts'; /** * Extra declaration that will be included in final WGSL code * when resolving objects that use it. */ export interface TgpuRawCodeSnippet<TDataType extends BaseData> { $: InferGPU<TDataType>; readonly [$gpuValueOf]: InferGPU<TDataType>; $uses(dependencyMap: Record<string, unknown>): this; } export type RawCodeSnippetOrigin = Exclude<Origin, 'function' | 'local-def' | 'argument' | 'constant-immutable-def' | 'runtime-immutable-def'>; /** * An advanced API that creates a typed shader expression which * can be injected into the final shader bundle upon use. * * @param expression The code snippet that will be injected in place of `foo.$` * @param type The type of the expression * @param [origin='runtime'] Where the value originates from. * @param [possibleSideEffects=true] Whether generating this snippet may produce a WGSL expression with observable side-effects (e.g. calling a barrier, discarding a fragment, or writing to memory). * * **-- Which origin to choose?** * * Usually 'runtime' (the default) is a safe bet, but if you're sure that the expression or * computation is constant (either a reference to a constant, a numeric literal, * or an operation on constants), then pass 'constant' as it might lead to better * optimizations. * * If what the expression is a direct reference to an existing value (e.g. a uniform, a * storage binding, ...), then choose from 'uniform', 'mutable', 'readonly', 'workgroup', * 'private' or 'handle' depending on the address space of the referred value. * * @example * ```ts * // An identifier that we know will be in the * // final shader bundle, but we cannot * // refer to it in any other way. * const existingGlobal = tgpu['~unstable'] * .rawCodeSnippet('EXISTING_GLOBAL', d.f32, 'constant', false); * * const foo = () => { * 'use gpu'; * return existingGlobal.$ * 2; * }; * * const wgsl = tgpu.resolve([foo]); * // fn foo() -> f32 { * // return EXISTING_GLOBAL * 2; * // } * ``` */ export declare function rawCodeSnippet<TDataType extends AnyData>(expression: string, type: TDataType, origin?: RawCodeSnippetOrigin | undefined, possibleSideEffects?: boolean | undefined): TgpuRawCodeSnippet<TDataType>;