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