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
use {
    super::PipelineLayout,
    crate::{
        format::Format,
        render_pass::RenderPass,
        sampler::CompareOp,
        shader::{FragmentShader, VertexShader},
        Rect2d,
    },
    ordered_float::OrderedFloat,
};

pub use {
    self::State::{Dynamic, Static},
    crate::backend::GraphicsPipeline,
};

/// Wrapper for pipeline states that can be either static or dynamic.
#[derive(Clone, Copy, Debug, PartialEq, Eq, Hash)]
#[cfg_attr(feature = "serde-1", derive(serde::Serialize, serde::Deserialize))]
pub enum State<T> {
    /// Static state value.
    Static { value: T },

    /// Dynamic state marker,
    /// When some state is dynamic then it must be set via
    /// specific command before rendering.
    Dynamic,
}

impl<T> State<T> {
    pub fn is_dynamic(&self) -> bool {
        match self {
            Self::Dynamic => true,
            _ => false,
        }
    }

    pub fn dynamic() -> Self {
        Self::Dynamic
    }
}

impl<T> From<T> for State<T> {
    fn from(value: T) -> Self {
        State::Static { value }
    }
}

#[derive(Clone, Copy, Debug, PartialEq, Eq, Hash)]
#[cfg_attr(feature = "serde-1", derive(serde::Serialize, serde::Deserialize))]
pub struct Bounds {
    pub offset: OrderedFloat<f32>,
    pub size: OrderedFloat<f32>,
}

/// Graphics pipeline state definition.
/// Fields are ordered to match pipeline stages, including fixed functions.
#[derive(Clone, Debug, PartialEq, Eq, Hash)]
pub struct GraphicsPipelineInfo {
    /// For each vertex buffer specifies how it is bound.
    pub vertex_bindings: Vec<VertexInputBinding>,

    /// For each vertex attribute specifies where is the data is read from.
    pub vertex_attributes: Vec<VertexInputAttribute>,

    /// Input primitives topology.
    pub primitive_topology: PrimitiveTopology,

    /// If `True` then special marker index value `!0` will restart
    /// primitive assembly next index, discarding any incomplete primitives.
    pub primitive_restart_enable: bool,

    /// Vertex shader for pipeline.
    pub vertex_shader: VertexShader,

    /// Primitives rasteriazation behavior.
    /// If `None` then no rasterization is performed.
    /// This is useful when only side-effects of earlier stages are needed.
    pub rasterizer: Option<Rasterizer>,

    /// Pipeline layout.
    pub layout: PipelineLayout,

    /// Render pass within which this pipeline will be executed.
    pub render_pass: RenderPass,

    /// Subpass of the render pass within which this pipeline will be executed.
    pub subpass: u32,
}

/// Builder for `GraphicsPipelineInfo`.
/// Used in `graphics_pipeline_info` macro.
#[doc(hidden)]
#[allow(missing_debug_implementations)]
pub struct GraphicsPipelineInfoBuilder {
    pub vertex_bindings: Vec<VertexInputBinding>,
    pub vertex_attributes: Vec<VertexInputAttribute>,
    pub primitive_topology: PrimitiveTopology,
    pub primitive_restart_enable: bool,
    pub rasterizer: Option<Rasterizer>,
    pub subpass: u32,
}

#[doc(hidden)]
impl GraphicsPipelineInfoBuilder {
    pub fn new() -> Self {
        GraphicsPipelineInfoBuilder {
            vertex_bindings: Vec::new(),
            vertex_attributes: Vec::new(),
            primitive_topology: PrimitiveTopology::TriangleList,
            primitive_restart_enable: false,
            rasterizer: None,
            subpass: 0,
        }
    }
}

