@@ -31,7 +31,7 @@ mod conv;
3131mod device;
3232mod instance;
3333
34- use std:: { borrow:: Borrow , collections:: HashSet , ffi:: CStr , fmt, mem , num:: NonZeroU32 , sync:: Arc } ;
34+ use std:: { borrow:: Borrow , collections:: HashSet , ffi:: CStr , fmt, num:: NonZeroU32 , sync:: Arc } ;
3535
3636use arrayvec:: ArrayVec ;
3737use ash:: {
@@ -485,70 +485,12 @@ pub struct Device {
485485 render_doc : crate :: auxil:: renderdoc:: RenderDoc ,
486486}
487487
488- /// Semaphores that a given submission should wait on and signal.
489- struct RelaySemaphoreState {
490- wait : Option < vk:: Semaphore > ,
491- signal : vk:: Semaphore ,
492- }
493-
494- /// A pair of binary semaphores that are used to synchronize each submission with the next.
495- struct RelaySemaphores {
496- wait : vk:: Semaphore ,
497- /// Signals if the wait semaphore should be waited on.
498- ///
499- /// Because nothing will signal the semaphore for the first submission, we don't want to wait on it.
500- should_wait : bool ,
501- signal : vk:: Semaphore ,
502- }
503-
504- impl RelaySemaphores {
505- fn new ( device : & ash:: Device ) -> Result < Self , crate :: DeviceError > {
506- let wait = unsafe {
507- device
508- . create_semaphore ( & vk:: SemaphoreCreateInfo :: builder ( ) , None )
509- . map_err ( crate :: DeviceError :: from) ?
510- } ;
511- let signal = unsafe {
512- device
513- . create_semaphore ( & vk:: SemaphoreCreateInfo :: builder ( ) , None )
514- . map_err ( crate :: DeviceError :: from) ?
515- } ;
516- Ok ( Self {
517- wait,
518- should_wait : false ,
519- signal,
520- } )
521- }
522-
523- /// Advances the semaphores, returning the semaphores that should be used for a submission.
524- #[ must_use]
525- fn advance ( & mut self ) -> RelaySemaphoreState {
526- let old = RelaySemaphoreState {
527- wait : self . should_wait . then_some ( self . wait ) ,
528- signal : self . signal ,
529- } ;
530-
531- mem:: swap ( & mut self . wait , & mut self . signal ) ;
532- self . should_wait = true ;
533-
534- old
535- }
536-
537- /// Destroys the semaphores.
538- unsafe fn destroy ( & self , device : & ash:: Device ) {
539- unsafe {
540- device. destroy_semaphore ( self . wait , None ) ;
541- device. destroy_semaphore ( self . signal , None ) ;
542- }
543- }
544- }
545-
546488pub struct Queue {
547489 raw : vk:: Queue ,
548490 swapchain_fn : khr:: Swapchain ,
549491 device : Arc < DeviceShared > ,
550492 family_index : u32 ,
551- relay_semaphores : Mutex < RelaySemaphores > ,
493+ relay_semaphore : Mutex < Option < vk :: Semaphore > > ,
552494}
553495
554496#[ derive( Debug ) ]
@@ -928,16 +870,31 @@ impl crate::Queue for Queue {
928870 signal_values. push ( !0 ) ;
929871 }
930872
931- // In order for submissions to be strictly ordered, we encode a dependency between each submission
932- // using a pair of semaphores. This adds a wait if it is needed, and signals the next semaphore.
933- let semaphore_state = self . relay_semaphores . lock ( ) . advance ( ) ;
934-
935- if let Some ( sem) = semaphore_state. wait {
936- wait_stage_masks. push ( vk:: PipelineStageFlags :: TOP_OF_PIPE ) ;
937- wait_semaphores. push ( sem) ;
938- }
873+ let signal_semaphore = match * self . relay_semaphore . lock ( ) {
874+ Some ( sem) => {
875+ // Wait for the previous submission to complete.
876+ wait_stage_masks. push ( vk:: PipelineStageFlags :: TOP_OF_PIPE ) ;
877+ wait_semaphores. push ( sem) ;
878+ sem
879+ }
880+ ref mut next @ None => {
881+ // This is the first submission to this queue, so we needn't
882+ // wait for any prior submissions to complete. But we do need to
883+ // create a semaphore for this submission to signal when it's
884+ // complete.
885+ let new_sem = unsafe {
886+ self . device
887+ . raw
888+ . create_semaphore ( & vk:: SemaphoreCreateInfo :: builder ( ) , None )
889+ . map_err ( crate :: DeviceError :: from) ?
890+ } ;
891+ * next = Some ( new_sem) ;
892+ new_sem
893+ }
894+ } ;
939895
940- signal_semaphores. push ( semaphore_state. signal ) ;
896+ // This submission must signal the semaphore that the next one waits on.
897+ signal_semaphores. push ( signal_semaphore) ;
941898 signal_values. push ( !0 ) ;
942899
943900 // We need to signal our wgpu::Fence if we have one, this adds it to the signal list.
0 commit comments