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"
30 * This pass combines separate clip and cull distance arrays into a
31 * single array that contains both. Clip distances come first, then
32 * cull distances. It also populates nir_shader_info with the size
33 * of the original arrays so the driver knows which are which.
37 * Get the length of the clip/cull distance array, looking past
38 * any interface block arrays.
41 get_unwrapped_array_length(nir_shader
*nir
, nir_variable
*var
)
46 /* Unwrap GS input and TCS input/output interfaces. We want the
47 * underlying clip/cull distance array length, not the per-vertex
50 const struct glsl_type
*type
= var
->type
;
51 if (nir_is_per_vertex_io(var
, nir
->info
.stage
))
52 type
= glsl_get_array_element(type
);
54 assert(glsl_type_is_array(type
));
56 return glsl_get_length(type
);
60 * Update the type of the combined array (including interface block nesting).
63 update_type(nir_variable
*var
, gl_shader_stage stage
, unsigned length
)
65 const struct glsl_type
*type
= glsl_array_type(glsl_float_type(), length
);
67 if (nir_is_per_vertex_io(var
, stage
))
68 type
= glsl_array_type(type
, glsl_get_length(var
->type
));
74 rewrite_clip_cull_deref(nir_builder
*b
,
75 nir_deref_instr
*deref
,
76 const struct glsl_type
*type
,
81 if (glsl_type_is_array(type
)) {
82 const struct glsl_type
*child_type
= glsl_get_array_element(type
);
83 nir_foreach_use(src
, &deref
->dest
.ssa
) {
84 rewrite_clip_cull_deref(b
, nir_instr_as_deref(src
->parent_instr
),
85 child_type
, tail_offset
);
88 assert(glsl_type_is_scalar(type
));
90 /* This is the end of the line. Add the tail offset if needed */
91 if (tail_offset
> 0) {
92 b
->cursor
= nir_before_instr(&deref
->instr
);
93 assert(deref
->deref_type
== nir_deref_type_array
);
94 nir_ssa_def
*index
= nir_iadd(b
, deref
->arr
.index
.ssa
,
95 nir_imm_int(b
, tail_offset
));
96 nir_instr_rewrite_src(&deref
->instr
, &deref
->arr
.index
,
97 nir_src_for_ssa(index
));
103 rewrite_references(nir_builder
*b
,
105 nir_variable
*combined
,
106 unsigned cull_offset
)
108 if (instr
->type
!= nir_instr_type_deref
)
111 nir_deref_instr
*deref
= nir_instr_as_deref(instr
);
112 if (deref
->deref_type
!= nir_deref_type_var
)
115 if (deref
->var
->data
.mode
!= combined
->data
.mode
)
118 const unsigned location
= deref
->var
->data
.location
;
119 if (location
!= VARYING_SLOT_CLIP_DIST0
&&
120 location
!= VARYING_SLOT_CULL_DIST0
)
123 deref
->var
= combined
;
124 if (location
== VARYING_SLOT_CULL_DIST0
)
125 rewrite_clip_cull_deref(b
, deref
, combined
->type
, cull_offset
);
127 rewrite_clip_cull_deref(b
, deref
, combined
->type
, 0);
131 combine_clip_cull(nir_shader
*nir
,
132 struct exec_list
*vars
,
135 nir_variable
*cull
= NULL
;
136 nir_variable
*clip
= NULL
;
137 bool progress
= false;
139 nir_foreach_variable(var
, vars
) {
140 if (var
->data
.location
== VARYING_SLOT_CLIP_DIST0
)
143 if (var
->data
.location
== VARYING_SLOT_CULL_DIST0
)
147 const unsigned clip_array_size
= get_unwrapped_array_length(nir
, clip
);
148 const unsigned cull_array_size
= get_unwrapped_array_length(nir
, cull
);
151 nir
->info
.clip_distance_array_size
= clip_array_size
;
152 nir
->info
.cull_distance_array_size
= cull_array_size
;
156 clip
->data
.compact
= true;
159 cull
->data
.compact
= true;
161 if (cull_array_size
> 0) {
162 if (clip_array_size
== 0) {
163 /* No clip distances, just change the cull distance location */
164 cull
->data
.location
= VARYING_SLOT_CLIP_DIST0
;
166 /* Turn the ClipDistance array into a combined one */
167 update_type(clip
, nir
->info
.stage
, clip_array_size
+ cull_array_size
);
169 /* Rewrite CullDistance to reference the combined array */
170 nir_foreach_function(function
, nir
) {
171 if (function
->impl
) {
173 nir_builder_init(&b
, function
->impl
);
175 nir_foreach_block(block
, function
->impl
) {
176 nir_foreach_instr(instr
, block
) {
177 rewrite_references(&b
, instr
, clip
, clip_array_size
);
183 /* Delete the old CullDistance variable */
184 exec_node_remove(&cull
->node
);
188 nir_foreach_function(function
, nir
) {
189 if (function
->impl
) {
190 nir_metadata_preserve(function
->impl
,
191 nir_metadata_block_index
|
192 nir_metadata_dominance
);
202 nir_lower_clip_cull_distance_arrays(nir_shader
*nir
)
204 bool progress
= false;
206 if (nir
->info
.stage
<= MESA_SHADER_GEOMETRY
)
207 progress
|= combine_clip_cull(nir
, &nir
->outputs
, true);
209 if (nir
->info
.stage
> MESA_SHADER_VERTEX
)
210 progress
|= combine_clip_cull(nir
, &nir
->inputs
, false);