Skip to content

Commit

Permalink
Implement F16 Support
Browse files Browse the repository at this point in the history
Co-authored-by: FL33TW00D <[email protected]>
Co-authored-by: ErichDonGubler <[email protected]>
  • Loading branch information
3 people committed Feb 21, 2025
1 parent 5dad2c4 commit 2343369
Show file tree
Hide file tree
Showing 64 changed files with 3,828 additions and 989 deletions.
25 changes: 25 additions & 0 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

3 changes: 3 additions & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -96,6 +96,8 @@ flume = "0.11"
futures-lite = "2"
getrandom = "0.2"
glam = "0.29"
# TODO: Use `half` directly again after <https://github.com/starkat99/half-rs/pull/111> is released upstream.
half = { package = "half-2", version = "2.4.1" }
hashbrown = { version = "0.14.5", default-features = false, features = [
"ahash",
"inline-more",
Expand Down Expand Up @@ -142,6 +144,7 @@ strum = { version = "0.26.3", default-features = false, features = ["derive"] }
trybuild = "1"
tracy-client = "0.17"
thiserror = { version = "2", default-features = false }
walkdir = "2"
winit = { version = "0.29", features = ["android-native-activity"] }

# Metal dependencies
Expand Down
12 changes: 11 additions & 1 deletion naga/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -41,10 +41,17 @@ msl-out = []
## If you want to enable MSL output it regardless of the target platform, use `naga/msl-out`.
msl-out-if-target-apple = []

serialize = ["dep:serde", "bitflags/serde", "hashbrown/serde", "indexmap/serde"]
serialize = [
"dep:serde",
"bitflags/serde",
"half/serde",
"hashbrown/serde",
"indexmap/serde",
]
deserialize = [
"dep:serde",
"bitflags/serde",
"half/serde",
"hashbrown/serde",
"indexmap/serde",
]
Expand Down Expand Up @@ -84,9 +91,11 @@ termcolor = { version = "1.4.1" }
# https://github.com/brendanzab/codespan/commit/e99c867339a877731437e7ee6a903a3d03b5439e
codespan-reporting = { version = "0.11.0" }
hashbrown.workspace = true
half = { workspace = true, features = ["arbitrary", "num-traits"] }
rustc-hash.workspace = true
indexmap.workspace = true
log = "0.4"
num-traits = "0.2"
strum = { workspace = true, optional = true }
spirv = { version = "0.3", optional = true }
thiserror.workspace = true
Expand Down Expand Up @@ -118,3 +127,4 @@ ron = "0.8.0"
rspirv = { version = "0.11", git = "https://github.com/gfx-rs/rspirv", rev = "b969f175d5663258b4891e44b76c1544da9661ab" }
serde = { workspace = true, features = ["default", "derive"] }
spirv = { version = "0.3", features = ["deserialize"] }
walkdir.workspace = true
3 changes: 3 additions & 0 deletions naga/src/back/glsl/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2669,6 +2669,9 @@ impl<'a, W: Write> Writer<'a, W> {
// decimal part even it's zero which is needed for a valid glsl float constant
crate::Literal::F64(value) => write!(self.out, "{value:?}LF")?,
crate::Literal::F32(value) => write!(self.out, "{value:?}")?,
crate::Literal::F16(_) => {
return Err(Error::Custom("GLSL has no 16-bit float type".into()));
}
// Unsigned integers need a `u` at the end
//
// While `core` doesn't necessarily need it, it's allowed and since `es` needs it we
Expand Down
1 change: 1 addition & 0 deletions naga/src/back/hlsl/writer.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2634,6 +2634,7 @@ impl<'a, W: fmt::Write> super::Writer<'a, W> {
// decimal part even it's zero
crate::Literal::F64(value) => write!(self.out, "{value:?}L")?,
crate::Literal::F32(value) => write!(self.out, "{value:?}")?,
crate::Literal::F16(value) => write!(self.out, "{value:?}h")?,
crate::Literal::U32(value) => write!(self.out, "{value}u")?,
// HLSL has no suffix for explicit i32 literals, but not using any suffix
// makes the type ambiguous which prevents overload resolution from
Expand Down
33 changes: 28 additions & 5 deletions naga/src/back/msl/writer.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@ use crate::{
proc::{self, index, ExpressionKindTracker, NameKey, TypeResolution},
valid, FastHashMap, FastHashSet,
};
use half::f16;
use num_traits::real::Real;
#[cfg(test)]
use std::ptr;
use std::{
Expand Down Expand Up @@ -174,9 +176,11 @@ impl Display for TypeContext<'_> {
write!(out, "{}::atomic_{}", NAMESPACE, scalar.to_msl_name())
}
crate::TypeInner::Vector { size, scalar } => put_numeric_type(out, scalar, &[size]),
crate::TypeInner::Matrix { columns, rows, .. } => {
put_numeric_type(out, crate::Scalar::F32, &[rows, columns])
}
crate::TypeInner::Matrix {
columns,
rows,
scalar,
} => put_numeric_type(out, scalar, &[rows, columns]),
crate::TypeInner::Pointer { base, space } => {
let sub = Self {
handle: base,
Expand Down Expand Up @@ -413,8 +417,12 @@ impl crate::Scalar {
match self {
Self {
kind: Sk::Float,
width: _,
width: 4,
} => "float",
Self {
kind: Sk::Float,
width: 2,
} => "half",
Self {
kind: Sk::Sint,
width: 4,
Expand Down Expand Up @@ -471,7 +479,7 @@ fn should_pack_struct_member(
match *ty_inner {
crate::TypeInner::Vector {
size: crate::VectorSize::Tri,
scalar: scalar @ crate::Scalar { width: 4, .. },
scalar: scalar @ crate::Scalar { width: 4 | 2, .. },
} if is_tight => Some(scalar),
_ => None,
}
Expand Down Expand Up @@ -1446,6 +1454,21 @@ impl<W: Write> Writer<W> {
crate::Literal::F64(_) => {
return Err(Error::CapabilityNotSupported(valid::Capabilities::FLOAT64))
}
crate::Literal::F16(value) => {
if value.is_infinite() {
let sign = if value.is_sign_negative() { "-" } else { "" };
write!(self.out, "{sign}INFINITY")?;
} else if value.is_nan() {
write!(self.out, "NAN")?;
} else {
let suffix = if value.fract() == f16::from_f32(0.0) {
".0h"
} else {
"h"
};
write!(self.out, "{value}{suffix}")?;
}
}
crate::Literal::F32(value) => {
if value.is_infinite() {
let sign = if value.is_sign_negative() { "-" } else { "" };
Expand Down
Loading

0 comments on commit 2343369

Please sign in to comment.