byte-engine-ghi 0.1.0

Graphics hardware interface layer used by Byte-Engine.
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
use crate::{BaseBufferHandle, BufferHandle};

#[derive(Clone, Copy, PartialEq, Eq, Hash, Debug)]
/// A resource layout required by GPU work.
pub enum Layouts {
	/// No specific layout is required.
	Undefined,
	/// The image will be used as render target.
	RenderTarget,
	/// The resource will be used in a transfer operation.
	Transfer,
	/// The resource will be used as a presentation source.
	Present,
	/// The resource will be used as a read only sample source.
	Read,
	/// The resource will be used as a read/write storage.
	General,
	/// The resource will be used as a shader binding table.
	ShaderBindingTable,
	/// Indirect.
	Indirect,
}

#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
/// A texture filtering mode used by samplers.
pub enum FilteringModes {
	/// Closest mode filtering. Rounds floating point coordinates to the nearest pixel.
	Closest,
	/// Linear mode filtering. Blends samples linearly across neighbouring pixels.
	Linear,
}

#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
/// A rule for combining neighboring texels during image sampling.
pub enum SamplingReductionModes {
	/// The average of the samples. Weighted by the proximity of the sample to the sample point.
	WeightedAverage,
	/// The minimum of the samples is taken.
	Min,
	/// The maximum of the samples is taken.
	Max,
}

#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
/// A sampler rule for coordinates outside an image.
pub enum SamplerAddressingModes {
	/// Repeat mode addressing.
	Repeat,
	/// Mirror mode addressing.
	Mirror,
	/// Clamp mode addressing.
	Clamp,
	/// Border mode addressing.
	Border {},
}

#[derive(Clone, Copy, PartialEq, Eq, Hash, Debug)]
pub enum UseCases {
	STATIC,
	DYNAMIC,
}

bitflags::bitflags! {
	#[derive(Clone, Copy, PartialEq, Eq, PartialOrd, Ord, Hash, Debug)]
	/// Bit flags for the available resource uses.
	pub struct Uses : u32 {
		/// Resource will be used as a vertex buffer.
		const Vertex = 1 << 0;
		/// Resource will be used as an index buffer.
		const Index = 1 << 1;
		/// Resource will be used as a uniform buffer.
		const Uniform = 1 << 2;
		/// Resource will be used as a storage buffer.
		const Storage = 1 << 3;
		/// Resource will be used as an indirect buffer.
		const Indirect = 1 << 4;
		/// Resource will be used as an image.
		const Image = 1 << 5;
		/// Resource will be used as a render target.
		const RenderTarget = 1 << 6;
		/// Resource will be used as an input attachment.
		const InputAttachment = 1 << 15;
		/// Resource will be used as a depth stencil.
		const DepthStencil = 1 << 7;
		/// Resource will be used as an acceleration structure.
		const AccelerationStructure = 1 << 8;
		/// Resource will be used as a transfer source.
		const TransferSource = 1 << 9;
		/// Resource will be used as a transfer destination.
		const TransferDestination = 1 << 10;
		/// Resource will be used as a shader binding table.
		const ShaderBindingTable = 1 << 11;
		/// The resource is acceleration-structure build scratch storage.
		const AccelerationStructureBuildScratch = 1 << 12;

		const AccelerationStructureBuild = 1 << 13;

		const Clear = 1 << 14;

		/// Resource will be used as a source for a blit operation.
		const BlitSource = 1 << 9;
		/// Resource will be used as a destination for a blit operation.
		const BlitDestination = 1 << 10;
	}
}

