xref: /aosp_15_r20/external/mesa3d/src/compiler/nir/nir_lower_clip_disable.c (revision 6104692788411f58d303aa86923a9ff6ecaded22)
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 <[email protected]>
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       break;
132    default:
133       return false;
134    }
135    nir_io_semantics sem = nir_intrinsic_io_semantics(intr);
136    if (sem.location != VARYING_SLOT_CLIP_DIST0 &&
137        sem.location != VARYING_SLOT_CLIP_DIST1)
138       return false;
139 
140    b->cursor = nir_before_instr(&intr->instr);
141    nir_src *src_offset = nir_get_io_offset_src(intr);
142    int wrmask = nir_intrinsic_write_mask(intr);
143    int c = nir_intrinsic_component(intr);
144    nir_def *zero = nir_imm_int(b, 0);
145    if (nir_src_is_const(*src_offset)) {
146       int i = sem.location == VARYING_SLOT_CLIP_DIST1 ? 4 : 0;
147       i += nir_src_as_const_value(*src_offset)->u32 * 4;
148       i += c;
149       nir_def *val;
150       if (wrmask & 0x1) {
151          if (clip_plane_enable & BITFIELD_BIT(i))
152             return false;
153          val = zero;
154       } else
155          val = nir_undef(b, 1, 32);
156       nir_src_rewrite(&intr->src[0], val);
157    } else {
158       nir_def *first = clip_plane_enable & BITFIELD_BIT(c) ? intr->src[0].ssa : zero;
159       nir_def *second = clip_plane_enable & BITFIELD_BIT(c + 4) ? intr->src[0].ssa : zero;
160       nir_def *val = nir_bcsel(b, nir_ieq_imm(b, src_offset->ssa, 0), first, second);
161       nir_src_rewrite(&intr->src[0], val);
162    }
163    return true;
164 }
165 
166 bool
nir_lower_clip_disable(nir_shader * shader,unsigned clip_plane_enable)167 nir_lower_clip_disable(nir_shader *shader, unsigned clip_plane_enable)
168 {
169    /* if all user planes are enabled in API that are written in the array, always ignore;
170     * this explicitly covers the 2x vec4 case
171     */
172    if (clip_plane_enable == u_bit_consecutive(0, shader->info.clip_distance_array_size))
173       return false;
174 
175    return nir_shader_intrinsics_pass(shader,
176                                      shader->info.io_lowered ? lower_clip_plane_store_io : lower_clip_plane_store,
177                                        nir_metadata_control_flow,
178                                        &clip_plane_enable);
179 }
180