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_v41_pack.h"
34 vir_TMU_WRITE(struct v3d_compile
*c
, enum v3d_qpu_waddr waddr
, struct qreg val
,
37 /* XXX perf: We should figure out how to merge ALU operations
38 * producing the val with this MOV, when possible.
40 vir_MOV_dest(c
, vir_reg(QFILE_MAGIC
, waddr
), val
);
46 vir_WRTMUC(struct v3d_compile
*c
, enum quniform_contents contents
, uint32_t data
)
48 struct qinst
*inst
= vir_NOP(c
);
49 inst
->qpu
.sig
.wrtmuc
= true;
50 inst
->has_implicit_uniform
= true;
51 inst
->src
[0] = vir_uniform(c
, contents
, data
);
55 v3d40_vir_emit_tex(struct v3d_compile
*c
, nir_tex_instr
*instr
)
57 unsigned unit
= instr
->texture_index
;
59 static const struct V3D41_TMU_CONFIG_PARAMETER_2 p2_unpacked_default
= {
60 .op
= V3D_TMU_OP_REGULAR
,
63 struct V3D41_TMU_CONFIG_PARAMETER_0 p0_unpacked
= {
66 struct V3D41_TMU_CONFIG_PARAMETER_1 p1_unpacked
= {
67 .output_type_32_bit
= (c
->key
->tex
[unit
].return_size
== 32 &&
70 .unnormalized_coordinates
= (instr
->sampler_dim
==
71 GLSL_SAMPLER_DIM_RECT
),
74 struct V3D41_TMU_CONFIG_PARAMETER_2 p2_unpacked
= {
75 .op
= V3D_TMU_OP_REGULAR
,
77 .gather_mode
= instr
->op
== nir_texop_tg4
,
78 .gather_component
= instr
->component
,
80 .coefficient_mode
= instr
->op
== nir_texop_txd
,
82 .disable_autolod
= instr
->op
== nir_texop_tg4
85 int non_array_components
= instr
->coord_components
- instr
->is_array
;
88 for (unsigned i
= 0; i
< instr
->num_srcs
; i
++) {
89 switch (instr
->src
[i
].src_type
) {
90 case nir_tex_src_coord
:
91 /* S triggers the lookup, so save it for the end. */
92 s
= ntq_get_src(c
, instr
->src
[i
].src
, 0);
94 if (non_array_components
> 1) {
95 vir_TMU_WRITE(c
, V3D_QPU_WADDR_TMUT
,
96 ntq_get_src(c
, instr
->src
[i
].src
,
99 if (non_array_components
> 2) {
100 vir_TMU_WRITE(c
, V3D_QPU_WADDR_TMUR
,
101 ntq_get_src(c
, instr
->src
[i
].src
,
105 if (instr
->is_array
) {
106 vir_TMU_WRITE(c
, V3D_QPU_WADDR_TMUI
,
107 ntq_get_src(c
, instr
->src
[i
].src
,
108 instr
->coord_components
- 1),
113 case nir_tex_src_bias
:
114 vir_TMU_WRITE(c
, V3D_QPU_WADDR_TMUB
,
115 ntq_get_src(c
, instr
->src
[i
].src
, 0),
119 case nir_tex_src_lod
:
120 vir_TMU_WRITE(c
, V3D_QPU_WADDR_TMUB
,
121 ntq_get_src(c
, instr
->src
[i
].src
, 0),
124 if (instr
->op
!= nir_texop_txf
)
125 p2_unpacked
.disable_autolod
= true;
128 case nir_tex_src_comparator
:
129 vir_TMU_WRITE(c
, V3D_QPU_WADDR_TMUDREF
,
130 ntq_get_src(c
, instr
->src
[i
].src
, 0),
134 case nir_tex_src_offset
: {
135 if (nir_src_is_const(instr
->src
[i
].src
)) {
136 nir_const_value
*offset
=
137 nir_src_as_const_value(instr
->src
[i
].src
);
139 p2_unpacked
.offset_s
= offset
->i32
[0];
140 if (instr
->coord_components
>= 2)
141 p2_unpacked
.offset_t
= offset
->i32
[1];
142 if (instr
->coord_components
>= 3)
143 p2_unpacked
.offset_r
= offset
->i32
[2];
145 struct qreg mask
= vir_uniform_ui(c
, 0xf);
146 struct qreg x
, y
, offset
;
148 x
= vir_AND(c
, ntq_get_src(c
, instr
->src
[i
].src
,
150 y
= vir_AND(c
, ntq_get_src(c
, instr
->src
[i
].src
,
152 offset
= vir_OR(c
, x
,
154 vir_uniform_ui(c
, 4)));
156 vir_TMU_WRITE(c
, V3D_QPU_WADDR_TMUOFF
,
157 offset
, &tmu_writes
);
163 unreachable("unknown texture source");
167 /* Limit the number of channels returned to both how many the NIR
168 * instruction writes and how many the instruction could produce.
170 * XXX perf: Can we also limit to the number of channels that are
171 * actually read by the users of this NIR dest, so that we don't need
172 * to emit unused LDTMUs?
174 uint32_t instr_return_channels
= nir_tex_instr_dest_size(instr
);
175 if (!p1_unpacked
.output_type_32_bit
)
176 instr_return_channels
= (instr_return_channels
+ 1) / 2;
178 p0_unpacked
.return_words_of_texture_data
=
179 (1 << MIN2(instr_return_channels
,
180 c
->key
->tex
[unit
].return_channels
)) - 1;
182 /* Word enables can't ask for more channels than the output type could
183 * provide (2 for f16, 4 for 32-bit).
185 assert(!p1_unpacked
.output_type_32_bit
||
186 p0_unpacked
.return_words_of_texture_data
< (1 << 4));
187 assert(p1_unpacked
.output_type_32_bit
||
188 p0_unpacked
.return_words_of_texture_data
< (1 << 2));
191 V3D41_TMU_CONFIG_PARAMETER_0_pack(NULL
,
192 (uint8_t *)&p0_packed
,
196 V3D41_TMU_CONFIG_PARAMETER_1_pack(NULL
,
197 (uint8_t *)&p1_packed
,
201 V3D41_TMU_CONFIG_PARAMETER_2_pack(NULL
,
202 (uint8_t *)&p2_packed
,
205 /* Load unit number into the high bits of the texture or sampler
206 * address field, which will be be used by the driver to decide which
207 * texture to put in the actual address field.
209 p0_packed
|= unit
<< 24;
210 p1_packed
|= unit
<< 24;
212 vir_WRTMUC(c
, QUNIFORM_TMU_CONFIG_P0
, p0_packed
);
213 /* XXX perf: Can we skip p1 setup for txf ops? */
214 vir_WRTMUC(c
, QUNIFORM_TMU_CONFIG_P1
, p1_packed
);
215 if (memcmp(&p2_unpacked
, &p2_unpacked_default
, sizeof(p2_unpacked
)) != 0)
216 vir_WRTMUC(c
, QUNIFORM_CONSTANT
, p2_packed
);
218 if (instr
->op
== nir_texop_txf
) {
219 assert(instr
->sampler_dim
!= GLSL_SAMPLER_DIM_CUBE
);
220 vir_TMU_WRITE(c
, V3D_QPU_WADDR_TMUSF
, s
, &tmu_writes
);
221 } else if (instr
->sampler_dim
== GLSL_SAMPLER_DIM_CUBE
) {
222 vir_TMU_WRITE(c
, V3D_QPU_WADDR_TMUSCM
, s
, &tmu_writes
);
224 vir_TMU_WRITE(c
, V3D_QPU_WADDR_TMUS
, s
, &tmu_writes
);
229 /* The input FIFO has 16 slots across all threads, so make sure we
230 * don't overfill our allocation.
232 while (tmu_writes
> 16 / c
->threads
)
235 struct qreg return_values
[4];
236 for (int i
= 0; i
< 4; i
++) {
237 /* Swizzling .zw of an RG texture should give undefined
238 * results, not crash the compiler.
240 if (p0_unpacked
.return_words_of_texture_data
& (1 << i
))
241 return_values
[i
] = vir_LDTMU(c
);
243 return_values
[i
] = c
->undef
;
246 for (int i
= 0; i
< nir_tex_instr_dest_size(instr
); i
++) {
249 if (!p1_unpacked
.output_type_32_bit
) {
250 STATIC_ASSERT(PIPE_SWIZZLE_X
== 0);
251 chan
= return_values
[i
/ 2];
253 /* XXX perf: We should move this unpacking into NIR.
254 * That would give us exposure of these types to NIR
255 * optimization, so that (for example) a repacking of
256 * half-float samples to the half-float render target
257 * could be eliminated.
259 if (nir_alu_type_get_base_type(instr
->dest_type
) ==
261 enum v3d_qpu_input_unpack unpack
;
263 unpack
= V3D_QPU_UNPACK_H
;
265 unpack
= V3D_QPU_UNPACK_L
;
267 chan
= vir_FMOV(c
, chan
);
268 vir_set_unpack(c
->defs
[chan
.index
], 0, unpack
);
270 /* If we're unpacking the low field, shift it
271 * up to the top first.
274 chan
= vir_SHL(c
, chan
,
275 vir_uniform_ui(c
, 16));
278 /* Do proper sign extension to a 32-bit int. */
279 if (nir_alu_type_get_base_type(instr
->dest_type
) ==
281 chan
= vir_ASR(c
, chan
,
282 vir_uniform_ui(c
, 16));
284 chan
= vir_SHR(c
, chan
,
285 vir_uniform_ui(c
, 16));
289 chan
= vir_MOV(c
, return_values
[i
]);
291 ntq_store_dest(c
, &instr
->dest
, i
, chan
);