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
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
//! [Vulkan 1.2](https://registry.khronos.org/vulkan/specs/1.3-extensions/html/index.html) interface
//! based on smart pointers.
//!
//! # Resources
//!
//! Each resource contains an opaque Vulkan object handle and an information structure which
//! describes the object. Resources also contain an atomic [`AccessType`] state value which is used to
//! maintain consistency in any system which accesses the resource.
//!
//! The following resources are available:
//!
//! - [`AccelerationStructure`](accel_struct::AccelerationStructure)
//! - [`Buffer`](buffer::Buffer)
//! - [`Image`](image::Image)
//!
//! # Pipelines
//!
//! Pipelines allow you to run shader code which read and write resources using graphics hardware.
//!
//! Each pipeline contains an opaque Vulkan object handle and an information structure which
//! describes the configuration and shaders. They are immutable once created.
//!
//! The following pipelines are available:
//!
//! - [`ComputePipeline`](compute::ComputePipeline)
//! - [`GraphicPipeline`](graphic::GraphicPipeline)
//! - [`RayTracePipeline`](ray_trace::RayTracePipeline)

pub mod accel_struct;
pub mod buffer;
pub mod compute;
pub mod graphic;
pub mod image;
pub mod ray_trace;
pub mod shader;

mod cmd_buf;
mod descriptor_set;
mod descriptor_set_layout;
mod device;
mod instance;
mod physical_device;
mod render_pass;
mod surface;
mod swapchain;

pub use {
    self::{
        device::{Device, FeatureFlags},
        physical_device::{PhysicalDevice, QueueFamily, QueueFamilyProperties},
    },
    ash::{self},
    vk_sync::AccessType,
};

pub(crate) use self::{
    cmd_buf::CommandBuffer,
    descriptor_set::{DescriptorPool, DescriptorPoolInfo, DescriptorSet},
    descriptor_set_layout::DescriptorSetLayout,
    instance::Instance,
    render_pass::{
        AttachmentInfo, AttachmentRef, FramebufferKey, FramebufferKeyAttachment, RenderPass,
        RenderPassInfo, SubpassDependency, SubpassInfo,
    },
    shader::{DescriptorBinding, DescriptorBindingMap, DescriptorInfo},
    surface::Surface,
    swapchain::{Swapchain, SwapchainError, SwapchainImage, SwapchainInfo},
};

use {
    self::{
        buffer::{Buffer, BufferInfo},
        graphic::{DepthStencilMode, GraphicPipeline, VertexInputState},
        image::{Image, ImageInfo, ImageType, SampleCount},
    },
    ash::vk,
    derive_builder::{Builder, UninitializedFieldError},
    log::{debug, info, trace, warn},
    raw_window_handle::HasRawWindowHandle,
    std::{
        cmp::Ordering,
        error::Error,
        ffi::CStr,
        fmt::{Display, Formatter},
        ops::{Deref, Range},
        os::raw::c_char,
        sync::Arc,
    },
    vk_sync::ImageLayout,
};

const fn access_type_from_u8(access: u8) -> AccessType {
    match access {
        0 => AccessType::Nothing,
        1 => AccessType::CommandBufferReadNVX,
        2 => AccessType::IndirectBuffer,
        3 => AccessType::IndexBuffer,
        4 => AccessType::VertexBuffer,
        5 => AccessType::VertexShaderReadUniformBuffer,
        6 => AccessType::VertexShaderReadSampledImageOrUniformTexelBuffer,
        7 => AccessType::VertexShaderReadOther,
        8 => AccessType::TessellationControlShaderReadUniformBuffer,
        9 => AccessType::TessellationControlShaderReadSampledImageOrUniformTexelBuffer,
        10 => AccessType::TessellationControlShaderReadOther,
        11 => AccessType::TessellationEvaluationShaderReadUniformBuffer,
        12 => AccessType::TessellationEvaluationShaderReadSampledImageOrUniformTexelBuffer,
        13 => AccessType::TessellationEvaluationShaderReadOther,
        14 => AccessType::GeometryShaderReadUniformBuffer,
        15 => AccessType::GeometryShaderReadSampledImageOrUniformTexelBuffer,
        16 => AccessType::GeometryShaderReadOther,
        17 => AccessType::FragmentShaderReadUniformBuffer,
        18 => AccessType::FragmentShaderReadSampledImageOrUniformTexelBuffer,
        19 => AccessType::FragmentShaderReadColorInputAttachment,
        20 => AccessType::FragmentShaderReadDepthStencilInputAttachment,
        21 => AccessType::FragmentShaderReadOther,
        22 => AccessType::ColorAttachmentRead,
        23 => AccessType::DepthStencilAttachmentRead,
        24 => AccessType::ComputeShaderReadUniformBuffer,
        25 => AccessType::ComputeShaderReadSampledImageOrUniformTexelBuffer,
        26 => AccessType::ComputeShaderReadOther,
        27 => AccessType::AnyShaderReadUniformBuffer,
        28 => AccessType::AnyShaderReadUniformBufferOrVertexBuffer,
        29 => AccessType::AnyShaderReadSampledImageOrUniformTexelBuffer,
        30 => AccessType::AnyShaderReadOther,
        31 => AccessType::TransferRead,
        32 => AccessType::HostRead,
        33 => AccessType::Present,
        34 => AccessType::CommandBufferWriteNVX,
        35 => AccessType::VertexShaderWrite,
        36 => AccessType::TessellationControlShaderWrite,
        37 => AccessType::TessellationEvaluationShaderWrite,
        38 => AccessType::GeometryShaderWrite,
        39 => AccessType::FragmentShaderWrite,
        40 => AccessType::ColorAttachmentWrite,
        41 => AccessType::DepthStencilAttachmentWrite,
        42 => AccessType::DepthAttachmentWriteStencilReadOnly,
        43 => AccessType::StencilAttachmentWriteDepthReadOnly,
        44 => AccessType::ComputeShaderWrite,
        45 => AccessType::AnyShaderWrite,
        46 => AccessType::TransferWrite,
        47 => AccessType::HostWrite,
        48 => AccessType::ColorAttachmentReadWrite,
        49 => AccessType::General,
        50 => AccessType::RayTracingShaderReadSampledImageOrUniformTexelBuffer,
        51 => AccessType::RayTracingShaderReadColorInputAttachment,
        52 => AccessType::RayTracingShaderReadDepthStencilInputAttachment,
        53 => AccessType::RayTracingShaderReadAccelerationStructure,
        54 => AccessType::RayTracingShaderReadOther,
        55 => AccessType::AccelerationStructureBuildWrite,
        56 => AccessType::AccelerationStructureBuildRead,
        57 => AccessType::AccelerationStructureBufferWrite,
        _ => unimplemented!(),
    }
}

