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
#[cfg(feature = "serde-support")]
use serde::{Deserialize, Serialize};

use crate::{RafxBuffer, RafxBufferDef, RafxSampler, RafxTexture};
use rafx_base::DecimalF32;
use std::hash::{Hash, Hasher};

#[derive(Copy, Clone, Debug, PartialEq, Eq, Hash)]
pub enum RafxApiType {
    Vk = 0,
    Dx12 = 1,
    Metal = 2,
    Gles2 = 3,
    Gles3 = 4,
    Empty = 5,
}

// Generally we shouldn't use empty, it's excluded from this list
pub const RAFX_VALID_API_TYPES: [RafxApiType; 5] = [
    RafxApiType::Vk,
    RafxApiType::Dx12,
    RafxApiType::Metal,
    RafxApiType::Gles2,
    RafxApiType::Gles3,
];

/// Controls if validation is enabled or not. The requirements/behaviors of validation is
/// API-specific.
#[derive(Copy, Clone, Debug, PartialEq)]
pub enum RafxValidationMode {
    /// Do not enable validation. Even if validation is turned on through external means, do not
    /// intentionally fail initialization
    Disabled,

    /// Enable validation if possible. (Details on requirements to enable at runtime are
    /// API-specific)
    EnabledIfAvailable,

    /// Enable validation, and fail if we cannot enable it or detect that it is not enabled through
    /// external means. (Details on this are API-specific)
    Enabled,
}

impl Default for RafxValidationMode {
    fn default() -> Self {
        #[cfg(debug_assertions)]
        let validation_mode = RafxValidationMode::EnabledIfAvailable;
        #[cfg(not(debug_assertions))]
        let validation_mode = RafxValidationMode::Disabled;

        validation_mode
    }
}

/// Information about the device, mostly limits, requirements (like memory alignment), and flags to
/// indicate whether certain features are supported
pub struct RafxDeviceInfo {
    pub supports_multithreaded_usage: bool,
    pub debug_names_enabled: bool,
    pub min_uniform_buffer_offset_alignment: u32,
    pub min_storage_buffer_offset_alignment: u32,
    pub upload_texture_alignment: u32,
    pub upload_texture_row_alignment: u32,

    // Requires iOS 14.0, macOS 10.12
    pub supports_clamp_to_border_color: bool,

    pub max_vertex_attribute_count: u32,
    //max_vertex_input_binding_count: u32,
    // max_root_signature_dwords: u32,
    // wave_lane_count: u32,
    // wave_ops_support_flags: u32,
    // gpu_vendor_preset: u32,
    // metal_argument_buffer_max_textures: u32,
    // metal_heaps: u32,
    // metal_placement_heaps: u32,
    // metal_draw_index_vertex_offset_supported: bool,
}

/// Used to indicate which type of queue to use. Some operations require certain types of queues.
#[derive(Debug, Copy, Clone, PartialEq, Eq, Hash)]
pub enum RafxQueueType {
    /// Graphics queues generally supports all operations and are a safe default choice
    Graphics,

    /// Compute queues can be used for compute-based work.
    Compute,

    /// Transfer queues are generally limited to basic operations like copying data from buffers
    /// to images.
    Transfer,
}

/// The color space an image data is in. The correct color space often varies between texture types
/// (like normal maps vs. albedo maps).
#[derive(Copy, Clone, Debug)]
pub enum RafxColorType {
    Linear,
    Srgb,
}

// /// Texture will allocate its own memory (COMMITTED resource)
// TEXTURE_CREATION_FLAG_OWN_MEMORY_BIT = 0x01,
// /// Use on-tile memory to store this texture
// TEXTURE_CREATION_FLAG_ON_TILE = 0x20,
// /// Force 2D instead of automatically determining dimension based on width, height, depth
// TEXTURE_CREATION_FLAG_FORCE_2D = 0x80,
// /// Force 3D instead of automatically determining dimension based on width, height, depth
// TEXTURE_CREATION_FLAG_FORCE_3D = 0x100,
// /// Display target
// TEXTURE_CREATION_FLAG_ALLOW_DISPLAY_TARGET = 0x200,
// /// Create an sRGB texture.
// TEXTURE_CREATION_FLAG_SRGB = 0x400,

