From 30364134f33dd0f9eb4552d75cdf8aa160dfc3f1 Mon Sep 17 00:00:00 2001 From: caelunshun Date: Wed, 15 Jan 2025 10:44:14 -0700 Subject: [PATCH] Expose conversions between TextureFormat and naga::StorageFormat (#6185) Co-authored-by: Connor Fitzgerald --- CHANGELOG.md | 1 + wgpu-core/src/lib.rs | 2 ++ wgpu-core/src/validation.rs | 4 ++-- wgpu/src/util/mod.rs | 43 +++++++++++++++++++++++++++++++++++++ 4 files changed, 48 insertions(+), 2 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 264c17d538..2b6d91e27b 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -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 diff --git a/wgpu-core/src/lib.rs b/wgpu-core/src/lib.rs index 5bab0ee2a2..4c2ea81490 100644 --- a/wgpu-core/src/lib.rs +++ b/wgpu-core/src/lib.rs @@ -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; diff --git a/wgpu-core/src/validation.rs b/wgpu-core/src/validation.rs index 8a1384ad49..9f0c8b78ea 100644 --- a/wgpu-core/src/validation.rs +++ b/wgpu-core/src/validation.rs @@ -278,7 +278,7 @@ pub enum StageError { InvalidResource(#[from] InvalidResourceError), } -fn map_storage_format_to_naga(format: wgt::TextureFormat) -> Option { +pub fn map_storage_format_to_naga(format: wgt::TextureFormat) -> Option { use naga::StorageFormat as Sf; use wgt::TextureFormat as Tf; @@ -335,7 +335,7 @@ fn map_storage_format_to_naga(format: wgt::TextureFormat) -> Option wgt::TextureFormat { +pub fn map_storage_format_from_naga(format: naga::StorageFormat) -> wgt::TextureFormat { use naga::StorageFormat as Sf; use wgt::TextureFormat as Tf; diff --git a/wgpu/src/util/mod.rs b/wgpu/src/util/mod.rs index 7158811d87..85809064d8 100644 --- a/wgpu/src/util/mod.rs +++ b/wgpu/src/util/mod.rs @@ -189,3 +189,46 @@ pub fn pipeline_cache_key(adapter_info: &wgt::AdapterInfo) -> Option { _ => 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; +} + +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 { + wgc::map_storage_format_to_naga(*self) + } +}