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
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
// Copyright (c) 2017 The vulkano developers
// Licensed under the Apache License, Version 2.0
// <LICENSE-APACHE or
// http://www.apache.org/licenses/LICENSE-2.0> or the MIT
// license <LICENSE-MIT or http://opensource.org/licenses/MIT>,
// at your option. All files in the project carrying such
// notice may not be copied, modified, or distributed except
// according to those terms.

use std::sync::Arc;

use buffer::BufferAccess;
use command_buffer::submit::SubmitAnyBuilder;
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 VulkanObject;

/// Joins two futures together.
// TODO: handle errors
#[inline]
pub fn join<F, S>(first: F, second: S) -> JoinFuture<F, S>
    where F: GpuFuture,
          S: GpuFuture
{
    assert_eq!(first.device().internal_object(),
               second.device().internal_object());

    if !first.queue_change_allowed() && !second.queue_change_allowed() {
        assert!(first.queue().unwrap().is_same(&second.queue().unwrap()));
    }

    JoinFuture {
        first: first,
        second: second,
    }
}

/// Two futures joined into one.
#[must_use]
pub struct JoinFuture<A, B> {
    first: A,
    second: B,
}

unsafe impl<A, B> DeviceOwned for JoinFuture<A, B>
    where A: DeviceOwned,
          B: DeviceOwned
{
    #[inline]
    fn device(&self) -> &Arc<Device> {
        let device = self.first.device();
        debug_assert_eq!(self.second.device().internal_object(),
                         device.internal_object());
        device
    }
}

