nir/lower_clip: Fix incorrect driver loc for clipdist outputs
[mesa.git] / src / compiler / 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 kills 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, unsigned array_size)
46 {
47 nir_variable *var = rzalloc(shader, nir_variable);
48
49 var->data.driver_location = drvloc;
50 var->data.mode = output ? nir_var_shader_out : nir_var_shader_in;
51 var->name = ralloc_asprintf(var, "clipdist_%d", drvloc);
52 var->data.index = 0;
53 var->data.location = slot;
54
55 if (array_size > 0) {
56 var->type = glsl_array_type(glsl_float_type(), array_size,
57 sizeof(float));
58 var->data.compact = 1;
59 } else
60 var->type = glsl_vec4_type();
61
62 if (output) {
63 exec_list_push_tail(&shader->outputs, &var->node);
64 shader->num_outputs++; /* TODO use type_size() */
65 }
66 else {
67 exec_list_push_tail(&shader->inputs, &var->node);
68 shader->num_inputs++; /* TODO use type_size() */
69 }
70 return var;
71 }
72
73 static void
74 create_clipdist_vars(nir_shader *shader, nir_variable **io_vars,
75 unsigned ucp_enables, int *drvloc, bool output,
76 bool use_clipdist_array)
77 {
78 if (use_clipdist_array) {
79 io_vars[0] =
80 create_clipdist_var(shader, ++(*drvloc), true,
81 VARYING_SLOT_CLIP_DIST0,
82 util_last_bit(ucp_enables));
83 } else {
84 if (ucp_enables & 0x0f)
85 io_vars[0] =
86 create_clipdist_var(shader, ++(*drvloc), output,
87 VARYING_SLOT_CLIP_DIST0, 0);
88 if (ucp_enables & 0xf0)
89 io_vars[1] =
90 create_clipdist_var(shader, ++(*drvloc), output,
91 VARYING_SLOT_CLIP_DIST1, 0);
92 }
93 }
94
95 static void
96 store_clipdist_output(nir_builder *b, nir_variable *out, nir_ssa_def **val)
97 {
98 nir_intrinsic_instr *store;
99
100 store = nir_intrinsic_instr_create(b->shader, nir_intrinsic_store_output);
101 store->num_components = 4;
102 nir_intrinsic_set_base(store, out->data.driver_location);
103 nir_intrinsic_set_write_mask(store, 0xf);
104 store->src[0].ssa = nir_vec4(b, val[0], val[1], val[2], val[3]);
105 store->src[0].is_ssa = true;
106 store->src[1] = nir_src_for_ssa(nir_imm_int(b, 0));
107 nir_builder_instr_insert(b, &store->instr);
108 }
109
110 static void
111 load_clipdist_input(nir_builder *b, nir_variable *in, nir_ssa_def **val)
112 {
113 nir_intrinsic_instr *load;
114
115 load = nir_intrinsic_instr_create(b->shader, nir_intrinsic_load_input);
116 load->num_components = 4;
117 nir_intrinsic_set_base(load, in->data.driver_location);
118 load->src[0] = nir_src_for_ssa(nir_imm_int(b, 0));
119 nir_ssa_dest_init(&load->instr, &load->dest, 4, 32, NULL);
120 nir_builder_instr_insert(b, &load->instr);
121
122 val[0] = nir_channel(b, &load->dest.ssa, 0);
123 val[1] = nir_channel(b, &load->dest.ssa, 1);
124 val[2] = nir_channel(b, &load->dest.ssa, 2);
125 val[3] = nir_channel(b, &load->dest.ssa, 3);
126 }
127
128 static nir_ssa_def *
129 find_output_in_block(nir_block *block, unsigned drvloc)
130 {
131 nir_foreach_instr(instr, block) {
132
133 if (instr->type == nir_instr_type_intrinsic) {
134 nir_intrinsic_instr *intr = nir_instr_as_intrinsic(instr);
135 if ((intr->intrinsic == nir_intrinsic_store_output) &&
136 nir_intrinsic_base(intr) == drvloc) {
137 assert(intr->src[0].is_ssa);
138 assert(nir_src_is_const(intr->src[1]));
139 return intr->src[0].ssa;
140 }
141 }
142 }
143
144 return NULL;
145 }
146
147 /* TODO: maybe this would be a useful helper?
148 * NOTE: assumes each output is written exactly once (and unconditionally)
149 * so if needed nir_lower_outputs_to_temporaries()
150 */
151 static nir_ssa_def *
152 find_output(nir_shader *shader, unsigned drvloc)
153 {
154 nir_ssa_def *def = NULL;
155 nir_foreach_function(function, shader) {
156 if (function->impl) {
157 nir_foreach_block_reverse(block, function->impl) {
158 nir_ssa_def *new_def = find_output_in_block(block, drvloc);
159 assert(!(new_def && def));
160 def = new_def;
161 #if !defined(DEBUG)
162 /* for debug builds, scan entire shader to assert
163 * if output is written multiple times. For release
164 * builds just assume all is well and bail when we
165 * find first:
166 */
167 if (def)
168 break;
169 #endif
170 }
171 }
172 }
173
174 return def;
175 }
176
177 static bool
178 find_clipvertex_and_position_outputs(nir_shader *shader,
179 nir_variable **clipvertex,
180 nir_variable **position)
181 {
182 nir_foreach_variable(var, &shader->outputs) {
183 switch (var->data.location) {
184 case VARYING_SLOT_POS:
185 *position = var;
186 break;
187 case VARYING_SLOT_CLIP_VERTEX:
188 *clipvertex = var;
189 break;
190 case VARYING_SLOT_CLIP_DIST0:
191 case VARYING_SLOT_CLIP_DIST1:
192 /* if shader is already writing CLIPDIST, then
193 * there should be no user-clip-planes to deal
194 * with.
195 *
196 * We assume nir_remove_dead_variables has removed the clipdist
197 * variables if they're not written.
198 */
199 return false;
200 }
201 }
202
203 return *clipvertex || *position;
204 }
205
206 static nir_ssa_def *
207 get_ucp(nir_builder *b, int plane,
208 const gl_state_index16 clipplane_state_tokens[][STATE_LENGTH])
209 {
210 if (clipplane_state_tokens) {
211 char tmp[100];
212 snprintf(tmp, ARRAY_SIZE(tmp), "gl_ClipPlane%dMESA", plane);
213 nir_variable *var = nir_variable_create(b->shader,
214 nir_var_uniform,
215 glsl_vec4_type(),
216 tmp);
217
218 var->num_state_slots = 1;
219 var->state_slots = ralloc_array(var, nir_state_slot, 1);
220 memcpy(var->state_slots[0].tokens,
221 clipplane_state_tokens[plane],
222 sizeof(var->state_slots[0].tokens));
223 return nir_load_var(b, var);
224 } else
225 return nir_load_user_clip_plane(b, plane);
226 }
227
228
229 static void
230 lower_clip_outputs(nir_builder *b, nir_variable *position,
231 nir_variable *clipvertex, nir_variable **out,
232 unsigned ucp_enables, bool use_vars,
233 bool use_clipdist_array,
234 const gl_state_index16 clipplane_state_tokens[][STATE_LENGTH])
235 {
236 nir_ssa_def *clipdist[MAX_CLIP_PLANES];
237 nir_ssa_def *cv;
238
239 if (use_vars) {
240 cv = nir_load_var(b, clipvertex ? clipvertex : position);
241
242 if (clipvertex) {
243 exec_node_remove(&clipvertex->node);
244 clipvertex->data.mode = nir_var_shader_temp;
245 exec_list_push_tail(&b->shader->globals, &clipvertex->node);
246 nir_fixup_deref_modes(b->shader);
247 }
248 } else {
249 if (clipvertex)
250 cv = find_output(b->shader, clipvertex->data.driver_location);
251 else {
252 assert(position);
253 cv = find_output(b->shader, position->data.driver_location);
254 }
255 }
256
257 for (int plane = 0; plane < MAX_CLIP_PLANES; plane++) {
258 if (ucp_enables & (1 << plane)) {
259 nir_ssa_def *ucp = get_ucp(b, plane, clipplane_state_tokens);
260
261 /* calculate clipdist[plane] - dot(ucp, cv): */
262 clipdist[plane] = nir_fdot4(b, ucp, cv);
263 } else {
264 /* 0.0 == don't-clip == disabled: */
265 clipdist[plane] = nir_imm_float(b, 0.0);
266 }
267 if (use_clipdist_array && plane < util_last_bit(ucp_enables)) {
268 assert(use_vars);
269 nir_deref_instr *deref;
270 deref = nir_build_deref_array_imm(b,
271 nir_build_deref_var(b, out[0]),
272 plane);
273 nir_store_deref(b, deref, clipdist[plane], 1);
274 }
275 }
276
277 if (!use_clipdist_array) {
278 if (use_vars) {
279 if (ucp_enables & 0x0f)
280 nir_store_var(b, out[0], nir_vec(b, clipdist, 4), 0xf);
281 if (ucp_enables & 0xf0)
282 nir_store_var(b, out[1], nir_vec(b, &clipdist[4], 4), 0xf);
283 } else {
284 if (ucp_enables & 0x0f)
285 store_clipdist_output(b, out[0], &clipdist[0]);
286 if (ucp_enables & 0xf0)
287 store_clipdist_output(b, out[1], &clipdist[4]);
288 }
289 }
290 }
291
292 /*
293 * VS lowering
294 */
295
296 /* ucp_enables is bitmask of enabled ucps. Actual ucp values are
297 * passed in to shader via user_clip_plane system-values
298 *
299 * If use_vars is true, the pass will use variable loads and stores instead
300 * of working with store_output intrinsics.
301 *
302 * If use_clipdist_array is true, the pass will use compact arrays for the
303 * clipdist output instead of two vec4s.
304 */
305 bool
306 nir_lower_clip_vs(nir_shader *shader, unsigned ucp_enables, bool use_vars,
307 bool use_clipdist_array,
308 const gl_state_index16 clipplane_state_tokens[][STATE_LENGTH])
309 {
310 nir_function_impl *impl = nir_shader_get_entrypoint(shader);
311 nir_builder b;
312 int maxloc = -1;
313 nir_variable *position = NULL;
314 nir_variable *clipvertex = NULL;
315 nir_variable *out[2] = { NULL };
316
317 if (!ucp_enables)
318 return false;
319
320 /* find clipvertex/position outputs: */
321 nir_foreach_variable(var, &shader->outputs) {
322 int loc = var->data.driver_location;
323
324 /* keep track of last used driver-location.. we'll be
325 * appending CLIP_DIST0/CLIP_DIST1 after last existing
326 * output:
327 */
328 maxloc = MAX2(maxloc, loc);
329 }
330
331 nir_builder_init(&b, impl);
332
333 /* NIR should ensure that, even in case of loops/if-else, there
334 * should be only a single predecessor block to end_block, which
335 * makes the perfect place to insert the clipdist calculations.
336 *
337 * NOTE: in case of early returns, these would have to be lowered
338 * to jumps to end_block predecessor in a previous pass. Not sure
339 * if there is a good way to sanity check this, but for now the
340 * users of this pass don't support sub-routines.
341 */
342 assert(impl->end_block->predecessors->entries == 1);
343 b.cursor = nir_after_cf_list(&impl->body);
344
345 /* find clipvertex/position outputs */
346 if (!find_clipvertex_and_position_outputs(shader, &clipvertex, &position))
347 return false;
348
349 /* insert CLIPDIST outputs */
350 create_clipdist_vars(shader, out, ucp_enables, &maxloc, true,
351 use_clipdist_array);
352
353 lower_clip_outputs(&b, position, clipvertex, out, ucp_enables, use_vars,
354 use_clipdist_array, clipplane_state_tokens);
355
356 nir_metadata_preserve(impl, nir_metadata_dominance);
357
358 return true;
359 }
360
361 static void
362 lower_clip_in_gs_block(nir_builder *b, nir_block *block, nir_variable *position,
363 nir_variable *clipvertex, nir_variable **out,
364 unsigned ucp_enables, bool use_clipdist_array,
365 const gl_state_index16 clipplane_state_tokens[][STATE_LENGTH])
366 {
367 nir_foreach_instr_safe(instr, block) {
368 if (instr->type != nir_instr_type_intrinsic)
369 continue;
370
371 nir_intrinsic_instr *intrin = nir_instr_as_intrinsic(instr);
372 switch (intrin->intrinsic) {
373 case nir_intrinsic_emit_vertex_with_counter:
374 case nir_intrinsic_emit_vertex:
375 b->cursor = nir_before_instr(instr);
376 lower_clip_outputs(b, position, clipvertex, out, ucp_enables, true,
377 use_clipdist_array, clipplane_state_tokens);
378 break;
379 default:
380 /* not interesting; skip this */
381 break;
382 }
383 }
384 }
385
386 /*
387 * GS lowering
388 */
389
390 bool
391 nir_lower_clip_gs(nir_shader *shader, unsigned ucp_enables,
392 bool use_clipdist_array,
393 const gl_state_index16 clipplane_state_tokens[][STATE_LENGTH])
394 {
395 nir_function_impl *impl = nir_shader_get_entrypoint(shader);
396 nir_builder b;
397 int maxloc = -1;
398 nir_variable *position = NULL;
399 nir_variable *clipvertex = NULL;
400 nir_variable *out[2] = { NULL };
401
402 if (!ucp_enables)
403 return false;
404
405 /* find clipvertex/position outputs */
406 if (!find_clipvertex_and_position_outputs(shader, &clipvertex, &position))
407 return false;
408
409 /* insert CLIPDIST outputs */
410 create_clipdist_vars(shader, out, ucp_enables, &maxloc, true,
411 use_clipdist_array);
412
413 nir_builder_init(&b, impl);
414
415 nir_foreach_block(block, impl)
416 lower_clip_in_gs_block(&b, block, position, clipvertex, out,
417 ucp_enables, use_clipdist_array,
418 clipplane_state_tokens);
419
420 nir_metadata_preserve(impl, nir_metadata_dominance);
421
422 return true;
423 }
424
425 /*
426 * FS lowering
427 */
428
429 static void
430 lower_clip_fs(nir_function_impl *impl, unsigned ucp_enables,
431 nir_variable **in)
432 {
433 nir_ssa_def *clipdist[MAX_CLIP_PLANES];
434 nir_builder b;
435
436 nir_builder_init(&b, impl);
437 b.cursor = nir_before_cf_list(&impl->body);
438
439 if (ucp_enables & 0x0f)
440 load_clipdist_input(&b, in[0], &clipdist[0]);
441 if (ucp_enables & 0xf0)
442 load_clipdist_input(&b, in[1], &clipdist[4]);
443
444 for (int plane = 0; plane < MAX_CLIP_PLANES; plane++) {
445 if (ucp_enables & (1 << plane)) {
446 nir_intrinsic_instr *discard;
447 nir_ssa_def *cond;
448
449 cond = nir_flt(&b, clipdist[plane], nir_imm_float(&b, 0.0));
450
451 discard = nir_intrinsic_instr_create(b.shader,
452 nir_intrinsic_discard_if);
453 discard->src[0] = nir_src_for_ssa(cond);
454 nir_builder_instr_insert(&b, &discard->instr);
455
456 b.shader->info.fs.uses_discard = true;
457 }
458 }
459
460 nir_metadata_preserve(impl, nir_metadata_dominance);
461 }
462
463 /* insert conditional kill based on interpolated CLIPDIST
464 */
465 bool
466 nir_lower_clip_fs(nir_shader *shader, unsigned ucp_enables,
467 bool use_clipdist_array)
468 {
469 nir_variable *in[2];
470 int maxloc = -1;
471
472 if (!ucp_enables)
473 return false;
474
475 nir_foreach_variable(var, &shader->inputs) {
476 int loc = var->data.driver_location;
477
478 /* keep track of last used driver-location.. we'll be
479 * appending CLIP_DIST0/CLIP_DIST1 after last existing
480 * input:
481 */
482 maxloc = MAX2(maxloc, loc);
483 }
484
485 /* The shader won't normally have CLIPDIST inputs, so we
486 * must add our own:
487 */
488 /* insert CLIPDIST inputs */
489 create_clipdist_vars(shader, in, ucp_enables, &maxloc, false,
490 use_clipdist_array);
491
492 nir_foreach_function(function, shader) {
493 if (!strcmp(function->name, "main"))
494 lower_clip_fs(function->impl, ucp_enables, in);
495 }
496
497 return true;
498 }