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 p0_unpacked
.texel_offset_for_s_coordinate
=
110 nir_src_comp_as_int(instr
->src
[i
].src
, 0);
112 if (instr
->coord_components
>= 2)
113 p0_unpacked
.texel_offset_for_t_coordinate
=
114 nir_src_comp_as_int(instr
->src
[i
].src
, 1);
116 if (instr
->coord_components
>= 3)
117 p0_unpacked
.texel_offset_for_r_coordinate
=
118 nir_src_comp_as_int(instr
->src
[i
].src
, 2);
123 unreachable("unknown texture source");
127 /* Limit the number of channels returned to both how many the NIR
128 * instruction writes and how many the instruction could produce.
130 assert(instr
->dest
.is_ssa
);
131 p1_unpacked
.return_words_of_texture_data
=
132 nir_ssa_def_components_read(&instr
->dest
.ssa
);
135 V3D33_TEXTURE_UNIFORM_PARAMETER_0_CFG_MODE1_pack(NULL
,
136 (uint8_t *)&p0_packed
,
140 V3D33_TEXTURE_UNIFORM_PARAMETER_1_CFG_MODE1_pack(NULL
,
141 (uint8_t *)&p1_packed
,
143 /* Load unit number into the address field, which will be be used by
144 * the driver to decide which texture to put in the actual address
147 p1_packed
|= unit
<< 5;
149 /* There is no native support for GL texture rectangle coordinates, so
150 * we have to rescale from ([0, width], [0, height]) to ([0, 1], [0,
153 if (instr
->sampler_dim
== GLSL_SAMPLER_DIM_RECT
) {
154 coords
[0] = vir_FMUL(c
, coords
[0],
155 vir_uniform(c
, QUNIFORM_TEXRECT_SCALE_X
,
157 coords
[1] = vir_FMUL(c
, coords
[1],
158 vir_uniform(c
, QUNIFORM_TEXRECT_SCALE_Y
,
163 vir_get_uniform_index(c
, QUNIFORM_TEXTURE_CONFIG_P0_0
+ unit
, p0_packed
),
164 vir_get_uniform_index(c
, QUNIFORM_TEXTURE_CONFIG_P1
, p1_packed
),
167 for (int i
= 0; i
< next_coord
; i
++) {
170 if (i
== next_coord
- 1)
171 dst
= vir_reg(QFILE_MAGIC
, V3D_QPU_WADDR_TMUL
);
173 dst
= vir_reg(QFILE_MAGIC
, V3D_QPU_WADDR_TMU
);
175 struct qinst
*tmu
= vir_MOV_dest(c
, dst
, coords
[i
]);
178 tmu
->uniform
= texture_u
[i
];
183 for (int i
= 0; i
< 4; i
++) {
184 if (p1_unpacked
.return_words_of_texture_data
& (1 << i
))
185 ntq_store_dest(c
, &instr
->dest
, i
, vir_LDTMU(c
));