2 * Copyright © 2020 Mike Blumenkrantz
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 * Mike Blumenkrantz <michael.blumenkrantz@gmail.com>
28 #include "nir_builder.h"
32 * This pass uses the enabled clip planes from the rasterizer state to rewrite
33 * vertex shader store operations and store an undef to the corresponding gl_ClipDistance[n]
34 * value if the plane is disabled
37 /* recursively nest if/else blocks until we get to an array index,
38 * then overwrite it if that plane isn't enabled
41 recursive_if_chain(nir_builder
*b
, nir_deref_instr
*deref
, nir_ssa_def
*value
, unsigned clip_plane_enable
, nir_ssa_def
*index
, unsigned start
, unsigned end
)
43 if (start
== end
- 1) {
44 /* store the original value again if the clip plane is enabled */
45 if (clip_plane_enable
& (1 << start
))
46 nir_store_deref(b
, deref
, value
, 1 << start
);
48 nir_store_deref(b
, deref
, nir_ssa_undef(b
, 1, 32), 1 << start
);
52 unsigned mid
= start
+ (end
- start
) / 2;
53 nir_push_if(b
, nir_ilt(b
, index
, nir_imm_int(b
, mid
)));
54 recursive_if_chain(b
, deref
, value
, clip_plane_enable
, index
, start
, mid
);
55 nir_push_else(b
, NULL
);
56 recursive_if_chain(b
, deref
, value
, clip_plane_enable
, index
, mid
, end
);
60 /* vulkan (and some drivers) provides no concept of enabling clip planes through api,
61 * so we rewrite disabled clip planes to an undefined value in order to disable them
64 lower_clip_plane_store(nir_intrinsic_instr
*instr
, unsigned clip_plane_enable
, nir_builder
*b
)
69 if (instr
->intrinsic
!= nir_intrinsic_store_deref
)
72 nir_deref_instr
*deref
= nir_src_as_deref(instr
->src
[0]);
74 out
= nir_deref_instr_get_variable(deref
);
75 if ((out
->data
.location
!= VARYING_SLOT_CLIP_DIST0
) ||
76 out
->data
.mode
!= nir_var_shader_out
)
79 b
->cursor
= nir_after_instr(&instr
->instr
);
80 if (nir_src_is_const(deref
->arr
.index
)) {
81 /* storing using a constant index */
82 plane
= nir_src_as_uint(deref
->arr
.index
);
83 /* no need to make changes if the clip plane is enabled */
84 if (clip_plane_enable
& (1 << plane
))
87 nir_store_deref(b
, deref
, nir_ssa_undef(b
, 1, 32), 1 << plane
);
89 /* storing using a variable index */
90 nir_ssa_def
*index
= nir_ssa_for_src(b
, deref
->arr
.index
, 1);
91 unsigned length
= glsl_get_length(nir_deref_instr_parent(deref
)->type
);
93 recursive_if_chain(b
, deref
, instr
->src
[1].ssa
, clip_plane_enable
, index
, 0, length
);
95 nir_instr_remove(&instr
->instr
);
100 nir_lower_clip_disable(nir_shader
*shader
, unsigned clip_plane_enable
)
102 bool progress
= false;
104 nir_foreach_variable(var
, &shader
->outputs
) {
105 if (var
->data
.location
== VARYING_SLOT_CLIP_DIST0
) {
106 unsigned size
= glsl_get_length(var
->type
);
107 /* if currently-enabled planes match used planes then no-op */
108 if (clip_plane_enable
== (1u << size
) - 1)
113 nir_foreach_function(function
, shader
) {
114 if (function
->impl
) {
116 nir_builder_init(&builder
, function
->impl
);
117 nir_foreach_block(block
, function
->impl
) {
118 nir_foreach_instr_safe(instr
, block
) {
119 if (instr
->type
== nir_instr_type_intrinsic
)
120 progress
|= lower_clip_plane_store(nir_instr_as_intrinsic(instr
),
126 nir_metadata_preserve(function
->impl
, nir_metadata_block_index
| nir_metadata_dominance
);