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"
43 const nir_lower_tex_options
*options
;
48 project_src(nir_builder
*b
, nir_tex_instr
*tex
)
50 /* Find the projector in the srcs list, if present. */
52 for (proj_index
= 0; proj_index
< tex
->num_srcs
; proj_index
++) {
53 if (tex
->src
[proj_index
].src_type
== nir_tex_src_projector
)
56 if (proj_index
== tex
->num_srcs
)
59 b
->cursor
= nir_before_instr(&tex
->instr
);
61 nir_ssa_def
*inv_proj
=
62 nir_frcp(b
, nir_ssa_for_src(b
, tex
->src
[proj_index
].src
, 1));
64 /* Walk through the sources projecting the arguments. */
65 for (unsigned i
= 0; i
< tex
->num_srcs
; i
++) {
66 switch (tex
->src
[i
].src_type
) {
67 case nir_tex_src_coord
:
68 case nir_tex_src_comparitor
:
73 nir_ssa_def
*unprojected
=
74 nir_ssa_for_src(b
, tex
->src
[i
].src
, nir_tex_instr_src_size(tex
, i
));
75 nir_ssa_def
*projected
= nir_fmul(b
, unprojected
, inv_proj
);
77 /* Array indices don't get projected, so make an new vector with the
78 * coordinate's array index untouched.
80 if (tex
->is_array
&& tex
->src
[i
].src_type
== nir_tex_src_coord
) {
81 switch (tex
->coord_components
) {
83 projected
= nir_vec4(b
,
84 nir_channel(b
, projected
, 0),
85 nir_channel(b
, projected
, 1),
86 nir_channel(b
, projected
, 2),
87 nir_channel(b
, unprojected
, 3));
90 projected
= nir_vec3(b
,
91 nir_channel(b
, projected
, 0),
92 nir_channel(b
, projected
, 1),
93 nir_channel(b
, unprojected
, 2));
96 projected
= nir_vec2(b
,
97 nir_channel(b
, projected
, 0),
98 nir_channel(b
, unprojected
, 1));
101 unreachable("bad texture coord count for array");
106 nir_instr_rewrite_src(&tex
->instr
,
108 nir_src_for_ssa(projected
));
111 /* Now move the later tex sources down the array so that the projector
114 nir_instr_rewrite_src(&tex
->instr
, &tex
->src
[proj_index
].src
,
116 for (unsigned i
= proj_index
+ 1; i
< tex
->num_srcs
; i
++) {
117 tex
->src
[i
-1].src_type
= tex
->src
[i
].src_type
;
118 nir_instr_move_src(&tex
->instr
, &tex
->src
[i
-1].src
, &tex
->src
[i
].src
);
124 get_texture_size(nir_builder
*b
, nir_tex_instr
*tex
)
126 b
->cursor
= nir_before_instr(&tex
->instr
);
128 /* RECT textures should not be array: */
129 assert(!tex
->is_array
);
133 txs
= nir_tex_instr_create(b
->shader
, 1);
134 txs
->op
= nir_texop_txs
;
135 txs
->sampler_dim
= GLSL_SAMPLER_DIM_RECT
;
136 txs
->sampler_index
= tex
->sampler_index
;
137 txs
->dest_type
= nir_type_int
;
139 /* only single src, the lod: */
140 txs
->src
[0].src
= nir_src_for_ssa(nir_imm_int(b
, 0));
141 txs
->src
[0].src_type
= nir_tex_src_lod
;
143 nir_ssa_dest_init(&txs
->instr
, &txs
->dest
, 2, NULL
);
144 nir_builder_instr_insert(b
, &txs
->instr
);
146 return nir_i2f(b
, &txs
->dest
.ssa
);
150 lower_rect(nir_builder
*b
, nir_tex_instr
*tex
)
152 nir_ssa_def
*txs
= get_texture_size(b
, tex
);
153 nir_ssa_def
*scale
= nir_frcp(b
, txs
);
155 /* Walk through the sources normalizing the requested arguments. */
156 for (unsigned i
= 0; i
< tex
->num_srcs
; i
++) {
157 if (tex
->src
[i
].src_type
!= nir_tex_src_coord
)
160 nir_ssa_def
*coords
=
161 nir_ssa_for_src(b
, tex
->src
[i
].src
, tex
->coord_components
);
162 nir_instr_rewrite_src(&tex
->instr
,
164 nir_src_for_ssa(nir_fmul(b
, coords
, scale
)));
167 tex
->sampler_dim
= GLSL_SAMPLER_DIM_2D
;
171 saturate_src(nir_builder
*b
, nir_tex_instr
*tex
, unsigned sat_mask
)
173 b
->cursor
= nir_before_instr(&tex
->instr
);
175 /* Walk through the sources saturating the requested arguments. */
176 for (unsigned i
= 0; i
< tex
->num_srcs
; i
++) {
177 if (tex
->src
[i
].src_type
!= nir_tex_src_coord
)
181 nir_ssa_for_src(b
, tex
->src
[i
].src
, tex
->coord_components
);
183 /* split src into components: */
184 nir_ssa_def
*comp
[4];
186 for (unsigned j
= 0; j
< tex
->coord_components
; j
++)
187 comp
[j
] = nir_channel(b
, src
, j
);
189 /* clamp requested components, array index does not get clamped: */
190 unsigned ncomp
= tex
->coord_components
;
194 for (unsigned j
= 0; j
< ncomp
; j
++) {
195 if ((1 << j
) & sat_mask
) {
196 if (tex
->sampler_dim
== GLSL_SAMPLER_DIM_RECT
) {
197 /* non-normalized texture coords, so clamp to texture
198 * size rather than [0.0, 1.0]
200 nir_ssa_def
*txs
= get_texture_size(b
, tex
);
201 comp
[j
] = nir_fmax(b
, comp
[j
], nir_imm_float(b
, 0.0));
202 comp
[j
] = nir_fmin(b
, comp
[j
], nir_channel(b
, txs
, j
));
204 comp
[j
] = nir_fsat(b
, comp
[j
]);
209 /* and move the result back into a single vecN: */
210 src
= nir_vec(b
, comp
, tex
->coord_components
);
212 nir_instr_rewrite_src(&tex
->instr
,
214 nir_src_for_ssa(src
));
219 get_zero_or_one(nir_builder
*b
, nir_alu_type type
, uint8_t swizzle_val
)
223 memset(&v
, 0, sizeof(v
));
225 if (swizzle_val
== 4) {
226 v
.u
[0] = v
.u
[1] = v
.u
[2] = v
.u
[3] = 0;
228 assert(swizzle_val
== 5);
229 if (type
== nir_type_float
)
230 v
.f
[0] = v
.f
[1] = v
.f
[2] = v
.f
[3] = 1.0;
232 v
.u
[0] = v
.u
[1] = v
.u
[2] = v
.u
[3] = 1;
235 return nir_build_imm(b
, 4, v
);
239 swizzle_result(nir_builder
*b
, nir_tex_instr
*tex
, const uint8_t swizzle
[4])
241 assert(tex
->dest
.is_ssa
);
243 b
->cursor
= nir_after_instr(&tex
->instr
);
245 nir_ssa_def
*swizzled
;
246 if (tex
->op
== nir_texop_tg4
) {
247 if (swizzle
[tex
->component
] < 4) {
248 /* This one's easy */
249 tex
->component
= swizzle
[tex
->component
];
252 swizzled
= get_zero_or_one(b
, tex
->dest_type
, swizzle
[tex
->component
]);
255 assert(nir_tex_instr_dest_size(tex
) == 4);
256 if (swizzle
[0] < 4 && swizzle
[1] < 4 &&
257 swizzle
[2] < 4 && swizzle
[3] < 4) {
258 unsigned swiz
[4] = { swizzle
[0], swizzle
[1], swizzle
[2], swizzle
[3] };
259 /* We have no 0's or 1's, just emit a swizzling MOV */
260 swizzled
= nir_swizzle(b
, &tex
->dest
.ssa
, swiz
, 4, false);
262 nir_ssa_def
*srcs
[4];
263 for (unsigned i
= 0; i
< 4; i
++) {
264 if (swizzle
[i
] < 4) {
265 srcs
[i
] = nir_channel(b
, &tex
->dest
.ssa
, swizzle
[i
]);
267 srcs
[i
] = get_zero_or_one(b
, tex
->dest_type
, swizzle
[i
]);
270 swizzled
= nir_vec(b
, srcs
, 4);
274 nir_ssa_def_rewrite_uses_after(&tex
->dest
.ssa
, nir_src_for_ssa(swizzled
),
275 swizzled
->parent_instr
);
279 nir_lower_tex_block(nir_block
*block
, void *void_state
)
281 lower_tex_state
*state
= void_state
;
282 nir_builder
*b
= &state
->b
;
284 nir_foreach_instr_safe(block
, instr
) {
285 if (instr
->type
!= nir_instr_type_tex
)
288 nir_tex_instr
*tex
= nir_instr_as_tex(instr
);
289 bool lower_txp
= !!(state
->options
->lower_txp
& (1 << tex
->sampler_dim
));
291 /* mask of src coords to saturate (clamp): */
292 unsigned sat_mask
= 0;
294 if ((1 << tex
->sampler_index
) & state
->options
->saturate_r
)
295 sat_mask
|= (1 << 2); /* .z */
296 if ((1 << tex
->sampler_index
) & state
->options
->saturate_t
)
297 sat_mask
|= (1 << 1); /* .y */
298 if ((1 << tex
->sampler_index
) & state
->options
->saturate_s
)
299 sat_mask
|= (1 << 0); /* .x */
301 /* If we are clamping any coords, we must lower projector first
302 * as clamping happens *after* projection:
304 if (lower_txp
|| sat_mask
) {
306 state
->progress
= true;
309 if ((tex
->sampler_dim
== GLSL_SAMPLER_DIM_RECT
) &&
310 state
->options
->lower_rect
) {
312 state
->progress
= true;
316 saturate_src(b
, tex
, sat_mask
);
317 state
->progress
= true;
320 if (((1 << tex
->sampler_index
) & state
->options
->swizzle_result
) &&
321 !nir_tex_instr_is_query(tex
) &&
322 !(tex
->is_shadow
&& tex
->is_new_style_shadow
)) {
323 swizzle_result(b
, tex
, state
->options
->swizzles
[tex
->sampler_index
]);
324 state
->progress
= true;
332 nir_lower_tex_impl(nir_function_impl
*impl
, lower_tex_state
*state
)
334 nir_builder_init(&state
->b
, impl
);
336 nir_foreach_block(impl
, nir_lower_tex_block
, state
);
338 nir_metadata_preserve(impl
, nir_metadata_block_index
|
339 nir_metadata_dominance
);
343 nir_lower_tex(nir_shader
*shader
, const nir_lower_tex_options
*options
)
345 lower_tex_state state
;
346 state
.options
= options
;
347 state
.progress
= false;
349 nir_foreach_function(shader
, function
) {
351 nir_lower_tex_impl(function
->impl
, &state
);
354 return state
.progress
;