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