Skip to content

Commit fab7c02

Browse files
authored
fix: Allow TgpuGenericFn to be resolved (#2245)
1 parent 3ab173d commit fab7c02

2 files changed

Lines changed: 35 additions & 1 deletion

File tree

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

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -329,7 +329,11 @@ function createGenericFn<T extends AnyFn>(inner: T, pairs: SlotValuePair[]): Tgp
329329
if (!getName(inner)) {
330330
setName(inner, label);
331331
}
332-
return this as TgpuGenericFn<T>;
332+
return this as TgpuGenericFn<T> & SelfResolvable;
333+
},
334+
335+
[$resolve](ctx: ResolutionCtx): ResolvedSnippet {
336+
return ctx.resolve(inner);
333337
},
334338

335339
with(

packages/typegpu/tests/tgpuGenericFn.test.ts

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -372,4 +372,34 @@ describe('TgpuGenericFn - shellless callback wrapper', () => {
372372
}"
373373
`);
374374
});
375+
376+
it('can be resolved', () => {
377+
const foo = tgpu.fn(() => {
378+
'use gpu';
379+
return 3;
380+
});
381+
382+
const resolved = tgpu.resolve([foo]);
383+
expect(resolved).toMatchInlineSnapshot(`
384+
"fn foo() -> i32 {
385+
return 3;
386+
}"
387+
`);
388+
});
389+
390+
it('can be resolved (with provided slots)', () => {
391+
const mulSlot = tgpu.slot<number>();
392+
393+
const foo = tgpu.fn(() => {
394+
'use gpu';
395+
return 3 * mulSlot.$;
396+
});
397+
398+
const resolved = tgpu.resolve([foo.with(mulSlot, 2)]);
399+
expect(resolved).toMatchInlineSnapshot(`
400+
"fn foo() -> i32 {
401+
return 6;
402+
}"
403+
`);
404+
});
375405
});

0 commit comments

Comments
 (0)