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
// Copyright (c) 2016 The vulkano developers
// Licensed under the Apache License, Version 2.0
// <LICENSE-APACHE or
// http://www.apache.org/licenses/LICENSE-2.0> or the MIT
// license <LICENSE-MIT or http://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.

//! Lowest-level interface for command buffers creation.
//! 
//! This module provides the structs necessary to create command buffers. The main purpose of this
//! module is to provide a nice API, and almost no safety checks are performed except through
//! `debug_assert!`.
//! 
//! # Safety
//! 
//! Each individual function is documented and indicates what exactly must be done in order to be
//! safe.
//! 
//! Things to generally look for when you use an `UnsafeCommandBuffer` are:
//! 
//! - The objects must be kept alive for at least as long as the command buffer.
//! - The objects must be properly synchronized and transitionned with pipeline barriers.
//! - The capabilities of the queue must be checked (for example running a draw operation on a
//!   transfer-only queue won't work).
//! - Some commands can only be called from inside or outside of a render pass.
//!

use std::cmp;
use std::mem;
use std::ops::Range;
use std::ptr;
use std::sync::atomic::AtomicBool;
use std::sync::Arc;
use std::u32;
use smallvec::SmallVec;

use buffer::Buffer;
use buffer::BufferSlice;
use buffer::sys::UnsafeBuffer;
use buffer::traits::PipelineBarrierRequest as BufferPipelineBarrierRequest;
use command_buffer::pool::AllocatedCommandBuffer;
use command_buffer::pool::CommandPool;
use command_buffer::pool::CommandPoolFinished;
use descriptor::pipeline_layout::PipelineLayout;
use descriptor::descriptor_set::UnsafeDescriptorSet;
use descriptor::descriptor::ShaderStages;
use device::Device;
use format::ClearValue;
use format::FormatTy;
use framebuffer::RenderPass;
use framebuffer::Subpass;
use framebuffer::UnsafeRenderPass;
use framebuffer::traits::Framebuffer;
use image::Image;
use image::sys::Layout;
use image::sys::UnsafeImage;
use image::traits::PipelineBarrierRequest as ImagePipelineBarrierRequest;
use pipeline::ComputePipeline;
use pipeline::GraphicsPipeline;
use pipeline::input_assembly::IndexType;
use sync::AccessFlagBits;
use sync::PipelineStages;

use OomError;
use VulkanObject;
use VulkanPointers;
use check_errors;
use vk;

pub struct UnsafeCommandBufferBuilder<P> where P: CommandPool {
    cmd: Option<vk::CommandBuffer>,
    pool: Option<P>,
    device: Arc<Device>,

    // Flags that were used at creation.
    flags: Flags,

    // True if we are a secondary command buffer.
    secondary_cb: bool,

    // True if we are within a render pass.
    within_render_pass: bool,
}

impl<P> UnsafeCommandBufferBuilder<P> where P: CommandPool {
    /// Creates a new builder.
    pub fn new<R, F>(pool: P, kind: Kind<R, F>, flags: Flags)
                     -> Result<UnsafeCommandBufferBuilder<P>, OomError>
        where R: RenderPass, F: Framebuffer
    {
        let secondary = match kind {
            Kind::Primary => false,
            Kind::Secondary | Kind::SecondaryRenderPass { .. } => true,
        };

        let cmd = try!(pool.alloc(secondary, 1)).next().unwrap();
        
        match unsafe { UnsafeCommandBufferBuilder::already_allocated(pool, cmd, kind, flags) } {
            Ok(cmd) => Ok(cmd),
            Err(err) => {
                // FIXME: uncomment this and solve the fact that `pool` has been moved
                //unsafe { pool.free(secondary, Some(cmd.into()).into_iter()) };
                Err(err)
            },
        }
    }

    /// Creates a new command buffer builder from an already-allocated command buffer.
    ///
    /// # Safety
    ///
    /// - The allocated command buffer must belong to the pool and must not be used anywhere else
    ///   in the code for the duration of this command buffer.
    ///
    pub unsafe fn already_allocated<R, F>(pool: P, cmd: AllocatedCommandBuffer,
                                          kind: Kind<R, F>, flags: Flags)
                                          -> Result<UnsafeCommandBufferBuilder<P>, OomError>
        where R: RenderPass, F: Framebuffer
    {
        let device = pool.device().clone();
        let vk = device.pointers();
        let cmd = cmd.internal_object();

        let vk_flags = {
            let a = match flags {
                Flags::None => 0,
                Flags::SimultaneousUse => vk::COMMAND_BUFFER_USAGE_SIMULTANEOUS_USE_BIT,
                Flags::OneTimeSubmit => vk::COMMAND_BUFFER_USAGE_ONE_TIME_SUBMIT_BIT,
            };

            let b = match kind {
                Kind::Primary | Kind::Secondary => 0,
                Kind::SecondaryRenderPass { .. } => {
                    vk::COMMAND_BUFFER_USAGE_RENDER_PASS_CONTINUE_BIT
                },
            };

            a | b
        };

        let (rp, sp) = if let Kind::SecondaryRenderPass { subpass, .. } = kind {
            (subpass.render_pass().inner().internal_object(), subpass.index())
        } else {
            (0, 0)
        };

        let framebuffer = if let Kind::SecondaryRenderPass { subpass, framebuffer: Some(ref framebuffer) } = kind {
            // TODO: restore check
            //assert!(framebuffer.is_compatible_with(subpass.render_pass()));     // TODO: proper error
            framebuffer.internal_object()
        } else {
            0
        };

        let inheritance = vk::CommandBufferInheritanceInfo {
            sType: vk::STRUCTURE_TYPE_COMMAND_BUFFER_INHERITANCE_INFO,
            pNext: ptr::null(),
            renderPass: rp,
            subpass: sp,
            framebuffer: framebuffer,
            occlusionQueryEnable: 0,            // TODO:
            queryFlags: 0,          // TODO:
            pipelineStatistics: 0,          // TODO:
        };

        let infos = vk::CommandBufferBeginInfo {
            sType: vk::STRUCTURE_TYPE_COMMAND_BUFFER_BEGIN_INFO,
            pNext: ptr::null(),
            flags: vk_flags,
            pInheritanceInfo: &inheritance,
        };

        try!(check_errors(vk.BeginCommandBuffer(cmd, &infos)));

        Ok(UnsafeCommandBufferBuilder {
            device: device.clone(),
            pool: Some(pool),
            cmd: Some(cmd),
            flags: flags,
            secondary_cb: match kind {
                Kind::Primary => false,
                Kind::Secondary | Kind::SecondaryRenderPass { .. } => true,
            },
            within_render_pass: match kind {
                Kind::Primary | Kind::Secondary => false,
                Kind::SecondaryRenderPass { .. } => true,
            },
        })
    }