bitflags::bitflags! {
    /// The current state of a resource. When an operation is performed that references a resource,
    /// it must be in the correct state. Resources are moved between state using barriers.
    ///
    /// The implementation of resource_state_to_access_flags() in the vulkan backend gives a more
    /// thorough explanation for what these states imply about syncrhonization. See also
    /// determine_pipeline_stage_flags() and resource_state_to_image_layout() in the vulkan backend.
    pub struct RafxResourceState: u32 {
        const UNDEFINED = 0;
        const VERTEX_AND_CONSTANT_BUFFER = 0x1;
        const INDEX_BUFFER = 0x2;
        /// Similar to vulkan's COLOR_ATTACHMENT_OPTIMAL image layout
        const RENDER_TARGET = 0x4;
        /// Shader read/write
        const UNORDERED_ACCESS = 0x8;
        /// Similar to vulkan's DEPTH_STENCIL_ATTACHMENT_OPTIMAL image layout
        const DEPTH_WRITE = 0x10;
        const DEPTH_READ = 0x20;
        const NON_PIXEL_SHADER_RESOURCE = 0x40;
        const PIXEL_SHADER_RESOURCE = 0x80;
        /// Similar to vulkan's SHADER_READ_ONLY_OPTIMAL image layout
        const SHADER_RESOURCE = 0x40 | 0x80;
        const STREAM_OUT = 0x100;
        const INDIRECT_ARGUMENT = 0x200;
        /// Similar to vulkan's TRANSFER_DST_OPTIMAL image layout
        const COPY_DST = 0x400;
        /// Similar to vulkan's TRANSFER_SRC_OPTIMAL image layout
        const COPY_SRC = 0x800;
        const GENERIC_READ = (((((0x1 | 0x2) | 0x40) | 0x80) | 0x200) | 0x800);
        /// Similar to vulkan's PRESENT_SRC_KHR image layout
        const PRESENT = 0x1000;
        /// Similar to vulkan's COMMON image layout
        const COMMON = 0x2000;
    }
}

/// A 2d size for windows, textures, etc.
#[derive(Default, Copy, Clone, Debug, PartialEq, Eq, Hash)]
pub struct RafxExtents2D {
    pub width: u32,
    pub height: u32,
}

/// A 3d size for windows, textures, etc.
#[derive(Default, Copy, Clone, Debug, PartialEq, Eq, Hash)]
pub struct RafxExtents3D {
    pub width: u32,
    pub height: u32,
    pub depth: u32,
}

impl RafxExtents3D {
    pub fn to_2d(self) -> RafxExtents2D {
        RafxExtents2D {
            width: self.width,
            height: self.height,
        }
    }
}

/// Number of MSAA samples to use. 1xMSAA and 4xMSAA are most broadly supported
#[derive(Debug, Copy, Clone, PartialEq, Eq, Hash)]
#[cfg_attr(feature = "serde-support", derive(Serialize, Deserialize))]
pub enum RafxSampleCount {
    SampleCount1,
    SampleCount2,
    SampleCount4,
    SampleCount8,
    SampleCount16,
}

impl Default for RafxSampleCount {
    fn default() -> Self {
        RafxSampleCount::SampleCount1
    }
}

impl RafxSampleCount {
    pub fn from_u32(samples: u32) -> Self {
        match samples {
            1 => RafxSampleCount::SampleCount1,
            2 => RafxSampleCount::SampleCount2,
            4 => RafxSampleCount::SampleCount4,
            8 => RafxSampleCount::SampleCount8,
            16 => RafxSampleCount::SampleCount16,
            _ => unimplemented!(),
        }
    }

    pub fn as_u32(self) -> u32 {
        match self {
            RafxSampleCount::SampleCount1 => 1,
            RafxSampleCount::SampleCount2 => 2,
            RafxSampleCount::SampleCount4 => 4,
            RafxSampleCount::SampleCount8 => 8,
            RafxSampleCount::SampleCount16 => 16,
        }
    }
}