const fn access_type_into_u8(access: AccessType) -> u8 {
    match access {
        AccessType::Nothing => 0,
        AccessType::CommandBufferReadNVX => 1,
        AccessType::IndirectBuffer => 2,
        AccessType::IndexBuffer => 3,
        AccessType::VertexBuffer => 4,
        AccessType::VertexShaderReadUniformBuffer => 5,
        AccessType::VertexShaderReadSampledImageOrUniformTexelBuffer => 6,
        AccessType::VertexShaderReadOther => 7,
        AccessType::TessellationControlShaderReadUniformBuffer => 8,
        AccessType::TessellationControlShaderReadSampledImageOrUniformTexelBuffer => 9,
        AccessType::TessellationControlShaderReadOther => 10,
        AccessType::TessellationEvaluationShaderReadUniformBuffer => 11,
        AccessType::TessellationEvaluationShaderReadSampledImageOrUniformTexelBuffer => 12,
        AccessType::TessellationEvaluationShaderReadOther => 13,
        AccessType::GeometryShaderReadUniformBuffer => 14,
        AccessType::GeometryShaderReadSampledImageOrUniformTexelBuffer => 15,
        AccessType::GeometryShaderReadOther => 16,
        AccessType::FragmentShaderReadUniformBuffer => 17,
        AccessType::FragmentShaderReadSampledImageOrUniformTexelBuffer => 18,
        AccessType::FragmentShaderReadColorInputAttachment => 19,
        AccessType::FragmentShaderReadDepthStencilInputAttachment => 20,
        AccessType::FragmentShaderReadOther => 21,
        AccessType::ColorAttachmentRead => 22,
        AccessType::DepthStencilAttachmentRead => 23,
        AccessType::ComputeShaderReadUniformBuffer => 24,
        AccessType::ComputeShaderReadSampledImageOrUniformTexelBuffer => 25,
        AccessType::ComputeShaderReadOther => 26,
        AccessType::AnyShaderReadUniformBuffer => 27,
        AccessType::AnyShaderReadUniformBufferOrVertexBuffer => 28,
        AccessType::AnyShaderReadSampledImageOrUniformTexelBuffer => 29,
        AccessType::AnyShaderReadOther => 30,
        AccessType::TransferRead => 31,
        AccessType::HostRead => 32,
        AccessType::Present => 33,
        AccessType::CommandBufferWriteNVX => 34,
        AccessType::VertexShaderWrite => 35,
        AccessType::TessellationControlShaderWrite => 36,
        AccessType::TessellationEvaluationShaderWrite => 37,
        AccessType::GeometryShaderWrite => 38,
        AccessType::FragmentShaderWrite => 39,
        AccessType::ColorAttachmentWrite => 40,
        AccessType::DepthStencilAttachmentWrite => 41,
        AccessType::DepthAttachmentWriteStencilReadOnly => 42,
        AccessType::StencilAttachmentWriteDepthReadOnly => 43,
        AccessType::ComputeShaderWrite => 44,
        AccessType::AnyShaderWrite => 45,
        AccessType::TransferWrite => 46,
        AccessType::HostWrite => 47,
        AccessType::ColorAttachmentReadWrite => 48,
        AccessType::General => 49,
        AccessType::RayTracingShaderReadSampledImageOrUniformTexelBuffer => 50,
        AccessType::RayTracingShaderReadColorInputAttachment => 51,
        AccessType::RayTracingShaderReadDepthStencilInputAttachment => 52,
        AccessType::RayTracingShaderReadAccelerationStructure => 53,
        AccessType::RayTracingShaderReadOther => 54,
        AccessType::AccelerationStructureBuildWrite => 55,
        AccessType::AccelerationStructureBuildRead => 56,
        AccessType::AccelerationStructureBufferWrite => 57,
    }
}

#[allow(clippy::reversed_empty_ranges)]
pub(super) fn buffer_copy_subresources(
    regions: &[vk::BufferCopy],
) -> (Range<vk::DeviceSize>, Range<vk::DeviceSize>) {
    let mut src = vk::DeviceSize::MAX..vk::DeviceSize::MIN;
    let mut dst = vk::DeviceSize::MAX..vk::DeviceSize::MIN;
    for region in regions.iter() {
        src.start = src.start.min(region.src_offset);
        src.end = src.end.max(region.src_offset + region.size);

        dst.start = dst.start.min(region.dst_offset);
        dst.end = dst.end.max(region.dst_offset + region.size);
    }

    debug_assert!(src.end > src.start);
    debug_assert!(dst.end > dst.start);

    (src, dst)
}

#[allow(clippy::reversed_empty_ranges)]
pub(super) fn buffer_image_copy_subresource(
    regions: &[vk::BufferImageCopy],
) -> Range<vk::DeviceSize> {
    debug_assert!(!regions.is_empty());

    let mut res = vk::DeviceSize::MAX..vk::DeviceSize::MIN;
    for region in regions.iter() {
        res.start = res.start.min(region.buffer_offset);
        res.end = res.end.max(
            region.buffer_offset
                + (region.buffer_row_length * region.buffer_image_height) as vk::DeviceSize,
        );
    }

    debug_assert!(res.end > res.start);

    res
}

