nir/lower_clip: add lower_clip_outputs() helper
[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)
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 create_clipdist_vars(nir_shader *shader, nir_variable **io_vars,
69 unsigned ucp_enables, int *drvloc, bool output)
70 {
71 if (ucp_enables & 0x0f)
72 io_vars[0] =
73 create_clipdist_var(shader, ++(*drvloc), output,
74 VARYING_SLOT_CLIP_DIST0);
75 if (ucp_enables & 0xf0)
76 io_vars[1] =
77 create_clipdist_var(shader, ++(*drvloc), output,
78 VARYING_SLOT_CLIP_DIST1);
79 }
80
81 static void
82 store_clipdist_output(nir_builder *b, nir_variable *out, nir_ssa_def **val)
83 {
84 nir_intrinsic_instr *store;
85
86 store = nir_intrinsic_instr_create(b->shader, nir_intrinsic_store_output);
87 store->num_components = 4;
88 nir_intrinsic_set_base(store, out->data.driver_location);
89 nir_intrinsic_set_write_mask(store, 0xf);
90 store->src[0].ssa = nir_vec4(b, val[0], val[1], val[2], val[3]);
91 store->src[0].is_ssa = true;
92 store->src[1] = nir_src_for_ssa(nir_imm_int(b, 0));
93 nir_builder_instr_insert(b, &store->instr);
94 }
95
96 static void
97 load_clipdist_input(nir_builder *b, nir_variable *in, nir_ssa_def **val)
98 {
99 nir_intrinsic_instr *load;
100
101 load = nir_intrinsic_instr_create(b->shader, nir_intrinsic_load_input);
102 load->num_components = 4;
103 nir_intrinsic_set_base(load, in->data.driver_location);
104 load->src[0] = nir_src_for_ssa(nir_imm_int(b, 0));
105 nir_ssa_dest_init(&load->instr, &load->dest, 4, 32, NULL);
106 nir_builder_instr_insert(b, &load->instr);
107
108 val[0] = nir_channel(b, &load->dest.ssa, 0);
109 val[1] = nir_channel(b, &load->dest.ssa, 1);
110 val[2] = nir_channel(b, &load->dest.ssa, 2);
111 val[3] = nir_channel(b, &load->dest.ssa, 3);
112 }
113
114 static nir_ssa_def *
115 find_output_in_block(nir_block *block, unsigned drvloc)
116 {
117 nir_foreach_instr(instr, block) {
118
119 if (instr->type == nir_instr_type_intrinsic) {
120 nir_intrinsic_instr *intr = nir_instr_as_intrinsic(instr);
121 if ((intr->intrinsic == nir_intrinsic_store_output) &&
122 nir_intrinsic_base(intr) == drvloc) {
123 assert(intr->src[0].is_ssa);
124 assert(nir_src_is_const(intr->src[1]));
125 return intr->src[0].ssa;
126 }
127 }
128 }
129
130 return NULL;
131 }
132
133 /* TODO: maybe this would be a useful helper?
134 * NOTE: assumes each output is written exactly once (and unconditionally)
135 * so if needed nir_lower_outputs_to_temporaries()
136 */
137 static nir_ssa_def *
138 find_output(nir_shader *shader, unsigned drvloc)
139 {
140 nir_ssa_def *def = NULL;
141 nir_foreach_function(function, shader) {
142 if (function->impl) {
143 nir_foreach_block_reverse(block, function->impl) {
144 nir_ssa_def *new_def = find_output_in_block(block, drvloc);
145 assert(!(new_def && def));
146 def = new_def;
147 #if !defined(DEBUG)
148 /* for debug builds, scan entire shader to assert
149 * if output is written multiple times. For release
150 * builds just assume all is well and bail when we
151 * find first:
152 */
153 if (def)
154 break;
155 #endif
156 }
157 }
158 }
159
160 return def;
161 }
162
163 static bool
164 find_clipvertex_and_position_outputs(nir_shader *shader,
165 nir_variable **clipvertex,
166 nir_variable **position)
167 {
168 nir_foreach_variable(var, &shader->outputs) {
169 switch (var->data.location) {
170 case VARYING_SLOT_POS:
171 *position = var;
172 break;
173 case VARYING_SLOT_CLIP_VERTEX:
174 *clipvertex = var;
175 break;
176 case VARYING_SLOT_CLIP_DIST0:
177 case VARYING_SLOT_CLIP_DIST1:
178 /* if shader is already writing CLIPDIST, then
179 * there should be no user-clip-planes to deal
180 * with.
181 *
182 * We assume nir_remove_dead_variables has removed the clipdist
183 * variables if they're not written.
184 */
185 return false;
186 }
187 }
188
189 return *clipvertex || *position;
190 }
191
192 static void
193 lower_clip_outputs(nir_builder *b, nir_variable *position,
194 nir_variable *clipvertex, nir_variable **out,
195 unsigned ucp_enables, bool use_vars)
196 {
197 nir_ssa_def *clipdist[MAX_CLIP_PLANES];
198 nir_ssa_def *cv;
199
200 if (use_vars) {
201 cv = nir_load_var(b, clipvertex ? clipvertex : position);
202
203 if (clipvertex) {
204 exec_node_remove(&clipvertex->node);
205 clipvertex->data.mode = nir_var_shader_temp;
206 exec_list_push_tail(&b->shader->globals, &clipvertex->node);
207 }
208 } else {
209 if (clipvertex)
210 cv = find_output(b->shader, clipvertex->data.driver_location);
211 else {
212 assert(position);
213 cv = find_output(b->shader, position->data.driver_location);
214 }
215 }
216
217 for (int plane = 0; plane < MAX_CLIP_PLANES; plane++) {
218 if (ucp_enables & (1 << plane)) {
219 nir_ssa_def *ucp = nir_load_user_clip_plane(b, plane);
220
221 /* calculate clipdist[plane] - dot(ucp, cv): */
222 clipdist[plane] = nir_fdot4(b, ucp, cv);
223 } else {
224 /* 0.0 == don't-clip == disabled: */
225 clipdist[plane] = nir_imm_float(b, 0.0);
226 }
227 }
228
229 if (use_vars) {
230 if (ucp_enables & 0x0f)
231 nir_store_var(b, out[0], nir_vec(b, clipdist, 4), 0xf);
232 if (ucp_enables & 0xf0)
233 nir_store_var(b, out[1], nir_vec(b, &clipdist[4], 4), 0xf);
234 } else {
235 if (ucp_enables & 0x0f)
236 store_clipdist_output(b, out[0], &clipdist[0]);
237 if (ucp_enables & 0xf0)
238 store_clipdist_output(b, out[1], &clipdist[4]);
239 }
240 }
241
242 /*
243 * VS lowering
244 */
245
246 /* ucp_enables is bitmask of enabled ucps. Actual ucp values are
247 * passed in to shader via user_clip_plane system-values
248 *
249 * If use_vars is true, the pass will use variable loads and stores instead
250 * of working with store_output intrinsics.
251 */
252 bool
253 nir_lower_clip_vs(nir_shader *shader, unsigned ucp_enables, bool use_vars)
254 {
255 nir_function_impl *impl = nir_shader_get_entrypoint(shader);
256 nir_builder b;
257 int maxloc = -1;
258 nir_variable *position = NULL;
259 nir_variable *clipvertex = NULL;
260 nir_variable *out[2] = { NULL };
261
262 if (!ucp_enables)
263 return false;
264
265 nir_builder_init(&b, impl);
266
267 /* NIR should ensure that, even in case of loops/if-else, there
268 * should be only a single predecessor block to end_block, which
269 * makes the perfect place to insert the clipdist calculations.
270 *
271 * NOTE: in case of early returns, these would have to be lowered
272 * to jumps to end_block predecessor in a previous pass. Not sure
273 * if there is a good way to sanity check this, but for now the
274 * users of this pass don't support sub-routines.
275 */
276 assert(impl->end_block->predecessors->entries == 1);
277 b.cursor = nir_after_cf_list(&impl->body);
278
279 /* find clipvertex/position outputs */
280 if (!find_clipvertex_and_position_outputs(shader, &clipvertex, &position))
281 return false;
282
283 /* insert CLIPDIST outputs */
284 create_clipdist_vars(shader, out, ucp_enables, &maxloc, true);
285
286 lower_clip_outputs(&b, position, clipvertex, out, ucp_enables, use_vars);
287
288 nir_metadata_preserve(impl, nir_metadata_dominance);
289
290 return true;
291 }
292
293 /*
294 * FS lowering
295 */
296
297 static void
298 lower_clip_fs(nir_function_impl *impl, unsigned ucp_enables,
299 nir_variable **in)
300 {
301 nir_ssa_def *clipdist[MAX_CLIP_PLANES];
302 nir_builder b;
303
304 nir_builder_init(&b, impl);
305 b.cursor = nir_before_cf_list(&impl->body);
306
307 if (ucp_enables & 0x0f)
308 load_clipdist_input(&b, in[0], &clipdist[0]);
309 if (ucp_enables & 0xf0)
310 load_clipdist_input(&b, in[1], &clipdist[4]);
311
312 for (int plane = 0; plane < MAX_CLIP_PLANES; plane++) {
313 if (ucp_enables & (1 << plane)) {
314 nir_intrinsic_instr *discard;
315 nir_ssa_def *cond;
316
317 cond = nir_flt(&b, clipdist[plane], nir_imm_float(&b, 0.0));
318
319 discard = nir_intrinsic_instr_create(b.shader,
320 nir_intrinsic_discard_if);
321 discard->src[0] = nir_src_for_ssa(cond);
322 nir_builder_instr_insert(&b, &discard->instr);
323
324 b.shader->info.fs.uses_discard = true;
325 }
326 }
327
328 nir_metadata_preserve(impl, nir_metadata_dominance);
329 }
330
331 /* insert conditional kill based on interpolated CLIPDIST
332 */
333 bool
334 nir_lower_clip_fs(nir_shader *shader, unsigned ucp_enables)
335 {
336 nir_variable *in[2];
337 int maxloc = -1;
338
339 if (!ucp_enables)
340 return false;
341
342 nir_foreach_variable(var, &shader->inputs) {
343 int loc = var->data.driver_location;
344
345 /* keep track of last used driver-location.. we'll be
346 * appending CLIP_DIST0/CLIP_DIST1 after last existing
347 * input:
348 */
349 maxloc = MAX2(maxloc, loc);
350 }
351
352 /* The shader won't normally have CLIPDIST inputs, so we
353 * must add our own:
354 */
355 /* insert CLIPDIST inputs */
356 create_clipdist_vars(shader, in, ucp_enables, &maxloc, false);
357
358 nir_foreach_function(function, shader) {
359 if (!strcmp(function->name, "main"))
360 lower_clip_fs(function->impl, ucp_enables, in);
361 }
362
363 return true;
364 }