1 /*
2 * Copyright © 2017 Broadcom
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 /**
25 * @file
26 *
27 * Implements GL alpha testing by comparing the output color's alpha to the
28 * alpha_ref intrinsic and emitting a discard based on it.
29 *
30 * The alpha_to_one value overrides the source alpha to 1.0 to implement
31 * GL_SAMPLE_ALPHA_TO_ONE, which applies before the alpha test (and would be
32 * rather silly to use with alpha test, but the spec permits).
33 */
34
35 #include "nir/nir.h"
36 #include "nir/nir_builder.h"
37
38 struct alpha_test_state {
39 bool alpha_to_one;
40 enum compare_func func;
41 const gl_state_index16 *alpha_ref_state_tokens;
42 };
43
44 static bool
is_color_output(int location)45 is_color_output(int location)
46 {
47 return location == FRAG_RESULT_COLOR || location == FRAG_RESULT_DATA0;
48 }
49
50 static bool
lower(nir_builder * b,nir_intrinsic_instr * intr,void * data)51 lower(nir_builder *b, nir_intrinsic_instr *intr, void *data)
52 {
53 struct alpha_test_state *state = data;
54
55 switch (intr->intrinsic) {
56 case nir_intrinsic_store_output:
57 if (!is_color_output(nir_intrinsic_io_semantics(intr).location))
58 return false;
59 break;
60 default:
61 return false;
62 }
63
64 b->cursor = nir_before_instr(&intr->instr);
65
66 nir_def *alpha;
67 if (state->alpha_to_one)
68 alpha = nir_imm_float(b, 1.0);
69 else
70 alpha = nir_channel(b, intr->src[0].ssa, 3);
71
72 nir_variable *var = nir_state_variable_create(b->shader, glsl_float_type(),
73 "gl_AlphaRefMESA",
74 state->alpha_ref_state_tokens);
75 nir_def *alpha_ref = nir_load_var(b, var);
76 nir_def *condition = nir_compare_func(b, state->func, alpha, alpha_ref);
77
78 nir_discard_if(b, nir_inot(b, condition));
79 b->shader->info.fs.uses_discard = true;
80 return true;
81 }
82
83 bool
nir_lower_alpha_test(nir_shader * shader,enum compare_func func,bool alpha_to_one,const gl_state_index16 * alpha_ref_state_tokens)84 nir_lower_alpha_test(nir_shader *shader, enum compare_func func,
85 bool alpha_to_one,
86 const gl_state_index16 *alpha_ref_state_tokens)
87 {
88 assert(shader->info.io_lowered);
89 struct alpha_test_state state = {
90 .alpha_ref_state_tokens = alpha_ref_state_tokens,
91 .alpha_to_one = alpha_to_one,
92 .func = func,
93 };
94
95 return nir_shader_intrinsics_pass(shader, lower,
96 nir_metadata_control_flow,
97 &state);
98 }
99