bitflags::bitflags! {
	#[derive(Clone, Copy, PartialEq, Eq, Hash, Debug)]
	/// Bit flags for the available pipeline stages.
	pub struct Stages : u64 {
		/// No stage.
		const NONE = 0b0;
		/// The vertex stage.
		const VERTEX = 1 << 1;
		const INDEX = 1 << 2;
		/// The task stage.
		const TASK = 1 << 3;
		/// The mesh shader execution stage.
		const MESH = 1 << 4;
		/// The fragment stage.
		const FRAGMENT = 1 << 5;
		/// The compute stage.
		const COMPUTE = 1 << 6;
		/// The transfer stage.
		const TRANSFER = 1 << 7;
		/// The presentation stage.
		const PRESENTATION = 1 << 8;
		/// The host stage.
		const HOST = 1 << 9;
		/// The shader write stage.
		const SHADER_WRITE = 1 << 10;
		/// The ray generation stage.
		const RAYGEN = 1 << 11;
		/// The closest hit stage.
		const CLOSEST_HIT = 1 << 12;
		/// The any hit stage.
		const ANY_HIT = 1 << 13;
		/// The intersection stage.
		const INTERSECTION = 1 << 14;
		/// The miss stage.
		const MISS = 1 << 15;
		/// The callable stage.
		const CALLABLE = 1 << 16;
		/// The acceleration structure build stage.
		const ACCELERATION_STRUCTURE_BUILD = 1 << 17;
		/// The last or bottom stage.
		const LAST = 1 << 63;
	}
}

#[derive(PartialEq, Eq, Clone, Copy, Debug)]
/// A pixel format supported by GHI images.
pub enum Formats {
	/// 8 bit unsigned per component floating point R.
	R8F,
	/// 8 bit unsigned normalized R.
	R8UNORM,
	/// 8 bit signed normalized R.
	R8SNORM,
	/// 8 bit sRGB R.
	R8sRGB,

	/// 16 bit unsigned per component floating point R.
	R16F,
	/// 16 bit unsigned normalized R.
	R16UNORM,
	/// 16 bit signed normalized R.
	R16SNORM,
	/// 16 bit sRGB R.
	R16sRGB,

	/// 32 bit unsigned per component floating point R.
	R32F,
	/// 32 bit unsigned normalized R.
	R32UNORM,
	/// 32 bit signed normalized R.
	R32SNORM,
	/// 32 bit sRGB R.
	R32sRGB,

	/// 8 bit unsigned per component floating point RG.
	RG8F,
	/// 8 bit unsigned normalized RG.
	RG8UNORM,
	/// 8 bit signed normalized RG.
	RG8SNORM,
	/// 8 bit sRGB RG.
	RG8sRGB,

	/// 16 bit unsigned per component floating point RG.
	RG16F,
	/// 16 bit unsigned normalized RG.
	RG16UNORM,
	/// 16 bit signed normalized RG.
	RG16SNORM,
	/// 16 bit sRGB RG.
	RG16sRGB,

	/// 8 bit unsigned per component floating point RGB.
	RGB8F,
	/// 8 bit unsigned normalized RGB.
	RGB8UNORM,
	/// 8 bit signed normalized RGB.
	RGB8SNORM,
	/// 8 bit sRGB RGB.
	RGB8sRGB,

	/// 16 bit unsigned per component floating point RGB.
	RGB16F,
	/// 16 bit unsigned normalized RGB.
	RGB16UNORM,
	/// 16 bit signed normalized RGB.
	RGB16SNORM,
	/// 16 bit sRGB RGB.
	RGB16sRGB,

	/// 8 bit unsigned per component floating point RGBA.
	RGBA8F,
	/// 8 bit unsigned normalized RGBA.
	RGBA8UNORM,
	/// 8 bit signed normalized RGBA.
	RGBA8SNORM,
	/// 8 bit sRGB RGBA.
	RGBA8sRGB,

	/// 16 bit unsigned per component floating point RGBA.
	RGBA16F,
	/// 16 bit unsigned normalized RGBA.
	RGBA16UNORM,
	/// 16 bit signed normalized RGBA.
	RGBA16SNORM,
	/// 16 bit sRGB RGBA.
	RGBA16sRGB,

