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 nir_variable *out = NULL;
55
56 switch (intr->intrinsic) {
57 case nir_intrinsic_store_deref:
58 out = nir_intrinsic_get_var(intr, 0);
59 if (out->data.mode != nir_var_shader_out)
60 return false;
61
62 if (!is_color_output(out->data.location))
63 return false;
64 break;
65 case nir_intrinsic_store_output:
66 if (!is_color_output(nir_intrinsic_io_semantics(intr).location))
67 return false;
68 break;
69 default:
70 return false;
71 }
72
73 b->cursor = nir_before_instr(&intr->instr);
74
75 nir_def *alpha;
76 if (state->alpha_to_one)
77 alpha = nir_imm_float(b, 1.0);
78 else if (intr->intrinsic == nir_intrinsic_store_deref)
79 alpha = nir_channel(b, intr->src[1].ssa, 3);
80 else
81 alpha = nir_channel(b, intr->src[0].ssa, 3);
82
83 nir_variable *var = nir_state_variable_create(b->shader, glsl_float_type(),
84 "gl_AlphaRefMESA",
85 state->alpha_ref_state_tokens);
86 nir_def *alpha_ref = nir_load_var(b, var);
87 nir_def *condition = nir_compare_func(b, state->func, alpha, alpha_ref);
88
89 nir_discard_if(b, nir_inot(b, condition));
90 b->shader->info.fs.uses_discard = true;
91 return true;
92 }
93
94 bool
nir_lower_alpha_test(nir_shader * shader,enum compare_func func,bool alpha_to_one,const gl_state_index16 * alpha_ref_state_tokens)95 nir_lower_alpha_test(nir_shader *shader, enum compare_func func,
96 bool alpha_to_one,
97 const gl_state_index16 *alpha_ref_state_tokens)
98 {
99 struct alpha_test_state state = {
100 .alpha_ref_state_tokens = alpha_ref_state_tokens,
101 .alpha_to_one = alpha_to_one,
102 .func = func,
103 };
104
105 return nir_shader_intrinsics_pass(shader, lower,
106 nir_metadata_control_flow,
107 &state);
108 }
109