From ac2d19290770bea5a249783effa5cf459a4fabad Mon Sep 17 00:00:00 2001 From: Konrad Reczko Date: Tue, 26 May 2026 16:53:55 +0200 Subject: [PATCH 1/4] fix --- packages/typegpu/src/core/slot/accessor.ts | 6 +-- packages/typegpu/tests/tgsl/comptime.test.ts | 42 ++++++++++++++++++++ 2 files changed, 45 insertions(+), 3 deletions(-) diff --git a/packages/typegpu/src/core/slot/accessor.ts b/packages/typegpu/src/core/slot/accessor.ts index 321f866b29..846dab496f 100644 --- a/packages/typegpu/src/core/slot/accessor.ts +++ b/packages/typegpu/src/core/slot/accessor.ts @@ -2,7 +2,7 @@ import { type AnyData, isData } from '../../data/dataTypes.ts'; import { schemaCallWrapper } from '../../data/schemaCallWrapper.ts'; import { isSnippet, type ResolvedSnippet, snip } from '../../data/snippet.ts'; import type { BaseData } from '../../data/wgslTypes.ts'; -import { getResolutionCtx, inCodegenMode } from '../../execMode.ts'; +import { getResolutionCtx } from '../../execMode.ts'; import { getName, hasTinyestMetadata, setName } from '../../shared/meta.ts'; import type { InferGPU } from '../../shared/repr.ts'; import { @@ -174,7 +174,7 @@ export class TgpuAccessorImpl } get $(): InferGPU { - if (inCodegenMode()) { + if (getResolutionCtx()) { return this[$gpuValueOf]; } @@ -198,7 +198,7 @@ export class TgpuMutableAccessorImpl } get $(): InferGPU { - if (inCodegenMode()) { + if (getResolutionCtx()) { return this[$gpuValueOf]; } diff --git a/packages/typegpu/tests/tgsl/comptime.test.ts b/packages/typegpu/tests/tgsl/comptime.test.ts index f4fc57f42c..dbe857c305 100644 --- a/packages/typegpu/tests/tgsl/comptime.test.ts +++ b/packages/typegpu/tests/tgsl/comptime.test.ts @@ -75,4 +75,46 @@ describe('comptime', () => { }" `); }); + + it('can read accessors during shader resolution', () => { + const value = tgpu.accessor(d.f32, 1); + const readValue = tgpu.comptime(() => value.$); + + const myFn = tgpu.fn( + [], + d.f32, + )(() => { + return readValue(); + }); + + expect(tgpu.resolve([myFn])).toMatchInlineSnapshot(` + "fn myFn() -> f32 { + return 1f; + }" + `); + + expect(tgpu.resolve([myFn.with(value, 2)])).toMatchInlineSnapshot(` + "fn myFn() -> f32 { + return 2f; + }" + `); + }); + + it('still throws when a comptime-read accessor has no value', () => { + const value = tgpu.accessor(d.f32); + const readValue = tgpu.comptime(() => value.$); + const myFn = tgpu.fn( + [], + d.f32, + )(() => { + return readValue(); + }); + + expect(() => tgpu.resolve([myFn])).toThrowErrorMatchingInlineSnapshot(` + [Error: Resolution of the following tree failed: + - + - fn:myFn + - fn:readValue: Missing value for 'slot:value'] + `); + }); }); From 829c75b1fe70b8091fd047c38d5fd242bbe37121 Mon Sep 17 00:00:00 2001 From: Konrad Reczko Date: Tue, 26 May 2026 16:56:27 +0200 Subject: [PATCH 2/4] nit --- packages/typegpu/tests/tgsl/comptime.test.ts | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/packages/typegpu/tests/tgsl/comptime.test.ts b/packages/typegpu/tests/tgsl/comptime.test.ts index dbe857c305..236719e821 100644 --- a/packages/typegpu/tests/tgsl/comptime.test.ts +++ b/packages/typegpu/tests/tgsl/comptime.test.ts @@ -100,7 +100,7 @@ describe('comptime', () => { `); }); - it('still throws when a comptime-read accessor has no value', () => { + it('throws when a comptime-read accessor has no value', () => { const value = tgpu.accessor(d.f32); const readValue = tgpu.comptime(() => value.$); const myFn = tgpu.fn( From 2f3a40df87fa64c5de1c29738820e9b2b0b80c88 Mon Sep 17 00:00:00 2001 From: Konrad Reczko Date: Tue, 26 May 2026 16:57:48 +0200 Subject: [PATCH 3/4] ban turtles --- packages/typegpu/tests/tgsl/comptime.test.ts | 11 +++++------ 1 file changed, 5 insertions(+), 6 deletions(-) diff --git a/packages/typegpu/tests/tgsl/comptime.test.ts b/packages/typegpu/tests/tgsl/comptime.test.ts index 236719e821..c179d45433 100644 --- a/packages/typegpu/tests/tgsl/comptime.test.ts +++ b/packages/typegpu/tests/tgsl/comptime.test.ts @@ -103,17 +103,16 @@ describe('comptime', () => { it('throws when a comptime-read accessor has no value', () => { const value = tgpu.accessor(d.f32); const readValue = tgpu.comptime(() => value.$); - const myFn = tgpu.fn( - [], - d.f32, - )(() => { + const myFn = () => { + 'use gpu'; return readValue(); - }); + }; expect(() => tgpu.resolve([myFn])).toThrowErrorMatchingInlineSnapshot(` [Error: Resolution of the following tree failed: - - - fn:myFn + - fn*:myFn + - fn*:myFn() - fn:readValue: Missing value for 'slot:value'] `); }); From 63373b251c26333dbce6f06db763a074c409c8cc Mon Sep 17 00:00:00 2001 From: Iwo Plaza Date: Tue, 26 May 2026 17:56:43 +0200 Subject: [PATCH 4/4] Failing test --- packages/typegpu/tests/tgsl/comptime.test.ts | 24 ++++++++++++++++++++ 1 file changed, 24 insertions(+) diff --git a/packages/typegpu/tests/tgsl/comptime.test.ts b/packages/typegpu/tests/tgsl/comptime.test.ts index c179d45433..9bc061c97b 100644 --- a/packages/typegpu/tests/tgsl/comptime.test.ts +++ b/packages/typegpu/tests/tgsl/comptime.test.ts @@ -100,6 +100,30 @@ describe('comptime', () => { `); }); + it('can read and work with accessors in comptime', () => { + const valueAccess = tgpu.accessor(d.f32, 1); + const doubleValue = tgpu.comptime(() => valueAccess.$ * 2); + + const myFn = tgpu.fn( + [], + d.f32, + )(() => { + return doubleValue(); + }); + + expect(tgpu.resolve([myFn])).toMatchInlineSnapshot(` + "fn myFn() -> f32 { + return NaNf; + }" + `); + + expect(tgpu.resolve([myFn.with(valueAccess, 2)])).toMatchInlineSnapshot(` + "fn myFn() -> f32 { + return NaNf; + }" + `); + }); + it('throws when a comptime-read accessor has no value', () => { const value = tgpu.accessor(d.f32); const readValue = tgpu.comptime(() => value.$);