    /// Finishes building the command buffer.
    pub fn build(mut self) -> Result<UnsafeCommandBuffer<P>, OomError> {
        unsafe {
            let vk = self.device.pointers();
            let cmd = self.cmd.take().unwrap();
            try!(check_errors(vk.EndCommandBuffer(cmd)));

            Ok(UnsafeCommandBuffer {
                cmd: cmd,
                device: self.device.clone(),
                pool: self.pool.take().unwrap().finish(),
                flags: self.flags,
                already_submitted: AtomicBool::new(false),
                secondary_cb: self.secondary_cb,
            })
        }
    }

    /// Returns the pool used to create this command buffer builder.
    #[inline]
    pub fn pool(&self) -> &P {
        self.pool.as_ref().unwrap()
    }

    /// Returns the device this command buffer belongs to.
    #[inline]
    pub fn device(&self) -> &Arc<Device> {
        &self.device
    }

    /// Returns true if this is a secondary command buffer.
    #[inline]
    pub fn is_secondary(&self) -> bool {
        self.secondary_cb
    }

    /// Clears an image with a color format, from outside of a render pass.
    ///
    /// If `general_layout` is true, then the `General` image layout is used. Otherwise the
    /// `TransferDstOptimal` layout is used.
    ///
    /// # Panic
    ///
    /// - Panics if the image was not created with the same device as this command buffer.
    /// - Panics if the clear values is not a color value.
    ///
    /// # Safety
    ///
    /// - The image must be kept alive and must be properly synchronized while this command
    ///   buffer runs.
    /// - The ranges must be in range of the image.
    /// - The image must have a non-compressed color format.
    /// - The clear value must match the format of the image.
    /// - The queue family must support graphics or compute operations.
    /// - The image must have been created with the "transfer_dest" usage.
    /// - Must be called outside of a render pass.
    ///
    pub unsafe fn clear_color_image<I>(&mut self, image: &UnsafeImage, general_layout: bool,
                                       color: ClearValue, ranges: I)
        where I: Iterator<Item = ImageSubresourcesRange>
    {
        assert_eq!(image.device().internal_object(), self.device.internal_object());

        let clear_value = match color {
            ClearValue::None => panic!(),
            ClearValue::Float(val) => {
                debug_assert_eq!(image.format().ty(), FormatTy::Float);
                vk::ClearColorValue::float32(val)
            },
            ClearValue::Int(val) => {
                debug_assert_eq!(image.format().ty(), FormatTy::Sint);
                vk::ClearColorValue::int32(val)
            },
            ClearValue::Uint(val) => {
                debug_assert_eq!(image.format().ty(), FormatTy::Uint);
                vk::ClearColorValue::uint32(val)
            },
            ClearValue::Depth(_) => panic!(),
            ClearValue::Stencil(_) => panic!(),
            ClearValue::DepthStencil(_) => panic!(),
        };

        let ranges: SmallVec<[_; 4]> = ranges.filter_map(|range| {
            debug_assert!(range.first_mipmap_level + range.num_mipmap_levels <=
                          image.mipmap_levels());
            debug_assert!(range.first_array_layer + range.num_array_layers <=
                          image.dimensions().array_layers());

            if range.num_mipmap_levels == 0 {
                return None;
            }

            if range.num_array_layers == 0 {
                return None;
            }

            Some(vk::ImageSubresourceRange {
                aspectMask: vk::IMAGE_ASPECT_COLOR_BIT,
                baseMipLevel: range.first_mipmap_level,
                levelCount: range.num_mipmap_levels,
                baseArrayLayer: range.first_array_layer,
                layerCount: range.num_array_layers,
            })
        }).collect();

        // Do nothing if no range to clear.
        if ranges.is_empty() {
            return;
        }

        let layout = if general_layout { vk::IMAGE_LAYOUT_GENERAL }
                     else { vk::IMAGE_LAYOUT_TRANSFER_DST_OPTIMAL };

        let vk = self.device.pointers();
        let cmd = self.cmd.clone().take().unwrap();
        vk.CmdClearColorImage(cmd, image.internal_object(), layout, &clear_value,
                              ranges.len() as u32, ranges.as_ptr());
    }

    /// Clears an image with a depth, stencil or depth-stencil format, from outside of a
    /// render pass.
    ///
    /// If the `ClearValue` is a depth value, then only the depth component will be cleared. Same
    /// for stencil. If it contains a depth-stencil value, then they will both be cleared.
    ///
    /// If `general_layout` is true, then the `General` image layout is used. Otherwise the
    /// `TransferDstOptimal` layout is used.
    ///
    /// # Panic
    ///
    /// - Panics if the image was not created with the same device as this command buffer.
    /// - Panics if the mipmap levels range or the array layers range is invalid, ie. if the end
    ///   is inferior to the start.
    /// - Panics if the clear values is not a depth, stencil or depth-stencil value.
    ///
    /// # Safety
    ///
    /// - The image must be kept alive and must be properly synchronized while this command
    ///   buffer runs.
    /// - The ranges must be in range of the image.
    /// - The image must have a depth, stencil or depth-stencil format.
    /// - The clear value must match the format of the image.
    /// - The queue family must support graphics operations.
    /// - The image must have been created with the "transfer_dest" usage.
    /// - Must be called outside of a render pass.
    ///
    pub unsafe fn clear_depth_stencil_image<I>(&mut self, image: &UnsafeImage, general_layout: bool,
                                               color: ClearValue, ranges: I)
        where I: Iterator<Item = ImageSubresourcesRange>
    {
        assert_eq!(image.device().internal_object(), self.device.internal_object());

        let (clear_value, aspect_mask) = match color {
            ClearValue::None => panic!(),
            ClearValue::Float(_) => panic!(),
            ClearValue::Int(_) => panic!(),
            ClearValue::Uint(_) => panic!(),
            ClearValue::Depth(val) => {
                debug_assert!(image.format().ty() == FormatTy::Depth ||
                              image.format().ty() == FormatTy::DepthStencil);
                let clear = vk::ClearDepthStencilValue { depth: val, stencil: 0 };
                let aspect = vk::IMAGE_ASPECT_DEPTH_BIT;
                (clear, aspect)
            },
            ClearValue::Stencil(val) => {
                debug_assert!(image.format().ty() == FormatTy::Stencil ||
                              image.format().ty() == FormatTy::DepthStencil);
                let clear = vk::ClearDepthStencilValue { depth: 0.0, stencil: val };
                let aspect = vk::IMAGE_ASPECT_STENCIL_BIT;
                (clear, aspect)
            },
            ClearValue::DepthStencil((depth, stencil)) => {
                debug_assert_eq!(image.format().ty(), FormatTy::DepthStencil);
                let clear = vk::ClearDepthStencilValue { depth: depth, stencil: stencil };
                let aspect = vk::IMAGE_ASPECT_DEPTH_BIT | vk::IMAGE_ASPECT_STENCIL_BIT;
                (clear, aspect)
            },
        };

        let ranges: SmallVec<[_; 4]> = ranges.filter_map(|range| {
            debug_assert!(range.first_mipmap_level + range.num_mipmap_levels <=
                          image.mipmap_levels());
            debug_assert!(range.first_array_layer + range.num_array_layers <=
                          image.dimensions().array_layers());

            if range.num_mipmap_levels == 0 {
                return None;
            }

            if range.num_array_layers == 0 {
                return None;
            }

            Some(vk::ImageSubresourceRange {
                aspectMask: aspect_mask,
                baseMipLevel: range.first_mipmap_level,
                levelCount: range.num_mipmap_levels,
                baseArrayLayer: range.first_array_layer,
                layerCount: range.num_array_layers,
            })
        }).collect();

        // Do nothing if no range to clear.
        if ranges.is_empty() {
            return;
        }

        let layout = if general_layout { vk::IMAGE_LAYOUT_GENERAL }
                     else { vk::IMAGE_LAYOUT_TRANSFER_DST_OPTIMAL };

        let vk = self.device.pointers();
        let cmd = self.cmd.clone().take().unwrap();
        vk.CmdClearDepthStencilImage(cmd, image.internal_object(), layout, &clear_value,
                                     ranges.len() as u32, ranges.as_ptr());
    }

