diff --git a/packages/typegpu-gl/src/glslGenerator.ts b/packages/typegpu-gl/src/glslGenerator.ts index deb15ac312..0adb286de5 100644 --- a/packages/typegpu-gl/src/glslGenerator.ts +++ b/packages/typegpu-gl/src/glslGenerator.ts @@ -823,6 +823,15 @@ export class GlslGenerator extends WgslGenerator { } override emitBinaryOp(lhs: Snippet, op: BinaryOperator, rhs: Snippet): string { + 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 27956bddcb..990820c545 100644 --- a/packages/typegpu-gl/tests/glslGenerator.test.ts +++ b/packages/typegpu-gl/tests/glslGenerator.test.ts @@ -353,6 +353,26 @@ 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); + }" + `); + }); + + 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 badbddbcc1..fc34053dae 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, }); @@ -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, });