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
//! Vulkan Commands

use vk::*;
use {VkHandle, Device, DeviceChild};
#[cfg(feature = "Implements")]
use VkResultHandler;
#[cfg(feature = "Implements")]
use std::mem::{size_of, transmute};
use std::ops::Range;
use std::borrow::Borrow;
use {Image, Buffer, ImageLayout};
#[cfg(feature = "Implements")]
use {Framebuffer, RenderPass, Pipeline, PipelineLayout, PipelineStageFlags, ShaderStage};
#[cfg(feature = "Implements")]
use {StencilFaceMask, FilterMode, Event};
#[cfg(feature = "Implements")]
use {QueryPipelineStatisticFlags, QueryPool, QueryResultFlags};

/// Opaque handle to a command pool object
#[derive(Clone)] pub struct CommandPool(VkCommandPool, ::Device);
/// Opaque handle to a command buffer object
#[repr(C)] #[derive(Clone, Copy)] pub struct CommandBuffer(VkCommandBuffer);

#[cfg(feature = "Implements")] DeviceChildCommonDrop!{ for CommandPool[vkDestroyCommandPool] }
impl VkHandle for CommandPool   { type Handle = VkCommandPool;   fn native_ptr(&self) -> VkCommandPool   { self.0 } }
impl VkHandle for CommandBuffer { type Handle = VkCommandBuffer; fn native_ptr(&self) -> VkCommandBuffer { self.0 } }
impl DeviceChild for CommandPool { fn device(&self) -> &Device { &self.1 } }

/// The recording state of commandbuffers
#[cfg(feature = "Implements")]
pub struct CmdRecord<'d> { ptr: &'d CommandBuffer, layout: [Option<VkPipelineLayout>; 2] }

/// Implicitly closing the recording state. This may cause a panic when there are errors in commands
#[cfg(feature = "Implements")]
impl<'d> Drop for CmdRecord<'d>
{
	fn drop(&mut self)
	{
		unsafe
		{
			vkEndCommandBuffer(self.ptr.native_ptr()).into_result().expect("Error closing command recording state");
		}
	}
}

/// Following methods are enabled with [feature = "Implements"]
#[cfg(feature = "Implements")]
impl CommandPool
{
	/// Create a new command pool object
	/// # Failures
	/// On failure, this command returns
	///
	/// * `VK_ERROR_OUT_OF_HOST_MEMORY`
	/// * `VK_ERROR_OUT_OF_DEVICE_MEMORY`
	pub fn new(device: &Device, queue_family: u32, transient: bool, indiv_resettable: bool) -> ::Result<Self>
	{
		let cinfo = VkCommandPoolCreateInfo
		{
			queueFamilyIndex: queue_family, flags: if transient { VK_COMMAND_POOL_CREATE_TRANSIENT_BIT } else { 0 }
				| if indiv_resettable { VK_COMMAND_POOL_CREATE_RESET_COMMAND_BUFFER_BIT } else { 0 },
			.. Default::default()
		};
		let mut h = VK_NULL_HANDLE as _;
		unsafe
		{
			vkCreateCommandPool(device.native_ptr(), &cinfo, ::std::ptr::null(), &mut h)
				.into_result().map(|_| CommandPool(h, device.clone()))
		}
	}
	/// Allocate command buffers from an existing command pool
	/// # Failures
	/// On failure, this command returns
	///
	/// * `VK_ERROR_OUT_OF_HOST_MEMORY`
	/// * `VK_ERROR_OUT_OF_DEVICE_MEMORY`
	pub fn alloc(&self, count: u32, primary: bool) -> ::Result<Vec<CommandBuffer>>
	{
		let ainfo = VkCommandBufferAllocateInfo
		{
			commandBufferCount: count, level: if primary { VK_COMMAND_BUFFER_LEVEL_PRIMARY } else { VK_COMMAND_BUFFER_LEVEL_SECONDARY },
			commandPool: self.0, .. Default::default()
		};
		let mut hs = vec![VK_NULL_HANDLE as _; count as _];
		unsafe
		{
			vkAllocateCommandBuffers(self.1.native_ptr(), &ainfo, hs.as_mut_ptr()).into_result().map(|_| transmute(hs))
		}
	}
    /// Resets a command pool
    /// # Safety
    /// Application cannot use command buffers after this call
    /// # Failures
    /// On failure, this command returns
	///
    /// * `VK_ERROR_OUT_OF_HOST_MEMORY`
    /// * `VK_ERROR_OUT_OF_DEVICE_MEMORY`
	pub fn reset(&self, release_resources: bool) -> ::Result<()>
	{
		let flags = if release_resources { VK_COMMAND_POOL_RESET_RELEASE_RESOURCES_BIT } else { 0 };
		unsafe { vkResetCommandPool(self.1.native_ptr(), self.0, flags).into_result() }
	}
	/// Free command buffers
	pub fn free(&self, buffers: &[CommandBuffer])
	{
		unsafe { vkFreeCommandBuffers(self.1.native_ptr(), self.0, buffers.len() as _, buffers.as_ptr() as *const _) };
	}
}

