Skip to content

Commit 57a95d0

Browse files
committed
Rework to pass in A::SurfaceTexture references
1 parent e7b7246 commit 57a95d0

File tree

13 files changed

+48
-118
lines changed

13 files changed

+48
-118
lines changed

wgpu-core/src/device/queue.rs

Lines changed: 21 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -22,8 +22,9 @@ use crate::{
2222
resource_log, track, FastHashMap, SubmissionIndex,
2323
};
2424

25-
use hal::{CommandEncoder as _, Device as _, Queue as _, RawSet as _};
25+
use hal::{CommandEncoder as _, Device as _, Queue as _};
2626
use parking_lot::Mutex;
27+
use smallvec::SmallVec;
2728

2829
use std::{
2930
iter, mem, ptr,
@@ -227,20 +228,6 @@ impl<A: HalApi> PendingWrites<A> {
227228
.push(TempResource::StagingBuffer(buffer));
228229
}
229230

230-
#[must_use]
231-
fn pre_submit(&mut self) -> Option<&A::CommandBuffer> {
232-
self.dst_buffers.clear();
233-
self.dst_textures.clear();
234-
if self.is_active {
235-
let cmd_buf = unsafe { self.command_encoder.end_encoding().unwrap() };
236-
self.is_active = false;
237-
self.executing_command_buffers.push(cmd_buf);
238-
self.executing_command_buffers.last()
239-
} else {
240-
None
241-
}
242-
}
243-
244231
#[must_use]
245232
fn post_submit(
246233
&mut self,
@@ -1116,15 +1103,12 @@ impl<G: GlobalIdentityHandlerFactory> Global<G> {
11161103
+ 1;
11171104
let mut active_executions = Vec::new();
11181105

1119-
// SAFETY: We're constructing this during the submission phase,
1120-
// where all resources it uses are guaranteed to outlive this
1121-
// short-lived set.
1122-
let mut submit_surface_textures = A::SubmitSurfaceTextureSet::new();
1123-
11241106
let mut used_surface_textures = track::TextureUsageScope::new();
11251107

11261108
let snatch_guard = device.snatchable_lock.read();
11271109

1110+
let mut submit_surface_textures = SmallVec::<[_; 2]>::new();
1111+
11281112
{
11291113
let mut command_buffer_guard = hub.command_buffers.write();
11301114

@@ -1231,9 +1215,7 @@ impl<G: GlobalIdentityHandlerFactory> Global<G> {
12311215
has_work.store(true, Ordering::Relaxed);
12321216

12331217
if let Some(raw) = raw {
1234-
unsafe {
1235-
submit_surface_textures.insert(raw);
1236-
}
1218+
submit_surface_textures.push(raw);
12371219
}
12381220

12391221
true
@@ -1434,9 +1416,7 @@ impl<G: GlobalIdentityHandlerFactory> Global<G> {
14341416
has_work.store(true, Ordering::Relaxed);
14351417

14361418
if let Some(raw) = raw {
1437-
unsafe {
1438-
submit_surface_textures.insert(raw);
1439-
}
1419+
submit_surface_textures.push(raw);
14401420
}
14411421

14421422
unsafe {
@@ -1468,8 +1448,18 @@ impl<G: GlobalIdentityHandlerFactory> Global<G> {
14681448
}
14691449
}
14701450

1471-
let refs = pending_writes
1472-
.pre_submit()
1451+
pending_writes.dst_buffers.clear();
1452+
1453+
let refs = if pending_writes.is_active {
1454+
let cmd_buf = unsafe { pending_writes.command_encoder.end_encoding().unwrap() };
1455+
pending_writes.is_active = false;
1456+
pending_writes.executing_command_buffers.push(cmd_buf);
1457+
pending_writes.executing_command_buffers.last()
1458+
} else {
1459+
None
1460+
};
1461+
1462+
let refs = refs
14731463
.into_iter()
14741464
.chain(
14751465
active_executions
@@ -1487,6 +1477,9 @@ impl<G: GlobalIdentityHandlerFactory> Global<G> {
14871477
.map_err(DeviceError::from)?;
14881478
}
14891479

1480+
// We clear this here, since dst_textures is in use during the call to submit.
1481+
pending_writes.dst_textures.clear();
1482+
14901483
profiling::scope!("cleanup");
14911484
if let Some(pending_execution) = pending_writes.post_submit(
14921485
device.command_allocator.lock().as_mut().unwrap(),

wgpu-core/src/resource.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -399,7 +399,7 @@ impl<A: HalApi> Drop for Buffer<A> {
399399
}
400400

401401
impl<A: HalApi> Buffer<A> {
402-
pub(crate) fn raw(&self, guard: &SnatchGuard) -> Option<&A::Buffer> {
402+
pub(crate) fn raw<'snatch>(&self, guard: &'snatch SnatchGuard) -> Option<&'snatch A::Buffer> {
403403
self.raw.get(guard)
404404
}
405405

wgpu-core/src/snatch.rs

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -25,8 +25,15 @@ impl<T> Snatchable<T> {
2525
}
2626
}
2727

28-
/// Get read access to the value. Requires a the snatchable lock's read guard.
29-
pub fn get(&self, _guard: &SnatchGuard) -> Option<&T> {
28+
/// Get read access to the value. Requires a the snatchable lock's read
29+
/// guard.
30+
///
31+
/// Note that this returns a reference with a lifetime matching
32+
/// `SnatchGuard`, which ensures that the returned reference doesn't outlive
33+
/// the guard it's indirectly projected from. It is very important that the
34+
/// resource doesn't use the incorrect snatch guard, since that could result
35+
/// in the resource being modified while it's in use.
36+
pub fn get<'snatch>(&self, _guard: &'snatch SnatchGuard) -> Option<&'snatch T> {
3037
unsafe { (*self.value.get()).as_ref() }
3138
}
3239

wgpu-hal/examples/halmark/main.rs

Lines changed: 4 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -3,8 +3,7 @@
33
extern crate wgpu_hal as hal;
44

55
use hal::{
6-
Adapter as _, CommandEncoder as _, Device as _, Instance as _, Queue as _, RawSet as _,
7-
Surface as _,
6+
Adapter as _, CommandEncoder as _, Device as _, Instance as _, Queue as _, Surface as _,
87
};
98
use raw_window_handle::{HasDisplayHandle, HasWindowHandle};
109
use winit::{
@@ -490,13 +489,8 @@ impl<A: hal::Api> Example<A> {
490489
let fence = unsafe {
491490
let mut fence = device.create_fence().unwrap();
492491
let init_cmd = cmd_encoder.end_encoding().unwrap();
493-
let surface_textures = A::SubmitSurfaceTextureSet::new();
494492
queue
495-
.submit(
496-
&[&init_cmd],
497-
&surface_textures,
498-
Some((&mut fence, init_fence_value)),
499-
)
493+
.submit(&[&init_cmd], &[], Some((&mut fence, init_fence_value)))
500494
.unwrap();
501495
device.wait(&fence, init_fence_value, !0).unwrap();
502496
device.destroy_buffer(staging_buffer);
@@ -547,13 +541,8 @@ impl<A: hal::Api> Example<A> {
547541
unsafe {
548542
{
549543
let ctx = &mut self.contexts[self.context_index];
550-
let surface_textures = A::SubmitSurfaceTextureSet::new();
551544
self.queue
552-
.submit(
553-
&[],
554-
&surface_textures,
555-
Some((&mut ctx.fence, ctx.fence_value)),
556-
)
545+
.submit(&[], &[], Some((&mut ctx.fence, ctx.fence_value)))
557546
.unwrap();
558547
}
559548

@@ -740,10 +729,8 @@ impl<A: hal::Api> Example<A> {
740729
} else {
741730
None
742731
};
743-
let mut surface_textures = A::SubmitSurfaceTextureSet::new();
744-
surface_textures.insert(&surface_tex);
745732
self.queue
746-
.submit(&[&cmd_buf], &surface_textures, fence_param)
733+
.submit(&[&cmd_buf], &[&surface_tex], fence_param)
747734
.unwrap();
748735
self.queue.present(&self.surface, surface_tex).unwrap();
749736
ctx.used_cmd_bufs.push(cmd_buf);

wgpu-hal/examples/raw-gles.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -183,6 +183,6 @@ fn fill_screen(exposed: &hal::ExposedAdapter<hal::api::Gles>, width: u32, height
183183
encoder.begin_render_pass(&rp_desc);
184184
encoder.end_render_pass();
185185
let cmd_buf = encoder.end_encoding().unwrap();
186-
od.queue.submit(&[&cmd_buf], &(), None).unwrap();
186+
od.queue.submit(&[&cmd_buf], &[], None).unwrap();
187187
}
188188
}

wgpu-hal/examples/ray-traced-triangle/main.rs

Lines changed: 4 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,7 @@
11
extern crate wgpu_hal as hal;
22

33
use hal::{
4-
Adapter as _, CommandEncoder as _, Device as _, Instance as _, Queue as _, RawSet as _,
5-
Surface as _,
4+
Adapter as _, CommandEncoder as _, Device as _, Instance as _, Queue as _, Surface as _,
65
};
76
use raw_window_handle::{HasDisplayHandle, HasWindowHandle};
87

@@ -755,13 +754,8 @@ impl<A: hal::Api> Example<A> {
755754
let fence = unsafe {
756755
let mut fence = device.create_fence().unwrap();
757756
let init_cmd = cmd_encoder.end_encoding().unwrap();
758-
let surface_textures = A::SubmitSurfaceTextureSet::new();
759757
queue
760-
.submit(
761-
&[&init_cmd],
762-
&surface_textures,
763-
Some((&mut fence, init_fence_value)),
764-
)
758+
.submit(&[&init_cmd], &[], Some((&mut fence, init_fence_value)))
765759
.unwrap();
766760
device.wait(&fence, init_fence_value, !0).unwrap();
767761
cmd_encoder.reset_all(iter::once(init_cmd));
@@ -966,10 +960,8 @@ impl<A: hal::Api> Example<A> {
966960
} else {
967961
None
968962
};
969-
let mut surface_textures = A::SubmitSurfaceTextureSet::new();
970-
surface_textures.insert(&surface_tex);
971963
self.queue
972-
.submit(&[&cmd_buf], &surface_textures, fence_param)
964+
.submit(&[&cmd_buf], &[&surface_tex], fence_param)
973965
.unwrap();
974966
self.queue.present(&self.surface, surface_tex).unwrap();
975967
ctx.used_cmd_bufs.push(cmd_buf);
@@ -1008,13 +1000,8 @@ impl<A: hal::Api> Example<A> {
10081000
unsafe {
10091001
{
10101002
let ctx = &mut self.contexts[self.context_index];
1011-
let surface_textures = A::SubmitSurfaceTextureSet::new();
10121003
self.queue
1013-
.submit(
1014-
&[],
1015-
&surface_textures,
1016-
Some((&mut ctx.fence, ctx.fence_value)),
1017-
)
1004+
.submit(&[], &[], Some((&mut ctx.fence, ctx.fence_value)))
10181005
.unwrap();
10191006
}
10201007

wgpu-hal/src/dx12/mod.rs

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -84,7 +84,6 @@ impl crate::Api for Api {
8484
type ComputePipeline = ComputePipeline;
8585

8686
type AccelerationStructure = AccelerationStructure;
87-
type SubmitSurfaceTextureSet = ();
8887
}
8988

9089
// Limited by D3D12's root signature size of 64. Each element takes 1 or 2 entries.
@@ -878,7 +877,7 @@ impl crate::Queue<Api> for Queue {
878877
unsafe fn submit(
879878
&self,
880879
command_buffers: &[&CommandBuffer],
881-
_surface_textures: &(),
880+
_surface_textures: &[&Texture],
882881
signal_fence: Option<(&mut Fence, crate::FenceValue)>,
883882
) -> Result<(), crate::DeviceError> {
884883
let mut temp_lists = self.temp_lists.lock();

wgpu-hal/src/empty.rs

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,6 @@ impl crate::Api for Api {
3737
type ShaderModule = Resource;
3838
type RenderPipeline = Resource;
3939
type ComputePipeline = Resource;
40-
type SubmitSurfaceTextureSet = ();
4140
}
4241

4342
impl crate::Instance<Api> for Context {
@@ -105,7 +104,7 @@ impl crate::Queue<Api> for Context {
105104
unsafe fn submit(
106105
&self,
107106
command_buffers: &[&Resource],
108-
surface_textures: &(),
107+
surface_textures: &[&Resource],
109108
signal_fence: Option<(&mut Resource, crate::FenceValue)>,
110109
) -> DeviceResult<()> {
111110
Ok(())

wgpu-hal/src/gles/mod.rs

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -161,7 +161,6 @@ impl crate::Api for Api {
161161
type ShaderModule = ShaderModule;
162162
type RenderPipeline = RenderPipeline;
163163
type ComputePipeline = ComputePipeline;
164-
type SubmitSurfaceTextureSet = ();
165164
}
166165

167166
bitflags::bitflags! {

wgpu-hal/src/gles/queue.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1748,7 +1748,7 @@ impl crate::Queue<super::Api> for super::Queue {
17481748
unsafe fn submit(
17491749
&self,
17501750
command_buffers: &[&super::CommandBuffer],
1751-
_surface_textures: &(),
1751+
_surface_textures: &[&super::Texture],
17521752
signal_fence: Option<(&mut super::Fence, crate::FenceValue)>,
17531753
) -> Result<(), crate::DeviceError> {
17541754
let shared = Arc::clone(&self.shared);

0 commit comments

Comments
 (0)