1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
use std::sync::Arc;
use std::sync::Mutex;
use std::sync::atomic::AtomicBool;
use std::sync::atomic::Ordering;
use buffer::BufferAccess;
use command_buffer::submit::SubmitAnyBuilder;
use command_buffer::submit::SubmitCommandBufferBuilder;
use command_buffer::submit::SubmitSemaphoresWaitBuilder;
use device::Device;
use device::DeviceOwned;
use device::Queue;
use image::ImageAccess;
use image::ImageLayout;
use sync::AccessCheckError;
use sync::AccessFlagBits;
use sync::FlushError;
use sync::GpuFuture;
use sync::PipelineStages;
use sync::Semaphore;
#[inline]
pub fn then_signal_semaphore<F>(future: F) -> SemaphoreSignalFuture<F>
where F: GpuFuture
{
let device = future.device().clone();
assert!(future.queue().is_some());
SemaphoreSignalFuture {
previous: future,
semaphore: Semaphore::from_pool(device).unwrap(),
wait_submitted: Mutex::new(false),
finished: AtomicBool::new(false),
}
}
#[must_use = "Dropping this object will immediately block the thread until the GPU has finished \
processing the submission"]
pub struct SemaphoreSignalFuture<F>
where F: GpuFuture
{
previous: F,
semaphore: Semaphore,
wait_submitted: Mutex<bool>,
finished: AtomicBool,
}
unsafe impl<F> GpuFuture for SemaphoreSignalFuture<F>
where F: GpuFuture
{
#[inline]
fn cleanup_finished(&mut self) {
self.previous.cleanup_finished();
}
#[inline]
unsafe fn build_submission(&self) -> Result<SubmitAnyBuilder, FlushError> {
self.flush()?;
let mut sem = SubmitSemaphoresWaitBuilder::new();
sem.add_wait_semaphore(&self.semaphore);
Ok(SubmitAnyBuilder::SemaphoresWait(sem))
}
fn flush(&self) -> Result<(), FlushError> {
unsafe {
let mut wait_submitted = self.wait_submitted.lock().unwrap();
if *wait_submitted {
return Ok(());
}
let queue = self.previous.queue().unwrap().clone();
match self.previous.build_submission()? {
SubmitAnyBuilder::Empty => {
let mut builder = SubmitCommandBufferBuilder::new();
builder.add_signal_semaphore(&self.semaphore);
builder.submit(&queue)?;
},
SubmitAnyBuilder::SemaphoresWait(sem) => {
let mut builder: SubmitCommandBufferBuilder = sem.into();
builder.add_signal_semaphore(&self.semaphore);
builder.submit(&queue)?;
},
SubmitAnyBuilder::CommandBuffer(mut builder) => {
debug_assert_eq!(builder.num_signal_semaphores(), 0);
builder.add_signal_semaphore(&self.semaphore);
builder.submit(&queue)?;
},
SubmitAnyBuilder::BindSparse(_) => {
unimplemented!()
},
SubmitAnyBuilder::QueuePresent(present) => {
present.submit(&queue)?;
let mut builder = SubmitCommandBufferBuilder::new();
builder.add_signal_semaphore(&self.semaphore);
builder.submit(&queue)?;
},
};
*wait_submitted = true;
Ok(())
}
}
#[inline]
unsafe fn signal_finished(&self) {
debug_assert!(*self.wait_submitted.lock().unwrap());
self.finished.store(true, Ordering::SeqCst);
self.previous.signal_finished();
}
#[inline]
fn queue_change_allowed(&self) -> bool {
true
}
#[inline]
fn queue(&self) -> Option<Arc<Queue>> {
self.previous.queue()
}
#[inline]
fn check_buffer_access(
&self, buffer: &dyn BufferAccess, exclusive: bool, queue: &Queue)
-> Result<Option<(PipelineStages, AccessFlagBits)>, AccessCheckError> {
self.previous
.check_buffer_access(buffer, exclusive, queue)
.map(|_| None)
}
#[inline]
fn check_image_access(&self, image: &dyn ImageAccess, layout: ImageLayout, exclusive: bool,
queue: &Queue)
-> Result<Option<(PipelineStages, AccessFlagBits)>, AccessCheckError> {
self.previous
.check_image_access(image, layout, exclusive, queue)
.map(|_| None)
}
}
unsafe impl<F> DeviceOwned for SemaphoreSignalFuture<F>
where F: GpuFuture
{
#[inline]
fn device(&self) -> &Arc<Device> {
self.semaphore.device()
}
}
impl<F> Drop for SemaphoreSignalFuture<F>
where F: GpuFuture
{
fn drop(&mut self) {
unsafe {
if !*self.finished.get_mut() {
self.flush().unwrap();
self.queue().unwrap().wait().unwrap();
self.previous.signal_finished();
}
}
}
}