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();
68 nir_shader_add_variable(shader
, var
);
73 create_clipdist_vars(nir_shader
*shader
, nir_variable
**io_vars
,
74 unsigned ucp_enables
, bool output
,
75 bool use_clipdist_array
)
77 if (use_clipdist_array
) {
79 create_clipdist_var(shader
, output
,
80 VARYING_SLOT_CLIP_DIST0
,
81 util_last_bit(ucp_enables
));
83 if (ucp_enables
& 0x0f)
85 create_clipdist_var(shader
, output
,
86 VARYING_SLOT_CLIP_DIST0
, 0);
87 if (ucp_enables
& 0xf0)
89 create_clipdist_var(shader
, output
,
90 VARYING_SLOT_CLIP_DIST1
, 0);
95 store_clipdist_output(nir_builder
*b
, nir_variable
*out
, nir_ssa_def
**val
)
97 nir_intrinsic_instr
*store
;
99 store
= nir_intrinsic_instr_create(b
->shader
, nir_intrinsic_store_output
);
100 store
->num_components
= 4;
101 nir_intrinsic_set_base(store
, out
->data
.driver_location
);
102 nir_intrinsic_set_write_mask(store
, 0xf);
103 store
->src
[0].ssa
= nir_vec4(b
, val
[0], val
[1], val
[2], val
[3]);
104 store
->src
[0].is_ssa
= true;
105 store
->src
[1] = nir_src_for_ssa(nir_imm_int(b
, 0));
106 nir_builder_instr_insert(b
, &store
->instr
);
110 load_clipdist_input(nir_builder
*b
, nir_variable
*in
, int location_offset
,
113 nir_intrinsic_instr
*load
;
115 load
= nir_intrinsic_instr_create(b
->shader
, nir_intrinsic_load_input
);
116 load
->num_components
= 4;
117 nir_intrinsic_set_base(load
, in
->data
.driver_location
+ location_offset
);
118 load
->src
[0] = nir_src_for_ssa(nir_imm_int(b
, 0));
119 nir_ssa_dest_init(&load
->instr
, &load
->dest
, 4, 32, NULL
);
120 nir_builder_instr_insert(b
, &load
->instr
);
122 val
[0] = nir_channel(b
, &load
->dest
.ssa
, 0);
123 val
[1] = nir_channel(b
, &load
->dest
.ssa
, 1);
124 val
[2] = nir_channel(b
, &load
->dest
.ssa
, 2);
125 val
[3] = nir_channel(b
, &load
->dest
.ssa
, 3);
129 find_output_in_block(nir_block
*block
, unsigned drvloc
)
131 nir_foreach_instr(instr
, block
) {
133 if (instr
->type
== nir_instr_type_intrinsic
) {
134 nir_intrinsic_instr
*intr
= nir_instr_as_intrinsic(instr
);
135 if ((intr
->intrinsic
== nir_intrinsic_store_output
) &&
136 nir_intrinsic_base(intr
) == drvloc
) {
137 assert(intr
->src
[0].is_ssa
);
138 assert(nir_src_is_const(intr
->src
[1]));
139 return intr
->src
[0].ssa
;
147 /* TODO: maybe this would be a useful helper?
148 * NOTE: assumes each output is written exactly once (and unconditionally)
149 * so if needed nir_lower_outputs_to_temporaries()
152 find_output(nir_shader
*shader
, unsigned drvloc
)
154 nir_ssa_def
*def
= NULL
;
155 nir_foreach_function(function
, shader
) {
156 if (function
->impl
) {
157 nir_foreach_block_reverse(block
, function
->impl
) {
158 nir_ssa_def
*new_def
= find_output_in_block(block
, drvloc
);
159 assert(!(new_def
&& def
));
162 /* for debug builds, scan entire shader to assert
163 * if output is written multiple times. For release
164 * builds just assume all is well and bail when we
178 find_clipvertex_and_position_outputs(nir_shader
*shader
,
179 nir_variable
**clipvertex
,
180 nir_variable
**position
)
182 nir_foreach_shader_out_variable(var
, shader
) {
183 switch (var
->data
.location
) {
184 case VARYING_SLOT_POS
:
187 case VARYING_SLOT_CLIP_VERTEX
:
190 case VARYING_SLOT_CLIP_DIST0
:
191 case VARYING_SLOT_CLIP_DIST1
:
192 /* if shader is already writing CLIPDIST, then
193 * there should be no user-clip-planes to deal
196 * We assume nir_remove_dead_variables has removed the clipdist
197 * variables if they're not written.
203 return *clipvertex
|| *position
;
207 get_ucp(nir_builder
*b
, int plane
,
208 const gl_state_index16 clipplane_state_tokens
[][STATE_LENGTH
])
210 if (clipplane_state_tokens
) {
212 snprintf(tmp
, ARRAY_SIZE(tmp
), "gl_ClipPlane%dMESA", plane
);
213 nir_variable
*var
= nir_variable_create(b
->shader
,
218 var
->num_state_slots
= 1;
219 var
->state_slots
= ralloc_array(var
, nir_state_slot
, 1);
220 memcpy(var
->state_slots
[0].tokens
,
221 clipplane_state_tokens
[plane
],
222 sizeof(var
->state_slots
[0].tokens
));
223 return nir_load_var(b
, var
);
225 return nir_load_user_clip_plane(b
, plane
);
230 lower_clip_outputs(nir_builder
*b
, nir_variable
*position
,
231 nir_variable
*clipvertex
, nir_variable
**out
,
232 unsigned ucp_enables
, bool use_vars
,
233 bool use_clipdist_array
,
234 const gl_state_index16 clipplane_state_tokens
[][STATE_LENGTH
])
236 nir_ssa_def
*clipdist
[MAX_CLIP_PLANES
];
240 cv
= nir_load_var(b
, clipvertex
? clipvertex
: position
);
243 clipvertex
->data
.mode
= nir_var_shader_temp
;
244 nir_fixup_deref_modes(b
->shader
);
248 cv
= find_output(b
->shader
, clipvertex
->data
.driver_location
);
251 cv
= find_output(b
->shader
, position
->data
.driver_location
);
255 for (int plane
= 0; plane
< MAX_CLIP_PLANES
; plane
++) {
256 if (ucp_enables
& (1 << plane
)) {
257 nir_ssa_def
*ucp
= get_ucp(b
, plane
, clipplane_state_tokens
);
259 /* calculate clipdist[plane] - dot(ucp, cv): */
260 clipdist
[plane
] = nir_fdot4(b
, ucp
, cv
);
262 /* 0.0 == don't-clip == disabled: */
263 clipdist
[plane
] = nir_imm_float(b
, 0.0);
265 if (use_clipdist_array
&& plane
< util_last_bit(ucp_enables
)) {
267 nir_deref_instr
*deref
;
268 deref
= nir_build_deref_array_imm(b
,
269 nir_build_deref_var(b
, out
[0]),
271 nir_store_deref(b
, deref
, clipdist
[plane
], 1);
275 if (!use_clipdist_array
) {
277 if (ucp_enables
& 0x0f)
278 nir_store_var(b
, out
[0], nir_vec(b
, clipdist
, 4), 0xf);
279 if (ucp_enables
& 0xf0)
280 nir_store_var(b
, out
[1], nir_vec(b
, &clipdist
[4], 4), 0xf);
282 if (ucp_enables
& 0x0f)
283 store_clipdist_output(b
, out
[0], &clipdist
[0]);
284 if (ucp_enables
& 0xf0)
285 store_clipdist_output(b
, out
[1], &clipdist
[4]);
294 /* ucp_enables is bitmask of enabled ucps. Actual ucp values are
295 * passed in to shader via user_clip_plane system-values
297 * If use_vars is true, the pass will use variable loads and stores instead
298 * of working with store_output intrinsics.
300 * If use_clipdist_array is true, the pass will use compact arrays for the
301 * clipdist output instead of two vec4s.
304 nir_lower_clip_vs(nir_shader
*shader
, unsigned ucp_enables
, bool use_vars
,
305 bool use_clipdist_array
,
306 const gl_state_index16 clipplane_state_tokens
[][STATE_LENGTH
])
308 nir_function_impl
*impl
= nir_shader_get_entrypoint(shader
);
310 nir_variable
*position
= NULL
;
311 nir_variable
*clipvertex
= NULL
;
312 nir_variable
*out
[2] = { NULL
};
317 nir_builder_init(&b
, impl
);
319 /* NIR should ensure that, even in case of loops/if-else, there
320 * should be only a single predecessor block to end_block, which
321 * makes the perfect place to insert the clipdist calculations.
323 * NOTE: in case of early returns, these would have to be lowered
324 * to jumps to end_block predecessor in a previous pass. Not sure
325 * if there is a good way to sanity check this, but for now the
326 * users of this pass don't support sub-routines.
328 assert(impl
->end_block
->predecessors
->entries
== 1);
329 b
.cursor
= nir_after_cf_list(&impl
->body
);
331 /* find clipvertex/position outputs */
332 if (!find_clipvertex_and_position_outputs(shader
, &clipvertex
, &position
))
335 /* insert CLIPDIST outputs */
336 create_clipdist_vars(shader
, out
, ucp_enables
, true,
339 lower_clip_outputs(&b
, position
, clipvertex
, out
, ucp_enables
, use_vars
,
340 use_clipdist_array
, clipplane_state_tokens
);
342 nir_metadata_preserve(impl
, nir_metadata_dominance
);
348 lower_clip_in_gs_block(nir_builder
*b
, nir_block
*block
, nir_variable
*position
,
349 nir_variable
*clipvertex
, nir_variable
**out
,
350 unsigned ucp_enables
, bool use_clipdist_array
,
351 const gl_state_index16 clipplane_state_tokens
[][STATE_LENGTH
])
353 nir_foreach_instr_safe(instr
, block
) {
354 if (instr
->type
!= nir_instr_type_intrinsic
)
357 nir_intrinsic_instr
*intrin
= nir_instr_as_intrinsic(instr
);
358 switch (intrin
->intrinsic
) {
359 case nir_intrinsic_emit_vertex_with_counter
:
360 case nir_intrinsic_emit_vertex
:
361 b
->cursor
= nir_before_instr(instr
);
362 lower_clip_outputs(b
, position
, clipvertex
, out
, ucp_enables
, true,
363 use_clipdist_array
, clipplane_state_tokens
);
366 /* not interesting; skip this */
377 nir_lower_clip_gs(nir_shader
*shader
, unsigned ucp_enables
,
378 bool use_clipdist_array
,
379 const gl_state_index16 clipplane_state_tokens
[][STATE_LENGTH
])
381 nir_function_impl
*impl
= nir_shader_get_entrypoint(shader
);
383 nir_variable
*position
= NULL
;
384 nir_variable
*clipvertex
= NULL
;
385 nir_variable
*out
[2] = { NULL
};
390 /* find clipvertex/position outputs */
391 if (!find_clipvertex_and_position_outputs(shader
, &clipvertex
, &position
))
394 /* insert CLIPDIST outputs */
395 create_clipdist_vars(shader
, out
, ucp_enables
, true,
398 nir_builder_init(&b
, impl
);
400 nir_foreach_block(block
, impl
)
401 lower_clip_in_gs_block(&b
, block
, position
, clipvertex
, out
,
402 ucp_enables
, use_clipdist_array
,
403 clipplane_state_tokens
);
405 nir_metadata_preserve(impl
, nir_metadata_dominance
);
415 lower_clip_fs(nir_function_impl
*impl
, unsigned ucp_enables
,
416 nir_variable
**in
, bool use_clipdist_array
)
418 nir_ssa_def
*clipdist
[MAX_CLIP_PLANES
];
421 nir_builder_init(&b
, impl
);
422 b
.cursor
= nir_before_cf_list(&impl
->body
);
424 if (!use_clipdist_array
) {
425 if (ucp_enables
& 0x0f)
426 load_clipdist_input(&b
, in
[0], 0, &clipdist
[0]);
427 if (ucp_enables
& 0xf0)
428 load_clipdist_input(&b
, in
[1], 0, &clipdist
[4]);
430 if (ucp_enables
& 0x0f)
431 load_clipdist_input(&b
, in
[0], 0, &clipdist
[0]);
432 if (ucp_enables
& 0xf0)
433 load_clipdist_input(&b
, in
[0], 1, &clipdist
[4]);
436 for (int plane
= 0; plane
< MAX_CLIP_PLANES
; plane
++) {
437 if (ucp_enables
& (1 << plane
)) {
438 nir_intrinsic_instr
*discard
;
441 cond
= nir_flt(&b
, clipdist
[plane
], nir_imm_float(&b
, 0.0));
443 discard
= nir_intrinsic_instr_create(b
.shader
,
444 nir_intrinsic_discard_if
);
445 discard
->src
[0] = nir_src_for_ssa(cond
);
446 nir_builder_instr_insert(&b
, &discard
->instr
);
448 b
.shader
->info
.fs
.uses_discard
= true;
452 nir_metadata_preserve(impl
, nir_metadata_dominance
);
456 fs_has_clip_dist_input_var(nir_shader
*shader
, nir_variable
**io_vars
,
457 unsigned *ucp_enables
)
459 assert(shader
->info
.stage
== MESA_SHADER_FRAGMENT
);
460 nir_foreach_shader_in_variable(var
, shader
) {
461 switch (var
->data
.location
) {
462 case VARYING_SLOT_CLIP_DIST0
:
463 assert(var
->data
.compact
);
465 *ucp_enables
&= (1 << glsl_get_length(var
->type
)) - 1;
474 /* insert conditional kill based on interpolated CLIPDIST
477 nir_lower_clip_fs(nir_shader
*shader
, unsigned ucp_enables
,
478 bool use_clipdist_array
)
480 nir_variable
*in
[2] = {0};
485 /* Fragment shaders can't read gl_ClipDistance[] in OpenGL so it will not
486 * have the variable defined, but Vulkan allows this, in which case the
487 * SPIR-V compiler would have already added it as a compact array.
489 if (!fs_has_clip_dist_input_var(shader
, in
, &ucp_enables
))
490 create_clipdist_vars(shader
, in
, ucp_enables
, false, use_clipdist_array
);
492 assert(use_clipdist_array
);
494 nir_foreach_function(function
, shader
) {
495 if (!strcmp(function
->name
, "main"))
496 lower_clip_fs(function
->impl
, ucp_enables
, in
, use_clipdist_array
);