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
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
use {
    super::{
        level::{Level, PrimaryLevel, SecondaryLevel},
        state::RecordingState,
        submit::Submittable,
        usage::RenderPassContinue,
        CommandBuffer,
    },
    crate::{
        capability::{Capability, Compute, Graphics, Supports, Transfer},
        family::FamilyId,
    },
};

/// Draw command for [`draw_indirect`].

///

/// [`draw_indirect`]: ../struct.RenderPassEncoder.html#method.draw_indirect

#[repr(C)]
#[derive(Clone, Copy, Debug)]
pub struct DrawCommand {
    /// Number of vertices to draw.

    pub vertex_count: u32,

    /// Number of instanced to draw.

    pub instance_count: u32,

    /// First vertex index.

    pub first_vertex: u32,

    /// First instance index.

    pub first_instance: u32,
}

/// Draw command for [`draw_indexed_indirect`].

///

/// [`draw_indexed_indirect`]: ../struct.RenderPassEncoder.html#method.draw_indexed_indirect

#[repr(C)]
#[derive(Clone, Copy, Debug)]
pub struct DrawIndexedCommand {
    /// Number of indices to draw.

    pub index_count: u32,

    /// Number of instances to draw.

    pub instance_count: u32,

    /// First index.

    pub first_index: u32,

    /// Vertex offset that is added to index before indexing the vertex buffer.

    pub vertex_offset: i32,

    /// First instance index.

    pub first_instance: u32,
}

/// Draw command for dispatch.

#[repr(C)]
#[derive(Clone, Copy, Debug)]
pub struct DispatchCommand {
    /// Number of local workgroups to dispatch in the X dimension.

    pub x: u32,

    /// Number of local workgroups to dispatch in the Y dimension.

    pub y: u32,

    /// Number of local workgroups to dispatch in the Z dimension.

    pub z: u32,
}

/// Encoder for recording commands inside or outside renderpass.

#[derive(Debug)]
pub struct EncoderCommon<'a, B: rendy_core::hal::Backend, C> {
    raw: &'a mut B::CommandBuffer,
    capability: C,
    family: FamilyId,
}

