v3d: Retry with the fallback scheduler when RA fails
[mesa.git] / src / broadcom / compiler / v3d_nir_lower_line_smooth.c
1 /*
2 * Copyright © 2020 Raspberry Pi
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
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 void
58 lower_line_smooth_func(struct lower_line_smooth_state *state,
59 nir_function_impl *impl)
60 {
61 nir_builder b;
62
63 nir_builder_init(&b, impl);
64
65 nir_foreach_block(block, impl) {
66 nir_foreach_instr_safe(instr, block) {
67 if (instr->type != nir_instr_type_intrinsic)
68 continue;
69
70 nir_intrinsic_instr *intr =
71 nir_instr_as_intrinsic(instr);
72
73 if (intr->intrinsic != nir_intrinsic_store_output ||
74 nir_intrinsic_base(intr) != 0 ||
75 intr->num_components != 4 ||
76 !intr->src[0].is_ssa)
77 continue;
78
79 lower_line_smooth_intrinsic(state, &b, intr);
80 }
81 }
82 }
83
84 static void
85 initialise_coverage_var(struct lower_line_smooth_state *state,
86 nir_function_impl *impl)
87 {
88 nir_builder b;
89
90 nir_builder_init(&b, impl);
91
92 b.cursor = nir_before_block(nir_start_block(impl));
93
94 nir_ssa_def *line_width = nir_load_line_width(&b);
95
96 nir_ssa_def *real_line_width = nir_load_aa_line_width(&b);
97
98 /* The line coord varies from 0.0 to 1.0 across the width of the line */
99 nir_ssa_def *line_coord = nir_load_line_coord(&b);
100
101 /* fabs(line_coord - 0.5) * real_line_width */
102 nir_ssa_def *pixels_from_center =
103 nir_fmul(&b, real_line_width,
104 nir_fabs(&b, nir_fsub(&b, line_coord,
105 nir_imm_float(&b, 0.5f))));
106
107 /* 0.5 - 1/√2 * (pixels_from_center - line_width * 0.5) */
108 nir_ssa_def *coverage =
109 nir_fsub(&b,
110 nir_imm_float(&b, 0.5f),
111 nir_fmul(&b,
112 nir_imm_float(&b, 1.0f / M_SQRT2),
113 nir_fsub(&b, pixels_from_center,
114 nir_fmul(&b,
115 line_width,
116 nir_imm_float(&b, 0.5f)))));
117
118 /* Discard fragments that aren’t covered at all by the line */
119 nir_ssa_def *outside = nir_fge(&b, nir_imm_float(&b, 0.0f), coverage);
120
121 nir_intrinsic_instr *discard =
122 nir_intrinsic_instr_create(state->shader,
123 nir_intrinsic_discard_if);
124 discard->src[0] = nir_src_for_ssa(outside);
125 nir_builder_instr_insert(&b, &discard->instr);
126
127 /* Clamp to at most 1.0. If it was less than 0.0 then the fragment will
128 * be discarded so we don’t need to handle that.
129 */
130 nir_ssa_def *clamped = nir_fmin(&b, coverage, nir_imm_float(&b, 1.0f));
131
132 nir_store_var(&b, state->coverage, clamped, 0x1 /* writemask */);
133 }
134
135 static nir_variable *
136 make_coverage_var(nir_shader *s)
137 {
138 nir_variable *var = nir_variable_create(s,
139 nir_var_shader_temp,
140 glsl_float_type(),
141 "line_coverage");
142 var->data.how_declared = nir_var_hidden;
143
144 return var;
145 }
146
147 void
148 v3d_nir_lower_line_smooth(nir_shader *s)
149 {
150 assert(s->info.stage == MESA_SHADER_FRAGMENT);
151
152 struct lower_line_smooth_state state = {
153 .shader = s,
154 .coverage = make_coverage_var(s),
155 };
156
157 nir_foreach_function(function, s) {
158 if (function->is_entrypoint)
159 initialise_coverage_var(&state, function->impl);
160
161 lower_line_smooth_func(&state, function->impl);
162 }
163 }