nir/lower-tex: make options a local var
[mesa.git] / src / compiler / 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->texture_index = tex->texture_index;
137 txs->dest_type = nir_type_int;
138
139 /* only single src, the lod: */
140 txs->src[0].src = nir_src_for_ssa(nir_imm_int(b, 0));
141 txs->src[0].src_type = nir_tex_src_lod;
142
143 nir_ssa_dest_init(&txs->instr, &txs->dest, 2, 32, NULL);
144 nir_builder_instr_insert(b, &txs->instr);
145
146 return nir_i2f(b, &txs->dest.ssa);
147 }
148
149 static void
150 lower_rect(nir_builder *b, nir_tex_instr *tex)
151 {
152 nir_ssa_def *txs = get_texture_size(b, tex);
153 nir_ssa_def *scale = nir_frcp(b, txs);
154
155 /* Walk through the sources normalizing the requested arguments. */
156 for (unsigned i = 0; i < tex->num_srcs; i++) {
157 if (tex->src[i].src_type != nir_tex_src_coord)
158 continue;
159
160 nir_ssa_def *coords =
161 nir_ssa_for_src(b, tex->src[i].src, tex->coord_components);
162 nir_instr_rewrite_src(&tex->instr,
163 &tex->src[i].src,
164 nir_src_for_ssa(nir_fmul(b, coords, scale)));
165 }
166
167 tex->sampler_dim = GLSL_SAMPLER_DIM_2D;
168 }
169
170 static void
171 saturate_src(nir_builder *b, nir_tex_instr *tex, unsigned sat_mask)
172 {
173 b->cursor = nir_before_instr(&tex->instr);
174
175 /* Walk through the sources saturating the requested arguments. */
176 for (unsigned i = 0; i < tex->num_srcs; i++) {
177 if (tex->src[i].src_type != nir_tex_src_coord)
178 continue;
179
180 nir_ssa_def *src =
181 nir_ssa_for_src(b, tex->src[i].src, tex->coord_components);
182
183 /* split src into components: */
184 nir_ssa_def *comp[4];
185
186 for (unsigned j = 0; j < tex->coord_components; j++)
187 comp[j] = nir_channel(b, src, j);
188
189 /* clamp requested components, array index does not get clamped: */
190 unsigned ncomp = tex->coord_components;
191 if (tex->is_array)
192 ncomp--;
193
194 for (unsigned j = 0; j < ncomp; j++) {
195 if ((1 << j) & sat_mask) {
196 if (tex->sampler_dim == GLSL_SAMPLER_DIM_RECT) {
197 /* non-normalized texture coords, so clamp to texture
198 * size rather than [0.0, 1.0]
199 */
200 nir_ssa_def *txs = get_texture_size(b, tex);
201 comp[j] = nir_fmax(b, comp[j], nir_imm_float(b, 0.0));
202 comp[j] = nir_fmin(b, comp[j], nir_channel(b, txs, j));
203 } else {
204 comp[j] = nir_fsat(b, comp[j]);
205 }
206 }
207 }
208
209 /* and move the result back into a single vecN: */
210 src = nir_vec(b, comp, tex->coord_components);
211
212 nir_instr_rewrite_src(&tex->instr,
213 &tex->src[i].src,
214 nir_src_for_ssa(src));
215 }
216 }
217
218 static nir_ssa_def *
219 get_zero_or_one(nir_builder *b, nir_alu_type type, uint8_t swizzle_val)
220 {
221 nir_const_value v;
222
223 memset(&v, 0, sizeof(v));
224
225 if (swizzle_val == 4) {
226 v.u32[0] = v.u32[1] = v.u32[2] = v.u32[3] = 0;
227 } else {
228 assert(swizzle_val == 5);
229 if (type == nir_type_float)
230 v.f32[0] = v.f32[1] = v.f32[2] = v.f32[3] = 1.0;
231 else
232 v.u32[0] = v.u32[1] = v.u32[2] = v.u32[3] = 1;
233 }
234
235 return nir_build_imm(b, 4, v);
236 }
237
238 static void
239 swizzle_result(nir_builder *b, nir_tex_instr *tex, const uint8_t swizzle[4])
240 {
241 assert(tex->dest.is_ssa);
242
243 b->cursor = nir_after_instr(&tex->instr);
244
245 nir_ssa_def *swizzled;
246 if (tex->op == nir_texop_tg4) {
247 if (swizzle[tex->component] < 4) {
248 /* This one's easy */
249 tex->component = swizzle[tex->component];
250 return;
251 } else {
252 swizzled = get_zero_or_one(b, tex->dest_type, swizzle[tex->component]);
253 }
254 } else {
255 assert(nir_tex_instr_dest_size(tex) == 4);
256 if (swizzle[0] < 4 && swizzle[1] < 4 &&
257 swizzle[2] < 4 && swizzle[3] < 4) {
258 unsigned swiz[4] = { swizzle[0], swizzle[1], swizzle[2], swizzle[3] };
259 /* We have no 0's or 1's, just emit a swizzling MOV */
260 swizzled = nir_swizzle(b, &tex->dest.ssa, swiz, 4, false);
261 } else {
262 nir_ssa_def *srcs[4];
263 for (unsigned i = 0; i < 4; i++) {
264 if (swizzle[i] < 4) {
265 srcs[i] = nir_channel(b, &tex->dest.ssa, swizzle[i]);
266 } else {
267 srcs[i] = get_zero_or_one(b, tex->dest_type, swizzle[i]);
268 }
269 }
270 swizzled = nir_vec(b, srcs, 4);
271 }
272 }
273
274 nir_ssa_def_rewrite_uses_after(&tex->dest.ssa, nir_src_for_ssa(swizzled),
275 swizzled->parent_instr);
276 }
277
278 static bool
279 nir_lower_tex_block(nir_block *block, void *void_state)
280 {
281 lower_tex_state *state = void_state;
282 const nir_lower_tex_options *options = state->options;
283 nir_builder *b = &state->b;
284
285 nir_foreach_instr_safe(block, instr) {
286 if (instr->type != nir_instr_type_tex)
287 continue;
288
289 nir_tex_instr *tex = nir_instr_as_tex(instr);
290 bool lower_txp = !!(options->lower_txp & (1 << tex->sampler_dim));
291
292 /* mask of src coords to saturate (clamp): */
293 unsigned sat_mask = 0;
294
295 if ((1 << tex->sampler_index) & options->saturate_r)
296 sat_mask |= (1 << 2); /* .z */
297 if ((1 << tex->sampler_index) & options->saturate_t)
298 sat_mask |= (1 << 1); /* .y */
299 if ((1 << tex->sampler_index) & options->saturate_s)
300 sat_mask |= (1 << 0); /* .x */
301
302 /* If we are clamping any coords, we must lower projector first
303 * as clamping happens *after* projection:
304 */
305 if (lower_txp || sat_mask) {
306 project_src(b, tex);
307 state->progress = true;
308 }
309
310 if ((tex->sampler_dim == GLSL_SAMPLER_DIM_RECT) && options->lower_rect) {
311 lower_rect(b, tex);
312 state->progress = true;
313 }
314
315 if (sat_mask) {
316 saturate_src(b, tex, sat_mask);
317 state->progress = true;
318 }
319
320 if (((1 << tex->texture_index) & options->swizzle_result) &&
321 !nir_tex_instr_is_query(tex) &&
322 !(tex->is_shadow && tex->is_new_style_shadow)) {
323 swizzle_result(b, tex, options->swizzles[tex->texture_index]);
324 state->progress = true;
325 }
326 }
327
328 return true;
329 }
330
331 static void
332 nir_lower_tex_impl(nir_function_impl *impl, lower_tex_state *state)
333 {
334 nir_builder_init(&state->b, impl);
335
336 nir_foreach_block(impl, nir_lower_tex_block, state);
337
338 nir_metadata_preserve(impl, nir_metadata_block_index |
339 nir_metadata_dominance);
340 }
341
342 bool
343 nir_lower_tex(nir_shader *shader, const nir_lower_tex_options *options)
344 {
345 lower_tex_state state;
346 state.options = options;
347 state.progress = false;
348
349 nir_foreach_function(shader, function) {
350 if (function->impl)
351 nir_lower_tex_impl(function->impl, &state);
352 }
353
354 return state.progress;
355 }