impl<'a, B, C> EncoderCommon<'a, B, C>
where
    B: rendy_core::hal::Backend,
{
    /// Bind index buffer.

    /// Last bound index buffer is used in [`draw_indexed`] command.

    ///

    /// Note that `draw*` commands available only inside renderpass.

    ///

    /// [`draw_indexed`]: ../struct.RenderPassEncoder.html#method.draw_indexed

    ///

    /// # Safety

    ///

    /// `offset` must not be greater than the size of `buffer`.

    /// Sum of `offset` and starting address of the `buffer` must be

    /// multiple of index size indicated by `index_type`.

    ///

    /// See: https://www.khronos.org/registry/vulkan/specs/1.1-extensions/man/html/vkCmdBindIndexBuffer.html

    pub unsafe fn bind_index_buffer<'b>(
        &mut self,
        buffer: &'b B::Buffer,
        offset: u64,
        index_type: rendy_core::hal::IndexType,
    ) where
        C: Supports<Graphics>,
    {
        self.capability.assert();
        rendy_core::hal::command::CommandBuffer::bind_index_buffer(
            self.raw,
            rendy_core::hal::buffer::IndexBufferView {
                buffer: buffer,
                offset,
                index_type,
            },
        )
    }

    /// Bind vertex buffers.

    /// Last bound vertex buffer is used in [`draw`] and [`draw_indexed`] commands.

    ///

    /// Note that `draw*` commands available only inside renderpass.

    ///

    /// [`draw`]: ../struct.RenderPassEncoder.html#method.draw

    /// [`draw_indexed`]: ../struct.RenderPassEncoder.html#method.draw_indexed

    ///

    /// # Safety

    ///

    /// `first_binding + buffers.into_iter().count()` must less than or equal to the `maxVertexInputBindings`

    /// device limit.

    ///

    /// See: https://www.khronos.org/registry/vulkan/specs/1.1-extensions/man/html/vkCmdBindVertexBuffers.html

    pub unsafe fn bind_vertex_buffers<'b>(
        &mut self,
        first_binding: u32,
        buffers: impl IntoIterator<Item = (&'b B::Buffer, u64)>,
    ) where
        C: Supports<Graphics>,
    {
        self.capability.assert();
        rendy_core::hal::command::CommandBuffer::bind_vertex_buffers(
            self.raw,
            first_binding,
            buffers,
        )
    }

    /// Bind graphics pipeline.

    ///

    /// Last bound vertex buffer is used in [`draw`], [`draw_indexed`], [`draw_indirect`] and [`draw_indexed_indirect`] commands.

    ///

    /// [`draw_indexed_indirect`]: ../struct.RenderPassEncoder.html#method.draw_indexed_indirect

    /// [`draw_indirect`]: ../struct.RenderPassEncoder.html#method.draw_indirect

    pub fn bind_graphics_pipeline(&mut self, pipeline: &B::GraphicsPipeline)
    where
        C: Supports<Graphics>,
    {
        self.capability.assert();

        unsafe {
            rendy_core::hal::command::CommandBuffer::bind_graphics_pipeline(self.raw, pipeline);
        }
    }

    /// Bind descriptor sets to graphics pipeline.

    ///

    /// # Safety

    ///

    /// See: https://www.khronos.org/registry/vulkan/specs/1.1-extensions/man/html/vkCmdBindDescriptorSets.html

    pub unsafe fn bind_graphics_descriptor_sets<'b>(
        &mut self,
        layout: &B::PipelineLayout,
        first_set: u32,
        sets: impl IntoIterator<Item = &'b B::DescriptorSet>,
        offsets: impl IntoIterator<Item = u32>,
    ) where
        C: Supports<Graphics>,
    {
        self.capability.assert();

        rendy_core::hal::command::CommandBuffer::bind_graphics_descriptor_sets(
            self.raw,
            layout,
            first_set as _,
            sets,
            offsets,
        );
    }

    /// Bind compute pipeline.

    pub fn bind_compute_pipeline(&mut self, pipeline: &B::ComputePipeline)
    where
        C: Supports<Compute>,
    {
        self.capability.assert();

        unsafe {
            rendy_core::hal::command::CommandBuffer::bind_compute_pipeline(self.raw, pipeline);
        }
    }

    /// Bind descriptor sets to compute pipeline.

    ///

    /// # Safety

    ///

    /// See: https://www.khronos.org/registry/vulkan/specs/1.1-extensions/man/html/vkCmdBindDescriptorSets.html

    pub unsafe fn bind_compute_descriptor_sets<'b>(
        &mut self,
        layout: &B::PipelineLayout,
        first_set: u32,
        sets: impl IntoIterator<Item = &'b B::DescriptorSet>,
        offsets: impl IntoIterator<Item = u32>,
    ) where
        C: Supports<Compute>,
    {
        self.capability.assert();

        rendy_core::hal::command::CommandBuffer::bind_compute_descriptor_sets(
            self.raw,
            layout,
            first_set as usize,
            sets,
            offsets,
        );
    }

    /// Insert pipeline barrier.

    ///

    /// # Safety

    ///

    /// See: https://www.khronos.org/registry/vulkan/specs/1.1-extensions/man/html/vkCmdPipelineBarrier.html

    pub unsafe fn pipeline_barrier<'b>(
        &mut self,
        stages: std::ops::Range<rendy_core::hal::pso::PipelineStage>,
        dependencies: rendy_core::hal::memory::Dependencies,
        barriers: impl IntoIterator<Item = rendy_core::hal::memory::Barrier<'b, B>>,
    ) {
        rendy_core::hal::command::CommandBuffer::pipeline_barrier(
            self.raw,
            stages,
            dependencies,
            barriers,
        )
    }

    /// Push graphics constants.

    ///

    /// # Safety

    ///

    /// `offset` must be multiple of 4.

    /// `constants.len() + offset`, must be less than or equal to the

    /// `maxPushConstantsSize` device limit.

    ///

    /// See: https://www.khronos.org/registry/vulkan/specs/1.1-extensions/man/html/vkCmdPushConstants.html

    pub unsafe fn push_constants<'b>(
        &mut self,
        layout: &B::PipelineLayout,
        stages: rendy_core::hal::pso::ShaderStageFlags,
        offset: u32,
        constants: &[u32],
    ) {
        rendy_core::hal::command::CommandBuffer::push_graphics_constants(
            self.raw, layout, stages, offset, constants,
        );
    }

    /// Set viewports

    ///

    /// See: https://www.khronos.org/registry/vulkan/specs/1.1-extensions/man/html/vkCmdSetViewport.html

    pub unsafe fn set_viewports<'b>(
        &mut self,
        first_viewport: u32,
        viewports: impl IntoIterator<Item = &'b rendy_core::hal::pso::Viewport>,
    ) where
        C: Supports<Graphics>,
    {
        self.capability.assert();
        rendy_core::hal::command::CommandBuffer::set_viewports(self.raw, first_viewport, viewports)
    }

    /// Set scissors

    ///

    /// # Safety

    ///

    /// `first_scissor + rects.count()` must be less than the

    /// `maxViewports` device limit.

    ///

    /// See: https://www.khronos.org/registry/vulkan/specs/1.1-extensions/man/html/vkCmdSetScissor.html

    pub unsafe fn set_scissors<'b>(
        &mut self,
        first_scissor: u32,
        rects: impl IntoIterator<Item = &'b rendy_core::hal::pso::Rect>,
    ) where
        C: Supports<Graphics>,
    {
        self.capability.assert();
        rendy_core::hal::command::CommandBuffer::set_scissors(self.raw, first_scissor, rects)
    }

    /// Set the stencil reference dynamic state

    ///

    /// See: https://www.khronos.org/registry/vulkan/specs/1.1-extensions/man/html/vkCmdSetStencilReference.html

    pub unsafe fn set_stencil_reference(
        &mut self,
        faces: rendy_core::hal::pso::Face,
        value: rendy_core::hal::pso::StencilValue,
    ) where
        C: Supports<Graphics>,
    {
        self.capability.assert();
        rendy_core::hal::command::CommandBuffer::set_stencil_reference(self.raw, faces, value);
    }

    /// Set the stencil compare mask dynamic state

    ///

    /// See: https://www.khronos.org/registry/vulkan/specs/1.1-extensions/man/html/vkCmdSetStencilCompareMask.html

    pub unsafe fn set_stencil_read_mask(
        &mut self,
        faces: rendy_core::hal::pso::Face,
        value: rendy_core::hal::pso::StencilValue,
    ) where
        C: Supports<Graphics>,
    {
        self.capability.assert();
        rendy_core::hal::command::CommandBuffer::set_stencil_read_mask(self.raw, faces, value);
    }

    /// Set the stencil write mask dynamic state

    ///

    /// See: https://www.khronos.org/registry/vulkan/specs/1.1-extensions/man/html/vkCmdSetStencilWriteMask.html

    pub unsafe fn set_stencil_write_mask(
        &mut self,
        faces: rendy_core::hal::pso::Face,
        value: rendy_core::hal::pso::StencilValue,
    ) where
        C: Supports<Graphics>,
    {
        self.capability.assert();
        rendy_core::hal::command::CommandBuffer::set_stencil_write_mask(self.raw, faces, value);
    }

    /// Set the values of blend constants

    ///

    /// See: https://www.khronos.org/registry/vulkan/specs/1.1-extensions/man/html/vkCmdSetBlendConstants.html

    pub unsafe fn set_blend_constants(&mut self, color: rendy_core::hal::pso::ColorValue)
    where
        C: Supports<Graphics>,
    {
        self.capability.assert();
        rendy_core::hal::command::CommandBuffer::set_blend_constants(self.raw, color);
    }

    /// Set the depth bounds test values

    ///

    /// See: https://www.khronos.org/registry/vulkan/specs/1.1-extensions/man/html/vkCmdSetDepthBounds.html

    pub unsafe fn set_depth_bounds(&mut self, bounds: std::ops::Range<f32>)
    where
        C: Supports<Graphics>,
    {
        self.capability.assert();
        rendy_core::hal::command::CommandBuffer::set_depth_bounds(self.raw, bounds);
    }

    /// Set the dynamic line width state

    ///

    /// See: https://www.khronos.org/registry/vulkan/specs/1.1-extensions/man/html/vkCmdSetLineWidth.html

    pub unsafe fn set_line_width(&mut self, width: f32)
    where
        C: Supports<Graphics>,
    {
        self.capability.assert();
        rendy_core::hal::command::CommandBuffer::set_line_width(self.raw, width);
    }

    /// Set the depth bias dynamic state

    ///

    /// See: https://www.khronos.org/registry/vulkan/specs/1.1-extensions/man/html/vkCmdSetDepthBias.html

    pub unsafe fn set_depth_bias(&mut self, depth_bias: rendy_core::hal::pso::DepthBias)
    where
        C: Supports<Graphics>,
    {
        self.capability.assert();
        rendy_core::hal::command::CommandBuffer::set_depth_bias(self.raw, depth_bias);
    }

    /// Reborrow encoder.

    pub fn reborrow<K>(&mut self) -> EncoderCommon<'_, B, K>
    where
        C: Supports<K>,
    {
        EncoderCommon {
            capability: self.capability.supports().unwrap(),
            raw: &mut *self.raw,
            family: self.family,
        }
    }
}

