From 9679612c506d7dec23fce824f20aa5e7fd6a8894 Mon Sep 17 00:00:00 2001 From: oxura Date: Wed, 2 Sep 2026 14:46:39 +0600 Subject: [PATCH 1/2] fix(gl): translate vector equality to GLSL equal --- packages/typegpu-gl/src/glslGenerator.ts | 4 ++++ packages/typegpu-gl/tests/glslGenerator.test.ts | 10 ++++++++++ packages/typegpu/src/std/boolean.ts | 2 +- 3 files changed, 15 insertions(+), 1 deletion(-) diff --git a/packages/typegpu-gl/src/glslGenerator.ts b/packages/typegpu-gl/src/glslGenerator.ts index deb15ac312..d2e2c00f41 100644 --- a/packages/typegpu-gl/src/glslGenerator.ts +++ b/packages/typegpu-gl/src/glslGenerator.ts @@ -823,6 +823,10 @@ export class GlslGenerator extends WgslGenerator { } override emitBinaryOp(lhs: Snippet, op: BinaryOperator, rhs: Snippet): string { + if (op === '==' && lhs.dataType !== UnknownData && lhs.dataType.type.startsWith('vec')) { + return super.emitCall('equal', [], [lhs, rhs]); + } + if (op === '%' && (isF32VecfSchema(lhs.dataType) || isF32VecfSchema(rhs.dataType))) { const result = this._callShellless(HELPERS.remainder, [lhs, rhs]); if (!result) { diff --git a/packages/typegpu-gl/tests/glslGenerator.test.ts b/packages/typegpu-gl/tests/glslGenerator.test.ts index 27956bddcb..2125e8d2f4 100644 --- a/packages/typegpu-gl/tests/glslGenerator.test.ts +++ b/packages/typegpu-gl/tests/glslGenerator.test.ts @@ -353,6 +353,16 @@ describe('GlslGenerator - operator', () => { }" `); }); + + it('translates component-wise vector equality to equal', () => { + const compare = tgpu.fn([d.vec3f, d.vec3f], d.vec3b)((lhs, rhs) => std.eq(lhs, rhs)); + + expect(tgpu.resolve([compare], glOptions())).toMatchInlineSnapshot(` + "bvec3 compare(vec3 lhs, vec3 rhs) { + return equal(lhs, rhs); + }" + `); + }); }); describe('GlslGenerator - function definitions', () => { diff --git a/packages/typegpu/src/std/boolean.ts b/packages/typegpu/src/std/boolean.ts index badbddbcc1..809b2e16d2 100644 --- a/packages/typegpu/src/std/boolean.ts +++ b/packages/typegpu/src/std/boolean.ts @@ -87,7 +87,7 @@ export const eq = dualImpl({ returnType: correspondingBooleanVectorSchema(argTypes[0]), }), normalImpl: cpuEq, - codegenImpl: (_ctx, [lhs, rhs]) => stitch`(${lhs} == ${rhs})`, + codegenImpl: (ctx, [lhs, rhs]) => ctx.gen.emitBinaryOp(lhs, '==', rhs), sideEffects: false, }); From 34edcac7b41078088db8a1da76e06d7029fb708e Mon Sep 17 00:00:00 2001 From: oxura Date: Wed, 2 Sep 2026 16:33:33 +0600 Subject: [PATCH 2/2] fix(gl): translate vector inequality --- packages/typegpu-gl/src/glslGenerator.ts | 11 ++++++++--- packages/typegpu-gl/tests/glslGenerator.test.ts | 10 ++++++++++ packages/typegpu/src/std/boolean.ts | 2 +- 3 files changed, 19 insertions(+), 4 deletions(-) diff --git a/packages/typegpu-gl/src/glslGenerator.ts b/packages/typegpu-gl/src/glslGenerator.ts index d2e2c00f41..0adb286de5 100644 --- a/packages/typegpu-gl/src/glslGenerator.ts +++ b/packages/typegpu-gl/src/glslGenerator.ts @@ -823,10 +823,15 @@ export class GlslGenerator extends WgslGenerator { } override emitBinaryOp(lhs: Snippet, op: BinaryOperator, rhs: Snippet): string { - if (op === '==' && lhs.dataType !== UnknownData && lhs.dataType.type.startsWith('vec')) { - return super.emitCall('equal', [], [lhs, rhs]); + if ( + (op === '==' || op === '!=') && + lhs.dataType !== UnknownData && + rhs.dataType !== UnknownData && + lhs.dataType.type.startsWith('vec') && + rhs.dataType.type.startsWith('vec') + ) { + return super.emitCall(op === '==' ? 'equal' : 'notEqual', [], [lhs, rhs]); } - if (op === '%' && (isF32VecfSchema(lhs.dataType) || isF32VecfSchema(rhs.dataType))) { const result = this._callShellless(HELPERS.remainder, [lhs, rhs]); if (!result) { diff --git a/packages/typegpu-gl/tests/glslGenerator.test.ts b/packages/typegpu-gl/tests/glslGenerator.test.ts index 2125e8d2f4..990820c545 100644 --- a/packages/typegpu-gl/tests/glslGenerator.test.ts +++ b/packages/typegpu-gl/tests/glslGenerator.test.ts @@ -363,6 +363,16 @@ describe('GlslGenerator - operator', () => { }" `); }); + + it('translates component-wise vector inequality to notEqual', () => { + const compare = tgpu.fn([d.vec3f, d.vec3f], d.vec3b)((lhs, rhs) => std.ne(lhs, rhs)); + + expect(tgpu.resolve([compare], glOptions())).toMatchInlineSnapshot(` + "bvec3 compare(vec3 lhs, vec3 rhs) { + return notEqual(lhs, rhs); + }" + `); + }); }); describe('GlslGenerator - function definitions', () => { diff --git a/packages/typegpu/src/std/boolean.ts b/packages/typegpu/src/std/boolean.ts index 809b2e16d2..fc34053dae 100644 --- a/packages/typegpu/src/std/boolean.ts +++ b/packages/typegpu/src/std/boolean.ts @@ -106,7 +106,7 @@ export const ne = dualImpl({ returnType: correspondingBooleanVectorSchema(argTypes[0]), }), normalImpl: (lhs: T, rhs: T) => cpuNot(cpuEq(lhs, rhs)), - codegenImpl: (_ctx, [lhs, rhs]) => stitch`(${lhs} != ${rhs})`, + codegenImpl: (ctx, [lhs, rhs]) => ctx.gen.emitBinaryOp(lhs, '!=', rhs), sideEffects: false, });