logo
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
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
1293
1294
1295
1296
1297
1298
1299
1300
1301
1302
1303
1304
1305
1306
1307
1308
1309
1310
1311
1312
1313
1314
1315
1316
1317
1318
1319
1320
1321
1322
1323
1324
1325
1326
1327
1328
1329
1330
1331
1332
1333
1334
1335
1336
1337
1338
1339
1340
1341
1342
1343
1344
1345
1346
1347
1348
1349
1350
1351
1352
1353
1354
1355
1356
1357
1358
1359
1360
1361
1362
1363
1364
1365
1366
1367
1368
1369
1370
1371
1372
1373
1374
1375
1376
1377
1378
1379
1380
1381
1382
1383
1384
1385
1386
1387
1388
1389
1390
1391
1392
1393
1394
1395
1396
1397
1398
1399
1400
1401
1402
1403
1404
1405
1406
1407
1408
1409
1410
1411
1412
1413
1414
1415
1416
1417
1418
1419
1420
1421
1422
1423
1424
1425
1426
1427
1428
1429
1430
1431
1432
1433
1434
1435
1436
1437
1438
1439
1440
1441
1442
1443
1444
1445
1446
1447
1448
1449
1450
1451
1452
1453
1454
1455
1456
1457
1458
1459
1460
1461
1462
1463
1464
1465
1466
1467
1468
1469
1470
1471
1472
1473
1474
1475
1476
1477
1478
1479
1480
1481
1482
1483
1484
1485
1486
1487
1488
1489
1490
1491
1492
1493
1494
1495
1496
1497
1498
1499
1500
1501
1502
1503
1504
1505
1506
1507
1508
1509
1510
1511
1512
1513
1514
1515
1516
1517
1518
1519
1520
1521
1522
1523
1524
1525
1526
1527
1528
1529
1530
1531
1532
1533
1534
1535
1536
1537
1538
1539
1540
1541
1542
1543
1544
1545
1546
1547
1548
1549
1550
1551
1552
1553
1554
1555
1556
1557
1558
1559
1560
1561
1562
1563
1564
1565
1566
1567
1568
1569
1570
1571
1572
1573
1574
1575
1576
1577
1578
1579
1580
1581
1582
1583
1584
1585
1586
1587
1588
1589
1590
1591
1592
1593
1594
1595
1596
1597
1598
1599
1600
1601
1602
1603
1604
1605
1606
1607
1608
1609
1610
1611
1612
1613
1614
1615
1616
1617
1618
1619
1620
1621
1622
1623
1624
1625
1626
1627
1628
1629
1630
1631
1632
1633
1634
1635
1636
1637
1638
1639
1640
1641
1642
1643
1644
1645
1646
1647
1648
1649
1650
1651
1652
1653
1654
1655
1656
1657
1658
1659
1660
1661
1662
1663
1664
1665
1666
1667
1668
1669
1670
1671
1672
1673
1674
1675
1676
1677
1678
1679
1680
1681
1682
1683
1684
1685
1686
1687
1688
1689
1690
1691
1692
1693
1694
1695
1696
1697
1698
1699
1700
1701
1702
1703
1704
1705
1706
1707
1708
1709
1710
1711
1712
1713
1714
1715
1716
1717
1718
1719
1720
1721
1722
1723
1724
1725
1726
1727
1728
1729
1730
1731
1732
1733
1734
1735
1736
1737
1738
1739
1740
1741
1742
1743
1744
// Copyright (c) 2017 The vulkano developers
// Licensed under the Apache License, Version 2.0
// <LICENSE-APACHE or
// https://www.apache.org/licenses/LICENSE-2.0> or the MIT
// license <LICENSE-MIT or https://opensource.org/licenses/MIT>,
// at your option. All files in the project carrying such
// notice may not be copied, modified, or distributed except
// according to those terms.

// TODO: graphics pipeline params are deprecated, but are still the primary implementation in order
// to avoid duplicating code, so we hide the warnings for now
#![allow(deprecated)]

use crate::check_errors;
use crate::descriptor_set::layout::{DescriptorSetLayout, DescriptorSetLayoutCreateInfo};
use crate::device::Device;
use crate::format::NumericType;
use crate::pipeline::cache::PipelineCache;
use crate::pipeline::graphics::color_blend::{
    AttachmentBlend, ColorBlendAttachmentState, ColorBlendState, ColorComponents, LogicOp,
};
use crate::pipeline::graphics::depth_stencil::DepthStencilState;
use crate::pipeline::graphics::discard_rectangle::DiscardRectangleState;
use crate::pipeline::graphics::input_assembly::{InputAssemblyState, PrimitiveTopology};
use crate::pipeline::graphics::multisample::MultisampleState;
use crate::pipeline::graphics::rasterization::{
    CullMode, FrontFace, PolygonMode, RasterizationState,
};
use crate::pipeline::graphics::tessellation::TessellationState;
use crate::pipeline::graphics::vertex_input::{
    BuffersDefinition, Vertex, VertexDefinition, VertexInputState,
};
use crate::pipeline::graphics::viewport::{Scissor, Viewport, ViewportState};
use crate::pipeline::graphics::{GraphicsPipeline, GraphicsPipelineCreationError};
use crate::pipeline::layout::{PipelineLayout, PipelineLayoutCreateInfo, PushConstantRange};
use crate::pipeline::{DynamicState, PartialStateMode, StateMode};
use crate::render_pass::Subpass;
use crate::shader::{
    DescriptorRequirements, EntryPoint, ShaderExecution, ShaderStage, SpecializationConstants,
    SpecializationMapEntry,
};
use crate::DeviceSize;
use crate::VulkanObject;
use smallvec::SmallVec;
use std::collections::{hash_map::Entry, HashMap};
use std::mem::MaybeUninit;
use std::ptr;
use std::sync::Arc;
use std::u32;

