refactor texture format to scalar conversion (#6451)

Co-authored-by: Erich Gubler <erichdongubler@gmail.com>
This commit is contained in:
atlv 2024-10-23 16:06:40 -04:00 committed by GitHub
parent e23146aa3e
commit 64a61ee5c6
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
9 changed files with 36 additions and 31 deletions

View File

@ -166,6 +166,7 @@ By @bradwerth [#6216](https://github.com/gfx-rs/wgpu/pull/6216).
- Invalidate the device when we encounter driver-induced device loss or on unexpected errors. By @teoxoy in [#6229](https://github.com/gfx-rs/wgpu/pull/6229). - Invalidate the device when we encounter driver-induced device loss or on unexpected errors. By @teoxoy in [#6229](https://github.com/gfx-rs/wgpu/pull/6229).
- Make Vulkan error handling more robust. By @teoxoy in [#6119](https://github.com/gfx-rs/wgpu/pull/6119). - Make Vulkan error handling more robust. By @teoxoy in [#6119](https://github.com/gfx-rs/wgpu/pull/6119).
- Add bounds checking to Buffer slice method. By @beholdnec in [#6432](https://github.com/gfx-rs/wgpu/pull/6432). - Add bounds checking to Buffer slice method. By @beholdnec in [#6432](https://github.com/gfx-rs/wgpu/pull/6432).
- Replace `impl From<StorageFormat> for ScalarKind` with `impl From<StorageFormat> for Scalar` so that byte width is included. By @atlv24 in [#6451](https://github.com/gfx-rs/wgpu/pull/6451).
#### Internal #### Internal

View File

@ -1095,12 +1095,16 @@ impl<'a, W: Write> Writer<'a, W> {
// - Array - used if it's an image array // - Array - used if it's an image array
// - Shadow - used if it's a depth image // - Shadow - used if it's a depth image
use crate::ImageClass as Ic; use crate::ImageClass as Ic;
use crate::Scalar as S;
let (base, kind, ms, comparison) = match class { let float = S {
Ic::Sampled { kind, multi: true } => ("sampler", kind, "MS", ""), kind: crate::ScalarKind::Float,
Ic::Sampled { kind, multi: false } => ("sampler", kind, "", ""), width: 4,
Ic::Depth { multi: true } => ("sampler", crate::ScalarKind::Float, "MS", ""), };
Ic::Depth { multi: false } => ("sampler", crate::ScalarKind::Float, "", "Shadow"), let (base, scalar, ms, comparison) = match class {
Ic::Sampled { kind, multi: true } => ("sampler", S { kind, width: 4 }, "MS", ""),
Ic::Sampled { kind, multi: false } => ("sampler", S { kind, width: 4 }, "", ""),
Ic::Depth { multi: true } => ("sampler", float, "MS", ""),
Ic::Depth { multi: false } => ("sampler", float, "", "Shadow"),
Ic::Storage { format, .. } => ("image", format.into(), "", ""), Ic::Storage { format, .. } => ("image", format.into(), "", ""),
}; };
@ -1114,7 +1118,7 @@ impl<'a, W: Write> Writer<'a, W> {
self.out, self.out,
"{}{}{}{}{}{}{}", "{}{}{}{}{}{}{}",
precision, precision,
glsl_scalar(crate::Scalar { kind, width: 4 })?.prefix, glsl_scalar(scalar)?.prefix,
base, base,
glsl_dimension(dim), glsl_dimension(dim),
ms, ms,

View File

@ -215,14 +215,15 @@ impl<'a> Display for TypeContext<'a> {
crate::ImageDimension::D3 => "3d", crate::ImageDimension::D3 => "3d",
crate::ImageDimension::Cube => "cube", crate::ImageDimension::Cube => "cube",
}; };
let (texture_str, msaa_str, kind, access) = match class { let (texture_str, msaa_str, scalar, access) = match class {
crate::ImageClass::Sampled { kind, multi } => { crate::ImageClass::Sampled { kind, multi } => {
let (msaa_str, access) = if multi { let (msaa_str, access) = if multi {
("_ms", "read") ("_ms", "read")
} else { } else {
("", "sample") ("", "sample")
}; };
("texture", msaa_str, kind, access) let scalar = crate::Scalar { kind, width: 4 };
("texture", msaa_str, scalar, access)
} }
crate::ImageClass::Depth { multi } => { crate::ImageClass::Depth { multi } => {
let (msaa_str, access) = if multi { let (msaa_str, access) = if multi {
@ -230,7 +231,11 @@ impl<'a> Display for TypeContext<'a> {
} else { } else {
("", "sample") ("", "sample")
}; };
("depth", msaa_str, crate::ScalarKind::Float, access) let scalar = crate::Scalar {
kind: crate::ScalarKind::Float,
width: 4,
};
("depth", msaa_str, scalar, access)
} }
crate::ImageClass::Storage { format, .. } => { crate::ImageClass::Storage { format, .. } => {
let access = if self let access = if self
@ -254,7 +259,7 @@ impl<'a> Display for TypeContext<'a> {
("texture", "", format.into(), access) ("texture", "", format.into(), access)
} }
}; };
let base_name = crate::Scalar { kind, width: 4 }.to_msl_name(); let base_name = scalar.to_msl_name();
let array_str = if arrayed { "_array" } else { "" }; let array_str = if arrayed { "_array" } else { "" };
write!( write!(
out, out,

View File

@ -213,7 +213,7 @@ impl Function {
/// where practical. /// where practical.
#[derive(Debug, PartialEq, Hash, Eq, Copy, Clone)] #[derive(Debug, PartialEq, Hash, Eq, Copy, Clone)]
struct LocalImageType { struct LocalImageType {
sampled_type: crate::ScalarKind, sampled_type: crate::Scalar,
dim: spirv::Dim, dim: spirv::Dim,
flags: ImageTypeFlags, flags: ImageTypeFlags,
image_format: spirv::ImageFormat, image_format: spirv::ImageFormat,
@ -244,19 +244,22 @@ impl LocalImageType {
match class { match class {
crate::ImageClass::Sampled { kind, multi } => LocalImageType { crate::ImageClass::Sampled { kind, multi } => LocalImageType {
sampled_type: kind, sampled_type: crate::Scalar { kind, width: 4 },
dim, dim,
flags: make_flags(multi, ImageTypeFlags::SAMPLED), flags: make_flags(multi, ImageTypeFlags::SAMPLED),
image_format: spirv::ImageFormat::Unknown, image_format: spirv::ImageFormat::Unknown,
}, },
crate::ImageClass::Depth { multi } => LocalImageType { crate::ImageClass::Depth { multi } => LocalImageType {
sampled_type: crate::ScalarKind::Float, sampled_type: crate::Scalar {
kind: crate::ScalarKind::Float,
width: 4,
},
dim, dim,
flags: make_flags(multi, ImageTypeFlags::DEPTH | ImageTypeFlags::SAMPLED), flags: make_flags(multi, ImageTypeFlags::DEPTH | ImageTypeFlags::SAMPLED),
image_format: spirv::ImageFormat::Unknown, image_format: spirv::ImageFormat::Unknown,
}, },
crate::ImageClass::Storage { format, access: _ } => LocalImageType { crate::ImageClass::Storage { format, access: _ } => LocalImageType {
sampled_type: crate::ScalarKind::from(format), sampled_type: format.into(),
dim, dim,
flags: make_flags(false, ImageTypeFlags::empty()), flags: make_flags(false, ImageTypeFlags::empty()),
image_format: format.into(), image_format: format.into(),

View File

@ -902,10 +902,7 @@ impl Writer {
Instruction::type_pointer(id, class, type_id) Instruction::type_pointer(id, class, type_id)
} }
LocalType::Image(image) => { LocalType::Image(image) => {
let local_type = LocalType::Numeric(NumericType::Scalar(crate::Scalar { let local_type = LocalType::Numeric(NumericType::Scalar(image.sampled_type));
kind: image.sampled_type,
width: 4,
}));
let type_id = self.get_type_id(LookupType::Local(local_type)); let type_id = self.get_type_id(LookupType::Local(local_type));
Instruction::type_image(id, type_id, image.dim, image.flags, image.image_format) Instruction::type_image(id, type_id, image.dim, image.flags, image.image_format)
} }

View File

@ -622,7 +622,7 @@ impl Frontend {
// check that the format scalar kind matches // check that the format scalar kind matches
let good_format = overload_format == call_format let good_format = overload_format == call_format
|| (overload.internal || (overload.internal
&& ScalarKind::from(overload_format) == ScalarKind::from(call_format)); && Scalar::from(overload_format) == Scalar::from(call_format));
if !(good_size && good_format) { if !(good_size && good_format) {
continue 'outer; continue 'outer;
} }

View File

@ -20,10 +20,10 @@ pub use namer::{EntryPointIndex, NameKey, Namer};
pub use terminator::ensure_block_returns; pub use terminator::ensure_block_returns;
pub use typifier::{ResolveContext, ResolveError, TypeResolution}; pub use typifier::{ResolveContext, ResolveError, TypeResolution};
impl From<super::StorageFormat> for super::ScalarKind { impl From<super::StorageFormat> for super::Scalar {
fn from(format: super::StorageFormat) -> Self { fn from(format: super::StorageFormat) -> Self {
use super::{ScalarKind as Sk, StorageFormat as Sf}; use super::{ScalarKind as Sk, StorageFormat as Sf};
match format { let kind = match format {
Sf::R8Unorm => Sk::Float, Sf::R8Unorm => Sk::Float,
Sf::R8Snorm => Sk::Float, Sf::R8Snorm => Sk::Float,
Sf::R8Uint => Sk::Uint, Sf::R8Uint => Sk::Uint,
@ -64,7 +64,8 @@ impl From<super::StorageFormat> for super::ScalarKind {
Sf::Rg16Snorm => Sk::Float, Sf::Rg16Snorm => Sk::Float,
Sf::Rgba16Unorm => Sk::Float, Sf::Rgba16Unorm => Sk::Float,
Sf::Rgba16Snorm => Sk::Float, Sf::Rgba16Snorm => Sk::Float,
} };
super::Scalar { kind, width: 4 }
} }
} }

View File

@ -494,10 +494,7 @@ impl<'a> ResolveContext<'a> {
size: crate::VectorSize::Quad, size: crate::VectorSize::Quad,
}, },
crate::ImageClass::Storage { format, .. } => Ti::Vector { crate::ImageClass::Storage { format, .. } => Ti::Vector {
scalar: crate::Scalar { scalar: format.into(),
kind: format.into(),
width: 4,
},
size: crate::VectorSize::Quad, size: crate::VectorSize::Quad,
}, },
}), }),

View File

@ -1089,10 +1089,7 @@ impl super::Validator {
crate::ImageClass::Storage { format, .. } => { crate::ImageClass::Storage { format, .. } => {
crate::TypeInner::Vector { crate::TypeInner::Vector {
size: crate::VectorSize::Quad, size: crate::VectorSize::Quad,
scalar: crate::Scalar { scalar: format.into(),
kind: format.into(),
width: 4,
},
} }
} }
_ => { _ => {