Skip to content

Commit d45bde7

Browse files
authored
A second set of circular dependency fixes. (#292)
1 parent 23f5201 commit d45bde7

18 files changed

+134
-117
lines changed

packages/typegpu/src/builtin.ts

Lines changed: 16 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
1-
import { TgpuIdentifier } from './tgpuIdentifier';
1+
import { identifier } from './tgpuIdentifier';
2+
import type { Builtin } from './types';
23

34
export const builtin = {
45
vertexIndex: Symbol('builtin_vertexIndex'),
@@ -17,112 +18,104 @@ export const builtin = {
1718
numWorkgroups: Symbol('builtin_numWorkgroups'),
1819
} as const;
1920

20-
export interface Builtin {
21-
symbol: symbol;
22-
name: string;
23-
stage: 'vertex' | 'fragment' | 'compute';
24-
direction: 'input' | 'output';
25-
identifier: TgpuIdentifier;
26-
}
27-
2821
const builtinSymbolToObj: Record<symbol, Builtin> = {
2922
[builtin.vertexIndex]: {
3023
symbol: builtin.vertexIndex,
3124
name: 'vertex_index',
3225
stage: 'vertex',
3326
direction: 'input',
34-
identifier: new TgpuIdentifier().$name('vertex_index'),
27+
identifier: identifier().$name('vertex_index'),
3528
},
3629
[builtin.instanceIndex]: {
3730
symbol: builtin.instanceIndex,
3831
name: 'instance_index',
3932
stage: 'vertex',
4033
direction: 'input',
41-
identifier: new TgpuIdentifier().$name('instance_index'),
34+
identifier: identifier().$name('instance_index'),
4235
},
4336
[builtin.position]: {
4437
symbol: builtin.position,
4538
name: 'position',
4639
stage: 'vertex',
4740
direction: 'output',
48-
identifier: new TgpuIdentifier().$name('position'),
41+
identifier: identifier().$name('position'),
4942
},
5043
[builtin.clipDistances]: {
5144
symbol: builtin.clipDistances,
5245
name: 'clip_distances',
5346
stage: 'vertex',
5447
direction: 'output',
55-
identifier: new TgpuIdentifier().$name('clip_distances'),
48+
identifier: identifier().$name('clip_distances'),
5649
},
5750
[builtin.frontFacing]: {
5851
symbol: builtin.frontFacing,
5952
name: 'front_facing',
6053
stage: 'fragment',
6154
direction: 'input',
62-
identifier: new TgpuIdentifier().$name('front_facing'),
55+
identifier: identifier().$name('front_facing'),
6356
},
6457
[builtin.fragDepth]: {
6558
symbol: builtin.fragDepth,
6659
name: 'frag_depth',
6760
stage: 'fragment',
6861
direction: 'output',
69-
identifier: new TgpuIdentifier().$name('frag_depth'),
62+
identifier: identifier().$name('frag_depth'),
7063
},
7164
[builtin.sampleIndex]: {
7265
symbol: builtin.sampleIndex,
7366
name: 'sample_index',
7467
stage: 'fragment',
7568
direction: 'input',
76-
identifier: new TgpuIdentifier().$name('sample_index'),
69+
identifier: identifier().$name('sample_index'),
7770
},
7871
[builtin.sampleMask]: {
7972
symbol: builtin.sampleMask,
8073
name: 'sample_mask',
8174
stage: 'fragment',
8275
direction: 'input',
83-
identifier: new TgpuIdentifier().$name('sample_mask'),
76+
identifier: identifier().$name('sample_mask'),
8477
},
8578
[builtin.fragment]: {
8679
symbol: builtin.fragment,
8780
name: 'fragment',
8881
stage: 'fragment',
8982
direction: 'input',
90-
identifier: new TgpuIdentifier().$name('fragment'),
83+
identifier: identifier().$name('fragment'),
9184
},
9285
[builtin.localInvocationId]: {
9386
symbol: builtin.localInvocationId,
9487
name: 'local_invocation_id',
9588
stage: 'compute',
9689
direction: 'input',
97-
identifier: new TgpuIdentifier().$name('local_invocation_id'),
90+
identifier: identifier().$name('local_invocation_id'),
9891
},
9992
[builtin.localInvocationIndex]: {
10093
symbol: builtin.localInvocationIndex,
10194
name: 'local_invocation_index',
10295
stage: 'compute',
10396
direction: 'input',
104-
identifier: new TgpuIdentifier().$name('local_invocation_index'),
97+
identifier: identifier().$name('local_invocation_index'),
10598
},
10699
[builtin.globalInvocationId]: {
107100
symbol: builtin.globalInvocationId,
108101
name: 'global_invocation_id',
109102
stage: 'compute',
110103
direction: 'input',
111-
identifier: new TgpuIdentifier().$name('global_invocation_id'),
104+
identifier: identifier().$name('global_invocation_id'),
112105
},
113106
[builtin.workgroupId]: {
114107
symbol: builtin.workgroupId,
115108
name: 'workgroup_id',
116109
stage: 'compute',
117110
direction: 'input',
118-
identifier: new TgpuIdentifier().$name('workgroup_id'),
111+
identifier: identifier().$name('workgroup_id'),
119112
},
120113
[builtin.numWorkgroups]: {
121114
symbol: builtin.numWorkgroups,
122115
name: 'num_workgroups',
123116
stage: 'compute',
124117
direction: 'input',
125-
identifier: new TgpuIdentifier().$name('num_workgroups'),
118+
identifier: identifier().$name('num_workgroups'),
126119
},
127120
};
128121

packages/typegpu/src/data/struct.ts

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ import {
1313
import { RecursiveDataTypeError } from '../errors';
1414
import type { TgpuNamable } from '../namable';
1515
import { code } from '../tgpuCode';
16-
import { TgpuIdentifier } from '../tgpuIdentifier';
16+
import { identifier } from '../tgpuIdentifier';
1717
import type { AnyTgpuData, ResolutionCtx, TgpuData } from '../types';
1818
import { TgpuAlignedImpl } from './align';
1919
import alignIO from './alignIO';
@@ -87,15 +87,15 @@ class TgpuStructImpl<TProps extends Record<string, AnyTgpuData>>
8787
}
8888

8989
resolve(ctx: ResolutionCtx): string {
90-
const identifier = new TgpuIdentifier().$name(this._label);
90+
const ident = identifier().$name(this._label);
9191

9292
ctx.addDeclaration(code`
93-
struct ${identifier} {
93+
struct ${ident} {
9494
${Object.entries(this._properties).map(([key, field]) => code`${getAttribute(field) ?? ''}${key}: ${field},\n`)}
9595
}
9696
`);
9797

98-
return ctx.resolve(identifier);
98+
return ctx.resolve(ident);
9999
}
100100
}
101101

packages/typegpu/src/experimental/index.ts

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,6 @@ export type {
3535
TgpuBufferMutable,
3636
TgpuBufferVertex,
3737
} from '../tgpuBufferUsage';
38-
export type { TgpuCode } from '../tgpuCode';
3938
export type { TgpuConst } from '../tgpuConstant';
4039
export type { TgpuFn } from '../tgpuFunction';
4140
export type { TgpuPlum } from '../tgpuPlumTypes';

packages/typegpu/src/macro/repeat.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
import { code } from '../tgpuCode';
2-
import { TgpuIdentifier } from '../tgpuIdentifier';
2+
import { identifier } from '../tgpuIdentifier';
33
import type { Eventual, Wgsl } from '../types';
44

55
export function repeat(
@@ -21,7 +21,7 @@ export function repeat(
2121
const snippetValue = get(snippet);
2222
2323
if (typeof countValue !== 'number') {
24-
const index = new TgpuIdentifier().$name('i');
24+
const index = identifier().$name('i');
2525
2626
if (typeof snippetValue === 'function') {
2727
return code`

packages/typegpu/src/programBuilder.ts

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -5,14 +5,19 @@ import {
55
getUsedBuiltins,
66
getUsedBuiltinsNamed,
77
} from './builtin';
8-
import { builtinToType } from './builtinTypes';
8+
import { builtinToType } from './builtinDataTypes';
99
import type { SimpleTgpuData, TgpuArray } from './data';
1010
import { type NameRegistry, RandomNameRegistry } from './nameRegistry';
1111
import { ResolutionCtxImpl } from './resolutionCtx';
1212
import type { TgpuBufferVertex } from './tgpuBufferUsage';
13-
import { type BoundTgpuCode, type TgpuCode, code } from './tgpuCode';
13+
import { code } from './tgpuCode';
1414
import type { TgpuRuntime } from './tgpuRuntime';
15-
import type { AnyTgpuData, TgpuResolvable } from './types';
15+
import type {
16+
AnyTgpuData,
17+
BoundTgpuCode,
18+
TgpuCode,
19+
TgpuResolvable,
20+
} from './types';
1621

1722
export type Program = {
1823
readonly bindGroupResolver: BindGroupResolver;

packages/typegpu/src/resolutionCtx.ts

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,8 @@
1-
import type { Builtin } from './builtin';
21
import { MissingSlotValueError, ResolutionError } from './errors';
32
import type { NameRegistry } from './nameRegistry';
43
import { code } from './tgpuCode';
5-
import type { TgpuIdentifier } from './tgpuIdentifier';
64
import { isTextureView } from './tgpuTexture';
5+
import type { Builtin, TgpuIdentifier } from './types';
76
import type {
87
BufferUsage,
98
Eventual,

packages/typegpu/src/tgpuBufferUsage.ts

Lines changed: 7 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ import {
88
isUsableAsUniform,
99
isUsableAsVertex,
1010
} from './tgpuBuffer';
11-
import { TgpuIdentifier } from './tgpuIdentifier';
11+
import { identifier } from './tgpuIdentifier';
1212
import type {
1313
AnyTgpuData,
1414
BufferUsage,
@@ -59,11 +59,9 @@ class TgpuBufferUsageImpl<TData extends AnyTgpuData, TUsage extends BufferUsage>
5959
}
6060

6161
resolve(ctx: ResolutionCtx): string {
62-
const identifier = new TgpuIdentifier().$name(this.label);
63-
64-
ctx.addBinding(this, identifier);
65-
66-
return ctx.resolve(identifier);
62+
const ident = identifier().$name(this.label);
63+
ctx.addBinding(this, ident);
64+
return ctx.resolve(ident);
6765
}
6866

6967
toString(): string {
@@ -103,9 +101,9 @@ class TgpuBufferVertexImpl<TData extends AnyTgpuData>
103101
}
104102

105103
resolve(ctx: ResolutionCtx): string {
106-
const identifier = new TgpuIdentifier().$name(this.label);
107-
ctx.addBinding(this, identifier);
108-
return ctx.resolve(identifier);
104+
const ident = identifier().$name(this.label);
105+
ctx.addBinding(this, ident);
106+
return ctx.resolve(ident);
109107
}
110108

111109
toString(): string {

packages/typegpu/src/tgpuCode.ts

Lines changed: 2 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,11 @@
11
import { getBuiltinInfo } from './builtin';
2-
import type { TgpuNamable } from './namable';
32
import {
3+
type BoundTgpuCode,
44
type Eventual,
55
type InlineResolve,
66
type ResolutionCtx,
77
type SlotValuePair,
8-
type TgpuResolvable,
8+
type TgpuCode,
99
type TgpuSlot,
1010
type Wgsl,
1111
isResolvable,
@@ -15,12 +15,6 @@ import {
1515
// Public API
1616
// ----------
1717

18-
export interface TgpuCode extends TgpuResolvable, TgpuNamable {
19-
with<T>(slot: TgpuSlot<T>, value: Eventual<T>): BoundTgpuCode;
20-
}
21-
22-
export type BoundTgpuCode = Omit<TgpuCode, '$name'>;
23-
2418
export function code(
2519
strings: TemplateStringsArray,
2620
...params: (Wgsl | Wgsl[] | InlineResolve)[]

packages/typegpu/src/tgpuConstant.ts

Lines changed: 4 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
import type { TgpuNamable } from './namable';
22
import { code } from './tgpuCode';
3-
import { TgpuIdentifier } from './tgpuIdentifier';
3+
import { identifier } from './tgpuIdentifier';
44
import type { ResolutionCtx, TgpuResolvable, Wgsl } from './types';
55

66
// ----------
@@ -36,10 +36,8 @@ class TgpuConstImpl implements TgpuConst {
3636
}
3737

3838
resolve(ctx: ResolutionCtx): string {
39-
const identifier = new TgpuIdentifier().$name(this._label);
40-
41-
ctx.addDeclaration(code`const ${identifier} = ${this.expr};`);
42-
43-
return ctx.resolve(identifier);
39+
const ident = identifier().$name(this._label);
40+
ctx.addDeclaration(code`const ${ident} = ${this.expr};`);
41+
return ctx.resolve(ident);
4442
}
4543
}

packages/typegpu/src/tgpuFunction.ts

Lines changed: 4 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
import type { TgpuNamable } from './namable';
22
import { code } from './tgpuCode';
3-
import { TgpuIdentifier } from './tgpuIdentifier';
3+
import { identifier } from './tgpuIdentifier';
44
import type {
55
Eventual,
66
InlineResolve,
@@ -47,11 +47,9 @@ class TgpuFnImpl implements TgpuFn {
4747
}
4848

4949
resolve(ctx: ResolutionCtx): string {
50-
const identifier = new TgpuIdentifier().$name(this._label);
51-
52-
ctx.addDeclaration(code`fn ${identifier}${this.body}`.$name(this._label));
53-
54-
return ctx.resolve(identifier);
50+
const ident = identifier().$name(this._label);
51+
ctx.addDeclaration(code`fn ${ident}${this.body}`.$name(this._label));
52+
return ctx.resolve(ident);
5553
}
5654

5755
with<T>(slot: TgpuSlot<T>, value: T): BoundTgpuFn {

packages/typegpu/src/tgpuFunctionExperimental.ts

Lines changed: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,13 @@
11
import { type AsCallable, CallableImpl } from './callable';
22
import type { TgpuNamable } from './namable';
33
import { code } from './tgpuCode';
4-
import { TgpuIdentifier } from './tgpuIdentifier';
4+
import { identifier } from './tgpuIdentifier';
55
import { isPointer } from './types';
66
import type {
77
AnyTgpuData,
88
ResolutionCtx,
99
TgpuFnArgument,
10+
TgpuIdentifier,
1011
TgpuResolvable,
1112
TgpuValue,
1213
Wgsl,
@@ -31,7 +32,7 @@ export function fn<
3132
TReturn extends AnyTgpuData | undefined = undefined,
3233
>(argTypes: TArgTypes, returnType?: TReturn) {
3334
const argPairs = argTypes.map(
34-
(argType) => [new TgpuIdentifier(), argType] as const,
35+
(argType) => [identifier(), argType] as const,
3536
) as PairsFromTypes<TArgTypes>;
3637

3738
const argValues = argPairs.map(
@@ -128,7 +129,7 @@ class TgpuFnImpl<
128129
}
129130

130131
resolve(ctx: ResolutionCtx): string {
131-
const identifier = new TgpuIdentifier().$name(this._label);
132+
const fnIdent = identifier().$name(this._label);
132133

133134
const argsCode = this.argPairs.map(([ident, argType], idx) => {
134135
const comma = idx < this.argPairs.length - 1 ? ', ' : '';
@@ -141,16 +142,16 @@ class TgpuFnImpl<
141142
});
142143

143144
if (this.returnType !== undefined) {
144-
ctx.addDeclaration(code`fn ${identifier}(${argsCode}) -> ${this.returnType} {
145+
ctx.addDeclaration(code`fn ${fnIdent}(${argsCode}) -> ${this.returnType} {
145146
${this.body}
146147
}`);
147148
} else {
148-
ctx.addDeclaration(code`fn ${identifier}(${argsCode}) {
149+
ctx.addDeclaration(code`fn ${fnIdent}(${argsCode}) {
149150
${this.body}
150151
}`);
151152
}
152153

153-
return ctx.resolve(identifier);
154+
return ctx.resolve(fnIdent);
154155
}
155156

156157
_call(

0 commit comments

Comments
 (0)