	/// 11 bit unsigned for R, G and 10 bit unsigned for B normalized RGB.
	RGBu11u11u10,
	/// 8 bit unsigned per component normalized BGRA.
	BGRAu8,
	/// 8 bit sRGB RGBA.
	BGRAsRGB,
	/// 32 bit float depth.
	Depth32,
	/// 32 bit unsigned integer.
	U32,
	/// BC5 block compressed format (unsigned normalized).
	BC5,
	/// BC5 block compressed format (signed normalized) for normal maps.
	BC5SNORM,
	/// BC7 block compressed format.
	BC7,
	/// BC7 block compressed sRGB format.
	BC7SRGB,
}

/// The `BcLayout` struct defines the compact block layout for one BC-compressed image level.
#[derive(Clone, Copy, Debug, PartialEq, Eq)]
pub struct BcLayout {
	pub blocks_w: u32,
	pub blocks_h: u32,
	pub bytes_per_block: u32,
	pub bytes_per_row: u32,
	pub bytes_per_image: u32,
}

/// Computes the compact 4x4 block layout for one BC-compressed image level.
pub fn bc_layout(width: u32, height: u32, bytes_per_block: u32) -> BcLayout {
	let blocks_w = width.max(1).div_ceil(4).max(1);
	let blocks_h = height.max(1).div_ceil(4).max(1);
	let bytes_per_row = blocks_w * bytes_per_block;
	let bytes_per_image = bytes_per_row * blocks_h;

	BcLayout {
		blocks_w,
		blocks_h,
		bytes_per_block,
		bytes_per_row,
		bytes_per_image,
	}
}

impl Formats {
	/// Returns the byte size of one compressed block for BC formats.
	pub fn bc_bytes_per_block(&self) -> Option<u32> {
		match self {
			Formats::BC5 | Formats::BC5SNORM | Formats::BC7 | Formats::BC7SRGB => Some(16),
			_ => None,
		}
	}

	/// Computes the compact BC layout for this format and image level.
	pub fn bc_layout(&self, width: u32, height: u32) -> Option<BcLayout> {
		Some(bc_layout(width, height, self.bc_bytes_per_block()?))
	}

	/// Returns compact row bytes, row count, and image bytes for one texture level.
	pub fn compact_copy_layout(&self, width: u32, height: u32) -> (usize, usize, usize) {
		if let Some(layout) = self.bc_layout(width, height) {
			return (
				layout.bytes_per_row as usize,
				layout.blocks_h as usize,
				layout.bytes_per_image as usize,
			);
		}

		let bytes_per_row = width as usize * self.size();
		let row_count = height as usize;
		(bytes_per_row, row_count, bytes_per_row * row_count)
	}

	/// Returns the encoding of the format.
	pub fn encoding(&self) -> Option<Encodings> {
		match self {
			Formats::R8F
			| Formats::R16F
			| Formats::R32F
			| Formats::RG8F
			| Formats::RG16F
			| Formats::RGB8F
			| Formats::RGB16F
			| Formats::RGBA8F
			| Formats::RGBA16F
			| Formats::Depth32 => Some(Encodings::FloatingPoint),

			Formats::R8UNORM
			| Formats::R16UNORM
			| Formats::R32UNORM
			| Formats::RG8UNORM
			| Formats::RG16UNORM
			| Formats::RGB8UNORM
			| Formats::RGB16UNORM
			| Formats::RGBA8UNORM
			| Formats::RGBA16UNORM
			| Formats::RGBu11u11u10
			| Formats::BGRAu8 => Some(Encodings::UnsignedNormalized),

			Formats::R8SNORM
			| Formats::R16SNORM
			| Formats::R32SNORM
			| Formats::RG8SNORM
			| Formats::RG16SNORM
			| Formats::RGB8SNORM
			| Formats::RGB16SNORM
			| Formats::RGBA8SNORM
			| Formats::RGBA16SNORM => Some(Encodings::SignedNormalized),

			Formats::R8sRGB
			| Formats::R16sRGB
			| Formats::R32sRGB
			| Formats::RG8sRGB
			| Formats::RG16sRGB
			| Formats::RGB8sRGB
			| Formats::RGB16sRGB
			| Formats::RGBA8sRGB
			| Formats::RGBA16sRGB
			| Formats::BGRAsRGB => Some(Encodings::sRGB),

			Formats::BC7SRGB => Some(Encodings::sRGB),

			Formats::BC5SNORM => Some(Encodings::SignedNormalized),

			Formats::U32 | Formats::BC5 | Formats::BC7 => None,
		}
	}

