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