nir: Use nir_builder in nir_lower_io's get_io_offset().
[mesa.git] / src / glsl / nir / nir_lower_io.c
1 /*
2 * Copyright © 2014 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 * Authors:
24 * Connor Abbott (cwabbott0@gmail.com)
25 * Jason Ekstrand (jason@jlekstrand.net)
26 *
27 */
28
29 /*
30 * This lowering pass converts references to input/output variables with
31 * loads/stores to actual input/output intrinsics.
32 */
33
34 #include "nir.h"
35 #include "nir_builder.h"
36
37 struct lower_io_state {
38 nir_builder builder;
39 void *mem_ctx;
40 bool is_scalar;
41 };
42
43 static int
44 type_size_vec4(const struct glsl_type *type)
45 {
46 unsigned int i;
47 int size;
48
49 switch (glsl_get_base_type(type)) {
50 case GLSL_TYPE_UINT:
51 case GLSL_TYPE_INT:
52 case GLSL_TYPE_FLOAT:
53 case GLSL_TYPE_BOOL:
54 if (glsl_type_is_matrix(type)) {
55 return glsl_get_matrix_columns(type);
56 } else {
57 return 1;
58 }
59 case GLSL_TYPE_ARRAY:
60 return type_size_vec4(glsl_get_array_element(type)) * glsl_get_length(type);
61 case GLSL_TYPE_STRUCT:
62 size = 0;
63 for (i = 0; i < glsl_get_length(type); i++) {
64 size += type_size_vec4(glsl_get_struct_field(type, i));
65 }
66 return size;
67 case GLSL_TYPE_SUBROUTINE:
68 return 1;
69 case GLSL_TYPE_SAMPLER:
70 return 0;
71 case GLSL_TYPE_ATOMIC_UINT:
72 return 0;
73 case GLSL_TYPE_IMAGE:
74 case GLSL_TYPE_VOID:
75 case GLSL_TYPE_DOUBLE:
76 case GLSL_TYPE_ERROR:
77 case GLSL_TYPE_INTERFACE:
78 unreachable("not reached");
79 }
80
81 return 0;
82 }
83
84 static unsigned
85 type_size_scalar(const struct glsl_type *type)
86 {
87 unsigned int size, i;
88
89 switch (glsl_get_base_type(type)) {
90 case GLSL_TYPE_UINT:
91 case GLSL_TYPE_INT:
92 case GLSL_TYPE_FLOAT:
93 case GLSL_TYPE_BOOL:
94 return glsl_get_components(type);
95 case GLSL_TYPE_ARRAY:
96 return type_size_scalar(glsl_get_array_element(type)) * glsl_get_length(type);
97 case GLSL_TYPE_STRUCT:
98 size = 0;
99 for (i = 0; i < glsl_get_length(type); i++) {
100 size += type_size_scalar(glsl_get_struct_field(type, i));
101 }
102 return size;
103 case GLSL_TYPE_SUBROUTINE:
104 return 1;
105 case GLSL_TYPE_SAMPLER:
106 return 0;
107 case GLSL_TYPE_ATOMIC_UINT:
108 return 0;
109 case GLSL_TYPE_INTERFACE:
110 return 0;
111 case GLSL_TYPE_IMAGE:
112 return 0;
113 case GLSL_TYPE_VOID:
114 case GLSL_TYPE_ERROR:
115 case GLSL_TYPE_DOUBLE:
116 unreachable("not reached");
117 }
118
119 return 0;
120 }
121
122 static unsigned
123 type_size(const struct glsl_type *type, bool is_scalar)
124 {
125 if (is_scalar)
126 return type_size_scalar(type);
127 else
128 return type_size_vec4(type);
129 }
130
131 void
132 nir_assign_var_locations(struct exec_list *var_list, unsigned *size, bool is_scalar)
133 {
134 unsigned location = 0;
135
136 foreach_list_typed(nir_variable, var, node, var_list) {
137 /*
138 * UBO's have their own address spaces, so don't count them towards the
139 * number of global uniforms
140 */
141 if ((var->data.mode == nir_var_uniform || var->data.mode == nir_var_shader_storage) &&
142 var->interface_type != NULL)
143 continue;
144
145 var->data.driver_location = location;
146 location += type_size(var->type, is_scalar);
147 }
148
149 *size = location;
150 }
151
152 static bool
153 deref_has_indirect(nir_deref_var *deref)
154 {
155 for (nir_deref *tail = deref->deref.child; tail; tail = tail->child) {
156 if (tail->deref_type == nir_deref_type_array) {
157 nir_deref_array *arr = nir_deref_as_array(tail);
158 if (arr->deref_array_type == nir_deref_array_type_indirect)
159 return true;
160 }
161 }
162
163 return false;
164 }
165
166 static bool
167 mark_indirect_uses_block(nir_block *block, void *void_state)
168 {
169 struct set *indirect_set = void_state;
170
171 nir_foreach_instr(block, instr) {
172 if (instr->type != nir_instr_type_intrinsic)
173 continue;
174
175 nir_intrinsic_instr *intrin = nir_instr_as_intrinsic(instr);
176
177 for (unsigned i = 0;
178 i < nir_intrinsic_infos[intrin->intrinsic].num_variables; i++) {
179 if (deref_has_indirect(intrin->variables[i]))
180 _mesa_set_add(indirect_set, intrin->variables[i]->var);
181 }
182 }
183
184 return true;
185 }
186
187 /* Identical to nir_assign_var_locations_packed except that it assigns
188 * locations to the variables that are used 100% directly first and then
189 * assigns locations to variables that are used indirectly.
190 */
191 void
192 nir_assign_var_locations_direct_first(nir_shader *shader,
193 struct exec_list *var_list,
194 unsigned *direct_size,
195 unsigned *size,
196 bool is_scalar)
197 {
198 struct set *indirect_set = _mesa_set_create(NULL, _mesa_hash_pointer,
199 _mesa_key_pointer_equal);
200
201 nir_foreach_overload(shader, overload) {
202 if (overload->impl)
203 nir_foreach_block(overload->impl, mark_indirect_uses_block,
204 indirect_set);
205 }
206
207 unsigned location = 0;
208
209 foreach_list_typed(nir_variable, var, node, var_list) {
210 if ((var->data.mode == nir_var_uniform || var->data.mode == nir_var_shader_storage) &&
211 var->interface_type != NULL)
212 continue;
213
214 if (_mesa_set_search(indirect_set, var))
215 continue;
216
217 var->data.driver_location = location;
218 location += type_size(var->type, is_scalar);
219 }
220
221 *direct_size = location;
222
223 foreach_list_typed(nir_variable, var, node, var_list) {
224 if ((var->data.mode == nir_var_uniform || var->data.mode == nir_var_shader_storage) &&
225 var->interface_type != NULL)
226 continue;
227
228 if (!_mesa_set_search(indirect_set, var))
229 continue;
230
231 var->data.driver_location = location;
232 location += type_size(var->type, is_scalar);
233 }
234
235 *size = location;
236
237 _mesa_set_destroy(indirect_set, NULL);
238 }
239
240 static unsigned
241 get_io_offset(nir_deref_var *deref, nir_instr *instr, nir_src *indirect,
242 struct lower_io_state *state)
243 {
244 bool found_indirect = false;
245 unsigned base_offset = 0;
246
247 nir_builder *b = &state->builder;
248 nir_builder_insert_before_instr(b, instr);
249
250 nir_deref *tail = &deref->deref;
251 while (tail->child != NULL) {
252 const struct glsl_type *parent_type = tail->type;
253 tail = tail->child;
254
255 if (tail->deref_type == nir_deref_type_array) {
256 nir_deref_array *deref_array = nir_deref_as_array(tail);
257 unsigned size = type_size(tail->type, state->is_scalar);
258
259 base_offset += size * deref_array->base_offset;
260
261 if (deref_array->deref_array_type == nir_deref_array_type_indirect) {
262 nir_ssa_def *mul =
263 nir_imul(b, nir_imm_int(b, size),
264 nir_ssa_for_src(b, deref_array->indirect, 1));
265
266 if (found_indirect) {
267 indirect->ssa =
268 nir_iadd(b, nir_ssa_for_src(b, *indirect, 1), mul);
269 } else {
270 indirect->ssa = mul;
271 }
272 indirect->is_ssa = true;
273 found_indirect = true;
274 }
275 } else if (tail->deref_type == nir_deref_type_struct) {
276 nir_deref_struct *deref_struct = nir_deref_as_struct(tail);
277
278 for (unsigned i = 0; i < deref_struct->index; i++)
279 base_offset += type_size(glsl_get_struct_field(parent_type, i),
280 state->is_scalar);
281 }
282 }
283
284 return base_offset;
285 }
286
287 static nir_intrinsic_op
288 load_op(nir_variable_mode mode, bool has_indirect)
289 {
290 nir_intrinsic_op op;
291 switch (mode) {
292 case nir_var_shader_in:
293 op = has_indirect ? nir_intrinsic_load_input_indirect :
294 nir_intrinsic_load_input;
295 break;
296 case nir_var_uniform:
297 op = has_indirect ? nir_intrinsic_load_uniform_indirect :
298 nir_intrinsic_load_uniform;
299 break;
300 default:
301 unreachable("Unknown variable mode");
302 }
303 return op;
304 }
305
306 static bool
307 nir_lower_io_block(nir_block *block, void *void_state)
308 {
309 struct lower_io_state *state = void_state;
310
311 nir_foreach_instr_safe(block, instr) {
312 if (instr->type != nir_instr_type_intrinsic)
313 continue;
314
315 nir_intrinsic_instr *intrin = nir_instr_as_intrinsic(instr);
316
317 switch (intrin->intrinsic) {
318 case nir_intrinsic_load_var: {
319 nir_variable_mode mode = intrin->variables[0]->var->data.mode;
320 if (mode != nir_var_shader_in && mode != nir_var_uniform)
321 continue;
322
323 bool has_indirect = deref_has_indirect(intrin->variables[0]);
324
325 nir_intrinsic_instr *load =
326 nir_intrinsic_instr_create(state->mem_ctx,
327 load_op(mode, has_indirect));
328 load->num_components = intrin->num_components;
329
330 nir_src indirect;
331 unsigned offset = get_io_offset(intrin->variables[0],
332 &intrin->instr, &indirect, state);
333 offset += intrin->variables[0]->var->data.driver_location;
334
335 load->const_index[0] = offset;
336
337 if (has_indirect)
338 load->src[0] = indirect;
339
340 if (intrin->dest.is_ssa) {
341 nir_ssa_dest_init(&load->instr, &load->dest,
342 intrin->num_components, NULL);
343 nir_ssa_def_rewrite_uses(&intrin->dest.ssa,
344 nir_src_for_ssa(&load->dest.ssa),
345 state->mem_ctx);
346 } else {
347 nir_dest_copy(&load->dest, &intrin->dest, state->mem_ctx);
348 }
349
350 nir_instr_insert_before(&intrin->instr, &load->instr);
351 nir_instr_remove(&intrin->instr);
352 break;
353 }
354
355 case nir_intrinsic_store_var: {
356 if (intrin->variables[0]->var->data.mode != nir_var_shader_out)
357 continue;
358
359 bool has_indirect = deref_has_indirect(intrin->variables[0]);
360
361 nir_intrinsic_op store_op;
362 if (has_indirect) {
363 store_op = nir_intrinsic_store_output_indirect;
364 } else {
365 store_op = nir_intrinsic_store_output;
366 }
367
368 nir_intrinsic_instr *store = nir_intrinsic_instr_create(state->mem_ctx,
369 store_op);
370 store->num_components = intrin->num_components;
371
372 nir_src indirect;
373 unsigned offset = get_io_offset(intrin->variables[0],
374 &intrin->instr, &indirect, state);
375 offset += intrin->variables[0]->var->data.driver_location;
376
377 store->const_index[0] = offset;
378
379 nir_src_copy(&store->src[0], &intrin->src[0], state->mem_ctx);
380
381 if (has_indirect)
382 store->src[1] = indirect;
383
384 nir_instr_insert_before(&intrin->instr, &store->instr);
385 nir_instr_remove(&intrin->instr);
386 break;
387 }
388
389 default:
390 break;
391 }
392 }
393
394 return true;
395 }
396
397 static void
398 nir_lower_io_impl(nir_function_impl *impl, bool is_scalar)
399 {
400 struct lower_io_state state;
401
402 nir_builder_init(&state.builder, impl);
403 state.mem_ctx = ralloc_parent(impl);
404 state.is_scalar = is_scalar;
405
406 nir_foreach_block(impl, nir_lower_io_block, &state);
407
408 nir_metadata_preserve(impl, nir_metadata_block_index |
409 nir_metadata_dominance);
410 }
411
412 void
413 nir_lower_io(nir_shader *shader, bool is_scalar)
414 {
415 nir_foreach_overload(shader, overload) {
416 if (overload->impl)
417 nir_lower_io_impl(overload->impl, is_scalar);
418 }
419 }