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