Skip to content

Commit 974e9f9

Browse files
authored
refactor: Double-down on BaseData (#2092)
1 parent 0d01420 commit 974e9f9

Some content is hidden

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

54 files changed

+359
-320
lines changed

packages/typegpu/src/builtin.ts

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,6 @@ import type { LooseDecorated } from './data/dataTypes.ts';
44
import { bool, f32, u32 } from './data/numeric.ts';
55
import { vec3u, vec4f } from './data/vector.ts';
66
import type {
7-
AnyWgslData,
87
BaseData,
98
Bool,
109
Builtin,
@@ -62,7 +61,7 @@ export type BuiltinSubgroupId = Decorated<U32, [Builtin<'subgroup_id'>]>;
6261
export type BuiltinNumSubgroups = Decorated<U32, [Builtin<'num_subgroups'>]>;
6362

6463
function defineBuiltin<T extends Decorated | LooseDecorated>(
65-
dataType: AnyWgslData,
64+
dataType: BaseData,
6665
value: T['attribs'][0] extends { params: [infer TValue] } ? TValue : never,
6766
): T {
6867
return attribute(dataType, {

packages/typegpu/src/core/buffer/buffer.ts

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -144,30 +144,30 @@ export function INTERNAL_createBuffer<TData extends AnyData>(
144144
return new TgpuBufferImpl(group, typeSchema, initialOrBuffer);
145145
}
146146

147-
export function isBuffer<T extends TgpuBuffer<AnyData>>(
147+
export function isBuffer<T extends TgpuBuffer<BaseData>>(
148148
value: T | unknown,
149149
): value is T {
150-
return (value as TgpuBuffer<AnyData>).resourceType === 'buffer';
150+
return (value as T).resourceType === 'buffer';
151151
}
152152

153-
export function isUsableAsVertex<T extends TgpuBuffer<AnyData>>(
153+
export function isUsableAsVertex<T extends TgpuBuffer<BaseData>>(
154154
buffer: T,
155155
): buffer is T & VertexFlag {
156-
return !!(buffer as unknown as VertexFlag).usableAsVertex;
156+
return !!(buffer as T).usableAsVertex;
157157
}
158158

159-
export function isUsableAsIndex<T extends TgpuBuffer<AnyData>>(
159+
export function isUsableAsIndex<T extends TgpuBuffer<BaseData>>(
160160
buffer: T,
161161
): buffer is T & IndexFlag {
162-
return !!(buffer as unknown as IndexFlag).usableAsIndex;
162+
return !!(buffer as T).usableAsIndex;
163163
}
164164

165165
// --------------
166166
// Implementation
167167
// --------------
168168
const endianness = getSystemEndianness();
169169

170-
class TgpuBufferImpl<TData extends AnyData> implements TgpuBuffer<TData> {
170+
class TgpuBufferImpl<TData extends BaseData> implements TgpuBuffer<TData> {
171171
public readonly [$internal] = true;
172172
public readonly resourceType = 'buffer';
173173
public flags: GPUBufferUsageFlags = GPUBufferUsage.COPY_DST |

packages/typegpu/src/core/buffer/bufferUsage.ts

Lines changed: 10 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,3 @@
1-
import type { AnyData } from '../../data/dataTypes.ts';
21
import { schemaCallWrapper } from '../../data/schemaCallWrapper.ts';
32
import { type ResolvedSnippet, snip } from '../../data/snippet.ts';
43
import {
@@ -80,10 +79,10 @@ export interface TgpuFixedBufferUsage<TData extends BaseData>
8079
export interface TgpuBufferMutable<TData extends BaseData>
8180
extends TgpuBufferUsage<TData, 'mutable'> {}
8281

83-
export function isUsableAsUniform<T extends TgpuBuffer<AnyData>>(
82+
export function isUsableAsUniform<T extends TgpuBuffer<BaseData>>(
8483
buffer: T,
8584
): buffer is T & UniformFlag {
86-
return !!(buffer as unknown as UniformFlag).usableAsUniform;
85+
return !!(buffer as T).usableAsUniform;
8786
}
8887

8988
// --------------
@@ -97,7 +96,7 @@ const usageToVarTemplateMap: Record<BindableBufferUsage, string> = {
9796
};
9897

9998
class TgpuFixedBufferImpl<
100-
TData extends AnyWgslData,
99+
TData extends BaseData,
101100
TUsage extends BindableBufferUsage,
102101
> implements
103102
TgpuBufferUsage<TData, TUsage>,
@@ -237,7 +236,7 @@ class TgpuFixedBufferImpl<
237236
}
238237

239238
export class TgpuLaidOutBufferImpl<
240-
TData extends AnyWgslData,
239+
TData extends BaseData,
241240
TUsage extends BindableBufferUsage,
242241
> implements TgpuBufferUsage<TData, TUsage>, SelfResolvable {
243242
/** Type-token, not available at runtime */
@@ -312,8 +311,8 @@ export class TgpuLaidOutBufferImpl<
312311
}
313312

314313
const mutableUsageMap = new WeakMap<
315-
TgpuBuffer<AnyWgslData>,
316-
TgpuFixedBufferImpl<AnyWgslData, 'mutable'>
314+
TgpuBuffer<BaseData>,
315+
TgpuFixedBufferImpl<BaseData, 'mutable'>
317316
>();
318317

319318
export function mutable<TData extends AnyWgslData>(
@@ -336,8 +335,8 @@ export function mutable<TData extends AnyWgslData>(
336335
}
337336

338337
const readonlyUsageMap = new WeakMap<
339-
TgpuBuffer<AnyWgslData>,
340-
TgpuFixedBufferImpl<AnyWgslData, 'readonly'>
338+
TgpuBuffer<BaseData>,
339+
TgpuFixedBufferImpl<BaseData, 'readonly'>
341340
>();
342341

343342
export function readonly<TData extends AnyWgslData>(
@@ -360,8 +359,8 @@ export function readonly<TData extends AnyWgslData>(
360359
}
361360

362361
const uniformUsageMap = new WeakMap<
363-
TgpuBuffer<AnyWgslData>,
364-
TgpuFixedBufferImpl<AnyWgslData, 'uniform'>
362+
TgpuBuffer<BaseData>,
363+
TgpuFixedBufferImpl<BaseData, 'uniform'>
365364
>();
366365

367366
export function uniform<TData extends AnyWgslData>(

packages/typegpu/src/core/constant/tgpuConstant.ts

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
import type { AnyData } from '../../data/dataTypes.ts';
22
import { type ResolvedSnippet, snip } from '../../data/snippet.ts';
3-
import { isNaturallyEphemeral } from '../../data/wgslTypes.ts';
3+
import { type BaseData, isNaturallyEphemeral } from '../../data/wgslTypes.ts';
44
import { inCodegenMode } from '../../execMode.ts';
55
import type { TgpuNamable } from '../../shared/meta.ts';
66
import { getName, setName } from '../../shared/meta.ts';
@@ -24,7 +24,7 @@ type DeepReadonly<T> = T extends { [$internal]: unknown } ? T
2424
? { readonly [K in keyof T]: DeepReadonly<T[K]> }
2525
: T;
2626

27-
export interface TgpuConst<TDataType extends AnyData = AnyData>
27+
export interface TgpuConst<TDataType extends BaseData = BaseData>
2828
extends TgpuNamable {
2929
readonly resourceType: 'const';
3030
readonly [$gpuValueOf]: DeepReadonly<InferGPU<TDataType>>;
@@ -71,7 +71,7 @@ function deepFreeze<T extends object>(object: T): T {
7171
return Object.freeze(object);
7272
}
7373

74-
class TgpuConstImpl<TDataType extends AnyData>
74+
class TgpuConstImpl<TDataType extends BaseData>
7575
implements TgpuConst<TDataType>, SelfResolvable {
7676
readonly [$internal] = {};
7777
readonly resourceType: 'const';

packages/typegpu/src/core/function/dualImpl.ts

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,3 @@
1-
import type { AnyData } from '../../data/dataTypes.ts';
21
import { type MapValueToSnippet, snip } from '../../data/snippet.ts';
32
import { setName } from '../../shared/meta.ts';
43
import { $gpuCallable } from '../../shared/symbols.ts';
@@ -9,8 +8,9 @@ import {
98
NormalState,
109
type ResolutionCtx,
1110
} from '../../types.ts';
11+
import { type BaseData, isPtr } from '../../data/wgslTypes.ts';
1212

13-
type MapValueToDataType<T> = { [K in keyof T]: AnyData };
13+
type MapValueToDataType<T> = { [K in keyof T]: BaseData };
1414
type AnyFn = (...args: never[]) => unknown;
1515

1616
interface DualImplOptions<T extends AnyFn> {
@@ -21,10 +21,10 @@ interface DualImplOptions<T extends AnyFn> {
2121
args: MapValueToSnippet<Parameters<T>>,
2222
) => string;
2323
readonly signature:
24-
| { argTypes: AnyData[]; returnType: AnyData }
24+
| { argTypes: BaseData[]; returnType: BaseData }
2525
| ((
2626
...inArgTypes: MapValueToDataType<Parameters<T>>
27-
) => { argTypes: AnyData[]; returnType: AnyData });
27+
) => { argTypes: BaseData[]; returnType: BaseData });
2828
/**
2929
* Whether the function should skip trying to execute the "normal" implementation if
3030
* all arguments are known at compile time.
@@ -64,7 +64,7 @@ export function dualImpl<T extends AnyFn>(
6464
? options.signature(
6565
...args.map((s) => {
6666
// Dereference implicit pointers
67-
if (s.dataType.type === 'ptr' && s.dataType.implicit) {
67+
if (isPtr(s.dataType) && s.dataType.implicit) {
6868
return s.dataType.inner;
6969
}
7070
return s.dataType;

packages/typegpu/src/core/function/fnCore.ts

Lines changed: 11 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,12 @@
11
import { getAttributesString } from '../../data/attributes.ts';
2-
import { type AnyData, undecorate } from '../../data/dataTypes.ts';
2+
import { undecorate } from '../../data/dataTypes.ts';
33
import { type ResolvedSnippet, snip } from '../../data/snippet.ts';
4-
import { isWgslData, isWgslStruct, Void } from '../../data/wgslTypes.ts';
4+
import {
5+
type BaseData,
6+
isWgslData,
7+
isWgslStruct,
8+
Void,
9+
} from '../../data/wgslTypes.ts';
510
import { MissingLinksError } from '../../errors.ts';
611
import { getMetaData, getName, setName } from '../../shared/meta.ts';
712
import type { ResolutionCtx } from '../../types.ts';
@@ -17,12 +22,12 @@ export interface FnCore {
1722
applyExternals(newExternals: ExternalMap): void;
1823
resolve(
1924
ctx: ResolutionCtx,
20-
argTypes: AnyData[],
25+
argTypes: BaseData[],
2126
/**
2227
* The return type of the function. If undefined, the type should be inferred
2328
* from the implementation (relevant for shellless functions).
2429
*/
25-
returnType: AnyData | undefined,
30+
returnType: BaseData | undefined,
2631
): ResolvedSnippet;
2732
}
2833

@@ -45,8 +50,8 @@ export function createFnCore(
4550

4651
resolve(
4752
ctx: ResolutionCtx,
48-
argTypes: AnyData[],
49-
returnType: AnyData | undefined,
53+
argTypes: BaseData[],
54+
returnType: BaseData | undefined,
5055
): ResolvedSnippet {
5156
const externalMap: ExternalMap = {};
5257

packages/typegpu/src/core/function/shelllessImpl.ts

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
1-
import type { AnyData } from '../../data/dataTypes.ts';
21
import type { ResolvedSnippet } from '../../data/snippet.ts';
2+
import type { BaseData } from '../../data/wgslTypes.ts';
33
import { getName } from '../../shared/meta.ts';
44
import { $getNameForward, $internal, $resolve } from '../../shared/symbols.ts';
55
import type { ResolutionCtx, SelfResolvable } from '../../types.ts';
@@ -23,12 +23,12 @@ import { createFnCore } from './fnCore.ts';
2323
*/
2424
export interface ShelllessImpl extends SelfResolvable {
2525
readonly resourceType: 'shellless-impl';
26-
readonly argTypes: AnyData[];
26+
readonly argTypes: BaseData[];
2727
readonly [$getNameForward]: unknown;
2828
}
2929

3030
export function createShelllessImpl(
31-
argTypes: AnyData[],
31+
argTypes: BaseData[],
3232
implementation: (...args: never[]) => unknown,
3333
): ShelllessImpl {
3434
const core = createFnCore(implementation, '');

packages/typegpu/src/core/function/tgpuFn.ts

Lines changed: 15 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
1-
import type { AnyData } from '../../data/dataTypes.ts';
21
import type { ResolvedSnippet } from '../../data/snippet.ts';
32
import { schemaCallWrapper } from '../../data/schemaCallWrapper.ts';
4-
import { Void } from '../../data/wgslTypes.ts';
3+
import { type BaseData, Void } from '../../data/wgslTypes.ts';
54
import { ExecutionError } from '../../errors.ts';
65
import { provideInsideTgpuFn } from '../../execMode.ts';
76
import type { TgpuNamable } from '../../shared/meta.ts';
@@ -51,8 +50,8 @@ import type { Withable } from '../root/rootTypes.ts';
5150
* Describes a function signature (its arguments and return type)
5251
*/
5352
type TgpuFnShellHeader<
54-
Args extends AnyData[],
55-
Return extends AnyData,
53+
Args extends BaseData[],
54+
Return extends BaseData,
5655
> = {
5756
readonly [$internal]: true;
5857
readonly argTypes: Args;
@@ -66,8 +65,8 @@ type TgpuFnShellHeader<
6665
* and passing the implementation (as WGSL string or JS function) as the argument.
6766
*/
6867
export type TgpuFnShell<
69-
Args extends AnyData[],
70-
Return extends AnyData,
68+
Args extends BaseData[],
69+
Return extends BaseData,
7170
> =
7271
& TgpuFnShellHeader<Args, Return>
7372
& (<T extends (...args: InferArgs<Args>) => Infer<Return>>(
@@ -89,7 +88,7 @@ interface TgpuFnBase<ImplSchema extends AnyFn>
8988
readonly resourceType: 'function';
9089
readonly shell: TgpuFnShellHeader<
9190
Parameters<ImplSchema>,
92-
Extract<ReturnType<ImplSchema>, AnyData>
91+
Extract<ReturnType<ImplSchema>, BaseData>
9392
>;
9493
readonly [$providing]?: Providing | undefined;
9594

@@ -102,22 +101,22 @@ export type TgpuFn<ImplSchema extends AnyFn = (...args: any[]) => any> =
102101
& TgpuFnBase<ImplSchema>;
103102

104103
export function fn<
105-
Args extends AnyData[] | [],
104+
Args extends BaseData[] | [],
106105
>(argTypes: Args, returnType?: undefined): TgpuFnShell<Args, Void>;
107106

108107
export function fn<
109-
Args extends AnyData[] | [],
110-
Return extends AnyData,
108+
Args extends BaseData[] | [],
109+
Return extends BaseData,
111110
>(argTypes: Args, returnType: Return): TgpuFnShell<Args, Return>;
112111

113112
export function fn<
114-
Args extends AnyData[] | [],
115-
Return extends AnyData = Void,
113+
Args extends BaseData[] | [],
114+
Return extends BaseData = Void,
116115
>(argTypes: Args, returnType?: Return | undefined): TgpuFnShell<Args, Return> {
117116
const shell: TgpuFnShellHeader<Args, Return> = {
118117
[$internal]: true,
119118
argTypes,
120-
returnType: returnType ?? Void as Return,
119+
returnType: returnType ?? Void as unknown as Return,
121120
isEntry: false,
122121
};
123122

@@ -133,7 +132,7 @@ export function fn<
133132
return Object.assign(call, shell) as unknown as TgpuFnShell<Args, Return>;
134133
}
135134

136-
export function isTgpuFn<Args extends AnyData[] | [], Return extends AnyData>(
135+
export function isTgpuFn<Args extends BaseData[] | [], Return extends BaseData>(
137136
value: unknown | TgpuFn<(...args: Args) => Return>,
138137
): value is TgpuFn<(...args: Args) => Return> {
139138
return isMarkedInternal(value) &&
@@ -151,7 +150,7 @@ function stringifyPair([slot, value]: SlotValuePair): string {
151150
function createFn<ImplSchema extends AnyFn>(
152151
shell: TgpuFnShellHeader<
153152
Parameters<ImplSchema>,
154-
Extract<ReturnType<ImplSchema>, AnyData>
153+
Extract<ReturnType<ImplSchema>, BaseData>
155154
>,
156155
implementation: Implementation<ImplSchema>,
157156
): TgpuFn<ImplSchema> {
@@ -217,7 +216,7 @@ function createFn<ImplSchema extends AnyFn>(
217216
}
218217

219218
const castAndCopiedArgs = args.map((arg, index) =>
220-
schemaCallWrapper(shell.argTypes[index] as unknown as AnyData, arg)
219+
schemaCallWrapper(shell.argTypes[index] as unknown as BaseData, arg)
221220
) as InferArgs<Parameters<ImplSchema>>;
222221

223222
const result = implementation(...castAndCopiedArgs);

0 commit comments

Comments
 (0)