pub(super) const fn format_aspect_mask(fmt: vk::Format) -> vk::ImageAspectFlags {
    match fmt {
        vk::Format::D16_UNORM => vk::ImageAspectFlags::DEPTH,
        vk::Format::X8_D24_UNORM_PACK32 => vk::ImageAspectFlags::DEPTH,
        vk::Format::D32_SFLOAT => vk::ImageAspectFlags::DEPTH,
        vk::Format::S8_UINT => vk::ImageAspectFlags::STENCIL,
        vk::Format::D16_UNORM_S8_UINT => vk::ImageAspectFlags::from_raw(
            vk::ImageAspectFlags::DEPTH.as_raw() | vk::ImageAspectFlags::STENCIL.as_raw(),
        ),
        vk::Format::D24_UNORM_S8_UINT => vk::ImageAspectFlags::from_raw(
            vk::ImageAspectFlags::DEPTH.as_raw() | vk::ImageAspectFlags::STENCIL.as_raw(),
        ),
        vk::Format::D32_SFLOAT_S8_UINT => vk::ImageAspectFlags::from_raw(
            vk::ImageAspectFlags::DEPTH.as_raw() | vk::ImageAspectFlags::STENCIL.as_raw(),
        ),
        _ => vk::ImageAspectFlags::COLOR,
    }
}

pub(super) const fn image_access_layout(access: AccessType) -> ImageLayout {
    if matches!(access, AccessType::Present | AccessType::ComputeShaderWrite) {
        ImageLayout::General
    } else {
        ImageLayout::Optimal
    }
}

pub(super) const fn is_read_access(ty: AccessType) -> bool {
    !is_write_access(ty)
}

pub(super) const fn is_write_access(ty: AccessType) -> bool {
    use AccessType::*;
    match ty {
        Nothing
        | CommandBufferReadNVX
        | IndirectBuffer
        | IndexBuffer
        | VertexBuffer
        | VertexShaderReadUniformBuffer
        | VertexShaderReadSampledImageOrUniformTexelBuffer
        | VertexShaderReadOther
        | TessellationControlShaderReadUniformBuffer
        | TessellationControlShaderReadSampledImageOrUniformTexelBuffer
        | TessellationControlShaderReadOther
        | TessellationEvaluationShaderReadUniformBuffer
        | TessellationEvaluationShaderReadSampledImageOrUniformTexelBuffer
        | TessellationEvaluationShaderReadOther
        | GeometryShaderReadUniformBuffer
        | GeometryShaderReadSampledImageOrUniformTexelBuffer
        | GeometryShaderReadOther
        | FragmentShaderReadUniformBuffer
        | FragmentShaderReadSampledImageOrUniformTexelBuffer
        | FragmentShaderReadColorInputAttachment
        | FragmentShaderReadDepthStencilInputAttachment
        | FragmentShaderReadOther
        | ColorAttachmentRead
        | DepthStencilAttachmentRead
        | ComputeShaderReadUniformBuffer
        | ComputeShaderReadSampledImageOrUniformTexelBuffer
        | ComputeShaderReadOther
        | AnyShaderReadUniformBuffer
        | AnyShaderReadUniformBufferOrVertexBuffer
        | AnyShaderReadSampledImageOrUniformTexelBuffer
        | AnyShaderReadOther
        | TransferRead
        | HostRead
        | Present
        | RayTracingShaderReadSampledImageOrUniformTexelBuffer
        | RayTracingShaderReadColorInputAttachment
        | RayTracingShaderReadDepthStencilInputAttachment
        | RayTracingShaderReadAccelerationStructure
        | RayTracingShaderReadOther
        | AccelerationStructureBuildRead => false,
        CommandBufferWriteNVX
        | VertexShaderWrite
        | TessellationControlShaderWrite
        | TessellationEvaluationShaderWrite
        | GeometryShaderWrite
        | FragmentShaderWrite
        | ColorAttachmentWrite
        | DepthStencilAttachmentWrite
        | DepthAttachmentWriteStencilReadOnly
        | StencilAttachmentWriteDepthReadOnly
        | ComputeShaderWrite
        | AnyShaderWrite
        | TransferWrite
        | HostWrite
        | ColorAttachmentReadWrite
        | General
        | AccelerationStructureBuildWrite
        | AccelerationStructureBufferWrite => true,
    }
}

fn merge_push_constant_ranges(push_constants: &mut Vec<vk::PushConstantRange>) {
    // Convert overlapping push constant regions such as this:
    // VERTEX 0..64
    // FRAGMENT 0..80
    //
    // To this:
    // VERTEX | FRAGMENT 0..64
    // FRAGMENT 64..80
    //
    // We do this now so that submission doesn't need to check for overlaps
    // See https://github.com/KhronosGroup/Vulkan-Docs/issues/609
    if push_constants.len() > 1 {
        push_constants.sort_unstable_by(|lhs, rhs| match lhs.offset.cmp(&rhs.offset) {
            Ordering::Equal => lhs.size.cmp(&rhs.size),
            res => res,
        });

        let mut idx = 0;
        while idx + 1 < push_constants.len() {
            let curr = push_constants[idx];
            let next = push_constants[idx + 1];
            let curr_end = curr.offset + curr.size;

            // Check for overlapping push constant ranges; combine them and move the next
            // one so it no longer overlaps
            if curr_end > next.offset {
                push_constants[idx].stage_flags |= next.stage_flags;

                idx += 1;
                push_constants[idx].offset = curr_end;
                push_constants[idx].size -= curr_end - next.offset;
            }

            idx += 1;
        }

        for pcr in &*push_constants {
            trace!(
                "effective push constants: {:?} {}..{}",
                pcr.stage_flags,
                pcr.offset,
                pcr.offset + pcr.size
            );
        }
    } else {
        for pcr in &*push_constants {
            trace!(
                "detected push constants: {:?} {}..{}",
                pcr.stage_flags,
                pcr.offset,
                pcr.offset + pcr.size
            );
        }
    }
}