/// Convenient macro to build `GraphicsPipelineInfo`.
/// Allows to skip fields when their default values are sufficient.
/// The only required fields are:
/// * `vertex_shader`
/// * `layout`
/// * `render_pass`
/// Note that default `primitive_topology` is `TriangleList`.
///
/// # Example
///
/// ```ignore
/// graphics_pipeline_info! {
///     vertex_shader: Shader::new(vertex_shader.clone()),
///     layout: pipeline_layout.clone(),
///     render_pass: render_pass.clone(),
///     rasterizer: Rasterizer::simple(
///         Shader::new(fragment_shader.clone()),
///         false,
///     ),
/// }
/// ```
#[macro_export]
macro_rules! graphics_pipeline_info {
    ($($field:ident : $value:expr),* $(,)?) => {
        graphics_pipeline_info!(@UNFOLD builder { let mut builder = GraphicsPipelineInfoBuilder::new(); } { $($field: $value),* } {})
    };

    (@UNFOLD $builder:ident { $($stmts:stmt)* } { vertex_bindings: $vertex_bindings:expr $(, $field:ident : $value:expr)* } { $($rfield:ident : $rvalue:expr),* }) => {
        graphics_pipeline_info!(@UNFOLD $builder { $($stmts)* $builder.vertex_bindings = $vertex_bindings.into(); } { $($field: $value),* } {$($rfield:$rvalue),*})
    };

    (@UNFOLD $builder:ident { $($stmts:stmt)* } { vertex_attributes: $vertex_attributes:expr $(, $field:ident : $value:expr)* } { $($rfield:ident : $rvalue:expr),* }) => {
        graphics_pipeline_info!(@UNFOLD $builder { $($stmts)* $builder.vertex_attributes = $vertex_attributes.into(); } { $($field: $value),* } {$($rfield:$rvalue),*})
    };

    (@UNFOLD $builder:ident { $($stmts:stmt)* } { primitive_topology: $primitive_topology:expr $(, $field:ident : $value:expr)* } { $($rfield:ident : $rvalue:expr),* }) => {
        graphics_pipeline_info!(@UNFOLD $builder { $($stmts)* $builder.primitive_topology = $primitive_topology.into(); } { $($field: $value),* } {$($rfield:$rvalue),*})
    };

    (@UNFOLD $builder:ident { $($stmts:stmt)* } { primitive_restart_enable: $primitive_restart_enable:expr $(, $field:ident : $value:expr)* } { $($rfield:ident : $rvalue:expr),* }) => {
        graphics_pipeline_info!(@UNFOLD $builder { $($stmts)* $builder.primitive_restart_enable = $primitive_restart_enable.into(); } { $($field: $value),* } {$($rfield:$rvalue),*})
    };

    (@UNFOLD $builder:ident { $($stmts:stmt)* } { rasterizer: $rasterizer:expr $(, $field:ident : $value:expr)* } { $($rfield:ident : $rvalue:expr),* }) => {
        graphics_pipeline_info!(@UNFOLD $builder { $($stmts)* $builder.rasterizer = $rasterizer.into(); } { $($field: $value),* } {$($rfield:$rvalue),*})
    };

    (@UNFOLD $builder:ident { $($stmts:stmt)* } { subpass: $subpass:expr $(, $field:ident : $value:expr)* } { $($rfield:ident : $rvalue:expr),* }) => {
        graphics_pipeline_info!(@UNFOLD $builder { $($stmts)* $builder.subpass = $subpass.into(); } { $($field: $value),* } {$($rfield:$rvalue),*})
    };

    (@UNFOLD $builder:ident { $($stmts:stmt)* } { layout: $layout:expr $(, $field:ident : $value:expr)* } { $($rfield:ident : $rvalue:expr),* }) => {
        graphics_pipeline_info!(@UNFOLD $builder { $($stmts)* } { $($field: $value),* } { layout: $layout $(,$rfield:$rvalue)*})
    };

    (@UNFOLD $builder:ident { $($stmts:stmt)* } { render_pass: $render_pass:expr $(, $field:ident : $value:expr)* } { $($rfield:ident : $rvalue:expr),* }) => {
        graphics_pipeline_info!(@UNFOLD $builder { $($stmts)* } { $($field: $value),* } { render_pass: $render_pass $(, $rfield:$rvalue)*})
    };

    (@UNFOLD $builder:ident { $($stmts:stmt)* } { vertex_shader: $vertex_shader:expr $(, $field:ident : $value:expr)* } { $($rfield:ident : $rvalue:expr),* }) => {
        graphics_pipeline_info!(@UNFOLD $builder { $($stmts)* } { $($field: $value),* } { vertex_shader: $vertex_shader $(,$rfield:$rvalue)*})
    };

    (@UNFOLD $builder:ident { $($stmts:stmt)* } {} { $($rfield:ident : $rvalue:expr),* }) => {
        {
            $($stmts)*
            GraphicsPipelineInfo {
                vertex_bindings: $builder.vertex_bindings,
                vertex_attributes: $builder.vertex_attributes,
                primitive_topology: $builder.primitive_topology,
                primitive_restart_enable: $builder.primitive_restart_enable,
                rasterizer: $builder.rasterizer,
                subpass: $builder.subpass,
                $($rfield: $rvalue,)*
            }
        }
    };
}

