Skip to content

Commit a0d7d65

Browse files
committed
runsOn impl
1 parent f9bd90c commit a0d7d65

3 files changed

Lines changed: 102 additions & 0 deletions

File tree

packages/typegpu/src/std/index.ts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -189,3 +189,5 @@ export { extensionEnabled } from './extensions.ts';
189189
export { bitcastU32toF32, bitcastU32toI32 } from './bitcast.ts';
190190

191191
export { range } from './range.ts';
192+
193+
export { runsOn, type Runtime } from './runsOn.ts';

packages/typegpu/src/std/runsOn.ts

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
import { bool } from '../data/numeric.ts';
2+
import { snip } from '../data/snippet.ts';
3+
import { $gpuCallable } from '../shared/symbols.ts';
4+
import type { DualFn } from '../types.ts';
5+
6+
export type Runtime = 'cpu' | 'gpu';
7+
8+
const impl = ((runtime: Runtime): boolean => runtime === 'cpu') as DualFn<
9+
(runtime: Runtime) => boolean
10+
>;
11+
impl.toString = () => 'runsOn';
12+
impl[$gpuCallable] = {
13+
call(_ctx, args) {
14+
return snip((args[0].value as Runtime) === 'gpu', bool, 'constant');
15+
},
16+
};
17+
18+
export const runsOn = impl;
Lines changed: 82 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,82 @@
1+
import { it } from 'typegpu-testing-utility';
2+
import { expect, describe } from 'vitest';
3+
4+
import tgpu, { d } from '../../src/index.js';
5+
import { runsOn, type Runtime } from '../../src/std/index.ts';
6+
7+
describe('runsOn', () => {
8+
it('correctly determines cpu runtime top level', () => {
9+
expect(runsOn('cpu')).toBe(true);
10+
expect(runsOn('gpu')).toBe(false);
11+
});
12+
13+
it('correctly determines gpu runtime at function resolution time', () => {
14+
const f = () => {
15+
'use gpu';
16+
if (runsOn('gpu')) {
17+
return 7;
18+
} else {
19+
return -7;
20+
}
21+
};
22+
23+
expect(tgpu.resolve([f])).toMatchInlineSnapshot(`
24+
"fn f() -> i32 {
25+
{
26+
return 7;
27+
}
28+
}"
29+
`);
30+
});
31+
32+
it('correctly determines cpu runtime in comptime', () => {
33+
const checkRuntime = tgpu.comptime((runtime: Runtime) => runsOn(runtime));
34+
35+
expect(checkRuntime('cpu')).toBe(true);
36+
expect(checkRuntime('gpu')).toBe(false);
37+
38+
const f = () => {
39+
'use gpu';
40+
const _cpu = checkRuntime('cpu');
41+
const _gpu = checkRuntime('gpu');
42+
};
43+
44+
expect(tgpu.resolve([f])).toMatchInlineSnapshot(`
45+
"fn f() {
46+
const _cpu = true;
47+
const _gpu = false;
48+
}"
49+
`);
50+
});
51+
52+
it('correctly determines cpu runtime in lazy', () => {
53+
const checkRuntimeCPU = tgpu.lazy(() => runsOn('cpu'));
54+
const checkRuntimeGPU = tgpu.lazy(() => runsOn('gpu'));
55+
56+
const f = () => {
57+
'use gpu';
58+
const _cpu = checkRuntimeCPU.$;
59+
const _gpu = checkRuntimeGPU.$;
60+
};
61+
62+
expect(tgpu.resolve([f])).toMatchInlineSnapshot(`
63+
"fn f() {
64+
const _cpu = true;
65+
const _gpu = false;
66+
}"
67+
`);
68+
});
69+
70+
it('correctly branches on cpu/gpu runtime during execution', () => {
71+
const f = () => {
72+
'use gpu';
73+
if (runsOn('gpu')) {
74+
return 7;
75+
} else {
76+
return -7;
77+
}
78+
};
79+
80+
expect(f()).toBe(-7);
81+
});
82+
});

0 commit comments

Comments
 (0)