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
->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_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 combine_clip_cull(nir_shader
*nir
,
126 struct exec_list
*vars
,
129 nir_variable
*cull
= NULL
;
130 nir_variable
*clip
= NULL
;
132 nir_foreach_variable(var
, vars
) {
133 if (var
->data
.location
== VARYING_SLOT_CLIP_DIST0
)
136 if (var
->data
.location
== VARYING_SLOT_CULL_DIST0
)
140 const unsigned clip_array_size
= get_unwrapped_array_length(nir
, clip
);
141 const unsigned cull_array_size
= get_unwrapped_array_length(nir
, cull
);
144 nir
->info
->clip_distance_array_size
= clip_array_size
;
145 nir
->info
->cull_distance_array_size
= cull_array_size
;
149 clip
->data
.compact
= true;
152 cull
->data
.compact
= true;
154 if (cull_array_size
> 0) {
155 if (clip_array_size
== 0) {
156 /* No clip distances, just change the cull distance location */
157 cull
->data
.location
= VARYING_SLOT_CLIP_DIST0
;
159 /* Turn the ClipDistance array into a combined one */
160 update_type(clip
, nir
->stage
, clip_array_size
+ cull_array_size
);
162 /* Rewrite CullDistance to reference the combined array */
163 nir_foreach_function(function
, nir
) {
164 if (function
->impl
) {
165 nir_foreach_block(block
, function
->impl
) {
166 nir_foreach_instr(instr
, block
) {
167 rewrite_references(instr
, clip
, clip_array_size
);
173 /* Delete the old CullDistance variable */
174 exec_node_remove(&cull
->node
);
181 nir_lower_clip_cull_distance_arrays(nir_shader
*nir
)
183 if (nir
->stage
<= MESA_SHADER_GEOMETRY
)
184 combine_clip_cull(nir
, &nir
->outputs
, true);
186 if (nir
->stage
> MESA_SHADER_VERTEX
)
187 combine_clip_cull(nir
, &nir
->inputs
, false);