pub(super) const fn pipeline_stage_access_flags(
    access_type: AccessType,
) -> (vk::PipelineStageFlags, vk::AccessFlags) {
    use {
        vk::{AccessFlags as access, PipelineStageFlags as stage},
        AccessType as ty,
    };

    match access_type {
        ty::Nothing => (stage::empty(), access::empty()),
        ty::CommandBufferReadNVX => (
            stage::COMMAND_PREPROCESS_NV,
            access::COMMAND_PREPROCESS_READ_NV,
        ),
        ty::IndirectBuffer => (stage::DRAW_INDIRECT, access::INDIRECT_COMMAND_READ),
        ty::IndexBuffer => (stage::VERTEX_INPUT, access::INDEX_READ),
        ty::VertexBuffer => (stage::VERTEX_INPUT, access::VERTEX_ATTRIBUTE_READ),
        ty::VertexShaderReadUniformBuffer => (stage::VERTEX_SHADER, access::SHADER_READ),
        ty::VertexShaderReadSampledImageOrUniformTexelBuffer => {
            (stage::VERTEX_SHADER, access::SHADER_READ)
        }
        ty::VertexShaderReadOther => (stage::VERTEX_SHADER, access::SHADER_READ),
        ty::TessellationControlShaderReadUniformBuffer => {
            (stage::TESSELLATION_CONTROL_SHADER, access::UNIFORM_READ)
        }
        ty::TessellationControlShaderReadSampledImageOrUniformTexelBuffer => {
            (stage::TESSELLATION_CONTROL_SHADER, access::SHADER_READ)
        }
        ty::TessellationControlShaderReadOther => {
            (stage::TESSELLATION_CONTROL_SHADER, access::SHADER_READ)
        }
        ty::TessellationEvaluationShaderReadUniformBuffer => {
            (stage::TESSELLATION_EVALUATION_SHADER, access::UNIFORM_READ)
        }
        ty::TessellationEvaluationShaderReadSampledImageOrUniformTexelBuffer => {
            (stage::TESSELLATION_EVALUATION_SHADER, access::SHADER_READ)
        }
        ty::TessellationEvaluationShaderReadOther => {
            (stage::TESSELLATION_EVALUATION_SHADER, access::SHADER_READ)
        }
        ty::GeometryShaderReadUniformBuffer => (stage::GEOMETRY_SHADER, access::UNIFORM_READ),
        ty::GeometryShaderReadSampledImageOrUniformTexelBuffer => {
            (stage::GEOMETRY_SHADER, access::SHADER_READ)
        }
        ty::GeometryShaderReadOther => (stage::GEOMETRY_SHADER, access::SHADER_READ),
        ty::FragmentShaderReadUniformBuffer => (stage::FRAGMENT_SHADER, access::UNIFORM_READ),
        ty::FragmentShaderReadSampledImageOrUniformTexelBuffer => {
            (stage::FRAGMENT_SHADER, access::SHADER_READ)
        }
        ty::FragmentShaderReadColorInputAttachment => {
            (stage::FRAGMENT_SHADER, access::INPUT_ATTACHMENT_READ)
        }
        ty::FragmentShaderReadDepthStencilInputAttachment => {
            (stage::FRAGMENT_SHADER, access::INPUT_ATTACHMENT_READ)
        }
        ty::FragmentShaderReadOther => (stage::FRAGMENT_SHADER, access::SHADER_READ),
        ty::ColorAttachmentRead => (
            stage::COLOR_ATTACHMENT_OUTPUT,
            access::COLOR_ATTACHMENT_READ,
        ),
        ty::DepthStencilAttachmentRead => (
            stage::from_raw(
                stage::EARLY_FRAGMENT_TESTS.as_raw()
                    | vk::PipelineStageFlags::LATE_FRAGMENT_TESTS.as_raw(),
            ),
            access::DEPTH_STENCIL_ATTACHMENT_READ,
        ),
        ty::ComputeShaderReadUniformBuffer => (stage::COMPUTE_SHADER, access::UNIFORM_READ),
        ty::ComputeShaderReadSampledImageOrUniformTexelBuffer => {
            (stage::COMPUTE_SHADER, access::SHADER_READ)
        }
        ty::ComputeShaderReadOther => (stage::COMPUTE_SHADER, access::SHADER_READ),
        ty::AnyShaderReadUniformBuffer => (stage::ALL_COMMANDS, access::UNIFORM_READ),
        ty::AnyShaderReadUniformBufferOrVertexBuffer => (
            stage::ALL_COMMANDS,
            access::from_raw(
                access::UNIFORM_READ.as_raw() | vk::AccessFlags::VERTEX_ATTRIBUTE_READ.as_raw(),
            ),
        ),
        ty::AnyShaderReadSampledImageOrUniformTexelBuffer => {
            (stage::ALL_COMMANDS, access::SHADER_READ)
        }
        ty::AnyShaderReadOther => (stage::ALL_COMMANDS, access::SHADER_READ),
        ty::TransferRead => (stage::TRANSFER, access::TRANSFER_READ),
        ty::HostRead => (stage::HOST, access::HOST_READ),
        ty::Present => (stage::empty(), access::empty()),
        ty::CommandBufferWriteNVX => (
            stage::COMMAND_PREPROCESS_NV,
            access::COMMAND_PREPROCESS_WRITE_NV,
        ),
        ty::VertexShaderWrite => (stage::VERTEX_SHADER, access::SHADER_WRITE),
        ty::TessellationControlShaderWrite => {
            (stage::TESSELLATION_CONTROL_SHADER, access::SHADER_WRITE)
        }
        ty::TessellationEvaluationShaderWrite => {
            (stage::TESSELLATION_EVALUATION_SHADER, access::SHADER_WRITE)
        }
        ty::GeometryShaderWrite => (stage::GEOMETRY_SHADER, access::SHADER_WRITE),
        ty::FragmentShaderWrite => (stage::FRAGMENT_SHADER, access::SHADER_WRITE),
        ty::ColorAttachmentWrite => (
            stage::COLOR_ATTACHMENT_OUTPUT,
            access::COLOR_ATTACHMENT_WRITE,
        ),
        ty::DepthStencilAttachmentWrite => (
            stage::from_raw(
                stage::EARLY_FRAGMENT_TESTS.as_raw()
                    | vk::PipelineStageFlags::LATE_FRAGMENT_TESTS.as_raw(),
            ),
            access::DEPTH_STENCIL_ATTACHMENT_WRITE,
        ),
        ty::DepthAttachmentWriteStencilReadOnly => (
            stage::from_raw(
                stage::EARLY_FRAGMENT_TESTS.as_raw()
                    | vk::PipelineStageFlags::LATE_FRAGMENT_TESTS.as_raw(),
            ),
            access::from_raw(
                access::DEPTH_STENCIL_ATTACHMENT_WRITE.as_raw()
                    | vk::AccessFlags::DEPTH_STENCIL_ATTACHMENT_READ.as_raw(),
            ),
        ),
        ty::StencilAttachmentWriteDepthReadOnly => (
            stage::from_raw(
                stage::EARLY_FRAGMENT_TESTS.as_raw()
                    | vk::PipelineStageFlags::LATE_FRAGMENT_TESTS.as_raw(),
            ),
            access::from_raw(
                access::DEPTH_STENCIL_ATTACHMENT_WRITE.as_raw()
                    | vk::AccessFlags::DEPTH_STENCIL_ATTACHMENT_READ.as_raw(),
            ),
        ),
        ty::ComputeShaderWrite => (stage::COMPUTE_SHADER, access::SHADER_WRITE),
        ty::AnyShaderWrite => (stage::ALL_COMMANDS, access::SHADER_WRITE),
        ty::TransferWrite => (stage::TRANSFER, access::TRANSFER_WRITE),
        ty::HostWrite => (stage::HOST, access::HOST_WRITE),
        ty::ColorAttachmentReadWrite => (
            stage::COLOR_ATTACHMENT_OUTPUT,
            access::from_raw(
                access::COLOR_ATTACHMENT_READ.as_raw()
                    | vk::AccessFlags::COLOR_ATTACHMENT_WRITE.as_raw(),
            ),
        ),
        ty::General => (
            stage::ALL_COMMANDS,
            access::from_raw(access::MEMORY_READ.as_raw() | vk::AccessFlags::MEMORY_WRITE.as_raw()),
        ),
        ty::RayTracingShaderReadSampledImageOrUniformTexelBuffer => {
            (stage::RAY_TRACING_SHADER_KHR, access::SHADER_READ)
        }
        ty::RayTracingShaderReadColorInputAttachment => {
            (stage::RAY_TRACING_SHADER_KHR, access::INPUT_ATTACHMENT_READ)
        }
        ty::RayTracingShaderReadDepthStencilInputAttachment => {
            (stage::RAY_TRACING_SHADER_KHR, access::INPUT_ATTACHMENT_READ)
        }
        ty::RayTracingShaderReadAccelerationStructure => (
            stage::RAY_TRACING_SHADER_KHR,
            access::ACCELERATION_STRUCTURE_READ_KHR,
        ),
        ty::RayTracingShaderReadOther => (stage::RAY_TRACING_SHADER_KHR, access::SHADER_READ),
        ty::AccelerationStructureBuildWrite => (
            stage::ACCELERATION_STRUCTURE_BUILD_KHR,
            access::ACCELERATION_STRUCTURE_WRITE_KHR,
        ),
        ty::AccelerationStructureBuildRead => (
            stage::ACCELERATION_STRUCTURE_BUILD_KHR,
            access::ACCELERATION_STRUCTURE_READ_KHR,
        ),
        ty::AccelerationStructureBufferWrite => (
            stage::ACCELERATION_STRUCTURE_BUILD_KHR,
            access::TRANSFER_WRITE,
        ),
    }
}

