Skip to content

Commit

Permalink
A second set of circular dependency fixes. (#292)
Browse files Browse the repository at this point in the history
  • Loading branch information
iwoplaza authored Sep 10, 2024
1 parent 23f5201 commit d45bde7
Show file tree
Hide file tree
Showing 18 changed files with 134 additions and 117 deletions.
39 changes: 16 additions & 23 deletions packages/typegpu/src/builtin.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import { TgpuIdentifier } from './tgpuIdentifier';
import { identifier } from './tgpuIdentifier';
import type { Builtin } from './types';

export const builtin = {
vertexIndex: Symbol('builtin_vertexIndex'),
Expand All @@ -17,112 +18,104 @@ export const builtin = {
numWorkgroups: Symbol('builtin_numWorkgroups'),
} as const;

export interface Builtin {
symbol: symbol;
name: string;
stage: 'vertex' | 'fragment' | 'compute';
direction: 'input' | 'output';
identifier: TgpuIdentifier;
}

const builtinSymbolToObj: Record<symbol, Builtin> = {
[builtin.vertexIndex]: {
symbol: builtin.vertexIndex,
name: 'vertex_index',
stage: 'vertex',
direction: 'input',
identifier: new TgpuIdentifier().$name('vertex_index'),
identifier: identifier().$name('vertex_index'),
},
[builtin.instanceIndex]: {
symbol: builtin.instanceIndex,
name: 'instance_index',
stage: 'vertex',
direction: 'input',
identifier: new TgpuIdentifier().$name('instance_index'),
identifier: identifier().$name('instance_index'),
},
[builtin.position]: {
symbol: builtin.position,
name: 'position',
stage: 'vertex',
direction: 'output',
identifier: new TgpuIdentifier().$name('position'),
identifier: identifier().$name('position'),
},
[builtin.clipDistances]: {
symbol: builtin.clipDistances,
name: 'clip_distances',
stage: 'vertex',
direction: 'output',
identifier: new TgpuIdentifier().$name('clip_distances'),
identifier: identifier().$name('clip_distances'),
},
[builtin.frontFacing]: {
symbol: builtin.frontFacing,
name: 'front_facing',
stage: 'fragment',
direction: 'input',
identifier: new TgpuIdentifier().$name('front_facing'),
identifier: identifier().$name('front_facing'),
},
[builtin.fragDepth]: {
symbol: builtin.fragDepth,
name: 'frag_depth',
stage: 'fragment',
direction: 'output',
identifier: new TgpuIdentifier().$name('frag_depth'),
identifier: identifier().$name('frag_depth'),
},
[builtin.sampleIndex]: {
symbol: builtin.sampleIndex,
name: 'sample_index',
stage: 'fragment',
direction: 'input',
identifier: new TgpuIdentifier().$name('sample_index'),
identifier: identifier().$name('sample_index'),
},
[builtin.sampleMask]: {
symbol: builtin.sampleMask,
name: 'sample_mask',
stage: 'fragment',
direction: 'input',
identifier: new TgpuIdentifier().$name('sample_mask'),
identifier: identifier().$name('sample_mask'),
},
[builtin.fragment]: {
symbol: builtin.fragment,
name: 'fragment',
stage: 'fragment',
direction: 'input',
identifier: new TgpuIdentifier().$name('fragment'),
identifier: identifier().$name('fragment'),
},
[builtin.localInvocationId]: {
symbol: builtin.localInvocationId,
name: 'local_invocation_id',
stage: 'compute',
direction: 'input',
identifier: new TgpuIdentifier().$name('local_invocation_id'),
identifier: identifier().$name('local_invocation_id'),
},
[builtin.localInvocationIndex]: {
symbol: builtin.localInvocationIndex,
name: 'local_invocation_index',
stage: 'compute',
direction: 'input',
identifier: new TgpuIdentifier().$name('local_invocation_index'),
identifier: identifier().$name('local_invocation_index'),
},
[builtin.globalInvocationId]: {
symbol: builtin.globalInvocationId,
name: 'global_invocation_id',
stage: 'compute',
direction: 'input',
identifier: new TgpuIdentifier().$name('global_invocation_id'),
identifier: identifier().$name('global_invocation_id'),
},
[builtin.workgroupId]: {
symbol: builtin.workgroupId,
name: 'workgroup_id',
stage: 'compute',
direction: 'input',
identifier: new TgpuIdentifier().$name('workgroup_id'),
identifier: identifier().$name('workgroup_id'),
},
[builtin.numWorkgroups]: {
symbol: builtin.numWorkgroups,
name: 'num_workgroups',
stage: 'compute',
direction: 'input',
identifier: new TgpuIdentifier().$name('num_workgroups'),
identifier: identifier().$name('num_workgroups'),
},
};

Expand Down
File renamed without changes.
8 changes: 4 additions & 4 deletions packages/typegpu/src/data/struct.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ import {
import { RecursiveDataTypeError } from '../errors';
import type { TgpuNamable } from '../namable';
import { code } from '../tgpuCode';
import { TgpuIdentifier } from '../tgpuIdentifier';
import { identifier } from '../tgpuIdentifier';
import type { AnyTgpuData, ResolutionCtx, TgpuData } from '../types';
import { TgpuAlignedImpl } from './align';
import alignIO from './alignIO';
Expand Down Expand Up @@ -87,15 +87,15 @@ class TgpuStructImpl<TProps extends Record<string, AnyTgpuData>>
}

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

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

return ctx.resolve(identifier);
return ctx.resolve(ident);
}
}