/// Following methods are enabled with [feature = "Implements"]
#[cfg(feature = "Implements")]
impl CommandBuffer
{
	/// Start recording a primary command buffer
	/// # Failures
	/// On failure, this command returns
	///
	/// * `VK_ERROR_OUT_OF_HOST_MEMORY`
	/// * `VK_ERROR_OUT_OF_DEVICE_MEMORY`
	pub fn begin(&self) -> ::Result<CmdRecord>
	{
		unsafe
		{
			vkBeginCommandBuffer(self.0, &Default::default()).into_result()
				.map(|_| CmdRecord { ptr: self, layout: [None, None] })
		}
	}
	/// Start recording a primary command buffer that will be submitted once
	/// # Failures
	/// On failure, this command returns
	/// 
	/// * `VK_ERROR_OUT_OF_HOST_MEMORY`
	/// * `VK_ERROR_OUT_OF_DEVICE_MEMORY`
	pub fn begin_once(&self) -> ::Result<CmdRecord>
	{
		let info = VkCommandBufferBeginInfo { flags: VK_COMMAND_BUFFER_USAGE_ONE_TIME_SUBMIT_BIT, .. Default::default() };
		unsafe
		{
			vkBeginCommandBuffer(self.0, &info).into_result().map(|_| CmdRecord { ptr: self, layout: [None, None] })
		}
	}
	/// Start recording a secondary command buffer
	/// # Failures
	/// On failure, this command returns
	///
	/// * `VK_ERROR_OUT_OF_HOST_MEMORY`
	/// * `VK_ERROR_OUT_OF_DEVICE_MEMORY`
	pub fn begin_inherit(&self, renderpass: Option<(&Framebuffer, &RenderPass, u32)>,
		query: Option<(OcclusionQuery, QueryPipelineStatisticFlags)>) -> ::Result<CmdRecord>
	{
		let flags = if renderpass.is_some() { VK_COMMAND_BUFFER_USAGE_RENDER_PASS_CONTINUE_BIT } else { 0 };
		let (fb, rp, s) = renderpass.map(|(f, r, s)| (f.native_ptr(), r.native_ptr(), s))
			.unwrap_or((VK_NULL_HANDLE as _, VK_NULL_HANDLE as _, 0));
		let (oq, psq) = query.map(|(o, p)| (o, p.0)).unwrap_or((OcclusionQuery::Disable, 0));
		let inherit = VkCommandBufferInheritanceInfo
		{
			framebuffer: fb, renderPass: rp, subpass: s, occlusionQueryEnable: (oq != OcclusionQuery::Disable) as _,
			queryFlags: if oq == OcclusionQuery::Precise { VK_QUERY_CONTROL_PRECISE_BIT } else { 0 },
			pipelineStatistics: psq, .. Default::default()
		};
		let binfo = VkCommandBufferBeginInfo { pInheritanceInfo: &inherit, flags, .. Default::default() };
		unsafe
		{
			vkBeginCommandBuffer(self.0, &binfo).into_result().map(|_| CmdRecord { ptr: self, layout: [None, None] })
		}
	}
}

/// [feature = "Implements"] Graphics/Compute Commands: Pipeline Setup
#[cfg(feature = "Implements")]
impl<'d> CmdRecord<'d>
{
	/// Bind a pipeline object to a command buffer
	pub fn bind_graphics_pipeline(&mut self, pipeline: &Pipeline) -> &mut Self
	{
		unsafe { vkCmdBindPipeline(self.ptr.native_ptr(), VK_PIPELINE_BIND_POINT_GRAPHICS, pipeline.native_ptr()) };
		return self;
	}
	/// Bind a pipeline object to a command buffer
	pub fn bind_compute_pipeline(&mut self, pipeline: &Pipeline) -> &mut Self
	{
		unsafe { vkCmdBindPipeline(self.ptr.native_ptr(), VK_PIPELINE_BIND_POINT_COMPUTE, pipeline.native_ptr()) };
		return self;
	}
	/// Bind a pipeline layout object to a command buffer
	pub fn bind_graphics_pipeline_layout(&mut self, layout: &PipelineLayout) -> &mut Self
	{
		self.layout[VK_PIPELINE_BIND_POINT_GRAPHICS as usize] = Some(layout.native_ptr());
		return self;
	}
	/// Bind a pipeline layout object to a command buffer
	pub fn bind_compute_pipeline_layout(&mut self, layout: &PipelineLayout) -> &mut Self
	{
		self.layout[VK_PIPELINE_BIND_POINT_COMPUTE as usize] = Some(layout.native_ptr());
		return self;
	}
	/// Bind a pipeline object and a pipeline layout object to a command buffer
	pub fn bind_graphics_pipeline_pair(&mut self, pipeline: &Pipeline, layout: &PipelineLayout) -> &mut Self
	{
		self.bind_graphics_pipeline_layout(layout).bind_graphics_pipeline(pipeline)
	}
	/// Bind a pipeline object and a pipeline layout object to a command buffer
	pub fn bind_compute_pipeline_pair(&mut self, pipeline: &Pipeline, layout: &PipelineLayout) -> &mut Self
	{
		self.bind_compute_pipeline_layout(layout).bind_compute_pipeline(pipeline)
	}
	fn current_pipeline_layout_g(&self) -> VkPipelineLayout
	{
		self.layout[VK_PIPELINE_BIND_POINT_GRAPHICS as usize].expect("Pipeline is not bound for Graphics")
	}
	fn current_pipeline_layout_c(&self) -> VkPipelineLayout
	{
		self.layout[VK_PIPELINE_BIND_POINT_COMPUTE as usize].expect("Pipeline is not bound for Compute")
	}
	/// Binds descriptor sets to a command buffer
	pub fn bind_graphics_descriptor_sets(&mut self, first: u32,
		descriptor_sets: &[VkDescriptorSet], dynamic_offsets: &[u32]) -> &mut Self
	{
		unsafe
		{
			vkCmdBindDescriptorSets(self.ptr.native_ptr(), VK_PIPELINE_BIND_POINT_GRAPHICS,
				self.current_pipeline_layout_g(),
				first, descriptor_sets.len() as _, descriptor_sets.as_ptr(),
				dynamic_offsets.len() as _, dynamic_offsets.as_ptr())
		};
		return self;
	}
	/// Binds descriptor sets to a command buffer
	pub fn bind_compute_descriptor_sets(&mut self, first: u32,
		descriptor_sets:&[VkDescriptorSet], dynamic_offsets: &[u32]) -> &mut Self
	{
		unsafe
		{ 
			vkCmdBindDescriptorSets(self.ptr.native_ptr(), VK_PIPELINE_BIND_POINT_COMPUTE,
				self.current_pipeline_layout_c(),
				first, descriptor_sets.len() as _, descriptor_sets.as_ptr(),
				dynamic_offsets.len() as _, dynamic_offsets.as_ptr())
		};
		return self;
	}
	/// Update the value of push constant
	pub fn push_graphics_constant<T>(&mut self, stage: ShaderStage, offset: u32, value: &T) -> &mut Self
	{
		unsafe
		{
			vkCmdPushConstants(self.ptr.native_ptr(), self.current_pipeline_layout_g(),
				stage.0, offset, size_of::<T>() as _, value as *const T as *const _);
		}
		return self;
	}
	/// Update the value of push constant
	pub fn push_compute_constant<T>(&mut self, stage: ShaderStage, offset: u32, value: &T) -> &mut Self
	{
		unsafe
		{
			vkCmdPushConstants(self.ptr.native_ptr(), self.current_pipeline_layout_c(),
				stage.0, offset, size_of::<T>() as _, value as *const T as *const _);
		}
		return self;
	}

