1 /*
2 * Copyright © 2022 Advanced Micro Devices, Inc.
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 "nir.h"
25 #include "nir_builder.h"
26
27 /**
28 * This NIR lowers pass for polygon and line smoothing by modifying the alpha
29 * value of fragment outputs using the sample coverage mask.
30 */
31
32 static bool
lower_polylinesmooth(nir_builder * b,nir_instr * instr,void * data)33 lower_polylinesmooth(nir_builder *b, nir_instr *instr, void *data)
34 {
35 unsigned *num_smooth_aa_sample = data;
36
37 if (instr->type != nir_instr_type_intrinsic)
38 return false;
39
40 nir_intrinsic_instr *intr = nir_instr_as_intrinsic(instr);
41
42 if (intr->intrinsic != nir_intrinsic_store_output)
43 return false;
44
45 int location = nir_intrinsic_io_semantics(intr).location;
46 if ((location != FRAG_RESULT_COLOR && location < FRAG_RESULT_DATA0) ||
47 nir_intrinsic_src_type(intr) != nir_type_float32)
48 return false;
49
50 assert(intr->num_components == 4);
51
52 b->cursor = nir_before_instr(&intr->instr);
53
54 nir_def *res1, *res2;
55
56 nir_if *if_enabled = nir_push_if(b, nir_load_poly_line_smooth_enabled(b));
57 {
58 nir_def *coverage = nir_load_sample_mask_in(b);
59
60 /* coverage = (coverage) / SI_NUM_SMOOTH_AA_SAMPLES */
61 coverage = nir_bit_count(b, coverage);
62 coverage = nir_u2f32(b, coverage);
63 coverage = nir_fmul_imm(b, coverage, 1.0 / *num_smooth_aa_sample);
64
65 /* Write out the fragment color*vec4(1, 1, 1, alpha) */
66 nir_def *one = nir_imm_float(b, 1.0f);
67 res1 = nir_fmul(b, nir_vec4(b, one, one, one, coverage), intr->src[0].ssa);
68 }
69 nir_push_else(b, if_enabled);
70 {
71 res2 = intr->src[0].ssa;
72 }
73 nir_pop_if(b, if_enabled);
74
75 nir_def *new_dest = nir_if_phi(b, res1, res2);
76
77 nir_src_rewrite(&intr->src[0], new_dest);
78 return true;
79 }
80
81 bool
nir_lower_poly_line_smooth(nir_shader * shader,unsigned num_smooth_aa_sample)82 nir_lower_poly_line_smooth(nir_shader *shader, unsigned num_smooth_aa_sample)
83 {
84 assert(shader->info.stage == MESA_SHADER_FRAGMENT);
85 return nir_shader_instructions_pass(shader, lower_polylinesmooth, 0,
86 &num_smooth_aa_sample);
87 }
88