xref: /aosp_15_r20/external/mesa3d/src/compiler/nir/nir_lower_point_smooth.c (revision 6104692788411f58d303aa86923a9ff6ecaded22)
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 #include "nir_builtin_builder.h"
27 
28 /**
29  * This NIR lowers pass for point smoothing by modifying the alpha value of
30  * fragment outputs using the distance from the center of the point.
31  * Anti-aliased points get rounded with respect to their radius.
32  */
33 
34 static bool
lower_point_smooth(nir_builder * b,nir_intrinsic_instr * intr,UNUSED void * _state)35 lower_point_smooth(nir_builder *b, nir_intrinsic_instr *intr,
36                    UNUSED void *_state)
37 {
38    if (intr->intrinsic != nir_intrinsic_store_output &&
39        intr->intrinsic != nir_intrinsic_store_deref)
40       return false;
41 
42    int out_src_idx;
43    if (intr->intrinsic == nir_intrinsic_store_output) {
44       int location = nir_intrinsic_io_semantics(intr).location;
45       if ((location != FRAG_RESULT_COLOR && location < FRAG_RESULT_DATA0) ||
46           nir_intrinsic_src_type(intr) != nir_type_float32)
47          return false;
48       out_src_idx = 0;
49    } else {
50       nir_variable *var = nir_intrinsic_get_var(intr, 0);
51       if ((var->data.location != FRAG_RESULT_COLOR &&
52            var->data.location < FRAG_RESULT_DATA0) ||
53           glsl_get_base_type(var->type) != GLSL_TYPE_FLOAT)
54          return false;
55       out_src_idx = 1;
56    }
57 
58    assert(intr->num_components == 4);
59 
60    b->cursor = nir_before_instr(&intr->instr);
61 
62    nir_def *coord = nir_load_point_coord_maybe_flipped(b);
63 
64    /* point_size = 1.0 / dFdx(gl_PointCoord.x); */
65    nir_def *point_size = nir_frcp(b, nir_ddx(b, nir_channel(b, coord, 0)));
66 
67    /* radius = point_size * 0.5 */
68    nir_def *radius = nir_fmul_imm(b, point_size, 0.5);
69    ;
70 
71    /**
72     * Compute the distance of point from centre
73     * distance = √ (x - 0.5)^2 + (y - 0.5)^2
74     */
75    nir_def *distance = nir_fast_distance(b, coord,
76                                          nir_imm_vec2(b, 0.5, 0.5));
77    distance = nir_fmul(b, distance, point_size);
78 
79    /* alpha = min(max(radius - distance, 0.0), 1.0) */
80    nir_def *coverage = nir_fsat(b, nir_fsub(b, radius, distance));
81 
82    /* Discard fragments that are not covered by the point */
83    nir_discard_if(b, nir_feq_imm(b, coverage, 0.0f));
84 
85    /* Write out the fragment color*vec4(1, 1, 1, coverage)*/
86    nir_def *one = nir_imm_float(b, 1.0f);
87    nir_def *new_val = nir_fmul(b, nir_vec4(b, one, one, one, coverage),
88                                intr->src[out_src_idx].ssa);
89    nir_src_rewrite(&intr->src[out_src_idx], new_val);
90 
91    return true;
92 }
93 
94 bool
nir_lower_point_smooth(nir_shader * shader)95 nir_lower_point_smooth(nir_shader *shader)
96 {
97    assert(shader->info.stage == MESA_SHADER_FRAGMENT);
98    return nir_shader_intrinsics_pass(shader, lower_point_smooth,
99                                        nir_metadata_loop_analysis |
100                                           nir_metadata_block_index |
101                                           nir_metadata_dominance,
102                                        NULL);
103 }
104