	/// Push descriptor updates into a command buffer
	#[cfg(feature = "VK_KHR_push_descriptor")]
	pub fn push_graphics_descriptor_set(&mut self, set: u32, writes: &[DescriptorSetWriteInfo]) -> &mut Self
	{
		// save flatten results
		let wt = writes.iter().map(|x|
		{
			let (ty, cnt, iv, bv, bvv) = x.3.decomposite();
			let ivs = iv.iter().map(|&(s, v, l)| VkDescriptorImageInfo
			{
				sampler: s.unwrap_or(VK_NULL_HANDLE as _), imageView: v, imageLayout: l as _
			}).collect::<Vec<_>>();
			let bvs = bv.iter()
				.map(|&(b, ref r)| VkDescriptorBufferInfo { buffer: b, offset: r.start as _, range: r.len() as _ })
				.collect::<Vec<_>>();
			(x.0, x.1, x.2, ty, cnt, ivs, bvs, bvv)
		}).collect::<Vec<_>>();
		let w = wt.iter().map(|&(set, binding, array, dty, count, ref iv, ref bv, ref bvv)| VkWriteDescriptorSet
		{
			dstSet: set, dstBinding: binding, dstArrayElement: array, descriptorType: dty as _, descriptorCount: count,
			pImageInfo: iv.as_ptr(), pBufferInfo: bv.as_ptr(), pTexelBufferView: bvv.as_ptr(), .. Default::default()
		}).collect::<Vec<_>>();
		unsafe
		{
			vkCmdPushDescriptorSetKHR(self.ptr.native_ptr(), VK_PIPELINE_BIND_POINT_GRAPHICS,
				self.current_pipeline_layout_g(), set, w.len() as _, w.as_ptr())
		};
		return self;
	}
	/// Push descriptor updates into a command buffer
	#[cfg(feature = "VK_KHR_push_descriptor")]
	pub fn push_compute_descriptor_set(&mut self, set: u32, writes: &[DescriptorSetWriteInfo]) -> &mut Self
	{
		// save flatten results
		let wt = writes.iter().map(|x|
		{
			let (ty, cnt, iv, bv, bvv) = x.3.decomposite();
			let ivs = iv.iter().map(|&(s, v, l)| VkDescriptorImageInfo
			{
				sampler: s.unwrap_or(VK_NULL_HANDLE as _), imageView: v, imageLayout: l as _
			}).collect::<Vec<_>>();
			let bvs = bv.iter()
				.map(|&(b, ref r)| VkDescriptorBufferInfo { buffer: b, offset: r.start as _, range: r.len() as _ })
				.collect::<Vec<_>>();
			(x.0, x.1, x.2, ty, cnt, ivs, bvs, bvv)
		}).collect::<Vec<_>>();
		let w = wt.iter().map(|&(set, binding, array, dty, count, ref iv, ref bv, ref bvv)| VkWriteDescriptorSet
		{
			dstSet: set, dstBinding: binding, dstArrayElement: array, descriptorType: dty as _, descriptorCount: count,
			pImageInfo: iv.as_ptr(), pBufferInfo: bv.as_ptr(), pTexelBufferView: bvv.as_ptr(), .. Default::default()
		}).collect::<Vec<_>>();
		unsafe
		{
			vkCmdPushDescriptorSetKHR(self.ptr.native_ptr(), VK_PIPELINE_BIND_POINT_COMPUTE,
				self.current_pipeline_layout_c(), set, w.len() as _, w.as_ptr())
		};
		return self;
	}
}

/// [feature = "Implements"] Graphics Commands: Updating dynamic states
#[cfg(feature = "Implements")]
impl<'d> CmdRecord<'d>
{
	/// Set the viewport on a command buffer
	pub fn set_viewport(&mut self, first: u32, viewports: &[VkViewport]) -> &mut Self
	{
		unsafe { vkCmdSetViewport(self.ptr.native_ptr(), first, viewports.len() as _, viewports.as_ptr()) };
		return self;
	}
	/// Set the dynamic scissor rectangles on a command buffer
	pub fn set_scissor(&mut self, first: u32, scissors: &[VkRect2D]) -> &mut Self
	{
		unsafe { vkCmdSetScissor(self.ptr.native_ptr(), first, scissors.len() as _, scissors.as_ptr()) };
		return self;
	}
	/// Set the dynamic line width state
	pub fn set_line_width(&mut self, w: f32) -> &Self
	{
		unsafe { vkCmdSetLineWidth(self.ptr.native_ptr(), w) };
		return self;
	}
	/// Set the depth bias dynamic state
	pub fn set_depth_bias(&mut self, constant_factor: f32, clamp: f32, slope_factor: f32) -> &mut Self
	{
		unsafe { vkCmdSetDepthBias(self.ptr.native_ptr(), constant_factor, clamp, slope_factor) };
		return self;
	}
	/// Set the values of blend constants
	pub fn set_blend_constants(&mut self, blend_constants: [f32; 4]) -> &mut Self
	{
		unsafe { vkCmdSetBlendConstants(self.ptr.native_ptr(), blend_constants) };
		return self;
	}
	/// Set the depth bounds test values for a command buffer
	pub fn set_depth_bounds(&mut self, bounds: Range<f32>) -> &mut Self
	{
		unsafe { vkCmdSetDepthBounds(self.ptr.native_ptr(), bounds.start, bounds.end) };
		return self;
	}
	/// Set the stencil compare mask dynamic state
	pub fn set_stencil_compare_mask(&mut self, face_mask: StencilFaceMask, compare_mask: u32) -> &mut Self
	{
		unsafe { vkCmdSetStencilCompareMask(self.ptr.native_ptr(), face_mask as _, compare_mask) };
		return self;
	}
	/// Set the stencil write mask dynamic state
	pub fn set_stencil_write_mask(&mut self, face_mask: StencilFaceMask, write_mask: u32) -> &mut Self
	{
		unsafe { vkCmdSetStencilWriteMask(self.ptr.native_ptr(), face_mask as _, write_mask) };
		return self;
	}
	/// Set the stencil reference dynamic state
	pub fn set_stencil_reference(&mut self, face_mask: StencilFaceMask, reference: u32) -> &mut Self
	{
		unsafe { vkCmdSetStencilReference(self.ptr.native_ptr(), face_mask as _, reference) };
		return self;
	}
	/// [feature = "VK_EXT_sample_locations"]
	/// Set the sample locations state
	#[cfg(feature = "VK_EXT_sample_locations")]
	pub fn set_sample_locations(&mut self, info: &VkSampleLocationsInfoEXT) -> &mut Self
	{
		unsafe { vkCmdSetSampleLocationsEXT(self.ptr.native_ptr(), info as _); }
		return self;
	}
}

