Skip to content

Commit 3036413

Browse files
Expose conversions between TextureFormat and naga::StorageFormat (#6185)
Co-authored-by: Connor Fitzgerald <[email protected]>
1 parent 0f37714 commit 3036413

File tree

4 files changed

+48
-2
lines changed

4 files changed

+48
-2
lines changed

CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -529,6 +529,7 @@ By @MarijnS95 in [#6006](https://github.com/gfx-rs/wgpu/pull/6006).
529529
- 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).
530530
- Add a separate pipeline constants error. By @teoxoy in [#6094](https://github.com/gfx-rs/wgpu/pull/6094).
531531
- 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).
532+
- Add conversions between `TextureFormat` and ` StorageFormat`. By @caelunshun in [#6185](https://github.com/gfx-rs/wgpu/pull/6185)
532533

533534
#### GLES / OpenGL
534535

wgpu-core/src/lib.rs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -90,6 +90,8 @@ mod weak_vec;
9090
mod scratch;
9191
pub mod validation;
9292

93+
pub use validation::{map_storage_format_from_naga, map_storage_format_to_naga};
94+
9395
pub use hal::{api, MAX_BIND_GROUPS, MAX_COLOR_ATTACHMENTS, MAX_VERTEX_BUFFERS};
9496
pub use naga;
9597

wgpu-core/src/validation.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -278,7 +278,7 @@ pub enum StageError {
278278
InvalidResource(#[from] InvalidResourceError),
279279
}
280280

281-
fn map_storage_format_to_naga(format: wgt::TextureFormat) -> Option<naga::StorageFormat> {
281+
pub fn map_storage_format_to_naga(format: wgt::TextureFormat) -> Option<naga::StorageFormat> {
282282
use naga::StorageFormat as Sf;
283283
use wgt::TextureFormat as Tf;
284284

@@ -335,7 +335,7 @@ fn map_storage_format_to_naga(format: wgt::TextureFormat) -> Option<naga::Storag
335335
})
336336
}
337337

338-
fn map_storage_format_from_naga(format: naga::StorageFormat) -> wgt::TextureFormat {
338+
pub fn map_storage_format_from_naga(format: naga::StorageFormat) -> wgt::TextureFormat {
339339
use naga::StorageFormat as Sf;
340340
use wgt::TextureFormat as Tf;
341341

wgpu/src/util/mod.rs

Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -189,3 +189,46 @@ pub fn pipeline_cache_key(adapter_info: &wgt::AdapterInfo) -> Option<String> {
189189
_ => None,
190190
}
191191
}
192+
193+
/// Adds extra conversion functions to `TextureFormat`.
194+
pub trait TextureFormatExt {
195+
/// Finds the [`TextureFormat`](wgt::TextureFormat) corresponding to the given
196+
/// [`StorageFormat`](wgc::naga::StorageFormat).
197+
///
198+
/// # Examples
199+
/// ```
200+
/// use wgpu::util::TextureFormatExt;
201+
/// assert_eq!(wgpu::TextureFormat::from_storage_format(wgpu::naga::StorageFormat::Bgra8Unorm), wgpu::TextureFormat::Bgra8Unorm);
202+
/// ```
203+
#[cfg_attr(docsrs, doc(cfg(any(wgpu_core, naga))))]
204+
#[cfg(any(wgpu_core, naga))]
205+
fn from_storage_format(storage_format: crate::naga::StorageFormat) -> Self;
206+
207+
/// Finds the [`StorageFormat`](wgc::naga::StorageFormat) corresponding to the given [`TextureFormat`](wgt::TextureFormat).
208+
/// Returns `None` if there is no matching storage format,
209+
/// which typically indicates this format is not supported
210+
/// for storage textures.
211+
///
212+
/// # Examples
213+
/// ```
214+
/// use wgpu::util::TextureFormatExt;
215+
/// assert_eq!(wgpu::TextureFormat::Bgra8Unorm.to_storage_format(), Some(wgpu::naga::StorageFormat::Bgra8Unorm));
216+
/// ```
217+
#[cfg_attr(docsrs, doc(cfg(any(wgpu_core, naga))))]
218+
#[cfg(any(wgpu_core, naga))]
219+
fn to_storage_format(&self) -> Option<crate::naga::StorageFormat>;
220+
}
221+
222+
impl TextureFormatExt for wgt::TextureFormat {
223+
#[cfg_attr(docsrs, doc(cfg(any(wgpu_core, naga))))]
224+
#[cfg(any(wgpu_core, naga))]
225+
fn from_storage_format(storage_format: crate::naga::StorageFormat) -> Self {
226+
wgc::map_storage_format_from_naga(storage_format)
227+
}
228+
229+
#[cfg_attr(docsrs, doc(cfg(any(wgpu_core, naga))))]
230+
#[cfg(any(wgpu_core, naga))]
231+
fn to_storage_format(&self) -> Option<crate::naga::StorageFormat> {
232+
wgc::map_storage_format_to_naga(*self)
233+
}
234+
}

0 commit comments

Comments
 (0)