Skip to content

Commit

Permalink
Track a GL fence's last_completed better, ensuring we don't query the…
Browse files Browse the repository at this point in the history
… same sync object more than once
  • Loading branch information
Dinnerbone committed Oct 20, 2024
1 parent 840de2d commit e227c30
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 11 deletions.
21 changes: 14 additions & 7 deletions wgpu-hal/src/gles/device.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ use std::{
sync::{Arc, Mutex},
};

use crate::AtomicFenceValue;
use arrayvec::ArrayVec;
use std::sync::atomic::Ordering;

Expand Down Expand Up @@ -1534,7 +1535,7 @@ impl crate::Device for super::Device {
unsafe fn create_fence(&self) -> Result<super::Fence, crate::DeviceError> {
self.counters.fences.add(1);
Ok(super::Fence {
last_completed: 0,
last_completed: AtomicFenceValue::new(0),
pending: Vec::new(),
})
}
Expand All @@ -1560,7 +1561,7 @@ impl crate::Device for super::Device {
wait_value: crate::FenceValue,
timeout_ms: u32,
) -> Result<bool, crate::DeviceError> {
if fence.last_completed < wait_value {
if fence.last_completed.load(Ordering::Relaxed) < wait_value {
let gl = &self.shared.context.lock();
let timeout_ns = if cfg!(any(webgl, Emscripten)) {
0
Expand All @@ -1572,19 +1573,25 @@ impl crate::Device for super::Device {
.iter()
.find(|&&(value, _)| value >= wait_value)
{
return match unsafe {
let signalled = match unsafe {
gl.client_wait_sync(sync, glow::SYNC_FLUSH_COMMANDS_BIT, timeout_ns as i32)
} {
// for some reason firefox returns WAIT_FAILED, to investigate
#[cfg(any(webgl, Emscripten))]
glow::WAIT_FAILED => {
log::warn!("wait failed!");
Ok(false)
false
}
glow::TIMEOUT_EXPIRED => Ok(false),
glow::CONDITION_SATISFIED | glow::ALREADY_SIGNALED => Ok(true),
_ => Err(crate::DeviceError::Lost),
glow::TIMEOUT_EXPIRED => false,
glow::CONDITION_SATISFIED | glow::ALREADY_SIGNALED => true,
_ => return Err(crate::DeviceError::Lost),
};
if signalled {
fence
.last_completed
.fetch_max(wait_value, Ordering::Relaxed);
}
return Ok(signalled);
}
}
Ok(true)
Expand Down
11 changes: 7 additions & 4 deletions wgpu-hal/src/gles/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -120,7 +120,7 @@ use glow::HasContext;

use naga::FastHashMap;
use parking_lot::Mutex;
use std::sync::atomic::{AtomicU32, AtomicU8};
use std::sync::atomic::{AtomicU32, AtomicU8, Ordering};
use std::{fmt, ops::Range, sync::Arc};

#[derive(Clone, Debug)]
Expand Down Expand Up @@ -718,7 +718,7 @@ impl crate::DynQuerySet for QuerySet {}

#[derive(Debug)]
pub struct Fence {
last_completed: crate::FenceValue,
last_completed: crate::AtomicFenceValue,
pending: Vec<(crate::FenceValue, glow::Fence)>,
}

Expand All @@ -743,7 +743,7 @@ unsafe impl Sync for Fence {}

impl Fence {
fn get_latest(&self, gl: &glow::Context) -> crate::FenceValue {
let mut max_value = self.last_completed;
let mut max_value = self.last_completed.load(Ordering::Relaxed);
for &(value, sync) in self.pending.iter() {
if value <= max_value {
// We already know this was good, no need to check again
Expand All @@ -757,6 +757,10 @@ impl Fence {
break;
}
}

// Track the latest value, to save ourselves some querying later
self.last_completed.fetch_max(max_value, Ordering::Relaxed);

max_value
}

Expand All @@ -770,7 +774,6 @@ impl Fence {
}
}
self.pending.retain(|&(value, _)| value > latest);
self.last_completed = latest;
}
}

Expand Down

0 comments on commit e227c30

Please sign in to comment.