bitflags::bitflags! {
    /// Indicates how a resource will be used. In some cases, multiple flags are allowed.
    #[derive(Default)]
    #[cfg_attr(feature = "serde-support", derive(Serialize, Deserialize))]
    pub struct RafxResourceType: u32 {
        const UNDEFINED = 0;
        const SAMPLER = 1<<0;
        /// Similar to DX12 SRV and vulkan SAMPLED image usage flag and SAMPLED_IMAGE descriptor type
        const TEXTURE = 1<<1;
        /// Similar to DX12 UAV and vulkan STORAGE image usage flag and STORAGE_IMAGE descriptor type
        const TEXTURE_READ_WRITE = 1<<2;
        /// Similar to DX12 SRV and vulkan STORAGE_BUFFER descriptor type
        const BUFFER = 1<<3;
        /// Similar to DX12 UAV and vulkan STORAGE_BUFFER descriptor type
        const BUFFER_READ_WRITE = 1<<5;
        /// Similar to vulkan UNIFORM_BUFFER descriptor type
        const UNIFORM_BUFFER = 1<<7;
        // Push constant / Root constant
        /// Similar to DX12 root constants and vulkan push constants
        const ROOT_CONSTANT = 1<<8;
        // Input assembler
        /// Similar to vulkan VERTEX_BUFFER buffer usage flag
        const VERTEX_BUFFER = 1<<9;
        /// Similar to vulkan INDEX_BUFFER buffer usage flag
        const INDEX_BUFFER = 1<<10;
        /// Similar to vulkan INDIRECT_BUFFER buffer usage flag
        const INDIRECT_BUFFER = 1<<11;
        // Cubemap SRV
        /// Similar to vulkan's CUBE_COMPATIBLE image create flag and metal's Cube texture type
        const TEXTURE_CUBE = 1<<12 | RafxResourceType::TEXTURE.bits();
        // RTV
        const RENDER_TARGET_MIP_SLICES = 1<<13;
        const RENDER_TARGET_ARRAY_SLICES = 1<<14;
        const RENDER_TARGET_DEPTH_SLICES = 1<<15;
        // Vulkan-only stuff
        const INPUT_ATTACHMENT = 1<<16;
        const TEXEL_BUFFER = 1<<17;
        const TEXEL_BUFFER_READ_WRITE = 1<<18;
        const COMBINED_IMAGE_SAMPLER = 1<<19;
        // Metal-only stuff
        const ARGUMENT_BUFFER = 1<<20;
        const INDIRECT_COMMAND_BUFFER = 1<<21;
        const RENDER_PIPELINE_STATE = 1<<22;
        // Render target types
        /// A color attachment in a renderpass
        const RENDER_TARGET_COLOR = 1<<23;
        /// A depth/stencil attachment in a renderpass
        const RENDER_TARGET_DEPTH_STENCIL = 1<<24;
    }
}

impl RafxResourceType {
    pub fn is_uniform_buffer(self) -> bool {
        self.intersects(RafxResourceType::UNIFORM_BUFFER)
    }

    pub fn is_storage_buffer(self) -> bool {
        self.intersects(RafxResourceType::BUFFER | RafxResourceType::BUFFER_READ_WRITE)
    }

    pub fn is_render_target(self) -> bool {
        self.intersects(
            RafxResourceType::RENDER_TARGET_COLOR | RafxResourceType::RENDER_TARGET_DEPTH_STENCIL,
        )
    }

    pub fn is_texture(self) -> bool {
        self.intersects(RafxResourceType::TEXTURE | RafxResourceType::TEXTURE_READ_WRITE)
    }
}

bitflags::bitflags! {
    /// Flags for enabling/disabling color channels, used with `RafxBlendState`
    #[cfg_attr(feature = "serde-support", derive(Serialize, Deserialize))]
    pub struct RafxColorFlags: u8 {
        const RED = 1;
        const GREEN = 2;
        const BLUE = 4;
        const ALPHA = 8;
        const ALL = 0x0F;
    }
}

impl Default for RafxColorFlags {
    fn default() -> Self {
        RafxColorFlags::ALL
    }
}

/// Indicates how the memory will be accessed and affects where in memory it needs to be allocated.
#[derive(Clone, Copy, PartialEq, Debug)]
pub enum RafxMemoryUsage {
    Unknown,

    /// The memory is only accessed by the GPU
    GpuOnly,

    /// The memory is only accessed by the CPU
    CpuOnly,

    /// The memory is written by the CPU and read by the GPU
    CpuToGpu,

    /// The memory is written by the GPU and read by the CPU
    GpuToCpu,
}

/// Indicates the result of presenting a swapchain image
#[derive(Clone, Copy, PartialEq, Debug)]
pub enum RafxPresentSuccessResult {
    /// The image was shown and the swapchain can continue to be used.
    Success,

