Skip to content

Commit

Permalink
Expose conversions between TextureFormat and naga::StorageFormat (#6185)
Browse files Browse the repository at this point in the history
Co-authored-by: Connor Fitzgerald <[email protected]>
  • Loading branch information
caelunshun and cwfitzgerald authored Jan 15, 2025
1 parent 0f37714 commit 3036413
Show file tree
Hide file tree
Showing 4 changed files with 48 additions and 2 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down
2 changes: 2 additions & 0 deletions wgpu-core/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand Down
4 changes: 2 additions & 2 deletions wgpu-core/src/validation.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand Down Expand Up @@ -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;

Expand Down
43 changes: 43 additions & 0 deletions wgpu/src/util/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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)
}
}

0 comments on commit 3036413

Please sign in to comment.