Skip to content

Commit 45d2832

Browse files
authored
impr: Namespaces use strict naming scheme by default (#1969)
1 parent 048a136 commit 45d2832

3 files changed

Lines changed: 103 additions & 59 deletions

File tree

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -95,7 +95,7 @@ export function getUniqueName(
9595
}
9696

9797
export function namespace(options?: NamespaceOptions | undefined): Namespace {
98-
const { names = 'random' } = options || {};
98+
const { names = 'strict' } = options ?? {};
9999

100100
return new NamespaceImpl(
101101
names === 'strict' ? new StrictNameRegistry() : new RandomNameRegistry(),

packages/typegpu/tests/examples/individual/disco.test.ts

Lines changed: 49 additions & 49 deletions
Original file line numberDiff line numberDiff line change
@@ -18,26 +18,26 @@ describe('disco example', () => {
1818
}, device);
1919

2020
expect(shaderCodes).toMatchInlineSnapshot(`
21-
"struct mainVertex_Output_1 {
21+
"struct mainVertex_Output {
2222
@builtin(position) outPos: vec4f,
2323
@location(0) uv: vec2f,
2424
}
2525
26-
struct mainVertex_Input_2 {
26+
struct mainVertex_Input {
2727
@builtin(vertex_index) vertexIndex: u32,
2828
}
2929
30-
@vertex fn mainVertex_0(_arg_0: mainVertex_Input_2) -> mainVertex_Output_1 {
30+
@vertex fn mainVertex(_arg_0: mainVertex_Input) -> mainVertex_Output {
3131
var pos = array<vec2f, 6>(vec2f(-1, 1), vec2f(-1), vec2f(1, -1), vec2f(-1, 1), vec2f(1, -1), vec2f(1));
3232
var uv = array<vec2f, 6>(vec2f(0, 1), vec2f(), vec2f(1, 0), vec2f(0, 1), vec2f(1, 0), vec2f(1));
33-
return mainVertex_Output_1(vec4f(pos[_arg_0.vertexIndex], 0f, 1f), uv[_arg_0.vertexIndex]);
33+
return mainVertex_Output(vec4f(pos[_arg_0.vertexIndex], 0f, 1f), uv[_arg_0.vertexIndex]);
3434
}
3535
36-
@group(0) @binding(0) var<uniform> resolutionUniform_5: vec2f;
36+
@group(0) @binding(0) var<uniform> resolutionUniform: vec2f;
3737
38-
fn aspectCorrected_4(uv: vec2f) -> vec2f {
38+
fn aspectCorrected(uv: vec2f) -> vec2f {
3939
var v = ((uv - 0.5) * 2);
40-
let aspect = (resolutionUniform_5.x / resolutionUniform_5.y);
40+
let aspect = (resolutionUniform.x / resolutionUniform.y);
4141
if ((aspect > 1f)) {
4242
v.x *= aspect;
4343
}
@@ -47,9 +47,9 @@ describe('disco example', () => {
4747
return v;
4848
}
4949
50-
@group(0) @binding(1) var<uniform> time_6: f32;
50+
@group(0) @binding(1) var<uniform> time: f32;
5151
52-
fn palette_7(t: f32) -> vec3f {
52+
fn palette(t: f32) -> vec3f {
5353
var a = vec3f(0.5, 0.5899999737739563, 0.8500000238418579);
5454
var b = vec3f(0.18000000715255737, 0.41999998688697815, 0.4000000059604645);
5555
var c = vec3f(0.18000000715255737, 0.47999998927116394, 0.4099999964237213);
@@ -58,40 +58,40 @@ describe('disco example', () => {
5858
return (a + (b * expr));
5959
}
6060
61-
fn accumulate_8(acc: vec3f, col: vec3f, weight: f32) -> vec3f {
61+
fn accumulate(acc: vec3f, col: vec3f, weight: f32) -> vec3f {
6262
return (acc + (col * weight));
6363
}
6464
65-
struct mainFragment2_Input_9 {
65+
struct mainFragment2_Input {
6666
@location(0) uv: vec2f,
6767
}
6868
69-
@fragment fn mainFragment2_3(_arg_0: mainFragment2_Input_9) -> @location(0) vec4f {
70-
var originalUv = aspectCorrected_4(_arg_0.uv);
69+
@fragment fn mainFragment2(_arg_0: mainFragment2_Input) -> @location(0) vec4f {
70+
var originalUv = aspectCorrected(_arg_0.uv);
7171
var aspectUv = originalUv;
7272
var accumulatedColor = vec3f();
7373
for (var iteration = 0; (iteration < 3i); iteration++) {
7474
aspectUv = (fract((aspectUv * -0.9)) - 0.5);
7575
var radialLength = (length(aspectUv) * exp((-(length(originalUv)) * 0.5f)));
76-
var paletteColor = palette_7((length(originalUv) + (time_6 * 0.9f)));
77-
radialLength = (sin(((radialLength * 8f) + time_6)) / 8f);
76+
var paletteColor = palette((length(originalUv) + (time * 0.9f)));
77+
radialLength = (sin(((radialLength * 8f) + time)) / 8f);
7878
radialLength = abs(radialLength);
7979
radialLength = smoothstep(0, 0.1, radialLength);
8080
radialLength = (0.1f / radialLength);
81-
accumulatedColor = accumulate_8(accumulatedColor, paletteColor, radialLength);
81+
accumulatedColor = accumulate(accumulatedColor, paletteColor, radialLength);
8282
}
8383
return vec4f(accumulatedColor, 1f);
8484
}
8585
86-
struct mainFragment3_Input_11 {
86+
struct mainFragment3_Input {
8787
@location(0) uv: vec2f,
8888
}
8989
90-
@fragment fn mainFragment3_10(_arg_0: mainFragment3_Input_11) -> @location(0) vec4f {
91-
var originalUv = aspectCorrected_4(_arg_0.uv);
90+
@fragment fn mainFragment3(_arg_0: mainFragment3_Input) -> @location(0) vec4f {
91+
var originalUv = aspectCorrected(_arg_0.uv);
9292
var aspectUv = originalUv;
9393
var accumulatedColor = vec3f();
94-
let baseAngle = (time_6 * 0.3f);
94+
let baseAngle = (time * 0.3f);
9595
let cosBaseAngle = cos(baseAngle);
9696
let sinBaseAngle = sin(baseAngle);
9797
for (var iteration = 0; (iteration < 4i); iteration++) {
@@ -100,29 +100,29 @@ describe('disco example', () => {
100100
let rotatedY = ((aspectUv.x * sinBaseAngle) + (aspectUv.y * cosBaseAngle));
101101
aspectUv = vec2f(rotatedX, rotatedY);
102102
aspectUv = (aspectUv * (1.15f + (iterationF32 * 0.05f)));
103-
aspectUv = (fract((aspectUv * (1.2f * sin(((time_6 * 0.9f) + (iterationF32 * 0.3f)))))) - 0.5);
103+
aspectUv = (fract((aspectUv * (1.2f * sin(((time * 0.9f) + (iterationF32 * 0.3f)))))) - 0.5);
104104
var radialLength = (length(aspectUv) * exp((-(length(originalUv)) * 1.6f)));
105-
var paletteColor = palette_7(((length(originalUv) + (time_6 * 0.8f)) + (iterationF32 * 0.05f)));
106-
radialLength = (sin(((radialLength * 7f) + (time_6 * 0.9f))) / 8f);
105+
var paletteColor = palette(((length(originalUv) + (time * 0.8f)) + (iterationF32 * 0.05f)));
106+
radialLength = (sin(((radialLength * 7f) + (time * 0.9f))) / 8f);
107107
radialLength = abs(radialLength);
108108
radialLength = smoothstep(0, 0.11, radialLength);
109109
radialLength = (0.055f / (radialLength + 1e-5f));
110-
accumulatedColor = accumulate_8(accumulatedColor, paletteColor, radialLength);
110+
accumulatedColor = accumulate(accumulatedColor, paletteColor, radialLength);
111111
}
112112
return vec4f(accumulatedColor, 1f);
113113
}
114114
115-
struct mainFragment4_Input_13 {
115+
struct mainFragment4_Input {
116116
@location(0) uv: vec2f,
117117
}
118118
119-
@fragment fn mainFragment4_12(_arg_0: mainFragment4_Input_13) -> @location(0) vec4f {
120-
var aspectUv = aspectCorrected_4(_arg_0.uv);
119+
@fragment fn mainFragment4(_arg_0: mainFragment4_Input) -> @location(0) vec4f {
120+
var aspectUv = aspectCorrected(_arg_0.uv);
121121
var mirroredUv = ((vec2f(abs((fract((aspectUv.x * 1.2f)) - 0.5f)), abs((fract((aspectUv.y * 1.2f)) - 0.5f))) * 2) - 1);
122122
aspectUv = mirroredUv;
123123
var originalUv = aspectUv;
124124
var accumulatedColor = vec3f();
125-
let time = time_6;
125+
let time = time;
126126
for (var iteration = 0; (iteration < 4i); iteration++) {
127127
let iterationF32 = f32(iteration);
128128
let angle = ((time * (0.4f + (iterationF32 * 0.1f))) + (iterationF32 * 0.9f));
@@ -137,50 +137,50 @@ describe('disco example', () => {
137137
radialLength = abs(radialLength);
138138
radialLength = smoothstep(0, 0.105, radialLength);
139139
radialLength = ((0.058f + (iterationF32 * 6e-3f)) / (radialLength + 1e-5f));
140-
var paletteColor = palette_7(((length(originalUv) + (time * 0.65f)) + (iterationF32 * 0.045f)));
141-
accumulatedColor = accumulate_8(accumulatedColor, paletteColor, radialLength);
140+
var paletteColor = palette(((length(originalUv) + (time * 0.65f)) + (iterationF32 * 0.045f)));
141+
accumulatedColor = accumulate(accumulatedColor, paletteColor, radialLength);
142142
}
143143
return vec4f(accumulatedColor, 1f);
144144
}
145145
146-
struct mainFragment5_Input_15 {
146+
struct mainFragment5_Input {
147147
@location(0) uv: vec2f,
148148
}
149149
150-
@fragment fn mainFragment5_14(_arg_0: mainFragment5_Input_15) -> @location(0) vec4f {
151-
var originalUv = aspectCorrected_4(_arg_0.uv);
150+
@fragment fn mainFragment5(_arg_0: mainFragment5_Input) -> @location(0) vec4f {
151+
var originalUv = aspectCorrected(_arg_0.uv);
152152
var aspectUv = originalUv;
153153
var accumulatedColor = vec3f();
154154
for (var iteration = 0; (iteration < 3i); iteration++) {
155155
let iterationF32 = f32(iteration);
156156
let radius = (length(aspectUv) + 1e-4f);
157-
let angle = ((radius * (8f + (iterationF32 * 2f))) - (time_6 * (1.5f + (iterationF32 * 0.2f))));
157+
let angle = ((radius * (8f + (iterationF32 * 2f))) - (time * (1.5f + (iterationF32 * 0.2f))));
158158
let cosAngle = cos(angle);
159159
let sinAngle = sin(angle);
160160
let rotatedX = ((aspectUv.x * cosAngle) - (aspectUv.y * sinAngle));
161161
let rotatedY = ((aspectUv.x * sinAngle) + (aspectUv.y * cosAngle));
162162
aspectUv = (vec2f(rotatedX, rotatedY) * (-0.85f - (iterationF32 * 0.07f)));
163163
aspectUv = (fract(aspectUv) - 0.5);
164164
var radialLength = (length(aspectUv) * exp((-(length(originalUv)) * (0.4f + (iterationF32 * 0.1f)))));
165-
var paletteColor = palette_7(((length(originalUv) + (time_6 * 0.9f)) + (iterationF32 * 0.08f)));
166-
radialLength = (sin(((radialLength * (6f + iterationF32)) + time_6)) / 8f);
165+
var paletteColor = palette(((length(originalUv) + (time * 0.9f)) + (iterationF32 * 0.08f)));
166+
radialLength = (sin(((radialLength * (6f + iterationF32)) + time)) / 8f);
167167
radialLength = abs(radialLength);
168168
radialLength = smoothstep(0, 0.1, radialLength);
169169
radialLength = ((0.085f + (iterationF32 * 5e-3f)) / (radialLength + 1e-5f));
170-
accumulatedColor = accumulate_8(accumulatedColor, paletteColor, radialLength);
170+
accumulatedColor = accumulate(accumulatedColor, paletteColor, radialLength);
171171
}
172172
return vec4f(accumulatedColor, 1f);
173173
}
174174
175-
struct mainFragment6_Input_17 {
175+
struct mainFragment6_Input {
176176
@location(0) uv: vec2f,
177177
}
178178
179-
@fragment fn mainFragment6_16(_arg_0: mainFragment6_Input_17) -> @location(0) vec4f {
180-
var aspectUv = aspectCorrected_4(_arg_0.uv);
179+
@fragment fn mainFragment6(_arg_0: mainFragment6_Input) -> @location(0) vec4f {
180+
var aspectUv = aspectCorrected(_arg_0.uv);
181181
var originalUv = aspectUv;
182182
var accumulatedColor = vec3f();
183-
let time = time_6;
183+
let time = time;
184184
for (var iteration = 0; (iteration < 5i); iteration++) {
185185
let iterationF32 = f32(iteration);
186186
let angle = ((time * (0.25f + (iterationF32 * 0.05f))) + (iterationF32 * 0.6f));
@@ -195,22 +195,22 @@ describe('disco example', () => {
195195
radialLength = abs(radialLength);
196196
radialLength = smoothstep(0, 0.1, radialLength);
197197
radialLength = ((0.05f + (iterationF32 * 5e-3f)) / (radialLength + 1e-5f));
198-
var paletteColor = palette_7(((length(originalUv) + (time * 0.7f)) + (iterationF32 * 0.04f)));
199-
accumulatedColor = accumulate_8(accumulatedColor, paletteColor, radialLength);
198+
var paletteColor = palette(((length(originalUv) + (time * 0.7f)) + (iterationF32 * 0.04f)));
199+
accumulatedColor = accumulate(accumulatedColor, paletteColor, radialLength);
200200
}
201201
return vec4f(accumulatedColor, 1f);
202202
}
203203
204-
struct mainFragment7_Input_19 {
204+
struct mainFragment7_Input {
205205
@location(0) uv: vec2f,
206206
}
207207
208-
@fragment fn mainFragment7_18(_arg_0: mainFragment7_Input_19) -> @location(0) vec4f {
209-
var aspectUv = aspectCorrected_4(_arg_0.uv);
208+
@fragment fn mainFragment7(_arg_0: mainFragment7_Input) -> @location(0) vec4f {
209+
var aspectUv = aspectCorrected(_arg_0.uv);
210210
aspectUv = (vec2f(abs((fract((aspectUv.x * 1.5f)) - 0.5f)), abs((fract((aspectUv.y * 1.5f)) - 0.5f))) * 2);
211211
var originalUv = aspectUv;
212212
var accumulatedColor = vec3f();
213-
let time = time_6;
213+
let time = time;
214214
for (var iteration = 0; (iteration < 4i); iteration++) {
215215
let iterationF32 = f32(iteration);
216216
let angle = ((iterationF32 * 0.8f) + (time * 0.35f));
@@ -227,8 +227,8 @@ describe('disco example', () => {
227227
radialLength = abs(radialLength);
228228
radialLength = smoothstep(0, 0.11, radialLength);
229229
radialLength = ((0.06f + (iterationF32 * 5e-3f)) / (radialLength + 1e-5f));
230-
var paletteColor = palette_7(((length(originalUv) + (time * 0.75f)) + (iterationF32 * 0.05f)));
231-
accumulatedColor = accumulate_8(accumulatedColor, paletteColor, radialLength);
230+
var paletteColor = palette(((length(originalUv) + (time * 0.75f)) + (iterationF32 * 0.05f)));
231+
accumulatedColor = accumulate(accumulatedColor, paletteColor, radialLength);
232232
}
233233
return vec4f(accumulatedColor, 1f);
234234
}

packages/typegpu/tests/namespace.test.ts

Lines changed: 53 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -24,13 +24,13 @@ describe('tgpu.namespace', () => {
2424
});
2525

2626
expect(code1).toMatchInlineSnapshot(`
27-
"struct Boid_0 {
27+
"struct Boid {
2828
pos: vec3f,
29-
}var<private> foo: Boid_0"
29+
}var<private> foo: Boid"
3030
`);
3131

3232
// Should be just the template, as Boid was already defined in the namespace
33-
expect(code2).toMatchInlineSnapshot(`"var<private> foo: Boid_0"`);
33+
expect(code2).toMatchInlineSnapshot(`"var<private> foo: Boid"`);
3434
});
3535

3636
it('defines transitive dependencies only once', () => {
@@ -53,17 +53,17 @@ describe('tgpu.namespace', () => {
5353
const code2 = tgpu.resolve([updateBoid], { names });
5454

5555
expect(code1).toMatchInlineSnapshot(`
56-
"struct Boid_1 {
56+
"struct Boid {
5757
pos: vec3f,
5858
}
5959
60-
fn createBoid_0() -> Boid_1 {
61-
return Boid_1();
60+
fn createBoid() -> Boid {
61+
return Boid();
6262
}"
6363
`);
6464

6565
expect(code2).toMatchInlineSnapshot(`
66-
"fn updateBoid_2(boid: ptr<function, Boid_1>) {
66+
"fn updateBoid(boid: ptr<function, Boid>) {
6767
(*boid).pos.x += 1f;
6868
}"
6969
`);
@@ -82,10 +82,10 @@ describe('tgpu.namespace', () => {
8282
const code = tgpu.resolve([Boid], { names });
8383

8484
expect(listener).toHaveBeenCalledTimes(1);
85-
expect(listener).toHaveBeenCalledWith({ name: 'Boid_0', target: Boid });
85+
expect(listener).toHaveBeenCalledWith({ name: 'Boid', target: Boid });
8686

8787
expect(code).toMatchInlineSnapshot(`
88-
"struct Boid_0 {
88+
"struct Boid {
8989
pos: vec3f,
9090
}"
9191
`);
@@ -96,4 +96,48 @@ describe('tgpu.namespace', () => {
9696
expect(listener).toHaveBeenCalledTimes(1);
9797
expect(code2).toMatchInlineSnapshot(`""`);
9898
});
99+
100+
it('handles name collision', () => {
101+
let code1: string, code2: string;
102+
const names = tgpu['~unstable'].namespace();
103+
{
104+
const Boid = d.struct({
105+
pos: d.vec3f,
106+
});
107+
const createBoid = tgpu.fn([], Boid)(() => {
108+
return Boid();
109+
});
110+
code1 = tgpu.resolve([createBoid], { names });
111+
}
112+
113+
{
114+
const Boid = d.struct({
115+
pos: d.vec3i,
116+
});
117+
const createBoid = tgpu.fn([], Boid)(() => {
118+
return Boid();
119+
});
120+
code2 = tgpu.resolve([createBoid], { names });
121+
}
122+
123+
expect(code1).toMatchInlineSnapshot(`
124+
"struct Boid {
125+
pos: vec3f,
126+
}
127+
128+
fn createBoid() -> Boid {
129+
return Boid();
130+
}"
131+
`);
132+
133+
expect(code2).toMatchInlineSnapshot(`
134+
"struct Boid_1 {
135+
pos: vec3i,
136+
}
137+
138+
fn createBoid_1() -> Boid_1 {
139+
return Boid_1();
140+
}"
141+
`);
142+
});
99143
});

0 commit comments

Comments
 (0)