/// Holds a constructed graphics driver.
#[derive(Debug)]
pub struct Driver {
    /// The current device.
    pub device: Arc<Device>,

    pub(crate) swapchain: Swapchain,
}

impl Driver {
    /// Constructs a new `Driver` from the given configuration.
    pub fn new(
        window: &impl HasRawWindowHandle,
        cfg: DriverConfig,
        width: u32,
        height: u32,
    ) -> Result<Self, DriverError> {
        trace!("new {:?}", cfg);

        let required_extensions = ash_window::enumerate_required_extensions(window)
            .map_err(|err| {
                warn!("{err}");

                DriverError::Unsupported
            })?
            .iter()
            .map(|ext| unsafe { CStr::from_ptr(*ext as *const _) });
        let instance = Arc::new(Instance::new(cfg.debug, required_extensions)?);
        let surface = Surface::new(&instance, window)?;
        let physical_devices = Instance::physical_devices(&instance)?
            .filter(|physical_device| {
                // Filters this list down to only supported devices
                if cfg.presentation
                    && !PhysicalDevice::has_presentation_support(
                        physical_device,
                        &instance,
                        &surface,
                    )
                {
                    info!("{:?} lacks presentation support", unsafe {
                        CStr::from_ptr(physical_device.props.device_name.as_ptr() as *const c_char)
                    });

                    return false;
                }

                if cfg.ray_tracing && !PhysicalDevice::has_ray_tracing_support(physical_device) {
                    info!("{:?} lacks ray tracing support", unsafe {
                        CStr::from_ptr(physical_device.props.device_name.as_ptr() as *const c_char)
                    });

                    return false;
                }

                // TODO: Check vkGetPhysicalDeviceFeatures for samplerAnisotropy (it should exist, but to be sure)

                true
            })
            .collect::<Vec<_>>();

        for physical_device in &physical_devices {
            debug!("supported: {:?}", physical_device);
        }

        let physical_device = physical_devices
            .into_iter()
            // If there are multiple devices with the same score, `max_by_key` would choose the last,
            // and we want to preserve the order of devices from `enumerate_physical_devices`.
            .rev()
            .max_by_key(PhysicalDevice::score_device_type)
            .ok_or(DriverError::Unsupported)?;

        debug!("selected: {:?}", physical_device);

        let device = Arc::new(Device::create(&instance, physical_device, cfg)?);
        let surface_formats = Device::surface_formats(&device, &surface)?;

        for fmt in &surface_formats {
            debug!("surface: {:#?} ({:#?})", fmt.format, fmt.color_space);
        }

        // TODO: Explicitly fallback to BGRA_UNORM
        let format = surface_formats
            .into_iter()
            .find(|format| Self::select_swapchain_format(*format))
            .ok_or(DriverError::Unsupported)?;
        let swapchain = Swapchain::new(
            &device,
            surface,
            SwapchainInfo {
                desired_image_count: cfg.desired_swapchain_image_count,
                format,
                height,
                sync_display: cfg.sync_display,
                width,
            },
        )?;

        info!("OK");

        Ok(Self { device, swapchain })
    }

