[metal] simplify fence tracking by removing pending command buffers (#10570)

Co-authored-by: Connor Fitzgerald <connorwadefitzgerald@gmail.com>
This commit is contained in:
Abel LucasandConnor Fitzgerald authored and GitHub committed 2026-11-06 14:20:04 +00:00
1 parent 8d49278285
commit b031e60a4d
2 files changed
+11 -52

No files matched your search

+1 -13
View File
@@ -22,7 +22,7 @@ use objc2_metal::{
MTLTexture, MTLTextureDescriptor, MTLTextureType, MTLTextureUsage, MTLTriangleFillMode,
MTLVertexDescriptor, MTLVertexStepFunction,
};
use wgpu_sync::{Condvar, CondvarMutex, RwLock};
use wgpu_sync::{Condvar, CondvarMutex};
use super::{adapter::MAX_BUFFERS, conv, AttachmentInfo, PassthroughShader, ShaderModuleSource};
use crate::{auxil::map_naga_stage, DropCallback, DropGuard, TlasInstance};
@@ -2151,7 +2151,6 @@ impl crate::Device for super::Device {
};
Ok(super::Fence {
sync: Arc::new((CondvarMutex::new(0), Condvar::new())),
pending_command_buffers: RwLock::new(Vec::new()),
shared_event,
})
}
@@ -2176,17 +2175,6 @@ impl crate::Device for super::Device {
return Ok(true);
}
{
let pending_command_buffers = fence.pending_command_buffers.read();
if !pending_command_buffers
.iter()
.any(|&(value, _)| value >= wait_value)
{
log::error!("No active command buffers for fence value {wait_value}");
return Err(crate::DeviceError::Lost);
}
}
if let Some(timeout) = timeout {
let result = condvar.wait_while_for(&mut lock, |value| *value < wait_value, timeout);
if result.timed_out() {
+10 -39
View File
@@ -48,13 +48,12 @@ use objc2::{
use objc2_foundation::{ns_string, NSString};
use objc2_metal::{
MTLAccelerationStructure, MTLAccelerationStructureCommandEncoder, MTLArgumentBuffersTier,
MTLBlitCommandEncoder, MTLBuffer, MTLCommandBuffer, MTLCommandBufferStatus, MTLCommandQueue,
MTLComputeCommandEncoder, MTLComputePipelineState, MTLCounterSampleBuffer, MTLCullMode,
MTLDepthClipMode, MTLDepthStencilState, MTLDevice, MTLDrawable, MTLIndexType,
MTLLanguageVersion, MTLLibrary, MTLPrimitiveType, MTLReadWriteTextureTier,
MTLRenderCommandEncoder, MTLRenderPipelineState, MTLRenderStages, MTLResource,
MTLResourceUsage, MTLSamplerState, MTLSharedEvent, MTLSize, MTLTexture, MTLTextureType,
MTLTriangleFillMode, MTLWinding,
MTLBlitCommandEncoder, MTLBuffer, MTLCommandBuffer, MTLCommandQueue, MTLComputeCommandEncoder,
MTLComputePipelineState, MTLCounterSampleBuffer, MTLCullMode, MTLDepthClipMode,
MTLDepthStencilState, MTLDevice, MTLDrawable, MTLIndexType, MTLLanguageVersion, MTLLibrary,
MTLPrimitiveType, MTLReadWriteTextureTier, MTLRenderCommandEncoder, MTLRenderPipelineState,
MTLRenderStages, MTLResource, MTLResourceUsage, MTLSamplerState, MTLSharedEvent, MTLSize,
MTLTexture, MTLTextureType, MTLTriangleFillMode, MTLWinding,
};
use objc2_quartz_core::CAMetalLayer;
use wgpu_sync::{atomic, Condvar, Mutex, OnceCell, RwLock};
@@ -789,7 +788,9 @@ impl crate::Queue for Queue {
let extra_command_buffer = {
let fence_sync = Arc::clone(&signal_fence.sync);
let block = block2::RcBlock::new(move |_cmd_buf| {
*fence_sync.0.lock() = signal_value;
let mut value = fence_sync.0.lock();
*value = (*value).max(signal_value);
drop(value);
fence_sync.1.notify_all();
});
@@ -807,12 +808,6 @@ impl crate::Queue for Queue {
raw.setLabel(Some(ns_string!("(wgpu internal) Signal")));
unsafe { raw.addCompletedHandler(block2::RcBlock::as_ptr(&block)) };
signal_fence.maintain();
signal_fence
.pending_command_buffers
.write()
.push((signal_value, raw.clone()));
if let Some(shared_event) = &signal_fence.shared_event {
raw.encodeSignalEvent_value(shared_event.as_ref(), signal_value);
}
@@ -1313,16 +1308,9 @@ unsafe impl Sync for QuerySet {}
#[derive(Debug)]
pub struct Fence {
sync: Arc<(wgpu_sync::CondvarMutex<crate::FenceValue>, Condvar)>,
/// The pending fence values have to be ascending.
pending_command_buffers: RwLock<Vec<PendingCommandBuffer>>,
shared_event: Option<Retained<ProtocolObject<dyn MTLSharedEvent>>>,
}
type PendingCommandBuffer = (
crate::FenceValue,
Retained<ProtocolObject<dyn MTLCommandBuffer>>,
);
impl crate::DynFence for Fence {}
unsafe impl Send for Fence {}
@@ -1330,24 +1318,7 @@ unsafe impl Sync for Fence {}
impl Fence {
fn get_latest(&self) -> crate::FenceValue {
let mut max_value = *self.sync.0.lock();
let pending_command_buffers = self.pending_command_buffers.read();
for &(value, ref cmd_buf) in pending_command_buffers.iter() {
match cmd_buf.status() {
MTLCommandBufferStatus::Completed | MTLCommandBufferStatus::Error => {
max_value = value;
}
_ => {}
}
}
max_value
}
fn maintain(&self) {
let latest = self.get_latest();
self.pending_command_buffers
.write()
.retain(|&(value, _)| value > latest);
*self.sync.0.lock()
}
pub fn raw_shared_event(&self) -> Option<&ProtocolObject<dyn MTLSharedEvent>> {