|
| 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