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