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