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
,
45 bool output
, gl_varying_slot slot
, unsigned array_size
)
47 nir_variable
*var
= rzalloc(shader
, nir_variable
);
49 /* TODO use type_size() for num_inputs/outputs */
51 var
->data
.driver_location
= shader
->num_outputs
++;
52 var
->data
.mode
= nir_var_shader_out
;
54 var
->data
.driver_location
= shader
->num_inputs
++;
55 var
->data
.mode
= nir_var_shader_in
;
57 var
->name
= ralloc_asprintf(var
, "clipdist_%d", var
->data
.driver_location
);
59 var
->data
.location
= slot
;
62 var
->type
= glsl_array_type(glsl_float_type(), array_size
,
64 var
->data
.compact
= 1;
66 var
->type
= glsl_vec4_type();
69 exec_list_push_tail(&shader
->outputs
, &var
->node
);
72 exec_list_push_tail(&shader
->inputs
, &var
->node
);
78 create_clipdist_vars(nir_shader
*shader
, nir_variable
**io_vars
,
79 unsigned ucp_enables
, bool output
,
80 bool use_clipdist_array
)
82 if (use_clipdist_array
) {
84 create_clipdist_var(shader
, true,
85 VARYING_SLOT_CLIP_DIST0
,
86 util_last_bit(ucp_enables
));
88 if (ucp_enables
& 0x0f)
90 create_clipdist_var(shader
, output
,
91 VARYING_SLOT_CLIP_DIST0
, 0);
92 if (ucp_enables
& 0xf0)
94 create_clipdist_var(shader
, output
,
95 VARYING_SLOT_CLIP_DIST1
, 0);
100 store_clipdist_output(nir_builder
*b
, nir_variable
*out
, nir_ssa_def
**val
)
102 nir_intrinsic_instr
*store
;
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
);
115 load_clipdist_input(nir_builder
*b
, nir_variable
*in
, nir_ssa_def
**val
)
117 nir_intrinsic_instr
*load
;
119 load
= nir_intrinsic_instr_create(b
->shader
, nir_intrinsic_load_input
);
120 load
->num_components
= 4;
121 nir_intrinsic_set_base(load
, in
->data
.driver_location
);
122 load
->src
[0] = nir_src_for_ssa(nir_imm_int(b
, 0));
123 nir_ssa_dest_init(&load
->instr
, &load
->dest
, 4, 32, NULL
);
124 nir_builder_instr_insert(b
, &load
->instr
);
126 val
[0] = nir_channel(b
, &load
->dest
.ssa
, 0);
127 val
[1] = nir_channel(b
, &load
->dest
.ssa
, 1);
128 val
[2] = nir_channel(b
, &load
->dest
.ssa
, 2);
129 val
[3] = nir_channel(b
, &load
->dest
.ssa
, 3);
133 find_output_in_block(nir_block
*block
, unsigned drvloc
)
135 nir_foreach_instr(instr
, block
) {
137 if (instr
->type
== nir_instr_type_intrinsic
) {
138 nir_intrinsic_instr
*intr
= nir_instr_as_intrinsic(instr
);
139 if ((intr
->intrinsic
== nir_intrinsic_store_output
) &&
140 nir_intrinsic_base(intr
) == drvloc
) {
141 assert(intr
->src
[0].is_ssa
);
142 assert(nir_src_is_const(intr
->src
[1]));
143 return intr
->src
[0].ssa
;
151 /* TODO: maybe this would be a useful helper?
152 * NOTE: assumes each output is written exactly once (and unconditionally)
153 * so if needed nir_lower_outputs_to_temporaries()
156 find_output(nir_shader
*shader
, unsigned drvloc
)
158 nir_ssa_def
*def
= NULL
;
159 nir_foreach_function(function
, shader
) {
160 if (function
->impl
) {
161 nir_foreach_block_reverse(block
, function
->impl
) {
162 nir_ssa_def
*new_def
= find_output_in_block(block
, drvloc
);
163 assert(!(new_def
&& def
));
166 /* for debug builds, scan entire shader to assert
167 * if output is written multiple times. For release
168 * builds just assume all is well and bail when we
182 find_clipvertex_and_position_outputs(nir_shader
*shader
,
183 nir_variable
**clipvertex
,
184 nir_variable
**position
)
186 nir_foreach_variable(var
, &shader
->outputs
) {
187 switch (var
->data
.location
) {
188 case VARYING_SLOT_POS
:
191 case VARYING_SLOT_CLIP_VERTEX
:
194 case VARYING_SLOT_CLIP_DIST0
:
195 case VARYING_SLOT_CLIP_DIST1
:
196 /* if shader is already writing CLIPDIST, then
197 * there should be no user-clip-planes to deal
200 * We assume nir_remove_dead_variables has removed the clipdist
201 * variables if they're not written.
207 return *clipvertex
|| *position
;
211 get_ucp(nir_builder
*b
, int plane
,
212 const gl_state_index16 clipplane_state_tokens
[][STATE_LENGTH
])
214 if (clipplane_state_tokens
) {
216 snprintf(tmp
, ARRAY_SIZE(tmp
), "gl_ClipPlane%dMESA", plane
);
217 nir_variable
*var
= nir_variable_create(b
->shader
,
222 var
->num_state_slots
= 1;
223 var
->state_slots
= ralloc_array(var
, nir_state_slot
, 1);
224 memcpy(var
->state_slots
[0].tokens
,
225 clipplane_state_tokens
[plane
],
226 sizeof(var
->state_slots
[0].tokens
));
227 return nir_load_var(b
, var
);
229 return nir_load_user_clip_plane(b
, plane
);
234 lower_clip_outputs(nir_builder
*b
, nir_variable
*position
,
235 nir_variable
*clipvertex
, nir_variable
**out
,
236 unsigned ucp_enables
, bool use_vars
,
237 bool use_clipdist_array
,
238 const gl_state_index16 clipplane_state_tokens
[][STATE_LENGTH
])
240 nir_ssa_def
*clipdist
[MAX_CLIP_PLANES
];
244 cv
= nir_load_var(b
, clipvertex
? clipvertex
: position
);
247 exec_node_remove(&clipvertex
->node
);
248 clipvertex
->data
.mode
= nir_var_shader_temp
;
249 exec_list_push_tail(&b
->shader
->globals
, &clipvertex
->node
);
250 nir_fixup_deref_modes(b
->shader
);
254 cv
= find_output(b
->shader
, clipvertex
->data
.driver_location
);
257 cv
= find_output(b
->shader
, position
->data
.driver_location
);
261 for (int plane
= 0; plane
< MAX_CLIP_PLANES
; plane
++) {
262 if (ucp_enables
& (1 << plane
)) {
263 nir_ssa_def
*ucp
= get_ucp(b
, plane
, clipplane_state_tokens
);
265 /* calculate clipdist[plane] - dot(ucp, cv): */
266 clipdist
[plane
] = nir_fdot4(b
, ucp
, cv
);
268 /* 0.0 == don't-clip == disabled: */
269 clipdist
[plane
] = nir_imm_float(b
, 0.0);
271 if (use_clipdist_array
&& plane
< util_last_bit(ucp_enables
)) {
273 nir_deref_instr
*deref
;
274 deref
= nir_build_deref_array_imm(b
,
275 nir_build_deref_var(b
, out
[0]),
277 nir_store_deref(b
, deref
, clipdist
[plane
], 1);
281 if (!use_clipdist_array
) {
283 if (ucp_enables
& 0x0f)
284 nir_store_var(b
, out
[0], nir_vec(b
, clipdist
, 4), 0xf);
285 if (ucp_enables
& 0xf0)
286 nir_store_var(b
, out
[1], nir_vec(b
, &clipdist
[4], 4), 0xf);
288 if (ucp_enables
& 0x0f)
289 store_clipdist_output(b
, out
[0], &clipdist
[0]);
290 if (ucp_enables
& 0xf0)
291 store_clipdist_output(b
, out
[1], &clipdist
[4]);
300 /* ucp_enables is bitmask of enabled ucps. Actual ucp values are
301 * passed in to shader via user_clip_plane system-values
303 * If use_vars is true, the pass will use variable loads and stores instead
304 * of working with store_output intrinsics.
306 * If use_clipdist_array is true, the pass will use compact arrays for the
307 * clipdist output instead of two vec4s.
310 nir_lower_clip_vs(nir_shader
*shader
, unsigned ucp_enables
, bool use_vars
,
311 bool use_clipdist_array
,
312 const gl_state_index16 clipplane_state_tokens
[][STATE_LENGTH
])
314 nir_function_impl
*impl
= nir_shader_get_entrypoint(shader
);
316 nir_variable
*position
= NULL
;
317 nir_variable
*clipvertex
= NULL
;
318 nir_variable
*out
[2] = { NULL
};
323 nir_builder_init(&b
, impl
);
325 /* NIR should ensure that, even in case of loops/if-else, there
326 * should be only a single predecessor block to end_block, which
327 * makes the perfect place to insert the clipdist calculations.
329 * NOTE: in case of early returns, these would have to be lowered
330 * to jumps to end_block predecessor in a previous pass. Not sure
331 * if there is a good way to sanity check this, but for now the
332 * users of this pass don't support sub-routines.
334 assert(impl
->end_block
->predecessors
->entries
== 1);
335 b
.cursor
= nir_after_cf_list(&impl
->body
);
337 /* find clipvertex/position outputs */
338 if (!find_clipvertex_and_position_outputs(shader
, &clipvertex
, &position
))
341 /* insert CLIPDIST outputs */
342 create_clipdist_vars(shader
, out
, ucp_enables
, true,
345 lower_clip_outputs(&b
, position
, clipvertex
, out
, ucp_enables
, use_vars
,
346 use_clipdist_array
, clipplane_state_tokens
);
348 nir_metadata_preserve(impl
, nir_metadata_dominance
);
354 lower_clip_in_gs_block(nir_builder
*b
, nir_block
*block
, nir_variable
*position
,
355 nir_variable
*clipvertex
, nir_variable
**out
,
356 unsigned ucp_enables
, bool use_clipdist_array
,
357 const gl_state_index16 clipplane_state_tokens
[][STATE_LENGTH
])
359 nir_foreach_instr_safe(instr
, block
) {
360 if (instr
->type
!= nir_instr_type_intrinsic
)
363 nir_intrinsic_instr
*intrin
= nir_instr_as_intrinsic(instr
);
364 switch (intrin
->intrinsic
) {
365 case nir_intrinsic_emit_vertex_with_counter
:
366 case nir_intrinsic_emit_vertex
:
367 b
->cursor
= nir_before_instr(instr
);
368 lower_clip_outputs(b
, position
, clipvertex
, out
, ucp_enables
, true,
369 use_clipdist_array
, clipplane_state_tokens
);
372 /* not interesting; skip this */
383 nir_lower_clip_gs(nir_shader
*shader
, unsigned ucp_enables
,
384 bool use_clipdist_array
,
385 const gl_state_index16 clipplane_state_tokens
[][STATE_LENGTH
])
387 nir_function_impl
*impl
= nir_shader_get_entrypoint(shader
);
389 nir_variable
*position
= NULL
;
390 nir_variable
*clipvertex
= NULL
;
391 nir_variable
*out
[2] = { NULL
};
396 /* find clipvertex/position outputs */
397 if (!find_clipvertex_and_position_outputs(shader
, &clipvertex
, &position
))
400 /* insert CLIPDIST outputs */
401 create_clipdist_vars(shader
, out
, ucp_enables
, true,
404 nir_builder_init(&b
, impl
);
406 nir_foreach_block(block
, impl
)
407 lower_clip_in_gs_block(&b
, block
, position
, clipvertex
, out
,
408 ucp_enables
, use_clipdist_array
,
409 clipplane_state_tokens
);
411 nir_metadata_preserve(impl
, nir_metadata_dominance
);
421 lower_clip_fs(nir_function_impl
*impl
, unsigned ucp_enables
,
424 nir_ssa_def
*clipdist
[MAX_CLIP_PLANES
];
427 nir_builder_init(&b
, impl
);
428 b
.cursor
= nir_before_cf_list(&impl
->body
);
430 if (ucp_enables
& 0x0f)
431 load_clipdist_input(&b
, in
[0], &clipdist
[0]);
432 if (ucp_enables
& 0xf0)
433 load_clipdist_input(&b
, in
[1], &clipdist
[4]);
435 for (int plane
= 0; plane
< MAX_CLIP_PLANES
; plane
++) {
436 if (ucp_enables
& (1 << plane
)) {
437 nir_intrinsic_instr
*discard
;
440 cond
= nir_flt(&b
, clipdist
[plane
], nir_imm_float(&b
, 0.0));
442 discard
= nir_intrinsic_instr_create(b
.shader
,
443 nir_intrinsic_discard_if
);
444 discard
->src
[0] = nir_src_for_ssa(cond
);
445 nir_builder_instr_insert(&b
, &discard
->instr
);
447 b
.shader
->info
.fs
.uses_discard
= true;
451 nir_metadata_preserve(impl
, nir_metadata_dominance
);
454 /* insert conditional kill based on interpolated CLIPDIST
457 nir_lower_clip_fs(nir_shader
*shader
, unsigned ucp_enables
,
458 bool use_clipdist_array
)
460 nir_variable
*in
[2] = {0};
465 /* The shader won't normally have CLIPDIST inputs, so we
468 /* insert CLIPDIST inputs */
469 create_clipdist_vars(shader
, in
, ucp_enables
, false, use_clipdist_array
);
471 nir_foreach_function(function
, shader
) {
472 if (!strcmp(function
->name
, "main"))
473 lower_clip_fs(function
->impl
, ucp_enables
, in
);