nir: Add a lowering pass for YUV textures
[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 nir_ssa_def *
165 sample_plane(nir_builder *b, nir_tex_instr *tex, int plane)
166 {
167 assert(tex->dest.is_ssa);
168 assert(nir_tex_instr_dest_size(tex) == 4);
169 assert(nir_alu_type_get_base_type(tex->dest_type) == nir_type_float);
170 assert(tex->op == nir_texop_tex);
171 assert(tex->coord_components == 2);
172
173 nir_tex_instr *plane_tex = nir_tex_instr_create(b->shader, 2);
174 nir_src_copy(&plane_tex->src[0].src, &tex->src[0].src, plane_tex);
175 plane_tex->src[0].src_type = nir_tex_src_coord;
176 plane_tex->src[1].src = nir_src_for_ssa(nir_imm_int(b, plane));
177 plane_tex->src[1].src_type = nir_tex_src_plane;
178 plane_tex->op = nir_texop_tex;
179 plane_tex->sampler_dim = 2;
180 plane_tex->dest_type = nir_type_float;
181 plane_tex->coord_components = 2;
182
183 plane_tex->texture_index = tex->texture_index;
184 plane_tex->texture = (nir_deref_var *)
185 nir_copy_deref(plane_tex, &tex->texture->deref);
186 plane_tex->sampler_index = tex->sampler_index;
187 plane_tex->sampler = (nir_deref_var *)
188 nir_copy_deref(plane_tex, &tex->sampler->deref);
189
190 nir_ssa_dest_init(&plane_tex->instr, &plane_tex->dest, 4, 32, NULL);
191
192 nir_builder_instr_insert(b, &plane_tex->instr);
193
194 return &plane_tex->dest.ssa;
195 }
196
197 static void
198 convert_yuv_to_rgb(nir_builder *b, nir_tex_instr *tex,
199 nir_ssa_def *y, nir_ssa_def *u, nir_ssa_def *v)
200 {
201 nir_const_value m[3] = {
202 { .f32 = { 1.0f, 0.0f, 1.59602678f, 0.0f } },
203 { .f32 = { 1.0f, -0.39176229f, -0.81296764f, 0.0f } },
204 { .f32 = { 1.0f, 2.01723214f, 0.0f, 0.0f } }
205 };
206
207 nir_ssa_def *yuv =
208 nir_vec4(b,
209 nir_fmul(b, nir_imm_float(b, 1.16438356f),
210 nir_fadd(b, y, nir_imm_float(b, -0.0625f))),
211 nir_channel(b, nir_fadd(b, u, nir_imm_float(b, -0.5f)), 0),
212 nir_channel(b, nir_fadd(b, v, nir_imm_float(b, -0.5f)), 0),
213 nir_imm_float(b, 0.0));
214
215 nir_ssa_def *red = nir_fdot4(b, yuv, nir_build_imm(b, 4, 32, m[0]));
216 nir_ssa_def *green = nir_fdot4(b, yuv, nir_build_imm(b, 4, 32, m[1]));
217 nir_ssa_def *blue = nir_fdot4(b, yuv, nir_build_imm(b, 4, 32, m[2]));
218
219 nir_ssa_def *result = nir_vec4(b, red, green, blue, nir_imm_float(b, 1.0f));
220
221 nir_ssa_def_rewrite_uses(&tex->dest.ssa, nir_src_for_ssa(result));
222 }
223
224 static void
225 lower_y_uv_external(nir_builder *b, nir_tex_instr *tex)
226 {
227 b->cursor = nir_after_instr(&tex->instr);
228
229 nir_ssa_def *y = sample_plane(b, tex, 0);
230 nir_ssa_def *uv = sample_plane(b, tex, 1);
231
232 convert_yuv_to_rgb(b, tex,
233 nir_channel(b, y, 0),
234 nir_channel(b, uv, 0),
235 nir_channel(b, uv, 1));
236 }
237
238 static void
239 lower_y_u_v_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 *u = sample_plane(b, tex, 1);
245 nir_ssa_def *v = sample_plane(b, tex, 2);
246
247 convert_yuv_to_rgb(b, tex,
248 nir_channel(b, y, 0),
249 nir_channel(b, u, 0),
250 nir_channel(b, v, 0));
251 }
252
253 static void
254 lower_yx_xuxv_external(nir_builder *b, nir_tex_instr *tex)
255 {
256 b->cursor = nir_after_instr(&tex->instr);
257
258 nir_ssa_def *y = sample_plane(b, tex, 0);
259 nir_ssa_def *xuxv = sample_plane(b, tex, 1);
260
261 convert_yuv_to_rgb(b, tex,
262 nir_channel(b, y, 0),
263 nir_channel(b, xuxv, 1),
264 nir_channel(b, xuxv, 3));
265 }
266
267 static void
268 saturate_src(nir_builder *b, nir_tex_instr *tex, unsigned sat_mask)
269 {
270 b->cursor = nir_before_instr(&tex->instr);
271
272 /* Walk through the sources saturating the requested arguments. */
273 for (unsigned i = 0; i < tex->num_srcs; i++) {
274 if (tex->src[i].src_type != nir_tex_src_coord)
275 continue;
276
277 nir_ssa_def *src =
278 nir_ssa_for_src(b, tex->src[i].src, tex->coord_components);
279
280 /* split src into components: */
281 nir_ssa_def *comp[4];
282
283 assume(tex->coord_components >= 1);
284
285 for (unsigned j = 0; j < tex->coord_components; j++)
286 comp[j] = nir_channel(b, src, j);
287
288 /* clamp requested components, array index does not get clamped: */
289 unsigned ncomp = tex->coord_components;
290 if (tex->is_array)
291 ncomp--;
292
293 for (unsigned j = 0; j < ncomp; j++) {
294 if ((1 << j) & sat_mask) {
295 if (tex->sampler_dim == GLSL_SAMPLER_DIM_RECT) {
296 /* non-normalized texture coords, so clamp to texture
297 * size rather than [0.0, 1.0]
298 */
299 nir_ssa_def *txs = get_texture_size(b, tex);
300 comp[j] = nir_fmax(b, comp[j], nir_imm_float(b, 0.0));
301 comp[j] = nir_fmin(b, comp[j], nir_channel(b, txs, j));
302 } else {
303 comp[j] = nir_fsat(b, comp[j]);
304 }
305 }
306 }
307
308 /* and move the result back into a single vecN: */
309 src = nir_vec(b, comp, tex->coord_components);
310
311 nir_instr_rewrite_src(&tex->instr,
312 &tex->src[i].src,
313 nir_src_for_ssa(src));
314 }
315 }
316
317 static nir_ssa_def *
318 get_zero_or_one(nir_builder *b, nir_alu_type type, uint8_t swizzle_val)
319 {
320 nir_const_value v;
321
322 memset(&v, 0, sizeof(v));
323
324 if (swizzle_val == 4) {
325 v.u32[0] = v.u32[1] = v.u32[2] = v.u32[3] = 0;
326 } else {
327 assert(swizzle_val == 5);
328 if (type == nir_type_float)
329 v.f32[0] = v.f32[1] = v.f32[2] = v.f32[3] = 1.0;
330 else
331 v.u32[0] = v.u32[1] = v.u32[2] = v.u32[3] = 1;
332 }
333
334 return nir_build_imm(b, 4, 32, v);
335 }
336
337 static void
338 swizzle_result(nir_builder *b, nir_tex_instr *tex, const uint8_t swizzle[4])
339 {
340 assert(tex->dest.is_ssa);
341
342 b->cursor = nir_after_instr(&tex->instr);
343
344 nir_ssa_def *swizzled;
345 if (tex->op == nir_texop_tg4) {
346 if (swizzle[tex->component] < 4) {
347 /* This one's easy */
348 tex->component = swizzle[tex->component];
349 return;
350 } else {
351 swizzled = get_zero_or_one(b, tex->dest_type, swizzle[tex->component]);
352 }
353 } else {
354 assert(nir_tex_instr_dest_size(tex) == 4);
355 if (swizzle[0] < 4 && swizzle[1] < 4 &&
356 swizzle[2] < 4 && swizzle[3] < 4) {
357 unsigned swiz[4] = { swizzle[0], swizzle[1], swizzle[2], swizzle[3] };
358 /* We have no 0's or 1's, just emit a swizzling MOV */
359 swizzled = nir_swizzle(b, &tex->dest.ssa, swiz, 4, false);
360 } else {
361 nir_ssa_def *srcs[4];
362 for (unsigned i = 0; i < 4; i++) {
363 if (swizzle[i] < 4) {
364 srcs[i] = nir_channel(b, &tex->dest.ssa, swizzle[i]);
365 } else {
366 srcs[i] = get_zero_or_one(b, tex->dest_type, swizzle[i]);
367 }
368 }
369 swizzled = nir_vec(b, srcs, 4);
370 }
371 }
372
373 nir_ssa_def_rewrite_uses_after(&tex->dest.ssa, nir_src_for_ssa(swizzled),
374 swizzled->parent_instr);
375 }
376
377 static void
378 linearize_srgb_result(nir_builder *b, nir_tex_instr *tex)
379 {
380 assert(tex->dest.is_ssa);
381 assert(nir_tex_instr_dest_size(tex) == 4);
382 assert(nir_alu_type_get_base_type(tex->dest_type) == nir_type_float);
383
384 b->cursor = nir_after_instr(&tex->instr);
385
386 static const unsigned swiz[4] = {0, 1, 2, 0};
387 nir_ssa_def *comp = nir_swizzle(b, &tex->dest.ssa, swiz, 3, true);
388
389 /* Formula is:
390 * (comp <= 0.04045) ?
391 * (comp / 12.92) :
392 * pow((comp + 0.055) / 1.055, 2.4)
393 */
394 nir_ssa_def *low = nir_fmul(b, comp, nir_imm_float(b, 1.0 / 12.92));
395 nir_ssa_def *high = nir_fpow(b,
396 nir_fmul(b,
397 nir_fadd(b,
398 comp,
399 nir_imm_float(b, 0.055)),
400 nir_imm_float(b, 1.0 / 1.055)),
401 nir_imm_float(b, 2.4));
402 nir_ssa_def *cond = nir_fge(b, nir_imm_float(b, 0.04045), comp);
403 nir_ssa_def *rgb = nir_bcsel(b, cond, low, high);
404
405 /* alpha is untouched: */
406 nir_ssa_def *result = nir_vec4(b,
407 nir_channel(b, rgb, 0),
408 nir_channel(b, rgb, 1),
409 nir_channel(b, rgb, 2),
410 nir_channel(b, &tex->dest.ssa, 3));
411
412 nir_ssa_def_rewrite_uses_after(&tex->dest.ssa, nir_src_for_ssa(result),
413 result->parent_instr);
414 }
415
416 static bool
417 nir_lower_tex_block(nir_block *block, nir_builder *b,
418 const nir_lower_tex_options *options)
419 {
420 bool progress = false;
421
422 nir_foreach_instr_safe(instr, block) {
423 if (instr->type != nir_instr_type_tex)
424 continue;
425
426 nir_tex_instr *tex = nir_instr_as_tex(instr);
427 bool lower_txp = !!(options->lower_txp & (1 << tex->sampler_dim));
428
429 /* mask of src coords to saturate (clamp): */
430 unsigned sat_mask = 0;
431
432 if ((1 << tex->sampler_index) & options->saturate_r)
433 sat_mask |= (1 << 2); /* .z */
434 if ((1 << tex->sampler_index) & options->saturate_t)
435 sat_mask |= (1 << 1); /* .y */
436 if ((1 << tex->sampler_index) & options->saturate_s)
437 sat_mask |= (1 << 0); /* .x */
438
439 /* If we are clamping any coords, we must lower projector first
440 * as clamping happens *after* projection:
441 */
442 if (lower_txp || sat_mask) {
443 project_src(b, tex);
444 progress = true;
445 }
446
447 if ((tex->sampler_dim == GLSL_SAMPLER_DIM_RECT) && options->lower_rect) {
448 lower_rect(b, tex);
449 progress = true;
450 }
451
452 if ((1 << tex->texture_index) & options->lower_y_uv_external) {
453 lower_y_uv_external(b, tex);
454 progress = true;
455 }
456
457 if ((1 << tex->texture_index) & options->lower_y_u_v_external) {
458 lower_y_u_v_external(b, tex);
459 progress = true;
460 }
461
462 if ((1 << tex->texture_index) & options->lower_yx_xuxv_external) {
463 lower_yx_xuxv_external(b, tex);
464 progress = true;
465 }
466
467
468 if (sat_mask) {
469 saturate_src(b, tex, sat_mask);
470 progress = true;
471 }
472
473 if (((1 << tex->texture_index) & options->swizzle_result) &&
474 !nir_tex_instr_is_query(tex) &&
475 !(tex->is_shadow && tex->is_new_style_shadow)) {
476 swizzle_result(b, tex, options->swizzles[tex->texture_index]);
477 progress = true;
478 }
479
480 /* should be after swizzle so we know which channels are rgb: */
481 if (((1 << tex->texture_index) & options->lower_srgb) &&
482 !nir_tex_instr_is_query(tex) && !tex->is_shadow) {
483 linearize_srgb_result(b, tex);
484 progress = true;
485 }
486 }
487
488 return progress;
489 }
490
491 static bool
492 nir_lower_tex_impl(nir_function_impl *impl,
493 const nir_lower_tex_options *options)
494 {
495 bool progress = false;
496 nir_builder builder;
497 nir_builder_init(&builder, impl);
498
499 nir_foreach_block(block, impl) {
500 progress |= nir_lower_tex_block(block, &builder, options);
501 }
502
503 nir_metadata_preserve(impl, nir_metadata_block_index |
504 nir_metadata_dominance);
505 return progress;
506 }
507
508 bool
509 nir_lower_tex(nir_shader *shader, const nir_lower_tex_options *options)
510 {
511 bool progress = false;
512
513 nir_foreach_function(function, shader) {
514 if (function->impl)
515 progress |= nir_lower_tex_impl(function->impl, options);
516 }
517
518 return progress;
519 }