/// Special encoder to record render-pass commands.

#[derive(Debug)]
pub struct RenderPassEncoder<'a, B: rendy_core::hal::Backend> {
    inner: EncoderCommon<'a, B, Graphics>,
}

impl<'a, B> std::ops::Deref for RenderPassEncoder<'a, B>
where
    B: rendy_core::hal::Backend,
{
    type Target = EncoderCommon<'a, B, Graphics>;

    fn deref(&self) -> &EncoderCommon<'a, B, Graphics> {
        &self.inner
    }
}

impl<'a, B> std::ops::DerefMut for RenderPassEncoder<'a, B>
where
    B: rendy_core::hal::Backend,
{
    fn deref_mut(&mut self) -> &mut EncoderCommon<'a, B, Graphics> {
        &mut self.inner
    }
}

impl<'a, B> RenderPassEncoder<'a, B>
where
    B: rendy_core::hal::Backend,
{
    /// Clear regions within bound framebuffer attachments

    ///

    /// See: https://www.khronos.org/registry/vulkan/specs/1.1-extensions/man/html/vkCmdClearAttachments.html#vkCmdBeginRenderPass

    pub unsafe fn clear_attachments(
        &mut self,
        clears: impl IntoIterator<
            Item = impl std::borrow::Borrow<rendy_core::hal::command::AttachmentClear>,
        >,
        rects: impl IntoIterator<Item = impl std::borrow::Borrow<rendy_core::hal::pso::ClearRect>>,
    ) {
        rendy_core::hal::command::CommandBuffer::clear_attachments(self.inner.raw, clears, rects);
    }

    /// Draw.

    ///

    /// # Safety

    ///

    /// The range of `vertices` must not exceed the size of the currently bound vertex buffer,

    /// and the range of `instances` must not exceed the size of the currently bound instance

    /// buffer.

    ///

    /// See: https://www.khronos.org/registry/vulkan/specs/1.1-extensions/man/html/vkCmdDraw.html

    pub unsafe fn draw(&mut self, vertices: std::ops::Range<u32>, instances: std::ops::Range<u32>) {
        rendy_core::hal::command::CommandBuffer::draw(self.inner.raw, vertices, instances)
    }

    /// Draw indexed, with `base_vertex` specifying an offset that is treated as

    /// vertex number 0.

    ///

    /// # Safety

    ///

    /// Same as `draw()`, plus the value of `base_vertex`.  So, `base_vertex + indices.end`

    /// must not be larger than the currently bound vertex buffer.

    ///

    /// See: https://www.khronos.org/registry/vulkan/specs/1.1-extensions/man/html/vkCmdDrawIndexed.html

    pub unsafe fn draw_indexed(
        &mut self,
        indices: std::ops::Range<u32>,
        base_vertex: i32,
        instances: std::ops::Range<u32>,
    ) {
        rendy_core::hal::command::CommandBuffer::draw_indexed(
            self.inner.raw,
            indices,
            base_vertex,
            instances,
        )
    }

    /// Draw indirect.

    /// Similar to [`draw`] except takes vertices and instance data from `buffer` at specified `offset`.

    /// `buffer` must contain `draw_count` of [`DrawCommand`] starting from `offset` with `stride` bytes between each.

    ///

    /// [`draw`]: trait.RenderPassInlineEncoder.html#tymethod.draw

    /// [`DrawCommand`]: struct.DrawCommand.html

    ///

    /// # Safety

    ///

    /// Similar to `draw()`.

    ///

    /// See: https://www.khronos.org/registry/vulkan/specs/1.1-extensions/man/html/vkCmdDrawIndirect.html

    pub unsafe fn draw_indirect(
        &mut self,
        buffer: &B::Buffer,
        offset: u64,
        draw_count: u32,
        stride: u32,
    ) {
        rendy_core::hal::command::CommandBuffer::draw_indirect(
            self.inner.raw,
            buffer,
            offset,
            draw_count,
            stride,
        )
    }

    /// Draw indirect with indices.

    /// Similar to [`draw_indexed`] except takes vertices, indices and instance data from `buffer` at specified `offset`.

    /// `buffer` must contain `draw_count` of [`DrawIndexedCommand`] starting from `offset` with `stride` bytes between each.

    ///

    /// [`draw`]: trait.RenderPassInlineEncoder.html#tymethod.draw_indexed

    /// [`DrawIndexedCommand`]: struct.DrawIndexedCommand.html

    ///

    /// # Safety

    ///

    /// Similar to `draw_indexed()`

    ///

    /// See: https://www.khronos.org/registry/vulkan/specs/1.1-extensions/man/html/vkCmdDrawIndexedIndirect.html

    pub unsafe fn draw_indexed_indirect(
        &mut self,
        buffer: &B::Buffer,
        offset: u64,
        draw_count: u32,
        stride: u32,
    ) {
        rendy_core::hal::command::CommandBuffer::draw_indexed_indirect(
            self.inner.raw,
            buffer,
            offset,
            draw_count,
            stride,
        )
    }

    /// Reborrow encoder.

    pub fn reborrow(&mut self) -> RenderPassEncoder<'_, B> {
        RenderPassEncoder {
            inner: self.inner.reborrow(),
        }
    }
}

