-
Notifications
You must be signed in to change notification settings - Fork 1.2k
Expand file tree
/
Copy pathblock.rs
More file actions
4231 lines (3969 loc) · 183 KB
/
block.rs
File metadata and controls
4231 lines (3969 loc) · 183 KB
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
/*!
Implementations for `BlockContext` methods.
*/
use alloc::vec::Vec;
use arrayvec::ArrayVec;
use spirv::Word;
use super::{
helpers::map_storage_class, index::BoundsCheckResult, selection::Selection, Block,
BlockContext, Dimension, Error, IdGenerator, Instruction, LocalType, LookupType, NumericType,
ResultMember, WrappedFunction, Writer, WriterFlags,
};
use crate::{
arena::Handle, back::spv::helpers::is_uniform_matcx2_struct_member_access,
proc::index::GuardedIndex, Statement,
};
fn get_dimension(type_inner: &crate::TypeInner) -> Dimension {
match *type_inner {
crate::TypeInner::Scalar(_) => Dimension::Scalar,
crate::TypeInner::Vector { .. } => Dimension::Vector,
crate::TypeInner::Matrix { .. } => Dimension::Matrix,
crate::TypeInner::CooperativeMatrix { .. } => Dimension::CooperativeMatrix,
_ => unreachable!(),
}
}
/// How to derive the type of `OpAccessChain` instructions from Naga IR.
///
/// Most of the time, we compile Naga IR to SPIR-V instructions whose result
/// types are simply the direct SPIR-V analog of the Naga IR's. But in some
/// cases, the Naga IR and SPIR-V types need to diverge.
///
/// This enum specifies how [`BlockContext::write_access_chain`] should
/// choose a SPIR-V result type for the `OpAccessChain` it generates, based on
/// the type of the given Naga IR [`Expression`] it's generating code for.
///
/// [`Expression`]: crate::Expression
#[derive(Copy, Clone)]
enum AccessTypeAdjustment {
/// No adjustment needed: the SPIR-V type should be the direct
/// analog of the Naga IR expression type.
///
/// For most access chains, this is the right thing: the Naga IR access
/// expression produces a [`Pointer`] to the element / component, and the
/// SPIR-V `OpAccessChain` instruction does the same.
///
/// [`Pointer`]: crate::TypeInner::Pointer
None,
/// The SPIR-V type should be an `OpPointer` to the direct analog of the
/// Naga IR expression's type.
///
/// This is necessary for indexing binding arrays in the [`Handle`] address
/// space:
///
/// - In Naga IR, referencing a binding array [`GlobalVariable`] in the
/// [`Handle`] address space produces a value of type [`BindingArray`],
/// not a pointer to such. And [`Access`] and [`AccessIndex`] expressions
/// operate on handle binding arrays by value, and produce handle values,
/// not pointers.
///
/// - In SPIR-V, a binding array `OpVariable` produces a pointer to an
/// array, and `OpAccessChain` instructions operate on pointers,
/// regardless of whether the elements are opaque types or not.
///
/// See also the documentation for [`BindingArray`].
///
/// [`Handle`]: crate::AddressSpace::Handle
/// [`GlobalVariable`]: crate::GlobalVariable
/// [`BindingArray`]: crate::TypeInner::BindingArray
/// [`Access`]: crate::Expression::Access
/// [`AccessIndex`]: crate::Expression::AccessIndex
IntroducePointer(spirv::StorageClass),
/// The SPIR-V type should be an `OpPointer` to the std140 layout
/// compatible variant of the Naga IR expression's base type.
///
/// This is used when accessing a type through an [`AddressSpace::Uniform`]
/// pointer in cases where the original type is incompatible with std140
/// layout requirements and we have therefore declared the uniform to be of
/// an alternative std140 compliant type.
///
/// [`AddressSpace::Uniform`]: crate::AddressSpace::Uniform
UseStd140CompatType,
}
/// The results of emitting code for a left-hand-side expression.
///
/// On success, `write_access_chain` returns one of these.
enum ExpressionPointer {
/// The pointer to the expression's value is available, as the value of the
/// expression with the given id.
Ready { pointer_id: Word },
/// The access expression must be conditional on the value of `condition`, a boolean
/// expression that is true if all indices are in bounds. If `condition` is true, then
/// `access` is an `OpAccessChain` instruction that will compute a pointer to the
/// expression's value. If `condition` is false, then executing `access` would be
/// undefined behavior.
Conditional {
condition: Word,
access: Instruction,
},
}
/// The termination statement to be added to the end of the block
enum BlockExit {
/// Generates an OpReturn (void return)
Return,
/// Generates an OpBranch to the specified block
Branch {
/// The branch target block
target: Word,
},
/// Translates a loop `break if` into an `OpBranchConditional` to the
/// merge block if true (the merge block is passed through [`LoopContext::break_id`]
/// or else to the loop header (passed through [`preamble_id`])
///
/// [`preamble_id`]: Self::BreakIf::preamble_id
BreakIf {
/// The condition of the `break if`
condition: Handle<crate::Expression>,
/// The loop header block id
preamble_id: Word,
},
}
/// What code generation did with a provided [`BlockExit`] value.
///
/// A function that accepts a [`BlockExit`] argument should return a value of
/// this type, to indicate whether the code it generated ended up using the
/// provided exit, or ignored it and did a non-local exit of some other kind
/// (say, [`Break`] or [`Continue`]). Some callers must use this information to
/// decide whether to generate the target block at all.
///
/// [`Break`]: Statement::Break
/// [`Continue`]: Statement::Continue
#[must_use]
enum BlockExitDisposition {
/// The generated code used the provided `BlockExit` value. If it included a
/// block label, the caller should be sure to actually emit the block it
/// refers to.
Used,
/// The generated code did not use the provided `BlockExit` value. If it
/// included a block label, the caller should not bother to actually emit
/// the block it refers to, unless it knows the block is needed for
/// something else.
Discarded,
}
#[derive(Clone, Copy, Default)]
struct LoopContext {
continuing_id: Option<Word>,
break_id: Option<Word>,
}
#[derive(Debug)]
pub(crate) struct DebugInfoInner<'a> {
pub source_code: &'a str,
pub source_file_id: Word,
}
impl Writer {
// Flip Y coordinate to adjust for coordinate space difference
// between SPIR-V and our IR.
// The `position_id` argument is a pointer to a `vecN<f32>`,
// whose `y` component we will negate.
fn write_epilogue_position_y_flip(
&mut self,
position_id: Word,
body: &mut Vec<Instruction>,
) -> Result<(), Error> {
let float_ptr_type_id = self.get_f32_pointer_type_id(spirv::StorageClass::Output);
let index_y_id = self.get_index_constant(1);
let access_id = self.id_gen.next();
body.push(Instruction::access_chain(
float_ptr_type_id,
access_id,
position_id,
&[index_y_id],
));
let float_type_id = self.get_f32_type_id();
let load_id = self.id_gen.next();
body.push(Instruction::load(float_type_id, load_id, access_id, None));
let neg_id = self.id_gen.next();
body.push(Instruction::unary(
spirv::Op::FNegate,
float_type_id,
neg_id,
load_id,
));
body.push(Instruction::store(access_id, neg_id, None));
Ok(())
}
// Clamp fragment depth between 0 and 1.
fn write_epilogue_frag_depth_clamp(
&mut self,
frag_depth_id: Word,
body: &mut Vec<Instruction>,
) -> Result<(), Error> {
let float_type_id = self.get_f32_type_id();
let zero_scalar_id = self.get_constant_scalar(crate::Literal::F32(0.0));
let one_scalar_id = self.get_constant_scalar(crate::Literal::F32(1.0));
let original_id = self.id_gen.next();
body.push(Instruction::load(
float_type_id,
original_id,
frag_depth_id,
None,
));
let clamp_id = self.id_gen.next();
body.push(Instruction::ext_inst_gl_op(
self.gl450_ext_inst_id,
spirv::GLOp::FClamp,
float_type_id,
clamp_id,
&[original_id, zero_scalar_id, one_scalar_id],
));
body.push(Instruction::store(frag_depth_id, clamp_id, None));
Ok(())
}
fn write_entry_point_return(
&mut self,
value_id: Word,
ir_result: &crate::FunctionResult,
result_members: &[ResultMember],
body: &mut Vec<Instruction>,
) -> Result<Instruction, Error> {
for (index, res_member) in result_members.iter().enumerate() {
// This isn't a real builtin, and is handled elsewhere
if res_member.built_in == Some(crate::BuiltIn::MeshTaskSize) {
return Ok(Instruction::return_value(value_id));
}
let member_value_id = match ir_result.binding {
Some(_) => value_id,
None => {
let member_value_id = self.id_gen.next();
body.push(Instruction::composite_extract(
res_member.type_id,
member_value_id,
value_id,
&[index as u32],
));
member_value_id
}
};
self.store_io_with_f16_polyfill(body, res_member.id, member_value_id);
match res_member.built_in {
Some(crate::BuiltIn::Position { .. })
if self.flags.contains(WriterFlags::ADJUST_COORDINATE_SPACE) =>
{
self.write_epilogue_position_y_flip(res_member.id, body)?;
}
Some(crate::BuiltIn::FragDepth)
if self.flags.contains(WriterFlags::CLAMP_FRAG_DEPTH) =>
{
self.write_epilogue_frag_depth_clamp(res_member.id, body)?;
}
_ => {}
}
}
Ok(Instruction::return_void())
}
}
impl BlockContext<'_> {
/// Generates code to ensure that a loop is bounded. Should be called immediately
/// after adding the OpLoopMerge instruction to `block`. This function will
/// [`consume()`](crate::back::spv::Function::consume) `block` and append its
/// instructions to a new [`Block`], which will be returned to the caller for it to
/// consumed prior to writing the loop body.
///
/// Additionally this function will populate [`force_loop_bounding_vars`](crate::back::spv::Function::force_loop_bounding_vars),
/// ensuring that [`Function::to_words()`](crate::back::spv::Function::to_words) will
/// declare the required variables.
///
/// See [`crate::back::msl::Writer::gen_force_bounded_loop_statements`] for details
/// of why this is required.
fn write_force_bounded_loop_instructions(&mut self, mut block: Block, merge_id: Word) -> Block {
let uint_type_id = self.writer.get_u32_type_id();
let uint2_type_id = self.writer.get_vec2u_type_id();
let uint2_ptr_type_id = self
.writer
.get_vec2u_pointer_type_id(spirv::StorageClass::Function);
let bool_type_id = self.writer.get_bool_type_id();
let bool2_type_id = self.writer.get_vec2_bool_type_id();
let zero_uint_const_id = self.writer.get_constant_scalar(crate::Literal::U32(0));
let zero_uint2_const_id = self.writer.get_constant_composite(
LookupType::Local(LocalType::Numeric(NumericType::Vector {
size: crate::VectorSize::Bi,
scalar: crate::Scalar::U32,
})),
&[zero_uint_const_id, zero_uint_const_id],
);
let one_uint_const_id = self.writer.get_constant_scalar(crate::Literal::U32(1));
let max_uint_const_id = self
.writer
.get_constant_scalar(crate::Literal::U32(u32::MAX));
let max_uint2_const_id = self.writer.get_constant_composite(
LookupType::Local(LocalType::Numeric(NumericType::Vector {
size: crate::VectorSize::Bi,
scalar: crate::Scalar::U32,
})),
&[max_uint_const_id, max_uint_const_id],
);
let loop_counter_var_id = self.gen_id();
if self.writer.flags.contains(WriterFlags::DEBUG) {
self.writer
.debugs
.push(Instruction::name(loop_counter_var_id, "loop_bound"));
}
let var = super::LocalVariable {
id: loop_counter_var_id,
instruction: Instruction::variable(
uint2_ptr_type_id,
loop_counter_var_id,
spirv::StorageClass::Function,
Some(max_uint2_const_id),
),
};
self.function.force_loop_bounding_vars.push(var);
let break_if_block = self.gen_id();
self.function
.consume(block, Instruction::branch(break_if_block));
block = Block::new(break_if_block);
// Load the current loop counter value from its variable. We use a vec2<u32> to
// simulate a 64-bit counter.
let load_id = self.gen_id();
block.body.push(Instruction::load(
uint2_type_id,
load_id,
loop_counter_var_id,
None,
));
// If both the high and low u32s have reached 0 then break. ie
// if (all(eq(loop_counter, vec2(0)))) { break; }
let eq_id = self.gen_id();
block.body.push(Instruction::binary(
spirv::Op::IEqual,
bool2_type_id,
eq_id,
zero_uint2_const_id,
load_id,
));
let all_eq_id = self.gen_id();
block.body.push(Instruction::relational(
spirv::Op::All,
bool_type_id,
all_eq_id,
eq_id,
));
let inc_counter_block_id = self.gen_id();
block.body.push(Instruction::selection_merge(
inc_counter_block_id,
spirv::SelectionControl::empty(),
));
self.function.consume(
block,
Instruction::branch_conditional(all_eq_id, merge_id, inc_counter_block_id),
);
block = Block::new(inc_counter_block_id);
// To simulate a 64-bit counter we always decrement the low u32, and decrement
// the high u32 when the low u32 overflows. ie
// counter -= vec2(select(0u, 1u, counter.y == 0), 1u);
// Count down from u32::MAX rather than up from 0 to avoid hang on
// certain Intel drivers. See <https://github.com/gfx-rs/wgpu/issues/7319>.
let low_id = self.gen_id();
block.body.push(Instruction::composite_extract(
uint_type_id,
low_id,
load_id,
&[1],
));
let low_overflow_id = self.gen_id();
block.body.push(Instruction::binary(
spirv::Op::IEqual,
bool_type_id,
low_overflow_id,
low_id,
zero_uint_const_id,
));
let carry_bit_id = self.gen_id();
block.body.push(Instruction::select(
uint_type_id,
carry_bit_id,
low_overflow_id,
one_uint_const_id,
zero_uint_const_id,
));
let decrement_id = self.gen_id();
block.body.push(Instruction::composite_construct(
uint2_type_id,
decrement_id,
&[carry_bit_id, one_uint_const_id],
));
let result_id = self.gen_id();
block.body.push(Instruction::binary(
spirv::Op::ISub,
uint2_type_id,
result_id,
load_id,
decrement_id,
));
block
.body
.push(Instruction::store(loop_counter_var_id, result_id, None));
block
}
/// If `pointer` refers to an access chain that contains a dynamic indexing
/// of a two-row matrix in the [`Uniform`] address space, write code to
/// access the value returning the ID of the result. Else return None.
///
/// Two-row matrices in the uniform address space will have been declared
/// using a alternative std140 layout compatible type, where each column is
/// a member of a containing struct. As a result, SPIR-V is unable to access
/// its columns with a non-constant index. To work around this limitation
/// this function will call [`Self::write_checked_load()`] to load the
/// matrix itself, which handles conversion from the std140 compatible type
/// to the real matrix type. It then calls a [`wrapper function`] to obtain
/// the correct column from the matrix, and possibly extracts a component
/// from the vector too.
///
/// [`Uniform`]: crate::AddressSpace::Uniform
/// [`wrapper function`]: super::Writer::write_wrapped_matcx2_get_column
fn maybe_write_uniform_matcx2_dynamic_access(
&mut self,
pointer: Handle<crate::Expression>,
block: &mut Block,
) -> Result<Option<Word>, Error> {
// If this access chain contains a dynamic matrix access, `pointer` is
// either a pointer to a vector (the column) or a scalar (a component
// within the column). In either case grab the pointer to the column,
// and remember the component index if there is one. If `pointer`
// points to any other type we're not interested.
let (column_pointer, component_index) = match self.fun_info[pointer]
.ty
.inner_with(&self.ir_module.types)
.pointer_base_type()
{
Some(resolution) => match *resolution.inner_with(&self.ir_module.types) {
crate::TypeInner::Scalar(_) => match self.ir_function.expressions[pointer] {
crate::Expression::Access { base, index } => {
(base, Some(GuardedIndex::Expression(index)))
}
crate::Expression::AccessIndex { base, index } => {
(base, Some(GuardedIndex::Known(index)))
}
_ => return Ok(None),
},
crate::TypeInner::Vector { .. } => (pointer, None),
_ => return Ok(None),
},
None => return Ok(None),
};
// Ensure the column is accessed with a dynamic index (i.e.
// `Expression::Access`), and grab the pointer to the matrix.
let crate::Expression::Access {
base: matrix_pointer,
index: column_index,
} = self.ir_function.expressions[column_pointer]
else {
return Ok(None);
};
// Ensure the matrix pointer is in the uniform address space.
let crate::TypeInner::Pointer {
base: matrix_pointer_base_type,
space: crate::AddressSpace::Uniform,
} = *self.fun_info[matrix_pointer]
.ty
.inner_with(&self.ir_module.types)
else {
return Ok(None);
};
// Ensure the matrix pointer actually points to a Cx2 matrix.
let crate::TypeInner::Matrix {
columns,
rows: rows @ crate::VectorSize::Bi,
scalar,
} = self.ir_module.types[matrix_pointer_base_type].inner
else {
return Ok(None);
};
let matrix_type_id = self.get_numeric_type_id(NumericType::Matrix {
columns,
rows,
scalar,
});
let column_type_id = self.get_numeric_type_id(NumericType::Vector { size: rows, scalar });
let component_type_id = self.get_numeric_type_id(NumericType::Scalar(scalar));
let get_column_function_id = self.writer.wrapped_functions
[&WrappedFunction::MatCx2GetColumn {
r#type: matrix_pointer_base_type,
}];
let matrix_load_id = self.write_checked_load(
matrix_pointer,
block,
AccessTypeAdjustment::None,
matrix_type_id,
)?;
// Naga IR allows the index to be either an I32 or U32 but our wrapper
// function expects a U32 argument, so convert it if required.
let column_index_id = match *self.fun_info[column_index]
.ty
.inner_with(&self.ir_module.types)
{
crate::TypeInner::Scalar(crate::Scalar {
kind: crate::ScalarKind::Uint,
..
}) => self.cached[column_index],
crate::TypeInner::Scalar(crate::Scalar {
kind: crate::ScalarKind::Sint,
..
}) => {
let cast_id = self.gen_id();
let u32_type_id = self.writer.get_u32_type_id();
block.body.push(Instruction::unary(
spirv::Op::Bitcast,
u32_type_id,
cast_id,
self.cached[column_index],
));
cast_id
}
_ => return Err(Error::Validation("Matrix access index must be u32 or i32")),
};
let column_id = self.gen_id();
block.body.push(Instruction::function_call(
column_type_id,
column_id,
get_column_function_id,
&[matrix_load_id, column_index_id],
));
let result_id = match component_index {
Some(index) => self.write_vector_access(
component_type_id,
column_pointer,
Some(column_id),
index,
block,
)?,
None => column_id,
};
Ok(Some(result_id))
}
/// If `pointer` refers to two-row matrix that is a member of a struct in
/// the [`Uniform`] address space, write code to load the matrix returning
/// the ID of the result. Else return None.
///
/// Two-row matrices that are struct members in the uniform address space
/// will have been decomposed such that the struct contains a separate
/// vector member for each column of the matrix. This function will load
/// each column separately from the containing struct, then composite them
/// into the real matrix type.
///
/// [`Uniform`]: crate::AddressSpace::Uniform
fn maybe_write_load_uniform_matcx2_struct_member(
&mut self,
pointer: Handle<crate::Expression>,
block: &mut Block,
) -> Result<Option<Word>, Error> {
// Check this is a uniform address space pointer to a two-row matrix.
let crate::TypeInner::Pointer {
base: matrix_type,
space: space @ crate::AddressSpace::Uniform,
} = *self.fun_info[pointer].ty.inner_with(&self.ir_module.types)
else {
return Ok(None);
};
let crate::TypeInner::Matrix {
columns,
rows: rows @ crate::VectorSize::Bi,
scalar,
} = self.ir_module.types[matrix_type].inner
else {
return Ok(None);
};
// Check this is a struct member. Note struct members can only be
// accessed with `AccessIndex`.
let crate::Expression::AccessIndex {
base: struct_pointer,
index: member_index,
} = self.ir_function.expressions[pointer]
else {
return Ok(None);
};
let crate::TypeInner::Pointer {
base: struct_type, ..
} = *self.fun_info[struct_pointer]
.ty
.inner_with(&self.ir_module.types)
else {
return Ok(None);
};
let crate::TypeInner::Struct { .. } = self.ir_module.types[struct_type].inner else {
return Ok(None);
};
let matrix_type_id = self.get_numeric_type_id(NumericType::Matrix {
columns,
rows,
scalar,
});
let column_type_id = self.get_numeric_type_id(NumericType::Vector { size: rows, scalar });
let column_pointer_type_id =
self.get_pointer_type_id(column_type_id, map_storage_class(space));
let column0_index = self.writer.std140_compat_uniform_types[&struct_type].member_indices
[member_index as usize];
let column_indices = (0..columns as u32)
.map(|c| self.get_index_constant(column0_index + c))
.collect::<ArrayVec<_, 4>>();
// Load each column from the struct, then composite into the real
// matrix type.
let load_mat_from_struct =
|struct_pointer_id: Word, id_gen: &mut IdGenerator, block: &mut Block| -> Word {
let mut column_ids: ArrayVec<Word, 4> = ArrayVec::new();
for index in &column_indices {
let column_pointer_id = id_gen.next();
block.body.push(Instruction::access_chain(
column_pointer_type_id,
column_pointer_id,
struct_pointer_id,
&[*index],
));
let column_id = id_gen.next();
block.body.push(Instruction::load(
column_type_id,
column_id,
column_pointer_id,
None,
));
column_ids.push(column_id);
}
let result_id = id_gen.next();
block.body.push(Instruction::composite_construct(
matrix_type_id,
result_id,
&column_ids,
));
result_id
};
let result_id = match self.write_access_chain(
struct_pointer,
block,
AccessTypeAdjustment::UseStd140CompatType,
)? {
ExpressionPointer::Ready { pointer_id } => {
load_mat_from_struct(pointer_id, &mut self.writer.id_gen, block)
}
ExpressionPointer::Conditional { condition, access } => self
.write_conditional_indexed_load(
matrix_type_id,
condition,
block,
|id_gen, block| {
let pointer_id = access.result_id.unwrap();
block.body.push(access);
load_mat_from_struct(pointer_id, id_gen, block)
},
),
};
Ok(Some(result_id))
}
/// Cache an expression for a value.
pub(super) fn cache_expression_value(
&mut self,
expr_handle: Handle<crate::Expression>,
block: &mut Block,
) -> Result<(), Error> {
let is_named_expression = self
.ir_function
.named_expressions
.contains_key(&expr_handle);
if self.fun_info[expr_handle].ref_count == 0 && !is_named_expression {
return Ok(());
}
let result_type_id = self.get_expression_type_id(&self.fun_info[expr_handle].ty);
let id = match self.ir_function.expressions[expr_handle] {
crate::Expression::Literal(literal) => self.writer.get_constant_scalar(literal),
crate::Expression::Constant(handle) => {
let init = self.ir_module.constants[handle].init;
self.writer.constant_ids[init]
}
crate::Expression::Override(_) => return Err(Error::Override),
crate::Expression::ZeroValue(_) => self.writer.get_constant_null(result_type_id),
crate::Expression::Compose { ty, ref components } => {
self.temp_list.clear();
if self.expression_constness.is_const(expr_handle) {
self.temp_list.extend(
crate::proc::flatten_compose(
ty,
components,
&self.ir_function.expressions,
&self.ir_module.types,
)
.map(|component| self.cached[component]),
);
self.writer
.get_constant_composite(LookupType::Handle(ty), &self.temp_list)
} else {
self.temp_list
.extend(components.iter().map(|&component| self.cached[component]));
let id = self.gen_id();
block.body.push(Instruction::composite_construct(
result_type_id,
id,
&self.temp_list,
));
id
}
}
crate::Expression::Splat { size, value } => {
let value_id = self.cached[value];
let components = &[value_id; 4][..size as usize];
if self.expression_constness.is_const(expr_handle) {
let ty = self
.writer
.get_expression_lookup_type(&self.fun_info[expr_handle].ty);
self.writer.get_constant_composite(ty, components)
} else {
let id = self.gen_id();
block.body.push(Instruction::composite_construct(
result_type_id,
id,
components,
));
id
}
}
crate::Expression::Access { base, index } => {
let base_ty_inner = self.fun_info[base].ty.inner_with(&self.ir_module.types);
match *base_ty_inner {
crate::TypeInner::Pointer { .. } | crate::TypeInner::ValuePointer { .. } => {
// When we have a chain of `Access` and `AccessIndex` expressions
// operating on pointers, we want to generate a single
// `OpAccessChain` instruction for the whole chain. Put off
// generating any code for this until we find the `Expression`
// that actually dereferences the pointer.
0
}
_ if self.function.spilled_accesses.contains(base) => {
// As far as Naga IR is concerned, this expression does not yield
// a pointer (we just checked, above), but this backend spilled it
// to a temporary variable, so SPIR-V thinks we're accessing it
// via a pointer.
// Since the base expression was spilled, mark this access to it
// as spilled, too.
self.function.spilled_accesses.insert(expr_handle);
self.maybe_access_spilled_composite(expr_handle, block, result_type_id)?
}
crate::TypeInner::Vector { .. } => self.write_vector_access(
result_type_id,
base,
None,
GuardedIndex::Expression(index),
block,
)?,
crate::TypeInner::Array { .. } | crate::TypeInner::Matrix { .. } => {
// See if `index` is known at compile time.
match GuardedIndex::from_expression(
index,
&self.ir_function.expressions,
self.ir_module,
) {
GuardedIndex::Known(value) => {
// If `index` is known and in bounds, we can just use
// `OpCompositeExtract`.
//
// At the moment, validation rejects programs if this
// index is out of bounds, so we don't need bounds checks.
// However, that rejection is incorrect, since WGSL says
// that `let` bindings are not constant expressions
// (#6396). So eventually we will need to emulate bounds
// checks here.
let id = self.gen_id();
let base_id = self.cached[base];
block.body.push(Instruction::composite_extract(
result_type_id,
id,
base_id,
&[value],
));
id
}
GuardedIndex::Expression(_) => {
// We are subscripting an array or matrix that is not
// behind a pointer, using an index computed at runtime.
// SPIR-V has no instructions that do this, so the best we
// can do is spill the value to a new temporary variable,
// at which point we can get a pointer to that and just
// use `OpAccessChain` in the usual way.
self.spill_to_internal_variable(base, block);
// Since the base was spilled, mark this access to it as
// spilled, too.
self.function.spilled_accesses.insert(expr_handle);
self.maybe_access_spilled_composite(
expr_handle,
block,
result_type_id,
)?
}
}
}
crate::TypeInner::BindingArray {
base: binding_type, ..
} => {
// Only binding arrays in the `Handle` address space will take
// this path, since we handled the `Pointer` case above.
let result_id = match self.write_access_chain(
expr_handle,
block,
AccessTypeAdjustment::IntroducePointer(
spirv::StorageClass::UniformConstant,
),
)? {
ExpressionPointer::Ready { pointer_id } => pointer_id,
ExpressionPointer::Conditional { .. } => {
return Err(Error::FeatureNotImplemented(
"Texture array out-of-bounds handling",
));
}
};
let binding_type_id = self.get_handle_type_id(binding_type);
let load_id = self.gen_id();
block.body.push(Instruction::load(
binding_type_id,
load_id,
result_id,
None,
));
// Subsequent image operations require the image/sampler to be decorated as NonUniform
// if the image/sampler binding array was accessed with a non-uniform index
// see VUID-RuntimeSpirv-NonUniform-06274
if self.fun_info[index].uniformity.non_uniform_result.is_some() {
self.writer
.decorate_non_uniform_binding_array_access(load_id)?;
}
load_id
}
ref other => {
log::error!(
"Unable to access base {:?} of type {:?}",
self.ir_function.expressions[base],
other
);
return Err(Error::Validation(
"only vectors and arrays may be dynamically indexed by value",
));
}
}
}
crate::Expression::AccessIndex { base, index } => {
match *self.fun_info[base].ty.inner_with(&self.ir_module.types) {
crate::TypeInner::Pointer { .. } | crate::TypeInner::ValuePointer { .. } => {
// When we have a chain of `Access` and `AccessIndex` expressions
// operating on pointers, we want to generate a single
// `OpAccessChain` instruction for the whole chain. Put off
// generating any code for this until we find the `Expression`
// that actually dereferences the pointer.
0
}
_ if self.function.spilled_accesses.contains(base) => {
// As far as Naga IR is concerned, this expression does not yield
// a pointer (we just checked, above), but this backend spilled it
// to a temporary variable, so SPIR-V thinks we're accessing it
// via a pointer.
// Since the base expression was spilled, mark this access to it
// as spilled, too.
self.function.spilled_accesses.insert(expr_handle);
self.maybe_access_spilled_composite(expr_handle, block, result_type_id)?
}
crate::TypeInner::Vector { .. }
| crate::TypeInner::Matrix { .. }
| crate::TypeInner::Array { .. }
| crate::TypeInner::Struct { .. } => {
// We never need bounds checks here: dynamically sized arrays can
// only appear behind pointers, and are thus handled by the
// `is_intermediate` case above. Everything else's size is
// statically known and checked in validation.
let id = self.gen_id();
let base_id = self.cached[base];
block.body.push(Instruction::composite_extract(
result_type_id,
id,
base_id,
&[index],
));
id
}
crate::TypeInner::BindingArray {
base: binding_type, ..
} => {
// Only binding arrays in the `Handle` address space will take
// this path, since we handled the `Pointer` case above.
let result_id = match self.write_access_chain(
expr_handle,
block,
AccessTypeAdjustment::IntroducePointer(
spirv::StorageClass::UniformConstant,
),
)? {
ExpressionPointer::Ready { pointer_id } => pointer_id,
ExpressionPointer::Conditional { .. } => {
return Err(Error::FeatureNotImplemented(
"Texture array out-of-bounds handling",
));
}
};
let binding_type_id = self.get_handle_type_id(binding_type);
let load_id = self.gen_id();
block.body.push(Instruction::load(
binding_type_id,
load_id,
result_id,
None,
));
load_id
}
ref other => {
log::error!("Unable to access index of {other:?}");
return Err(Error::FeatureNotImplemented("access index for type"));
}
}
}
crate::Expression::GlobalVariable(handle) => {
self.writer.global_variables[handle].access_id
}
crate::Expression::Swizzle {
size,
vector,
pattern,
} => {
let vector_id = self.cached[vector];
self.temp_list.clear();
for &sc in pattern[..size as usize].iter() {
self.temp_list.push(sc as Word);
}
let id = self.gen_id();
block.body.push(Instruction::vector_shuffle(
result_type_id,
id,
vector_id,
vector_id,
&self.temp_list,
));
id
}
crate::Expression::Unary { op, expr } => {