/// Vertex buffer binding bahavior.
/// Controls what subrange corresponds for vertex X of instance Y.
#[derive(Copy, Clone, Debug, PartialEq, Eq, Hash)]
#[cfg_attr(feature = "serde-1", derive(serde::Serialize, serde::Deserialize))]
pub struct VertexInputBinding {
    /// Controls iteration frequency.
    pub rate: VertexInputRate,

    /// Size of the iteration step.
    pub stride: u32,
}

/// Controls vertex input iteration frequency.
#[derive(Clone, Copy, Debug, PartialEq, Eq, Hash)]
#[cfg_attr(feature = "serde-1", derive(serde::Serialize, serde::Deserialize))]
pub enum VertexInputRate {
    /// Iterate value once per vertex.
    /// Repeat for each instance.
    Vertex,

    /// Iterate value once per instance.
    /// All vertices of an instance will use same value.
    Instance,
}

/// Vertex sub-range to attribute mapping.
#[derive(Clone, Copy, Debug, PartialEq, Eq, Hash)]
#[cfg_attr(feature = "serde-1", derive(serde::Serialize, serde::Deserialize))]
pub struct VertexInputAttribute {
    /// Attribute index.
    /// Each index must appear at most once in `VertexInput::attributes` array.
    pub location: u32,

    /// Attribute format.
    /// Controls how attribute data is interpreted.
    /// Must match attribute type in vertex shader.
    pub format: Format,

    /// Index of vertex buffer from which attribute data is read from.
    pub binding: u32,

    /// Offset of this attribute in the vertex buffer sub-range.
    pub offset: u32,
}

/// Topology of primitives.
#[derive(Clone, Copy, Debug, PartialEq, Eq, Hash)]
#[cfg_attr(feature = "serde-1", derive(serde::Serialize, serde::Deserialize))]
pub enum PrimitiveTopology {
    /// Vertices are assembled into points.
    /// Each vertex form one point primitive.
    ///
    /// # Example
    ///
    /// Vertirces `a`, `b`, `c`, `d` will form points `a`, `b`, `c`, `d`.
    PointList,

    /// Vertices are assembled into lines.
    /// Each separate pair of vertices forms one line primitive.
    ///
    /// # Example
    ///
    /// Vertirces `a`, `b`, `c`, `d` will form lines `a, b` and `c, d`.
    LineList,

    /// Vertices are assembled into lines.
    /// Each pair of vertices forms one line primitive.
    ///
    /// # Example
    ///
    /// Vertirces `a`, `b`, `c`, `d` will form lines `a, b`, `b, c` and `c, d`.
    LineStrip,

    /// Vertices are assempled into triangles.
    /// Each separate triplet of vertices forms one triangle primitive.
    ///
    /// # Example
    ///
    /// Vertirces `a`, `b`, `c`, `d`, `e`, `f` will form triangles `a, b, c`
    /// and `d, e, f`.
    TriangleList,

    /// Vertices are assempled into triangles.
    /// Each triplet of vertices forms one triangle primitive.
    ///
    /// # Example
    ///
    /// Vertirces `a`, `b`, `c`, `d`, `e`, `f` will form triangles `a, b, c`,
    /// `b, c, d`, `c, d, e` and `d, e, f`.
    TriangleStrip,

    /// Vertices are assempled into triangles.
    /// First vertex is shared with all triangles.
    /// Then each pair and shared vertex form one triangle primitive.
    ///
    ///
    /// # Example
    ///
    /// Vertirces `a`, `b`, `c`, `d`, `e`, `f` will form triangles `a, b, c`,
    /// `a, c, d`, `a, d, e` and `a, e, f`.
    TriangleFan,
}

