Skip to content

Commit

Permalink
[naga] Support casting to f64 in the constant evaluator.
Browse files Browse the repository at this point in the history
  • Loading branch information
jimblandy committed Nov 22, 2023
1 parent ea23916 commit e8e57d4
Show file tree
Hide file tree
Showing 6 changed files with 38 additions and 35 deletions.
8 changes: 8 additions & 0 deletions naga/src/proc/constant_evaluator.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down
2 changes: 1 addition & 1 deletion naga/tests/out/glsl/f64.main.Compute.glsl
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}

Expand Down
2 changes: 1 addition & 1 deletion naga/tests/out/hlsl/f64.hlsl
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}

Expand Down
49 changes: 23 additions & 26 deletions naga/tests/out/spv/f64.spvasm
Original file line number Diff line number Diff line change
@@ -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
Expand All @@ -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
10 changes: 4 additions & 6 deletions naga/tests/out/wgsl/double-math-functions.frag.wgsl
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
fn main_1() {
var a: vec4<f64>;
var b: vec4<f64>;
var a: vec4<f64> = vec4(1.0lf);
var b: vec4<f64> = vec4(2.0lf);
var m: mat4x4<f64>;
var i: i32 = 5;
var ceilOut: vec4<f64>;
Expand Down Expand Up @@ -29,8 +29,6 @@ fn main_1() {
var smoothStepVector: vec4<f64>;
var smoothStepMixed: vec4<f64>;

a = vec4(f64(1.0));
b = vec4(f64(2.0));
let _e8 = a;
let _e9 = b;
let _e10 = a;
Expand Down Expand Up @@ -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;
}

Expand Down
2 changes: 1 addition & 1 deletion naga/tests/out/wgsl/f64.wgsl
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}

Expand Down

0 comments on commit e8e57d4

Please sign in to comment.