From 30865756db480bf978d4c532d55aa5bac9b51c2a Mon Sep 17 00:00:00 2001 From: Arvind Yadav Date: Tue, 22 Feb 2022 21:32:42 +0530 Subject: [PATCH] nir: Add a lowering pass for point smoothing MIME-Version: 1.0 Content-Type: text/plain; charset=utf8 Content-Transfer-Encoding: 8bit When point smoothing is enabled then this lowering pass will modifies the alpha component of every write to fragment output. Anti-aliased points get rounded with respect to their radius instead of square. Reviewed-by: Marek Olšák Part-of: --- src/compiler/nir/meson.build | 1 + src/compiler/nir/nir.h | 4 +- src/compiler/nir/nir_lower_point_smooth.c | 94 +++++++++++++++++++++++++++++++ 3 files changed, 97 insertions(+), 2 deletions(-) create mode 100644 src/compiler/nir/nir_lower_point_smooth.c diff --git a/src/compiler/nir/meson.build b/src/compiler/nir/meson.build index 337f29a..86a09ca 100644 --- a/src/compiler/nir/meson.build +++ b/src/compiler/nir/meson.build @@ -187,6 +187,7 @@ files_libnir = files( 'nir_lower_pntc_ytransform.c', 'nir_lower_point_size.c', 'nir_lower_point_size_mov.c', + 'nir_lower_point_smooth.c', 'nir_lower_poly_line_smooth.c', 'nir_lower_printf.c', 'nir_lower_regs_to_ssa.c', diff --git a/src/compiler/nir/nir.h b/src/compiler/nir/nir.h index 34f2f21..9cc5efb 100644 --- a/src/compiler/nir/nir.h +++ b/src/compiler/nir/nir.h @@ -5680,8 +5680,8 @@ nir_opt_preamble(nir_shader *shader, nir_function_impl *nir_shader_get_preamble(nir_shader *shader); -bool -nir_lower_poly_line_smooth(nir_shader *shader, unsigned num_smooth_aa_sample); +bool nir_lower_point_smooth(nir_shader *shader); +bool nir_lower_poly_line_smooth(nir_shader *shader, unsigned num_smooth_aa_sample); #include "nir_inline_helpers.h" diff --git a/src/compiler/nir/nir_lower_point_smooth.c b/src/compiler/nir/nir_lower_point_smooth.c new file mode 100644 index 0000000..4cc37cc --- /dev/null +++ b/src/compiler/nir/nir_lower_point_smooth.c @@ -0,0 +1,94 @@ +/* + * Copyright © 2022 Advanced Micro Devices, Inc. + * + * Permission is hereby granted, free of charge, to any person obtaining a + * copy of this software and associated documentation files (the "Software"), + * to deal in the Software without restriction, including without limitation + * the rights to use, copy, modify, merge, publish, distribute, sublicense, + * and/or sell copies of the Software, and to permit persons to whom the + * Software is furnished to do so, subject to the following conditions: + * + * The above copyright notice and this permission notice (including the next + * paragraph) shall be included in all copies or substantial portions of the + * Software. + * + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR + * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, + * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL + * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER + * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING + * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS + * IN THE SOFTWARE. + */ + +#include "nir.h" +#include "nir_builder.h" +#include "nir_builtin_builder.h" + +/** + * This NIR lowers pass for point smoothing by modifying the alpha value of + * fragment outputs using the distance from the center of the point. + * Anti-aliased points get rounded with respect to their radius. + */ + +static bool +lower_point_smooth(nir_builder *b, nir_instr *instr, UNUSED void *_state) +{ + if (instr->type != nir_instr_type_intrinsic) + return false; + + nir_intrinsic_instr *intr = nir_instr_as_intrinsic(instr); + + if (intr->intrinsic != nir_intrinsic_store_output) + return false; + + int location = nir_intrinsic_io_semantics(intr).location; + if ((location != FRAG_RESULT_COLOR && location < FRAG_RESULT_DATA0) || + nir_intrinsic_src_type(intr) != nir_type_float32) + return false; + + assert(intr->src[0].is_ssa); + assert(intr->num_components == 4); + + b->cursor = nir_before_instr(&intr->instr); + + nir_ssa_def *coord = nir_build_load_point_coord_maybe_flipped(b); + + /* point_size = 1.0 / dFdx(gl_PointCoord.x); */ + nir_ssa_def *point_size = nir_frcp(b, nir_fddx(b, nir_channel(b, coord, 0))); + + /* radius = point_size * 0.5 */ + nir_ssa_def *radius = nir_fmul_imm(b, point_size, 0.5);; + + /** + * Compute the distance of point from centre + * distance = √ (x - 0.5)^2 + (y - 0.5)^2 + */ + nir_ssa_def *distance = nir_fast_distance(b, coord, + nir_imm_vec2(b, 0.5, 0.5)); + distance = nir_fmul(b, distance, point_size); + + /* alpha = min(max(radius - distance, 0.0), 1.0) */ + nir_ssa_def *coverage = nir_fsat(b, nir_fsub(b, radius, distance)); + + /* Discard fragments that are not covered by the point */ + nir_discard_if(b, nir_feq(b, nir_imm_float(b, 0.0f), coverage)); + + /* Write out the fragment color*vec4(1, 1, 1, coverage)*/ + nir_ssa_def *one = nir_imm_float(b, 1.0f); + nir_ssa_def *new_val = nir_fmul(b, nir_vec4(b, one, one, one, coverage), + intr->src[0].ssa); + nir_instr_rewrite_src(instr, &intr->src[0], nir_src_for_ssa(new_val)); + + return true; +} + +bool +nir_lower_point_smooth(nir_shader *shader) +{ + assert(shader->info.stage == MESA_SHADER_FRAGMENT); + return nir_shader_instructions_pass(shader, lower_point_smooth, + nir_metadata_loop_analysis | + nir_metadata_block_index | + nir_metadata_dominance, NULL); +} -- 2.7.4