    fn select_swapchain_format(format: vk::SurfaceFormatKHR) -> bool {
        // TODO: Properly handle the request for SRGB and swapchain image usage flags: The device may not support SRGB and only in that case do we fall back to UNORM
        format.format == vk::Format::B8G8R8A8_UNORM
            && format.color_space == vk::ColorSpaceKHR::SRGB_NONLINEAR
    }
}

/// A list of required features. Features that are supported but not required will not be
/// available.
#[derive(Builder, Clone, Copy, Debug, Eq, Hash, PartialEq)]
#[builder(
    pattern = "owned",
    build_fn(private, name = "fallible_build", error = "DriverConfigBuilderError"),
    derive(Debug)
)]
pub struct DriverConfig {
    /// Enables Vulkan validation layers.
    ///
    /// This requires a Vulkan SDK installation and will cause validation errors to introduce
    /// panics as they happen.
    ///
    /// _NOTE:_ Consider turning OFF debug if you discover an unknown issue. Often the validation
    /// layers will throw an error before other layers can provide additional context such as the
    /// API dump info or other messages. You might find the "actual" issue is detailed in those
    /// subsequent details.
    #[builder(default)]
    pub debug: bool,

    /// The desired, but not garunteed, number of images that will be in the created swapchain.
    #[builder(default = "3")]
    pub desired_swapchain_image_count: u32,

    /// Determines if frames will be submitted to the display in a synchronous fashion or if they
    /// should be displayed as fast as possible instead.
    ///
    /// Turn on to eliminate visual tearing at the expense of latency.
    #[builder(default = "true")]
    pub sync_display: bool,

    /// Used to select devices which support presentation to the display.
    ///
    /// The default value is `true`.
    #[builder(default = "true")]
    pub presentation: bool,

    /// Used to select devices which support the [KHR ray tracing] extension.
    ///
    /// The default is `false`.
    ///
    /// [KHR ray tracing]: https://registry.khronos.org/vulkan/specs/1.3-extensions/html/vkspec.html#ray-tracing
    #[builder(default)]
    pub ray_tracing: bool,
}

impl DriverConfig {
    /// Specifies a default driver configuration.
    #[allow(clippy::new_ret_no_self)]
    pub fn new() -> DriverConfigBuilder {
        Default::default()
    }

    fn features(self) -> FeatureFlags {
        FeatureFlags {
            presentation: true,
            ray_tracing: self.ray_tracing,
        }
    }
}

// HACK: https://github.com/colin-kiegel/rust-derive-builder/issues/56
impl DriverConfigBuilder {
    /// Builds a new `DriverConfig`.
    pub fn build(self) -> DriverConfig {
        self.fallible_build().unwrap()
    }
}

#[derive(Debug)]
struct DriverConfigBuilderError;

impl From<UninitializedFieldError> for DriverConfigBuilderError {
    fn from(_: UninitializedFieldError) -> Self {
        Self
    }
}

/// Describes the general category of all graphics driver failure cases.
///
/// In the event of a failure you should follow the _Screen 13_ code to the responsible Vulkan API
/// and then to the `Ash` stub call; it will generally contain a link to the appropriate
/// specification. The specifications provide a table of possible error conditions which can be a
/// good starting point to debug the issue.
///
/// Feel free to open an issue on GitHub, [here](https://github.com/attackgoat/screen-13/issues) for
/// help debugging the issue.
#[derive(Debug)]
pub enum DriverError {
    /// The input data, or referenced data, is not valid for the current state.
    InvalidData,

    /// The requested feature, or input configuration, is not supported for the current state.
    Unsupported,

    /// The device has run out of physical memory.
    ///
    /// Many drivers return this value for generic or unhandled error conditions.
    OutOfMemory,
}

impl Display for DriverError {
    fn fmt(&self, f: &mut Formatter<'_>) -> std::fmt::Result {
        write!(f, "{:?}", self)
    }
}

impl Error for DriverError {}

/// Structure describing descriptor indexing features that can be supported by an implementation.
pub struct PhysicalDeviceDescriptorIndexingFeatures {
    /// Indicates whether arrays of input attachments can be indexed by dynamically uniform integer
    /// expressions in shader code.
    ///
    /// If this feature is not enabled, resources with a descriptor type of
    /// VK_DESCRIPTOR_TYPE_INPUT_ATTACHMENT must be indexed only by constant integral expressions
    /// when aggregated into arrays in shader code. This also indicates whether shader modules can
    /// declare the InputAttachmentArrayDynamicIndexing capability.
    pub shader_input_attachment_array_dynamic_indexing: bool,

    /// Indicates whether arrays of uniform texel buffers can be indexed by dynamically uniform
    /// integer expressions in shader code.
    ///
    /// If this feature is not enabled, resources with a descriptor type of
    /// VK_DESCRIPTOR_TYPE_UNIFORM_TEXEL_BUFFER must be indexed only by constant integral
    /// expressions when aggregated into arrays in shader code. This also indicates whether shader
    /// modules can declare the UniformTexelBufferArrayDynamicIndexing capability.
    pub shader_uniform_texel_buffer_array_dynamic_indexing: bool,

