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 any clip/cull distances to refer to the new combined array.
77 rewrite_var_references(nir_instr
*instr
,
78 nir_variable
*combined
,
81 if (instr
->type
!= nir_instr_type_intrinsic
)
84 nir_intrinsic_instr
*intrin
= nir_instr_as_intrinsic(instr
);
86 /* copy_var needs to be lowered to load/store before calling this pass */
87 assert(intrin
->intrinsic
!= nir_intrinsic_copy_var
);
89 if (intrin
->intrinsic
!= nir_intrinsic_load_var
&&
90 intrin
->intrinsic
!= nir_intrinsic_store_var
)
93 nir_deref_var
*var_ref
= intrin
->variables
[0];
94 if (var_ref
->var
->data
.mode
!= combined
->data
.mode
)
97 if (var_ref
->var
->data
.location
!= VARYING_SLOT_CLIP_DIST0
&&
98 var_ref
->var
->data
.location
!= VARYING_SLOT_CULL_DIST0
)
101 /* Update types along the deref chain */
102 const struct glsl_type
*type
= combined
->type
;
103 nir_deref
*deref
= &var_ref
->deref
;
106 deref
= deref
->child
;
107 type
= glsl_get_array_element(type
);
110 /* For cull distances, add an offset to the array index */
111 if (var_ref
->var
->data
.location
== VARYING_SLOT_CULL_DIST0
) {
112 nir_deref
*tail
= nir_deref_tail(&intrin
->variables
[0]->deref
);
113 nir_deref_array
*array_ref
= nir_deref_as_array(tail
);
115 array_ref
->base_offset
+= cull_offset
;
118 /* Point the deref at the combined array */
119 var_ref
->var
= combined
;
121 /* There's no need to update writemasks; it's a scalar array. */
125 rewrite_clip_cull_deref(nir_builder
*b
,
126 nir_deref_instr
*deref
,
127 const struct glsl_type
*type
,
128 unsigned tail_offset
)
132 if (glsl_type_is_array(type
)) {
133 const struct glsl_type
*child_type
= glsl_get_array_element(type
);
134 nir_foreach_use(src
, &deref
->dest
.ssa
) {
135 rewrite_clip_cull_deref(b
, nir_instr_as_deref(src
->parent_instr
),
136 child_type
, tail_offset
);
139 assert(glsl_type_is_scalar(type
));
141 /* This is the end of the line. Add the tail offset if needed */
142 if (tail_offset
> 0) {
143 b
->cursor
= nir_before_instr(&deref
->instr
);
144 assert(deref
->deref_type
== nir_deref_type_array
);
145 nir_ssa_def
*index
= nir_iadd(b
, deref
->arr
.index
.ssa
,
146 nir_imm_int(b
, tail_offset
));
147 nir_instr_rewrite_src(&deref
->instr
, &deref
->arr
.index
,
148 nir_src_for_ssa(index
));
154 rewrite_references(nir_builder
*b
,
156 nir_variable
*combined
,
157 unsigned cull_offset
)
159 if (instr
->type
!= nir_instr_type_deref
)
162 nir_deref_instr
*deref
= nir_instr_as_deref(instr
);
163 if (deref
->deref_type
!= nir_deref_type_var
)
166 if (deref
->var
->data
.mode
!= combined
->data
.mode
)
169 const unsigned location
= deref
->var
->data
.location
;
170 if (location
!= VARYING_SLOT_CLIP_DIST0
&&
171 location
!= VARYING_SLOT_CULL_DIST0
)
174 deref
->var
= combined
;
175 if (location
== VARYING_SLOT_CULL_DIST0
)
176 rewrite_clip_cull_deref(b
, deref
, combined
->type
, cull_offset
);
178 rewrite_clip_cull_deref(b
, deref
, combined
->type
, 0);
182 combine_clip_cull(nir_shader
*nir
,
183 struct exec_list
*vars
,
186 nir_variable
*cull
= NULL
;
187 nir_variable
*clip
= NULL
;
188 bool progress
= false;
190 nir_foreach_variable(var
, vars
) {
191 if (var
->data
.location
== VARYING_SLOT_CLIP_DIST0
)
194 if (var
->data
.location
== VARYING_SLOT_CULL_DIST0
)
198 const unsigned clip_array_size
= get_unwrapped_array_length(nir
, clip
);
199 const unsigned cull_array_size
= get_unwrapped_array_length(nir
, cull
);
202 nir
->info
.clip_distance_array_size
= clip_array_size
;
203 nir
->info
.cull_distance_array_size
= cull_array_size
;
207 clip
->data
.compact
= true;
210 cull
->data
.compact
= true;
212 if (cull_array_size
> 0) {
213 if (clip_array_size
== 0) {
214 /* No clip distances, just change the cull distance location */
215 cull
->data
.location
= VARYING_SLOT_CLIP_DIST0
;
217 /* Turn the ClipDistance array into a combined one */
218 update_type(clip
, nir
->info
.stage
, clip_array_size
+ cull_array_size
);
220 /* Rewrite CullDistance to reference the combined array */
221 nir_foreach_function(function
, nir
) {
222 if (function
->impl
) {
224 nir_builder_init(&b
, function
->impl
);
226 nir_foreach_block(block
, function
->impl
) {
227 nir_foreach_instr(instr
, block
) {
228 rewrite_var_references(instr
, clip
, clip_array_size
);
229 rewrite_references(&b
, instr
, clip
, clip_array_size
);
235 /* Delete the old CullDistance variable */
236 exec_node_remove(&cull
->node
);
240 nir_foreach_function(function
, nir
) {
241 if (function
->impl
) {
242 nir_metadata_preserve(function
->impl
,
243 nir_metadata_block_index
|
244 nir_metadata_dominance
);
254 nir_lower_clip_cull_distance_arrays(nir_shader
*nir
)
256 bool progress
= false;
258 if (nir
->info
.stage
<= MESA_SHADER_GEOMETRY
)
259 progress
|= combine_clip_cull(nir
, &nir
->outputs
, true);
261 if (nir
->info
.stage
> MESA_SHADER_VERTEX
)
262 progress
|= combine_clip_cull(nir
, &nir
->inputs
, false);