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
;
47 project_src(nir_builder
*b
, nir_tex_instr
*tex
)
49 /* Find the projector in the srcs list, if present. */
51 for (proj_index
= 0; proj_index
< tex
->num_srcs
; proj_index
++) {
52 if (tex
->src
[proj_index
].src_type
== nir_tex_src_projector
)
55 if (proj_index
== tex
->num_srcs
)
58 b
->cursor
= nir_before_instr(&tex
->instr
);
60 nir_ssa_def
*inv_proj
=
61 nir_frcp(b
, nir_ssa_for_src(b
, tex
->src
[proj_index
].src
, 1));
63 /* Walk through the sources projecting the arguments. */
64 for (unsigned i
= 0; i
< tex
->num_srcs
; i
++) {
65 switch (tex
->src
[i
].src_type
) {
66 case nir_tex_src_coord
:
67 case nir_tex_src_comparitor
:
72 nir_ssa_def
*unprojected
=
73 nir_ssa_for_src(b
, tex
->src
[i
].src
, nir_tex_instr_src_size(tex
, i
));
74 nir_ssa_def
*projected
= nir_fmul(b
, unprojected
, inv_proj
);
76 /* Array indices don't get projected, so make an new vector with the
77 * coordinate's array index untouched.
79 if (tex
->is_array
&& tex
->src
[i
].src_type
== nir_tex_src_coord
) {
80 switch (tex
->coord_components
) {
82 projected
= nir_vec4(b
,
83 nir_channel(b
, projected
, 0),
84 nir_channel(b
, projected
, 1),
85 nir_channel(b
, projected
, 2),
86 nir_channel(b
, unprojected
, 3));
89 projected
= nir_vec3(b
,
90 nir_channel(b
, projected
, 0),
91 nir_channel(b
, projected
, 1),
92 nir_channel(b
, unprojected
, 2));
95 projected
= nir_vec2(b
,
96 nir_channel(b
, projected
, 0),
97 nir_channel(b
, unprojected
, 1));
100 unreachable("bad texture coord count for array");
105 nir_instr_rewrite_src(&tex
->instr
,
107 nir_src_for_ssa(projected
));
110 /* Now move the later tex sources down the array so that the projector
113 nir_instr_rewrite_src(&tex
->instr
, &tex
->src
[proj_index
].src
,
115 for (unsigned i
= proj_index
+ 1; i
< tex
->num_srcs
; i
++) {
116 tex
->src
[i
-1].src_type
= tex
->src
[i
].src_type
;
117 nir_instr_move_src(&tex
->instr
, &tex
->src
[i
-1].src
, &tex
->src
[i
].src
);
123 get_texture_size(nir_builder
*b
, nir_tex_instr
*tex
)
125 b
->cursor
= nir_before_instr(&tex
->instr
);
127 /* RECT textures should not be array: */
128 assert(!tex
->is_array
);
132 txs
= nir_tex_instr_create(b
->shader
, 1);
133 txs
->op
= nir_texop_txs
;
134 txs
->sampler_dim
= GLSL_SAMPLER_DIM_RECT
;
135 txs
->sampler_index
= tex
->sampler_index
;
137 /* only single src, the lod: */
138 txs
->src
[0].src
= nir_src_for_ssa(nir_imm_int(b
, 0));
139 txs
->src
[0].src_type
= nir_tex_src_lod
;
141 nir_ssa_dest_init(&txs
->instr
, &txs
->dest
, 2, NULL
);
142 nir_builder_instr_insert(b
, &txs
->instr
);
144 return nir_i2f(b
, &txs
->dest
.ssa
);
148 lower_rect(nir_builder
*b
, nir_tex_instr
*tex
)
150 nir_ssa_def
*txs
= get_texture_size(b
, tex
);
151 nir_ssa_def
*scale
= nir_frcp(b
, txs
);
153 /* Walk through the sources normalizing the requested arguments. */
154 for (unsigned i
= 0; i
< tex
->num_srcs
; i
++) {
155 if (tex
->src
[i
].src_type
!= nir_tex_src_coord
)
158 nir_ssa_def
*coords
=
159 nir_ssa_for_src(b
, tex
->src
[i
].src
, tex
->coord_components
);
160 nir_instr_rewrite_src(&tex
->instr
,
162 nir_src_for_ssa(nir_fmul(b
, coords
, scale
)));
165 tex
->sampler_dim
= GLSL_SAMPLER_DIM_2D
;
169 saturate_src(nir_builder
*b
, nir_tex_instr
*tex
, unsigned sat_mask
)
171 b
->cursor
= nir_before_instr(&tex
->instr
);
173 /* Walk through the sources saturating the requested arguments. */
174 for (unsigned i
= 0; i
< tex
->num_srcs
; i
++) {
175 if (tex
->src
[i
].src_type
!= nir_tex_src_coord
)
179 nir_ssa_for_src(b
, tex
->src
[i
].src
, tex
->coord_components
);
181 /* split src into components: */
182 nir_ssa_def
*comp
[4];
184 for (unsigned j
= 0; j
< tex
->coord_components
; j
++)
185 comp
[j
] = nir_channel(b
, src
, j
);
187 /* clamp requested components, array index does not get clamped: */
188 unsigned ncomp
= tex
->coord_components
;
192 for (unsigned j
= 0; j
< ncomp
; j
++) {
193 if ((1 << j
) & sat_mask
) {
194 if (tex
->sampler_dim
== GLSL_SAMPLER_DIM_RECT
) {
195 /* non-normalized texture coords, so clamp to texture
196 * size rather than [0.0, 1.0]
198 nir_ssa_def
*txs
= get_texture_size(b
, tex
);
199 comp
[j
] = nir_fmax(b
, comp
[j
], nir_imm_float(b
, 0.0));
200 comp
[j
] = nir_fmin(b
, comp
[j
], nir_channel(b
, txs
, j
));
202 comp
[j
] = nir_fsat(b
, comp
[j
]);
207 /* and move the result back into a single vecN: */
208 src
= nir_vec(b
, comp
, tex
->coord_components
);
210 nir_instr_rewrite_src(&tex
->instr
,
212 nir_src_for_ssa(src
));
217 nir_lower_tex_block(nir_block
*block
, void *void_state
)
219 lower_tex_state
*state
= void_state
;
220 nir_builder
*b
= &state
->b
;
222 nir_foreach_instr_safe(block
, instr
) {
223 if (instr
->type
!= nir_instr_type_tex
)
226 nir_tex_instr
*tex
= nir_instr_as_tex(instr
);
227 bool lower_txp
= !!(state
->options
->lower_txp
& (1 << tex
->sampler_dim
));
229 /* mask of src coords to saturate (clamp): */
230 unsigned sat_mask
= 0;
232 if ((1 << tex
->sampler_index
) & state
->options
->saturate_r
)
233 sat_mask
|= (1 << 2); /* .z */
234 if ((1 << tex
->sampler_index
) & state
->options
->saturate_t
)
235 sat_mask
|= (1 << 1); /* .y */
236 if ((1 << tex
->sampler_index
) & state
->options
->saturate_s
)
237 sat_mask
|= (1 << 0); /* .x */
239 /* If we are clamping any coords, we must lower projector first
240 * as clamping happens *after* projection:
242 if (lower_txp
|| sat_mask
)
245 if ((tex
->sampler_dim
== GLSL_SAMPLER_DIM_RECT
) &&
246 state
->options
->lower_rect
)
250 saturate_src(b
, tex
, sat_mask
);
257 nir_lower_tex_impl(nir_function_impl
*impl
, lower_tex_state
*state
)
259 nir_builder_init(&state
->b
, impl
);
261 nir_foreach_block(impl
, nir_lower_tex_block
, state
);
263 nir_metadata_preserve(impl
, nir_metadata_block_index
|
264 nir_metadata_dominance
);
268 nir_lower_tex(nir_shader
*shader
, const nir_lower_tex_options
*options
)
270 lower_tex_state state
;
271 state
.options
= options
;
272 nir_foreach_overload(shader
, overload
) {
274 nir_lower_tex_impl(overload
->impl
, &state
);