    /// Clears attachments of the current render pass.
    ///
    /// You must pass a list of attachment ids and clear values, and a list of rectangles. Each
    /// rectangle of each attachment will be cleared. The rectangle's format is
    /// `[(x, width), (y, height), (array_layer, num_array_layers)]`.
    ///
    /// No memory barriers are needed between this function and preceding or subsequent draw or
    /// attachment clear commands in the same subpass.
    ///
    /// # Panic
    ///
    /// - Panics if one of the clear values is `None`.
    ///
    /// # Safety
    ///
    /// - The attachments ids must be valid, and the clear value must match the format of the
    ///   attachments.
    /// - Must be called from within a render pass.
    /// - The rects must be in range of the framebuffer.
    ///
    pub unsafe fn clear_attachments<Ia, Ir>(&mut self, attachments: Ia, rects: Ir)
        where Ia: Iterator<Item = (u32, ClearValue)>,
              Ir: Iterator<Item = [(u32, u32); 3]>,
    {
        let rects: SmallVec<[_; 3]> = rects.filter_map(|rect| {
            if rect[0].1 == 0 || rect[1].1 == 0 || rect[2].1 == 0 {
                return None;
            }

            Some(vk::ClearRect {
                rect: vk::Rect2D {
                    offset: vk::Offset2D {
                        x: rect[0].0 as i32,
                        y: rect[1].0 as i32,
                    },
                    extent: vk::Extent2D {
                        width: rect[0].1,
                        height: rect[1].1,
                    },
                },
                baseArrayLayer: rect[2].0,
                layerCount: rect[2].1,
            })
        }).collect();

        let attachments: SmallVec<[_; 8]> = attachments.map(|(attachment, clear_value)| {
            let (clear_value, aspect_mask) = match clear_value {
                ClearValue::None => panic!(),
                ClearValue::Float(val) => {
                    let clear = vk::ClearValue::color(vk::ClearColorValue::float32(val));
                    let aspect = vk::IMAGE_ASPECT_COLOR_BIT;
                    (clear, aspect)
                },
                ClearValue::Int(val) => {
                    let clear = vk::ClearValue::color(vk::ClearColorValue::int32(val));
                    let aspect = vk::IMAGE_ASPECT_COLOR_BIT;
                    (clear, aspect)
                },
                ClearValue::Uint(val) => {
                    let clear = vk::ClearValue::color(vk::ClearColorValue::uint32(val));
                    let aspect = vk::IMAGE_ASPECT_COLOR_BIT;
                    (clear, aspect)
                },
                ClearValue::Depth(val) => {
                    let clear = vk::ClearValue::depth_stencil(vk::ClearDepthStencilValue {
                        depth: val, stencil: 0
                    });
                    let aspect = vk::IMAGE_ASPECT_DEPTH_BIT;
                    (clear, aspect)
                },
                ClearValue::Stencil(val) => {
                    let clear = vk::ClearValue::depth_stencil(vk::ClearDepthStencilValue {
                        depth: 0.0, stencil: val
                    });
                    let aspect = vk::IMAGE_ASPECT_STENCIL_BIT;
                    (clear, aspect)
                },
                ClearValue::DepthStencil((depth, stencil)) => {
                    let clear = vk::ClearValue::depth_stencil(vk::ClearDepthStencilValue {
                        depth: depth, stencil: stencil,
                    });
                    let aspect = vk::IMAGE_ASPECT_DEPTH_BIT | vk::IMAGE_ASPECT_STENCIL_BIT;
                    (clear, aspect)
                },
            };

            vk::ClearAttachment {
                aspectMask: aspect_mask,
                colorAttachment: attachment,
                clearValue: clear_value,
            }
        }).collect();

        // Do nothing if nothing to do.
        if rects.is_empty() || attachments.is_empty() {
            return;
        }

        let vk = self.device.pointers();
        let cmd = self.cmd.clone().take().unwrap();
        vk.CmdClearAttachments(cmd, attachments.len() as u32, attachments.as_ptr(),
                               rects.len() as u32, rects.as_ptr());
    }

    /// Fills a buffer by repeating a 32 bits data.
    ///
    /// This is similar to the `memset` function in C/C++.
    ///
    /// # Panic
    ///
    /// - Panics if the buffer was not created with the same device as this command buffer.
    ///
    /// # Safety
    ///
    /// - The buffer must be kept alive and must be properly synchronized while this command
    ///   buffer runs.
    /// - The queue family must support graphics or compute operations.
    /// - Type safety is not checked.
    /// - The offset must be a multiple of four.
    /// - The size must be a multiple of four, or must point to the end of the buffer.
    /// - The buffer must have been created with the "transfer_dest" usage.
    /// - Must be called outside of a render pass.
    ///
    pub unsafe fn fill_buffer(&mut self, buffer: &UnsafeBuffer, offset: usize, size: usize,
                              data: u32)
    {
        assert_eq!(buffer.device().internal_object(), self.device.internal_object());

        debug_assert_eq!(offset % 4, 0);
        debug_assert!(offset + size <= buffer.size());

        let size = if offset + size == buffer.size() {
            vk::WHOLE_SIZE
        } else {
            debug_assert_eq!(size % 4, 0);
            size as vk::DeviceSize
        };

        let vk = self.device.pointers();
        let cmd = self.cmd.clone().take().unwrap();
        vk.CmdFillBuffer(cmd, buffer.internal_object(), offset as vk::DeviceSize,
                         size as vk::DeviceSize, data);
    }

