nir: Add a "compact array" flag and IO lowering code.
[mesa.git] / src / compiler / nir / nir_gather_info.c
1 /*
2 * Copyright © 2015 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
24 #include "main/mtypes.h"
25 #include "nir.h"
26
27 static void
28 set_io_mask(nir_shader *shader, nir_variable *var, int offset, int len)
29 {
30 for (int i = 0; i < len; i++) {
31 assert(var->data.location != -1);
32
33 int idx = var->data.location + offset + i;
34 bool is_patch_generic = var->data.patch &&
35 idx != VARYING_SLOT_TESS_LEVEL_INNER &&
36 idx != VARYING_SLOT_TESS_LEVEL_OUTER &&
37 idx != VARYING_SLOT_BOUNDING_BOX0 &&
38 idx != VARYING_SLOT_BOUNDING_BOX1;
39 uint64_t bitfield;
40
41 if (is_patch_generic) {
42 assert(idx >= VARYING_SLOT_PATCH0 && idx < VARYING_SLOT_TESS_MAX);
43 bitfield = BITFIELD64_BIT(idx - VARYING_SLOT_PATCH0);
44 }
45 else {
46 assert(idx < VARYING_SLOT_MAX);
47 bitfield = BITFIELD64_BIT(idx);
48 }
49
50 if (var->data.mode == nir_var_shader_in) {
51 if (is_patch_generic)
52 shader->info->patch_inputs_read |= bitfield;
53 else
54 shader->info->inputs_read |= bitfield;
55
56 /* double inputs read is only for vertex inputs */
57 if (shader->stage == MESA_SHADER_VERTEX &&
58 glsl_type_is_dual_slot(glsl_without_array(var->type)))
59 shader->info->double_inputs_read |= bitfield;
60
61 if (shader->stage == MESA_SHADER_FRAGMENT) {
62 shader->info->fs.uses_sample_qualifier |= var->data.sample;
63 }
64 } else {
65 assert(var->data.mode == nir_var_shader_out);
66 if (is_patch_generic) {
67 shader->info->patch_outputs_written |= bitfield;
68 } else if (!var->data.read_only) {
69 shader->info->outputs_written |= bitfield;
70 }
71
72 if (var->data.fb_fetch_output)
73 shader->info->outputs_read |= bitfield;
74 }
75 }
76 }
77
78 /**
79 * Mark an entire variable as used. Caller must ensure that the variable
80 * represents a shader input or output.
81 */
82 static void
83 mark_whole_variable(nir_shader *shader, nir_variable *var)
84 {
85 const struct glsl_type *type = var->type;
86 bool is_vertex_input = false;
87
88 if (nir_is_per_vertex_io(var, shader->stage)) {
89 assert(glsl_type_is_array(type));
90 type = glsl_get_array_element(type);
91 }
92
93 if (shader->stage == MESA_SHADER_VERTEX &&
94 var->data.mode == nir_var_shader_in)
95 is_vertex_input = true;
96
97 const unsigned slots =
98 var->data.compact ? DIV_ROUND_UP(glsl_get_length(type), 4)
99 : glsl_count_attribute_slots(type, is_vertex_input);
100
101 set_io_mask(shader, var, 0, slots);
102 }
103
104 static unsigned
105 get_io_offset(nir_deref_var *deref, bool is_vertex_input)
106 {
107 unsigned offset = 0;
108
109 nir_deref *tail = &deref->deref;
110 while (tail->child != NULL) {
111 tail = tail->child;
112
113 if (tail->deref_type == nir_deref_type_array) {
114 nir_deref_array *deref_array = nir_deref_as_array(tail);
115
116 if (deref_array->deref_array_type == nir_deref_array_type_indirect) {
117 return -1;
118 }
119
120 offset += glsl_count_attribute_slots(tail->type, is_vertex_input) *
121 deref_array->base_offset;
122 }
123 /* TODO: we can get the offset for structs here see nir_lower_io() */
124 }
125
126 return offset;
127 }
128
129 /**
130 * Try to mark a portion of the given varying as used. Caller must ensure
131 * that the variable represents a shader input or output.
132 *
133 * If the index can't be interpreted as a constant, or some other problem
134 * occurs, then nothing will be marked and false will be returned.
135 */
136 static bool
137 try_mask_partial_io(nir_shader *shader, nir_deref_var *deref)
138 {
139 nir_variable *var = deref->var;
140 const struct glsl_type *type = var->type;
141
142 if (nir_is_per_vertex_io(var, shader->stage)) {
143 assert(glsl_type_is_array(type));
144 type = glsl_get_array_element(type);
145 }
146
147 /* The code below only handles:
148 *
149 * - Indexing into matrices
150 * - Indexing into arrays of (arrays, matrices, vectors, or scalars)
151 *
152 * For now, we just give up if we see varying structs and arrays of structs
153 * here marking the entire variable as used.
154 */
155 if (!(glsl_type_is_matrix(type) ||
156 (glsl_type_is_array(type) && !var->data.compact &&
157 (glsl_type_is_numeric(glsl_without_array(type)) ||
158 glsl_type_is_boolean(glsl_without_array(type)))))) {
159
160 /* If we don't know how to handle this case, give up and let the
161 * caller mark the whole variable as used.
162 */
163 return false;
164 }
165
166 bool is_vertex_input = false;
167 if (shader->stage == MESA_SHADER_VERTEX &&
168 var->data.mode == nir_var_shader_in)
169 is_vertex_input = true;
170
171 unsigned offset = get_io_offset(deref, is_vertex_input);
172 if (offset == -1)
173 return false;
174
175 unsigned num_elems;
176 unsigned elem_width = 1;
177 unsigned mat_cols = 1;
178 if (glsl_type_is_array(type)) {
179 num_elems = glsl_get_aoa_size(type);
180 if (glsl_type_is_matrix(glsl_without_array(type)))
181 mat_cols = glsl_get_matrix_columns(glsl_without_array(type));
182 } else {
183 num_elems = glsl_get_matrix_columns(type);
184 }
185
186 /* double element width for double types that takes two slots */
187 if (!is_vertex_input &&
188 glsl_type_is_dual_slot(glsl_without_array(type))) {
189 elem_width *= 2;
190 }
191
192 if (offset >= num_elems * elem_width * mat_cols) {
193 /* Constant index outside the bounds of the matrix/array. This could
194 * arise as a result of constant folding of a legal GLSL program.
195 *
196 * Even though the spec says that indexing outside the bounds of a
197 * matrix/array results in undefined behaviour, we don't want to pass
198 * out-of-range values to set_io_mask() (since this could result in
199 * slots that don't exist being marked as used), so just let the caller
200 * mark the whole variable as used.
201 */
202 return false;
203 }
204
205 set_io_mask(shader, var, offset, elem_width);
206 return true;
207 }
208
209 static void
210 gather_intrinsic_info(nir_intrinsic_instr *instr, nir_shader *shader)
211 {
212 switch (instr->intrinsic) {
213 case nir_intrinsic_discard:
214 case nir_intrinsic_discard_if:
215 assert(shader->stage == MESA_SHADER_FRAGMENT);
216 shader->info->fs.uses_discard = true;
217 break;
218
219 case nir_intrinsic_interp_var_at_centroid:
220 case nir_intrinsic_interp_var_at_sample:
221 case nir_intrinsic_interp_var_at_offset:
222 case nir_intrinsic_load_var:
223 case nir_intrinsic_store_var:
224 if (instr->variables[0]->var->data.mode == nir_var_shader_in ||
225 instr->variables[0]->var->data.mode == nir_var_shader_out) {
226 if (!try_mask_partial_io(shader, instr->variables[0]))
227 mark_whole_variable(shader, instr->variables[0]->var);
228 }
229 break;
230
231 case nir_intrinsic_load_draw_id:
232 case nir_intrinsic_load_front_face:
233 case nir_intrinsic_load_vertex_id:
234 case nir_intrinsic_load_vertex_id_zero_base:
235 case nir_intrinsic_load_base_vertex:
236 case nir_intrinsic_load_base_instance:
237 case nir_intrinsic_load_instance_id:
238 case nir_intrinsic_load_sample_id:
239 case nir_intrinsic_load_sample_pos:
240 case nir_intrinsic_load_sample_mask_in:
241 case nir_intrinsic_load_primitive_id:
242 case nir_intrinsic_load_invocation_id:
243 case nir_intrinsic_load_local_invocation_id:
244 case nir_intrinsic_load_local_invocation_index:
245 case nir_intrinsic_load_work_group_id:
246 case nir_intrinsic_load_num_work_groups:
247 case nir_intrinsic_load_tess_coord:
248 case nir_intrinsic_load_tess_level_outer:
249 case nir_intrinsic_load_tess_level_inner:
250 shader->info->system_values_read |=
251 (1 << nir_system_value_from_intrinsic(instr->intrinsic));
252 break;
253
254 case nir_intrinsic_end_primitive:
255 case nir_intrinsic_end_primitive_with_counter:
256 assert(shader->stage == MESA_SHADER_GEOMETRY);
257 shader->info->gs.uses_end_primitive = 1;
258 break;
259
260 default:
261 break;
262 }
263 }
264
265 static void
266 gather_tex_info(nir_tex_instr *instr, nir_shader *shader)
267 {
268 if (instr->op == nir_texop_tg4)
269 shader->info->uses_texture_gather = true;
270 }
271
272 static void
273 gather_info_block(nir_block *block, nir_shader *shader)
274 {
275 nir_foreach_instr(instr, block) {
276 switch (instr->type) {
277 case nir_instr_type_intrinsic:
278 gather_intrinsic_info(nir_instr_as_intrinsic(instr), shader);
279 break;
280 case nir_instr_type_tex:
281 gather_tex_info(nir_instr_as_tex(instr), shader);
282 break;
283 case nir_instr_type_call:
284 assert(!"nir_shader_gather_info only works if functions are inlined");
285 break;
286 default:
287 break;
288 }
289 }
290 }
291
292 void
293 nir_shader_gather_info(nir_shader *shader, nir_function_impl *entrypoint)
294 {
295 shader->info->num_textures = 0;
296 shader->info->num_images = 0;
297 nir_foreach_variable(var, &shader->uniforms) {
298 const struct glsl_type *type = var->type;
299 unsigned count = 1;
300 if (glsl_type_is_array(type)) {
301 count = glsl_get_aoa_size(type);
302 type = glsl_without_array(type);
303 }
304
305 if (glsl_type_is_image(type)) {
306 shader->info->num_images += count;
307 } else if (glsl_type_is_sampler(type)) {
308 shader->info->num_textures += count;
309 }
310 }
311
312 shader->info->inputs_read = 0;
313 shader->info->outputs_written = 0;
314 shader->info->outputs_read = 0;
315 shader->info->double_inputs_read = 0;
316 shader->info->patch_inputs_read = 0;
317 shader->info->patch_outputs_written = 0;
318 shader->info->system_values_read = 0;
319 if (shader->stage == MESA_SHADER_FRAGMENT) {
320 shader->info->fs.uses_sample_qualifier = false;
321 }
322 nir_foreach_block(block, entrypoint) {
323 gather_info_block(block, shader);
324 }
325 }