nir/lower_io: Make get_io_offset() return a nir_ssa_def * for indirects.
[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 nir_foreach_variable(var, 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 unsigned
67 get_io_offset(nir_deref_var *deref, nir_instr *instr,
68 nir_ssa_def **out_indirect,
69 struct lower_io_state *state)
70 {
71 nir_ssa_def *indirect = NULL;
72 unsigned base_offset = 0;
73
74 nir_builder *b = &state->builder;
75 b->cursor = nir_before_instr(instr);
76
77 nir_deref *tail = &deref->deref;
78 while (tail->child != NULL) {
79 const struct glsl_type *parent_type = tail->type;
80 tail = tail->child;
81
82 if (tail->deref_type == nir_deref_type_array) {
83 nir_deref_array *deref_array = nir_deref_as_array(tail);
84 unsigned size = state->type_size(tail->type);
85
86 base_offset += size * deref_array->base_offset;
87
88 if (deref_array->deref_array_type == nir_deref_array_type_indirect) {
89 nir_ssa_def *mul =
90 nir_imul(b, nir_imm_int(b, size),
91 nir_ssa_for_src(b, deref_array->indirect, 1));
92
93 indirect = indirect ? nir_iadd(b, indirect, mul) : mul;
94 }
95 } else if (tail->deref_type == nir_deref_type_struct) {
96 nir_deref_struct *deref_struct = nir_deref_as_struct(tail);
97
98 for (unsigned i = 0; i < deref_struct->index; i++) {
99 base_offset +=
100 state->type_size(glsl_get_struct_field(parent_type, i));
101 }
102 }
103 }
104
105 *out_indirect = indirect;
106 return base_offset;
107 }
108
109 static nir_intrinsic_op
110 load_op(nir_variable_mode mode, bool has_indirect)
111 {
112 nir_intrinsic_op op;
113 switch (mode) {
114 case nir_var_shader_in:
115 op = has_indirect ? nir_intrinsic_load_input_indirect :
116 nir_intrinsic_load_input;
117 break;
118 case nir_var_uniform:
119 op = has_indirect ? nir_intrinsic_load_uniform_indirect :
120 nir_intrinsic_load_uniform;
121 break;
122 default:
123 unreachable("Unknown variable mode");
124 }
125 return op;
126 }
127
128 static bool
129 nir_lower_io_block(nir_block *block, void *void_state)
130 {
131 struct lower_io_state *state = void_state;
132
133 nir_foreach_instr_safe(block, instr) {
134 if (instr->type != nir_instr_type_intrinsic)
135 continue;
136
137 nir_intrinsic_instr *intrin = nir_instr_as_intrinsic(instr);
138
139 if (intrin->intrinsic != nir_intrinsic_load_var &&
140 intrin->intrinsic != nir_intrinsic_store_var)
141 continue;
142
143 nir_variable_mode mode = intrin->variables[0]->var->data.mode;
144
145 if (state->mode != -1 && state->mode != mode)
146 continue;
147
148 switch (intrin->intrinsic) {
149 case nir_intrinsic_load_var: {
150 if (mode != nir_var_shader_in && mode != nir_var_uniform)
151 continue;
152
153 nir_ssa_def *indirect;
154
155 unsigned offset = get_io_offset(intrin->variables[0], &intrin->instr,
156 &indirect, state);
157
158 nir_intrinsic_instr *load =
159 nir_intrinsic_instr_create(state->mem_ctx,
160 load_op(mode, indirect));
161 load->num_components = intrin->num_components;
162
163 unsigned location = intrin->variables[0]->var->data.driver_location;
164 if (mode == nir_var_uniform) {
165 load->const_index[0] = location;
166 load->const_index[1] = offset;
167 } else {
168 load->const_index[0] = location + offset;
169 }
170
171 if (indirect)
172 load->src[0] = nir_src_for_ssa(indirect);
173
174 if (intrin->dest.is_ssa) {
175 nir_ssa_dest_init(&load->instr, &load->dest,
176 intrin->num_components, NULL);
177 nir_ssa_def_rewrite_uses(&intrin->dest.ssa,
178 nir_src_for_ssa(&load->dest.ssa));
179 } else {
180 nir_dest_copy(&load->dest, &intrin->dest, state->mem_ctx);
181 }
182
183 nir_instr_insert_before(&intrin->instr, &load->instr);
184 nir_instr_remove(&intrin->instr);
185 break;
186 }
187
188 case nir_intrinsic_store_var: {
189 if (intrin->variables[0]->var->data.mode != nir_var_shader_out)
190 continue;
191
192 nir_ssa_def *indirect;
193
194 unsigned offset = get_io_offset(intrin->variables[0], &intrin->instr,
195 &indirect, state);
196 offset += intrin->variables[0]->var->data.driver_location;
197
198 nir_intrinsic_op store_op;
199 if (indirect) {
200 store_op = nir_intrinsic_store_output_indirect;
201 } else {
202 store_op = nir_intrinsic_store_output;
203 }
204
205 nir_intrinsic_instr *store = nir_intrinsic_instr_create(state->mem_ctx,
206 store_op);
207 store->num_components = intrin->num_components;
208 store->const_index[0] = offset;
209
210 nir_src_copy(&store->src[0], &intrin->src[0], store);
211
212 if (indirect)
213 store->src[1] = nir_src_for_ssa(indirect);
214
215 nir_instr_insert_before(&intrin->instr, &store->instr);
216 nir_instr_remove(&intrin->instr);
217 break;
218 }
219
220 default:
221 break;
222 }
223 }
224
225 return true;
226 }
227
228 static void
229 nir_lower_io_impl(nir_function_impl *impl,
230 nir_variable_mode mode,
231 int (*type_size)(const struct glsl_type *))
232 {
233 struct lower_io_state state;
234
235 nir_builder_init(&state.builder, impl);
236 state.mem_ctx = ralloc_parent(impl);
237 state.mode = mode;
238 state.type_size = type_size;
239
240 nir_foreach_block(impl, nir_lower_io_block, &state);
241
242 nir_metadata_preserve(impl, nir_metadata_block_index |
243 nir_metadata_dominance);
244 }
245
246 void
247 nir_lower_io(nir_shader *shader, nir_variable_mode mode,
248 int (*type_size)(const struct glsl_type *))
249 {
250 nir_foreach_overload(shader, overload) {
251 if (overload->impl)
252 nir_lower_io_impl(overload->impl, mode, type_size);
253 }
254 }