Expand Down
1 change: 0 additions & 1 deletion packages/typegpu/src/experimental/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,6 @@ export type {
TgpuBufferMutable,
TgpuBufferVertex,
} from '../tgpuBufferUsage';
export type { TgpuCode } from '../tgpuCode';
export type { TgpuConst } from '../tgpuConstant';
export type { TgpuFn } from '../tgpuFunction';
export type { TgpuPlum } from '../tgpuPlumTypes';
Expand Down
4 changes: 2 additions & 2 deletions packages/typegpu/src/macro/repeat.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { code } from '../tgpuCode';
import { TgpuIdentifier } from '../tgpuIdentifier';
import { identifier } from '../tgpuIdentifier';
import type { Eventual, Wgsl } from '../types';

export function repeat(
Expand All @@ -21,7 +21,7 @@ export function repeat(
const snippetValue = get(snippet);
if (typeof countValue !== 'number') {
const index = new TgpuIdentifier().$name('i');
const index = identifier().$name('i');
if (typeof snippetValue === 'function') {
return code`
Expand Down
11 changes: 8 additions & 3 deletions packages/typegpu/src/programBuilder.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,14 +5,19 @@ import {
getUsedBuiltins,
getUsedBuiltinsNamed,
} from './builtin';
import { builtinToType } from './builtinTypes';
import { builtinToType } from './builtinDataTypes';
import type { SimpleTgpuData, TgpuArray } from './data';
import { type NameRegistry, RandomNameRegistry } from './nameRegistry';
import { ResolutionCtxImpl } from './resolutionCtx';
import type { TgpuBufferVertex } from './tgpuBufferUsage';
import { type BoundTgpuCode, type TgpuCode, code } from './tgpuCode';
import { code } from './tgpuCode';
import type { TgpuRuntime } from './tgpuRuntime';
import type { AnyTgpuData, TgpuResolvable } from './types';
import type {
AnyTgpuData,
BoundTgpuCode,
TgpuCode,
TgpuResolvable,
} from './types';

export type Program = {
readonly bindGroupResolver: BindGroupResolver;
Expand Down
3 changes: 1 addition & 2 deletions packages/typegpu/src/resolutionCtx.ts
Original file line number Diff line number Diff line change
@@ -1,9 +1,8 @@
import type { Builtin } from './builtin';
import { MissingSlotValueError, ResolutionError } from './errors';
import type { NameRegistry } from './nameRegistry';
import { code } from './tgpuCode';
import type { TgpuIdentifier } from './tgpuIdentifier';
import { isTextureView } from './tgpuTexture';
import type { Builtin, TgpuIdentifier } from './types';
import type {
BufferUsage,
Eventual,
Expand Down
16 changes: 7 additions & 9 deletions packages/typegpu/src/tgpuBufferUsage.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ import {
isUsableAsUniform,
isUsableAsVertex,
} from './tgpuBuffer';
import { TgpuIdentifier } from './tgpuIdentifier';
import { identifier } from './tgpuIdentifier';
import type {
AnyTgpuData,
BufferUsage,
Expand Down Expand Up @@ -59,11 +59,9 @@ class TgpuBufferUsageImpl<TData extends AnyTgpuData, TUsage extends BufferUsage>
}

resolve(ctx: ResolutionCtx): string {
const identifier = new TgpuIdentifier().$name(this.label);

ctx.addBinding(this, identifier);

return ctx.resolve(identifier);
const ident = identifier().$name(this.label);
ctx.addBinding(this, ident);
return ctx.resolve(ident);
}

toString(): string {
Expand Down Expand Up @@ -103,9 +101,9 @@ class TgpuBufferVertexImpl<TData extends AnyTgpuData>
}

resolve(ctx: ResolutionCtx): string {
const identifier = new TgpuIdentifier().$name(this.label);
ctx.addBinding(this, identifier);
return ctx.resolve(identifier);
const ident = identifier().$name(this.label);
ctx.addBinding(this, ident);
return ctx.resolve(ident);
}

toString(): string {
Expand Down
10 changes: 2 additions & 8 deletions packages/typegpu/src/tgpuCode.ts
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
import { getBuiltinInfo } from './builtin';
import type { TgpuNamable } from './namable';
import {
type BoundTgpuCode,
type Eventual,
type InlineResolve,
type ResolutionCtx,
type SlotValuePair,
type TgpuResolvable,
type TgpuCode,
type TgpuSlot,
type Wgsl,
isResolvable,
Expand All @@ -15,12 +15,6 @@ import {
// Public API
// ----------

export interface TgpuCode extends TgpuResolvable, TgpuNamable {
with<T>(slot: TgpuSlot<T>, value: Eventual<T>): BoundTgpuCode;
}

export type BoundTgpuCode = Omit<TgpuCode, '$name'>;

export function code(
strings: TemplateStringsArray,
...params: (Wgsl | Wgsl[] | InlineResolve)[]
Expand Down
10 changes: 4 additions & 6 deletions packages/typegpu/src/tgpuConstant.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import type { TgpuNamable } from './namable';
import { code } from './tgpuCode';
import { TgpuIdentifier } from './tgpuIdentifier';
import { identifier } from './tgpuIdentifier';
import type { ResolutionCtx, TgpuResolvable, Wgsl } from './types';

// ----------
Expand Down Expand Up @@ -36,10 +36,8 @@ class TgpuConstImpl implements TgpuConst {
}

resolve(ctx: ResolutionCtx): string {
const identifier = new TgpuIdentifier().$name(this._label);

ctx.addDeclaration(code`const ${identifier} = ${this.expr};`);

return ctx.resolve(identifier);
const ident = identifier().$name(this._label);
ctx.addDeclaration(code`const ${ident} = ${this.expr};`);
return ctx.resolve(ident);
}
}
10 changes: 4 additions & 6 deletions packages/typegpu/src/tgpuFunction.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import type { TgpuNamable } from './namable';
import { code } from './tgpuCode';
import { TgpuIdentifier } from './tgpuIdentifier';
import { identifier } from './tgpuIdentifier';
import type {
Eventual,
InlineResolve,
Expand Down Expand Up @@ -47,11 +47,9 @@ class TgpuFnImpl implements TgpuFn {
}

resolve(ctx: ResolutionCtx): string {
const identifier = new TgpuIdentifier().$name(this._label);

ctx.addDeclaration(code`fn ${identifier}${this.body}`.$name(this._label));

return ctx.resolve(identifier);
const ident = identifier().$name(this._label);
ctx.addDeclaration(code`fn ${ident}${this.body}`.$name(this._label));
return ctx.resolve(ident);
}

with<T>(slot: TgpuSlot<T>, value: T): BoundTgpuFn {
Expand Down
13 changes: 7 additions & 6 deletions packages/typegpu/src/tgpuFunctionExperimental.ts
Original file line number Diff line number Diff line change
@@ -1,12 +1,13 @@
import { type AsCallable, CallableImpl } from './callable';
import type { TgpuNamable } from './namable';
import { code } from './tgpuCode';
import { TgpuIdentifier } from './tgpuIdentifier';
import { identifier } from './tgpuIdentifier';
import { isPointer } from './types';
import type {
AnyTgpuData,
ResolutionCtx,
TgpuFnArgument,
TgpuIdentifier,
TgpuResolvable,
TgpuValue,
Wgsl,
Expand All @@ -31,7 +32,7 @@ export function fn<
TReturn extends AnyTgpuData | undefined = undefined,
>(argTypes: TArgTypes, returnType?: TReturn) {
const argPairs = argTypes.map(
(argType) => [new TgpuIdentifier(), argType] as const,
(argType) => [identifier(), argType] as const,
) as PairsFromTypes<TArgTypes>;

const argValues = argPairs.map(
Expand Down Expand Up @@ -128,7 +129,7 @@ class TgpuFnImpl<
}

resolve(ctx: ResolutionCtx): string {
const identifier = new TgpuIdentifier().$name(this._label);
const fnIdent = identifier().$name(this._label);

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

if (this.returnType !== undefined) {
ctx.addDeclaration(code`fn ${identifier}(${argsCode}) -> ${this.returnType} {
ctx.addDeclaration(code`fn ${fnIdent}(${argsCode}) -> ${this.returnType} {
${this.body}
}`);
} else {
ctx.addDeclaration(code`fn ${identifier}(${argsCode}) {
ctx.addDeclaration(code`fn ${fnIdent}(${argsCode}) {
${this.body}
}`);
}

return ctx.resolve(identifier);
return ctx.resolve(fnIdent);
}

_call(
Expand Down
Loading

0 comments on commit d45bde7

Please sign in to comment.