/// Prototype for a `GraphicsPipeline`.
#[derive(Debug)]
pub struct GraphicsPipelineBuilder<'vs, 'tcs, 'tes, 'gs, 'fs, Vdef, Vss, Tcss, Tess, Gss, Fss> {
    subpass: Option<Subpass>,
    cache: Option<Arc<PipelineCache>>,

    vertex_shader: Option<(EntryPoint<'vs>, Vss)>,
    tessellation_shaders: Option<TessellationShaders<'tcs, 'tes, Tcss, Tess>>,
    geometry_shader: Option<(EntryPoint<'gs>, Gss)>,
    fragment_shader: Option<(EntryPoint<'fs>, Fss)>,

    vertex_input_state: Vdef,
    input_assembly_state: InputAssemblyState,
    tessellation_state: TessellationState,
    viewport_state: ViewportState,
    discard_rectangle_state: DiscardRectangleState,
    rasterization_state: RasterizationState,
    multisample_state: MultisampleState,
    depth_stencil_state: DepthStencilState,
    color_blend_state: ColorBlendState,
}

// Additional parameters if tessellation is used.
#[derive(Clone, Debug)]
struct TessellationShaders<'tcs, 'tes, Tcss, Tess> {
    control: (EntryPoint<'tcs>, Tcss),
    evaluation: (EntryPoint<'tes>, Tess),
}

impl
    GraphicsPipelineBuilder<
        'static,
        'static,
        'static,
        'static,
        'static,
        VertexInputState,
        (),
        (),
        (),
        (),
        (),
    >
{
    /// Builds a new empty builder.
    pub(super) fn new() -> Self {
        GraphicsPipelineBuilder {
            subpass: None,
            cache: None,

            vertex_shader: None,
            tessellation_shaders: None,
            geometry_shader: None,
            fragment_shader: None,

            vertex_input_state: Default::default(),
            input_assembly_state: Default::default(),
            tessellation_state: Default::default(),
            viewport_state: Default::default(),
            discard_rectangle_state: Default::default(),
            rasterization_state: Default::default(),
            multisample_state: Default::default(),
            depth_stencil_state: Default::default(),
            color_blend_state: Default::default(),
        }
    }
}

impl<'vs, 'tcs, 'tes, 'gs, 'fs, Vdef, Vss, Tcss, Tess, Gss, Fss>
    GraphicsPipelineBuilder<'vs, 'tcs, 'tes, 'gs, 'fs, Vdef, Vss, Tcss, Tess, Gss, Fss>
where
    Vdef: VertexDefinition,
    Vss: SpecializationConstants,
    Tcss: SpecializationConstants,
    Tess: SpecializationConstants,
    Gss: SpecializationConstants,
    Fss: SpecializationConstants,
{
    /// Builds the graphics pipeline, using an inferred a pipeline layout.
    pub fn build(
        self,
        device: Arc<Device>,
    ) -> Result<Arc<GraphicsPipeline>, GraphicsPipelineCreationError> {
        self.with_auto_layout(device, |_| {})
    }

    /// The same as `new`, but allows you to provide a closure that is given a mutable reference to
    /// the inferred descriptor set definitions. This can be used to make changes to the layout
    /// before it's created, for example to add dynamic buffers or immutable samplers.
    pub fn with_auto_layout<F>(
        self,
        device: Arc<Device>,
        func: F,
    ) -> Result<Arc<GraphicsPipeline>, GraphicsPipelineCreationError>
    where
        F: FnOnce(&mut [DescriptorSetLayoutCreateInfo]),
    {
        let (set_layout_create_infos, push_constant_ranges) = {
            let stages: SmallVec<[&EntryPoint; 5]> = [
                self.vertex_shader.as_ref().map(|s| &s.0),
                self.tessellation_shaders.as_ref().map(|s| &s.control.0),
                self.tessellation_shaders.as_ref().map(|s| &s.evaluation.0),
                self.geometry_shader.as_ref().map(|s| &s.0),
                self.fragment_shader.as_ref().map(|s| &s.0),
            ]
            .into_iter()
            .flatten()
            .collect();

            // Produce `DescriptorRequirements` for each binding, by iterating over all shaders
            // and adding the requirements of each.
            let mut descriptor_requirements: HashMap<(u32, u32), DescriptorRequirements> =
                HashMap::default();

            for (loc, reqs) in stages
                .iter()
                .map(|shader| shader.descriptor_requirements())
                .flatten()
            {
                match descriptor_requirements.entry(loc) {
                    Entry::Occupied(entry) => {
                        // Previous shaders already added requirements, so we produce the
                        // intersection of the previous requirements and those of the
                        // current shader.
                        let previous = entry.into_mut();
                        *previous = previous.intersection(reqs).expect("Could not produce an intersection of the shader descriptor requirements");
                    }
                    Entry::Vacant(entry) => {
                        // No previous shader had this descriptor yet, so we just insert the
                        // requirements.
                        entry.insert(reqs.clone());
                    }
                }
            }

            // Build a description of a descriptor set layout from the shader requirements, then
            // feed it to the user-provided closure to allow tweaking.
            let mut set_layout_create_infos = DescriptorSetLayoutCreateInfo::from_requirements(
                descriptor_requirements
                    .iter()
                    .map(|(&loc, reqs)| (loc, reqs)),
            );
            func(&mut set_layout_create_infos);

            // We want to union each push constant range into a set of ranges that do not have intersecting stage flags.
            // e.g. The range [0, 16) is either made available to Vertex | Fragment or we only make [0, 16) available to
            // Vertex and a subrange available to Fragment, like [0, 8)
            let mut range_map = HashMap::new();
            for stage in stages.iter() {
                if let Some(range) = stage.push_constant_requirements() {
                    match range_map.entry((range.offset, range.size)) {
                        Entry::Vacant(entry) => {
                            entry.insert(range.stages);
                        }
                        Entry::Occupied(mut entry) => {
                            *entry.get_mut() = *entry.get() | range.stages;
                        }
                    }
                }
            }
            let push_constant_ranges: Vec<_> = range_map
                .iter()
                .map(|((offset, size), stages)| PushConstantRange {
                    stages: *stages,
                    offset: *offset,
                    size: *size,
                })
                .collect();

            (set_layout_create_infos, push_constant_ranges)
        };

        let set_layouts = set_layout_create_infos
            .into_iter()
            .map(|desc| DescriptorSetLayout::new(device.clone(), desc))
            .collect::<Result<Vec<_>, _>>()?;
        let pipeline_layout = PipelineLayout::new(
            device.clone(),
            PipelineLayoutCreateInfo {
                set_layouts,
                push_constant_ranges,
                ..Default::default()
            },
        )
        .unwrap();
        self.with_pipeline_layout(device, pipeline_layout)
    }

    /// Builds the graphics pipeline.
    ///
    /// Does the same as `build`, except that `build` automatically builds the pipeline layout
    /// object corresponding to the union of your shaders while this function allows you to specify
    /// the pipeline layout.
    pub fn with_pipeline_layout(
        mut self,
        device: Arc<Device>,
        pipeline_layout: Arc<PipelineLayout>,
    ) -> Result<Arc<GraphicsPipeline>, GraphicsPipelineCreationError> {
        // TODO: return errors instead of panicking if missing param

        let fns = device.fns();
        let subpass = self.subpass.take().expect("Missing subpass");
        let self_vertex_input_state = self
            .vertex_input_state
            .definition(self.vertex_shader.as_ref().unwrap().0.input_interface())?;

        // Check individual shaders
        if let Some(vertex_shader) = &self.vertex_shader {
            match self.vertex_shader.as_ref().unwrap().0.execution() {
                ShaderExecution::Vertex => (),
                _ => return Err(GraphicsPipelineCreationError::WrongShaderType),
            }

            // Check that the vertex input state contains attributes for all the shader's input
            // variables.
            for element in vertex_shader.0.input_interface().elements() {
                assert!(!element.ty.is_64bit); // TODO: implement
                let location_range =
                    element.location..element.location + element.ty.num_locations();

                for location in location_range {
                    let attribute_desc = match self_vertex_input_state.attributes.get(&location) {
                        Some(attribute_desc) => attribute_desc,
                        None => {
                            return Err(
                                GraphicsPipelineCreationError::VertexInputAttributeMissing {
                                    location,
                                },
                            )
                        }
                    };

                    // TODO: Check component assignments too. Multiple variables can occupy the same
                    // location but in different components.

                    let shader_type = element.ty.to_format().type_color().unwrap();
                    let attribute_type = attribute_desc.format.type_color().unwrap();

                    if !matches!(
                        (shader_type, attribute_type),
                        (
                            NumericType::SFLOAT
                                | NumericType::UFLOAT
                                | NumericType::SNORM
                                | NumericType::UNORM
                                | NumericType::SSCALED
                                | NumericType::USCALED
                                | NumericType::SRGB,
                            NumericType::SFLOAT
                                | NumericType::UFLOAT
                                | NumericType::SNORM
                                | NumericType::UNORM
                                | NumericType::SSCALED
                                | NumericType::USCALED
                                | NumericType::SRGB,
                        ) | (NumericType::SINT, NumericType::SINT)
                            | (NumericType::UINT, NumericType::UINT)
                    ) {
                        return Err(
                            GraphicsPipelineCreationError::VertexInputAttributeIncompatibleFormat {
                                location,
                                shader_type,
                                attribute_type,
                            },
                        );
                    }
                }
            }
        } else {
            panic!("Missing vertex shader"); // TODO: return error
        }

        if let Some(tessellation_shaders) = &self.tessellation_shaders {
            // FIXME: must check that the control shader and evaluation shader are compatible

            if !device.enabled_features().tessellation_shader {
                return Err(GraphicsPipelineCreationError::FeatureNotEnabled {
                    feature: "tessellation_shader",
                    reason: "a tessellation shader was provided",
                });
            }

            match tessellation_shaders.control.0.execution() {
                ShaderExecution::TessellationControl => (),
                _ => return Err(GraphicsPipelineCreationError::WrongShaderType),
            }

            match tessellation_shaders.evaluation.0.execution() {
                ShaderExecution::TessellationEvaluation => (),
                _ => return Err(GraphicsPipelineCreationError::WrongShaderType),
            }

            if subpass.render_pass().views_used() != 0
                && !device.enabled_features().multiview_tessellation_shader
            {
                return Err(
                        GraphicsPipelineCreationError::FeatureNotEnabled {
                            feature: "multiview_tessellation_shader",
                            reason: "a tessellation shader was provided, and the render pass has multiview enabled with more than zero layers used",
                        },
                    );
            }
        }

        if let Some(geometry_shader) = &self.geometry_shader {
            if !device.enabled_features().geometry_shader {
                return Err(GraphicsPipelineCreationError::FeatureNotEnabled {
                    feature: "geometry_shader",
                    reason: "a geometry shader was provided",
                });
            }

            let input = match geometry_shader.0.execution() {
                ShaderExecution::Geometry(execution) => execution.input,
                _ => return Err(GraphicsPipelineCreationError::WrongShaderType),
            };

            if let PartialStateMode::Fixed(topology) = self.input_assembly_state.topology {
                if !input.is_compatible_with(topology) {
                    return Err(GraphicsPipelineCreationError::TopologyNotMatchingGeometryShader);
                }
            }

            if subpass.render_pass().views_used() != 0
                && !device.enabled_features().multiview_geometry_shader
            {
                return Err(
                    GraphicsPipelineCreationError::FeatureNotEnabled {
                        feature: "multiview_geometry_shader",
                        reason: "a geometry shader was provided, and the render pass has multiview enabled with more than zero layers used",
                    },
                );
            }

            // TODO: VUID-VkGraphicsPipelineCreateInfo-pStages-00739
            // If the pipeline is being created with pre-rasterization shader state and pStages
            // includes a geometry shader stage, and also includes tessellation shader stages,
            // its shader code must contain an OpExecutionMode instruction that specifies an
            // input primitive type that is compatible with the primitive topology that is
            // output by the tessellation stages
        }

        if let Some(fragment_shader) = &self.fragment_shader {
            match fragment_shader.0.execution() {
                ShaderExecution::Fragment => (),
                _ => return Err(GraphicsPipelineCreationError::WrongShaderType),
            }

            // Check that the subpass can accept the output of the fragment shader.
            // TODO: If there is no fragment shader, what should be checked then? The previous stage?
            if !subpass.is_compatible_with(fragment_shader.0.output_interface()) {
                return Err(GraphicsPipelineCreationError::FragmentShaderRenderPassIncompatible);
            }
        } else {
            // TODO: should probably error out here at least under some circumstances?
        }

        // Collect all the shader stages for operations not specific to any shader type.
        struct ShaderStageInfo<'a> {
            entry_point: &'a EntryPoint<'a>,
            specialization_map_entries: &'a [SpecializationMapEntry],
            specialization_data: &'a [u8],
        }

        let stages_info: SmallVec<[ShaderStageInfo; 5]> = [
            self.vertex_shader
                .as_ref()
                .map(|(entry_point, spec_consts)| ShaderStageInfo {
                    entry_point,
                    specialization_map_entries: Vss::descriptors(),
                    specialization_data: unsafe {
                        std::slice::from_raw_parts(
                            spec_consts as *const _ as *const u8,
                            std::mem::size_of_val(spec_consts),
                        )
                    },
                }),
            self.tessellation_shaders.as_ref().map(
                |TessellationShaders {
                     control: (entry_point, spec_consts),
                     ..
                 }| ShaderStageInfo {
                    entry_point,
                    specialization_map_entries: Tcss::descriptors(),
                    specialization_data: unsafe {
                        std::slice::from_raw_parts(
                            spec_consts as *const _ as *const u8,
                            std::mem::size_of_val(spec_consts),
                        )
                    },
                },
            ),
            self.tessellation_shaders.as_ref().map(
                |TessellationShaders {
                     evaluation: (entry_point, spec_consts),
                     ..
                 }| ShaderStageInfo {
                    entry_point,
                    specialization_map_entries: Tess::descriptors(),
                    specialization_data: unsafe {
                        std::slice::from_raw_parts(
                            spec_consts as *const _ as *const u8,
                            std::mem::size_of_val(spec_consts),
                        )
                    },
                },
            ),
            self.geometry_shader
                .as_ref()
                .map(|(entry_point, spec_consts)| ShaderStageInfo {
                    entry_point,
                    specialization_map_entries: Gss::descriptors(),
                    specialization_data: unsafe {
                        std::slice::from_raw_parts(
                            spec_consts as *const _ as *const u8,
                            std::mem::size_of_val(spec_consts),
                        )
                    },
                }),
            self.fragment_shader
                .as_ref()
                .map(|(entry_point, spec_consts)| ShaderStageInfo {
                    entry_point,
                    specialization_map_entries: Fss::descriptors(),
                    specialization_data: unsafe {
                        std::slice::from_raw_parts(
                            spec_consts as *const _ as *const u8,
                            std::mem::size_of_val(spec_consts),
                        )
                    },
                }),
        ]
        .into_iter()
        .flatten()
        .collect();

        let mut descriptor_requirements: HashMap<(u32, u32), DescriptorRequirements> =
            HashMap::default();

        // Check that the pipeline layout is compatible with the shader requirements, and collect
        // an intersection of the requirements.
        // TODO: more details in the errors
        for stage_info in &stages_info {
            pipeline_layout.ensure_compatible_with_shader(
                stage_info.entry_point.descriptor_requirements(),
                stage_info.entry_point.push_constant_requirements(),
            )?;

            for (loc, reqs) in stage_info.entry_point.descriptor_requirements() {
                match descriptor_requirements.entry(loc) {
                    Entry::Occupied(entry) => {
                        let previous = entry.into_mut();
                        *previous = previous.intersection(reqs).expect("Could not produce an intersection of the shader descriptor requirements");
                    }
                    Entry::Vacant(entry) => {
                        entry.insert(reqs.clone());
                    }
                }
            }
        }

        let num_used_descriptor_sets = descriptor_requirements
            .keys()
            .map(|loc| loc.0)
            .max()
            .map(|x| x + 1)
            .unwrap_or(0);

        // Check that the input interface of each shader matches the output interface of the
        // previous shader.
        // TODO: this check is too strict; the output only has to be a superset, any variables
        // not used in the input of the next shader are just ignored.
        for (output, input) in stages_info.iter().zip(stages_info.iter().skip(1)) {
            if let Err(err) = input
                .entry_point
                .input_interface()
                .matches(output.entry_point.output_interface())
            {
                return Err(GraphicsPipelineCreationError::ShaderStagesMismatch(err));
            }
        }

        // Creating the specialization for each stage.
        let specialization: SmallVec<[_; 5]> = stages_info
            .iter()
            .map(|stage_info| {
                for (constant_id, reqs) in stage_info
                    .entry_point
                    .specialization_constant_requirements()
                {
                    let map_entry = stage_info
                        .specialization_map_entries
                        .iter()
                        .find(|desc| desc.constant_id == constant_id)
                        .ok_or(
                            GraphicsPipelineCreationError::IncompatibleSpecializationConstants,
                        )?;

                    if map_entry.size as DeviceSize != reqs.size {
                        return Err(
                            GraphicsPipelineCreationError::IncompatibleSpecializationConstants,
                        );
                    }
                }

                Ok(ash::vk::SpecializationInfo {
                    map_entry_count: stage_info.specialization_map_entries.len() as u32,
                    p_map_entries: stage_info.specialization_map_entries.as_ptr() as *const _,
                    data_size: stage_info.specialization_data.len(),
                    p_data: stage_info.specialization_data.as_ptr() as *const _,
                })
            })
            .collect::<Result<_, _>>()?;

        debug_assert_eq!(specialization.len(), stages_info.len());

        // Creating the shader stages themselves.
        let stages: SmallVec<[_; 5]> = stages_info
            .iter()
            .zip(&specialization)
            .map(|(stage_info, specialization)| {
                let stage = ShaderStage::from(*stage_info.entry_point.execution());

                ash::vk::PipelineShaderStageCreateInfo {
                    flags: ash::vk::PipelineShaderStageCreateFlags::empty(),
                    stage: stage.into(),
                    module: stage_info.entry_point.module().internal_object(),
                    p_name: stage_info.entry_point.name().as_ptr(),
                    p_specialization_info: specialization as *const _,
                    ..Default::default()
                }
            })
            .collect();

        debug_assert_eq!(stages.len(), stages_info.len());

        // Will contain the list of dynamic states. Filled while checking the states.
        let mut dynamic_state_modes: HashMap<DynamicState, bool> = HashMap::default();

        // Vertex input state
        let binding_descriptions = self_vertex_input_state.to_vulkan_bindings(&device)?;
        let attribute_descriptions = self_vertex_input_state.to_vulkan_attributes(&device)?;
        let binding_divisor_descriptions =
            self_vertex_input_state.to_vulkan_binding_divisors(&device)?;
        let binding_divisor_state = self_vertex_input_state
            .to_vulkan_binding_divisor_state(binding_divisor_descriptions.as_ref());
        let vertex_input_state = Some(self_vertex_input_state.to_vulkan(
            &device,
            &mut dynamic_state_modes,
            &binding_descriptions,
            &attribute_descriptions,
            binding_divisor_state.as_ref(),
        ));

        // Input assembly state
        let input_assembly_state = if self.vertex_shader.is_some() {
            Some(
                self.input_assembly_state
                    .to_vulkan(&device, &mut dynamic_state_modes)?,
            )
        } else {
            None
        };

        // Tessellation state
        let tessellation_state = if self.tessellation_shaders.is_some() {
            Some(self.tessellation_state.to_vulkan(
                &device,
                &mut dynamic_state_modes,
                &self.input_assembly_state,
            )?)
        } else {
            None
        };

        // Viewport state
        let (viewport_count, viewports, scissor_count, scissors) = self
            .viewport_state
            .to_vulkan_viewports_scissors(&device, &mut dynamic_state_modes)?;
        let viewport_state = Some(self.viewport_state.to_vulkan(
            &device,
            &mut dynamic_state_modes,
            viewport_count,
            &viewports,
            scissor_count,
            &scissors,
        )?);

        // Discard rectangle state
        let discard_rectangles = self
            .discard_rectangle_state
            .to_vulkan_rectangles(&device, &mut dynamic_state_modes)?;
        let mut discard_rectangle_state = self.discard_rectangle_state.to_vulkan(
            &device,
            &mut dynamic_state_modes,
            &discard_rectangles,
        )?;

        // Rasterization state
        let mut rasterization_line_state = self
            .rasterization_state
            .to_vulkan_line_state(&device, &mut dynamic_state_modes)?;
        let rasterization_state = Some(self.rasterization_state.to_vulkan(
            &device,
            &mut dynamic_state_modes,
            rasterization_line_state.as_mut(),
        )?);

        // Fragment shader state
        let has_fragment_shader_state =
            self.rasterization_state.rasterizer_discard_enable != StateMode::Fixed(true);

        // Multisample state
        let multisample_state = if has_fragment_shader_state {
            Some(
                self.multisample_state
                    .to_vulkan(&device, &mut dynamic_state_modes, &subpass)?,
            )
        } else {
            None
        };

        // Depth/stencil state
        let depth_stencil_state = if has_fragment_shader_state
            && subpass.subpass_desc().depth_stencil_attachment.is_some()
        {
            Some(
                self.depth_stencil_state
                    .to_vulkan(&device, &mut dynamic_state_modes, &subpass)?,
            )
        } else {
            None
        };

        // Color blend state
        let (color_blend_attachments, color_write_enables) = self
            .color_blend_state
            .to_vulkan_attachments(&device, &mut dynamic_state_modes, &subpass)?;
        let mut color_write = self.color_blend_state.to_vulkan_color_write(
            &device,
            &mut dynamic_state_modes,
            &color_write_enables,
        )?;
        let color_blend_state = if has_fragment_shader_state {
            Some(self.color_blend_state.to_vulkan(
                &device,
                &mut dynamic_state_modes,
                &color_blend_attachments,
                color_write.as_mut(),
            )?)
        } else {
            None
        };

        // Dynamic state
        let dynamic_state_list: Vec<ash::vk::DynamicState> = dynamic_state_modes
            .iter()
            .filter(|(_, d)| **d)
            .map(|(&state, _)| state.into())
            .collect();

        let dynamic_state = if !dynamic_state_list.is_empty() {
            Some(ash::vk::PipelineDynamicStateCreateInfo {
                flags: ash::vk::PipelineDynamicStateCreateFlags::empty(),
                dynamic_state_count: dynamic_state_list.len() as u32,
                p_dynamic_states: dynamic_state_list.as_ptr(),
                ..Default::default()
            })
        } else {
            None
        };

        // Dynamic states not handled yet:
        // - ViewportWScaling (VkPipelineViewportWScalingStateCreateInfoNV)
        // - DiscardRectangle (VkPipelineDiscardRectangleStateCreateInfoEXT)
        // - SampleLocations (VkPipelineSampleLocationsStateCreateInfoEXT)
        // - ViewportShadingRatePalette (VkPipelineViewportShadingRateImageStateCreateInfoNV)
        // - ViewportCoarseSampleOrder (VkPipelineViewportCoarseSampleOrderStateCreateInfoNV)
        // - ExclusiveScissor (VkPipelineViewportExclusiveScissorStateCreateInfoNV)
        // - FragmentShadingRate (VkPipelineFragmentShadingRateStateCreateInfoKHR)

        // Finally, create the pipeline itself.
        let handle = unsafe {
            let mut create_info = ash::vk::GraphicsPipelineCreateInfo {
                flags: ash::vk::PipelineCreateFlags::empty(), // TODO: some flags are available but none are critical
                stage_count: stages.len() as u32,
                p_stages: stages.as_ptr(),
                p_vertex_input_state: vertex_input_state
                    .as_ref()
                    .map(|p| p as *const _)
                    .unwrap_or(ptr::null()),
                p_input_assembly_state: input_assembly_state
                    .as_ref()
                    .map(|p| p as *const _)
                    .unwrap_or(ptr::null()),
                p_tessellation_state: tessellation_state
                    .as_ref()
                    .map(|p| p as *const _)
                    .unwrap_or(ptr::null()),
                p_viewport_state: viewport_state
                    .as_ref()
                    .map(|p| p as *const _)
                    .unwrap_or(ptr::null()),
                p_rasterization_state: rasterization_state
                    .as_ref()
                    .map(|p| p as *const _)
                    .unwrap_or(ptr::null()),
                p_multisample_state: multisample_state
                    .as_ref()
                    .map(|p| p as *const _)
                    .unwrap_or(ptr::null()),
                p_depth_stencil_state: depth_stencil_state
                    .as_ref()
                    .map(|p| p as *const _)
                    .unwrap_or(ptr::null()),
                p_color_blend_state: color_blend_state
                    .as_ref()
                    .map(|p| p as *const _)
                    .unwrap_or(ptr::null()),
                p_dynamic_state: dynamic_state
                    .as_ref()
                    .map(|s| s as *const _)
                    .unwrap_or(ptr::null()),
                layout: pipeline_layout.internal_object(),
                render_pass: subpass.render_pass().internal_object(),
                subpass: subpass.index(),
                base_pipeline_handle: ash::vk::Pipeline::null(), // TODO:
                base_pipeline_index: -1,                         // TODO:
                ..Default::default()
            };

            if let Some(discard_rectangle_state) = discard_rectangle_state.as_mut() {
                discard_rectangle_state.p_next = create_info.p_next;
                create_info.p_next = discard_rectangle_state as *const _ as *const _;
            }

            let cache_handle = match self.cache.as_ref() {
                Some(cache) => cache.internal_object(),
                None => ash::vk::PipelineCache::null(),
            };

            let mut output = MaybeUninit::uninit();
            check_errors(fns.v1_0.create_graphics_pipelines(
                device.internal_object(),
                cache_handle,
                1,
                &create_info,
                ptr::null(),
                output.as_mut_ptr(),
            ))?;
            output.assume_init()
        };

        // Some drivers return `VK_SUCCESS` but provide a null handle if they
        // fail to create the pipeline (due to invalid shaders, etc)
        // This check ensures that we don't create an invalid `GraphicsPipeline` instance
        if handle == ash::vk::Pipeline::null() {
            panic!("vkCreateGraphicsPipelines provided a NULL handle");
        }

        Ok(Arc::new(GraphicsPipeline {
            handle,
            device: device.clone(),
            layout: pipeline_layout,
            subpass,
            shaders: stages_info
                .iter()
                .map(|stage_info| (ShaderStage::from(*stage_info.entry_point.execution()), ()))
                .collect(),
            descriptor_requirements,
            num_used_descriptor_sets,

            vertex_input_state: self_vertex_input_state, // Can be None if there's a mesh shader, but we don't support that yet
            input_assembly_state: self.input_assembly_state, // Can be None if there's a mesh shader, but we don't support that yet
            tessellation_state: if tessellation_state.is_some() {
                Some(self.tessellation_state)
            } else {
                None
            },
            viewport_state: if viewport_state.is_some() {
                Some(self.viewport_state)
            } else {
                None
            },
            discard_rectangle_state: if discard_rectangle_state.is_some() {
                Some(self.discard_rectangle_state)
            } else {
                None
            },
            rasterization_state: self.rasterization_state,
            multisample_state: if multisample_state.is_some() {
                Some(self.multisample_state)
            } else {
                None
            },
            depth_stencil_state: if depth_stencil_state.is_some() {
                Some(self.depth_stencil_state)
            } else {
                None
            },
            color_blend_state: if color_blend_state.is_some() {
                Some(self.color_blend_state)
            } else {
                None
            },
            dynamic_state: dynamic_state_modes,
        }))
    }

    // TODO: add build_with_cache method
}

impl<'vs, 'tcs, 'tes, 'gs, 'fs, Vdef, Vss, Tcss, Tess, Gss, Fss>
    GraphicsPipelineBuilder<'vs, 'tcs, 'tes, 'gs, 'fs, Vdef, Vss, Tcss, Tess, Gss, Fss>
{
    // TODO: add pipeline derivate system

    /// Sets the vertex shader to use.
    // TODO: correct specialization constants
    #[inline]
    pub fn vertex_shader<'vs2, Vss2>(
        self,
        shader: EntryPoint<'vs2>,
        specialization_constants: Vss2,
    ) -> GraphicsPipelineBuilder<'vs2, 'tcs, 'tes, 'gs, 'fs, Vdef, Vss2, Tcss, Tess, Gss, Fss>
    where
        Vss2: SpecializationConstants,
    {
        GraphicsPipelineBuilder {
            subpass: self.subpass,
            cache: self.cache,

            vertex_shader: Some((shader, specialization_constants)),
            tessellation_shaders: self.tessellation_shaders,
            geometry_shader: self.geometry_shader,
            fragment_shader: self.fragment_shader,

            vertex_input_state: self.vertex_input_state,
            input_assembly_state: self.input_assembly_state,
            tessellation_state: self.tessellation_state,
            viewport_state: self.viewport_state,
            discard_rectangle_state: self.discard_rectangle_state,
            rasterization_state: self.rasterization_state,
            multisample_state: self.multisample_state,
            depth_stencil_state: self.depth_stencil_state,
            color_blend_state: self.color_blend_state,
        }
    }

    /// Sets the tessellation shaders to use.
    // TODO: correct specialization constants
    #[inline]
    pub fn tessellation_shaders<'tcs2, 'tes2, Tcss2, Tess2>(
        self,
        control_shader: EntryPoint<'tcs2>,
        control_specialization_constants: Tcss2,
        evaluation_shader: EntryPoint<'tes2>,
        evaluation_specialization_constants: Tess2,
    ) -> GraphicsPipelineBuilder<'vs, 'tcs2, 'tes2, 'gs, 'fs, Vdef, Vss, Tcss2, Tess2, Gss, Fss>
    where
        Tcss2: SpecializationConstants,
        Tess2: SpecializationConstants,
    {
        GraphicsPipelineBuilder {
            subpass: self.subpass,
            cache: self.cache,

            vertex_shader: self.vertex_shader,
            tessellation_shaders: Some(TessellationShaders {
                control: (control_shader, control_specialization_constants),
                evaluation: (evaluation_shader, evaluation_specialization_constants),
            }),
            geometry_shader: self.geometry_shader,
            fragment_shader: self.fragment_shader,

            vertex_input_state: self.vertex_input_state,
            input_assembly_state: self.input_assembly_state,
            tessellation_state: self.tessellation_state,
            viewport_state: self.viewport_state,
            discard_rectangle_state: self.discard_rectangle_state,
            rasterization_state: self.rasterization_state,
            multisample_state: self.multisample_state,
            depth_stencil_state: self.depth_stencil_state,
            color_blend_state: self.color_blend_state,
        }
    }

    /// Sets the geometry shader to use.
    // TODO: correct specialization constants
    #[inline]
    pub fn geometry_shader<'gs2, Gss2>(
        self,
        shader: EntryPoint<'gs2>,
        specialization_constants: Gss2,
    ) -> GraphicsPipelineBuilder<'vs, 'tcs, 'tes, 'gs2, 'fs, Vdef, Vss, Tcss, Tess, Gss2, Fss>
    where
        Gss2: SpecializationConstants,
    {
        GraphicsPipelineBuilder {
            subpass: self.subpass,
            cache: self.cache,

            vertex_shader: self.vertex_shader,
            tessellation_shaders: self.tessellation_shaders,
            geometry_shader: Some((shader, specialization_constants)),
            fragment_shader: self.fragment_shader,

            vertex_input_state: self.vertex_input_state,
            input_assembly_state: self.input_assembly_state,
            tessellation_state: self.tessellation_state,
            viewport_state: self.viewport_state,
            discard_rectangle_state: self.discard_rectangle_state,
            rasterization_state: self.rasterization_state,
            multisample_state: self.multisample_state,
            depth_stencil_state: self.depth_stencil_state,
            color_blend_state: self.color_blend_state,
        }
    }

    /// Sets the fragment shader to use.
    ///
    /// The fragment shader is run once for each pixel that is covered by each primitive.
    // TODO: correct specialization constants
    #[inline]
    pub fn fragment_shader<'fs2, Fss2>(
        self,
        shader: EntryPoint<'fs2>,
        specialization_constants: Fss2,
    ) -> GraphicsPipelineBuilder<'vs, 'tcs, 'tes, 'gs, 'fs2, Vdef, Vss, Tcss, Tess, Gss, Fss2>
    where
        Fss2: SpecializationConstants,
    {
        GraphicsPipelineBuilder {
            subpass: self.subpass,
            cache: self.cache,

            vertex_shader: self.vertex_shader,
            tessellation_shaders: self.tessellation_shaders,
            geometry_shader: self.geometry_shader,
            fragment_shader: Some((shader, specialization_constants)),

            vertex_input_state: self.vertex_input_state,
            input_assembly_state: self.input_assembly_state,
            tessellation_state: self.tessellation_state,
            viewport_state: self.viewport_state,
            discard_rectangle_state: self.discard_rectangle_state,
            rasterization_state: self.rasterization_state,
            multisample_state: self.multisample_state,
            depth_stencil_state: self.depth_stencil_state,
            color_blend_state: self.color_blend_state,
        }
    }

    /// Sets the vertex input state.
    ///
    /// The default value is [`VertexInputState::default()`].
    #[inline]
    pub fn vertex_input_state<T>(
        self,
        vertex_input_state: T,
    ) -> GraphicsPipelineBuilder<'vs, 'tcs, 'tes, 'gs, 'fs, T, Vss, Tcss, Tess, Gss, Fss>
    where
        T: VertexDefinition,
    {
        GraphicsPipelineBuilder {
            subpass: self.subpass,
            cache: self.cache,

            vertex_shader: self.vertex_shader,
            tessellation_shaders: self.tessellation_shaders,
            geometry_shader: self.geometry_shader,
            fragment_shader: self.fragment_shader,

            vertex_input_state,
            input_assembly_state: self.input_assembly_state,
            tessellation_state: self.tessellation_state,
            viewport_state: self.viewport_state,
            discard_rectangle_state: self.discard_rectangle_state,
            rasterization_state: self.rasterization_state,
            multisample_state: self.multisample_state,
            depth_stencil_state: self.depth_stencil_state,
            color_blend_state: self.color_blend_state,
        }
    }

    /// Sets the input assembly state.
    ///
    /// The default value is [`InputAssemblyState::default()`].
    #[inline]
    pub fn input_assembly_state(mut self, input_assembly_state: InputAssemblyState) -> Self {
        self.input_assembly_state = input_assembly_state;
        self
    }

    /// Sets the tessellation state. This is required if the pipeline contains tessellation shaders,
    /// and ignored otherwise.
    ///
    /// The default value is [`TessellationState::default()`].
    #[inline]
    pub fn tessellation_state(mut self, tessellation_state: TessellationState) -> Self {
        self.tessellation_state = tessellation_state;
        self
    }

    /// Sets the viewport state.
    ///
    /// The default value is [`ViewportState::default()`].
    #[inline]
    pub fn viewport_state(mut self, viewport_state: ViewportState) -> Self {
        self.viewport_state = viewport_state;
        self
    }

    /// Sets the discard rectangle state.
    ///
    /// The default value is [`DiscardRectangleState::default()`].
    #[inline]
    pub fn discard_rectangle_state(
        mut self,
        discard_rectangle_state: DiscardRectangleState,
    ) -> Self {
        self.discard_rectangle_state = discard_rectangle_state;
        self
    }

    /// Sets the rasterization state.
    ///
    /// The default value is [`RasterizationState::default()`].
    #[inline]
    pub fn rasterization_state(mut self, rasterization_state: RasterizationState) -> Self {
        self.rasterization_state = rasterization_state;
        self
    }

    /// Sets the multisample state.
    ///
    /// The default value is [`MultisampleState::default()`].
    #[inline]
    pub fn multisample_state(mut self, multisample_state: MultisampleState) -> Self {
        self.multisample_state = multisample_state;
        self
    }

    /// Sets the depth/stencil state.
    ///
    /// The default value is [`DepthStencilState::default()`].
    #[inline]
    pub fn depth_stencil_state(mut self, depth_stencil_state: DepthStencilState) -> Self {
        self.depth_stencil_state = depth_stencil_state;
        self
    }

    /// Sets the color blend state.
    ///
    /// The default value is [`ColorBlendState::default()`].
    #[inline]
    pub fn color_blend_state(mut self, color_blend_state: ColorBlendState) -> Self {
        self.color_blend_state = color_blend_state;
        self
    }

    /// Sets the tessellation shaders stage as disabled. This is the default.
    #[deprecated(since = "0.27")]
    #[inline]
    pub fn tessellation_shaders_disabled(mut self) -> Self {
        self.tessellation_shaders = None;
        self
    }

    /// Sets the geometry shader stage as disabled. This is the default.
    #[deprecated(since = "0.27")]
    #[inline]
    pub fn geometry_shader_disabled(mut self) -> Self {
        self.geometry_shader = None;
        self
    }

    /// Sets the vertex input to a single vertex buffer.
    ///
    /// You will most likely need to explicitly specify the template parameter to the type of a
    /// vertex.
    #[deprecated(since = "0.27", note = "Use `vertex_input_state` instead")]
    #[inline]
    pub fn vertex_input_single_buffer<V: Vertex>(
        self,
    ) -> GraphicsPipelineBuilder<
        'vs,
        'tcs,
        'tes,
        'gs,
        'fs,
        BuffersDefinition,
        Vss,
        Tcss,
        Tess,
        Gss,
        Fss,
    > {
        self.vertex_input_state(BuffersDefinition::new().vertex::<V>())
    }

    /// Sets whether primitive restart is enabled.
    #[deprecated(since = "0.27", note = "Use `input_assembly_state` instead")]
    #[inline]
    pub fn primitive_restart(mut self, enabled: bool) -> Self {
        self.input_assembly_state.primitive_restart_enable = StateMode::Fixed(enabled);
        self
    }

    /// Sets the topology of the primitives that are expected by the pipeline.
    #[deprecated(since = "0.27", note = "Use `input_assembly_state` instead")]
    #[inline]
    pub fn primitive_topology(mut self, topology: PrimitiveTopology) -> Self {
        self.input_assembly_state.topology = PartialStateMode::Fixed(topology);
        self
    }

    /// Sets the topology of the primitives to a list of points.
    ///
    /// > **Note**: This is equivalent to
    /// > `self.primitive_topology(PrimitiveTopology::PointList)`.
    #[deprecated(since = "0.27", note = "Use `input_assembly_state` instead")]
    #[inline]
    pub fn point_list(self) -> Self {
        self.primitive_topology(PrimitiveTopology::PointList)
    }

    /// Sets the topology of the primitives to a list of lines.
    ///
    /// > **Note**: This is equivalent to
    /// > `self.primitive_topology(PrimitiveTopology::LineList)`.
    #[deprecated(since = "0.27", note = "Use `input_assembly_state` instead")]
    #[inline]
    pub fn line_list(self) -> Self {
        self.primitive_topology(PrimitiveTopology::LineList)
    }

    /// Sets the topology of the primitives to a line strip.
    ///
    /// > **Note**: This is equivalent to
    /// > `self.primitive_topology(PrimitiveTopology::LineStrip)`.
    #[deprecated(since = "0.27", note = "Use `input_assembly_state` instead")]
    #[inline]
    pub fn line_strip(self) -> Self {
        self.primitive_topology(PrimitiveTopology::LineStrip)
    }

    /// Sets the topology of the primitives to a list of triangles. Note that this is the default.
    ///
    /// > **Note**: This is equivalent to
    /// > `self.primitive_topology(PrimitiveTopology::TriangleList)`.
    #[deprecated(since = "0.27", note = "Use `input_assembly_state` instead")]
    #[inline]
    pub fn triangle_list(self) -> Self {
        self.primitive_topology(PrimitiveTopology::TriangleList)
    }

    /// Sets the topology of the primitives to a triangle strip.
    ///
    /// > **Note**: This is equivalent to
    /// > `self.primitive_topology(PrimitiveTopology::TriangleStrip)`.
    #[deprecated(since = "0.27", note = "Use `input_assembly_state` instead")]
    #[inline]
    pub fn triangle_strip(self) -> Self {
        self.primitive_topology(PrimitiveTopology::TriangleStrip)
    }

    /// Sets the topology of the primitives to a fan of triangles.
    ///
    /// > **Note**: This is equivalent to
    /// > `self.primitive_topology(PrimitiveTopology::TriangleFan)`.
    #[deprecated(since = "0.27", note = "Use `input_assembly_state` instead")]
    #[inline]
    pub fn triangle_fan(self) -> Self {
        self.primitive_topology(PrimitiveTopology::TriangleFan)
    }

    /// Sets the topology of the primitives to a list of lines with adjacency information.
    ///
    /// > **Note**: This is equivalent to
    /// > `self.primitive_topology(PrimitiveTopology::LineListWithAdjacency)`.
    #[deprecated(since = "0.27", note = "Use `input_assembly_state` instead")]
    #[inline]
    pub fn line_list_with_adjacency(self) -> Self {
        self.primitive_topology(PrimitiveTopology::LineListWithAdjacency)
    }

    /// Sets the topology of the primitives to a line strip with adjacency information.
    ///
    /// > **Note**: This is equivalent to
    /// > `self.primitive_topology(PrimitiveTopology::LineStripWithAdjacency)`.
    #[deprecated(since = "0.27", note = "Use `input_assembly_state` instead")]
    #[inline]
    pub fn line_strip_with_adjacency(self) -> Self {
        self.primitive_topology(PrimitiveTopology::LineStripWithAdjacency)
    }

    /// Sets the topology of the primitives to a list of triangles with adjacency information.
    ///
    /// > **Note**: This is equivalent to
    /// > `self.primitive_topology(PrimitiveTopology::TriangleListWithAdjacency)`.
    #[deprecated(since = "0.27", note = "Use `input_assembly_state` instead")]
    #[inline]
    pub fn triangle_list_with_adjacency(self) -> Self {
        self.primitive_topology(PrimitiveTopology::TriangleListWithAdjacency)
    }

    /// Sets the topology of the primitives to a triangle strip with adjacency information`
    ///
    /// > **Note**: This is equivalent to
    /// > `self.primitive_topology(PrimitiveTopology::TriangleStripWithAdjacency)`.
    #[deprecated(since = "0.27", note = "Use `input_assembly_state` instead")]
    #[inline]
    pub fn triangle_strip_with_adjacency(self) -> Self {
        self.primitive_topology(PrimitiveTopology::TriangleStripWithAdjacency)
    }

    /// Sets the topology of the primitives to a list of patches. Can only be used and must be used
    /// with a tessellation shader.
    ///
    /// > **Note**: This is equivalent to
    /// > `self.primitive_topology(PrimitiveTopology::PatchList)`.
    #[deprecated(since = "0.27", note = "Use `input_assembly_state` instead")]
    #[inline]
    pub fn patch_list(self) -> Self {
        self.primitive_topology(PrimitiveTopology::PatchList)
    }

    /// Sets the viewports to some value, and the scissor boxes to boxes that always cover the
    /// whole viewport.
    #[deprecated(since = "0.27", note = "Use `viewport_state` instead")]
    #[inline]
    pub fn viewports<I>(self, viewports: I) -> Self
    where
        I: IntoIterator<Item = Viewport>,
    {
        self.viewports_scissors(viewports.into_iter().map(|v| (v, Scissor::irrelevant())))
    }

    /// Sets the characteristics of viewports and scissor boxes in advance.
    #[deprecated(since = "0.27", note = "Use `viewport_state` instead")]
    #[inline]
    pub fn viewports_scissors<I>(mut self, viewports: I) -> Self
    where
        I: IntoIterator<Item = (Viewport, Scissor)>,
    {
        self.viewport_state = ViewportState::Fixed {
            data: viewports.into_iter().collect(),
        };
        self
    }

    /// Sets the scissor boxes to some values, and viewports to dynamic. The viewports will
    /// need to be set before drawing.
    #[deprecated(since = "0.27", note = "Use `viewport_state` instead")]
    #[inline]
    pub fn viewports_dynamic_scissors_fixed<I>(mut self, scissors: I) -> Self
    where
        I: IntoIterator<Item = Scissor>,
    {
        self.viewport_state = ViewportState::FixedScissor {
            scissors: scissors.into_iter().collect(),
            viewport_count_dynamic: false,
        };
        self
    }

    /// Sets the viewports to dynamic, and the scissor boxes to boxes that always cover the whole
    /// viewport. The viewports will need to be set before drawing.
    #[deprecated(since = "0.27", note = "Use `viewport_state` instead")]
    #[inline]
    pub fn viewports_dynamic_scissors_irrelevant(mut self, num: u32) -> Self {
        self.viewport_state = ViewportState::FixedScissor {
            scissors: (0..num).map(|_| Scissor::irrelevant()).collect(),
            viewport_count_dynamic: false,
        };
        self
    }

    /// Sets the viewports to some values, and scissor boxes to dynamic. The scissor boxes will
    /// need to be set before drawing.
    #[deprecated(since = "0.27", note = "Use `viewport_state` instead")]
    #[inline]
    pub fn viewports_fixed_scissors_dynamic<I>(mut self, viewports: I) -> Self
    where
        I: IntoIterator<Item = Viewport>,
    {
        self.viewport_state = ViewportState::FixedViewport {
            viewports: viewports.into_iter().collect(),
            scissor_count_dynamic: false,
        };
        self
    }

    /// Sets the viewports and scissor boxes to dynamic. They will both need to be set before
    /// drawing.
    #[deprecated(since = "0.27", note = "Use `viewport_state` instead")]
    #[inline]
    pub fn viewports_scissors_dynamic(mut self, count: u32) -> Self {
        self.viewport_state = ViewportState::Dynamic {
            count,
            viewport_count_dynamic: false,
            scissor_count_dynamic: false,
        };
        self
    }

    /// If true, then the depth value of the vertices will be clamped to the range `[0.0 ; 1.0]`.
    /// If false, fragments whose depth is outside of this range will be discarded before the
    /// fragment shader even runs.
    #[deprecated(since = "0.27", note = "Use `rasterization_state` instead")]
    #[inline]
    pub fn depth_clamp(mut self, clamp: bool) -> Self {
        self.rasterization_state.depth_clamp_enable = clamp;
        self
    }

    /// Sets the front-facing faces to counter-clockwise faces. This is the default.
    ///
    /// Triangles whose vertices are oriented counter-clockwise on the screen will be considered
    /// as facing their front. Otherwise they will be considered as facing their back.
    #[deprecated(since = "0.27", note = "Use `rasterization_state` instead")]
    #[inline]
    pub fn front_face_counter_clockwise(mut self) -> Self {
        self.rasterization_state.front_face = StateMode::Fixed(FrontFace::CounterClockwise);
        self
    }

    /// Sets the front-facing faces to clockwise faces.
    ///
    /// Triangles whose vertices are oriented clockwise on the screen will be considered
    /// as facing their front. Otherwise they will be considered as facing their back.
    #[deprecated(since = "0.27", note = "Use `rasterization_state` instead")]
    #[inline]
    pub fn front_face_clockwise(mut self) -> Self {
        self.rasterization_state.front_face = StateMode::Fixed(FrontFace::Clockwise);
        self
    }

    /// Sets backface culling as disabled. This is the default.
    #[deprecated(since = "0.27", note = "Use `rasterization_state` instead")]
    #[inline]
    pub fn cull_mode_disabled(mut self) -> Self {
        self.rasterization_state.cull_mode = StateMode::Fixed(CullMode::None);
        self
    }

    /// Sets backface culling to front faces. The front faces (as chosen with the `front_face_*`
    /// methods) will be discarded by the GPU when drawing.
    #[deprecated(since = "0.27", note = "Use `rasterization_state` instead")]
    #[inline]
    pub fn cull_mode_front(mut self) -> Self {
        self.rasterization_state.cull_mode = StateMode::Fixed(CullMode::Front);
        self
    }

    /// Sets backface culling to back faces. Faces that are not facing the front (as chosen with
    /// the `front_face_*` methods) will be discarded by the GPU when drawing.
    #[deprecated(since = "0.27", note = "Use `rasterization_state` instead")]
    #[inline]
    pub fn cull_mode_back(mut self) -> Self {
        self.rasterization_state.cull_mode = StateMode::Fixed(CullMode::Back);
        self
    }

    /// Sets backface culling to both front and back faces. All the faces will be discarded.
    ///
    /// > **Note**: This option exists for the sake of completeness. It has no known practical
    /// > usage.
    #[deprecated(since = "0.27", note = "Use `rasterization_state` instead")]
    #[inline]
    pub fn cull_mode_front_and_back(mut self) -> Self {
        self.rasterization_state.cull_mode = StateMode::Fixed(CullMode::FrontAndBack);
        self
    }

    /// Sets the polygon mode to "fill". This is the default.
    #[deprecated(since = "0.27", note = "Use `rasterization_state` instead")]
    #[inline]
    pub fn polygon_mode_fill(mut self) -> Self {
        self.rasterization_state.polygon_mode = PolygonMode::Fill;
        self
    }

    /// Sets the polygon mode to "line". Triangles will each be turned into three lines.
    #[deprecated(since = "0.27", note = "Use `rasterization_state` instead")]
    #[inline]
    pub fn polygon_mode_line(mut self) -> Self {
        self.rasterization_state.polygon_mode = PolygonMode::Line;
        self
    }

    /// Sets the polygon mode to "point". Triangles and lines will each be turned into three points.
    #[deprecated(since = "0.27", note = "Use `rasterization_state` instead")]
    #[inline]
    pub fn polygon_mode_point(mut self) -> Self {
        self.rasterization_state.polygon_mode = PolygonMode::Point;
        self
    }

    /// Sets the width of the lines, if the GPU needs to draw lines. The default is `1.0`.
    #[deprecated(since = "0.27", note = "Use `rasterization_state` instead")]
    #[inline]
    pub fn line_width(mut self, value: f32) -> Self {
        self.rasterization_state.line_width = StateMode::Fixed(value);
        self
    }

    /// Sets the width of the lines as dynamic, which means that you will need to set this value
    /// when drawing.
    #[deprecated(since = "0.27", note = "Use `rasterization_state` instead")]
    #[inline]
    pub fn line_width_dynamic(mut self) -> Self {
        self.rasterization_state.line_width = StateMode::Dynamic;
        self
    }

    /// Disables sample shading. The fragment shader will only be run once per fragment (ie. per
    /// pixel) and not once by sample. The output will then be copied in all of the covered
    /// samples.
    ///
    /// Sample shading is disabled by default.
    #[deprecated(since = "0.27", note = "Use `multisample_state` instead")]
    #[inline]
    pub fn sample_shading_disabled(mut self) -> Self {
        self.multisample_state.sample_shading = None;
        self
    }

    /// Enables sample shading. The fragment shader will be run once per sample at the borders of
    /// the object you're drawing.
    ///
    /// Enabling sampling shading requires the `sample_rate_shading` feature to be enabled on the
    /// device.
    ///
    /// The `min_fract` parameter is the minimum fraction of samples shading. For example if its
    /// value is 0.5, then the fragment shader will run for at least half of the samples. The other
    /// half of the samples will get their values determined automatically.
    ///
    /// Sample shading is disabled by default.
    ///
    /// # Panic
    ///
    /// - Panics if `min_fract` is not between 0.0 and 1.0.
    ///
    #[deprecated(since = "0.27", note = "Use `multisample_state` instead")]
    #[inline]
    pub fn sample_shading_enabled(mut self, min_fract: f32) -> Self {
        assert!(min_fract >= 0.0 && min_fract <= 1.0);
        self.multisample_state.sample_shading = Some(min_fract);
        self
    }

    // TODO: doc
    #[deprecated(since = "0.27", note = "Use `multisample_state` instead")]
    pub fn alpha_to_coverage_disabled(mut self) -> Self {
        self.multisample_state.alpha_to_coverage_enable = false;
        self
    }

    // TODO: doc
    #[deprecated(since = "0.27", note = "Use `multisample_state` instead")]
    pub fn alpha_to_coverage_enabled(mut self) -> Self {
        self.multisample_state.alpha_to_coverage_enable = true;
        self
    }

    /// Disables alpha-to-one.
    ///
    /// Alpha-to-one is disabled by default.
    #[deprecated(since = "0.27", note = "Use `multisample_state` instead")]
    #[inline]
    pub fn alpha_to_one_disabled(mut self) -> Self {
        self.multisample_state.alpha_to_one_enable = false;
        self
    }

    /// Enables alpha-to-one. The alpha component of the first color output of the fragment shader
    /// will be replaced by the value `1.0`.
    ///
    /// Enabling alpha-to-one requires the `alpha_to_one` feature to be enabled on the device.
    ///
    /// Alpha-to-one is disabled by default.
    #[deprecated(since = "0.27", note = "Use `multisample_state` instead")]
    #[inline]
    pub fn alpha_to_one_enabled(mut self) -> Self {
        self.multisample_state.alpha_to_one_enable = true;
        self
    }

    /// Sets the depth/stencil state.
    #[deprecated(since = "0.27", note = "Use `depth_stencil_state` instead")]
    #[inline]
    pub fn depth_stencil(self, depth_stencil_state: DepthStencilState) -> Self {
        self.depth_stencil_state(depth_stencil_state)
    }

    /// Sets the depth/stencil tests as disabled.
    ///
    /// > **Note**: This is a shortcut for all the other `depth_*` and `depth_stencil_*` methods
    /// > of the builder.
    #[deprecated(since = "0.27", note = "Use `depth_stencil_state` instead")]
    #[inline]
    pub fn depth_stencil_disabled(mut self) -> Self {
        self.depth_stencil_state = DepthStencilState::disabled();
        self
    }

    /// Sets the depth/stencil tests as a simple depth test and no stencil test.
    ///
    /// > **Note**: This is a shortcut for setting the depth test to `Less`, the depth write Into
    /// > ` true` and disable the stencil test.
    #[deprecated(since = "0.27", note = "Use `depth_stencil_state` instead")]
    #[inline]
    pub fn depth_stencil_simple_depth(mut self) -> Self {
        self.depth_stencil_state = DepthStencilState::simple_depth_test();
        self
    }

    /// Sets whether the depth buffer will be written.
    #[deprecated(since = "0.27", note = "Use `depth_stencil_state` instead")]
    #[inline]
    pub fn depth_write(mut self, write: bool) -> Self {
        let depth_state = self
            .depth_stencil_state
            .depth
            .get_or_insert(Default::default());
        depth_state.write_enable = StateMode::Fixed(write);
        self
    }

    #[deprecated(since = "0.27", note = "Use `color_blend_state` instead")]
    // TODO: When this method is removed, also remove the special casing in `with_pipeline_layout`
    // for self.color_blend_state.attachments.len() == 1
    #[inline]
    pub fn blend_collective(mut self, blend: AttachmentBlend) -> Self {
        self.color_blend_state.attachments = vec![ColorBlendAttachmentState {
            blend: Some(blend),
            color_write_mask: ColorComponents::all(),
            color_write_enable: StateMode::Fixed(true),
        }];
        self
    }

    #[deprecated(since = "0.27", note = "Use `color_blend_state` instead")]
    #[inline]
    pub fn blend_individual<I>(mut self, blend: I) -> Self
    where
        I: IntoIterator<Item = AttachmentBlend>,
    {
        self.color_blend_state.attachments = blend
            .into_iter()
            .map(|x| ColorBlendAttachmentState {
                blend: Some(x),
                color_write_mask: ColorComponents::all(),
                color_write_enable: StateMode::Fixed(true),
            })
            .collect();
        self
    }

    /// Each fragment shader output will have its value directly written to the framebuffer
    /// attachment. This is the default.
    #[deprecated(since = "0.27", note = "Use `color_blend_state` instead")]
    // TODO: When this method is removed, also remove the special casing in `with_pipeline_layout`
    // for self.color_blend_state.attachments.len() == 1
    #[inline]
    pub fn blend_pass_through(mut self) -> Self {
        self.color_blend_state.attachments = vec![ColorBlendAttachmentState {
            blend: None,
            color_write_mask: ColorComponents::all(),
            color_write_enable: StateMode::Fixed(true),
        }];
        self
    }

    #[deprecated(since = "0.27", note = "Use `color_blend_state` instead")]
    // TODO: When this method is removed, also remove the special casing in `with_pipeline_layout`
    // for self.color_blend_state.attachments.len() == 1
    #[inline]
    pub fn blend_alpha_blending(mut self) -> Self {
        self.color_blend_state.attachments = vec![ColorBlendAttachmentState {
            blend: Some(AttachmentBlend::alpha()),
            color_write_mask: ColorComponents::all(),
            color_write_enable: StateMode::Fixed(true),
        }];
        self
    }

    #[deprecated(since = "0.27", note = "Use `color_blend_state` instead")]
    #[inline]
    pub fn blend_logic_op(mut self, logic_op: LogicOp) -> Self {
        self.color_blend_state.logic_op = Some(StateMode::Fixed(logic_op));
        self
    }

    /// Sets the logic operation as disabled. This is the default.
    #[deprecated(since = "0.27", note = "Use `color_blend_state` instead")]
    #[inline]
    pub fn blend_logic_op_disabled(mut self) -> Self {
        self.color_blend_state.logic_op = None;
        self
    }

    /// Sets the blend constant. The default is `[0.0, 0.0, 0.0, 0.0]`.
    ///
    /// The blend constant is used for some blending calculations. It is irrelevant otherwise.
    #[deprecated(since = "0.27", note = "Use `color_blend_state` instead")]
    #[inline]
    pub fn blend_constants(mut self, constants: [f32; 4]) -> Self {
        self.color_blend_state.blend_constants = StateMode::Fixed(constants);
        self
    }

    /// Sets the blend constant value as dynamic. Its value will need to be set before drawing.
    ///
    /// The blend constant is used for some blending calculations. It is irrelevant otherwise.
    #[deprecated(since = "0.27", note = "Use `color_blend_state` instead")]
    #[inline]
    pub fn blend_constants_dynamic(mut self) -> Self {
        self.color_blend_state.blend_constants = StateMode::Dynamic;
        self
    }

    /// Sets the render pass subpass to use.
    #[inline]
    pub fn render_pass(self, subpass: Subpass) -> Self {
        GraphicsPipelineBuilder {
            subpass: Some(subpass),
            cache: self.cache,

            vertex_shader: self.vertex_shader,
            tessellation_shaders: self.tessellation_shaders,
            geometry_shader: self.geometry_shader,
            fragment_shader: self.fragment_shader,

            vertex_input_state: self.vertex_input_state,
            input_assembly_state: self.input_assembly_state,
            tessellation_state: self.tessellation_state,
            viewport_state: self.viewport_state,
            rasterization_state: self.rasterization_state,
            multisample_state: self.multisample_state,
            depth_stencil_state: self.depth_stencil_state,
            color_blend_state: self.color_blend_state,

            discard_rectangle_state: self.discard_rectangle_state,
        }
    }

    /// Enable caching of this pipeline via a PipelineCache object.
    ///
    /// If this pipeline already exists in the cache it will be used, if this is a new
    /// pipeline it will be inserted into the cache. The implementation handles the
    /// PipelineCache.
    #[inline]
    pub fn build_with_cache(mut self, pipeline_cache: Arc<PipelineCache>) -> Self {
        self.cache = Some(pipeline_cache);
        self
    }
}

