e094737344b8e6cb3a08a617ba02e45b0cde53da
[mesa.git] / src / gallium / drivers / etnaviv / etnaviv_resource.c
1 /*
2 * Copyright (c) 2012-2015 Etnaviv Project
3 *
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, sub license,
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:
10 *
11 * The above copyright notice and this permission notice (including the
12 * next paragraph) shall be included in all copies or substantial portions
13 * of the Software.
14 *
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 NON-INFRINGEMENT. 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
21 * DEALINGS IN THE SOFTWARE.
22 *
23 * Authors:
24 * Wladimir J. van der Laan <laanwj@gmail.com>
25 */
26
27 #include "etnaviv_resource.h"
28
29 #include "hw/common.xml.h"
30
31 #include "etnaviv_context.h"
32 #include "etnaviv_debug.h"
33 #include "etnaviv_screen.h"
34 #include "etnaviv_translate.h"
35
36 #include "util/u_inlines.h"
37 #include "util/u_memory.h"
38
39 /* A tile is 4x4 pixels, having 'screen->specs.bits_per_tile' of tile status.
40 * So, in a buffer of N pixels, there are N / (4 * 4) tiles.
41 * We need N * screen->specs.bits_per_tile / (4 * 4) bits of tile status, or
42 * N * screen->specs.bits_per_tile / (4 * 4 * 8) bytes.
43 */
44 bool
45 etna_screen_resource_alloc_ts(struct pipe_screen *pscreen,
46 struct etna_resource *rsc)
47 {
48 struct etna_screen *screen = etna_screen(pscreen);
49 size_t rt_ts_size, ts_layer_stride, pixels;
50
51 assert(!rsc->ts_bo);
52
53 /* TS only for level 0 -- XXX is this formula correct? */
54 pixels = rsc->levels[0].layer_stride / util_format_get_blocksize(rsc->base.format);
55 ts_layer_stride = align(pixels * screen->specs.bits_per_tile / 0x80,
56 0x100 * screen->specs.pixel_pipes);
57 rt_ts_size = ts_layer_stride * rsc->base.array_size;
58 if (rt_ts_size == 0)
59 return true;
60
61 DBG_F(ETNA_DBG_RESOURCE_MSGS, "%p: Allocating tile status of size %zu",
62 rsc, rt_ts_size);
63
64 struct etna_bo *rt_ts;
65 rt_ts = etna_bo_new(screen->dev, rt_ts_size, DRM_ETNA_GEM_CACHE_WC);
66
67 if (unlikely(!rt_ts)) {
68 BUG("Problem allocating tile status for resource");
69 return false;
70 }
71
72 rsc->ts_bo = rt_ts;
73 rsc->levels[0].ts_offset = 0;
74 rsc->levels[0].ts_layer_stride = ts_layer_stride;
75 rsc->levels[0].ts_size = rt_ts_size;
76
77 /* It is important to initialize the TS, as random pattern
78 * can result in crashes. Do this on the CPU as this only happens once
79 * per surface anyway and it's a small area, so it may not be worth
80 * queuing this to the GPU. */
81 void *ts_map = etna_bo_map(rt_ts);
82 memset(ts_map, screen->specs.ts_clear_value, rt_ts_size);
83
84 return true;
85 }
86
87 static boolean
88 etna_screen_can_create_resource(struct pipe_screen *pscreen,
89 const struct pipe_resource *templat)
90 {
91 struct etna_screen *screen = etna_screen(pscreen);
92 if (!translate_samples_to_xyscale(templat->nr_samples, NULL, NULL, NULL))
93 return false;
94
95 /* templat->bind is not set here, so we must use the minimum sizes */
96 uint max_size =
97 MIN2(screen->specs.max_rendertarget_size, screen->specs.max_texture_size);
98
99 if (templat->width0 > max_size || templat->height0 > max_size)
100 return false;
101
102 return true;
103 }
104
105 static unsigned
106 setup_miptree(struct etna_resource *rsc, unsigned paddingX, unsigned paddingY,
107 unsigned msaa_xscale, unsigned msaa_yscale)
108 {
109 struct pipe_resource *prsc = &rsc->base;
110 unsigned level, size = 0;
111 unsigned width = prsc->width0;
112 unsigned height = prsc->height0;
113 unsigned depth = prsc->depth0;
114
115 for (level = 0; level <= prsc->last_level; level++) {
116 struct etna_resource_level *mip = &rsc->levels[level];
117
118 mip->width = width;
119 mip->height = height;
120 mip->padded_width = align(width * msaa_xscale, paddingX);
121 mip->padded_height = align(height * msaa_yscale, paddingY);
122 mip->stride = util_format_get_stride(prsc->format, mip->padded_width);
123 mip->offset = size;
124 mip->layer_stride = mip->stride * util_format_get_nblocksy(prsc->format, mip->padded_height);
125 mip->size = prsc->array_size * mip->layer_stride;
126
127 /* align levels to 64 bytes to be able to render to them */
128 size += align(mip->size, ETNA_PE_ALIGNMENT) * depth;
129
130 width = u_minify(width, 1);
131 height = u_minify(height, 1);
132 depth = u_minify(depth, 1);
133 }
134
135 return size;
136 }
137
138 /* Create a new resource object, using the given template info */
139 struct pipe_resource *
140 etna_resource_alloc(struct pipe_screen *pscreen, unsigned layout,
141 const struct pipe_resource *templat)
142 {
143 struct etna_screen *screen = etna_screen(pscreen);
144 unsigned size;
145
146 DBG_F(ETNA_DBG_RESOURCE_MSGS,
147 "target=%d, format=%s, %ux%ux%u, array_size=%u, "
148 "last_level=%u, nr_samples=%u, usage=%u, bind=%x, flags=%x",
149 templat->target, util_format_name(templat->format), templat->width0,
150 templat->height0, templat->depth0, templat->array_size,
151 templat->last_level, templat->nr_samples, templat->usage,
152 templat->bind, templat->flags);
153
154 /* Determine scaling for antialiasing, allow override using debug flag */
155 int nr_samples = templat->nr_samples;
156 if ((templat->bind & (PIPE_BIND_RENDER_TARGET | PIPE_BIND_DEPTH_STENCIL)) &&
157 !(templat->bind & PIPE_BIND_SAMPLER_VIEW)) {
158 if (DBG_ENABLED(ETNA_DBG_MSAA_2X))
159 nr_samples = 2;
160 if (DBG_ENABLED(ETNA_DBG_MSAA_4X))
161 nr_samples = 4;
162 }
163
164 int msaa_xscale = 1, msaa_yscale = 1;
165 if (!translate_samples_to_xyscale(nr_samples, &msaa_xscale, &msaa_yscale, NULL)) {
166 /* Number of samples not supported */
167 return NULL;
168 }
169
170 /* If we have the TEXTURE_HALIGN feature, we can always align to the
171 * resolve engine's width. If not, we must not align resources used
172 * only for textures. */
173 bool rs_align = VIV_FEATURE(screen, chipMinorFeatures1, TEXTURE_HALIGN) ||
174 !etna_resource_sampler_only(templat);
175
176 /* Determine needed padding (alignment of height/width) */
177 unsigned paddingX = 0, paddingY = 0;
178 unsigned halign = TEXTURE_HALIGN_FOUR;
179 etna_layout_multiple(layout, screen->specs.pixel_pipes, rs_align, &paddingX,
180 &paddingY, &halign);
181 assert(paddingX && paddingY);
182
183 if (templat->target != PIPE_BUFFER)
184 etna_adjust_rs_align(screen->specs.pixel_pipes, NULL, &paddingY);
185
186 struct etna_resource *rsc = CALLOC_STRUCT(etna_resource);
187
188 if (!rsc)
189 return NULL;
190
191 rsc->base = *templat;
192 rsc->base.screen = pscreen;
193 rsc->base.nr_samples = nr_samples;
194 rsc->layout = layout;
195 rsc->halign = halign;
196
197 pipe_reference_init(&rsc->base.reference, 1);
198 list_inithead(&rsc->list);
199
200 size = setup_miptree(rsc, paddingX, paddingY, msaa_xscale, msaa_yscale);
201
202 uint32_t flags = DRM_ETNA_GEM_CACHE_WC;
203 if (templat->bind & PIPE_BIND_VERTEX_BUFFER)
204 flags |= DRM_ETNA_GEM_FORCE_MMU;
205 struct etna_bo *bo = etna_bo_new(screen->dev, size, flags);
206 if (unlikely(bo == NULL)) {
207 BUG("Problem allocating video memory for resource");
208 goto free_rsc;
209 }
210
211 rsc->bo = bo;
212 rsc->ts_bo = 0; /* TS is only created when first bound to surface */
213
214 if (templat->bind & PIPE_BIND_SCANOUT) {
215 struct pipe_resource scanout_templat = *templat;
216 struct winsys_handle handle;
217
218 /* pad scanout buffer size to be compatible with the RS */
219 etna_adjust_rs_align(screen->specs.pixel_pipes,
220 &scanout_templat.width0, &scanout_templat.height0);
221
222 rsc->scanout = renderonly_scanout_for_resource(&scanout_templat,
223 screen->ro, &handle);
224 if (!rsc->scanout)
225 goto free_rsc;
226
227 rsc->external = pscreen->resource_from_handle(pscreen, &rsc->base,
228 &handle,
229 PIPE_HANDLE_USAGE_WRITE);
230 close(handle.handle);
231 if (!rsc->external)
232 goto free_rsc;
233 }
234
235 if (DBG_ENABLED(ETNA_DBG_ZERO)) {
236 void *map = etna_bo_map(bo);
237 memset(map, 0, size);
238 }
239
240 return &rsc->base;
241
242 free_rsc:
243 FREE(rsc);
244 return NULL;
245 }
246
247 static struct pipe_resource *
248 etna_resource_create(struct pipe_screen *pscreen,
249 const struct pipe_resource *templat)
250 {
251 struct etna_screen *screen = etna_screen(pscreen);
252
253 /* Figure out what tiling to use -- for now, assume that texture cannot be linear.
254 * there is a capability LINEAR_TEXTURE_SUPPORT (supported on gc880 and
255 * gc2000 at least), but not sure how it works.
256 * Buffers always have LINEAR layout.
257 */
258 unsigned layout = ETNA_LAYOUT_LINEAR;
259 if (etna_resource_sampler_only(templat)) {
260 /* The buffer is only used for texturing, so create something
261 * directly compatible with the sampler. Such a buffer can
262 * never be rendered to. */
263 layout = ETNA_LAYOUT_TILED;
264
265 if (util_format_is_compressed(templat->format))
266 layout = ETNA_LAYOUT_LINEAR;
267 } else if (templat->target != PIPE_BUFFER) {
268 bool want_multitiled = false;
269 bool want_supertiled = screen->specs.can_supertile;
270
271 /* When this GPU supports single-buffer rendering, don't ever enable
272 * multi-tiling. This replicates the blob behavior on GC3000.
273 */
274 if (!screen->specs.single_buffer)
275 want_multitiled = screen->specs.pixel_pipes > 1;
276
277 /* Keep single byte blocksized resources as tiled, since we
278 * are unable to use the RS blit to de-tile them. However,
279 * if they're used as a render target or depth/stencil, they
280 * must be multi-tiled for GPUs with multiple pixel pipes.
281 * Ignore depth/stencil here, but it is an error for a render
282 * target.
283 */
284 if (util_format_get_blocksize(templat->format) == 1 &&
285 !(templat->bind & PIPE_BIND_DEPTH_STENCIL)) {
286 assert(!(templat->bind & PIPE_BIND_RENDER_TARGET && want_multitiled));
287 want_multitiled = want_supertiled = false;
288 }
289
290 layout = ETNA_LAYOUT_BIT_TILE;
291 if (want_multitiled)
292 layout |= ETNA_LAYOUT_BIT_MULTI;
293 if (want_supertiled)
294 layout |= ETNA_LAYOUT_BIT_SUPER;
295 }
296
297 if (templat->target == PIPE_TEXTURE_3D)
298 layout = ETNA_LAYOUT_LINEAR;
299
300 return etna_resource_alloc(pscreen, layout, templat);
301 }
302
303 static void
304 etna_resource_changed(struct pipe_screen *pscreen, struct pipe_resource *prsc)
305 {
306 struct etna_resource *res = etna_resource(prsc);
307
308 if (res->external)
309 etna_resource(res->external)->seqno++;
310 else
311 res->seqno++;
312 }
313
314 static void
315 etna_resource_destroy(struct pipe_screen *pscreen, struct pipe_resource *prsc)
316 {
317 struct etna_resource *rsc = etna_resource(prsc);
318
319 if (rsc->bo)
320 etna_bo_del(rsc->bo);
321
322 if (rsc->ts_bo)
323 etna_bo_del(rsc->ts_bo);
324
325 if (rsc->scanout)
326 renderonly_scanout_destroy(rsc->scanout, etna_screen(pscreen)->ro);
327
328 list_delinit(&rsc->list);
329
330 pipe_resource_reference(&rsc->texture, NULL);
331 pipe_resource_reference(&rsc->external, NULL);
332
333 FREE(rsc);
334 }
335
336 static struct pipe_resource *
337 etna_resource_from_handle(struct pipe_screen *pscreen,
338 const struct pipe_resource *tmpl,
339 struct winsys_handle *handle, unsigned usage)
340 {
341 struct etna_screen *screen = etna_screen(pscreen);
342 struct etna_resource *rsc;
343 struct etna_resource_level *level;
344 struct pipe_resource *prsc;
345 struct pipe_resource *ptiled = NULL;
346
347 DBG("target=%d, format=%s, %ux%ux%u, array_size=%u, last_level=%u, "
348 "nr_samples=%u, usage=%u, bind=%x, flags=%x",
349 tmpl->target, util_format_name(tmpl->format), tmpl->width0,
350 tmpl->height0, tmpl->depth0, tmpl->array_size, tmpl->last_level,
351 tmpl->nr_samples, tmpl->usage, tmpl->bind, tmpl->flags);
352
353 rsc = CALLOC_STRUCT(etna_resource);
354 if (!rsc)
355 return NULL;
356
357 level = &rsc->levels[0];
358 prsc = &rsc->base;
359
360 *prsc = *tmpl;
361
362 pipe_reference_init(&prsc->reference, 1);
363 list_inithead(&rsc->list);
364 prsc->screen = pscreen;
365
366 rsc->bo = etna_screen_bo_from_handle(pscreen, handle, &level->stride);
367 if (!rsc->bo)
368 goto fail;
369
370 rsc->seqno = 1;
371
372 level->width = tmpl->width0;
373 level->height = tmpl->height0;
374
375 /* We will be using the RS to copy with this resource, so we must
376 * ensure that it is appropriately aligned for the RS requirements. */
377 level->padded_width = level->width;
378 level->padded_height = level->height;
379 etna_adjust_rs_align(&level->padded_width, &level->padded_height);
380
381 level->layer_stride = level->stride * util_format_get_nblocksy(prsc->format,
382 level->padded_height);
383
384 /* The DDX must give us a BO which conforms to our padding size.
385 * The stride of the BO must be greater or equal to our padded
386 * stride. The size of the BO must accomodate the padded height. */
387 if (level->stride < util_format_get_stride(tmpl->format, level->padded_width)) {
388 BUG("BO stride is too small for RS engine width padding");
389 goto fail;
390 }
391 if (etna_bo_size(rsc->bo) < level->stride * level->padded_height) {
392 BUG("BO size is too small for RS engine height padding");
393 goto fail;
394 }
395
396 if (handle->type == DRM_API_HANDLE_TYPE_SHARED && tmpl->bind & PIPE_BIND_RENDER_TARGET) {
397 /* Render targets are linear in Xorg but must be tiled
398 * here. It would be nice if dri_drawable_get_format()
399 * set scanout for these buffers too. */
400
401 ptiled = etna_resource_create(pscreen, tmpl);
402 if (!ptiled)
403 goto fail;
404
405 etna_resource(ptiled)->external = prsc;
406
407 return ptiled;
408 }
409
410 return prsc;
411
412 fail:
413 etna_resource_destroy(pscreen, prsc);
414 if (ptiled)
415 etna_resource_destroy(pscreen, ptiled);
416
417 return NULL;
418 }
419
420 static boolean
421 etna_resource_get_handle(struct pipe_screen *pscreen,
422 struct pipe_context *pctx,
423 struct pipe_resource *prsc,
424 struct winsys_handle *handle, unsigned usage)
425 {
426 struct etna_resource *rsc = etna_resource(prsc);
427 /* Scanout is always attached to the base resource */
428 struct renderonly_scanout *scanout = rsc->scanout;
429
430 /*
431 * External resources are preferred, so a import->export chain of
432 * render/sampler incompatible buffers yield the same handle.
433 */
434 if (rsc->external)
435 rsc = etna_resource(rsc->external);
436
437 if (handle->type == DRM_API_HANDLE_TYPE_KMS &&
438 renderonly_get_handle(scanout, handle))
439 return TRUE;
440
441 return etna_screen_bo_get_handle(pscreen, rsc->bo, rsc->levels[0].stride,
442 handle);
443 }
444
445 void
446 etna_resource_used(struct etna_context *ctx, struct pipe_resource *prsc,
447 enum etna_resource_status status)
448 {
449 struct etna_resource *rsc;
450
451 if (!prsc)
452 return;
453
454 rsc = etna_resource(prsc);
455 rsc->status |= status;
456
457 /* TODO resources can actually be shared across contexts,
458 * so I'm not sure a single list-head will do the trick? */
459 debug_assert((rsc->pending_ctx == ctx) || !rsc->pending_ctx);
460 list_delinit(&rsc->list);
461 list_addtail(&rsc->list, &ctx->used_resources);
462 rsc->pending_ctx = ctx;
463 }
464
465 void
466 etna_resource_screen_init(struct pipe_screen *pscreen)
467 {
468 pscreen->can_create_resource = etna_screen_can_create_resource;
469 pscreen->resource_create = etna_resource_create;
470 pscreen->resource_from_handle = etna_resource_from_handle;
471 pscreen->resource_get_handle = etna_resource_get_handle;
472 pscreen->resource_changed = etna_resource_changed;
473 pscreen->resource_destroy = etna_resource_destroy;
474 }