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