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