    /// Fills a buffer with some data.
    ///
    /// The actual size that is copied is the minimum between the size of the slice and the size
    /// of the data.
    ///
    /// # Panic
    ///
    /// - Panics if the buffer was not created with the same device as this command buffer.
    ///
    /// # Safety
    ///
    /// - The buffer must be kept alive and must be properly synchronized while this command
    ///   buffer runs.
    /// - Type safety is not checked.
    /// - The offset and size must be multiples of four.
    /// - The size must be less than or equal to 65536 bytes (ie. 64kB).
    /// - The buffer must have been created with the "transfer_dest" usage.
    /// - Must be called outside of a render pass.
    ///
    pub unsafe fn update_buffer<D: ?Sized>(&mut self, buffer: &UnsafeBuffer, offset: usize,
                                           size: usize, data: &D)
        where D: Copy + 'static
    {
        assert_eq!(buffer.device().internal_object(), self.device.internal_object());

        let size = cmp::min(size, mem::size_of_val(data));

        debug_assert_eq!(offset % 4, 0);
        debug_assert_eq!(size % 4, 0);
        debug_assert!(size <= 65536);

        let vk = self.device.pointers();
        let cmd = self.cmd.clone().take().unwrap();
        vk.CmdUpdateBuffer(cmd, buffer.internal_object(), offset as vk::DeviceSize,
                           size as vk::DeviceSize, data as *const D as *const _);
    }

    /// Copies data from a source buffer to a destination buffer.
    ///
    /// This is similar to the `memcpy` function in C/C++.
    ///
    /// Automatically filters out empty regions.
    ///
    /// # Panic
    ///
    /// - Panics if one of the buffers was not created with the same device as this
    ///   command buffer.
    ///
    /// # Safety
    ///
    /// - The buffers must be kept alive and must be properly synchronized while this command
    ///   buffer runs.
    /// - Type safety is not checked.
    /// - The source buffer must have been created with the "transfer_src" usage.
    /// - The destination buffer must have been created with the "transfer_dest" usage.
    /// - Must be called outside of a render pass.
    /// - The offsets and size of the regions must be in range.
    ///
    pub unsafe fn copy_buffer<I>(&mut self, src: &UnsafeBuffer, dest: &UnsafeBuffer, regions: I)
        where I: IntoIterator<Item = BufferCopyRegion>
    {
        assert_eq!(src.device().internal_object(), self.device.internal_object());
        assert_eq!(src.device().internal_object(), dest.device().internal_object());

        let regions: SmallVec<[_; 4]> = {
            let mut res = SmallVec::new();
            for region in regions.into_iter() {
                if region.size == 0 { continue; }
                debug_assert!(region.source_offset < src.size());
                debug_assert!(region.source_offset + region.size <= src.size());
                debug_assert!(region.destination_offset < dest.size());
                debug_assert!(region.destination_offset + region.size <= dest.size());

                res.push(vk::BufferCopy {
                    srcOffset: region.source_offset as vk::DeviceSize,
                    dstOffset: region.destination_offset as vk::DeviceSize,
                    size: region.size as vk::DeviceSize,
                });
            }
            res
        };

        // Calling vkCmdCopyBuffer with 0 regions is forbidden. We just don't call the function
        // then.
        if regions.is_empty() {
            return;
        }

        let vk = self.device.pointers();
        let cmd = self.cmd.clone().take().unwrap();
        vk.CmdCopyBuffer(cmd, src.internal_object(), dest.internal_object(), regions.len() as u32,
                         regions.as_ptr());
    }

    /// Executes secondary command buffers..
    // TODO: check for same device
    // TODO: allow multiple command buffers from different pools
    pub unsafe fn execute_commands<'sec, I, SecP: 'sec>(&mut self, command_buffers: I)
        where I: IntoIterator<Item = &'sec UnsafeCommandBuffer<SecP>>,
              SecP: CommandPool
    {
        let raw_cbs: SmallVec<[_; 16]> = command_buffers.into_iter().map(|cb| {
            debug_assert!(cb.secondary_cb);
            cb.cmd
        }).collect();

        let vk = self.device.pointers();
        let cmd = self.cmd.clone().take().unwrap();
        vk.CmdExecuteCommands(cmd, raw_cbs.len() as u32, raw_cbs.as_ptr());
    }

    /// Adds a pipeline barrier to the command buffer.
    ///
    /// This function itself is not unsafe, but creating a pipeline barrier builder is.
    #[inline]
    pub fn pipeline_barrier(&mut self, barrier: PipelineBarrierBuilder) {
        // If barrier is empty, don't do anything.
        if barrier.src_stage_mask == 0 || barrier.dst_stage_mask == 0 {
            debug_assert!(barrier.src_stage_mask == 0 && barrier.dst_stage_mask == 0);
            debug_assert!(barrier.memory_barriers.is_empty());
            debug_assert!(barrier.buffer_barriers.is_empty());
            debug_assert!(barrier.image_barriers.is_empty());
            return;
        }

        let vk = self.device.pointers();
        let cmd = self.cmd.clone().take().unwrap();

        unsafe {
            vk.CmdPipelineBarrier(cmd, barrier.src_stage_mask, barrier.dst_stage_mask,
                                  barrier.dependency_flags, barrier.memory_barriers.len() as u32,
                                  barrier.memory_barriers.as_ptr(),
                                  barrier.buffer_barriers.len() as u32,
                                  barrier.buffer_barriers.as_ptr(),
                                  barrier.image_barriers.len() as u32,
                                  barrier.image_barriers.as_ptr());
        }
    }

