xref: /aosp_15_r20/external/mesa3d/src/compiler/nir/nir_lower_cl_images.c (revision 6104692788411f58d303aa86923a9ff6ecaded22)
1 /* SPDX-License-Identifier: MIT */
2 
3 #include "nir.h"
4 #include "nir_builder.h"
5 
6 static bool
var_is_inline_sampler(const nir_variable * var)7 var_is_inline_sampler(const nir_variable *var)
8 {
9    if (var->data.mode != nir_var_uniform)
10       return false;
11 
12    return glsl_type_is_sampler(var->type) &&
13           var->data.sampler.is_inline_sampler;
14 }
15 
16 static bool
inline_sampler_vars_equal(const nir_variable * a,const nir_variable * b)17 inline_sampler_vars_equal(const nir_variable *a, const nir_variable *b)
18 {
19    assert(var_is_inline_sampler(a) && var_is_inline_sampler(b));
20 
21    if (a == b)
22       return true;
23 
24    return a->data.sampler.addressing_mode == b->data.sampler.addressing_mode &&
25           a->data.sampler.normalized_coordinates == b->data.sampler.normalized_coordinates &&
26           a->data.sampler.filter_mode == b->data.sampler.filter_mode;
27 }
28 
29 static nir_variable *
find_identical_inline_sampler(nir_shader * nir,struct exec_list * inline_samplers,nir_variable * sampler)30 find_identical_inline_sampler(nir_shader *nir,
31                               struct exec_list *inline_samplers,
32                               nir_variable *sampler)
33 {
34    nir_foreach_variable_in_list(var, inline_samplers) {
35       if (inline_sampler_vars_equal(var, sampler))
36          return var;
37    }
38 
39    nir_foreach_uniform_variable(var, nir) {
40       if (!var_is_inline_sampler(var) ||
41           !inline_sampler_vars_equal(var, sampler))
42          continue;
43 
44       exec_node_remove(&var->node);
45       exec_list_push_tail(inline_samplers, &var->node);
46       return var;
47    }
48    unreachable("Should have at least found the input sampler");
49 }
50 
51 static bool
nir_dedup_inline_samplers_instr(nir_builder * b,nir_instr * instr,void * cb_data)52 nir_dedup_inline_samplers_instr(nir_builder *b,
53                                 nir_instr *instr,
54                                 void *cb_data)
55 {
56    struct exec_list *inline_samplers = cb_data;
57 
58    if (instr->type != nir_instr_type_deref)
59       return false;
60 
61    nir_deref_instr *deref = nir_instr_as_deref(instr);
62    if (deref->deref_type != nir_deref_type_var)
63       return false;
64 
65    nir_variable *sampler = nir_deref_instr_get_variable(deref);
66    if (!var_is_inline_sampler(sampler))
67       return false;
68 
69    nir_variable *replacement =
70       find_identical_inline_sampler(b->shader, inline_samplers, sampler);
71    deref->var = replacement;
72    return true;
73 }
74 
75 /** De-duplicates inline sampler variables
76  *
77  * Any dead or redundant inline sampler variables are removed any live inline
78  * sampler variables are placed at the end of the variables list.
79  */
80 bool
nir_dedup_inline_samplers(nir_shader * nir)81 nir_dedup_inline_samplers(nir_shader *nir)
82 {
83    struct exec_list inline_samplers;
84    exec_list_make_empty(&inline_samplers);
85 
86    nir_shader_instructions_pass(nir, nir_dedup_inline_samplers_instr,
87                                 nir_metadata_control_flow,
88                                 &inline_samplers);
89 
90    /* If we found any inline samplers in the instructions pass, they'll now be
91     * in the inline_samplers list.
92     */
93    bool progress = !exec_list_is_empty(&inline_samplers);
94 
95    /* Remove any dead samplers */
96    nir_foreach_uniform_variable_safe(var, nir) {
97       if (var_is_inline_sampler(var)) {
98          exec_node_remove(&var->node);
99          progress = true;
100       }
101    }
102 
103    exec_node_insert_list_after(exec_list_get_tail(&nir->variables),
104                                &inline_samplers);
105 
106    return progress;
107 }
108 
109 bool
nir_lower_cl_images(nir_shader * shader,bool lower_image_derefs,bool lower_sampler_derefs)110 nir_lower_cl_images(nir_shader *shader, bool lower_image_derefs, bool lower_sampler_derefs)
111 {
112    nir_function_impl *impl = nir_shader_get_entrypoint(shader);
113 
114    ASSERTED int last_loc = -1;
115    int num_rd_images = 0, num_wr_images = 0;
116 
117    BITSET_ZERO(shader->info.image_buffers);
118    BITSET_ZERO(shader->info.msaa_images);
119    nir_foreach_variable_with_modes(var, shader, nir_var_image | nir_var_uniform) {
120       if (!glsl_type_is_image(var->type) && !glsl_type_is_texture(var->type))
121          continue;
122 
123       /* Assume they come in order */
124       assert(var->data.location > last_loc);
125       last_loc = var->data.location;
126 
127       assert(glsl_type_is_image(var->type) || var->data.access & ACCESS_NON_WRITEABLE);
128       if (var->data.access & ACCESS_NON_WRITEABLE)
129          var->data.driver_location = num_rd_images++;
130       else
131          var->data.driver_location = num_wr_images++;
132       var->data.binding = var->data.driver_location;
133 
134       switch (glsl_get_sampler_dim(var->type)) {
135       case GLSL_SAMPLER_DIM_BUF:
136          BITSET_SET(shader->info.image_buffers, var->data.binding);
137          break;
138       case GLSL_SAMPLER_DIM_MS:
139          BITSET_SET(shader->info.msaa_images, var->data.binding);
140          break;
141       default:
142          break;
143       }
144    }
145    shader->info.num_textures = num_rd_images;
146    BITSET_ZERO(shader->info.textures_used);
147    if (num_rd_images)
148       BITSET_SET_RANGE(shader->info.textures_used, 0, num_rd_images - 1);
149 
150    BITSET_ZERO(shader->info.images_used);
151    if (num_wr_images)
152       BITSET_SET_RANGE(shader->info.images_used, 0, num_wr_images - 1);
153    shader->info.num_images = num_wr_images;
154 
155    last_loc = -1;
156    int num_samplers = 0;
157    nir_foreach_uniform_variable(var, shader) {
158       if (var->type == glsl_bare_sampler_type()) {
159          /* Assume they come in order */
160          assert(var->data.location > last_loc);
161          last_loc = var->data.location;
162          var->data.driver_location = num_samplers++;
163          var->data.binding = var->data.driver_location;
164       } else {
165          /* CL shouldn't have any sampled images */
166          assert(!glsl_type_is_sampler(var->type));
167       }
168    }
169    BITSET_ZERO(shader->info.samplers_used);
170    if (num_samplers)
171       BITSET_SET_RANGE(shader->info.samplers_used, 0, num_samplers - 1);
172 
173    nir_builder b = nir_builder_create(impl);
174 
175    /* don't need any lowering if we can keep the derefs */
176    if (!lower_image_derefs && !lower_sampler_derefs) {
177       nir_metadata_preserve(impl, nir_metadata_all);
178       return false;
179    }
180 
181    bool progress = false;
182    nir_foreach_block_reverse(block, impl) {
183       nir_foreach_instr_reverse_safe(instr, block) {
184          switch (instr->type) {
185          case nir_instr_type_deref: {
186             nir_deref_instr *deref = nir_instr_as_deref(instr);
187             if (deref->deref_type != nir_deref_type_var)
188                break;
189 
190             if (!glsl_type_is_image(deref->type) &&
191                 !glsl_type_is_texture(deref->type) &&
192                 !glsl_type_is_sampler(deref->type))
193                break;
194 
195             if (!lower_image_derefs && glsl_type_is_image(deref->type))
196                break;
197 
198             if (!lower_sampler_derefs &&
199                 (glsl_type_is_sampler(deref->type) || glsl_type_is_texture(deref->type)))
200                break;
201 
202             b.cursor = nir_instr_remove(&deref->instr);
203             nir_def *loc =
204                nir_imm_intN_t(&b, deref->var->data.driver_location,
205                               deref->def.bit_size);
206             nir_def_rewrite_uses(&deref->def, loc);
207             progress = true;
208             break;
209          }
210 
211          case nir_instr_type_tex: {
212             if (!lower_sampler_derefs)
213                break;
214 
215             nir_tex_instr *tex = nir_instr_as_tex(instr);
216             unsigned count = 0;
217             for (unsigned i = 0; i < tex->num_srcs; i++) {
218                if (tex->src[i].src_type == nir_tex_src_texture_deref ||
219                    tex->src[i].src_type == nir_tex_src_sampler_deref) {
220                   nir_deref_instr *deref = nir_src_as_deref(tex->src[i].src);
221                   if (deref->deref_type == nir_deref_type_var) {
222                      /* In this case, we know the actual variable */
223                      if (tex->src[i].src_type == nir_tex_src_texture_deref)
224                         tex->texture_index = deref->var->data.driver_location;
225                      else
226                         tex->sampler_index = deref->var->data.driver_location;
227                      /* This source gets discarded */
228                      nir_instr_clear_src(&tex->instr, &tex->src[i].src);
229                      continue;
230                   } else {
231                      b.cursor = nir_before_instr(&tex->instr);
232                      /* Back-ends expect a 32-bit thing, not 64-bit */
233                      nir_def *offset = nir_u2u32(&b, tex->src[i].src.ssa);
234                      if (tex->src[i].src_type == nir_tex_src_texture_deref)
235                         tex->src[count].src_type = nir_tex_src_texture_offset;
236                      else
237                         tex->src[count].src_type = nir_tex_src_sampler_offset;
238                      nir_src_rewrite(&tex->src[count].src, offset);
239                   }
240                } else {
241                   /* If we've removed a source, move this one down */
242                   if (count != i) {
243                      assert(count < i);
244                      tex->src[count].src_type = tex->src[i].src_type;
245                      nir_instr_move_src(&tex->instr, &tex->src[count].src,
246                                         &tex->src[i].src);
247                   }
248                }
249                count++;
250             }
251             tex->num_srcs = count;
252             progress = true;
253             break;
254          }
255 
256          case nir_instr_type_intrinsic: {
257             nir_intrinsic_instr *intrin = nir_instr_as_intrinsic(instr);
258             switch (intrin->intrinsic) {
259             case nir_intrinsic_image_deref_load:
260             case nir_intrinsic_image_deref_store:
261             case nir_intrinsic_image_deref_atomic:
262             case nir_intrinsic_image_deref_atomic_swap:
263             case nir_intrinsic_image_deref_size:
264             case nir_intrinsic_image_deref_samples: {
265                if (!lower_image_derefs)
266                   break;
267 
268                b.cursor = nir_before_instr(&intrin->instr);
269                /* Back-ends expect a 32-bit thing, not 64-bit */
270                nir_def *offset = nir_u2u32(&b, intrin->src[0].ssa);
271                nir_rewrite_image_intrinsic(intrin, offset, false);
272                progress = true;
273                break;
274             }
275 
276             default:
277                break;
278             }
279             break;
280          }
281 
282          default:
283             break;
284          }
285       }
286    }
287 
288    if (progress) {
289       nir_metadata_preserve(impl, nir_metadata_control_flow);
290    } else {
291       nir_metadata_preserve(impl, nir_metadata_all);
292    }
293 
294    return progress;
295 }
296