    /// The image was shown and the swapchain can continue to be used. However, this result also
    /// hints that there is a more optimal configuration for the swapchain to be in. This is vague
    /// because the precise meaning varies between platform. For example, windows may return this
    /// when the application is minimized.
    SuccessSuboptimal,

    // While this is an "error" being returned as success, it is expected and recoverable while
    // other errors usually aren't. This way the ? operator can still be used to bail out the
    // unrecoverable errors and the different flavors of "success" should be explicitly handled
    // in a match
    /// Indicates that the swapchain can no longer be used
    DeviceReset,
}

/// Indicates the current state of a fence.
#[derive(Clone, Copy, PartialEq, Debug)]
pub enum RafxFenceStatus {
    /// The fence was submitted to the command buffer and signaled as completed by the GPU
    Complete,
    /// The fence will be signaled as complete later by the GPU
    Incomplete,
    /// The fence was never submitted, or was submitted and already returned complete once, putting
    /// it back into the unsubmitted state
    Unsubmitted,
}

bitflags::bitflags! {
    /// Indicates what render targets are affected by a blend state
    #[cfg_attr(feature = "serde-support", derive(Serialize, Deserialize))]
    pub struct RafxBlendStateTargets : u8 {
        const BLEND_STATE_TARGET_0 = 0x01;
        const BLEND_STATE_TARGET_1 = 0x02;
        const BLEND_STATE_TARGET_2 = 0x04;
        const BLEND_STATE_TARGET_3 = 0x08;
        const BLEND_STATE_TARGET_4 = 0x10;
        const BLEND_STATE_TARGET_5 = 0x20;
        const BLEND_STATE_TARGET_6 = 0x40;
        const BLEND_STATE_TARGET_7 = 0x80;
        const BLEND_STATE_TARGET_ALL = 0xFF;
    }
}

bitflags::bitflags! {
    /// Indicates a particular stage of a shader, or set of stages in a shader. Similar to
    /// VkShaderStageFlagBits
    #[derive(Default)]
    #[cfg_attr(feature = "serde-support", derive(Serialize, Deserialize))]
    pub struct RafxShaderStageFlags : u32 {
        const NONE = 0;
        const VERTEX = 1;
        const TESSELLATION_CONTROL = 2;
        const TESSELLATION_EVALUATION = 4;
        const GEOMETRY = 8;
        const FRAGMENT = 16;
        const COMPUTE = 32;
        const ALL_GRAPHICS = 0x1F;
        const ALL = 0x7FFF_FFFF;
    }
}

/// Contains all the individual stages
pub const ALL_SHADER_STAGE_FLAGS: [RafxShaderStageFlags; 6] = [
    RafxShaderStageFlags::VERTEX,
    RafxShaderStageFlags::TESSELLATION_CONTROL, // dx12 hull shader
    RafxShaderStageFlags::TESSELLATION_EVALUATION, // dx12 domain shader
    RafxShaderStageFlags::GEOMETRY,
    RafxShaderStageFlags::FRAGMENT,
    RafxShaderStageFlags::COMPUTE,
];

/// Indicates the type of pipeline, roughly corresponds with RafxQueueType
#[derive(Clone, Copy, PartialEq, Debug)]
pub enum RafxPipelineType {
    Graphics = 0,
    Compute = 1,
}

/// Affects how quickly vertex attributes are consumed from buffers, similar to VkVertexInputRate
#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
pub enum RafxVertexAttributeRate {
    Vertex,
    Instance,
}

impl Default for RafxVertexAttributeRate {
    fn default() -> Self {
        RafxVertexAttributeRate::Vertex
    }
}

/// Determines if the contents of an image attachment in a renderpass begins with its previous
/// contents, a clear value, or undefined data. Similar to VkAttachmentLoadOp
#[derive(Copy, Clone, Debug, Hash, PartialEq)]
pub enum RafxLoadOp {
    DontCare,
    Load,
    Clear,
}

impl Default for RafxLoadOp {
    fn default() -> Self {
        RafxLoadOp::DontCare
    }
}

/// Determines if the contents of an image attachment in a rander pass will store the resulting
/// state for use after the render pass
#[derive(Copy, Clone, Debug, Hash, PartialEq)]
pub enum RafxStoreOp {
    /// Do not store the image, leaving the contents of it undefined
    DontCare,

    /// Persist the image's content after a render pass completes
    Store,
}

impl Default for RafxStoreOp {
    fn default() -> Self {
        RafxStoreOp::Store
    }
}

