b652fb796be71cc427c6c1dca30a019d92dcb4ae
[mesa.git] / src / compiler / nir / nir_lower_alpha_test.c
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 void
39 nir_lower_alpha_test(nir_shader *shader, enum compare_func func,
40 bool alpha_to_one,
41 const gl_state_index16 *alpha_ref_state_tokens)
42 {
43 assert(shader->info.stage == MESA_SHADER_FRAGMENT);
44
45 nir_foreach_function(function, shader) {
46 nir_function_impl *impl = function->impl;
47 nir_builder b;
48 nir_builder_init(&b, impl);
49 b.cursor = nir_before_cf_list(&impl->body);
50
51 nir_foreach_block(block, impl) {
52 nir_foreach_instr_safe(instr, block) {
53 if (instr->type == nir_instr_type_intrinsic) {
54 nir_intrinsic_instr *intr = nir_instr_as_intrinsic(instr);
55
56 nir_variable *out = NULL;
57
58 switch (intr->intrinsic) {
59 case nir_intrinsic_store_deref:
60 out = nir_deref_instr_get_variable(nir_src_as_deref(intr->src[0]));
61 break;
62 case nir_intrinsic_store_output:
63 /* already had i/o lowered.. lookup the matching output var: */
64 nir_foreach_variable(var, &shader->outputs) {
65 int drvloc = var->data.driver_location;
66 if (nir_intrinsic_base(intr) == drvloc) {
67 out = var;
68 break;
69 }
70 }
71 assume(out);
72 break;
73 default:
74 continue;
75 }
76
77 if (out->data.mode != nir_var_shader_out)
78 continue;
79
80 if (out->data.location != FRAG_RESULT_COLOR &&
81 out->data.location != FRAG_RESULT_DATA0)
82 continue;
83
84 b.cursor = nir_before_instr(&intr->instr);
85
86 nir_ssa_def *alpha;
87 if (alpha_to_one) {
88 alpha = nir_imm_float(&b, 1.0);
89 } else if (intr->intrinsic == nir_intrinsic_store_deref) {
90 alpha = nir_channel(&b, nir_ssa_for_src(&b, intr->src[1], 4),
91 3);
92 } else {
93 alpha = nir_channel(&b, nir_ssa_for_src(&b, intr->src[0], 4),
94 3);
95 }
96
97 nir_ssa_def *alpha_ref;
98 if (alpha_ref_state_tokens) {
99 nir_variable *var = nir_variable_create(shader,
100 nir_var_uniform,
101 glsl_float_type(),
102 "gl_AlphaRefMESA");
103 var->num_state_slots = 1;
104 var->state_slots = ralloc_array(var, nir_state_slot, 1);
105 memcpy(var->state_slots[0].tokens,
106 alpha_ref_state_tokens,
107 sizeof(var->state_slots[0].tokens));
108 alpha_ref = nir_load_var(&b, var);
109 } else
110 alpha_ref = nir_load_alpha_ref_float(&b);
111
112 nir_ssa_def *condition =
113 nir_compare_func(&b, func, alpha, alpha_ref);
114
115 nir_intrinsic_instr *discard =
116 nir_intrinsic_instr_create(b.shader,
117 nir_intrinsic_discard_if);
118 discard->num_components = 1;
119 discard->src[0] = nir_src_for_ssa(nir_inot(&b, condition));
120 nir_builder_instr_insert(&b, &discard->instr);
121 shader->info.fs.uses_discard = true;
122 }
123 }
124 }
125
126 nir_metadata_preserve(impl, nir_metadata_block_index |
127 nir_metadata_dominance);
128 }
129 }