wesl-tooling 0.6.3 → 0.6.5

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/dist/index.d.ts CHANGED
@@ -1,5 +1,5 @@
1
- import { VirtualLibraryFn, WeslBundle, WeslDevice } from "wesl";
2
- import { GPUElementFormat } from "thimbleberry";
1
+ import { VirtualLibraryFn, WeslBundle } from "wesl";
2
+ import { WgslElementType, WgslElementType as WgslElementType$1 } from "thimbleberry";
3
3
 
4
4
  //#region src/ParseDependencies.d.ts
5
5
  /**
@@ -42,6 +42,18 @@ declare function dependencyBundles(weslSrc: Record<string, string>, projectDir:
42
42
 
43
43
  //#endregion
44
44
  //#region src/SimpleComputeShader.d.ts
45
+ interface CompileShaderParams {
46
+ /** The project directory, used for resolving dependencies. */
47
+ projectDir: string;
48
+ /** The GPUDevice to use for shader compilation. */
49
+ device: GPUDevice;
50
+ /** The WGSL/WESL shader source code. */
51
+ src: string;
52
+ /** Optional conditions for shader compilation. */
53
+ conditions?: Record<string, boolean>;
54
+ /** Optional virtual libraries to include in the shader. */
55
+ virtualLibs?: Record<string, VirtualLibraryFn>;
56
+ }
45
57
  /**
46
58
  * Compiles a single WESL shader source string into a GPUShaderModule for testing
47
59
  * with automatic package detection.
@@ -51,11 +63,11 @@ declare function dependencyBundles(weslSrc: Record<string, string>, projectDir:
51
63
  * bundle to include in the link.
52
64
  *
53
65
  * @param projectDir - The project directory, used for resolving dependencies.
54
- * @param device - The WeslDevice to use for shader compilation.
66
+ * @param device - The GPUDevice to use for shader compilation.
55
67
  * @param src - The WESL shader source code.
56
68
  * @returns A Promise that resolves to the compiled GPUShaderModule.
57
69
  */
58
- declare function compileShader(projectDir: string, device: WeslDevice, src: string, virtualLibs?: Record<string, VirtualLibraryFn>): Promise<GPUShaderModule>;
70
+ declare function compileShader(params: CompileShaderParams): Promise<GPUShaderModule>;
59
71
  /**
60
72
  * Transpiles and runs a simple compute shader on the GPU for testing.
61
73
  *
@@ -72,7 +84,7 @@ declare function compileShader(projectDir: string, device: WeslDevice, src: stri
72
84
  * @param resultFormat - format for interpreting the result buffer data. (default u32)
73
85
  * @returns storage result array (typically four numbers if the buffer format is u32 or f32)
74
86
  */
75
- declare function testComputeShader(projectDir: string, gpu: GPU, src: string, resultFormat?: GPUElementFormat): Promise<number[]>;
87
+ declare function testComputeShader(projectDir: string, gpu: GPU, src: string, resultFormat: WgslElementType$1, conditions?: Record<string, boolean>): Promise<number[]>;
76
88
  /**
77
89
  * Transpiles and runs a simple compute shader on the GPU for testing.
78
90
  *
@@ -89,7 +101,7 @@ declare function testComputeShader(projectDir: string, gpu: GPU, src: string, re
89
101
  * @param resultFormat - format for interpreting the result buffer data. (default u32)
90
102
  * @returns storage result array
91
103
  */
92
- declare function runSimpleComputePipeline(device: GPUDevice, module: GPUShaderModule, resultFormat?: GPUElementFormat): Promise<number[]>;
104
+ declare function runSimpleComputePipeline(device: GPUDevice, module: GPUShaderModule, resultFormat?: WgslElementType$1): Promise<number[]>;
93
105
 
94
106
  //#endregion
95
- export { compileShader, dependencyBundles, parseDependencies, runSimpleComputePipeline, testComputeShader };
107
+ export { CompileShaderParams, WgslElementType, compileShader, dependencyBundles, parseDependencies, runSimpleComputePipeline, testComputeShader };
package/dist/index.js CHANGED
@@ -2,7 +2,7 @@ import { resolve } from "import-meta-resolve";
2
2
  import path from "node:path";
