nir: add nir_var_shader_storage
[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 void
80 nir_assign_var_locations_scalar(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->data.mode == nir_var_shader_storage) &&
90 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->data.mode == nir_var_shader_storage) &&
158 var->interface_type != NULL)
159 continue;
160
161 if (_mesa_set_search(indirect_set, var))
162 continue;
163
164 var->data.driver_location = location;
165 location += type_size(var->type);
166 }
167
168 *direct_size = location;
169
170 foreach_list_typed(nir_variable, var, node, var_list) {
171 if ((var->data.mode == nir_var_uniform || var->data.mode == nir_var_shader_storage) &&
172 var->interface_type != NULL)
173 continue;
174
175 if (!_mesa_set_search(indirect_set, var))
176 continue;
177
178 var->data.driver_location = location;
179 location += type_size(var->type);
180 }
181
182 *size = location;
183
184 _mesa_set_destroy(indirect_set, NULL);
185 }
186
187 static unsigned
188 get_io_offset(nir_deref_var *deref, nir_instr *instr, nir_src *indirect,
189 struct lower_io_state *state)
190 {
191 bool found_indirect = false;
192 unsigned base_offset = 0;
193
194 nir_deref *tail = &deref->deref;
195 while (tail->child != NULL) {
196 const struct glsl_type *parent_type = tail->type;
197 tail = tail->child;
198
199 if (tail->deref_type == nir_deref_type_array) {
200 nir_deref_array *deref_array = nir_deref_as_array(tail);
201 unsigned size = type_size(tail->type);
202
203 base_offset += size * deref_array->base_offset;
204
205 if (deref_array->deref_array_type == nir_deref_array_type_indirect) {
206 nir_load_const_instr *load_const =
207 nir_load_const_instr_create(state->mem_ctx, 1);
208 load_const->value.u[0] = size;
209 nir_instr_insert_before(instr, &load_const->instr);
210
211 nir_alu_instr *mul = nir_alu_instr_create(state->mem_ctx,
212 nir_op_imul);
213 mul->src[0].src.is_ssa = true;
214 mul->src[0].src.ssa = &load_const->def;
215 nir_src_copy(&mul->src[1].src, &deref_array->indirect,
216 state->mem_ctx);
217 mul->dest.write_mask = 1;
218 nir_ssa_dest_init(&mul->instr, &mul->dest.dest, 1, NULL);
219 nir_instr_insert_before(instr, &mul->instr);
220
221 if (found_indirect) {
222 nir_alu_instr *add = nir_alu_instr_create(state->mem_ctx,
223 nir_op_iadd);
224 add->src[0].src = *indirect;
225 add->src[1].src.is_ssa = true;
226 add->src[1].src.ssa = &mul->dest.dest.ssa;
227 add->dest.write_mask = 1;
228 nir_ssa_dest_init(&add->instr, &add->dest.dest, 1, NULL);
229 nir_instr_insert_before(instr, &add->instr);
230
231 indirect->is_ssa = true;
232 indirect->ssa = &add->dest.dest.ssa;
233 } else {
234 indirect->is_ssa = true;
235 indirect->ssa = &mul->dest.dest.ssa;
236 found_indirect = true;
237 }
238 }
239 } else if (tail->deref_type == nir_deref_type_struct) {
240 nir_deref_struct *deref_struct = nir_deref_as_struct(tail);
241
242 for (unsigned i = 0; i < deref_struct->index; i++)
243 base_offset += type_size(glsl_get_struct_field(parent_type, i));
244 }
245 }
246
247 return base_offset;
248 }
249
250 static bool
251 nir_lower_io_block(nir_block *block, void *void_state)
252 {
253 struct lower_io_state *state = void_state;
254
255 nir_foreach_instr_safe(block, instr) {
256 if (instr->type != nir_instr_type_intrinsic)
257 continue;
258
259 nir_intrinsic_instr *intrin = nir_instr_as_intrinsic(instr);
260
261 switch (intrin->intrinsic) {
262 case nir_intrinsic_load_var: {
263 nir_variable_mode mode = intrin->variables[0]->var->data.mode;
264 if (mode != nir_var_shader_in && mode != nir_var_uniform)
265 continue;
266
267 bool has_indirect = deref_has_indirect(intrin->variables[0]);
268
269 /* Figure out the opcode */
270 nir_intrinsic_op load_op;
271 switch (mode) {
272 case nir_var_shader_in:
273 load_op = has_indirect ? nir_intrinsic_load_input_indirect :
274 nir_intrinsic_load_input;
275 break;
276 case nir_var_uniform:
277 load_op = has_indirect ? nir_intrinsic_load_uniform_indirect :
278 nir_intrinsic_load_uniform;
279 break;
280 default:
281 unreachable("Unknown variable mode");
282 }
283
284 nir_intrinsic_instr *load = nir_intrinsic_instr_create(state->mem_ctx,
285 load_op);
286 load->num_components = intrin->num_components;
287
288 nir_src indirect;
289 unsigned offset = get_io_offset(intrin->variables[0],
290 &intrin->instr, &indirect, state);
291 offset += intrin->variables[0]->var->data.driver_location;
292
293 load->const_index[0] = offset;
294
295 if (has_indirect)
296 load->src[0] = indirect;
297
298 if (intrin->dest.is_ssa) {
299 nir_ssa_dest_init(&load->instr, &load->dest,
300 intrin->num_components, NULL);
301 nir_ssa_def_rewrite_uses(&intrin->dest.ssa,
302 nir_src_for_ssa(&load->dest.ssa),
303 state->mem_ctx);
304 } else {
305 nir_dest_copy(&load->dest, &intrin->dest, state->mem_ctx);
306 }
307
308 nir_instr_insert_before(&intrin->instr, &load->instr);
309 nir_instr_remove(&intrin->instr);
310 break;
311 }
312
313 case nir_intrinsic_store_var: {
314 if (intrin->variables[0]->var->data.mode != nir_var_shader_out)
315 continue;
316
317 bool has_indirect = deref_has_indirect(intrin->variables[0]);
318
319 nir_intrinsic_op store_op;
320 if (has_indirect) {
321 store_op = nir_intrinsic_store_output_indirect;
322 } else {
323 store_op = nir_intrinsic_store_output;
324 }
325
326 nir_intrinsic_instr *store = nir_intrinsic_instr_create(state->mem_ctx,
327 store_op);
328 store->num_components = intrin->num_components;
329
330 nir_src indirect;
331 unsigned offset = get_io_offset(intrin->variables[0],
332 &intrin->instr, &indirect, state);
333 offset += intrin->variables[0]->var->data.driver_location;
334
335 store->const_index[0] = offset;
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 }