    /// Enters a render pass.
    ///
    /// Any clear value that is equal to `None` is replaced with a dummy value. It is expected that
    /// `None` is passed only for attachments that are not cleared.
    ///
    /// # Panic
    ///
    /// - Panics if the render pass or framebuffer was not created with the same device as this
    ///   command buffer.
    /// - Panics if one of the ranges is invalid.
    ///
    /// # Safety
    ///
    /// - Must be called outside of a render pass.
    /// - The queue family must support graphics operations.
    /// - The render pass and the framebuffer must be kept alive.
    /// - The render pass and the framebuffer must be compatible.
    /// - The clear values must be valid for the attachments.
    ///
    pub unsafe fn begin_render_pass<I, F>(&mut self, render_pass: &UnsafeRenderPass,
                                          framebuffer: &F, clear_values: I,
                                          rect: [Range<u32>; 2], secondary: bool)
        where I: Iterator<Item = ClearValue>,
              F: Framebuffer
    {
        // TODO: restore these checks
        //assert_eq!(render_pass.device().internal_object(), framebuffer.device().internal_object());
        //assert_eq!(self.device.internal_object(), framebuffer.device().internal_object());

        let clear_values: SmallVec<[_; 12]> = clear_values.map(|clear_value| {
            match clear_value {
                ClearValue::None => {
                    vk::ClearValue::color(vk::ClearColorValue::float32([0.0; 4]))
                },
                ClearValue::Float(val) => {
                    vk::ClearValue::color(vk::ClearColorValue::float32(val))
                },
                ClearValue::Int(val) => {
                    vk::ClearValue::color(vk::ClearColorValue::int32(val))
                },
                ClearValue::Uint(val) => {
                    vk::ClearValue::color(vk::ClearColorValue::uint32(val))
                },
                ClearValue::Depth(val) => {
                    vk::ClearValue::depth_stencil(vk::ClearDepthStencilValue {
                        depth: val, stencil: 0
                    })
                },
                ClearValue::Stencil(val) => {
                    vk::ClearValue::depth_stencil(vk::ClearDepthStencilValue {
                        depth: 0.0, stencil: val
                    })
                },
                ClearValue::DepthStencil((depth, stencil)) => {
                    vk::ClearValue::depth_stencil(vk::ClearDepthStencilValue {
                        depth: depth, stencil: stencil,
                    })
                },
            }
        }).collect();

        assert!(rect[0].start <= rect[0].end);
        assert!(rect[1].start <= rect[1].end);

        let infos = vk::RenderPassBeginInfo {
            sType: vk::STRUCTURE_TYPE_RENDER_PASS_BEGIN_INFO,
            pNext: ptr::null(),
            renderPass: render_pass.internal_object(),
            framebuffer: framebuffer.internal_object(),
            renderArea: vk::Rect2D {
                offset: vk::Offset2D {
                    x: rect[0].start as i32,
                    y: rect[1].start as i32,
                },
                extent: vk::Extent2D {
                    width: rect[0].end - rect[0].start,
                    height: rect[1].end - rect[1].start,
                },
            },
            clearValueCount: clear_values.len() as u32,
            pClearValues: clear_values.as_ptr(),
        };

        let vk = self.device.pointers();
        let cmd = self.cmd.clone().take().unwrap();
        vk.CmdBeginRenderPass(cmd, &infos,
                              if secondary { vk::SUBPASS_CONTENTS_SECONDARY_COMMAND_BUFFERS }
                              else { vk::SUBPASS_CONTENTS_INLINE });
    }

    /// Goes to the next subpass of the render pass.
    ///
    /// # Safety
    ///
    /// - Must be called inside of a render pass.
    /// - Must not be at the last subpass of the render pass.
    ///
    #[inline]
    pub unsafe fn next_subpass(&mut self, secondary: bool) {
        let vk = self.device.pointers();
        let cmd = self.cmd.clone().take().unwrap();
        vk.CmdNextSubpass(cmd, if secondary { vk::SUBPASS_CONTENTS_SECONDARY_COMMAND_BUFFERS }
                               else { vk::SUBPASS_CONTENTS_INLINE });
    }

    /// Ends the current render pass.
    ///
    /// # Safety
    ///
    /// - Must be called inside of a render pass.
    /// - Must be at the last subpass of the render pass.
    ///
    #[inline]
    pub unsafe fn end_render_pass(&mut self) {
        let vk = self.device.pointers();
        let cmd = self.cmd.clone().take().unwrap();
        vk.CmdEndRenderPass(cmd);
    }

    /// Binds a graphics pipeline to the graphics pipeline bind point.
    ///
    /// # Safety
    ///
    /// - The queue family must support graphics operations.
    /// - If the variable multisample rate feature is not supported, the current subpass has no
    ///   attachments, and this is not the first call to this function with a graphics pipeline
    ///   after transitioning to the current subpass, then the sample count specified by this
    ///   pipeline must match that set in the previous pipeline.
    ///
    #[inline]
    pub unsafe fn bind_pipeline_graphics<V, L, R>(&mut self, pipeline: &GraphicsPipeline<V, L, R>) {
        let vk = self.device.pointers();
        let cmd = self.cmd.clone().take().unwrap();
        vk.CmdBindPipeline(cmd, vk::PIPELINE_BIND_POINT_GRAPHICS, pipeline.internal_object());
    }

    /// Binds a compute pipeline to the compute pipeline bind point.
    ///
    /// # Safety
    ///
    /// - The queue family must support compute operations.
    ///
    #[inline]
    pub unsafe fn bind_pipeline_compute<L>(&mut self, pipeline: &ComputePipeline<L>) {
        let vk = self.device.pointers();
        let cmd = self.cmd.clone().take().unwrap();
        vk.CmdBindPipeline(cmd, vk::PIPELINE_BIND_POINT_COMPUTE, pipeline.internal_object());
    }

    /// Calls `vkCmdDraw`.
    #[inline]
    pub unsafe fn draw(&mut self, vertex_count: u32, instance_count: u32, first_vertex: u32,
                       first_instance: u32)
    {
        let vk = self.device.pointers();
        let cmd = self.cmd.clone().take().unwrap();
        vk.CmdDraw(cmd, vertex_count, instance_count, first_vertex, first_instance);
    }

    /// Calls `vkCmdDrawIndexed`.
    #[inline]
    pub unsafe fn draw_indexed(&mut self, vertex_count: u32, instance_count: u32,
                               first_index: u32, vertex_offset: i32, first_instance: u32)
    {
        let vk = self.device.pointers();
        let cmd = self.cmd.clone().take().unwrap();
        vk.CmdDrawIndexed(cmd, vertex_count, instance_count, first_index, vertex_offset,
                          first_instance);
    }

    /// Calls `vkCmdDrawIndirect`.
    ///
    /// # Panic
    ///
    /// - Panics if the buffer was not created with the same device as this command buffer.
    ///
    #[inline]
    pub unsafe fn draw_indirect(&mut self, buffer: &UnsafeBuffer, offset: usize, draw_count: u32,
                                stride: u32)
    {
        assert_eq!(buffer.device().internal_object(), self.device.internal_object());

        let vk = self.device.pointers();
        let cmd = self.cmd.clone().take().unwrap();
        vk.CmdDrawIndirect(cmd, buffer.internal_object(), offset as vk::DeviceSize, draw_count,
                           stride);
    }

    /// Calls `vkCmdDrawIndexedIndirect`.
    ///
    /// # Panic
    ///
    /// - Panics if the buffer was not created with the same device as this command buffer.
    ///
    #[inline]
    pub unsafe fn draw_indexed_indirect(&mut self, buffer: &UnsafeBuffer, offset: usize,
                                        draw_count: u32, stride: u32)
    {
        assert_eq!(buffer.device().internal_object(), self.device.internal_object());

        let vk = self.device.pointers();
        let cmd = self.cmd.clone().take().unwrap();
        vk.CmdDrawIndexedIndirect(cmd, buffer.internal_object(), offset as vk::DeviceSize,
                                  draw_count, stride);
    }

    /// Calls `vkCmdDispatch`.
    #[inline]
    pub unsafe fn dispatch(&mut self, x: u32, y: u32, z: u32) {
        let vk = self.device.pointers();
        let cmd = self.cmd.clone().take().unwrap();
        vk.CmdDispatch(cmd, x, y, z);
    }

