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

feature: SHADER-F16 #5700

Closed
wants to merge 1 commit into from
Closed
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
2 changes: 2 additions & 0 deletions Cargo.lock

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

6 changes: 4 additions & 2 deletions naga/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -25,8 +25,9 @@ dot-out = []
glsl-in = ["dep:pp-rs"]
glsl-out = []
msl-out = []
serialize = ["dep:serde", "bitflags/serde", "indexmap/serde"]
deserialize = ["dep:serde", "bitflags/serde", "indexmap/serde"]
f16 = ["half"]
serialize = ["dep:serde", "bitflags/serde", "indexmap/serde", "half/serde"]
deserialize = ["dep:serde", "bitflags/serde", "indexmap/serde", "half/serde"]
arbitrary = ["dep:arbitrary", "bitflags/arbitrary", "indexmap/arbitrary"]
spv-in = ["dep:petgraph", "dep:spirv"]
spv-out = ["dep:spirv"]
Expand Down Expand Up @@ -59,6 +60,7 @@ pp-rs = { version = "0.2.1", optional = true }
hexf-parse = { version = "0.2.1", optional = true }
unicode-xid = { version = "0.2.3", optional = true }
arrayvec.workspace = true
half = {version = "2.4.1", optional = true}

[target.'cfg(not(target_arch = "wasm32"))'.dev-dependencies]
criterion = { version = "0.5", features = [] }
Expand Down
4 changes: 4 additions & 0 deletions naga/src/back/glsl/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2571,6 +2571,10 @@ 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, "{:?}LF", value)?,
crate::Literal::F32(value) => write!(self.out, "{:?}", value)?,
#[cfg(feature = "half")]
crate::Literal::F16(value) => {
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
2 changes: 2 additions & 0 deletions naga/src/back/hlsl/writer.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2236,6 +2236,8 @@ 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:?}")?,
#[cfg(feature = "half")]
crate::Literal::F16(value) => write!(self.out, "{value:?}h")?,
crate::Literal::U32(value) => write!(self.out, "{}u", value)?,
crate::Literal::I32(value) => write!(self.out, "{}", value)?,
crate::Literal::U64(value) => write!(self.out, "{}uL", value)?,
Expand Down
10 changes: 9 additions & 1 deletion naga/src/back/msl/writer.rs
Original file line number Diff line number Diff line change
Expand Up @@ -324,8 +324,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 @@ -1266,6 +1270,10 @@ impl<W: Write> Writer<W> {
crate::Literal::F64(_) => {
return Err(Error::CapabilityNotSupported(valid::Capabilities::FLOAT64))
}
#[cfg(feature = "half")]
crate::Literal::F16(_) => {
return Err(Error::CapabilityNotSupported(valid::Capabilities::FLOAT16))
}
crate::Literal::F32(value) => {
if value.is_infinite() {
let sign = if value.is_sign_negative() { "-" } else { "" };
Expand Down
6 changes: 6 additions & 0 deletions naga/src/back/wgsl/writer.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1211,6 +1211,8 @@ impl<W: Write> Writer<W> {

match expressions[expr] {
Expression::Literal(literal) => match literal {
#[cfg(feature = "half")]
crate::Literal::F16(value) => write!(self.out, "{}h", value)?,
crate::Literal::F32(value) => write!(self.out, "{}f", value)?,
crate::Literal::U32(value) => write!(self.out, "{}u", value)?,
crate::Literal::I32(value) => {
Expand Down Expand Up @@ -1957,6 +1959,10 @@ const fn scalar_kind_str(scalar: crate::Scalar) -> &'static str {
kind: Sk::Float,
width: 4,
} => "f32",
Scalar {
kind: Sk::Float,
width: 2,
} => "f16",
Scalar {
kind: Sk::Sint,
width: 4,
Expand Down
3 changes: 3 additions & 0 deletions naga/src/front/spv/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,9 @@ use crate::{
FastHashMap, FastHashSet, FastIndexMap,
};

#[cfg(feature = "half")]
use half::f16;

use petgraph::graphmap::GraphMap;
use std::{convert::TryInto, mem, num::NonZeroU32, path::PathBuf};

Expand Down
1 change: 1 addition & 0 deletions naga/src/front/wgsl/lower/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1656,6 +1656,7 @@ impl<'source, 'temp> Lowerer<'source, 'temp> {
let expr: Typed<crate::Expression> = match *expr {
ast::Expression::Literal(literal) => {
let literal = match literal {
ast::Literal::Number(Number::F16(f)) => crate::Literal::F16(f),
ast::Literal::Number(Number::F32(f)) => crate::Literal::F32(f),
ast::Literal::Number(Number::I32(i)) => crate::Literal::I32(i),
ast::Literal::Number(Number::U32(u)) => crate::Literal::U32(u),
Expand Down
5 changes: 5 additions & 0 deletions naga/src/front/wgsl/parse/number.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,9 @@
use crate::front::wgsl::error::NumberError;
use crate::front::wgsl::parse::lexer::Token;

#[cfg(feature = "half")]
use half::f16;

/// When using this type assume no Abstract Int/Float for now
#[derive(Copy, Clone, Debug, PartialEq)]
pub enum Number {
Expand All @@ -17,6 +20,8 @@ pub enum Number {
/// Concrete u64
U64(u64),
/// Concrete f32
F16(f16),
/// Concrete f32
F32(f32),
/// Concrete f64
F64(f64),
Expand Down
4 changes: 4 additions & 0 deletions naga/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -286,6 +286,8 @@ pub use crate::arena::{Arena, Handle, Range, UniqueArena};
pub use crate::span::{SourceLocation, Span, SpanContext, WithSpan};
#[cfg(feature = "arbitrary")]
use arbitrary::Arbitrary;
#[cfg(feature = "half")]
use half::f16;
#[cfg(feature = "deserialize")]
use serde::Deserialize;
#[cfg(feature = "serialize")]
Expand Down Expand Up @@ -881,6 +883,8 @@ pub enum Literal {
F64(f64),
/// May not be NaN or infinity.
F32(f32),
#[cfg(feature = "half")]
F16(f16),
U32(u32),
I32(i32),
U64(u64),
Expand Down
2 changes: 2 additions & 0 deletions naga/src/valid/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -114,6 +114,8 @@ bitflags::bitflags! {
const SUBGROUP = 0x10000;
/// Support for subgroup barriers.
const SUBGROUP_BARRIER = 0x20000;
/// Support for 16-bit floating-point types.
const FLOAT16 = 0x40000;
}
}

Expand Down
Loading