impl Default for PrimitiveTopology {
    fn default() -> Self {
        PrimitiveTopology::TriangleList
    }
}

/// Viewport transformation.
#[derive(Clone, Copy, Debug, PartialEq, Eq, Hash)]
#[cfg_attr(feature = "serde-1", derive(serde::Serialize, serde::Deserialize))]
pub struct Viewport {
    /// Viewport bounds along X (horizontal) axis.
    pub x: Bounds,

    /// Viewport bounds along Y (vertical) axis.
    pub y: Bounds,

    /// Viewport bounds along Z (depth) axis.
    pub z: Bounds,
}

#[derive(Clone, Debug, PartialEq, Eq, Hash)]
pub struct Rasterizer {
    /// Rendering viewport transformation.
    /// Determines how vertex coordinates are transformed to framebuffer
    /// coordinates.
    pub viewport: State<Viewport>,

    /// Scissors for the viewport.
    /// Determines bounds for scissor test.
    /// If the test fails for generated fragment that fragment is discared.
    pub scissor: State<Rect2d>,

    /// Should fragments out of bounds on Z axis are clamped or discared.
    /// If `true` - fragments are clamped. This also disables primitive
    /// clipping. Otherwise they are clipped.
    ///
    /// If `DepthClamp` feature is not enabled this value must be `false`.
    pub depth_clamp: bool,

    /// How polygon front face is determined.
    pub front_face: FrontFace,

    /// How polygons are culled before rasterization.
    pub culling: Option<Culling>,

    /// How polygons are rasterized.
    /// See `PolygonMode` for description.
    ///
    /// If `fillModeNonSolid` is not enabled this value must be
    /// `PolygonMode::Fill`.
    pub polygon_mode: PolygonMode,

    /// Depth test and operations.
    pub depth_test: Option<DepthTest>,

    /// Stencil test and operations.
    pub stencil_tests: Option<StencilTests>,

    /// Depth-bounds test.
    pub depth_bounds: Option<State<Bounds>>,

    /// Fragment shader used by the pipeline.
    pub fragment_shader: Option<FragmentShader>,

    /// Attachment color blending.
    pub color_blend: ColorBlend,
}

// if depth {
//     Some(DepthTest {
//         compare: CompareOp::Less,
//         write: true,
//     })
// } else {
//     None
// }
#[doc(hiddent)]
impl Rasterizer {
    pub fn new() -> Self {
        Rasterizer {
            viewport: Dynamic,
            scissor: Dynamic,
            depth_clamp: false,
            front_face: FrontFace::Clockwise,
            culling: None,
            polygon_mode: PolygonMode::Fill,
            depth_test: None,
            stencil_tests: None,
            depth_bounds: None,
            fragment_shader: None,
            color_blend: ColorBlend::Blending {
                blending: Some(Blending {
                    color_src_factor: BlendFactor::SrcAlpha,
                    color_dst_factor: BlendFactor::OneMinusSrcAlpha,
                    color_op: BlendOp::Add,
                    alpha_src_factor: BlendFactor::One,
                    alpha_dst_factor: BlendFactor::OneMinusSrcAlpha,
                    alpha_op: BlendOp::Add,
                }),
                write_mask: ComponentMask::RGBA,
                constants: Static {
                    value: [0.0.into(), 0.0.into(), 0.0.into(), 0.0.into()],
                },
            },
        }
    }
}

