2 * Copyright © 2020 Raspberry Pi
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:
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
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
24 #include "compiler/v3d_compiler.h"
25 #include "compiler/nir/nir_builder.h"
29 * Lowers line smoothing by modifying the alpha component of fragment outputs
30 * using the distance from the center of the line.
33 struct lower_line_smooth_state
{
35 nir_variable
*coverage
;
39 lower_line_smooth_intrinsic(struct lower_line_smooth_state
*state
,
41 nir_intrinsic_instr
*intr
)
43 b
->cursor
= nir_before_instr(&intr
->instr
);
45 nir_ssa_def
*one
= nir_imm_float(b
, 1.0f
);
47 nir_ssa_def
*coverage
= nir_load_var(b
, state
->coverage
);
49 nir_ssa_def
*new_val
= nir_fmul(b
, nir_vec4(b
, one
, one
, one
, coverage
),
52 nir_instr_rewrite_src(&intr
->instr
,
54 nir_src_for_ssa(new_val
));
58 lower_line_smooth_func(struct lower_line_smooth_state
*state
,
59 nir_function_impl
*impl
)
63 nir_builder_init(&b
, impl
);
65 nir_foreach_block(block
, impl
) {
66 nir_foreach_instr_safe(instr
, block
) {
67 if (instr
->type
!= nir_instr_type_intrinsic
)
70 nir_intrinsic_instr
*intr
=
71 nir_instr_as_intrinsic(instr
);
73 if (intr
->intrinsic
!= nir_intrinsic_store_output
||
74 nir_intrinsic_base(intr
) != 0 ||
75 intr
->num_components
!= 4 ||
79 lower_line_smooth_intrinsic(state
, &b
, intr
);
85 initialise_coverage_var(struct lower_line_smooth_state
*state
,
86 nir_function_impl
*impl
)
90 nir_builder_init(&b
, impl
);
92 b
.cursor
= nir_before_block(nir_start_block(impl
));
94 nir_ssa_def
*line_width
= nir_load_line_width(&b
);
96 nir_ssa_def
*real_line_width
= nir_load_aa_line_width(&b
);
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
);
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
))));
107 /* 0.5 - 1/√2 * (pixels_from_center - line_width * 0.5) */
108 nir_ssa_def
*coverage
=
110 nir_imm_float(&b
, 0.5f
),
112 nir_imm_float(&b
, 1.0f
/ M_SQRT2
),
113 nir_fsub(&b
, pixels_from_center
,
116 nir_imm_float(&b
, 0.5f
)))));
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
);
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
);
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.
130 nir_ssa_def
*clamped
= nir_fmin(&b
, coverage
, nir_imm_float(&b
, 1.0f
));
132 nir_store_var(&b
, state
->coverage
, clamped
, 0x1 /* writemask */);
135 static nir_variable
*
136 make_coverage_var(nir_shader
*s
)
138 nir_variable
*var
= nir_variable_create(s
,
142 var
->data
.how_declared
= nir_var_hidden
;
148 v3d_nir_lower_line_smooth(nir_shader
*s
)
150 assert(s
->info
.stage
== MESA_SHADER_FRAGMENT
);
152 struct lower_line_smooth_state state
= {
154 .coverage
= make_coverage_var(s
),
157 nir_foreach_function(function
, s
) {
158 if (function
->is_entrypoint
)
159 initialise_coverage_var(&state
, function
->impl
);
161 lower_line_smooth_func(&state
, function
->impl
);