1 /*
2 * Copyright (C) 2024 Collabora, Ltd.
3 *
4 * Permission is hereby granted, free of charge, to any person obtaining a
5 * copy of this software and associated documentation files (the "Software"),
6 * to deal in the Software without restriction, including without limitation
7 * the rights to use, copy, modify, merge, publish, distribute, sublicense,
8 * and/or sell copies of the Software, and to permit persons to whom the
9 * Software is furnished to do so, subject to the following conditions:
10 *
11 * The above copyright notice and this permission notice (including the next
12 * paragraph) shall be included in all copies or substantial portions of the
13 * Software.
14 *
15 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
18 * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
20 * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
21 * IN THE SOFTWARE.
22 *
23 * Authors (Collabora):
24 * Eric Smith <[email protected]>
25 */
26
27 #include "compiler/nir/nir.h"
28 #include "compiler/nir/nir_builder.h"
29 #include "pan_ir.h"
30
31 static bool
nir_lower_image_ms(nir_builder * b,nir_intrinsic_instr * intr,UNUSED void * data)32 nir_lower_image_ms(nir_builder *b, nir_intrinsic_instr *intr,
33 UNUSED void *data)
34 {
35 bool img_deref = false;
36
37 switch (intr->intrinsic) {
38 case nir_intrinsic_image_deref_load:
39 case nir_intrinsic_image_deref_store:
40 img_deref = true;
41 break;
42 case nir_intrinsic_image_texel_address:
43 case nir_intrinsic_image_load:
44 case nir_intrinsic_image_store:
45 break;
46 default:
47 return false;
48 }
49
50 if (nir_intrinsic_image_dim(intr) != GLSL_SAMPLER_DIM_MS)
51 return false;
52
53 b->cursor = nir_before_instr(&intr->instr);
54
55 nir_def *coord = intr->src[1].ssa;
56 nir_def *sample = nir_channel(b, intr->src[2].ssa, 0);
57
58 if (nir_intrinsic_image_array(intr)) {
59 /* Unlike textures, images only embed a single LOD, hence the zero. */
60 nir_def *lod = nir_imm_int(b, 0);
61 nir_def *img_size =
62 img_deref ? nir_image_deref_size(b, 3, 32, intr->src[0].ssa, lod) :
63 nir_image_size(b, 3, 32, intr->src[0].ssa, lod,
64 .image_array = true, .image_dim = GLSL_SAMPLER_DIM_MS);
65 nir_def *img_height = nir_channel(b, img_size, 1);
66 nir_def *y_coord = nir_channel(b, coord, 1);
67 nir_def *z_coord = nir_channel(b, coord, 2);
68
69 /* With image2DMSArray, the Z coord is already used to index the array. We
70 * assume sample planes are adjacent and patch the Y coordinate to address
71 * the right sample plane. This means our image height is effectively
72 * limited to 4k though.
73 *
74 * Note that we don't trust image intrinsic is_array information because
75 * arrays of size one are allowed, and we only get to know the actual
76 * image size at bind time.
77 */
78 nir_def *is_array = nir_ugt_imm(b, nir_channel(b, img_size, 2), 1);
79
80 y_coord = nir_bcsel(
81 b, is_array,
82 nir_iadd(b, nir_imul(b, img_height, sample), y_coord), y_coord);
83 z_coord = nir_bcsel(b, is_array, z_coord, sample);
84 coord = nir_vec4(b, nir_channel(b, coord, 0), y_coord, z_coord,
85 nir_channel(b, coord, 3));
86
87 nir_src_rewrite(&intr->src[1], coord);
88 } else {
89 /* image2DMS is treated by panfrost as if it were a 3D image, so
90 * the sample index is in src[2]. We need to put this into the coordinates
91 * in the Z component.
92 */
93 nir_src_rewrite(&intr->src[1],
94 nir_vector_insert_imm(b, coord, sample, 2));
95 }
96
97 nir_intrinsic_set_image_dim(intr, GLSL_SAMPLER_DIM_3D);
98 nir_intrinsic_set_image_array(intr, false);
99 return true;
100 }
101
102 bool
pan_nir_lower_image_ms(nir_shader * shader)103 pan_nir_lower_image_ms(nir_shader *shader)
104 {
105 return nir_shader_intrinsics_pass(
106 shader, nir_lower_image_ms,
107 nir_metadata_control_flow, NULL);
108 }
109