nir: Add a new memory_barrier_tcs_patch intrinsic
[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 #include "nir_builtin_builder.h"
41 #include "nir_format_convert.h"
42
43 static bool
44 project_src(nir_builder *b, nir_tex_instr *tex)
45 {
46 /* Find the projector in the srcs list, if present. */
47 int proj_index = nir_tex_instr_src_index(tex, nir_tex_src_projector);
48 if (proj_index < 0)
49 return false;
50
51 b->cursor = nir_before_instr(&tex->instr);
52
53 nir_ssa_def *inv_proj =
54 nir_frcp(b, nir_ssa_for_src(b, tex->src[proj_index].src, 1));
55
56 /* Walk through the sources projecting the arguments. */
57 for (unsigned i = 0; i < tex->num_srcs; i++) {
58 switch (tex->src[i].src_type) {
59 case nir_tex_src_coord:
60 case nir_tex_src_comparator:
61 break;
62 default:
63 continue;
64 }
65 nir_ssa_def *unprojected =
66 nir_ssa_for_src(b, tex->src[i].src, nir_tex_instr_src_size(tex, i));
67 nir_ssa_def *projected = nir_fmul(b, unprojected, inv_proj);
68
69 /* Array indices don't get projected, so make an new vector with the
70 * coordinate's array index untouched.
71 */
72 if (tex->is_array && tex->src[i].src_type == nir_tex_src_coord) {
73 switch (tex->coord_components) {
74 case 4:
75 projected = nir_vec4(b,
76 nir_channel(b, projected, 0),
77 nir_channel(b, projected, 1),
78 nir_channel(b, projected, 2),
79 nir_channel(b, unprojected, 3));
80 break;
81 case 3:
82 projected = nir_vec3(b,
83 nir_channel(b, projected, 0),
84 nir_channel(b, projected, 1),
85 nir_channel(b, unprojected, 2));
86 break;
87 case 2:
88 projected = nir_vec2(b,
89 nir_channel(b, projected, 0),
90 nir_channel(b, unprojected, 1));
91 break;
92 default:
93 unreachable("bad texture coord count for array");
94 break;
95 }
96 }
97
98 nir_instr_rewrite_src(&tex->instr,
99 &tex->src[i].src,
100 nir_src_for_ssa(projected));
101 }
102
103 nir_tex_instr_remove_src(tex, proj_index);
104 return true;
105 }
106
107 static bool
108 lower_offset(nir_builder *b, nir_tex_instr *tex)
109 {
110 int offset_index = nir_tex_instr_src_index(tex, nir_tex_src_offset);
111 if (offset_index < 0)
112 return false;
113
114 int coord_index = nir_tex_instr_src_index(tex, nir_tex_src_coord);
115 assert(coord_index >= 0);
116
117 assert(tex->src[offset_index].src.is_ssa);
118 assert(tex->src[coord_index].src.is_ssa);
119 nir_ssa_def *offset = tex->src[offset_index].src.ssa;
120 nir_ssa_def *coord = tex->src[coord_index].src.ssa;
121
122 b->cursor = nir_before_instr(&tex->instr);
123
124 nir_ssa_def *offset_coord;
125 if (nir_tex_instr_src_type(tex, coord_index) == nir_type_float) {
126 if (tex->sampler_dim == GLSL_SAMPLER_DIM_RECT) {
127 offset_coord = nir_fadd(b, coord, nir_i2f32(b, offset));
128 } else {
129 nir_ssa_def *txs = nir_get_texture_size(b, tex);
130 nir_ssa_def *scale = nir_frcp(b, txs);
131
132 offset_coord = nir_fadd(b, coord,
133 nir_fmul(b,
134 nir_i2f32(b, offset),
135 scale));
136 }
137 } else {
138 offset_coord = nir_iadd(b, coord, offset);
139 }
140
141 if (tex->is_array) {
142 /* The offset is not applied to the array index */
143 if (tex->coord_components == 2) {
144 offset_coord = nir_vec2(b, nir_channel(b, offset_coord, 0),
145 nir_channel(b, coord, 1));
146 } else if (tex->coord_components == 3) {
147 offset_coord = nir_vec3(b, nir_channel(b, offset_coord, 0),
148 nir_channel(b, offset_coord, 1),
149 nir_channel(b, coord, 2));
150 } else {
151 unreachable("Invalid number of components");
152 }
153 }
154
155 nir_instr_rewrite_src(&tex->instr, &tex->src[coord_index].src,
156 nir_src_for_ssa(offset_coord));
157
158 nir_tex_instr_remove_src(tex, offset_index);
159
160 return true;
161 }
162
163 static void
164 lower_rect(nir_builder *b, nir_tex_instr *tex)
165 {
166 /* Set the sampler_dim to 2D here so that get_texture_size picks up the
167 * right dimensionality.
168 */
169 tex->sampler_dim = GLSL_SAMPLER_DIM_2D;
170
171 nir_ssa_def *txs = nir_get_texture_size(b, tex);
172 nir_ssa_def *scale = nir_frcp(b, txs);
173
174 /* Walk through the sources normalizing the requested arguments. */
175 for (unsigned i = 0; i < tex->num_srcs; i++) {
176 if (tex->src[i].src_type != nir_tex_src_coord)
177 continue;
178
179 nir_ssa_def *coords =
180 nir_ssa_for_src(b, tex->src[i].src, tex->coord_components);
181 nir_instr_rewrite_src(&tex->instr,
182 &tex->src[i].src,
183 nir_src_for_ssa(nir_fmul(b, coords, scale)));
184 }
185 }
186
187 static void
188 lower_implicit_lod(nir_builder *b, nir_tex_instr *tex)
189 {
190 assert(tex->op == nir_texop_tex || tex->op == nir_texop_txb);
191 assert(nir_tex_instr_src_index(tex, nir_tex_src_lod) < 0);
192 assert(nir_tex_instr_src_index(tex, nir_tex_src_ddx) < 0);
193 assert(nir_tex_instr_src_index(tex, nir_tex_src_ddy) < 0);
194
195 b->cursor = nir_before_instr(&tex->instr);
196
197 nir_ssa_def *lod = nir_get_texture_lod(b, tex);
198
199 int bias_idx = nir_tex_instr_src_index(tex, nir_tex_src_bias);
200 if (bias_idx >= 0) {
201 /* If we have a bias, add it in */
202 lod = nir_fadd(b, lod, nir_ssa_for_src(b, tex->src[bias_idx].src, 1));
203 nir_tex_instr_remove_src(tex, bias_idx);
204 }
205
206 int min_lod_idx = nir_tex_instr_src_index(tex, nir_tex_src_min_lod);
207 if (min_lod_idx >= 0) {
208 /* If we have a minimum LOD, clamp LOD accordingly */
209 lod = nir_fmax(b, lod, nir_ssa_for_src(b, tex->src[min_lod_idx].src, 1));
210 nir_tex_instr_remove_src(tex, min_lod_idx);
211 }
212
213 nir_tex_instr_add_src(tex, nir_tex_src_lod, nir_src_for_ssa(lod));
214 tex->op = nir_texop_txl;
215 }
216
217 static nir_ssa_def *
218 sample_plane(nir_builder *b, nir_tex_instr *tex, int plane,
219 const nir_lower_tex_options *options)
220 {
221 assert(tex->dest.is_ssa);
222 assert(nir_tex_instr_dest_size(tex) == 4);
223 assert(nir_alu_type_get_base_type(tex->dest_type) == nir_type_float);
224 assert(tex->op == nir_texop_tex);
225 assert(tex->coord_components == 2);
226
227 nir_tex_instr *plane_tex =
228 nir_tex_instr_create(b->shader, tex->num_srcs + 1);
229 for (unsigned i = 0; i < tex->num_srcs; i++) {
230 nir_src_copy(&plane_tex->src[i].src, &tex->src[i].src, plane_tex);
231 plane_tex->src[i].src_type = tex->src[i].src_type;
232 }
233 plane_tex->src[tex->num_srcs].src = nir_src_for_ssa(nir_imm_int(b, plane));
234 plane_tex->src[tex->num_srcs].src_type = nir_tex_src_plane;
235 plane_tex->op = nir_texop_tex;
236 plane_tex->sampler_dim = GLSL_SAMPLER_DIM_2D;
237 plane_tex->dest_type = nir_type_float;
238 plane_tex->coord_components = 2;
239
240 plane_tex->texture_index = tex->texture_index;
241 plane_tex->sampler_index = tex->sampler_index;
242
243 nir_ssa_dest_init(&plane_tex->instr, &plane_tex->dest, 4, 32, NULL);
244
245 nir_builder_instr_insert(b, &plane_tex->instr);
246
247 /* If scaling_factor is set, return a scaled value. */
248 if (options->scale_factors[tex->texture_index])
249 return nir_fmul_imm(b, &plane_tex->dest.ssa,
250 options->scale_factors[tex->texture_index]);
251
252 return &plane_tex->dest.ssa;
253 }
254
255 static void
256 convert_yuv_to_rgb(nir_builder *b, nir_tex_instr *tex,
257 nir_ssa_def *y, nir_ssa_def *u, nir_ssa_def *v,
258 nir_ssa_def *a)
259 {
260 nir_const_value m[3][4] = {
261 { { .f32 = 1.16438356f }, { .f32 = 1.16438356f }, { .f32 = 1.16438356f }, { .f32 = 0.0f } },
262 { { .f32 = 0.0f }, { .f32 = -0.39176229f }, { .f32 = 2.01723214f }, { .f32 = 0.0f } },
263 { { .f32 = 1.59602678f }, { .f32 = -0.81296764f }, { .f32 = 0.0f }, { .f32 = 0.0f } },
264 };
265
266 nir_ssa_def *offset =
267 nir_vec4(b,
268 nir_imm_float(b, -0.874202214f),
269 nir_imm_float(b, 0.531667820f),
270 nir_imm_float(b, -1.085630787f),
271 a);
272
273 nir_ssa_def *result =
274 nir_ffma(b, y, nir_build_imm(b, 4, 32, m[0]),
275 nir_ffma(b, u, nir_build_imm(b, 4, 32, m[1]),
276 nir_ffma(b, v, nir_build_imm(b, 4, 32, m[2]),
277 offset)));
278
279 nir_ssa_def_rewrite_uses(&tex->dest.ssa, nir_src_for_ssa(result));
280 }
281
282 static void
283 lower_y_uv_external(nir_builder *b, nir_tex_instr *tex,
284 const nir_lower_tex_options *options)
285 {
286 b->cursor = nir_after_instr(&tex->instr);
287
288 nir_ssa_def *y = sample_plane(b, tex, 0, options);
289 nir_ssa_def *uv = sample_plane(b, tex, 1, options);
290
291 convert_yuv_to_rgb(b, tex,
292 nir_channel(b, y, 0),
293 nir_channel(b, uv, 0),
294 nir_channel(b, uv, 1),
295 nir_imm_float(b, 1.0f));
296 }
297
298 static void
299 lower_y_u_v_external(nir_builder *b, nir_tex_instr *tex,
300 const nir_lower_tex_options *options)
301 {
302 b->cursor = nir_after_instr(&tex->instr);
303
304 nir_ssa_def *y = sample_plane(b, tex, 0, options);
305 nir_ssa_def *u = sample_plane(b, tex, 1, options);
306 nir_ssa_def *v = sample_plane(b, tex, 2, options);
307
308 convert_yuv_to_rgb(b, tex,
309 nir_channel(b, y, 0),
310 nir_channel(b, u, 0),
311 nir_channel(b, v, 0),
312 nir_imm_float(b, 1.0f));
313 }
314
315 static void
316 lower_yx_xuxv_external(nir_builder *b, nir_tex_instr *tex,
317 const nir_lower_tex_options *options)
318 {
319 b->cursor = nir_after_instr(&tex->instr);
320
321 nir_ssa_def *y = sample_plane(b, tex, 0, options);
322 nir_ssa_def *xuxv = sample_plane(b, tex, 1, options);
323
324 convert_yuv_to_rgb(b, tex,
325 nir_channel(b, y, 0),
326 nir_channel(b, xuxv, 1),
327 nir_channel(b, xuxv, 3),
328 nir_imm_float(b, 1.0f));
329 }
330
331 static void
332 lower_xy_uxvx_external(nir_builder *b, nir_tex_instr *tex,
333 const nir_lower_tex_options *options)
334 {
335 b->cursor = nir_after_instr(&tex->instr);
336
337 nir_ssa_def *y = sample_plane(b, tex, 0, options);
338 nir_ssa_def *uxvx = sample_plane(b, tex, 1, options);
339
340 convert_yuv_to_rgb(b, tex,
341 nir_channel(b, y, 1),
342 nir_channel(b, uxvx, 0),
343 nir_channel(b, uxvx, 2),
344 nir_imm_float(b, 1.0f));
345 }
346
347 static void
348 lower_ayuv_external(nir_builder *b, nir_tex_instr *tex,
349 const nir_lower_tex_options *options)
350 {
351 b->cursor = nir_after_instr(&tex->instr);
352
353 nir_ssa_def *ayuv = sample_plane(b, tex, 0, options);
354
355 convert_yuv_to_rgb(b, tex,
356 nir_channel(b, ayuv, 2),
357 nir_channel(b, ayuv, 1),
358 nir_channel(b, ayuv, 0),
359 nir_channel(b, ayuv, 3));
360 }
361
362 static void
363 lower_xyuv_external(nir_builder *b, nir_tex_instr *tex,
364 const nir_lower_tex_options *options)
365 {
366 b->cursor = nir_after_instr(&tex->instr);
367
368 nir_ssa_def *xyuv = sample_plane(b, tex, 0, options);
369
370 convert_yuv_to_rgb(b, tex,
371 nir_channel(b, xyuv, 2),
372 nir_channel(b, xyuv, 1),
373 nir_channel(b, xyuv, 0),
374 nir_imm_float(b, 1.0f));
375 }
376
377 /*
378 * Converts a nir_texop_txd instruction to nir_texop_txl with the given lod
379 * computed from the gradients.
380 */
381 static void
382 replace_gradient_with_lod(nir_builder *b, nir_ssa_def *lod, nir_tex_instr *tex)
383 {
384 assert(tex->op == nir_texop_txd);
385
386 nir_tex_instr_remove_src(tex, nir_tex_instr_src_index(tex, nir_tex_src_ddx));
387 nir_tex_instr_remove_src(tex, nir_tex_instr_src_index(tex, nir_tex_src_ddy));
388
389 int min_lod_idx = nir_tex_instr_src_index(tex, nir_tex_src_min_lod);
390 if (min_lod_idx >= 0) {
391 /* If we have a minimum LOD, clamp LOD accordingly */
392 lod = nir_fmax(b, lod, nir_ssa_for_src(b, tex->src[min_lod_idx].src, 1));
393 nir_tex_instr_remove_src(tex, min_lod_idx);
394 }
395
396 nir_tex_instr_add_src(tex, nir_tex_src_lod, nir_src_for_ssa(lod));
397 tex->op = nir_texop_txl;
398 }
399
400 static void
401 lower_gradient_cube_map(nir_builder *b, nir_tex_instr *tex)
402 {
403 assert(tex->sampler_dim == GLSL_SAMPLER_DIM_CUBE);
404 assert(tex->op == nir_texop_txd);
405 assert(tex->dest.is_ssa);
406
407 /* Use textureSize() to get the width and height of LOD 0 */
408 nir_ssa_def *size = nir_get_texture_size(b, tex);
409
410 /* Cubemap texture lookups first generate a texture coordinate normalized
411 * to [-1, 1] on the appropiate face. The appropiate face is determined
412 * by which component has largest magnitude and its sign. The texture
413 * coordinate is the quotient of the remaining texture coordinates against
414 * that absolute value of the component of largest magnitude. This
415 * division requires that the computing of the derivative of the texel
416 * coordinate must use the quotient rule. The high level GLSL code is as
417 * follows:
418 *
419 * Step 1: selection
420 *
421 * vec3 abs_p, Q, dQdx, dQdy;
422 * abs_p = abs(ir->coordinate);
423 * if (abs_p.x >= max(abs_p.y, abs_p.z)) {
424 * Q = ir->coordinate.yzx;
425 * dQdx = ir->lod_info.grad.dPdx.yzx;
426 * dQdy = ir->lod_info.grad.dPdy.yzx;
427 * }
428 * if (abs_p.y >= max(abs_p.x, abs_p.z)) {
429 * Q = ir->coordinate.xzy;
430 * dQdx = ir->lod_info.grad.dPdx.xzy;
431 * dQdy = ir->lod_info.grad.dPdy.xzy;
432 * }
433 * if (abs_p.z >= max(abs_p.x, abs_p.y)) {
434 * Q = ir->coordinate;
435 * dQdx = ir->lod_info.grad.dPdx;
436 * dQdy = ir->lod_info.grad.dPdy;
437 * }
438 *
439 * Step 2: use quotient rule to compute derivative. The normalized to
440 * [-1, 1] texel coordinate is given by Q.xy / (sign(Q.z) * Q.z). We are
441 * only concerned with the magnitudes of the derivatives whose values are
442 * not affected by the sign. We drop the sign from the computation.
443 *
444 * vec2 dx, dy;
445 * float recip;
446 *
447 * recip = 1.0 / Q.z;
448 * dx = recip * ( dQdx.xy - Q.xy * (dQdx.z * recip) );
449 * dy = recip * ( dQdy.xy - Q.xy * (dQdy.z * recip) );
450 *
451 * Step 3: compute LOD. At this point we have the derivatives of the
452 * texture coordinates normalized to [-1,1]. We take the LOD to be
453 * result = log2(max(sqrt(dot(dx, dx)), sqrt(dy, dy)) * 0.5 * L)
454 * = -1.0 + log2(max(sqrt(dot(dx, dx)), sqrt(dy, dy)) * L)
455 * = -1.0 + log2(sqrt(max(dot(dx, dx), dot(dy,dy))) * L)
456 * = -1.0 + log2(sqrt(L * L * max(dot(dx, dx), dot(dy,dy))))
457 * = -1.0 + 0.5 * log2(L * L * max(dot(dx, dx), dot(dy,dy)))
458 * where L is the dimension of the cubemap. The code is:
459 *
460 * float M, result;
461 * M = max(dot(dx, dx), dot(dy, dy));
462 * L = textureSize(sampler, 0).x;
463 * result = -1.0 + 0.5 * log2(L * L * M);
464 */
465
466 /* coordinate */
467 nir_ssa_def *p =
468 tex->src[nir_tex_instr_src_index(tex, nir_tex_src_coord)].src.ssa;
469
470 /* unmodified dPdx, dPdy values */
471 nir_ssa_def *dPdx =
472 tex->src[nir_tex_instr_src_index(tex, nir_tex_src_ddx)].src.ssa;
473 nir_ssa_def *dPdy =
474 tex->src[nir_tex_instr_src_index(tex, nir_tex_src_ddy)].src.ssa;
475
476 nir_ssa_def *abs_p = nir_fabs(b, p);
477 nir_ssa_def *abs_p_x = nir_channel(b, abs_p, 0);
478 nir_ssa_def *abs_p_y = nir_channel(b, abs_p, 1);
479 nir_ssa_def *abs_p_z = nir_channel(b, abs_p, 2);
480
481 /* 1. compute selector */
482 nir_ssa_def *Q, *dQdx, *dQdy;
483
484 nir_ssa_def *cond_z = nir_fge(b, abs_p_z, nir_fmax(b, abs_p_x, abs_p_y));
485 nir_ssa_def *cond_y = nir_fge(b, abs_p_y, nir_fmax(b, abs_p_x, abs_p_z));
486
487 unsigned yzx[3] = { 1, 2, 0 };
488 unsigned xzy[3] = { 0, 2, 1 };
489
490 Q = nir_bcsel(b, cond_z,
491 p,
492 nir_bcsel(b, cond_y,
493 nir_swizzle(b, p, xzy, 3),
494 nir_swizzle(b, p, yzx, 3)));
495
496 dQdx = nir_bcsel(b, cond_z,
497 dPdx,
498 nir_bcsel(b, cond_y,
499 nir_swizzle(b, dPdx, xzy, 3),
500 nir_swizzle(b, dPdx, yzx, 3)));
501
502 dQdy = nir_bcsel(b, cond_z,
503 dPdy,
504 nir_bcsel(b, cond_y,
505 nir_swizzle(b, dPdy, xzy, 3),
506 nir_swizzle(b, dPdy, yzx, 3)));
507
508 /* 2. quotient rule */
509
510 /* tmp = Q.xy * recip;
511 * dx = recip * ( dQdx.xy - (tmp * dQdx.z) );
512 * dy = recip * ( dQdy.xy - (tmp * dQdy.z) );
513 */
514 nir_ssa_def *rcp_Q_z = nir_frcp(b, nir_channel(b, Q, 2));
515
516 nir_ssa_def *Q_xy = nir_channels(b, Q, 0x3);
517 nir_ssa_def *tmp = nir_fmul(b, Q_xy, rcp_Q_z);
518
519 nir_ssa_def *dQdx_xy = nir_channels(b, dQdx, 0x3);
520 nir_ssa_def *dQdx_z = nir_channel(b, dQdx, 2);
521 nir_ssa_def *dx =
522 nir_fmul(b, rcp_Q_z, nir_fsub(b, dQdx_xy, nir_fmul(b, tmp, dQdx_z)));
523
524 nir_ssa_def *dQdy_xy = nir_channels(b, dQdy, 0x3);
525 nir_ssa_def *dQdy_z = nir_channel(b, dQdy, 2);
526 nir_ssa_def *dy =
527 nir_fmul(b, rcp_Q_z, nir_fsub(b, dQdy_xy, nir_fmul(b, tmp, dQdy_z)));
528
529 /* M = max(dot(dx, dx), dot(dy, dy)); */
530 nir_ssa_def *M = nir_fmax(b, nir_fdot(b, dx, dx), nir_fdot(b, dy, dy));
531
532 /* size has textureSize() of LOD 0 */
533 nir_ssa_def *L = nir_channel(b, size, 0);
534
535 /* lod = -1.0 + 0.5 * log2(L * L * M); */
536 nir_ssa_def *lod =
537 nir_fadd(b,
538 nir_imm_float(b, -1.0f),
539 nir_fmul(b,
540 nir_imm_float(b, 0.5f),
541 nir_flog2(b, nir_fmul(b, L, nir_fmul(b, L, M)))));
542
543 /* 3. Replace the gradient instruction with an equivalent lod instruction */
544 replace_gradient_with_lod(b, lod, tex);
545 }
546
547 static void
548 lower_gradient(nir_builder *b, nir_tex_instr *tex)
549 {
550 /* Cubes are more complicated and have their own function */
551 if (tex->sampler_dim == GLSL_SAMPLER_DIM_CUBE) {
552 lower_gradient_cube_map(b, tex);
553 return;
554 }
555
556 assert(tex->sampler_dim != GLSL_SAMPLER_DIM_CUBE);
557 assert(tex->op == nir_texop_txd);
558 assert(tex->dest.is_ssa);
559
560 /* Use textureSize() to get the width and height of LOD 0 */
561 unsigned component_mask;
562 switch (tex->sampler_dim) {
563 case GLSL_SAMPLER_DIM_3D:
564 component_mask = 7;
565 break;
566 case GLSL_SAMPLER_DIM_1D:
567 component_mask = 1;
568 break;
569 default:
570 component_mask = 3;
571 break;
572 }
573
574 nir_ssa_def *size =
575 nir_channels(b, nir_get_texture_size(b, tex), component_mask);
576
577 /* Scale the gradients by width and height. Effectively, the incoming
578 * gradients are s'(x,y), t'(x,y), and r'(x,y) from equation 3.19 in the
579 * GL 3.0 spec; we want u'(x,y), which is w_t * s'(x,y).
580 */
581 nir_ssa_def *ddx =
582 tex->src[nir_tex_instr_src_index(tex, nir_tex_src_ddx)].src.ssa;
583 nir_ssa_def *ddy =
584 tex->src[nir_tex_instr_src_index(tex, nir_tex_src_ddy)].src.ssa;
585
586 nir_ssa_def *dPdx = nir_fmul(b, ddx, size);
587 nir_ssa_def *dPdy = nir_fmul(b, ddy, size);
588
589 nir_ssa_def *rho;
590 if (dPdx->num_components == 1) {
591 rho = nir_fmax(b, nir_fabs(b, dPdx), nir_fabs(b, dPdy));
592 } else {
593 rho = nir_fmax(b,
594 nir_fsqrt(b, nir_fdot(b, dPdx, dPdx)),
595 nir_fsqrt(b, nir_fdot(b, dPdy, dPdy)));
596 }
597
598 /* lod = log2(rho). We're ignoring GL state biases for now. */
599 nir_ssa_def *lod = nir_flog2(b, rho);
600
601 /* Replace the gradient instruction with an equivalent lod instruction */
602 replace_gradient_with_lod(b, lod, tex);
603 }
604
605 static void
606 saturate_src(nir_builder *b, nir_tex_instr *tex, unsigned sat_mask)
607 {
608 b->cursor = nir_before_instr(&tex->instr);
609
610 /* Walk through the sources saturating the requested arguments. */
611 for (unsigned i = 0; i < tex->num_srcs; i++) {
612 if (tex->src[i].src_type != nir_tex_src_coord)
613 continue;
614
615 nir_ssa_def *src =
616 nir_ssa_for_src(b, tex->src[i].src, tex->coord_components);
617
618 /* split src into components: */
619 nir_ssa_def *comp[4];
620
621 assume(tex->coord_components >= 1);
622
623 for (unsigned j = 0; j < tex->coord_components; j++)
624 comp[j] = nir_channel(b, src, j);
625
626 /* clamp requested components, array index does not get clamped: */
627 unsigned ncomp = tex->coord_components;
628 if (tex->is_array)
629 ncomp--;
630
631 for (unsigned j = 0; j < ncomp; j++) {
632 if ((1 << j) & sat_mask) {
633 if (tex->sampler_dim == GLSL_SAMPLER_DIM_RECT) {
634 /* non-normalized texture coords, so clamp to texture
635 * size rather than [0.0, 1.0]
636 */
637 nir_ssa_def *txs = nir_get_texture_size(b, tex);
638 comp[j] = nir_fmax(b, comp[j], nir_imm_float(b, 0.0));
639 comp[j] = nir_fmin(b, comp[j], nir_channel(b, txs, j));
640 } else {
641 comp[j] = nir_fsat(b, comp[j]);
642 }
643 }
644 }
645
646 /* and move the result back into a single vecN: */
647 src = nir_vec(b, comp, tex->coord_components);
648
649 nir_instr_rewrite_src(&tex->instr,
650 &tex->src[i].src,
651 nir_src_for_ssa(src));
652 }
653 }
654
655 static nir_ssa_def *
656 get_zero_or_one(nir_builder *b, nir_alu_type type, uint8_t swizzle_val)
657 {
658 nir_const_value v[4];
659
660 memset(&v, 0, sizeof(v));
661
662 if (swizzle_val == 4) {
663 v[0].u32 = v[1].u32 = v[2].u32 = v[3].u32 = 0;
664 } else {
665 assert(swizzle_val == 5);
666 if (type == nir_type_float)
667 v[0].f32 = v[1].f32 = v[2].f32 = v[3].f32 = 1.0;
668 else
669 v[0].u32 = v[1].u32 = v[2].u32 = v[3].u32 = 1;
670 }
671
672 return nir_build_imm(b, 4, 32, v);
673 }
674
675 static void
676 swizzle_tg4_broadcom(nir_builder *b, nir_tex_instr *tex)
677 {
678 assert(tex->dest.is_ssa);
679
680 b->cursor = nir_after_instr(&tex->instr);
681
682 assert(nir_tex_instr_dest_size(tex) == 4);
683 unsigned swiz[4] = { 2, 3, 1, 0 };
684 nir_ssa_def *swizzled = nir_swizzle(b, &tex->dest.ssa, swiz, 4);
685
686 nir_ssa_def_rewrite_uses_after(&tex->dest.ssa, nir_src_for_ssa(swizzled),
687 swizzled->parent_instr);
688 }
689
690 static void
691 swizzle_result(nir_builder *b, nir_tex_instr *tex, const uint8_t swizzle[4])
692 {
693 assert(tex->dest.is_ssa);
694
695 b->cursor = nir_after_instr(&tex->instr);
696
697 nir_ssa_def *swizzled;
698 if (tex->op == nir_texop_tg4) {
699 if (swizzle[tex->component] < 4) {
700 /* This one's easy */
701 tex->component = swizzle[tex->component];
702 return;
703 } else {
704 swizzled = get_zero_or_one(b, tex->dest_type, swizzle[tex->component]);
705 }
706 } else {
707 assert(nir_tex_instr_dest_size(tex) == 4);
708 if (swizzle[0] < 4 && swizzle[1] < 4 &&
709 swizzle[2] < 4 && swizzle[3] < 4) {
710 unsigned swiz[4] = { swizzle[0], swizzle[1], swizzle[2], swizzle[3] };
711 /* We have no 0s or 1s, just emit a swizzling MOV */
712 swizzled = nir_swizzle(b, &tex->dest.ssa, swiz, 4);
713 } else {
714 nir_ssa_def *srcs[4];
715 for (unsigned i = 0; i < 4; i++) {
716 if (swizzle[i] < 4) {
717 srcs[i] = nir_channel(b, &tex->dest.ssa, swizzle[i]);
718 } else {
719 srcs[i] = get_zero_or_one(b, tex->dest_type, swizzle[i]);
720 }
721 }
722 swizzled = nir_vec(b, srcs, 4);
723 }
724 }
725
726 nir_ssa_def_rewrite_uses_after(&tex->dest.ssa, nir_src_for_ssa(swizzled),
727 swizzled->parent_instr);
728 }
729
730 static void
731 linearize_srgb_result(nir_builder *b, nir_tex_instr *tex)
732 {
733 assert(tex->dest.is_ssa);
734 assert(nir_tex_instr_dest_size(tex) == 4);
735 assert(nir_alu_type_get_base_type(tex->dest_type) == nir_type_float);
736
737 b->cursor = nir_after_instr(&tex->instr);
738
739 nir_ssa_def *rgb =
740 nir_format_srgb_to_linear(b, nir_channels(b, &tex->dest.ssa, 0x7));
741
742 /* alpha is untouched: */
743 nir_ssa_def *result = nir_vec4(b,
744 nir_channel(b, rgb, 0),
745 nir_channel(b, rgb, 1),
746 nir_channel(b, rgb, 2),
747 nir_channel(b, &tex->dest.ssa, 3));
748
749 nir_ssa_def_rewrite_uses_after(&tex->dest.ssa, nir_src_for_ssa(result),
750 result->parent_instr);
751 }
752
753 /**
754 * Lowers texture instructions from giving a vec4 result to a vec2 of f16,
755 * i16, or u16, or a single unorm4x8 value.
756 *
757 * Note that we don't change the destination num_components, because
758 * nir_tex_instr_dest_size() will still return 4. The driver is just expected
759 * to not store the other channels, given that nothing at the NIR level will
760 * read them.
761 */
762 static void
763 lower_tex_packing(nir_builder *b, nir_tex_instr *tex,
764 const nir_lower_tex_options *options)
765 {
766 nir_ssa_def *color = &tex->dest.ssa;
767
768 b->cursor = nir_after_instr(&tex->instr);
769
770 switch (options->lower_tex_packing[tex->sampler_index]) {
771 case nir_lower_tex_packing_none:
772 return;
773
774 case nir_lower_tex_packing_16: {
775 static const unsigned bits[4] = {16, 16, 16, 16};
776
777 switch (nir_alu_type_get_base_type(tex->dest_type)) {
778 case nir_type_float:
779 if (tex->is_shadow && tex->is_new_style_shadow) {
780 color = nir_unpack_half_2x16_split_x(b, nir_channel(b, color, 0));
781 } else {
782 nir_ssa_def *rg = nir_channel(b, color, 0);
783 nir_ssa_def *ba = nir_channel(b, color, 1);
784 color = nir_vec4(b,
785 nir_unpack_half_2x16_split_x(b, rg),
786 nir_unpack_half_2x16_split_y(b, rg),
787 nir_unpack_half_2x16_split_x(b, ba),
788 nir_unpack_half_2x16_split_y(b, ba));
789 }
790 break;
791
792 case nir_type_int:
793 color = nir_format_unpack_sint(b, color, bits, 4);
794 break;
795
796 case nir_type_uint:
797 color = nir_format_unpack_uint(b, color, bits, 4);
798 break;
799
800 default:
801 unreachable("unknown base type");
802 }
803 break;
804 }
805
806 case nir_lower_tex_packing_8:
807 assert(nir_alu_type_get_base_type(tex->dest_type) == nir_type_float);
808 color = nir_unpack_unorm_4x8(b, nir_channel(b, color, 0));
809 break;
810 }
811
812 nir_ssa_def_rewrite_uses_after(&tex->dest.ssa, nir_src_for_ssa(color),
813 color->parent_instr);
814 }
815
816 static bool
817 sampler_index_lt(nir_tex_instr *tex, unsigned max)
818 {
819 assert(nir_tex_instr_src_index(tex, nir_tex_src_sampler_deref) == -1);
820
821 unsigned sampler_index = tex->sampler_index;
822
823 int sampler_offset_idx =
824 nir_tex_instr_src_index(tex, nir_tex_src_sampler_offset);
825 if (sampler_offset_idx >= 0) {
826 if (!nir_src_is_const(tex->src[sampler_offset_idx].src))
827 return false;
828
829 sampler_index += nir_src_as_uint(tex->src[sampler_offset_idx].src);
830 }
831
832 return sampler_index < max;
833 }
834
835 static bool
836 lower_tg4_offsets(nir_builder *b, nir_tex_instr *tex)
837 {
838 assert(tex->op == nir_texop_tg4);
839 assert(nir_tex_instr_has_explicit_tg4_offsets(tex));
840 assert(nir_tex_instr_src_index(tex, nir_tex_src_offset) == -1);
841
842 b->cursor = nir_after_instr(&tex->instr);
843
844 nir_ssa_def *dest[4];
845 for (unsigned i = 0; i < 4; ++i) {
846 nir_tex_instr *tex_copy = nir_tex_instr_create(b->shader, tex->num_srcs + 1);
847 tex_copy->op = tex->op;
848 tex_copy->coord_components = tex->coord_components;
849 tex_copy->sampler_dim = tex->sampler_dim;
850 tex_copy->is_array = tex->is_array;
851 tex_copy->is_shadow = tex->is_shadow;
852 tex_copy->is_new_style_shadow = tex->is_new_style_shadow;
853 tex_copy->component = tex->component;
854 tex_copy->dest_type = tex->dest_type;
855
856 for (unsigned j = 0; j < tex->num_srcs; ++j) {
857 nir_src_copy(&tex_copy->src[j].src, &tex->src[j].src, tex_copy);
858 tex_copy->src[j].src_type = tex->src[j].src_type;
859 }
860
861 nir_tex_src src;
862 src.src = nir_src_for_ssa(nir_imm_ivec2(b, tex->tg4_offsets[i][0],
863 tex->tg4_offsets[i][1]));
864 src.src_type = nir_tex_src_offset;
865 tex_copy->src[tex_copy->num_srcs - 1] = src;
866
867 nir_ssa_dest_init(&tex_copy->instr, &tex_copy->dest,
868 nir_tex_instr_dest_size(tex), 32, NULL);
869
870 nir_builder_instr_insert(b, &tex_copy->instr);
871
872 dest[i] = nir_channel(b, &tex_copy->dest.ssa, 3);
873 }
874
875 nir_ssa_def *res = nir_vec4(b, dest[0], dest[1], dest[2], dest[3]);
876 nir_ssa_def_rewrite_uses(&tex->dest.ssa, nir_src_for_ssa(res));
877 nir_instr_remove(&tex->instr);
878
879 return true;
880 }
881
882 static bool
883 nir_lower_txs_lod(nir_builder *b, nir_tex_instr *tex)
884 {
885 int lod_idx = nir_tex_instr_src_index(tex, nir_tex_src_lod);
886 if (lod_idx < 0 ||
887 (nir_src_is_const(tex->src[lod_idx].src) &&
888 nir_src_as_int(tex->src[lod_idx].src) == 0))
889 return false;
890
891 unsigned dest_size = nir_tex_instr_dest_size(tex);
892
893 b->cursor = nir_before_instr(&tex->instr);
894 nir_ssa_def *lod = nir_ssa_for_src(b, tex->src[lod_idx].src, 1);
895
896 /* Replace the non-0-LOD in the initial TXS operation by a 0-LOD. */
897 nir_instr_rewrite_src(&tex->instr, &tex->src[lod_idx].src,
898 nir_src_for_ssa(nir_imm_int(b, 0)));
899
900 /* TXS(LOD) = max(TXS(0) >> LOD, 1) */
901 b->cursor = nir_after_instr(&tex->instr);
902 nir_ssa_def *minified = nir_imax(b, nir_ushr(b, &tex->dest.ssa, lod),
903 nir_imm_int(b, 1));
904
905 /* Make sure the component encoding the array size (if any) is not
906 * minified.
907 */
908 if (tex->is_array) {
909 nir_ssa_def *comp[3];
910
911 assert(dest_size <= ARRAY_SIZE(comp));
912 for (unsigned i = 0; i < dest_size - 1; i++)
913 comp[i] = nir_channel(b, minified, i);
914
915 comp[dest_size - 1] = nir_channel(b, &tex->dest.ssa, dest_size - 1);
916 minified = nir_vec(b, comp, dest_size);
917 }
918
919 nir_ssa_def_rewrite_uses_after(&tex->dest.ssa, nir_src_for_ssa(minified),
920 minified->parent_instr);
921 return true;
922 }
923
924 static bool
925 nir_lower_tex_block(nir_block *block, nir_builder *b,
926 const nir_lower_tex_options *options)
927 {
928 bool progress = false;
929
930 nir_foreach_instr_safe(instr, block) {
931 if (instr->type != nir_instr_type_tex)
932 continue;
933
934 nir_tex_instr *tex = nir_instr_as_tex(instr);
935 bool lower_txp = !!(options->lower_txp & (1 << tex->sampler_dim));
936
937 /* mask of src coords to saturate (clamp): */
938 unsigned sat_mask = 0;
939
940 if ((1 << tex->sampler_index) & options->saturate_r)
941 sat_mask |= (1 << 2); /* .z */
942 if ((1 << tex->sampler_index) & options->saturate_t)
943 sat_mask |= (1 << 1); /* .y */
944 if ((1 << tex->sampler_index) & options->saturate_s)
945 sat_mask |= (1 << 0); /* .x */
946
947 /* If we are clamping any coords, we must lower projector first
948 * as clamping happens *after* projection:
949 */
950 if (lower_txp || sat_mask) {
951 progress |= project_src(b, tex);
952 }
953
954 if ((tex->op == nir_texop_txf && options->lower_txf_offset) ||
955 (sat_mask && nir_tex_instr_src_index(tex, nir_tex_src_coord) >= 0) ||
956 (tex->sampler_dim == GLSL_SAMPLER_DIM_RECT &&
957 options->lower_rect_offset)) {
958 progress = lower_offset(b, tex) || progress;
959 }
960
961 if ((tex->sampler_dim == GLSL_SAMPLER_DIM_RECT) && options->lower_rect) {
962 lower_rect(b, tex);
963 progress = true;
964 }
965
966 if ((1 << tex->texture_index) & options->lower_y_uv_external) {
967 lower_y_uv_external(b, tex, options);
968 progress = true;
969 }
970
971 if ((1 << tex->texture_index) & options->lower_y_u_v_external) {
972 lower_y_u_v_external(b, tex, options);
973 progress = true;
974 }
975
976 if ((1 << tex->texture_index) & options->lower_yx_xuxv_external) {
977 lower_yx_xuxv_external(b, tex, options);
978 progress = true;
979 }
980
981 if ((1 << tex->texture_index) & options->lower_xy_uxvx_external) {
982 lower_xy_uxvx_external(b, tex, options);
983 progress = true;
984 }
985
986 if ((1 << tex->texture_index) & options->lower_ayuv_external) {
987 lower_ayuv_external(b, tex, options);
988 progress = true;
989 }
990
991 if ((1 << tex->texture_index) & options->lower_xyuv_external) {
992 lower_xyuv_external(b, tex, options);
993 progress = true;
994 }
995
996 if (sat_mask) {
997 saturate_src(b, tex, sat_mask);
998 progress = true;
999 }
1000
1001 if (tex->op == nir_texop_tg4 && options->lower_tg4_broadcom_swizzle) {
1002 swizzle_tg4_broadcom(b, tex);
1003 progress = true;
1004 }
1005
1006 if (((1 << tex->texture_index) & options->swizzle_result) &&
1007 !nir_tex_instr_is_query(tex) &&
1008 !(tex->is_shadow && tex->is_new_style_shadow)) {
1009 swizzle_result(b, tex, options->swizzles[tex->texture_index]);
1010 progress = true;
1011 }
1012
1013 /* should be after swizzle so we know which channels are rgb: */
1014 if (((1 << tex->texture_index) & options->lower_srgb) &&
1015 !nir_tex_instr_is_query(tex) && !tex->is_shadow) {
1016 linearize_srgb_result(b, tex);
1017 progress = true;
1018 }
1019
1020 const bool has_min_lod =
1021 nir_tex_instr_src_index(tex, nir_tex_src_min_lod) >= 0;
1022 const bool has_offset =
1023 nir_tex_instr_src_index(tex, nir_tex_src_offset) >= 0;
1024
1025 if (tex->op == nir_texop_txb && tex->is_shadow && has_min_lod &&
1026 options->lower_txb_shadow_clamp) {
1027 lower_implicit_lod(b, tex);
1028 progress = true;
1029 }
1030
1031 if (options->lower_tex_packing[tex->sampler_index] !=
1032 nir_lower_tex_packing_none &&
1033 tex->op != nir_texop_txs &&
1034 tex->op != nir_texop_query_levels) {
1035 lower_tex_packing(b, tex, options);
1036 progress = true;
1037 }
1038
1039 if (tex->op == nir_texop_txd &&
1040 (options->lower_txd ||
1041 (options->lower_txd_shadow && tex->is_shadow) ||
1042 (options->lower_txd_shadow_clamp && tex->is_shadow && has_min_lod) ||
1043 (options->lower_txd_offset_clamp && has_offset && has_min_lod) ||
1044 (options->lower_txd_clamp_bindless_sampler && has_min_lod &&
1045 nir_tex_instr_src_index(tex, nir_tex_src_sampler_handle) != -1) ||
1046 (options->lower_txd_clamp_if_sampler_index_not_lt_16 &&
1047 has_min_lod && !sampler_index_lt(tex, 16)) ||
1048 (options->lower_txd_cube_map &&
1049 tex->sampler_dim == GLSL_SAMPLER_DIM_CUBE) ||
1050 (options->lower_txd_3d &&
1051 tex->sampler_dim == GLSL_SAMPLER_DIM_3D))) {
1052 lower_gradient(b, tex);
1053 progress = true;
1054 continue;
1055 }
1056
1057 bool shader_supports_implicit_lod =
1058 b->shader->info.stage == MESA_SHADER_FRAGMENT ||
1059 (b->shader->info.stage == MESA_SHADER_COMPUTE &&
1060 b->shader->info.cs.derivative_group != DERIVATIVE_GROUP_NONE);
1061
1062 /* TXF, TXS and TXL require a LOD but not everything we implement using those
1063 * three opcodes provides one. Provide a default LOD of 0.
1064 */
1065 if ((nir_tex_instr_src_index(tex, nir_tex_src_lod) == -1) &&
1066 (tex->op == nir_texop_txf || tex->op == nir_texop_txs ||
1067 tex->op == nir_texop_txl || tex->op == nir_texop_query_levels ||
1068 (tex->op == nir_texop_tex && !shader_supports_implicit_lod))) {
1069 b->cursor = nir_before_instr(&tex->instr);
1070 nir_tex_instr_add_src(tex, nir_tex_src_lod, nir_src_for_ssa(nir_imm_int(b, 0)));
1071 if (tex->op == nir_texop_tex && options->lower_tex_without_implicit_lod)
1072 tex->op = nir_texop_txl;
1073 progress = true;
1074 continue;
1075 }
1076
1077 if (options->lower_txs_lod && tex->op == nir_texop_txs) {
1078 progress |= nir_lower_txs_lod(b, tex);
1079 continue;
1080 }
1081
1082 /* has to happen after all the other lowerings as the original tg4 gets
1083 * replaced by 4 tg4 instructions.
1084 */
1085 if (tex->op == nir_texop_tg4 &&
1086 nir_tex_instr_has_explicit_tg4_offsets(tex) &&
1087 options->lower_tg4_offsets) {
1088 progress |= lower_tg4_offsets(b, tex);
1089 continue;
1090 }
1091 }
1092
1093 return progress;
1094 }
1095
1096 static bool
1097 nir_lower_tex_impl(nir_function_impl *impl,
1098 const nir_lower_tex_options *options)
1099 {
1100 bool progress = false;
1101 nir_builder builder;
1102 nir_builder_init(&builder, impl);
1103
1104 nir_foreach_block(block, impl) {
1105 progress |= nir_lower_tex_block(block, &builder, options);
1106 }
1107
1108 nir_metadata_preserve(impl, nir_metadata_block_index |
1109 nir_metadata_dominance);
1110 return progress;
1111 }
1112
1113 bool
1114 nir_lower_tex(nir_shader *shader, const nir_lower_tex_options *options)
1115 {
1116 bool progress = false;
1117
1118 nir_foreach_function(function, shader) {
1119 if (function->impl)
1120 progress |= nir_lower_tex_impl(function->impl, options);
1121 }
1122
1123 return progress;
1124 }