/// Convenient macro to build `Rasterizer`.
/// Allows to skip fields when their default values are sufficient.
///
/// # Example
///
/// ```ignore
/// rasterizer! {
///     fragment_shader: Shader::new(fragment_shader.clone()),
/// }
/// ```
#[macro_export]
macro_rules! rasterizer {
    ($($field:ident : $value:expr),* $(,)?) => {
        rasterizer!(@UNFOLD builder { let mut builder = Rasterizer::new(); } { $($field: $value),* })
    };

    (@UNFOLD $builder:ident { $($stmts:stmt)* } { viewport: $viewport:expr $(, $field:ident : $value:expr)* }) => {
        rasterizer!(@UNFOLD $builder { $($stmts)* $builder.viewport = $viewport.into(); } { $($field: $value),* })
    };

    (@UNFOLD $builder:ident { $($stmts:stmt)* } { scissor: $scissor:expr $(, $field:ident : $value:expr)* }) => {
        rasterizer!(@UNFOLD $builder { $($stmts)* $builder.scissor = $scissor.into(); } { $($field: $value),* })
    };

    (@UNFOLD $builder:ident { $($stmts:stmt)* } { depth_clamp: $depth_clamp:expr $(, $field:ident : $value:expr)* }) => {
        rasterizer!(@UNFOLD $builder { $($stmts)* $builder.depth_clamp = $depth_clamp.into(); } { $($field: $value),* })
    };

    (@UNFOLD $builder:ident { $($stmts:stmt)* } { front_face: $front_face:expr $(, $field:ident : $value:expr)* }) => {
        rasterizer!(@UNFOLD $builder { $($stmts)* $builder.front_face = $front_face.into(); } { $($field: $value),* })
    };

    (@UNFOLD $builder:ident { $($stmts:stmt)* } { culling: $culling:expr $(, $field:ident : $value:expr)* }) => {
        rasterizer!(@UNFOLD $builder { $($stmts)* $builder.culling = $culling.into(); } { $($field: $value),* })
    };

    (@UNFOLD $builder:ident { $($stmts:stmt)* } { polygon_mode: $polygon_mode:expr $(, $field:ident : $value:expr)* }) => {
        rasterizer!(@UNFOLD $builder { $($stmts)* $builder.polygon_mode = $polygon_mode.into(); } { $($field: $value),* })
    };

    (@UNFOLD $builder:ident { $($stmts:stmt)* } { depth_test: $depth_test:expr $(, $field:ident : $value:expr)* }) => {
        rasterizer!(@UNFOLD $builder { $($stmts)* $builder.depth_test = $depth_test.into(); } { $($field: $value),* })
    };

    (@UNFOLD $builder:ident { $($stmts:stmt)* } { depth: $depth:expr $(, $field:ident : $value:expr)* }) => {
        rasterizer!(@UNFOLD $builder { $($stmts)* $builder.depth_test = if $depth { Some(DepthTest { compare: CompareOp::Less, write: true, }) } else { None }; } { $($field: $value),* })
    };

    (@UNFOLD $builder:ident { $($stmts:stmt)* } { stencil_tests: $stencil_tests:expr $(, $field:ident : $value:expr)* }) => {
        rasterizer!(@UNFOLD $builder { $($stmts)* $builder.stencil_tests = $stencil_tests.into(); } { $($field: $value),* })
    };

    (@UNFOLD $builder:ident { $($stmts:stmt)* } { depth_bounds: $depth_bounds:expr $(, $field:ident : $value:expr)* }) => {
        rasterizer!(@UNFOLD $builder { $($stmts)* $builder.depth_bounds = $depth_bounds.into(); } { $($field: $value),* })
    };

    (@UNFOLD $builder:ident { $($stmts:stmt)* } { fragment_shader: $fragment_shader:expr $(, $field:ident : $value:expr)* }) => {
        rasterizer!(@UNFOLD $builder { $($stmts)* $builder.fragment_shader = $fragment_shader.into(); } { $($field: $value),* })
    };

    (@UNFOLD $builder:ident { $($stmts:stmt)* } { color_blend: $color_blend:expr $(, $field:ident : $value:expr)* }) => {
        rasterizer!(@UNFOLD $builder { $($stmts)* $builder.color_blend = $color_blend.into(); } { $($field: $value),* })
    };

    (@UNFOLD $builder:ident { $($stmts:stmt)* } {}) => {
        {
            $($stmts)*
            Rasterizer {
                viewport: $builder.viewport,
                scissor: $builder.scissor,
                depth_clamp: $builder.depth_clamp,
                front_face: $builder.front_face,
                culling: $builder.culling,
                polygon_mode: $builder.polygon_mode,
                depth_test: $builder.depth_test,
                stencil_tests: $builder.stencil_tests,
                depth_bounds: $builder.depth_bounds,
                fragment_shader: $builder.fragment_shader,
                color_blend: $builder.color_blend,
            }
        }
    };
}

