Skip to content

Commit 048a136

Browse files
authored
chore: Remove 'createDualImpl' (#1961)
1 parent b4eee32 commit 048a136

27 files changed

Lines changed: 793 additions & 1138 deletions

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

Lines changed: 13 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -1,42 +1,12 @@
11
import type { DualFn } from '../../data/dualFn.ts';
2-
import {
3-
type MapValueToSnippet,
4-
snip,
5-
type Snippet,
6-
} from '../../data/snippet.ts';
7-
import { inCodegenMode } from '../../execMode.ts';
8-
import { type FnArgsConversionHint, isKnownAtComptime } from '../../types.ts';
2+
import { type MapValueToSnippet, snip } from '../../data/snippet.ts';
3+
import { getResolutionCtx, inCodegenMode } from '../../execMode.ts';
4+
import { isKnownAtComptime, NormalState } from '../../types.ts';
95
import { setName } from '../../shared/meta.ts';
106
import { $internal } from '../../shared/symbols.ts';
117
import { tryConvertSnippet } from '../../tgsl/conversion.ts';
128
import type { AnyData } from '../../data/dataTypes.ts';
139

14-
export function createDualImpl<T extends (...args: never[]) => unknown>(
15-
jsImpl: T,
16-
gpuImpl: (...args: MapValueToSnippet<Parameters<T>>) => Snippet,
17-
name: string,
18-
argConversionHint: FnArgsConversionHint = 'keep',
19-
): DualFn<T> {
20-
const impl = ((...args: Parameters<T>) => {
21-
if (inCodegenMode()) {
22-
return gpuImpl(...(args as MapValueToSnippet<Parameters<T>>)) as Snippet;
23-
}
24-
return jsImpl(...args);
25-
}) as T;
26-
27-
setName(impl, name);
28-
impl.toString = () => name;
29-
Object.defineProperty(impl, $internal, {
30-
value: {
31-
jsImpl,
32-
gpuImpl,
33-
argConversionHint,
34-
},
35-
});
36-
37-
return impl as DualFn<T>;
38-
}
39-
4010
type MapValueToDataType<T> = { [K in keyof T]: AnyData };
4111

4212
interface DualImplOptions<T extends (...args: never[]) => unknown> {
@@ -68,6 +38,8 @@ export function dualImpl<T extends (...args: never[]) => unknown>(
6838
options: DualImplOptions<T>,
6939
): DualFn<T> {
7040
const gpuImpl = (...args: MapValueToSnippet<Parameters<T>>) => {
41+
// biome-ignore lint/style/noNonNullAssertion: it's there
42+
const ctx = getResolutionCtx()!;
7143
const { argTypes, returnType } = typeof options.signature === 'function'
7244
? options.signature(
7345
...args.map((s) => {
@@ -94,6 +66,7 @@ export function dualImpl<T extends (...args: never[]) => unknown>(
9466
converted.every((s) => isKnownAtComptime(s)) &&
9567
typeof options.normalImpl === 'function'
9668
) {
69+
ctx.pushMode(new NormalState());
9770
try {
9871
return snip(
9972
options.normalImpl(...converted.map((s) => s.value) as never[]),
@@ -108,6 +81,8 @@ export function dualImpl<T extends (...args: never[]) => unknown>(
10881
if (!(e instanceof MissingCpuImplError)) {
10982
throw e;
11083
}
84+
} finally {
85+
ctx.popMode('normal');
11186
}
11287
}
11388

@@ -135,9 +110,11 @@ export function dualImpl<T extends (...args: never[]) => unknown>(
135110
value: {
136111
jsImpl: options.normalImpl,
137112
gpuImpl,
138-
strictSignature: typeof options.signature !== 'function'
139-
? options.signature
140-
: undefined,
113+
get strictSignature() {
114+
return typeof options.signature !== 'function'
115+
? options.signature
116+
: undefined;
117+
},
141118
argConversionHint: 'keep',
142119
},
143120
});

packages/typegpu/src/core/valueProxyUtils.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
import type { Snippet } from '../data/snippet.ts';
22
import { getGPUValue } from '../getGPUValue.ts';
33
import { $internal, $ownSnippet, $resolve } from '../shared/symbols.ts';
4-
import { accessProp } from '../tgsl/generationHelpers.ts';
4+
import { accessProp } from '../tgsl/accessProp.ts';
55
import {
66
getOwnSnippet,
77
type SelfResolvable,

packages/typegpu/src/data/array.ts

Lines changed: 5 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,7 @@
1-
import { createDualImpl } from '../core/function/dualImpl.ts';
1+
import { comptime } from '../core/function/comptime.ts';
22
import { $internal } from '../shared/symbols.ts';
3-
import { UnknownData } from './dataTypes.ts';
4-
import { sizeOf } from './sizeOf.ts';
5-
import { snip, type Snippet } from './snippet.ts';
63
import { schemaCallWrapper } from './schemaCallWrapper.ts';
4+
import { sizeOf } from './sizeOf.ts';
75
import type { AnyWgslData, WgslArray } from './wgslTypes.ts';
86

97
// ----------
@@ -37,39 +35,14 @@ interface WgslArrayConstructor {
3735
* @param elementType The type of elements in the array.
3836
* @param elementCount The number of elements in the array.
3937
*/
40-
export const arrayOf = createDualImpl(
41-
// JS implementation
38+
export const arrayOf = comptime(
4239
((elementType, elementCount) => {
4340
if (elementCount === undefined) {
44-
return (count: number) => cpu_arrayOf(elementType, count);
41+
return comptime((count: number) => cpu_arrayOf(elementType, count));
4542
}
4643
return cpu_arrayOf(elementType, elementCount);
4744
}) as WgslArrayConstructor,
48-
// CODEGEN implementation
49-
(elementType, elementCount) => {
50-
if (elementCount?.value === undefined) {
51-
const partial = (count: Snippet) =>
52-
arrayOf[$internal].gpuImpl(elementType, count);
53-
// Marking so the WGSL generator lets this function through
54-
partial[$internal] = true;
55-
56-
return snip(partial, UnknownData, /* origin */ 'constant');
57-
}
58-
59-
if (typeof elementCount.value !== 'number') {
60-
throw new Error(
61-
`Cannot create array schema with count unknown at compile-time: '${elementCount.value}'`,
62-
);
63-
}
64-
65-
return snip(
66-
cpu_arrayOf(elementType.value as AnyWgslData, elementCount.value),
67-
elementType.value as AnyWgslData,
68-
/* origin */ 'runtime',
69-
);
70-
},
71-
'arrayOf',
72-
);
45+
).$name('arrayOf');
7346

7447
// --------------
7548
// Implementation

packages/typegpu/src/data/disarray.ts

Lines changed: 3 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,7 @@
1-
import { createDualImpl } from '../core/function/dualImpl.ts';
1+
import { comptime } from '../core/function/comptime.ts';
22
import { $internal } from '../shared/symbols.ts';
3-
import { UnknownData } from './dataTypes.ts';
4-
import { snip, type Snippet } from './snippet.ts';
53
import type { AnyData, Disarray } from './dataTypes.ts';
64
import { schemaCallWrapper } from './schemaCallWrapper.ts';
7-
import type { AnyWgslData } from './wgslTypes.ts';
85

96
// ----------
107
// Public API
@@ -43,39 +40,14 @@ interface DisarrayConstructor {
4340
* @param elementType The type of elements in the array.
4441
* @param elementCount The number of elements in the array.
4542
*/
46-
export const disarrayOf = createDualImpl(
47-
// JS implementation
43+
export const disarrayOf = comptime(
4844
((elementType, elementCount) => {
4945
if (elementCount === undefined) {
5046
return (count: number) => cpu_disarrayOf(elementType, count);
5147
}
5248
return cpu_disarrayOf(elementType, elementCount);
5349
}) as DisarrayConstructor,
54-
// CODEGEN implementation
55-
(elementType, elementCount) => {
56-
if (elementCount === undefined || elementCount.value === undefined) {
57-
const partial = (count: Snippet) =>
58-
disarrayOf[$internal].gpuImpl(elementType, count);
59-
// Marking so the WGSL generator lets this function through
60-
partial[$internal] = true;
61-
62-
return snip(partial, UnknownData, /* origin */ 'constant');
63-
}
64-
65-
if (typeof elementCount.value !== 'number') {
66-
throw new Error(
67-
`Cannot create disarray schema with count unknown at compile-time: '${elementCount.value}'`,
68-
);
69-
}
70-
71-
return snip(
72-
cpu_disarrayOf(elementType.value as AnyWgslData, elementCount.value),
73-
elementType.value as AnyWgslData,
74-
/* origin */ 'runtime',
75-
);
76-
},
77-
'disarrayOf',
78-
);
50+
).$name('disarrayOf');
7951

8052
export function cpu_disarrayOf<TElement extends AnyData>(
8153
elementType: TElement,

packages/typegpu/src/data/index.ts

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2,10 +2,7 @@
22
* @module typegpu/data
33
*/
44

5-
import {
6-
type InfixOperator,
7-
infixOperators,
8-
} from '../tgsl/generationHelpers.ts';
5+
import { type InfixOperator, infixOperators } from '../tgsl/accessProp.ts';
96
import { $internal } from '../shared/symbols.ts';
107
import { MatBase } from './matrix.ts';
118
import { VecBase } from './vectorImpl.ts';

0 commit comments

Comments
 (0)