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
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
//! Command buffer module docs.

mod encoder;
mod level;
mod reset;
mod state;
mod submit;
mod usage;

use {
    crate::{
        capability::{Capability, Supports},
        family::FamilyId,
    },
    rendy_core::hal::Backend,
};

pub use self::{encoder::*, level::*, reset::*, state::*, submit::*, usage::*};

/// Command buffer wrapper.
/// This wrapper defines state with usage, level and ability to be individually reset at type level.
/// This way many methods become safe.
#[derive(Debug)]
pub struct CommandBuffer<B: Backend, C, S, L = PrimaryLevel, R = NoIndividualReset> {
    raw: std::ptr::NonNull<B::CommandBuffer>,
    capability: C,
    state: S,
    level: L,
    reset: R,
    family: FamilyId,
    relevant: relevant::Relevant,
}

family_owned!(CommandBuffer<B, C, S, L, R>);

unsafe impl<B, C, S, L, R> Send for CommandBuffer<B, C, S, L, R>
where
    B: Backend,
    B::CommandBuffer: Send,
    C: Send,
    S: Send,
    L: Send,
    R: Send,
    FamilyId: Send,
    relevant::Relevant: Send,
{
}

unsafe impl<B, C, S, L, R> Sync for CommandBuffer<B, C, S, L, R>
where
    B: Backend,
    B::CommandBuffer: Sync,
    C: Sync,
    S: Sync,
    L: Sync,
    R: Sync,
    FamilyId: Sync,
    relevant::Relevant: Sync,
{
}

impl<B, C, S, L, R> CommandBuffer<B, C, S, L, R>
where
    B: Backend,
{
    /// Wrap raw buffer handle.
    ///
    /// # Safety
    ///
    /// * `raw` must be valid command buffer handle.
    /// * `capability` must be subset of `family` capability.
    /// * `state` must represent actual state buffer currently in.
    /// * command buffer must be allocated with specified `level`.
    /// * If `reset` is `IndividualReset` then buffer must be allocated from pool created with `IndividualReset` marker.
    /// * command buffer must be allocated from pool created for `family`.
    pub(crate) unsafe fn from_raw(
        raw: B::CommandBuffer,
        capability: C,
        state: S,
        level: L,
        reset: R,
        family: FamilyId,
    ) -> Self {
        CommandBuffer {
            raw: std::ptr::NonNull::new_unchecked(Box::into_raw(Box::new(raw))),
            capability,
            state,
            level,
            reset,
            family,
            relevant: relevant::Relevant,
        }
    }

    /// Change state of the command buffer.
    ///
    /// # Safety
    ///
    /// * This method must be used only to reflect state changed due to raw handle usage.
    pub unsafe fn change_state<U>(self, f: impl FnOnce(S) -> U) -> CommandBuffer<B, C, U, L, R> {
        CommandBuffer {
            raw: self.raw,
            capability: self.capability,
            state: f(self.state),
            level: self.level,
            reset: self.reset,
            family: self.family,
            relevant: self.relevant,
        }
    }

    /// Get buffers capability.
    pub fn capability(&self) -> C
    where
        C: Capability,
    {
        self.capability
    }

    /// Get buffers family.
    pub fn family(&self) -> FamilyId {
        self.family
    }

    /// Convert capability level.
    pub fn with_queue_type(self) -> CommandBuffer<B, rendy_core::hal::queue::QueueType, S, L, R>
    where
        C: Capability,
    {
        CommandBuffer {
            raw: self.raw,
            capability: self.capability.into_queue_type(),
            state: self.state,
            level: self.level,
            reset: self.reset,
            family: self.family,
            relevant: self.relevant,
        }
    }

    /// Convert capability level.
    pub fn with_capability<U>(self) -> Result<CommandBuffer<B, U, S, L, R>, Self>
    where
        C: Supports<U>,
    {
        if let Some(capability) = self.capability.supports() {
            Ok(CommandBuffer {
                raw: self.raw,
                capability: capability,
                state: self.state,
                level: self.level,
                reset: self.reset,
                family: self.family,
                relevant: self.relevant,
            })
        } else {
            Err(self)
        }
    }
}

/// Begin info for specific level and render pass relation.
pub unsafe trait BeginInfo<'a, B: Backend, L> {
    /// Pass relation type.
    type PassRelation: RenderPassRelation<L>;

    /// Get command buffer inheritance info.
    fn inheritance_info(self) -> rendy_core::hal::command::CommandBufferInheritanceInfo<'a, B>;
}

unsafe impl<'a, B, L> BeginInfo<'a, B, L> for ()
where
    B: Backend,
    L: Level,
{
    type PassRelation = OutsideRenderPass;

    fn inheritance_info(self) -> rendy_core::hal::command::CommandBufferInheritanceInfo<'a, B> {
        rendy_core::hal::command::CommandBufferInheritanceInfo::default()
    }
}

unsafe impl<'a, B> BeginInfo<'a, B, SecondaryLevel> for rendy_core::hal::pass::Subpass<'a, B>
where
    B: Backend,
{
    type PassRelation = RenderPassContinue;

    fn inheritance_info(self) -> rendy_core::hal::command::CommandBufferInheritanceInfo<'a, B> {
        rendy_core::hal::command::CommandBufferInheritanceInfo {
            subpass: Some(self),
            framebuffer: None,
            ..rendy_core::hal::command::CommandBufferInheritanceInfo::default()
        }
    }
}

