Merge ../mesa into vulkan
[mesa.git] / src / glsl / nir / nir_lower_clip.c
1 /*
2 * Copyright © 2015 Red Hat
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 * Authors:
24 * Rob Clark <robclark@freedesktop.org>
25 */
26
27 #include "nir.h"
28 #include "nir_builder.h"
29
30 #define MAX_CLIP_PLANES 8
31
32 /* Generates the lowering code for user-clip-planes, generating CLIPDIST
33 * from UCP[n] + CLIPVERTEX or POSITION. Additionally, an optional pass
34 * for fragment shaders to insert conditional kill's based on the inter-
35 * polated CLIPDIST
36 *
37 * NOTE: should be run after nir_lower_outputs_to_temporaries() (or at
38 * least in scenarios where you can count on each output written once
39 * and only once).
40 */
41
42
43 static nir_variable *
44 create_clipdist_var(nir_shader *shader, unsigned drvloc,
45 bool output, gl_varying_slot slot)
46 {
47 nir_variable *var = rzalloc(shader, nir_variable);
48
49 var->data.driver_location = drvloc;
50 var->type = glsl_vec4_type();
51 var->data.mode = output ? nir_var_shader_out : nir_var_shader_in;
52 var->name = ralloc_asprintf(var, "clipdist_%d", drvloc);
53 var->data.index = 0;
54 var->data.location = slot;
55
56 if (output) {
57 exec_list_push_tail(&shader->outputs, &var->node);
58 shader->num_outputs++; /* TODO use type_size() */
59 }
60 else {
61 exec_list_push_tail(&shader->inputs, &var->node);
62 shader->num_inputs++; /* TODO use type_size() */
63 }
64 return var;
65 }
66
67 static void
68 store_clipdist_output(nir_builder *b, nir_variable *out, nir_ssa_def **val)
69 {
70 nir_intrinsic_instr *store;
71
72 store = nir_intrinsic_instr_create(b->shader, nir_intrinsic_store_output);
73 store->num_components = 4;
74 store->const_index[0] = out->data.driver_location;
75 store->src[0].ssa = nir_vec4(b, val[0], val[1], val[2], val[3]);
76 store->src[0].is_ssa = true;
77 store->src[1] = nir_src_for_ssa(nir_imm_int(b, 0));
78 nir_builder_instr_insert(b, &store->instr);
79 }
80
81 static void
82 load_clipdist_input(nir_builder *b, nir_variable *in, nir_ssa_def **val)
83 {
84 nir_intrinsic_instr *load;
85
86 load = nir_intrinsic_instr_create(b->shader, nir_intrinsic_load_input);
87 load->num_components = 4;
88 load->const_index[0] = in->data.driver_location;
89 load->src[0] = nir_src_for_ssa(nir_imm_int(b, 0));
90 nir_ssa_dest_init(&load->instr, &load->dest, 4, NULL);
91 nir_builder_instr_insert(b, &load->instr);
92
93 val[0] = nir_channel(b, &load->dest.ssa, 0);
94 val[1] = nir_channel(b, &load->dest.ssa, 1);
95 val[2] = nir_channel(b, &load->dest.ssa, 2);
96 val[3] = nir_channel(b, &load->dest.ssa, 3);
97 }
98
99 struct find_output_state
100 {
101 unsigned drvloc;
102 nir_ssa_def *def;
103 };
104
105 static bool
106 find_output_in_block(nir_block *block, void *void_state)
107 {
108 struct find_output_state *state = void_state;
109 nir_foreach_instr(block, instr) {
110
111 if (instr->type == nir_instr_type_intrinsic) {
112 nir_intrinsic_instr *intr = nir_instr_as_intrinsic(instr);
113 if ((intr->intrinsic == nir_intrinsic_store_output) &&
114 intr->const_index[0] == state->drvloc) {
115 assert(state->def == NULL);
116 assert(intr->src[0].is_ssa);
117 assert(nir_src_as_const_value(intr->src[1]));
118 state->def = intr->src[0].ssa;
119
120 #if !defined(DEBUG)
121 /* for debug builds, scan entire shader to assert
122 * if output is written multiple times. For release
123 * builds just assume all is well and bail when we
124 * find first:
125 */
126 return false;
127 #endif
128 }
129 }
130 }
131
132 return true;
133 }
134
135 /* TODO: maybe this would be a useful helper?
136 * NOTE: assumes each output is written exactly once (and unconditionally)
137 * so if needed nir_lower_outputs_to_temporaries()
138 */
139 static nir_ssa_def *
140 find_output(nir_shader *shader, unsigned drvloc)
141 {
142 struct find_output_state state = {
143 .drvloc = drvloc,
144 };
145
146 nir_foreach_overload(shader, overload) {
147 if (overload->impl) {
148 nir_foreach_block_reverse(overload->impl,
149 find_output_in_block, &state);
150 }
151 }
152
153 return state.def;
154 }
155
156 /*
157 * VS lowering
158 */
159
160 static void
161 lower_clip_vs(nir_function_impl *impl, unsigned ucp_enables,
162 nir_ssa_def *cv, nir_variable **out)
163 {
164 nir_ssa_def *clipdist[MAX_CLIP_PLANES];
165 nir_builder b;
166
167 nir_builder_init(&b, impl);
168
169 /* NIR should ensure that, even in case of loops/if-else, there
170 * should be only a single predecessor block to end_block, which
171 * makes the perfect place to insert the clipdist calculations.
172 *
173 * NOTE: in case of early return's, these would have to be lowered
174 * to jumps to end_block predecessor in a previous pass. Not sure
175 * if there is a good way to sanity check this, but for now the
176 * users of this pass don't support sub-routines.
177 */
178 assert(impl->end_block->predecessors->entries == 1);
179 b.cursor = nir_after_cf_list(&impl->body);
180
181 for (int plane = 0; plane < MAX_CLIP_PLANES; plane++) {
182 if (ucp_enables & (1 << plane)) {
183 nir_intrinsic_instr *ucp;
184
185 /* insert intrinsic to fetch ucp[plane]: */
186 ucp = nir_intrinsic_instr_create(b.shader,
187 nir_intrinsic_load_user_clip_plane);
188 ucp->num_components = 4;
189 ucp->const_index[0] = plane;
190 nir_ssa_dest_init(&ucp->instr, &ucp->dest, 4, NULL);
191 nir_builder_instr_insert(&b, &ucp->instr);
192
193 /* calculate clipdist[plane] - dot(ucp, cv): */
194 clipdist[plane] = nir_fdot4(&b, &ucp->dest.ssa, cv);
195 }
196 else {
197 /* 0.0 == don't-clip == disabled: */
198 clipdist[plane] = nir_imm_float(&b, 0.0);
199 }
200 }
201
202 if (ucp_enables & 0x0f)
203 store_clipdist_output(&b, out[0], &clipdist[0]);
204 if (ucp_enables & 0xf0)
205 store_clipdist_output(&b, out[1], &clipdist[4]);
206
207 nir_metadata_preserve(impl, nir_metadata_dominance);
208 }
209
210 /* ucp_enables is bitmask of enabled ucp's. Actual ucp values are
211 * passed in to shader via user_clip_plane system-values
212 */
213 void
214 nir_lower_clip_vs(nir_shader *shader, unsigned ucp_enables)
215 {
216 int clipvertex = -1;
217 int position = -1;
218 int maxloc = -1;
219 nir_ssa_def *cv;
220 nir_variable *out[2] = { NULL };
221
222 if (!ucp_enables)
223 return;
224
225 /* find clipvertex/position outputs: */
226 nir_foreach_variable(var, &shader->outputs) {
227 int loc = var->data.driver_location;
228
229 /* keep track of last used driver-location.. we'll be
230 * appending CLIP_DIST0/CLIP_DIST1 after last existing
231 * output:
232 */
233 maxloc = MAX2(maxloc, loc);
234
235 switch (var->data.location) {
236 case VARYING_SLOT_POS:
237 position = loc;
238 break;
239 case VARYING_SLOT_CLIP_VERTEX:
240 clipvertex = loc;
241 break;
242 case VARYING_SLOT_CLIP_DIST0:
243 case VARYING_SLOT_CLIP_DIST1:
244 /* if shader is already writing CLIPDIST, then
245 * there should be no user-clip-planes to deal
246 * with.
247 */
248 return;
249 }
250 }
251
252 if (clipvertex != -1)
253 cv = find_output(shader, clipvertex);
254 else if (position != -1)
255 cv = find_output(shader, position);
256 else
257 return;
258
259 /* insert CLIPDIST outputs: */
260 if (ucp_enables & 0x0f)
261 out[0] =
262 create_clipdist_var(shader, ++maxloc, true, VARYING_SLOT_CLIP_DIST0);
263 if (ucp_enables & 0xf0)
264 out[1] =
265 create_clipdist_var(shader, ++maxloc, true, VARYING_SLOT_CLIP_DIST1);
266
267 nir_foreach_overload(shader, overload) {
268 if (!strcmp(overload->function->name, "main"))
269 lower_clip_vs(overload->impl, ucp_enables, cv, out);
270 }
271 }
272
273 /*
274 * FS lowering
275 */
276
277 static void
278 lower_clip_fs(nir_function_impl *impl, unsigned ucp_enables,
279 nir_variable **in)
280 {
281 nir_ssa_def *clipdist[MAX_CLIP_PLANES];
282 nir_builder b;
283
284 nir_builder_init(&b, impl);
285 b.cursor = nir_before_cf_list(&impl->body);
286
287 if (ucp_enables & 0x0f)
288 load_clipdist_input(&b, in[0], &clipdist[0]);
289 if (ucp_enables & 0xf0)
290 load_clipdist_input(&b, in[1], &clipdist[4]);
291
292 for (int plane = 0; plane < MAX_CLIP_PLANES; plane++) {
293 if (ucp_enables & (1 << plane)) {
294 nir_intrinsic_instr *discard;
295 nir_ssa_def *cond;
296
297 cond = nir_flt(&b, clipdist[plane], nir_imm_float(&b, 0.0));
298
299 discard = nir_intrinsic_instr_create(b.shader,
300 nir_intrinsic_discard_if);
301 discard->src[0] = nir_src_for_ssa(cond);
302 nir_builder_instr_insert(&b, &discard->instr);
303 }
304 }
305 }
306
307 /* insert conditional kill based on interpolated CLIPDIST
308 */
309 void
310 nir_lower_clip_fs(nir_shader *shader, unsigned ucp_enables)
311 {
312 nir_variable *in[2];
313 int maxloc = -1;
314
315 if (!ucp_enables)
316 return;
317
318 nir_foreach_variable(var, &shader->inputs) {
319 int loc = var->data.driver_location;
320
321 /* keep track of last used driver-location.. we'll be
322 * appending CLIP_DIST0/CLIP_DIST1 after last existing
323 * input:
324 */
325 maxloc = MAX2(maxloc, loc);
326 }
327
328 /* The shader won't normally have CLIPDIST inputs, so we
329 * must add our own:
330 */
331 /* insert CLIPDIST outputs: */
332 if (ucp_enables & 0x0f)
333 in[0] =
334 create_clipdist_var(shader, ++maxloc, false,
335 VARYING_SLOT_CLIP_DIST0);
336 if (ucp_enables & 0xf0)
337 in[1] =
338 create_clipdist_var(shader, ++maxloc, false,
339 VARYING_SLOT_CLIP_DIST1);
340
341 nir_foreach_overload(shader, overload) {
342 if (!strcmp(overload->function->name, "main"))
343 lower_clip_fs(overload->impl, ucp_enables, in);
344 }
345 }