Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[naga] Exercise GLSL double-precision builtin functions, and fix the fallout. #4684

Merged
merged 5 commits into from
Nov 15, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -63,9 +63,9 @@ For naga changelogs at or before v0.14.0. See [naga's changelog](naga/CHANGELOG.

- Improve algorithm used by module compaction. By @jimblandy in [#4662](https://github.com/gfx-rs/wgpu/pull/4662).

##### MSL-OUT
- In Metal Shading Language output, fix issue where local variables were sometimes using variable names from previous functions.

- Fix issue where local variables were sometimes using variable names from previous functions.
- When reading GLSL, fix the argument types of the double-precision floating-point overloads of the `dot`, `reflect`, `distance`, and `ldexp` builtin functions. Correct the WGSL generated for constructing 64-bit floating-point matrices. Add tests for all the above. By @jimblandy in [#4684](https://github.com/gfx-rs/wgpu/pull/4684).

## v0.18.0 (2023-10-25)

Expand Down
6 changes: 3 additions & 3 deletions naga/src/back/wgsl/writer.rs
Original file line number Diff line number Diff line change
Expand Up @@ -524,14 +524,14 @@ impl<W: Write> Writer<W> {
TypeInner::Matrix {
columns,
rows,
width: _,
width,
} => {
write!(
self.out,
//TODO: Can matrix be other than f32?
"mat{}x{}<f32>",
"mat{}x{}<{}>",
back::vector_size_str(columns),
back::vector_size_str(rows),
scalar_kind_str(crate::Scalar::float(width))
jimblandy marked this conversation as resolved.
Show resolved Hide resolved
)?;
}
TypeInner::Pointer { base, space } => {
Expand Down
12 changes: 4 additions & 8 deletions naga/src/front/glsl/builtins.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1246,18 +1246,14 @@ fn inject_common_builtin(
_ => unreachable!(),
};

let second_scalar = if fun == MacroCall::MathFunction(MathFunction::Ldexp) {
Scalar {
kind: Sk::Sint,
width: float_width,
}
} else {
float_scalar
let second_scalar = match fun {
MacroCall::MathFunction(MathFunction::Ldexp) => Scalar::I32,
_ => float_scalar,
};

declaration
.overloads
.push(module.add_builtin(vec![ty(Scalar::F32), ty(second_scalar)], fun))
.push(module.add_builtin(vec![ty(float_scalar), ty(second_scalar)], fun))
}
}
"transpose" => {
Expand Down
34 changes: 34 additions & 0 deletions naga/tests/in/glsl/double-math-functions.frag
jimblandy marked this conversation as resolved.
Show resolved Hide resolved
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
#version 450

void main() {
dvec4 a = dvec4(1.0);
dvec4 b = dvec4(2.0);
dmat4 m = dmat4(a, b, a, b);
int i = 5;

dvec4 ceilOut = ceil(a);
dvec4 roundOut = round(a);
dvec4 floorOut = floor(a);
dvec4 fractOut = fract(a);
dvec4 truncOut = trunc(a);
dvec4 absOut = abs(a);
dvec4 sqrtOut = sqrt(a);
dvec4 inversesqrtOut = inversesqrt(a);
dvec4 signOut = sign(a);
dmat4 transposeOut = transpose(m);
dvec4 normalizeOut = normalize(a);
double lengthOut = length(a);
double determinantOut = determinant(m);
double modOut = mod(a.x, b.x);
double dotOut = dot(a, b);
dvec4 maxOut = max(a, b);
dvec4 minOut = min(a, b);
dvec4 reflectOut = reflect(a, b);
dvec3 crossOut = cross(a.xyz, b.xyz);
double distanceOut = distance(a, b);
dvec4 stepOut = step(a, b);
double ldexpOut = ldexp(a.x, i);
double smoothStepScalar = smoothstep(0.0, 1.0, 0.5);
dvec4 smoothStepVector = smoothstep(dvec4(0.0), dvec4(1.0), dvec4(0.5));
dvec4 smoothStepMixed = smoothstep(0.0, 1.0, dvec4(0.5));
}
107 changes: 107 additions & 0 deletions naga/tests/out/wgsl/double-math-functions.frag.wgsl
Original file line number Diff line number Diff line change
@@ -0,0 +1,107 @@
fn main_1() {
var a: vec4<f64>;
var b: vec4<f64>;
var m: mat4x4<f64>;
var i: i32 = 5;
var ceilOut: vec4<f64>;
var roundOut: vec4<f64>;
var floorOut: vec4<f64>;
var fractOut: vec4<f64>;
var truncOut: vec4<f64>;
var absOut: vec4<f64>;
var sqrtOut: vec4<f64>;
var inversesqrtOut: vec4<f64>;
var signOut: vec4<f64>;
var transposeOut: mat4x4<f64>;
var normalizeOut: vec4<f64>;
var lengthOut: f64;
var determinantOut: f64;
var modOut: f64;
var dotOut: f64;
var maxOut: vec4<f64>;
var minOut: vec4<f64>;
var reflectOut: vec4<f64>;
var crossOut: vec3<f64>;
var distanceOut: f64;
var stepOut: vec4<f64>;
var ldexpOut: f64;
var smoothStepScalar: f64;
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;
let _e11 = b;
m = mat4x4<f64>(vec4<f64>(_e8.x, _e8.y, _e8.z, _e8.w), vec4<f64>(_e9.x, _e9.y, _e9.z, _e9.w), vec4<f64>(_e10.x, _e10.y, _e10.z, _e10.w), vec4<f64>(_e11.x, _e11.y, _e11.z, _e11.w));
let _e37 = a;
ceilOut = ceil(_e37);
let _e41 = a;
roundOut = round(_e41);
let _e45 = a;
floorOut = floor(_e45);
let _e49 = a;
fractOut = fract(_e49);
let _e53 = a;
truncOut = trunc(_e53);
let _e57 = a;
absOut = abs(_e57);
let _e61 = a;
sqrtOut = sqrt(_e61);
let _e65 = a;
inversesqrtOut = inverseSqrt(_e65);
let _e69 = a;
signOut = sign(_e69);
let _e73 = m;
transposeOut = transpose(_e73);
let _e77 = a;
normalizeOut = normalize(_e77);
let _e81 = a;
lengthOut = length(_e81);
let _e85 = m;
determinantOut = determinant(_e85);
let _e88 = a;
let _e90 = b;
let _e92 = a;
let _e94 = b;
modOut = (_e92.x - (floor((_e92.x / _e94.x)) * _e94.x));
let _e103 = a;
let _e104 = b;
dotOut = dot(_e103, _e104);
let _e109 = a;
let _e110 = b;
maxOut = max(_e109, _e110);
let _e115 = a;
let _e116 = b;
minOut = min(_e115, _e116);
let _e121 = a;
let _e122 = b;
reflectOut = reflect(_e121, _e122);
let _e125 = a;
let _e127 = b;
let _e129 = a;
let _e131 = b;
crossOut = cross(_e129.xyz, _e131.xyz);
let _e137 = a;
let _e138 = b;
distanceOut = distance(_e137, _e138);
let _e143 = a;
let _e144 = b;
stepOut = step(_e143, _e144);
let _e147 = a;
let _e150 = a;
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)));
return;
}

@fragment
fn main() {
main_1();
return;
}
Loading