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

feat: implement F16 support in shaders #5701

Open
wants to merge 27 commits into
base: trunk
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
27 commits
Select commit Hold shift + click to select a range
01bc1bd
Support F16
cwfitzgerald Feb 1, 2025
722bc20
Remove Example
cwfitzgerald Feb 5, 2025
2d9cac9
Add More Tests
cwfitzgerald Feb 5, 2025
136d365
Cargo?
cwfitzgerald Feb 5, 2025
d83d28c
Fix `wgpu-info`'s use of DXC
cwfitzgerald Feb 5, 2025
94f88d4
Write F16 Alignment Test
cwfitzgerald Feb 5, 2025
8a12011
Clippy
cwfitzgerald Feb 5, 2025
dcc6161
Format
cwfitzgerald Feb 5, 2025
59a5795
Validate out f16 in Push Constants
cwfitzgerald Feb 5, 2025
60def99
Fix example_wgsl Test
cwfitzgerald Feb 5, 2025
c13c5ab
Fix Spirv Generation
cwfitzgerald Feb 5, 2025
429d771
Fix Enable in wgsl-out Ouput
cwfitzgerald Feb 5, 2025
a41cfe1
Add glsl-in and spirv-in Support
cwfitzgerald Feb 6, 2025
efb441d
Merge remote-tracking branch 'origin/trunk' into feature/enable
cwfitzgerald Feb 6, 2025
63d0434
Add Matrices to spirv/glsl tests
cwfitzgerald Feb 6, 2025
5a5992e
Fix spirv-out `As` Expressions
cwfitzgerald Feb 6, 2025
fe41e30
Basic f16 Alignment
cwfitzgerald Feb 6, 2025
8fee958
Remove Borked Test
cwfitzgerald Feb 6, 2025
49febfc
Test Matrix and Vector Shorthand
cwfitzgerald Feb 7, 2025
67d1f99
Remove f16 IR Output
cwfitzgerald Feb 7, 2025
dd1e7c3
Matrix Tests
cwfitzgerald Feb 7, 2025
b170cc8
Padding
cwfitzgerald Feb 7, 2025
0bd1536
Fix matrix type generation
cwfitzgerald Feb 7, 2025
ad55db7
Improve Validation Errors with Naga Validator
cwfitzgerald Feb 7, 2025
a15bf38
Merge branch 'trunk' into feature/enable
cwfitzgerald Feb 7, 2025
556a3c4
Fix Lack of Alignment on f16 inputs
cwfitzgerald Feb 7, 2025
7a0660c
Layout Test
cwfitzgerald Feb 8, 2025
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
26 changes: 26 additions & 0 deletions Cargo.lock

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

2 changes: 2 additions & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -94,6 +94,7 @@ flume = "0.11"
futures-lite = "2"
getrandom = "0.2"
glam = "0.29"
half = { package = "half-2", version = "2.4.1" }
hashbrown = { version = "0.14.5", default-features = false, features = [
"ahash",
"inline-more",
Expand Down Expand Up @@ -140,6 +141,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
4 changes: 4 additions & 0 deletions examples/features/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,10 @@ encase = { workspace = true, features = ["glam"] }
flume.workspace = true
getrandom.workspace = true
glam = { workspace = true, features = ["bytemuck"] }
half = { package = "half-2", version = "2.4.1", features = [
"arbitrary",
"bytemuck",
] }
ktx2.workspace = true
log.workspace = true
nanorand.workspace = true
Expand Down
14 changes: 13 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 @@ -98,6 +105,10 @@ petgraph = { version = "0.7", optional = true }
pp-rs = { version = "0.2.1", optional = true }
hexf-parse = { version = "0.2.1", optional = true }
unicode-xid = { version = "0.2.6", optional = true }
# TODO: Use `half` directly again after <https://github.com/starkat99/half-rs/pull/111> is released
# upstream.
half = { workspace = true, features = ["arbitrary", "num-traits"] }
num-traits = "0.2"

[build-dependencies]
cfg_aliases.workspace = true
Expand All @@ -118,3 +129,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 @@ -2616,6 +2616,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")?,
crate::Literal::I32(value) => write!(self.out, "{value}")?,
crate::Literal::U64(value) => write!(self.out, "{value}uL")?,
Expand Down
31 changes: 27 additions & 4 deletions naga/src/back/msl/writer.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,8 @@ use crate::{
proc::{self, NameKey, TypeResolution},
valid, FastHashMap, FastHashSet,
};
use half::f16;
use num_traits::real::Real;
#[cfg(test)]
use std::ptr;
use std::{
Expand Down Expand Up @@ -170,9 +172,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 @@ -391,8 +395,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 @@ -1423,6 +1431,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
Loading