/// How to intepret vertex data into a form of geometry. Similar to VkPrimitiveTopology
#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
#[cfg_attr(feature = "serde-support", derive(Serialize, Deserialize))]
pub enum RafxPrimitiveTopology {
    PointList,
    LineList,
    LineStrip,
    TriangleList,
    TriangleStrip,
    PatchList,
}

/// The size of index buffer elements
#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
#[cfg_attr(feature = "serde-support", derive(Serialize, Deserialize))]
pub enum RafxIndexType {
    Uint32,
    Uint16,
}

impl Default for RafxIndexType {
    fn default() -> Self {
        RafxIndexType::Uint32
    }
}

impl RafxIndexType {
    pub fn size_in_bytes(self) -> usize {
        match self {
            RafxIndexType::Uint32 => 4,
            RafxIndexType::Uint16 => 2,
        }
    }
}

/// Affects blending. Similar to VkBlendFactor
#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
#[cfg_attr(feature = "serde-support", derive(Serialize, Deserialize))]
pub enum RafxBlendFactor {
    Zero,
    One,
    SrcColor,
    OneMinusSrcColor,
    DstColor,
    OneMinusDstColor,
    SrcAlpha,
    OneMinusSrcAlpha,
    DstAlpha,
    OneMinusDstAlpha,
    SrcAlphaSaturate,
    ConstantColor,
    OneMinusConstantColor,
}

impl Default for RafxBlendFactor {
    fn default() -> Self {
        RafxBlendFactor::Zero
    }
}

/// Affects blending. Similar to VkBlendOp
#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
#[cfg_attr(feature = "serde-support", derive(Serialize, Deserialize))]
pub enum RafxBlendOp {
    Add,
    Subtract,
    ReverseSubtract,
    Min,
    Max,
}

impl Default for RafxBlendOp {
    fn default() -> Self {
        RafxBlendOp::Add
    }
}

/// Affects depth testing and sampling. Similar to VkCompareOp
#[derive(Copy, Clone, Debug, PartialEq, Eq, Hash)]
#[cfg_attr(feature = "serde-support", derive(Serialize, Deserialize))]
pub enum RafxCompareOp {
    Never,
    Less,
    Equal,
    LessOrEqual,
    Greater,
    NotEqual,
    GreaterOrEqual,
    Always,
}

impl Default for RafxCompareOp {
    fn default() -> Self {
        RafxCompareOp::Never
    }
}

/// Similar to VkStencilOp
#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
#[cfg_attr(feature = "serde-support", derive(Serialize, Deserialize))]
pub enum RafxStencilOp {
    Keep,
    Zero,
    Replace,
    IncrementAndClamp,
    DecrementAndClamp,
    Invert,
    IncrementAndWrap,
    DecrementAndWrap,
}

impl Default for RafxStencilOp {
    fn default() -> Self {
        RafxStencilOp::Keep
    }
}

/// Determines if we cull polygons that are front-facing or back-facing. Facing direction is
/// determined by RafxFrontFace, sometimes called "winding order". Similar to VkCullModeFlags
#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
#[cfg_attr(feature = "serde-support", derive(Serialize, Deserialize))]
pub enum RafxCullMode {
    None,
    Back,
    Front,
}

impl Default for RafxCullMode {
    fn default() -> Self {
        RafxCullMode::None
    }
}

/// Determines what winding order is considerered the front face of a polygon. Similar to
/// VkFrontFace
#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
#[cfg_attr(feature = "serde-support", derive(Serialize, Deserialize))]
pub enum RafxFrontFace {
    CounterClockwise,
    Clockwise,
}

impl Default for RafxFrontFace {
    fn default() -> Self {
        RafxFrontFace::CounterClockwise
    }
}

/// Whether to fill in polygons or not. Similar to VkPolygonMode
#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
#[cfg_attr(feature = "serde-support", derive(Serialize, Deserialize))]
pub enum RafxFillMode {
    Solid,
    Wireframe,
}

impl Default for RafxFillMode {
    fn default() -> Self {
        RafxFillMode::Solid
    }
}

/// Filtering method when sampling. Similar to VkFilter
#[derive(Debug, Copy, Clone, PartialEq, Eq, Hash)]
#[cfg_attr(feature = "serde-support", derive(Serialize, Deserialize))]
pub enum RafxFilterType {
    /// Finds the closest value in the texture and uses it. Commonly used for "pixel-perfect"
    /// assets.
    Nearest,

