lima: remove lima_ctx_buff_va submit flags (v2)
[mesa.git] / src / gallium / drivers / lima / lima_texture.c
1 /*
2 * Copyright (c) 2011-2013 Luc Verhaegen <libv@skynet.be>
3 * Copyright (c) 2018-2019 Lima Project
4 *
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:
11 *
12 * The above copyright notice and this permission notice (including the
13 * next paragraph) shall be included in all copies or substantial portions
14 * of the Software.
15 *
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.
23 *
24 */
25
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"
31
32 #include "lima_bo.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"
40
41 #include <drm-uapi/lima_drm.h>
42
43
44 #define lima_tex_list_size 64
45
46 static_assert(offsetof(lima_tex_desc, va) == 24, "lima_tex_desc->va offset isn't 24");
47
48
49 static void
50 lima_texture_desc_set_va(lima_tex_desc *desc,
51 int idx,
52 uint32_t va)
53 {
54 unsigned va_bit_idx = VA_BIT_OFFSET + (VA_BIT_SIZE * idx);
55 unsigned va_idx = va_bit_idx / 32;
56 va_bit_idx %= 32;
57
58 va >>= 6;
59
60 desc->va[va_idx] |= va << va_bit_idx;
61 if (va_bit_idx <= 6)
62 return;
63 desc->va[va_idx + 1] |= va >> (32 - va_bit_idx);
64 }
65
66 void
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)
70 {
71 unsigned width, height, layout, i;
72 struct lima_resource *lima_res = lima_resource(prsc);
73
74 width = prsc->width0;
75 height = prsc->height0;
76 if (first_level != 0) {
77 width = u_minify(width, first_level);
78 height = u_minify(height, first_level);
79 }
80
81 desc->format = lima_format_get_texel(prsc->format);
82 desc->swap_r_b = lima_format_get_swap_rb(prsc->format);
83 desc->width = width;
84 desc->height = height;
85 desc->unknown_3_1 = 1;
86
87 if (lima_res->tiled)
88 layout = 3;
89 else {
90 /* for padded linear texture */
91 if (lima_res->levels[first_level].width != width) {
92 desc->stride = lima_res->levels[first_level].stride;
93 desc->has_stride = 1;
94 }
95 layout = 0;
96 }
97
98 uint32_t base_va = lima_res->bo->va;
99
100 /* attach first level */
101 uint32_t first_va = base_va + lima_res->levels[first_level].offset;
102 desc->va_s.va_0 = first_va >> 6;
103 desc->va_s.layout = layout;
104
105 /* Attach remaining levels.
106 * Each subsequent mipmap address is specified using the 26 msbs.
107 * These addresses are then packed continuously in memory */
108 for (i = 1; i <= (last_level - first_level); i++) {
109 uint32_t address = base_va + lima_res->levels[first_level + i].offset;
110 lima_texture_desc_set_va(desc, i, address);
111 }
112 }
113
114 static void
115 lima_update_tex_desc(struct lima_context *ctx, struct lima_sampler_state *sampler,
116 struct lima_sampler_view *texture, void *pdesc,
117 unsigned desc_size)
118 {
119 /* unit is 1/16 since lod_bias is in fixed format */
120 int lod_bias_delta = 0;
121 lima_tex_desc *desc = pdesc;
122 unsigned first_level;
123 unsigned last_level;
124 float max_lod;
125
126 memset(desc, 0, desc_size);
127
128 switch (texture->base.target) {
129 case PIPE_TEXTURE_2D:
130 case PIPE_TEXTURE_RECT:
131 desc->texture_type = LIMA_TEXTURE_TYPE_2D;
132 break;
133 case PIPE_TEXTURE_CUBE:
134 desc->texture_type = LIMA_TEXTURE_TYPE_CUBE;
135 break;
136 default:
137 break;
138 }
139
140 if (!sampler->base.normalized_coords)
141 desc->unnorm_coords = 1;
142
143 first_level = texture->base.u.tex.first_level;
144 last_level = texture->base.u.tex.last_level;
145 if (last_level - first_level >= LIMA_MAX_MIP_LEVELS)
146 last_level = first_level + LIMA_MAX_MIP_LEVELS - 1;
147
148 desc->min_lod = lima_float_to_fixed8(sampler->base.min_lod);
149 max_lod = MIN2(sampler->base.max_lod, sampler->base.min_lod +
150 (last_level - first_level));
151 desc->max_lod = lima_float_to_fixed8(max_lod);
152 desc->lod_bias = lima_float_to_fixed8(sampler->base.lod_bias);
153
154 switch (sampler->base.min_mip_filter) {
155 case PIPE_TEX_MIPFILTER_LINEAR:
156 desc->min_mipfilter_2 = 3;
157 break;
158 case PIPE_TEX_MIPFILTER_NEAREST:
159 desc->min_mipfilter_2 = 0;
160 break;
161 case PIPE_TEX_MIPFILTER_NONE:
162 desc->max_lod = desc->min_lod;
163 break;
164 default:
165 break;
166 }
167
168 switch (sampler->base.mag_img_filter) {
169 case PIPE_TEX_FILTER_LINEAR:
170 desc->mag_img_filter_nearest = 0;
171 break;
172 case PIPE_TEX_FILTER_NEAREST:
173 default:
174 desc->mag_img_filter_nearest = 1;
175 break;
176 }
177
178 switch (sampler->base.min_img_filter) {
179 break;
180 case PIPE_TEX_FILTER_LINEAR:
181 desc->min_img_filter_nearest = 0;
182 break;
183 case PIPE_TEX_FILTER_NEAREST:
184 default:
185 lod_bias_delta = 8;
186 desc->min_img_filter_nearest = 1;
187 break;
188 }
189
190 /* Only clamp, clamp to edge, repeat and mirror repeat are supported */
191 switch (sampler->base.wrap_s) {
192 case PIPE_TEX_WRAP_CLAMP:
193 desc->wrap_s_clamp = 1;
194 break;
195 case PIPE_TEX_WRAP_CLAMP_TO_EDGE:
196 case PIPE_TEX_WRAP_CLAMP_TO_BORDER:
197 desc->wrap_s_clamp_to_edge = 1;
198 break;
199 case PIPE_TEX_WRAP_MIRROR_REPEAT:
200 desc->wrap_s_mirror_repeat = 1;
201 break;
202 case PIPE_TEX_WRAP_REPEAT:
203 default:
204 break;
205 }
206
207 /* Only clamp, clamp to edge, repeat and mirror repeat are supported */
208 switch (sampler->base.wrap_t) {
209 case PIPE_TEX_WRAP_CLAMP:
210 desc->wrap_t_clamp = 1;
211 break;
212 case PIPE_TEX_WRAP_CLAMP_TO_EDGE:
213 case PIPE_TEX_WRAP_CLAMP_TO_BORDER:
214 desc->wrap_t_clamp_to_edge = 1;
215 break;
216 case PIPE_TEX_WRAP_MIRROR_REPEAT:
217 desc->wrap_t_mirror_repeat = 1;
218 break;
219 case PIPE_TEX_WRAP_REPEAT:
220 default:
221 break;
222 }
223
224 if (desc->min_img_filter_nearest && desc->mag_img_filter_nearest &&
225 desc->min_mipfilter_2 == 0 &&
226 (desc->min_lod != desc->max_lod))
227 lod_bias_delta = -1;
228
229 desc->lod_bias += lod_bias_delta;
230
231 lima_texture_desc_set_res(ctx, desc, texture->base.texture,
232 first_level, last_level);
233 }
234
235 static unsigned
236 lima_calc_tex_desc_size(struct lima_sampler_view *texture)
237 {
238 unsigned size = offsetof(lima_tex_desc, va);
239 unsigned va_bit_size;
240 unsigned first_level = texture->base.u.tex.first_level;
241 unsigned last_level = texture->base.u.tex.last_level;
242
243 if (last_level - first_level >= LIMA_MAX_MIP_LEVELS)
244 last_level = first_level + LIMA_MAX_MIP_LEVELS - 1;
245
246 va_bit_size = VA_BIT_OFFSET + VA_BIT_SIZE * (last_level - first_level + 1);
247 size += (va_bit_size + 7) >> 3;
248 size = align(size, lima_min_tex_desc_size);
249
250 return size;
251 }
252
253 void
254 lima_update_textures(struct lima_context *ctx)
255 {
256 struct lima_texture_stateobj *lima_tex = &ctx->tex_stateobj;
257
258 assert (lima_tex->num_samplers <= 16);
259
260 /* Nothing to do - we have no samplers or textures */
261 if (!lima_tex->num_samplers || !lima_tex->num_textures)
262 return;
263
264 /* we always need to add texture bo to submit */
265 for (int i = 0; i < lima_tex->num_samplers; i++) {
266 struct lima_sampler_view *texture = lima_sampler_view(lima_tex->textures[i]);
267 struct lima_resource *rsc = lima_resource(texture->base.texture);
268 lima_submit_add_bo(ctx->pp_submit, rsc->bo, LIMA_SUBMIT_BO_READ);
269 }
270
271 /* do not regenerate texture desc if no change */
272 if (!(ctx->dirty & LIMA_CONTEXT_DIRTY_TEXTURES))
273 return;
274
275 unsigned size = lima_tex_list_size;
276 for (int i = 0; i < lima_tex->num_samplers; i++) {
277 struct lima_sampler_view *texture = lima_sampler_view(lima_tex->textures[i]);
278 size += lima_calc_tex_desc_size(texture);
279 }
280
281 uint32_t *descs =
282 lima_ctx_buff_alloc(ctx, lima_ctx_buff_pp_tex_desc, size);
283
284 off_t offset = lima_tex_list_size;
285 for (int i = 0; i < lima_tex->num_samplers; i++) {
286 struct lima_sampler_state *sampler = lima_sampler_state(lima_tex->samplers[i]);
287 struct lima_sampler_view *texture = lima_sampler_view(lima_tex->textures[i]);
288 unsigned desc_size = lima_calc_tex_desc_size(texture);
289
290 descs[i] = lima_ctx_buff_va(ctx, lima_ctx_buff_pp_tex_desc) + offset;
291 lima_update_tex_desc(ctx, sampler, texture, (void *)descs + offset, desc_size);
292 offset += desc_size;
293 }
294
295 lima_dump_command_stream_print(
296 descs, size, false, "add textures_desc at va %x\n",
297 lima_ctx_buff_va(ctx, lima_ctx_buff_pp_tex_desc));
298
299 lima_dump_texture_descriptor(
300 descs, size,
301 lima_ctx_buff_va(ctx, lima_ctx_buff_pp_tex_desc) + lima_tex_list_size,
302 lima_tex_list_size);
303 }