nir: Support deref instructions in lower_clip_cull
[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);
66
67 if (nir_is_per_vertex_io(var, stage))
68 type = glsl_array_type(type, glsl_get_length(var->type));
69
70 var->type = type;
71 }
72
73 /**
74 * Rewrite any clip/cull distances to refer to the new combined array.
75 */
76 static void
77 rewrite_var_references(nir_instr *instr,
78 nir_variable *combined,
79 unsigned cull_offset)
80 {
81 if (instr->type != nir_instr_type_intrinsic)
82 return;
83
84 nir_intrinsic_instr *intrin = nir_instr_as_intrinsic(instr);
85
86 /* copy_var needs to be lowered to load/store before calling this pass */
87 assert(intrin->intrinsic != nir_intrinsic_copy_var);
88
89 if (intrin->intrinsic != nir_intrinsic_load_var &&
90 intrin->intrinsic != nir_intrinsic_store_var)
91 return;
92
93 nir_deref_var *var_ref = intrin->variables[0];
94 if (var_ref->var->data.mode != combined->data.mode)
95 return;
96
97 if (var_ref->var->data.location != VARYING_SLOT_CLIP_DIST0 &&
98 var_ref->var->data.location != VARYING_SLOT_CULL_DIST0)
99 return;
100
101 /* Update types along the deref chain */
102 const struct glsl_type *type = combined->type;
103 nir_deref *deref = &var_ref->deref;
104 while (deref) {
105 deref->type = type;
106 deref = deref->child;
107 type = glsl_get_array_element(type);
108 }
109
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);
114
115 array_ref->base_offset += cull_offset;
116 }
117
118 /* Point the deref at the combined array */
119 var_ref->var = combined;
120
121 /* There's no need to update writemasks; it's a scalar array. */
122 }
123
124 static void
125 rewrite_clip_cull_deref(nir_builder *b,
126 nir_deref_instr *deref,
127 const struct glsl_type *type,
128 unsigned tail_offset)
129 {
130 deref->type = type;
131
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);
137 }
138 } else {
139 assert(glsl_type_is_scalar(type));
140
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));
149 }
150 }
151 }
152
153 static void
154 rewrite_references(nir_builder *b,
155 nir_instr *instr,
156 nir_variable *combined,
157 unsigned cull_offset)
158 {
159 if (instr->type != nir_instr_type_deref)
160 return;
161
162 nir_deref_instr *deref = nir_instr_as_deref(instr);
163 if (deref->deref_type != nir_deref_type_var)
164 return;
165
166 if (deref->var->data.mode != combined->data.mode)
167 return;
168
169 const unsigned location = deref->var->data.location;
170 if (location != VARYING_SLOT_CLIP_DIST0 &&
171 location != VARYING_SLOT_CULL_DIST0)
172 return;
173
174 deref->var = combined;
175 if (location == VARYING_SLOT_CULL_DIST0)
176 rewrite_clip_cull_deref(b, deref, combined->type, cull_offset);
177 else
178 rewrite_clip_cull_deref(b, deref, combined->type, 0);
179 }
180
181 static bool
182 combine_clip_cull(nir_shader *nir,
183 struct exec_list *vars,
184 bool store_info)
185 {
186 nir_variable *cull = NULL;
187 nir_variable *clip = NULL;
188 bool progress = false;
189
190 nir_foreach_variable(var, vars) {
191 if (var->data.location == VARYING_SLOT_CLIP_DIST0)
192 clip = var;
193
194 if (var->data.location == VARYING_SLOT_CULL_DIST0)
195 cull = var;
196 }
197
198 const unsigned clip_array_size = get_unwrapped_array_length(nir, clip);
199 const unsigned cull_array_size = get_unwrapped_array_length(nir, cull);
200
201 if (store_info) {
202 nir->info.clip_distance_array_size = clip_array_size;
203 nir->info.cull_distance_array_size = cull_array_size;
204 }
205
206 if (clip)
207 clip->data.compact = true;
208
209 if (cull)
210 cull->data.compact = true;
211
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;
216 } else {
217 /* Turn the ClipDistance array into a combined one */
218 update_type(clip, nir->info.stage, clip_array_size + cull_array_size);
219
220 /* Rewrite CullDistance to reference the combined array */
221 nir_foreach_function(function, nir) {
222 if (function->impl) {
223 nir_builder b;
224 nir_builder_init(&b, function->impl);
225
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);
230 }
231 }
232 }
233 }
234
235 /* Delete the old CullDistance variable */
236 exec_node_remove(&cull->node);
237 ralloc_free(cull);
238 }
239
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);
245 }
246 }
247 progress = true;
248 }
249
250 return progress;
251 }
252
253 bool
254 nir_lower_clip_cull_distance_arrays(nir_shader *nir)
255 {
256 bool progress = false;
257
258 if (nir->info.stage <= MESA_SHADER_GEOMETRY)
259 progress |= combine_clip_cull(nir, &nir->outputs, true);
260
261 if (nir->info.stage > MESA_SHADER_VERTEX)
262 progress |= combine_clip_cull(nir, &nir->inputs, false);
263
264 return progress;
265 }