    /// Calls `vkCmdDispatchIndirect`.
    ///
    /// # Panic
    ///
    /// - Panics if the buffer was not created with the same device as this command buffer.
    ///
    #[inline]
    pub unsafe fn dispatch_indirect(&mut self, buffer: &UnsafeBuffer, offset: usize) {
        assert_eq!(buffer.device().internal_object(), self.device.internal_object());

        let vk = self.device.pointers();
        let cmd = self.cmd.clone().take().unwrap();
        vk.CmdDispatchIndirect(cmd, buffer.internal_object(), offset as vk::DeviceSize);
    }

    /// Calls `vkCmdBindVertexBuffers`.
    ///
    /// The iterator yields a list of buffers and offset of the first byte.
    ///
    /// # Panic
    ///
    /// - Panics if one of the buffers was not created with the same device as this command buffer.
    ///
    #[inline]
    pub unsafe fn bind_vertex_buffers<'a, I>(&mut self, first_binding: u32, buffers: I)
        where I: IntoIterator<Item = (&'a UnsafeBuffer, usize)>
    {
        let mut raw_buffers: SmallVec<[_; 8]> = SmallVec::new();
        let mut raw_offsets: SmallVec<[_; 8]> = SmallVec::new();

        for (buf, off) in buffers {
            assert_eq!(buf.device().internal_object(), self.device.internal_object());
            raw_buffers.push(buf.internal_object());
            raw_offsets.push(off as vk::DeviceSize);
        }

        let vk = self.device.pointers();
        let cmd = self.cmd.clone().take().unwrap();
        vk.CmdBindVertexBuffers(cmd, first_binding, raw_buffers.len() as u32, raw_buffers.as_ptr(),
                                raw_offsets.as_ptr());
    }

    /// Calls `vkCmdBindIndexBuffer`.
    ///
    /// # Panic
    ///
    /// - Panics if the buffer was not created with the same device as this command buffer.
    ///
    #[inline]
    pub unsafe fn bind_index_buffer(&mut self, buffer: &UnsafeBuffer, offset: usize,
                                    index_ty: IndexType)
    {
        assert_eq!(buffer.device().internal_object(), self.device.internal_object());

        let vk = self.device.pointers();
        let cmd = self.cmd.clone().take().unwrap();
        vk.CmdBindIndexBuffer(cmd, buffer.internal_object(), offset as vk::DeviceSize,
                              index_ty as u32);
    }

    /// Calls `vkCmdBindDescriptorSets`.
    ///
    /// # Panic
    ///
    /// - Panics if the layout or one of the sets were not created with the same device as this
    ///   command buffer.
    ///
    #[inline]
    // TODO: change the API to take implementations of DescriptorSet instead of UnsafeDescriptorSet
    pub unsafe fn bind_descriptor_sets<'a, L, Ides, Idyn>(&mut self, graphics_bind_point: bool,
                                                          layout: &L, first_set: u32,
                                                          descriptor_sets: Ides,
                                                          dynamic_offsets: Idyn)
        where L: PipelineLayout,
              Ides: IntoIterator<Item = &'a UnsafeDescriptorSet>,
              Idyn: IntoIterator<Item = u32>
    {
        let bind_point = if graphics_bind_point { vk::PIPELINE_BIND_POINT_GRAPHICS }
                         else { vk::PIPELINE_BIND_POINT_COMPUTE };

        assert_eq!(layout.inner().device().internal_object(), self.device.internal_object());

        let descriptor_sets: SmallVec<[_; 16]> = descriptor_sets.into_iter().map(|set| {
            assert_eq!(set.layout().device().internal_object(), self.device.internal_object());
            set.internal_object()
        }).collect();

        let dynamic_offsets: SmallVec<[_; 64]> = dynamic_offsets.into_iter().collect();

        let vk = self.device.pointers();
        let cmd = self.cmd.clone().take().unwrap();
        vk.CmdBindDescriptorSets(cmd, bind_point, layout.inner().internal_object(), first_set,
                                 descriptor_sets.len() as u32, descriptor_sets.as_ptr(),
                                 dynamic_offsets.len() as u32, dynamic_offsets.as_ptr());
    }

    /// Calls `vkCmdPushConstants`.
    ///
    /// # Panic
    ///
    /// - Panics if the layout was not created with the same device as this command buffer.
    ///
    #[inline]
    pub unsafe fn push_constants<L, D: ?Sized>(&mut self, layout: &L,
                                               stages: ShaderStages, offset: usize, data: &D)
        where L: PipelineLayout
    {
        assert_eq!(layout.inner().device().internal_object(), self.device.internal_object());

        debug_assert!(offset <= u32::MAX as usize);
        debug_assert!(mem::size_of_val(data) <= u32::MAX as usize);

        let vk = self.device.pointers();
        let cmd = self.cmd.clone().take().unwrap();
        vk.CmdPushConstants(cmd, layout.inner().internal_object(), stages.into(), offset as u32,
                            mem::size_of_val(data) as u32, data as *const D as *const _);
    }
}

unsafe impl<P> VulkanObject for UnsafeCommandBufferBuilder<P> where P: CommandPool {
    type Object = vk::CommandBuffer;

    #[inline]
    fn internal_object(&self) -> vk::CommandBuffer {
        self.cmd.unwrap()
    }
}

impl<P> Drop for UnsafeCommandBufferBuilder<P> where P: CommandPool {
    #[inline]
    fn drop(&mut self) {
        if let Some(cmd) = self.cmd {
            unsafe {
                let vk = self.device.pointers();
                vk.EndCommandBuffer(cmd);       // TODO: really needed?

                self.pool.as_ref().unwrap().free(self.secondary_cb, Some(cmd.into()).into_iter());
            }
        }
    }
}

/// Determines the kind of command buffer that we want to create.
#[derive(Clone)]        // TODO: Debug
pub enum Kind<'a, R: 'a, F: 'a> {
    /// A primary command buffer can execute all commands and can call secondary command buffers.
    Primary,

    /// A secondary command buffer can execute all dispatch and transfer operations, but not
    /// drawing operations.
    Secondary,

    /// A secondary command buffer within a render pass can only call draw operations that can
    /// be executed from within a specific subpass.
    SecondaryRenderPass {
        /// Which subpass this secondary command buffer can be called from.
        subpass: Subpass<'a, R>,
        /// The framebuffer object that will be used when calling the command buffer.
        /// This parameter is optional and is an optimization hint for the implementation.
        framebuffer: Option<&'a F>,
    },
}

/// Flags to pass when creating a command buffer.
///
/// The safest option is `SimultaneousUse`, but it may be slower than the other two.
#[derive(Debug, Copy, Clone, PartialEq, Eq)]
pub enum Flags {
    /// The command buffer can be used multiple times, but must not execute more than once
    /// simultaneously.
    None,

    /// The command buffer can be executed multiple times in parallel.
    SimultaneousUse,

    /// The command buffer can only be submitted once. Any further submit is forbidden.
    OneTimeSubmit,
}