unsafe impl<'a, B, F> BeginInfo<'a, B, SecondaryLevel>
    for (rendy_core::hal::pass::Subpass<'a, B>, Option<&'a F>)
where
    B: Backend,
    F: std::borrow::Borrow<B::Framebuffer>,
{
    type PassRelation = RenderPassContinue;

    fn inheritance_info(self) -> rendy_core::hal::command::CommandBufferInheritanceInfo<'a, B> {
        rendy_core::hal::command::CommandBufferInheritanceInfo {
            subpass: Some(self.0),
            framebuffer: self.1.map(F::borrow),
            ..rendy_core::hal::command::CommandBufferInheritanceInfo::default()
        }
    }
}

unsafe impl<'a, B, F> BeginInfo<'a, B, SecondaryLevel>
    for (rendy_core::hal::pass::Subpass<'a, B>, &'a F)
where
    B: Backend,
    F: std::borrow::Borrow<B::Framebuffer>,
{
    type PassRelation = RenderPassContinue;

    fn inheritance_info(self) -> rendy_core::hal::command::CommandBufferInheritanceInfo<'a, B> {
        rendy_core::hal::command::CommandBufferInheritanceInfo {
            subpass: Some(self.0),
            framebuffer: Some(self.1.borrow()),
            ..rendy_core::hal::command::CommandBufferInheritanceInfo::default()
        }
    }
}

impl<B, C, L, R> CommandBuffer<B, C, InitialState, L, R>
where
    B: Backend,
{
    /// Begin recording command buffer.
    ///
    /// # Parameters
    ///
    /// `usage` - specifies usage of the command buffer. Possible types are `OneShot`, `MultiShot`.
    pub fn begin<'a, U, P>(
        mut self,
        usage: U,
        info: impl BeginInfo<'a, B, L, PassRelation = P>,
    ) -> CommandBuffer<B, C, RecordingState<U, P>, L, R>
    where
        U: Usage,
        P: RenderPassRelation<L>,
    {
        let pass_relation = P::default();
        unsafe {
            rendy_core::hal::command::CommandBuffer::begin(
                self.raw(),
                usage.flags() | pass_relation.flags(),
                info.inheritance_info(),
            );

            self.change_state(|_| RecordingState(usage, pass_relation))
        }
    }
}

impl<'a, B, C, U, P, L, R> CommandBuffer<B, C, RecordingState<U, P>, L, R>
where
    B: Backend,
{
    /// Finish recording command buffer.
    pub fn finish(mut self) -> CommandBuffer<B, C, ExecutableState<U, P>, L, R> {
        unsafe {
            rendy_core::hal::command::CommandBuffer::finish(self.raw());

            self.change_state(|s| ExecutableState(s.0, s.1))
        }
    }
}

impl<B, C, N, L, R> CommandBuffer<B, C, PendingState<N>, L, R>
where
    B: Backend,
{
    /// Mark command buffer as complete.
    ///
    /// # Safety
    ///
    /// None of [`Submit`] instances created from this `CommandBuffer` are alive.
    ///
    /// If this is `PrimaryLevel` buffer then
    /// for each command queue where [`Submit`] instance (created from this `CommandBuffer`)
    /// was submitted at least one [`Fence`] submitted within same `Submission` or later in unset state was `set`.
    ///
    /// If this is `Secondary` buffer then
    /// all primary command buffers where [`Submit`] instance (created from this `CommandBuffer`)
    /// was submitted must be complete.
    ///
    /// [`Submit`]: struct.Submit
    /// [waiting]: ..rendy_core::hal/device/trait.Device.html#method.wait_for_fences
    /// [`Fence`]: ..rendy_core::hal/trait.Backend.html#associatedtype.Fence
    /// [submitted]: ..rendy_core::hal/queue/struct.CommandQueue.html#method.submit
    pub unsafe fn mark_complete(self) -> CommandBuffer<B, C, N, L, R> {
        self.change_state(|PendingState(state)| state)
    }
}

impl<B, C, S, L> CommandBuffer<B, C, S, L, IndividualReset>
where
    B: Backend,
    S: Resettable,
{
    /// Reset command buffer.
    pub fn reset(self) -> CommandBuffer<B, C, InitialState, L, IndividualReset> {
        unsafe { self.change_state(|_| InitialState) }
    }
}

impl<B, C, S, L> CommandBuffer<B, C, S, L>
where
    B: Backend,
    S: Resettable,
{
    /// Mark command buffer as reset.
    ///
    /// # Safety
    ///
    /// * This function must be used only to reflect command buffer being reset implicitly.
    /// For instance:
    /// * [`CommandPool::reset`](struct.CommandPool.html#method.reset) on pool from which the command buffer was allocated.
    /// * Raw handle usage.
    pub unsafe fn mark_reset(self) -> CommandBuffer<B, C, InitialState, L> {
        self.change_state(|_| InitialState)
    }
}

impl<B, C, S, L, R> CommandBuffer<B, C, S, L, R>
where
    B: Backend,
    S: Resettable,
{
    /// Dispose of command buffer wrapper releasing raw comman buffer value.
    /// This function is intended to be used to deallocate command buffer.
    pub fn into_raw(self) -> B::CommandBuffer {
        self.relevant.dispose();
        unsafe {
            // state guarantees that raw command buffer is not shared.
            *Box::from_raw(self.raw.as_ptr())
        }
    }

    /// Get raw command buffer handle.
    pub fn raw(&mut self) -> &mut B::CommandBuffer {
        unsafe {
            // state guarantees that raw command buffer is not shared.
            self.raw.as_mut()
        }
    }
}