lima: track write submits of context (v3)
[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 /*
67 * Note: this function is used by both draw and flush code path,
68 * make sure no lima_submit_get() is called inside this.
69 */
70 void
71 lima_texture_desc_set_res(struct lima_context *ctx, lima_tex_desc *desc,
72 struct pipe_resource *prsc,
73 unsigned first_level, unsigned last_level)
74 {
75 unsigned width, height, layout, i;
76 struct lima_resource *lima_res = lima_resource(prsc);
77
78 width = prsc->width0;
79 height = prsc->height0;
80 if (first_level != 0) {
81 width = u_minify(width, first_level);
82 height = u_minify(height, first_level);
83 }
84
85 desc->format = lima_format_get_texel(prsc->format);
86 desc->swap_r_b = lima_format_get_swap_rb(prsc->format);
87 desc->width = width;
88 desc->height = height;
89 desc->unknown_3_1 = 1;
90
91 if (lima_res->tiled)
92 layout = 3;
93 else {
94 /* for padded linear texture */
95 if (lima_res->levels[first_level].width != width) {
96 desc->stride = lima_res->levels[first_level].stride;
97 desc->has_stride = 1;
98 }
99 layout = 0;
100 }
101
102 uint32_t base_va = lima_res->bo->va;
103
104 /* attach first level */
105 uint32_t first_va = base_va + lima_res->levels[first_level].offset;
106 desc->va_s.va_0 = first_va >> 6;
107 desc->va_s.layout = layout;
108
109 /* Attach remaining levels.
110 * Each subsequent mipmap address is specified using the 26 msbs.
111 * These addresses are then packed continuously in memory */
112 for (i = 1; i <= (last_level - first_level); i++) {
113 uint32_t address = base_va + lima_res->levels[first_level + i].offset;
114 lima_texture_desc_set_va(desc, i, address);
115 }
116 }
117
118 static void
119 lima_update_tex_desc(struct lima_context *ctx, struct lima_sampler_state *sampler,
120 struct lima_sampler_view *texture, void *pdesc,
121 unsigned desc_size)
122 {
123 /* unit is 1/16 since lod_bias is in fixed format */
124 int lod_bias_delta = 0;
125 lima_tex_desc *desc = pdesc;
126 unsigned first_level;
127 unsigned last_level;
128 float max_lod;
129
130 memset(desc, 0, desc_size);
131
132 switch (texture->base.target) {
133 case PIPE_TEXTURE_2D:
134 case PIPE_TEXTURE_RECT:
135 desc->texture_type = LIMA_TEXTURE_TYPE_2D;
136 break;
137 case PIPE_TEXTURE_CUBE:
138 desc->texture_type = LIMA_TEXTURE_TYPE_CUBE;
139 break;
140 default:
141 break;
142 }
143
144 if (!sampler->base.normalized_coords)
145 desc->unnorm_coords = 1;
146
147 first_level = texture->base.u.tex.first_level;
148 last_level = texture->base.u.tex.last_level;
149 if (last_level - first_level >= LIMA_MAX_MIP_LEVELS)
150 last_level = first_level + LIMA_MAX_MIP_LEVELS - 1;
151
152 desc->min_lod = lima_float_to_fixed8(sampler->base.min_lod);
153 max_lod = MIN2(sampler->base.max_lod, sampler->base.min_lod +
154 (last_level - first_level));
155 desc->max_lod = lima_float_to_fixed8(max_lod);
156 desc->lod_bias = lima_float_to_fixed8(sampler->base.lod_bias);
157
158 switch (sampler->base.min_mip_filter) {
159 case PIPE_TEX_MIPFILTER_LINEAR:
160 desc->min_mipfilter_2 = 3;
161 break;
162 case PIPE_TEX_MIPFILTER_NEAREST:
163 desc->min_mipfilter_2 = 0;
164 break;
165 case PIPE_TEX_MIPFILTER_NONE:
166 desc->max_lod = desc->min_lod;
167 break;
168 default:
169 break;
170 }
171
172 switch (sampler->base.mag_img_filter) {
173 case PIPE_TEX_FILTER_LINEAR:
174 desc->mag_img_filter_nearest = 0;
175 break;
176 case PIPE_TEX_FILTER_NEAREST:
177 default:
178 desc->mag_img_filter_nearest = 1;
179 break;
180 }
181
182 switch (sampler->base.min_img_filter) {
183 break;
184 case PIPE_TEX_FILTER_LINEAR:
185 desc->min_img_filter_nearest = 0;
186 break;
187 case PIPE_TEX_FILTER_NEAREST:
188 default:
189 lod_bias_delta = 8;
190 desc->min_img_filter_nearest = 1;
191 break;
192 }
193
194 /* Only clamp, clamp to edge, repeat and mirror repeat are supported */
195 switch (sampler->base.wrap_s) {
196 case PIPE_TEX_WRAP_CLAMP:
197 desc->wrap_s_clamp = 1;
198 break;
199 case PIPE_TEX_WRAP_CLAMP_TO_EDGE:
200 case PIPE_TEX_WRAP_CLAMP_TO_BORDER:
201 desc->wrap_s_clamp_to_edge = 1;
202 break;
203 case PIPE_TEX_WRAP_MIRROR_REPEAT:
204 desc->wrap_s_mirror_repeat = 1;
205 break;
206 case PIPE_TEX_WRAP_REPEAT:
207 default:
208 break;
209 }
210
211 /* Only clamp, clamp to edge, repeat and mirror repeat are supported */
212 switch (sampler->base.wrap_t) {
213 case PIPE_TEX_WRAP_CLAMP:
214 desc->wrap_t_clamp = 1;
215 break;
216 case PIPE_TEX_WRAP_CLAMP_TO_EDGE:
217 case PIPE_TEX_WRAP_CLAMP_TO_BORDER:
218 desc->wrap_t_clamp_to_edge = 1;
219 break;
220 case PIPE_TEX_WRAP_MIRROR_REPEAT:
221 desc->wrap_t_mirror_repeat = 1;
222 break;
223 case PIPE_TEX_WRAP_REPEAT:
224 default:
225 break;
226 }
227
228 if (desc->min_img_filter_nearest && desc->mag_img_filter_nearest &&
229 desc->min_mipfilter_2 == 0 &&
230 (desc->min_lod != desc->max_lod))
231 lod_bias_delta = -1;
232
233 desc->lod_bias += lod_bias_delta;
234
235 lima_texture_desc_set_res(ctx, desc, texture->base.texture,
236 first_level, last_level);
237 }
238
239 static unsigned
240 lima_calc_tex_desc_size(struct lima_sampler_view *texture)
241 {
242 unsigned size = offsetof(lima_tex_desc, va);
243 unsigned va_bit_size;
244 unsigned first_level = texture->base.u.tex.first_level;
245 unsigned last_level = texture->base.u.tex.last_level;
246
247 if (last_level - first_level >= LIMA_MAX_MIP_LEVELS)
248 last_level = first_level + LIMA_MAX_MIP_LEVELS - 1;
249
250 va_bit_size = VA_BIT_OFFSET + VA_BIT_SIZE * (last_level - first_level + 1);
251 size += (va_bit_size + 7) >> 3;
252 size = align(size, lima_min_tex_desc_size);
253
254 return size;
255 }
256
257 void
258 lima_update_textures(struct lima_context *ctx)
259 {
260 struct lima_submit *submit = lima_submit_get(ctx);
261 struct lima_texture_stateobj *lima_tex = &ctx->tex_stateobj;
262
263 assert (lima_tex->num_samplers <= 16);
264
265 /* Nothing to do - we have no samplers or textures */
266 if (!lima_tex->num_samplers || !lima_tex->num_textures)
267 return;
268
269 /* we always need to add texture bo to submit */
270 for (int i = 0; i < lima_tex->num_samplers; i++) {
271 struct lima_sampler_view *texture = lima_sampler_view(lima_tex->textures[i]);
272 struct lima_resource *rsc = lima_resource(texture->base.texture);
273 lima_flush_previous_submit_writing_resource(ctx, texture->base.texture);
274 lima_submit_add_bo(submit, LIMA_PIPE_PP, rsc->bo, LIMA_SUBMIT_BO_READ);
275 }
276
277 /* do not regenerate texture desc if no change */
278 if (!(ctx->dirty & LIMA_CONTEXT_DIRTY_TEXTURES))
279 return;
280
281 unsigned size = lima_tex_list_size;
282 for (int i = 0; i < lima_tex->num_samplers; i++) {
283 struct lima_sampler_view *texture = lima_sampler_view(lima_tex->textures[i]);
284 size += lima_calc_tex_desc_size(texture);
285 }
286
287 uint32_t *descs =
288 lima_ctx_buff_alloc(ctx, lima_ctx_buff_pp_tex_desc, size);
289
290 off_t offset = lima_tex_list_size;
291 for (int i = 0; i < lima_tex->num_samplers; i++) {
292 struct lima_sampler_state *sampler = lima_sampler_state(lima_tex->samplers[i]);
293 struct lima_sampler_view *texture = lima_sampler_view(lima_tex->textures[i]);
294 unsigned desc_size = lima_calc_tex_desc_size(texture);
295
296 descs[i] = lima_ctx_buff_va(ctx, lima_ctx_buff_pp_tex_desc) + offset;
297 lima_update_tex_desc(ctx, sampler, texture, (void *)descs + offset, desc_size);
298 offset += desc_size;
299 }
300
301 lima_dump_command_stream_print(
302 descs, size, false, "add textures_desc at va %x\n",
303 lima_ctx_buff_va(ctx, lima_ctx_buff_pp_tex_desc));
304
305 lima_dump_texture_descriptor(
306 descs, size,
307 lima_ctx_buff_va(ctx, lima_ctx_buff_pp_tex_desc) + lima_tex_list_size,
308 lima_tex_list_size);
309 }