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