nir: Add a lowering pass to split 64bit phis
[mesa.git] / src / compiler / nir / nir_lower_clamp_color_outputs.c
1 /*
2 * Copyright © 2015 Red Hat
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 FROM,
20 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
21 * SOFTWARE.
22 */
23
24
25 #include "nir.h"
26 #include "nir_builder.h"
27
28 typedef struct {
29 nir_shader *shader;
30 nir_builder b;
31 } lower_state;
32
33 static bool
34 is_color_output(lower_state *state, nir_variable *out)
35 {
36 switch (state->shader->info.stage) {
37 case MESA_SHADER_VERTEX:
38 case MESA_SHADER_GEOMETRY:
39 case MESA_SHADER_TESS_EVAL:
40 switch (out->data.location) {
41 case VARYING_SLOT_COL0:
42 case VARYING_SLOT_COL1:
43 case VARYING_SLOT_BFC0:
44 case VARYING_SLOT_BFC1:
45 return true;
46 default:
47 return false;
48 }
49 break;
50 case MESA_SHADER_FRAGMENT:
51 return (out->data.location == FRAG_RESULT_COLOR ||
52 out->data.location >= FRAG_RESULT_DATA0);
53 default:
54 return false;
55 }
56 }
57
58 static bool
59 lower_intrinsic(lower_state *state, nir_intrinsic_instr *intr)
60 {
61 nir_variable *out = NULL;
62 nir_builder *b = &state->b;
63 nir_ssa_def *s;
64
65 switch (intr->intrinsic) {
66 case nir_intrinsic_store_deref:
67 out = nir_deref_instr_get_variable(nir_src_as_deref(intr->src[0]));
68 break;
69 case nir_intrinsic_store_output:
70 /* already had i/o lowered.. lookup the matching output var: */
71 nir_foreach_shader_out_variable(var, state->shader) {
72 int drvloc = var->data.driver_location;
73 if (nir_intrinsic_base(intr) == drvloc) {
74 out = var;
75 break;
76 }
77 }
78 assume(out);
79 break;
80 default:
81 return false;
82 }
83
84 if (out->data.mode != nir_var_shader_out)
85 return false;
86
87 if (is_color_output(state, out)) {
88 b->cursor = nir_before_instr(&intr->instr);
89 int src = intr->intrinsic == nir_intrinsic_store_deref ? 1 : 0;
90 s = nir_ssa_for_src(b, intr->src[src], intr->num_components);
91 s = nir_fsat(b, s);
92 nir_instr_rewrite_src(&intr->instr, &intr->src[src], nir_src_for_ssa(s));
93 }
94
95 return true;
96 }
97
98 static bool
99 lower_block(lower_state *state, nir_block *block)
100 {
101 bool progress = false;
102
103 nir_foreach_instr_safe(instr, block) {
104 if (instr->type == nir_instr_type_intrinsic)
105 progress |= lower_intrinsic(state, nir_instr_as_intrinsic(instr));
106 }
107
108 return progress;
109 }
110
111 static bool
112 lower_impl(lower_state *state, nir_function_impl *impl)
113 {
114 nir_builder_init(&state->b, impl);
115 bool progress = false;
116
117 nir_foreach_block(block, impl) {
118 progress |= lower_block(state, block);
119 }
120 nir_metadata_preserve(impl, nir_metadata_block_index |
121 nir_metadata_dominance);
122
123 return progress;
124 }
125
126 bool
127 nir_lower_clamp_color_outputs(nir_shader *shader)
128 {
129 bool progress = false;
130 lower_state state = {
131 .shader = shader,
132 };
133
134 nir_foreach_function(function, shader) {
135 if (function->impl)
136 progress |= lower_impl(&state, function->impl);
137 }
138
139 return progress;
140 }