/// Polygon front face definition.
#[derive(Clone, Copy, Debug, PartialEq, Eq, Hash)]
#[cfg_attr(feature = "serde-1", derive(serde::Serialize, serde::Deserialize))]
pub enum FrontFace {
    /// Polygon which has vertices ordered in clockwise
    /// from some point of view is front faced to that point.
    Clockwise,

    /// Polygon which has vertices ordered in counter-clockwise
    /// from some point of view is front faced to that point.
    CounterClockwise,
}

impl Default for FrontFace {
    fn default() -> Self {
        FrontFace::Clockwise
    }
}

/// Polygione culling mode.
#[derive(Clone, Copy, Debug, PartialEq, Eq, Hash)]
#[cfg_attr(feature = "serde-1", derive(serde::Serialize, serde::Deserialize))]
pub enum Culling {
    /// Front facing polygons are culled.
    Front,

    /// Back facing polygons are culled.
    Back,

    /// All polygons are culled.
    FrontAndBack,
}

/// PolygonMode rasterization mode.
#[derive(Clone, Copy, Debug, PartialEq, Eq, Hash)]
#[cfg_attr(feature = "serde-1", derive(serde::Serialize, serde::Deserialize))]
pub enum PolygonMode {
    /// Whole polygon is rasterized.
    /// That is, fragments are generated to cover all points inside the
    /// polygon.
    Fill,

    /// Edges are rasterized as lines.
    /// That is, fragments are generated to cover all points on polygon edges.
    Line,

    /// Vertices are rasterized as points.
    /// That is, fragments are generated to cover only points that are polygon
    /// vertices.
    Point,
}

impl Default for PolygonMode {
    fn default() -> Self {
        PolygonMode::Fill
    }
}

/// Defines how depth testing is performed.
#[derive(Clone, Copy, Debug, PartialEq, Eq, Hash)]
#[cfg_attr(feature = "serde-1", derive(serde::Serialize, serde::Deserialize))]
pub struct DepthTest {
    /// Comparison operation between value stored in depth buffer and
    /// fragment's depth.
    pub compare: CompareOp,

    /// Whether fragment's depth should be written.
    pub write: bool,
}

#[derive(Clone, Copy, Debug, PartialEq, Eq, Hash)]
#[cfg_attr(feature = "serde-1", derive(serde::Serialize, serde::Deserialize))]
pub struct StencilTests {
    pub front: StencilTest,
    pub back: StencilTest,
}

#[derive(Clone, Copy, Debug, PartialEq, Eq, Hash)]
#[cfg_attr(feature = "serde-1", derive(serde::Serialize, serde::Deserialize))]
pub struct StencilTest {
    /// Comparison operation between value stored in stencil buffer and refence
    /// value.
    pub compare: CompareOp,

    /// Selects the bits of the unsigned integer stencil values participating
    /// in the stencil test.
    pub compare_mask: State<u32>,

    /// Selects the bits of the unsigned integer stencil values updated by the
    /// stencil test in the stencil buffer.
    pub write_mask: State<u32>,

    /// Reference value for comparison and operations.
    pub reference: State<u32>,

    /// Action performed on samples that fail the stencil test.
    pub fail: StencilOp,

    /// Action performed on samples that pass both the depth and stencil tests.
    pub pass: StencilOp,

    /// Action performed on samples that pass the stencil test and fail the
    /// depth test.
    pub depth_fail: StencilOp,
}

/// Defines what operation should be peformed on value in stencil buffer.
#[derive(Clone, Copy, Debug, PartialEq, Eq, Hash)]
#[cfg_attr(feature = "serde-1", derive(serde::Serialize, serde::Deserialize))]
pub enum StencilOp {
    /// Keep the current value.
    Keep,

    /// Write 0.
    Zero,

    /// Replace value with reference value.
    Replace,

    /// Increment value and clamp it to maximum value representable in stencil
    /// buffer format.
    IncrementAndClamp,

    /// Decrement value and clamp to 0.
    DecrementAndClamp,