	/// Returns the channel bit size of the format.
	pub fn channel_bit_size(&self) -> ChannelBitSize {
		match self {
			Formats::R8F
			| Formats::R8UNORM
			| Formats::R8SNORM
			| Formats::R8sRGB
			| Formats::RG8F
			| Formats::RG8UNORM
			| Formats::RG8SNORM
			| Formats::RG8sRGB
			| Formats::RGB8F
			| Formats::RGB8UNORM
			| Formats::RGB8SNORM
			| Formats::RGB8sRGB
			| Formats::RGBA8F
			| Formats::RGBA8UNORM
			| Formats::RGBA8SNORM
			| Formats::RGBA8sRGB
			| Formats::BGRAu8
			| Formats::BGRAsRGB => ChannelBitSize::Bits8,

			Formats::R16F
			| Formats::R16UNORM
			| Formats::R16SNORM
			| Formats::R16sRGB
			| Formats::RG16F
			| Formats::RG16UNORM
			| Formats::RG16SNORM
			| Formats::RG16sRGB
			| Formats::RGB16F
			| Formats::RGB16UNORM
			| Formats::RGB16SNORM
			| Formats::RGB16sRGB
			| Formats::RGBA16F
			| Formats::RGBA16UNORM
			| Formats::RGBA16SNORM
			| Formats::RGBA16sRGB => ChannelBitSize::Bits16,

			Formats::R32F | Formats::R32UNORM | Formats::R32SNORM | Formats::R32sRGB | Formats::Depth32 | Formats::U32 => {
				ChannelBitSize::Bits32
			}

			Formats::RGBu11u11u10 => ChannelBitSize::Bits11_11_10,

			Formats::BC5 | Formats::BC5SNORM | Formats::BC7 | Formats::BC7SRGB => ChannelBitSize::Compressed,
		}
	}

	/// Returns the channel layout of the format.
	pub fn channel_layout(&self) -> ChannelLayout {
		match self {
			Formats::R8F
			| Formats::R8UNORM
			| Formats::R8SNORM
			| Formats::R8sRGB
			| Formats::R16F
			| Formats::R16UNORM
			| Formats::R16SNORM
			| Formats::R16sRGB
			| Formats::R32F
			| Formats::R32UNORM
			| Formats::R32SNORM
			| Formats::R32sRGB => ChannelLayout::R,

			Formats::RG8F
			| Formats::RG8UNORM
			| Formats::RG8SNORM
			| Formats::RG8sRGB
			| Formats::RG16F
			| Formats::RG16UNORM
			| Formats::RG16SNORM
			| Formats::RG16sRGB => ChannelLayout::RG,

			Formats::RGB8F
			| Formats::RGB8UNORM
			| Formats::RGB8SNORM
			| Formats::RGB8sRGB
			| Formats::RGB16F
			| Formats::RGB16UNORM
			| Formats::RGB16SNORM
			| Formats::RGB16sRGB
			| Formats::RGBu11u11u10 => ChannelLayout::RGB,

			Formats::RGBA8F
			| Formats::RGBA8UNORM
			| Formats::RGBA8SNORM
			| Formats::RGBA8sRGB
			| Formats::RGBA16F
			| Formats::RGBA16UNORM
			| Formats::RGBA16SNORM
			| Formats::RGBA16sRGB => ChannelLayout::RGBA,

			Formats::BGRAu8 | Formats::BGRAsRGB => ChannelLayout::BGRA,

			Formats::Depth32 => ChannelLayout::Depth,

			Formats::U32 => ChannelLayout::Packed,

			Formats::BC5 | Formats::BC5SNORM | Formats::BC7 | Formats::BC7SRGB => ChannelLayout::BC,
		}
	}
}

pub trait Size {
	fn size(&self) -> usize;
}