/// Special encoder to record commands inside render pass.

#[derive(Debug)]
pub struct RenderPassInlineEncoder<'a, B: rendy_core::hal::Backend> {
    inner: RenderPassEncoder<'a, B>,
}

impl<'a, B> Drop for RenderPassInlineEncoder<'a, B>
where
    B: rendy_core::hal::Backend,
{
    fn drop(&mut self) {
        unsafe { rendy_core::hal::command::CommandBuffer::end_render_pass(self.inner.inner.raw) }
    }
}

impl<'a, B> std::ops::Deref for RenderPassInlineEncoder<'a, B>
where
    B: rendy_core::hal::Backend,
{
    type Target = RenderPassEncoder<'a, B>;

    fn deref(&self) -> &RenderPassEncoder<'a, B> {
        &self.inner
    }
}

impl<'a, B> std::ops::DerefMut for RenderPassInlineEncoder<'a, B>
where
    B: rendy_core::hal::Backend,
{
    fn deref_mut(&mut self) -> &mut RenderPassEncoder<'a, B> {
        &mut self.inner
    }
}

impl<'a, B> RenderPassInlineEncoder<'a, B>
where
    B: rendy_core::hal::Backend,
{
    /// Record next subpass inline.

    pub fn next_subpass_inline(self) -> RenderPassInlineEncoder<'a, B> {
        unsafe {
            rendy_core::hal::command::CommandBuffer::next_subpass(
                self.inner.inner.raw,
                rendy_core::hal::command::SubpassContents::Inline,
            );
        }

        self
    }

    /// Record next subpass secondary.

    pub fn next_subpass_secondary(self) -> RenderPassSecondaryEncoder<'a, B> {
        unsafe {
            rendy_core::hal::command::CommandBuffer::next_subpass(
                self.inner.inner.raw,
                rendy_core::hal::command::SubpassContents::SecondaryBuffers,
            );
        }

        unsafe {
            let next = RenderPassSecondaryEncoder {
                inner: std::ptr::read(&self.inner.inner),
            };

            std::mem::forget(self);
            next
        }
    }
}

