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