Expose conversions between TextureFormat and naga::StorageFormat (#6185)

Co-authored-by: Connor Fitzgerald <connorwadefitzgerald@gmail.com>
This commit is contained in:
caelunshunandConnor Fitzgerald authored and GitHub committed 2025-01-15 17:44:14 +00:00
1 parent 0f37714fb8
commit 30364134f3
4 files changed
+48 -2

No files matched your search

+1
View File
@@ -529,6 +529,7 @@ By @MarijnS95 in [#6006](https://github.com/gfx-rs/wgpu/pull/6006).
- When mapping buffers for reading, mark buffers as initialized only when they have `MAP_WRITE` usage. By @teoxoy in [#6178](https://github.com/gfx-rs/wgpu/pull/6178).
- Add a separate pipeline constants error. By @teoxoy in [#6094](https://github.com/gfx-rs/wgpu/pull/6094).
- Ensure safety of indirect dispatch by injecting a compute shader that validates the content of the indirect buffer. By @teoxoy in [#5714](https://github.com/gfx-rs/wgpu/pull/5714).
- Add conversions between `TextureFormat` and ` StorageFormat`. By @caelunshun in [#6185](https://github.com/gfx-rs/wgpu/pull/6185)
#### GLES / OpenGL
+2
View File
@@ -90,6 +90,8 @@ mod weak_vec;
mod scratch;
pub mod validation;
pub use validation::{map_storage_format_from_naga, map_storage_format_to_naga};
pub use hal::{api, MAX_BIND_GROUPS, MAX_COLOR_ATTACHMENTS, MAX_VERTEX_BUFFERS};
pub use naga;
+2 -2
View File
@@ -278,7 +278,7 @@ pub enum StageError {
InvalidResource(#[from] InvalidResourceError),
}
fn map_storage_format_to_naga(format: wgt::TextureFormat) -> Option<naga::StorageFormat> {
pub fn map_storage_format_to_naga(format: wgt::TextureFormat) -> Option<naga::StorageFormat> {
use naga::StorageFormat as Sf;
use wgt::TextureFormat as Tf;
@@ -335,7 +335,7 @@ fn map_storage_format_to_naga(format: wgt::TextureFormat) -> Option<naga::Storag
})
}
fn map_storage_format_from_naga(format: naga::StorageFormat) -> wgt::TextureFormat {
pub fn map_storage_format_from_naga(format: naga::StorageFormat) -> wgt::TextureFormat {
use naga::StorageFormat as Sf;
use wgt::TextureFormat as Tf;
+43
View File
@@ -189,3 +189,46 @@ pub fn pipeline_cache_key(adapter_info: &wgt::AdapterInfo) -> Option<String> {
_ => None,
}
}
/// Adds extra conversion functions to `TextureFormat`.
pub trait TextureFormatExt {
/// Finds the [`TextureFormat`](wgt::TextureFormat) corresponding to the given
/// [`StorageFormat`](wgc::naga::StorageFormat).
///
/// # Examples
/// ```
/// use wgpu::util::TextureFormatExt;
/// assert_eq!(wgpu::TextureFormat::from_storage_format(wgpu::naga::StorageFormat::Bgra8Unorm), wgpu::TextureFormat::Bgra8Unorm);
/// ```
#[cfg_attr(docsrs, doc(cfg(any(wgpu_core, naga))))]
#[cfg(any(wgpu_core, naga))]
fn from_storage_format(storage_format: crate::naga::StorageFormat) -> Self;
/// Finds the [`StorageFormat`](wgc::naga::StorageFormat) corresponding to the given [`TextureFormat`](wgt::TextureFormat).
/// Returns `None` if there is no matching storage format,
/// which typically indicates this format is not supported
/// for storage textures.
///
/// # Examples
/// ```
/// use wgpu::util::TextureFormatExt;
/// assert_eq!(wgpu::TextureFormat::Bgra8Unorm.to_storage_format(), Some(wgpu::naga::StorageFormat::Bgra8Unorm));
/// ```
#[cfg_attr(docsrs, doc(cfg(any(wgpu_core, naga))))]
#[cfg(any(wgpu_core, naga))]
fn to_storage_format(&self) -> Option<crate::naga::StorageFormat>;
}
impl TextureFormatExt for wgt::TextureFormat {
#[cfg_attr(docsrs, doc(cfg(any(wgpu_core, naga))))]
#[cfg(any(wgpu_core, naga))]
fn from_storage_format(storage_format: crate::naga::StorageFormat) -> Self {
wgc::map_storage_format_from_naga(storage_format)
}
#[cfg_attr(docsrs, doc(cfg(any(wgpu_core, naga))))]
#[cfg(any(wgpu_core, naga))]
fn to_storage_format(&self) -> Option<crate::naga::StorageFormat> {
wgc::map_storage_format_to_naga(*self)
}
}