vulkano_taskgraph/command_buffer/commands/
dynamic_state.rs

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
use crate::command_buffer::{RecordingCommandBuffer, Result};
use ash::vk;
use smallvec::SmallVec;
use std::ops::RangeInclusive;
use vulkano::{
    device::DeviceOwned,
    pipeline::graphics::{
        color_blend::LogicOp,
        depth_stencil::{CompareOp, StencilFaces, StencilOp},
        fragment_shading_rate::FragmentShadingRateCombinerOp,
        input_assembly::PrimitiveTopology,
        rasterization::{ConservativeRasterizationMode, CullMode, FrontFace},
        vertex_input::{
            VertexInputAttributeDescription, VertexInputBindingDescription, VertexInputState,
        },
        viewport::{Scissor, Viewport},
    },
    Version, VulkanObject,
};

/// # Commands to set dynamic state for pipelines
///
/// These commands require a queue with a pipeline type that uses the given state.
impl RecordingCommandBuffer<'_> {
    /// Sets the dynamic blend constants for future draw calls.
    pub unsafe fn set_blend_constants(&mut self, constants: &[f32; 4]) -> Result<&mut Self> {
        Ok(unsafe { self.set_blend_constants_unchecked(constants) })
    }

    pub unsafe fn set_blend_constants_unchecked(&mut self, constants: &[f32; 4]) -> &mut Self {
        let fns = self.device().fns();
        unsafe { (fns.v1_0.cmd_set_blend_constants)(self.handle(), constants) };

        self
    }

    /// Sets whether dynamic color writes should be enabled for each attachment in the framebuffer.
    pub unsafe fn set_color_write_enable(&mut self, enables: &[bool]) -> Result<&mut Self> {
        Ok(unsafe { self.set_color_write_enable_unchecked(enables) })
    }

    pub unsafe fn set_color_write_enable_unchecked(&mut self, enables: &[bool]) -> &mut Self {
        if enables.is_empty() {
            return self;
        }

        let enables_vk = enables
            .iter()
            .copied()
            .map(|v| v as vk::Bool32)
            .collect::<SmallVec<[_; 4]>>();

        let fns = self.device().fns();
        unsafe {
            (fns.ext_color_write_enable.cmd_set_color_write_enable_ext)(
                self.handle(),
                enables_vk.len() as u32,
                enables_vk.as_ptr(),
            )
        };

        self
    }

    /// Sets the dynamic cull mode for future draw calls.
    pub unsafe fn set_cull_mode(&mut self, cull_mode: CullMode) -> Result<&mut Self> {
        Ok(unsafe { self.set_cull_mode_unchecked(cull_mode) })
    }

    pub unsafe fn set_cull_mode_unchecked(&mut self, cull_mode: CullMode) -> &mut Self {
        let fns = self.device().fns();
        let cmd_set_cull_mode = if self.device().api_version() >= Version::V1_3 {
            fns.v1_3.cmd_set_cull_mode
        } else {
            fns.ext_extended_dynamic_state.cmd_set_cull_mode_ext
        };

        unsafe { cmd_set_cull_mode(self.handle(), cull_mode.into()) };

        self
    }

    /// Sets the dynamic depth bias values for future draw calls.
    pub unsafe fn set_depth_bias(
        &mut self,
        constant_factor: f32,
        clamp: f32,
        slope_factor: f32,
    ) -> Result<&mut Self> {
        Ok(unsafe { self.set_depth_bias_unchecked(constant_factor, clamp, slope_factor) })
    }

    pub unsafe fn set_depth_bias_unchecked(
        &mut self,
        constant_factor: f32,
        clamp: f32,
        slope_factor: f32,
    ) -> &mut Self {
        let fns = self.device().fns();
        unsafe {
            (fns.v1_0.cmd_set_depth_bias)(self.handle(), constant_factor, clamp, slope_factor)
        };

        self
    }

    /// Sets whether dynamic depth bias is enabled for future draw calls.
    pub unsafe fn set_depth_bias_enable(&mut self, enable: bool) -> Result<&mut Self> {
        Ok(unsafe { self.set_depth_bias_enable_unchecked(enable) })
    }

    pub unsafe fn set_depth_bias_enable_unchecked(&mut self, enable: bool) -> &mut Self {
        let fns = self.device().fns();
        let cmd_set_depth_bias_enable = if self.device().api_version() >= Version::V1_3 {
            fns.v1_3.cmd_set_depth_bias_enable
        } else {
            fns.ext_extended_dynamic_state2
                .cmd_set_depth_bias_enable_ext
        };

        unsafe { cmd_set_depth_bias_enable(self.handle(), enable.into()) };

        self
    }

    /// Sets the dynamic depth bounds for future draw calls.
    pub unsafe fn set_depth_bounds(&mut self, bounds: RangeInclusive<f32>) -> Result<&mut Self> {
        Ok(unsafe { self.set_depth_bounds_unchecked(bounds.clone()) })
    }

    pub unsafe fn set_depth_bounds_unchecked(&mut self, bounds: RangeInclusive<f32>) -> &mut Self {
        let fns = self.device().fns();
        unsafe { (fns.v1_0.cmd_set_depth_bounds)(self.handle(), *bounds.start(), *bounds.end()) };

        self
    }

    /// Sets whether dynamic depth bounds testing is enabled for future draw calls.
    pub unsafe fn set_depth_bounds_test_enable(&mut self, enable: bool) -> Result<&mut Self> {
        Ok(unsafe { self.set_depth_bounds_test_enable_unchecked(enable) })
    }

    pub unsafe fn set_depth_bounds_test_enable_unchecked(&mut self, enable: bool) -> &mut Self {
        let fns = self.device().fns();
        let cmd_set_depth_bounds_test_enable = if self.device().api_version() >= Version::V1_3 {
            fns.v1_3.cmd_set_depth_bounds_test_enable
        } else {
            fns.ext_extended_dynamic_state
                .cmd_set_depth_bounds_test_enable_ext
        };

        unsafe { cmd_set_depth_bounds_test_enable(self.handle(), enable.into()) };

        self
    }

    /// Sets the dynamic depth compare op for future draw calls.
    pub unsafe fn set_depth_compare_op(&mut self, compare_op: CompareOp) -> Result<&mut Self> {
        Ok(unsafe { self.set_depth_compare_op_unchecked(compare_op) })
    }

    pub unsafe fn set_depth_compare_op_unchecked(&mut self, compare_op: CompareOp) -> &mut Self {
        let fns = self.device().fns();
        let cmd_set_depth_compare_op = if self.device().api_version() >= Version::V1_3 {
            fns.v1_3.cmd_set_depth_compare_op
        } else {
            fns.ext_extended_dynamic_state.cmd_set_depth_compare_op_ext
        };

        unsafe { cmd_set_depth_compare_op(self.handle(), compare_op.into()) };

        self
    }

    /// Sets whether dynamic depth testing is enabled for future draw calls.
    pub unsafe fn set_depth_test_enable(&mut self, enable: bool) -> Result<&mut Self> {
        Ok(unsafe { self.set_depth_test_enable_unchecked(enable) })
    }

    pub unsafe fn set_depth_test_enable_unchecked(&mut self, enable: bool) -> &mut Self {
        let fns = self.device().fns();
        let cmd_set_depth_test_enable = if self.device().api_version() >= Version::V1_3 {
            fns.v1_3.cmd_set_depth_test_enable
        } else {
            fns.ext_extended_dynamic_state.cmd_set_depth_test_enable_ext
        };

        unsafe { cmd_set_depth_test_enable(self.handle(), enable.into()) };

        self
    }

    /// Sets whether dynamic depth write is enabled for future draw calls.
    pub unsafe fn set_depth_write_enable(&mut self, enable: bool) -> Result<&mut Self> {
        Ok(unsafe { self.set_depth_write_enable_unchecked(enable) })
    }

    pub unsafe fn set_depth_write_enable_unchecked(&mut self, enable: bool) -> &mut Self {
        let fns = self.device().fns();
        let cmd_set_depth_write_enable = if self.device().api_version() >= Version::V1_3 {
            fns.v1_3.cmd_set_depth_write_enable
        } else {
            fns.ext_extended_dynamic_state
                .cmd_set_depth_write_enable_ext
        };

        unsafe { cmd_set_depth_write_enable(self.handle(), enable.into()) };

        self
    }

    /// Sets the dynamic discard rectangles for future draw calls.
    pub unsafe fn set_discard_rectangle(
        &mut self,
        first_rectangle: u32,
        rectangles: &[Scissor],
    ) -> Result<&mut Self> {
        Ok(unsafe { self.set_discard_rectangle_unchecked(first_rectangle, rectangles) })
    }

    pub unsafe fn set_discard_rectangle_unchecked(
        &mut self,
        first_rectangle: u32,
        rectangles: &[Scissor],
    ) -> &mut Self {
        if rectangles.is_empty() {
            return self;
        }

        let rectangles_vk = rectangles
            .iter()
            .map(Scissor::to_vk)
            .collect::<SmallVec<[_; 2]>>();

        let fns = self.device().fns();
        unsafe {
            (fns.ext_discard_rectangles.cmd_set_discard_rectangle_ext)(
                self.handle(),
                first_rectangle,
                rectangles_vk.len() as u32,
                rectangles_vk.as_ptr(),
            )
        };

        self
    }

    /// Sets the dynamic front face for future draw calls.
    pub unsafe fn set_front_face(&mut self, face: FrontFace) -> Result<&mut Self> {
        Ok(unsafe { self.set_front_face_unchecked(face) })
    }

    pub unsafe fn set_front_face_unchecked(&mut self, face: FrontFace) -> &mut Self {
        let fns = self.device().fns();
        let cmd_set_front_face = if self.device().api_version() >= Version::V1_3 {
            fns.v1_3.cmd_set_front_face
        } else {
            fns.ext_extended_dynamic_state.cmd_set_front_face_ext
        };

        unsafe { cmd_set_front_face(self.handle(), face.into()) };

        self
    }

    /// Sets the dynamic line stipple values for future draw calls.
    pub unsafe fn set_line_stipple(&mut self, factor: u32, pattern: u16) -> Result<&mut Self> {
        Ok(unsafe { self.set_line_stipple_unchecked(factor, pattern) })
    }

    pub unsafe fn set_line_stipple_unchecked(&mut self, factor: u32, pattern: u16) -> &mut Self {
        let fns = self.device().fns();
        unsafe {
            (fns.ext_line_rasterization.cmd_set_line_stipple_ext)(self.handle(), factor, pattern)
        };

        self
    }

    /// Sets the dynamic line width for future draw calls.
    pub unsafe fn set_line_width(&mut self, line_width: f32) -> Result<&mut Self> {
        Ok(unsafe { self.set_line_width_unchecked(line_width) })
    }

    pub unsafe fn set_line_width_unchecked(&mut self, line_width: f32) -> &mut Self {
        let fns = self.device().fns();
        unsafe { (fns.v1_0.cmd_set_line_width)(self.handle(), line_width) };

        self
    }

    /// Sets the dynamic logic op for future draw calls.
    pub unsafe fn set_logic_op(&mut self, logic_op: LogicOp) -> Result<&mut Self> {
        Ok(unsafe { self.set_logic_op_unchecked(logic_op) })
    }

    pub unsafe fn set_logic_op_unchecked(&mut self, logic_op: LogicOp) -> &mut Self {
        let fns = self.device().fns();
        unsafe {
            (fns.ext_extended_dynamic_state2.cmd_set_logic_op_ext)(self.handle(), logic_op.into())
        };

        self
    }

    /// Sets the dynamic number of patch control points for future draw calls.
    pub unsafe fn set_patch_control_points(&mut self, num: u32) -> Result<&mut Self> {
        Ok(unsafe { self.set_patch_control_points_unchecked(num) })
    }

    pub unsafe fn set_patch_control_points_unchecked(&mut self, num: u32) -> &mut Self {
        let fns = self.device().fns();
        unsafe {
            (fns.ext_extended_dynamic_state2
                .cmd_set_patch_control_points_ext)(self.handle(), num)
        };

        self
    }

    /// Sets whether dynamic primitive restart is enabled for future draw calls.
    pub unsafe fn set_primitive_restart_enable(&mut self, enable: bool) -> Result<&mut Self> {
        Ok(unsafe { self.set_primitive_restart_enable_unchecked(enable) })
    }

    pub unsafe fn set_primitive_restart_enable_unchecked(&mut self, enable: bool) -> &mut Self {
        let fns = self.device().fns();
        let cmd_set_primitive_restart_enable = if self.device().api_version() >= Version::V1_3 {
            fns.v1_3.cmd_set_primitive_restart_enable
        } else {
            fns.ext_extended_dynamic_state2
                .cmd_set_primitive_restart_enable_ext
        };

        unsafe { cmd_set_primitive_restart_enable(self.handle(), enable.into()) };

        self
    }

    /// Sets the dynamic primitive topology for future draw calls.
    pub unsafe fn set_primitive_topology(
        &mut self,
        topology: PrimitiveTopology,
    ) -> Result<&mut Self> {
        Ok(unsafe { self.set_primitive_topology_unchecked(topology) })
    }

    pub unsafe fn set_primitive_topology_unchecked(
        &mut self,
        topology: PrimitiveTopology,
    ) -> &mut Self {
        let fns = self.device().fns();
        let cmd_set_primitive_topology = if self.device().api_version() >= Version::V1_3 {
            fns.v1_3.cmd_set_primitive_topology
        } else {
            fns.ext_extended_dynamic_state
                .cmd_set_primitive_topology_ext
        };

        unsafe { cmd_set_primitive_topology(self.handle(), topology.into()) };

        self
    }

    /// Sets whether dynamic rasterizer discard is enabled for future draw calls.
    pub unsafe fn set_rasterizer_discard_enable(&mut self, enable: bool) -> Result<&mut Self> {
        Ok(unsafe { self.set_rasterizer_discard_enable_unchecked(enable) })
    }

    pub unsafe fn set_rasterizer_discard_enable_unchecked(&mut self, enable: bool) -> &mut Self {
        let fns = self.device().fns();
        let cmd_set_rasterizer_discard_enable = if self.device().api_version() >= Version::V1_3 {
            fns.v1_3.cmd_set_rasterizer_discard_enable
        } else {
            fns.ext_extended_dynamic_state2
                .cmd_set_rasterizer_discard_enable_ext
        };

        unsafe { cmd_set_rasterizer_discard_enable(self.handle(), enable.into()) };

        self
    }

    /// Sets the dynamic scissors for future draw calls.
    pub unsafe fn set_scissor(
        &mut self,
        first_scissor: u32,
        scissors: &[Scissor],
    ) -> Result<&mut Self> {
        Ok(unsafe { self.set_scissor_unchecked(first_scissor, scissors) })
    }

    pub unsafe fn set_scissor_unchecked(
        &mut self,
        first_scissor: u32,
        scissors: &[Scissor],
    ) -> &mut Self {
        if scissors.is_empty() {
            return self;
        }

        let scissors_vk = scissors
            .iter()
            .map(Scissor::to_vk)
            .collect::<SmallVec<[_; 2]>>();

        let fns = self.device().fns();
        unsafe {
            (fns.v1_0.cmd_set_scissor)(
                self.handle(),
                first_scissor,
                scissors_vk.len() as u32,
                scissors_vk.as_ptr(),
            )
        };

        self
    }

    /// Sets the dynamic scissors with count for future draw calls.
    pub unsafe fn set_scissor_with_count(&mut self, scissors: &[Scissor]) -> Result<&mut Self> {
        Ok(unsafe { self.set_scissor_with_count_unchecked(scissors) })
    }

    pub unsafe fn set_scissor_with_count_unchecked(&mut self, scissors: &[Scissor]) -> &mut Self {
        if scissors.is_empty() {
            return self;
        }

        let scissors_vk = scissors
            .iter()
            .map(Scissor::to_vk)
            .collect::<SmallVec<[_; 2]>>();

        let fns = self.device().fns();
        let cmd_set_scissor_with_count = if self.device().api_version() >= Version::V1_3 {
            fns.v1_3.cmd_set_scissor_with_count
        } else {
            fns.ext_extended_dynamic_state
                .cmd_set_scissor_with_count_ext
        };

        unsafe {
            cmd_set_scissor_with_count(
                self.handle(),
                scissors_vk.len() as u32,
                scissors_vk.as_ptr(),
            )
        };

        self
    }

    /// Sets the dynamic stencil compare mask on one or both faces for future draw calls.
    pub unsafe fn set_stencil_compare_mask(
        &mut self,
        faces: StencilFaces,
        compare_mask: u32,
    ) -> Result<&mut Self> {
        Ok(unsafe { self.set_stencil_compare_mask_unchecked(faces, compare_mask) })
    }

    pub unsafe fn set_stencil_compare_mask_unchecked(
        &mut self,
        faces: StencilFaces,
        compare_mask: u32,
    ) -> &mut Self {
        let fns = self.device().fns();
        unsafe {
            (fns.v1_0.cmd_set_stencil_compare_mask)(self.handle(), faces.into(), compare_mask)
        };

        self
    }

    /// Sets the dynamic stencil ops on one or both faces for future draw calls.
    pub unsafe fn set_stencil_op(
        &mut self,
        faces: StencilFaces,
        fail_op: StencilOp,
        pass_op: StencilOp,
        depth_fail_op: StencilOp,
        compare_op: CompareOp,
    ) -> Result<&mut Self> {
        Ok(unsafe {
            self.set_stencil_op_unchecked(faces, fail_op, pass_op, depth_fail_op, compare_op)
        })
    }

    pub unsafe fn set_stencil_op_unchecked(
        &mut self,
        faces: StencilFaces,
        fail_op: StencilOp,
        pass_op: StencilOp,
        depth_fail_op: StencilOp,
        compare_op: CompareOp,
    ) -> &mut Self {
        let fns = self.device().fns();
        let cmd_set_stencil_op = if self.device().api_version() >= Version::V1_3 {
            fns.v1_3.cmd_set_stencil_op
        } else {
            fns.ext_extended_dynamic_state.cmd_set_stencil_op_ext
        };

        unsafe {
            cmd_set_stencil_op(
                self.handle(),
                faces.into(),
                fail_op.into(),
                pass_op.into(),
                depth_fail_op.into(),
                compare_op.into(),
            )
        };

        self
    }

    /// Sets the dynamic stencil reference on one or both faces for future draw calls.
    pub unsafe fn set_stencil_reference(
        &mut self,
        faces: StencilFaces,
        reference: u32,
    ) -> Result<&mut Self> {
        Ok(unsafe { self.set_stencil_reference_unchecked(faces, reference) })
    }

    pub unsafe fn set_stencil_reference_unchecked(
        &mut self,
        faces: StencilFaces,
        reference: u32,
    ) -> &mut Self {
        let fns = self.device().fns();
        unsafe { (fns.v1_0.cmd_set_stencil_reference)(self.handle(), faces.into(), reference) };

        self
    }

    /// Sets whether dynamic stencil testing is enabled for future draw calls.
    pub unsafe fn set_stencil_test_enable(&mut self, enable: bool) -> Result<&mut Self> {
        Ok(unsafe { self.set_stencil_test_enable_unchecked(enable) })
    }

    pub unsafe fn set_stencil_test_enable_unchecked(&mut self, enable: bool) -> &mut Self {
        let fns = self.device().fns();
        let cmd_set_stencil_test_enable = if self.device().api_version() >= Version::V1_3 {
            fns.v1_3.cmd_set_stencil_test_enable
        } else {
            fns.ext_extended_dynamic_state
                .cmd_set_stencil_test_enable_ext
        };

        unsafe { cmd_set_stencil_test_enable(self.handle(), enable.into()) };

        self
    }

    /// Sets the dynamic stencil write mask on one or both faces for future draw calls.
    pub unsafe fn set_stencil_write_mask(
        &mut self,
        faces: StencilFaces,
        write_mask: u32,
    ) -> Result<&mut Self> {
        Ok(unsafe { self.set_stencil_write_mask_unchecked(faces, write_mask) })
    }

    pub unsafe fn set_stencil_write_mask_unchecked(
        &mut self,
        faces: StencilFaces,
        write_mask: u32,
    ) -> &mut Self {
        let fns = self.device().fns();
        unsafe { (fns.v1_0.cmd_set_stencil_write_mask)(self.handle(), faces.into(), write_mask) };

        self
    }

    /// Sets the dynamic vertex input for future draw calls.
    pub unsafe fn set_vertex_input(
        &mut self,
        vertex_input_state: &VertexInputState,
    ) -> Result<&mut Self> {
        Ok(unsafe { self.set_vertex_input_unchecked(vertex_input_state) })
    }

    pub unsafe fn set_vertex_input_unchecked(
        &mut self,
        vertex_input_state: &VertexInputState,
    ) -> &mut Self {
        let mut vertex_binding_descriptions_vk: SmallVec<[_; 8]> = SmallVec::new();
        let mut vertex_attribute_descriptions_vk: SmallVec<[_; 8]> = SmallVec::new();

        let VertexInputState {
            bindings,
            attributes,
            _ne: _,
        } = vertex_input_state;

        vertex_binding_descriptions_vk.extend(bindings.iter().map(|(&binding, binding_desc)| {
            let &VertexInputBindingDescription {
                stride,
                input_rate,
                _ne: _,
            } = binding_desc;

            let (input_rate, divisor) = input_rate.to_vk();

            vk::VertexInputBindingDescription2EXT {
                binding,
                stride,
                input_rate,
                divisor,
                ..Default::default()
            }
        }));

        vertex_attribute_descriptions_vk.extend(attributes.iter().map(
            |(&location, attribute_desc)| {
                let &VertexInputAttributeDescription {
                    binding,
                    format,
                    offset,
                    _ne: _,
                } = attribute_desc;

                vk::VertexInputAttributeDescription2EXT {
                    location,
                    binding,
                    format: format.into(),
                    offset,
                    ..Default::default()
                }
            },
        ));

        let fns = self.device().fns();
        unsafe {
            (fns.ext_vertex_input_dynamic_state.cmd_set_vertex_input_ext)(
                self.handle(),
                vertex_binding_descriptions_vk.len() as u32,
                vertex_binding_descriptions_vk.as_ptr(),
                vertex_attribute_descriptions_vk.len() as u32,
                vertex_attribute_descriptions_vk.as_ptr(),
            )
        };

        self
    }

    /// Sets the dynamic viewports for future draw calls.
    pub unsafe fn set_viewport(
        &mut self,
        first_viewport: u32,
        viewports: &[Viewport],
    ) -> Result<&mut Self> {
        Ok(unsafe { self.set_viewport_unchecked(first_viewport, viewports) })
    }

    pub unsafe fn set_viewport_unchecked(
        &mut self,
        first_viewport: u32,
        viewports: &[Viewport],
    ) -> &mut Self {
        if viewports.is_empty() {
            return self;
        }

        let viewports_vk = viewports
            .iter()
            .map(Viewport::to_vk)
            .collect::<SmallVec<[_; 2]>>();

        let fns = self.device().fns();
        unsafe {
            (fns.v1_0.cmd_set_viewport)(
                self.handle(),
                first_viewport,
                viewports_vk.len() as u32,
                viewports_vk.as_ptr(),
            )
        };

        self
    }

    /// Sets the dynamic viewports with count for future draw calls.
    pub unsafe fn set_viewport_with_count(&mut self, viewports: &[Viewport]) -> Result<&mut Self> {
        Ok(unsafe { self.set_viewport_with_count_unchecked(viewports) })
    }

    pub unsafe fn set_viewport_with_count_unchecked(
        &mut self,
        viewports: &[Viewport],
    ) -> &mut Self {
        if viewports.is_empty() {
            return self;
        }

        let viewports_vk = viewports
            .iter()
            .map(Viewport::to_vk)
            .collect::<SmallVec<[_; 2]>>();

        let fns = self.device().fns();
        let cmd_set_viewport_with_count = if self.device().api_version() >= Version::V1_3 {
            fns.v1_3.cmd_set_viewport_with_count
        } else {
            fns.ext_extended_dynamic_state
                .cmd_set_viewport_with_count_ext
        };

        unsafe {
            cmd_set_viewport_with_count(
                self.handle(),
                viewports_vk.len() as u32,
                viewports_vk.as_ptr(),
            )
        };

        self
    }

    /// Sets the dynamic conservative rasterization mode for future draw calls.
    pub unsafe fn set_conservative_rasterization_mode(
        &mut self,
        conservative_rasterization_mode: ConservativeRasterizationMode,
    ) -> Result<&mut Self> {
        Ok(unsafe {
            self.set_conservative_rasterization_mode_unchecked(conservative_rasterization_mode)
        })
    }

    pub unsafe fn set_conservative_rasterization_mode_unchecked(
        &mut self,
        conservative_rasterization_mode: ConservativeRasterizationMode,
    ) -> &mut Self {
        let fns = self.device().fns();
        unsafe {
            (fns.ext_extended_dynamic_state3
                .cmd_set_conservative_rasterization_mode_ext)(
                self.handle(),
                conservative_rasterization_mode.into(),
            )
        };

        self
    }

    /// Sets the dynamic extra primitive overestimation size for future draw calls.
    pub unsafe fn set_extra_primitive_overestimation_size(
        &mut self,
        extra_primitive_overestimation_size: f32,
    ) -> Result<&mut Self> {
        Ok(unsafe {
            self.set_extra_primitive_overestimation_size_unchecked(
                extra_primitive_overestimation_size,
            )
        })
    }

    pub unsafe fn set_extra_primitive_overestimation_size_unchecked(
        &mut self,
        extra_primitive_overestimation_size: f32,
    ) -> &mut Self {
        let fns = self.device().fns();
        unsafe {
            (fns.ext_extended_dynamic_state3
                .cmd_set_extra_primitive_overestimation_size_ext)(
                self.handle(),
                extra_primitive_overestimation_size,
            )
        };

        self
    }

    /// Sets the dynamic fragment shading rate for future draw calls.
    pub unsafe fn set_fragment_shading_rate(
        &mut self,
        fragment_size: [u32; 2],
        combiner_ops: [FragmentShadingRateCombinerOp; 2],
    ) -> Result<&mut Self> {
        Ok(unsafe { self.set_fragment_shading_rate_unchecked(fragment_size, combiner_ops) })
    }

    pub unsafe fn set_fragment_shading_rate_unchecked(
        &mut self,
        fragment_size: [u32; 2],
        combiner_ops: [FragmentShadingRateCombinerOp; 2],
    ) -> &mut Self {
        let fns = self.device().fns();
        let fragment_size = vk::Extent2D {
            width: fragment_size[0],
            height: fragment_size[1],
        };
        let combiner_ops = [combiner_ops[0].into(), combiner_ops[1].into()];
        unsafe {
            (fns.khr_fragment_shading_rate
                .cmd_set_fragment_shading_rate_khr)(
                self.handle(), &fragment_size, &combiner_ops
            )
        }

        self
    }
}