    /// "Averages" color values of the texture. A common choice for most cases but may make some
    /// "pixel-perfect" assets appear blurry
    Linear,
}

impl Default for RafxFilterType {
    fn default() -> Self {
        RafxFilterType::Nearest
    }
}

/// Affects image sampling, particularly for UV coordinates outside the [0, 1] range. Similar to
/// VkSamplerAddressMode
#[derive(Debug, Copy, Clone, PartialEq, Eq, Hash)]
#[cfg_attr(feature = "serde-support", derive(Serialize, Deserialize))]
pub enum RafxAddressMode {
    Mirror,
    Repeat,
    ClampToEdge,
    ClampToBorder,
}

impl Default for RafxAddressMode {
    fn default() -> Self {
        RafxAddressMode::Mirror
    }
}

/// Similar to VkSamplerMipmapMode
#[derive(Debug, Copy, Clone, PartialEq, Eq, Hash)]
#[cfg_attr(feature = "serde-support", derive(Serialize, Deserialize))]
pub enum RafxMipMapMode {
    Nearest,
    Linear,
}

impl Default for RafxMipMapMode {
    fn default() -> Self {
        RafxMipMapMode::Nearest
    }
}

/// A clear value for color attachments
#[derive(Copy, Clone, Debug, Default)]
pub struct RafxColorClearValue(pub [f32; 4]);

impl Hash for RafxColorClearValue {
    fn hash<H: Hasher>(
        &self,
        mut state: &mut H,
    ) {
        for &value in &self.0 {
            DecimalF32(value).hash(&mut state);
        }
    }
}

/// A clear values for depth/stencil attachments. One or both values may be used depending on the
/// format of the attached image
#[derive(Clone, Copy, Debug)]
pub struct RafxDepthStencilClearValue {
    pub depth: f32,
    pub stencil: u32,
}

impl Default for RafxDepthStencilClearValue {
    fn default() -> Self {
        RafxDepthStencilClearValue {
            depth: 0.0,
            stencil: 0,
        }
    }
}

impl Hash for RafxDepthStencilClearValue {
    fn hash<H: Hasher>(
        &self,
        mut state: &mut H,
    ) {
        DecimalF32(self.depth).hash(&mut state);
        self.stencil.hash(&mut state);
    }
}

/// Determines if a barrier is transferring a resource from one queue to another.
#[derive(Debug, Copy, Clone)]
pub enum RafxBarrierQueueTransition {
    /// No queue transition will take place
    None,

    /// A barrier for the "sending" queue. Contains the "receiving" queue. (the "sending" queue is
    /// inferred by the queue on which the barrier is submitted)
    ReleaseTo(RafxQueueType),

    /// A barrier for the "receiving" queue. Contains the "sending" queue. (the "receiving" queue is
    /// inferred by the queue on which the barrier is submitted)
    AcquireFrom(RafxQueueType),
}

impl Default for RafxBarrierQueueTransition {
    fn default() -> Self {
        RafxBarrierQueueTransition::None
    }
}

/// A memory barrier for buffers. This is used to transition buffers between resource states and
/// possibly from one queue to another
pub struct RafxBufferBarrier<'a> {
    pub buffer: &'a RafxBuffer,
    pub src_state: RafxResourceState,
    pub dst_state: RafxResourceState,
    pub queue_transition: RafxBarrierQueueTransition,
    pub offset_size: Option<RafxOffsetSize>,
}

/// A memory barrier for textures. This is used to transition textures between resource states and
/// possibly from one queue to another.
pub struct RafxTextureBarrier<'a> {
    pub texture: &'a RafxTexture,
    pub src_state: RafxResourceState,
    pub dst_state: RafxResourceState,
    pub queue_transition: RafxBarrierQueueTransition,
    /// If set, only the specified array element is included
    pub array_slice: Option<u16>,
    /// If set, only the specified mip level is included
    pub mip_slice: Option<u8>,
}

impl<'a> RafxTextureBarrier<'a> {
    /// Creates a simple state transition
    pub fn state_transition(
        texture: &'a RafxTexture,
        src_state: RafxResourceState,
        dst_state: RafxResourceState,
    ) -> RafxTextureBarrier {
        RafxTextureBarrier {
            texture,
            src_state,
            dst_state,
            queue_transition: RafxBarrierQueueTransition::None,
            array_slice: None,
            mip_slice: None,
        }
    }
}

