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
25 #include "nir_builder.h"
27 #include "util/hash_table.h"
29 /* This file contains various little helpers for doing simple linking in
30 * NIR. Eventually, we'll probably want a full-blown varying packing
31 * implementation in here. Right now, it just deletes unused things.
35 * Returns the bits in the inputs_read, outputs_written, or
36 * system_values_read bitfield corresponding to this variable.
39 get_variable_io_mask(nir_variable
*var
, gl_shader_stage stage
)
41 if (var
->data
.location
< 0)
44 unsigned location
= var
->data
.patch
?
45 var
->data
.location
- VARYING_SLOT_PATCH0
: var
->data
.location
;
47 assert(var
->data
.mode
== nir_var_shader_in
||
48 var
->data
.mode
== nir_var_shader_out
||
49 var
->data
.mode
== nir_var_system_value
);
50 assert(var
->data
.location
>= 0);
52 const struct glsl_type
*type
= var
->type
;
53 if (nir_is_per_vertex_io(var
, stage
) || var
->data
.per_view
) {
54 assert(glsl_type_is_array(type
));
55 type
= glsl_get_array_element(type
);
58 unsigned slots
= glsl_count_attribute_slots(type
, false);
59 return ((1ull << slots
) - 1) << location
;
63 get_num_components(nir_variable
*var
)
65 if (glsl_type_is_struct_or_ifc(glsl_without_array(var
->type
)))
68 return glsl_get_vector_elements(glsl_without_array(var
->type
));
72 tcs_add_output_reads(nir_shader
*shader
, uint64_t *read
, uint64_t *patches_read
)
74 nir_foreach_function(function
, shader
) {
78 nir_foreach_block(block
, function
->impl
) {
79 nir_foreach_instr(instr
, block
) {
80 if (instr
->type
!= nir_instr_type_intrinsic
)
83 nir_intrinsic_instr
*intrin
= nir_instr_as_intrinsic(instr
);
84 if (intrin
->intrinsic
!= nir_intrinsic_load_deref
)
87 nir_deref_instr
*deref
= nir_src_as_deref(intrin
->src
[0]);
88 if (deref
->mode
!= nir_var_shader_out
)
91 nir_variable
*var
= nir_deref_instr_get_variable(deref
);
92 for (unsigned i
= 0; i
< get_num_components(var
); i
++) {
93 if (var
->data
.patch
) {
94 patches_read
[var
->data
.location_frac
+ i
] |=
95 get_variable_io_mask(var
, shader
->info
.stage
);
97 read
[var
->data
.location_frac
+ i
] |=
98 get_variable_io_mask(var
, shader
->info
.stage
);
107 * Helper for removing unused shader I/O variables, by demoting them to global
108 * variables (which may then by dead code eliminated).
112 * progress = nir_remove_unused_io_vars(producer,
113 * &producer->outputs,
114 * read, patches_read) ||
117 * The "used" should be an array of 4 uint64_ts (probably of VARYING_BIT_*)
118 * representing each .location_frac used. Note that for vector variables,
119 * only the first channel (.location_frac) is examined for deciding if the
123 nir_remove_unused_io_vars(nir_shader
*shader
, struct exec_list
*var_list
,
124 uint64_t *used_by_other_stage
,
125 uint64_t *used_by_other_stage_patches
)
127 bool progress
= false;
130 nir_foreach_variable_safe(var
, var_list
) {
132 used
= used_by_other_stage_patches
;
134 used
= used_by_other_stage
;
136 if (var
->data
.location
< VARYING_SLOT_VAR0
&& var
->data
.location
>= 0)
139 if (var
->data
.always_active_io
)
142 if (var
->data
.explicit_xfb_buffer
)
145 uint64_t other_stage
= used
[var
->data
.location_frac
];
147 if (!(other_stage
& get_variable_io_mask(var
, shader
->info
.stage
))) {
148 /* This one is invalid, make it a global variable instead */
149 var
->data
.location
= 0;
150 var
->data
.mode
= nir_var_shader_temp
;
152 exec_node_remove(&var
->node
);
153 exec_list_push_tail(&shader
->globals
, &var
->node
);
160 nir_fixup_deref_modes(shader
);
166 nir_remove_unused_varyings(nir_shader
*producer
, nir_shader
*consumer
)
168 assert(producer
->info
.stage
!= MESA_SHADER_FRAGMENT
);
169 assert(consumer
->info
.stage
!= MESA_SHADER_VERTEX
);
171 uint64_t read
[4] = { 0 }, written
[4] = { 0 };
172 uint64_t patches_read
[4] = { 0 }, patches_written
[4] = { 0 };
174 nir_foreach_variable(var
, &producer
->outputs
) {
175 for (unsigned i
= 0; i
< get_num_components(var
); i
++) {
176 if (var
->data
.patch
) {
177 patches_written
[var
->data
.location_frac
+ i
] |=
178 get_variable_io_mask(var
, producer
->info
.stage
);
180 written
[var
->data
.location_frac
+ i
] |=
181 get_variable_io_mask(var
, producer
->info
.stage
);
186 nir_foreach_variable(var
, &consumer
->inputs
) {
187 for (unsigned i
= 0; i
< get_num_components(var
); i
++) {
188 if (var
->data
.patch
) {
189 patches_read
[var
->data
.location_frac
+ i
] |=
190 get_variable_io_mask(var
, consumer
->info
.stage
);
192 read
[var
->data
.location_frac
+ i
] |=
193 get_variable_io_mask(var
, consumer
->info
.stage
);
198 /* Each TCS invocation can read data written by other TCS invocations,
199 * so even if the outputs are not used by the TES we must also make
200 * sure they are not read by the TCS before demoting them to globals.
202 if (producer
->info
.stage
== MESA_SHADER_TESS_CTRL
)
203 tcs_add_output_reads(producer
, read
, patches_read
);
205 bool progress
= false;
206 progress
= nir_remove_unused_io_vars(producer
, &producer
->outputs
, read
,
209 progress
= nir_remove_unused_io_vars(consumer
, &consumer
->inputs
, written
,
210 patches_written
) || progress
;
216 get_interp_type(nir_variable
*var
, const struct glsl_type
*type
,
217 bool default_to_smooth_interp
)
219 if (glsl_type_is_integer(type
))
220 return INTERP_MODE_FLAT
;
221 else if (var
->data
.interpolation
!= INTERP_MODE_NONE
)
222 return var
->data
.interpolation
;
223 else if (default_to_smooth_interp
)
224 return INTERP_MODE_SMOOTH
;
226 return INTERP_MODE_NONE
;
229 #define INTERPOLATE_LOC_SAMPLE 0
230 #define INTERPOLATE_LOC_CENTROID 1
231 #define INTERPOLATE_LOC_CENTER 2
234 get_interp_loc(nir_variable
*var
)
236 if (var
->data
.sample
)
237 return INTERPOLATE_LOC_SAMPLE
;
238 else if (var
->data
.centroid
)
239 return INTERPOLATE_LOC_CENTROID
;
241 return INTERPOLATE_LOC_CENTER
;
245 is_packing_supported_for_type(const struct glsl_type
*type
)
247 /* We ignore complex types such as arrays, matrices, structs and bitsizes
248 * other then 32bit. All other vector types should have been split into
249 * scalar variables by the lower_io_to_scalar pass. The only exception
250 * should be OpenGL xfb varyings.
251 * TODO: add support for more complex types?
253 return glsl_type_is_scalar(type
) && glsl_type_is_32bit(type
);
256 struct assigned_comps
264 /* Packing arrays and dual slot varyings is difficult so to avoid complex
265 * algorithms this function just assigns them their existing location for now.
266 * TODO: allow better packing of complex types.
269 get_unmoveable_components_masks(struct exec_list
*var_list
,
270 struct assigned_comps
*comps
,
271 gl_shader_stage stage
,
272 bool default_to_smooth_interp
)
274 nir_foreach_variable_safe(var
, var_list
) {
275 assert(var
->data
.location
>= 0);
277 /* Only remap things that aren't built-ins. */
278 if (var
->data
.location
>= VARYING_SLOT_VAR0
&&
279 var
->data
.location
- VARYING_SLOT_VAR0
< MAX_VARYINGS_INCL_PATCH
) {
281 const struct glsl_type
*type
= var
->type
;
282 if (nir_is_per_vertex_io(var
, stage
) || var
->data
.per_view
) {
283 assert(glsl_type_is_array(type
));
284 type
= glsl_get_array_element(type
);
287 /* If we can pack this varying then don't mark the components as
290 if (is_packing_supported_for_type(type
))
293 unsigned location
= var
->data
.location
- VARYING_SLOT_VAR0
;
296 glsl_type_is_vector_or_scalar(glsl_without_array(type
)) ?
297 glsl_get_vector_elements(glsl_without_array(type
)) : 4;
299 bool dual_slot
= glsl_type_is_dual_slot(glsl_without_array(type
));
300 unsigned slots
= glsl_count_attribute_slots(type
, false);
301 unsigned dmul
= glsl_type_is_64bit(glsl_without_array(type
)) ? 2 : 1;
302 unsigned comps_slot2
= 0;
303 for (unsigned i
= 0; i
< slots
; i
++) {
306 comps
[location
+ i
].comps
|= ((1 << comps_slot2
) - 1);
308 unsigned num_comps
= 4 - var
->data
.location_frac
;
309 comps_slot2
= (elements
* dmul
) - num_comps
;
311 /* Assume ARB_enhanced_layouts packing rules for doubles */
312 assert(var
->data
.location_frac
== 0 ||
313 var
->data
.location_frac
== 2);
314 assert(comps_slot2
<= 4);
316 comps
[location
+ i
].comps
|=
317 ((1 << num_comps
) - 1) << var
->data
.location_frac
;
320 comps
[location
+ i
].comps
|=
321 ((1 << (elements
* dmul
)) - 1) << var
->data
.location_frac
;
324 comps
[location
+ i
].interp_type
=
325 get_interp_type(var
, type
, default_to_smooth_interp
);
326 comps
[location
+ i
].interp_loc
= get_interp_loc(var
);
327 comps
[location
+ i
].is_32bit
=
328 glsl_type_is_32bit(glsl_without_array(type
));
341 mark_all_used_slots(nir_variable
*var
, uint64_t *slots_used
,
342 uint64_t slots_used_mask
, unsigned num_slots
)
344 unsigned loc_offset
= var
->data
.patch
? VARYING_SLOT_PATCH0
: 0;
346 slots_used
[var
->data
.patch
? 1 : 0] |= slots_used_mask
&
347 BITFIELD64_RANGE(var
->data
.location
- loc_offset
, num_slots
);
351 mark_used_slot(nir_variable
*var
, uint64_t *slots_used
, unsigned offset
)
353 unsigned loc_offset
= var
->data
.patch
? VARYING_SLOT_PATCH0
: 0;
355 slots_used
[var
->data
.patch
? 1 : 0] |=
356 BITFIELD64_BIT(var
->data
.location
- loc_offset
+ offset
);
360 remap_slots_and_components(struct exec_list
*var_list
, gl_shader_stage stage
,
361 struct varying_loc (*remap
)[4],
362 uint64_t *slots_used
, uint64_t *out_slots_read
,
363 uint32_t *p_slots_used
, uint32_t *p_out_slots_read
)
365 uint64_t out_slots_read_tmp
[2] = {0};
366 uint64_t slots_used_tmp
[2] = {0};
368 /* We don't touch builtins so just copy the bitmask */
369 slots_used_tmp
[0] = *slots_used
& BITFIELD64_RANGE(0, VARYING_SLOT_VAR0
);
371 nir_foreach_variable(var
, var_list
) {
372 assert(var
->data
.location
>= 0);
374 /* Only remap things that aren't built-ins */
375 if (var
->data
.location
>= VARYING_SLOT_VAR0
&&
376 var
->data
.location
- VARYING_SLOT_VAR0
< MAX_VARYINGS_INCL_PATCH
) {
378 const struct glsl_type
*type
= var
->type
;
379 if (nir_is_per_vertex_io(var
, stage
) || var
->data
.per_view
) {
380 assert(glsl_type_is_array(type
));
381 type
= glsl_get_array_element(type
);
384 unsigned num_slots
= glsl_count_attribute_slots(type
, false);
385 bool used_across_stages
= false;
386 bool outputs_read
= false;
388 unsigned location
= var
->data
.location
- VARYING_SLOT_VAR0
;
389 struct varying_loc
*new_loc
= &remap
[location
][var
->data
.location_frac
];
391 unsigned loc_offset
= var
->data
.patch
? VARYING_SLOT_PATCH0
: 0;
392 uint64_t used
= var
->data
.patch
? *p_slots_used
: *slots_used
;
394 var
->data
.patch
? *p_out_slots_read
: *out_slots_read
;
396 BITFIELD64_RANGE(var
->data
.location
- loc_offset
, num_slots
);
399 used_across_stages
= true;
401 if (slots
& outs_used
)
404 if (new_loc
->location
) {
405 var
->data
.location
= new_loc
->location
;
406 var
->data
.location_frac
= new_loc
->component
;
409 if (var
->data
.always_active_io
) {
410 /* We can't apply link time optimisations (specifically array
411 * splitting) to these so we need to copy the existing mask
412 * otherwise we will mess up the mask for things like partially
415 if (used_across_stages
)
416 mark_all_used_slots(var
, slots_used_tmp
, used
, num_slots
);
419 mark_all_used_slots(var
, out_slots_read_tmp
, outs_used
,
423 for (unsigned i
= 0; i
< num_slots
; i
++) {
424 if (used_across_stages
)
425 mark_used_slot(var
, slots_used_tmp
, i
);
428 mark_used_slot(var
, out_slots_read_tmp
, i
);
434 *slots_used
= slots_used_tmp
[0];
435 *out_slots_read
= out_slots_read_tmp
[0];
436 *p_slots_used
= slots_used_tmp
[1];
437 *p_out_slots_read
= out_slots_read_tmp
[1];
440 struct varying_component
{
446 bool is_intra_stage_only
;
451 cmp_varying_component(const void *comp1_v
, const void *comp2_v
)
453 struct varying_component
*comp1
= (struct varying_component
*) comp1_v
;
454 struct varying_component
*comp2
= (struct varying_component
*) comp2_v
;
456 /* We want patches to be order at the end of the array */
457 if (comp1
->is_patch
!= comp2
->is_patch
)
458 return comp1
->is_patch
? 1 : -1;
460 /* We want to try to group together TCS outputs that are only read by other
461 * TCS invocations and not consumed by the follow stage.
463 if (comp1
->is_intra_stage_only
!= comp2
->is_intra_stage_only
)
464 return comp1
->is_intra_stage_only
? 1 : -1;
466 /* We can only pack varyings with matching interpolation types so group
469 if (comp1
->interp_type
!= comp2
->interp_type
)
470 return comp1
->interp_type
- comp2
->interp_type
;
472 /* Interpolation loc must match also. */
473 if (comp1
->interp_loc
!= comp2
->interp_loc
)
474 return comp1
->interp_loc
- comp2
->interp_loc
;
476 /* If everything else matches just use the original location to sort */
477 return comp1
->var
->data
.location
- comp2
->var
->data
.location
;
481 gather_varying_component_info(nir_shader
*producer
, nir_shader
*consumer
,
482 struct varying_component
**varying_comp_info
,
483 unsigned *varying_comp_info_size
,
484 bool default_to_smooth_interp
)
486 unsigned store_varying_info_idx
[MAX_VARYINGS_INCL_PATCH
][4] = {{0}};
487 unsigned num_of_comps_to_pack
= 0;
489 /* Count the number of varying that can be packed and create a mapping
490 * of those varyings to the array we will pass to qsort.
492 nir_foreach_variable(var
, &producer
->outputs
) {
494 /* Only remap things that aren't builtins. */
495 if (var
->data
.location
>= VARYING_SLOT_VAR0
&&
496 var
->data
.location
- VARYING_SLOT_VAR0
< MAX_VARYINGS_INCL_PATCH
) {
498 /* We can't repack xfb varyings. */
499 if (var
->data
.always_active_io
)
502 const struct glsl_type
*type
= var
->type
;
503 if (nir_is_per_vertex_io(var
, producer
->info
.stage
) || var
->data
.per_view
) {
504 assert(glsl_type_is_array(type
));
505 type
= glsl_get_array_element(type
);
508 if (!is_packing_supported_for_type(type
))
511 unsigned loc
= var
->data
.location
- VARYING_SLOT_VAR0
;
512 store_varying_info_idx
[loc
][var
->data
.location_frac
] =
513 ++num_of_comps_to_pack
;
517 *varying_comp_info_size
= num_of_comps_to_pack
;
518 *varying_comp_info
= rzalloc_array(NULL
, struct varying_component
,
519 num_of_comps_to_pack
);
521 nir_function_impl
*impl
= nir_shader_get_entrypoint(consumer
);
523 /* Walk over the shader and populate the varying component info array */
524 nir_foreach_block(block
, impl
) {
525 nir_foreach_instr(instr
, block
) {
526 if (instr
->type
!= nir_instr_type_intrinsic
)
529 nir_intrinsic_instr
*intr
= nir_instr_as_intrinsic(instr
);
530 if (intr
->intrinsic
!= nir_intrinsic_load_deref
&&
531 intr
->intrinsic
!= nir_intrinsic_interp_deref_at_centroid
&&
532 intr
->intrinsic
!= nir_intrinsic_interp_deref_at_sample
&&
533 intr
->intrinsic
!= nir_intrinsic_interp_deref_at_offset
&&
534 intr
->intrinsic
!= nir_intrinsic_interp_deref_at_vertex
)
537 nir_deref_instr
*deref
= nir_src_as_deref(intr
->src
[0]);
538 if (deref
->mode
!= nir_var_shader_in
)
541 /* We only remap things that aren't builtins. */
542 nir_variable
*in_var
= nir_deref_instr_get_variable(deref
);
543 if (in_var
->data
.location
< VARYING_SLOT_VAR0
)
546 unsigned location
= in_var
->data
.location
- VARYING_SLOT_VAR0
;
547 if (location
>= MAX_VARYINGS_INCL_PATCH
)
550 unsigned var_info_idx
=
551 store_varying_info_idx
[location
][in_var
->data
.location_frac
];
555 struct varying_component
*vc_info
=
556 &(*varying_comp_info
)[var_info_idx
-1];
558 if (!vc_info
->initialised
) {
559 const struct glsl_type
*type
= in_var
->type
;
560 if (nir_is_per_vertex_io(in_var
, consumer
->info
.stage
) ||
561 in_var
->data
.per_view
) {
562 assert(glsl_type_is_array(type
));
563 type
= glsl_get_array_element(type
);
566 vc_info
->var
= in_var
;
567 vc_info
->interp_type
=
568 get_interp_type(in_var
, type
, default_to_smooth_interp
);
569 vc_info
->interp_loc
= get_interp_loc(in_var
);
570 vc_info
->is_32bit
= glsl_type_is_32bit(type
);
571 vc_info
->is_patch
= in_var
->data
.patch
;
572 vc_info
->is_intra_stage_only
= false;
573 vc_info
->initialised
= true;
578 /* Walk over the shader and populate the varying component info array
579 * for varyings which are read by other TCS instances but are not consumed
582 if (producer
->info
.stage
== MESA_SHADER_TESS_CTRL
) {
583 impl
= nir_shader_get_entrypoint(producer
);
585 nir_foreach_block(block
, impl
) {
586 nir_foreach_instr(instr
, block
) {
587 if (instr
->type
!= nir_instr_type_intrinsic
)
590 nir_intrinsic_instr
*intr
= nir_instr_as_intrinsic(instr
);
591 if (intr
->intrinsic
!= nir_intrinsic_load_deref
)
594 nir_deref_instr
*deref
= nir_src_as_deref(intr
->src
[0]);
595 if (deref
->mode
!= nir_var_shader_out
)
598 /* We only remap things that aren't builtins. */
599 nir_variable
*out_var
= nir_deref_instr_get_variable(deref
);
600 if (out_var
->data
.location
< VARYING_SLOT_VAR0
)
603 unsigned location
= out_var
->data
.location
- VARYING_SLOT_VAR0
;
604 if (location
>= MAX_VARYINGS_INCL_PATCH
)
607 unsigned var_info_idx
=
608 store_varying_info_idx
[location
][out_var
->data
.location_frac
];
610 /* Something went wrong, the shader interfaces didn't match, so
611 * abandon packing. This can happen for example when the
612 * inputs are scalars but the outputs are struct members.
614 *varying_comp_info_size
= 0;
618 struct varying_component
*vc_info
=
619 &(*varying_comp_info
)[var_info_idx
-1];
621 if (!vc_info
->initialised
) {
622 const struct glsl_type
*type
= out_var
->type
;
623 if (nir_is_per_vertex_io(out_var
, producer
->info
.stage
)) {
624 assert(glsl_type_is_array(type
));
625 type
= glsl_get_array_element(type
);
628 vc_info
->var
= out_var
;
629 vc_info
->interp_type
=
630 get_interp_type(out_var
, type
, default_to_smooth_interp
);
631 vc_info
->interp_loc
= get_interp_loc(out_var
);
632 vc_info
->is_32bit
= glsl_type_is_32bit(type
);
633 vc_info
->is_patch
= out_var
->data
.patch
;
634 vc_info
->is_intra_stage_only
= true;
635 vc_info
->initialised
= true;
641 for (unsigned i
= 0; i
< *varying_comp_info_size
; i
++ ) {
642 struct varying_component
*vc_info
= &(*varying_comp_info
)[i
];
643 if (!vc_info
->initialised
) {
644 /* Something went wrong, the shader interfaces didn't match, so
645 * abandon packing. This can happen for example when the outputs are
646 * scalars but the inputs are struct members.
648 *varying_comp_info_size
= 0;
655 assign_remap_locations(struct varying_loc (*remap
)[4],
656 struct assigned_comps
*assigned_comps
,
657 struct varying_component
*info
,
658 unsigned *cursor
, unsigned *comp
,
659 unsigned max_location
)
661 unsigned tmp_cursor
= *cursor
;
662 unsigned tmp_comp
= *comp
;
664 for (; tmp_cursor
< max_location
; tmp_cursor
++) {
666 if (assigned_comps
[tmp_cursor
].comps
) {
667 /* We can only pack varyings with matching interpolation types,
668 * interpolation loc must match also.
669 * TODO: i965 can handle interpolation locations that don't match,
670 * but the radeonsi nir backend handles everything as vec4s and so
671 * expects this to be the same for all components. We could make this
672 * check driver specfific or drop it if NIR ever become the only
675 if (assigned_comps
[tmp_cursor
].interp_type
!= info
->interp_type
||
676 assigned_comps
[tmp_cursor
].interp_loc
!= info
->interp_loc
) {
681 /* We can only pack varyings with matching types, and the current
682 * algorithm only supports packing 32-bit.
684 if (!assigned_comps
[tmp_cursor
].is_32bit
) {
689 while (tmp_comp
< 4 &&
690 (assigned_comps
[tmp_cursor
].comps
& (1 << tmp_comp
))) {
700 unsigned location
= info
->var
->data
.location
- VARYING_SLOT_VAR0
;
702 /* Once we have assigned a location mark it as used */
703 assigned_comps
[tmp_cursor
].comps
|= (1 << tmp_comp
);
704 assigned_comps
[tmp_cursor
].interp_type
= info
->interp_type
;
705 assigned_comps
[tmp_cursor
].interp_loc
= info
->interp_loc
;
706 assigned_comps
[tmp_cursor
].is_32bit
= info
->is_32bit
;
708 /* Assign remap location */
709 remap
[location
][info
->var
->data
.location_frac
].component
= tmp_comp
++;
710 remap
[location
][info
->var
->data
.location_frac
].location
=
711 tmp_cursor
+ VARYING_SLOT_VAR0
;
716 *cursor
= tmp_cursor
;
720 /* If there are empty components in the slot compact the remaining components
721 * as close to component 0 as possible. This will make it easier to fill the
722 * empty components with components from a different slot in a following pass.
725 compact_components(nir_shader
*producer
, nir_shader
*consumer
,
726 struct assigned_comps
*assigned_comps
,
727 bool default_to_smooth_interp
)
729 struct exec_list
*input_list
= &consumer
->inputs
;
730 struct exec_list
*output_list
= &producer
->outputs
;
731 struct varying_loc remap
[MAX_VARYINGS_INCL_PATCH
][4] = {{{0}, {0}}};
732 struct varying_component
*varying_comp_info
;
733 unsigned varying_comp_info_size
;
735 /* Gather varying component info */
736 gather_varying_component_info(producer
, consumer
, &varying_comp_info
,
737 &varying_comp_info_size
,
738 default_to_smooth_interp
);
740 /* Sort varying components. */
741 qsort(varying_comp_info
, varying_comp_info_size
,
742 sizeof(struct varying_component
), cmp_varying_component
);
747 /* Set the remap array based on the sorted components */
748 for (unsigned i
= 0; i
< varying_comp_info_size
; i
++ ) {
749 struct varying_component
*info
= &varying_comp_info
[i
];
751 assert(info
->is_patch
|| cursor
< MAX_VARYING
);
752 if (info
->is_patch
) {
753 /* The list should be sorted with all non-patch inputs first followed
754 * by patch inputs. When we hit our first patch input, we need to
755 * reset the cursor to MAX_VARYING so we put them in the right slot.
757 if (cursor
< MAX_VARYING
) {
758 cursor
= MAX_VARYING
;
762 assign_remap_locations(remap
, assigned_comps
, info
,
763 &cursor
, &comp
, MAX_VARYINGS_INCL_PATCH
);
765 assign_remap_locations(remap
, assigned_comps
, info
,
766 &cursor
, &comp
, MAX_VARYING
);
768 /* Check if we failed to assign a remap location. This can happen if
769 * for example there are a bunch of unmovable components with
770 * mismatching interpolation types causing us to skip over locations
771 * that would have been useful for packing later components.
772 * The solution is to iterate over the locations again (this should
773 * happen very rarely in practice).
775 if (cursor
== MAX_VARYING
) {
778 assign_remap_locations(remap
, assigned_comps
, info
,
779 &cursor
, &comp
, MAX_VARYING
);
784 ralloc_free(varying_comp_info
);
788 remap_slots_and_components(input_list
, consumer
->info
.stage
, remap
,
789 &consumer
->info
.inputs_read
, &zero
,
790 &consumer
->info
.patch_inputs_read
, &zero32
);
791 remap_slots_and_components(output_list
, producer
->info
.stage
, remap
,
792 &producer
->info
.outputs_written
,
793 &producer
->info
.outputs_read
,
794 &producer
->info
.patch_outputs_written
,
795 &producer
->info
.patch_outputs_read
);
798 /* We assume that this has been called more-or-less directly after
799 * remove_unused_varyings. At this point, all of the varyings that we
800 * aren't going to be using have been completely removed and the
801 * inputs_read and outputs_written fields in nir_shader_info reflect
802 * this. Therefore, the total set of valid slots is the OR of the two
803 * sets of varyings; this accounts for varyings which one side may need
804 * to read/write even if the other doesn't. This can happen if, for
805 * instance, an array is used indirectly from one side causing it to be
806 * unsplittable but directly from the other.
809 nir_compact_varyings(nir_shader
*producer
, nir_shader
*consumer
,
810 bool default_to_smooth_interp
)
812 assert(producer
->info
.stage
!= MESA_SHADER_FRAGMENT
);
813 assert(consumer
->info
.stage
!= MESA_SHADER_VERTEX
);
815 struct assigned_comps assigned_comps
[MAX_VARYINGS_INCL_PATCH
] = {{0}};
817 get_unmoveable_components_masks(&producer
->outputs
, assigned_comps
,
818 producer
->info
.stage
,
819 default_to_smooth_interp
);
820 get_unmoveable_components_masks(&consumer
->inputs
, assigned_comps
,
821 consumer
->info
.stage
,
822 default_to_smooth_interp
);
824 compact_components(producer
, consumer
, assigned_comps
,
825 default_to_smooth_interp
);
829 * Mark XFB varyings as always_active_io in the consumer so the linking opts
833 nir_link_xfb_varyings(nir_shader
*producer
, nir_shader
*consumer
)
835 nir_variable
*input_vars
[MAX_VARYING
] = { 0 };
837 nir_foreach_variable(var
, &consumer
->inputs
) {
838 if (var
->data
.location
>= VARYING_SLOT_VAR0
&&
839 var
->data
.location
- VARYING_SLOT_VAR0
< MAX_VARYING
) {
841 unsigned location
= var
->data
.location
- VARYING_SLOT_VAR0
;
842 input_vars
[location
] = var
;
846 nir_foreach_variable(var
, &producer
->outputs
) {
847 if (var
->data
.location
>= VARYING_SLOT_VAR0
&&
848 var
->data
.location
- VARYING_SLOT_VAR0
< MAX_VARYING
) {
850 if (!var
->data
.always_active_io
)
853 unsigned location
= var
->data
.location
- VARYING_SLOT_VAR0
;
854 if (input_vars
[location
]) {
855 input_vars
[location
]->data
.always_active_io
= true;
862 does_varying_match(nir_variable
*out_var
, nir_variable
*in_var
)
864 return in_var
->data
.location
== out_var
->data
.location
&&
865 in_var
->data
.location_frac
== out_var
->data
.location_frac
;
868 static nir_variable
*
869 get_matching_input_var(nir_shader
*consumer
, nir_variable
*out_var
)
871 nir_foreach_variable(var
, &consumer
->inputs
) {
872 if (does_varying_match(out_var
, var
))
880 can_replace_varying(nir_variable
*out_var
)
882 /* Skip types that require more complex handling.
883 * TODO: add support for these types.
885 if (glsl_type_is_array(out_var
->type
) ||
886 glsl_type_is_dual_slot(out_var
->type
) ||
887 glsl_type_is_matrix(out_var
->type
) ||
888 glsl_type_is_struct_or_ifc(out_var
->type
))
891 /* Limit this pass to scalars for now to keep things simple. Most varyings
892 * should have been lowered to scalars at this point anyway.
894 if (!glsl_type_is_scalar(out_var
->type
))
897 if (out_var
->data
.location
< VARYING_SLOT_VAR0
||
898 out_var
->data
.location
- VARYING_SLOT_VAR0
>= MAX_VARYING
)
905 replace_constant_input(nir_shader
*shader
, nir_intrinsic_instr
*store_intr
)
907 nir_function_impl
*impl
= nir_shader_get_entrypoint(shader
);
910 nir_builder_init(&b
, impl
);
912 nir_variable
*out_var
=
913 nir_deref_instr_get_variable(nir_src_as_deref(store_intr
->src
[0]));
915 bool progress
= false;
916 nir_foreach_block(block
, impl
) {
917 nir_foreach_instr(instr
, block
) {
918 if (instr
->type
!= nir_instr_type_intrinsic
)
921 nir_intrinsic_instr
*intr
= nir_instr_as_intrinsic(instr
);
922 if (intr
->intrinsic
!= nir_intrinsic_load_deref
)
925 nir_deref_instr
*in_deref
= nir_src_as_deref(intr
->src
[0]);
926 if (in_deref
->mode
!= nir_var_shader_in
)
929 nir_variable
*in_var
= nir_deref_instr_get_variable(in_deref
);
931 if (!does_varying_match(out_var
, in_var
))
934 b
.cursor
= nir_before_instr(instr
);
936 nir_load_const_instr
*out_const
=
937 nir_instr_as_load_const(store_intr
->src
[1].ssa
->parent_instr
);
939 /* Add new const to replace the input */
940 nir_ssa_def
*nconst
= nir_build_imm(&b
, store_intr
->num_components
,
941 intr
->dest
.ssa
.bit_size
,
944 nir_ssa_def_rewrite_uses(&intr
->dest
.ssa
, nir_src_for_ssa(nconst
));
954 replace_duplicate_input(nir_shader
*shader
, nir_variable
*input_var
,
955 nir_intrinsic_instr
*dup_store_intr
)
959 nir_function_impl
*impl
= nir_shader_get_entrypoint(shader
);
962 nir_builder_init(&b
, impl
);
964 nir_variable
*dup_out_var
=
965 nir_deref_instr_get_variable(nir_src_as_deref(dup_store_intr
->src
[0]));
967 bool progress
= false;
968 nir_foreach_block(block
, impl
) {
969 nir_foreach_instr(instr
, block
) {
970 if (instr
->type
!= nir_instr_type_intrinsic
)
973 nir_intrinsic_instr
*intr
= nir_instr_as_intrinsic(instr
);
974 if (intr
->intrinsic
!= nir_intrinsic_load_deref
)
977 nir_deref_instr
*in_deref
= nir_src_as_deref(intr
->src
[0]);
978 if (in_deref
->mode
!= nir_var_shader_in
)
981 nir_variable
*in_var
= nir_deref_instr_get_variable(in_deref
);
983 if (!does_varying_match(dup_out_var
, in_var
) ||
984 in_var
->data
.interpolation
!= input_var
->data
.interpolation
||
985 get_interp_loc(in_var
) != get_interp_loc(input_var
))
988 b
.cursor
= nir_before_instr(instr
);
990 nir_ssa_def
*load
= nir_load_var(&b
, input_var
);
991 nir_ssa_def_rewrite_uses(&intr
->dest
.ssa
, nir_src_for_ssa(load
));
1001 nir_link_opt_varyings(nir_shader
*producer
, nir_shader
*consumer
)
1003 /* TODO: Add support for more shader stage combinations */
1004 if (consumer
->info
.stage
!= MESA_SHADER_FRAGMENT
||
1005 (producer
->info
.stage
!= MESA_SHADER_VERTEX
&&
1006 producer
->info
.stage
!= MESA_SHADER_TESS_EVAL
))
1009 bool progress
= false;
1011 nir_function_impl
*impl
= nir_shader_get_entrypoint(producer
);
1013 struct hash_table
*varying_values
= _mesa_pointer_hash_table_create(NULL
);
1015 /* If we find a store in the last block of the producer we can be sure this
1016 * is the only possible value for this output.
1018 nir_block
*last_block
= nir_impl_last_block(impl
);
1019 nir_foreach_instr_reverse(instr
, last_block
) {
1020 if (instr
->type
!= nir_instr_type_intrinsic
)
1023 nir_intrinsic_instr
*intr
= nir_instr_as_intrinsic(instr
);
1025 if (intr
->intrinsic
!= nir_intrinsic_store_deref
)
1028 nir_deref_instr
*out_deref
= nir_src_as_deref(intr
->src
[0]);
1029 if (out_deref
->mode
!= nir_var_shader_out
)
1032 nir_variable
*out_var
= nir_deref_instr_get_variable(out_deref
);
1033 if (!can_replace_varying(out_var
))
1036 if (intr
->src
[1].ssa
->parent_instr
->type
== nir_instr_type_load_const
) {
1037 progress
|= replace_constant_input(consumer
, intr
);
1039 struct hash_entry
*entry
=
1040 _mesa_hash_table_search(varying_values
, intr
->src
[1].ssa
);
1042 progress
|= replace_duplicate_input(consumer
,
1043 (nir_variable
*) entry
->data
,
1046 nir_variable
*in_var
= get_matching_input_var(consumer
, out_var
);
1048 _mesa_hash_table_insert(varying_values
, intr
->src
[1].ssa
,
1055 _mesa_hash_table_destroy(varying_values
, NULL
);
1060 /* TODO any better helper somewhere to sort a list? */
1063 insert_sorted(struct exec_list
*var_list
, nir_variable
*new_var
)
1065 nir_foreach_variable(var
, var_list
) {
1066 if (var
->data
.location
> new_var
->data
.location
) {
1067 exec_node_insert_node_before(&var
->node
, &new_var
->node
);
1071 exec_list_push_tail(var_list
, &new_var
->node
);
1075 sort_varyings(struct exec_list
*var_list
)
1077 struct exec_list new_list
;
1078 exec_list_make_empty(&new_list
);
1079 nir_foreach_variable_safe(var
, var_list
) {
1080 exec_node_remove(&var
->node
);
1081 insert_sorted(&new_list
, var
);
1083 exec_list_move_nodes_to(&new_list
, var_list
);
1087 nir_assign_io_var_locations(struct exec_list
*var_list
, unsigned *size
,
1088 gl_shader_stage stage
)
1090 unsigned location
= 0;
1091 unsigned assigned_locations
[VARYING_SLOT_TESS_MAX
];
1092 uint64_t processed_locs
[2] = {0};
1094 sort_varyings(var_list
);
1096 int UNUSED last_loc
= 0;
1097 bool last_partial
= false;
1098 nir_foreach_variable(var
, var_list
) {
1099 const struct glsl_type
*type
= var
->type
;
1100 if (nir_is_per_vertex_io(var
, stage
) || var
->data
.per_view
) {
1101 assert(glsl_type_is_array(type
));
1102 type
= glsl_get_array_element(type
);
1106 if (var
->data
.mode
== nir_var_shader_in
&& stage
== MESA_SHADER_VERTEX
)
1107 base
= VERT_ATTRIB_GENERIC0
;
1108 else if (var
->data
.mode
== nir_var_shader_out
&&
1109 stage
== MESA_SHADER_FRAGMENT
)
1110 base
= FRAG_RESULT_DATA0
;
1112 base
= VARYING_SLOT_VAR0
;
1115 if (var
->data
.compact
) {
1116 /* If we are inside a partial compact,
1117 * don't allow another compact to be in this slot
1118 * if it starts at component 0.
1120 if (last_partial
&& var
->data
.location_frac
== 0) {
1124 /* compact variables must be arrays of scalars */
1125 assert(glsl_type_is_array(type
));
1126 assert(glsl_type_is_scalar(glsl_get_array_element(type
)));
1127 unsigned start
= 4 * location
+ var
->data
.location_frac
;
1128 unsigned end
= start
+ glsl_get_length(type
);
1129 var_size
= end
/ 4 - location
;
1130 last_partial
= end
% 4 != 0;
1132 /* Compact variables bypass the normal varying compacting pass,
1133 * which means they cannot be in the same vec4 slot as a normal
1134 * variable. If part of the current slot is taken up by a compact
1135 * variable, we need to go to the next one.
1139 last_partial
= false;
1141 var_size
= glsl_count_attribute_slots(type
, false);
1144 /* Builtins don't allow component packing so we only need to worry about
1145 * user defined varyings sharing the same location.
1147 bool processed
= false;
1148 if (var
->data
.location
>= base
) {
1149 unsigned glsl_location
= var
->data
.location
- base
;
1151 for (unsigned i
= 0; i
< var_size
; i
++) {
1152 if (processed_locs
[var
->data
.index
] &
1153 ((uint64_t)1 << (glsl_location
+ i
)))
1156 processed_locs
[var
->data
.index
] |=
1157 ((uint64_t)1 << (glsl_location
+ i
));
1161 /* Because component packing allows varyings to share the same location
1162 * we may have already have processed this location.
1165 unsigned driver_location
= assigned_locations
[var
->data
.location
];
1166 var
->data
.driver_location
= driver_location
;
1168 /* An array may be packed such that is crosses multiple other arrays
1169 * or variables, we need to make sure we have allocated the elements
1170 * consecutively if the previously proccessed var was shorter than
1171 * the current array we are processing.
1173 * NOTE: The code below assumes the var list is ordered in ascending
1176 assert(last_loc
<= var
->data
.location
);
1177 last_loc
= var
->data
.location
;
1178 unsigned last_slot_location
= driver_location
+ var_size
;
1179 if (last_slot_location
> location
) {
1180 unsigned num_unallocated_slots
= last_slot_location
- location
;
1181 unsigned first_unallocated_slot
= var_size
- num_unallocated_slots
;
1182 for (unsigned i
= first_unallocated_slot
; i
< var_size
; i
++) {
1183 assigned_locations
[var
->data
.location
+ i
] = location
;
1190 for (unsigned i
= 0; i
< var_size
; i
++) {
1191 assigned_locations
[var
->data
.location
+ i
] = location
+ i
;
1194 var
->data
.driver_location
= location
;
1195 location
+= var_size
;
1205 get_linked_variable_location(unsigned location
, bool patch
)
1210 /* Reserve locations 0...3 for special patch variables
1211 * like tess factors and bounding boxes, and the generic patch
1212 * variables will come after them.
1214 if (location
>= VARYING_SLOT_PATCH0
)
1215 return location
- VARYING_SLOT_PATCH0
+ 4;
1216 else if (location
>= VARYING_SLOT_TESS_LEVEL_OUTER
&&
1217 location
<= VARYING_SLOT_BOUNDING_BOX1
)
1218 return location
- VARYING_SLOT_TESS_LEVEL_OUTER
;
1220 unreachable("Unsupported variable in get_linked_variable_location.");
1224 get_linked_variable_io_mask(nir_variable
*variable
, gl_shader_stage stage
)
1226 const struct glsl_type
*type
= variable
->type
;
1228 if (nir_is_per_vertex_io(variable
, stage
)) {
1229 assert(glsl_type_is_array(type
));
1230 type
= glsl_get_array_element(type
);
1233 unsigned slots
= glsl_count_attribute_slots(type
, false);
1234 if (variable
->data
.compact
) {
1235 unsigned component_count
= variable
->data
.location_frac
+ glsl_get_length(type
);
1236 slots
= DIV_ROUND_UP(component_count
, 4);
1239 uint64_t mask
= u_bit_consecutive64(0, slots
);
1243 nir_linked_io_var_info
1244 nir_assign_linked_io_var_locations(nir_shader
*producer
, nir_shader
*consumer
)
1249 uint64_t producer_output_mask
= 0;
1250 uint64_t producer_patch_output_mask
= 0;
1252 nir_foreach_variable(variable
, &producer
->outputs
) {
1253 uint64_t mask
= get_linked_variable_io_mask(variable
, producer
->info
.stage
);
1254 uint64_t loc
= get_linked_variable_location(variable
->data
.location
, variable
->data
.patch
);
1256 if (variable
->data
.patch
)
1257 producer_patch_output_mask
|= mask
<< loc
;
1259 producer_output_mask
|= mask
<< loc
;
1262 uint64_t consumer_input_mask
= 0;
1263 uint64_t consumer_patch_input_mask
= 0;
1265 nir_foreach_variable(variable
, &consumer
->inputs
) {
1266 uint64_t mask
= get_linked_variable_io_mask(variable
, consumer
->info
.stage
);
1267 uint64_t loc
= get_linked_variable_location(variable
->data
.location
, variable
->data
.patch
);
1269 if (variable
->data
.patch
)
1270 consumer_patch_input_mask
|= mask
<< loc
;
1272 consumer_input_mask
|= mask
<< loc
;
1275 uint64_t io_mask
= producer_output_mask
| consumer_input_mask
;
1276 uint64_t patch_io_mask
= producer_patch_output_mask
| consumer_patch_input_mask
;
1278 nir_foreach_variable(variable
, &producer
->outputs
) {
1279 uint64_t loc
= get_linked_variable_location(variable
->data
.location
, variable
->data
.patch
);
1281 if (variable
->data
.patch
)
1282 variable
->data
.driver_location
= util_bitcount64(patch_io_mask
& u_bit_consecutive64(0, loc
)) * 4;
1284 variable
->data
.driver_location
= util_bitcount64(io_mask
& u_bit_consecutive64(0, loc
)) * 4;
1287 nir_foreach_variable(variable
, &consumer
->inputs
) {
1288 uint64_t loc
= get_linked_variable_location(variable
->data
.location
, variable
->data
.patch
);
1290 if (variable
->data
.patch
)
1291 variable
->data
.driver_location
= util_bitcount64(patch_io_mask
& u_bit_consecutive64(0, loc
)) * 4;
1293 variable
->data
.driver_location
= util_bitcount64(io_mask
& u_bit_consecutive64(0, loc
)) * 4;
1296 nir_linked_io_var_info result
= {
1297 .num_linked_io_vars
= util_bitcount64(io_mask
),
1298 .num_linked_patch_io_vars
= util_bitcount64(patch_io_mask
),