impl Size for Formats {
	fn size(&self) -> usize {
		match self {
			Formats::R8F | Formats::R8UNORM | Formats::R8SNORM | Formats::R8sRGB => 1,
			Formats::R16F | Formats::R16UNORM | Formats::R16SNORM | Formats::R16sRGB => 2,
			Formats::R32F | Formats::R32UNORM | Formats::R32SNORM | Formats::R32sRGB => 4,
			Formats::RG8F | Formats::RG8UNORM | Formats::RG8SNORM | Formats::RG8sRGB => 2,
			Formats::RG16F | Formats::RG16UNORM | Formats::RG16SNORM | Formats::RG16sRGB => 4,
			Formats::RGB8F | Formats::RGB8UNORM | Formats::RGB8SNORM | Formats::RGB8sRGB => 3,
			Formats::RGB16F | Formats::RGB16UNORM | Formats::RGB16SNORM | Formats::RGB16sRGB => 6,
			Formats::RGBA8F | Formats::RGBA8UNORM | Formats::RGBA8SNORM | Formats::RGBA8sRGB => 4,
			Formats::RGBA16F | Formats::RGBA16UNORM | Formats::RGBA16SNORM | Formats::RGBA16sRGB => 8,
			Formats::RGBu11u11u10 => 4,
			Formats::BGRAu8 | Formats::BGRAsRGB => 4,
			Formats::Depth32 => 4,
			Formats::U32 => 4,
			Formats::BC5 | Formats::BC5SNORM => 1,
			Formats::BC7 | Formats::BC7SRGB => 1,
		}
	}
}

#[derive(Clone, Copy, Debug)]
pub enum CompressionSchemes {
	BC5,
	BC7,
}

bitflags::bitflags! {
	#[derive(Clone, Copy, PartialEq, Eq, Hash, Debug)]
	/// Bit flags for the available access policies.
	pub struct AccessPolicies : u8 {
		/// No access.
		const NONE = 0b00000000;
		/// Read access.
		const READ = 0b00000001;
		/// Write access.
		const WRITE = 0b00000010;
		/// Read and write access.
		const READ_WRITE = Self::READ.bits() | Self::WRITE.bits();
	}
}

/// A primitive data type shared by GPU resources and shaders.
#[derive(Hash, Clone, Copy, PartialEq, Eq)]
pub enum DataTypes {
	Float,
	Float2,
	Float3,
	Float4,
	U8,
	U16,
	U32,
	Int,
	Int2,
	Int3,
	Int4,
	UInt,
	UInt2,
	UInt3,
	UInt4,
}

impl DataTypes {
	pub fn size(self) -> usize {
		match self {
			DataTypes::Float => std::mem::size_of::<f32>(),
			DataTypes::Float2 => std::mem::size_of::<f32>() * 2,
			DataTypes::Float3 => std::mem::size_of::<f32>() * 3,
			DataTypes::Float4 => std::mem::size_of::<f32>() * 4,
			DataTypes::U8 => std::mem::size_of::<u8>(),
			DataTypes::U16 => std::mem::size_of::<u16>(),
			DataTypes::U32 => std::mem::size_of::<u32>(),
			DataTypes::Int => std::mem::size_of::<i32>(),
			DataTypes::Int2 => std::mem::size_of::<i32>() * 2,
			DataTypes::Int3 => std::mem::size_of::<i32>() * 3,
			DataTypes::Int4 => std::mem::size_of::<i32>() * 4,
			DataTypes::UInt => std::mem::size_of::<u32>(),
			DataTypes::UInt2 => std::mem::size_of::<u32>() * 2,
			DataTypes::UInt3 => std::mem::size_of::<u32>() * 3,
			DataTypes::UInt4 => std::mem::size_of::<u32>() * 4,
		}
	}
}

impl Size for DataTypes {
	fn size(&self) -> usize {
		(*self).size()
	}
}

