software-mansion / TypeGPU

TypeGPU is a TypeScript library that enhances the WebGPU API, allowing resource management in a type-safe, declarative way.
http://typegpu.com
MIT License
82 stars 0 forks source link

Make @typegpu/tgsl-tools work without the access to the JS runtime. #308

Open iwoplaza opened 1 week ago

iwoplaza commented 1 week ago

Currently, the transpileFn function exported by @typegpu/tgsl-tools receives a context with the argTypes, returnType and an externalMap.

type Context = {
  argTypes: AnyTgpuData[];
  externalMap: Record<string, Wgsl>;
  returnType: AnyTgpuData | undefined;
};

function transpileFn(ctx: Context, jsCode: string): { head: Wgsl, body: Wgsl } {}

This makes it impossible to use in a Vite plugin, since we cannot statically extract the value of what is passed to the shell: tgpu.fn(...). To fix this, transpileFn has to construct a data structure that encodes the WGSL AST, along with placeholders for external values.