From c841633bb30c360cea99febc980b14d23248b590 Mon Sep 17 00:00:00 2001 From: Luo Zhihao Date: Fri, 19 Jun 2026 09:28:52 +0800 Subject: [PATCH] Bring transient attachment in line with webgpu spec (#9568) --- CHANGELOG.md | 2 + cts_runner/test.lst | 4 + deno_webgpu/01_webgpu.js | 3 + deno_webgpu/surface.rs | 9 + examples/features/src/msaa_line/mod.rs | 3 +- examples/features/src/shadow/mod.rs | 3 +- examples/features/src/skybox/mod.rs | 3 +- tests/tests/wgpu-gpu/transient.rs | 3 +- tests/tests/wgpu-validation/api/texture.rs | 21 +- wgpu-core/src/command/render.rs | 235 ++++++++++++++++----- wgpu-core/src/conv.rs | 4 +- wgpu-core/src/device/resource.rs | 57 ++++- wgpu-core/src/instance.rs | 2 +- wgpu-core/src/pipeline_cache.rs | 2 +- wgpu-core/src/resource.rs | 23 +- wgpu-hal/src/dx12/adapter.rs | 2 +- wgpu-hal/src/metal/mod.rs | 2 +- wgpu-hal/src/vulkan/adapter.rs | 2 +- wgpu-info/src/human.rs | 2 +- wgpu-types/src/adapter.rs | 7 +- wgpu-types/src/texture.rs | 21 +- wgpu-types/src/texture/format.rs | 3 +- wgpu/src/backend/webgpu.rs | 4 +- 23 files changed, 311 insertions(+), 106 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index fa9562d90..9e39f8e80 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -193,6 +193,8 @@ By @beholdnec in [#8505](https://github.com/gfx-rs/wgpu/pull/8505). - `TextureDescriptor::map_label_and_view_formats` and `SurfaceConfiguration::map_view_formats` now take `FnOnce(&V)` instead of `FnOnce(V)`. - 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). - Relaxed locking within `wgpu-core` to enable queue submission processing on one thread to proceed while another thread is blocked in a device poll. To facilitate this, `wgpu-hal` fences are now internally synchronized. By @Vecvec in [#9475](https://github.com/gfx-rs/wgpu/pull/9475). +- `AdapterInfo::transient_saves_memory` now is `Option` instead of `bool`. It is `None` on web and `Some` on native platforms. By @beicause in [#9568](https://github.com/gfx-rs/wgpu/pull/9568). +- BREAKING: `TextureUsages::TRANSIENT` is renamed to `TextureUsages::TRANSIENT_ATTACHMENT` and brought in line with WebGPU spec. Transient textures may now only be used with `LoadOp::Clear` or `LoadOp::DontCare` (if it is available) and `StoreOp::Discard`. By @beicause in [#9568](https://github.com/gfx-rs/wgpu/pull/9568). #### Validation diff --git a/cts_runner/test.lst b/cts_runner/test.lst index f7faff6c6..18a635e72 100644 --- a/cts_runner/test.lst +++ b/cts_runner/test.lst @@ -133,12 +133,15 @@ webgpu:api,validation,createBindGroupLayout:visibility,* webgpu:api,validation,createBindGroupLayout:visibility:* webgpu:api,validation,createPipelineLayout:* webgpu:api,validation,createSampler:* +webgpu:api,validation,createTexture:depthOrArrayLayers_and_mipLevelCount_for_transient_attachments:* webgpu:api,validation,createTexture:dimension_type_and_format_compatibility:* webgpu:api,validation,createTexture:mipLevelCount,* webgpu:api,validation,createTexture:sample_count,* webgpu:api,validation,createTexture:sampleCount,* webgpu:api,validation,createTexture:texture_size,* webgpu:api,validation,createTexture:texture_usage,* +webgpu:api,validation,createTexture:transient_viewFormats:* +webgpu:api,validation,createTexture:usage:* webgpu:api,validation,createTexture:viewFormats:* webgpu:api,validation,createTexture:zero_size_and_usage:* webgpu:api,validation,createView:array_layers:* @@ -147,6 +150,7 @@ webgpu:api,validation,createView:cube_faces_square:* webgpu:api,validation,createView:dimension:* webgpu:api,validation,createView:format:* webgpu:api,validation,createView:mip_levels:* +webgpu:api,validation,createView:texture_view_usage_of_multiple_usages:* webgpu:api,validation,createView:texture_view_usage_with_view_format:* webgpu:api,validation,createView:texture_view_usage:* webgpu:api,validation,debugMarker:* diff --git a/deno_webgpu/01_webgpu.js b/deno_webgpu/01_webgpu.js index 2aa056ae3..ba97ecb95 100644 --- a/deno_webgpu/01_webgpu.js +++ b/deno_webgpu/01_webgpu.js @@ -482,6 +482,9 @@ class GPUTextureUsage { static get RENDER_ATTACHMENT() { return 0x10; } + static get TRANSIENT_ATTACHMENT() { + return 0x20; + } } const GPUTextureViewPrototype = GPUTextureView.prototype; diff --git a/deno_webgpu/surface.rs b/deno_webgpu/surface.rs index 24b4b1188..7bb0c8ffc 100644 --- a/deno_webgpu/surface.rs +++ b/deno_webgpu/surface.rs @@ -74,6 +74,15 @@ impl GPUCanvasContext { &self, #[webidl] configuration: GPUCanvasConfiguration, ) -> Result<(), JsErrorBox> { + if configuration + .usage + .0 + .contains(wgpu_types::TextureUsages::TRANSIENT_ATTACHMENT) + { + return Err(JsErrorBox::type_error( + "`GPUCanvasConfiguration.usage` must not include `TRANSIENT_ATTACHMENT`", + )); + } let format = configuration.format.clone().into(); let conf = wgpu_types::SurfaceConfiguration { usage: configuration.usage.into(), diff --git a/examples/features/src/msaa_line/mod.rs b/examples/features/src/msaa_line/mod.rs index 9e8a34a59..a84578a01 100644 --- a/examples/features/src/msaa_line/mod.rs +++ b/examples/features/src/msaa_line/mod.rs @@ -112,7 +112,8 @@ impl Example { sample_count, dimension: wgpu::TextureDimension::D2, format: config.view_formats[0], - usage: wgpu::TextureUsages::RENDER_ATTACHMENT | wgpu::TextureUsages::TRANSIENT, + usage: wgpu::TextureUsages::RENDER_ATTACHMENT + | wgpu::TextureUsages::TRANSIENT_ATTACHMENT, label: None, view_formats: &[], }; diff --git a/examples/features/src/shadow/mod.rs b/examples/features/src/shadow/mod.rs index 1369037d5..32f59b6a7 100644 --- a/examples/features/src/shadow/mod.rs +++ b/examples/features/src/shadow/mod.rs @@ -192,7 +192,8 @@ impl Example { sample_count: 1, dimension: wgpu::TextureDimension::D2, format: Self::DEPTH_FORMAT, - usage: wgpu::TextureUsages::RENDER_ATTACHMENT | wgpu::TextureUsages::TRANSIENT, + usage: wgpu::TextureUsages::RENDER_ATTACHMENT + | wgpu::TextureUsages::TRANSIENT_ATTACHMENT, label: None, view_formats: &[], }); diff --git a/examples/features/src/skybox/mod.rs b/examples/features/src/skybox/mod.rs index 4ce57e2d1..bd67c88f2 100644 --- a/examples/features/src/skybox/mod.rs +++ b/examples/features/src/skybox/mod.rs @@ -80,7 +80,8 @@ impl Example { sample_count: 1, dimension: wgpu::TextureDimension::D2, format: Self::DEPTH_FORMAT, - usage: wgpu::TextureUsages::RENDER_ATTACHMENT | wgpu::TextureUsages::TRANSIENT, + usage: wgpu::TextureUsages::RENDER_ATTACHMENT + | wgpu::TextureUsages::TRANSIENT_ATTACHMENT, label: None, view_formats: &[], }); diff --git a/tests/tests/wgpu-gpu/transient.rs b/tests/tests/wgpu-gpu/transient.rs index 6ec3629a1..50d81b9d8 100644 --- a/tests/tests/wgpu-gpu/transient.rs +++ b/tests/tests/wgpu-gpu/transient.rs @@ -76,7 +76,8 @@ static RESOLVE_WITH_TRANSIENT: GpuTestConfiguration = GpuTestConfiguration::new( sample_count: 4, dimension: wgpu::TextureDimension::D2, format: wgpu::TextureFormat::Rgba8Unorm, - usage: wgpu::TextureUsages::RENDER_ATTACHMENT | wgpu::TextureUsages::TRANSIENT, + usage: wgpu::TextureUsages::RENDER_ATTACHMENT + | wgpu::TextureUsages::TRANSIENT_ATTACHMENT, view_formats: &[], }); diff --git a/tests/tests/wgpu-validation/api/texture.rs b/tests/tests/wgpu-validation/api/texture.rs index d4fefafa0..704d60317 100644 --- a/tests/tests/wgpu-validation/api/texture.rs +++ b/tests/tests/wgpu-validation/api/texture.rs @@ -581,7 +581,7 @@ fn copy_buffer_to_texture_forbidden_format_aspect() { } } -/// Ensures that attempting to create a texture with [`wgpu::TextureUsages::TRANSIENT`] +/// Ensures that attempting to create a texture with [`wgpu::TextureUsages::TRANSIENT_ATTACHMENT`] /// and its unsupported usages fails validation. #[test] fn transient_invalid_usage() { @@ -595,9 +595,12 @@ fn transient_invalid_usage() { let invalid_usages = wgpu::TextureUsages::all() - wgpu::TextureUsages::RENDER_ATTACHMENT - - wgpu::TextureUsages::TRANSIENT; + - wgpu::TextureUsages::TRANSIENT_ATTACHMENT; for usage in invalid_usages { + let usage = wgpu::TextureUsages::RENDER_ATTACHMENT + | wgpu::TextureUsages::TRANSIENT_ATTACHMENT + | usage; let invalid_texture_descriptor = wgpu::TextureDescriptor { label: None, size, @@ -605,13 +608,13 @@ fn transient_invalid_usage() { sample_count: 1, dimension: wgpu::TextureDimension::D2, format: wgpu::TextureFormat::Rgba8Unorm, - usage: wgpu::TextureUsages::RENDER_ATTACHMENT | wgpu::TextureUsages::TRANSIENT | usage, + usage, view_formats: &[], }; fail( &device, || device.create_texture(&invalid_texture_descriptor), - Some(&format!("Texture usage TextureUsages(TRANSIENT) is not compatible with texture usage {usage:?}")), + Some(&format!("Transient texture usage must be equal to `TRANSIENT_ATTACHMENT | RENDER_ATTACHMENT`, but got `{usage:?}`")), ); } @@ -622,17 +625,17 @@ fn transient_invalid_usage() { sample_count: 1, dimension: wgpu::TextureDimension::D2, format: wgpu::TextureFormat::Rgba8Unorm, - usage: wgpu::TextureUsages::TRANSIENT, + usage: wgpu::TextureUsages::TRANSIENT_ATTACHMENT, view_formats: &[], }; fail( &device, || device.create_texture(&invalid_texture_descriptor), - Some("Invalid usage flags TextureUsages(TRANSIENT)"), + Some("Transient texture usage must be equal to `TRANSIENT_ATTACHMENT | RENDER_ATTACHMENT`, but got `TextureUsages(TRANSIENT_ATTACHMENT)`"), ); } -/// Ensures that attempting to use a texture of [`wgpu::TextureUsages::TRANSIENT`] +/// Ensures that attempting to use a texture of [`wgpu::TextureUsages::TRANSIENT_ATTACHMENT`] /// with [`wgpu::StoreOp::Store`] fails validation. #[test] fn transient_invalid_storeop() { @@ -651,7 +654,7 @@ fn transient_invalid_storeop() { sample_count: 1, dimension: wgpu::TextureDimension::D2, format: wgpu::TextureFormat::Rgba8Unorm, - usage: wgpu::TextureUsages::RENDER_ATTACHMENT | wgpu::TextureUsages::TRANSIENT, + usage: wgpu::TextureUsages::RENDER_ATTACHMENT | wgpu::TextureUsages::TRANSIENT_ATTACHMENT, view_formats: &[], }); @@ -681,6 +684,6 @@ fn transient_invalid_storeop() { encoder.finish() }, - Some("Color attachment's usage contains TextureUsages(TRANSIENT). This can only be used with StoreOp::Discard, but StoreOp::Store was provided") + Some("Color attachment with `TRANSIENT_ATTACHMENT` usage can only be used with `LoadOp::Clear` or `LoadOp::DontCare` (if it is available) and `StoreOp::Discard`. Operations `(Clear(Color { r: 0.0, g: 0.0, b: 0.0, a: 0.0 }), Store)` were provided") ); } diff --git a/wgpu-core/src/command/render.rs b/wgpu-core/src/command/render.rs index fd5913bb2..8618791c2 100644 --- a/wgpu-core/src/command/render.rs +++ b/wgpu-core/src/command/render.rs @@ -809,8 +809,11 @@ pub enum ColorAttachmentError { mip_level: u32, depth_or_array_layer: u32, }, - #[error("Color attachment's usage contains {0:?}. This can only be used with StoreOp::{1:?}, but StoreOp::{2:?} was provided")] - InvalidUsageForStoreOp(TextureUsages, StoreOp, StoreOp), + #[error( + "Color attachment with `TRANSIENT_ATTACHMENT` usage can only be used with \ + `LoadOp::Clear` or `LoadOp::DontCare` (if it is available) and `StoreOp::Discard`. Operations `{0:?}` were provided" + )] + InvalidTransientAttachmentOp((LoadOp, StoreOp)), #[error("Color attachment's load op is `LoadOp::DontCare` but `InstanceFlags::STRICT_WEBGPU_COMPLIANCE` is set")] LoadOpDontCareUnderStrictWebgpuCompliance, } @@ -826,6 +829,20 @@ impl WebGpuError for ColorAttachmentError { pub enum AttachmentError { #[error("The format of the depth-stencil attachment ({0:?}) is not a depth-or-stencil format")] InvalidDepthStencilAttachmentFormat(wgt::TextureFormat), + #[error( + "Depth attachment with `TRANSIENT_ATTACHMENT` usage can only be used with \ + `LoadOp::Clear` or `LoadOp::DontCare` (if it is available) and `StoreOp::Discard`. Operations `{0:?}` were provided" + )] + InvalidTransientDepthAttachmentOps((LoadOp>, StoreOp)), + #[error("Depth attachment with `TRANSIENT_ATTACHMENT` usage cannot be read-only")] + ReadOnlyTransientDepthAttachment, + #[error( + "Stencil attachment with `TRANSIENT_ATTACHMENT` usage can only be used with \ + `LoadOp::Clear` or `LoadOp::DontCare` (if it is available) and `StoreOp::Discard`. Operations `{0:?}` were provided" + )] + InvalidTransientStencilAttachmentOps((LoadOp>, StoreOp)), + #[error("Stencil attachment with `TRANSIENT_ATTACHMENT` usage cannot be read-only")] + ReadOnlyTransientStencilAttachment, #[error("LoadOp must be None for read-only attachments")] ReadOnlyWithLoad, #[error("StoreOp must be None for read-only attachments")] @@ -878,6 +895,8 @@ pub enum RenderPassErrorInner { location: AttachmentErrorLocation, format: wgt::TextureFormat, }, + #[error("The {location} is not valid, because the texture has `TRANSIENT_ATTACHMENT` usage")] + InvalidTransientResolveTarget { location: AttachmentErrorLocation }, #[error("No color attachments or depth attachments were provided, at least one attachment of any kind must be provided")] MissingAttachments, #[error("The {location} is not renderable:")] @@ -1054,6 +1073,7 @@ impl WebGpuError for RenderPassError { RenderPassErrorInner::InvalidParentEncoder | RenderPassErrorInner::UnsupportedResolveTargetFormat { .. } + | RenderPassErrorInner::InvalidTransientResolveTarget { .. } | RenderPassErrorInner::MissingAttachments | RenderPassErrorInner::TextureViewIsNotRenderable { .. } | RenderPassErrorInner::AttachmentsDimensionMismatch { .. } @@ -1546,6 +1566,15 @@ impl RenderPassInfo { format: resolve_view.desc.format, }); } + if resolve_view + .desc + .usage + .contains(TextureUsages::TRANSIENT_ATTACHMENT) + { + return Err(RenderPassErrorInner::InvalidTransientResolveTarget { + location: resolve_location, + }); + } texture_memory_actions.register_implicit_init( &resolve_view.parent, @@ -1766,6 +1795,13 @@ impl RenderPassInfo { } } +fn check_transient_attachment_ops(load_op: LoadOp, store_op: StoreOp) -> bool { + matches!( + (load_op, store_op), + (LoadOp::Clear(_) | LoadOp::DontCare(_), StoreOp::Discard) + ) +} + impl Global { /// Creates a render pass. /// @@ -1816,7 +1852,6 @@ impl Global { { let view = texture_views.get(*view_id).get()?; view.same_device(device)?; - if matches!(*load_op, LoadOp::DontCare(..)) && device .instance_flags @@ -1827,15 +1862,16 @@ impl Global { )); } - if view.desc.usage.contains(TextureUsages::TRANSIENT) - && *store_op != StoreOp::Discard + if view + .desc + .usage + .contains(TextureUsages::TRANSIENT_ATTACHMENT) + && !check_transient_attachment_ops(*load_op, *store_op) { return Err(RenderPassErrorInner::ColorAttachment( - ColorAttachmentError::InvalidUsageForStoreOp( - TextureUsages::TRANSIENT, - StoreOp::Discard, - *store_op, - ), + ColorAttachmentError::InvalidTransientAttachmentOp(( + *load_op, *store_op, + )), )); } @@ -1862,58 +1898,145 @@ impl Global { } } - arc_desc.depth_stencil_attachment = - // https://gpuweb.github.io/gpuweb/#abstract-opdef-gpurenderpassdepthstencilattachment-gpurenderpassdepthstencilattachment-valid-usage - if let Some(depth_stencil_attachment) = desc.depth_stencil_attachment.as_ref() { - let view = texture_views.get(depth_stencil_attachment.view).get()?; - view.same_device(device)?; + // https://gpuweb.github.io/gpuweb/#abstract-opdef-gpurenderpassdepthstencilattachment-gpurenderpassdepthstencilattachment-valid-usage + arc_desc.depth_stencil_attachment = if let Some(depth_stencil_attachment) = + desc.depth_stencil_attachment.as_ref() + { + let view = texture_views.get(depth_stencil_attachment.view).get()?; + view.same_device(device)?; - let format = view.desc.format; - if !format.is_depth_stencil_format() { - return Err(RenderPassErrorInner::InvalidAttachment(AttachmentError::InvalidDepthStencilAttachmentFormat( - view.desc.format, - ))); + let format = view.desc.format; + if !format.is_depth_stencil_format() { + return Err(RenderPassErrorInner::InvalidAttachment( + AttachmentError::InvalidDepthStencilAttachmentFormat(view.desc.format), + )); + } + + if view + .desc + .usage + .contains(TextureUsages::TRANSIENT_ATTACHMENT) + { + // We raise errors here for transient depth/stencil attachments if they + // are read only, or use unsupported ops. `resolve` (called below) + // validates that operations are specified iff the attachment is not + // read-only. + if format.has_depth_aspect() { + match depth_stencil_attachment.depth { + PassChannel { + load_op: Some(load_op), + store_op: Some(store_op), + read_only: _, + } => { + if !check_transient_attachment_ops(load_op, store_op) { + return Err(RenderPassErrorInner::InvalidAttachment( + AttachmentError::InvalidTransientDepthAttachmentOps(( + load_op, store_op, + )), + )); + } + } + PassChannel { + read_only: true, .. + } => { + return Err(RenderPassErrorInner::InvalidAttachment( + AttachmentError::ReadOnlyTransientDepthAttachment, + )) + } + _ => {} + } } - Some(ResolvedRenderPassDepthStencilAttachment { - view, - depth: if format.has_depth_aspect() { - depth_stencil_attachment.depth.resolve(device.instance_flags, |clear| if let Some(clear) = clear { - // If this.depthLoadOp is "clear", this.depthClearValue must be provided and must be between 0.0 and 1.0, inclusive. - if !(0.0..=1.0).contains(&clear) { - Err(AttachmentError::ClearValueOutOfRange(clear)) - } else { - Ok(clear) + if format.has_stencil_aspect() { + match depth_stencil_attachment.stencil { + PassChannel { + load_op: Some(load_op), + store_op: Some(store_op), + read_only: _, + } => { + if !check_transient_attachment_ops(load_op, store_op) { + return Err(RenderPassErrorInner::InvalidAttachment( + AttachmentError::InvalidTransientStencilAttachmentOps(( + load_op, store_op, + )), + )); } - } else { - Err(AttachmentError::NoClearValue) - })? - } else { - if depth_stencil_attachment.depth.load_op.is_some() || depth_stencil_attachment.depth.store_op.is_some() { - return Err(RenderPassErrorInner::InvalidAttachment(AttachmentError::DepthOpsWithoutAspect { - format, - ops: (depth_stencil_attachment.depth.load_op, depth_stencil_attachment.depth.store_op) - })); } - ResolvedPassChannel::ReadOnly - }, - stencil: if format.has_stencil_aspect() { - depth_stencil_attachment.stencil.resolve(device.instance_flags, |clear| { - Ok(convert_stencil_value(clear.unwrap_or_default(), Some(format))) - })? - } else { - if depth_stencil_attachment.stencil.load_op.is_some() || depth_stencil_attachment.stencil.store_op.is_some() { - return Err(RenderPassErrorInner::InvalidAttachment(AttachmentError::StencilOpsWithoutAspect { - format, - ops: (depth_stencil_attachment.stencil.load_op, depth_stencil_attachment.stencil.store_op) - })); + PassChannel { + read_only: true, .. + } => { + return Err(RenderPassErrorInner::InvalidAttachment( + AttachmentError::ReadOnlyTransientStencilAttachment, + )) } - ResolvedPassChannel::ReadOnly - }, - }) - } else { - None - }; + _ => {} + } + } + } + + Some(ResolvedRenderPassDepthStencilAttachment { + view, + depth: if format.has_depth_aspect() { + depth_stencil_attachment + .depth + .resolve(device.instance_flags, |clear| { + if let Some(clear) = clear { + // If this.depthLoadOp is "clear", this.depthClearValue must be provided and must be between 0.0 and 1.0, inclusive. + if !(0.0..=1.0).contains(&clear) { + Err(AttachmentError::ClearValueOutOfRange(clear)) + } else { + Ok(clear) + } + } else { + Err(AttachmentError::NoClearValue) + } + })? + } else { + if depth_stencil_attachment.depth.load_op.is_some() + || depth_stencil_attachment.depth.store_op.is_some() + { + return Err(RenderPassErrorInner::InvalidAttachment( + AttachmentError::DepthOpsWithoutAspect { + format, + ops: ( + depth_stencil_attachment.depth.load_op, + depth_stencil_attachment.depth.store_op, + ), + }, + )); + } + ResolvedPassChannel::ReadOnly + }, + stencil: if format.has_stencil_aspect() { + depth_stencil_attachment.stencil.resolve( + device.instance_flags, + |clear| { + Ok(convert_stencil_value( + clear.unwrap_or_default(), + Some(format), + )) + }, + )? + } else { + if depth_stencil_attachment.stencil.load_op.is_some() + || depth_stencil_attachment.stencil.store_op.is_some() + { + return Err(RenderPassErrorInner::InvalidAttachment( + AttachmentError::StencilOpsWithoutAspect { + format, + ops: ( + depth_stencil_attachment.stencil.load_op, + depth_stencil_attachment.stencil.store_op, + ), + }, + )); + } + ResolvedPassChannel::ReadOnly + }, + }) + } else { + None + }; arc_desc.timestamp_writes = desc .timestamp_writes diff --git a/wgpu-core/src/conv.rs b/wgpu-core/src/conv.rs index 303980567..f0ca779a5 100644 --- a/wgpu-core/src/conv.rs +++ b/wgpu-core/src/conv.rs @@ -131,7 +131,7 @@ pub fn map_texture_usage( ); u.set( wgt::TextureUses::TRANSIENT, - usage.contains(wgt::TextureUsages::TRANSIENT), + usage.contains(wgt::TextureUsages::TRANSIENT_ATTACHMENT), ); u } @@ -193,7 +193,7 @@ pub fn map_texture_usage_from_hal(uses: wgt::TextureUses) -> wgt::TextureUsages uses.contains(wgt::TextureUses::STORAGE_ATOMIC), ); u.set( - wgt::TextureUsages::TRANSIENT, + wgt::TextureUsages::TRANSIENT_ATTACHMENT, uses.contains(wgt::TextureUses::TRANSIENT), ); u diff --git a/wgpu-core/src/device/resource.rs b/wgpu-core/src/device/resource.rs index b89bc4c36..b5299b724 100644 --- a/wgpu-core/src/device/resource.rs +++ b/wgpu-core/src/device/resource.rs @@ -1400,6 +1400,16 @@ impl Device { desc.format, )); } + // Transient textures can only be 2D + if desc + .usage + .contains(wgt::TextureUsages::TRANSIENT_ATTACHMENT) + { + return Err(CreateTextureError::InvalidDimensionUsages( + wgt::TextureUsages::TRANSIENT_ATTACHMENT, + desc.dimension, + )); + } } if desc.dimension != wgt::TextureDimension::D2 @@ -1503,20 +1513,32 @@ impl Device { } } - if desc.usage.contains(wgt::TextureUsages::TRANSIENT) { - if !desc.usage.contains(wgt::TextureUsages::RENDER_ATTACHMENT) { - return Err(CreateTextureError::InvalidUsage( - wgt::TextureUsages::TRANSIENT, + if desc + .usage + .contains(wgt::TextureUsages::TRANSIENT_ATTACHMENT) + { + if desc.usage + != (wgt::TextureUsages::TRANSIENT_ATTACHMENT + | wgt::TextureUsages::RENDER_ATTACHMENT) + { + return Err(CreateTextureError::InvalidTransientTextureUsage(desc.usage)); + } + + if desc.mip_level_count != 1 { + return Err(CreateTextureError::InvalidTransientTextureMipLevelCount( + desc.mip_level_count, )); } - let extra_usage = - desc.usage - wgt::TextureUsages::TRANSIENT - wgt::TextureUsages::RENDER_ATTACHMENT; - if !extra_usage.is_empty() { - return Err(CreateTextureError::IncompatibleUsage( - wgt::TextureUsages::TRANSIENT, - extra_usage, + + if desc.size.depth_or_array_layers != 1 { + return Err(CreateTextureError::InvalidTransientTextureLayerCount( + desc.size.depth_or_array_layers, )); } + + if !desc.view_formats.is_empty() { + return Err(CreateTextureError::InvalidTransientTextureViewFormats); + } } let format_features = self @@ -1787,6 +1809,21 @@ impl Device { if usage.is_empty() { texture.desc.usage } else if texture.desc.usage.contains(usage) { + // Transient texture usage subsetting is disallowed + if texture + .desc + .usage + .contains(wgt::TextureUsages::TRANSIENT_ATTACHMENT) + && texture.desc.usage != usage + { + return Err( + resource::CreateTextureViewError::InvalidTransientTextureViewUsage { + texture: texture.desc.usage, + view: usage, + }, + ); + } + usage } else { return Err(resource::CreateTextureViewError::InvalidTextureViewUsage { diff --git a/wgpu-core/src/instance.rs b/wgpu-core/src/instance.rs index 91c1cb8cb..6ae84a64b 100644 --- a/wgpu-core/src/instance.rs +++ b/wgpu-core/src/instance.rs @@ -763,7 +763,7 @@ impl Adapter { ), ); allowed_usages.set( - wgt::TextureUsages::RENDER_ATTACHMENT | wgt::TextureUsages::TRANSIENT, + wgt::TextureUsages::RENDER_ATTACHMENT | wgt::TextureUsages::TRANSIENT_ATTACHMENT, caps.intersects(Tfc::COLOR_ATTACHMENT | Tfc::DEPTH_STENCIL_ATTACHMENT), ); allowed_usages.set( diff --git a/wgpu-core/src/pipeline_cache.rs b/wgpu-core/src/pipeline_cache.rs index 31b923229..77e1f117e 100644 --- a/wgpu-core/src/pipeline_cache.rs +++ b/wgpu-core/src/pipeline_cache.rs @@ -325,7 +325,7 @@ mod tests { backend: wgt::Backend::Vulkan, subgroup_min_size: 32, subgroup_max_size: 32, - transient_saves_memory: true, + transient_saves_memory: Some(true), limit_bucket: None, }; diff --git a/wgpu-core/src/resource.rs b/wgpu-core/src/resource.rs index 02b2b4b56..e55bdbcde 100644 --- a/wgpu-core/src/resource.rs +++ b/wgpu-core/src/resource.rs @@ -1689,8 +1689,6 @@ pub enum CreateTextureError { CreateTextureView(#[from] CreateTextureViewError), #[error("Invalid usage flags {0:?}")] InvalidUsage(wgt::TextureUsages), - #[error("Texture usage {0:?} is not compatible with texture usage {1:?}")] - IncompatibleUsage(wgt::TextureUsages, wgt::TextureUsages), #[error(transparent)] InvalidDimension(#[from] TextureDimensionError), #[error("Depth texture ({1:?}) can't be created as {0:?}")] @@ -1708,6 +1706,10 @@ pub enum CreateTextureError { InvalidFormatUsages(wgt::TextureUsages, wgt::TextureFormat, bool), #[error("The view format {0:?} is not compatible with texture format {1:?}, only changing srgb-ness is allowed.")] InvalidViewFormat(wgt::TextureFormat, wgt::TextureFormat), + #[error("Transient texture usage must be equal to `TRANSIENT_ATTACHMENT | RENDER_ATTACHMENT`, but got `{0:?}`")] + InvalidTransientTextureUsage(wgt::TextureUsages), + #[error("Transient texture view formats must be empty")] + InvalidTransientTextureViewFormats, #[error("Texture usages {0:?} are not allowed on a texture of dimensions {1:?}")] InvalidDimensionUsages(wgt::TextureUsages, wgt::TextureDimension), #[error("Texture usage STORAGE_BINDING is not allowed for multisampled textures")] @@ -1718,6 +1720,10 @@ pub enum CreateTextureError { InvalidSampleCount(u32, wgt::TextureFormat, Vec, Vec), #[error("Multisampled textures must have RENDER_ATTACHMENT usage")] MultisampledNotRenderAttachment, + #[error("Transient texture mip level count ({0}) must be 1")] + InvalidTransientTextureMipLevelCount(u32), + #[error("Transient texture layer count ({0}) must be 1")] + InvalidTransientTextureLayerCount(u32), #[error("Texture format {0:?} can't be used due to missing features")] MissingFeatures(wgt::TextureFormat, #[source] MissingFeatures), #[error(transparent)] @@ -1746,7 +1752,6 @@ impl WebGpuError for CreateTextureError { Self::MissingDownlevelFlags(e) => e.webgpu_error_type(), Self::InvalidUsage(_) - | Self::IncompatibleUsage(_, _) | Self::InvalidDepthDimension(_, _) | Self::InvalidCompressedDimension(_, _) | Self::InvalidMipLevelCount { .. } @@ -1756,6 +1761,10 @@ impl WebGpuError for CreateTextureError { | Self::InvalidMultisampledStorageBinding | Self::InvalidMultisampledFormat(_) | Self::InvalidSampleCount(..) + | Self::InvalidTransientTextureUsage(_) + | Self::InvalidTransientTextureMipLevelCount(_) + | Self::InvalidTransientTextureLayerCount(_) + | Self::InvalidTransientTextureViewFormats | Self::MultisampledNotRenderAttachment => ErrorType::Validation, } } @@ -1956,6 +1965,13 @@ pub enum CreateTextureViewError { texture: wgt::TextureFormat, view: wgt::TextureFormat, }, + #[error( + "The texture view (`{view:?}`) from transient texture (`{texture:?}`) must have the same usage" + )] + InvalidTransientTextureViewUsage { + texture: wgt::TextureUsages, + view: wgt::TextureUsages, + }, #[error(transparent)] InvalidResource(#[from] InvalidResourceError), #[error(transparent)] @@ -1984,6 +2000,7 @@ impl WebGpuError for CreateTextureViewError { | Self::TextureViewFormatNotRenderable(_) | Self::TextureViewFormatNotStorage(_) | Self::InvalidTextureViewUsage { .. } + | Self::InvalidTransientTextureViewUsage { .. } | Self::MissingFeatures(_) => ErrorType::Validation, } } diff --git a/wgpu-hal/src/dx12/adapter.rs b/wgpu-hal/src/dx12/adapter.rs index 4707298aa..efb486469 100644 --- a/wgpu-hal/src/dx12/adapter.rs +++ b/wgpu-hal/src/dx12/adapter.rs @@ -207,7 +207,7 @@ impl super::Adapter { driver_info: String::new(), subgroup_min_size: features1.WaveLaneCountMin, subgroup_max_size: features1.WaveLaneCountMax, - transient_saves_memory: false, + transient_saves_memory: Some(false), limit_bucket: None, }; diff --git a/wgpu-hal/src/metal/mod.rs b/wgpu-hal/src/metal/mod.rs index e0decffc2..04f8e8fa1 100644 --- a/wgpu-hal/src/metal/mod.rs +++ b/wgpu-hal/src/metal/mod.rs @@ -432,7 +432,7 @@ impl AdapterShared { // for more information. subgroup_min_size: 4, subgroup_max_size: 64, - transient_saves_memory: shared.private_caps.supports_memoryless_storage, + transient_saves_memory: Some(shared.private_caps.supports_memoryless_storage), ..wgt::AdapterInfo::new(shared.private_caps.device_type(), wgt::Backend::Metal) }, features, diff --git a/wgpu-hal/src/vulkan/adapter.rs b/wgpu-hal/src/vulkan/adapter.rs index d99580529..e6c3f7735 100644 --- a/wgpu-hal/src/vulkan/adapter.rs +++ b/wgpu-hal/src/vulkan/adapter.rs @@ -2187,7 +2187,7 @@ impl super::Instance { .subgroup_size_control .map(|subgroup_size| subgroup_size.max_subgroup_size) .unwrap_or(wgt::MAXIMUM_SUBGROUP_MAX_SIZE), - transient_saves_memory: supports_lazily_allocated, + transient_saves_memory: Some(supports_lazily_allocated), ..wgt::AdapterInfo::new(device_type, wgt::Backend::Vulkan) }; let mut workarounds = super::Workarounds::empty(); diff --git a/wgpu-info/src/human.rs b/wgpu-info/src/human.rs index 1e642c49c..0b7a8424f 100644 --- a/wgpu-info/src/human.rs +++ b/wgpu-info/src/human.rs @@ -122,7 +122,7 @@ fn print_adapter(output: &mut impl io::Write, report: &AdapterReport, idx: usize writeln!(output, "\t Driver Info: {}", print_empty_string(driver_info))?; writeln!(output, "\t Subgroup Min Size: {subgroup_min_size}")?; writeln!(output, "\t Subgroup Max Size: {subgroup_max_size}")?; - writeln!(output, "\tTransient Saves Memory: {transient_saves_memory}")?; + writeln!(output, "\tTransient Saves Memory: {transient_saves_memory:?}")?; writeln!(output, "\t Limit Bucket: {}", limit_bucket.as_ref().map_or("", |b| &b.name))?; writeln!(output, "\t WebGPU Compliant: {:?}", downlevel.is_webgpu_compliant())?; diff --git a/wgpu-types/src/adapter.rs b/wgpu-types/src/adapter.rs index 99bbb15e6..a146a4bec 100644 --- a/wgpu-types/src/adapter.rs +++ b/wgpu-types/src/adapter.rs @@ -209,8 +209,9 @@ pub struct AdapterInfo { /// - WARP: 4 or 128 /// - lavapipe: 8 pub subgroup_max_size: u32, - /// If true, adding [`TextureUsages::TRANSIENT`] to a texture will decrease memory usage. - pub transient_saves_memory: bool, + /// Whether adding [`TextureUsages::TRANSIENT_ATTACHMENT`] to a texture will decrease memory usage. + /// This is None on web, which means it is unknown from the adapter. + pub transient_saves_memory: Option, /// If limit bucketing was requested, contains the name of the applied /// bucket and the original capabilities of the adapter. @@ -233,7 +234,7 @@ impl AdapterInfo { backend, subgroup_min_size: crate::MINIMUM_SUBGROUP_MIN_SIZE, subgroup_max_size: crate::MAXIMUM_SUBGROUP_MAX_SIZE, - transient_saves_memory: false, + transient_saves_memory: None, limit_bucket: None, } } diff --git a/wgpu-types/src/texture.rs b/wgpu-types/src/texture.rs index b089d5799..302a92fd0 100644 --- a/wgpu-types/src/texture.rs +++ b/wgpu-types/src/texture.rs @@ -186,9 +186,19 @@ bitflags::bitflags! { const STORAGE_BINDING = 1 << 3; /// Allows a texture to be an output attachment of a render pass. /// - /// Consider adding [`TextureUsages::TRANSIENT`] if the contents are not reused. + /// Consider adding [`TextureUsages::TRANSIENT_ATTACHMENT`] if the contents are not reused. const RENDER_ATTACHMENT = 1 << 4; + /// Specifies the contents of this texture will not be used in another pass to potentially reduce memory usage and bandwidth. + /// + /// No-op on platforms on platforms that do not benefit from transient textures. + /// Generally mobile and Apple chips care about this. + /// + /// Incompatible with ALL other usages except [`TextureUsages::RENDER_ATTACHMENT`] and requires it. + /// + /// Requires [`LoadOp::Clear`] or [`LoadOp::DontCare`] (if it is available) and [`StoreOp::Discard`]. + const TRANSIENT_ATTACHMENT = 1 << 5; + // // ---- Restart Numbering for Native Features --- // @@ -196,15 +206,6 @@ bitflags::bitflags! { // /// Allows a texture to be used with image atomics. Requires [`Features::TEXTURE_ATOMIC`]. const STORAGE_ATOMIC = 1 << 16; - /// Specifies the contents of this texture will not be used in another pass to potentially reduce memory usage and bandwidth. - /// - /// No-op on platforms on platforms that do not benefit from transient textures. - /// Generally mobile and Apple chips care about this. - /// - /// Incompatible with ALL other usages except [`TextureUsages::RENDER_ATTACHMENT`] and requires it. - /// - /// Requires [`StoreOp::Discard`]. - const TRANSIENT = 1 << 17; } } diff --git a/wgpu-types/src/texture/format.rs b/wgpu-types/src/texture/format.rs index a4d27844c..370a0b91f 100644 --- a/wgpu-types/src/texture/format.rs +++ b/wgpu-types/src/texture/format.rs @@ -924,7 +924,8 @@ impl TextureFormat { // Flags let basic = TextureUsages::COPY_SRC | TextureUsages::COPY_DST | TextureUsages::TEXTURE_BINDING; - let attachment = basic | TextureUsages::RENDER_ATTACHMENT | TextureUsages::TRANSIENT; + let attachment = + basic | TextureUsages::RENDER_ATTACHMENT | TextureUsages::TRANSIENT_ATTACHMENT; let storage = basic | TextureUsages::STORAGE_BINDING; let binding = TextureUsages::TEXTURE_BINDING; let all_flags = attachment | storage | binding; diff --git a/wgpu/src/backend/webgpu.rs b/wgpu/src/backend/webgpu.rs index 292ccbab9..f3eb0da7e 100644 --- a/wgpu/src/backend/webgpu.rs +++ b/wgpu/src/backend/webgpu.rs @@ -897,7 +897,7 @@ fn map_adapter_info(adapter_info: &webgpu_sys::GpuAdapterInfo) -> wgt::AdapterIn backend: wgt::Backend::BrowserWebGpu, subgroup_min_size: adapter_info.subgroup_min_size(), subgroup_max_size: adapter_info.subgroup_max_size(), - transient_saves_memory: false, + transient_saves_memory: None, limit_bucket: None, } } @@ -2373,7 +2373,7 @@ impl dispatch::DeviceInterface for WebDevice { let mapped_desc = webgpu_sys::GpuTextureDescriptor::new_with_gpu_extent_3d_dict( map_texture_format(desc.format), &map_extent_3d(desc.size), - (desc.usage - crate::TextureUsages::TRANSIENT).bits(), + desc.usage.bits(), ); if let Some(label) = desc.label { mapped_desc.set_label(label);