2 * Copyright (c) 2011-2013 Luc Verhaegen <libv@skynet.be>
3 * Copyright (c) 2018-2019 Lima Project
5 * Permission is hereby granted, free of charge, to any person obtaining a
6 * copy of this software and associated documentation files (the "Software"),
7 * to deal in the Software without restriction, including without limitation
8 * the rights to use, copy, modify, merge, publish, distribute, sub license,
9 * and/or sell copies of the Software, and to permit persons to whom the
10 * Software is furnished to do so, subject to the following conditions:
12 * The above copyright notice and this permission notice (including the
13 * next paragraph) shall be included in all copies or substantial portions
16 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
17 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
18 * FITNESS FOR A PARTICULAR PURPOSE AND NON-INFRINGEMENT. IN NO EVENT SHALL
19 * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
20 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
21 * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
22 * DEALINGS IN THE SOFTWARE.
26 #include "util/u_memory.h"
27 #include "util/u_upload_mgr.h"
28 #include "util/u_math.h"
29 #include "util/u_debug.h"
30 #include "util/u_transfer.h"
33 #include "lima_context.h"
34 #include "lima_screen.h"
35 #include "lima_texture.h"
36 #include "lima_resource.h"
37 #include "lima_submit.h"
38 #include "lima_util.h"
39 #include "lima_format.h"
41 #include <drm-uapi/lima_drm.h>
44 #define lima_tex_list_size 64
46 static_assert(offsetof(lima_tex_desc
, va
) == 24, "lima_tex_desc->va offset isn't 24");
50 lima_texture_desc_set_va(lima_tex_desc
*desc
,
54 unsigned va_bit_idx
= VA_BIT_OFFSET
+ (VA_BIT_SIZE
* idx
);
55 unsigned va_idx
= va_bit_idx
/ 32;
60 desc
->va
[va_idx
] |= va
<< va_bit_idx
;
63 desc
->va
[va_idx
+ 1] |= va
>> (32 - va_bit_idx
);
67 lima_texture_desc_set_res(struct lima_context
*ctx
, lima_tex_desc
*desc
,
68 struct pipe_resource
*prsc
,
69 unsigned first_level
, unsigned last_level
)
71 unsigned width
, height
, layout
, i
;
72 struct lima_resource
*lima_res
= lima_resource(prsc
);
75 height
= prsc
->height0
;
76 if (first_level
!= 0) {
77 width
= u_minify(width
, first_level
);
78 height
= u_minify(height
, first_level
);
81 desc
->format
= lima_format_get_texel(prsc
->format
);
82 desc
->swap_r_b
= lima_format_get_swap_rb(prsc
->format
);
84 desc
->height
= height
;
85 desc
->unknown_3_1
= 1;
90 /* for padded linear texture */
91 if (lima_res
->levels
[first_level
].width
!= width
) {
92 desc
->stride
= lima_res
->levels
[first_level
].stride
;
98 lima_submit_add_bo(ctx
->pp_submit
, lima_res
->bo
, LIMA_SUBMIT_BO_READ
);
100 uint32_t base_va
= lima_res
->bo
->va
;
102 /* attach first level */
103 uint32_t first_va
= base_va
+ lima_res
->levels
[first_level
].offset
;
104 desc
->va_s
.va_0
= first_va
>> 6;
105 desc
->va_s
.layout
= layout
;
107 /* Attach remaining levels.
108 * Each subsequent mipmap address is specified using the 26 msbs.
109 * These addresses are then packed continuously in memory */
110 for (i
= 1; i
<= (last_level
- first_level
); i
++) {
111 uint32_t address
= base_va
+ lima_res
->levels
[first_level
+ i
].offset
;
112 lima_texture_desc_set_va(desc
, i
, address
);
117 lima_update_tex_desc(struct lima_context
*ctx
, struct lima_sampler_state
*sampler
,
118 struct lima_sampler_view
*texture
, void *pdesc
,
121 lima_tex_desc
*desc
= pdesc
;
122 unsigned first_level
;
125 memset(desc
, 0, desc_size
);
128 desc
->texture_2d
= 1;
130 if (!sampler
->base
.normalized_coords
)
131 desc
->unnorm_coords
= 1;
133 first_level
= texture
->base
.u
.tex
.first_level
;
134 last_level
= texture
->base
.u
.tex
.last_level
;
135 if (last_level
- first_level
>= LIMA_MAX_MIP_LEVELS
)
136 last_level
= first_level
+ LIMA_MAX_MIP_LEVELS
- 1;
138 desc
->miplevels
= (last_level
- first_level
);
140 switch (sampler
->base
.min_mip_filter
) {
141 case PIPE_TEX_MIPFILTER_LINEAR
:
142 desc
->min_mipfilter_1
= 0;
143 desc
->min_mipfilter_2
= 3;
145 case PIPE_TEX_MIPFILTER_NEAREST
:
146 desc
->min_mipfilter_1
= 0x1ff;
147 desc
->min_mipfilter_2
= 0;
149 case PIPE_TEX_MIPFILTER_NONE
:
154 switch (sampler
->base
.mag_img_filter
) {
155 case PIPE_TEX_FILTER_LINEAR
:
156 desc
->mag_img_filter_nearest
= 0;
158 case PIPE_TEX_FILTER_NEAREST
:
160 desc
->mag_img_filter_nearest
= 1;
164 switch (sampler
->base
.min_img_filter
) {
166 case PIPE_TEX_FILTER_LINEAR
:
167 desc
->min_img_filter_nearest
= 0;
169 case PIPE_TEX_FILTER_NEAREST
:
171 desc
->min_img_filter_nearest
= 1;
175 /* Only clamp, clamp to edge, repeat and mirror repeat are supported */
176 switch (sampler
->base
.wrap_s
) {
177 case PIPE_TEX_WRAP_CLAMP
:
178 desc
->wrap_s_clamp
= 1;
180 case PIPE_TEX_WRAP_CLAMP_TO_EDGE
:
181 case PIPE_TEX_WRAP_CLAMP_TO_BORDER
:
182 desc
->wrap_s_clamp_to_edge
= 1;
184 case PIPE_TEX_WRAP_MIRROR_REPEAT
:
185 desc
->wrap_s_mirror_repeat
= 1;
187 case PIPE_TEX_WRAP_REPEAT
:
192 /* Only clamp, clamp to edge, repeat and mirror repeat are supported */
193 switch (sampler
->base
.wrap_t
) {
194 case PIPE_TEX_WRAP_CLAMP
:
195 desc
->wrap_t_clamp
= 1;
197 case PIPE_TEX_WRAP_CLAMP_TO_EDGE
:
198 case PIPE_TEX_WRAP_CLAMP_TO_BORDER
:
199 desc
->wrap_t_clamp_to_edge
= 1;
201 case PIPE_TEX_WRAP_MIRROR_REPEAT
:
202 desc
->wrap_t_mirror_repeat
= 1;
204 case PIPE_TEX_WRAP_REPEAT
:
209 lima_texture_desc_set_res(ctx
, desc
, texture
->base
.texture
,
210 first_level
, last_level
);
214 lima_calc_tex_desc_size(struct lima_sampler_view
*texture
)
216 unsigned size
= offsetof(lima_tex_desc
, va
);
217 unsigned va_bit_size
;
218 unsigned first_level
= texture
->base
.u
.tex
.first_level
;
219 unsigned last_level
= texture
->base
.u
.tex
.last_level
;
221 if (last_level
- first_level
>= LIMA_MAX_MIP_LEVELS
)
222 last_level
= first_level
+ LIMA_MAX_MIP_LEVELS
- 1;
224 va_bit_size
= VA_BIT_OFFSET
+ VA_BIT_SIZE
* (last_level
- first_level
+ 1);
225 size
+= (va_bit_size
+ 7) >> 3;
226 size
= align(size
, lima_min_tex_desc_size
);
232 lima_update_textures(struct lima_context
*ctx
)
234 struct lima_texture_stateobj
*lima_tex
= &ctx
->tex_stateobj
;
236 assert (lima_tex
->num_samplers
<= 16);
238 /* Nothing to do - we have no samplers or textures */
239 if (!lima_tex
->num_samplers
|| !lima_tex
->num_textures
)
242 unsigned size
= lima_tex_list_size
;
243 for (int i
= 0; i
< lima_tex
->num_samplers
; i
++) {
244 struct lima_sampler_view
*texture
= lima_sampler_view(lima_tex
->textures
[i
]);
245 size
+= lima_calc_tex_desc_size(texture
);
249 lima_ctx_buff_alloc(ctx
, lima_ctx_buff_pp_tex_desc
, size
, true);
251 off_t offset
= lima_tex_list_size
;
252 for (int i
= 0; i
< lima_tex
->num_samplers
; i
++) {
253 struct lima_sampler_state
*sampler
= lima_sampler_state(lima_tex
->samplers
[i
]);
254 struct lima_sampler_view
*texture
= lima_sampler_view(lima_tex
->textures
[i
]);
255 unsigned desc_size
= lima_calc_tex_desc_size(texture
);
257 descs
[i
] = lima_ctx_buff_va(ctx
, lima_ctx_buff_pp_tex_desc
,
258 LIMA_CTX_BUFF_SUBMIT_PP
) + offset
;
259 lima_update_tex_desc(ctx
, sampler
, texture
, (void *)descs
+ offset
, desc_size
);
263 lima_dump_command_stream_print(
264 descs
, size
, false, "add textures_desc at va %x\n",
265 lima_ctx_buff_va(ctx
, lima_ctx_buff_pp_tex_desc
, 0));