/// Special encoder to execute secondary buffers inside render pass.

#[derive(Debug)]
pub struct RenderPassSecondaryEncoder<'a, B: rendy_core::hal::Backend> {
    inner: EncoderCommon<'a, B, Graphics>,
}

impl<'a, B> Drop for RenderPassSecondaryEncoder<'a, B>
where
    B: rendy_core::hal::Backend,
{
    fn drop(&mut self) {
        unsafe { rendy_core::hal::command::CommandBuffer::end_render_pass(self.inner.raw) }
    }
}

impl<'a, B> RenderPassSecondaryEncoder<'a, B>
where
    B: rendy_core::hal::Backend,
{
    /// Execute commands from secondary buffers.

    pub fn execute_commands(
        &mut self,
        submittables: impl IntoIterator<Item = impl Submittable<B, SecondaryLevel, RenderPassContinue>>,
    ) {
        let family = self.inner.family;
        unsafe {
            rendy_core::hal::command::CommandBuffer::execute_commands(
                self.inner.raw,
                submittables.into_iter().map(|submit| {
                    assert_eq!(family, submit.family());
                    submit.raw()
                }),
            )
        }
    }

    /// Record next subpass inline.

    pub fn next_subpass_inline(self) -> RenderPassInlineEncoder<'a, B> {
        unsafe {
            rendy_core::hal::command::CommandBuffer::next_subpass(
                self.inner.raw,
                rendy_core::hal::command::SubpassContents::Inline,
            );

            let next = RenderPassInlineEncoder {
                inner: RenderPassEncoder {
                    inner: std::ptr::read(&self.inner),
                },
            };

            std::mem::forget(self);
            next
        }
    }

    /// Record next subpass secondary.

    pub fn next_subpass_secondary(self) -> RenderPassSecondaryEncoder<'a, B> {
        unsafe {
            rendy_core::hal::command::CommandBuffer::next_subpass(
                self.inner.raw,
                rendy_core::hal::command::SubpassContents::SecondaryBuffers,
            );
        }

        self
    }
}

