2 * Copyright © 2017 Broadcom
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:
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
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
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.
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).
36 #include "nir/nir_builder.h"
39 nir_lower_alpha_test(nir_shader
*shader
, enum compare_func func
,
41 const gl_state_index16
*alpha_ref_state_tokens
)
43 assert(alpha_ref_state_tokens
);
44 assert(shader
->info
.stage
== MESA_SHADER_FRAGMENT
);
46 nir_foreach_function(function
, shader
) {
47 nir_function_impl
*impl
= function
->impl
;
49 nir_builder_init(&b
, impl
);
50 b
.cursor
= nir_before_cf_list(&impl
->body
);
52 nir_foreach_block(block
, impl
) {
53 nir_foreach_instr_safe(instr
, block
) {
54 if (instr
->type
== nir_instr_type_intrinsic
) {
55 nir_intrinsic_instr
*intr
= nir_instr_as_intrinsic(instr
);
57 nir_variable
*out
= NULL
;
59 switch (intr
->intrinsic
) {
60 case nir_intrinsic_store_deref
:
61 out
= nir_deref_instr_get_variable(nir_src_as_deref(intr
->src
[0]));
63 case nir_intrinsic_store_output
:
64 /* already had i/o lowered.. lookup the matching output var: */
65 nir_foreach_variable(var
, &shader
->outputs
) {
66 int drvloc
= var
->data
.driver_location
;
67 if (nir_intrinsic_base(intr
) == drvloc
) {
78 if (out
->data
.mode
!= nir_var_shader_out
)
81 if (out
->data
.location
!= FRAG_RESULT_COLOR
&&
82 out
->data
.location
!= FRAG_RESULT_DATA0
)
85 b
.cursor
= nir_before_instr(&intr
->instr
);
89 alpha
= nir_imm_float(&b
, 1.0);
90 } else if (intr
->intrinsic
== nir_intrinsic_store_deref
) {
91 alpha
= nir_channel(&b
, nir_ssa_for_src(&b
, intr
->src
[1], 4),
94 alpha
= nir_channel(&b
, nir_ssa_for_src(&b
, intr
->src
[0], 4),
98 nir_variable
*var
= nir_variable_create(shader
,
102 var
->num_state_slots
= 1;
103 var
->state_slots
= ralloc_array(var
, nir_state_slot
, 1);
104 memcpy(var
->state_slots
[0].tokens
,
105 alpha_ref_state_tokens
,
106 sizeof(var
->state_slots
[0].tokens
));
107 nir_ssa_def
*alpha_ref
= nir_load_var(&b
, var
);
109 nir_ssa_def
*condition
=
110 nir_compare_func(&b
, func
, alpha
, alpha_ref
);
112 nir_intrinsic_instr
*discard
=
113 nir_intrinsic_instr_create(b
.shader
,
114 nir_intrinsic_discard_if
);
115 discard
->num_components
= 1;
116 discard
->src
[0] = nir_src_for_ssa(nir_inot(&b
, condition
));
117 nir_builder_instr_insert(&b
, &discard
->instr
);
118 shader
->info
.fs
.uses_discard
= true;
123 nir_metadata_preserve(impl
, nir_metadata_block_index
|
124 nir_metadata_dominance
);