Skip to content

Commit 0173ffa

Browse files
authored
fix: Allow arguments in rhs of compound assignment (#2219)
1 parent 980846e commit 0173ffa

2 files changed

Lines changed: 39 additions & 0 deletions

File tree

packages/typegpu/src/tgsl/wgslGenerator.ts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -410,7 +410,9 @@ ${this.ctx.pre}}`;
410410
);
411411
}
412412

413+
// Compound assignment operators are okay, e.g. +=, -=, *=, /=, ...
413414
if (
415+
op === '=' &&
414416
rhsExpr.origin === 'argument' &&
415417
!wgsl.isNaturallyEphemeral(rhsExpr.dataType)
416418
) {

packages/typegpu/tests/tgsl/operatorOverloads.test.ts

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -111,6 +111,43 @@ test('vec3f() %', () => {
111111
);
112112
});
113113

114+
test('+= refOfVec3f', () => {
115+
const constant = tgpu.const(d.vec3f, d.vec3f(-10));
116+
const foo = (arg: d.v3f) => {
117+
'use gpu';
118+
const local = d.vec3f(100, 10, 1);
119+
120+
let result = d.vec3f();
121+
result += local;
122+
result += arg;
123+
result += constant.$;
124+
return result;
125+
};
126+
127+
const main = () => {
128+
'use gpu';
129+
return foo(d.vec3f(1, 2, 3));
130+
};
131+
132+
expect(tgpu.resolve([main])).toMatchInlineSnapshot(`
133+
"const constant: vec3f = vec3f(-10);
134+
135+
fn foo(arg: vec3f) -> vec3f {
136+
var local = vec3f(100, 10, 1);
137+
var result = vec3f();
138+
result += local;
139+
result += arg;
140+
result += constant;
141+
return result;
142+
}
143+
144+
fn main() -> vec3f {
145+
return foo(vec3f(1, 2, 3));
146+
}"
147+
`);
148+
expect(main().toString()).toMatchInlineSnapshot(`"vec3f(91, 2, -6)"`);
149+
});
150+
114151
describe('num op', () => {
115152
test('num +', () => {
116153
const main = () => {

0 commit comments

Comments
 (0)