unsafe impl<A, B> GpuFuture for JoinFuture<A, B>
    where A: GpuFuture,
          B: GpuFuture
{
    #[inline]
    fn cleanup_finished(&mut self) {
        self.first.cleanup_finished();
        self.second.cleanup_finished();
    }

    #[inline]
    fn flush(&self) -> Result<(), FlushError> {
        // Since each future remembers whether it has been flushed, there's no safety issue here
        // if we call this function multiple times.
        self.first.flush()?;
        self.second.flush()?;
        Ok(())
    }

    #[inline]
    unsafe fn build_submission(&self) -> Result<SubmitAnyBuilder, FlushError> {
        // TODO: review this function

        let first = self.first.build_submission()?;
        let second = self.second.build_submission()?;

        Ok(match (first, second) {
               (SubmitAnyBuilder::Empty, b) => b,
               (a, SubmitAnyBuilder::Empty) => a,
               (SubmitAnyBuilder::SemaphoresWait(mut a), SubmitAnyBuilder::SemaphoresWait(b)) => {
                   a.merge(b);
                   SubmitAnyBuilder::SemaphoresWait(a)
               },
               (SubmitAnyBuilder::SemaphoresWait(a), SubmitAnyBuilder::CommandBuffer(b)) => {
                   b.submit(&self.second.queue().clone().unwrap())?;
                   SubmitAnyBuilder::SemaphoresWait(a)
               },
               (SubmitAnyBuilder::CommandBuffer(a), SubmitAnyBuilder::SemaphoresWait(b)) => {
                   a.submit(&self.first.queue().clone().unwrap())?;
                   SubmitAnyBuilder::SemaphoresWait(b)
               },
               (SubmitAnyBuilder::SemaphoresWait(a), SubmitAnyBuilder::QueuePresent(b)) => {
                   b.submit(&self.second.queue().clone().unwrap())?;
                   SubmitAnyBuilder::SemaphoresWait(a)
               },
               (SubmitAnyBuilder::QueuePresent(a), SubmitAnyBuilder::SemaphoresWait(b)) => {
                   a.submit(&self.first.queue().clone().unwrap())?;
                   SubmitAnyBuilder::SemaphoresWait(b)
               },
               (SubmitAnyBuilder::SemaphoresWait(a), SubmitAnyBuilder::BindSparse(b)) => {
                   b.submit(&self.second.queue().clone().unwrap())?;
                   SubmitAnyBuilder::SemaphoresWait(a)
               },
               (SubmitAnyBuilder::BindSparse(a), SubmitAnyBuilder::SemaphoresWait(b)) => {
                   a.submit(&self.first.queue().clone().unwrap())?;
                   SubmitAnyBuilder::SemaphoresWait(b)
               },
               (SubmitAnyBuilder::CommandBuffer(a), SubmitAnyBuilder::CommandBuffer(b)) => {
                   // TODO: we may want to add debug asserts here
                   let new = a.merge(b);
                   SubmitAnyBuilder::CommandBuffer(new)
               },
               (SubmitAnyBuilder::QueuePresent(a), SubmitAnyBuilder::QueuePresent(b)) => {
                   a.submit(&self.first.queue().clone().unwrap())?;
                   b.submit(&self.second.queue().clone().unwrap())?;
                   SubmitAnyBuilder::Empty
               },
               (SubmitAnyBuilder::CommandBuffer(a), SubmitAnyBuilder::QueuePresent(b)) => {
                   unimplemented!()
               },
               (SubmitAnyBuilder::QueuePresent(a), SubmitAnyBuilder::CommandBuffer(b)) => {
                   unimplemented!()
               },
               (SubmitAnyBuilder::BindSparse(a), SubmitAnyBuilder::QueuePresent(b)) => {
                   unimplemented!()
               },
               (SubmitAnyBuilder::QueuePresent(a), SubmitAnyBuilder::BindSparse(b)) => {
                   unimplemented!()
               },
               (SubmitAnyBuilder::BindSparse(a), SubmitAnyBuilder::CommandBuffer(b)) => {
                   unimplemented!()
               },
               (SubmitAnyBuilder::CommandBuffer(a), SubmitAnyBuilder::BindSparse(b)) => {
                   unimplemented!()
               },
               (SubmitAnyBuilder::BindSparse(mut a), SubmitAnyBuilder::BindSparse(b)) => {
                   match a.merge(b) {
                       Ok(()) => SubmitAnyBuilder::BindSparse(a),
                       Err(_) => {
                           // TODO: this happens if both bind sparse have been given a fence already
                           //       annoying, but not impossible, to handle
                           unimplemented!()
                       },
                   }
               },
           })
    }

    #[inline]
    unsafe fn signal_finished(&self) {
        self.first.signal_finished();
        self.second.signal_finished();
    }

    #[inline]
    fn queue_change_allowed(&self) -> bool {
        self.first.queue_change_allowed() && self.second.queue_change_allowed()
    }

    #[inline]
    fn queue(&self) -> Option<Arc<Queue>> {
        match (self.first.queue(), self.second.queue()) {
            (Some(q1), Some(q2)) => if q1.is_same(&q2) {
                Some(q1)
            } else if self.first.queue_change_allowed() {
                Some(q2)
            } else if self.second.queue_change_allowed() {
                Some(q1)
            } else {
                None
            },
            (Some(q), None) => Some(q),
            (None, Some(q)) => Some(q),
            (None, None) => None,
        }
    }

    #[inline]
    fn check_buffer_access(
        &self, buffer: &dyn BufferAccess, exclusive: bool, queue: &Queue)
        -> Result<Option<(PipelineStages, AccessFlagBits)>, AccessCheckError> {
        let first = self.first.check_buffer_access(buffer, exclusive, queue);
        let second = self.second.check_buffer_access(buffer, exclusive, queue);
        debug_assert!(!exclusive || !(first.is_ok() && second.is_ok()),
                      "Two futures gave exclusive access to the same resource");
        match (first, second) {
            (v, Err(AccessCheckError::Unknown)) => v,
            (Err(AccessCheckError::Unknown), v) => v,
            (Err(AccessCheckError::Denied(e1)), Err(AccessCheckError::Denied(e2))) =>
                Err(AccessCheckError::Denied(e1)),        // TODO: which one?
            (Ok(_), Err(AccessCheckError::Denied(_))) |
            (Err(AccessCheckError::Denied(_)), Ok(_)) => panic!("Contradictory information \
                                                                 between two futures"),
            (Ok(None), Ok(None)) => Ok(None),
            (Ok(Some(a)), Ok(None)) |
            (Ok(None), Ok(Some(a))) => Ok(Some(a)),
            (Ok(Some((a1, a2))), Ok(Some((b1, b2)))) => {
                Ok(Some((a1 | b1, a2 | b2)))
            },
        }
    }

    #[inline]
    fn check_image_access(&self, image: &dyn ImageAccess, layout: ImageLayout, exclusive: bool,
                          queue: &Queue)
                          -> Result<Option<(PipelineStages, AccessFlagBits)>, AccessCheckError> {
        let first = self.first
            .check_image_access(image, layout, exclusive, queue);
        let second = self.second
            .check_image_access(image, layout, exclusive, queue);
        debug_assert!(!exclusive || !(first.is_ok() && second.is_ok()),
                      "Two futures gave exclusive access to the same resource");
        match (first, second) {
            (v, Err(AccessCheckError::Unknown)) => v,
            (Err(AccessCheckError::Unknown), v) => v,
            (Err(AccessCheckError::Denied(e1)), Err(AccessCheckError::Denied(e2))) =>
                Err(AccessCheckError::Denied(e1)),        // TODO: which one?
            (Ok(_), Err(AccessCheckError::Denied(_))) |
            (Err(AccessCheckError::Denied(_)), Ok(_)) => panic!("Contradictory information \
                                                                 between two futures"),
            (Ok(None), Ok(None)) => Ok(None),
            (Ok(Some(a)), Ok(None)) |
            (Ok(None), Ok(Some(a))) => Ok(Some(a)),
            (Ok(Some((a1, a2))), Ok(Some((b1, b2)))) => {
                Ok(Some((a1 | b1, a2 | b2)))
            },
        }
    }
}