Skip to content

Commit d6b058d

Browse files
committed
Review fixes
1 parent 7975fd0 commit d6b058d

2 files changed

Lines changed: 21 additions & 2 deletions

File tree

packages/typegpu/src/tgsl/accessIndex.ts

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -25,10 +25,16 @@ export function accessIndex(target: Snippet, indexArg: Snippet | number): Snippe
2525

2626
if (target.origin === 'constant-immutable-def') {
2727
// Constant refs stay const unless the element/index forces runtime materialization
28-
origin = index.origin === 'constant' ? 'constant-immutable-def' : 'runtime-immutable-def';
28+
origin =
29+
index.origin === 'constant' || index.origin === 'constant-immutable-def'
30+
? 'constant-immutable-def'
31+
: 'runtime-immutable-def';
2932
} else if (target.origin === 'constant') {
3033
// Ephemeral constants indexed with constants stay constant, otherwise they become runtime-known
31-
origin = index.origin === 'constant' ? 'constant' : 'runtime';
34+
origin =
35+
index.origin === 'constant' || index.origin === 'constant-immutable-def'
36+
? 'constant'
37+
: 'runtime';
3238
} else {
3339
// Fallthrough
3440
origin = target.origin;

packages/typegpu/tests/constant.test.ts

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
import { describe, expect, it } from 'vitest';
22
import { d, tgpu } from '../src/index.js';
3+
import { extractSnippetFromFn } from './utils/parseResolved.ts';
34

45
const Boid = d.struct({
56
pos: d.vec3f,
@@ -190,4 +191,16 @@ describe('tgpu.const', () => {
190191
- fn*:testFn(): 'c.$.x = 1' is invalid, because the left side is a constant.]
191192
`);
192193
});
194+
195+
it('allows for tgpu.const-tgpu.const index access', () => {
196+
const myConstArray = tgpu.const(d.arrayOf(d.u32, 2), [1, 2]);
197+
const myConstIndex = tgpu.const(d.u32, 1);
198+
199+
const fn = () => {
200+
'use gpu';
201+
return myConstArray.$[myConstIndex.$];
202+
};
203+
204+
expect(extractSnippetFromFn(fn).origin).toBe('constant-immutable-def');
205+
});
193206
});

0 commit comments

Comments
 (0)