Skip to content

Commit 1c8de59

Browse files
authored
fix: Lift limitation on passing tgpu.const references as arguments (#2286)
1 parent ace8591 commit 1c8de59

2 files changed

Lines changed: 11 additions & 62 deletions

File tree

packages/typegpu/src/tgsl/shellless.ts

Lines changed: 0 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,6 @@ import { RefOperator } from '../data/ref.ts';
44
import type { Snippet } from '../data/snippet.ts';
55
import { type BaseData, isPtr, isWgslArray, isWgslStruct } from '../data/wgslTypes.ts';
66
import { WgslTypeError } from '../errors.ts';
7-
import { getResolutionCtx } from '../execMode.ts';
87
import { getMetaData, getName } from '../shared/meta.ts';
98
import { concretize } from './generationHelpers.ts';
109

@@ -64,16 +63,6 @@ export class ShelllessRepository {
6463

6564
let type = concretize(s.dataType);
6665

67-
if (s.origin === 'constant-tgpu-const-ref' || s.origin === 'runtime-tgpu-const-ref') {
68-
// oxlint-disable-next-line typescript/no-non-null-assertion -- it's there
69-
const ctx = getResolutionCtx()!;
70-
throw new Error(
71-
`Cannot pass constant references as function arguments. Explicitly copy them by wrapping them in a schema: '${
72-
ctx.resolve(type).value
73-
}(...)'`,
74-
);
75-
}
76-
7766
if (isPtr(type) && type.implicit) {
7867
// If the pointer was made implicitly (e.g. by assigning a reference to a const variable),
7968
// then we dereference the pointer before passing it to the function. The main reason for this,

packages/typegpu/tests/constant.test.ts

Lines changed: 11 additions & 51 deletions
Original file line numberDiff line numberDiff line change
@@ -46,7 +46,7 @@ describe('tgpu.const', () => {
4646
`);
4747
});
4848

49-
it('cannot be passed directly to shellless functions', () => {
49+
it('can be passed directly to shellless functions (as arguments cannot be mutated anyway)', () => {
5050
const fn1 = (v: d.v3f) => {
5151
'use gpu';
5252
return v.x * v.y * v.z;
@@ -58,11 +58,16 @@ describe('tgpu.const', () => {
5858
return fn1(foo.$);
5959
};
6060

61-
expect(() => tgpu.resolve([fn2])).toThrowErrorMatchingInlineSnapshot(`
62-
[Error: Resolution of the following tree failed:
63-
- <root>
64-
- fn*:fn2
65-
- fn*:fn2(): Cannot pass constant references as function arguments. Explicitly copy them by wrapping them in a schema: 'vec3f(...)']
61+
expect(tgpu.resolve([fn2])).toMatchInlineSnapshot(`
62+
"fn fn1(v: vec3f) -> f32 {
63+
return ((v.x * v.y) * v.z);
64+
}
65+
66+
const foo: vec3f = vec3f(1, 2, 3);
67+
68+
fn fn2() -> f32 {
69+
return fn1(foo);
70+
}"
6671
`);
6772
});
6873

@@ -116,49 +121,4 @@ describe('tgpu.const', () => {
116121
}"
117122
`);
118123
});
119-
120-
it('cannot be passed directly to shellless functions', () => {
121-
const fn1 = (v: d.v3f) => {
122-
'use gpu';
123-
return v.x * v.y * v.z;
124-
};
125-
126-
const foo = tgpu.const(d.vec3f, d.vec3f(1, 2, 3));
127-
const fn2 = () => {
128-
'use gpu';
129-
return fn1(foo.$);
130-
};
131-
132-
expect(() => tgpu.resolve([fn2])).toThrowErrorMatchingInlineSnapshot(`
133-
[Error: Resolution of the following tree failed:
134-
- <root>
135-
- fn*:fn2
136-
- fn*:fn2(): Cannot pass constant references as function arguments. Explicitly copy them by wrapping them in a schema: 'vec3f(...)']
137-
`);
138-
});
139-
140-
it('cannot be mutated', () => {
141-
const boid = tgpu.const(Boid, {
142-
pos: d.vec3f(1, 2, 3),
143-
vel: d.vec3u(4, 5, 6),
144-
});
145-
146-
const fn = () => {
147-
'use gpu';
148-
// @ts-expect-error: Cannot assign to read-only property
149-
boid.$.pos = d.vec3f(0, 0, 0);
150-
};
151-
152-
expect(() => tgpu.resolve([fn])).toThrowErrorMatchingInlineSnapshot(`
153-
[Error: Resolution of the following tree failed:
154-
- <root>
155-
- fn*:fn
156-
- fn*:fn(): 'boid.pos = vec3f()' is invalid, because boid.pos is a constant. This error may also occur when assigning to a value defined outside of a TypeGPU function's scope.]
157-
`);
158-
159-
// Since we freeze the object, we cannot mutate when running the function in JS either
160-
expect(() => fn()).toThrowErrorMatchingInlineSnapshot(
161-
`[TypeError: Cannot assign to read only property 'pos' of object '#<Object>']`,
162-
);
163-
});
164124
});

0 commit comments

Comments
 (0)