/// [feature = "Implements"] Graphics Commands: Binding Buffers
#[cfg(feature = "Implements")]
impl<'d> CmdRecord<'d>
{
	/// Bind an index buffer to a command buffer
	pub fn bind_index_buffer(&mut self, buffer: &Buffer, offset: usize, index_type: IndexType) -> &mut Self
	{
		unsafe { vkCmdBindIndexBuffer(self.ptr.native_ptr(), buffer.native_ptr(), offset as _, index_type as _) };
		return self;
	}
	/// Bind vertex buffers to a command buffer
	pub fn bind_vertex_buffers(&mut self, first: u32, buffers: &[(&Buffer, usize)]) -> &mut Self
	{
		let (bufs, ofs): (Vec<_>, Vec<_>) =
			buffers.into_iter().map(|&(b, o)| (b.native_ptr(), o as VkDeviceSize)).unzip();
		unsafe { vkCmdBindVertexBuffers(self.ptr.native_ptr(), first, bufs.len() as _, bufs.as_ptr(), ofs.as_ptr()) };
		return self;
	}
}

/// [feature = "Implements"] Graphics Commands: Inside a Render Pass
#[cfg(feature = "Implements")]
impl<'d> CmdRecord<'d>
{
	/// Draw primitives
	pub fn draw(&mut self, vertex_count: u32, instance_count: u32, first_vertex: u32, first_instance: u32) -> &mut Self
	{
		unsafe { vkCmdDraw(self.ptr.native_ptr(), vertex_count, instance_count, first_vertex, first_instance) };
		return self;
	}
	/// Issue an indexed draw into a command buffer
	pub fn draw_indexed(&mut self, index_count: u32, instance_count: u32,
		first_index: u32, vertex_offset: i32, first_instance: u32) -> &mut Self
	{
		unsafe
		{
			vkCmdDrawIndexed(self.ptr.native_ptr(), index_count, instance_count,
				first_index, vertex_offset, first_instance)
		};
		return self;
	}
	/// Issue an indirect draw into a command buffer
	pub fn draw_indirect(&mut self, buffer: &Buffer, offset: usize, draw_count: u32, stride: u32) -> &mut Self
	{
		unsafe { vkCmdDrawIndirect(self.ptr.native_ptr(), buffer.native_ptr(), offset as _, draw_count, stride) };
		return self;
	}
	/// Perform an indexed indirect draw
	pub fn draw_indexed_indirect(&mut self, buffer: &Buffer, offset: usize, draw_count: u32, stride: u32) -> &mut Self
	{
		unsafe
		{
			vkCmdDrawIndexedIndirect(self.ptr.native_ptr(), buffer.native_ptr(), offset as _, draw_count, stride)
		};
		return self;
	}
}

/// [feature = "Implements"] Compute Commands: Dispatching kernels
#[cfg(feature = "Implements")]
impl<'d> CmdRecord<'d>
{
	/// Dispatch compute work items
	pub fn dispatch(&mut self, group_count_x: u32, group_count_y: u32, group_count_z: u32) -> &mut Self
	{
		unsafe { vkCmdDispatch(self.ptr.native_ptr(), group_count_x, group_count_y, group_count_z) };
		return self;
	}
	/// Dispatch compute work items using indirect parameters
	pub fn dispatch_indirect(&mut self, buffer: &Buffer, offset: usize) -> &mut Self
	{
		unsafe { vkCmdDispatchIndirect(self.ptr.native_ptr(), buffer.native_ptr(), offset as _) };
		return self;
	}
}

