• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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_block_index |
88                                    nir_metadata_dominance,
89                                 &inline_samplers);
90 
91    /* If we found any inline samplers in the instructions pass, they'll now be
92     * in the inline_samplers list.
93     */
94    bool progress = !exec_list_is_empty(&inline_samplers);
95 
96    /* Remove any dead samplers */
97    nir_foreach_uniform_variable_safe(var, nir) {
98       if (var_is_inline_sampler(var)) {
99          exec_node_remove(&var->node);
100          progress = true;
101       }
102    }
103 
104    exec_node_insert_list_after(exec_list_get_tail(&nir->variables),
105                                &inline_samplers);
106 
107    return progress;
108 }
109 
110 bool
nir_lower_cl_images(nir_shader * shader,bool lower_image_derefs,bool lower_sampler_derefs)111 nir_lower_cl_images(nir_shader *shader, bool lower_image_derefs, bool lower_sampler_derefs)
112 {
113    nir_function_impl *impl = nir_shader_get_entrypoint(shader);
114 
115    ASSERTED int last_loc = -1;
116    int num_rd_images = 0, num_wr_images = 0;
117 
118    BITSET_ZERO(shader->info.image_buffers);
119    BITSET_ZERO(shader->info.msaa_images);
120    nir_foreach_variable_with_modes(var, shader, nir_var_image | nir_var_uniform) {
121       if (!glsl_type_is_image(var->type) && !glsl_type_is_texture(var->type))
122          continue;
123 
124       /* Assume they come in order */
125       assert(var->data.location > last_loc);
126       last_loc = var->data.location;
127 
128       assert(glsl_type_is_image(var->type) || var->data.access & ACCESS_NON_WRITEABLE);
129       if (var->data.access & ACCESS_NON_WRITEABLE)
130          var->data.driver_location = num_rd_images++;
131       else
132          var->data.driver_location = num_wr_images++;
133       var->data.binding = var->data.driver_location;
134 
135       switch (glsl_get_sampler_dim(var->type)) {
136       case GLSL_SAMPLER_DIM_BUF:
137          BITSET_SET(shader->info.image_buffers, var->data.binding);
138          break;
139       case GLSL_SAMPLER_DIM_MS:
140          BITSET_SET(shader->info.msaa_images, var->data.binding);
141          break;
142       default:
143          break;
144       }
145    }
146    shader->info.num_textures = num_rd_images;
147    BITSET_ZERO(shader->info.textures_used);
148    if (num_rd_images)
149       BITSET_SET_RANGE(shader->info.textures_used, 0, num_rd_images - 1);
150 
151    BITSET_ZERO(shader->info.images_used);
152    if (num_wr_images)
153       BITSET_SET_RANGE(shader->info.images_used, 0, num_wr_images - 1);
154    shader->info.num_images = num_wr_images;
155 
156    last_loc = -1;
157    int num_samplers = 0;
158    nir_foreach_uniform_variable(var, shader) {
159       if (var->type == glsl_bare_sampler_type()) {
160          /* Assume they come in order */
161          assert(var->data.location > last_loc);
162          last_loc = var->data.location;
163          var->data.driver_location = num_samplers++;
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_block_index |
290                                      nir_metadata_dominance);
291    } else {
292       nir_metadata_preserve(impl, nir_metadata_all);
293    }
294 
295    return progress;
296 }
297