From e8e57d4e33d1376c8d98169530115e61961ab0e5 Mon Sep 17 00:00:00 2001 From: Jim Blandy Date: Wed, 22 Nov 2023 10:13:48 -0800 Subject: [PATCH] [naga] Support casting to f64 in the constant evaluator. --- naga/src/proc/constant_evaluator.rs | 8 +++ naga/tests/out/glsl/f64.main.Compute.glsl | 2 +- naga/tests/out/hlsl/f64.hlsl | 2 +- naga/tests/out/spv/f64.spvasm | 49 +++++++++---------- .../out/wgsl/double-math-functions.frag.wgsl | 10 ++-- naga/tests/out/wgsl/f64.wgsl | 2 +- 6 files changed, 38 insertions(+), 35 deletions(-) diff --git a/naga/src/proc/constant_evaluator.rs b/naga/src/proc/constant_evaluator.rs index d401b1e4de..e3c07f9e16 100644 --- a/naga/src/proc/constant_evaluator.rs +++ b/naga/src/proc/constant_evaluator.rs @@ -998,6 +998,14 @@ impl<'a> ConstantEvaluator<'a> { return Err(ConstantEvaluatorError::InvalidCastArg) } }), + Sc::F64 => Literal::F64(match literal { + Literal::I32(v) => v as f64, + Literal::U32(v) => v as f64, + Literal::F32(v) => v as f64, + Literal::Bool(v) => v as u32 as f64, + Literal::F64(v) => v, + Literal::I64(_) => return Err(ConstantEvaluatorError::InvalidCastArg), + }), Sc::BOOL => Literal::Bool(match literal { Literal::I32(v) => v != 0, Literal::U32(v) => v != 0, diff --git a/naga/tests/out/glsl/f64.main.Compute.glsl b/naga/tests/out/glsl/f64.main.Compute.glsl index 7d20f8d6fc..055965d153 100644 --- a/naga/tests/out/glsl/f64.main.Compute.glsl +++ b/naga/tests/out/glsl/f64.main.Compute.glsl @@ -8,7 +8,7 @@ const double k = 2.0LF; double f(double x) { double z = 0.0; double y = (30.0LF + 400.0LF); - z = (y + double(5)); + z = (y + 5.0LF); return (((x + y) + k) + 5.0LF); } diff --git a/naga/tests/out/hlsl/f64.hlsl b/naga/tests/out/hlsl/f64.hlsl index 5b87b26341..7b0fb9f67e 100644 --- a/naga/tests/out/hlsl/f64.hlsl +++ b/naga/tests/out/hlsl/f64.hlsl @@ -7,7 +7,7 @@ double f(double x) double z = (double)0; double y = (30.0L + 400.0L); - z = (y + double(5)); + z = (y + 5.0L); return (((x + y) + k) + 5.0L); } diff --git a/naga/tests/out/spv/f64.spvasm b/naga/tests/out/spv/f64.spvasm index cdf70f326d..1edf0d8cb6 100644 --- a/naga/tests/out/spv/f64.spvasm +++ b/naga/tests/out/spv/f64.spvasm @@ -1,13 +1,13 @@ ; SPIR-V ; Version: 1.0 ; Generator: rspirv -; Bound: 33 +; Bound: 30 OpCapability Shader OpCapability Float64 %1 = OpExtInstImport "GLSL.std.450" OpMemoryModel Logical GLSL450 -OpEntryPoint GLCompute %28 "main" -OpExecutionMode %28 LocalSize 1 1 1 +OpEntryPoint GLCompute %25 "main" +OpExecutionMode %25 LocalSize 1 1 1 %2 = OpTypeVoid %3 = OpTypeFloat 64 %4 = OpConstant %3 1.0 @@ -17,32 +17,29 @@ OpExecutionMode %28 LocalSize 1 1 1 %11 = OpTypeFunction %3 %3 %12 = OpConstant %3 30.0 %13 = OpConstant %3 400.0 -%14 = OpTypeInt 32 1 -%15 = OpConstant %14 5 -%16 = OpConstant %3 5.0 -%18 = OpTypePointer Function %3 -%19 = OpConstantNull %3 -%29 = OpTypeFunction %2 -%30 = OpConstant %3 6.0 +%14 = OpConstant %3 5.0 +%16 = OpTypePointer Function %3 +%17 = OpConstantNull %3 +%26 = OpTypeFunction %2 +%27 = OpConstant %3 6.0 %10 = OpFunction %3 None %11 %9 = OpFunctionParameter %3 %8 = OpLabel -%17 = OpVariable %18 Function %19 -OpBranch %20 -%20 = OpLabel -%21 = OpFAdd %3 %12 %13 -%22 = OpConvertSToF %3 %15 -%23 = OpFAdd %3 %21 %22 -OpStore %17 %23 -%24 = OpFAdd %3 %9 %21 -%25 = OpFAdd %3 %24 %5 -%26 = OpFAdd %3 %25 %16 -OpReturnValue %26 +%15 = OpVariable %16 Function %17 +OpBranch %18 +%18 = OpLabel +%19 = OpFAdd %3 %12 %13 +%20 = OpFAdd %3 %19 %14 +OpStore %15 %20 +%21 = OpFAdd %3 %9 %19 +%22 = OpFAdd %3 %21 %5 +%23 = OpFAdd %3 %22 %14 +OpReturnValue %23 OpFunctionEnd -%28 = OpFunction %2 None %29 -%27 = OpLabel -OpBranch %31 -%31 = OpLabel -%32 = OpFunctionCall %3 %10 %30 +%25 = OpFunction %2 None %26 +%24 = OpLabel +OpBranch %28 +%28 = OpLabel +%29 = OpFunctionCall %3 %10 %27 OpReturn OpFunctionEnd \ No newline at end of file diff --git a/naga/tests/out/wgsl/double-math-functions.frag.wgsl b/naga/tests/out/wgsl/double-math-functions.frag.wgsl index d21d99f4de..dfb171eeec 100644 --- a/naga/tests/out/wgsl/double-math-functions.frag.wgsl +++ b/naga/tests/out/wgsl/double-math-functions.frag.wgsl @@ -1,6 +1,6 @@ fn main_1() { - var a: vec4; - var b: vec4; + var a: vec4 = vec4(1.0lf); + var b: vec4 = vec4(2.0lf); var m: mat4x4; var i: i32 = 5; var ceilOut: vec4; @@ -29,8 +29,6 @@ fn main_1() { var smoothStepVector: vec4; var smoothStepMixed: vec4; - a = vec4(f64(1.0)); - b = vec4(f64(2.0)); let _e8 = a; let _e9 = b; let _e10 = a; @@ -95,8 +93,8 @@ fn main_1() { let _e152 = i; ldexpOut = ldexp(_e150.x, _e152); smoothStepScalar = f64(smoothstep(0.0, 1.0, 0.5)); - smoothStepVector = smoothstep(vec4(f64(0.0)), vec4(f64(1.0)), vec4(f64(0.5))); - smoothStepMixed = smoothstep(vec4(f64(0.0)), vec4(f64(1.0)), vec4(f64(0.5))); + smoothStepVector = smoothstep(vec4(0.0lf), vec4(1.0lf), vec4(0.5lf)); + smoothStepMixed = smoothstep(vec4(0.0lf), vec4(1.0lf), vec4(0.5lf)); return; } diff --git a/naga/tests/out/wgsl/f64.wgsl b/naga/tests/out/wgsl/f64.wgsl index 61f9c867e5..65699237fe 100644 --- a/naga/tests/out/wgsl/f64.wgsl +++ b/naga/tests/out/wgsl/f64.wgsl @@ -6,7 +6,7 @@ fn f(x: f64) -> f64 { var z: f64; let y = (30.0lf + 400.0lf); - z = (y + f64(5)); + z = (y + 5.0lf); return (((x + y) + k) + 5.0lf); }