glsl_type: Add support for explicitly laid out matrices and arrays
[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 the GLSL lowering pass has already run, don't bother repeating */
148 if (!cull && clip) {
149 if (!glsl_type_is_array(clip->type))
150 return false;
151 }
152
153 const unsigned clip_array_size = get_unwrapped_array_length(nir, clip);
154 const unsigned cull_array_size = get_unwrapped_array_length(nir, cull);
155
156 if (store_info) {
157 nir->info.clip_distance_array_size = clip_array_size;
158 nir->info.cull_distance_array_size = cull_array_size;
159 }
160
161 if (clip)
162 clip->data.compact = true;
163
164 if (cull)
165 cull->data.compact = true;
166
167 if (cull_array_size > 0) {
168 if (clip_array_size == 0) {
169 /* No clip distances, just change the cull distance location */
170 cull->data.location = VARYING_SLOT_CLIP_DIST0;
171 } else {
172 /* Turn the ClipDistance array into a combined one */
173 update_type(clip, nir->info.stage, clip_array_size + cull_array_size);
174
175 /* Rewrite CullDistance to reference the combined array */
176 nir_foreach_function(function, nir) {
177 if (function->impl) {
178 nir_builder b;
179 nir_builder_init(&b, function->impl);
180
181 nir_foreach_block(block, function->impl) {
182 nir_foreach_instr(instr, block) {
183 rewrite_references(&b, instr, clip, clip_array_size);
184 }
185 }
186 }
187 }
188
189 /* Delete the old CullDistance variable */
190 exec_node_remove(&cull->node);
191 ralloc_free(cull);
192 }
193
194 nir_foreach_function(function, nir) {
195 if (function->impl) {
196 nir_metadata_preserve(function->impl,
197 nir_metadata_block_index |
198 nir_metadata_dominance);
199 }
200 }
201 progress = true;
202 }
203
204 return progress;
205 }
206
207 bool
208 nir_lower_clip_cull_distance_arrays(nir_shader *nir)
209 {
210 bool progress = false;
211
212 if (nir->info.stage <= MESA_SHADER_GEOMETRY)
213 progress |= combine_clip_cull(nir, &nir->outputs, true);
214
215 if (nir->info.stage > MESA_SHADER_VERTEX)
216 progress |= combine_clip_cull(nir, &nir->inputs, false);
217
218 return progress;
219 }