nir/lower_tex: fixup for new foreach_block()
[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 static void
42 project_src(nir_builder *b, nir_tex_instr *tex)
43 {
44 /* Find the projector in the srcs list, if present. */
45 unsigned proj_index;
46 for (proj_index = 0; proj_index < tex->num_srcs; proj_index++) {
47 if (tex->src[proj_index].src_type == nir_tex_src_projector)
48 break;
49 }
50 if (proj_index == tex->num_srcs)
51 return;
52
53 b->cursor = nir_before_instr(&tex->instr);
54
55 nir_ssa_def *inv_proj =
56 nir_frcp(b, nir_ssa_for_src(b, tex->src[proj_index].src, 1));
57
58 /* Walk through the sources projecting the arguments. */
59 for (unsigned i = 0; i < tex->num_srcs; i++) {
60 switch (tex->src[i].src_type) {
61 case nir_tex_src_coord:
62 case nir_tex_src_comparitor:
63 break;
64 default:
65 continue;
66 }
67 nir_ssa_def *unprojected =
68 nir_ssa_for_src(b, tex->src[i].src, nir_tex_instr_src_size(tex, i));
69 nir_ssa_def *projected = nir_fmul(b, unprojected, inv_proj);
70
71 /* Array indices don't get projected, so make an new vector with the
72 * coordinate's array index untouched.
73 */
74 if (tex->is_array && tex->src[i].src_type == nir_tex_src_coord) {
75 switch (tex->coord_components) {
76 case 4:
77 projected = nir_vec4(b,
78 nir_channel(b, projected, 0),
79 nir_channel(b, projected, 1),
80 nir_channel(b, projected, 2),
81 nir_channel(b, unprojected, 3));
82 break;
83 case 3:
84 projected = nir_vec3(b,
85 nir_channel(b, projected, 0),
86 nir_channel(b, projected, 1),
87 nir_channel(b, unprojected, 2));
88 break;
89 case 2:
90 projected = nir_vec2(b,
91 nir_channel(b, projected, 0),
92 nir_channel(b, unprojected, 1));
93 break;
94 default:
95 unreachable("bad texture coord count for array");
96 break;
97 }
98 }
99
100 nir_instr_rewrite_src(&tex->instr,
101 &tex->src[i].src,
102 nir_src_for_ssa(projected));
103 }
104
105 /* Now move the later tex sources down the array so that the projector
106 * disappears.
107 */
108 nir_instr_rewrite_src(&tex->instr, &tex->src[proj_index].src,
109 NIR_SRC_INIT);
110 for (unsigned i = proj_index + 1; i < tex->num_srcs; i++) {
111 tex->src[i-1].src_type = tex->src[i].src_type;
112 nir_instr_move_src(&tex->instr, &tex->src[i-1].src, &tex->src[i].src);
113 }
114 tex->num_srcs--;
115 }
116
117 static nir_ssa_def *
118 get_texture_size(nir_builder *b, nir_tex_instr *tex)
119 {
120 b->cursor = nir_before_instr(&tex->instr);
121
122 /* RECT textures should not be array: */
123 assert(!tex->is_array);
124
125 nir_tex_instr *txs;
126
127 txs = nir_tex_instr_create(b->shader, 1);
128 txs->op = nir_texop_txs;
129 txs->sampler_dim = GLSL_SAMPLER_DIM_RECT;
130 txs->texture_index = tex->texture_index;
131 txs->dest_type = nir_type_int;
132
133 /* only single src, the lod: */
134 txs->src[0].src = nir_src_for_ssa(nir_imm_int(b, 0));
135 txs->src[0].src_type = nir_tex_src_lod;
136
137 nir_ssa_dest_init(&txs->instr, &txs->dest, 2, 32, NULL);
138 nir_builder_instr_insert(b, &txs->instr);
139
140 return nir_i2f(b, &txs->dest.ssa);
141 }
142
143 static void
144 lower_rect(nir_builder *b, nir_tex_instr *tex)
145 {
146 nir_ssa_def *txs = get_texture_size(b, tex);
147 nir_ssa_def *scale = nir_frcp(b, txs);
148
149 /* Walk through the sources normalizing the requested arguments. */
150 for (unsigned i = 0; i < tex->num_srcs; i++) {
151 if (tex->src[i].src_type != nir_tex_src_coord)
152 continue;
153
154 nir_ssa_def *coords =
155 nir_ssa_for_src(b, tex->src[i].src, tex->coord_components);
156 nir_instr_rewrite_src(&tex->instr,
157 &tex->src[i].src,
158 nir_src_for_ssa(nir_fmul(b, coords, scale)));
159 }
160
161 tex->sampler_dim = GLSL_SAMPLER_DIM_2D;
162 }
163
164 static void
165 saturate_src(nir_builder *b, nir_tex_instr *tex, unsigned sat_mask)
166 {
167 b->cursor = nir_before_instr(&tex->instr);
168
169 /* Walk through the sources saturating the requested arguments. */
170 for (unsigned i = 0; i < tex->num_srcs; i++) {
171 if (tex->src[i].src_type != nir_tex_src_coord)
172 continue;
173
174 nir_ssa_def *src =
175 nir_ssa_for_src(b, tex->src[i].src, tex->coord_components);
176
177 /* split src into components: */
178 nir_ssa_def *comp[4];
179
180 for (unsigned j = 0; j < tex->coord_components; j++)
181 comp[j] = nir_channel(b, src, j);
182
183 /* clamp requested components, array index does not get clamped: */
184 unsigned ncomp = tex->coord_components;
185 if (tex->is_array)
186 ncomp--;
187
188 for (unsigned j = 0; j < ncomp; j++) {
189 if ((1 << j) & sat_mask) {
190 if (tex->sampler_dim == GLSL_SAMPLER_DIM_RECT) {
191 /* non-normalized texture coords, so clamp to texture
192 * size rather than [0.0, 1.0]
193 */
194 nir_ssa_def *txs = get_texture_size(b, tex);
195 comp[j] = nir_fmax(b, comp[j], nir_imm_float(b, 0.0));
196 comp[j] = nir_fmin(b, comp[j], nir_channel(b, txs, j));
197 } else {
198 comp[j] = nir_fsat(b, comp[j]);
199 }
200 }
201 }
202
203 /* and move the result back into a single vecN: */
204 src = nir_vec(b, comp, tex->coord_components);
205
206 nir_instr_rewrite_src(&tex->instr,
207 &tex->src[i].src,
208 nir_src_for_ssa(src));
209 }
210 }
211
212 static nir_ssa_def *
213 get_zero_or_one(nir_builder *b, nir_alu_type type, uint8_t swizzle_val)
214 {
215 nir_const_value v;
216
217 memset(&v, 0, sizeof(v));
218
219 if (swizzle_val == 4) {
220 v.u32[0] = v.u32[1] = v.u32[2] = v.u32[3] = 0;
221 } else {
222 assert(swizzle_val == 5);
223 if (type == nir_type_float)
224 v.f32[0] = v.f32[1] = v.f32[2] = v.f32[3] = 1.0;
225 else
226 v.u32[0] = v.u32[1] = v.u32[2] = v.u32[3] = 1;
227 }
228
229 return nir_build_imm(b, 4, 32, v);
230 }
231
232 static void
233 swizzle_result(nir_builder *b, nir_tex_instr *tex, const uint8_t swizzle[4])
234 {
235 assert(tex->dest.is_ssa);
236
237 b->cursor = nir_after_instr(&tex->instr);
238
239 nir_ssa_def *swizzled;
240 if (tex->op == nir_texop_tg4) {
241 if (swizzle[tex->component] < 4) {
242 /* This one's easy */
243 tex->component = swizzle[tex->component];
244 return;
245 } else {
246 swizzled = get_zero_or_one(b, tex->dest_type, swizzle[tex->component]);
247 }
248 } else {
249 assert(nir_tex_instr_dest_size(tex) == 4);
250 if (swizzle[0] < 4 && swizzle[1] < 4 &&
251 swizzle[2] < 4 && swizzle[3] < 4) {
252 unsigned swiz[4] = { swizzle[0], swizzle[1], swizzle[2], swizzle[3] };
253 /* We have no 0's or 1's, just emit a swizzling MOV */
254 swizzled = nir_swizzle(b, &tex->dest.ssa, swiz, 4, false);
255 } else {
256 nir_ssa_def *srcs[4];
257 for (unsigned i = 0; i < 4; i++) {
258 if (swizzle[i] < 4) {
259 srcs[i] = nir_channel(b, &tex->dest.ssa, swizzle[i]);
260 } else {
261 srcs[i] = get_zero_or_one(b, tex->dest_type, swizzle[i]);
262 }
263 }
264 swizzled = nir_vec(b, srcs, 4);
265 }
266 }
267
268 nir_ssa_def_rewrite_uses_after(&tex->dest.ssa, nir_src_for_ssa(swizzled),
269 swizzled->parent_instr);
270 }
271
272 static void
273 linearize_srgb_result(nir_builder *b, nir_tex_instr *tex)
274 {
275 assert(tex->dest.is_ssa);
276 assert(nir_tex_instr_dest_size(tex) == 4);
277 assert(nir_alu_type_get_base_type(tex->dest_type) == nir_type_float);
278
279 b->cursor = nir_after_instr(&tex->instr);
280
281 static const unsigned swiz[4] = {0, 1, 2, 0};
282 nir_ssa_def *comp = nir_swizzle(b, &tex->dest.ssa, swiz, 3, true);
283
284 /* Formula is:
285 * (comp <= 0.04045) ?
286 * (comp / 12.92) :
287 * pow((comp + 0.055) / 1.055, 2.4)
288 */
289 nir_ssa_def *low = nir_fmul(b, comp, nir_imm_float(b, 1.0 / 12.92));
290 nir_ssa_def *high = nir_fpow(b,
291 nir_fmul(b,
292 nir_fadd(b,
293 comp,
294 nir_imm_float(b, 0.055)),
295 nir_imm_float(b, 1.0 / 1.055)),
296 nir_imm_float(b, 2.4));
297 nir_ssa_def *cond = nir_fge(b, nir_imm_float(b, 0.04045), comp);
298 nir_ssa_def *rgb = nir_bcsel(b, cond, low, high);
299
300 /* alpha is untouched: */
301 nir_ssa_def *result = nir_vec4(b,
302 nir_channel(b, rgb, 0),
303 nir_channel(b, rgb, 1),
304 nir_channel(b, rgb, 2),
305 nir_channel(b, &tex->dest.ssa, 3));
306
307 nir_ssa_def_rewrite_uses_after(&tex->dest.ssa, nir_src_for_ssa(result),
308 result->parent_instr);
309 }
310
311 static bool
312 nir_lower_tex_block(nir_block *block, nir_builder *b,
313 const nir_lower_tex_options *options)
314 {
315 bool progress = false;
316
317 nir_foreach_instr_safe(instr, block) {
318 if (instr->type != nir_instr_type_tex)
319 continue;
320
321 nir_tex_instr *tex = nir_instr_as_tex(instr);
322 bool lower_txp = !!(options->lower_txp & (1 << tex->sampler_dim));
323
324 /* mask of src coords to saturate (clamp): */
325 unsigned sat_mask = 0;
326
327 if ((1 << tex->sampler_index) & options->saturate_r)
328 sat_mask |= (1 << 2); /* .z */
329 if ((1 << tex->sampler_index) & options->saturate_t)
330 sat_mask |= (1 << 1); /* .y */
331 if ((1 << tex->sampler_index) & options->saturate_s)
332 sat_mask |= (1 << 0); /* .x */
333
334 /* If we are clamping any coords, we must lower projector first
335 * as clamping happens *after* projection:
336 */
337 if (lower_txp || sat_mask) {
338 project_src(b, tex);
339 progress = true;
340 }
341
342 if ((tex->sampler_dim == GLSL_SAMPLER_DIM_RECT) && options->lower_rect) {
343 lower_rect(b, tex);
344 progress = true;
345 }
346
347 if (sat_mask) {
348 saturate_src(b, tex, sat_mask);
349 progress = true;
350 }
351
352 if (((1 << tex->texture_index) & options->swizzle_result) &&
353 !nir_tex_instr_is_query(tex) &&
354 !(tex->is_shadow && tex->is_new_style_shadow)) {
355 swizzle_result(b, tex, options->swizzles[tex->texture_index]);
356 progress = true;
357 }
358
359 /* should be after swizzle so we know which channels are rgb: */
360 if (((1 << tex->texture_index) & options->lower_srgb) &&
361 !nir_tex_instr_is_query(tex) && !tex->is_shadow) {
362 linearize_srgb_result(b, tex);
363 progress = true;
364 }
365 }
366
367 return progress;
368 }
369
370 static bool
371 nir_lower_tex_impl(nir_function_impl *impl,
372 const nir_lower_tex_options *options)
373 {
374 bool progress = false;
375 nir_builder builder;
376 nir_builder_init(&builder, impl);
377
378 nir_foreach_block(block, impl) {
379 progress |= nir_lower_tex_block(block, &builder, options);
380 }
381
382 nir_metadata_preserve(impl, nir_metadata_block_index |
383 nir_metadata_dominance);
384 return progress;
385 }
386
387 bool
388 nir_lower_tex(nir_shader *shader, const nir_lower_tex_options *options)
389 {
390 bool progress = false;
391
392 nir_foreach_function(function, shader) {
393 if (function->impl)
394 progress |= nir_lower_tex_impl(function->impl, options);
395 }
396
397 return progress;
398 }