/// [feature = "Implements"] Transfer Commands: Copying resources
#[cfg(feature = "Implements")]
impl<'d> CmdRecord<'d>
{
	/// Copy data between buffer regions
	pub fn copy_buffer(&mut self, src: &Buffer, dst: &Buffer, regions: &[VkBufferCopy]) -> &mut Self
	{
		unsafe
		{
			vkCmdCopyBuffer(self.ptr.native_ptr(), src.native_ptr(),
				dst.native_ptr(), regions.len() as _, regions.as_ptr())
		};
		return self;
	}
	/// Copy data between images
	pub fn copy_image(&mut self, src: &Image, src_layout: ImageLayout,
		dst: &Image, dst_layout: ImageLayout, regions: &[VkImageCopy]) -> &mut Self
	{
		unsafe
		{
			vkCmdCopyImage(self.ptr.native_ptr(), src.native_ptr(), src_layout as _,
				dst.native_ptr(), dst_layout as _, regions.len() as _, regions.as_ptr())
		};
		return self;
	}
	/// Copy regions of an image, potentially performing format conversion
	pub fn blit_image(&mut self, src: &Image, src_layout: ImageLayout, dst: &Image, dst_layout: ImageLayout,
		regions: &[VkImageBlit], filter: FilterMode) -> &mut Self
	{
		unsafe
		{
			vkCmdBlitImage(self.ptr.native_ptr(), src.native_ptr(), src_layout as _, dst.native_ptr(), dst_layout as _,
				regions.len() as _, regions.as_ptr(), filter as _)
		};
		return self;
	}
	/// Copy data from a buffer into an image
	pub fn copy_buffer_to_image(&mut self, src_buffer: &Buffer, dst_image: &Image, dst_layout: ImageLayout,
		regions: &[VkBufferImageCopy]) -> &mut Self
	{
		unsafe
		{
			vkCmdCopyBufferToImage(self.ptr.native_ptr(), src_buffer.native_ptr(),
				dst_image.native_ptr(), dst_layout as _, regions.len() as _, regions.as_ptr())
		};
		return self;
	}
	/// Copy image data into a buffer
	pub fn copy_image_to_buffer(&mut self, src_image: &Image, src_layout: ImageLayout, dst_buffer: &Buffer,
		regions: &[VkBufferImageCopy]) -> &mut Self
	{
		unsafe
		{
			vkCmdCopyImageToBuffer(self.ptr.native_ptr(), src_image.native_ptr(), src_layout as _,
				dst_buffer.native_ptr(), regions.len() as _, regions.as_ptr())
		};
		return self;
	}
	/// Update a buffer's contents from host memory
	pub fn update_buffer<T>(&mut self, dst: &Buffer, dst_offset: usize, size: usize, data: &T) -> &mut Self
	{
		assert!(size <= size_of::<T>(), "Updated size exceeds size of datatype");
		unsafe
		{
			vkCmdUpdateBuffer(self.ptr.native_ptr(), dst.native_ptr(), dst_offset as _, size as _,
				data as *const T as *const _)
		};
		return self;
	}
}

/// [feature = "Implements"] Graphics/Compute Commands: Transfer-like(clearing/filling) commands
#[cfg(feature = "Implements")]
impl<'d> CmdRecord<'d>
{
	/// Fill a region of a buffer with a fixed value.  
	/// `size` is number of bytes to fill
	pub fn fill_buffer(&mut self, dst: &Buffer, dst_offset: usize, size: usize, data: u32) -> &mut Self
	{
		unsafe { vkCmdFillBuffer(self.ptr.native_ptr(), dst.native_ptr(), dst_offset as _, size as _, data) };
		return self;
	}
	/// Clear regions of a color image
	pub fn clear_color_image<T: ClearColorValue>(&mut self, image: &Image, layout: ImageLayout,
		color: &T, ranges: &[VkImageSubresourceRange]) -> &mut Self
	{
		unsafe
		{
			vkCmdClearColorImage(self.ptr.native_ptr(), image.native_ptr(), layout as _,
				color.represent(), ranges.len() as _, ranges.as_ptr())
		};
		return self;
	}
	/// Fill regions of a combined depth/stencil image
	pub fn clear_depth_stencil_image(&mut self, image: &Image, layout: ImageLayout, depth: f32, stencil: u32,
		ranges: &[VkImageSubresourceRange]) -> &mut Self
	{
		unsafe
		{
			vkCmdClearDepthStencilImage(self.ptr.native_ptr(), image.native_ptr(),
				layout as _, &VkClearDepthStencilValue { depth, stencil }, ranges.len() as _, ranges.as_ptr())
		};
		return self;
	}
	/// Clear regions within currently bound framebuffer attachments
	pub fn clear_attachments(&mut self, attachments: &[VkClearAttachment], rects: &[VkClearRect]) -> &mut Self
	{
		unsafe
		{
			vkCmdClearAttachments(self.ptr.native_ptr(), attachments.len() as _,
				attachments.as_ptr(), rects.len() as _, rects.as_ptr())
		};
		return self;
	}
}

/// [feature = "Implements"] Graphics Commands: Executing Subcommands
#[cfg(feature = "Implements")]
impl<'d> CmdRecord<'d>
{
	/// Execute a secondary command buffer from a primary command buffer
	/// # Safety
	/// 
	/// Caller must be primary buffer and in the render pass when executing secondary command buffer
	pub unsafe fn execute_commands(&mut self, buffers: &[VkCommandBuffer]) -> &mut Self
	{
		vkCmdExecuteCommands(self.ptr.native_ptr(), buffers.len() as _, buffers.as_ptr());
		return self;
	}
}

/// [feature = "Implements"] Graphics Commands: Resolving an image to another image
#[cfg(feature = "Implements")]
impl<'d> CmdRecord<'d>
{
	/// Resolve regions of an image
	pub fn resolve_image(&mut self, src: &Image, src_layout: ImageLayout, dst: &Image, dst_layout: ImageLayout,
		regions: &[VkImageResolve]) -> &mut Self
	{
		unsafe
		{
			vkCmdResolveImage(self.ptr.native_ptr(), src.native_ptr(), src_layout as _,
				dst.native_ptr(), dst_layout as _, regions.len() as _, regions.as_ptr())
		};
		return self;
	}
}

