Path: blob/21.2-virgl/src/compiler/nir/nir_lower_alpha_test.c
4545 views
/*1* Copyright © 2017 Broadcom2*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/**24* @file25*26* Implements GL alpha testing by comparing the output color's alpha to the27* alpha_ref intrinsic and emitting a discard based on it.28*29* The alpha_to_one value overrides the source alpha to 1.0 to implement30* GL_SAMPLE_ALPHA_TO_ONE, which applies before the alpha test (and would be31* rather silly to use with alpha test, but the spec permits).32*/3334#include "nir/nir.h"35#include "nir/nir_builder.h"3637void38nir_lower_alpha_test(nir_shader *shader, enum compare_func func,39bool alpha_to_one,40const gl_state_index16 *alpha_ref_state_tokens)41{42assert(alpha_ref_state_tokens);43assert(shader->info.stage == MESA_SHADER_FRAGMENT);4445nir_foreach_function(function, shader) {46nir_function_impl *impl = function->impl;47nir_builder b;48nir_builder_init(&b, impl);49b.cursor = nir_before_cf_list(&impl->body);5051nir_foreach_block(block, impl) {52nir_foreach_instr_safe(instr, block) {53if (instr->type == nir_instr_type_intrinsic) {54nir_intrinsic_instr *intr = nir_instr_as_intrinsic(instr);5556nir_variable *out = NULL;5758switch (intr->intrinsic) {59case nir_intrinsic_store_deref:60out = nir_deref_instr_get_variable(nir_src_as_deref(intr->src[0]));61break;62case nir_intrinsic_store_output:63/* already had i/o lowered.. lookup the matching output var: */64nir_foreach_shader_out_variable(var, shader) {65int drvloc = var->data.driver_location;66if (nir_intrinsic_base(intr) == drvloc) {67out = var;68break;69}70}71assume(out);72break;73default:74continue;75}7677if (out->data.mode != nir_var_shader_out)78continue;7980if (out->data.location != FRAG_RESULT_COLOR &&81out->data.location != FRAG_RESULT_DATA0)82continue;8384b.cursor = nir_before_instr(&intr->instr);8586nir_ssa_def *alpha;87if (alpha_to_one) {88alpha = nir_imm_float(&b, 1.0);89} else if (intr->intrinsic == nir_intrinsic_store_deref) {90alpha = nir_channel(&b, nir_ssa_for_src(&b, intr->src[1], 4),913);92} else {93alpha = nir_channel(&b, nir_ssa_for_src(&b, intr->src[0], 4),943);95}9697nir_variable *var = nir_variable_create(shader,98nir_var_uniform,99glsl_float_type(),100"gl_AlphaRefMESA");101var->num_state_slots = 1;102var->state_slots = ralloc_array(var, nir_state_slot, 1);103memcpy(var->state_slots[0].tokens,104alpha_ref_state_tokens,105sizeof(var->state_slots[0].tokens));106nir_ssa_def *alpha_ref = nir_load_var(&b, var);107108nir_ssa_def *condition =109nir_compare_func(&b, func, alpha, alpha_ref);110111nir_discard_if(&b, nir_inot(&b, condition));112shader->info.fs.uses_discard = true;113}114}115}116117nir_metadata_preserve(impl, nir_metadata_block_index |118nir_metadata_dominance);119}120}121122123