/// Trait to encode commands outside render pass.

#[derive(Debug)]
pub struct Encoder<'a, B: rendy_core::hal::Backend, C, L> {
    inner: EncoderCommon<'a, B, C>,
    level: L,
}

impl<'a, B, C, L> std::ops::Deref for Encoder<'a, B, C, L>
where
    B: rendy_core::hal::Backend,
{
    type Target = EncoderCommon<'a, B, C>;

    fn deref(&self) -> &EncoderCommon<'a, B, C> {
        &self.inner
    }
}

impl<'a, B, C, L> std::ops::DerefMut for Encoder<'a, B, C, L>
where
    B: rendy_core::hal::Backend,
{
    fn deref_mut(&mut self) -> &mut EncoderCommon<'a, B, C> {
        &mut self.inner
    }
}

impl<'a, B, C> Encoder<'a, B, C, PrimaryLevel>
where
    B: rendy_core::hal::Backend,
{
    /// Beging recording render pass inline.

    pub fn begin_render_pass_inline(
        &mut self,
        render_pass: &B::RenderPass,
        framebuffer: &B::Framebuffer,
        render_area: rendy_core::hal::pso::Rect,
        clear_values: &[rendy_core::hal::command::ClearValue],
    ) -> RenderPassInlineEncoder<'_, B>
    where
        C: Supports<Graphics>,
    {
        self.capability.assert();

        unsafe {
            rendy_core::hal::command::CommandBuffer::begin_render_pass(
                self.inner.raw,
                render_pass,
                framebuffer,
                render_area,
                clear_values,
                rendy_core::hal::command::SubpassContents::Inline,
            )
        }

        RenderPassInlineEncoder {
            inner: RenderPassEncoder {
                inner: self.inner.reborrow(),
            },
        }
    }

    /// Beging recording render pass secondary.

    pub fn begin_render_pass_secondary(
        &mut self,
        render_pass: &B::RenderPass,
        framebuffer: &B::Framebuffer,
        render_area: rendy_core::hal::pso::Rect,
        clear_values: &[rendy_core::hal::command::ClearValue],
    ) -> RenderPassSecondaryEncoder<'_, B>
    where
        C: Supports<Graphics>,
    {
        self.capability.assert();

        unsafe {
            rendy_core::hal::command::CommandBuffer::begin_render_pass(
                self.inner.raw,
                render_pass,
                framebuffer,
                render_area,
                clear_values,
                rendy_core::hal::command::SubpassContents::SecondaryBuffers,
            )
        }

        RenderPassSecondaryEncoder {
            inner: self.inner.reborrow(),
        }
    }

    /// Execute commands from secondary buffers.

    pub fn execute_commands(
        &mut self,
        submittables: impl IntoIterator<Item = impl Submittable<B, SecondaryLevel>>,
    ) {
        let family = self.inner.family;
        unsafe {
            rendy_core::hal::command::CommandBuffer::execute_commands(
                self.inner.raw,
                submittables.into_iter().map(|submit| {
                    assert_eq!(family, submit.family());
                    submit.raw()
                }),
            )
        }
    }
}