bitflags::bitflags! {
	#[derive(Clone, Copy, PartialEq, Eq, PartialOrd, Ord, Hash, Debug)]
	pub struct DeviceAccesses: u16 {
		const CpuRead = 1 << 0;
		const CpuWrite = 1 << 1;
		const GpuRead = 1 << 2;
		const GpuWrite = 1 << 3;

		const DeviceOnly = 1 << 2 | 1 << 3;
		const HostOnly = 1 << 0 | 1 << 1;
		const HostToDevice = 1 << 1 | 1 << 2;
		const DeviceToHost = 1 << 0 | 1 << 3;
	}
}

/// A programmable shader stage.
#[derive(Clone, Copy, Debug)]
pub enum ShaderTypes {
	/// A vertex shader.
	Vertex,
	/// A fragment shader.
	Fragment,
	/// A compute shader.
	Compute,
	Task,
	Mesh,
	RayGen,
	ClosestHit,
	AnyHit,
	Intersection,
	Miss,
	Callable,
}

impl From<ShaderTypes> for Stages {
	fn from(ty: ShaderTypes) -> Self {
		match ty {
			ShaderTypes::Vertex => Self::VERTEX,
			ShaderTypes::Fragment => Self::FRAGMENT,
			ShaderTypes::Compute => Self::COMPUTE,
			ShaderTypes::Task => Self::TASK,
			ShaderTypes::Mesh => Self::MESH,
			ShaderTypes::RayGen => Self::RAYGEN,
			ShaderTypes::ClosestHit => Self::CLOSEST_HIT,
			ShaderTypes::AnyHit => Self::ANY_HIT,
			ShaderTypes::Intersection => Self::INTERSECTION,
			ShaderTypes::Miss => Self::MISS,
			ShaderTypes::Callable => Self::CALLABLE,
		}
	}
}

#[derive(PartialEq, Eq, Clone, Copy, Debug)]
pub enum Encodings {
	FloatingPoint,
	UnsignedNormalized,
	SignedNormalized,
	#[allow(non_camel_case_types)]
	sRGB,
}

#[derive(PartialEq, Eq, Clone, Copy, Debug)]
/// The channel order in a pixel format.
pub enum ChannelLayout {
	/// Single channel (R).
	R,
	/// Two channels (RG).
	RG,
	/// Three channels (RGB).
	RGB,
	/// Four channels (RGBA).
	RGBA,
	/// Four channels in BGRA order.
	BGRA,
	/// Special packed format.
	Packed,
	/// Depth channel.
	Depth,
	/// Block compressed format.
	BC,
}

#[derive(PartialEq, Eq, Clone, Copy, Debug)]
/// The bit width of each channel in a pixel format.
pub enum ChannelBitSize {
	/// 8 bits per channel.
	Bits8,
	/// 16 bits per channel.
	Bits16,
	/// 32 bits per channel.
	Bits32,
	/// Special case: 11 bits for R and G, 10 bits for B.
	Bits11_11_10,
	/// Block compressed format (variable bit size).
	Compressed,
}

/// The `BufferCopyDescriptor` struct configures one byte-range copy between buffers.
pub struct BufferCopyDescriptor {
	pub source_buffer: BaseBufferHandle,
	pub source_offset: usize,
	pub destination_buffer: BaseBufferHandle,
	pub destination_offset: usize,
	pub size: usize,
}

impl BufferCopyDescriptor {
	/// Creates a buffer copy descriptor from source and destination byte ranges.
	pub fn new(
		source_buffer: BaseBufferHandle,
		source_offset: usize,
		destination_buffer: BaseBufferHandle,
		destination_offset: usize,
		size: usize,
	) -> Self {
		Self {
			source_buffer,
			source_offset,
			destination_buffer,
			destination_offset,
			size,
		}
	}
}

/// The `BufferImageCopyDescriptor` struct configures one image upload from a buffer.
pub struct BufferImageCopyDescriptor {
	pub source_buffer: BaseBufferHandle,
	pub source_offset: usize,
	pub source_bytes_per_row: usize,
	pub source_bytes_per_image: usize,
	pub destination_image: crate::BaseImageHandle,
}