impl<'vs, 'tcs, 'tes, 'gs, 'fs, Vdef, Vss, Tcss, Tess, Gss, Fss> Clone
    for GraphicsPipelineBuilder<'vs, 'tcs, 'tes, 'gs, 'fs, Vdef, Vss, Tcss, Tess, Gss, Fss>
where
    Vdef: Clone,
    Vss: Clone,
    Tcss: Clone,
    Tess: Clone,
    Gss: Clone,
    Fss: Clone,
{
    fn clone(&self) -> Self {
        GraphicsPipelineBuilder {
            subpass: self.subpass.clone(),
            cache: self.cache.clone(),

            vertex_shader: self.vertex_shader.clone(),
            tessellation_shaders: self.tessellation_shaders.clone(),
            geometry_shader: self.geometry_shader.clone(),
            fragment_shader: self.fragment_shader.clone(),

            vertex_input_state: self.vertex_input_state.clone(),
            input_assembly_state: self.input_assembly_state,
            tessellation_state: self.tessellation_state,
            viewport_state: self.viewport_state.clone(),
            rasterization_state: self.rasterization_state.clone(),
            multisample_state: self.multisample_state,
            depth_stencil_state: self.depth_stencil_state.clone(),
            color_blend_state: self.color_blend_state.clone(),

            discard_rectangle_state: self.discard_rectangle_state.clone(),
        }
    }
}