Skip to content

Commit

Permalink
[hlsl-out] Fix countOneBits and reverseBits for signed integers
Browse files Browse the repository at this point in the history
  • Loading branch information
hasali19 committed May 16, 2022
1 parent 60ae549 commit 01325c6
Showing 1 changed file with 19 additions and 3 deletions.
22 changes: 19 additions & 3 deletions src/back/hlsl/writer.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ use super::{
use crate::{
back,
proc::{self, NameKey},
valid, Handle, Module, ShaderStage, TypeInner,
valid, Handle, Module, ScalarKind, ShaderStage, TypeInner,
};
use std::{fmt, mem};

Expand Down Expand Up @@ -2190,6 +2190,7 @@ impl<'a, W: fmt::Write> super::Writer<'a, W> {
Asincosh { is_sin: bool },
Atanh,
Regular(&'static str),
UintWrapped(&'static str),
}

let fun = match fun {
Expand Down Expand Up @@ -2251,8 +2252,8 @@ impl<'a, W: fmt::Write> super::Writer<'a, W> {
Mf::Transpose => Function::Regular("transpose"),
Mf::Determinant => Function::Regular("determinant"),
// bits
Mf::CountOneBits => Function::Regular("countbits"),
Mf::ReverseBits => Function::Regular("reversebits"),
Mf::CountOneBits => Function::UintWrapped("countbits"),
Mf::ReverseBits => Function::UintWrapped("reversebits"),
Mf::FindLsb => Function::Regular("firstbitlow"),
Mf::FindMsb => Function::Regular("firstbithigh"),
_ => return Err(Error::Unimplemented(format!("write_expr_math {:?}", fun))),
Expand Down Expand Up @@ -2295,6 +2296,21 @@ impl<'a, W: fmt::Write> super::Writer<'a, W> {
}
write!(self.out, ")")?
}
Function::UintWrapped(fun_name) => {
let scalar_kind = &func_ctx.info[arg]
.ty
.inner_with(&module.types)
.scalar_kind();
if matches!(scalar_kind, Some(ScalarKind::Sint)) {
write!(self.out, "asint({}(asuint(", fun_name)?;
self.write_expr(module, arg, func_ctx)?;
write!(self.out, ")))")?;
} else {
write!(self.out, "{}(", fun_name)?;
self.write_expr(module, arg, func_ctx)?;
write!(self.out, ")")?;
}
}
}
}
Expression::Swizzle {
Expand Down

0 comments on commit 01325c6

Please sign in to comment.