2 * Copyright © 2015 Broadcom
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:
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
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
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.
39 #include "nir_builder.h"
42 project_src(nir_builder
*b
, nir_tex_instr
*tex
)
44 /* Find the projector in the srcs list, if present. */
45 int proj_index
= nir_tex_instr_src_index(tex
, nir_tex_src_projector
);
49 b
->cursor
= nir_before_instr(&tex
->instr
);
51 nir_ssa_def
*inv_proj
=
52 nir_frcp(b
, nir_ssa_for_src(b
, tex
->src
[proj_index
].src
, 1));
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_comparitor
:
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
);
67 /* Array indices don't get projected, so make an new vector with the
68 * coordinate's array index untouched.
70 if (tex
->is_array
&& tex
->src
[i
].src_type
== nir_tex_src_coord
) {
71 switch (tex
->coord_components
) {
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));
80 projected
= nir_vec3(b
,
81 nir_channel(b
, projected
, 0),
82 nir_channel(b
, projected
, 1),
83 nir_channel(b
, unprojected
, 2));
86 projected
= nir_vec2(b
,
87 nir_channel(b
, projected
, 0),
88 nir_channel(b
, unprojected
, 1));
91 unreachable("bad texture coord count for array");
96 nir_instr_rewrite_src(&tex
->instr
,
98 nir_src_for_ssa(projected
));
101 nir_tex_instr_remove_src(tex
, proj_index
);
105 lower_offset(nir_builder
*b
, nir_tex_instr
*tex
)
107 int offset_index
= nir_tex_instr_src_index(tex
, nir_tex_src_offset
);
108 if (offset_index
< 0)
111 int coord_index
= nir_tex_instr_src_index(tex
, nir_tex_src_coord
);
112 assert(coord_index
>= 0);
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
;
119 b
->cursor
= nir_before_instr(&tex
->instr
);
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
));
126 offset_coord
= nir_iadd(b
, coord
, offset
);
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));
139 unreachable("Invalid number of components");
143 nir_instr_rewrite_src(&tex
->instr
, &tex
->src
[coord_index
].src
,
144 nir_src_for_ssa(offset_coord
));
146 nir_tex_instr_remove_src(tex
, offset_index
);
153 get_texture_size(nir_builder
*b
, nir_tex_instr
*tex
)
155 b
->cursor
= nir_before_instr(&tex
->instr
);
157 /* RECT textures should not be array: */
158 assert(!tex
->is_array
);
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
;
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
;
172 nir_ssa_dest_init(&txs
->instr
, &txs
->dest
, 2, 32, NULL
);
173 nir_builder_instr_insert(b
, &txs
->instr
);
175 return nir_i2f(b
, &txs
->dest
.ssa
);
179 lower_rect(nir_builder
*b
, nir_tex_instr
*tex
)
181 nir_ssa_def
*txs
= get_texture_size(b
, tex
);
182 nir_ssa_def
*scale
= nir_frcp(b
, txs
);
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
)
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
,
193 nir_src_for_ssa(nir_fmul(b
, coords
, scale
)));
196 tex
->sampler_dim
= GLSL_SAMPLER_DIM_2D
;
200 sample_plane(nir_builder
*b
, nir_tex_instr
*tex
, int plane
)
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);
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
= 2;
215 plane_tex
->dest_type
= nir_type_float
;
216 plane_tex
->coord_components
= 2;
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
);
225 nir_ssa_dest_init(&plane_tex
->instr
, &plane_tex
->dest
, 4, 32, NULL
);
227 nir_builder_instr_insert(b
, &plane_tex
->instr
);
229 return &plane_tex
->dest
.ssa
;
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
)
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
} }
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));
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]));
254 nir_ssa_def
*result
= nir_vec4(b
, red
, green
, blue
, nir_imm_float(b
, 1.0f
));
256 nir_ssa_def_rewrite_uses(&tex
->dest
.ssa
, nir_src_for_ssa(result
));
260 lower_y_uv_external(nir_builder
*b
, nir_tex_instr
*tex
)
262 b
->cursor
= nir_after_instr(&tex
->instr
);
264 nir_ssa_def
*y
= sample_plane(b
, tex
, 0);
265 nir_ssa_def
*uv
= sample_plane(b
, tex
, 1);
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));
274 lower_y_u_v_external(nir_builder
*b
, nir_tex_instr
*tex
)
276 b
->cursor
= nir_after_instr(&tex
->instr
);
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);
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));
289 lower_yx_xuxv_external(nir_builder
*b
, nir_tex_instr
*tex
)
291 b
->cursor
= nir_after_instr(&tex
->instr
);
293 nir_ssa_def
*y
= sample_plane(b
, tex
, 0);
294 nir_ssa_def
*xuxv
= sample_plane(b
, tex
, 1);
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));
303 saturate_src(nir_builder
*b
, nir_tex_instr
*tex
, unsigned sat_mask
)
305 b
->cursor
= nir_before_instr(&tex
->instr
);
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
)
313 nir_ssa_for_src(b
, tex
->src
[i
].src
, tex
->coord_components
);
315 /* split src into components: */
316 nir_ssa_def
*comp
[4];
318 assume(tex
->coord_components
>= 1);
320 for (unsigned j
= 0; j
< tex
->coord_components
; j
++)
321 comp
[j
] = nir_channel(b
, src
, j
);
323 /* clamp requested components, array index does not get clamped: */
324 unsigned ncomp
= tex
->coord_components
;
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]
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
));
338 comp
[j
] = nir_fsat(b
, comp
[j
]);
343 /* and move the result back into a single vecN: */
344 src
= nir_vec(b
, comp
, tex
->coord_components
);
346 nir_instr_rewrite_src(&tex
->instr
,
348 nir_src_for_ssa(src
));
353 get_zero_or_one(nir_builder
*b
, nir_alu_type type
, uint8_t swizzle_val
)
357 memset(&v
, 0, sizeof(v
));
359 if (swizzle_val
== 4) {
360 v
.u32
[0] = v
.u32
[1] = v
.u32
[2] = v
.u32
[3] = 0;
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;
366 v
.u32
[0] = v
.u32
[1] = v
.u32
[2] = v
.u32
[3] = 1;
369 return nir_build_imm(b
, 4, 32, v
);
373 swizzle_result(nir_builder
*b
, nir_tex_instr
*tex
, const uint8_t swizzle
[4])
375 assert(tex
->dest
.is_ssa
);
377 b
->cursor
= nir_after_instr(&tex
->instr
);
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
];
386 swizzled
= get_zero_or_one(b
, tex
->dest_type
, swizzle
[tex
->component
]);
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);
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
]);
401 srcs
[i
] = get_zero_or_one(b
, tex
->dest_type
, swizzle
[i
]);
404 swizzled
= nir_vec(b
, srcs
, 4);
408 nir_ssa_def_rewrite_uses_after(&tex
->dest
.ssa
, nir_src_for_ssa(swizzled
),
409 swizzled
->parent_instr
);
413 linearize_srgb_result(nir_builder
*b
, nir_tex_instr
*tex
)
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
);
419 b
->cursor
= nir_after_instr(&tex
->instr
);
421 static const unsigned swiz
[4] = {0, 1, 2, 0};
422 nir_ssa_def
*comp
= nir_swizzle(b
, &tex
->dest
.ssa
, swiz
, 3, true);
425 * (comp <= 0.04045) ?
427 * pow((comp + 0.055) / 1.055, 2.4)
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
,
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
);
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));
447 nir_ssa_def_rewrite_uses_after(&tex
->dest
.ssa
, nir_src_for_ssa(result
),
448 result
->parent_instr
);
452 nir_lower_tex_block(nir_block
*block
, nir_builder
*b
,
453 const nir_lower_tex_options
*options
)
455 bool progress
= false;
457 nir_foreach_instr_safe(instr
, block
) {
458 if (instr
->type
!= nir_instr_type_tex
)
461 nir_tex_instr
*tex
= nir_instr_as_tex(instr
);
462 bool lower_txp
= !!(options
->lower_txp
& (1 << tex
->sampler_dim
));
464 /* mask of src coords to saturate (clamp): */
465 unsigned sat_mask
= 0;
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 */
474 /* If we are clamping any coords, we must lower projector first
475 * as clamping happens *after* projection:
477 if (lower_txp
|| sat_mask
) {
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
;
488 if ((tex
->sampler_dim
== GLSL_SAMPLER_DIM_RECT
) && options
->lower_rect
) {
493 if ((1 << tex
->texture_index
) & options
->lower_y_uv_external
) {
494 lower_y_uv_external(b
, tex
);
498 if ((1 << tex
->texture_index
) & options
->lower_y_u_v_external
) {
499 lower_y_u_v_external(b
, tex
);
503 if ((1 << tex
->texture_index
) & options
->lower_yx_xuxv_external
) {
504 lower_yx_xuxv_external(b
, tex
);
510 saturate_src(b
, tex
, sat_mask
);
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
]);
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
);
533 nir_lower_tex_impl(nir_function_impl
*impl
,
534 const nir_lower_tex_options
*options
)
536 bool progress
= false;
538 nir_builder_init(&builder
, impl
);
540 nir_foreach_block(block
, impl
) {
541 progress
|= nir_lower_tex_block(block
, &builder
, options
);
544 nir_metadata_preserve(impl
, nir_metadata_block_index
|
545 nir_metadata_dominance
);
550 nir_lower_tex(nir_shader
*shader
, const nir_lower_tex_options
*options
)
552 bool progress
= false;
554 nir_foreach_function(function
, shader
) {
556 progress
|= nir_lower_tex_impl(function
->impl
, options
);