Skip to content

Commit cc8ba75

Browse files
committed
hal/vulkan: use different indexes for acquire and present
1 parent 4f18c26 commit cc8ba75

File tree

4 files changed

+198
-96
lines changed

4 files changed

+198
-96
lines changed

CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -44,6 +44,7 @@ Bottom level categories:
4444

4545
### Bug Fixes
4646

47+
- Fixed vulkan validation error regarding the swapchain in latest SDK. By @cwfitzgerald in [#7971](https://github.com/gfx-rs/wgpu/pull/7971).
4748
- Fixed flickering on AMD devices and crashes inside Renderdoc due to incorrect caching of `VkFramebuffer`s when the driver re-used image view handles. By @cwfitzgerald in [#7972](https://github.com/gfx-rs/wgpu/pull/7972).
4849
> [!WARNING]
4950
> There is formally a breaking change in `wgpu_hal::vulkan::Device::texture_from_raw` as there is now a `&self` receiver where

wgpu-hal/src/vulkan/device.rs

Lines changed: 11 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -573,25 +573,29 @@ impl super::Device {
573573
let images =
574574
unsafe { functor.get_swapchain_images(raw) }.map_err(super::map_host_device_oom_err)?;
575575

576-
// NOTE: It's important that we define at least images.len() wait
577-
// semaphores, since we prospectively need to provide the call to
578-
// acquire the next image with an unsignaled semaphore.
579-
let surface_semaphores = (0..=images.len())
576+
// NOTE: It's important that we define the same number of acquire/present semaphores
577+
// as we will need to index into them with the image index.
578+
let acquire_semaphores = (0..=images.len())
580579
.map(|i| {
581-
super::SwapchainImageSemaphores::new(&self.shared, i)
580+
super::SwapchainAcquireSemaphore::new(&self.shared, i)
582581
.map(Mutex::new)
583582
.map(Arc::new)
584583
})
585584
.collect::<Result<Vec<_>, _>>()?;
586585

586+
let present_semaphores = (0..=images.len())
587+
.map(|i| Arc::new(Mutex::new(super::SwapchainPresentSemaphores::new(i))))
588+
.collect::<Vec<_>>();
589+
587590
Ok(super::Swapchain {
588591
raw,
589592
functor,
590593
device: Arc::clone(&self.shared),
591594
images,
592595
config: config.clone(),
593-
surface_semaphores,
594-
next_semaphore_index: 0,
596+
acquire_semaphores,
597+
next_acquire_index: 0,
598+
present_semaphores,
595599
next_present_time: None,
596600
})
597601
}

wgpu-hal/src/vulkan/instance.rs

Lines changed: 21 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -188,9 +188,18 @@ impl super::Swapchain {
188188
};
189189

190190
// We cannot take this by value, as the function returns `self`.
191-
for semaphore in self.surface_semaphores.drain(..) {
191+
for semaphore in self.acquire_semaphores.drain(..) {
192192
let arc_removed = Arc::into_inner(semaphore).expect(
193-
"Trying to destroy a SurfaceSemaphores that is still in use by a SurfaceTexture",
193+
"Trying to destroy a SurfaceAcquireSemaphores that is still in use by a SurfaceTexture",
194+
);
195+
let mutex_removed = arc_removed.into_inner();
196+
197+
unsafe { mutex_removed.destroy(device) };
198+
}
199+
200+
for semaphore in self.present_semaphores.drain(..) {
201+
let arc_removed = Arc::into_inner(semaphore).expect(
202+
"Trying to destroy a SurfacePresentSemaphores that is still in use by a SurfaceTexture",
194203
);
195204
let mutex_removed = arc_removed.into_inner();
196205

@@ -1074,9 +1083,9 @@ impl crate::Surface for super::Surface {
10741083
timeout_ns = u64::MAX;
10751084
}
10761085

1077-
let swapchain_semaphores_arc = swapchain.get_surface_semaphores();
1086+
let acquire_semaphore_arc = swapchain.get_acquire_semaphore();
10781087
// Nothing should be using this, so we don't block, but panic if we fail to lock.
1079-
let locked_swapchain_semaphores = swapchain_semaphores_arc
1088+
let acquire_semaphore_guard = acquire_semaphore_arc
10801089
.try_lock()
10811090
.expect("Failed to lock a SwapchainSemaphores.");
10821091

@@ -1095,7 +1104,7 @@ impl crate::Surface for super::Surface {
10951104
// `vkAcquireNextImageKHR` again.
10961105
swapchain.device.wait_for_fence(
10971106
fence,
1098-
locked_swapchain_semaphores.previously_used_submission_index,
1107+
acquire_semaphore_guard.previously_used_submission_index,
10991108
timeout_ns,
11001109
)?;
11011110

@@ -1105,7 +1114,7 @@ impl crate::Surface for super::Surface {
11051114
swapchain.functor.acquire_next_image(
11061115
swapchain.raw,
11071116
timeout_ns,
1108-
locked_swapchain_semaphores.acquire,
1117+
acquire_semaphore_guard.acquire,
11091118
vk::Fence::null(),
11101119
)
11111120
} {
@@ -1129,12 +1138,12 @@ impl crate::Surface for super::Surface {
11291138
}
11301139
};
11311140

1132-
log::error!("Got swapchain image {index}");
1133-
1134-
drop(locked_swapchain_semaphores);
1141+
drop(acquire_semaphore_guard);
11351142
// We only advance the surface semaphores if we successfully acquired an image, otherwise
11361143
// we should try to re-acquire using the same semaphores.
1137-
swapchain.advance_surface_semaphores();
1144+
swapchain.advance_acquire_semaphore();
1145+
1146+
let present_semaphore_arc = swapchain.get_present_semaphores(index);
11381147

11391148
// special case for Intel Vulkan returning bizarre values (ugh)
11401149
if swapchain.device.vendor_id == crate::auxil::db::intel::VENDOR && index > 0x100 {
@@ -1158,7 +1167,8 @@ impl crate::Surface for super::Surface {
11581167
},
11591168
identity,
11601169
},
1161-
surface_semaphores: swapchain_semaphores_arc,
1170+
acquire_semaphores: acquire_semaphore_arc,
1171+
present_semaphores: present_semaphore_arc,
11621172
};
11631173
Ok(Some(crate::AcquiredSurfaceTexture {
11641174
texture,

0 commit comments

Comments
 (0)