/// Range of an image subresource.
pub struct ImageSubresourcesRange {
    pub first_mipmap_level: u32,
    pub num_mipmap_levels: u32,
    pub first_array_layer: u32,
    pub num_array_layers: u32,
}

/// A copy between two buffers.
#[derive(Debug, Copy, Clone, PartialEq, Eq, Hash)]
pub struct BufferCopyRegion {
    /// Offset of the first byte to read from the source buffer.
    pub source_offset: usize,
    /// Offset of the first byte to write to the destination buffer.
    pub destination_offset: usize,
    /// Size in bytes of the copy.
    pub size: usize,
}

/// Prototype for a pipeline barrier that's going to be added to a command buffer builder.
///
/// Note: we use a builder-like API here so that users can pass multiple buffers or images of
/// multiple different types. Doing so with a single function would be very tedious in terms of
/// API.
pub struct PipelineBarrierBuilder {
    src_stage_mask: vk::PipelineStageFlags,
    dst_stage_mask: vk::PipelineStageFlags,
    dependency_flags: vk::DependencyFlags,
    memory_barriers: SmallVec<[vk::MemoryBarrier; 2]>,
    buffer_barriers: SmallVec<[vk::BufferMemoryBarrier; 8]>,
    image_barriers: SmallVec<[vk::ImageMemoryBarrier; 8]>,
}

impl PipelineBarrierBuilder {
    /// Adds a command that adds a pipeline barrier to a command buffer.
    #[inline]
    pub fn new() -> PipelineBarrierBuilder {
        PipelineBarrierBuilder {
            src_stage_mask: 0,
            dst_stage_mask: 0,
            dependency_flags: vk::DEPENDENCY_BY_REGION_BIT,
            memory_barriers: SmallVec::new(),
            buffer_barriers: SmallVec::new(),
            image_barriers: SmallVec::new(),
        }
    }

    /// Returns true if no barrier or execution dependency has been added yet.
    #[inline]
    pub fn is_empty(&self) -> bool {
        self.src_stage_mask == 0 || self.dst_stage_mask == 0
    }

    /// Merges another pipeline builder into this one.
    #[inline]
    pub fn merge(&mut self, other: PipelineBarrierBuilder) {
        self.src_stage_mask |= other.src_stage_mask;
        self.dst_stage_mask |= other.dst_stage_mask;
        self.dependency_flags &= other.dependency_flags;

        self.memory_barriers.extend(other.memory_barriers.into_iter());
        self.buffer_barriers.extend(other.buffer_barriers.into_iter());
        self.image_barriers.extend(other.image_barriers.into_iter());
    }

    /// Adds an execution dependency. This means that all the stages in `source` of the previous
    /// commands must finish before any of the stages in `dest` of the following commands can start.
    ///
    /// # Safety
    ///
    /// - If the pipeline stages include geometry or tessellation stages, then the corresponding
    ///   features must have been enabled.
    /// - There are certain rules regarding the pipeline barriers inside render passes.
    ///
    #[inline]
    pub unsafe fn add_execution_dependency(&mut self, source: PipelineStages, dest: PipelineStages,
                                           by_region: bool)
    {
        if !by_region {
            self.dependency_flags = 0;
        }

        self.src_stage_mask |= source.into();
        self.dst_stage_mask |= dest.into();
    }

    /// Adds a memory barrier. This means that all the memory writes by the given source stages
    /// for the given source accesses must be visible by the given dest stages for the given dest
    /// accesses.
    ///
    /// Also adds an execution dependency.
    ///
    /// # Safety
    ///
    /// - If the pipeline stages include geometry or tessellation stages, then the corresponding
    ///   features must have been enabled.
    /// - There are certain rules regarding the pipeline barriers inside render passes.
    ///
    pub unsafe fn add_memory_barrier(&mut self, source_stage: PipelineStages,
                                     source_access: AccessFlagBits, dest_stage: PipelineStages,
                                     dest_access: AccessFlagBits, by_region: bool)
    {
        self.add_execution_dependency(source_stage, dest_stage, by_region);

        self.memory_barriers.push(vk::MemoryBarrier {
            sType: vk::STRUCTURE_TYPE_MEMORY_BARRIER,
            pNext: ptr::null(),
            srcAccessMask: source_access.into(),
            dstAccessMask: dest_access.into(),
        });
    }

    pub unsafe fn add_buffer_barrier_request(&mut self, buffer: &UnsafeBuffer,
                                             request: BufferPipelineBarrierRequest)
    {
        if !request.by_region {
            self.dependency_flags = 0;
        }

        self.src_stage_mask |= request.source_stage.into();
        self.dst_stage_mask |= request.destination_stages.into();

        if let Some(memory_barrier) = request.memory_barrier {
            let (src_queue, dest_queue) = /*if let Some((src_queue, dest_queue)) = queue_transfer {
                (src_queue, dest_queue)
            } else {*/
                (vk::QUEUE_FAMILY_IGNORED, vk::QUEUE_FAMILY_IGNORED)
            /*}*/;
            
            // TODO: add more debug asserts

            debug_assert!(memory_barrier.offset + memory_barrier.size <= buffer.size());

            self.buffer_barriers.push(vk::BufferMemoryBarrier {
                sType: vk::STRUCTURE_TYPE_BUFFER_MEMORY_BARRIER,
                pNext: ptr::null(),
                srcAccessMask: memory_barrier.source_access.into(),
                dstAccessMask: memory_barrier.destination_access.into(),
                srcQueueFamilyIndex: src_queue,
                dstQueueFamilyIndex: dest_queue,
                buffer: buffer.internal_object(),
                offset: memory_barrier.offset as vk::DeviceSize,
                size: memory_barrier.size as vk::DeviceSize,
            });
        }
    }

    pub unsafe fn add_image_barrier_request(&mut self, image: &UnsafeImage,
                                            request: ImagePipelineBarrierRequest)
    {
        if !request.by_region {
            self.dependency_flags = 0;
        }

        self.src_stage_mask |= request.source_stage.into();
        self.dst_stage_mask |= request.destination_stages.into();

        if let Some(memory_barrier) = request.memory_barrier {
            let (src_queue, dest_queue) = /*if let Some((src_queue, dest_queue)) = queue_transfer {
                (src_queue, dest_queue)
            } else {*/
                (vk::QUEUE_FAMILY_IGNORED, vk::QUEUE_FAMILY_IGNORED)
            /*}*/;

            // TODO: add more debug asserts

            debug_assert!(memory_barrier.first_mipmap +
                          memory_barrier.num_mipmaps <= image.mipmap_levels());
            debug_assert!(memory_barrier.first_layer +
                          memory_barrier.num_layers <= image.dimensions().array_layers());

            self.image_barriers.push(vk::ImageMemoryBarrier {
                sType: vk::STRUCTURE_TYPE_IMAGE_MEMORY_BARRIER,
                pNext: ptr::null(),
                srcAccessMask: memory_barrier.source_access.into(),
                dstAccessMask: memory_barrier.destination_access.into(),
                oldLayout: memory_barrier.old_layout as u32,
                newLayout: memory_barrier.new_layout as u32,
                srcQueueFamilyIndex: src_queue,
                dstQueueFamilyIndex: dest_queue,
                image: image.internal_object(),
                subresourceRange: vk::ImageSubresourceRange {
                    aspectMask: 1 | 2 | 4 | 8,      // FIXME: wrong
                    baseMipLevel: memory_barrier.first_mipmap,
                    levelCount: memory_barrier.num_mipmaps,
                    baseArrayLayer: memory_barrier.first_layer,
                    layerCount: memory_barrier.num_layers,
                },
            });
        }
    }

