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_ssa_def *one = nir_imm_float(b, 1.0f);
46
47 nir_ssa_def *coverage = nir_load_var(b, state->coverage);
48
49 nir_ssa_def *new_val = nir_fmul(b, nir_vec4(b, one, one, one, coverage),
50 intr->src[0].ssa);
51
52 nir_instr_rewrite_src(&intr->instr,
53 &intr->src[0],
54 nir_src_for_ssa(new_val));
55 }
56
57 static bool
lower_line_smooth_func(struct lower_line_smooth_state * state,nir_function_impl * impl)58 lower_line_smooth_func(struct lower_line_smooth_state *state,
59 nir_function_impl *impl)
60 {
61 bool progress = false;
62
63 nir_builder b;
64
65 nir_builder_init(&b, impl);
66
67 nir_foreach_block(block, impl) {
68 nir_foreach_instr_safe(instr, block) {
69 if (instr->type != nir_instr_type_intrinsic)
70 continue;
71
72 nir_intrinsic_instr *intr =
73 nir_instr_as_intrinsic(instr);
74
75 if (intr->intrinsic != nir_intrinsic_store_output ||
76 nir_intrinsic_base(intr) != 0 ||
77 intr->num_components != 4 ||
78 !intr->src[0].is_ssa)
79 continue;
80
81 lower_line_smooth_intrinsic(state, &b, intr);
82 progress = true;
83 }
84 }
85
86 return progress;
87 }
88
89 static void
initialise_coverage_var(struct lower_line_smooth_state * state,nir_function_impl * impl)90 initialise_coverage_var(struct lower_line_smooth_state *state,
91 nir_function_impl *impl)
92 {
93 nir_builder b;
94
95 nir_builder_init(&b, impl);
96
97 b.cursor = nir_before_block(nir_start_block(impl));
98
99 nir_ssa_def *line_width = nir_load_line_width(&b);
100
101 nir_ssa_def *real_line_width = nir_load_aa_line_width(&b);
102
103 /* The line coord varies from 0.0 to 1.0 across the width of the line */
104 nir_ssa_def *line_coord = nir_load_line_coord(&b);
105
106 /* fabs(line_coord - 0.5) * real_line_width */
107 nir_ssa_def *pixels_from_center =
108 nir_fmul(&b, real_line_width,
109 nir_fabs(&b, nir_fsub(&b, line_coord,
110 nir_imm_float(&b, 0.5f))));
111
112 /* 0.5 - 1/√2 * (pixels_from_center - line_width * 0.5) */
113 nir_ssa_def *coverage =
114 nir_fsub(&b,
115 nir_imm_float(&b, 0.5f),
116 nir_fmul(&b,
117 nir_imm_float(&b, 1.0f / M_SQRT2),
118 nir_fsub(&b, pixels_from_center,
119 nir_fmul(&b,
120 line_width,
121 nir_imm_float(&b, 0.5f)))));
122
123 /* Discard fragments that aren’t covered at all by the line */
124 nir_ssa_def *outside = nir_fge(&b, nir_imm_float(&b, 0.0f), coverage);
125
126 nir_discard_if(&b, outside);
127
128 /* Clamp to at most 1.0. If it was less than 0.0 then the fragment will
129 * be discarded so we don’t need to handle that.
130 */
131 nir_ssa_def *clamped = nir_fmin(&b, coverage, nir_imm_float(&b, 1.0f));
132
133 nir_store_var(&b, state->coverage, clamped, 0x1 /* writemask */);
134 }
135
136 static nir_variable *
make_coverage_var(nir_shader * s)137 make_coverage_var(nir_shader *s)
138 {
139 nir_variable *var = nir_variable_create(s,
140 nir_var_shader_temp,
141 glsl_float_type(),
142 "line_coverage");
143 var->data.how_declared = nir_var_hidden;
144
145 return var;
146 }
147
148 bool
v3d_nir_lower_line_smooth(nir_shader * s)149 v3d_nir_lower_line_smooth(nir_shader *s)
150 {
151 bool progress = false;
152
153 assert(s->info.stage == MESA_SHADER_FRAGMENT);
154
155 struct lower_line_smooth_state state = {
156 .shader = s,
157 .coverage = make_coverage_var(s),
158 };
159
160 nir_foreach_function(function, s) {
161 if (function->is_entrypoint)
162 initialise_coverage_var(&state, function->impl);
163
164 progress |= lower_line_smooth_func(&state, function->impl);
165
166 if (progress) {
167 nir_metadata_preserve(function->impl,
168 nir_metadata_block_index |
169 nir_metadata_dominance);
170 } else {
171 nir_metadata_preserve(function->impl, nir_metadata_all);
172 }
173 }
174
175 return progress;
176 }
177