Skip to content

Commit

Permalink
[glsl-out] translate mod fn correctly
Browse files Browse the repository at this point in the history
  • Loading branch information
teoxoy committed Apr 27, 2022
1 parent 5d88d0d commit 90b45cc
Show file tree
Hide file tree
Showing 4 changed files with 79 additions and 46 deletions.
35 changes: 33 additions & 2 deletions src/front/glsl/builtins.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1970,14 +1970,45 @@ impl MacroCall {
MacroCall::Mod(size) => {
ctx.implicit_splat(parser, &mut args[1], meta, size)?;

ctx.add_expression(
// x - y * floor(x / y)

let div = ctx.add_expression(
Expression::Binary {
op: BinaryOperator::Modulo,
op: BinaryOperator::Divide,
left: args[0],
right: args[1],
},
Span::default(),
body,
);
let floor = ctx.add_expression(
Expression::Math {
fun: MathFunction::Floor,
arg: div,
arg1: None,
arg2: None,
arg3: None,
},
Span::default(),
body,
);
let mult = ctx.add_expression(
Expression::Binary {
op: BinaryOperator::Multiply,
left: floor,
right: args[1],
},
Span::default(),
body,
);
ctx.add_expression(
Expression::Binary {
op: BinaryOperator::Subtract,
left: args[0],
right: mult,
},
Span::default(),
body,
)
}
MacroCall::Splatted(fun, size, i) => {
Expand Down
2 changes: 1 addition & 1 deletion src/front/glsl/constants.rs
Original file line number Diff line number Diff line change
Expand Up @@ -506,7 +506,7 @@ impl<'a> ConstantSolver<'a> {
BinaryOperator::Subtract => a - b,
BinaryOperator::Multiply => a * b,
BinaryOperator::Divide => a / b,
BinaryOperator::Modulo => a % b,
BinaryOperator::Modulo => a - b * (a / b).floor(),
_ => return Err(ConstantSolvingError::InvalidBinaryOpArgs),
})
}
Expand Down
20 changes: 11 additions & 9 deletions tests/out/wgsl/246-collatz-comp.wgsl
Original file line number Diff line number Diff line change
Expand Up @@ -18,23 +18,25 @@ fn collatz_iterations(n: u32) -> u32 {
}
{
let _e14 = n_1;
if ((f32(_e14) % f32(2)) == f32(0)) {
let _e15 = f32(_e14);
let _e17 = f32(2);
if ((_e15 - (floor((_e15 / _e17)) * _e17)) == f32(0)) {
{
let _e22 = n_1;
n_1 = (_e22 / u32(2));
let _e25 = n_1;
n_1 = (_e25 / u32(2));
}
} else {
{
let _e27 = n_1;
n_1 = ((u32(3) * _e27) + u32(1));
let _e30 = n_1;
n_1 = ((u32(3) * _e30) + u32(1));
}
}
let _e33 = i;
i = (_e33 + 1u);
let _e36 = i;
i = (_e36 + 1u);
}
}
let _e36 = i;
return _e36;
let _e39 = i;
return _e39;
}

fn main_1() {
Expand Down
68 changes: 34 additions & 34 deletions tests/out/wgsl/math-functions-vert.wgsl
Original file line number Diff line number Diff line change
Expand Up @@ -116,41 +116,41 @@ fn main_1() {
let _e160 = b;
let _e162 = a;
let _e164 = b;
modOut = (_e162.x % _e164.x);
let _e170 = a;
let _e171 = b;
powOut = pow(_e170, _e171);
let _e176 = a;
let _e177 = b;
dotOut = dot(_e176, _e177);
let _e182 = a;
let _e183 = b;
maxOut = max(_e182, _e183);
let _e188 = a;
let _e189 = b;
minOut = min(_e188, _e189);
let _e194 = a;
let _e195 = b;
reflectOut = reflect(_e194, _e195);
let _e198 = a;
let _e200 = b;
let _e202 = a;
let _e204 = b;
crossOut = cross(_e202.xyz, _e204.xyz);
let _e210 = a;
let _e211 = b;
outerProductOut = outerProduct(_e210, _e211);
let _e216 = a;
let _e217 = b;
distanceOut = distance(_e216, _e217);
let _e222 = a;
let _e223 = b;
stepOut = step(_e222, _e223);
let _e227 = a;
rad = radians(_e227);
modOut = (_e162.x - (floor((_e162.x / _e164.x)) * _e164.x));
let _e173 = a;
let _e174 = b;
powOut = pow(_e173, _e174);
let _e179 = a;
let _e180 = b;
dotOut = dot(_e179, _e180);
let _e185 = a;
let _e186 = b;
maxOut = max(_e185, _e186);
let _e191 = a;
let _e192 = b;
minOut = min(_e191, _e192);
let _e197 = a;
let _e198 = b;
reflectOut = reflect(_e197, _e198);
let _e201 = a;
let _e203 = b;
let _e205 = a;
let _e207 = b;
crossOut = cross(_e205.xyz, _e207.xyz);
let _e213 = a;
let _e214 = b;
outerProductOut = outerProduct(_e213, _e214);
let _e219 = a;
let _e220 = b;
distanceOut = distance(_e219, _e220);
let _e225 = a;
let _e226 = b;
stepOut = step(_e225, _e226);
let _e230 = a;
let _e232 = a;
deg = degrees(_e232.x);
rad = radians(_e230);
let _e233 = a;
let _e235 = a;
deg = degrees(_e235.x);
return;
}

Expand Down

0 comments on commit 90b45cc

Please sign in to comment.