impl<'a, B, C, L> Encoder<'a, B, C, L>
where
    B: rendy_core::hal::Backend,
{
    /// Get encoder level.

    pub fn level(&self) -> L
    where
        L: Level,
    {
        self.level
    }

    /// Copy buffer regions.

    /// `src` and `dst` can be the same buffer or alias in memory.

    /// But regions must not overlap.

    /// Otherwise resulting values are undefined.

    ///

    /// # Safety

    ///

    /// The size of the copy region in any `regions` must not exceed the

    /// length of the corresponding buffer.

    ///

    /// See: https://www.khronos.org/registry/vulkan/specs/1.1-extensions/man/html/vkCmdCopyBuffer.html

    pub unsafe fn copy_buffer(
        &mut self,
        src: &B::Buffer,
        dst: &B::Buffer,
        regions: impl IntoIterator<Item = rendy_core::hal::command::BufferCopy>,
    ) where
        C: Supports<Transfer>,
    {
        self.capability.assert();

        rendy_core::hal::command::CommandBuffer::copy_buffer(self.inner.raw, src, dst, regions)
    }

    /// Copy buffer region to image subresource range.

    ///

    /// # Safety

    ///

    /// Same as `copy_buffer()`

    ///

    /// See: https://www.khronos.org/registry/vulkan/specs/1.1-extensions/man/html/vkCmdCopyBufferToImage.html

    pub unsafe fn copy_buffer_to_image(
        &mut self,
        src: &B::Buffer,
        dst: &B::Image,
        dst_layout: rendy_core::hal::image::Layout,
        regions: impl IntoIterator<Item = rendy_core::hal::command::BufferImageCopy>,
    ) where
        C: Supports<Transfer>,
    {
        self.capability.assert();

        rendy_core::hal::command::CommandBuffer::copy_buffer_to_image(
            self.inner.raw,
            src,
            dst,
            dst_layout,
            regions,
        )
    }

    /// Copy image regions.

    ///

    /// # Safety

    ///

    /// Same as `copy_buffer()`

    ///

    /// See: https://www.khronos.org/registry/vulkan/specs/1.1-extensions/man/html/vkCmdCopyImage.html

    pub unsafe fn copy_image(
        &mut self,
        src: &B::Image,
        src_layout: rendy_core::hal::image::Layout,
        dst: &B::Image,
        dst_layout: rendy_core::hal::image::Layout,
        regions: impl IntoIterator<Item = rendy_core::hal::command::ImageCopy>,
    ) where
        C: Supports<Transfer>,
    {
        self.capability.assert();

        rendy_core::hal::command::CommandBuffer::copy_image(
            self.inner.raw,
            src,
            src_layout,
            dst,
            dst_layout,
            regions,
        )
    }

    /// Blit image regions, potentially using specified filter when resize is necessary.

    ///

    /// # Safety

    ///

    /// Same as `copy_buffer()`

    ///

    /// See: https://www.khronos.org/registry/vulkan/specs/1.1-extensions/man/html/vkCmdBlitImage.html

    pub unsafe fn blit_image(
        &mut self,
        src: &B::Image,
        src_layout: rendy_core::hal::image::Layout,
        dst: &B::Image,
        dst_layout: rendy_core::hal::image::Layout,
        filter: rendy_core::hal::image::Filter,
        regions: impl IntoIterator<Item = rendy_core::hal::command::ImageBlit>,
    ) where
        C: Supports<Graphics>,
    {
        self.capability.assert();

        rendy_core::hal::command::CommandBuffer::blit_image(
            self.inner.raw,
            src,
            src_layout,
            dst,
            dst_layout,
            filter,
            regions,
        )
    }

    /// Dispatch compute.

    ///

    /// # Safety

    ///

    /// See: https://www.khronos.org/registry/vulkan/specs/1.1-extensions/man/html/vkCmdDispatch.html

    pub unsafe fn dispatch(&mut self, x: u32, y: u32, z: u32)
    where
        C: Supports<Compute>,
    {
        self.capability.assert();

        rendy_core::hal::command::CommandBuffer::dispatch(self.inner.raw, [x, y, z])
    }

    /// Dispatch indirect.

    /// Similar to [`dispatch`] except takes vertices and indices from `buffer` at specified `offset`.

    /// `buffer` must contain [`DispatchCommand`] at `offset`.

    ///

    /// [`dispatch`]: trait.Encoder.html#tymethod.dispatch

    /// [`DispatchCommand`]: struct.DispatchCommand.html

    ///

    /// # Safety

    ///

    /// See: https://www.khronos.org/registry/vulkan/specs/1.1-extensions/man/html/vkCmdDispatchIndirect.html

    pub unsafe fn dispatch_indirect(&mut self, buffer: &B::Buffer, offset: u64)
    where
        C: Supports<Compute>,
    {
        self.capability.assert();

        rendy_core::hal::command::CommandBuffer::dispatch_indirect(self.inner.raw, buffer, offset)
    }
}

impl<B, C, U, L, R> CommandBuffer<B, C, RecordingState<U>, L, R>
where
    B: rendy_core::hal::Backend,
    C: Capability,
    L: Level,
{
    /// Get encoder that will encode commands into this command buffer.

    pub fn encoder(&mut self) -> Encoder<'_, B, C, L> {
        Encoder {
            level: self.level,
            inner: EncoderCommon {
                capability: self.capability,
                family: self.family,
                raw: self.raw(),
            },
        }
    }
}

impl<B, C, U, R> CommandBuffer<B, C, RecordingState<U, RenderPassContinue>, SecondaryLevel, R>
where
    B: rendy_core::hal::Backend,
    C: Supports<Graphics>,
{
    /// Get encoder that will encode render-pass commands into this command buffer.

    pub fn render_pass_encoder(&mut self) -> RenderPassEncoder<'_, B> {
        RenderPassEncoder {
            inner: EncoderCommon {
                capability: self.capability.supports().unwrap(),
                family: self.family,
                raw: self.raw(),
            },
        }
    }
}