diff --git a/CHANGELOG.md b/CHANGELOG.md index a9ccfb37f5c..ef50940bbc9 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -49,6 +49,7 @@ Bottom level categories: - Support the `wasm64-unknown-unknown` target for the web backend. Building for wasm64 requires a nightly toolchain with `-Z build-std=std,panic_abort`. By @nickbabcock in [#9836](https://github.com/gfx-rs/wgpu/pull/9836). - Many types now offer `pub const fn default()` in addition to implementing the `Default` trait, allowing constants to make use of default values. By @kpreid in [#9929](https://github.com/gfx-rs/wgpu/pull/9929). - `wgpu-core` now exposes `validate_device_descriptor` and `validate_texture_descriptor` functions that perform the same descriptor validation the corresponding resource creation APIs would, without actually creating a resource. This may be useful in conjunction with hal raw APIs. By @andyleiserson in [#9967](https://github.com/gfx-rs/wgpu/pull/9967) and [#9979](https://github.com/gfx-rs/wgpu/pull/9979). +- Added `TextureDescriptor::theoretical_memory_footprint` to estimate memory footprint of a texture. By @sagudev in [#10032](https://github.com/gfx-rs/wgpu/pull/10032). #### Hal diff --git a/wgpu-types/src/texture.rs b/wgpu-types/src/texture.rs index b22c39db01f..ada29b9705f 100644 --- a/wgpu-types/src/texture.rs +++ b/wgpu-types/src/texture.rs @@ -647,6 +647,21 @@ impl TextureDescriptor { TextureDimension::D2 => self.size.depth_or_array_layers, } } + + /// Returns the theoretical memory footprint of a texture. + /// + /// Actual memory usage may greatly exceed this value due to alignment and padding. + #[must_use] + pub fn theoretical_memory_footprint(&self) -> u64 { + (0..self.mip_level_count).fold(0, |acc, level| { + acc.saturating_add( + self.format.theoretical_memory_footprint( + self.mip_level_size(level) + .expect("mipmap level should be inbounds"), + ), + ) + }) + } } /// Describes a `Sampler`.