    /// Invert all bits.
    Invert,

    /// Increment value and wrap to 0 if maximum value representable in stencil
    /// buffer format would be exeeded.
    IncrementAndWrap,

    /// Decrement value and wraps to maximum value representable in stencil
    /// buffer format if value would go below 0.
    DecrementAndWrap,
}

/// Defines how color stored in attachment should be blended with color output
/// of fragment shader.
#[derive(Clone, Debug, PartialEq, Eq, Hash)]
#[cfg_attr(feature = "serde-1", derive(serde::Serialize, serde::Deserialize))]
pub enum ColorBlend {
    /// Values should be treated as unsigned integers and logic operation
    /// perforned. Color format must support logic operations.
    Logic {
        /// Logical operations to be applied.
        op: LogicOp,
    },

    /// Color and alpha of all attachments should be blended in the same way.
    Blending {
        /// Blending state.
        /// If `None` then fragment's output color is written unmodified.
        blending: Option<Blending>,

        /// Bitmask that specifies components that will be written to the
        /// attachment.
        write_mask: ComponentMask,

        /// Constants for certain blending factors.
        constants: State<[OrderedFloat<f32>; 4]>,
    },

    /// Color and alpha of all attachments should be blended in specified way.
    IndependentBlending {
        /// A tuple of two states:
        /// 1. Blending state for each attachment.
        /// If `None` then fragment's output color is written unmodified.
        ///
        /// 2. Bitmask that specifies components that will be written to the
        /// attachment.
        blending: Vec<(Option<Blending>, ComponentMask)>,

        /// Constants for certain blending factors.
        constants: State<[OrderedFloat<f32>; 4]>,
    },
}

impl Default for ColorBlend {
    fn default() -> Self {
        ColorBlend::Blending {
            blending: Some(Blending {
                color_src_factor: BlendFactor::SrcAlpha,
                color_dst_factor: BlendFactor::OneMinusSrcAlpha,
                color_op: BlendOp::Add,
                alpha_src_factor: BlendFactor::One,
                alpha_dst_factor: BlendFactor::OneMinusSrcAlpha,
                alpha_op: BlendOp::Add,
            }),
            write_mask: ComponentMask::RGBA,
            constants: Static {
                value: [0.0.into(), 0.0.into(), 0.0.into(), 0.0.into()],
            },
        }
    }
}

/// Defines how color value from fragment shader's color output should be
/// blended with value stored in attachment.
#[derive(Clone, Copy, Debug, PartialEq, Eq, Hash)]
#[cfg_attr(feature = "serde-1", derive(serde::Serialize, serde::Deserialize))]
pub struct Blending {
    /// Blend factory to apply to color component values from fragment shader's
    /// color output.
    pub color_src_factor: BlendFactor,

    /// Blend factory to apply to color component values stored in attachment.
    pub color_dst_factor: BlendFactor,

    /// Operation to be performed over color component values.
    pub color_op: BlendOp,

    /// Blend factory to apply to alpha component value from fragment shader's
    /// color output.
    pub alpha_src_factor: BlendFactor,

    /// Blend factory to apply to alpha component value stored in attachment.
    pub alpha_dst_factor: BlendFactor,

    /// Operation to be performed over alpha component values.
    pub alpha_op: BlendOp,
}

/// Logical operation to be applied between color value from fragment shader's
/// color output and value stored in attachment.
///
/// For each operation comment contains an equivalent Rust expression
/// where `s` is value from fragment shader's color output
/// and `d` is value stored in attachment.
#[derive(Clone, Copy, Debug, PartialEq, Eq, Hash)]
#[cfg_attr(feature = "serde-1", derive(serde::Serialize, serde::Deserialize))]
pub enum LogicOp {
    /// `0`.
    Clear,

    /// `s & d`
    And,

    /// `s & !d`
    AndReverse,

    /// `s`
    Copy,

    /// `!s & d`
    AndInverted,

    /// `d`
    NoOp,

    /// `s ^ d`
    Xor,

    /// `s | d`
    Or,

    /// `!(s | d)`
    Nor,

    /// `!(s ^ d)`
    Equivalent,

