32f855624276dc31de78e3edfa1cbcc239c93cd1
[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 switch (out->data.location) {
40 case VARYING_SLOT_COL0:
41 case VARYING_SLOT_COL1:
42 case VARYING_SLOT_BFC0:
43 case VARYING_SLOT_BFC1:
44 return true;
45 default:
46 return false;
47 }
48 break;
49 case MESA_SHADER_FRAGMENT:
50 return (out->data.location == FRAG_RESULT_COLOR ||
51 out->data.location >= FRAG_RESULT_DATA0);
52 default:
53 return false;
54 }
55 }
56
57 static bool
58 lower_intrinsic(lower_state *state, nir_intrinsic_instr *intr)
59 {
60 nir_variable *out = NULL;
61 nir_builder *b = &state->b;
62 nir_ssa_def *s;
63
64 switch (intr->intrinsic) {
65 case nir_intrinsic_store_deref:
66 out = nir_deref_instr_get_variable(nir_src_as_deref(intr->src[0]));
67 break;
68 case nir_intrinsic_store_output:
69 /* already had i/o lowered.. lookup the matching output var: */
70 nir_foreach_variable(var, &state->shader->outputs) {
71 int drvloc = var->data.driver_location;
72 if (nir_intrinsic_base(intr) == drvloc) {
73 out = var;
74 break;
75 }
76 }
77 assume(out);
78 break;
79 default:
80 return false;
81 }
82
83 if (out->data.mode != nir_var_shader_out)
84 return false;
85
86 if (is_color_output(state, out)) {
87 b->cursor = nir_before_instr(&intr->instr);
88 int src = intr->intrinsic == nir_intrinsic_store_deref ? 1 : 0;
89 s = nir_ssa_for_src(b, intr->src[src], intr->num_components);
90 s = nir_fsat(b, s);
91 nir_instr_rewrite_src(&intr->instr, &intr->src[src], nir_src_for_ssa(s));
92 }
93
94 return true;
95 }
96
97 static bool
98 lower_block(lower_state *state, nir_block *block)
99 {
100 bool progress = false;
101
102 nir_foreach_instr_safe(instr, block) {
103 if (instr->type == nir_instr_type_intrinsic)
104 progress |= lower_intrinsic(state, nir_instr_as_intrinsic(instr));
105 }
106
107 return progress;
108 }
109
110 static bool
111 lower_impl(lower_state *state, nir_function_impl *impl)
112 {
113 nir_builder_init(&state->b, impl);
114 bool progress = false;
115
116 nir_foreach_block(block, impl) {
117 progress |= lower_block(state, block);
118 }
119 nir_metadata_preserve(impl, nir_metadata_block_index |
120 nir_metadata_dominance);
121
122 return progress;
123 }
124
125 bool
126 nir_lower_clamp_color_outputs(nir_shader *shader)
127 {
128 bool progress = false;
129 lower_state state = {
130 .shader = shader,
131 };
132
133 nir_foreach_function(function, shader) {
134 if (function->impl)
135 progress |= lower_impl(&state, function->impl);
136 }
137
138 return progress;
139 }