Merge remote-tracking branch 'mesa-public/master' into vulkan
[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 #include "nir_builder.h"
36
37 struct lower_io_state {
38 nir_builder builder;
39 void *mem_ctx;
40 int (*type_size)(const struct glsl_type *type);
41 nir_variable_mode mode;
42 };
43
44 void
45 nir_assign_var_locations(struct exec_list *var_list, unsigned *size,
46 int (*type_size)(const struct glsl_type *))
47 {
48 unsigned location = 0;
49
50 foreach_list_typed(nir_variable, var, node, var_list) {
51 /*
52 * UBO's have their own address spaces, so don't count them towards the
53 * number of global uniforms
54 */
55 if ((var->data.mode == nir_var_uniform || var->data.mode == nir_var_shader_storage) &&
56 var->interface_type != NULL)
57 continue;
58
59 var->data.driver_location = location;
60 location += type_size(var->type);
61 }
62
63 *size = location;
64 }
65
66 static bool
67 deref_has_indirect(nir_deref_var *deref)
68 {
69 for (nir_deref *tail = deref->deref.child; tail; tail = tail->child) {
70 if (tail->deref_type == nir_deref_type_array) {
71 nir_deref_array *arr = nir_deref_as_array(tail);
72 if (arr->deref_array_type == nir_deref_array_type_indirect)
73 return true;
74 }
75 }
76
77 return false;
78 }
79
80 static unsigned
81 get_io_offset(nir_deref_var *deref, nir_instr *instr, nir_src *indirect,
82 struct lower_io_state *state)
83 {
84 bool found_indirect = false;
85 unsigned base_offset = 0;
86
87 nir_builder *b = &state->builder;
88 b->cursor = nir_before_instr(instr);
89
90 nir_deref *tail = &deref->deref;
91 while (tail->child != NULL) {
92 const struct glsl_type *parent_type = tail->type;
93 tail = tail->child;
94
95 if (tail->deref_type == nir_deref_type_array) {
96 nir_deref_array *deref_array = nir_deref_as_array(tail);
97 unsigned size = state->type_size(tail->type);
98
99 base_offset += size * deref_array->base_offset;
100
101 if (deref_array->deref_array_type == nir_deref_array_type_indirect) {
102 nir_ssa_def *mul =
103 nir_imul(b, nir_imm_int(b, size),
104 nir_ssa_for_src(b, deref_array->indirect, 1));
105
106 if (found_indirect) {
107 indirect->ssa =
108 nir_iadd(b, nir_ssa_for_src(b, *indirect, 1), mul);
109 } else {
110 indirect->ssa = mul;
111 }
112 indirect->is_ssa = true;
113 found_indirect = true;
114 }
115 } else if (tail->deref_type == nir_deref_type_struct) {
116 nir_deref_struct *deref_struct = nir_deref_as_struct(tail);
117
118 for (unsigned i = 0; i < deref_struct->index; i++) {
119 base_offset +=
120 state->type_size(glsl_get_struct_field(parent_type, i));
121 }
122 }
123 }
124
125 return base_offset;
126 }
127
128 static nir_intrinsic_op
129 load_op(nir_variable_mode mode, bool has_indirect)
130 {
131 nir_intrinsic_op op;
132 switch (mode) {
133 case nir_var_shader_in:
134 op = has_indirect ? nir_intrinsic_load_input_indirect :
135 nir_intrinsic_load_input;
136 break;
137 case nir_var_uniform:
138 op = has_indirect ? nir_intrinsic_load_uniform_indirect :
139 nir_intrinsic_load_uniform;
140 break;
141 default:
142 unreachable("Unknown variable mode");
143 }
144 return op;
145 }
146
147 static bool
148 nir_lower_io_block(nir_block *block, void *void_state)
149 {
150 struct lower_io_state *state = void_state;
151
152 nir_foreach_instr_safe(block, instr) {
153 if (instr->type != nir_instr_type_intrinsic)
154 continue;
155
156 nir_intrinsic_instr *intrin = nir_instr_as_intrinsic(instr);
157
158 if (intrin->intrinsic != nir_intrinsic_load_var &&
159 intrin->intrinsic != nir_intrinsic_store_var)
160 continue;
161
162 nir_variable_mode mode = intrin->variables[0]->var->data.mode;
163
164 if (state->mode != -1 && state->mode != mode)
165 continue;
166
167 switch (intrin->intrinsic) {
168 case nir_intrinsic_load_var: {
169 if (mode != nir_var_shader_in && mode != nir_var_uniform)
170 continue;
171
172 bool has_indirect = deref_has_indirect(intrin->variables[0]);
173
174 nir_intrinsic_instr *load =
175 nir_intrinsic_instr_create(state->mem_ctx,
176 load_op(mode, has_indirect));
177 load->num_components = intrin->num_components;
178
179 nir_src indirect;
180 unsigned offset = get_io_offset(intrin->variables[0],
181 &intrin->instr, &indirect, state);
182
183 unsigned location = intrin->variables[0]->var->data.driver_location;
184 if (mode == nir_var_uniform) {
185 load->const_index[0] = location;
186 load->const_index[1] = offset;
187 } else {
188 load->const_index[0] = location + offset;
189 }
190
191 if (has_indirect)
192 load->src[0] = indirect;
193
194 if (intrin->dest.is_ssa) {
195 nir_ssa_dest_init(&load->instr, &load->dest,
196 intrin->num_components, NULL);
197 nir_ssa_def_rewrite_uses(&intrin->dest.ssa,
198 nir_src_for_ssa(&load->dest.ssa));
199 } else {
200 nir_dest_copy(&load->dest, &intrin->dest, state->mem_ctx);
201 }
202
203 nir_instr_insert_before(&intrin->instr, &load->instr);
204 nir_instr_remove(&intrin->instr);
205 break;
206 }
207
208 case nir_intrinsic_store_var: {
209 if (intrin->variables[0]->var->data.mode != nir_var_shader_out)
210 continue;
211
212 bool has_indirect = deref_has_indirect(intrin->variables[0]);
213
214 nir_intrinsic_op store_op;
215 if (has_indirect) {
216 store_op = nir_intrinsic_store_output_indirect;
217 } else {
218 store_op = nir_intrinsic_store_output;
219 }
220
221 nir_intrinsic_instr *store = nir_intrinsic_instr_create(state->mem_ctx,
222 store_op);
223 store->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 store->const_index[0] = offset;
231
232 nir_src_copy(&store->src[0], &intrin->src[0], store);
233
234 if (has_indirect)
235 store->src[1] = indirect;
236
237 nir_instr_insert_before(&intrin->instr, &store->instr);
238 nir_instr_remove(&intrin->instr);
239 break;
240 }
241
242 default:
243 break;
244 }
245 }
246
247 return true;
248 }
249
250 static void
251 nir_lower_io_impl(nir_function_impl *impl,
252 nir_variable_mode mode,
253 int (*type_size)(const struct glsl_type *))
254 {
255 struct lower_io_state state;
256
257 nir_builder_init(&state.builder, impl);
258 state.mem_ctx = ralloc_parent(impl);
259 state.mode = mode;
260 state.type_size = type_size;
261
262 nir_foreach_block(impl, nir_lower_io_block, &state);
263
264 nir_metadata_preserve(impl, nir_metadata_block_index |
265 nir_metadata_dominance);
266 }
267
268 void
269 nir_lower_io(nir_shader *shader, nir_variable_mode mode,
270 int (*type_size)(const struct glsl_type *))
271 {
272 nir_foreach_overload(shader, overload) {
273 if (overload->impl)
274 nir_lower_io_impl(overload->impl, mode, type_size);
275 }
276 }