Skip to content

Commit

Permalink
Rework to pass in A::SurfaceTexture references
Browse files Browse the repository at this point in the history
  • Loading branch information
udoprog committed Jan 17, 2024
1 parent 940986f commit 3355cfd
Show file tree
Hide file tree
Showing 11 changed files with 35 additions and 101 deletions.
32 changes: 18 additions & 14 deletions wgpu-core/src/device/queue.rs
Original file line number Diff line number Diff line change
Expand Up @@ -22,8 +22,9 @@ use crate::{
resource_log, track, FastHashMap, SubmissionIndex,
};

use hal::{CommandEncoder as _, Device as _, Queue as _, RawSet as _};
use hal::{CommandEncoder as _, Device as _, Queue as _};
use parking_lot::Mutex;
use smallvec::SmallVec;

use std::{
iter, mem, ptr,
Expand Down Expand Up @@ -1116,15 +1117,12 @@ impl<G: GlobalIdentityHandlerFactory> Global<G> {
+ 1;
let mut active_executions = Vec::new();

// SAFETY: We're constructing this during the submission phase,
// where all resources it uses are guaranteed to outlive this
// short-lived set.
let mut submit_surface_textures = A::SubmitSurfaceTextureSet::new();

let mut used_surface_textures = track::TextureUsageScope::new();

let snatch_guard = device.snatchable_lock.read();

let mut submit_surface_textures_owned = SmallVec::<[_; 2]>::new();

{
let mut command_buffer_guard = hub.command_buffers.write();

Expand Down Expand Up @@ -1230,10 +1228,8 @@ impl<G: GlobalIdentityHandlerFactory> Global<G> {
}) => {
has_work.store(true, Ordering::Relaxed);

if let Some(raw) = raw {
unsafe {
submit_surface_textures.insert(raw);
}
if raw.is_some() {
submit_surface_textures_owned.push(texture.clone());
}

true
Expand Down Expand Up @@ -1433,10 +1429,8 @@ impl<G: GlobalIdentityHandlerFactory> Global<G> {
}) => {
has_work.store(true, Ordering::Relaxed);

if let Some(raw) = raw {
unsafe {
submit_surface_textures.insert(raw);
}
if raw.is_some() {
submit_surface_textures_owned.push(texture.clone());
}

unsafe {
Expand Down Expand Up @@ -1478,6 +1472,16 @@ impl<G: GlobalIdentityHandlerFactory> Global<G> {
)
.collect::<Vec<_>>();

let mut submit_surface_textures =
SmallVec::<[_; 2]>::with_capacity(submit_surface_textures_owned.len());

for texture in submit_surface_textures_owned {
submit_surface_textures.extend(match texture.inner.get(&snatch_guard) {
Some(TextureInner::Surface { raw, .. }) => raw.as_ref(),
_ => None,
});
}

unsafe {
queue
.raw
Expand Down
21 changes: 4 additions & 17 deletions wgpu-hal/examples/halmark/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,7 @@
extern crate wgpu_hal as hal;

use hal::{
Adapter as _, CommandEncoder as _, Device as _, Instance as _, Queue as _, RawSet as _,
Surface as _,
Adapter as _, CommandEncoder as _, Device as _, Instance as _, Queue as _, Surface as _,
};
use raw_window_handle::{HasDisplayHandle, HasWindowHandle};
use winit::{
Expand Down Expand Up @@ -490,13 +489,8 @@ impl<A: hal::Api> Example<A> {
let fence = unsafe {
let mut fence = device.create_fence().unwrap();
let init_cmd = cmd_encoder.end_encoding().unwrap();
let surface_textures = A::SubmitSurfaceTextureSet::new();
queue
.submit(
&[&init_cmd],
&surface_textures,
Some((&mut fence, init_fence_value)),
)
.submit(&[&init_cmd], &[], Some((&mut fence, init_fence_value)))
.unwrap();
device.wait(&fence, init_fence_value, !0).unwrap();
device.destroy_buffer(staging_buffer);
Expand Down Expand Up @@ -547,13 +541,8 @@ impl<A: hal::Api> Example<A> {
unsafe {
{
let ctx = &mut self.contexts[self.context_index];
let surface_textures = A::SubmitSurfaceTextureSet::new();
self.queue
.submit(
&[],
&surface_textures,
Some((&mut ctx.fence, ctx.fence_value)),
)
.submit(&[], &[], Some((&mut ctx.fence, ctx.fence_value)))
.unwrap();
}

Expand Down Expand Up @@ -740,10 +729,8 @@ impl<A: hal::Api> Example<A> {
} else {
None
};
let mut surface_textures = A::SubmitSurfaceTextureSet::new();
surface_textures.insert(&surface_tex);
self.queue
.submit(&[&cmd_buf], &surface_textures, fence_param)
.submit(&[&cmd_buf], &[&surface_tex], fence_param)
.unwrap();
self.queue.present(&self.surface, surface_tex).unwrap();
ctx.used_cmd_bufs.push(cmd_buf);
Expand Down
2 changes: 1 addition & 1 deletion wgpu-hal/examples/raw-gles.rs
Original file line number Diff line number Diff line change
Expand Up @@ -183,6 +183,6 @@ fn fill_screen(exposed: &hal::ExposedAdapter<hal::api::Gles>, width: u32, height
encoder.begin_render_pass(&rp_desc);
encoder.end_render_pass();
let cmd_buf = encoder.end_encoding().unwrap();
od.queue.submit(&[&cmd_buf], &(), None).unwrap();
od.queue.submit(&[&cmd_buf], &[], None).unwrap();
}
}
21 changes: 4 additions & 17 deletions wgpu-hal/examples/ray-traced-triangle/main.rs
Original file line number Diff line number Diff line change
@@ -1,8 +1,7 @@
extern crate wgpu_hal as hal;

use hal::{
Adapter as _, CommandEncoder as _, Device as _, Instance as _, Queue as _, RawSet as _,
Surface as _,
Adapter as _, CommandEncoder as _, Device as _, Instance as _, Queue as _, Surface as _,
};
use raw_window_handle::{HasDisplayHandle, HasWindowHandle};

Expand Down Expand Up @@ -755,13 +754,8 @@ impl<A: hal::Api> Example<A> {
let fence = unsafe {
let mut fence = device.create_fence().unwrap();
let init_cmd = cmd_encoder.end_encoding().unwrap();
let surface_textures = A::SubmitSurfaceTextureSet::new();
queue
.submit(
&[&init_cmd],
&surface_textures,
Some((&mut fence, init_fence_value)),
)
.submit(&[&init_cmd], &[], Some((&mut fence, init_fence_value)))
.unwrap();
device.wait(&fence, init_fence_value, !0).unwrap();
cmd_encoder.reset_all(iter::once(init_cmd));
Expand Down Expand Up @@ -966,10 +960,8 @@ impl<A: hal::Api> Example<A> {
} else {
None
};
let mut surface_textures = A::SubmitSurfaceTextureSet::new();
surface_textures.insert(&surface_tex);
self.queue
.submit(&[&cmd_buf], &surface_textures, fence_param)
.submit(&[&cmd_buf], &[&surface_tex], fence_param)
.unwrap();
self.queue.present(&self.surface, surface_tex).unwrap();
ctx.used_cmd_bufs.push(cmd_buf);
Expand Down Expand Up @@ -1008,13 +1000,8 @@ impl<A: hal::Api> Example<A> {
unsafe {
{
let ctx = &mut self.contexts[self.context_index];
let surface_textures = A::SubmitSurfaceTextureSet::new();
self.queue
.submit(
&[],
&surface_textures,
Some((&mut ctx.fence, ctx.fence_value)),
)
.submit(&[], &[], Some((&mut ctx.fence, ctx.fence_value)))
.unwrap();
}

Expand Down
3 changes: 1 addition & 2 deletions wgpu-hal/src/dx12/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -84,7 +84,6 @@ impl crate::Api for Api {
type ComputePipeline = ComputePipeline;

type AccelerationStructure = AccelerationStructure;
type SubmitSurfaceTextureSet = ();
}

// Limited by D3D12's root signature size of 64. Each element takes 1 or 2 entries.
Expand Down Expand Up @@ -883,7 +882,7 @@ impl crate::Queue<Api> for Queue {
unsafe fn submit(
&self,
command_buffers: &[&CommandBuffer],
_surface_textures: &(),
_surface_textures: &[&Texture],
signal_fence: Option<(&mut Fence, crate::FenceValue)>,
) -> Result<(), crate::DeviceError> {
let mut temp_lists = self.temp_lists.lock();
Expand Down
3 changes: 1 addition & 2 deletions wgpu-hal/src/empty.rs
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,6 @@ impl crate::Api for Api {
type ShaderModule = Resource;
type RenderPipeline = Resource;
type ComputePipeline = Resource;
type SubmitSurfaceTextureSet = ();
}

impl crate::Instance<Api> for Context {
Expand Down Expand Up @@ -105,7 +104,7 @@ impl crate::Queue<Api> for Context {
unsafe fn submit(
&self,
command_buffers: &[&Resource],
surface_textures: &(),
surface_textures: &[&Resource],
signal_fence: Option<(&mut Resource, crate::FenceValue)>,
) -> DeviceResult<()> {
Ok(())
Expand Down
1 change: 0 additions & 1 deletion wgpu-hal/src/gles/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -161,7 +161,6 @@ impl crate::Api for Api {
type ShaderModule = ShaderModule;
type RenderPipeline = RenderPipeline;
type ComputePipeline = ComputePipeline;
type SubmitSurfaceTextureSet = ();
}

bitflags::bitflags! {
Expand Down
2 changes: 1 addition & 1 deletion wgpu-hal/src/gles/queue.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1748,7 +1748,7 @@ impl crate::Queue<super::Api> for super::Queue {
unsafe fn submit(
&self,
command_buffers: &[&super::CommandBuffer],
_surface_textures: &(),
_surface_textures: &[&super::Texture],
signal_fence: Option<(&mut super::Fence, crate::FenceValue)>,
) -> Result<(), crate::DeviceError> {
let shared = Arc::clone(&self.shared);
Expand Down
22 changes: 1 addition & 21 deletions wgpu-hal/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -214,7 +214,6 @@ pub trait Api: Clone + fmt::Debug + Sized {
type ComputePipeline: fmt::Debug + WasmNotSendSync;

type AccelerationStructure: fmt::Debug + WasmNotSendSync + 'static;
type SubmitSurfaceTextureSet: RawSet<Self::SurfaceTexture>;
}

pub trait Instance<A: Api>: Sized + WasmNotSendSync {
Expand Down Expand Up @@ -419,7 +418,7 @@ pub trait Queue<A: Api>: WasmNotSendSync {
unsafe fn submit(
&self,
command_buffers: &[&A::CommandBuffer],
surface_textures: &A::SubmitSurfaceTextureSet,
surface_textures: &[&A::SurfaceTexture],
signal_fence: Option<(&mut A::Fence, FenceValue)>,
) -> Result<(), DeviceError>;
unsafe fn present(
Expand Down Expand Up @@ -722,25 +721,6 @@ bitflags!(
}
);

pub trait RawSet<T> {
/// Construct a new set unsafely.
fn new() -> Self;

/// Insert a value into the raw set.
///
/// The caller is responsible for ensuring that the set doesn't outlive the
/// values it contains. The exact requirements depends on which set is being
/// constructed.
unsafe fn insert(&mut self, value: &T);
}

/// Provide a default implementation for () for backends which do not need to
/// track any raw resources so they can easily be stubbed out.
impl<T> RawSet<T> for () {
fn new() -> Self {}
unsafe fn insert(&mut self, _: &T) {}
}

bitflags!(
/// Texture format capability flags.
#[derive(Debug, Copy, Clone, PartialEq, Eq, Hash)]
Expand Down
3 changes: 1 addition & 2 deletions wgpu-hal/src/metal/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -68,7 +68,6 @@ impl crate::Api for Api {
type ComputePipeline = ComputePipeline;

type AccelerationStructure = AccelerationStructure;
type SubmitSurfaceTextureSet = ();
}

pub struct Instance {
Expand Down Expand Up @@ -369,7 +368,7 @@ impl crate::Queue<Api> for Queue {
unsafe fn submit(
&self,
command_buffers: &[&CommandBuffer],
_surface_textures: &(),
_surface_textures: &[&SurfaceTexture],
signal_fence: Option<(&mut Fence, crate::FenceValue)>,
) -> Result<(), crate::DeviceError> {
objc::rc::autoreleasepool(|| {
Expand Down
26 changes: 3 additions & 23 deletions wgpu-hal/src/vulkan/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -48,9 +48,6 @@ use ash::{
vk,
};
use parking_lot::{Mutex, RwLock};
use smallvec::SmallVec;

use crate::RawSet;

const MILLIS_TO_NANOS: u64 = 1_000_000;
const MAX_TOTAL_ATTACHMENTS: usize = crate::MAX_COLOR_ATTACHMENTS * 2 + 1;
Expand Down Expand Up @@ -83,23 +80,6 @@ impl crate::Api for Api {
type ShaderModule = ShaderModule;
type RenderPipeline = RenderPipeline;
type ComputePipeline = ComputePipeline;
type SubmitSurfaceTextureSet = SubmitSurfaceTextureSet;
}

pub struct SubmitSurfaceTextureSet {
semaphores: SmallVec<[vk::Semaphore; 2]>,
}

impl RawSet<SurfaceTexture> for SubmitSurfaceTextureSet {
fn new() -> Self {
Self {
semaphores: SmallVec::new(),
}
}

unsafe fn insert(&mut self, texture: &SurfaceTexture) {
self.semaphores.push(texture.wait_semaphore);
}
}

struct DebugUtils {
Expand Down Expand Up @@ -610,7 +590,7 @@ impl crate::Queue<Api> for Queue {
unsafe fn submit(
&self,
command_buffers: &[&CommandBuffer],
surface_textures: &SubmitSurfaceTextureSet,
surface_textures: &[&SurfaceTexture],
signal_fence: Option<(&mut Fence, crate::FenceValue)>,
) -> Result<(), crate::DeviceError> {
let mut fence_raw = vk::Fence::null();
Expand All @@ -620,9 +600,9 @@ impl crate::Queue<Api> for Queue {
let mut signal_semaphores = ArrayVec::<_, 2>::new();
let mut signal_values = ArrayVec::<_, 2>::new();

for &wait in &surface_textures.semaphores {
for &surface_texture in surface_textures {
wait_stage_masks.push(vk::PipelineStageFlags::TOP_OF_PIPE);
wait_semaphores.push(wait);
wait_semaphores.push(surface_texture.wait_semaphore);
}

let old_index = self.relay_index.load(Ordering::Relaxed);
Expand Down

0 comments on commit 3355cfd

Please sign in to comment.