    /// Indicates whether arrays of storage texel buffers can be indexed by dynamically uniform
    /// integer expressions in shader code.
    ///
    /// If this feature is not enabled, resources with a descriptor type of
    /// VK_DESCRIPTOR_TYPE_STORAGE_TEXEL_BUFFER must be indexed only by constant integral
    /// expressions when aggregated into arrays in shader code. This also indicates whether shader
    /// modules can declare the StorageTexelBufferArrayDynamicIndexing capability.
    pub shader_storage_texel_buffer_array_dynamic_indexing: bool,

    /// Indicates whether arrays of uniform buffers can be indexed by non-uniform integer
    /// expressions in shader code.
    ///
    /// If this feature is not enabled, resources with a descriptor type of
    /// VK_DESCRIPTOR_TYPE_UNIFORM_BUFFER or VK_DESCRIPTOR_TYPE_UNIFORM_BUFFER_DYNAMIC must not be
    /// indexed by non-uniform integer expressions when aggregated into arrays in shader code. This
    /// also indicates whether shader modules can declare the UniformBufferArrayNonUniformIndexing
    /// capability.
    pub shader_uniform_buffer_array_non_uniform_indexing: bool,

    /// Indicates whether arrays of samplers or sampled images can be indexed by non-uniform integer
    /// expressions in shader code.
    ///
    /// If this feature is not enabled, resources with a descriptor type of
    /// VK_DESCRIPTOR_TYPE_SAMPLER, VK_DESCRIPTOR_TYPE_COMBINED_IMAGE_SAMPLER, or
    /// VK_DESCRIPTOR_TYPE_SAMPLED_IMAGE must not be indexed by non-uniform integer expressions when
    /// aggregated into arrays in shader code. This also indicates whether shader modules can
    /// declare the SampledImageArrayNonUniformIndexing capability.
    pub shader_sampled_image_array_non_uniform_indexing: bool,

    /// Indicates whether arrays of storage buffers can be indexed by non-uniform integer
    /// expressions in shader code.
    ///
    /// If this feature is not enabled, resources with a descriptor type of
    /// VK_DESCRIPTOR_TYPE_STORAGE_BUFFER or VK_DESCRIPTOR_TYPE_STORAGE_BUFFER_DYNAMIC must not be
    /// indexed by non-uniform integer expressions when aggregated into arrays in shader code. This
    /// also indicates whether shader modules can declare the StorageBufferArrayNonUniformIndexing
    /// capability.
    pub shader_storage_buffer_array_non_uniform_indexing: bool,

    /// Indicates whether arrays of storage images can be indexed by non-uniform integer expressions
    /// in shader code.
    ///
    /// If this feature is not enabled, resources with a descriptor type of
    /// VK_DESCRIPTOR_TYPE_STORAGE_IMAGE must not be indexed by non-uniform integer expressions when
    /// aggregated into arrays in shader code. This also indicates whether shader modules can
    /// declare the StorageImageArrayNonUniformIndexing capability.
    pub shader_storage_image_array_non_uniform_indexing: bool,

    /// Indicates whether arrays of input attachments can be indexed by non-uniform integer
    /// expressions in shader code.
    ///
    /// If this feature is not enabled, resources with a descriptor type of
    /// VK_DESCRIPTOR_TYPE_INPUT_ATTACHMENT must not be indexed by non-uniform integer expressions
    /// when aggregated into arrays in shader code. This also indicates whether shader modules can
    /// declare the InputAttachmentArrayNonUniformIndexing capability.
    pub shader_input_attachment_array_non_uniform_indexing: bool,

    /// Indicates whether arrays of uniform texel buffers can be indexed by non-uniform integer
    /// expressions in shader code.
    ///
    /// If this feature is not enabled, resources with a descriptor type of
    /// VK_DESCRIPTOR_TYPE_UNIFORM_TEXEL_BUFFER must not be indexed by non-uniform integer
    /// expressions when aggregated into arrays in shader code. This also indicates whether shader
    /// modules can declare the UniformTexelBufferArrayNonUniformIndexing capability.
    pub shader_uniform_texel_buffer_array_non_uniform_indexing: bool,

    /// Indicates whether arrays of storage texel buffers can be indexed by non-uniform integer
    /// expressions in shader code.
    ///
    /// If this feature is not enabled, resources with a descriptor type of
    /// VK_DESCRIPTOR_TYPE_STORAGE_TEXEL_BUFFER must not be indexed by non-uniform integer
    /// expressions when aggregated into arrays in shader code. This also indicates whether shader
    /// modules can declare the StorageTexelBufferArrayNonUniformIndexing capability.
    pub shader_storage_texel_buffer_array_non_uniform_indexing: bool,

    // Unused:
    // pub descriptor_binding_uniform_buffer_update_after_bind: bool,
    // pub descriptor_binding_sampled_image_update_after_bind: bool,
    // pub descriptor_binding_storage_image_update_after_bind: bool,
    // pub descriptor_binding_storage_buffer_update_after_bind: bool,
    // pub descriptor_binding_uniform_texel_buffer_update_after_bind: bool,
    // pub descriptor_binding_storage_texel_buffer_update_after_bind: bool,
    // pub descriptor_binding_update_unused_while_pending: bool,
    /// Indicates whether the implementation supports statically using a descriptor set binding in
    /// which some descriptors are not valid. If this feature is not enabled,
    /// VK_DESCRIPTOR_BINDING_PARTIALLY_BOUND_BIT must not be used.
    pub descriptor_binding_partially_bound: bool,

    /// Indicates whether the implementation supports descriptor sets with a variable-sized last
    /// binding. If this feature is not enabled, VK_DESCRIPTOR_BINDING_VARIABLE_DESCRIPTOR_COUNT_BIT
    /// must not be used.
    pub descriptor_binding_variable_descriptor_count: bool,

    /// Indicates whether the implementation supports the SPIR-V RuntimeDescriptorArray capability.
    ///
    /// If this feature is not enabled, descriptors must not be declared in runtime arrays.
    pub runtime_descriptor_array: bool,
}