3
3
  import { pathToFileURL } from "node:url";
4
4
  import { WeslParseError, filterMap, findUnboundIdents, link, parseIntoRegistry, parsedRegistry, requestWeslDevice } from "wesl";
5
- import { copyBuffer } from "thimbleberry";
5
+ import { copyBuffer, elementStride } from "thimbleberry";
6
6
 
7
7
  //#region src/ParseDependencies.ts
8
8
  /**
@@ -95,17 +95,19 @@ const resultBufferSize = 16;
95
95
  * bundle to include in the link.
96
96
  *
97
97
  * @param projectDir - The project directory, used for resolving dependencies.
98
- * @param device - The WeslDevice to use for shader compilation.
98
+ * @param device - The GPUDevice to use for shader compilation.
99
99
  * @param src - The WESL shader source code.
100
100
  * @returns A Promise that resolves to the compiled GPUShaderModule.
101
101
  */
102
- async function compileShader(projectDir, device, src, virtualLibs) {
102
+ async function compileShader(params) {
103
+ const { projectDir, device, src, conditions, virtualLibs } = params;
103
104
  const weslSrc = { main: src };
104
105
  const libs = await dependencyBundles(weslSrc, projectDir);
105
106
  const linked = await link({
106
107
  weslSrc,
107
108
  libs,
108
- virtualLibs
109
+ virtualLibs,
110
+ conditions
109
111
  });
110
112
  return device.createShaderModule({ code: linked.dest });
111
113
  }
@@ -125,26 +127,27 @@ async function compileShader(projectDir, device, src, virtualLibs) {
125
127
  * @param resultFormat - format for interpreting the result buffer data. (default u32)
126
128
  * @returns storage result array (typically four numbers if the buffer format is u32 or f32)
127
129
  */
128
- async function testComputeShader(projectDir, gpu, src, resultFormat = "f32") {
130
+ async function testComputeShader(projectDir, gpu, src, resultFormat, conditions = {}) {
129
131
  const adapter = await gpu.requestAdapter();
130
132
  const device = await requestWeslDevice(adapter);
131
133
  try {
132
- const arraySize = resultBufferSize / elementByteSize(resultFormat);
134
+ const arraySize = resultBufferSize / elementStride(resultFormat);
133
135
  const arrayType = `array<${resultFormat}, ${arraySize}>`;
134
136
  const virtualLibs = { test: () => `@group(0) @binding(0) var <storage, read_write> results: ${arrayType};` };
135
- const module = await compileShader(projectDir, device, src, virtualLibs);
137
+ const params = {
138
+ projectDir,
139
+ device,
140
+ src,
141
+ conditions,
142
+ virtualLibs
143
+ };
144
+ const module = await compileShader(params);
136
145
  const result = await runSimpleComputePipeline(device, module, resultFormat);
137
146
  return result;
138
147
  } finally {
139
148
  device.destroy();
140
149
  }
141
150
  }
142
- /** size in bytes of a wgsl numeric type, e.g. 'f32' => 4 */
143
- function elementByteSize(fmt) {
144
- const found = fmt.match(/\d+/);
145
- const bits = Number.parseInt(found?.[0]);
146
- return bits / 8;
147
- }
148
151
  /**
149
152
  * Transpiles and runs a simple compute shader on the GPU for testing.
150
153
  *
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "wesl-tooling",
3
- "version": "0.6.3",
3
+ "version": "0.6.5",
4
4
  "type": "module",
5
5
  "files": [
6
6
  "dist"
@@ -10,7 +10,7 @@
10
10
  "types": "./dist/index.d.ts",
11
11
  "dependencies": {
12
12
  "import-meta-resolve": "^4.1.0",
13
- "thimbleberry": "^0.2.9"
13
+ "thimbleberry": "^0.2.10"
14
14
  },
15
15
  "devDependencies": {
16
16
  "dependent_package": "x",