// Recommended format/color space options for desktop vulkan are:
// HDR: R16G16B16A16_SFLOAT/EXTENDED_SRGB_LINEAR_EXT (windows only)
// Non-HDR: B8G8R8A8_SRGB/SRGB_NONLINEAR_KHR
//
// Both of these work well on apple/metal
#[derive(Debug, Copy, Clone, PartialEq, Eq, Hash)]
#[cfg_attr(feature = "serde-support", derive(Serialize, Deserialize))]
pub enum RafxSwapchainColorSpace {
    Srgb,
    SrgbExtended,

    // Only supported on apple/metal, SrgbExtended is recommended for all HDR, including
    // on apple devices.
    DisplayP3Extended,
}

impl RafxSwapchainColorSpace {
    pub fn is_extended(self) -> bool {
        match self {
            RafxSwapchainColorSpace::Srgb => false,
            RafxSwapchainColorSpace::SrgbExtended => true,
            RafxSwapchainColorSpace::DisplayP3Extended => true,
        }
    }

    pub fn is_srgb(self) -> bool {
        match self {
            RafxSwapchainColorSpace::Srgb => true,
            RafxSwapchainColorSpace::SrgbExtended => true,
            RafxSwapchainColorSpace::DisplayP3Extended => false,
        }
    }
}

impl Default for RafxSwapchainColorSpace {
    fn default() -> Self {
        RafxSwapchainColorSpace::Srgb
    }
}

/// Represents an image owned by the swapchain
#[derive(Clone)]
pub struct RafxSwapchainImage {
    pub texture: RafxTexture,
    pub swapchain_image_index: u32,
}

/// A color render target bound during a renderpass
#[derive(Debug)]
pub struct RafxColorRenderTargetBinding<'a> {
    pub texture: &'a RafxTexture,
    pub load_op: RafxLoadOp,
    pub store_op: RafxStoreOp,
    pub mip_slice: Option<u8>,
    pub array_slice: Option<u16>,
    pub clear_value: RafxColorClearValue,
    pub resolve_target: Option<&'a RafxTexture>,
    pub resolve_store_op: RafxStoreOp,
    pub resolve_mip_slice: Option<u8>,
    pub resolve_array_slice: Option<u16>,
}

/// A depth/stencil render target to be bound during a renderpass
#[derive(Debug)]
pub struct RafxDepthStencilRenderTargetBinding<'a> {
    pub texture: &'a RafxTexture,
    pub depth_load_op: RafxLoadOp,
    pub stencil_load_op: RafxLoadOp,
    pub depth_store_op: RafxStoreOp,
    pub stencil_store_op: RafxStoreOp,
    pub mip_slice: Option<u8>,
    pub array_slice: Option<u16>,
    pub clear_value: RafxDepthStencilClearValue,
}

/// A vertex buffer to be bound during a renderpass
pub struct RafxVertexBufferBinding<'a> {
    pub buffer: &'a RafxBuffer,
    pub byte_offset: u64,
}

/// An index buffer to be bound during a renderpass
pub struct RafxIndexBufferBinding<'a> {
    pub buffer: &'a RafxBuffer,
    pub byte_offset: u64,
    pub index_type: RafxIndexType,
}

/// Parameters for copying a buffer to a texture
#[derive(Default, Clone)]
pub struct RafxCmdCopyBufferToBufferParams {
    pub src_byte_offset: u64,
    pub dst_byte_offset: u64,
    pub size: u64,
}

impl RafxCmdCopyBufferToBufferParams {
    pub fn full_copy(
        src_def: &RafxBufferDef,
        dst_def: &RafxBufferDef,
    ) -> Self {
        assert_eq!(src_def.size, dst_def.size);
        RafxCmdCopyBufferToBufferParams {
            src_byte_offset: 0,
            dst_byte_offset: 0,
            size: src_def.size,
        }
    }
}

/// Parameters for copying a buffer to a texture
#[derive(Default, Clone)]
pub struct RafxCmdCopyBufferToTextureParams {
    pub buffer_offset: u64,
    pub array_layer: u16,
    pub mip_level: u8,
}

#[derive(Default, Clone)]
pub struct RafxCmdCopyTextureToTextureParams {
    pub src_offset: RafxExtents3D,
    pub dst_offset: RafxExtents3D,
    pub extents: RafxExtents3D,
    pub src_mip_level: u8,
    pub dst_mip_level: u8,
    // If none, operate on all image slices (we assume images have same number of slices)
    pub array_slices: Option<[u16; 2]>,
}