    /// `!d`
    Invert,

    /// `s | !d`
    OrReverse,

    /// `!s`
    CopyInverted,

    /// `!s | d`
    OrInverted,

    /// `!(s & d)`
    Nand,

    /// `!0`
    Set,
}

/// Defines how blend factor is calculated.
///
/// For each variant comment contains an equivalent Rust expression
/// where `Rs`, `Gs`, `Bs` and `As` are value components from fragment shader's
/// color output, `Rd`, `Gd`, `Bd` and `Ad` are value components stored in
/// attachment, and `Rc`, `Gc`, `Bc` and `Ac` are value components defined in
/// `constants`.
#[derive(Clone, Copy, Debug, PartialEq, Eq, Hash)]
#[cfg_attr(feature = "serde-1", derive(serde::Serialize, serde::Deserialize))]
pub enum BlendFactor {
    /// Color: `(0.0, 0.0, 0.0)`
    /// Alpha: `0.0`
    Zero,

    /// Color: `(1.0, 1.0, 1.0)`
    /// Alpha: `1.0`
    One,

    /// Color: `(Rs, Gs, Bs)`
    /// Alpha: `As`
    SrcColor,

    /// Color: `(1.0 - Rs, 1.0 - Gs, 1.0 - Bs)`
    /// Alpha: `1.0 - As`
    OneMinusSrcColor,

    /// Color: `(Rd, Gd, Bd)`
    /// Alpha: `Ad`
    DstColor,

    /// Color: `(1.0 - Rd, 1.0 - Gd, 1.0 - Bd)`
    /// Alpha: `1.0 - Ad`
    OneMinusDstColor,

    /// Color: `(As, As, As)`
    /// Alpha: `As`
    SrcAlpha,

    /// Color: `(1.0 - As, 1.0 - As, 1.0 - As)`
    /// Alpha: `1.0 - As`
    OneMinusSrcAlpha,

    /// Color: `(Ad, Ad, Ad)`
    /// Alpha: `Ad`
    DstAlpha,

    /// Color: `(1.0 - Ad, 1.0 - Ad, 1.0 - Ad)`
    /// Alpha: `1.0 - Ad`
    OneMinusDstAlpha,

    /// Color: `(Rc, Gc, Bc)`
    /// Alpha: `Ac`
    ConstantColor,

    /// Color: `(1.0 - Rc, 1.0 - Gc, 1.0 - Bc)`
    /// Alpha: `1.0 - Ac`
    OneMinusConstantColor,

    /// Color: `(Ac, Ac, Ac)`
    /// Alpha: `Ac`
    ConstantAlpha,

    /// Color: `(1.0 - Ac, 1.0 - Ac, 1.0 - Ac)`
    /// Alpha: `1.0 - Ac`
    OneMinusConstantAlpha,

    /// Color: `{let f = min(As, 1.0 - Ad); (f,f,f)}`
    /// Alpha: `1.0`
    SrcAlphaSaturate,
}

/// Blending operation to be applied between color value from fragment shader's
/// color output and value stored in attachment.
///
/// For each operation comment contains an equivalent Rust expression
/// where `S` is value from fragment shader's color output, `Sf` is factor
/// calculated for fragment shader's color output, D` is value stored in
/// attachment and `Df` is factor calculated for value stored in attachment.
#[derive(Clone, Copy, Debug, PartialEq, Eq, Hash)]
#[cfg_attr(feature = "serde-1", derive(serde::Serialize, serde::Deserialize))]
pub enum BlendOp {
    /// `S * Sf + D * Df`.
    Add,

    /// `S * Sf - D * Df`
    Subtract,

    /// `D * Df - S * Sf`
    ReverseSubtract,

    /// `min(S, D)`
    Min,

    /// `max(S, D)`
    Max,
}

bitflags::bitflags! {
    /// Flags for each of color components.
    #[cfg_attr(feature = "serde-1", derive(serde::Serialize, serde::Deserialize))]
    pub struct ComponentMask: u8 {
        const R = 0b0001;
        const G = 0b0010;
        const B = 0b0100;
        const A = 0b1000;
        const RGB = 0b0111;
        const RGBA = 0b1111;
    }
}