From 493bfe30fed4c64f0396314e054e98a9c25513ec Mon Sep 17 00:00:00 2001 From: Andy Leiserson Date: Wed, 17 Jun 2026 07:24:55 -0700 Subject: [PATCH] fix(naga hlsl-out): Reduce stack usage (#9648) --- cts_runner/test.lst | 26 +- naga/src/back/hlsl/writer.rs | 1057 +++++++++++++++++----------------- 2 files changed, 533 insertions(+), 550 deletions(-) diff --git a/cts_runner/test.lst b/cts_runner/test.lst index f017612d1..beb282a2c 100644 --- a/cts_runner/test.lst +++ b/cts_runner/test.lst @@ -312,31 +312,7 @@ webgpu:api,validation,texture,destroy:invalid_texture:* webgpu:api,validation,texture,destroy:twice:* webgpu:api,validation,texture,float32_filterable:create_bind_group:* webgpu:api,validation,texture,rg11b10ufloat_renderable:* -webgpu:compat,api,validation,render_pipeline,vertex_state:maxVertexAttributesVertexIndexInstanceIndex:useVertexIndex=false;useInstanceIndex=false;numAttribsToReserve=0;isAsync=false -webgpu:compat,api,validation,render_pipeline,vertex_state:maxVertexAttributesVertexIndexInstanceIndex:useVertexIndex=false;useInstanceIndex=false;numAttribsToReserve=0;isAsync=true -webgpu:compat,api,validation,render_pipeline,vertex_state:maxVertexAttributesVertexIndexInstanceIndex:useVertexIndex=false;useInstanceIndex=false;numAttribsToReserve=1;isAsync=false -webgpu:compat,api,validation,render_pipeline,vertex_state:maxVertexAttributesVertexIndexInstanceIndex:useVertexIndex=false;useInstanceIndex=false;numAttribsToReserve=1;isAsync=true -webgpu:compat,api,validation,render_pipeline,vertex_state:maxVertexAttributesVertexIndexInstanceIndex:useVertexIndex=false;useInstanceIndex=false;numAttribsToReserve=2;isAsync=false -webgpu:compat,api,validation,render_pipeline,vertex_state:maxVertexAttributesVertexIndexInstanceIndex:useVertexIndex=false;useInstanceIndex=false;numAttribsToReserve=2;isAsync=true -webgpu:compat,api,validation,render_pipeline,vertex_state:maxVertexAttributesVertexIndexInstanceIndex:useVertexIndex=false;useInstanceIndex=true;numAttribsToReserve=0;isAsync=false -webgpu:compat,api,validation,render_pipeline,vertex_state:maxVertexAttributesVertexIndexInstanceIndex:useVertexIndex=false;useInstanceIndex=true;numAttribsToReserve=0;isAsync=true -webgpu:compat,api,validation,render_pipeline,vertex_state:maxVertexAttributesVertexIndexInstanceIndex:useVertexIndex=false;useInstanceIndex=true;numAttribsToReserve=1;isAsync=false -webgpu:compat,api,validation,render_pipeline,vertex_state:maxVertexAttributesVertexIndexInstanceIndex:useVertexIndex=false;useInstanceIndex=true;numAttribsToReserve=1;isAsync=true -webgpu:compat,api,validation,render_pipeline,vertex_state:maxVertexAttributesVertexIndexInstanceIndex:useVertexIndex=false;useInstanceIndex=true;numAttribsToReserve=2;isAsync=false -webgpu:compat,api,validation,render_pipeline,vertex_state:maxVertexAttributesVertexIndexInstanceIndex:useVertexIndex=false;useInstanceIndex=true;numAttribsToReserve=2;isAsync=true -webgpu:compat,api,validation,render_pipeline,vertex_state:maxVertexAttributesVertexIndexInstanceIndex:useVertexIndex=true;useInstanceIndex=false;numAttribsToReserve=0;isAsync=false -webgpu:compat,api,validation,render_pipeline,vertex_state:maxVertexAttributesVertexIndexInstanceIndex:useVertexIndex=true;useInstanceIndex=false;numAttribsToReserve=0;isAsync=true -webgpu:compat,api,validation,render_pipeline,vertex_state:maxVertexAttributesVertexIndexInstanceIndex:useVertexIndex=true;useInstanceIndex=false;numAttribsToReserve=1;isAsync=false -webgpu:compat,api,validation,render_pipeline,vertex_state:maxVertexAttributesVertexIndexInstanceIndex:useVertexIndex=true;useInstanceIndex=false;numAttribsToReserve=1;isAsync=true -webgpu:compat,api,validation,render_pipeline,vertex_state:maxVertexAttributesVertexIndexInstanceIndex:useVertexIndex=true;useInstanceIndex=false;numAttribsToReserve=2;isAsync=false -webgpu:compat,api,validation,render_pipeline,vertex_state:maxVertexAttributesVertexIndexInstanceIndex:useVertexIndex=true;useInstanceIndex=false;numAttribsToReserve=2;isAsync=true -webgpu:compat,api,validation,render_pipeline,vertex_state:maxVertexAttributesVertexIndexInstanceIndex:useVertexIndex=true;useInstanceIndex=true;numAttribsToReserve=0;isAsync=false -// https://github.com/gfx-rs/wgpu/issues/9456 -fails-if(dx12) webgpu:compat,api,validation,render_pipeline,vertex_state:maxVertexAttributesVertexIndexInstanceIndex:useVertexIndex=true;useInstanceIndex=true;numAttribsToReserve=0;isAsync=true -webgpu:compat,api,validation,render_pipeline,vertex_state:maxVertexAttributesVertexIndexInstanceIndex:useVertexIndex=true;useInstanceIndex=true;numAttribsToReserve=1;isAsync=false -webgpu:compat,api,validation,render_pipeline,vertex_state:maxVertexAttributesVertexIndexInstanceIndex:useVertexIndex=true;useInstanceIndex=true;numAttribsToReserve=1;isAsync=true -webgpu:compat,api,validation,render_pipeline,vertex_state:maxVertexAttributesVertexIndexInstanceIndex:useVertexIndex=true;useInstanceIndex=true;numAttribsToReserve=2;isAsync=false -webgpu:compat,api,validation,render_pipeline,vertex_state:maxVertexAttributesVertexIndexInstanceIndex:useVertexIndex=true;useInstanceIndex=true;numAttribsToReserve=2;isAsync=true +webgpu:compat,api,validation,render_pipeline,vertex_state:maxVertexAttributesVertexIndexInstanceIndex:* fails-if(dx12) webgpu:shader,execution,expression,call,builtin,atan2:f16:* webgpu:shader,execution,expression,call,builtin,atan2:f32:* diff --git a/naga/src/back/hlsl/writer.rs b/naga/src/back/hlsl/writer.rs index 97a398efa..af3736ff1 100644 --- a/naga/src/back/hlsl/writer.rs +++ b/naga/src/back/hlsl/writer.rs @@ -3068,6 +3068,537 @@ impl<'a, W: fmt::Write> super::Writer<'a, W> { Ok(()) } + #[allow(clippy::too_many_arguments)] + fn write_math_expression( + &mut self, + module: &Module, + fun: crate::MathFunction, + arg: Handle, + arg1: Option>, + arg2: Option>, + arg3: Option>, + func_ctx: &back::FunctionCtx<'_>, + ) -> BackendResult { + use crate::MathFunction as Mf; + + enum Function { + Asincosh { is_sin: bool }, + Atanh, + Pack2x16float, + Pack2x16snorm, + Pack2x16unorm, + Pack4x8snorm, + Pack4x8unorm, + Pack4xI8, + Pack4xU8, + Pack4xI8Clamp, + Pack4xU8Clamp, + Unpack2x16float, + Unpack2x16snorm, + Unpack2x16unorm, + Unpack4x8snorm, + Unpack4x8unorm, + Unpack4xI8, + Unpack4xU8, + Dot4I8Packed, + Dot4U8Packed, + QuantizeToF16, + Regular(&'static str), + MissingIntOverload(&'static str), + MissingIntReturnType(&'static str), + CountTrailingZeros, + CountLeadingZeros, + } + + let fun = match fun { + // comparison + Mf::Abs => match func_ctx.resolve_type(arg, &module.types).scalar() { + Some(Scalar::I32) => Function::Regular(ABS_FUNCTION), + _ => Function::Regular("abs"), + }, + Mf::Min => Function::Regular("min"), + Mf::Max => Function::Regular("max"), + Mf::Clamp => Function::Regular("clamp"), + Mf::Saturate => Function::Regular("saturate"), + // trigonometry + Mf::Cos => Function::Regular("cos"), + Mf::Cosh => Function::Regular("cosh"), + Mf::Sin => Function::Regular("sin"), + Mf::Sinh => Function::Regular("sinh"), + Mf::Tan => Function::Regular("tan"), + Mf::Tanh => Function::Regular("tanh"), + Mf::Acos => Function::Regular("acos"), + Mf::Asin => Function::Regular("asin"), + Mf::Atan => Function::Regular("atan"), + Mf::Atan2 => Function::Regular("atan2"), + Mf::Asinh => Function::Asincosh { is_sin: true }, + Mf::Acosh => Function::Asincosh { is_sin: false }, + Mf::Atanh => Function::Atanh, + Mf::Radians => Function::Regular("radians"), + Mf::Degrees => Function::Regular("degrees"), + // decomposition + Mf::Ceil => Function::Regular("ceil"), + Mf::Floor => Function::Regular("floor"), + Mf::Round => Function::Regular("round"), + Mf::Fract => Function::Regular("frac"), + Mf::Trunc => Function::Regular("trunc"), + Mf::Modf => Function::Regular(MODF_FUNCTION), + Mf::Frexp => Function::Regular(FREXP_FUNCTION), + Mf::Ldexp => Function::Regular("ldexp"), + // exponent + Mf::Exp => Function::Regular("exp"), + Mf::Exp2 => Function::Regular("exp2"), + Mf::Log => Function::Regular("log"), + Mf::Log2 => Function::Regular("log2"), + Mf::Pow => Function::Regular("pow"), + // geometry + Mf::Dot => Function::Regular("dot"), + Mf::Dot4I8Packed => Function::Dot4I8Packed, + Mf::Dot4U8Packed => Function::Dot4U8Packed, + //Mf::Outer => , + Mf::Cross => Function::Regular("cross"), + Mf::Distance => Function::Regular("distance"), + Mf::Length => Function::Regular("length"), + Mf::Normalize => Function::Regular("normalize"), + Mf::FaceForward => Function::Regular("faceforward"), + Mf::Reflect => Function::Regular("reflect"), + Mf::Refract => Function::Regular("refract"), + // computational + Mf::Sign => Function::Regular("sign"), + Mf::Fma => Function::Regular("mad"), + Mf::Mix => Function::Regular("lerp"), + Mf::Step => Function::Regular("step"), + Mf::SmoothStep => Function::Regular("smoothstep"), + Mf::Sqrt => Function::Regular("sqrt"), + Mf::InverseSqrt => Function::Regular("rsqrt"), + //Mf::Inverse =>, + Mf::Transpose => Function::Regular("transpose"), + Mf::Determinant => Function::Regular("determinant"), + Mf::QuantizeToF16 => Function::QuantizeToF16, + // bits + Mf::CountTrailingZeros => Function::CountTrailingZeros, + Mf::CountLeadingZeros => Function::CountLeadingZeros, + Mf::CountOneBits => Function::MissingIntOverload("countbits"), + Mf::ReverseBits => Function::MissingIntOverload("reversebits"), + Mf::FirstTrailingBit => Function::MissingIntReturnType("firstbitlow"), + Mf::FirstLeadingBit => Function::MissingIntReturnType("firstbithigh"), + Mf::ExtractBits => Function::Regular(EXTRACT_BITS_FUNCTION), + Mf::InsertBits => Function::Regular(INSERT_BITS_FUNCTION), + // Data Packing + Mf::Pack2x16float => Function::Pack2x16float, + Mf::Pack2x16snorm => Function::Pack2x16snorm, + Mf::Pack2x16unorm => Function::Pack2x16unorm, + Mf::Pack4x8snorm => Function::Pack4x8snorm, + Mf::Pack4x8unorm => Function::Pack4x8unorm, + Mf::Pack4xI8 => Function::Pack4xI8, + Mf::Pack4xU8 => Function::Pack4xU8, + Mf::Pack4xI8Clamp => Function::Pack4xI8Clamp, + Mf::Pack4xU8Clamp => Function::Pack4xU8Clamp, + // Data Unpacking + Mf::Unpack2x16float => Function::Unpack2x16float, + Mf::Unpack2x16snorm => Function::Unpack2x16snorm, + Mf::Unpack2x16unorm => Function::Unpack2x16unorm, + Mf::Unpack4x8snorm => Function::Unpack4x8snorm, + Mf::Unpack4x8unorm => Function::Unpack4x8unorm, + Mf::Unpack4xI8 => Function::Unpack4xI8, + Mf::Unpack4xU8 => Function::Unpack4xU8, + _ => return Err(Error::Unimplemented(format!("write_expr_math {fun:?}"))), + }; + + match fun { + Function::Asincosh { is_sin } => { + write!(self.out, "log(")?; + self.write_expr(module, arg, func_ctx)?; + write!(self.out, " + sqrt(")?; + self.write_expr(module, arg, func_ctx)?; + write!(self.out, " * ")?; + self.write_expr(module, arg, func_ctx)?; + match is_sin { + true => write!(self.out, " + 1.0))")?, + false => write!(self.out, " - 1.0))")?, + } + } + Function::Atanh => { + write!(self.out, "0.5 * log((1.0 + ")?; + self.write_expr(module, arg, func_ctx)?; + write!(self.out, ") / (1.0 - ")?; + self.write_expr(module, arg, func_ctx)?; + write!(self.out, "))")?; + } + Function::Pack2x16float => { + write!(self.out, "(f32tof16(")?; + self.write_expr(module, arg, func_ctx)?; + write!(self.out, "[0]) | f32tof16(")?; + self.write_expr(module, arg, func_ctx)?; + write!(self.out, "[1]) << 16)")?; + } + Function::Pack2x16snorm => { + let scale = 32767; + + write!(self.out, "uint((int(round(clamp(")?; + self.write_expr(module, arg, func_ctx)?; + write!( + self.out, + "[0], -1.0, 1.0) * {scale}.0)) & 0xFFFF) | ((int(round(clamp(" + )?; + self.write_expr(module, arg, func_ctx)?; + write!(self.out, "[1], -1.0, 1.0) * {scale}.0)) & 0xFFFF) << 16))",)?; + } + Function::Pack2x16unorm => { + let scale = 65535; + + write!(self.out, "(uint(round(clamp(")?; + self.write_expr(module, arg, func_ctx)?; + write!(self.out, "[0], 0.0, 1.0) * {scale}.0)) | uint(round(clamp(")?; + self.write_expr(module, arg, func_ctx)?; + write!(self.out, "[1], 0.0, 1.0) * {scale}.0)) << 16)")?; + } + Function::Pack4x8snorm => { + let scale = 127; + + write!(self.out, "uint((int(round(clamp(")?; + self.write_expr(module, arg, func_ctx)?; + write!( + self.out, + "[0], -1.0, 1.0) * {scale}.0)) & 0xFF) | ((int(round(clamp(" + )?; + self.write_expr(module, arg, func_ctx)?; + write!( + self.out, + "[1], -1.0, 1.0) * {scale}.0)) & 0xFF) << 8) | ((int(round(clamp(" + )?; + self.write_expr(module, arg, func_ctx)?; + write!( + self.out, + "[2], -1.0, 1.0) * {scale}.0)) & 0xFF) << 16) | ((int(round(clamp(" + )?; + self.write_expr(module, arg, func_ctx)?; + write!(self.out, "[3], -1.0, 1.0) * {scale}.0)) & 0xFF) << 24))",)?; + } + Function::Pack4x8unorm => { + let scale = 255; + + write!(self.out, "(uint(round(clamp(")?; + self.write_expr(module, arg, func_ctx)?; + write!(self.out, "[0], 0.0, 1.0) * {scale}.0)) | uint(round(clamp(")?; + self.write_expr(module, arg, func_ctx)?; + write!( + self.out, + "[1], 0.0, 1.0) * {scale}.0)) << 8 | uint(round(clamp(" + )?; + self.write_expr(module, arg, func_ctx)?; + write!( + self.out, + "[2], 0.0, 1.0) * {scale}.0)) << 16 | uint(round(clamp(" + )?; + self.write_expr(module, arg, func_ctx)?; + write!(self.out, "[3], 0.0, 1.0) * {scale}.0)) << 24)")?; + } + fun @ (Function::Pack4xI8 + | Function::Pack4xU8 + | Function::Pack4xI8Clamp + | Function::Pack4xU8Clamp) => { + let was_signed = matches!(fun, Function::Pack4xI8 | Function::Pack4xI8Clamp); + let clamp_bounds = match fun { + Function::Pack4xI8Clamp => Some(("-128", "127")), + Function::Pack4xU8Clamp => Some(("0", "255")), + _ => None, + }; + if was_signed { + write!(self.out, "uint(")?; + } + let write_arg = |this: &mut Self| -> BackendResult { + if let Some((min, max)) = clamp_bounds { + write!(this.out, "clamp(")?; + this.write_expr(module, arg, func_ctx)?; + write!(this.out, ", {min}, {max})")?; + } else { + this.write_expr(module, arg, func_ctx)?; + } + Ok(()) + }; + write!(self.out, "(")?; + write_arg(self)?; + write!(self.out, "[0] & 0xFF) | ((")?; + write_arg(self)?; + write!(self.out, "[1] & 0xFF) << 8) | ((")?; + write_arg(self)?; + write!(self.out, "[2] & 0xFF) << 16) | ((")?; + write_arg(self)?; + write!(self.out, "[3] & 0xFF) << 24)")?; + if was_signed { + write!(self.out, ")")?; + } + } + + Function::Unpack2x16float => { + write!(self.out, "float2(f16tof32(")?; + self.write_expr(module, arg, func_ctx)?; + write!(self.out, "), f16tof32((")?; + self.write_expr(module, arg, func_ctx)?; + write!(self.out, ") >> 16))")?; + } + Function::Unpack2x16snorm => { + let scale = 32767; + + write!(self.out, "(float2(int2(")?; + self.write_expr(module, arg, func_ctx)?; + write!(self.out, " << 16, ")?; + self.write_expr(module, arg, func_ctx)?; + write!(self.out, ") >> 16) / {scale}.0)")?; + } + Function::Unpack2x16unorm => { + let scale = 65535; + + write!(self.out, "(float2(")?; + self.write_expr(module, arg, func_ctx)?; + write!(self.out, " & 0xFFFF, ")?; + self.write_expr(module, arg, func_ctx)?; + write!(self.out, " >> 16) / {scale}.0)")?; + } + Function::Unpack4x8snorm => { + let scale = 127; + + write!(self.out, "(float4(int4(")?; + self.write_expr(module, arg, func_ctx)?; + write!(self.out, " << 24, ")?; + self.write_expr(module, arg, func_ctx)?; + write!(self.out, " << 16, ")?; + self.write_expr(module, arg, func_ctx)?; + write!(self.out, " << 8, ")?; + self.write_expr(module, arg, func_ctx)?; + write!(self.out, ") >> 24) / {scale}.0)")?; + } + Function::Unpack4x8unorm => { + let scale = 255; + + write!(self.out, "(float4(")?; + self.write_expr(module, arg, func_ctx)?; + write!(self.out, " & 0xFF, ")?; + self.write_expr(module, arg, func_ctx)?; + write!(self.out, " >> 8 & 0xFF, ")?; + self.write_expr(module, arg, func_ctx)?; + write!(self.out, " >> 16 & 0xFF, ")?; + self.write_expr(module, arg, func_ctx)?; + write!(self.out, " >> 24) / {scale}.0)")?; + } + fun @ (Function::Unpack4xI8 | Function::Unpack4xU8) => { + write!(self.out, "(")?; + if matches!(fun, Function::Unpack4xU8) { + write!(self.out, "u")?; + } + write!(self.out, "int4(")?; + self.write_expr(module, arg, func_ctx)?; + write!(self.out, ", ")?; + self.write_expr(module, arg, func_ctx)?; + write!(self.out, " >> 8, ")?; + self.write_expr(module, arg, func_ctx)?; + write!(self.out, " >> 16, ")?; + self.write_expr(module, arg, func_ctx)?; + write!(self.out, " >> 24) << 24 >> 24)")?; + } + fun @ (Function::Dot4I8Packed | Function::Dot4U8Packed) => { + let arg1 = arg1.unwrap(); + + if self.options.shader_model >= ShaderModel::V6_4 { + // Intrinsics `dot4add_{i, u}8packed` are available in SM 6.4 and later. + let function_name = match fun { + Function::Dot4I8Packed => "dot4add_i8packed", + Function::Dot4U8Packed => "dot4add_u8packed", + _ => unreachable!(), + }; + write!(self.out, "{function_name}(")?; + self.write_expr(module, arg, func_ctx)?; + write!(self.out, ", ")?; + self.write_expr(module, arg1, func_ctx)?; + write!(self.out, ", 0)")?; + } else { + // Fall back to a polyfill as `dot4add_u8packed` is not available. + write!(self.out, "dot(")?; + + if matches!(fun, Function::Dot4U8Packed) { + write!(self.out, "u")?; + } + write!(self.out, "int4(")?; + self.write_expr(module, arg, func_ctx)?; + write!(self.out, ", ")?; + self.write_expr(module, arg, func_ctx)?; + write!(self.out, " >> 8, ")?; + self.write_expr(module, arg, func_ctx)?; + write!(self.out, " >> 16, ")?; + self.write_expr(module, arg, func_ctx)?; + write!(self.out, " >> 24) << 24 >> 24, ")?; + + if matches!(fun, Function::Dot4U8Packed) { + write!(self.out, "u")?; + } + write!(self.out, "int4(")?; + self.write_expr(module, arg1, func_ctx)?; + write!(self.out, ", ")?; + self.write_expr(module, arg1, func_ctx)?; + write!(self.out, " >> 8, ")?; + self.write_expr(module, arg1, func_ctx)?; + write!(self.out, " >> 16, ")?; + self.write_expr(module, arg1, func_ctx)?; + write!(self.out, " >> 24) << 24 >> 24)")?; + } + } + Function::QuantizeToF16 => { + write!(self.out, "f16tof32(f32tof16(")?; + self.write_expr(module, arg, func_ctx)?; + write!(self.out, "))")?; + } + Function::Regular(fun_name) => { + 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)?; + } + if let Some(arg) = arg3 { + write!(self.out, ", ")?; + self.write_expr(module, arg, func_ctx)?; + } + write!(self.out, ")")? + } + // These overloads are only missing on FXC, so this is only needed for 32bit types, + // as non-32bit types are DXC only. + Function::MissingIntOverload(fun_name) => { + let scalar_kind = func_ctx.resolve_type(arg, &module.types).scalar(); + if let Some(Scalar::I32) = scalar_kind { + write!(self.out, "asint({fun_name}(asuint(")?; + 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, ")")?; + } + } + // These overloads are only missing on FXC, so this is only needed for 32bit types, + // as non-32bit types are DXC only. + Function::MissingIntReturnType(fun_name) => { + let scalar_kind = func_ctx.resolve_type(arg, &module.types).scalar(); + if let Some(Scalar::I32) = scalar_kind { + write!(self.out, "asint({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, ")")?; + } + } + Function::CountTrailingZeros => { + match *func_ctx.resolve_type(arg, &module.types) { + TypeInner::Vector { size, scalar } => { + let s = match size { + crate::VectorSize::Bi => ".xx", + crate::VectorSize::Tri => ".xxx", + crate::VectorSize::Quad => ".xxxx", + }; + + let scalar_width_bits = scalar.width * 8; + + if scalar.kind == ScalarKind::Uint || scalar.width != 4 { + write!(self.out, "min(({scalar_width_bits}u){s}, firstbitlow(")?; + self.write_expr(module, arg, func_ctx)?; + write!(self.out, "))")?; + } else { + // This is only needed for the FXC path, on 32bit signed integers. + write!( + self.out, + "asint(min(({scalar_width_bits}u){s}, firstbitlow(" + )?; + self.write_expr(module, arg, func_ctx)?; + write!(self.out, ")))")?; + } + } + TypeInner::Scalar(scalar) => { + let scalar_width_bits = scalar.width * 8; + + if scalar.kind == ScalarKind::Uint || scalar.width != 4 { + write!(self.out, "min({scalar_width_bits}u, firstbitlow(")?; + self.write_expr(module, arg, func_ctx)?; + write!(self.out, "))")?; + } else { + // This is only needed for the FXC path, on 32bit signed integers. + write!(self.out, "asint(min({scalar_width_bits}u, firstbitlow(")?; + self.write_expr(module, arg, func_ctx)?; + write!(self.out, ")))")?; + } + } + _ => unreachable!(), + } + + return Ok(()); + } + Function::CountLeadingZeros => { + match *func_ctx.resolve_type(arg, &module.types) { + TypeInner::Vector { size, scalar } => { + let s = match size { + crate::VectorSize::Bi => ".xx", + crate::VectorSize::Tri => ".xxx", + crate::VectorSize::Quad => ".xxxx", + }; + + // scalar width - 1 + let constant = scalar.width * 8 - 1; + + if scalar.kind == ScalarKind::Uint { + write!(self.out, "(({constant}u){s} - firstbithigh(")?; + self.write_expr(module, arg, func_ctx)?; + write!(self.out, "))")?; + } else { + let conversion_func = match scalar.width { + 4 => "asint", + _ => "", + }; + write!(self.out, "(")?; + self.write_expr(module, arg, func_ctx)?; + write!( + self.out, + " < (0){s} ? (0){s} : ({constant}){s} - {conversion_func}(firstbithigh(" + )?; + self.write_expr(module, arg, func_ctx)?; + write!(self.out, ")))")?; + } + } + TypeInner::Scalar(scalar) => { + // scalar width - 1 + let constant = scalar.width * 8 - 1; + + if let ScalarKind::Uint = scalar.kind { + write!(self.out, "({constant}u - firstbithigh(")?; + self.write_expr(module, arg, func_ctx)?; + write!(self.out, "))")?; + } else { + let conversion_func = match scalar.width { + 4 => "asint", + _ => "", + }; + write!(self.out, "(")?; + self.write_expr(module, arg, func_ctx)?; + write!( + self.out, + " < 0 ? 0 : {constant} - {conversion_func}(firstbithigh(" + )?; + self.write_expr(module, arg, func_ctx)?; + write!(self.out, ")))")?; + } + } + _ => unreachable!(), + } + } + } + + Ok(()) + } + fn write_const_expression( &mut self, module: &Module, @@ -3980,531 +4511,7 @@ impl<'a, W: fmt::Write> super::Writer<'a, W> { arg2, arg3, } => { - use crate::MathFunction as Mf; - - enum Function { - Asincosh { is_sin: bool }, - Atanh, - Pack2x16float, - Pack2x16snorm, - Pack2x16unorm, - Pack4x8snorm, - Pack4x8unorm, - Pack4xI8, - Pack4xU8, - Pack4xI8Clamp, - Pack4xU8Clamp, - Unpack2x16float, - Unpack2x16snorm, - Unpack2x16unorm, - Unpack4x8snorm, - Unpack4x8unorm, - Unpack4xI8, - Unpack4xU8, - Dot4I8Packed, - Dot4U8Packed, - QuantizeToF16, - Regular(&'static str), - MissingIntOverload(&'static str), - MissingIntReturnType(&'static str), - CountTrailingZeros, - CountLeadingZeros, - } - - let fun = match fun { - // comparison - Mf::Abs => match func_ctx.resolve_type(arg, &module.types).scalar() { - Some(Scalar::I32) => Function::Regular(ABS_FUNCTION), - _ => Function::Regular("abs"), - }, - Mf::Min => Function::Regular("min"), - Mf::Max => Function::Regular("max"), - Mf::Clamp => Function::Regular("clamp"), - Mf::Saturate => Function::Regular("saturate"), - // trigonometry - Mf::Cos => Function::Regular("cos"), - Mf::Cosh => Function::Regular("cosh"), - Mf::Sin => Function::Regular("sin"), - Mf::Sinh => Function::Regular("sinh"), - Mf::Tan => Function::Regular("tan"), - Mf::Tanh => Function::Regular("tanh"), - Mf::Acos => Function::Regular("acos"), - Mf::Asin => Function::Regular("asin"), - Mf::Atan => Function::Regular("atan"), - Mf::Atan2 => Function::Regular("atan2"), - Mf::Asinh => Function::Asincosh { is_sin: true }, - Mf::Acosh => Function::Asincosh { is_sin: false }, - Mf::Atanh => Function::Atanh, - Mf::Radians => Function::Regular("radians"), - Mf::Degrees => Function::Regular("degrees"), - // decomposition - Mf::Ceil => Function::Regular("ceil"), - Mf::Floor => Function::Regular("floor"), - Mf::Round => Function::Regular("round"), - Mf::Fract => Function::Regular("frac"), - Mf::Trunc => Function::Regular("trunc"), - Mf::Modf => Function::Regular(MODF_FUNCTION), - Mf::Frexp => Function::Regular(FREXP_FUNCTION), - Mf::Ldexp => Function::Regular("ldexp"), - // exponent - Mf::Exp => Function::Regular("exp"), - Mf::Exp2 => Function::Regular("exp2"), - Mf::Log => Function::Regular("log"), - Mf::Log2 => Function::Regular("log2"), - Mf::Pow => Function::Regular("pow"), - // geometry - Mf::Dot => Function::Regular("dot"), - Mf::Dot4I8Packed => Function::Dot4I8Packed, - Mf::Dot4U8Packed => Function::Dot4U8Packed, - //Mf::Outer => , - Mf::Cross => Function::Regular("cross"), - Mf::Distance => Function::Regular("distance"), - Mf::Length => Function::Regular("length"), - Mf::Normalize => Function::Regular("normalize"), - Mf::FaceForward => Function::Regular("faceforward"), - Mf::Reflect => Function::Regular("reflect"), - Mf::Refract => Function::Regular("refract"), - // computational - Mf::Sign => Function::Regular("sign"), - Mf::Fma => Function::Regular("mad"), - Mf::Mix => Function::Regular("lerp"), - Mf::Step => Function::Regular("step"), - Mf::SmoothStep => Function::Regular("smoothstep"), - Mf::Sqrt => Function::Regular("sqrt"), - Mf::InverseSqrt => Function::Regular("rsqrt"), - //Mf::Inverse =>, - Mf::Transpose => Function::Regular("transpose"), - Mf::Determinant => Function::Regular("determinant"), - Mf::QuantizeToF16 => Function::QuantizeToF16, - // bits - Mf::CountTrailingZeros => Function::CountTrailingZeros, - Mf::CountLeadingZeros => Function::CountLeadingZeros, - Mf::CountOneBits => Function::MissingIntOverload("countbits"), - Mf::ReverseBits => Function::MissingIntOverload("reversebits"), - Mf::FirstTrailingBit => Function::MissingIntReturnType("firstbitlow"), - Mf::FirstLeadingBit => Function::MissingIntReturnType("firstbithigh"), - Mf::ExtractBits => Function::Regular(EXTRACT_BITS_FUNCTION), - Mf::InsertBits => Function::Regular(INSERT_BITS_FUNCTION), - // Data Packing - Mf::Pack2x16float => Function::Pack2x16float, - Mf::Pack2x16snorm => Function::Pack2x16snorm, - Mf::Pack2x16unorm => Function::Pack2x16unorm, - Mf::Pack4x8snorm => Function::Pack4x8snorm, - Mf::Pack4x8unorm => Function::Pack4x8unorm, - Mf::Pack4xI8 => Function::Pack4xI8, - Mf::Pack4xU8 => Function::Pack4xU8, - Mf::Pack4xI8Clamp => Function::Pack4xI8Clamp, - Mf::Pack4xU8Clamp => Function::Pack4xU8Clamp, - // Data Unpacking - Mf::Unpack2x16float => Function::Unpack2x16float, - Mf::Unpack2x16snorm => Function::Unpack2x16snorm, - Mf::Unpack2x16unorm => Function::Unpack2x16unorm, - Mf::Unpack4x8snorm => Function::Unpack4x8snorm, - Mf::Unpack4x8unorm => Function::Unpack4x8unorm, - Mf::Unpack4xI8 => Function::Unpack4xI8, - Mf::Unpack4xU8 => Function::Unpack4xU8, - _ => return Err(Error::Unimplemented(format!("write_expr_math {fun:?}"))), - }; - - match fun { - Function::Asincosh { is_sin } => { - write!(self.out, "log(")?; - self.write_expr(module, arg, func_ctx)?; - write!(self.out, " + sqrt(")?; - self.write_expr(module, arg, func_ctx)?; - write!(self.out, " * ")?; - self.write_expr(module, arg, func_ctx)?; - match is_sin { - true => write!(self.out, " + 1.0))")?, - false => write!(self.out, " - 1.0))")?, - } - } - Function::Atanh => { - write!(self.out, "0.5 * log((1.0 + ")?; - self.write_expr(module, arg, func_ctx)?; - write!(self.out, ") / (1.0 - ")?; - self.write_expr(module, arg, func_ctx)?; - write!(self.out, "))")?; - } - Function::Pack2x16float => { - write!(self.out, "(f32tof16(")?; - self.write_expr(module, arg, func_ctx)?; - write!(self.out, "[0]) | f32tof16(")?; - self.write_expr(module, arg, func_ctx)?; - write!(self.out, "[1]) << 16)")?; - } - Function::Pack2x16snorm => { - let scale = 32767; - - write!(self.out, "uint((int(round(clamp(")?; - self.write_expr(module, arg, func_ctx)?; - write!( - self.out, - "[0], -1.0, 1.0) * {scale}.0)) & 0xFFFF) | ((int(round(clamp(" - )?; - self.write_expr(module, arg, func_ctx)?; - write!(self.out, "[1], -1.0, 1.0) * {scale}.0)) & 0xFFFF) << 16))",)?; - } - Function::Pack2x16unorm => { - let scale = 65535; - - write!(self.out, "(uint(round(clamp(")?; - self.write_expr(module, arg, func_ctx)?; - write!(self.out, "[0], 0.0, 1.0) * {scale}.0)) | uint(round(clamp(")?; - self.write_expr(module, arg, func_ctx)?; - write!(self.out, "[1], 0.0, 1.0) * {scale}.0)) << 16)")?; - } - Function::Pack4x8snorm => { - let scale = 127; - - write!(self.out, "uint((int(round(clamp(")?; - self.write_expr(module, arg, func_ctx)?; - write!( - self.out, - "[0], -1.0, 1.0) * {scale}.0)) & 0xFF) | ((int(round(clamp(" - )?; - self.write_expr(module, arg, func_ctx)?; - write!( - self.out, - "[1], -1.0, 1.0) * {scale}.0)) & 0xFF) << 8) | ((int(round(clamp(" - )?; - self.write_expr(module, arg, func_ctx)?; - write!( - self.out, - "[2], -1.0, 1.0) * {scale}.0)) & 0xFF) << 16) | ((int(round(clamp(" - )?; - self.write_expr(module, arg, func_ctx)?; - write!(self.out, "[3], -1.0, 1.0) * {scale}.0)) & 0xFF) << 24))",)?; - } - Function::Pack4x8unorm => { - let scale = 255; - - write!(self.out, "(uint(round(clamp(")?; - self.write_expr(module, arg, func_ctx)?; - write!(self.out, "[0], 0.0, 1.0) * {scale}.0)) | uint(round(clamp(")?; - self.write_expr(module, arg, func_ctx)?; - write!( - self.out, - "[1], 0.0, 1.0) * {scale}.0)) << 8 | uint(round(clamp(" - )?; - self.write_expr(module, arg, func_ctx)?; - write!( - self.out, - "[2], 0.0, 1.0) * {scale}.0)) << 16 | uint(round(clamp(" - )?; - self.write_expr(module, arg, func_ctx)?; - write!(self.out, "[3], 0.0, 1.0) * {scale}.0)) << 24)")?; - } - fun @ (Function::Pack4xI8 - | Function::Pack4xU8 - | Function::Pack4xI8Clamp - | Function::Pack4xU8Clamp) => { - let was_signed = - matches!(fun, Function::Pack4xI8 | Function::Pack4xI8Clamp); - let clamp_bounds = match fun { - Function::Pack4xI8Clamp => Some(("-128", "127")), - Function::Pack4xU8Clamp => Some(("0", "255")), - _ => None, - }; - if was_signed { - write!(self.out, "uint(")?; - } - let write_arg = |this: &mut Self| -> BackendResult { - if let Some((min, max)) = clamp_bounds { - write!(this.out, "clamp(")?; - this.write_expr(module, arg, func_ctx)?; - write!(this.out, ", {min}, {max})")?; - } else { - this.write_expr(module, arg, func_ctx)?; - } - Ok(()) - }; - write!(self.out, "(")?; - write_arg(self)?; - write!(self.out, "[0] & 0xFF) | ((")?; - write_arg(self)?; - write!(self.out, "[1] & 0xFF) << 8) | ((")?; - write_arg(self)?; - write!(self.out, "[2] & 0xFF) << 16) | ((")?; - write_arg(self)?; - write!(self.out, "[3] & 0xFF) << 24)")?; - if was_signed { - write!(self.out, ")")?; - } - } - - Function::Unpack2x16float => { - write!(self.out, "float2(f16tof32(")?; - self.write_expr(module, arg, func_ctx)?; - write!(self.out, "), f16tof32((")?; - self.write_expr(module, arg, func_ctx)?; - write!(self.out, ") >> 16))")?; - } - Function::Unpack2x16snorm => { - let scale = 32767; - - write!(self.out, "(float2(int2(")?; - self.write_expr(module, arg, func_ctx)?; - write!(self.out, " << 16, ")?; - self.write_expr(module, arg, func_ctx)?; - write!(self.out, ") >> 16) / {scale}.0)")?; - } - Function::Unpack2x16unorm => { - let scale = 65535; - - write!(self.out, "(float2(")?; - self.write_expr(module, arg, func_ctx)?; - write!(self.out, " & 0xFFFF, ")?; - self.write_expr(module, arg, func_ctx)?; - write!(self.out, " >> 16) / {scale}.0)")?; - } - Function::Unpack4x8snorm => { - let scale = 127; - - write!(self.out, "(float4(int4(")?; - self.write_expr(module, arg, func_ctx)?; - write!(self.out, " << 24, ")?; - self.write_expr(module, arg, func_ctx)?; - write!(self.out, " << 16, ")?; - self.write_expr(module, arg, func_ctx)?; - write!(self.out, " << 8, ")?; - self.write_expr(module, arg, func_ctx)?; - write!(self.out, ") >> 24) / {scale}.0)")?; - } - Function::Unpack4x8unorm => { - let scale = 255; - - write!(self.out, "(float4(")?; - self.write_expr(module, arg, func_ctx)?; - write!(self.out, " & 0xFF, ")?; - self.write_expr(module, arg, func_ctx)?; - write!(self.out, " >> 8 & 0xFF, ")?; - self.write_expr(module, arg, func_ctx)?; - write!(self.out, " >> 16 & 0xFF, ")?; - self.write_expr(module, arg, func_ctx)?; - write!(self.out, " >> 24) / {scale}.0)")?; - } - fun @ (Function::Unpack4xI8 | Function::Unpack4xU8) => { - write!(self.out, "(")?; - if matches!(fun, Function::Unpack4xU8) { - write!(self.out, "u")?; - } - write!(self.out, "int4(")?; - self.write_expr(module, arg, func_ctx)?; - write!(self.out, ", ")?; - self.write_expr(module, arg, func_ctx)?; - write!(self.out, " >> 8, ")?; - self.write_expr(module, arg, func_ctx)?; - write!(self.out, " >> 16, ")?; - self.write_expr(module, arg, func_ctx)?; - write!(self.out, " >> 24) << 24 >> 24)")?; - } - fun @ (Function::Dot4I8Packed | Function::Dot4U8Packed) => { - let arg1 = arg1.unwrap(); - - if self.options.shader_model >= ShaderModel::V6_4 { - // Intrinsics `dot4add_{i, u}8packed` are available in SM 6.4 and later. - let function_name = match fun { - Function::Dot4I8Packed => "dot4add_i8packed", - Function::Dot4U8Packed => "dot4add_u8packed", - _ => unreachable!(), - }; - write!(self.out, "{function_name}(")?; - self.write_expr(module, arg, func_ctx)?; - write!(self.out, ", ")?; - self.write_expr(module, arg1, func_ctx)?; - write!(self.out, ", 0)")?; - } else { - // Fall back to a polyfill as `dot4add_u8packed` is not available. - write!(self.out, "dot(")?; - - if matches!(fun, Function::Dot4U8Packed) { - write!(self.out, "u")?; - } - write!(self.out, "int4(")?; - self.write_expr(module, arg, func_ctx)?; - write!(self.out, ", ")?; - self.write_expr(module, arg, func_ctx)?; - write!(self.out, " >> 8, ")?; - self.write_expr(module, arg, func_ctx)?; - write!(self.out, " >> 16, ")?; - self.write_expr(module, arg, func_ctx)?; - write!(self.out, " >> 24) << 24 >> 24, ")?; - - if matches!(fun, Function::Dot4U8Packed) { - write!(self.out, "u")?; - } - write!(self.out, "int4(")?; - self.write_expr(module, arg1, func_ctx)?; - write!(self.out, ", ")?; - self.write_expr(module, arg1, func_ctx)?; - write!(self.out, " >> 8, ")?; - self.write_expr(module, arg1, func_ctx)?; - write!(self.out, " >> 16, ")?; - self.write_expr(module, arg1, func_ctx)?; - write!(self.out, " >> 24) << 24 >> 24)")?; - } - } - Function::QuantizeToF16 => { - write!(self.out, "f16tof32(f32tof16(")?; - self.write_expr(module, arg, func_ctx)?; - write!(self.out, "))")?; - } - Function::Regular(fun_name) => { - 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)?; - } - if let Some(arg) = arg3 { - write!(self.out, ", ")?; - self.write_expr(module, arg, func_ctx)?; - } - write!(self.out, ")")? - } - // These overloads are only missing on FXC, so this is only needed for 32bit types, - // as non-32bit types are DXC only. - Function::MissingIntOverload(fun_name) => { - let scalar_kind = func_ctx.resolve_type(arg, &module.types).scalar(); - if let Some(Scalar::I32) = scalar_kind { - write!(self.out, "asint({fun_name}(asuint(")?; - 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, ")")?; - } - } - // These overloads are only missing on FXC, so this is only needed for 32bit types, - // as non-32bit types are DXC only. - Function::MissingIntReturnType(fun_name) => { - let scalar_kind = func_ctx.resolve_type(arg, &module.types).scalar(); - if let Some(Scalar::I32) = scalar_kind { - write!(self.out, "asint({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, ")")?; - } - } - Function::CountTrailingZeros => { - match *func_ctx.resolve_type(arg, &module.types) { - TypeInner::Vector { size, scalar } => { - let s = match size { - crate::VectorSize::Bi => ".xx", - crate::VectorSize::Tri => ".xxx", - crate::VectorSize::Quad => ".xxxx", - }; - - let scalar_width_bits = scalar.width * 8; - - if scalar.kind == ScalarKind::Uint || scalar.width != 4 { - write!( - self.out, - "min(({scalar_width_bits}u){s}, firstbitlow(" - )?; - self.write_expr(module, arg, func_ctx)?; - write!(self.out, "))")?; - } else { - // This is only needed for the FXC path, on 32bit signed integers. - write!( - self.out, - "asint(min(({scalar_width_bits}u){s}, firstbitlow(" - )?; - self.write_expr(module, arg, func_ctx)?; - write!(self.out, ")))")?; - } - } - TypeInner::Scalar(scalar) => { - let scalar_width_bits = scalar.width * 8; - - if scalar.kind == ScalarKind::Uint || scalar.width != 4 { - write!(self.out, "min({scalar_width_bits}u, firstbitlow(")?; - self.write_expr(module, arg, func_ctx)?; - write!(self.out, "))")?; - } else { - // This is only needed for the FXC path, on 32bit signed integers. - write!( - self.out, - "asint(min({scalar_width_bits}u, firstbitlow(" - )?; - self.write_expr(module, arg, func_ctx)?; - write!(self.out, ")))")?; - } - } - _ => unreachable!(), - } - - return Ok(()); - } - Function::CountLeadingZeros => { - match *func_ctx.resolve_type(arg, &module.types) { - TypeInner::Vector { size, scalar } => { - let s = match size { - crate::VectorSize::Bi => ".xx", - crate::VectorSize::Tri => ".xxx", - crate::VectorSize::Quad => ".xxxx", - }; - - // scalar width - 1 - let constant = scalar.width * 8 - 1; - - if scalar.kind == ScalarKind::Uint { - write!(self.out, "(({constant}u){s} - firstbithigh(")?; - self.write_expr(module, arg, func_ctx)?; - write!(self.out, "))")?; - } else { - let conversion_func = match scalar.width { - 4 => "asint", - _ => "", - }; - write!(self.out, "(")?; - self.write_expr(module, arg, func_ctx)?; - write!( - self.out, - " < (0){s} ? (0){s} : ({constant}){s} - {conversion_func}(firstbithigh(" - )?; - self.write_expr(module, arg, func_ctx)?; - write!(self.out, ")))")?; - } - } - TypeInner::Scalar(scalar) => { - // scalar width - 1 - let constant = scalar.width * 8 - 1; - - if let ScalarKind::Uint = scalar.kind { - write!(self.out, "({constant}u - firstbithigh(")?; - self.write_expr(module, arg, func_ctx)?; - write!(self.out, "))")?; - } else { - let conversion_func = match scalar.width { - 4 => "asint", - _ => "", - }; - write!(self.out, "(")?; - self.write_expr(module, arg, func_ctx)?; - write!( - self.out, - " < 0 ? 0 : {constant} - {conversion_func}(firstbithigh(" - )?; - self.write_expr(module, arg, func_ctx)?; - write!(self.out, ")))")?; - } - } - _ => unreachable!(), - } - - return Ok(()); - } - } + return self.write_math_expression(module, fun, arg, arg1, arg2, arg3, func_ctx); } Expression::Swizzle { size,