-
Notifications
You must be signed in to change notification settings - Fork 438
/
structs.rs
1064 lines (926 loc) · 34.8 KB
/
structs.rs
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
use crate::{bail, codegen::Shader, LinAlgType, MacroInput};
use foldhash::HashMap;
use proc_macro2::{Span, TokenStream};
use quote::{format_ident, quote, ToTokens, TokenStreamExt};
use std::{cmp::Ordering, num::NonZeroUsize};
use syn::{Error, Ident, Result};
use vulkano::shader::spirv::{Decoration, Id, Instruction};
#[derive(Default)]
pub struct TypeRegistry {
registered_structs: HashMap<Ident, RegisteredType>,
}
impl TypeRegistry {
fn register_struct(&mut self, shader: &Shader, ty: &TypeStruct) -> Result<bool> {
// Checking with registry if this struct is already registered by another shader, and if
// their signatures match.
if let Some(registered) = self.registered_structs.get(&ty.ident) {
registered.validate_signatures(&shader.name, ty)?;
// If the struct is already registered and matches this one, skip the duplicate.
Ok(false)
} else {
self.registered_structs.insert(
ty.ident.clone(),
RegisteredType {
shader: shader.name.clone(),
ty: ty.clone(),
},
);
Ok(true)
}
}
}
struct RegisteredType {
shader: String,
ty: TypeStruct,
}
impl RegisteredType {
fn validate_signatures(&self, other_shader: &str, other_ty: &TypeStruct) -> Result<()> {
let (shader, struct_ident) = (&self.shader, &self.ty.ident);
if self.ty.members.len() > other_ty.members.len() {
let member_ident = &self.ty.members[other_ty.members.len()].ident;
bail!(
"shaders `{shader}` and `{other_shader}` declare structs with the same name \
`{struct_ident}`, but the struct from shader `{shader}` contains an extra field \
`{member_ident}`",
);
}
if self.ty.members.len() < other_ty.members.len() {
let member_ident = &other_ty.members[self.ty.members.len()].ident;
bail!(
"shaders `{shader}` and `{other_shader}` declare structs with the same name \
`{struct_ident}`, but the struct from shader `{other_shader}` contains an extra \
field `{member_ident}`",
);
}
for (index, (member, other_member)) in self
.ty
.members
.iter()
.zip(other_ty.members.iter())
.enumerate()
{
if member.ty != other_member.ty {
let (member_ty, other_member_ty) = (&member.ty, &other_member.ty);
bail!(
"shaders `{shader}` and `{other_shader}` declare structs with the same name \
`{struct_ident}`, but the struct from shader `{shader}` contains a field of \
type `{member_ty:?}` at index `{index}`, whereas the same struct from shader \
`{other_shader}` contains a field of type `{other_member_ty:?}` in the same \
position",
);
}
}
Ok(())
}
}
/// Translates all the structs that are contained in the SPIR-V document as Rust structs.
pub(super) fn write_structs(
input: &MacroInput,
shader: &Shader,
type_registry: &mut TypeRegistry,
) -> Result<TokenStream> {
if !input.generate_structs {
return Ok(TokenStream::new());
}
let mut structs = TokenStream::new();
for (struct_id, member_type_ids) in shader
.spirv
.types()
.iter()
.filter_map(|instruction| match *instruction {
Instruction::TypeStruct {
result_id,
ref member_types,
} => Some((result_id, member_types)),
_ => None,
})
.filter(|&(struct_id, _)| has_defined_layout(shader, struct_id))
{
let struct_ty = TypeStruct::new(shader, struct_id, member_type_ids)?;
// Register the type if needed.
if !type_registry.register_struct(shader, &struct_ty)? {
continue;
}
let custom_derives = if struct_ty.size().is_some() {
input.custom_derives.as_slice()
} else {
&[]
};
let struct_ser = Serializer(&struct_ty, input);
structs.extend(quote! {
#[allow(non_camel_case_types, non_snake_case)]
#[derive(::vulkano::buffer::BufferContents #(, #custom_derives )* )]
#[repr(C)]
#struct_ser
})
}
Ok(structs)
}
fn has_defined_layout(shader: &Shader, struct_id: Id) -> bool {
for member_info in shader.spirv.id(struct_id).members() {
let mut offset_found = false;
for instruction in member_info.decorations() {
match instruction {
Instruction::MemberDecorate {
decoration: Decoration::BuiltIn { .. },
..
} => {
// Ignore the whole struct if a member is built in, which includes
// `gl_Position` for example.
return false;
}
Instruction::MemberDecorate {
decoration: Decoration::Offset { .. },
..
} => {
offset_found = true;
}
_ => (),
}
}
// Some structs don't have `Offset` decorations, in that case they are used as local
// variables only. Ignoring these.
if !offset_found {
return false;
}
}
true
}
#[derive(Clone, Copy, PartialEq, Eq)]
#[repr(u8)]
enum Alignment {
A1 = 1,
A2 = 2,
A4 = 4,
A8 = 8,
A16 = 16,
A32 = 32,
}
impl Alignment {
fn new(alignment: usize) -> Self {
match alignment {
1 => Alignment::A1,
2 => Alignment::A2,
4 => Alignment::A4,
8 => Alignment::A8,
16 => Alignment::A16,
32 => Alignment::A32,
_ => unreachable!(),
}
}
}
impl PartialOrd for Alignment {
fn partial_cmp(&self, other: &Self) -> Option<Ordering> {
Some(self.cmp(other))
}
}
impl Ord for Alignment {
fn cmp(&self, other: &Self) -> Ordering {
(*self as usize).cmp(&(*other as usize))
}
}
fn align_up(offset: usize, alignment: Alignment) -> usize {
(offset + alignment as usize - 1) & !(alignment as usize - 1)
}
fn is_aligned(offset: usize, alignment: Alignment) -> bool {
offset & (alignment as usize - 1) == 0
}
#[derive(Clone, Debug, PartialEq, Eq)]
enum Type {
Scalar(TypeScalar),
Pointer(TypePointer),
Vector(TypeVector),
Matrix(TypeMatrix),
Array(TypeArray),
Struct(TypeStruct),
}
impl Type {
fn new(shader: &Shader, type_id: Id) -> Result<Self> {
let id_info = shader.spirv.id(type_id);
let ty = match *id_info.instruction() {
Instruction::TypeBool { .. } => bail!(shader.source, "can't put booleans in structs"),
Instruction::TypeInt {
width, signedness, ..
} => Type::Scalar(TypeScalar::Int(TypeInt::new(shader, width, signedness)?)),
Instruction::TypeFloat { width, .. } => {
Type::Scalar(TypeScalar::Float(TypeFloat::new(shader, width)?))
}
Instruction::TypePointer { .. } => Type::Pointer(TypePointer::new(shader)?),
Instruction::TypeVector {
component_type,
component_count,
..
} => Type::Vector(TypeVector::new(shader, component_type, component_count)?),
Instruction::TypeMatrix {
column_type,
column_count,
..
} => Type::Matrix(TypeMatrix::new(shader, column_type, column_count)?),
Instruction::TypeArray {
element_type,
length,
..
} => Type::Array(TypeArray::new(shader, type_id, element_type, Some(length))?),
Instruction::TypeRuntimeArray { element_type, .. } => {
Type::Array(TypeArray::new(shader, type_id, element_type, None)?)
}
Instruction::TypeStruct {
ref member_types, ..
} => Type::Struct(TypeStruct::new(shader, type_id, member_types)?),
_ => bail!(shader.source, "type {type_id} was not found"),
};
Ok(ty)
}
fn size(&self) -> Option<usize> {
match self {
Self::Scalar(ty) => Some(ty.size()),
Self::Pointer(ty) => Some(ty.size()),
Self::Vector(ty) => Some(ty.size()),
Self::Matrix(ty) => Some(ty.size()),
Self::Array(ty) => ty.size(),
Self::Struct(ty) => ty.size(),
}
}
fn scalar_alignment(&self) -> Alignment {
match self {
Self::Scalar(ty) => ty.alignment(),
Self::Pointer(ty) => ty.alignment(),
Self::Vector(ty) => ty.component_type.alignment(),
Self::Matrix(ty) => ty.component_type.alignment(),
Self::Array(ty) => ty.scalar_alignment(),
Self::Struct(ty) => ty.scalar_alignment(),
}
}
}
#[derive(Clone, Debug, PartialEq, Eq)]
enum TypeScalar {
Int(TypeInt),
Float(TypeFloat),
}
impl TypeScalar {
fn size(&self) -> usize {
match self {
Self::Int(ty) => ty.size(),
Self::Float(ty) => ty.size(),
}
}
fn alignment(&self) -> Alignment {
match self {
Self::Int(ty) => ty.alignment(),
Self::Float(ty) => ty.alignment(),
}
}
}
impl ToTokens for TypeScalar {
fn to_tokens(&self, tokens: &mut TokenStream) {
match self {
Self::Int(ty) => ty.to_tokens(tokens),
Self::Float(ty) => ty.to_tokens(tokens),
}
}
}
#[derive(Clone, Debug, PartialEq, Eq)]
struct TypeInt {
width: IntWidth,
signed: bool,
}
#[derive(Clone, Copy, Debug, PartialEq, Eq)]
#[repr(u8)]
enum IntWidth {
W8 = 8,
W16 = 16,
W32 = 32,
W64 = 64,
}
impl TypeInt {
fn new(shader: &Shader, width: u32, signedness: u32) -> Result<Self> {
let width = match width {
8 => IntWidth::W8,
16 => IntWidth::W16,
32 => IntWidth::W32,
64 => IntWidth::W64,
_ => bail!(shader.source, "integers must be 8, 16, 32, or 64-bit wide"),
};
let signed = match signedness {
0 => false,
1 => true,
_ => bail!(shader.source, "signedness must be 0 or 1"),
};
Ok(TypeInt { width, signed })
}
fn size(&self) -> usize {
self.width as usize >> 3
}
fn alignment(&self) -> Alignment {
Alignment::new(self.size())
}
#[rustfmt::skip]
fn as_str(&self) -> &'static str {
match (self.width, self.signed) {
(IntWidth::W8, false) => "u8",
(IntWidth::W16, false) => "u16",
(IntWidth::W32, false) => "u32",
(IntWidth::W64, false) => "u64",
(IntWidth::W8, true) => "i8",
(IntWidth::W16, true) => "i16",
(IntWidth::W32, true) => "i32",
(IntWidth::W64, true) => "i64",
}
}
fn to_ident(&self) -> Ident {
Ident::new(self.as_str(), Span::call_site())
}
}
impl ToTokens for TypeInt {
fn to_tokens(&self, tokens: &mut TokenStream) {
tokens.append(self.to_ident());
}
}
#[derive(Clone, Debug, PartialEq, Eq)]
struct TypeFloat {
width: FloatWidth,
}
#[derive(Clone, Copy, Debug, PartialEq, Eq)]
#[repr(u8)]
enum FloatWidth {
W16 = 16,
W32 = 32,
W64 = 64,
}
impl TypeFloat {
fn new(shader: &Shader, width: u32) -> Result<Self> {
let width = match width {
16 => FloatWidth::W16,
32 => FloatWidth::W32,
64 => FloatWidth::W64,
_ => bail!(shader.source, "floats must be 16, 32, or 64-bit wide"),
};
Ok(TypeFloat { width })
}
fn size(&self) -> usize {
self.width as usize >> 3
}
fn alignment(&self) -> Alignment {
Alignment::new(self.size())
}
fn as_str(&self) -> &'static str {
match self.width {
FloatWidth::W16 => "f16",
FloatWidth::W32 => "f32",
FloatWidth::W64 => "f64",
}
}
fn to_ident(&self) -> Ident {
Ident::new(self.as_str(), Span::call_site())
}
}
impl ToTokens for TypeFloat {
fn to_tokens(&self, tokens: &mut TokenStream) {
if self.width == FloatWidth::W16 {
tokens.extend(quote! { ::vulkano::half:: });
}
tokens.append(self.to_ident());
}
}
#[derive(Clone, Debug, PartialEq, Eq)]
struct TypePointer;
impl TypePointer {
fn new(_shader: &Shader) -> Result<Self> {
Ok(TypePointer)
}
fn size(&self) -> usize {
8
}
fn alignment(&self) -> Alignment {
Alignment::A8
}
}
impl ToTokens for TypePointer {
fn to_tokens(&self, tokens: &mut TokenStream) {
tokens.extend(quote! { ::vulkano::DeviceAddress });
}
}
#[derive(Clone, Debug, PartialEq, Eq)]
struct TypeVector {
component_type: TypeScalar,
component_count: ComponentCount,
}
impl TypeVector {
fn new(shader: &Shader, component_type_id: Id, component_count: u32) -> Result<Self> {
let component_count = ComponentCount::new(shader, component_count)?;
let component_type = match *shader.spirv.id(component_type_id).instruction() {
Instruction::TypeBool { .. } => bail!(shader.source, "can't put booleans in structs"),
Instruction::TypeInt {
width, signedness, ..
} => TypeScalar::Int(TypeInt::new(shader, width, signedness)?),
Instruction::TypeFloat { width, .. } => {
TypeScalar::Float(TypeFloat::new(shader, width)?)
}
_ => bail!(shader.source, "vector components must be scalars"),
};
Ok(TypeVector {
component_type,
component_count,
})
}
fn size(&self) -> usize {
self.component_type.size() * self.component_count as usize
}
}
#[derive(Clone, Debug, PartialEq, Eq)]
struct TypeMatrix {
component_type: TypeFloat,
column_count: ComponentCount,
row_count: ComponentCount,
stride: usize,
majorness: MatrixMajorness,
}
impl TypeMatrix {
fn new(shader: &Shader, column_type_id: Id, column_count: u32) -> Result<Self> {
let column_count = ComponentCount::new(shader, column_count)?;
let (component_type, row_count) = match *shader.spirv.id(column_type_id).instruction() {
Instruction::TypeVector {
component_type,
component_count,
..
} => match *shader.spirv.id(component_type).instruction() {
Instruction::TypeFloat { width, .. } => (
TypeFloat::new(shader, width)?,
ComponentCount::new(shader, component_count)?,
),
_ => bail!(shader.source, "matrix components must be floats"),
},
_ => bail!(shader.source, "matrix columns must be vectors"),
};
// We can't know these until we get to the members and their decorations, so just use
// defaults for now.
let stride = component_type.size() * row_count as usize;
let majorness = MatrixMajorness::ColumnMajor;
Ok(TypeMatrix {
component_type,
column_count,
row_count,
stride,
majorness,
})
}
fn size(&self) -> usize {
self.stride * self.vector_count() as usize
}
fn vector_size(&self) -> usize {
self.component_type.size() * self.component_count() as usize
}
fn vector_count(&self) -> ComponentCount {
match self.majorness {
MatrixMajorness::ColumnMajor => self.column_count,
MatrixMajorness::RowMajor => self.row_count,
}
}
fn component_count(&self) -> ComponentCount {
match self.majorness {
MatrixMajorness::ColumnMajor => self.row_count,
MatrixMajorness::RowMajor => self.column_count,
}
}
}
#[derive(Clone, Copy, Debug, PartialEq, Eq)]
enum MatrixMajorness {
ColumnMajor,
RowMajor,
}
#[derive(Clone, Copy, Debug, PartialEq, Eq)]
#[repr(u8)]
enum ComponentCount {
C2 = 2,
C3 = 3,
C4 = 4,
}
impl ComponentCount {
fn new(shader: &Shader, count: u32) -> Result<Self> {
let count = match count {
2 => ComponentCount::C2,
3 => ComponentCount::C3,
4 => ComponentCount::C4,
_ => bail!(shader.source, "component counts must be 2, 3 or 4"),
};
Ok(count)
}
}
#[derive(Clone, Debug, PartialEq, Eq)]
struct TypeArray {
element_type: Box<Type>,
length: Option<NonZeroUsize>,
stride: usize,
}
impl TypeArray {
fn new(
shader: &Shader,
array_id: Id,
element_type_id: Id,
length_id: Option<Id>,
) -> Result<Self> {
let element_type = Box::new(Type::new(shader, element_type_id)?);
let length = length_id
.map(|id| match shader.spirv.id(id).instruction() {
Instruction::Constant { value, .. } | Instruction::SpecConstant { value, .. } => {
assert!(matches!(value.len(), 1 | 2));
let len = value.iter().rev().fold(0u64, |a, &b| (a << 32) | b as u64);
NonZeroUsize::new(len.try_into().unwrap()).ok_or_else(|| {
Error::new_spanned(&shader.source, "arrays must have a non-zero length")
})
}
_ => bail!(shader.source, "failed to find array length"),
})
.transpose()?;
let stride = {
let mut strides =
shader
.spirv
.id(array_id)
.decorations()
.iter()
.filter_map(|instruction| match *instruction {
Instruction::Decorate {
decoration: Decoration::ArrayStride { array_stride },
..
} => Some(array_stride as usize),
_ => None,
});
let stride = strides.next().ok_or_else(|| {
Error::new_spanned(
&shader.source,
"arrays inside structs must have an `ArrayStride` decoration",
)
})?;
if !strides.all(|s| s == stride) {
bail!(shader.source, "found conflicting `ArrayStride` decorations");
}
if !is_aligned(stride, element_type.scalar_alignment()) {
bail!(
shader.source,
"array strides must be aligned for the element type",
);
}
let element_size = element_type.size().ok_or_else(|| {
Error::new_spanned(&shader.source, "array elements must be sized")
})?;
if stride < element_size {
bail!(shader.source, "array elements must not overlap");
}
stride
};
Ok(TypeArray {
element_type,
length,
stride,
})
}
fn size(&self) -> Option<usize> {
self.length.map(|length| self.stride * length.get())
}
fn scalar_alignment(&self) -> Alignment {
self.element_type.scalar_alignment()
}
}
#[derive(Clone, Debug, PartialEq, Eq)]
struct TypeStruct {
ident: Ident,
members: Vec<Member>,
}
impl TypeStruct {
fn new(shader: &Shader, struct_id: Id, member_type_ids: &[Id]) -> Result<Self> {
let id_info = shader.spirv.id(struct_id);
let ident = id_info
.names()
.iter()
.find_map(|instruction| match instruction {
Instruction::Name { name, .. } => {
// Replace chars that could potentially cause the ident to be invalid with "_".
// For example, Rust-GPU names structs by their fully qualified rust name (e.g.
// "foo::bar::MyStruct") in which the ":" is an invalid character for idents.
let mut name =
name.replace(|c: char| !(c.is_ascii_alphanumeric() || c == '_'), "_");
if name.starts_with(|c: char| !c.is_ascii_alphabetic()) {
name.insert(0, '_');
}
// Worst case: invalid idents will get the UnnamedX name below
syn::parse_str(&name).ok()
}
_ => None,
})
.unwrap_or_else(|| format_ident!("Unnamed{}", struct_id.as_raw()));
let mut members = Vec::<Member>::with_capacity(member_type_ids.len());
for (member_index, (&member_id, member_info)) in
member_type_ids.iter().zip(id_info.members()).enumerate()
{
let ident = member_info
.names()
.iter()
.find_map(|instruction| match instruction {
Instruction::MemberName { name, .. } => {
Some(Ident::new(name, Span::call_site()))
}
_ => None,
})
.unwrap_or_else(|| format_ident!("unnamed{member_index}"));
let mut ty = Type::new(shader, member_id)?;
{
// If the member is an array, then matrix-decorations can be applied to it if the
// innermost type of the array is a matrix. Else this will stay being the type of
// the member.
let mut ty = &mut ty;
while let Type::Array(TypeArray { element_type, .. }) = ty {
ty = element_type;
}
if let Type::Matrix(matrix) = ty {
let mut strides =
member_info.decorations().iter().filter_map(
|instruction| match *instruction {
Instruction::MemberDecorate {
decoration: Decoration::MatrixStride { matrix_stride },
..
} => Some(matrix_stride as usize),
_ => None,
},
);
matrix.stride = strides.next().ok_or_else(|| {
Error::new_spanned(
&shader.source,
"matrices inside structs must have a `MatrixStride` decoration",
)
})?;
if !strides.all(|s| s == matrix.stride) {
bail!(
shader.source,
"found conflicting `MatrixStride` decorations",
);
}
if !is_aligned(matrix.stride, matrix.component_type.alignment()) {
bail!(
shader.source,
"matrix strides must be an integer multiple of the size of the \
component",
);
}
let mut majornessess = member_info.decorations().iter().filter_map(
|instruction| match *instruction {
Instruction::MemberDecorate {
decoration: Decoration::ColMajor,
..
} => Some(MatrixMajorness::ColumnMajor),
Instruction::MemberDecorate {
decoration: Decoration::RowMajor,
..
} => Some(MatrixMajorness::RowMajor),
_ => None,
},
);
matrix.majorness = majornessess.next().ok_or_else(|| {
Error::new_spanned(
&shader.source,
"matrices inside structs must have a `ColMajor` or `RowMajor` \
decoration",
)
})?;
if !majornessess.all(|m| m == matrix.majorness) {
bail!(
shader.source,
"found conflicting matrix majorness decorations",
);
}
// NOTE(Marc): It is crucial that we do this check after setting the majorness,
// because `TypeMatrix::vector_size` depends on it.
if matrix.stride < matrix.vector_size() {
bail!(shader.source, "matrix columns/rows must not overlap");
}
}
}
let offset = member_info
.decorations()
.iter()
.find_map(|instruction| match *instruction {
Instruction::MemberDecorate {
decoration: Decoration::Offset { byte_offset },
..
} => Some(byte_offset as usize),
_ => None,
})
.ok_or_else(|| {
Error::new_spanned(
&shader.source,
"struct members must have an `Offset` decoration",
)
})?;
if !is_aligned(offset, ty.scalar_alignment()) {
bail!(
shader.source,
"struct member offsets must be aligned for the member type",
);
}
if let Some(last) = members.last() {
if !is_aligned(offset, last.ty.scalar_alignment()) {
bail!(
shader.source,
"expected struct member offset to be aligned for the preceding member type",
);
}
let last_size = last.ty.size().ok_or_else(|| {
Error::new_spanned(
&shader.source,
"all members except the last member of a struct must be sized",
)
})?;
if last.offset + last_size > offset {
bail!(shader.source, "struct members must not overlap");
}
}
members.push(Member { ident, ty, offset });
}
Ok(TypeStruct { ident, members })
}
fn size(&self) -> Option<usize> {
self.members
.last()
.map(|member| {
member
.ty
.size()
.map(|size| align_up(member.offset + size, self.scalar_alignment()))
})
.unwrap_or(Some(0))
}
fn scalar_alignment(&self) -> Alignment {
self.members
.iter()
.map(|member| member.ty.scalar_alignment())
.max()
.unwrap_or(Alignment::A1)
}
}
#[derive(Clone, Debug)]
struct Member {
ident: Ident,
ty: Type,
offset: usize,
}
impl PartialEq for Member {
fn eq(&self, other: &Self) -> bool {
self.ty == other.ty && self.offset == other.offset
}
}
impl Eq for Member {}
/// Helper for serializing a type to tokens with respect to macro input.
struct Serializer<'a, T>(&'a T, &'a MacroInput);
impl ToTokens for Serializer<'_, Type> {
fn to_tokens(&self, tokens: &mut TokenStream) {
match &self.0 {
Type::Scalar(ty) => ty.to_tokens(tokens),
Type::Pointer(ty) => ty.to_tokens(tokens),
Type::Vector(ty) => Serializer(ty, self.1).to_tokens(tokens),
Type::Matrix(ty) => Serializer(ty, self.1).to_tokens(tokens),
Type::Array(ty) => Serializer(ty, self.1).to_tokens(tokens),
Type::Struct(ty) => tokens.append(ty.ident.clone()),
}
}
}
impl ToTokens for Serializer<'_, TypeVector> {
fn to_tokens(&self, tokens: &mut TokenStream) {
let component_type = &self.0.component_type;
let component_count = self.0.component_count as usize;
match self.1.linalg_type {
LinAlgType::Std => {
tokens.extend(quote! { [#component_type; #component_count] });
}
LinAlgType::CgMath => {
let vector = format_ident!("Vector{}", component_count);
tokens.extend(quote! { ::cgmath::#vector<#component_type> });
}
LinAlgType::Nalgebra => {
tokens.extend(quote! {
::nalgebra::base::SVector<#component_type, #component_count>
});
}
}
}
}
impl ToTokens for Serializer<'_, TypeMatrix> {
fn to_tokens(&self, tokens: &mut TokenStream) {
let component_type = &self.0.component_type;
let vector_count = self.0.vector_count() as usize;
let component_count = self.0.component_count() as usize;
let majorness = self.0.majorness;
// This can't overflow because the stride must be at least the vector size.
let padding = self.0.stride - self.0.vector_size();
match self.1.linalg_type {
// cgmath only has column-major matrices. It also only has square matrices, and its 3x3
// matrix is not padded right. Fall back to std for anything else.
LinAlgType::CgMath
if majorness == MatrixMajorness::ColumnMajor
&& padding == 0
&& vector_count == component_count =>
{
let matrix = format_ident!("Matrix{}", component_count);
tokens.extend(quote! { ::cgmath::#matrix<#component_type> });
}
// nalgebra only has column-major matrices, and its 3xN matrices are not padded right.
// Fall back to std for anything else.
LinAlgType::Nalgebra if majorness == MatrixMajorness::ColumnMajor && padding == 0 => {
tokens.extend(quote! {
::nalgebra::base::SMatrix<#component_type, #component_count, #vector_count>
});
}
_ => {
let vector = Padded(quote! { [#component_type; #component_count] }, padding);
tokens.extend(quote! { [#vector; #vector_count] });
}
}
}
}
impl ToTokens for Serializer<'_, TypeArray> {
fn to_tokens(&self, tokens: &mut TokenStream) {
let element_type = &*self.0.element_type;
// This can't panic because array elements must be sized.
let element_size = element_type.size().unwrap();
// This can't overflow because the stride must be at least the element size.
let padding = self.0.stride - element_size;
let element_type = Padded(Serializer(element_type, self.1), padding);
if let Some(length) = self.0.length.map(NonZeroUsize::get) {
tokens.extend(quote! { [#element_type; #length] });
} else {
tokens.extend(quote! { [#element_type] });
}
}
}
impl ToTokens for Serializer<'_, TypeStruct> {
fn to_tokens(&self, tokens: &mut TokenStream) {
let struct_ident = &self.0.ident;
let member_idents = self.0.members.iter().map(|member| &member.ident);
let mut member_types = Vec::new();
// TODO: Replace with the `ArrayWindows` iterator once it is stabilized.
for member_pair in self.0.members.windows(2) {
let member = &member_pair[0];
let next_member = &member_pair[1];
let offset = member.offset;
// This can't panic, because only the last member can be unsized.
let size = member.ty.size().unwrap();
let next_offset = next_member.offset;
let next_alignment = next_member.ty.scalar_alignment();
let ser = Serializer(&member.ty, self.1);