1bf215546Sopenharmony_ci/*
2bf215546Sopenharmony_ci * Copyright © 2017 Broadcom
3bf215546Sopenharmony_ci *
4bf215546Sopenharmony_ci * Permission is hereby granted, free of charge, to any person obtaining a
5bf215546Sopenharmony_ci * copy of this software and associated documentation files (the "Software"),
6bf215546Sopenharmony_ci * to deal in the Software without restriction, including without limitation
7bf215546Sopenharmony_ci * the rights to use, copy, modify, merge, publish, distribute, sublicense,
8bf215546Sopenharmony_ci * and/or sell copies of the Software, and to permit persons to whom the
9bf215546Sopenharmony_ci * Software is furnished to do so, subject to the following conditions:
10bf215546Sopenharmony_ci *
11bf215546Sopenharmony_ci * The above copyright notice and this permission notice (including the next
12bf215546Sopenharmony_ci * paragraph) shall be included in all copies or substantial portions of the
13bf215546Sopenharmony_ci * Software.
14bf215546Sopenharmony_ci *
15bf215546Sopenharmony_ci * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16bf215546Sopenharmony_ci * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17bf215546Sopenharmony_ci * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.  IN NO EVENT SHALL
18bf215546Sopenharmony_ci * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19bf215546Sopenharmony_ci * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
20bf215546Sopenharmony_ci * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
21bf215546Sopenharmony_ci * IN THE SOFTWARE.
22bf215546Sopenharmony_ci */
23bf215546Sopenharmony_ci
24bf215546Sopenharmony_ci/**
25bf215546Sopenharmony_ci * @file
26bf215546Sopenharmony_ci *
27bf215546Sopenharmony_ci * Implements GL alpha testing by comparing the output color's alpha to the
28bf215546Sopenharmony_ci * alpha_ref intrinsic and emitting a discard based on it.
29bf215546Sopenharmony_ci *
30bf215546Sopenharmony_ci * The alpha_to_one value overrides the source alpha to 1.0 to implement
31bf215546Sopenharmony_ci * GL_SAMPLE_ALPHA_TO_ONE, which applies before the alpha test (and would be
32bf215546Sopenharmony_ci * rather silly to use with alpha test, but the spec permits).
33bf215546Sopenharmony_ci */
34bf215546Sopenharmony_ci
35bf215546Sopenharmony_ci#include "nir/nir.h"
36bf215546Sopenharmony_ci#include "nir/nir_builder.h"
37bf215546Sopenharmony_ci
38bf215546Sopenharmony_civoid
39bf215546Sopenharmony_cinir_lower_alpha_test(nir_shader *shader, enum compare_func func,
40bf215546Sopenharmony_ci                     bool alpha_to_one,
41bf215546Sopenharmony_ci                     const gl_state_index16 *alpha_ref_state_tokens)
42bf215546Sopenharmony_ci{
43bf215546Sopenharmony_ci   assert(alpha_ref_state_tokens);
44bf215546Sopenharmony_ci   assert(shader->info.stage == MESA_SHADER_FRAGMENT);
45bf215546Sopenharmony_ci
46bf215546Sopenharmony_ci   nir_foreach_function(function, shader) {
47bf215546Sopenharmony_ci      nir_function_impl *impl = function->impl;
48bf215546Sopenharmony_ci      nir_builder b;
49bf215546Sopenharmony_ci      nir_builder_init(&b, impl);
50bf215546Sopenharmony_ci      b.cursor = nir_before_cf_list(&impl->body);
51bf215546Sopenharmony_ci
52bf215546Sopenharmony_ci      nir_foreach_block(block, impl) {
53bf215546Sopenharmony_ci         nir_foreach_instr_safe(instr, block) {
54bf215546Sopenharmony_ci            if (instr->type == nir_instr_type_intrinsic) {
55bf215546Sopenharmony_ci               nir_intrinsic_instr *intr = nir_instr_as_intrinsic(instr);
56bf215546Sopenharmony_ci
57bf215546Sopenharmony_ci               nir_variable *out = NULL;
58bf215546Sopenharmony_ci
59bf215546Sopenharmony_ci               switch (intr->intrinsic) {
60bf215546Sopenharmony_ci               case nir_intrinsic_store_deref:
61bf215546Sopenharmony_ci                  out = nir_deref_instr_get_variable(nir_src_as_deref(intr->src[0]));
62bf215546Sopenharmony_ci                  break;
63bf215546Sopenharmony_ci               case nir_intrinsic_store_output:
64bf215546Sopenharmony_ci                  /* already had i/o lowered.. lookup the matching output var: */
65bf215546Sopenharmony_ci                  nir_foreach_shader_out_variable(var, shader) {
66bf215546Sopenharmony_ci                     int drvloc = var->data.driver_location;
67bf215546Sopenharmony_ci                     if (nir_intrinsic_base(intr) == drvloc) {
68bf215546Sopenharmony_ci                        out = var;
69bf215546Sopenharmony_ci                        break;
70bf215546Sopenharmony_ci                     }
71bf215546Sopenharmony_ci                  }
72bf215546Sopenharmony_ci                  assume(out);
73bf215546Sopenharmony_ci                  break;
74bf215546Sopenharmony_ci               default:
75bf215546Sopenharmony_ci                  continue;
76bf215546Sopenharmony_ci               }
77bf215546Sopenharmony_ci
78bf215546Sopenharmony_ci               if (out->data.mode != nir_var_shader_out)
79bf215546Sopenharmony_ci                  continue;
80bf215546Sopenharmony_ci
81bf215546Sopenharmony_ci               if (out->data.location != FRAG_RESULT_COLOR &&
82bf215546Sopenharmony_ci                   out->data.location != FRAG_RESULT_DATA0)
83bf215546Sopenharmony_ci                  continue;
84bf215546Sopenharmony_ci
85bf215546Sopenharmony_ci               b.cursor = nir_before_instr(&intr->instr);
86bf215546Sopenharmony_ci
87bf215546Sopenharmony_ci               nir_ssa_def *alpha;
88bf215546Sopenharmony_ci               if (alpha_to_one) {
89bf215546Sopenharmony_ci                  alpha = nir_imm_float(&b, 1.0);
90bf215546Sopenharmony_ci               } else if (intr->intrinsic == nir_intrinsic_store_deref) {
91bf215546Sopenharmony_ci                  alpha = nir_channel(&b, nir_ssa_for_src(&b, intr->src[1], 4),
92bf215546Sopenharmony_ci                                      3);
93bf215546Sopenharmony_ci               } else {
94bf215546Sopenharmony_ci                  alpha = nir_channel(&b, nir_ssa_for_src(&b, intr->src[0], 4),
95bf215546Sopenharmony_ci                                      3);
96bf215546Sopenharmony_ci               }
97bf215546Sopenharmony_ci
98bf215546Sopenharmony_ci               nir_variable *var = nir_variable_create(shader,
99bf215546Sopenharmony_ci                                                       nir_var_uniform,
100bf215546Sopenharmony_ci                                                       glsl_float_type(),
101bf215546Sopenharmony_ci                                                       "gl_AlphaRefMESA");
102bf215546Sopenharmony_ci               var->num_state_slots = 1;
103bf215546Sopenharmony_ci               var->state_slots = ralloc_array(var, nir_state_slot, 1);
104bf215546Sopenharmony_ci               memcpy(var->state_slots[0].tokens,
105bf215546Sopenharmony_ci                      alpha_ref_state_tokens,
106bf215546Sopenharmony_ci                      sizeof(var->state_slots[0].tokens));
107bf215546Sopenharmony_ci               nir_ssa_def *alpha_ref = nir_load_var(&b, var);
108bf215546Sopenharmony_ci
109bf215546Sopenharmony_ci               nir_ssa_def *condition =
110bf215546Sopenharmony_ci                  nir_compare_func(&b, func, alpha, alpha_ref);
111bf215546Sopenharmony_ci
112bf215546Sopenharmony_ci               nir_discard_if(&b, nir_inot(&b, condition));
113bf215546Sopenharmony_ci               shader->info.fs.uses_discard = true;
114bf215546Sopenharmony_ci            }
115bf215546Sopenharmony_ci         }
116bf215546Sopenharmony_ci      }
117bf215546Sopenharmony_ci
118bf215546Sopenharmony_ci      nir_metadata_preserve(impl, nir_metadata_block_index |
119bf215546Sopenharmony_ci                            nir_metadata_dominance);
120bf215546Sopenharmony_ci   }
121bf215546Sopenharmony_ci}
122