Skip to content

Commit

Permalink
[glsl-in] Add support for float encoding/decoding builtins
Browse files Browse the repository at this point in the history
  • Loading branch information
JCapucho authored and kvark committed Aug 21, 2021
1 parent fd3f954 commit c09354c
Showing 1 changed file with 39 additions and 3 deletions.
42 changes: 39 additions & 3 deletions src/front/glsl/builtins.rs
Original file line number Diff line number Diff line change
Expand Up @@ -451,7 +451,8 @@ pub fn inject_builtin(declaration: &mut FunctionDeclaration, module: &mut Module
}
}
"sin" | "exp" | "exp2" | "sinh" | "cos" | "cosh" | "tan" | "tanh" | "acos" | "asin"
| "log" | "log2" | "radians" | "degrees" | "asinh" | "acosh" | "atanh" => {
| "log" | "log2" | "radians" | "degrees" | "asinh" | "acosh" | "atanh"
| "floatBitsToInt" | "floatBitsToUint" => {
// bits layout
// bit 0 trough 1 - dims
for bits in 0..(0b100) {
Expand Down Expand Up @@ -486,11 +487,37 @@ pub fn inject_builtin(declaration: &mut FunctionDeclaration, module: &mut Module
"atanh" => MacroCall::MathFunction(MathFunction::Atanh),
"radians" => MacroCall::ConstMultiply(std::f64::consts::PI / 180.0),
"degrees" => MacroCall::ConstMultiply(180.0 / std::f64::consts::PI),
"floatBitsToInt" => MacroCall::BitCast(Sk::Sint),
"floatBitsToUint" => MacroCall::BitCast(Sk::Uint),
_ => unreachable!(),
},
))
}
}
"intBitsToFloat" | "uintBitsToFloat" => {
// bits layout
// bit 0 trough 1 - dims
for bits in 0..(0b100) {
let size = match bits {
0b00 => None,
0b01 => Some(VectorSize::Bi),
0b10 => Some(VectorSize::Tri),
_ => Some(VectorSize::Quad),
};
let kind = match name {
"intBitsToFloat" => Sk::Sint,
_ => Sk::Uint,
};

declaration.overloads.push(module.add_builtin(
vec![match size {
Some(size) => TypeInner::Vector { size, kind, width },
None => TypeInner::Scalar { kind, width },
}],
MacroCall::BitCast(Sk::Float),
))
}
}
"pow" => {
// bits layout
// bit 0 trough 1 - dims
Expand Down Expand Up @@ -979,8 +1006,7 @@ fn inject_common_builtin(

let fun = match name {
"ceil" => MacroCall::MathFunction(MathFunction::Ceil),
"round" => MacroCall::MathFunction(MathFunction::Round),
"roundEven" => MacroCall::MathFunction(MathFunction::Round),
"round" | "roundEven" => MacroCall::MathFunction(MathFunction::Round),
"floor" => MacroCall::MathFunction(MathFunction::Floor),
"fract" => MacroCall::MathFunction(MathFunction::Fract),
"trunc" => MacroCall::MathFunction(MathFunction::Trunc),
Expand Down Expand Up @@ -1368,6 +1394,7 @@ pub enum MacroCall {
MixBoolean,
Clamp(Option<VectorSize>),
ConstMultiply(f64),
BitCast(Sk),
}

impl MacroCall {
Expand Down Expand Up @@ -1603,6 +1630,15 @@ impl MacroCall {
body,
))
}
MacroCall::BitCast(kind) => Ok(ctx.add_expression(
Expression::As {
expr: args[0],
kind,
convert: None,
},
SourceMetadata::none(),
body,
)),
}
}
}
Expand Down

0 comments on commit c09354c

Please sign in to comment.