nir: Avoid clip/cull distance lowering multiple times.
[mesa.git] / src / compiler / nir / nir_lower_clip_cull_distance_arrays.c
1 /*
2 * Copyright © 2015 Intel Corporation
3 *
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:
10 *
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
13 * Software.
14 *
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
21 * IN THE SOFTWARE.
22 */
23
24 #include "nir.h"
25 #include "nir_builder.h"
26
27 /**
28 * @file
29 *
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.
34 */
35
36 /**
37 * Get the length of the clip/cull distance array, looking past
38 * any interface block arrays.
39 */
40 static unsigned
41 get_unwrapped_array_length(nir_shader *nir, nir_variable *var)
42 {
43 if (!var)
44 return 0;
45
46 /* Unwrap GS input and TCS input/output interfaces. We want the
47 * underlying clip/cull distance array length, not the per-vertex
48 * array length.
49 */
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);
53
54 assert(glsl_type_is_array(type));
55
56 return glsl_get_length(type);
57 }
58
59 /**
60 * Update the type of the combined array (including interface block nesting).
61 */
62 static void
63 update_type(nir_variable *var, gl_shader_stage stage, unsigned length)
64 {
65 const struct glsl_type *type = glsl_array_type(glsl_float_type(), length, 0);
66
67 if (nir_is_per_vertex_io(var, stage))
68 type = glsl_array_type(type, glsl_get_length(var->type), 0);
69
70 var->type = type;
71 }
72
73 static void
74 rewrite_clip_cull_deref(nir_builder *b,
75 nir_deref_instr *deref,
76 const struct glsl_type *type,
77 unsigned tail_offset)
78 {
79 deref->type = type;
80
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);
86 }
87 } else {
88 assert(glsl_type_is_scalar(type));
89
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));
98 }
99 }
100 }
101
102 static void
103 rewrite_references(nir_builder *b,
104 nir_instr *instr,
105 nir_variable *combined,
106 unsigned cull_offset)
107 {
108 if (instr->type != nir_instr_type_deref)
109 return;
110
111 nir_deref_instr *deref = nir_instr_as_deref(instr);
112 if (deref->deref_type != nir_deref_type_var)
113 return;
114
115 if (deref->var->data.mode != combined->data.mode)
116 return;
117
118 const unsigned location = deref->var->data.location;
119 if (location != VARYING_SLOT_CLIP_DIST0 &&
120 location != VARYING_SLOT_CULL_DIST0)
121 return;
122
123 deref->var = combined;
124 if (location == VARYING_SLOT_CULL_DIST0)
125 rewrite_clip_cull_deref(b, deref, combined->type, cull_offset);
126 else
127 rewrite_clip_cull_deref(b, deref, combined->type, 0);
128 }
129
130 static bool
131 combine_clip_cull(nir_shader *nir,
132 struct exec_list *vars,
133 bool store_info)
134 {
135 nir_variable *cull = NULL;
136 nir_variable *clip = NULL;
137 bool progress = false;
138
139 nir_foreach_variable(var, vars) {
140 if (var->data.location == VARYING_SLOT_CLIP_DIST0)
141 clip = var;
142
143 if (var->data.location == VARYING_SLOT_CULL_DIST0)
144 cull = var;
145 }
146
147 if (!cull && clip) {
148 /* The GLSL IR lowering pass must have converted these to vectors */
149 if (!clip->data.compact)
150 return false;
151
152 /* If this pass has already run, don't repeat. We would think that
153 * the combined clip/cull distance array was clip-only and mess up.
154 */
155 if (clip->data.how_declared == nir_var_hidden)
156 return false;
157 }
158
159 const unsigned clip_array_size = get_unwrapped_array_length(nir, clip);
160 const unsigned cull_array_size = get_unwrapped_array_length(nir, cull);
161
162 if (store_info) {
163 nir->info.clip_distance_array_size = clip_array_size;
164 nir->info.cull_distance_array_size = cull_array_size;
165 }
166
167 if (clip) {
168 clip->data.compact = true;
169 clip->data.how_declared = nir_var_hidden;
170 }
171
172 if (cull) {
173 cull->data.compact = true;
174 cull->data.how_declared = nir_var_hidden;
175 }
176
177 if (cull_array_size > 0) {
178 if (clip_array_size == 0) {
179 /* No clip distances, just change the cull distance location */
180 cull->data.location = VARYING_SLOT_CLIP_DIST0;
181 } else {
182 /* Turn the ClipDistance array into a combined one */
183 update_type(clip, nir->info.stage, clip_array_size + cull_array_size);
184
185 /* Rewrite CullDistance to reference the combined array */
186 nir_foreach_function(function, nir) {
187 if (function->impl) {
188 nir_builder b;
189 nir_builder_init(&b, function->impl);
190
191 nir_foreach_block(block, function->impl) {
192 nir_foreach_instr(instr, block) {
193 rewrite_references(&b, instr, clip, clip_array_size);
194 }
195 }
196 }
197 }
198
199 /* Delete the old CullDistance variable */
200 exec_node_remove(&cull->node);
201 ralloc_free(cull);
202 }
203
204 nir_foreach_function(function, nir) {
205 if (function->impl) {
206 nir_metadata_preserve(function->impl,
207 nir_metadata_block_index |
208 nir_metadata_dominance);
209 }
210 }
211 progress = true;
212 }
213
214 return progress;
215 }
216
217 bool
218 nir_lower_clip_cull_distance_arrays(nir_shader *nir)
219 {
220 bool progress = false;
221
222 if (nir->info.stage <= MESA_SHADER_GEOMETRY)
223 progress |= combine_clip_cull(nir, &nir->outputs, true);
224
225 if (nir->info.stage > MESA_SHADER_VERTEX)
226 progress |= combine_clip_cull(nir, &nir->inputs, false);
227
228 return progress;
229 }