/// [feature = "Implements"] Graphics/Compute Commands: Synchronization between command buffers/queues
#[cfg(feature = "Implements")]
impl<'d> CmdRecord<'d>
{
	/// Set an event object to signaled state
	pub fn set_event(&mut self, event: &Event, stage_mask: PipelineStageFlags) -> &mut Self
	{
		unsafe { vkCmdSetEvent(self.ptr.native_ptr(), event.0, stage_mask.0) }; return self;
	}
	/// Reset an event object to non-signaled state
	pub fn reset_event(&mut self, event: &Event, stage_mask: PipelineStageFlags) -> &mut Self
	{
		unsafe { vkCmdResetEvent(self.ptr.native_ptr(), event.0, stage_mask.0) }; return self;
	}
	/// Wait for one or more events and insert a set of memory
	pub fn wait_events(&mut self, events: &[&Event],
		src_stage_mask: PipelineStageFlags, dst_stage_mask: PipelineStageFlags,
		memory_barriers: &[VkMemoryBarrier], buffer_memory_barriers: &[VkBufferMemoryBarrier],
		image_memory_barriers: &[VkImageMemoryBarrier]) -> &mut Self
	{
		let evs = events.into_iter().map(|x| x.0).collect::<Vec<_>>();
		unsafe
		{
			vkCmdWaitEvents(self.ptr.native_ptr(), evs.len() as _, evs.as_ptr(), src_stage_mask.0, dst_stage_mask.0,
				memory_barriers.len() as _, memory_barriers.as_ptr(),
				buffer_memory_barriers.len() as _, buffer_memory_barriers.as_ptr(),
				image_memory_barriers.len() as _, image_memory_barriers.as_ptr())
		};
		return self;
	}
	/// Insert a memory dependency
	pub fn pipeline_barrier(&mut self, src_stage_mask: PipelineStageFlags, dst_stage_mask: PipelineStageFlags,
		by_region: bool, memory_barriers: &[VkMemoryBarrier], buffer_memory_barriers: &[BufferMemoryBarrier],
		image_memory_barriers: &[ImageMemoryBarrier]) -> &mut Self
	{
		unsafe
		{
			vkCmdPipelineBarrier(self.ptr.native_ptr(), src_stage_mask.0, dst_stage_mask.0,
				if by_region { VK_DEPENDENCY_BY_REGION_BIT } else { 0 },
				memory_barriers.len() as _, memory_barriers.as_ptr(),
				buffer_memory_barriers.len() as _, buffer_memory_barriers.as_ptr() as _,
				image_memory_barriers.len() as _, image_memory_barriers.as_ptr() as _)
		};
		return self;
	}
}

/// [feature = "Implements"] Graphics/Compute Commands: Querying
#[cfg(feature = "Implements")]
impl<'d> CmdRecord<'d>
{
	/// Begin a query
	pub fn begin_query(&mut self, pool: &QueryPool, query: u32, precise_query: bool) -> &mut Self
	{
		unsafe
		{
			vkCmdBeginQuery(self.ptr.native_ptr(), pool.0, query,
				if precise_query { VK_QUERY_CONTROL_PRECISE_BIT } else { 0 })
		};
		return self;
	}
	/// Ends a query
	pub fn end_query(&mut self, pool: &QueryPool, query: u32) -> &mut Self
	{
		unsafe { vkCmdEndQuery(self.ptr.native_ptr(), pool.0, query) }; return self;
	}
	/// Reset queries in a query pool
	pub fn reset_query_pool(&mut self, pool: &QueryPool, range: Range<u32>) -> &mut Self
	{
		unsafe { vkCmdResetQueryPool(self.ptr.native_ptr(), pool.0, range.start, range.end - range.start) };
		return self;
	}
	/// Write a device timestamp into a query object
	pub fn write_timestamp(&mut self, stage: PipelineStageFlags, pool: &QueryPool, query: u32) -> &mut Self
	{
		unsafe { vkCmdWriteTimestamp(self.ptr.native_ptr(), stage.0, pool.0, query) }; return self;
	}
	/// Copy the results of queries in a query pool to a buffer object
	pub fn copy_query_pool_results(&mut self, pool: &QueryPool, range: Range<u32>, dst: &Buffer, dst_offset: usize,
		stride: usize, wide_result: bool, flags: QueryResultFlags) -> &mut Self
	{
		unsafe
		{
			vkCmdCopyQueryPoolResults(self.ptr.native_ptr(), pool.0, range.start, range.end - range.start,
				dst.native_ptr(), dst_offset as _, stride as _,
				flags.0 | if wide_result { VK_QUERY_RESULT_64_BIT } else { 0 })
		};
		return self;
	}
}

/// [feature = "Implements"] Graphics Commands: Manipulating with Render Passes
#[cfg(feature = "Implements")]
impl<'d> CmdRecord<'d>
{
	/// Begin a new render pass
	pub fn begin_render_pass(&mut self, pass: &RenderPass, framebuffer: &Framebuffer, render_area: VkRect2D,
		clear_values: &[ClearValue], inline_commands: bool) -> &mut Self
	{
		let cvalues = clear_values.into_iter().map(|x| match x
		{
			&ClearValue::Color(ref color) => VkClearValue { color: VkClearColorValue { float32: color.clone() } },
			&ClearValue::DepthStencil(depth, stencil) =>
				VkClearValue { depthStencil: VkClearDepthStencilValue { depth, stencil } }
		}).collect::<Vec<_>>();
		let binfo = VkRenderPassBeginInfo
		{
			renderPass: pass.native_ptr(), framebuffer: framebuffer.native_ptr(), renderArea: render_area,
			clearValueCount: cvalues.len() as _, pClearValues: cvalues.as_ptr(), .. Default::default()
		};
		unsafe
		{
			vkCmdBeginRenderPass(self.ptr.native_ptr(), &binfo,
				if inline_commands { VK_SUBPASS_CONTENTS_INLINE } else { VK_SUBPASS_CONTENTS_SECONDARY_COMMAND_BUFFERS })
		};
		return self;
	}
	/// Transition to the next subpass of a render pass
	pub fn next_subpass(&mut self, inline_commands: bool) -> &mut Self
	{
		unsafe
		{
			vkCmdNextSubpass(self.ptr.native_ptr(),
				if inline_commands { VK_SUBPASS_CONTENTS_INLINE } else { VK_SUBPASS_CONTENTS_SECONDARY_COMMAND_BUFFERS })
		};
		return self;
	}
	/// End the current render pass
	pub fn end_render_pass(&mut self) -> &mut Self { unsafe { vkCmdEndRenderPass(self.ptr.native_ptr()) }; return self; }
}

/// The trait representation of `VkClearColorValue`
pub trait ClearColorValue
{
	fn represent(&self) -> &VkClearColorValue;
}
impl ClearColorValue for [f32; 4] { fn represent(&self) -> &VkClearColorValue { unsafe { ::std::mem::transmute(self) } } }
impl ClearColorValue for [i32; 4] { fn represent(&self) -> &VkClearColorValue { unsafe { ::std::mem::transmute(self) } } }
impl ClearColorValue for [u32; 4] { fn represent(&self) -> &VkClearColorValue { unsafe { ::std::mem::transmute(self) } } }

