1 /*
2 * Copyright © 2020 Mike Blumenkrantz
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 * Mike Blumenkrantz <michael.blumenkrantz@gmail.com>
25 */
26
27 #include "nir.h"
28 #include "nir_builder.h"
29
30 /**
31 * This pass uses the enabled clip planes from the rasterizer state to rewrite
32 * vertex shader store operations and store a 0 to the corresponding gl_ClipDistance[n]
33 * value if the plane is disabled
34 */
35
36 /* recursively nest if/else blocks until we get to an array index,
37 * then overwrite it if that plane isn't enabled
38 */
39 static void
recursive_if_chain(nir_builder * b,nir_deref_instr * deref,nir_def * value,unsigned clip_plane_enable,nir_def * index,unsigned start,unsigned end)40 recursive_if_chain(nir_builder *b, nir_deref_instr *deref, nir_def *value, unsigned clip_plane_enable, nir_def *index, unsigned start, unsigned end)
41 {
42 if (start == end - 1) {
43 /* store the original value again if the clip plane is enabled */
44 if (clip_plane_enable & (1 << start))
45 nir_store_deref(b, deref, value, 1 << start);
46 else
47 nir_store_deref(b, deref, nir_imm_int(b, 0), 1 << start);
48 return;
49 }
50
51 unsigned mid = start + (end - start) / 2;
52 nir_push_if(b, nir_ilt_imm(b, index, mid));
53 recursive_if_chain(b, deref, value, clip_plane_enable, index, start, mid);
54 nir_push_else(b, NULL);
55 recursive_if_chain(b, deref, value, clip_plane_enable, index, mid, end);
56 nir_pop_if(b, NULL);
57 }
58
59 /* vulkan (and some drivers) provides no concept of enabling clip planes through api,
60 * so we rewrite disabled clip planes to a zero value in order to disable them
61 */
62 static bool
lower_clip_plane_store(nir_builder * b,nir_intrinsic_instr * instr,void * cb_data)63 lower_clip_plane_store(nir_builder *b, nir_intrinsic_instr *instr,
64 void *cb_data)
65 {
66 unsigned clip_plane_enable = *(unsigned *)cb_data;
67 nir_variable *out;
68 unsigned plane;
69
70 if (instr->intrinsic != nir_intrinsic_store_deref)
71 return false;
72
73 nir_deref_instr *deref = nir_src_as_deref(instr->src[0]);
74
75 out = nir_deref_instr_get_variable(deref);
76 if ((out->data.location != VARYING_SLOT_CLIP_DIST0 &&
77 out->data.location != VARYING_SLOT_CLIP_DIST1) ||
78 out->data.mode != nir_var_shader_out)
79 return false;
80
81 b->cursor = nir_after_instr(&instr->instr);
82 if (deref->deref_type == nir_deref_type_var) {
83 int wrmask = nir_intrinsic_write_mask(instr);
84
85 nir_def *components[4];
86 int start = out->data.location == VARYING_SLOT_CLIP_DIST1 ? 4 : 0;
87 /* rewrite components as zeroes for planes that aren't enabled */
88 for (int i = 0; i < 4; i++) {
89 if (wrmask & (1 << i)) {
90 if (!(clip_plane_enable & (1 << (start + i))))
91 components[i] = nir_imm_int(b, 0);
92 else
93 components[i] = nir_channel(b, instr->src[1].ssa, i);
94 } else
95 components[i] = nir_undef(b, 1, 32);
96 }
97 nir_store_deref(b, deref, nir_vec(b, components, instr->num_components), wrmask);
98 } else if (nir_src_is_const(deref->arr.index)) {
99 /* storing using a constant index */
100 plane = nir_src_as_uint(deref->arr.index);
101 /* no need to make changes if the clip plane is enabled */
102 if (clip_plane_enable & (1 << plane))
103 return false;
104
105 assert(nir_intrinsic_write_mask(instr) == 1);
106 nir_store_deref(b, deref, nir_imm_int(b, 0), 1);
107 } else {
108 /* storing using a variable index */
109 nir_def *index = deref->arr.index.ssa;
110 unsigned length = glsl_get_length(nir_deref_instr_parent(deref)->type);
111
112 recursive_if_chain(b, deref, instr->src[1].ssa, clip_plane_enable, index, 0, length);
113 }
114 nir_instr_remove(&instr->instr);
115 return true;
116 }
117
118 /* vulkan (and some drivers) provides no concept of enabling clip planes through api,
119 * so we rewrite disabled clip planes to a zero value in order to disable them
120 */
121 static bool
lower_clip_plane_store_io(nir_builder * b,nir_intrinsic_instr * intr,void * cb_data)122 lower_clip_plane_store_io(nir_builder *b, nir_intrinsic_instr *intr,
123 void *cb_data)
124 {
125 unsigned clip_plane_enable = *(unsigned *)cb_data;
126
127 switch (intr->intrinsic) {
128 case nir_intrinsic_store_output:
129 case nir_intrinsic_store_per_primitive_output:
130 case nir_intrinsic_store_per_vertex_output:
131 case nir_intrinsic_store_per_view_output:
132 break;
133 default:
134 return false;
135 }
136
137 nir_io_semantics sem = nir_intrinsic_io_semantics(intr);
138 if (sem.location != VARYING_SLOT_CLIP_DIST0 &&
139 sem.location != VARYING_SLOT_CLIP_DIST1)
140 return false;
141
142 b->cursor = nir_before_instr(&intr->instr);
143 nir_src *src_offset = nir_get_io_offset_src(intr);
144 unsigned wrmask = nir_intrinsic_write_mask(intr);
145 unsigned base_index = (sem.location == VARYING_SLOT_CLIP_DIST1 ? 4 : 0) +
146 nir_intrinsic_component(intr);
147 nir_def *zero = nir_imm_int(b, 0);
148
149 if (nir_src_is_const(*src_offset)) {
150 base_index += nir_src_as_uint(*src_offset) * 4;
151
152 u_foreach_bit(bit, wrmask) {
153 if (!(clip_plane_enable & BITFIELD_BIT(base_index + bit))) {
154 nir_def *vec = nir_vector_insert_imm(b, intr->src[0].ssa, zero, bit);
155 nir_src_rewrite(&intr->src[0], vec);
156 }
157 }
158 } else {
159 u_foreach_bit(bit, wrmask) {
160 unsigned index = base_index + bit;
161 nir_def *chan = nir_channel(b, intr->src[0].ssa, bit);
162 nir_def *dist0 = clip_plane_enable & BITFIELD_BIT(index) ? chan : zero;
163 nir_def *dist1 = clip_plane_enable & BITFIELD_BIT(index + 4) ? chan : zero;
164 chan = nir_bcsel(b, nir_ieq_imm(b, src_offset->ssa, 0), dist0, dist1);
165 nir_def *vec = nir_vector_insert_imm(b, intr->src[0].ssa, chan, bit);
166 nir_src_rewrite(&intr->src[0], vec);
167 }
168 }
169 return true;
170 }
171
172 bool
nir_lower_clip_disable(nir_shader * shader,unsigned clip_plane_enable)173 nir_lower_clip_disable(nir_shader *shader, unsigned clip_plane_enable)
174 {
175 /* if all user planes are enabled in API that are written in the array, always ignore;
176 * this explicitly covers the 2x vec4 case
177 */
178 if (clip_plane_enable == u_bit_consecutive(0, shader->info.clip_distance_array_size))
179 return false;
180
181 return nir_shader_intrinsics_pass(shader,
182 shader->info.io_lowered ? lower_clip_plane_store_io : lower_clip_plane_store,
183 nir_metadata_control_flow,
184 &clip_plane_enable);
185 }
186