Skip to content

fix: division operator resolution #1446

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 25 commits into
base: main
Choose a base branch
from
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
Original file line number Diff line number Diff line change
Expand Up @@ -122,14 +122,13 @@ export class IcosphereGenerator {
private maxBufferSize?: number,
) {
const { prevVertices, nextVertices, smoothFlag } = generatorLayout.bound;

this.smoothBuffer = this.root.createBuffer(d.u32).$usage('uniform');

const computeFn = tgpu['~unstable'].computeFn({
in: { gid: d.builtin.globalInvocationId },
workgroupSize: [WORKGROUP_SIZE, 1, 1],
})((input) => {
const triangleCount = std.arrayLength(prevVertices.value) / d.u32(3);
const triangleCount = d.u32(std.arrayLength(prevVertices.value) / 3);
const triangleIndex = input.gid.x + input.gid.y * MAX_DISPATCH;
if (triangleIndex >= triangleCount) {
return;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -112,7 +112,7 @@ const getStableStateBelow = tgpu.fn([d.u32, d.u32], d.u32)((upper, lower) => {
return totalMass;
}
if (totalMass >= MAX_WATER_LEVEL_UNPRESSURIZED.$ * 2 && upper > lower) {
return totalMass / 2 + MAX_PRESSURE.$;
return d.u32(totalMass / 2) + MAX_PRESSURE.$;
}
return MAX_WATER_LEVEL_UNPRESSURIZED.$;
});
Expand Down Expand Up @@ -263,11 +263,11 @@ const vertex = tgpu['~unstable'].vertexFn({
d.f32(w)) /
d.f32(std.max(w, h));
const y =
((d.f32((input.idx - (input.idx % w)) / w + d.u32(input.squareData.y)) /
((d.f32((input.idx - (input.idx % w)) / w) + d.f32(input.squareData.y)) /
d.f32(h) -
0.5) *
2 *
d.f32(h)) /
2 *
d.f32(h) /
d.f32(std.max(w, h));
const cellFlags = input.currentStateData >> 24;
let cell = d.f32(input.currentStateData & 0xffffff);
Expand Down
33 changes: 33 additions & 0 deletions packages/typegpu/src/tgsl/wgslGenerator.ts
Original file line number Diff line number Diff line change
Expand Up @@ -156,6 +156,39 @@ export function generateExpression(
const rhsStr = ctx.resolve(convRhs.value);
const type = operatorToType(convLhs.dataType, op, convRhs.dataType);

if (op === '/') {
const lhsIsFloat = convLhs.dataType.type === 'f32' ||
convLhs.dataType.type === 'f16';
const rhsIsFloat = convRhs.dataType.type === 'f32' ||
convRhs.dataType.type === 'f16';

if (!lhsIsFloat && !rhsIsFloat) {
if (
(convLhs.dataType.type === 'u32' ||
convLhs.dataType.type === 'i32') &&
(convRhs.dataType.type === 'u32' ||
convRhs.dataType.type === 'i32')
) {
console.warn(
'In the division, both sides were automatically cast to f32. This might not be the expected behavior. Consider using explicit conversions instead.',
);
}
return snip(
`f32(${lhsStr}) / f32(${rhsStr})`,
d.f32,
);
}

if (!lhsIsFloat || !rhsIsFloat) {
return snip(
!lhsIsFloat
? `f32(${lhsStr}) / ${rhsStr}`
: `${lhsStr} / f32(${rhsStr})`,
d.f32,
);
}
}

return snip(
parenthesizedOps.includes(op)
? `(${lhsStr} ${op} ${rhsStr})`
Expand Down
82 changes: 82 additions & 0 deletions packages/typegpu/tests/tgsl/wgslGenerator.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -966,3 +966,85 @@ describe('wgslGenerator', () => {
`);
});
});

describe('wgslGenerator division operator', () => {
it('tests division operator resolution - u32', () => {
const div = tgpu.fn([], d.f32)(() => {
return d.u32(1) / d.u32(2);
});
expect(div()).toBe(0.5);
expect(parseResolved({ div })).toMatchInlineSnapshot(
`"fn div ( ) -> f32 { return f32 ( u32 ( 1 ) ) / f32 ( u32 ( 2 ) ) ; }"`,
);
});

it('tests division operator resolution - i32', () => {
const div = tgpu.fn([], d.f32)(() => {
return d.i32(1.0) / d.i32(2.0);
});
expect(div()).toBe(0.5);
expect(parseResolved({ divide1: div })).toMatchInlineSnapshot(
`"fn div ( ) -> f32 { return f32 ( i32 ( 1 ) ) / f32 ( i32 ( 2 ) ) ; }"`,
);
});

it('tests division operator resolution - f32', () => {
const div = tgpu.fn([], d.f32)(() => {
return d.f32(1.0) / d.f32(2.0);
});
expect(div()).toBe(0.5);
expect(parseResolved({ divide1: div })).toMatchInlineSnapshot(
`"fn div ( ) -> f32 { return ( f32 ( 1 ) / f32 ( 2 ) ) ; }"`,
);
});

it('tests division operator resolution - f32 & i32', () => {
const div = tgpu.fn([], d.f32)(() => {
return d.f32(1.0) / d.i32(2.0);
});
expect(div()).toBe(0.5);
expect(parseResolved({ divide1: div })).toMatchInlineSnapshot(
`"fn div ( ) -> f32 { return f32 ( 1 ) / f32 ( i32 ( 2 ) ) ; }"`,
);
});

it('tests division operator resolution - u32 & i32', () => {
const div = tgpu.fn([], d.f32)(() => {
return d.u32(1) / d.i32(2);
});
expect(div()).toBe(0.5);
expect(parseResolved({ divide1: div })).toMatchInlineSnapshot(
`"fn div ( ) -> f32 { return f32 ( u32 ( 1 ) ) / f32 ( i32 ( 2 ) ) ; }"`,
);
});

it('tests division operator resolution - f16 & f32', () => {
const div = tgpu.fn([], d.f32)(() => {
return d.f16(1.0) / d.f32(2.0);
});
expect(div()).toBe(0.5);
expect(parseResolved({ divide1: div })).toMatchInlineSnapshot(
`"fn div ( ) -> f32 { return ( f16 ( 1 ) / f32 ( 2 ) ) ; }"`,
);
});

it('tests division operator resolution - decimal & f32', () => {
const div = tgpu.fn([], d.f32)(() => {
return d.f16(1 / 2) / d.f32(5.0);
});
expect(div()).toBe(0.1);
expect(parseResolved({ divide1: div })).toMatchInlineSnapshot(
`"fn div ( ) -> f32 { return ( f16 ( f32 ( 1 ) / f32 ( 2 ) ) / f32 ( 5 ) ) ; }"`,
);
});

it('tests division operator resolution - internal sum & f32', () => {
const div = tgpu.fn([], d.f32)(() => {
return (d.u32(1 + 2) / d.f32(5.0));
});
expect(div()).toBe(0.6);
expect(parseResolved({ divide1: div })).toMatchInlineSnapshot(
`"fn div ( ) -> f32 { return f32 ( u32 ( ( 1 + 2 ) ) ) / f32 ( 5 ) ; }"`,
);
});
});