2 * Copyright © 2015 Red Hat
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:
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
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
24 * Rob Clark <robclark@freedesktop.org>
28 #include "nir_builder.h"
30 #define MAX_CLIP_PLANES 8
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-
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
44 create_clipdist_var(nir_shader
*shader
, unsigned drvloc
,
45 bool output
, gl_varying_slot slot
)
47 nir_variable
*var
= rzalloc(shader
, nir_variable
);
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
);
54 var
->data
.location
= slot
;
57 exec_list_push_tail(&shader
->outputs
, &var
->node
);
58 shader
->num_outputs
++; /* TODO use type_size() */
61 exec_list_push_tail(&shader
->inputs
, &var
->node
);
62 shader
->num_inputs
++; /* TODO use type_size() */
68 store_clipdist_output(nir_builder
*b
, nir_variable
*out
, nir_ssa_def
**val
)
70 nir_intrinsic_instr
*store
;
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
);
83 load_clipdist_input(nir_builder
*b
, nir_variable
*in
, nir_ssa_def
**val
)
85 nir_intrinsic_instr
*load
;
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, 32, NULL
);
92 nir_builder_instr_insert(b
, &load
->instr
);
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);
101 find_output_in_block(nir_block
*block
, unsigned drvloc
)
103 nir_foreach_instr(instr
, block
) {
105 if (instr
->type
== nir_instr_type_intrinsic
) {
106 nir_intrinsic_instr
*intr
= nir_instr_as_intrinsic(instr
);
107 if ((intr
->intrinsic
== nir_intrinsic_store_output
) &&
108 nir_intrinsic_base(intr
) == drvloc
) {
109 assert(intr
->src
[0].is_ssa
);
110 assert(nir_src_as_const_value(intr
->src
[1]));
111 return intr
->src
[0].ssa
;
119 /* TODO: maybe this would be a useful helper?
120 * NOTE: assumes each output is written exactly once (and unconditionally)
121 * so if needed nir_lower_outputs_to_temporaries()
124 find_output(nir_shader
*shader
, unsigned drvloc
)
126 nir_ssa_def
*def
= NULL
;
127 nir_foreach_function(function
, shader
) {
128 if (function
->impl
) {
129 nir_foreach_block_reverse(block
, function
->impl
) {
130 nir_ssa_def
*new_def
= find_output_in_block(block
, drvloc
);
131 assert(!(new_def
&& def
));
134 /* for debug builds, scan entire shader to assert
135 * if output is written multiple times. For release
136 * builds just assume all is well and bail when we
154 lower_clip_vs(nir_function_impl
*impl
, unsigned ucp_enables
,
155 nir_ssa_def
*cv
, nir_variable
**out
)
157 nir_ssa_def
*clipdist
[MAX_CLIP_PLANES
];
160 nir_builder_init(&b
, impl
);
162 /* NIR should ensure that, even in case of loops/if-else, there
163 * should be only a single predecessor block to end_block, which
164 * makes the perfect place to insert the clipdist calculations.
166 * NOTE: in case of early returns, these would have to be lowered
167 * to jumps to end_block predecessor in a previous pass. Not sure
168 * if there is a good way to sanity check this, but for now the
169 * users of this pass don't support sub-routines.
171 assert(impl
->end_block
->predecessors
->entries
== 1);
172 b
.cursor
= nir_after_cf_list(&impl
->body
);
174 for (int plane
= 0; plane
< MAX_CLIP_PLANES
; plane
++) {
175 if (ucp_enables
& (1 << plane
)) {
177 nir_load_system_value(&b
, nir_intrinsic_load_user_clip_plane
, plane
);
179 /* calculate clipdist[plane] - dot(ucp, cv): */
180 clipdist
[plane
] = nir_fdot4(&b
, ucp
, cv
);
183 /* 0.0 == don't-clip == disabled: */
184 clipdist
[plane
] = nir_imm_float(&b
, 0.0);
188 if (ucp_enables
& 0x0f)
189 store_clipdist_output(&b
, out
[0], &clipdist
[0]);
190 if (ucp_enables
& 0xf0)
191 store_clipdist_output(&b
, out
[1], &clipdist
[4]);
193 nir_metadata_preserve(impl
, nir_metadata_dominance
);
196 /* ucp_enables is bitmask of enabled ucps. Actual ucp values are
197 * passed in to shader via user_clip_plane system-values
200 nir_lower_clip_vs(nir_shader
*shader
, unsigned ucp_enables
)
206 nir_variable
*out
[2] = { NULL
};
211 /* find clipvertex/position outputs: */
212 nir_foreach_variable(var
, &shader
->outputs
) {
213 int loc
= var
->data
.driver_location
;
215 /* keep track of last used driver-location.. we'll be
216 * appending CLIP_DIST0/CLIP_DIST1 after last existing
219 maxloc
= MAX2(maxloc
, loc
);
221 switch (var
->data
.location
) {
222 case VARYING_SLOT_POS
:
225 case VARYING_SLOT_CLIP_VERTEX
:
228 case VARYING_SLOT_CLIP_DIST0
:
229 case VARYING_SLOT_CLIP_DIST1
:
230 /* if shader is already writing CLIPDIST, then
231 * there should be no user-clip-planes to deal
238 if (clipvertex
!= -1)
239 cv
= find_output(shader
, clipvertex
);
240 else if (position
!= -1)
241 cv
= find_output(shader
, position
);
245 /* insert CLIPDIST outputs: */
246 if (ucp_enables
& 0x0f)
248 create_clipdist_var(shader
, ++maxloc
, true, VARYING_SLOT_CLIP_DIST0
);
249 if (ucp_enables
& 0xf0)
251 create_clipdist_var(shader
, ++maxloc
, true, VARYING_SLOT_CLIP_DIST1
);
253 nir_foreach_function(function
, shader
) {
254 if (!strcmp(function
->name
, "main"))
255 lower_clip_vs(function
->impl
, ucp_enables
, cv
, out
);
266 lower_clip_fs(nir_function_impl
*impl
, unsigned ucp_enables
,
269 nir_ssa_def
*clipdist
[MAX_CLIP_PLANES
];
272 nir_builder_init(&b
, impl
);
273 b
.cursor
= nir_before_cf_list(&impl
->body
);
275 if (ucp_enables
& 0x0f)
276 load_clipdist_input(&b
, in
[0], &clipdist
[0]);
277 if (ucp_enables
& 0xf0)
278 load_clipdist_input(&b
, in
[1], &clipdist
[4]);
280 for (int plane
= 0; plane
< MAX_CLIP_PLANES
; plane
++) {
281 if (ucp_enables
& (1 << plane
)) {
282 nir_intrinsic_instr
*discard
;
285 cond
= nir_flt(&b
, clipdist
[plane
], nir_imm_float(&b
, 0.0));
287 discard
= nir_intrinsic_instr_create(b
.shader
,
288 nir_intrinsic_discard_if
);
289 discard
->src
[0] = nir_src_for_ssa(cond
);
290 nir_builder_instr_insert(&b
, &discard
->instr
);
292 b
.shader
->info
.fs
.uses_discard
= true;
296 nir_metadata_preserve(impl
, nir_metadata_dominance
);
299 /* insert conditional kill based on interpolated CLIPDIST
302 nir_lower_clip_fs(nir_shader
*shader
, unsigned ucp_enables
)
310 nir_foreach_variable(var
, &shader
->inputs
) {
311 int loc
= var
->data
.driver_location
;
313 /* keep track of last used driver-location.. we'll be
314 * appending CLIP_DIST0/CLIP_DIST1 after last existing
317 maxloc
= MAX2(maxloc
, loc
);
320 /* The shader won't normally have CLIPDIST inputs, so we
323 /* insert CLIPDIST outputs: */
324 if (ucp_enables
& 0x0f)
326 create_clipdist_var(shader
, ++maxloc
, false,
327 VARYING_SLOT_CLIP_DIST0
);
328 if (ucp_enables
& 0xf0)
330 create_clipdist_var(shader
, ++maxloc
, false,
331 VARYING_SLOT_CLIP_DIST1
);
333 nir_foreach_function(function
, shader
) {
334 if (!strcmp(function
->name
, "main"))
335 lower_clip_fs(function
->impl
, ucp_enables
, in
);