nir/lower_tex: Report progress
[mesa.git] / src / glsl / nir / nir_lower_tex.c
1 /*
2 * Copyright © 2015 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 * This lowering pass supports (as configured via nir_lower_tex_options)
26 * various texture related conversions:
27 * + texture projector lowering: converts the coordinate division for
28 * texture projection to be done in ALU instructions instead of
29 * asking the texture operation to do so.
30 * + lowering RECT: converts the un-normalized RECT texture coordinates
31 * to normalized coordinates with txs plus ALU instructions
32 * + saturate s/t/r coords: to emulate certain texture clamp/wrap modes,
33 * inserts instructions to clamp specified coordinates to [0.0, 1.0].
34 * Note that this automatically triggers texture projector lowering if
35 * needed, since clamping must happen after projector lowering.
36 */
37
38 #include "nir.h"
39 #include "nir_builder.h"
40
41 typedef struct {
42 nir_builder b;
43 const nir_lower_tex_options *options;
44 bool progress;
45 } lower_tex_state;
46
47 static void
48 project_src(nir_builder *b, nir_tex_instr *tex)
49 {
50 /* Find the projector in the srcs list, if present. */
51 unsigned proj_index;
52 for (proj_index = 0; proj_index < tex->num_srcs; proj_index++) {
53 if (tex->src[proj_index].src_type == nir_tex_src_projector)
54 break;
55 }
56 if (proj_index == tex->num_srcs)
57 return;
58
59 b->cursor = nir_before_instr(&tex->instr);
60
61 nir_ssa_def *inv_proj =
62 nir_frcp(b, nir_ssa_for_src(b, tex->src[proj_index].src, 1));
63
64 /* Walk through the sources projecting the arguments. */
65 for (unsigned i = 0; i < tex->num_srcs; i++) {
66 switch (tex->src[i].src_type) {
67 case nir_tex_src_coord:
68 case nir_tex_src_comparitor:
69 break;
70 default:
71 continue;
72 }
73 nir_ssa_def *unprojected =
74 nir_ssa_for_src(b, tex->src[i].src, nir_tex_instr_src_size(tex, i));
75 nir_ssa_def *projected = nir_fmul(b, unprojected, inv_proj);
76
77 /* Array indices don't get projected, so make an new vector with the
78 * coordinate's array index untouched.
79 */
80 if (tex->is_array && tex->src[i].src_type == nir_tex_src_coord) {
81 switch (tex->coord_components) {
82 case 4:
83 projected = nir_vec4(b,
84 nir_channel(b, projected, 0),
85 nir_channel(b, projected, 1),
86 nir_channel(b, projected, 2),
87 nir_channel(b, unprojected, 3));
88 break;
89 case 3:
90 projected = nir_vec3(b,
91 nir_channel(b, projected, 0),
92 nir_channel(b, projected, 1),
93 nir_channel(b, unprojected, 2));
94 break;
95 case 2:
96 projected = nir_vec2(b,
97 nir_channel(b, projected, 0),
98 nir_channel(b, unprojected, 1));
99 break;
100 default:
101 unreachable("bad texture coord count for array");
102 break;
103 }
104 }
105
106 nir_instr_rewrite_src(&tex->instr,
107 &tex->src[i].src,
108 nir_src_for_ssa(projected));
109 }
110
111 /* Now move the later tex sources down the array so that the projector
112 * disappears.
113 */
114 nir_instr_rewrite_src(&tex->instr, &tex->src[proj_index].src,
115 NIR_SRC_INIT);
116 for (unsigned i = proj_index + 1; i < tex->num_srcs; i++) {
117 tex->src[i-1].src_type = tex->src[i].src_type;
118 nir_instr_move_src(&tex->instr, &tex->src[i-1].src, &tex->src[i].src);
119 }
120 tex->num_srcs--;
121 }
122
123 static nir_ssa_def *
124 get_texture_size(nir_builder *b, nir_tex_instr *tex)
125 {
126 b->cursor = nir_before_instr(&tex->instr);
127
128 /* RECT textures should not be array: */
129 assert(!tex->is_array);
130
131 nir_tex_instr *txs;
132
133 txs = nir_tex_instr_create(b->shader, 1);
134 txs->op = nir_texop_txs;
135 txs->sampler_dim = GLSL_SAMPLER_DIM_RECT;
136 txs->sampler_index = tex->sampler_index;
137
138 /* only single src, the lod: */
139 txs->src[0].src = nir_src_for_ssa(nir_imm_int(b, 0));
140 txs->src[0].src_type = nir_tex_src_lod;
141
142 nir_ssa_dest_init(&txs->instr, &txs->dest, 2, NULL);
143 nir_builder_instr_insert(b, &txs->instr);
144
145 return nir_i2f(b, &txs->dest.ssa);
146 }
147
148 static void
149 lower_rect(nir_builder *b, nir_tex_instr *tex)
150 {
151 nir_ssa_def *txs = get_texture_size(b, tex);
152 nir_ssa_def *scale = nir_frcp(b, txs);
153
154 /* Walk through the sources normalizing the requested arguments. */
155 for (unsigned i = 0; i < tex->num_srcs; i++) {
156 if (tex->src[i].src_type != nir_tex_src_coord)
157 continue;
158
159 nir_ssa_def *coords =
160 nir_ssa_for_src(b, tex->src[i].src, tex->coord_components);
161 nir_instr_rewrite_src(&tex->instr,
162 &tex->src[i].src,
163 nir_src_for_ssa(nir_fmul(b, coords, scale)));
164 }
165
166 tex->sampler_dim = GLSL_SAMPLER_DIM_2D;
167 }
168
169 static void
170 saturate_src(nir_builder *b, nir_tex_instr *tex, unsigned sat_mask)
171 {
172 b->cursor = nir_before_instr(&tex->instr);
173
174 /* Walk through the sources saturating the requested arguments. */
175 for (unsigned i = 0; i < tex->num_srcs; i++) {
176 if (tex->src[i].src_type != nir_tex_src_coord)
177 continue;
178
179 nir_ssa_def *src =
180 nir_ssa_for_src(b, tex->src[i].src, tex->coord_components);
181
182 /* split src into components: */
183 nir_ssa_def *comp[4];
184
185 for (unsigned j = 0; j < tex->coord_components; j++)
186 comp[j] = nir_channel(b, src, j);
187
188 /* clamp requested components, array index does not get clamped: */
189 unsigned ncomp = tex->coord_components;
190 if (tex->is_array)
191 ncomp--;
192
193 for (unsigned j = 0; j < ncomp; j++) {
194 if ((1 << j) & sat_mask) {
195 if (tex->sampler_dim == GLSL_SAMPLER_DIM_RECT) {
196 /* non-normalized texture coords, so clamp to texture
197 * size rather than [0.0, 1.0]
198 */
199 nir_ssa_def *txs = get_texture_size(b, tex);
200 comp[j] = nir_fmax(b, comp[j], nir_imm_float(b, 0.0));
201 comp[j] = nir_fmin(b, comp[j], nir_channel(b, txs, j));
202 } else {
203 comp[j] = nir_fsat(b, comp[j]);
204 }
205 }
206 }
207
208 /* and move the result back into a single vecN: */
209 src = nir_vec(b, comp, tex->coord_components);
210
211 nir_instr_rewrite_src(&tex->instr,
212 &tex->src[i].src,
213 nir_src_for_ssa(src));
214 }
215 }
216
217 static bool
218 nir_lower_tex_block(nir_block *block, void *void_state)
219 {
220 lower_tex_state *state = void_state;
221 nir_builder *b = &state->b;
222
223 nir_foreach_instr_safe(block, instr) {
224 if (instr->type != nir_instr_type_tex)
225 continue;
226
227 nir_tex_instr *tex = nir_instr_as_tex(instr);
228 bool lower_txp = !!(state->options->lower_txp & (1 << tex->sampler_dim));
229
230 /* mask of src coords to saturate (clamp): */
231 unsigned sat_mask = 0;
232
233 if ((1 << tex->sampler_index) & state->options->saturate_r)
234 sat_mask |= (1 << 2); /* .z */
235 if ((1 << tex->sampler_index) & state->options->saturate_t)
236 sat_mask |= (1 << 1); /* .y */
237 if ((1 << tex->sampler_index) & state->options->saturate_s)
238 sat_mask |= (1 << 0); /* .x */
239
240 /* If we are clamping any coords, we must lower projector first
241 * as clamping happens *after* projection:
242 */
243 if (lower_txp || sat_mask) {
244 project_src(b, tex);
245 state->progress = true;
246 }
247
248 if ((tex->sampler_dim == GLSL_SAMPLER_DIM_RECT) &&
249 state->options->lower_rect) {
250 lower_rect(b, tex);
251 state->progress = true;
252 }
253
254 if (sat_mask) {
255 saturate_src(b, tex, sat_mask);
256 state->progress = true;
257 }
258 }
259
260 return true;
261 }
262
263 static void
264 nir_lower_tex_impl(nir_function_impl *impl, lower_tex_state *state)
265 {
266 nir_builder_init(&state->b, impl);
267
268 nir_foreach_block(impl, nir_lower_tex_block, state);
269
270 nir_metadata_preserve(impl, nir_metadata_block_index |
271 nir_metadata_dominance);
272 }
273
274 bool
275 nir_lower_tex(nir_shader *shader, const nir_lower_tex_options *options)
276 {
277 lower_tex_state state;
278 state.options = options;
279 state.progress = false;
280
281 nir_foreach_overload(shader, overload) {
282 if (overload->impl)
283 nir_lower_tex_impl(overload->impl, &state);
284 }
285
286 return state.progress;
287 }