impl From<vk::PhysicalDeviceDescriptorIndexingFeatures>
    for PhysicalDeviceDescriptorIndexingFeatures
{
    fn from(features: vk::PhysicalDeviceDescriptorIndexingFeatures) -> Self {
        Self {
            shader_input_attachment_array_dynamic_indexing: features
                .shader_input_attachment_array_dynamic_indexing
                == vk::TRUE,
            shader_uniform_texel_buffer_array_dynamic_indexing: features
                .shader_uniform_texel_buffer_array_dynamic_indexing
                == vk::TRUE,
            shader_storage_texel_buffer_array_dynamic_indexing: features
                .shader_storage_texel_buffer_array_dynamic_indexing
                == vk::TRUE,
            shader_uniform_buffer_array_non_uniform_indexing: features
                .shader_uniform_buffer_array_non_uniform_indexing
                == vk::TRUE,
            shader_sampled_image_array_non_uniform_indexing: features
                .shader_sampled_image_array_non_uniform_indexing
                == vk::TRUE,
            shader_storage_buffer_array_non_uniform_indexing: features
                .shader_storage_buffer_array_non_uniform_indexing
                == vk::TRUE,
            shader_storage_image_array_non_uniform_indexing: features
                .shader_storage_image_array_non_uniform_indexing
                == vk::TRUE,
            shader_input_attachment_array_non_uniform_indexing: features
                .shader_input_attachment_array_non_uniform_indexing
                == vk::TRUE,
            shader_uniform_texel_buffer_array_non_uniform_indexing: features
                .shader_uniform_texel_buffer_array_non_uniform_indexing
                == vk::TRUE,
            shader_storage_texel_buffer_array_non_uniform_indexing: features
                .shader_storage_texel_buffer_array_non_uniform_indexing
                == vk::TRUE,
            // descriptor_binding_uniform_buffer_update_after_bind: features
            //     .descriptor_binding_uniform_buffer_update_after_bind
            //     == vk::TRUE,
            // descriptor_binding_sampled_image_update_after_bind: features
            //     .descriptor_binding_sampled_image_update_after_bind
            //     == vk::TRUE,
            // descriptor_binding_storage_image_update_after_bind: features
            //     .descriptor_binding_storage_image_update_after_bind
            //     == vk::TRUE,
            // descriptor_binding_storage_buffer_update_after_bind: features
            //     .descriptor_binding_storage_buffer_update_after_bind
            //     == vk::TRUE,
            // descriptor_binding_uniform_texel_buffer_update_after_bind: features
            //     .descriptor_binding_uniform_texel_buffer_update_after_bind
            //     == vk::TRUE,
            // descriptor_binding_storage_texel_buffer_update_after_bind: features
            //     .descriptor_binding_storage_texel_buffer_update_after_bind
            //     == vk::TRUE,
            // descriptor_binding_update_unused_while_pending: features
            //     .descriptor_binding_update_unused_while_pending
            //     == vk::TRUE,
            descriptor_binding_partially_bound: features.descriptor_binding_partially_bound
                == vk::TRUE,
            descriptor_binding_variable_descriptor_count: features
                .descriptor_binding_variable_descriptor_count
                == vk::TRUE,
            runtime_descriptor_array: features.runtime_descriptor_array == vk::TRUE,
        }
    }
}

/// Properties of the physical device for ray tracing.
#[derive(Debug)]
pub struct PhysicalDeviceRayTracePipelineProperties {
    /// The size in bytes of the shader header.
    pub shader_group_handle_size: u32,

    /// The maximum number of levels of ray recursion allowed in a trace command.
    pub max_ray_recursion_depth: u32,

    /// The maximum stride in bytes allowed between shader groups in the shader binding table.
    pub max_shader_group_stride: u32,

    /// The required alignment in bytes for the base of the shader binding table.
    pub shader_group_base_alignment: u32,

    /// The number of bytes for the information required to do capture and replay for shader group
    /// handles.
    pub shader_group_handle_capture_replay_size: u32,

    /// The maximum number of ray generation shader invocations which may be produced by a single
    /// vkCmdTraceRaysIndirectKHR or vkCmdTraceRaysKHR command.
    pub max_ray_dispatch_invocation_count: u32,

    /// The required alignment in bytes for each shader binding table entry.
    ///
    /// The value must be a power of two.
    pub shader_group_handle_alignment: u32,

    /// The maximum size in bytes for a ray attribute structure.
    pub max_ray_hit_attribute_size: u32,
}

impl From<vk::PhysicalDeviceRayTracingPipelinePropertiesKHR>
    for PhysicalDeviceRayTracePipelineProperties
{
    fn from(props: vk::PhysicalDeviceRayTracingPipelinePropertiesKHR) -> Self {
        Self {
            shader_group_handle_size: props.shader_group_handle_size,
            max_ray_recursion_depth: props.max_ray_recursion_depth,
            max_shader_group_stride: props.max_shader_group_stride,
            shader_group_base_alignment: props.shader_group_base_alignment,
            shader_group_handle_capture_replay_size: props.shader_group_handle_capture_replay_size,
            max_ray_dispatch_invocation_count: props.max_ray_dispatch_invocation_count,
            shader_group_handle_alignment: props.shader_group_handle_alignment,
            max_ray_hit_attribute_size: props.max_ray_hit_attribute_size,
        }
    }
}

/// An execution queue.
pub struct Queue {
    queue: vk::Queue,

    /// Properties of the family which this queue belongs to.
    pub family: QueueFamily,
}

impl Deref for Queue {
    type Target = vk::Queue;

    fn deref(&self) -> &Self::Target {
        &self.queue
    }
}

#[derive(Clone, Copy, Debug, Eq, Hash, PartialEq)]
pub(super) struct SamplerDesc {
    pub address_modes: vk::SamplerAddressMode,
    pub mipmap_mode: vk::SamplerMipmapMode,
    pub texel_filter: vk::Filter,
}