nir/lower_tex: Add some helpers for working with tex sources
[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 int
42 tex_instr_find_src(nir_tex_instr *tex, nir_tex_src_type src_type)
43 {
44 for (unsigned i = 0; i < tex->num_srcs; i++) {
45 if (tex->src[i].src_type == src_type)
46 return i;
47 }
48
49 return -1;
50 }
51
52 static void
53 tex_instr_remove_src(nir_tex_instr *tex, unsigned src_idx)
54 {
55 assert(src_idx < tex->num_srcs);
56
57 /* First rewrite the source to NIR_SRC_INIT */
58 nir_instr_rewrite_src(&tex->instr, &tex->src[src_idx].src, NIR_SRC_INIT);
59
60 /* Now, move all of the other sources down */
61 for (unsigned i = src_idx + 1; i < tex->num_srcs; i++) {
62 tex->src[i-1].src_type = tex->src[i].src_type;
63 nir_instr_move_src(&tex->instr, &tex->src[i-1].src, &tex->src[i].src);
64 }
65 tex->num_srcs--;
66 }
67
68 static void
69 project_src(nir_builder *b, nir_tex_instr *tex)
70 {
71 /* Find the projector in the srcs list, if present. */
72 int proj_index = tex_instr_find_src(tex, nir_tex_src_projector);
73 if (proj_index < 0)
74 return;
75
76 b->cursor = nir_before_instr(&tex->instr);
77
78 nir_ssa_def *inv_proj =
79 nir_frcp(b, nir_ssa_for_src(b, tex->src[proj_index].src, 1));
80
81 /* Walk through the sources projecting the arguments. */
82 for (unsigned i = 0; i < tex->num_srcs; i++) {
83 switch (tex->src[i].src_type) {
84 case nir_tex_src_coord:
85 case nir_tex_src_comparitor:
86 break;
87 default:
88 continue;
89 }
90 nir_ssa_def *unprojected =
91 nir_ssa_for_src(b, tex->src[i].src, nir_tex_instr_src_size(tex, i));
92 nir_ssa_def *projected = nir_fmul(b, unprojected, inv_proj);
93
94 /* Array indices don't get projected, so make an new vector with the
95 * coordinate's array index untouched.
96 */
97 if (tex->is_array && tex->src[i].src_type == nir_tex_src_coord) {
98 switch (tex->coord_components) {
99 case 4:
100 projected = nir_vec4(b,
101 nir_channel(b, projected, 0),
102 nir_channel(b, projected, 1),
103 nir_channel(b, projected, 2),
104 nir_channel(b, unprojected, 3));
105 break;
106 case 3:
107 projected = nir_vec3(b,
108 nir_channel(b, projected, 0),
109 nir_channel(b, projected, 1),
110 nir_channel(b, unprojected, 2));
111 break;
112 case 2:
113 projected = nir_vec2(b,
114 nir_channel(b, projected, 0),
115 nir_channel(b, unprojected, 1));
116 break;
117 default:
118 unreachable("bad texture coord count for array");
119 break;
120 }
121 }
122
123 nir_instr_rewrite_src(&tex->instr,
124 &tex->src[i].src,
125 nir_src_for_ssa(projected));
126 }
127
128 tex_instr_remove_src(tex, proj_index);
129 }
130
131 static nir_ssa_def *
132 get_texture_size(nir_builder *b, nir_tex_instr *tex)
133 {
134 b->cursor = nir_before_instr(&tex->instr);
135
136 /* RECT textures should not be array: */
137 assert(!tex->is_array);
138
139 nir_tex_instr *txs;
140
141 txs = nir_tex_instr_create(b->shader, 1);
142 txs->op = nir_texop_txs;
143 txs->sampler_dim = GLSL_SAMPLER_DIM_RECT;
144 txs->texture_index = tex->texture_index;
145 txs->dest_type = nir_type_int;
146
147 /* only single src, the lod: */
148 txs->src[0].src = nir_src_for_ssa(nir_imm_int(b, 0));
149 txs->src[0].src_type = nir_tex_src_lod;
150
151 nir_ssa_dest_init(&txs->instr, &txs->dest, 2, 32, NULL);
152 nir_builder_instr_insert(b, &txs->instr);
153
154 return nir_i2f(b, &txs->dest.ssa);
155 }
156
157 static void
158 lower_rect(nir_builder *b, nir_tex_instr *tex)
159 {
160 nir_ssa_def *txs = get_texture_size(b, tex);
161 nir_ssa_def *scale = nir_frcp(b, txs);
162
163 /* Walk through the sources normalizing the requested arguments. */
164 for (unsigned i = 0; i < tex->num_srcs; i++) {
165 if (tex->src[i].src_type != nir_tex_src_coord)
166 continue;
167
168 nir_ssa_def *coords =
169 nir_ssa_for_src(b, tex->src[i].src, tex->coord_components);
170 nir_instr_rewrite_src(&tex->instr,
171 &tex->src[i].src,
172 nir_src_for_ssa(nir_fmul(b, coords, scale)));
173 }
174
175 tex->sampler_dim = GLSL_SAMPLER_DIM_2D;
176 }
177
178 static nir_ssa_def *
179 sample_plane(nir_builder *b, nir_tex_instr *tex, int plane)
180 {
181 assert(tex->dest.is_ssa);
182 assert(nir_tex_instr_dest_size(tex) == 4);
183 assert(nir_alu_type_get_base_type(tex->dest_type) == nir_type_float);
184 assert(tex->op == nir_texop_tex);
185 assert(tex->coord_components == 2);
186
187 nir_tex_instr *plane_tex = nir_tex_instr_create(b->shader, 2);
188 nir_src_copy(&plane_tex->src[0].src, &tex->src[0].src, plane_tex);
189 plane_tex->src[0].src_type = nir_tex_src_coord;
190 plane_tex->src[1].src = nir_src_for_ssa(nir_imm_int(b, plane));
191 plane_tex->src[1].src_type = nir_tex_src_plane;
192 plane_tex->op = nir_texop_tex;
193 plane_tex->sampler_dim = 2;
194 plane_tex->dest_type = nir_type_float;
195 plane_tex->coord_components = 2;
196
197 plane_tex->texture_index = tex->texture_index;
198 plane_tex->texture = (nir_deref_var *)
199 nir_copy_deref(plane_tex, &tex->texture->deref);
200 plane_tex->sampler_index = tex->sampler_index;
201 plane_tex->sampler = (nir_deref_var *)
202 nir_copy_deref(plane_tex, &tex->sampler->deref);
203
204 nir_ssa_dest_init(&plane_tex->instr, &plane_tex->dest, 4, 32, NULL);
205
206 nir_builder_instr_insert(b, &plane_tex->instr);
207
208 return &plane_tex->dest.ssa;
209 }
210
211 static void
212 convert_yuv_to_rgb(nir_builder *b, nir_tex_instr *tex,
213 nir_ssa_def *y, nir_ssa_def *u, nir_ssa_def *v)
214 {
215 nir_const_value m[3] = {
216 { .f32 = { 1.0f, 0.0f, 1.59602678f, 0.0f } },
217 { .f32 = { 1.0f, -0.39176229f, -0.81296764f, 0.0f } },
218 { .f32 = { 1.0f, 2.01723214f, 0.0f, 0.0f } }
219 };
220
221 nir_ssa_def *yuv =
222 nir_vec4(b,
223 nir_fmul(b, nir_imm_float(b, 1.16438356f),
224 nir_fadd(b, y, nir_imm_float(b, -0.0625f))),
225 nir_channel(b, nir_fadd(b, u, nir_imm_float(b, -0.5f)), 0),
226 nir_channel(b, nir_fadd(b, v, nir_imm_float(b, -0.5f)), 0),
227 nir_imm_float(b, 0.0));
228
229 nir_ssa_def *red = nir_fdot4(b, yuv, nir_build_imm(b, 4, 32, m[0]));
230 nir_ssa_def *green = nir_fdot4(b, yuv, nir_build_imm(b, 4, 32, m[1]));
231 nir_ssa_def *blue = nir_fdot4(b, yuv, nir_build_imm(b, 4, 32, m[2]));
232
233 nir_ssa_def *result = nir_vec4(b, red, green, blue, nir_imm_float(b, 1.0f));
234
235 nir_ssa_def_rewrite_uses(&tex->dest.ssa, nir_src_for_ssa(result));
236 }
237
238 static void
239 lower_y_uv_external(nir_builder *b, nir_tex_instr *tex)
240 {
241 b->cursor = nir_after_instr(&tex->instr);
242
243 nir_ssa_def *y = sample_plane(b, tex, 0);
244 nir_ssa_def *uv = sample_plane(b, tex, 1);
245
246 convert_yuv_to_rgb(b, tex,
247 nir_channel(b, y, 0),
248 nir_channel(b, uv, 0),
249 nir_channel(b, uv, 1));
250 }
251
252 static void
253 lower_y_u_v_external(nir_builder *b, nir_tex_instr *tex)
254 {
255 b->cursor = nir_after_instr(&tex->instr);
256
257 nir_ssa_def *y = sample_plane(b, tex, 0);
258 nir_ssa_def *u = sample_plane(b, tex, 1);
259 nir_ssa_def *v = sample_plane(b, tex, 2);
260
261 convert_yuv_to_rgb(b, tex,
262 nir_channel(b, y, 0),
263 nir_channel(b, u, 0),
264 nir_channel(b, v, 0));
265 }
266
267 static void
268 lower_yx_xuxv_external(nir_builder *b, nir_tex_instr *tex)
269 {
270 b->cursor = nir_after_instr(&tex->instr);
271
272 nir_ssa_def *y = sample_plane(b, tex, 0);
273 nir_ssa_def *xuxv = sample_plane(b, tex, 1);
274
275 convert_yuv_to_rgb(b, tex,
276 nir_channel(b, y, 0),
277 nir_channel(b, xuxv, 1),
278 nir_channel(b, xuxv, 3));
279 }
280
281 static void
282 saturate_src(nir_builder *b, nir_tex_instr *tex, unsigned sat_mask)
283 {
284 b->cursor = nir_before_instr(&tex->instr);
285
286 /* Walk through the sources saturating the requested arguments. */
287 for (unsigned i = 0; i < tex->num_srcs; i++) {
288 if (tex->src[i].src_type != nir_tex_src_coord)
289 continue;
290
291 nir_ssa_def *src =
292 nir_ssa_for_src(b, tex->src[i].src, tex->coord_components);
293
294 /* split src into components: */
295 nir_ssa_def *comp[4];
296
297 assume(tex->coord_components >= 1);
298
299 for (unsigned j = 0; j < tex->coord_components; j++)
300 comp[j] = nir_channel(b, src, j);
301
302 /* clamp requested components, array index does not get clamped: */
303 unsigned ncomp = tex->coord_components;
304 if (tex->is_array)
305 ncomp--;
306
307 for (unsigned j = 0; j < ncomp; j++) {
308 if ((1 << j) & sat_mask) {
309 if (tex->sampler_dim == GLSL_SAMPLER_DIM_RECT) {
310 /* non-normalized texture coords, so clamp to texture
311 * size rather than [0.0, 1.0]
312 */
313 nir_ssa_def *txs = get_texture_size(b, tex);
314 comp[j] = nir_fmax(b, comp[j], nir_imm_float(b, 0.0));
315 comp[j] = nir_fmin(b, comp[j], nir_channel(b, txs, j));
316 } else {
317 comp[j] = nir_fsat(b, comp[j]);
318 }
319 }
320 }
321
322 /* and move the result back into a single vecN: */
323 src = nir_vec(b, comp, tex->coord_components);
324
325 nir_instr_rewrite_src(&tex->instr,
326 &tex->src[i].src,
327 nir_src_for_ssa(src));
328 }
329 }
330
331 static nir_ssa_def *
332 get_zero_or_one(nir_builder *b, nir_alu_type type, uint8_t swizzle_val)
333 {
334 nir_const_value v;
335
336 memset(&v, 0, sizeof(v));
337
338 if (swizzle_val == 4) {
339 v.u32[0] = v.u32[1] = v.u32[2] = v.u32[3] = 0;
340 } else {
341 assert(swizzle_val == 5);
342 if (type == nir_type_float)
343 v.f32[0] = v.f32[1] = v.f32[2] = v.f32[3] = 1.0;
344 else
345 v.u32[0] = v.u32[1] = v.u32[2] = v.u32[3] = 1;
346 }
347
348 return nir_build_imm(b, 4, 32, v);
349 }
350
351 static void
352 swizzle_result(nir_builder *b, nir_tex_instr *tex, const uint8_t swizzle[4])
353 {
354 assert(tex->dest.is_ssa);
355
356 b->cursor = nir_after_instr(&tex->instr);
357
358 nir_ssa_def *swizzled;
359 if (tex->op == nir_texop_tg4) {
360 if (swizzle[tex->component] < 4) {
361 /* This one's easy */
362 tex->component = swizzle[tex->component];
363 return;
364 } else {
365 swizzled = get_zero_or_one(b, tex->dest_type, swizzle[tex->component]);
366 }
367 } else {
368 assert(nir_tex_instr_dest_size(tex) == 4);
369 if (swizzle[0] < 4 && swizzle[1] < 4 &&
370 swizzle[2] < 4 && swizzle[3] < 4) {
371 unsigned swiz[4] = { swizzle[0], swizzle[1], swizzle[2], swizzle[3] };
372 /* We have no 0's or 1's, just emit a swizzling MOV */
373 swizzled = nir_swizzle(b, &tex->dest.ssa, swiz, 4, false);
374 } else {
375 nir_ssa_def *srcs[4];
376 for (unsigned i = 0; i < 4; i++) {
377 if (swizzle[i] < 4) {
378 srcs[i] = nir_channel(b, &tex->dest.ssa, swizzle[i]);
379 } else {
380 srcs[i] = get_zero_or_one(b, tex->dest_type, swizzle[i]);
381 }
382 }
383 swizzled = nir_vec(b, srcs, 4);
384 }
385 }
386
387 nir_ssa_def_rewrite_uses_after(&tex->dest.ssa, nir_src_for_ssa(swizzled),
388 swizzled->parent_instr);
389 }
390
391 static void
392 linearize_srgb_result(nir_builder *b, nir_tex_instr *tex)
393 {
394 assert(tex->dest.is_ssa);
395 assert(nir_tex_instr_dest_size(tex) == 4);
396 assert(nir_alu_type_get_base_type(tex->dest_type) == nir_type_float);
397
398 b->cursor = nir_after_instr(&tex->instr);
399
400 static const unsigned swiz[4] = {0, 1, 2, 0};
401 nir_ssa_def *comp = nir_swizzle(b, &tex->dest.ssa, swiz, 3, true);
402
403 /* Formula is:
404 * (comp <= 0.04045) ?
405 * (comp / 12.92) :
406 * pow((comp + 0.055) / 1.055, 2.4)
407 */
408 nir_ssa_def *low = nir_fmul(b, comp, nir_imm_float(b, 1.0 / 12.92));
409 nir_ssa_def *high = nir_fpow(b,
410 nir_fmul(b,
411 nir_fadd(b,
412 comp,
413 nir_imm_float(b, 0.055)),
414 nir_imm_float(b, 1.0 / 1.055)),
415 nir_imm_float(b, 2.4));
416 nir_ssa_def *cond = nir_fge(b, nir_imm_float(b, 0.04045), comp);
417 nir_ssa_def *rgb = nir_bcsel(b, cond, low, high);
418
419 /* alpha is untouched: */
420 nir_ssa_def *result = nir_vec4(b,
421 nir_channel(b, rgb, 0),
422 nir_channel(b, rgb, 1),
423 nir_channel(b, rgb, 2),
424 nir_channel(b, &tex->dest.ssa, 3));
425
426 nir_ssa_def_rewrite_uses_after(&tex->dest.ssa, nir_src_for_ssa(result),
427 result->parent_instr);
428 }
429
430 static bool
431 nir_lower_tex_block(nir_block *block, nir_builder *b,
432 const nir_lower_tex_options *options)
433 {
434 bool progress = false;
435
436 nir_foreach_instr_safe(instr, block) {
437 if (instr->type != nir_instr_type_tex)
438 continue;
439
440 nir_tex_instr *tex = nir_instr_as_tex(instr);
441 bool lower_txp = !!(options->lower_txp & (1 << tex->sampler_dim));
442
443 /* mask of src coords to saturate (clamp): */
444 unsigned sat_mask = 0;
445
446 if ((1 << tex->sampler_index) & options->saturate_r)
447 sat_mask |= (1 << 2); /* .z */
448 if ((1 << tex->sampler_index) & options->saturate_t)
449 sat_mask |= (1 << 1); /* .y */
450 if ((1 << tex->sampler_index) & options->saturate_s)
451 sat_mask |= (1 << 0); /* .x */
452
453 /* If we are clamping any coords, we must lower projector first
454 * as clamping happens *after* projection:
455 */
456 if (lower_txp || sat_mask) {
457 project_src(b, tex);
458 progress = true;
459 }
460
461 if ((tex->sampler_dim == GLSL_SAMPLER_DIM_RECT) && options->lower_rect) {
462 lower_rect(b, tex);
463 progress = true;
464 }
465
466 if ((1 << tex->texture_index) & options->lower_y_uv_external) {
467 lower_y_uv_external(b, tex);
468 progress = true;
469 }
470
471 if ((1 << tex->texture_index) & options->lower_y_u_v_external) {
472 lower_y_u_v_external(b, tex);
473 progress = true;
474 }
475
476 if ((1 << tex->texture_index) & options->lower_yx_xuxv_external) {
477 lower_yx_xuxv_external(b, tex);
478 progress = true;
479 }
480
481
482 if (sat_mask) {
483 saturate_src(b, tex, sat_mask);
484 progress = true;
485 }
486
487 if (((1 << tex->texture_index) & options->swizzle_result) &&
488 !nir_tex_instr_is_query(tex) &&
489 !(tex->is_shadow && tex->is_new_style_shadow)) {
490 swizzle_result(b, tex, options->swizzles[tex->texture_index]);
491 progress = true;
492 }
493
494 /* should be after swizzle so we know which channels are rgb: */
495 if (((1 << tex->texture_index) & options->lower_srgb) &&
496 !nir_tex_instr_is_query(tex) && !tex->is_shadow) {
497 linearize_srgb_result(b, tex);
498 progress = true;
499 }
500 }
501
502 return progress;
503 }
504
505 static bool
506 nir_lower_tex_impl(nir_function_impl *impl,
507 const nir_lower_tex_options *options)
508 {
509 bool progress = false;
510 nir_builder builder;
511 nir_builder_init(&builder, impl);
512
513 nir_foreach_block(block, impl) {
514 progress |= nir_lower_tex_block(block, &builder, options);
515 }
516
517 nir_metadata_preserve(impl, nir_metadata_block_index |
518 nir_metadata_dominance);
519 return progress;
520 }
521
522 bool
523 nir_lower_tex(nir_shader *shader, const nir_lower_tex_options *options)
524 {
525 bool progress = false;
526
527 nir_foreach_function(function, shader) {
528 if (function->impl)
529 progress |= nir_lower_tex_impl(function->impl, options);
530 }
531
532 return progress;
533 }