[hlsl-out] Implement more expressions

This commit is contained in:
Gordon-F 2021-06-24 23:52:37 +03:00 committed by Dzmitry Malyshau
parent 24b2c548bd
commit 19de60f6e4
2 changed files with 135 additions and 4 deletions

View File

@ -847,6 +847,17 @@ impl<'a, W: Write> Writer<'a, W> {
write!(self.out, ")")?
}
}
Expression::Binary {
op: crate::BinaryOperator::Multiply,
left,
right,
} => {
write!(self.out, "mul(")?;
self.write_expr(module, left, func_ctx)?;
write!(self.out, ", ")?;
self.write_expr(module, right, func_ctx)?;
write!(self.out, ")")?;
}
// TODO: copy-paste from wgsl-out
Expression::Binary { op, left, right } => {
write!(self.out, "(")?;
@ -933,12 +944,17 @@ impl<'a, W: Write> Writer<'a, W> {
let name = &self.names[&NameKey::GlobalVariable(handle)];
write!(self.out, "{}", name)?;
}
Expression::Load { pointer } => self.write_expr(module, pointer, func_ctx)?,
Expression::LocalVariable(handle) => {
write!(self.out, "{}", self.names[&func_ctx.name_key(handle)])?
}
Expression::Load { pointer } => {
self.write_expr(module, pointer, func_ctx)?;
}
Expression::Access { base, index } => {
self.write_expr(module, base, func_ctx)?;
write!(self.out, "[")?;
self.write_expr(module, index, func_ctx)?;
write!(self.out, "]")?
write!(self.out, "]")?;
}
Expression::Unary { op, expr } => {
// https://docs.microsoft.com/en-us/windows/win32/direct3dhlsl/dx-graphics-hlsl-operators#unary-operators
@ -966,6 +982,121 @@ impl<'a, W: Write> Writer<'a, W> {
write!(self.out, ")")?
}
Expression::As { expr, kind, .. } => {
let inner = func_ctx.info[expr].ty.inner_with(&module.types);
match *inner {
TypeInner::Vector { size, width, .. } => {
write!(
self.out,
"{}{}",
scalar_kind_str(kind, width)?,
back::vector_size_str(size),
)?;
}
TypeInner::Scalar { width, .. } => {
write!(self.out, "{}", scalar_kind_str(kind, width)?)?
}
_ => {
return Err(Error::Unimplemented(format!(
"write_expr expression::as {:?}",
inner
)));
}
};
write!(self.out, "(")?;
self.write_expr(module, expr, func_ctx)?;
write!(self.out, ")")?;
}
Expression::Math {
fun,
arg,
arg1,
arg2,
} => {
use crate::MathFunction as Mf;
let fun_name = match fun {
// comparison
Mf::Abs => "abs",
Mf::Min => "min",
Mf::Max => "max",
Mf::Clamp => "clamp",
// trigonometry
Mf::Cos => "cos",
Mf::Cosh => "cosh",
Mf::Sin => "sin",
Mf::Sinh => "sinh",
Mf::Tan => "tan",
Mf::Tanh => "tanh",
Mf::Acos => "acos",
Mf::Asin => "asin",
Mf::Atan => "atan",
Mf::Atan2 => "atan2",
// decomposition
Mf::Ceil => "ceil",
Mf::Floor => "floor",
Mf::Round => "round",
Mf::Fract => "frac",
Mf::Trunc => "trunc",
Mf::Modf => "modf",
Mf::Frexp => "frexp",
Mf::Ldexp => "ldexp",
// exponent
Mf::Exp => "exp",
Mf::Exp2 => "exp2",
Mf::Log => "log",
Mf::Log2 => "log2",
Mf::Pow => "pow",
// geometry
Mf::Dot => "dot",
//Mf::Outer => ,
Mf::Cross => "cross",
Mf::Distance => "distance",
Mf::Length => "length",
Mf::Normalize => "normalize",
Mf::FaceForward => "faceforward",
Mf::Reflect => "reflect",
Mf::Refract => "refract",
// computational
Mf::Sign => "sign",
Mf::Fma => "fma",
Mf::Mix => "lerp",
Mf::Step => "step",
Mf::SmoothStep => "smoothstep",
Mf::Sqrt => "sqrt",
Mf::InverseSqrt => "rsqrt",
//Mf::Inverse =>,
Mf::Transpose => "transpose",
Mf::Determinant => "determinant",
// bits
Mf::CountOneBits => "countbits",
Mf::ReverseBits => "reversebits",
_ => return Err(Error::Unimplemented(format!("write_expr_math {:?}", fun))),
};
write!(self.out, "{}(", fun_name)?;
self.write_expr(module, arg, func_ctx)?;
if let Some(arg) = arg1 {
write!(self.out, ", ")?;
self.write_expr(module, arg, func_ctx)?;
}
if let Some(arg) = arg2 {
write!(self.out, ", ")?;
self.write_expr(module, arg, func_ctx)?;
}
write!(self.out, ")")?
}
Expression::Swizzle {
size,
vector,
pattern,
} => {
self.write_expr(module, vector, func_ctx)?;
write!(self.out, ".")?;
for &sc in pattern[..size as usize].iter() {
self.out.write_char(back::COMPONENTS[sc as usize])?;
}
}
_ => return Err(Error::Unimplemented(format!("write_expr {:?}", expression))),
}

View File

@ -19,7 +19,7 @@ struct FragmentInput_main {
VertexOutput main(VertexInput_main vertexinput_main)
{
const VertexOutput vertexoutput1 = { vertexinput_main.uv2, float4((c_scale * vertexinput_main.pos1), 0.0, 1.0) };
const VertexOutput vertexoutput1 = { vertexinput_main.uv2, float4(mul(c_scale, vertexinput_main.pos1), 0.0, 1.0) };
return vertexoutput1;
}
@ -29,7 +29,7 @@ float4 main1(FragmentInput_main fragmentinput_main) : SV_Target0
if ((color.w == 0.0)) {
discard;
}
float4 premultiplied = (color.w * color);
float4 premultiplied = mul(color.w, color);
return premultiplied;
}