Skip to content

Commit a0f492b

Browse files
authored
Fix CI for wasm atomics (#12730)
# Objective CI is currently broken because of `DiagnosticsRecorder` not being Send and Sync as required by Resource. ## Solution Wrap `DiagnosticsRecorder` internally with a `WgpuWrapper`.
1 parent 31d9146 commit a0f492b

File tree

1 file changed

+30
-18
lines changed

1 file changed

+30
-18
lines changed

crates/bevy_render/src/diagnostic/internal.rs

Lines changed: 30 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@ use wgpu::{
1717
PipelineStatisticsTypes, QuerySet, QuerySetDescriptor, QueryType, Queue, RenderPass,
1818
};
1919

20-
use crate::renderer::RenderDevice;
20+
use crate::renderer::{RenderDevice, WgpuWrapper};
2121

2222
use super::RecordDiagnostics;
2323

@@ -28,17 +28,19 @@ const MAX_PIPELINE_STATISTICS: u32 = 128;
2828
const TIMESTAMP_SIZE: u64 = 8;
2929
const PIPELINE_STATISTICS_SIZE: u64 = 40;
3030

31-
/// Records diagnostics into [`QuerySet`]'s keeping track of the mapping between
32-
/// spans and indices to the corresponding entries in the [`QuerySet`].
33-
#[derive(Resource)]
34-
pub struct DiagnosticsRecorder {
31+
struct DiagnosticsRecorderInternal {
3532
timestamp_period_ns: f32,
3633
features: Features,
3734
current_frame: Mutex<FrameData>,
3835
submitted_frames: Vec<FrameData>,
3936
finished_frames: Vec<FrameData>,
4037
}
4138

39+
/// Records diagnostics into [`QuerySet`]'s keeping track of the mapping between
40+
/// spans and indices to the corresponding entries in the [`QuerySet`].
41+
#[derive(Resource)]
42+
pub struct DiagnosticsRecorder(WgpuWrapper<DiagnosticsRecorderInternal>);
43+
4244
impl DiagnosticsRecorder {
4345
/// Creates the new `DiagnosticsRecorder`.
4446
pub fn new(device: &RenderDevice, queue: &Queue) -> DiagnosticsRecorder {
@@ -50,30 +52,32 @@ impl DiagnosticsRecorder {
5052
0.0
5153
};
5254

53-
DiagnosticsRecorder {
55+
DiagnosticsRecorder(WgpuWrapper::new(DiagnosticsRecorderInternal {
5456
timestamp_period_ns,
5557
features,
5658
current_frame: Mutex::new(FrameData::new(device, features)),
5759
submitted_frames: Vec::new(),
5860
finished_frames: Vec::new(),
59-
}
61+
}))
6062
}
6163

6264
fn current_frame_mut(&mut self) -> &mut FrameData {
63-
self.current_frame.get_mut().expect("lock poisoned")
65+
self.0.current_frame.get_mut().expect("lock poisoned")
6466
}
6567

6668
fn current_frame_lock(&self) -> impl DerefMut<Target = FrameData> + '_ {
67-
self.current_frame.lock().expect("lock poisoned")
69+
self.0.current_frame.lock().expect("lock poisoned")
6870
}
6971

7072
/// Begins recording diagnostics for a new frame.
7173
pub fn begin_frame(&mut self) {
74+
let internal = &mut self.0;
7275
let mut idx = 0;
73-
while idx < self.submitted_frames.len() {
74-
if self.submitted_frames[idx].run_mapped_callback(self.timestamp_period_ns) {
75-
self.finished_frames
76-
.push(self.submitted_frames.swap_remove(idx));
76+
while idx < internal.submitted_frames.len() {
77+
let timestamp = internal.timestamp_period_ns;
78+
if internal.submitted_frames[idx].run_mapped_callback(timestamp) {
79+
let removed = internal.submitted_frames.swap_remove(idx);
80+
internal.finished_frames.push(removed);
7781
} else {
7882
idx += 1;
7983
}
@@ -100,16 +104,24 @@ impl DiagnosticsRecorder {
100104
device: &RenderDevice,
101105
callback: impl FnOnce(RenderDiagnostics) + Send + Sync + 'static,
102106
) {
103-
self.current_frame_mut().finish(callback);
107+
let internal = &mut self.0;
108+
internal
109+
.current_frame
110+
.get_mut()
111+
.expect("lock poisoned")
112+
.finish(callback);
104113

105114
// reuse one of the finished frames, if we can
106-
let new_frame = match self.finished_frames.pop() {
115+
let new_frame = match internal.finished_frames.pop() {
107116
Some(frame) => frame,
108-
None => FrameData::new(device, self.features),
117+
None => FrameData::new(device, internal.features),
109118
};
110119

111-
let old_frame = std::mem::replace(self.current_frame_mut(), new_frame);
112-
self.submitted_frames.push(old_frame);
120+
let old_frame = std::mem::replace(
121+
internal.current_frame.get_mut().expect("lock poisoned"),
122+
new_frame,
123+
);
124+
internal.submitted_frames.push(old_frame);
113125
}
114126
}
115127

0 commit comments

Comments
 (0)