Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -407,6 +407,9 @@ By @cwfitzgerald in [#8388](https://github.com/gfx-rs/wgpu/pull/8388).
- Polyfill workgroup zero init in mesh and task shaders on AMD's proprietary driver, which fails with `VK_ERROR_INITIALIZATION_FAILED` when they use `OpVariable` initializers. By @apollo-2006 in [#10692](https://github.com/gfx-rs/wgpu/pull/10692).
- Fix a fence data race between submitting and polling on devices without timeline semaphores. By @JasperDeSutter in [#10960](https://github.com/gfx-rs/wgpu/pull/10960).
- Fix pass timestamp queries on Vulkan sometimes reporting a duration of almost zero for a render or compute pass, when the GPU was busy with other work. By @kikijiki in [#10837](https://github.com/gfx-rs/wgpu/pull/10837).
- Skip direct compute dispatches that have a zero dimension instead of passing them to the driver. Arm's proprietary Mali driver (seen with `r20p0` on a Mali-G52) stalled for about six seconds on each one and lost the device for `dispatch_workgroups(0, 0, 0)`. By @matthargett in [#XXXXX](https://github.com/gfx-rs/wgpu/pull/XXXXX).
- Work around Arm's proprietary Mali driver losing the device when a render pass has a depth-stencil attachment with read-only depth and writable stencil (seen with `r20p0` on a Mali-G52 and `depth24plus-stencil8`). On Arm drivers up to `r20`, wgpu now uses the `DEPTH_STENCIL_ATTACHMENT_OPTIMAL` layout in place of `DEPTH_READ_ONLY_STENCIL_ATTACHMENT_OPTIMAL`. By @matthargett in [#XXXXX](https://github.com/gfx-rs/wgpu/pull/XXXXX).
- Clamp the reported `max_compute_workgroups_per_dimension` to the product of the three workgroup size limits. Arm's proprietary Mali driver (seen with `r20p0` on a Mali-G52) reports `u32::MAX`, which left no larger value for validation to reject. By @matthargett in [#XXXXX](https://github.com/gfx-rs/wgpu/pull/XXXXX).

#### Metal

Expand Down
56 changes: 53 additions & 3 deletions wgpu-hal/src/vulkan/adapter.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1936,9 +1936,23 @@ impl PhysicalDeviceProperties {
max_compute_workgroup_size_x: limits.max_compute_work_group_size[0],
max_compute_workgroup_size_y: limits.max_compute_work_group_size[1],
max_compute_workgroup_size_z: limits.max_compute_work_group_size[2],
max_compute_workgroups_per_dimension: limits.max_compute_work_group_count[0]
.min(limits.max_compute_work_group_count[1])
.min(limits.max_compute_work_group_count[2]),
max_compute_workgroups_per_dimension: {
// WebGPU expects this limit to be no larger than the product of the
// three workgroup size limits, and expects `limit + 1` to be a value
// that `dispatchWorkgroups` can be called with and reject. Some
// drivers report `u32::MAX` workgroups per dimension (Arm Mali
// `r20p0`, with a size limit of 384 per dimension), which satisfies
// neither.
let max_workgroup_size_product = limits
.max_compute_work_group_size
.iter()
.fold(1u32, |product, &size| product.saturating_mul(size));
limits.max_compute_work_group_count[0]
.min(limits.max_compute_work_group_count[1])
.min(limits.max_compute_work_group_count[2])
.min(max_workgroup_size_product)
.min(u32::MAX - 1)
},
max_immediate_size: limits.max_push_constants_size,
//
// NATIVE (Non-WebGPU) LIMITS:
Expand Down Expand Up @@ -2546,6 +2560,11 @@ impl super::Instance {
phd_capabilities.is_driver(vk::DriverId::ARM_PROPRIETARY)
&& arm_driver_leaks_timeline_semaphores(&info.driver_info),
);
workarounds.set(
super::Workarounds::AVOID_DEPTH_READ_ONLY_STENCIL_ATTACHMENT_LAYOUT,
phd_capabilities.is_driver(vk::DriverId::ARM_PROPRIETARY)
&& arm_driver_breaks_depth_read_only_stencil_attachment(&info.driver_info),
);
};

if let Some(driver) = phd_capabilities.driver {
Expand Down Expand Up @@ -3777,6 +3796,21 @@ fn arm_driver_leaks_timeline_semaphores(driver_info: &str) -> bool {
}
}

/// Returns `true` if Arm's proprietary driver loses the device when a draw writes stencil
/// through an attachment in the `DEPTH_READ_ONLY_STENCIL_ATTACHMENT_OPTIMAL` layout.
///
/// Observed with `r20`. Earlier `r` releases are assumed to share the defect; later ones have
/// not been tested. If `driver_info` does not start with `v1.r<N>p`, this returns `false`,
/// because the replacement layout is not valid Vulkan when the depth aspect is sampled.
fn arm_driver_breaks_depth_read_only_stencil_attachment(driver_info: &str) -> bool {
const LAST_KNOWN_AFFECTED: u32 = 20;
driver_info
.strip_prefix("v1.r")
.and_then(|rest| rest.split_once('p'))
.and_then(|(number, _)| number.parse::<u32>().ok())
.is_some_and(|n| n <= LAST_KNOWN_AFFECTED)
}

/// Convert Vulkan component type to wgt::CooperativeScalarType.
fn map_vk_component_type(ty: vk::ComponentTypeKHR) -> Option<wgt::CooperativeScalarType> {
match ty {
Expand Down Expand Up @@ -3903,8 +3937,24 @@ fn query_cooperative_matrix_properties(

#[cfg(test)]
mod tests {
use super::arm_driver_breaks_depth_read_only_stencil_attachment as breaks_layout;
use super::arm_driver_leaks_timeline_semaphores as leaks;

#[test]
fn arm_depth_read_only_stencil_attachment_driver_versions() {
assert!(breaks_layout(
"v1.r20p0-01rel0.2ecdebafc539890b5b86a06d83f2e755"
));
assert!(breaks_layout("v1.r16p0-01rel0"));
assert!(!breaks_layout("v1.r21p0-01rel0"));
assert!(!breaks_layout("v1.r46p0-01eac0"));
assert!(!breaks_layout(
"v1.g18p0-01eac0.9e534f339477793d151cc7ab4c27f2c0"
));
assert!(!breaks_layout("?"));
assert!(!breaks_layout("v1.r20"));
}

#[test]
fn arm_timeline_semaphore_driver_versions() {
assert!(leaks("v1.g18p0-01eac0.9e534f339477793d151cc7ab4c27f2c0"));
Expand Down
32 changes: 25 additions & 7 deletions wgpu-hal/src/vulkan/command.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,13 +9,17 @@ const DST_IMAGE_LAYOUT: vk::ImageLayout = vk::ImageLayout::TRANSFER_DST_OPTIMAL;

/// The layout for `usage`. Identical to [`conv::derive_image_layout`] except that
/// `TextureUses::PRESENT` is `GENERAL` for [`super::PresentTransition::ReleaseToExternal`]
/// textures.
fn barrier_image_layout(texture: &super::Texture, usage: wgt::TextureUses) -> vk::ImageLayout {
/// textures, and that layouts ruled out by `workarounds` are replaced.
fn barrier_image_layout(
texture: &super::Texture,
usage: wgt::TextureUses,
workarounds: super::Workarounds,
) -> vk::ImageLayout {
match (usage, texture.present_transition) {
(wgt::TextureUses::PRESENT, super::PresentTransition::ReleaseToExternal) => {
vk::ImageLayout::GENERAL
}
_ => conv::derive_image_layout(usage, texture.format),
_ => workarounds.image_layout(conv::derive_image_layout(usage, texture.format)),
}
}

Expand Down Expand Up @@ -322,15 +326,17 @@ impl crate::CommandEncoder for super::CommandEncoder {
self.device.private_caps.store_op_none,
self.device.features,
);
let src_layout = barrier_image_layout(bar.texture, bar.usage.from);
let src_layout =
barrier_image_layout(bar.texture, bar.usage.from, self.device.workarounds);
src_stages |= src_stage;
let (dst_stage, dst_access) = conv::map_texture_usage_to_barrier(
bar.usage.to,
self.device.queue_flags,
self.device.private_caps.store_op_none,
self.device.features,
);
let dst_layout = barrier_image_layout(bar.texture, bar.usage.to);
let dst_layout =
barrier_image_layout(bar.texture, bar.usage.to, self.device.workarounds);
dst_stages |= dst_stage;

// Insert a queue family ownership transfer if the caller requested
Expand Down Expand Up @@ -923,10 +929,13 @@ impl crate::CommandEncoder for super::CommandEncoder {
color: unsafe { cat.make_vk_clear_color() },
});
let color = super::ColorAttachmentKey {
base: cat.target.make_attachment_key(cat.ops),
base: cat
.target
.make_attachment_key(cat.ops, self.device.workarounds),
resolve: cat.resolve_target.as_ref().map(|target| {
target.make_attachment_key(
crate::AttachmentOps::LOAD_CLEAR | crate::AttachmentOps::STORE,
self.device.workarounds,
)
}),
};
Expand All @@ -951,7 +960,9 @@ impl crate::CommandEncoder for super::CommandEncoder {
},
});
rp_key.depth_stencil = Some(super::DepthStencilAttachmentKey {
base: ds.target.make_attachment_key(ds.depth_ops),
base: ds
.target
.make_attachment_key(ds.depth_ops, self.device.workarounds),
stencil_ops: ds.stencil_ops,
});
fb_key.push_view(ds.target.view.identified_raw_view());
Expand Down Expand Up @@ -1450,6 +1461,13 @@ impl crate::CommandEncoder for super::CommandEncoder {
}

unsafe fn dispatch_workgroups(&mut self, count: [u32; 3]) {
// A dispatch with a zero dimension runs nothing, but not every driver
// treats it as a no-op: Arm's proprietary Mali driver (seen with r20p0
// on a Mali-G52) stalls for about six seconds per such dispatch, and
// loses the device for `[0, 0, 0]`.
if count.contains(&0) {
return;
}
unsafe {
self.device
.raw
Expand Down
24 changes: 22 additions & 2 deletions wgpu-hal/src/vulkan/conv.rs
Original file line number Diff line number Diff line change
Expand Up @@ -212,10 +212,14 @@ pub fn map_surface_color_space(color_space: wgt::SurfaceColorSpace) -> vk::Color
}

impl crate::Attachment<'_, super::TextureView> {
pub(super) fn make_attachment_key(&self, ops: crate::AttachmentOps) -> super::AttachmentKey {
pub(super) fn make_attachment_key(
&self,
ops: crate::AttachmentOps,
workarounds: super::Workarounds,
) -> super::AttachmentKey {
super::AttachmentKey {
format: self.view.raw_format,
layout: derive_image_layout(self.usage, self.view.format),
layout: workarounds.image_layout(derive_image_layout(self.usage, self.view.format)),
ops,
}
}
Expand Down Expand Up @@ -267,6 +271,22 @@ pub fn derive_image_layout(usage: wgt::TextureUses, format: wgt::TextureFormat)
}
}

impl super::Workarounds {
/// Returns the layout to use where wgpu would otherwise use `layout`.
///
/// Every layout that an image is transitioned to or used in as an attachment
/// must go through this, so that barriers and render passes agree.
pub(super) fn image_layout(self, layout: vk::ImageLayout) -> vk::ImageLayout {
if layout == vk::ImageLayout::DEPTH_READ_ONLY_STENCIL_ATTACHMENT_OPTIMAL
&& self.contains(Self::AVOID_DEPTH_READ_ONLY_STENCIL_ATTACHMENT_LAYOUT)
{
vk::ImageLayout::DEPTH_STENCIL_ATTACHMENT_OPTIMAL
Comment on lines +280 to +283

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

P1 Badge Preserve a sampleable layout for read-only depth attachments

On the affected Arm drivers, a valid render pass that samples its read-only depth aspect while writing stencil now transitions the image to DEPTH_STENCIL_ATTACHMENT_OPTIMAL, which does not permit sampling. wgpu-core/src/command/render.rs explicitly allows this combination, and the sampled-image descriptor created in wgpu-hal/src/vulkan/device.rs still declares a read-only layout. Consequently, safe API calls produce invalid Vulkan commands, causing validation errors and undefined behavior even if the tested Mali-G52 happens to render correctly. Use a Vulkan-valid fallback with consistent attachment, barrier, and descriptor layouts, or stop exposing the unsupported combination on these drivers.

AGENTS.md reference: AGENTS.md:L19-L19

Useful? React with 👍 / 👎.

} else {
layout
}
}
}

pub fn map_damage_rects(damage_rects: &[wgt::Rect<u32>]) -> Vec<vk::RectLayerKHR> {
damage_rects
.iter()
Expand Down
20 changes: 11 additions & 9 deletions wgpu-hal/src/vulkan/device.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2271,15 +2271,17 @@ impl crate::Device for super::Device {
ds.is_stencil_read_only(desc.primitive.cull_mode),
);
let vk_format = self.shared.private_caps.map_texture_format(ds.format);
let vk_layout = match (
compatible_rp_key.depth_read_only,
compatible_rp_key.stencil_read_only,
) {
(true, true) => vk::ImageLayout::DEPTH_STENCIL_READ_ONLY_OPTIMAL,
(true, false) => vk::ImageLayout::DEPTH_READ_ONLY_STENCIL_ATTACHMENT_OPTIMAL,
(false, true) => vk::ImageLayout::DEPTH_ATTACHMENT_STENCIL_READ_ONLY_OPTIMAL,
(false, false) => vk::ImageLayout::DEPTH_STENCIL_ATTACHMENT_OPTIMAL,
};
let vk_layout = self.shared.workarounds.image_layout(
match (
compatible_rp_key.depth_read_only,
compatible_rp_key.stencil_read_only,
) {
(true, true) => vk::ImageLayout::DEPTH_STENCIL_READ_ONLY_OPTIMAL,
(true, false) => vk::ImageLayout::DEPTH_READ_ONLY_STENCIL_ATTACHMENT_OPTIMAL,
(false, true) => vk::ImageLayout::DEPTH_ATTACHMENT_STENCIL_READ_ONLY_OPTIMAL,
(false, false) => vk::ImageLayout::DEPTH_STENCIL_ATTACHMENT_OPTIMAL,
},
);
compatible_rp_key.depth_stencil = Some(super::DepthStencilAttachmentKey {
base: super::AttachmentKey::compatible(vk_format, vk_layout),
stencil_ops: crate::AttachmentOps::all(),
Expand Down
19 changes: 19 additions & 0 deletions wgpu-hal/src/vulkan/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -611,6 +611,25 @@ bitflags::bitflags!(
/// [`Fence`]: super::Fence
/// [`FencePool`]: super::FencePool
const DISABLE_TIMELINE_SEMAPHORES = 1 << 4;
/// Never put a depth-stencil image in the
/// `DEPTH_READ_ONLY_STENCIL_ATTACHMENT_OPTIMAL` layout; use
/// `DEPTH_STENCIL_ATTACHMENT_OPTIMAL` in its place.
///
/// Arm's proprietary Mali driver loses the device when a draw writes stencil in a
/// render pass whose depth-stencil attachment is in that layout. Observed with
/// driver `v1.r20p0-01rel0` on a Mali-G52 MC2 (MT6768, Android 10) and a
/// `D24_UNORM_S8_UINT` attachment: the submission stalls for about nine seconds
/// and the device is lost, whether or not the depth aspect is sampled in the
/// pass. The same pass with the attachment in `DEPTH_STENCIL_ATTACHMENT_OPTIMAL`
/// renders correctly.
///
/// When the pass also samples the read-only depth aspect, the image is then
/// sampled while in an attachment layout, which Vulkan does not allow. Nothing
/// writes depth in such a pass, and the affected driver renders it correctly.
///
/// Set for Arm drivers up to and including `r20`, the only release known to be
/// affected. Later releases have not been tested.
const AVOID_DEPTH_READ_ONLY_STENCIL_ATTACHMENT_LAYOUT = 1 << 5;
}
);

Expand Down
Loading