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 /* The line coord varies from 0.0 to 1.0 across the width of the line */
95 nir_def *line_coord = nir_load_line_coord(&b);
96
97 /* fabs(line_coord - 0.5) * real_line_width */
98 nir_def *pixels_from_center =
99 nir_fmul(&b, real_line_width,
100 nir_fabs(&b, nir_fsub(&b, line_coord,
101 nir_imm_float(&b, 0.5f))));
102
103 /* 0.5 - 1/√2 * (pixels_from_center - line_width * 0.5) */
104 nir_def *coverage =
105 nir_fsub(&b,
106 nir_imm_float(&b, 0.5f),
107 nir_fmul(&b,
108 nir_imm_float(&b, 1.0f / M_SQRT2),
109 nir_fsub(&b, pixels_from_center,
110 nir_fmul_imm(&b,
111 line_width,
112 0.5f))));
113
114 /* Discard fragments that aren’t covered at all by the line */
115 nir_def *outside = nir_fle_imm(&b, coverage, 0.0f);
116
117 nir_discard_if(&b, outside);
118
119 /* Clamp to at most 1.0. If it was less than 0.0 then the fragment will
120 * be discarded so we don’t need to handle that.
121 */
122 nir_def *clamped = nir_fmin(&b, coverage, nir_imm_float(&b, 1.0f));
123
124 nir_store_var(&b, state->coverage, clamped, 0x1 /* writemask */);
125 }
126
127 static nir_variable *
make_coverage_var(nir_shader * s)128 make_coverage_var(nir_shader *s)
129 {
130 nir_variable *var = nir_variable_create(s,
131 nir_var_shader_temp,
132 glsl_float_type(),
133 "line_coverage");
134 var->data.how_declared = nir_var_hidden;
135
136 return var;
137 }
138
139 bool
v3d_nir_lower_line_smooth(nir_shader * s)140 v3d_nir_lower_line_smooth(nir_shader *s)
141 {
142 bool progress = false;
143
144 assert(s->info.stage == MESA_SHADER_FRAGMENT);
145
146 struct lower_line_smooth_state state = {
147 .shader = s,
148 .coverage = make_coverage_var(s),
149 };
150
151 nir_foreach_function_with_impl(function, impl, s) {
152 if (function->is_entrypoint)
153 initialise_coverage_var(&state, impl);
154
155 progress |= lower_line_smooth_func(&state, impl);
156
157 if (progress) {
158 nir_metadata_preserve(impl,
159 nir_metadata_block_index |
160 nir_metadata_dominance);
161 } else {
162 nir_metadata_preserve(impl, nir_metadata_all);
163 }
164 }
165
166 return progress;
167 }
168