    /// Adds a buffer memory barrier. This means that all the memory writes to the given buffer by
    /// the given source stages for the given source accesses must be visible by the given dest
    /// stages for the given dest accesses.
    ///
    /// Also adds an execution dependency.
    ///
    /// Also allows transfering buffer ownership between queues.
    ///
    /// # Safety
    ///
    /// - If the pipeline stages include geometry or tessellation stages, then the corresponding
    ///   features must have been enabled.
    /// - There are certain rules regarding the pipeline barriers inside render passes.
    /// - The buffer must be alive for at least as long as the command buffer to which this barrier
    ///   is added.
    /// - Queue ownership transfers must be correct.
    ///
    pub unsafe fn add_buffer_memory_barrier<'a, T: ?Sized, B>
                  (&mut self, buffer: BufferSlice<'a, T, B>, source_stage: PipelineStages,
                   source_access: AccessFlagBits, dest_stage: PipelineStages,
                   dest_access: AccessFlagBits, by_region: bool,
                   queue_transfer: Option<(u32, u32)>)
        where B: Buffer
    {
        self.add_execution_dependency(source_stage, dest_stage, by_region);

        debug_assert!(buffer.size() + buffer.offset() <= buffer.buffer().size());

        let (src_queue, dest_queue) = if let Some((src_queue, dest_queue)) = queue_transfer {
            (src_queue, dest_queue)
        } else {
            (vk::QUEUE_FAMILY_IGNORED, vk::QUEUE_FAMILY_IGNORED)
        };

        self.buffer_barriers.push(vk::BufferMemoryBarrier {
            sType: vk::STRUCTURE_TYPE_BUFFER_MEMORY_BARRIER,
            pNext: ptr::null(),
            srcAccessMask: source_access.into(),
            dstAccessMask: dest_access.into(),
            srcQueueFamilyIndex: src_queue,
            dstQueueFamilyIndex: dest_queue,
            buffer: buffer.buffer().inner().internal_object(),
            offset: buffer.offset() as vk::DeviceSize,
            size: buffer.size() as vk::DeviceSize,
        });
    }

    /// Adds an image memory barrier. This is the equivalent of `add_buffer_memory_barrier` but
    /// for images.
    ///
    /// In addition to transfering image ownership between queues, it also allows changing the
    /// layout of images.
    ///
    /// # Safety
    ///
    /// - If the pipeline stages include geometry or tessellation stages, then the corresponding
    ///   features must have been enabled.
    /// - There are certain rules regarding the pipeline barriers inside render passes.
    /// - The buffer must be alive for at least as long as the command buffer to which this barrier
    ///   is added.
    /// - Queue ownership transfers must be correct.
    /// - Image layouts transfers must be correct.
    /// - Access flags must be compatible with the image usage flags passed at image creation.
    ///
    pub unsafe fn add_image_memory_barrier<I>(&mut self, image: &Arc<I>, mipmaps: Range<u32>,
                  layers: Range<u32>, source_stage: PipelineStages, source_access: AccessFlagBits,
                  dest_stage: PipelineStages, dest_access: AccessFlagBits, by_region: bool,
                  queue_transfer: Option<(u32, u32)>, current_layout: Layout, new_layout: Layout)
        where I: Image
    {
        self.add_execution_dependency(source_stage, dest_stage, by_region);

        debug_assert!(mipmaps.start < mipmaps.end);
        // TODO: debug_assert!(mipmaps.end <= image.mipmap_levels());
        debug_assert!(layers.start < layers.end);
        debug_assert!(layers.end <= image.dimensions().array_layers());

        let (src_queue, dest_queue) = if let Some((src_queue, dest_queue)) = queue_transfer {
            (src_queue, dest_queue)
        } else {
            (vk::QUEUE_FAMILY_IGNORED, vk::QUEUE_FAMILY_IGNORED)
        };

        self.image_barriers.push(vk::ImageMemoryBarrier {
            sType: vk::STRUCTURE_TYPE_IMAGE_MEMORY_BARRIER,
            pNext: ptr::null(),
            srcAccessMask: source_access.into(),
            dstAccessMask: dest_access.into(),
            oldLayout: current_layout as u32,
            newLayout: new_layout as u32,
            srcQueueFamilyIndex: src_queue,
            dstQueueFamilyIndex: dest_queue,
            image: image.inner().internal_object(),
            subresourceRange: vk::ImageSubresourceRange {
                aspectMask: 1 | 2 | 4 | 8,      // FIXME: wrong
                baseMipLevel: mipmaps.start,
                levelCount: mipmaps.end - mipmaps.start,
                baseArrayLayer: layers.start,
                layerCount: layers.end - layers.start,
            },
        });
    }
}

pub struct UnsafeCommandBuffer<P> where P: CommandPool {
    // The Vulkan command buffer.
    cmd: vk::CommandBuffer,

    // Device that owns the command buffer.
    device: Arc<Device>,

    // Pool that owns the command buffer.
    pool: P::Finished,

    // Flags that were used at creation.
    flags: Flags,

    // True if the command buffer has always been submitted once. Only relevant if `flags` is
    // `OneTimeSubmit`.
    already_submitted: AtomicBool,

    // True if we are a secondary command buffer.
    secondary_cb: bool,
}

impl<P> UnsafeCommandBuffer<P> where P: CommandPool {
    /// Returns the device used to create this command buffer.
    #[inline]
    pub fn device(&self) -> &Arc<Device> {
        &self.device
    }
}

// Since the only moment where we access the pool is in the `Drop` trait, we can safely implement
// `Sync` on the command buffer.
// TODO: this could be generalized with a general-purpose wrapper that only allows &mut access
unsafe impl<P> Sync for UnsafeCommandBuffer<P> where P: CommandPool {}

unsafe impl<P> VulkanObject for UnsafeCommandBuffer<P> where P: CommandPool {
    type Object = vk::CommandBuffer;

    #[inline]
    fn internal_object(&self) -> vk::CommandBuffer {
        self.cmd
    }
}

impl<P> Drop for UnsafeCommandBuffer<P> where P: CommandPool {
    #[inline]
    fn drop(&mut self) {
        unsafe {
            self.pool.free(self.secondary_cb, Some(self.cmd.into()).into_iter());
        }
    }
}