Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 9 additions & 0 deletions packages/typegpu-gl/src/glslGenerator.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand Down
20 changes: 20 additions & 0 deletions packages/typegpu-gl/tests/glslGenerator.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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', () => {
Expand Down
4 changes: 2 additions & 2 deletions packages/typegpu/src/std/boolean.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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),
Comment thread
oxura marked this conversation as resolved.
sideEffects: false,
});

Expand All @@ -106,7 +106,7 @@ export const ne = dualImpl({
returnType: correspondingBooleanVectorSchema(argTypes[0]),
}),
normalImpl: <T extends AnyVecInstance>(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,
});

Expand Down