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