/// Parameters for blitting one image to another (vulkan backend only)
#[derive(Clone)]
pub struct RafxCmdBlitParams {
    pub src_state: RafxResourceState,
    pub dst_state: RafxResourceState,
    pub src_extents: [RafxExtents3D; 2],
    pub dst_extents: [RafxExtents3D; 2],
    pub src_mip_level: u8,
    pub dst_mip_level: u8,
    pub array_slices: Option<[u16; 2]>,
}

/// A rafx-specific index that refers to a particular binding. Instead of doing name/binding lookups
/// every frame, query the descriptor index during startup and use it instead. This is a more
/// efficient way to address descriptors.
#[derive(Copy, Clone, PartialEq, Eq, Hash, Debug)]
pub struct RafxDescriptorIndex(pub(crate) u32);

/// Selects a particular descriptor in a descriptor set
#[derive(Clone, PartialEq, Eq, Hash, Debug)]
pub enum RafxDescriptorKey<'a> {
    Undefined,
    Name(&'a str),
    Binding(u32),
    DescriptorIndex(RafxDescriptorIndex),
}

impl<'a> Default for RafxDescriptorKey<'a> {
    fn default() -> Self {
        RafxDescriptorKey::Undefined
    }
}

/// Used in various APIs where we supply an offset/size pair
#[derive(Default, Clone, Copy, Debug)]
pub struct RafxOffsetSize {
    pub byte_offset: u64,
    pub size: u64,
}

/// Specifies what value to assign to a descriptor set
#[derive(Default, Debug)]
pub struct RafxDescriptorElements<'a> {
    pub textures: Option<&'a [&'a RafxTexture]>,
    pub samplers: Option<&'a [&'a RafxSampler]>,
    pub buffers: Option<&'a [&'a RafxBuffer]>,
    pub buffer_offset_sizes: Option<&'a [RafxOffsetSize]>,
}

/// Used when binding a texture to select between different ways to bind the texture
#[derive(Clone, Copy, Debug, PartialEq, Eq, Hash)]
pub enum RafxTextureBindType {
    // Color or depth only
    Srv,
    // stencil?
    SrvStencil,
    // Bind all mip levels of the 0th provided texture
    UavMipChain,
    // Bind a particular mip slice of all provided textures
    UavMipSlice(u32),
}

/// Describes how to update a single descriptor
#[derive(Debug)]
pub struct RafxDescriptorUpdate<'a> {
    pub array_index: u32,
    pub descriptor_key: RafxDescriptorKey<'a>,
    pub elements: RafxDescriptorElements<'a>,
    pub dst_element_offset: u32,
    // Srv when read-only, UavMipSlice(0) when read-write
    pub texture_bind_type: Option<RafxTextureBindType>,
}

impl<'a> Default for RafxDescriptorUpdate<'a> {
    fn default() -> Self {
        RafxDescriptorUpdate {
            array_index: 0,
            descriptor_key: RafxDescriptorKey::Undefined,
            elements: RafxDescriptorElements::default(),
            dst_element_offset: 0,
            texture_bind_type: None,
        }
    }
}

// Corresponds 1:1 with VkDrawIndirectCommand, MTLDrawPrimitivesIndirectArguments, D3D12_DRAW_ARGUMENTS
pub struct RafxDrawIndirectCommand {
    pub vertex_count: u32,
    pub instance_count: u32,
    pub first_vertex: u32,
    pub first_instance: u32,
}

// Corresponds 1:1 with VkDrawIndexedIndirectCommand, MTLDrawIndexedPrimitivesIndirectArguments, D3D12_DRAW_INDEXED_ARGUMENTS
pub struct RafxDrawIndexedIndirectCommand {
    pub index_count: u32,
    pub instance_count: u32,
    pub first_index: u32,
    pub vertex_offset: i32, // value added to the vertex index before indexing into the vertex buffer
    pub first_instance: u32,
}

// Corresponds 1:1 with VkDispatchIndirectCommand, MTLDispatchThreadgroupsIndirectArguments, D3D12_DISPATCH_ARGUMENTS
pub struct RafxDispatchIndirectCommand {
    pub group_count_x: u32,
    pub group_count_y: u32,
    pub group_count_z: u32,
}