/// The enum representation of `VkClearValue`
pub enum ClearValue
{
	/// Color Value: r, g, b, a
	Color([f32; 4]),
	/// Depth and Stencil Value: depth, stencil
	DepthStencil(f32, u32)
}

/// Type of index buffer indices
#[repr(C)] #[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub enum IndexType
{
	/// Indices are 16-bit unsigned integer values
	U16 = VK_INDEX_TYPE_UINT16 as _,
	/// Indices are 32-bit unsigned integer values
	U32 = VK_INDEX_TYPE_UINT32 as _
}

/// Enabling or disabling the occlusion query
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub enum OcclusionQuery
{
	Disable, Enable,
	/// `VK_QUERY_CONTROL_PRECISE_BIT`
	Precise
}

/// Access Types
pub struct AccessFlags { pub read: VkAccessFlags, pub write: VkAccessFlags }
impl AccessFlags
{
	/// Specifies read access to an indirect command structure read as part of an indirect drawing or dispatch command.
	pub const INDIRECT_COMMAND_READ: VkAccessFlags = VK_ACCESS_INDIRECT_COMMAND_READ_BIT;
	/// Specifies read access to an index buffer as part of an indexed drawing command, bound by `vkCmdBindIndexBuffer`.
	pub const INDEX_READ: VkAccessFlags = VK_ACCESS_INDEX_READ_BIT;
	/// Specifies read access to a vertex buffer as part of a drawing command, bound by `vkCmdBindVertexBuffers`.
	pub const VERTEX_ATTRIBUTE_READ: VkAccessFlags = VK_ACCESS_VERTEX_ATTRIBUTE_READ_BIT;
	/// Specifies read access to a [uniform buffer](https://www.khronos.org/registry/vulkan/specs/1.0/html/vkspec.html#descriptorsets-uniformbuffer).
	pub const UNIFORM_READ: VkAccessFlags = VK_ACCESS_UNIFORM_READ_BIT;
	/// Specifies read access to an [input attachment](https://www.khronos.org/registry/vulkan/specs/1.0/html/vkspec.html#renderpass) within a render pass during fragment shading.
	pub const INPUT_ATTACHMENT_READ: VkAccessFlags = VK_ACCESS_INPUT_ATTACHMENT_READ_BIT;
	/// Specifies read/write access to a [storage buffer](https://www.khronos.org/registry/vulkan/specs/1.0/html/vkspec.html#descriptorsets-storagebuffer),
	/// [uniform texel buffer](https://www.khronos.org/registry/vulkan/specs/1.0/html/vkspec.html#descriptorsets-uniformtexelbuffer)(read only),
	/// [storage texel buffer](https://www.khronos.org/registry/vulkan/specs/1.0/html/vkspec.html#descriptorsets-storagetexelbuffer),
	/// [samples image](https://www.khronos.org/registry/vulkan/specs/1.0/html/vkspec.html#descriptorsets-sampledimage)(read only),
	/// or [storage image](https://www.khronos.org/registry/vulkan/specs/1.0/html/vkspec.html#descriptorsets-storageimage).
	pub const SHADER: Self = AccessFlags { read: VK_ACCESS_SHADER_READ_BIT, write: VK_ACCESS_SHADER_WRITE_BIT };
	/// - `read`: Specifies read access to a [color attachment](https://www.khronos.org/registry/vulkan/specs/1.0/html/vkspec.html#renderpass),
	///   such as via [blending](https://www.khronos.org/registry/vulkan/specs/1.0/html/vkspec.html#framebuffer-blending),
	///   [logic operations](https://www.khronos.org/registry/vulkan/specs/1.0/html/vkspec.html#framebuffer-logicop),
	///   or via certain [subpass load operations](https://www.khronos.org/registry/vulkan/specs/1.0/html/vkspec.html#framebuffer-logicop).
	/// - `write`: specifies write access to a [color or resolve attachment](https://www.khronos.org/registry/vulkan/specs/1.0/html/vkspec.html#renderpass)
	///   during a [render pass](https://www.khronos.org/registry/vulkan/specs/1.0/html/vkspec.html#renderpass)
	///   or via certain [subpass load and store operations](https://www.khronos.org/registry/vulkan/specs/1.0/html/vkspec.html#renderpass-load-store-ops).
	pub const COLOR_ATTACHMENT: Self = AccessFlags
	{
		read: VK_ACCESS_COLOR_ATTACHMENT_READ_BIT, write: VK_ACCESS_COLOR_ATTACHMENT_WRITE_BIT
	};
	/// - `read`: Specifies read access to a [depth/stencil attachment](https://www.khronos.org/registry/vulkan/specs/1.0/html/vkspec.html#renderpass),
	///   via [depth or stencil operations](https://www.khronos.org/registry/vulkan/specs/1.0/html/vkspec.html#fragops-ds-state)
	///   or via certain [subpass load operations](https://www.khronos.org/registry/vulkan/specs/1.0/html/vkspec.html#renderpass-load-store-ops).
	/// - `write`: Specifies write access to a [depth/stencil attachment](https://www.khronos.org/registry/vulkan/specs/1.0/html/vkspec.html#renderpass),
	///   via [depth or stencil operations](https://www.khronos.org/registry/vulkan/specs/1.0/html/vkspec.html#fragops-ds-state)
	///   or via certain [subpass load and store operations](https://www.khronos.org/registry/vulkan/specs/1.0/html/vkspec.html#renderpass-load-store-ops).
	pub const DEPTH_STENCIL_ATTACHMENT: Self = AccessFlags
	{
		read: VK_ACCESS_DEPTH_STENCIL_ATTACHMENT_READ_BIT, write: VK_ACCESS_DEPTH_STENCIL_ATTACHMENT_WRITE_BIT
	};
	/// Specifies read/write access to an image or buffer in a [clear](https://www.khronos.org/registry/vulkan/specs/1.0/html/vkspec.html#clears)(write only)
	/// or [copy](https://www.khronos.org/registry/vulkan/specs/1.0/html/vkspec.html#copies) operation.
	pub const TRANSFER: Self = AccessFlags { read: VK_ACCESS_TRANSFER_READ_BIT, write: VK_ACCESS_TRANSFER_WRITE_BIT };
	/// Specifies read/write access by a host operation.
	/// Accesses of this type are not performed through a resource, but directly on memory.
	pub const HOST: Self = AccessFlags { read: VK_ACCESS_HOST_READ_BIT, write: VK_ACCESS_HOST_WRITE_BIT };
	/// Specifies read/write access via non-specific entities.
	/// These entities include the Vulkan device and host, but *may* also include entities external to the Vulkan device
	/// or otherwise not part of the core Vulkan pipeline.
	/// 
	/// - When the `write` mask included in a source access mask, all writes that are performed by entities known to the
	///   Vulkan device are made available.
	/// - When included in a destination access mask, makes all available writes visible to all future read accesses on
	///   entities known to the Vulkan device.
	pub const MEMORY: Self = AccessFlags { read: VK_ACCESS_MEMORY_READ_BIT, write: VK_ACCESS_MEMORY_WRITE_BIT };
}

