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