2 * Copyright © 2016-2018 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
24 #include "v3d_compiler.h"
26 /* We don't do any address packing. */
27 #define __gen_user_data void
28 #define __gen_address_type uint32_t
29 #define __gen_address_offset(reloc) (*reloc)
30 #define __gen_emit_reloc(cl, reloc)
31 #include "cle/v3d_packet_v33_pack.h"
34 v3d33_vir_emit_tex(struct v3d_compile
*c
, nir_tex_instr
*instr
)
36 unsigned unit
= instr
->texture_index
;
38 struct V3D33_TEXTURE_UNIFORM_PARAMETER_0_CFG_MODE1 p0_unpacked
= {
39 V3D33_TEXTURE_UNIFORM_PARAMETER_0_CFG_MODE1_header
,
41 .fetch_sample_mode
= instr
->op
== nir_texop_txf
,
44 struct V3D33_TEXTURE_UNIFORM_PARAMETER_1_CFG_MODE1 p1_unpacked
= {
47 switch (instr
->sampler_dim
) {
48 case GLSL_SAMPLER_DIM_1D
:
50 p0_unpacked
.lookup_type
= TEXTURE_1D_ARRAY
;
52 p0_unpacked
.lookup_type
= TEXTURE_1D
;
54 case GLSL_SAMPLER_DIM_2D
:
55 case GLSL_SAMPLER_DIM_RECT
:
57 p0_unpacked
.lookup_type
= TEXTURE_2D_ARRAY
;
59 p0_unpacked
.lookup_type
= TEXTURE_2D
;
61 case GLSL_SAMPLER_DIM_3D
:
62 p0_unpacked
.lookup_type
= TEXTURE_3D
;
64 case GLSL_SAMPLER_DIM_CUBE
:
65 p0_unpacked
.lookup_type
= TEXTURE_CUBE_MAP
;
68 unreachable("Bad sampler type");
71 struct qreg coords
[5];
73 for (unsigned i
= 0; i
< instr
->num_srcs
; i
++) {
74 switch (instr
->src
[i
].src_type
) {
75 case nir_tex_src_coord
:
76 for (int j
= 0; j
< instr
->coord_components
; j
++) {
77 coords
[next_coord
++] =
78 ntq_get_src(c
, instr
->src
[i
].src
, j
);
80 if (instr
->coord_components
< 2)
81 coords
[next_coord
++] = vir_uniform_f(c
, 0.5);
83 case nir_tex_src_bias
:
84 coords
[next_coord
++] =
85 ntq_get_src(c
, instr
->src
[i
].src
, 0);
87 p0_unpacked
.bias_supplied
= true;
90 coords
[next_coord
++] =
92 ntq_get_src(c
, instr
->src
[i
].src
, 0),
93 vir_uniform(c
, QUNIFORM_TEXTURE_FIRST_LEVEL
,
96 if (instr
->op
!= nir_texop_txf
&&
97 instr
->op
!= nir_texop_tg4
) {
98 p0_unpacked
.disable_autolod_use_bias_only
= true;
101 case nir_tex_src_comparator
:
102 coords
[next_coord
++] =
103 ntq_get_src(c
, instr
->src
[i
].src
, 0);
105 p0_unpacked
.shadow
= true;
108 case nir_tex_src_offset
: {
109 nir_const_value
*offset
=
110 nir_src_as_const_value(instr
->src
[i
].src
);
111 p0_unpacked
.texel_offset_for_s_coordinate
=
114 if (instr
->coord_components
>= 2)
115 p0_unpacked
.texel_offset_for_t_coordinate
=
118 if (instr
->coord_components
>= 3)
119 p0_unpacked
.texel_offset_for_r_coordinate
=
125 unreachable("unknown texture source");
129 bool return_16
= (c
->key
->tex
[unit
].return_size
== 16 ||
132 /* Limit the number of channels returned to both how many the NIR
133 * instruction writes and how many the instruction could produce.
135 uint32_t instr_return_channels
= nir_tex_instr_dest_size(instr
);
137 instr_return_channels
= (instr_return_channels
+ 1) / 2;
139 p1_unpacked
.return_words_of_texture_data
=
140 (1 << MIN2(instr_return_channels
,
141 c
->key
->tex
[unit
].return_channels
)) - 1;
144 V3D33_TEXTURE_UNIFORM_PARAMETER_0_CFG_MODE1_pack(NULL
,
145 (uint8_t *)&p0_packed
,
149 V3D33_TEXTURE_UNIFORM_PARAMETER_1_CFG_MODE1_pack(NULL
,
150 (uint8_t *)&p1_packed
,
152 /* Load unit number into the address field, which will be be used by
153 * the driver to decide which texture to put in the actual address
156 p1_packed
|= unit
<< 5;
158 /* There is no native support for GL texture rectangle coordinates, so
159 * we have to rescale from ([0, width], [0, height]) to ([0, 1], [0,
162 if (instr
->sampler_dim
== GLSL_SAMPLER_DIM_RECT
) {
163 coords
[0] = vir_FMUL(c
, coords
[0],
164 vir_uniform(c
, QUNIFORM_TEXRECT_SCALE_X
,
166 coords
[1] = vir_FMUL(c
, coords
[1],
167 vir_uniform(c
, QUNIFORM_TEXRECT_SCALE_Y
,
171 struct qreg texture_u
[] = {
172 vir_uniform(c
, QUNIFORM_TEXTURE_CONFIG_P0_0
+ unit
, p0_packed
),
173 vir_uniform(c
, QUNIFORM_TEXTURE_CONFIG_P1
, p1_packed
),
175 uint32_t next_texture_u
= 0;
177 for (int i
= 0; i
< next_coord
; i
++) {
180 if (i
== next_coord
- 1)
181 dst
= vir_reg(QFILE_MAGIC
, V3D_QPU_WADDR_TMUL
);
183 dst
= vir_reg(QFILE_MAGIC
, V3D_QPU_WADDR_TMU
);
185 struct qinst
*tmu
= vir_MOV_dest(c
, dst
, coords
[i
]);
188 tmu
->has_implicit_uniform
= true;
189 tmu
->src
[vir_get_implicit_uniform_src(tmu
)] =
190 texture_u
[next_texture_u
++];
196 struct qreg return_values
[4];
197 for (int i
= 0; i
< 4; i
++) {
198 /* Swizzling .zw of an RG texture should give undefined
199 * results, not crash the compiler.
201 if (p1_unpacked
.return_words_of_texture_data
& (1 << i
))
202 return_values
[i
] = vir_LDTMU(c
);
204 return_values
[i
] = c
->undef
;
207 for (int i
= 0; i
< nir_tex_instr_dest_size(instr
); i
++) {
211 STATIC_ASSERT(PIPE_SWIZZLE_X
== 0);
212 chan
= return_values
[i
/ 2];
214 if (nir_alu_type_get_base_type(instr
->dest_type
) ==
216 enum v3d_qpu_input_unpack unpack
;
218 unpack
= V3D_QPU_UNPACK_H
;
220 unpack
= V3D_QPU_UNPACK_L
;
222 chan
= vir_FMOV(c
, chan
);
223 vir_set_unpack(c
->defs
[chan
.index
], 0, unpack
);
225 /* If we're unpacking the low field, shift it
226 * up to the top first.
229 chan
= vir_SHL(c
, chan
,
230 vir_uniform_ui(c
, 16));
233 /* Do proper sign extension to a 32-bit int. */
234 if (nir_alu_type_get_base_type(instr
->dest_type
) ==
236 chan
= vir_ASR(c
, chan
,
237 vir_uniform_ui(c
, 16));
239 chan
= vir_SHR(c
, chan
,
240 vir_uniform_ui(c
, 16));
244 chan
= vir_MOV(c
, return_values
[i
]);
246 ntq_store_dest(c
, &instr
->dest
, i
, chan
);