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