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