xref: /aosp_15_r20/external/mesa3d/src/broadcom/compiler/v3d_nir_lower_line_smooth.c (revision 6104692788411f58d303aa86923a9ff6ecaded22)
1 /*
2  * Copyright © 2020 Raspberry Pi Ltd
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 
24 #include "compiler/v3d_compiler.h"
25 #include "compiler/nir/nir_builder.h"
26 #include <math.h>
27 
28 /**
29  * Lowers line smoothing by modifying the alpha component of fragment outputs
30  * using the distance from the center of the line.
31  */
32 
33 struct lower_line_smooth_state {
34         nir_shader *shader;
35         nir_variable *coverage;
36 };
37 
38 static void
lower_line_smooth_intrinsic(struct lower_line_smooth_state * state,nir_builder * b,nir_intrinsic_instr * intr)39 lower_line_smooth_intrinsic(struct lower_line_smooth_state *state,
40                             nir_builder *b,
41                             nir_intrinsic_instr *intr)
42 {
43         b->cursor = nir_before_instr(&intr->instr);
44 
45         nir_def *one = nir_imm_float(b, 1.0f);
46 
47         nir_def *coverage = nir_load_var(b, state->coverage);
48 
49         nir_def *new_val = nir_fmul(b, nir_vec4(b, one, one, one, coverage),
50                                         intr->src[0].ssa);
51 
52         nir_src_rewrite(&intr->src[0], new_val);
53 }
54 
55 static bool
lower_line_smooth_func(struct lower_line_smooth_state * state,nir_function_impl * impl)56 lower_line_smooth_func(struct lower_line_smooth_state *state,
57                        nir_function_impl *impl)
58 {
59         bool progress = false;
60 
61         nir_builder b = nir_builder_create(impl);
62 
63         nir_foreach_block(block, impl) {
64                 nir_foreach_instr_safe(instr, block) {
65                         if (instr->type != nir_instr_type_intrinsic)
66                                 continue;
67 
68                         nir_intrinsic_instr *intr =
69                                 nir_instr_as_intrinsic(instr);
70 
71                         if (intr->intrinsic != nir_intrinsic_store_output ||
72                             nir_intrinsic_base(intr) != 0 ||
73                             intr->num_components != 4)
74                                 continue;
75 
76                         lower_line_smooth_intrinsic(state, &b, intr);
77                         progress = true;
78                 }
79         }
80 
81         return progress;
82 }
83 
84 static void
initialise_coverage_var(struct lower_line_smooth_state * state,nir_function_impl * impl)85 initialise_coverage_var(struct lower_line_smooth_state *state,
86                         nir_function_impl *impl)
87 {
88         nir_builder b = nir_builder_at(nir_before_impl(impl));
89 
90         nir_def *line_width = nir_load_line_width(&b);
91 
92         nir_def *real_line_width = nir_load_aa_line_width(&b);
93 
94         /* According to the PRM, the line coord varies from 0.0 to 1.0 across
95          * the width of the line. But actually, when a perspective projection
96          * is used, it is also applied to the line coords, so the values end
97          * up being between [min_coord, 1], based on the Wc coordinate.  We
98          * need to re-map the values to be between [0.0, 1.0].
99          */
100         nir_def *line_coord = nir_load_line_coord(&b);
101         nir_def *wc = nir_load_fep_w_v3d(&b, 32);
102         nir_def *min_coord_val = nir_fsub(&b, nir_imm_float(&b, 1.0f), wc);
103         nir_def *normalized_line_coord = nir_fdiv(&b,
104                                                   nir_fsub(&b, line_coord, min_coord_val),
105                                                   nir_fsub_imm(&b, 1.0, min_coord_val));;
106 
107         /* fabs(line_coord - 0.5) * real_line_width */
108         nir_def *pixels_from_center =
109                 nir_fmul(&b, real_line_width,
110                          nir_fabs(&b, nir_fsub(&b, normalized_line_coord,
111                                                nir_imm_float(&b, 0.5f))));
112 
113         /* 0.5 - 1/√2 * (pixels_from_center - line_width * 0.5) */
114         nir_def *coverage =
115                 nir_fsub(&b,
116                          nir_imm_float(&b, 0.5f),
117                          nir_fmul(&b,
118                                   nir_imm_float(&b, 1.0f / M_SQRT2),
119                                   nir_fsub(&b, pixels_from_center,
120                                            nir_fmul_imm(&b,
121                                                         line_width,
122                                                         0.5f))));
123 
124         /* Discard fragments that aren’t covered at all by the line */
125         nir_def *outside = nir_fle_imm(&b, coverage, 0.0f);
126 
127         nir_discard_if(&b, outside);
128 
129         /* Clamp to at most 1.0. If it was less than 0.0 then the fragment will
130          * be discarded so we don’t need to handle that.
131          */
132         nir_def *clamped = nir_fmin(&b, coverage, nir_imm_float(&b, 1.0f));
133 
134         nir_store_var(&b, state->coverage, clamped, 0x1 /* writemask */);
135 }
136 
137 static nir_variable *
make_coverage_var(nir_shader * s)138 make_coverage_var(nir_shader *s)
139 {
140         nir_variable *var = nir_variable_create(s,
141                                                 nir_var_shader_temp,
142                                                 glsl_float_type(),
143                                                 "line_coverage");
144         var->data.how_declared = nir_var_hidden;
145 
146         return var;
147 }
148 
149 bool
v3d_nir_lower_line_smooth(nir_shader * s)150 v3d_nir_lower_line_smooth(nir_shader *s)
151 {
152         bool progress = false;
153 
154         assert(s->info.stage == MESA_SHADER_FRAGMENT);
155 
156         struct lower_line_smooth_state state = {
157                 .shader = s,
158                 .coverage = make_coverage_var(s),
159         };
160 
161         nir_foreach_function_with_impl(function, impl, s) {
162                 if (function->is_entrypoint)
163                         initialise_coverage_var(&state, impl);
164 
165                 progress |= lower_line_smooth_func(&state, impl);
166 
167                 if (progress) {
168                         nir_metadata_preserve(impl,
169                                               nir_metadata_control_flow);
170                 } else {
171                         nir_metadata_preserve(impl, nir_metadata_all);
172                 }
173         }
174 
175         return progress;
176 }
177