@vgpu/wgsl1 symbolView source ↗

Symbols in this topic

compile

Converts one raw runtime WGSL string into the data-only ResolvedShader shape used by @vgpu/wgsl. Use it when the shader source is already a complete WGSL entry module and does not need import resolution.

Import

TypeScript
1
import { compile } from "@vgpu/wgsl";

Signature

TypeScript
1
2
3
import type { ResolvedShader } from "@vgpu/wgsl";
 
declare function compile(wgsl: string): ResolvedShader;

Parameters

ParamTypeRequiredDefaultNotes
wgslstringComplete WGSL source for one runtime shader. It is copied byte-for-byte to resolved.wgsl. Top-level import syntax is rejected; use resolveShader or a build-time loader for WGSL import graphs.

Returns: ResolvedShader — a data object with kind: "wgsl", the original wgsl, passthrough source/AST/source-map metadata, deterministic cacheKey, detected entryPoints, and source stats.

Throws: VGPU-WGSL-RUNTIME-IMPORT when the trimmed source starts with a top-level import statement after comments are stripped — remove the import, pre-resolve with resolveShader, or use the Vite/webpack loader.

Examples

TypeScript
1
2
3
4
5
6
7
8
9
10
11
import { compile } from "@vgpu/wgsl";
 
const shader = compile(`
@compute @workgroup_size(1)
fn main() {
}
`);
 
shader.kind satisfies "wgsl";
shader.entryPoints satisfies readonly string[];
console.log(shader.entryPoints.includes("main"));
TypeScript
1
2
3
4
5
6
7
8
9
import { compile } from "@vgpu/wgsl";
 
try {
  compile(`import { helper } from "./helper.wgsl";`);
} catch (error) {
  if (error instanceof Error && "code" in error) {
    console.log(error.code === "VGPU-WGSL-RUNTIME-IMPORT");
  }
}

Notes

  • compile() does not read files, resolve packages, mangle imported symbols, reflect binding layouts, or validate WGSL semantics.
  • The only syntax check is import rejection. Invalid WGSL without import still returns a ResolvedShader; WebGPU validation happens later when a shader module is created.
  • stats.bindGroups is 0 in this runtime passthrough shape. Do not use it as reflection for resource bindings.
  • Runtime WGSL modules with resources are allowed because there is no import graph. For imported WGSL modules, keep modules pure and declare every @group/@binding resource in the entry module.
  • See also: ResolvedShader, ShaderSource, resolveShader.