Skip to content

Commit 6e8a4dc

Browse files
feat: Use strict names by default and fallback to item (#1916)
1 parent 82150b2 commit 6e8a4dc

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

58 files changed

+833
-877
lines changed

packages/typegpu/src/core/resolve/tgpuResolve.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -191,7 +191,7 @@ function resolveFromTemplate(
191191
template,
192192
externals,
193193
shaderGenerator,
194-
names = 'random',
194+
names = 'strict',
195195
config,
196196
enableExtensions,
197197
} = options;
@@ -232,7 +232,7 @@ function resolveFromArray(
232232
): ResolutionResult {
233233
const {
234234
shaderGenerator,
235-
names = 'random',
235+
names = 'strict',
236236
config,
237237
enableExtensions,
238238
} = options ?? {};

packages/typegpu/src/nameRegistry.ts

Lines changed: 19 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -198,6 +198,16 @@ export interface NameRegistry {
198198
makeValid(primer: string): string;
199199
}
200200

201+
function sanitizePrimer(primer: string | undefined) {
202+
if (primer) {
203+
// sanitizing
204+
return primer
205+
.replaceAll(/\s/g, '_') // whitespaces
206+
.replaceAll(/[^\w\d]/g, ''); // removing illegal characters
207+
}
208+
return 'item';
209+
}
210+
201211
/**
202212
* A function for checking whether an identifier needs renaming.
203213
* Throws if provided with an invalid identifier that cannot be easily renamed.
@@ -233,16 +243,9 @@ export class RandomNameRegistry extends NameRegistryImpl {
233243
private lastUniqueId = 0;
234244

235245
makeUnique(primer?: string | undefined): string {
236-
let label: string;
237-
if (primer) {
238-
// sanitizing
239-
label = primer.replaceAll(/\s/g, '_'); // whitespace -> _
240-
label = label.replaceAll(/[^\w\d]/g, ''); // removing illegal characters
241-
} else {
242-
label = 'item';
243-
}
246+
const sanitizedPrimer = sanitizePrimer(primer);
244247

245-
return `${label}_${this.lastUniqueId++}`;
248+
return `${sanitizedPrimer}_${this.lastUniqueId++}`;
246249
}
247250
}
248251

@@ -253,19 +256,18 @@ export class StrictNameRegistry extends NameRegistryImpl {
253256
*/
254257
private readonly _usedNames = new Set<string>(bannedTokens);
255258

259+
// TODO: optimize this with a map
256260
makeUnique(primer?: string | undefined): string {
257-
if (primer === undefined) {
258-
throw new Error('Unnamed item found when using a strict name registry');
259-
}
261+
const sanitizedPrimer = sanitizePrimer(primer);
260262

261263
let index = 0;
262-
let unusedName = primer;
263-
while (this._usedNames.has(unusedName)) {
264+
let label = sanitizedPrimer;
265+
while (this._usedNames.has(label)) {
264266
index++;
265-
unusedName = `${primer}_${index}`;
267+
label = `${sanitizedPrimer}_${index}`;
266268
}
267269

268-
this._usedNames.add(unusedName);
269-
return unusedName;
270+
this._usedNames.add(label);
271+
return label;
270272
}
271273
}

packages/typegpu/tests/accessor.test.ts

Lines changed: 8 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,6 @@ import { describe, expect } from 'vitest';
22
import * as d from '../src/data/index.ts';
33
import tgpu from '../src/index.ts';
44
import { it } from './utils/extendedIt.ts';
5-
import { asWgsl } from './utils/parseResolved.ts';
65

76
const RED = d.vec3f(1, 0, 0);
87
const RED_RESOLVED = 'vec3f(1, 0, 0)';
@@ -17,7 +16,7 @@ describe('tgpu.accessor', () => {
1716
.$uses({ colorAccess })
1817
.with(colorAccess, red);
1918

20-
expect(asWgsl(getColor)).toMatchInlineSnapshot(`
19+
expect(tgpu.resolve([getColor])).toMatchInlineSnapshot(`
2120
"fn red() -> vec3f{ return vec3f(1, 0, 0); }
2221
2322
fn getColor() -> vec3f{ return red(); }"
@@ -36,7 +35,7 @@ describe('tgpu.accessor', () => {
3635
.$uses({ colorAccess })
3736
.with(colorAccess, redUniform);
3837

39-
expect(asWgsl(getColor)).toMatchInlineSnapshot(`
38+
expect(tgpu.resolve([getColor])).toMatchInlineSnapshot(`
4039
"@group(0) @binding(0) var<uniform> redUniform: vec3f;
4140
4241
fn getColor() -> vec3f{ return redUniform; }"
@@ -55,7 +54,7 @@ describe('tgpu.accessor', () => {
5554
.with(colorAccess, RED)
5655
.with(multiplierAccess, 2);
5756

58-
expect(asWgsl(getColor)).toMatchInlineSnapshot(
57+
expect(tgpu.resolve([getColor])).toMatchInlineSnapshot(
5958
`"fn getColor() -> vec3f{ return vec3f(1, 0, 0) * 2f; }"`,
6059
);
6160
});
@@ -66,7 +65,7 @@ describe('tgpu.accessor', () => {
6665
const getColor = tgpu.fn([], d.vec3f)`() { return colorAccess; }`
6766
.$uses({ colorAccess });
6867

69-
expect(asWgsl(getColor)).toMatchInlineSnapshot(
68+
expect(tgpu.resolve([getColor])).toMatchInlineSnapshot(
7069
`"fn getColor() -> vec3f{ return vec3f(1, 0, 0); }"`,
7170
);
7271
});
@@ -83,7 +82,7 @@ describe('tgpu.accessor', () => {
8382
const main = tgpu.fn([])`() { return getColorWithGreen(); }`
8483
.$uses({ getColorWithGreen });
8584

86-
expect(asWgsl(main)).toMatchInlineSnapshot(`
85+
expect(tgpu.resolve([main])).toMatchInlineSnapshot(`
8786
"fn getColor() -> vec3f{ return vec3f(0, 1, 0); }
8887
8988
fn main() { return getColor(); }"
@@ -96,7 +95,7 @@ describe('tgpu.accessor', () => {
9695
const getColor = tgpu.fn([], d.vec3f)`() { return colorAccess; }`
9796
.$uses({ colorAccess });
9897

99-
expect(() => asWgsl(getColor))
98+
expect(() => tgpu.resolve([getColor]))
10099
.toThrowErrorMatchingInlineSnapshot(`
101100
[Error: Resolution of the following tree failed:
102101
- <root>
@@ -127,7 +126,7 @@ describe('tgpu.accessor', () => {
127126
const color3X = colorAccessorFn.value.x;
128127
});
129128

130-
expect(asWgsl(main)).toMatchInlineSnapshot(`
129+
expect(tgpu.resolve([main])).toMatchInlineSnapshot(`
131130
"@group(0) @binding(0) var<uniform> redUniform: vec3f;
132131
133132
fn getColor() -> vec3f {
@@ -153,7 +152,7 @@ describe('tgpu.accessor', () => {
153152
const foo = fooAccess.$;
154153
});
155154

156-
expect(asWgsl(main)).toMatchInlineSnapshot(`
155+
expect(tgpu.resolve([main])).toMatchInlineSnapshot(`
157156
"fn main() {
158157
const foo = 1f;
159158
}"

packages/typegpu/tests/align.test.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
import { describe, expect, expectTypeOf, it } from 'vitest';
22
import { alignmentOf } from '../src/data/alignmentOf.ts';
33
import * as d from '../src/data/index.ts';
4-
import { asWgsl } from './utils/parseResolved.ts';
4+
import tgpu from '../src/index.ts';
55

66
describe('d.align', () => {
77
it('adds @align attribute for custom aligned struct members', () => {
@@ -11,7 +11,7 @@ describe('d.align', () => {
1111
c: d.u32,
1212
});
1313

14-
expect(asWgsl(s1)).toContain(
14+
expect(tgpu.resolve([s1])).toContain(
1515
'@align(16) b: u32,',
1616
);
1717
});

packages/typegpu/tests/array.test.ts

Lines changed: 15 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,6 @@ import { namespace } from '../src/core/resolve/namespace.ts';
88
import { resolve } from '../src/resolutionCtx.ts';
99
import type { Infer } from '../src/shared/repr.ts';
1010
import { arrayLength } from '../src/std/array.ts';
11-
import { asWgsl } from './utils/parseResolved.ts';
1211

1312
describe('array', () => {
1413
it('produces a visually pleasant type', () => {
@@ -179,7 +178,7 @@ describe('array', () => {
179178
const defaultValue = Outer();
180179
});
181180

182-
expect(asWgsl(testFunction)).toMatchInlineSnapshot(`
181+
expect(tgpu.resolve([testFunction])).toMatchInlineSnapshot(`
183182
"fn testFunction() {
184183
var defaultValue = array<array<f32, 1>, 2>();
185184
}"
@@ -195,7 +194,7 @@ describe('array', () => {
195194
return;
196195
});
197196

198-
expect(asWgsl(testFn)).toMatchInlineSnapshot(`
197+
expect(tgpu.resolve([testFn])).toMatchInlineSnapshot(`
199198
"fn testFn() {
200199
var myArray = array<u32, 1>(10u);
201200
var myClone = myArray;
@@ -213,7 +212,7 @@ describe('array', () => {
213212
return;
214213
});
215214

216-
expect(asWgsl(testFn)).toMatchInlineSnapshot(`
215+
expect(tgpu.resolve([testFn])).toMatchInlineSnapshot(`
217216
"fn testFn() {
218217
var myArrays = array<array<i32, 1>, 1>(array<i32, 1>(10i));
219218
var myClone = myArrays[0i];
@@ -227,7 +226,7 @@ describe('array', () => {
227226
const result = d.arrayOf(d.f32, 4)();
228227
});
229228

230-
expect(asWgsl(foo)).toMatchInlineSnapshot(`
229+
expect(tgpu.resolve([foo])).toMatchInlineSnapshot(`
231230
"fn foo() {
232231
var result = array<f32, 4>();
233232
}"
@@ -239,7 +238,7 @@ describe('array', () => {
239238
const result = d.arrayOf(d.f32)(4)();
240239
});
241240

242-
expect(asWgsl(foo)).toMatchInlineSnapshot(`
241+
expect(tgpu.resolve([foo])).toMatchInlineSnapshot(`
243242
"fn foo() {
244243
var result = array<f32, 4>();
245244
}"
@@ -251,7 +250,7 @@ describe('array', () => {
251250
const result = d.arrayOf(d.f32, count)();
252251
});
253252

254-
expect(() => asWgsl(foo)).toThrowErrorMatchingInlineSnapshot(`
253+
expect(() => tgpu.resolve([foo])).toThrowErrorMatchingInlineSnapshot(`
255254
[Error: Resolution of the following tree failed:
256255
- <root>
257256
- fn:foo
@@ -265,7 +264,7 @@ describe('array', () => {
265264
});
266265

267266
expect(
268-
asWgsl(...Object.values(testLayout.bound)),
267+
tgpu.resolve([...Object.values(testLayout.bound)]),
269268
).toMatchInlineSnapshot(
270269
`"@group(0) @binding(0) var<storage, read> testArray: array<u32>;"`,
271270
);
@@ -276,7 +275,7 @@ describe('array', () => {
276275
const result = d.arrayOf(d.f32, 4)([1, 2, 3, 4]);
277276
});
278277

279-
expect(asWgsl(foo)).toMatchInlineSnapshot(`
278+
expect(tgpu.resolve([foo])).toMatchInlineSnapshot(`
280279
"fn foo() {
281280
var result = array<f32, 4>(1f, 2f, 3f, 4f);
282281
}"
@@ -288,7 +287,7 @@ describe('array', () => {
288287
const result = d.arrayOf(d.f32)(4)([4, 3, 2, 1]);
289288
});
290289

291-
expect(asWgsl(foo)).toMatchInlineSnapshot(`
290+
expect(tgpu.resolve([foo])).toMatchInlineSnapshot(`
292291
"fn foo() {
293292
var result = array<f32, 4>(4f, 3f, 2f, 1f);
294293
}"
@@ -302,7 +301,7 @@ describe('array', () => {
302301
const result = d.arrayOf(d.f32, arraySizeSlot.$)([4, 3, 2, 1]);
303302
});
304303

305-
expect(asWgsl(foo)).toMatchInlineSnapshot(`
304+
expect(tgpu.resolve([foo])).toMatchInlineSnapshot(`
306305
"fn foo() {
307306
var result = array<f32, 4>(4f, 3f, 2f, 1f);
308307
}"
@@ -324,7 +323,7 @@ describe('array', () => {
324323
);
325324
});
326325

327-
expect(asWgsl(foo)).toMatchInlineSnapshot(`
326+
expect(tgpu.resolve([foo])).toMatchInlineSnapshot(`
328327
"fn foo() {
329328
var result = array<f32, 8>(0f, 1f, 2f, 3f, 4f, 5f, 6f, 7f);
330329
}"
@@ -349,7 +348,7 @@ describe('array.length', () => {
349348
}
350349
});
351350

352-
expect(asWgsl(foo)).toMatchInlineSnapshot(`
351+
expect(tgpu.resolve([foo])).toMatchInlineSnapshot(`
353352
"@group(0) @binding(0) var<storage, read_write> values: array<f32>;
354353
355354
fn foo() {
@@ -378,7 +377,7 @@ describe('array.length', () => {
378377
}
379378
});
380379

381-
expect(asWgsl(foo)).toMatchInlineSnapshot(`
380+
expect(tgpu.resolve([foo])).toMatchInlineSnapshot(`
382381
"@group(0) @binding(0) var<storage, read_write> values: array<f32, 128>;
383382
384383
fn foo() {
@@ -405,7 +404,7 @@ describe('array.length', () => {
405404
return arrayLength(layout.$.values);
406405
});
407406

408-
expect(asWgsl(testFn)).toMatchInlineSnapshot(`
407+
expect(tgpu.resolve([testFn])).toMatchInlineSnapshot(`
409408
"fn testFn() -> i32 {
410409
return 5;
411410
}"
@@ -425,7 +424,7 @@ describe('array.length', () => {
425424
return arrayLength(layout.bound.values.value);
426425
});
427426

428-
expect(asWgsl(testFn)).toMatchInlineSnapshot(`
427+
expect(tgpu.resolve([testFn])).toMatchInlineSnapshot(`
429428
"@group(0) @binding(0) var<storage, read_write> values: array<f32>;
430429
431430
fn testFn() -> u32 {

packages/typegpu/tests/attributes.test.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
import { describe, expect, expectTypeOf, it } from 'vitest';
22
import * as d from '../src/data/index.ts';
3-
import { asWgsl } from './utils/parseResolved.ts';
3+
import tgpu from '../src/index.ts';
44

55
describe('attributes', () => {
66
it('adds attributes in the correct order', () => {
@@ -11,7 +11,7 @@ describe('attributes', () => {
1111
c: d.u32,
1212
});
1313

14-
expect(asWgsl(s1)).toContain('@size(8) @align(16) b: u32,');
14+
expect(tgpu.resolve([s1])).toContain('@size(8) @align(16) b: u32,');
1515

1616
expectTypeOf(s1).toEqualTypeOf<
1717
d.WgslStruct<{

packages/typegpu/tests/bindGroupLayout.test.ts

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,6 @@ import {
1919
type UnwrapRuntimeConstructor,
2020
} from '../src/tgpuBindGroupLayout.ts';
2121
import { it } from './utils/extendedIt.ts';
22-
import { asWgsl } from './utils/parseResolved.ts';
2322
import './utils/webgpuGlobals.ts';
2423

2524
const DEFAULT_READONLY_VISIBILITY_FLAGS = GPUShaderStage.COMPUTE |
@@ -223,7 +222,7 @@ describe('TgpuBindGroupLayout', () => {
223222
const main = tgpu.fn([])`() { textureLoad(fooTexture); }`
224223
.$uses({ fooTexture });
225224

226-
expect(asWgsl(main)).toMatchInlineSnapshot(`
225+
expect(tgpu.resolve([main])).toMatchInlineSnapshot(`
227226
"@group(0) @binding(0) var fooTexture: texture_1d<f32>;
228227
229228
fn main() { textureLoad(fooTexture); }"
@@ -247,7 +246,7 @@ describe('TgpuBindGroupLayout', () => {
247246
return Boid(boids[0]!);
248247
};
249248

250-
expect(asWgsl(getFirst)).toMatchInlineSnapshot(`
249+
expect(tgpu.resolve([getFirst])).toMatchInlineSnapshot(`
251250
"struct Boid {
252251
pos: vec3f,
253252
vel: vec3f,

0 commit comments

Comments
 (0)