Path: blob/21.2-virgl/src/broadcom/compiler/v3d_nir_lower_line_smooth.c
4564 views
/*1* Copyright © 2020 Raspberry Pi2*3* Permission is hereby granted, free of charge, to any person obtaining a4* copy of this software and associated documentation files (the "Software"),5* to deal in the Software without restriction, including without limitation6* the rights to use, copy, modify, merge, publish, distribute, sublicense,7* and/or sell copies of the Software, and to permit persons to whom the8* Software is furnished to do so, subject to the following conditions:9*10* The above copyright notice and this permission notice (including the next11* paragraph) shall be included in all copies or substantial portions of the12* Software.13*14* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR15* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,16* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL17* THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER18* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING19* FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS20* IN THE SOFTWARE.21*/2223#include "compiler/v3d_compiler.h"24#include "compiler/nir/nir_builder.h"25#include <math.h>2627/**28* Lowers line smoothing by modifying the alpha component of fragment outputs29* using the distance from the center of the line.30*/3132struct lower_line_smooth_state {33nir_shader *shader;34nir_variable *coverage;35};3637static void38lower_line_smooth_intrinsic(struct lower_line_smooth_state *state,39nir_builder *b,40nir_intrinsic_instr *intr)41{42b->cursor = nir_before_instr(&intr->instr);4344nir_ssa_def *one = nir_imm_float(b, 1.0f);4546nir_ssa_def *coverage = nir_load_var(b, state->coverage);4748nir_ssa_def *new_val = nir_fmul(b, nir_vec4(b, one, one, one, coverage),49intr->src[0].ssa);5051nir_instr_rewrite_src(&intr->instr,52&intr->src[0],53nir_src_for_ssa(new_val));54}5556static void57lower_line_smooth_func(struct lower_line_smooth_state *state,58nir_function_impl *impl)59{60nir_builder b;6162nir_builder_init(&b, impl);6364nir_foreach_block(block, impl) {65nir_foreach_instr_safe(instr, block) {66if (instr->type != nir_instr_type_intrinsic)67continue;6869nir_intrinsic_instr *intr =70nir_instr_as_intrinsic(instr);7172if (intr->intrinsic != nir_intrinsic_store_output ||73nir_intrinsic_base(intr) != 0 ||74intr->num_components != 4 ||75!intr->src[0].is_ssa)76continue;7778lower_line_smooth_intrinsic(state, &b, intr);79}80}81}8283static void84initialise_coverage_var(struct lower_line_smooth_state *state,85nir_function_impl *impl)86{87nir_builder b;8889nir_builder_init(&b, impl);9091b.cursor = nir_before_block(nir_start_block(impl));9293nir_ssa_def *line_width = nir_load_line_width(&b);9495nir_ssa_def *real_line_width = nir_load_aa_line_width(&b);9697/* The line coord varies from 0.0 to 1.0 across the width of the line */98nir_ssa_def *line_coord = nir_load_line_coord(&b);99100/* fabs(line_coord - 0.5) * real_line_width */101nir_ssa_def *pixels_from_center =102nir_fmul(&b, real_line_width,103nir_fabs(&b, nir_fsub(&b, line_coord,104nir_imm_float(&b, 0.5f))));105106/* 0.5 - 1/√2 * (pixels_from_center - line_width * 0.5) */107nir_ssa_def *coverage =108nir_fsub(&b,109nir_imm_float(&b, 0.5f),110nir_fmul(&b,111nir_imm_float(&b, 1.0f / M_SQRT2),112nir_fsub(&b, pixels_from_center,113nir_fmul(&b,114line_width,115nir_imm_float(&b, 0.5f)))));116117/* Discard fragments that aren’t covered at all by the line */118nir_ssa_def *outside = nir_fge(&b, nir_imm_float(&b, 0.0f), coverage);119120nir_discard_if(&b, outside);121122/* Clamp to at most 1.0. If it was less than 0.0 then the fragment will123* be discarded so we don’t need to handle that.124*/125nir_ssa_def *clamped = nir_fmin(&b, coverage, nir_imm_float(&b, 1.0f));126127nir_store_var(&b, state->coverage, clamped, 0x1 /* writemask */);128}129130static nir_variable *131make_coverage_var(nir_shader *s)132{133nir_variable *var = nir_variable_create(s,134nir_var_shader_temp,135glsl_float_type(),136"line_coverage");137var->data.how_declared = nir_var_hidden;138139return var;140}141142void143v3d_nir_lower_line_smooth(nir_shader *s)144{145assert(s->info.stage == MESA_SHADER_FRAGMENT);146147struct lower_line_smooth_state state = {148.shader = s,149.coverage = make_coverage_var(s),150};151152nir_foreach_function(function, s) {153if (function->is_entrypoint)154initialise_coverage_var(&state, function->impl);155156lower_line_smooth_func(&state, function->impl);157}158}159160161