unplugin-typegpu 0.10.2 → 0.11.0
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/README.md +14 -4
- package/babel-Bnfeonoo.mjs +126 -0
- package/babel-C0RyQRMk.d.mts +14 -0
- package/babel-C1TaJd38.d.cts +14 -0
- package/babel-izRcz0Uk.cjs +134 -0
- package/babel.cjs +6 -161
- package/babel.d.cts +2 -10
- package/babel.d.mts +2 -11
- package/babel.mjs +2 -158
- package/bun-9JrHWE4u.d.mts +6 -0
- package/bun-CUGitP44.cjs +147 -0
- package/bun-ChNeklBe.d.cts +6 -0
- package/bun-DFr_PiLh.mjs +132 -0
- package/bun.cjs +6 -39
- package/bun.d.cts +1 -4
- package/bun.d.mts +1 -5
- package/bun.mjs +2 -36
- package/common-BKqBivuE.d.cts +84 -0
- package/common-BvAFT5IV.d.mts +85 -0
- package/common-Ccx_dEqH.cjs +329 -0
- package/common-e08X6w0a.mjs +272 -0
- package/esbuild.cjs +6 -6
- package/esbuild.d.cts +1 -1
- package/esbuild.d.mts +1 -1
- package/esbuild.mjs +2 -4
- package/farm.cjs +6 -6
- package/farm.d.cts +1 -1
- package/farm.d.mts +1 -1
- package/farm.mjs +2 -4
- package/index-BuKVXaph.d.mts +48 -0
- package/index-HrLja1mV.d.cts +48 -0
- package/index.cjs +19 -29
- package/index.d.cts +5 -17
- package/index.d.mts +5 -17
- package/index.mjs +4 -17
- package/package.json +11 -8
- package/rolldown.cjs +6 -6
- package/rolldown.d.cts +1 -1
- package/rolldown.d.mts +1 -1
- package/rolldown.mjs +2 -4
- package/rollup.cjs +6 -6
- package/rollup.d.cts +1 -1
- package/rollup.d.mts +1 -1
- package/rollup.mjs +2 -4
- package/rspack.cjs +6 -6
- package/rspack.d.cts +1 -1
- package/rspack.d.mts +1 -1
- package/rspack.mjs +2 -4
- package/src-2vvPp2dY.cjs +77 -0
- package/src-BV9Uh6WO.mjs +17 -0
- package/vite.cjs +6 -6
- package/vite.d.cts +1 -1
- package/vite.d.mts +1 -1
- package/vite.mjs +2 -4
- package/webpack.cjs +6 -6
- package/webpack.d.cts +1 -1
- package/webpack.d.mts +1 -1
- package/webpack.mjs +2 -4
- package/common-CRPQ2KE6.d.mts +0 -25
- package/common-Dpl8jppn.d.cts +0 -25
- package/filter-6T-5w4kG.mjs +0 -234
- package/filter-CKmD4HRk.cjs +0 -327
- package/index-legacy-DRI9LQiG.d.mts +0 -1488
- package/index-legacy-xKi5C2ga.d.cts +0 -1488
- package/rolldown-browser-DKeiFM_e.mjs +0 -129
- package/rolldown-browser-DUanNY-R.cjs +0 -141
- package/rolldown-browser-JYqfNLQ8.d.mts +0 -1247
- package/rollup-impl-DqC_d-_W.d.cts +0 -1246
|
@@ -0,0 +1,85 @@
|
|
|
1
|
+
import { FilterPattern } from "unplugin";
|
|
2
|
+
import { MagicStringAST } from "magic-string-ast";
|
|
3
|
+
import { NodePath } from "@babel/traverse";
|
|
4
|
+
import { transpileFn } from "tinyest-for-wgsl";
|
|
5
|
+
import * as t from "@babel/types";
|
|
6
|
+
|
|
7
|
+
//#region src/core/common.d.ts
|
|
8
|
+
interface Options {
|
|
9
|
+
/** @default [/\.m?[jt]sx?$/] */
|
|
10
|
+
include?: FilterPattern;
|
|
11
|
+
/** @default undefined */
|
|
12
|
+
exclude?: FilterPattern;
|
|
13
|
+
/** @default undefined */
|
|
14
|
+
enforce?: 'post' | 'pre' | undefined;
|
|
15
|
+
/** @default undefined */
|
|
16
|
+
forceTgpuAlias?: string | undefined;
|
|
17
|
+
/** @default true */
|
|
18
|
+
autoNamingEnabled?: boolean | undefined;
|
|
19
|
+
/**
|
|
20
|
+
* Skipping files that don't contain "typegpu", "tgpu" or "use gpu".
|
|
21
|
+
* In case this early pruning hinders transformation, you
|
|
22
|
+
* can disable it.
|
|
23
|
+
*
|
|
24
|
+
* @default true
|
|
25
|
+
*/
|
|
26
|
+
earlyPruning?: boolean | undefined;
|
|
27
|
+
}
|
|
28
|
+
type MetadatableFunction = t.FunctionDeclaration | t.FunctionExpression | t.ArrowFunctionExpression;
|
|
29
|
+
interface TransformMethods {
|
|
30
|
+
warn(message: string): void;
|
|
31
|
+
/**
|
|
32
|
+
* For function statements to have metadata assigned, they first have to be manually hoisted
|
|
33
|
+
* to the top of their scope, then replaced with a variable declaration.
|
|
34
|
+
*
|
|
35
|
+
* @example
|
|
36
|
+
* ```ts
|
|
37
|
+
* const value = mod(13, 10);
|
|
38
|
+
* function mod(a: number, b: number): number {
|
|
39
|
+
* 'use gpu';
|
|
40
|
+
* return a % b;
|
|
41
|
+
* }
|
|
42
|
+
* ```
|
|
43
|
+
*
|
|
44
|
+
* Should be transformed to:
|
|
45
|
+
*
|
|
46
|
+
* ```ts
|
|
47
|
+
* const mod = (a: number, b: number): number => {
|
|
48
|
+
* 'use gpu';
|
|
49
|
+
* return a % b;
|
|
50
|
+
* };
|
|
51
|
+
* const value = mod(13, 10);
|
|
52
|
+
* ```
|
|
53
|
+
*
|
|
54
|
+
* Note that named function expressions aren't hoisted, so they don't pose a problem.
|
|
55
|
+
* ```ts
|
|
56
|
+
* double(2); // Uncaught ReferenceError: double is not defined
|
|
57
|
+
* console.log(function double(a) { return a * 2; });
|
|
58
|
+
* ```
|
|
59
|
+
*/
|
|
60
|
+
assignMetadata(this: PluginState, path: NodePath<MetadatableFunction>, name: string | undefined, ast: ReturnType<typeof transpileFn>): void;
|
|
61
|
+
wrapInAutoName(this: PluginState, path: NodePath<t.Expression>, name: string): void;
|
|
62
|
+
replaceWithAssignmentOverload(path: NodePath<t.AssignmentExpression>, runtimeFn: string): void;
|
|
63
|
+
replaceWithBinaryOverload(path: NodePath<t.BinaryExpression>, runtimeFn: string): void;
|
|
64
|
+
}
|
|
65
|
+
interface PluginState extends TransformMethods {
|
|
66
|
+
/**
|
|
67
|
+
* How the `tgpu` object is used in code. Since it can be aliased, we
|
|
68
|
+
* need to catch that and act accordingly.
|
|
69
|
+
*/
|
|
70
|
+
tgpuAliases: Set<string>;
|
|
71
|
+
autoNamingEnabled: boolean;
|
|
72
|
+
/**
|
|
73
|
+
* Populated by Babel
|
|
74
|
+
*/
|
|
75
|
+
filename?: string | undefined;
|
|
76
|
+
/**
|
|
77
|
+
* In Babel, options are assigned to the property `opts` on the plugin state.
|
|
78
|
+
* We use this pattern everywhere for consistency.
|
|
79
|
+
*/
|
|
80
|
+
opts: Options;
|
|
81
|
+
inUseGpuScope: boolean;
|
|
82
|
+
alreadyTransformed: WeakSet<t.Node>;
|
|
83
|
+
}
|
|
84
|
+
//#endregion
|
|
85
|
+
export { PluginState as n, Options as t };
|
|
@@ -0,0 +1,329 @@
|
|
|
1
|
+
//#region \0rolldown/runtime.js
|
|
2
|
+
var __create = Object.create;
|
|
3
|
+
var __defProp = Object.defineProperty;
|
|
4
|
+
var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
|
|
5
|
+
var __getOwnPropNames = Object.getOwnPropertyNames;
|
|
6
|
+
var __getProtoOf = Object.getPrototypeOf;
|
|
7
|
+
var __hasOwnProp = Object.prototype.hasOwnProperty;
|
|
8
|
+
var __copyProps = (to, from, except, desc) => {
|
|
9
|
+
if (from && typeof from === "object" || typeof from === "function") for (var keys = __getOwnPropNames(from), i = 0, n = keys.length, key; i < n; i++) {
|
|
10
|
+
key = keys[i];
|
|
11
|
+
if (!__hasOwnProp.call(to, key) && key !== except) __defProp(to, key, {
|
|
12
|
+
get: ((k) => from[k]).bind(null, key),
|
|
13
|
+
enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable
|
|
14
|
+
});
|
|
15
|
+
}
|
|
16
|
+
return to;
|
|
17
|
+
};
|
|
18
|
+
var __toESM = (mod, isNodeMode, target) => (target = mod != null ? __create(__getProtoOf(mod)) : {}, __copyProps(isNodeMode || !mod || !mod.__esModule ? __defProp(target, "default", {
|
|
19
|
+
value: mod,
|
|
20
|
+
enumerable: true
|
|
21
|
+
}) : target, mod));
|
|
22
|
+
require("magic-string-ast");
|
|
23
|
+
let tinyest_for_wgsl = require("tinyest-for-wgsl");
|
|
24
|
+
let _babel_types = require("@babel/types");
|
|
25
|
+
_babel_types = __toESM(_babel_types);
|
|
26
|
+
//#region src/core/common.ts
|
|
27
|
+
function initPluginState(state, methods) {
|
|
28
|
+
var _state$opts$autoNamin;
|
|
29
|
+
state.tgpuAliases = new Set(state.opts.forceTgpuAlias ? [state.opts.forceTgpuAlias] : []);
|
|
30
|
+
state.autoNamingEnabled = (_state$opts$autoNamin = state.opts.autoNamingEnabled) !== null && _state$opts$autoNamin !== void 0 ? _state$opts$autoNamin : true;
|
|
31
|
+
state.inUseGpuScope = false;
|
|
32
|
+
state.alreadyTransformed = /* @__PURE__ */ new WeakSet();
|
|
33
|
+
Object.assign(state, methods);
|
|
34
|
+
}
|
|
35
|
+
/** Regular expressions used for early pruning (to avoid unnecessary parsing, which is expensive) */
|
|
36
|
+
const earlyPruneRegex = [/["']use gpu["']/, /t(ype)?gpu/];
|
|
37
|
+
const defaultOptions = {
|
|
38
|
+
include: /\.m?[jt]sx?(?:\?.*)?$/,
|
|
39
|
+
autoNamingEnabled: true,
|
|
40
|
+
earlyPruning: true
|
|
41
|
+
};
|
|
42
|
+
/**
|
|
43
|
+
* Returns the block scope of a function declaration, if one exists.
|
|
44
|
+
* Used to hoist a function declaration to the top of the scope it's visible in.
|
|
45
|
+
*/
|
|
46
|
+
function getBlockScope(path) {
|
|
47
|
+
if (!path.node.id) return;
|
|
48
|
+
const binding = path.scope.getBinding(path.node.id.name);
|
|
49
|
+
if (!binding) return;
|
|
50
|
+
let scopePath = binding.scope.path;
|
|
51
|
+
if (_babel_types.isNode(scopePath.node.body)) scopePath = scopePath.get("body");
|
|
52
|
+
if (_babel_types.isBlockStatement(scopePath.node) || _babel_types.isProgram(scopePath.node)) return scopePath;
|
|
53
|
+
}
|
|
54
|
+
/**
|
|
55
|
+
* Checks if `node` is an alias for the 'tgpu' object, traditionally
|
|
56
|
+
* available via `import tgpu from 'typegpu'`.
|
|
57
|
+
*/
|
|
58
|
+
function isTgpu(state, node) {
|
|
59
|
+
let path = "";
|
|
60
|
+
let tail = node;
|
|
61
|
+
while (true) if (tail.type === "MemberExpression") {
|
|
62
|
+
if (tail.property.type === "StringLiteral" && tail.property.value === "~unstable") {
|
|
63
|
+
tail = tail.object;
|
|
64
|
+
continue;
|
|
65
|
+
}
|
|
66
|
+
if (tail.property.type !== "Identifier") break;
|
|
67
|
+
path = path ? `${tail.property.name}.${path}` : tail.property.name;
|
|
68
|
+
tail = tail.object;
|
|
69
|
+
} else if (tail.type === "Identifier") {
|
|
70
|
+
path = path ? `${tail.name}.${path}` : tail.name;
|
|
71
|
+
break;
|
|
72
|
+
} else break;
|
|
73
|
+
return state.tgpuAliases.has(path);
|
|
74
|
+
}
|
|
75
|
+
function gatherTgpuAliases(state, node) {
|
|
76
|
+
if (node.source.value !== "typegpu") return;
|
|
77
|
+
for (const spec of node.specifiers) if (spec.type === "ImportDefaultSpecifier" || spec.type === "ImportSpecifier" && spec.imported.type === "Identifier" && spec.imported.name === "tgpu") state.tgpuAliases.add(spec.local.name);
|
|
78
|
+
else if (spec.type === "ImportNamespaceSpecifier") state.tgpuAliases.add(`${spec.local.name}.tgpu`);
|
|
79
|
+
}
|
|
80
|
+
const fnShellFunctionNames = [
|
|
81
|
+
"fn",
|
|
82
|
+
"vertexFn",
|
|
83
|
+
"fragmentFn",
|
|
84
|
+
"computeFn"
|
|
85
|
+
];
|
|
86
|
+
function isShellImplementationCall(node, state) {
|
|
87
|
+
return node.callee.type === "CallExpression" && node.callee.callee.type === "MemberExpression" && node.callee.callee.property.type === "Identifier" && fnShellFunctionNames.includes(node.callee.callee.property.name) && node.arguments.length === 1 && isTgpu(state, node.callee.callee.object);
|
|
88
|
+
}
|
|
89
|
+
/**
|
|
90
|
+
* Extracts a name and expression from nodes that contain a label and an expression,
|
|
91
|
+
* such as VariableDeclarator or PropertyDefinition.
|
|
92
|
+
* Returns a tuple of [name, expression] if found, otherwise undefined.
|
|
93
|
+
*
|
|
94
|
+
* @example
|
|
95
|
+
* extractLabelledExpression(node`let name = tgpu.bindGroupLayout({});`)
|
|
96
|
+
* // ["name", node`tgpu.bindGroupLayout({})`]
|
|
97
|
+
*/
|
|
98
|
+
function extractLabelledExpression(path) {
|
|
99
|
+
if (path.node.type === "VariableDeclarator" && path.node.id.type === "Identifier" && path.node.init) return [path.node.id.name, path.get("init")];
|
|
100
|
+
else if (path.node.type === "AssignmentExpression") {
|
|
101
|
+
const maybeName = tryFindIdentifier(path.node.left);
|
|
102
|
+
if (maybeName) return [maybeName, path.get("right")];
|
|
103
|
+
} else if (path.node.type === "ObjectProperty" && path.node.key.type === "Identifier") return [path.node.key.name, path.get("value")];
|
|
104
|
+
else if (path.node.type === "ClassProperty" && path.node.value && path.node.key.type === "Identifier") return [path.node.key.name, path.get("value")];
|
|
105
|
+
}
|
|
106
|
+
function getFunctionName(path) {
|
|
107
|
+
var _extractLabelledExpre, _path$node$id;
|
|
108
|
+
const maybeName = path.parentPath ? (_extractLabelledExpre = extractLabelledExpression(path.parentPath)) === null || _extractLabelledExpre === void 0 ? void 0 : _extractLabelledExpre[0] : void 0;
|
|
109
|
+
return maybeName !== null && maybeName !== void 0 ? maybeName : path.node.type === "FunctionDeclaration" || path.node.type === "FunctionExpression" ? (_path$node$id = path.node.id) === null || _path$node$id === void 0 ? void 0 : _path$node$id.name : void 0;
|
|
110
|
+
}
|
|
111
|
+
const resourceConstructors = [
|
|
112
|
+
"bindGroupLayout",
|
|
113
|
+
"vertexLayout",
|
|
114
|
+
"privateVar",
|
|
115
|
+
"workgroupVar",
|
|
116
|
+
"const",
|
|
117
|
+
"slot",
|
|
118
|
+
"accessor",
|
|
119
|
+
"mutableAccessor",
|
|
120
|
+
"comptime",
|
|
121
|
+
...fnShellFunctionNames,
|
|
122
|
+
"createBuffer",
|
|
123
|
+
"createMutable",
|
|
124
|
+
"createReadonly",
|
|
125
|
+
"createUniform",
|
|
126
|
+
"createQuerySet",
|
|
127
|
+
"createPipeline",
|
|
128
|
+
"createComputePipeline",
|
|
129
|
+
"createGuardedComputePipeline",
|
|
130
|
+
"createRenderPipeline",
|
|
131
|
+
"createTexture",
|
|
132
|
+
"createSampler",
|
|
133
|
+
"createComparisonSampler",
|
|
134
|
+
"struct",
|
|
135
|
+
"unstruct",
|
|
136
|
+
"createView"
|
|
137
|
+
];
|
|
138
|
+
/**
|
|
139
|
+
* Checks if `node` should be wrapped in an autoname function.
|
|
140
|
+
* Since it is mostly for debugging and clean WGSL generation,
|
|
141
|
+
* some false positives and false negatives are admissible.
|
|
142
|
+
*/
|
|
143
|
+
function containsResourceConstructorCall(node, state) {
|
|
144
|
+
if (node.type === "CallExpression") {
|
|
145
|
+
if (isShellImplementationCall(node, state)) return true;
|
|
146
|
+
if (node.callee.type === "Identifier" && resourceConstructors.includes(node.callee.name)) return true;
|
|
147
|
+
if (node.callee.type === "MemberExpression") {
|
|
148
|
+
if (node.callee.property.type === "Identifier") {
|
|
149
|
+
if (resourceConstructors.includes(node.callee.property.name)) return true;
|
|
150
|
+
if (node.callee.property.name === "$name") return false;
|
|
151
|
+
}
|
|
152
|
+
return containsResourceConstructorCall(node.callee.object, state);
|
|
153
|
+
}
|
|
154
|
+
}
|
|
155
|
+
if (node.type === "TaggedTemplateExpression") return containsResourceConstructorCall(node.tag, state);
|
|
156
|
+
return false;
|
|
157
|
+
}
|
|
158
|
+
/**
|
|
159
|
+
* Tries to find an identifier in a node.
|
|
160
|
+
*
|
|
161
|
+
* @example
|
|
162
|
+
* // syntax is simplified, imagine the arguments are appropriate nodes instead
|
|
163
|
+
* tryFindIdentifier('myBuffer'); // 'myBuffer'
|
|
164
|
+
* tryFindIdentifier('buffers.myBuffer'); // 'myBuffer'
|
|
165
|
+
* tryFindIdentifier('this.myBuffer'); // 'myBuffer'
|
|
166
|
+
* tryFindIdentifier('[a, b]'); // undefined
|
|
167
|
+
*/
|
|
168
|
+
function tryFindIdentifier(node) {
|
|
169
|
+
if (node.type === "Identifier") return node.name;
|
|
170
|
+
if (node.type === "PrivateName") return tryFindIdentifier(node.id);
|
|
171
|
+
if (node.type === "MemberExpression") return tryFindIdentifier(node.property);
|
|
172
|
+
}
|
|
173
|
+
/**
|
|
174
|
+
* Checks if `node` contains a label and a tgpu expression that could be named.
|
|
175
|
+
* If so, it calls the provided callback. Nodes selected for naming include:
|
|
176
|
+
*
|
|
177
|
+
* `let name = tgpu.bindGroupLayout({});` (VariableDeclarator)
|
|
178
|
+
*
|
|
179
|
+
* `name = tgpu.bindGroupLayout({});` (AssignmentExpression)
|
|
180
|
+
*
|
|
181
|
+
* `property: tgpu.bindGroupLayout({})` (Property/ObjectProperty)
|
|
182
|
+
*
|
|
183
|
+
* Since it is mostly for debugging and clean WGSL generation,
|
|
184
|
+
* some false positives and false negatives are admissible.
|
|
185
|
+
*
|
|
186
|
+
* This function is NOT used for auto-naming shell-less functions.
|
|
187
|
+
* Those are handled separately.
|
|
188
|
+
*
|
|
189
|
+
* @privateRemarks
|
|
190
|
+
* When adding new checks, you need to call this method in the corresponding node in Babel.
|
|
191
|
+
*/
|
|
192
|
+
function performExpressionNaming(state, path, namingCallback) {
|
|
193
|
+
if (!state.autoNamingEnabled) return;
|
|
194
|
+
const labelledExpression = extractLabelledExpression(path);
|
|
195
|
+
if (labelledExpression) {
|
|
196
|
+
const [label, expressionPath] = labelledExpression;
|
|
197
|
+
if (containsResourceConstructorCall(expressionPath.node, state)) namingCallback(expressionPath, label);
|
|
198
|
+
}
|
|
199
|
+
}
|
|
200
|
+
const operators = {
|
|
201
|
+
"+": "__tsover_add",
|
|
202
|
+
"-": "__tsover_sub",
|
|
203
|
+
"*": "__tsover_mul",
|
|
204
|
+
"/": "__tsover_div",
|
|
205
|
+
"%": "__tsover_mod",
|
|
206
|
+
"+=": "__tsover_add",
|
|
207
|
+
"-=": "__tsover_sub",
|
|
208
|
+
"*=": "__tsover_mul",
|
|
209
|
+
"/=": "__tsover_div",
|
|
210
|
+
"%=": "__tsover_mod"
|
|
211
|
+
};
|
|
212
|
+
function containsUseGpuDirective(node) {
|
|
213
|
+
var _node$body$directives, _node$body;
|
|
214
|
+
return ("directives" in node.body ? (_node$body$directives = (_node$body = node.body) === null || _node$body === void 0 ? void 0 : _node$body.directives) !== null && _node$body$directives !== void 0 ? _node$body$directives : [] : []).map((directive) => directive.value.value).includes("use gpu");
|
|
215
|
+
}
|
|
216
|
+
const fnNodeToTranspiledMap = /* @__PURE__ */ new WeakMap();
|
|
217
|
+
function functionOnExit(path, state) {
|
|
218
|
+
const node = path.node;
|
|
219
|
+
if (!containsUseGpuDirective(node)) return;
|
|
220
|
+
state.inUseGpuScope = false;
|
|
221
|
+
if (state.alreadyTransformed.has(node)) return;
|
|
222
|
+
const ast = fnNodeToTranspiledMap.get(path.node);
|
|
223
|
+
const maybeName = getFunctionName(path);
|
|
224
|
+
if (!ast) throw new Error(`No metadata found for function ${maybeName !== null && maybeName !== void 0 ? maybeName : "<unnamed>"}`);
|
|
225
|
+
state.assignMetadata(path, maybeName, ast);
|
|
226
|
+
state.alreadyTransformed.add(node);
|
|
227
|
+
path.skip();
|
|
228
|
+
}
|
|
229
|
+
const functionVisitor = {
|
|
230
|
+
ImportDeclaration(path, state) {
|
|
231
|
+
gatherTgpuAliases(state, path.node);
|
|
232
|
+
},
|
|
233
|
+
VariableDeclarator(path, state) {
|
|
234
|
+
performExpressionNaming(state, path, (pathToName, name) => {
|
|
235
|
+
state.wrapInAutoName(pathToName, name);
|
|
236
|
+
});
|
|
237
|
+
},
|
|
238
|
+
ObjectProperty(path, state) {
|
|
239
|
+
performExpressionNaming(state, path, (pathToName, name) => state.wrapInAutoName(pathToName, name));
|
|
240
|
+
},
|
|
241
|
+
ClassProperty(path, state) {
|
|
242
|
+
performExpressionNaming(state, path, (pathToName, name) => state.wrapInAutoName(pathToName, name));
|
|
243
|
+
},
|
|
244
|
+
AssignmentExpression: { exit(path, state) {
|
|
245
|
+
const runtimeFn = operators[path.node.operator];
|
|
246
|
+
if (state.inUseGpuScope && runtimeFn) state.replaceWithAssignmentOverload(path, runtimeFn);
|
|
247
|
+
performExpressionNaming(state, path, (pathToName, name) => state.wrapInAutoName(pathToName, name));
|
|
248
|
+
path.skip();
|
|
249
|
+
} },
|
|
250
|
+
BinaryExpression: { exit(path, state) {
|
|
251
|
+
const runtimeFn = operators[path.node.operator];
|
|
252
|
+
if (state.inUseGpuScope && runtimeFn) state.replaceWithBinaryOverload(path, runtimeFn);
|
|
253
|
+
path.skip();
|
|
254
|
+
} },
|
|
255
|
+
ArrowFunctionExpression: {
|
|
256
|
+
enter(path, state) {
|
|
257
|
+
if (containsUseGpuDirective(path.node)) {
|
|
258
|
+
fnNodeToTranspiledMap.set(path.node, (0, tinyest_for_wgsl.transpileFn)(path.node));
|
|
259
|
+
if (state.inUseGpuScope) throw new Error(`Nesting 'use gpu' functions is not allowed`);
|
|
260
|
+
state.inUseGpuScope = true;
|
|
261
|
+
}
|
|
262
|
+
},
|
|
263
|
+
exit: functionOnExit
|
|
264
|
+
},
|
|
265
|
+
FunctionExpression: {
|
|
266
|
+
enter(path, state) {
|
|
267
|
+
if (containsUseGpuDirective(path.node)) {
|
|
268
|
+
fnNodeToTranspiledMap.set(path.node, (0, tinyest_for_wgsl.transpileFn)(path.node));
|
|
269
|
+
if (state.inUseGpuScope) throw new Error(`Nesting 'use gpu' functions is not allowed`);
|
|
270
|
+
state.inUseGpuScope = true;
|
|
271
|
+
}
|
|
272
|
+
},
|
|
273
|
+
exit: functionOnExit
|
|
274
|
+
},
|
|
275
|
+
FunctionDeclaration: {
|
|
276
|
+
enter(path, state) {
|
|
277
|
+
if (containsUseGpuDirective(path.node)) {
|
|
278
|
+
fnNodeToTranspiledMap.set(path.node, (0, tinyest_for_wgsl.transpileFn)(path.node));
|
|
279
|
+
if (state.inUseGpuScope) throw new Error(`Nesting 'use gpu' functions is not allowed`);
|
|
280
|
+
state.inUseGpuScope = true;
|
|
281
|
+
}
|
|
282
|
+
},
|
|
283
|
+
exit: functionOnExit
|
|
284
|
+
},
|
|
285
|
+
CallExpression: { exit(path, state) {
|
|
286
|
+
const node = path.node;
|
|
287
|
+
if (isShellImplementationCall(node, state)) {
|
|
288
|
+
const implementation = node.arguments[0];
|
|
289
|
+
if (implementation && (implementation.type === "FunctionExpression" || implementation.type === "ArrowFunctionExpression") && !containsUseGpuDirective(implementation)) state.assignMetadata(path.get("arguments.0"), getFunctionName(path.get("arguments.0")), (0, tinyest_for_wgsl.transpileFn)(implementation));
|
|
290
|
+
}
|
|
291
|
+
} }
|
|
292
|
+
};
|
|
293
|
+
//#endregion
|
|
294
|
+
Object.defineProperty(exports, "__toESM", {
|
|
295
|
+
enumerable: true,
|
|
296
|
+
get: function() {
|
|
297
|
+
return __toESM;
|
|
298
|
+
}
|
|
299
|
+
});
|
|
300
|
+
Object.defineProperty(exports, "defaultOptions", {
|
|
301
|
+
enumerable: true,
|
|
302
|
+
get: function() {
|
|
303
|
+
return defaultOptions;
|
|
304
|
+
}
|
|
305
|
+
});
|
|
306
|
+
Object.defineProperty(exports, "earlyPruneRegex", {
|
|
307
|
+
enumerable: true,
|
|
308
|
+
get: function() {
|
|
309
|
+
return earlyPruneRegex;
|
|
310
|
+
}
|
|
311
|
+
});
|
|
312
|
+
Object.defineProperty(exports, "functionVisitor", {
|
|
313
|
+
enumerable: true,
|
|
314
|
+
get: function() {
|
|
315
|
+
return functionVisitor;
|
|
316
|
+
}
|
|
317
|
+
});
|
|
318
|
+
Object.defineProperty(exports, "getBlockScope", {
|
|
319
|
+
enumerable: true,
|
|
320
|
+
get: function() {
|
|
321
|
+
return getBlockScope;
|
|
322
|
+
}
|
|
323
|
+
});
|
|
324
|
+
Object.defineProperty(exports, "initPluginState", {
|
|
325
|
+
enumerable: true,
|
|
326
|
+
get: function() {
|
|
327
|
+
return initPluginState;
|
|
328
|
+
}
|
|
329
|
+
});
|
|
@@ -0,0 +1,272 @@
|
|
|
1
|
+
import "magic-string-ast";
|
|
2
|
+
import { transpileFn } from "tinyest-for-wgsl";
|
|
3
|
+
import * as t from "@babel/types";
|
|
4
|
+
//#region src/core/common.ts
|
|
5
|
+
function initPluginState(state, methods) {
|
|
6
|
+
var _state$opts$autoNamin;
|
|
7
|
+
state.tgpuAliases = new Set(state.opts.forceTgpuAlias ? [state.opts.forceTgpuAlias] : []);
|
|
8
|
+
state.autoNamingEnabled = (_state$opts$autoNamin = state.opts.autoNamingEnabled) !== null && _state$opts$autoNamin !== void 0 ? _state$opts$autoNamin : true;
|
|
9
|
+
state.inUseGpuScope = false;
|
|
10
|
+
state.alreadyTransformed = /* @__PURE__ */ new WeakSet();
|
|
11
|
+
Object.assign(state, methods);
|
|
12
|
+
}
|
|
13
|
+
/** Regular expressions used for early pruning (to avoid unnecessary parsing, which is expensive) */
|
|
14
|
+
const earlyPruneRegex = [/["']use gpu["']/, /t(ype)?gpu/];
|
|
15
|
+
const defaultOptions = {
|
|
16
|
+
include: /\.m?[jt]sx?(?:\?.*)?$/,
|
|
17
|
+
autoNamingEnabled: true,
|
|
18
|
+
earlyPruning: true
|
|
19
|
+
};
|
|
20
|
+
/**
|
|
21
|
+
* Returns the block scope of a function declaration, if one exists.
|
|
22
|
+
* Used to hoist a function declaration to the top of the scope it's visible in.
|
|
23
|
+
*/
|
|
24
|
+
function getBlockScope(path) {
|
|
25
|
+
if (!path.node.id) return;
|
|
26
|
+
const binding = path.scope.getBinding(path.node.id.name);
|
|
27
|
+
if (!binding) return;
|
|
28
|
+
let scopePath = binding.scope.path;
|
|
29
|
+
if (t.isNode(scopePath.node.body)) scopePath = scopePath.get("body");
|
|
30
|
+
if (t.isBlockStatement(scopePath.node) || t.isProgram(scopePath.node)) return scopePath;
|
|
31
|
+
}
|
|
32
|
+
/**
|
|
33
|
+
* Checks if `node` is an alias for the 'tgpu' object, traditionally
|
|
34
|
+
* available via `import tgpu from 'typegpu'`.
|
|
35
|
+
*/
|
|
36
|
+
function isTgpu(state, node) {
|
|
37
|
+
let path = "";
|
|
38
|
+
let tail = node;
|
|
39
|
+
while (true) if (tail.type === "MemberExpression") {
|
|
40
|
+
if (tail.property.type === "StringLiteral" && tail.property.value === "~unstable") {
|
|
41
|
+
tail = tail.object;
|
|
42
|
+
continue;
|
|
43
|
+
}
|
|
44
|
+
if (tail.property.type !== "Identifier") break;
|
|
45
|
+
path = path ? `${tail.property.name}.${path}` : tail.property.name;
|
|
46
|
+
tail = tail.object;
|
|
47
|
+
} else if (tail.type === "Identifier") {
|
|
48
|
+
path = path ? `${tail.name}.${path}` : tail.name;
|
|
49
|
+
break;
|
|
50
|
+
} else break;
|
|
51
|
+
return state.tgpuAliases.has(path);
|
|
52
|
+
}
|
|
53
|
+
function gatherTgpuAliases(state, node) {
|
|
54
|
+
if (node.source.value !== "typegpu") return;
|
|
55
|
+
for (const spec of node.specifiers) if (spec.type === "ImportDefaultSpecifier" || spec.type === "ImportSpecifier" && spec.imported.type === "Identifier" && spec.imported.name === "tgpu") state.tgpuAliases.add(spec.local.name);
|
|
56
|
+
else if (spec.type === "ImportNamespaceSpecifier") state.tgpuAliases.add(`${spec.local.name}.tgpu`);
|
|
57
|
+
}
|
|
58
|
+
const fnShellFunctionNames = [
|
|
59
|
+
"fn",
|
|
60
|
+
"vertexFn",
|
|
61
|
+
"fragmentFn",
|
|
62
|
+
"computeFn"
|
|
63
|
+
];
|
|
64
|
+
function isShellImplementationCall(node, state) {
|
|
65
|
+
return node.callee.type === "CallExpression" && node.callee.callee.type === "MemberExpression" && node.callee.callee.property.type === "Identifier" && fnShellFunctionNames.includes(node.callee.callee.property.name) && node.arguments.length === 1 && isTgpu(state, node.callee.callee.object);
|
|
66
|
+
}
|
|
67
|
+
/**
|
|
68
|
+
* Extracts a name and expression from nodes that contain a label and an expression,
|
|
69
|
+
* such as VariableDeclarator or PropertyDefinition.
|
|
70
|
+
* Returns a tuple of [name, expression] if found, otherwise undefined.
|
|
71
|
+
*
|
|
72
|
+
* @example
|
|
73
|
+
* extractLabelledExpression(node`let name = tgpu.bindGroupLayout({});`)
|
|
74
|
+
* // ["name", node`tgpu.bindGroupLayout({})`]
|
|
75
|
+
*/
|
|
76
|
+
function extractLabelledExpression(path) {
|
|
77
|
+
if (path.node.type === "VariableDeclarator" && path.node.id.type === "Identifier" && path.node.init) return [path.node.id.name, path.get("init")];
|
|
78
|
+
else if (path.node.type === "AssignmentExpression") {
|
|
79
|
+
const maybeName = tryFindIdentifier(path.node.left);
|
|
80
|
+
if (maybeName) return [maybeName, path.get("right")];
|
|
81
|
+
} else if (path.node.type === "ObjectProperty" && path.node.key.type === "Identifier") return [path.node.key.name, path.get("value")];
|
|
82
|
+
else if (path.node.type === "ClassProperty" && path.node.value && path.node.key.type === "Identifier") return [path.node.key.name, path.get("value")];
|
|
83
|
+
}
|
|
84
|
+
function getFunctionName(path) {
|
|
85
|
+
var _extractLabelledExpre, _path$node$id;
|
|
86
|
+
const maybeName = path.parentPath ? (_extractLabelledExpre = extractLabelledExpression(path.parentPath)) === null || _extractLabelledExpre === void 0 ? void 0 : _extractLabelledExpre[0] : void 0;
|
|
87
|
+
return maybeName !== null && maybeName !== void 0 ? maybeName : path.node.type === "FunctionDeclaration" || path.node.type === "FunctionExpression" ? (_path$node$id = path.node.id) === null || _path$node$id === void 0 ? void 0 : _path$node$id.name : void 0;
|
|
88
|
+
}
|
|
89
|
+
const resourceConstructors = [
|
|
90
|
+
"bindGroupLayout",
|
|
91
|
+
"vertexLayout",
|
|
92
|
+
"privateVar",
|
|
93
|
+
"workgroupVar",
|
|
94
|
+
"const",
|
|
95
|
+
"slot",
|
|
96
|
+
"accessor",
|
|
97
|
+
"mutableAccessor",
|
|
98
|
+
"comptime",
|
|
99
|
+
...fnShellFunctionNames,
|
|
100
|
+
"createBuffer",
|
|
101
|
+
"createMutable",
|
|
102
|
+
"createReadonly",
|
|
103
|
+
"createUniform",
|
|
104
|
+
"createQuerySet",
|
|
105
|
+
"createPipeline",
|
|
106
|
+
"createComputePipeline",
|
|
107
|
+
"createGuardedComputePipeline",
|
|
108
|
+
"createRenderPipeline",
|
|
109
|
+
"createTexture",
|
|
110
|
+
"createSampler",
|
|
111
|
+
"createComparisonSampler",
|
|
112
|
+
"struct",
|
|
113
|
+
"unstruct",
|
|
114
|
+
"createView"
|
|
115
|
+
];
|
|
116
|
+
/**
|
|
117
|
+
* Checks if `node` should be wrapped in an autoname function.
|
|
118
|
+
* Since it is mostly for debugging and clean WGSL generation,
|
|
119
|
+
* some false positives and false negatives are admissible.
|
|
120
|
+
*/
|
|
121
|
+
function containsResourceConstructorCall(node, state) {
|
|
122
|
+
if (node.type === "CallExpression") {
|
|
123
|
+
if (isShellImplementationCall(node, state)) return true;
|
|
124
|
+
if (node.callee.type === "Identifier" && resourceConstructors.includes(node.callee.name)) return true;
|
|
125
|
+
if (node.callee.type === "MemberExpression") {
|
|
126
|
+
if (node.callee.property.type === "Identifier") {
|
|
127
|
+
if (resourceConstructors.includes(node.callee.property.name)) return true;
|
|
128
|
+
if (node.callee.property.name === "$name") return false;
|
|
129
|
+
}
|
|
130
|
+
return containsResourceConstructorCall(node.callee.object, state);
|
|
131
|
+
}
|
|
132
|
+
}
|
|
133
|
+
if (node.type === "TaggedTemplateExpression") return containsResourceConstructorCall(node.tag, state);
|
|
134
|
+
return false;
|
|
135
|
+
}
|
|
136
|
+
/**
|
|
137
|
+
* Tries to find an identifier in a node.
|
|
138
|
+
*
|
|
139
|
+
* @example
|
|
140
|
+
* // syntax is simplified, imagine the arguments are appropriate nodes instead
|
|
141
|
+
* tryFindIdentifier('myBuffer'); // 'myBuffer'
|
|
142
|
+
* tryFindIdentifier('buffers.myBuffer'); // 'myBuffer'
|
|
143
|
+
* tryFindIdentifier('this.myBuffer'); // 'myBuffer'
|
|
144
|
+
* tryFindIdentifier('[a, b]'); // undefined
|
|
145
|
+
*/
|
|
146
|
+
function tryFindIdentifier(node) {
|
|
147
|
+
if (node.type === "Identifier") return node.name;
|
|
148
|
+
if (node.type === "PrivateName") return tryFindIdentifier(node.id);
|
|
149
|
+
if (node.type === "MemberExpression") return tryFindIdentifier(node.property);
|
|
150
|
+
}
|
|
151
|
+
/**
|
|
152
|
+
* Checks if `node` contains a label and a tgpu expression that could be named.
|
|
153
|
+
* If so, it calls the provided callback. Nodes selected for naming include:
|
|
154
|
+
*
|
|
155
|
+
* `let name = tgpu.bindGroupLayout({});` (VariableDeclarator)
|
|
156
|
+
*
|
|
157
|
+
* `name = tgpu.bindGroupLayout({});` (AssignmentExpression)
|
|
158
|
+
*
|
|
159
|
+
* `property: tgpu.bindGroupLayout({})` (Property/ObjectProperty)
|
|
160
|
+
*
|
|
161
|
+
* Since it is mostly for debugging and clean WGSL generation,
|
|
162
|
+
* some false positives and false negatives are admissible.
|
|
163
|
+
*
|
|
164
|
+
* This function is NOT used for auto-naming shell-less functions.
|
|
165
|
+
* Those are handled separately.
|
|
166
|
+
*
|
|
167
|
+
* @privateRemarks
|
|
168
|
+
* When adding new checks, you need to call this method in the corresponding node in Babel.
|
|
169
|
+
*/
|
|
170
|
+
function performExpressionNaming(state, path, namingCallback) {
|
|
171
|
+
if (!state.autoNamingEnabled) return;
|
|
172
|
+
const labelledExpression = extractLabelledExpression(path);
|
|
173
|
+
if (labelledExpression) {
|
|
174
|
+
const [label, expressionPath] = labelledExpression;
|
|
175
|
+
if (containsResourceConstructorCall(expressionPath.node, state)) namingCallback(expressionPath, label);
|
|
176
|
+
}
|
|
177
|
+
}
|
|
178
|
+
const operators = {
|
|
179
|
+
"+": "__tsover_add",
|
|
180
|
+
"-": "__tsover_sub",
|
|
181
|
+
"*": "__tsover_mul",
|
|
182
|
+
"/": "__tsover_div",
|
|
183
|
+
"%": "__tsover_mod",
|
|
184
|
+
"+=": "__tsover_add",
|
|
185
|
+
"-=": "__tsover_sub",
|
|
186
|
+
"*=": "__tsover_mul",
|
|
187
|
+
"/=": "__tsover_div",
|
|
188
|
+
"%=": "__tsover_mod"
|
|
189
|
+
};
|
|
190
|
+
function containsUseGpuDirective(node) {
|
|
191
|
+
var _node$body$directives, _node$body;
|
|
192
|
+
return ("directives" in node.body ? (_node$body$directives = (_node$body = node.body) === null || _node$body === void 0 ? void 0 : _node$body.directives) !== null && _node$body$directives !== void 0 ? _node$body$directives : [] : []).map((directive) => directive.value.value).includes("use gpu");
|
|
193
|
+
}
|
|
194
|
+
const fnNodeToTranspiledMap = /* @__PURE__ */ new WeakMap();
|
|
195
|
+
function functionOnExit(path, state) {
|
|
196
|
+
const node = path.node;
|
|
197
|
+
if (!containsUseGpuDirective(node)) return;
|
|
198
|
+
state.inUseGpuScope = false;
|
|
199
|
+
if (state.alreadyTransformed.has(node)) return;
|
|
200
|
+
const ast = fnNodeToTranspiledMap.get(path.node);
|
|
201
|
+
const maybeName = getFunctionName(path);
|
|
202
|
+
if (!ast) throw new Error(`No metadata found for function ${maybeName !== null && maybeName !== void 0 ? maybeName : "<unnamed>"}`);
|
|
203
|
+
state.assignMetadata(path, maybeName, ast);
|
|
204
|
+
state.alreadyTransformed.add(node);
|
|
205
|
+
path.skip();
|
|
206
|
+
}
|
|
207
|
+
const functionVisitor = {
|
|
208
|
+
ImportDeclaration(path, state) {
|
|
209
|
+
gatherTgpuAliases(state, path.node);
|
|
210
|
+
},
|
|
211
|
+
VariableDeclarator(path, state) {
|
|
212
|
+
performExpressionNaming(state, path, (pathToName, name) => {
|
|
213
|
+
state.wrapInAutoName(pathToName, name);
|
|
214
|
+
});
|
|
215
|
+
},
|
|
216
|
+
ObjectProperty(path, state) {
|
|
217
|
+
performExpressionNaming(state, path, (pathToName, name) => state.wrapInAutoName(pathToName, name));
|
|
218
|
+
},
|
|
219
|
+
ClassProperty(path, state) {
|
|
220
|
+
performExpressionNaming(state, path, (pathToName, name) => state.wrapInAutoName(pathToName, name));
|
|
221
|
+
},
|
|
222
|
+
AssignmentExpression: { exit(path, state) {
|
|
223
|
+
const runtimeFn = operators[path.node.operator];
|
|
224
|
+
if (state.inUseGpuScope && runtimeFn) state.replaceWithAssignmentOverload(path, runtimeFn);
|
|
225
|
+
performExpressionNaming(state, path, (pathToName, name) => state.wrapInAutoName(pathToName, name));
|
|
226
|
+
path.skip();
|
|
227
|
+
} },
|
|
228
|
+
BinaryExpression: { exit(path, state) {
|
|
229
|
+
const runtimeFn = operators[path.node.operator];
|
|
230
|
+
if (state.inUseGpuScope && runtimeFn) state.replaceWithBinaryOverload(path, runtimeFn);
|
|
231
|
+
path.skip();
|
|
232
|
+
} },
|
|
233
|
+
ArrowFunctionExpression: {
|
|
234
|
+
enter(path, state) {
|
|
235
|
+
if (containsUseGpuDirective(path.node)) {
|
|
236
|
+
fnNodeToTranspiledMap.set(path.node, transpileFn(path.node));
|
|
237
|
+
if (state.inUseGpuScope) throw new Error(`Nesting 'use gpu' functions is not allowed`);
|
|
238
|
+
state.inUseGpuScope = true;
|
|
239
|
+
}
|
|
240
|
+
},
|
|
241
|
+
exit: functionOnExit
|
|
242
|
+
},
|
|
243
|
+
FunctionExpression: {
|
|
244
|
+
enter(path, state) {
|
|
245
|
+
if (containsUseGpuDirective(path.node)) {
|
|
246
|
+
fnNodeToTranspiledMap.set(path.node, transpileFn(path.node));
|
|
247
|
+
if (state.inUseGpuScope) throw new Error(`Nesting 'use gpu' functions is not allowed`);
|
|
248
|
+
state.inUseGpuScope = true;
|
|
249
|
+
}
|
|
250
|
+
},
|
|
251
|
+
exit: functionOnExit
|
|
252
|
+
},
|
|
253
|
+
FunctionDeclaration: {
|
|
254
|
+
enter(path, state) {
|
|
255
|
+
if (containsUseGpuDirective(path.node)) {
|
|
256
|
+
fnNodeToTranspiledMap.set(path.node, transpileFn(path.node));
|
|
257
|
+
if (state.inUseGpuScope) throw new Error(`Nesting 'use gpu' functions is not allowed`);
|
|
258
|
+
state.inUseGpuScope = true;
|
|
259
|
+
}
|
|
260
|
+
},
|
|
261
|
+
exit: functionOnExit
|
|
262
|
+
},
|
|
263
|
+
CallExpression: { exit(path, state) {
|
|
264
|
+
const node = path.node;
|
|
265
|
+
if (isShellImplementationCall(node, state)) {
|
|
266
|
+
const implementation = node.arguments[0];
|
|
267
|
+
if (implementation && (implementation.type === "FunctionExpression" || implementation.type === "ArrowFunctionExpression") && !containsUseGpuDirective(implementation)) state.assignMetadata(path.get("arguments.0"), getFunctionName(path.get("arguments.0")), transpileFn(implementation));
|
|
268
|
+
}
|
|
269
|
+
} }
|
|
270
|
+
};
|
|
271
|
+
//#endregion
|
|
272
|
+
export { initPluginState as a, getBlockScope as i, earlyPruneRegex as n, functionVisitor as r, defaultOptions as t };
|