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