use std::mem::replace;
/// Image Subresource Slice
#[derive(Clone)]
pub struct ImageSubref<'d>(pub &'d Image, pub VkImageSubresourceRange);
impl<'d> ImageSubref<'d>
{
	/// Construct a slice for the Color aspect(`VK_IMAGE_ASPECT_COLOR_BIT`)
	pub fn color<Levels, Layers>(image: &'d Image, mip_levels: Levels, array_layers: Layers) -> Self
		where Levels: ::AnalogNumRange<u32>, Layers: ::AnalogNumRange<u32>
	{
		ImageSubref(image, VkImageSubresourceRange
		{
			aspectMask: VK_IMAGE_ASPECT_COLOR_BIT,
			baseMipLevel: mip_levels.begin(), baseArrayLayer: array_layers.begin(),
			levelCount: mip_levels.count(), layerCount: array_layers.count()
		})
	}
	/// Construct a slice for the Stencil aspect(`VK_IMAGE_ASPECT_STENCIL_BIT`)
	pub fn stencil<Levels, Layers>(image: &'d Image, mip_levels: Levels, array_layers: Layers) -> Self
		where Levels: ::AnalogNumRange<u32>, Layers: ::AnalogNumRange<u32>
	{
		ImageSubref(image, VkImageSubresourceRange
		{
			aspectMask: VK_IMAGE_ASPECT_STENCIL_BIT,
			baseMipLevel: mip_levels.begin(), baseArrayLayer: array_layers.begin(),
			levelCount: mip_levels.count(), layerCount: array_layers.count()
		})
	}
}

/// Wrapper object of `VkImageMemoryBarrier`, derscribes a memory barrier of an image.
#[derive(Clone)]
pub struct ImageMemoryBarrier(VkImageMemoryBarrier);
impl ImageMemoryBarrier
{
	/// Construct a new barrier descriptor
	pub fn new(img: &ImageSubref, old_layout: ImageLayout, new_layout: ImageLayout) -> Self
	{
		ImageMemoryBarrier(VkImageMemoryBarrier
		{
			image: img.0.native_ptr(), subresourceRange: img.1.clone(),
			oldLayout: old_layout as _, newLayout: new_layout as _,
			srcAccessMask: old_layout.default_access_mask(),
			dstAccessMask: new_layout.default_access_mask(), .. Default::default()
		})
	}
	/// Construct a new barrier descriptor from discrete pair of resource and subresource range
	pub fn new_raw<SR>(res: &Image, subres: &SR, old: ImageLayout, new: ImageLayout) -> Self
		where SR: Borrow<VkImageSubresourceRange>
	{
		ImageMemoryBarrier(VkImageMemoryBarrier
		{
			image: res.native_ptr(), subresourceRange: subres.borrow().clone(),
			oldLayout: old as _, newLayout: new as _,
			srcAccessMask: old.default_access_mask(), dstAccessMask: new.default_access_mask(), .. Default::default()
		})
	}
	/// Update the source access mask
	pub fn src_access_mask(mut self, mask: VkAccessFlags) -> Self
	{
		self.0.srcAccessMask = mask; return self;
	}
	/// Update the destination access mask
	pub fn dest_access_mask(mut self, mask: VkAccessFlags) -> Self
	{
		self.0.dstAccessMask = mask; return self;
	}
	/// Flip access masks and image layouts
	pub fn flip(mut self) -> Self
	{
		self.0.dstAccessMask = replace(&mut self.0.srcAccessMask, self.0.dstAccessMask);
		self.0.newLayout = replace(&mut self.0.oldLayout, self.0.newLayout);
		return self;
	}
}
/// Wrapper object of `VkBufferMemoryBarrier`, describes a memory barrier of a buffer.
#[derive(Clone)]
pub struct BufferMemoryBarrier(VkBufferMemoryBarrier);
impl BufferMemoryBarrier
{
	/// Construct a new buffer descriptor
	pub fn new(buf: &Buffer, range: Range<usize>, src_access_mask: VkAccessFlags, dst_access_mask: VkAccessFlags)
		-> Self
	{
		BufferMemoryBarrier(VkBufferMemoryBarrier
		{
			buffer: buf.native_ptr(), offset: range.start as _, size: (range.end - range.start) as _,
			srcAccessMask: src_access_mask, dstAccessMask: dst_access_mask, .. Default::default()
		})
	}
	/// Update the source access mask
	pub fn src_access_mask(mut self, mask: VkAccessFlags) -> Self
	{
		self.0.srcAccessMask = mask; return self;
	}
	/// Update the destination access mask
	pub fn dest_access_mask(mut self, mask: VkAccessFlags) -> Self
	{
		self.0.dstAccessMask = mask; return self;
	}
	/// Flip access masks
	pub fn flip(mut self) -> Self
	{
		self.0.dstAccessMask = replace(&mut self.0.srcAccessMask, self.0.dstAccessMask);
		return self;
	}
}