Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 3 additions & 3 deletions packages/typegpu/src/core/slot/accessor.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down Expand Up @@ -174,7 +174,7 @@ export class TgpuAccessorImpl<T extends BaseData>
}

get $(): InferGPU<T> {
if (inCodegenMode()) {
if (getResolutionCtx()) {
return this[$gpuValueOf];
}

Expand All @@ -198,7 +198,7 @@ export class TgpuMutableAccessorImpl<T extends BaseData>
}

get $(): InferGPU<T> {
if (inCodegenMode()) {
if (getResolutionCtx()) {
return this[$gpuValueOf];
}

Expand Down
65 changes: 65 additions & 0 deletions packages/typegpu/tests/tgsl/comptime.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -75,4 +75,69 @@ 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('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.$);
const myFn = () => {
'use gpu';
return readValue();
};

expect(() => tgpu.resolve([myFn])).toThrowErrorMatchingInlineSnapshot(`
[Error: Resolution of the following tree failed:
- <root>
- fn*:myFn
- fn*:myFn()
- fn:readValue: Missing value for 'slot:value']
`);
});
});
Loading