xref: /aosp_15_r20/external/mesa3d/src/compiler/nir/nir_lower_input_attachments.c (revision 6104692788411f58d303aa86923a9ff6ecaded22)
1 /*
2  * Copyright © 2016 Intel Corporation
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 
24 #include "nir.h"
25 #include "nir_builder.h"
26 
27 static nir_def *
load_frag_coord(nir_builder * b,nir_deref_instr * deref,const nir_input_attachment_options * options)28 load_frag_coord(nir_builder *b, nir_deref_instr *deref,
29                 const nir_input_attachment_options *options)
30 {
31    if (options->use_fragcoord_sysval) {
32       nir_def *frag_coord = nir_load_frag_coord(b);
33       if (options->unscaled_input_attachment_ir3) {
34          nir_variable *var = nir_deref_instr_get_variable(deref);
35          unsigned base = var->data.index;
36          nir_def *unscaled_frag_coord = nir_load_frag_coord_unscaled_ir3(b);
37          if (deref->deref_type == nir_deref_type_array) {
38             nir_def *unscaled =
39                nir_i2b(b, nir_iand(b, nir_ishr(b, nir_imm_int(b, options->unscaled_input_attachment_ir3 >> base), deref->arr.index.ssa),
40                                    nir_imm_int(b, 1)));
41             frag_coord = nir_bcsel(b, unscaled, unscaled_frag_coord, frag_coord);
42          } else {
43             assert(deref->deref_type == nir_deref_type_var);
44             bool unscaled = (options->unscaled_input_attachment_ir3 >> base) & 1;
45             frag_coord = unscaled ? unscaled_frag_coord : frag_coord;
46          }
47       }
48       return frag_coord;
49    }
50 
51    nir_variable *pos = nir_get_variable_with_location(b->shader, nir_var_shader_in,
52                                                       VARYING_SLOT_POS, glsl_vec4_type());
53 
54    /**
55     * From Vulkan spec:
56     *   "The OriginLowerLeft execution mode must not be used; fragment entry
57     *    points must declare OriginUpperLeft."
58     *
59     * So at this point origin_upper_left should be true
60     */
61    assert(b->shader->info.fs.origin_upper_left == true);
62 
63    return nir_load_var(b, pos);
64 }
65 
66 static nir_def *
load_layer_id(nir_builder * b,const nir_input_attachment_options * options)67 load_layer_id(nir_builder *b, const nir_input_attachment_options *options)
68 {
69    if (options->use_layer_id_sysval) {
70       if (options->use_view_id_for_layer)
71          return nir_load_view_index(b);
72       else
73          return nir_load_layer_id(b);
74    }
75 
76    gl_varying_slot slot = options->use_view_id_for_layer ? VARYING_SLOT_VIEW_INDEX : VARYING_SLOT_LAYER;
77    nir_variable *layer_id = nir_get_variable_with_location(b->shader, nir_var_shader_in,
78                                                            slot, glsl_int_type());
79    layer_id->data.interpolation = INTERP_MODE_FLAT;
80 
81    return nir_load_var(b, layer_id);
82 }
83 
84 static bool
try_lower_input_load(nir_builder * b,nir_intrinsic_instr * load,const nir_input_attachment_options * options)85 try_lower_input_load(nir_builder *b, nir_intrinsic_instr *load,
86                      const nir_input_attachment_options *options)
87 {
88    nir_deref_instr *deref = nir_src_as_deref(load->src[0]);
89    assert(glsl_type_is_image(deref->type));
90 
91    enum glsl_sampler_dim image_dim = glsl_get_sampler_dim(deref->type);
92    if (image_dim != GLSL_SAMPLER_DIM_SUBPASS &&
93        image_dim != GLSL_SAMPLER_DIM_SUBPASS_MS)
94       return false;
95 
96    const bool multisampled = (image_dim == GLSL_SAMPLER_DIM_SUBPASS_MS);
97 
98    b->cursor = nir_instr_remove(&load->instr);
99 
100    nir_def *frag_coord = load_frag_coord(b, deref, options);
101    frag_coord = nir_f2i32(b, frag_coord);
102    nir_def *offset = nir_trim_vector(b, load->src[1].ssa, 2);
103    nir_def *pos = nir_iadd(b, frag_coord, offset);
104 
105    nir_def *layer = load_layer_id(b, options);
106    nir_def *coord =
107       nir_vec3(b, nir_channel(b, pos, 0), nir_channel(b, pos, 1), layer);
108 
109    nir_tex_instr *tex = nir_tex_instr_create(b->shader, 3 + multisampled);
110 
111    tex->op = nir_texop_txf;
112    tex->sampler_dim = image_dim;
113 
114    tex->dest_type =
115       nir_get_nir_type_for_glsl_base_type(glsl_get_sampler_result_type(deref->type));
116    tex->is_array = true;
117    tex->is_shadow = false;
118    tex->is_sparse = load->intrinsic == nir_intrinsic_image_deref_sparse_load;
119 
120    tex->texture_index = 0;
121    tex->sampler_index = 0;
122 
123    tex->src[0] = nir_tex_src_for_ssa(nir_tex_src_texture_deref,
124                                      &deref->def);
125    tex->src[1] = nir_tex_src_for_ssa(nir_tex_src_coord, coord);
126    tex->coord_components = 3;
127 
128    tex->src[2] = nir_tex_src_for_ssa(nir_tex_src_lod, nir_imm_int(b, 0));
129 
130    if (image_dim == GLSL_SAMPLER_DIM_SUBPASS_MS) {
131       tex->op = nir_texop_txf_ms;
132       tex->src[3].src_type = nir_tex_src_ms_index;
133       tex->src[3].src = load->src[2];
134    }
135 
136    tex->texture_non_uniform = nir_intrinsic_access(load) & ACCESS_NON_UNIFORM;
137 
138    nir_def_init(&tex->instr, &tex->def, nir_tex_instr_dest_size(tex), 32);
139    nir_builder_instr_insert(b, &tex->instr);
140 
141    if (tex->is_sparse) {
142       unsigned load_result_size = load->def.num_components - 1;
143       nir_component_mask_t load_result_mask = nir_component_mask(load_result_size);
144       nir_def *res = nir_channels(
145          b, &tex->def, load_result_mask | 0x10);
146 
147       nir_def_rewrite_uses(&load->def, res);
148    } else {
149       nir_def_rewrite_uses(&load->def,
150                            &tex->def);
151    }
152 
153    return true;
154 }
155 
156 static bool
try_lower_input_texop(nir_builder * b,nir_tex_instr * tex,const nir_input_attachment_options * options)157 try_lower_input_texop(nir_builder *b, nir_tex_instr *tex,
158                       const nir_input_attachment_options *options)
159 {
160    nir_deref_instr *deref = nir_src_as_deref(tex->src[0].src);
161 
162    if (glsl_get_sampler_dim(deref->type) != GLSL_SAMPLER_DIM_SUBPASS_MS)
163       return false;
164 
165    b->cursor = nir_before_instr(&tex->instr);
166 
167    nir_def *frag_coord = load_frag_coord(b, deref, options);
168    frag_coord = nir_f2i32(b, frag_coord);
169 
170    nir_def *layer = load_layer_id(b, options);
171    nir_def *coord = nir_vec3(b, nir_channel(b, frag_coord, 0),
172                              nir_channel(b, frag_coord, 1), layer);
173 
174    tex->coord_components = 3;
175 
176    nir_src_rewrite(&tex->src[1].src, coord);
177 
178    return true;
179 }
180 
181 static bool
lower_input_attachments_instr(nir_builder * b,nir_instr * instr,void * _data)182 lower_input_attachments_instr(nir_builder *b, nir_instr *instr, void *_data)
183 {
184    const nir_input_attachment_options *options = _data;
185 
186    switch (instr->type) {
187    case nir_instr_type_tex: {
188       nir_tex_instr *tex = nir_instr_as_tex(instr);
189 
190       if (tex->op == nir_texop_fragment_mask_fetch_amd ||
191           tex->op == nir_texop_fragment_fetch_amd)
192          return try_lower_input_texop(b, tex, options);
193 
194       return false;
195    }
196    case nir_instr_type_intrinsic: {
197       nir_intrinsic_instr *load = nir_instr_as_intrinsic(instr);
198 
199       if (load->intrinsic == nir_intrinsic_image_deref_load ||
200           load->intrinsic == nir_intrinsic_image_deref_sparse_load)
201          return try_lower_input_load(b, load, options);
202 
203       return false;
204    }
205 
206    default:
207       return false;
208    }
209 }
210 
211 bool
nir_lower_input_attachments(nir_shader * shader,const nir_input_attachment_options * options)212 nir_lower_input_attachments(nir_shader *shader,
213                             const nir_input_attachment_options *options)
214 {
215    assert(shader->info.stage == MESA_SHADER_FRAGMENT);
216 
217    return nir_shader_instructions_pass(shader, lower_input_attachments_instr,
218                                        nir_metadata_control_flow,
219                                        (void *)options);
220 }
221