impl BufferImageCopyDescriptor {
	/// Creates a buffer-to-image copy descriptor from a source byte layout and destination image.
	pub fn new(
		source_buffer: BaseBufferHandle,
		source_offset: usize,
		source_bytes_per_row: usize,
		source_bytes_per_image: usize,
		destination_image: crate::BaseImageHandle,
	) -> Self {
		Self {
			source_buffer,
			source_offset,
			source_bytes_per_row,
			source_bytes_per_image,
			destination_image,
		}
	}
}

/// The `ImageBufferCopyDescriptor` struct exists to configure one image download into a destination buffer.
#[derive(Clone, Copy)]
pub struct ImageBufferCopyDescriptor {
	pub source: crate::ImageOrSwapchain,
	pub destination_buffer: BaseBufferHandle,
	pub destination_offset: usize,
	pub destination_bytes_per_row: usize,
	pub destination_bytes_per_image: usize,
}

impl ImageBufferCopyDescriptor {
	/// Creates an image-to-buffer copy descriptor from a source image and destination byte layout.
	pub fn new(
		source_image: crate::BaseImageHandle,
		destination_buffer: BaseBufferHandle,
		destination_offset: usize,
		destination_bytes_per_row: usize,
		destination_bytes_per_image: usize,
	) -> Self {
		Self {
			source: source_image.into(),
			destination_buffer,
			destination_offset,
			destination_bytes_per_row,
			destination_bytes_per_image,
		}
	}

	/// Creates an image-to-buffer copy descriptor from a swapchain and destination byte layout.
	pub fn swapchain(
		source_swapchain: crate::SwapchainHandle,
		destination_buffer: BaseBufferHandle,
		destination_offset: usize,
		destination_bytes_per_row: usize,
		destination_bytes_per_image: usize,
	) -> Self {
		Self {
			source: source_swapchain.into(),
			destination_buffer,
			destination_offset,
			destination_bytes_per_row,
			destination_bytes_per_image,
		}
	}
}

pub struct BufferDescriptor {
	pub(super) buffer: BaseBufferHandle,
	pub(super) offset: usize,
	pub(super) index_type: Option<DataTypes>,
}

impl BufferDescriptor {
	pub fn new<T: Copy, const N: usize>(buffer: BufferHandle<[T; N]>) -> Self {
		Self {
			buffer: buffer.into(),
			offset: 0,
			index_type: None,
		}
	}

	pub fn offset(mut self, offset: usize) -> Self {
		self.offset = offset;
		self
	}

	pub fn index_type(mut self, index_type: DataTypes) -> Self {
		self.index_type = Some(index_type);
		self
	}
}

#[cfg(test)]
mod tests {
	use super::*;

	#[test]
	fn bc_layout_uses_ceil_block_counts_and_keeps_small_mips_nonzero() {
		let layout = bc_layout(5, 7, 16);

		assert_eq!(layout.blocks_w, 2);
		assert_eq!(layout.blocks_h, 2);
		assert_eq!(layout.bytes_per_block, 16);
		assert_eq!(layout.bytes_per_row, 32);
		assert_eq!(layout.bytes_per_image, 64);

		let small = bc_layout(1, 1, 8);
		assert_eq!(small.blocks_w, 1);
		assert_eq!(small.blocks_h, 1);
		assert_eq!(small.bytes_per_row, 8);
		assert_eq!(small.bytes_per_image, 8);
	}

	#[test]
	fn format_bc_layout_uses_format_block_size() {
		assert_eq!(Formats::BC5.bc_bytes_per_block(), Some(16));
		assert_eq!(Formats::BC7.bc_layout(8, 4).unwrap().bytes_per_image, 32);
		assert_eq!(Formats::RGBA8UNORM.bc_layout(8, 4), None);
	}

	#[test]
	fn compact_copy_layout_preserves_texel_rows_and_bc_block_rows() {
		let cases = [
			(Formats::RGBA8UNORM, 5, 7, (20, 7, 140)),
			(Formats::RGB16UNORM, 5, 7, (30, 7, 210)),
			(Formats::RGBu11u11u10, 5, 7, (20, 7, 140)),
			(Formats::Depth32, 5, 7, (20, 7, 140)),
			(Formats::BC7, 5, 7, (32, 2, 64)),
			(Formats::RGBA8UNORM, 0, 0, (0, 0, 0)),
			(Formats::BC7, 0, 0, (16, 1, 16)),
		];

		for (format, width, height, expected) in cases {
			assert_eq!(format.compact_copy_layout(width, height), expected);
		}
	}

