diff --git a/CHANGELOG.md b/CHANGELOG.md index 2cf8c693e59..313130d2bd9 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -171,6 +171,10 @@ By @beholdnec in [#8505](https://github.com/gfx-rs/wgpu/pull/8505). - Added new `InvalidWorkgroupSizeError`, which is now used by `DrawError::InvalidGroupSize` and `StageError::InvalidWorkgroupSize`. - Added `BuildAccelerationStructureError` variant `OffsetLimitedTo4GB` and changed `IndirectBufferOverrun` to contain offset and size rather than start and end offsets. - `IndexFormat::byte_size` now returns `u32` instead of `usize`. +- BREAKING: `map_label` helpers have changed slightly. By @beicause and @andyleiserson in [#9480](https://github.com/gfx-rs/wgpu/pull/9480), [#9481](https://github.com/gfx-rs/wgpu/pull/9481), and TBD. + - `SurfaceConfiguration::map_label` now takes `FnOnce(&V) -> M` instead of `FnOnce(V) -> M`. + - All `map_label` helpers except `CreateShaderModuleDescriptorPassthrough` now have the signature `map_label<'a, K>(&'a self, fun: impl FnOnce(&'a L) -> K)` (previously the lifetimes were implicit and thus could differ). +- `create_texture` now takes a descriptor with `view_formats` having the parameterized type `V: AsRef<[TextureFormat]>`. #### Validation diff --git a/wgpu-types/src/buffer.rs b/wgpu-types/src/buffer.rs index 46d4c044c0e..7662fc720aa 100644 --- a/wgpu-types/src/buffer.rs +++ b/wgpu-types/src/buffer.rs @@ -34,7 +34,7 @@ pub struct BufferDescriptor { impl BufferDescriptor { /// Takes a closure and maps the label of the buffer descriptor into another. #[must_use] - pub fn map_label(&self, fun: impl FnOnce(&L) -> K) -> BufferDescriptor { + pub fn map_label<'a, K>(&'a self, fun: impl FnOnce(&'a L) -> K) -> BufferDescriptor { BufferDescriptor { label: fun(&self.label), size: self.size, diff --git a/wgpu-types/src/device.rs b/wgpu-types/src/device.rs index fe6c6fbd138..a18fdce67ac 100644 --- a/wgpu-types/src/device.rs +++ b/wgpu-types/src/device.rs @@ -37,7 +37,7 @@ pub struct DeviceDescriptor { impl DeviceDescriptor { /// Takes a closure and maps the label of the device descriptor into another. #[must_use] - pub fn map_label(&self, fun: impl FnOnce(&L) -> K) -> DeviceDescriptor { + pub fn map_label<'a, K>(&'a self, fun: impl FnOnce(&'a L) -> K) -> DeviceDescriptor { DeviceDescriptor { label: fun(&self.label), required_features: self.required_features, diff --git a/wgpu-types/src/lib.rs b/wgpu-types/src/lib.rs index e68588e327b..a51b455cf5e 100644 --- a/wgpu-types/src/lib.rs +++ b/wgpu-types/src/lib.rs @@ -403,7 +403,7 @@ pub struct CommandEncoderDescriptor { impl CommandEncoderDescriptor { /// Takes a closure and maps the label of the command encoder descriptor into another. #[must_use] - pub fn map_label(&self, fun: impl FnOnce(&L) -> K) -> CommandEncoderDescriptor { + pub fn map_label<'a, K>(&'a self, fun: impl FnOnce(&'a L) -> K) -> CommandEncoderDescriptor { CommandEncoderDescriptor { label: fun(&self.label), } @@ -489,7 +489,7 @@ pub struct CommandBufferDescriptor { impl CommandBufferDescriptor { /// Takes a closure and maps the label of the command buffer descriptor into another. #[must_use] - pub fn map_label(&self, fun: impl FnOnce(&L) -> K) -> CommandBufferDescriptor { + pub fn map_label<'a, K>(&'a self, fun: impl FnOnce(&'a L) -> K) -> CommandBufferDescriptor { CommandBufferDescriptor { label: fun(&self.label), } diff --git a/wgpu-types/src/ray_tracing.rs b/wgpu-types/src/ray_tracing.rs index 0ccf23339a0..c946cd5661c 100644 --- a/wgpu-types/src/ray_tracing.rs +++ b/wgpu-types/src/ray_tracing.rs @@ -86,7 +86,7 @@ pub struct CreateBlasDescriptor { impl CreateBlasDescriptor { /// Takes a closure and maps the label of the blas descriptor into another. - pub fn map_label(&self, fun: impl FnOnce(&L) -> K) -> CreateBlasDescriptor { + pub fn map_label<'a, K>(&'a self, fun: impl FnOnce(&'a L) -> K) -> CreateBlasDescriptor { CreateBlasDescriptor { label: fun(&self.label), flags: self.flags, @@ -112,7 +112,7 @@ pub struct CreateTlasDescriptor { impl CreateTlasDescriptor { /// Takes a closure and maps the label of the blas descriptor into another. - pub fn map_label(&self, fun: impl FnOnce(&L) -> K) -> CreateTlasDescriptor { + pub fn map_label<'a, K>(&'a self, fun: impl FnOnce(&'a L) -> K) -> CreateTlasDescriptor { CreateTlasDescriptor { label: fun(&self.label), flags: self.flags, diff --git a/wgpu-types/src/render.rs b/wgpu-types/src/render.rs index 2bef8e65bc0..a452e735375 100644 --- a/wgpu-types/src/render.rs +++ b/wgpu-types/src/render.rs @@ -929,7 +929,7 @@ pub struct RenderBundleDescriptor { impl RenderBundleDescriptor { /// Takes a closure and maps the label of the render bundle descriptor into another. #[must_use] - pub fn map_label(&self, fun: impl FnOnce(&L) -> K) -> RenderBundleDescriptor { + pub fn map_label<'a, K>(&'a self, fun: impl FnOnce(&'a L) -> K) -> RenderBundleDescriptor { RenderBundleDescriptor { label: fun(&self.label), } diff --git a/wgpu-types/src/surface.rs b/wgpu-types/src/surface.rs index a2f73c4a1fd..a22871d7d5c 100644 --- a/wgpu-types/src/surface.rs +++ b/wgpu-types/src/surface.rs @@ -241,7 +241,10 @@ pub struct SurfaceConfiguration { impl SurfaceConfiguration { /// Map `view_formats` of the texture descriptor into another. - pub fn map_view_formats(&self, fun: impl FnOnce(V) -> M) -> SurfaceConfiguration { + pub fn map_view_formats<'a, M>( + &'a self, + fun: impl FnOnce(&'a V) -> M, + ) -> SurfaceConfiguration { SurfaceConfiguration { usage: self.usage, format: self.format, @@ -250,7 +253,7 @@ impl SurfaceConfiguration { present_mode: self.present_mode, desired_maximum_frame_latency: self.desired_maximum_frame_latency, alpha_mode: self.alpha_mode, - view_formats: fun(self.view_formats.clone()), + view_formats: fun(&self.view_formats), } } } diff --git a/wgpu-types/src/texture.rs b/wgpu-types/src/texture.rs index bd190e5a441..b089d5799da 100644 --- a/wgpu-types/src/texture.rs +++ b/wgpu-types/src/texture.rs @@ -480,7 +480,7 @@ pub struct TextureViewDescriptor { impl TextureViewDescriptor { /// Takes a closure and maps the label of the texture view descriptor into another. #[must_use] - pub fn map_label(&self, fun: impl FnOnce(&L) -> K) -> TextureViewDescriptor { + pub fn map_label<'a, K>(&'a self, fun: impl FnOnce(&'a L) -> K) -> TextureViewDescriptor { TextureViewDescriptor { label: fun(&self.label), format: self.format, @@ -530,7 +530,7 @@ pub struct TextureDescriptor { impl TextureDescriptor { /// Takes a closure and maps the label of the texture descriptor into another. #[must_use] - pub fn map_label(&self, fun: impl FnOnce(&L) -> K) -> TextureDescriptor + pub fn map_label<'a, K>(&'a self, fun: impl FnOnce(&'a L) -> K) -> TextureDescriptor where V: Clone, { @@ -548,10 +548,10 @@ impl TextureDescriptor { /// Maps the label and view formats of the texture descriptor into another. #[must_use] - pub fn map_label_and_view_formats( - &self, - l_fun: impl FnOnce(&L) -> K, - v_fun: impl FnOnce(&V) -> M, + pub fn map_label_and_view_formats<'a, K, M>( + &'a self, + l_fun: impl FnOnce(&'a L) -> K, + v_fun: impl FnOnce(&'a V) -> M, ) -> TextureDescriptor { TextureDescriptor { label: l_fun(&self.label), @@ -703,7 +703,7 @@ impl Default for SamplerDescriptor { impl SamplerDescriptor { /// Takes a closure and maps the label of the sampler descriptor into another. #[must_use] - pub fn map_label(&self, fun: impl FnOnce(&L) -> K) -> SamplerDescriptor { + pub fn map_label<'a, K>(&'a self, fun: impl FnOnce(&'a L) -> K) -> SamplerDescriptor { SamplerDescriptor { label: fun(&self.label), address_mode_u: self.address_mode_u, diff --git a/wgpu-types/src/texture/external_texture.rs b/wgpu-types/src/texture/external_texture.rs index 0920913ee98..5d729b60407 100644 --- a/wgpu-types/src/texture/external_texture.rs +++ b/wgpu-types/src/texture/external_texture.rs @@ -141,7 +141,10 @@ pub struct ExternalTextureDescriptor { impl ExternalTextureDescriptor { /// Takes a closure and maps the label of the external texture descriptor into another. #[must_use] - pub fn map_label(&self, fun: impl FnOnce(&L) -> K) -> ExternalTextureDescriptor { + pub fn map_label<'a, K>( + &'a self, + fun: impl FnOnce(&'a L) -> K, + ) -> ExternalTextureDescriptor { ExternalTextureDescriptor { label: fun(&self.label), width: self.width, diff --git a/wgpu/Cargo.toml b/wgpu/Cargo.toml index 9a92244cc6a..11a9b79b170 100644 --- a/wgpu/Cargo.toml +++ b/wgpu/Cargo.toml @@ -83,7 +83,7 @@ angle = ["wgpu-core?/angle"] vulkan-portability = ["wgpu-core?/vulkan-portability"] ## Enables the GLES backend on WebAssembly only. -webgl = ["web", "wgpu-core/webgl", "dep:wgpu-hal", "dep:smallvec"] +webgl = ["web", "wgpu-core/webgl", "dep:wgpu-hal"] #! ### Backend features @@ -96,7 +96,7 @@ drm = ["wgpu-core?/drm"] ## but performs no computation. ## Because it lacks basic functionality, it is only actually used if explicitly enabled ## through `NoopBackendOptions`. -noop = ["wgpu-core/noop", "dep:wgpu-hal", "dep:smallvec"] +noop = ["wgpu-core/noop", "dep:wgpu-hal"] #! **Note:** In the documentation, if you see that an item depends on a backend, #! it means that the item is only available when that backend is enabled _and_ the backend @@ -220,6 +220,7 @@ hashbrown.workspace = true parking_lot = { workspace = true, optional = true } profiling.workspace = true raw-window-handle = { workspace = true, features = ["alloc"] } +smallvec.workspace = true static_assertions.workspace = true ######################################## @@ -238,8 +239,6 @@ wgpu-core = { workspace = true, features = [ ] } wgpu-hal.workspace = true -smallvec.workspace = true - ############### # Webassembly # ############### @@ -256,8 +255,6 @@ web-sys = { workspace = true, optional = true, features = [ wgpu-core = { workspace = true, optional = true } wgpu-hal = { workspace = true, optional = true } -smallvec = { workspace = true, optional = true } - # Needed for the webgpu backend. Optional as webgpu is optional on WebAssembly. wasm-bindgen-futures = { workspace = true, optional = true } @@ -268,8 +265,6 @@ wasm-bindgen-futures = { workspace = true, optional = true } wgpu-core.workspace = true wgpu-hal.workspace = true -smallvec.workspace = true - [target.'cfg(not(target_has_atomic = "64"))'.dependencies] portable-atomic.workspace = true diff --git a/wgpu/src/api/device.rs b/wgpu/src/api/device.rs index 6f533aa26b8..79b8e6e5225 100644 --- a/wgpu/src/api/device.rs +++ b/wgpu/src/api/device.rs @@ -3,6 +3,8 @@ use alloc::{boxed::Box, string::String, sync::Arc, vec}; use core::ops::Deref; use core::{error, fmt, future::Future, marker::PhantomData}; +use smallvec::SmallVec; + use crate::api::blas::{Blas, BlasGeometrySizeDescriptors, CreateBlasDescriptor}; use crate::api::tlas::{CreateTlasDescriptor, Tlas}; use crate::util::Mutex; @@ -291,16 +293,17 @@ impl Device { /// /// `desc` specifies the general format of the texture. #[must_use] - pub fn create_texture(&self, desc: &TextureDescriptor<'_>) -> Texture { - let texture = self.inner.create_texture(desc); + pub fn create_texture(&self, desc: &wgt::TextureDescriptor, V>) -> Texture + where + V: AsRef<[TextureFormat]>, + { + let descriptor = desc.map_label_and_view_formats(Option::clone, |v| v.as_ref()); + let texture = self.inner.create_texture(&descriptor); Texture { inner: texture, - descriptor: TextureDescriptor { - label: None, - view_formats: &[], - ..desc.clone() - }, + descriptor: descriptor + .map_label_and_view_formats(|_| None, |v| SmallVec::from_slice(v)), } } @@ -335,11 +338,7 @@ impl Device { }; Texture { inner: texture.into(), - descriptor: TextureDescriptor { - label: None, - view_formats: &[], - ..desc.clone() - }, + descriptor: desc.map_label_and_view_formats(|_| None, |v| SmallVec::from_slice(v)), } } diff --git a/wgpu/src/api/surface.rs b/wgpu/src/api/surface.rs index 11f6f64564e..e773ab11302 100644 --- a/wgpu/src/api/surface.rs +++ b/wgpu/src/api/surface.rs @@ -4,6 +4,7 @@ use core::ops::Deref; use core::{error, fmt}; use raw_window_handle::{HasDisplayHandle, HasWindowHandle}; +use smallvec::SmallVec; use crate::util::Mutex; use crate::*; @@ -154,7 +155,8 @@ impl Surface<'_> { let surface_texture = SurfaceTexture { texture: Texture { inner: texture, - descriptor, + descriptor: descriptor + .map_label_and_view_formats(|_| None, |v| SmallVec::from_slice(v)), }, presented: false, detail, diff --git a/wgpu/src/api/texture.rs b/wgpu/src/api/texture.rs index a2be922bb88..58713cfa4d7 100644 --- a/wgpu/src/api/texture.rs +++ b/wgpu/src/api/texture.rs @@ -1,6 +1,8 @@ #[cfg(wgpu_core)] use core::ops::Deref; +use smallvec::SmallVec; + use crate::*; /// Handle to a texture on the GPU. @@ -11,7 +13,10 @@ use crate::*; #[derive(Debug, Clone)] pub struct Texture { pub(crate) inner: dispatch::DispatchTexture, - pub(crate) descriptor: TextureDescriptor<'static>, + + /// The descriptor that was used to create this texture. + /// The label is not saved and will always be `None` in this copy. + pub(crate) descriptor: wgt::TextureDescriptor, SmallVec<[TextureFormat; 1]>>, } #[cfg(send_sync)] static_assertions::assert_impl_all!(Texture: Send, Sync); @@ -77,11 +82,7 @@ impl Texture { ) -> Self { Self { inner: dispatch::DispatchTexture::custom(texture), - descriptor: TextureDescriptor { - label: None, - view_formats: &[], - ..desc.clone() - }, + descriptor: desc.map_label_and_view_formats(|_| None, |v| SmallVec::from_slice(v)), } }