nir: Use a list instead of a hash_table for inputs, outputs, and uniforms
[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_VOID:
71 case GLSL_TYPE_ERROR:
72 case GLSL_TYPE_DOUBLE:
73 unreachable("not reached");
74 }
75
76 return 0;
77 }
78
79 static void
80 assign_var_locations(struct exec_list *var_list, unsigned *size)
81 {
82 unsigned location = 0;
83
84 foreach_list_typed(nir_variable, var, node, var_list) {
85 /*
86 * UBO's have their own address spaces, so don't count them towards the
87 * number of global uniforms
88 */
89 if (var->data.mode == nir_var_uniform && var->interface_type != NULL)
90 continue;
91
92 var->data.driver_location = location;
93 location += type_size(var->type);
94 }
95
96 *size = location;
97 }
98
99 static void
100 assign_var_locations_shader(nir_shader *shader)
101 {
102 assign_var_locations(&shader->inputs, &shader->num_inputs);
103 assign_var_locations(&shader->outputs, &shader->num_outputs);
104 assign_var_locations(&shader->uniforms, &shader->num_uniforms);
105 }
106
107 static bool
108 deref_has_indirect(nir_deref_var *deref)
109 {
110 for (nir_deref *tail = deref->deref.child; tail; tail = tail->child) {
111 if (tail->deref_type == nir_deref_type_array) {
112 nir_deref_array *arr = nir_deref_as_array(tail);
113 if (arr->deref_array_type == nir_deref_array_type_indirect)
114 return true;
115 }
116 }
117
118 return false;
119 }
120
121 static unsigned
122 get_io_offset(nir_deref_var *deref, nir_instr *instr, nir_src *indirect,
123 struct lower_io_state *state)
124 {
125 bool found_indirect = false;
126 unsigned base_offset = 0;
127
128 nir_deref *tail = &deref->deref;
129 while (tail->child != NULL) {
130 const struct glsl_type *parent_type = tail->type;
131 tail = tail->child;
132
133 if (tail->deref_type == nir_deref_type_array) {
134 nir_deref_array *deref_array = nir_deref_as_array(tail);
135 unsigned size = type_size(tail->type);
136
137 base_offset += size * deref_array->base_offset;
138
139 if (deref_array->deref_array_type == nir_deref_array_type_indirect) {
140 nir_load_const_instr *load_const =
141 nir_load_const_instr_create(state->mem_ctx, 1);
142 load_const->value.u[0] = size;
143 nir_instr_insert_before(instr, &load_const->instr);
144
145 nir_alu_instr *mul = nir_alu_instr_create(state->mem_ctx,
146 nir_op_imul);
147 mul->src[0].src.is_ssa = true;
148 mul->src[0].src.ssa = &load_const->def;
149 nir_src_copy(&mul->src[1].src, &deref_array->indirect,
150 state->mem_ctx);
151 mul->dest.write_mask = 1;
152 nir_ssa_dest_init(&mul->instr, &mul->dest.dest, 1, NULL);
153 nir_instr_insert_before(instr, &mul->instr);
154
155 if (found_indirect) {
156 nir_alu_instr *add = nir_alu_instr_create(state->mem_ctx,
157 nir_op_iadd);
158 add->src[0].src = *indirect;
159 add->src[1].src.is_ssa = true;
160 add->src[1].src.ssa = &mul->dest.dest.ssa;
161 add->dest.write_mask = 1;
162 nir_ssa_dest_init(&add->instr, &add->dest.dest, 1, NULL);
163 nir_instr_insert_before(instr, &add->instr);
164
165 indirect->is_ssa = true;
166 indirect->ssa = &add->dest.dest.ssa;
167 } else {
168 indirect->is_ssa = true;
169 indirect->ssa = &mul->dest.dest.ssa;
170 found_indirect = true;
171 }
172 }
173 } else if (tail->deref_type == nir_deref_type_struct) {
174 nir_deref_struct *deref_struct = nir_deref_as_struct(tail);
175
176 for (unsigned i = 0; i < deref_struct->index; i++)
177 base_offset += type_size(glsl_get_struct_field(parent_type, i));
178 }
179 }
180
181 return base_offset;
182 }
183
184 static bool
185 nir_lower_io_block(nir_block *block, void *void_state)
186 {
187 struct lower_io_state *state = void_state;
188
189 nir_foreach_instr_safe(block, instr) {
190 if (instr->type != nir_instr_type_intrinsic)
191 continue;
192
193 nir_intrinsic_instr *intrin = nir_instr_as_intrinsic(instr);
194
195 switch (intrin->intrinsic) {
196 case nir_intrinsic_load_var: {
197 nir_variable_mode mode = intrin->variables[0]->var->data.mode;
198 if (mode != nir_var_shader_in && mode != nir_var_uniform)
199 continue;
200
201 bool has_indirect = deref_has_indirect(intrin->variables[0]);
202
203 /* Figure out the opcode */
204 nir_intrinsic_op load_op;
205 switch (mode) {
206 case nir_var_shader_in:
207 load_op = has_indirect ? nir_intrinsic_load_input_indirect :
208 nir_intrinsic_load_input;
209 break;
210 case nir_var_uniform:
211 load_op = has_indirect ? nir_intrinsic_load_uniform_indirect :
212 nir_intrinsic_load_uniform;
213 break;
214 default:
215 unreachable("Unknown variable mode");
216 }
217
218 nir_intrinsic_instr *load = nir_intrinsic_instr_create(state->mem_ctx,
219 load_op);
220 load->num_components = intrin->num_components;
221
222 nir_src indirect;
223 unsigned offset = get_io_offset(intrin->variables[0],
224 &intrin->instr, &indirect, state);
225 offset += intrin->variables[0]->var->data.driver_location;
226
227 load->const_index[0] = offset;
228 load->const_index[1] = 1;
229
230 if (has_indirect)
231 load->src[0] = indirect;
232
233 if (intrin->dest.is_ssa) {
234 nir_ssa_dest_init(&load->instr, &load->dest,
235 intrin->num_components, NULL);
236 nir_ssa_def_rewrite_uses(&intrin->dest.ssa,
237 nir_src_for_ssa(&load->dest.ssa),
238 state->mem_ctx);
239 } else {
240 nir_dest_copy(&load->dest, &intrin->dest, state->mem_ctx);
241 }
242
243 nir_instr_insert_before(&intrin->instr, &load->instr);
244 nir_instr_remove(&intrin->instr);
245 break;
246 }
247
248 case nir_intrinsic_store_var: {
249 if (intrin->variables[0]->var->data.mode != nir_var_shader_out)
250 continue;
251
252 bool has_indirect = deref_has_indirect(intrin->variables[0]);
253
254 nir_intrinsic_op store_op;
255 if (has_indirect) {
256 store_op = nir_intrinsic_store_output_indirect;
257 } else {
258 store_op = nir_intrinsic_store_output;
259 }
260
261 nir_intrinsic_instr *store = nir_intrinsic_instr_create(state->mem_ctx,
262 store_op);
263 store->num_components = intrin->num_components;
264
265 nir_src indirect;
266 unsigned offset = get_io_offset(intrin->variables[0],
267 &intrin->instr, &indirect, state);
268 offset += intrin->variables[0]->var->data.driver_location;
269
270 store->const_index[0] = offset;
271 store->const_index[1] = 1;
272
273 nir_src_copy(&store->src[0], &intrin->src[0], state->mem_ctx);
274
275 if (has_indirect)
276 store->src[1] = indirect;
277
278 nir_instr_insert_before(&intrin->instr, &store->instr);
279 nir_instr_remove(&intrin->instr);
280 break;
281 }
282
283 default:
284 break;
285 }
286 }
287
288 return true;
289 }
290
291 static void
292 nir_lower_io_impl(nir_function_impl *impl)
293 {
294 struct lower_io_state state;
295
296 state.mem_ctx = ralloc_parent(impl);
297
298 nir_foreach_block(impl, nir_lower_io_block, &state);
299
300 nir_metadata_preserve(impl, nir_metadata_block_index |
301 nir_metadata_dominance);
302 }
303
304 void
305 nir_lower_io(nir_shader *shader)
306 {
307 assign_var_locations_shader(shader);
308
309 nir_foreach_overload(shader, overload) {
310 if (overload->impl)
311 nir_lower_io_impl(overload->impl);
312 }
313 }