	#[test]
	fn shader_stage_conversion_is_one_to_one() {
		let cases = [
			(ShaderTypes::Vertex, Stages::VERTEX),
			(ShaderTypes::Fragment, Stages::FRAGMENT),
			(ShaderTypes::Compute, Stages::COMPUTE),
			(ShaderTypes::Task, Stages::TASK),
			(ShaderTypes::Mesh, Stages::MESH),
			(ShaderTypes::RayGen, Stages::RAYGEN),
			(ShaderTypes::ClosestHit, Stages::CLOSEST_HIT),
			(ShaderTypes::AnyHit, Stages::ANY_HIT),
			(ShaderTypes::Intersection, Stages::INTERSECTION),
			(ShaderTypes::Miss, Stages::MISS),
			(ShaderTypes::Callable, Stages::CALLABLE),
		];
		for (shader, expected_stage) in cases {
			assert_eq!(Stages::from(shader), expected_stage);
		}
	}

	#[test]
	fn primitive_data_type_sizes_match_gpu_scalar_widths() {
		let cases = [
			(DataTypes::Float, 4),
			(DataTypes::Float2, 8),
			(DataTypes::Float3, 12),
			(DataTypes::Float4, 16),
			(DataTypes::U8, 1),
			(DataTypes::U16, 2),
			(DataTypes::U32, 4),
			(DataTypes::Int, 4),
			(DataTypes::Int2, 8),
			(DataTypes::Int3, 12),
			(DataTypes::Int4, 16),
			(DataTypes::UInt, 4),
			(DataTypes::UInt2, 8),
			(DataTypes::UInt3, 12),
			(DataTypes::UInt4, 16),
		];
		for (data_type, expected_size) in cases {
			assert_eq!(data_type.size(), expected_size);
			assert_eq!(Size::size(&data_type), expected_size);
		}
	}

	#[test]
	fn access_and_use_aliases_preserve_backend_bit_contracts() {
		assert_eq!(AccessPolicies::READ_WRITE, AccessPolicies::READ | AccessPolicies::WRITE);
		assert_eq!(DeviceAccesses::DeviceOnly, DeviceAccesses::GpuRead | DeviceAccesses::GpuWrite);
		assert_eq!(DeviceAccesses::HostOnly, DeviceAccesses::CpuRead | DeviceAccesses::CpuWrite);
		assert_eq!(Uses::BlitSource, Uses::TransferSource);
		assert_eq!(Uses::BlitDestination, Uses::TransferDestination);
	}
}

impl<T: Copy> From<BufferHandle<T>> for BufferDescriptor {
	fn from(val: BufferHandle<T>) -> Self {
		BufferDescriptor {
			buffer: val.into(),
			offset: 0,
			index_type: None,
		}
	}
}

pub struct BufferStridedRange {
	pub(super) buffer_offset: BufferDescriptor,
	pub(super) stride: usize,
	pub(super) size: usize,
}

impl BufferStridedRange {
	pub fn new(buffer: BaseBufferHandle, offset: usize, stride: usize, size: usize) -> Self {
		Self {
			buffer_offset: BufferDescriptor {
				buffer,
				offset,
				index_type: None,
			},
			stride,
			size,
		}
	}
}

bitflags::bitflags! {
	#[derive(Clone, Copy, PartialEq, Eq, PartialOrd, Ord, Hash, Debug)]
	pub struct WorkloadTypes: u16 {
		const RASTER = 1 << 0;
		const RAY_TRACING = 1 << 1;
		const COMPUTE = 1 << 2;
		const TRANSFER = 1 << 3;
		const VIDEO = 1 << 4;
		const IO = 1 << 5;
	}
}