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 converts the coordinate division for texture projection
26 * to be done in ALU instructions instead of asking the texture operation to
31 #include "nir_builder.h"
34 channel(nir_builder
*b
, nir_ssa_def
*def
, int c
)
36 return nir_swizzle(b
, def
, (unsigned[4]){c
, c
, c
, c
}, 1, false);
40 nir_lower_tex_projector_block(nir_block
*block
, void *void_state
)
42 nir_builder
*b
= void_state
;
44 nir_foreach_instr_safe(block
, instr
) {
45 if (instr
->type
!= nir_instr_type_tex
)
48 nir_tex_instr
*tex
= nir_instr_as_tex(instr
);
49 nir_builder_insert_before_instr(b
, &tex
->instr
);
51 /* Find the projector in the srcs list, if present. */
53 for (proj_index
= 0; proj_index
< tex
->num_srcs
; proj_index
++) {
54 if (tex
->src
[proj_index
].src_type
== nir_tex_src_projector
)
57 if (proj_index
== tex
->num_srcs
)
59 nir_ssa_def
*inv_proj
=
60 nir_frcp(b
, nir_ssa_for_src(b
, tex
->src
[proj_index
].src
, 1));
62 /* Walk through the sources projecting the arguments. */
63 for (int i
= 0; i
< tex
->num_srcs
; i
++) {
64 switch (tex
->src
[i
].src_type
) {
65 case nir_tex_src_coord
:
66 case nir_tex_src_comparitor
:
71 nir_ssa_def
*unprojected
=
72 nir_ssa_for_src(b
, tex
->src
[i
].src
, nir_tex_instr_src_size(tex
, i
));
73 nir_ssa_def
*projected
= nir_fmul(b
, unprojected
, inv_proj
);
75 /* Array indices don't get projected, so make an new vector with the
76 * coordinate's array index untouched.
78 if (tex
->is_array
&& tex
->src
[i
].src_type
== nir_tex_src_coord
) {
79 switch (tex
->coord_components
) {
81 projected
= nir_vec4(b
,
82 channel(b
, projected
, 0),
83 channel(b
, projected
, 1),
84 channel(b
, projected
, 2),
85 channel(b
, unprojected
, 3));
88 projected
= nir_vec3(b
,
89 channel(b
, projected
, 0),
90 channel(b
, projected
, 1),
91 channel(b
, unprojected
, 2));
94 projected
= nir_vec2(b
,
95 channel(b
, projected
, 0),
96 channel(b
, unprojected
, 1));
99 unreachable("bad texture coord count for array");
104 nir_instr_rewrite_src(&tex
->instr
,
106 nir_src_for_ssa(projected
));
109 /* Now move the later tex sources down the array so that the projector
112 nir_instr_rewrite_src(&tex
->instr
, &tex
->src
[proj_index
].src
,
114 memmove(&tex
->src
[proj_index
],
115 &tex
->src
[proj_index
+ 1],
116 (tex
->num_srcs
- proj_index
) * sizeof(*tex
->src
));
124 nir_lower_tex_projector_impl(nir_function_impl
*impl
)
127 nir_builder_init(&b
, impl
);
129 nir_foreach_block(impl
, nir_lower_tex_projector_block
, &b
);
131 nir_metadata_preserve(impl
, nir_metadata_block_index
|
132 nir_metadata_dominance
);
136 nir_lower_tex_projector(nir_shader
*shader
)
138 nir_foreach_overload(shader
, overload
) {
140 nir_lower_tex_projector_impl(overload
->impl
);