Debian Codegraph @

third_party/rust/naga/src/back/spv/image.rs

raw
1 /*!
2 Generating SPIR-V for image operations.
3 */
4
5 use spirv::Word;
6
7 use super::{
8 selection::{MergeTuple, Selection},
9 Block, BlockContext, Error, IdGenerator, Instruction, LocalType, LookupType, NumericType,
10 };
11 use crate::arena::Handle;
12
13 /// Information about a vector of coordinates.
14 ///
15 /// The coordinate vectors expected by SPIR-V `OpImageRead` and `OpImageFetch`
16 /// supply the array index for arrayed images as an additional component at
17 /// the end, whereas Naga's `ImageLoad`, `ImageStore`, and `ImageSample` carry
18 /// the array index as a separate field.
19 ///
20 /// In the process of generating code to compute the combined vector, we also
21 /// produce SPIR-V types and vector lengths that are useful elsewhere. This
22 /// struct gathers that information into one place, with standard names.
23 struct ImageCoordinates {
24 /// The SPIR-V id of the combined coordinate/index vector value.
25 ///
26 /// Note: when indexing a non-arrayed 1D image, this will be a scalar.
27 value_id: Word,
28
29 /// The SPIR-V id of the type of `value`.
30 type_id: Word,
31
32 /// The number of components in `value`, if it is a vector, or `None` if it
33 /// is a scalar.
34 size: Option<crate::VectorSize>,
35 }
36
37 /// A trait for image access (load or store) code generators.
38 ///
39 /// Types implementing this trait hold information about an `ImageStore` or
40 /// `ImageLoad` operation that is not affected by the bounds check policy. The
41 /// `generate` method emits code for the access, given the results of bounds
42 /// checking.
43 ///
44 /// The [`image`] bounds checks policy affects access coordinates, level of
45 /// detail, and sample index, but never the image id, result type (if any), or
46 /// the specific SPIR-V instruction used. Types that implement this trait gather
47 /// together the latter category, so we don't have to plumb them through the
48 /// bounds-checking code.
49 ///
50 /// [`image`]: crate::proc::BoundsCheckPolicies::index
51 trait Access {
52 /// The Rust type that represents SPIR-V values and types for this access.
53 ///
54 /// For operations like loads, this is `Word`. For operations like stores,
55 /// this is `()`.
56 ///
57 /// For `ReadZeroSkipWrite`, this will be the type of the selection
58 /// construct that performs the bounds checks, so it must implement
59 /// `MergeTuple`.
60 type Output: MergeTuple + Copy + Clone;
61
62 /// Write an image access to `block`.
63 ///
64 /// Access the texel at `coordinates_id`. The optional `level_id` indicates
65 /// the level of detail, and `sample_id` is the index of the sample to
66 /// access in a multisampled texel.
67 ///
68 /// This method assumes that `coordinates_id` has already had the image array
69 /// index, if any, folded in, as done by `write_image_coordinates`.
70 ///
71 /// Return the value id produced by the instruction, if any.
72 ///
73 /// Use `id_gen` to generate SPIR-V ids as necessary.
74 fn generate(
75 &self,
76 id_gen: &mut IdGenerator,
77 coordinates_id: Word,
78 level_id: Option<Word>,
79 sample_id: Option<Word>,
80 block: &mut Block,
81 ) -> Self::Output;
82
83 /// Return the SPIR-V type of the value produced by the code written by
84 /// `generate`. If the access does not produce a value, `Self::Output`
85 /// should be `()`.
86 fn result_type(&self) -> Self::Output;
87
88 /// Construct the SPIR-V 'zero' value to be returned for an out-of-bounds
89 /// access under the `ReadZeroSkipWrite` policy. If the access does not
90 /// produce a value, `Self::Output` should be `()`.
91 fn out_of_bounds_value(&self, ctx: &mut BlockContext<'_>) -> Self::Output;
92 }
93
94 /// Texel access information for an [`ImageLoad`] expression.
95 ///
96 /// [`ImageLoad`]: crate::Expression::ImageLoad
97 struct Load {
98 /// The specific opcode we'll use to perform the fetch. Storage images
99 /// require `OpImageRead`, while sampled images require `OpImageFetch`.
100 opcode: spirv::Op,
101
102 /// The type id produced by the actual image access instruction.
103 type_id: Word,
104
105 /// The id of the image being accessed.
106 image_id: Word,
107 }
108
109 impl Load {
110 fn from_image_expr(
111 ctx: &mut BlockContext<'_>,
112 image_id: Word,
113 image_class: crate::ImageClass,
114 result_type_id: Word,
115 ) -> Result<Load, Error> {
116 let opcode = match image_class {
117 crate::ImageClass::Storage { .. } => spirv::Op::ImageRead,
118 crate::ImageClass::Depth { .. } | crate::ImageClass::Sampled { .. } => {
119 spirv::Op::ImageFetch
120 }
121 };
122
123 // `OpImageRead` and `OpImageFetch` instructions produce vec4<f32>
124 // values. Most of the time, we can just use `result_type_id` for
125 // this. The exception is that `Expression::ImageLoad` from a depth
126 // image produces a scalar `f32`, so in that case we need to find
127 // the right SPIR-V type for the access instruction here.
128 let type_id = match image_class {
129 crate::ImageClass::Depth { .. } => ctx.get_numeric_type_id(NumericType::Vector {
130 size: crate::VectorSize::Quad,
131 scalar: crate::Scalar::F32,
132 }),
133 _ => result_type_id,
134 };
135
136 Ok(Load {
137 opcode,
138 type_id,
139 image_id,
140 })
141 }
142 }
143
144 impl Access for Load {
145 type Output = Word;
146
147 /// Write an instruction to access a given texel of this image.
148 fn generate(
149 &self,
150 id_gen: &mut IdGenerator,
151 coordinates_id: Word,
152 level_id: Option<Word>,
153 sample_id: Option<Word>,
154 block: &mut Block,
155 ) -> Word {
156 let texel_id = id_gen.next();
157 let mut instruction = Instruction::image_fetch_or_read(
158 self.opcode,
159 self.type_id,
160 texel_id,
161 self.image_id,
162 coordinates_id,
163 );
164
165 match (level_id, sample_id) {
166 (None, None) => {}
167 (Some(level_id), None) => {
168 instruction.add_operand(spirv::ImageOperands::LOD.bits());
169 instruction.add_operand(level_id);
170 }
171 (None, Some(sample_id)) => {
172 instruction.add_operand(spirv::ImageOperands::SAMPLE.bits());
173 instruction.add_operand(sample_id);
174 }
175 // There's no such thing as a multi-sampled mipmap.
176 (Some(_), Some(_)) => unreachable!(),
177 }
178
179 block.body.push(instruction);
180
181 texel_id
182 }
183
184 fn result_type(&self) -> Word {
185 self.type_id
186 }
187
188 fn out_of_bounds_value(&self, ctx: &mut BlockContext<'_>) -> Word {
189 ctx.writer.get_constant_null(self.type_id)
190 }
191 }
192
193 /// Texel access information for a [`Store`] statement.
194 ///
195 /// [`Store`]: crate::Statement::Store
196 struct Store {
197 /// The id of the image being written to.
198 image_id: Word,
199
200 /// The value we're going to write to the texel.
201 value_id: Word,
202 }
203
204 impl Access for Store {
205 /// Stores don't generate any value.
206 type Output = ();
207
208 fn generate(
209 &self,
210 _id_gen: &mut IdGenerator,
211 coordinates_id: Word,
212 _level_id: Option<Word>,
213 _sample_id: Option<Word>,
214 block: &mut Block,
215 ) {
216 block.body.push(Instruction::image_write(
217 self.image_id,
218 coordinates_id,
219 self.value_id,
220 ));
221 }
222
223 /// Stores don't generate any value, so this just returns `()`.
224 fn result_type(&self) {}
225
226 /// Stores don't generate any value, so this just returns `()`.
227 fn out_of_bounds_value(&self, _ctx: &mut BlockContext<'_>) {}
228 }
229
230 impl BlockContext<'_> {
231 /// Extend image coordinates with an array index, if necessary.
232 ///
233 /// Whereas [`Expression::ImageLoad`] and [`ImageSample`] treat the array
234 /// index as a separate operand from the coordinates, SPIR-V image access
235 /// instructions include the array index in the `coordinates` operand. This
236 /// function builds a SPIR-V coordinate vector from a Naga coordinate vector
237 /// and array index, if one is supplied, and returns a `ImageCoordinates`
238 /// struct describing what it built.
239 ///
240 /// If `array_index` is `Some(expr)`, then this function constructs a new
241 /// vector that is `coordinates` with `array_index` concatenated onto the
242 /// end: a `vec2` becomes a `vec3`, a scalar becomes a `vec2`, and so on.
243 ///
244 /// If `array_index` is `None`, then the return value uses `coordinates`
245 /// unchanged. Note that, when indexing a non-arrayed 1D image, this will be
246 /// a scalar value.
247 ///
248 /// If needed, this function generates code to convert the array index,
249 /// always an integer scalar, to match the component type of `coordinates`.
250 /// Naga's `ImageLoad` and SPIR-V's `OpImageRead`, `OpImageFetch`, and
251 /// `OpImageWrite` all use integer coordinates, while Naga's `ImageSample`
252 /// and SPIR-V's `OpImageSample...` instructions all take floating-point
253 /// coordinate vectors.
254 ///
255 /// [`Expression::ImageLoad`]: crate::Expression::ImageLoad
256 /// [`ImageSample`]: crate::Expression::ImageSample
257 fn write_image_coordinates(
258 &mut self,
259 coordinates: Handle<crate::Expression>,
260 array_index: Option<Handle<crate::Expression>>,
261 block: &mut Block,
262 ) -> Result<ImageCoordinates, Error> {
263 use crate::TypeInner as Ti;
264 use crate::VectorSize as Vs;
265
266 let coordinates_id = self.cached[coordinates];
267 let ty = &self.fun_info[coordinates].ty;
268 let inner_ty = ty.inner_with(&self.ir_module.types);
269
270 // If there's no array index, the image coordinates are exactly the
271 // `coordinate` field of the `Expression::ImageLoad`. No work is needed.
272 let array_index = match array_index {
273 None => {
274 let value_id = coordinates_id;
275 let type_id = self.get_expression_type_id(ty);
276 let size = match *inner_ty {
277 Ti::Scalar { .. } => None,
278 Ti::Vector { size, .. } => Some(size),
279 _ => return Err(Error::Validation("coordinate type")),
280 };
281 return Ok(ImageCoordinates {
282 value_id,
283 type_id,
284 size,
285 });
286 }
287 Some(ix) => ix,
288 };
289
290 // Find the component type of `coordinates`, and figure out the size the
291 // combined coordinate vector will have.
292 let (component_scalar, size) = match *inner_ty {
293 Ti::Scalar(scalar @ crate::Scalar { width: 4, .. }) => (scalar, Vs::Bi),
294 Ti::Vector {
295 scalar: scalar @ crate::Scalar { width: 4, .. },
296 size: Vs::Bi,
297 } => (scalar, Vs::Tri),
298 Ti::Vector {
299 scalar: scalar @ crate::Scalar { width: 4, .. },
300 size: Vs::Tri,
301 } => (scalar, Vs::Quad),
302 Ti::Vector { size: Vs::Quad, .. } => {
303 return Err(Error::Validation("extending vec4 coordinate"));
304 }
305 ref other => {
306 log::error!("wrong coordinate type {:?}", other);
307 return Err(Error::Validation("coordinate type"));
308 }
309 };
310
311 // Convert the index to the coordinate component type, if necessary.
312 let array_index_id = self.cached[array_index];
313 let ty = &self.fun_info[array_index].ty;
314 let inner_ty = ty.inner_with(&self.ir_module.types);
315 let array_index_scalar = match *inner_ty {
316 Ti::Scalar(
317 scalar @ crate::Scalar {
318 kind: crate::ScalarKind::Sint | crate::ScalarKind::Uint,
319 width: 4,
320 },
321 ) => scalar,
322 _ => unreachable!("we only allow i32 and u32"),
323 };
324 let cast = match (component_scalar.kind, array_index_scalar.kind) {
325 (crate::ScalarKind::Sint, crate::ScalarKind::Sint)
326 | (crate::ScalarKind::Uint, crate::ScalarKind::Uint) => None,
327 (crate::ScalarKind::Sint, crate::ScalarKind::Uint)
328 | (crate::ScalarKind::Uint, crate::ScalarKind::Sint) => Some(spirv::Op::Bitcast),
329 (crate::ScalarKind::Float, crate::ScalarKind::Sint) => Some(spirv::Op::ConvertSToF),
330 (crate::ScalarKind::Float, crate::ScalarKind::Uint) => Some(spirv::Op::ConvertUToF),
331 (crate::ScalarKind::Bool, _) => unreachable!("we don't allow bool for component"),
332 (_, crate::ScalarKind::Bool | crate::ScalarKind::Float) => {
333 unreachable!("we don't allow bool or float for array index")
334 }
335 (crate::ScalarKind::AbstractInt | crate::ScalarKind::AbstractFloat, _)
336 | (_, crate::ScalarKind::AbstractInt | crate::ScalarKind::AbstractFloat) => {
337 unreachable!("abstract types should never reach backends")
338 }
339 };
340 let reconciled_array_index_id = if let Some(cast) = cast {
341 let component_ty_id = self.get_numeric_type_id(NumericType::Scalar(component_scalar));
342 let reconciled_id = self.gen_id();
343 block.body.push(Instruction::unary(
344 cast,
345 component_ty_id,
346 reconciled_id,
347 array_index_id,
348 ));
349 reconciled_id
350 } else {
351 array_index_id
352 };
353
354 // Find the SPIR-V type for the combined coordinates/index vector.
355 let type_id = self.get_numeric_type_id(NumericType::Vector {
356 size,
357 scalar: component_scalar,
358 });
359
360 // Schmear the coordinates and index together.
361 let value_id = self.gen_id();
362 block.body.push(Instruction::composite_construct(
363 type_id,
364 value_id,
365 &[coordinates_id, reconciled_array_index_id],
366 ));
367 Ok(ImageCoordinates {
368 value_id,
369 type_id,
370 size: Some(size),
371 })
372 }
373
374 pub(super) fn get_handle_id(&mut self, expr_handle: Handle<crate::Expression>) -> Word {
375 let id = match self.ir_function.expressions[expr_handle] {
376 crate::Expression::GlobalVariable(handle) => {
377 self.writer.global_variables[handle].handle_id
378 }
379 crate::Expression::FunctionArgument(i) => {
380 self.function.parameters[i as usize].handle_id
381 }
382 crate::Expression::Access { .. } | crate::Expression::AccessIndex { .. } => {
383 self.cached[expr_handle]
384 }
385 ref other => unreachable!("Unexpected image expression {:?}", other),
386 };
387
388 if id == 0 {
389 unreachable!(
390 "Image expression {:?} doesn't have a handle ID",
391 expr_handle
392 );
393 }
394
395 id
396 }
397
398 /// Generate a vector or scalar 'one' for arithmetic on `coordinates`.
399 ///
400 /// If `coordinates` is a scalar, return a scalar one. Otherwise, return
401 /// a vector of ones.
402 fn write_coordinate_one(&mut self, coordinates: &ImageCoordinates) -> Result<Word, Error> {
403 let one = self.get_scope_constant(1);
404 match coordinates.size {
405 None => Ok(one),
406 Some(vector_size) => {
407 let ones = [one; 4];
408 let id = self.gen_id();
409 Instruction::constant_composite(
410 coordinates.type_id,
411 id,
412 &ones[..vector_size as usize],
413 )
414 .to_words(&mut self.writer.logical_layout.declarations);
415 Ok(id)
416 }
417 }
418 }
419
420 /// Generate code to restrict `input` to fall between zero and one less than
421 /// `size_id`.
422 ///
423 /// Both must be 32-bit scalar integer values, whose type is given by
424 /// `type_id`. The computed value is also of type `type_id`.
425 fn restrict_scalar(
426 &mut self,
427 type_id: Word,
428 input_id: Word,
429 size_id: Word,
430 block: &mut Block,
431 ) -> Result<Word, Error> {
432 let i32_one_id = self.get_scope_constant(1);
433
434 // Subtract one from `size` to get the largest valid value.
435 let limit_id = self.gen_id();
436 block.body.push(Instruction::binary(
437 spirv::Op::ISub,
438 type_id,
439 limit_id,
440 size_id,
441 i32_one_id,
442 ));
443
444 // Use an unsigned minimum, to handle both positive out-of-range values
445 // and negative values in a single instruction: negative values of
446 // `input_id` get treated as very large positive values.
447 let restricted_id = self.gen_id();
448 block.body.push(Instruction::ext_inst(
449 self.writer.gl450_ext_inst_id,
450 spirv::GLOp::UMin,
451 type_id,
452 restricted_id,
453 &[input_id, limit_id],
454 ));
455
456 Ok(restricted_id)
457 }
458
459 /// Write instructions to query the size of an image.
460 ///
461 /// This takes care of selecting the right instruction depending on whether
462 /// a level of detail parameter is present.
463 fn write_coordinate_bounds(
464 &mut self,
465 type_id: Word,
466 image_id: Word,
467 level_id: Option<Word>,
468 block: &mut Block,
469 ) -> Word {
470 let coordinate_bounds_id = self.gen_id();
471 match level_id {
472 Some(level_id) => {
473 // A level of detail was provided, so fetch the image size for
474 // that level.
475 let mut inst = Instruction::image_query(
476 spirv::Op::ImageQuerySizeLod,
477 type_id,
478 coordinate_bounds_id,
479 image_id,
480 );
481 inst.add_operand(level_id);
482 block.body.push(inst);
483 }
484 _ => {
485 // No level of detail was given.
486 block.body.push(Instruction::image_query(
487 spirv::Op::ImageQuerySize,
488 type_id,
489 coordinate_bounds_id,
490 image_id,
491 ));
492 }
493 }
494
495 coordinate_bounds_id
496 }
497
498 /// Write code to restrict coordinates for an image reference.
499 ///
500 /// First, clamp the level of detail or sample index to fall within bounds.
501 /// Then, obtain the image size, possibly using the clamped level of detail.
502 /// Finally, use an unsigned minimum instruction to force all coordinates
503 /// into range.
504 ///
505 /// Return a triple `(COORDS, LEVEL, SAMPLE)`, where `COORDS` is a coordinate
506 /// vector (including the array index, if any), `LEVEL` is an optional level
507 /// of detail, and `SAMPLE` is an optional sample index, all guaranteed to
508 /// be in-bounds for `image_id`.
509 ///
510 /// The result is usually a vector, but it is a scalar when indexing
511 /// non-arrayed 1D images.
512 fn write_restricted_coordinates(
513 &mut self,
514 image_id: Word,
515 coordinates: ImageCoordinates,
516 level_id: Option<Word>,
517 sample_id: Option<Word>,
518 block: &mut Block,
519 ) -> Result<(Word, Option<Word>, Option<Word>), Error> {
520 self.writer.require_any(
521 "the `Restrict` image bounds check policy",
522 &[spirv::Capability::ImageQuery],
523 )?;
524
525 let i32_type_id = self.get_numeric_type_id(NumericType::Scalar(crate::Scalar::I32));
526
527 // If `level` is `Some`, clamp it to fall within bounds. This must
528 // happen first, because we'll use it to query the image size for
529 // clamping the actual coordinates.
530 let level_id = level_id
531 .map(|level_id| {
532 // Find the number of mipmap levels in this image.
533 let num_levels_id = self.gen_id();
534 block.body.push(Instruction::image_query(
535 spirv::Op::ImageQueryLevels,
536 i32_type_id,
537 num_levels_id,
538 image_id,
539 ));
540
541 self.restrict_scalar(i32_type_id, level_id, num_levels_id, block)
542 })
543 .transpose()?;
544
545 // If `sample_id` is `Some`, clamp it to fall within bounds.
546 let sample_id = sample_id
547 .map(|sample_id| {
548 // Find the number of samples per texel.
549 let num_samples_id = self.gen_id();
550 block.body.push(Instruction::image_query(
551 spirv::Op::ImageQuerySamples,
552 i32_type_id,
553 num_samples_id,
554 image_id,
555 ));
556
557 self.restrict_scalar(i32_type_id, sample_id, num_samples_id, block)
558 })
559 .transpose()?;
560
561 // Obtain the image bounds, including the array element count.
562 let coordinate_bounds_id =
563 self.write_coordinate_bounds(coordinates.type_id, image_id, level_id, block);
564
565 // Compute maximum valid values from the bounds.
566 let ones = self.write_coordinate_one(&coordinates)?;
567 let coordinate_limit_id = self.gen_id();
568 block.body.push(Instruction::binary(
569 spirv::Op::ISub,
570 coordinates.type_id,
571 coordinate_limit_id,
572 coordinate_bounds_id,
573 ones,
574 ));
575
576 // Restrict the coordinates to fall within those bounds.
577 //
578 // Use an unsigned minimum, to handle both positive out-of-range values
579 // and negative values in a single instruction: negative values of
580 // `coordinates` get treated as very large positive values.
581 let restricted_coordinates_id = self.gen_id();
582 block.body.push(Instruction::ext_inst(
583 self.writer.gl450_ext_inst_id,
584 spirv::GLOp::UMin,
585 coordinates.type_id,
586 restricted_coordinates_id,
587 &[coordinates.value_id, coordinate_limit_id],
588 ));
589
590 Ok((restricted_coordinates_id, level_id, sample_id))
591 }
592
593 fn write_conditional_image_access<A: Access>(
594 &mut self,
595 image_id: Word,
596 coordinates: ImageCoordinates,
597 level_id: Option<Word>,
598 sample_id: Option<Word>,
599 block: &mut Block,
600 access: &A,
601 ) -> Result<A::Output, Error> {
602 self.writer.require_any(
603 "the `ReadZeroSkipWrite` image bounds check policy",
604 &[spirv::Capability::ImageQuery],
605 )?;
606
607 let bool_type_id = self.writer.get_bool_type_id();
608 let i32_type_id = self.get_numeric_type_id(NumericType::Scalar(crate::Scalar::I32));
609
610 let null_id = access.out_of_bounds_value(self);
611
612 let mut selection = Selection::start(block, access.result_type());
613
614 // If `level_id` is `Some`, check whether it is within bounds. This must
615 // happen first, because we'll be supplying this as an argument when we
616 // query the image size.
617 if let Some(level_id) = level_id {
618 // Find the number of mipmap levels in this image.
619 let num_levels_id = self.gen_id();
620 selection.block().body.push(Instruction::image_query(
621 spirv::Op::ImageQueryLevels,
622 i32_type_id,
623 num_levels_id,
624 image_id,
625 ));
626
627 let lod_cond_id = self.gen_id();
628 selection.block().body.push(Instruction::binary(
629 spirv::Op::ULessThan,
630 bool_type_id,
631 lod_cond_id,
632 level_id,
633 num_levels_id,
634 ));
635
636 selection.if_true(self, lod_cond_id, null_id);
637 }
638
639 // If `sample_id` is `Some`, check whether it is in bounds.
640 if let Some(sample_id) = sample_id {
641 // Find the number of samples per texel.
642 let num_samples_id = self.gen_id();
643 selection.block().body.push(Instruction::image_query(
644 spirv::Op::ImageQuerySamples,
645 i32_type_id,
646 num_samples_id,
647 image_id,
648 ));
649
650 let samples_cond_id = self.gen_id();
651 selection.block().body.push(Instruction::binary(
652 spirv::Op::ULessThan,
653 bool_type_id,
654 samples_cond_id,
655 sample_id,
656 num_samples_id,
657 ));
658
659 selection.if_true(self, samples_cond_id, null_id);
660 }
661
662 // Obtain the image bounds, including any array element count.
663 let coordinate_bounds_id = self.write_coordinate_bounds(
664 coordinates.type_id,
665 image_id,
666 level_id,
667 selection.block(),
668 );
669
670 // Compare the coordinates against the bounds.
671 let coords_numeric_type = match coordinates.size {
672 Some(size) => NumericType::Vector {
673 size,
674 scalar: crate::Scalar::BOOL,
675 },
676 None => NumericType::Scalar(crate::Scalar::BOOL),
677 };
678 let coords_bool_type_id = self.get_numeric_type_id(coords_numeric_type);
679 let coords_conds_id = self.gen_id();
680 selection.block().body.push(Instruction::binary(
681 spirv::Op::ULessThan,
682 coords_bool_type_id,
683 coords_conds_id,
684 coordinates.value_id,
685 coordinate_bounds_id,
686 ));
687
688 // If the comparison above was a vector comparison, then we need to
689 // check that all components of the comparison are true.
690 let coords_cond_id = if coords_bool_type_id != bool_type_id {
691 let id = self.gen_id();
692 selection.block().body.push(Instruction::relational(
693 spirv::Op::All,
694 bool_type_id,
695 id,
696 coords_conds_id,
697 ));
698 id
699 } else {
700 coords_conds_id
701 };
702
703 selection.if_true(self, coords_cond_id, null_id);
704
705 // All conditions are met. We can carry out the access.
706 let texel_id = access.generate(
707 &mut self.writer.id_gen,
708 coordinates.value_id,
709 level_id,
710 sample_id,
711 selection.block(),
712 );
713
714 // This, then, is the value of the 'true' branch.
715 Ok(selection.finish(self, texel_id))
716 }
717
718 /// Generate code for an `ImageLoad` expression.
719 ///
720 /// The arguments are the components of an `Expression::ImageLoad` variant.
721 #[allow(clippy::too_many_arguments)]
722 pub(super) fn write_image_load(
723 &mut self,
724 result_type_id: Word,
725 image: Handle<crate::Expression>,
726 coordinate: Handle<crate::Expression>,
727 array_index: Option<Handle<crate::Expression>>,
728 level: Option<Handle<crate::Expression>>,
729 sample: Option<Handle<crate::Expression>>,
730 block: &mut Block,
731 ) -> Result<Word, Error> {
732 let image_id = self.get_handle_id(image);
733 let image_type = self.fun_info[image].ty.inner_with(&self.ir_module.types);
734 let image_class = match *image_type {
735 crate::TypeInner::Image { class, .. } => class,
736 _ => return Err(Error::Validation("image type")),
737 };
738
739 let access = Load::from_image_expr(self, image_id, image_class, result_type_id)?;
740 let coordinates = self.write_image_coordinates(coordinate, array_index, block)?;
741
742 let level_id = level.map(|expr| self.cached[expr]);
743 let sample_id = sample.map(|expr| self.cached[expr]);
744
745 // Perform the access, according to the bounds check policy.
746 let access_id = match self.writer.bounds_check_policies.image_load {
747 crate::proc::BoundsCheckPolicy::Restrict => {
748 let (coords, level_id, sample_id) = self.write_restricted_coordinates(
749 image_id,
750 coordinates,
751 level_id,
752 sample_id,
753 block,
754 )?;
755 access.generate(&mut self.writer.id_gen, coords, level_id, sample_id, block)
756 }
757 crate::proc::BoundsCheckPolicy::ReadZeroSkipWrite => self
758 .write_conditional_image_access(
759 image_id,
760 coordinates,
761 level_id,
762 sample_id,
763 block,
764 &access,
765 )?,
766 crate::proc::BoundsCheckPolicy::Unchecked => access.generate(
767 &mut self.writer.id_gen,
768 coordinates.value_id,
769 level_id,
770 sample_id,
771 block,
772 ),
773 };
774
775 // For depth images, `ImageLoad` expressions produce a single f32,
776 // whereas the SPIR-V instructions always produce a vec4. So we may have
777 // to pull out the component we need.
778 let result_id = if result_type_id == access.result_type() {
779 // The instruction produced the type we expected. We can use
780 // its result as-is.
781 access_id
782 } else {
783 // For `ImageClass::Depth` images, SPIR-V gave us four components,
784 // but we only want the first one.
785 let component_id = self.gen_id();
786 block.body.push(Instruction::composite_extract(
787 result_type_id,
788 component_id,
789 access_id,
790 &[0],
791 ));
792 component_id
793 };
794
795 Ok(result_id)
796 }
797
798 /// Generate code for an `ImageSample` expression.
799 ///
800 /// The arguments are the components of an `Expression::ImageSample` variant.
801 #[allow(clippy::too_many_arguments)]
802 pub(super) fn write_image_sample(
803 &mut self,
804 result_type_id: Word,
805 image: Handle<crate::Expression>,
806 sampler: Handle<crate::Expression>,
807 gather: Option<crate::SwizzleComponent>,
808 coordinate: Handle<crate::Expression>,
809 array_index: Option<Handle<crate::Expression>>,
810 offset: Option<Handle<crate::Expression>>,
811 level: crate::SampleLevel,
812 depth_ref: Option<Handle<crate::Expression>>,
813 block: &mut Block,
814 ) -> Result<Word, Error> {
815 use super::instructions::SampleLod;
816 // image
817 let image_id = self.get_handle_id(image);
818 let image_type = self.fun_info[image].ty.handle().unwrap();
819 // SPIR-V doesn't know about our `Depth` class, and it returns
820 // `vec4<f32>`, so we need to grab the first component out of it.
821 let needs_sub_access = match self.ir_module.types[image_type].inner {
822 crate::TypeInner::Image {
823 class: crate::ImageClass::Depth { .. },
824 ..
825 } => depth_ref.is_none() && gather.is_none(),
826 _ => false,
827 };
828 let sample_result_type_id = if needs_sub_access {
829 self.get_numeric_type_id(NumericType::Vector {
830 size: crate::VectorSize::Quad,
831 scalar: crate::Scalar::F32,
832 })
833 } else {
834 result_type_id
835 };
836
837 // OpTypeSampledImage
838 let image_type_id = self.get_handle_type_id(image_type);
839 let sampled_image_type_id =
840 self.get_type_id(LookupType::Local(LocalType::SampledImage { image_type_id }));
841
842 let sampler_id = self.get_handle_id(sampler);
843 let coordinates_id = self
844 .write_image_coordinates(coordinate, array_index, block)?
845 .value_id;
846
847 let sampled_image_id = self.gen_id();
848 block.body.push(Instruction::sampled_image(
849 sampled_image_type_id,
850 sampled_image_id,
851 image_id,
852 sampler_id,
853 ));
854 let id = self.gen_id();
855
856 let depth_id = depth_ref.map(|handle| self.cached[handle]);
857 let mut mask = spirv::ImageOperands::empty();
858 mask.set(spirv::ImageOperands::CONST_OFFSET, offset.is_some());
859
860 let mut main_instruction = match (level, gather) {
861 (_, Some(component)) => {
862 let component_id = self.get_index_constant(component as u32);
863 let mut inst = Instruction::image_gather(
864 sample_result_type_id,
865 id,
866 sampled_image_id,
867 coordinates_id,
868 component_id,
869 depth_id,
870 );
871 if !mask.is_empty() {
872 inst.add_operand(mask.bits());
873 }
874 inst
875 }
876 (crate::SampleLevel::Zero, None) => {
877 let mut inst = Instruction::image_sample(
878 sample_result_type_id,
879 id,
880 SampleLod::Explicit,
881 sampled_image_id,
882 coordinates_id,
883 depth_id,
884 );
885
886 let zero_id = self.writer.get_constant_scalar(crate::Literal::F32(0.0));
887
888 mask |= spirv::ImageOperands::LOD;
889 inst.add_operand(mask.bits());
890 inst.add_operand(zero_id);
891
892 inst
893 }
894 (crate::SampleLevel::Auto, None) => {
895 let mut inst = Instruction::image_sample(
896 sample_result_type_id,
897 id,
898 SampleLod::Implicit,
899 sampled_image_id,
900 coordinates_id,
901 depth_id,
902 );
903 if !mask.is_empty() {
904 inst.add_operand(mask.bits());
905 }
906 inst
907 }
908 (crate::SampleLevel::Exact(lod_handle), None) => {
909 let mut inst = Instruction::image_sample(
910 sample_result_type_id,
911 id,
912 SampleLod::Explicit,
913 sampled_image_id,
914 coordinates_id,
915 depth_id,
916 );
917
918 let mut lod_id = self.cached[lod_handle];
919 // SPIR-V expects the LOD to be a float for all image classes.
920 // lod_id, however, will be an integer for depth images,
921 // therefore we must do a conversion.
922 if matches!(
923 self.ir_module.types[image_type].inner,
924 crate::TypeInner::Image {
925 class: crate::ImageClass::Depth { .. },
926 ..
927 }
928 ) {
929 let lod_f32_id = self.gen_id();
930 let f32_type_id =
931 self.get_numeric_type_id(NumericType::Scalar(crate::Scalar::F32));
932 let convert_op = match *self.fun_info[lod_handle]
933 .ty
934 .inner_with(&self.ir_module.types)
935 {
936 crate::TypeInner::Scalar(crate::Scalar {
937 kind: crate::ScalarKind::Uint,
938 width: 4,
939 }) => spirv::Op::ConvertUToF,
940 crate::TypeInner::Scalar(crate::Scalar {
941 kind: crate::ScalarKind::Sint,
942 width: 4,
943 }) => spirv::Op::ConvertSToF,
944 _ => unreachable!(),
945 };
946 block.body.push(Instruction::unary(
947 convert_op,
948 f32_type_id,
949 lod_f32_id,
950 lod_id,
951 ));
952 lod_id = lod_f32_id;
953 }
954 mask |= spirv::ImageOperands::LOD;
955 inst.add_operand(mask.bits());
956 inst.add_operand(lod_id);
957
958 inst
959 }
960 (crate::SampleLevel::Bias(bias_handle), None) => {
961 let mut inst = Instruction::image_sample(
962 sample_result_type_id,
963 id,
964 SampleLod::Implicit,
965 sampled_image_id,
966 coordinates_id,
967 depth_id,
968 );
969
970 let bias_id = self.cached[bias_handle];
971 mask |= spirv::ImageOperands::BIAS;
972 inst.add_operand(mask.bits());
973 inst.add_operand(bias_id);
974
975 inst
976 }
977 (crate::SampleLevel::Gradient { x, y }, None) => {
978 let mut inst = Instruction::image_sample(
979 sample_result_type_id,
980 id,
981 SampleLod::Explicit,
982 sampled_image_id,
983 coordinates_id,
984 depth_id,
985 );
986
987 let x_id = self.cached[x];
988 let y_id = self.cached[y];
989 mask |= spirv::ImageOperands::GRAD;
990 inst.add_operand(mask.bits());
991 inst.add_operand(x_id);
992 inst.add_operand(y_id);
993
994 inst
995 }
996 };
997
998 if let Some(offset_const) = offset {
999 let offset_id = self.cached[offset_const];
1000 main_instruction.add_operand(offset_id);
1001 }
1002
1003 block.body.push(main_instruction);
1004
1005 let id = if needs_sub_access {
1006 let sub_id = self.gen_id();
1007 block.body.push(Instruction::composite_extract(
1008 result_type_id,
1009 sub_id,
1010 id,
1011 &[0],
1012 ));
1013 sub_id
1014 } else {
1015 id
1016 };
1017
1018 Ok(id)
1019 }
1020
1021 /// Generate code for an `ImageQuery` expression.
1022 ///
1023 /// The arguments are the components of an `Expression::ImageQuery` variant.
1024 pub(super) fn write_image_query(
1025 &mut self,
1026 result_type_id: Word,
1027 image: Handle<crate::Expression>,
1028 query: crate::ImageQuery,
1029 block: &mut Block,
1030 ) -> Result<Word, Error> {
1031 use crate::{ImageClass as Ic, ImageDimension as Id, ImageQuery as Iq};
1032
1033 let image_id = self.get_handle_id(image);
1034 let image_type = self.fun_info[image].ty.handle().unwrap();
1035 let (dim, arrayed, class) = match self.ir_module.types[image_type].inner {
1036 crate::TypeInner::Image {
1037 dim,
1038 arrayed,
1039 class,
1040 } => (dim, arrayed, class),
1041 _ => {
1042 return Err(Error::Validation("image type"));
1043 }
1044 };
1045
1046 self.writer
1047 .require_any("image queries", &[spirv::Capability::ImageQuery])?;
1048
1049 let id = match query {
1050 Iq::Size { level } => {
1051 let dim_coords = match dim {
1052 Id::D1 => 1,
1053 Id::D2 | Id::Cube => 2,
1054 Id::D3 => 3,
1055 };
1056 let array_coords = usize::from(arrayed);
1057 let vector_size = match dim_coords + array_coords {
1058 2 => Some(crate::VectorSize::Bi),
1059 3 => Some(crate::VectorSize::Tri),
1060 4 => Some(crate::VectorSize::Quad),
1061 _ => None,
1062 };
1063 let vector_numeric_type = match vector_size {
1064 Some(size) => NumericType::Vector {
1065 size,
1066 scalar: crate::Scalar::U32,
1067 },
1068 None => NumericType::Scalar(crate::Scalar::U32),
1069 };
1070
1071 let extended_size_type_id = self.get_numeric_type_id(vector_numeric_type);
1072
1073 let (query_op, level_id) = match class {
1074 Ic::Sampled { multi: true, .. }
1075 | Ic::Depth { multi: true }
1076 | Ic::Storage { .. } => (spirv::Op::ImageQuerySize, None),
1077 _ => {
1078 let level_id = match level {
1079 Some(expr) => self.cached[expr],
1080 None => self.get_index_constant(0),
1081 };
1082 (spirv::Op::ImageQuerySizeLod, Some(level_id))
1083 }
1084 };
1085
1086 // The ID of the vector returned by SPIR-V, which contains the dimensions
1087 // as well as the layer count.
1088 let id_extended = self.gen_id();
1089 let mut inst = Instruction::image_query(
1090 query_op,
1091 extended_size_type_id,
1092 id_extended,
1093 image_id,
1094 );
1095 if let Some(expr_id) = level_id {
1096 inst.add_operand(expr_id);
1097 }
1098 block.body.push(inst);
1099
1100 if result_type_id != extended_size_type_id {
1101 let id = self.gen_id();
1102 let components = match dim {
1103 // always pick the first component, and duplicate it for all 3 dimensions
1104 Id::Cube => &[0u32, 0][..],
1105 _ => &[0u32, 1, 2, 3][..dim_coords],
1106 };
1107 block.body.push(Instruction::vector_shuffle(
1108 result_type_id,
1109 id,
1110 id_extended,
1111 id_extended,
1112 components,
1113 ));
1114
1115 id
1116 } else {
1117 id_extended
1118 }
1119 }
1120 Iq::NumLevels => {
1121 let query_id = self.gen_id();
1122 block.body.push(Instruction::image_query(
1123 spirv::Op::ImageQueryLevels,
1124 result_type_id,
1125 query_id,
1126 image_id,
1127 ));
1128
1129 query_id
1130 }
1131 Iq::NumLayers => {
1132 let vec_size = match dim {
1133 Id::D1 => crate::VectorSize::Bi,
1134 Id::D2 | Id::Cube => crate::VectorSize::Tri,
1135 Id::D3 => crate::VectorSize::Quad,
1136 };
1137 let extended_size_type_id = self.get_numeric_type_id(NumericType::Vector {
1138 size: vec_size,
1139 scalar: crate::Scalar::U32,
1140 });
1141 let id_extended = self.gen_id();
1142 let mut inst = Instruction::image_query(
1143 spirv::Op::ImageQuerySizeLod,
1144 extended_size_type_id,
1145 id_extended,
1146 image_id,
1147 );
1148 inst.add_operand(self.get_index_constant(0));
1149 block.body.push(inst);
1150
1151 let extract_id = self.gen_id();
1152 block.body.push(Instruction::composite_extract(
1153 result_type_id,
1154 extract_id,
1155 id_extended,
1156 &[vec_size as u32 - 1],
1157 ));
1158
1159 extract_id
1160 }
1161 Iq::NumSamples => {
1162 let query_id = self.gen_id();
1163 block.body.push(Instruction::image_query(
1164 spirv::Op::ImageQuerySamples,
1165 result_type_id,
1166 query_id,
1167 image_id,
1168 ));
1169
1170 query_id
1171 }
1172 };
1173
1174 Ok(id)
1175 }
1176
1177 pub(super) fn write_image_store(
1178 &mut self,
1179 image: Handle<crate::Expression>,
1180 coordinate: Handle<crate::Expression>,
1181 array_index: Option<Handle<crate::Expression>>,
1182 value: Handle<crate::Expression>,
1183 block: &mut Block,
1184 ) -> Result<(), Error> {
1185 let image_id = self.get_handle_id(image);
1186 let coordinates = self.write_image_coordinates(coordinate, array_index, block)?;
1187 let value_id = self.cached[value];
1188
1189 let write = Store { image_id, value_id };
1190
1191 match *self.fun_info[image].ty.inner_with(&self.ir_module.types) {
1192 crate::TypeInner::Image {
1193 class:
1194 crate::ImageClass::Storage {
1195 format: crate::StorageFormat::Bgra8Unorm,
1196 ..
1197 },
1198 ..
1199 } => self.writer.require_any(
1200 "Bgra8Unorm storage write",
1201 &[spirv::Capability::StorageImageWriteWithoutFormat],
1202 )?,
1203 _ => {}
1204 }
1205
1206 write.generate(
1207 &mut self.writer.id_gen,
1208 coordinates.value_id,
1209 None,
1210 None,
1211 block,
1212 );
1213
1214 Ok(())
1215 }
1216
1217 pub(super) fn write_image_atomic(
1218 &mut self,
1219 image: Handle<crate::Expression>,
1220 coordinate: Handle<crate::Expression>,
1221 array_index: Option<Handle<crate::Expression>>,
1222 fun: crate::AtomicFunction,
1223 value: Handle<crate::Expression>,
1224 block: &mut Block,
1225 ) -> Result<(), Error> {
1226 let image_id = match self.ir_function.originating_global(image) {
1227 Some(handle) => self.writer.global_variables[handle].var_id,
1228 _ => return Err(Error::Validation("Unexpected image type")),
1229 };
1230 let crate::TypeInner::Image { class, .. } =
1231 *self.fun_info[image].ty.inner_with(&self.ir_module.types)
1232 else {
1233 return Err(Error::Validation("Invalid image type"));
1234 };
1235 let crate::ImageClass::Storage { format, .. } = class else {
1236 return Err(Error::Validation("Invalid image class"));
1237 };
1238 let scalar = format.into();
1239 let scalar_type_id = self.get_numeric_type_id(NumericType::Scalar(scalar));
1240 let pointer_type_id = self.get_pointer_type_id(scalar_type_id, spirv::StorageClass::Image);
1241 let signed = scalar.kind == crate::ScalarKind::Sint;
1242 if scalar.width == 8 {
1243 self.writer
1244 .require_any("64 bit image atomics", &[spirv::Capability::Int64Atomics])?;
1245 }
1246 let pointer_id = self.gen_id();
1247 let coordinates = self.write_image_coordinates(coordinate, array_index, block)?;
1248 let sample_id = self.writer.get_constant_scalar(crate::Literal::U32(0));
1249 block.body.push(Instruction::image_texel_pointer(
1250 pointer_type_id,
1251 pointer_id,
1252 image_id,
1253 coordinates.value_id,
1254 sample_id,
1255 ));
1256
1257 let op = match fun {
1258 crate::AtomicFunction::Add => spirv::Op::AtomicIAdd,
1259 crate::AtomicFunction::Subtract => spirv::Op::AtomicISub,
1260 crate::AtomicFunction::And => spirv::Op::AtomicAnd,
1261 crate::AtomicFunction::ExclusiveOr => spirv::Op::AtomicXor,
1262 crate::AtomicFunction::InclusiveOr => spirv::Op::AtomicOr,
1263 crate::AtomicFunction::Min if signed => spirv::Op::AtomicSMin,
1264 crate::AtomicFunction::Min => spirv::Op::AtomicUMin,
1265 crate::AtomicFunction::Max if signed => spirv::Op::AtomicSMax,
1266 crate::AtomicFunction::Max => spirv::Op::AtomicUMax,
1267 crate::AtomicFunction::Exchange { .. } => {
1268 return Err(Error::Validation("Exchange atomics are not supported yet"))
1269 }
1270 };
1271 let result_type_id = self.get_expression_type_id(&self.fun_info[value].ty);
1272 let id = self.gen_id();
1273 let space = crate::AddressSpace::Handle;
1274 let (semantics, scope) = space.to_spirv_semantics_and_scope();
1275 let scope_constant_id = self.get_scope_constant(scope as u32);
1276 let semantics_id = self.get_index_constant(semantics.bits());
1277 let value_id = self.cached[value];
1278
1279 block.body.push(Instruction::image_atomic(
1280 op,
1281 result_type_id,
1282 id,
1283 pointer_id,
1284 scope_constant_id,
1285 semantics_id,
1286 value_id,
1287 ));
1288
1289 Ok(())
1290 }
1291 }