2 * Copyright © 2015 Intel Corporation
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:
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
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
26 #include "util/hash_table.h"
28 /* This file contains various little helpers for doing simple linking in
29 * NIR. Eventually, we'll probably want a full-blown varying packing
30 * implementation in here. Right now, it just deletes unused things.
34 * Returns the bits in the inputs_read, outputs_written, or
35 * system_values_read bitfield corresponding to this variable.
38 get_variable_io_mask(nir_variable
*var
, gl_shader_stage stage
)
40 if (var
->data
.location
< 0)
43 unsigned location
= var
->data
.patch
?
44 var
->data
.location
- VARYING_SLOT_PATCH0
: var
->data
.location
;
46 assert(var
->data
.mode
== nir_var_shader_in
||
47 var
->data
.mode
== nir_var_shader_out
||
48 var
->data
.mode
== nir_var_system_value
);
49 assert(var
->data
.location
>= 0);
51 const struct glsl_type
*type
= var
->type
;
52 if (nir_is_per_vertex_io(var
, stage
)) {
53 assert(glsl_type_is_array(type
));
54 type
= glsl_get_array_element(type
);
57 unsigned slots
= glsl_count_attribute_slots(type
, false);
58 return ((1ull << slots
) - 1) << location
;
62 tcs_add_output_reads(nir_shader
*shader
, uint64_t *read
, uint64_t *patches_read
)
64 nir_foreach_function(function
, shader
) {
68 nir_foreach_block(block
, function
->impl
) {
69 nir_foreach_instr(instr
, block
) {
70 if (instr
->type
!= nir_instr_type_intrinsic
)
73 nir_intrinsic_instr
*intrin
= nir_instr_as_intrinsic(instr
);
75 if (intrin
->intrinsic
== nir_intrinsic_load_var
) {
76 var
= intrin
->variables
[0]->var
;
77 } else if (intrin
->intrinsic
== nir_intrinsic_load_deref
) {
78 var
= nir_deref_instr_get_variable(nir_src_as_deref(intrin
->src
[0]));
83 if (var
->data
.mode
!= nir_var_shader_out
)
86 if (var
->data
.patch
) {
87 patches_read
[var
->data
.location_frac
] |=
88 get_variable_io_mask(var
, shader
->info
.stage
);
90 read
[var
->data
.location_frac
] |=
91 get_variable_io_mask(var
, shader
->info
.stage
);
99 remove_unused_io_vars(nir_shader
*shader
, struct exec_list
*var_list
,
100 uint64_t *used_by_other_stage
,
101 uint64_t *used_by_other_stage_patches
)
103 bool progress
= false;
106 nir_foreach_variable_safe(var
, var_list
) {
108 used
= used_by_other_stage_patches
;
110 used
= used_by_other_stage
;
112 if (var
->data
.location
< VARYING_SLOT_VAR0
&& var
->data
.location
>= 0)
115 if (var
->data
.always_active_io
)
118 uint64_t other_stage
= used
[var
->data
.location_frac
];
120 if (!(other_stage
& get_variable_io_mask(var
, shader
->info
.stage
))) {
121 /* This one is invalid, make it a global variable instead */
122 var
->data
.location
= 0;
123 var
->data
.mode
= nir_var_global
;
125 exec_node_remove(&var
->node
);
126 exec_list_push_tail(&shader
->globals
, &var
->node
);
136 nir_remove_unused_varyings(nir_shader
*producer
, nir_shader
*consumer
)
138 assert(producer
->info
.stage
!= MESA_SHADER_FRAGMENT
);
139 assert(consumer
->info
.stage
!= MESA_SHADER_VERTEX
);
141 uint64_t read
[4] = { 0 }, written
[4] = { 0 };
142 uint64_t patches_read
[4] = { 0 }, patches_written
[4] = { 0 };
144 nir_foreach_variable(var
, &producer
->outputs
) {
145 if (var
->data
.patch
) {
146 patches_written
[var
->data
.location_frac
] |=
147 get_variable_io_mask(var
, producer
->info
.stage
);
149 written
[var
->data
.location_frac
] |=
150 get_variable_io_mask(var
, producer
->info
.stage
);
154 nir_foreach_variable(var
, &consumer
->inputs
) {
155 if (var
->data
.patch
) {
156 patches_read
[var
->data
.location_frac
] |=
157 get_variable_io_mask(var
, consumer
->info
.stage
);
159 read
[var
->data
.location_frac
] |=
160 get_variable_io_mask(var
, consumer
->info
.stage
);
164 /* Each TCS invocation can read data written by other TCS invocations,
165 * so even if the outputs are not used by the TES we must also make
166 * sure they are not read by the TCS before demoting them to globals.
168 if (producer
->info
.stage
== MESA_SHADER_TESS_CTRL
)
169 tcs_add_output_reads(producer
, read
, patches_read
);
171 bool progress
= false;
172 progress
= remove_unused_io_vars(producer
, &producer
->outputs
, read
,
175 progress
= remove_unused_io_vars(consumer
, &consumer
->inputs
, written
,
176 patches_written
) || progress
;
182 get_interp_type(nir_variable
*var
, bool default_to_smooth_interp
)
184 if (var
->data
.interpolation
!= INTERP_MODE_NONE
)
185 return var
->data
.interpolation
;
186 else if (default_to_smooth_interp
)
187 return INTERP_MODE_SMOOTH
;
189 return INTERP_MODE_NONE
;
192 #define INTERPOLATE_LOC_SAMPLE 0
193 #define INTERPOLATE_LOC_CENTROID 1
194 #define INTERPOLATE_LOC_CENTER 2
197 get_interp_loc(nir_variable
*var
)
199 if (var
->data
.sample
)
200 return INTERPOLATE_LOC_SAMPLE
;
201 else if (var
->data
.centroid
)
202 return INTERPOLATE_LOC_CENTROID
;
204 return INTERPOLATE_LOC_CENTER
;
208 get_slot_component_masks_and_interp_types(struct exec_list
*var_list
,
210 uint8_t *interp_type
,
212 gl_shader_stage stage
,
213 bool default_to_smooth_interp
)
215 nir_foreach_variable_safe(var
, var_list
) {
216 assert(var
->data
.location
>= 0);
218 /* Only remap things that aren't built-ins.
219 * TODO: add TES patch support.
221 if (var
->data
.location
>= VARYING_SLOT_VAR0
&&
222 var
->data
.location
- VARYING_SLOT_VAR0
< 32) {
224 const struct glsl_type
*type
= var
->type
;
225 if (nir_is_per_vertex_io(var
, stage
)) {
226 assert(glsl_type_is_array(type
));
227 type
= glsl_get_array_element(type
);
230 unsigned location
= var
->data
.location
- VARYING_SLOT_VAR0
;
232 glsl_get_vector_elements(glsl_without_array(type
));
234 bool dual_slot
= glsl_type_is_dual_slot(glsl_without_array(type
));
235 unsigned slots
= glsl_count_attribute_slots(type
, false);
236 unsigned comps_slot2
= 0;
237 for (unsigned i
= 0; i
< slots
; i
++) {
238 interp_type
[location
+ i
] =
239 get_interp_type(var
, default_to_smooth_interp
);
240 interp_loc
[location
+ i
] = get_interp_loc(var
);
244 comps
[location
+ i
] |= ((1 << comps_slot2
) - 1);
246 unsigned num_comps
= 4 - var
->data
.location_frac
;
247 comps_slot2
= (elements
* 2) - num_comps
;
249 /* Assume ARB_enhanced_layouts packing rules for doubles */
250 assert(var
->data
.location_frac
== 0 ||
251 var
->data
.location_frac
== 2);
252 assert(comps_slot2
<= 4);
254 comps
[location
+ i
] |=
255 ((1 << num_comps
) - 1) << var
->data
.location_frac
;
258 comps
[location
+ i
] |=
259 ((1 << elements
) - 1) << var
->data
.location_frac
;
273 remap_slots_and_components(struct exec_list
*var_list
, gl_shader_stage stage
,
274 struct varying_loc (*remap
)[4],
275 uint64_t *slots_used
, uint64_t *out_slots_read
)
277 uint64_t out_slots_read_tmp
= 0;
279 /* We don't touch builtins so just copy the bitmask */
280 uint64_t slots_used_tmp
=
281 *slots_used
& (((uint64_t)1 << (VARYING_SLOT_VAR0
- 1)) - 1);
283 nir_foreach_variable(var
, var_list
) {
284 assert(var
->data
.location
>= 0);
286 /* Only remap things that aren't built-ins */
287 if (var
->data
.location
>= VARYING_SLOT_VAR0
&&
288 var
->data
.location
- VARYING_SLOT_VAR0
< 32) {
289 assert(var
->data
.location
- VARYING_SLOT_VAR0
< 32);
291 const struct glsl_type
*type
= var
->type
;
292 if (nir_is_per_vertex_io(var
, stage
)) {
293 assert(glsl_type_is_array(type
));
294 type
= glsl_get_array_element(type
);
297 unsigned num_slots
= glsl_count_attribute_slots(type
, false);
298 bool used_across_stages
= false;
299 bool outputs_read
= false;
301 unsigned location
= var
->data
.location
- VARYING_SLOT_VAR0
;
302 struct varying_loc
*new_loc
= &remap
[location
][var
->data
.location_frac
];
304 uint64_t slots
= (((uint64_t)1 << num_slots
) - 1) << var
->data
.location
;
305 if (slots
& *slots_used
)
306 used_across_stages
= true;
308 if (slots
& *out_slots_read
)
311 if (new_loc
->location
) {
312 var
->data
.location
= new_loc
->location
;
313 var
->data
.location_frac
= new_loc
->component
;
316 if (var
->data
.always_active_io
) {
317 /* We can't apply link time optimisations (specifically array
318 * splitting) to these so we need to copy the existing mask
319 * otherwise we will mess up the mask for things like partially
322 if (used_across_stages
) {
324 *slots_used
& (((uint64_t)1 << num_slots
) - 1) << var
->data
.location
;
328 out_slots_read_tmp
|=
329 *out_slots_read
& (((uint64_t)1 << num_slots
) - 1) << var
->data
.location
;
333 for (unsigned i
= 0; i
< num_slots
; i
++) {
334 if (used_across_stages
)
335 slots_used_tmp
|= (uint64_t)1 << (var
->data
.location
+ i
);
338 out_slots_read_tmp
|= (uint64_t)1 << (var
->data
.location
+ i
);
344 *slots_used
= slots_used_tmp
;
345 *out_slots_read
= out_slots_read_tmp
;
348 /* If there are empty components in the slot compact the remaining components
349 * as close to component 0 as possible. This will make it easier to fill the
350 * empty components with components from a different slot in a following pass.
353 compact_components(nir_shader
*producer
, nir_shader
*consumer
, uint8_t *comps
,
354 uint8_t *interp_type
, uint8_t *interp_loc
,
355 bool default_to_smooth_interp
)
357 struct exec_list
*input_list
= &consumer
->inputs
;
358 struct exec_list
*output_list
= &producer
->outputs
;
359 struct varying_loc remap
[32][4] = {{{0}, {0}}};
361 /* Create a cursor for each interpolation type */
362 unsigned cursor
[4] = {0};
364 /* We only need to pass over one stage and we choose the consumer as it seems
365 * to cause a larger reduction in instruction counts (tested on i965).
367 nir_foreach_variable(var
, input_list
) {
369 /* Only remap things that aren't builtins.
370 * TODO: add TES patch support.
372 if (var
->data
.location
>= VARYING_SLOT_VAR0
&&
373 var
->data
.location
- VARYING_SLOT_VAR0
< 32) {
375 /* We can't repack xfb varyings. */
376 if (var
->data
.always_active_io
)
379 const struct glsl_type
*type
= var
->type
;
380 if (nir_is_per_vertex_io(var
, consumer
->info
.stage
)) {
381 assert(glsl_type_is_array(type
));
382 type
= glsl_get_array_element(type
);
385 /* Skip types that require more complex packing handling.
386 * TODO: add support for these types.
388 if (glsl_type_is_array(type
) ||
389 glsl_type_is_dual_slot(type
) ||
390 glsl_type_is_matrix(type
) ||
391 glsl_type_is_struct(type
) ||
392 glsl_type_is_64bit(type
))
395 /* We ignore complex types above and all other vector types should
396 * have been split into scalar variables by the lower_io_to_scalar
397 * pass. The only exeption should by OpenGL xfb varyings.
399 if (glsl_get_vector_elements(type
) != 1)
402 unsigned location
= var
->data
.location
- VARYING_SLOT_VAR0
;
403 uint8_t used_comps
= comps
[location
];
405 /* If there are no empty components there is nothing more for us to do.
407 if (used_comps
== 0xf)
410 bool found_new_offset
= false;
411 uint8_t interp
= get_interp_type(var
, default_to_smooth_interp
);
412 for (; cursor
[interp
] < 32; cursor
[interp
]++) {
413 uint8_t cursor_used_comps
= comps
[cursor
[interp
]];
415 /* We couldn't find anywhere to pack the varying continue on. */
416 if (cursor
[interp
] == location
&&
417 (var
->data
.location_frac
== 0 ||
418 cursor_used_comps
& ((1 << (var
->data
.location_frac
)) - 1)))
421 /* We can only pack varyings with matching interpolation types */
422 if (interp_type
[cursor
[interp
]] != interp
)
425 /* Interpolation loc must match also.
426 * TODO: i965 can handle these if they don't match, but the
427 * radeonsi nir backend handles everything as vec4s and so expects
428 * this to be the same for all components. We could make this
429 * check driver specfific or drop it if NIR ever become the only
432 if (interp_loc
[cursor
[interp
]] != get_interp_loc(var
))
435 /* If the slot is empty just skip it for now, compact_var_list()
436 * can be called after this function to remove empty slots for us.
437 * TODO: finish implementing compact_var_list() requires array and
440 if (!cursor_used_comps
)
443 uint8_t unused_comps
= ~cursor_used_comps
;
445 for (unsigned i
= 0; i
< 4; i
++) {
446 uint8_t new_var_comps
= 1 << i
;
447 if (unused_comps
& new_var_comps
) {
448 remap
[location
][var
->data
.location_frac
].component
= i
;
449 remap
[location
][var
->data
.location_frac
].location
=
450 cursor
[interp
] + VARYING_SLOT_VAR0
;
452 found_new_offset
= true;
454 /* Turn off the mask for the component we are remapping */
455 if (comps
[location
] & 1 << var
->data
.location_frac
) {
456 comps
[location
] ^= 1 << var
->data
.location_frac
;
457 comps
[cursor
[interp
]] |= new_var_comps
;
463 if (found_new_offset
)
470 remap_slots_and_components(input_list
, consumer
->info
.stage
, remap
,
471 &consumer
->info
.inputs_read
, &zero
);
472 remap_slots_and_components(output_list
, producer
->info
.stage
, remap
,
473 &producer
->info
.outputs_written
,
474 &producer
->info
.outputs_read
);
477 /* We assume that this has been called more-or-less directly after
478 * remove_unused_varyings. At this point, all of the varyings that we
479 * aren't going to be using have been completely removed and the
480 * inputs_read and outputs_written fields in nir_shader_info reflect
481 * this. Therefore, the total set of valid slots is the OR of the two
482 * sets of varyings; this accounts for varyings which one side may need
483 * to read/write even if the other doesn't. This can happen if, for
484 * instance, an array is used indirectly from one side causing it to be
485 * unsplittable but directly from the other.
488 nir_compact_varyings(nir_shader
*producer
, nir_shader
*consumer
,
489 bool default_to_smooth_interp
)
491 assert(producer
->info
.stage
!= MESA_SHADER_FRAGMENT
);
492 assert(consumer
->info
.stage
!= MESA_SHADER_VERTEX
);
494 uint8_t comps
[32] = {0};
495 uint8_t interp_type
[32] = {0};
496 uint8_t interp_loc
[32] = {0};
498 get_slot_component_masks_and_interp_types(&producer
->outputs
, comps
,
499 interp_type
, interp_loc
,
500 producer
->info
.stage
,
501 default_to_smooth_interp
);
502 get_slot_component_masks_and_interp_types(&consumer
->inputs
, comps
,
503 interp_type
, interp_loc
,
504 consumer
->info
.stage
,
505 default_to_smooth_interp
);
507 compact_components(producer
, consumer
, comps
, interp_type
, interp_loc
,
508 default_to_smooth_interp
);