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 e7b7246 commit fd24c7b
Show file tree
Hide file tree
Showing 12 changed files with 49 additions and 95 deletions.
49 changes: 21 additions & 28 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 @@ -227,20 +228,6 @@ impl<A: HalApi> PendingWrites<A> {
.push(TempResource::StagingBuffer(buffer));
}

#[must_use]
fn pre_submit(&mut self) -> Option<&A::CommandBuffer> {
self.dst_buffers.clear();
self.dst_textures.clear();
if self.is_active {
let cmd_buf = unsafe { self.command_encoder.end_encoding().unwrap() };
self.is_active = false;
self.executing_command_buffers.push(cmd_buf);
self.executing_command_buffers.last()
} else {
None
}
}

#[must_use]
fn post_submit(
&mut self,
Expand Down Expand Up @@ -1116,15 +1103,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 = SmallVec::<[_; 2]>::new();

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

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

if let Some(raw) = raw {
unsafe {
submit_surface_textures.insert(raw);
}
submit_surface_textures.push(raw);
}

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

if let Some(raw) = raw {
unsafe {
submit_surface_textures.insert(raw);
}
submit_surface_textures.push(raw);
}

unsafe {
Expand Down Expand Up @@ -1468,8 +1448,18 @@ impl<G: GlobalIdentityHandlerFactory> Global<G> {
}
}

let refs = pending_writes
.pre_submit()
pending_writes.dst_buffers.clear();

let refs = if pending_writes.is_active {
let cmd_buf = unsafe { pending_writes.command_encoder.end_encoding().unwrap() };
pending_writes.is_active = false;
pending_writes.executing_command_buffers.push(cmd_buf);
pending_writes.executing_command_buffers.last()
} else {
None
};

let refs = refs
.into_iter()
.chain(
active_executions
Expand All @@ -1487,6 +1477,9 @@ impl<G: GlobalIdentityHandlerFactory> Global<G> {
.map_err(DeviceError::from)?;
}

// We clear this here, since dst_textures is in use during the call to submit.
pending_writes.dst_textures.clear();

profiling::scope!("cleanup");
if let Some(pending_execution) = pending_writes.post_submit(
device.command_allocator.lock().as_mut().unwrap(),
Expand Down
2 changes: 1 addition & 1 deletion wgpu-core/src/resource.rs
Original file line number Diff line number Diff line change
Expand Up @@ -399,7 +399,7 @@ impl<A: HalApi> Drop for Buffer<A> {
}

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

Expand Down
11 changes: 9 additions & 2 deletions wgpu-core/src/snatch.rs
Original file line number Diff line number Diff line change
Expand Up @@ -25,8 +25,15 @@ impl<T> Snatchable<T> {
}
}

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

Expand Down
11 changes: 5 additions & 6 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,7 +489,7 @@ 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();
let surface_textures = Vec::new();
queue
.submit(
&[&init_cmd],
Expand Down Expand Up @@ -547,7 +546,7 @@ impl<A: hal::Api> Example<A> {
unsafe {
{
let ctx = &mut self.contexts[self.context_index];
let surface_textures = A::SubmitSurfaceTextureSet::new();
let surface_textures = Vec::new();
self.queue
.submit(
&[],
Expand Down Expand Up @@ -740,8 +739,8 @@ impl<A: hal::Api> Example<A> {
} else {
None
};
let mut surface_textures = A::SubmitSurfaceTextureSet::new();
surface_textures.insert(&surface_tex);
let mut surface_textures = Vec::new();
surface_textures.push(&surface_tex);
self.queue
.submit(&[&cmd_buf], &surface_textures, fence_param)
.unwrap();
Expand Down
11 changes: 5 additions & 6 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,7 +754,7 @@ 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();
let surface_textures = Vec::new();
queue
.submit(
&[&init_cmd],
Expand Down Expand Up @@ -966,8 +965,8 @@ impl<A: hal::Api> Example<A> {
} else {
None
};
let mut surface_textures = A::SubmitSurfaceTextureSet::new();
surface_textures.insert(&surface_tex);
let mut surface_textures = Vec::new();
surface_textures.push(&surface_tex);
self.queue
.submit(&[&cmd_buf], &surface_textures, fence_param)
.unwrap();
Expand Down Expand Up @@ -1008,7 +1007,7 @@ impl<A: hal::Api> Example<A> {
unsafe {
{
let ctx = &mut self.contexts[self.context_index];
let surface_textures = A::SubmitSurfaceTextureSet::new();
let surface_textures = Vec::new();
self.queue
.submit(
&[],
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 @@ -878,7 +877,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 fd24c7b

Please sign in to comment.