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"
40 #include <drm-uapi/lima_drm.h>
42 #define LIMA_TEXEL_FORMAT_BGR_565 0x0e
43 #define LIMA_TEXEL_FORMAT_Z16 0x12
44 #define LIMA_TEXEL_FORMAT_RGB_888 0x15
45 #define LIMA_TEXEL_FORMAT_RGBA_8888 0x16
46 #define LIMA_TEXEL_FORMAT_RGBX_8888 0x17
47 #define LIMA_TEXEL_FORMAT_Z24S8 0x2c
49 #define lima_tex_list_size 64
51 static uint32_t pipe_format_to_lima(enum pipe_format pformat
)
53 unsigned swap_chans
= 0, flag1
= 0, format
;
56 case PIPE_FORMAT_R8G8B8A8_UNORM
:
58 case PIPE_FORMAT_B8G8R8A8_UNORM
:
59 format
= LIMA_TEXEL_FORMAT_RGBA_8888
;
61 case PIPE_FORMAT_R8G8B8X8_UNORM
:
63 case PIPE_FORMAT_B8G8R8X8_UNORM
:
64 format
= LIMA_TEXEL_FORMAT_RGBX_8888
;
66 case PIPE_FORMAT_R8G8B8_UNORM
:
68 format
= LIMA_TEXEL_FORMAT_RGB_888
;
70 case PIPE_FORMAT_B5G6R5_UNORM
:
71 format
= LIMA_TEXEL_FORMAT_BGR_565
;
73 case PIPE_FORMAT_Z24_UNORM_S8_UINT
:
74 case PIPE_FORMAT_Z24X8_UNORM
:
75 format
= LIMA_TEXEL_FORMAT_Z24S8
;
77 case PIPE_FORMAT_Z16_UNORM
:
78 format
= LIMA_TEXEL_FORMAT_Z16
;
85 return (swap_chans
<< 7) | (flag1
<< 6) | format
;
89 lima_texture_desc_set_res(struct lima_context
*ctx
, uint32_t *desc
,
90 struct pipe_resource
*prsc
,
91 unsigned first_level
, unsigned last_level
)
93 unsigned width
, height
, layout
, i
;
94 struct lima_resource
*lima_res
= lima_resource(prsc
);
97 height
= prsc
->height0
;
98 if (first_level
!= 0) {
99 width
= u_minify(width
, first_level
);
100 height
= u_minify(height
, first_level
);
103 desc
[0] |= pipe_format_to_lima(prsc
->format
);
104 desc
[2] |= (width
<< 22);
105 desc
[3] |= 0x10000 | (height
<< 3) | (width
>> 10);
110 /* for padded linear texture */
111 if (lima_res
->levels
[first_level
].width
!= width
) {
112 desc
[0] |= lima_res
->levels
[first_level
].width
<< 18;
118 lima_submit_add_bo(ctx
->pp_submit
, lima_res
->bo
, LIMA_SUBMIT_BO_READ
);
120 uint32_t base_va
= lima_res
->bo
->va
;
122 /* attach first level */
123 uint32_t first_va
= base_va
+ lima_res
->levels
[first_level
].offset
;
124 desc
[6] |= (first_va
<< 24) | (layout
<< 13);
125 desc
[7] |= first_va
>> 8;
127 /* Attach remaining levels.
128 * Each subsequent mipmap address is specified using the 26 msbs.
129 * These addresses are then packed continuously in memory */
130 unsigned current_desc_index
= 7;
131 unsigned current_desc_bit_index
= 24;
132 for (i
= first_level
+ 1; i
<= last_level
; i
++) {
133 uint32_t address
= base_va
+ lima_res
->levels
[i
].offset
;
134 address
= (address
>> 6);
135 desc
[current_desc_index
] |= (address
<< current_desc_bit_index
);
136 if (current_desc_bit_index
<= 6) {
137 current_desc_bit_index
+= 26;
138 if (current_desc_bit_index
>= 32) {
139 current_desc_bit_index
&= 0x1F;
140 current_desc_index
++;
144 desc
[current_desc_index
+ 1] |= (address
>> (32 - current_desc_bit_index
));
145 current_desc_bit_index
= (current_desc_bit_index
+ 26) & 0x1F;
146 current_desc_index
++;
151 lima_update_tex_desc(struct lima_context
*ctx
, struct lima_sampler_state
*sampler
,
152 struct lima_sampler_view
*texture
, void *pdesc
)
154 uint32_t *desc
= pdesc
;
155 unsigned first_level
;
159 memset(desc
, 0, lima_tex_desc_size
);
164 first_level
= texture
->base
.u
.tex
.first_level
;
165 last_level
= texture
->base
.u
.tex
.last_level
;
166 if (last_level
- first_level
>= LIMA_MAX_MIP_LEVELS
)
167 last_level
= first_level
+ LIMA_MAX_MIP_LEVELS
- 1;
169 switch (sampler
->base
.min_mip_filter
) {
170 case PIPE_TEX_MIPFILTER_LINEAR
:
172 case PIPE_TEX_MIPFILTER_NEAREST
:
174 desc
[1] |= ((last_level
- first_level
) << 24);
176 case PIPE_TEX_MIPFILTER_NONE
:
182 switch (sampler
->base
.mag_img_filter
) {
183 case PIPE_TEX_FILTER_LINEAR
:
185 /* no mipmap, filter_mag = linear */
187 desc
[1] |= 0x80000000;
189 case PIPE_TEX_FILTER_NEAREST
:
195 switch (sampler
->base
.min_img_filter
) {
197 case PIPE_TEX_FILTER_LINEAR
:
200 case PIPE_TEX_FILTER_NEAREST
:
206 /* Only clamp, clamp to edge, repeat and mirror repeat are supported */
208 switch (sampler
->base
.wrap_s
) {
209 case PIPE_TEX_WRAP_CLAMP
:
212 case PIPE_TEX_WRAP_CLAMP_TO_EDGE
:
213 case PIPE_TEX_WRAP_CLAMP_TO_BORDER
:
216 case PIPE_TEX_WRAP_MIRROR_REPEAT
:
219 case PIPE_TEX_WRAP_REPEAT
:
224 /* Only clamp, clamp to edge, repeat and mirror repeat are supported */
225 desc
[2] &= ~0x070000;
226 switch (sampler
->base
.wrap_t
) {
227 case PIPE_TEX_WRAP_CLAMP
:
230 case PIPE_TEX_WRAP_CLAMP_TO_EDGE
:
231 case PIPE_TEX_WRAP_CLAMP_TO_BORDER
:
234 case PIPE_TEX_WRAP_MIRROR_REPEAT
:
237 case PIPE_TEX_WRAP_REPEAT
:
242 lima_texture_desc_set_res(ctx
, desc
, texture
->base
.texture
,
243 first_level
, last_level
);
247 lima_update_textures(struct lima_context
*ctx
)
249 struct lima_texture_stateobj
*lima_tex
= &ctx
->tex_stateobj
;
251 assert (lima_tex
->num_samplers
<= 16);
253 /* Nothing to do - we have no samplers or textures */
254 if (!lima_tex
->num_samplers
|| !lima_tex
->num_textures
)
257 unsigned size
= lima_tex_list_size
+ lima_tex
->num_samplers
* lima_tex_desc_size
;
259 lima_ctx_buff_alloc(ctx
, lima_ctx_buff_pp_tex_desc
, size
, true);
261 for (int i
= 0; i
< lima_tex
->num_samplers
; i
++) {
262 off_t offset
= lima_tex_desc_size
* i
+ lima_tex_list_size
;
263 struct lima_sampler_state
*sampler
= lima_sampler_state(lima_tex
->samplers
[i
]);
264 struct lima_sampler_view
*texture
= lima_sampler_view(lima_tex
->textures
[i
]);
266 descs
[i
] = lima_ctx_buff_va(ctx
, lima_ctx_buff_pp_tex_desc
,
267 LIMA_CTX_BUFF_SUBMIT_PP
) + offset
;
268 lima_update_tex_desc(ctx
, sampler
, texture
, (void *)descs
+ offset
);
271 lima_dump_command_stream_print(
272 descs
, size
, false, "add textures_desc at va %x\n",
273 lima_ctx_buff_va(ctx
, lima_ctx_buff_pp_tex_desc
, 0));