panfrost: List primitive restart enable bit
[mesa.git] / src / gallium / drivers / panfrost / pan_resource.c
1 /**************************************************************************
2 *
3 * Copyright 2008 VMware, Inc.
4 * Copyright 2014 Broadcom
5 * Copyright 2018 Alyssa Rosenzweig
6 * All Rights Reserved.
7 *
8 * Permission is hereby granted, free of charge, to any person obtaining a
9 * copy of this software and associated documentation files (the
10 * "Software"), to deal in the Software without restriction, including
11 * without limitation the rights to use, copy, modify, merge, publish,
12 * distribute, sub license, and/or sell copies of the Software, and to
13 * permit persons to whom the Software is furnished to do so, subject to
14 * the following conditions:
15 *
16 * The above copyright notice and this permission notice (including the
17 * next paragraph) shall be included in all copies or substantial portions
18 * of the Software.
19 *
20 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
21 * OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
22 * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NON-INFRINGEMENT.
23 * IN NO EVENT SHALL VMWARE AND/OR ITS SUPPLIERS BE LIABLE FOR
24 * ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
25 * TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
26 * SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
27 *
28 **************************************************************************/
29
30 #include <xf86drm.h>
31 #include <fcntl.h>
32 #include "drm-uapi/drm_fourcc.h"
33
34 #include "state_tracker/winsys_handle.h"
35 #include "util/u_format.h"
36 #include "util/u_memory.h"
37 #include "util/u_surface.h"
38 #include "util/u_transfer.h"
39 #include "util/u_transfer_helper.h"
40
41 #include "pan_context.h"
42 #include "pan_screen.h"
43 #include "pan_resource.h"
44 #include "pan_swizzle.h"
45
46 static struct pipe_resource *
47 panfrost_resource_from_handle(struct pipe_screen *pscreen,
48 const struct pipe_resource *templat,
49 struct winsys_handle *whandle,
50 unsigned usage)
51 {
52 struct panfrost_screen *screen = pan_screen(pscreen);
53 struct panfrost_resource *rsc;
54 struct pipe_resource *prsc;
55
56 assert(whandle->type == WINSYS_HANDLE_TYPE_FD);
57
58 rsc = CALLOC_STRUCT(panfrost_resource);
59 if (!rsc)
60 return NULL;
61
62 prsc = &rsc->base;
63
64 *prsc = *templat;
65
66 pipe_reference_init(&prsc->reference, 1);
67 prsc->screen = pscreen;
68
69 rsc->bo = screen->driver->import_bo(screen, whandle);
70
71 return prsc;
72 }
73
74 static boolean
75 panfrost_resource_get_handle(struct pipe_screen *pscreen,
76 struct pipe_context *ctx,
77 struct pipe_resource *pt,
78 struct winsys_handle *handle,
79 unsigned usage)
80 {
81 struct panfrost_screen *screen = pan_screen(pscreen);
82 struct panfrost_resource *rsrc = (struct panfrost_resource *) pt;
83 struct renderonly_scanout *scanout = rsrc->scanout;
84 int bytes_per_pixel = util_format_get_blocksize(rsrc->base.format);
85 int stride = bytes_per_pixel * rsrc->base.width0; /* TODO: Alignment? */
86
87 handle->stride = stride;
88 handle->modifier = DRM_FORMAT_MOD_INVALID;
89
90 if (handle->type == WINSYS_HANDLE_TYPE_SHARED) {
91 printf("Missed shared handle\n");
92 return FALSE;
93 } else if (handle->type == WINSYS_HANDLE_TYPE_KMS) {
94 if (renderonly_get_handle(scanout, handle)) {
95 return TRUE;
96 } else {
97 printf("Missed nonrenderonly KMS handle for resource %p with scanout %p\n", pt, scanout);
98 return FALSE;
99 }
100 } else if (handle->type == WINSYS_HANDLE_TYPE_FD) {
101 if (scanout) {
102 struct drm_prime_handle args = {
103 .handle = scanout->handle,
104 .flags = DRM_CLOEXEC,
105 };
106
107 int ret = drmIoctl(screen->ro->kms_fd, DRM_IOCTL_PRIME_HANDLE_TO_FD, &args);
108 if (ret == -1)
109 return FALSE;
110
111 handle->handle = args.fd;
112
113 return TRUE;
114 } else {
115 printf("Missed nonscanout FD handle\n");
116 assert(0);
117 return FALSE;
118 }
119 }
120
121 return FALSE;
122 }
123
124 static void
125 panfrost_flush_resource(struct pipe_context *pctx, struct pipe_resource *prsc)
126 {
127 //fprintf(stderr, "TODO %s\n", __func__);
128 }
129
130 static void
131 panfrost_blit(struct pipe_context *pipe,
132 const struct pipe_blit_info *info)
133 {
134 /* STUB */
135 printf("Skipping blit XXX\n");
136 return;
137 }
138
139 static struct pipe_surface *
140 panfrost_create_surface(struct pipe_context *pipe,
141 struct pipe_resource *pt,
142 const struct pipe_surface *surf_tmpl)
143 {
144 struct pipe_surface *ps = NULL;
145
146 ps = CALLOC_STRUCT(pipe_surface);
147
148 if (ps) {
149 pipe_reference_init(&ps->reference, 1);
150 pipe_resource_reference(&ps->texture, pt);
151 ps->context = pipe;
152 ps->format = surf_tmpl->format;
153
154 if (pt->target != PIPE_BUFFER) {
155 assert(surf_tmpl->u.tex.level <= pt->last_level);
156 ps->width = u_minify(pt->width0, surf_tmpl->u.tex.level);
157 ps->height = u_minify(pt->height0, surf_tmpl->u.tex.level);
158 ps->u.tex.level = surf_tmpl->u.tex.level;
159 ps->u.tex.first_layer = surf_tmpl->u.tex.first_layer;
160 ps->u.tex.last_layer = surf_tmpl->u.tex.last_layer;
161 } else {
162 /* setting width as number of elements should get us correct renderbuffer width */
163 ps->width = surf_tmpl->u.buf.last_element - surf_tmpl->u.buf.first_element + 1;
164 ps->height = pt->height0;
165 ps->u.buf.first_element = surf_tmpl->u.buf.first_element;
166 ps->u.buf.last_element = surf_tmpl->u.buf.last_element;
167 assert(ps->u.buf.first_element <= ps->u.buf.last_element);
168 assert(ps->u.buf.last_element < ps->width);
169 }
170 }
171
172 return ps;
173 }
174
175 static void
176 panfrost_surface_destroy(struct pipe_context *pipe,
177 struct pipe_surface *surf)
178 {
179 assert(surf->texture);
180 pipe_resource_reference(&surf->texture, NULL);
181 free(surf);
182 }
183
184 static struct panfrost_bo *
185 panfrost_create_bo(struct panfrost_screen *screen, const struct pipe_resource *template)
186 {
187 struct panfrost_bo *bo = CALLOC_STRUCT(panfrost_bo);
188
189 /* Calculate the size of the bo */
190
191 int bytes_per_pixel = util_format_get_blocksize(template->format);
192 int stride = bytes_per_pixel * template->width0; /* TODO: Alignment? */
193 size_t sz = stride;
194
195 if (template->height0) sz *= template->height0;
196 if (template->depth0) sz *= template->depth0;
197
198 /* Tiling textures is almost always faster, unless we only use it once */
199 bo->tiled = (template->usage != PIPE_USAGE_STREAM) && (template->bind & PIPE_BIND_SAMPLER_VIEW);
200
201 if (bo->tiled) {
202 /* For tiled, we don't map directly, so just malloc any old buffer */
203
204 for (int l = 0; l < (template->last_level + 1); ++l) {
205 bo->cpu[l] = malloc(sz);
206 sz >>= 2;
207 }
208 } else {
209 /* But for linear, we can! */
210
211 struct pb_slab_entry *entry = pb_slab_alloc(&screen->slabs, sz, HEAP_TEXTURE);
212 struct panfrost_memory_entry *p_entry = (struct panfrost_memory_entry *) entry;
213 struct panfrost_memory *backing = (struct panfrost_memory *) entry->slab;
214 bo->entry[0] = p_entry;
215 bo->cpu[0] = backing->cpu + p_entry->offset;
216 bo->gpu[0] = backing->gpu + p_entry->offset;
217
218 /* TODO: Mipmap */
219 }
220
221 return bo;
222 }
223
224 static struct pipe_resource *
225 panfrost_resource_create(struct pipe_screen *screen,
226 const struct pipe_resource *template)
227 {
228 struct panfrost_resource *so = CALLOC_STRUCT(panfrost_resource);
229 struct panfrost_screen *pscreen = (struct panfrost_screen *) screen;
230
231 so->base = *template;
232 so->base.screen = screen;
233
234 pipe_reference_init(&so->base.reference, 1);
235
236 /* Make sure we're familiar */
237 switch (template->target) {
238 case PIPE_BUFFER:
239 case PIPE_TEXTURE_1D:
240 case PIPE_TEXTURE_2D:
241 case PIPE_TEXTURE_3D:
242 case PIPE_TEXTURE_RECT:
243 break;
244 default:
245 fprintf(stderr, "Unknown texture target %d\n", template->target);
246 assert(0);
247 }
248
249 if ((template->bind & PIPE_BIND_RENDER_TARGET) || (template->bind & PIPE_BIND_DEPTH_STENCIL)) {
250 if (template->bind & PIPE_BIND_DISPLAY_TARGET ||
251 template->bind & PIPE_BIND_SCANOUT ||
252 template->bind & PIPE_BIND_SHARED) {
253 struct pipe_resource scanout_templat = *template;
254 struct renderonly_scanout *scanout;
255 struct winsys_handle handle;
256
257 /* TODO: align width0 and height0? */
258
259 scanout = renderonly_scanout_for_resource(&scanout_templat,
260 pscreen->ro, &handle);
261 if (!scanout)
262 return NULL;
263
264 assert(handle.type == WINSYS_HANDLE_TYPE_FD);
265 /* TODO: handle modifiers? */
266 so = pan_resource(screen->resource_from_handle(screen, template,
267 &handle,
268 PIPE_HANDLE_USAGE_FRAMEBUFFER_WRITE));
269 close(handle.handle);
270 if (!so)
271 return NULL;
272
273 so->scanout = scanout;
274 pscreen->display_target = so;
275 } else {
276 so->bo = panfrost_create_bo(pscreen, template);
277 }
278 } else {
279 so->bo = panfrost_create_bo(pscreen, template);
280 }
281
282 return (struct pipe_resource *)so;
283 }
284
285 static void
286 panfrost_destroy_bo(struct panfrost_screen *screen, struct panfrost_bo *pbo)
287 {
288 struct panfrost_bo *bo = (struct panfrost_bo *)pbo;
289
290 for (int l = 0; l < MAX_MIP_LEVELS; ++l) {
291 if (bo->entry[l] != NULL) {
292 /* Most allocations have an entry to free */
293 bo->entry[l]->freed = true;
294 pb_slab_free(&screen->slabs, &bo->entry[l]->base);
295 }
296 }
297
298 if (bo->tiled) {
299 /* Tiled has a malloc'd CPU, so just plain ol' free needed */
300
301 for (int l = 0; l < MAX_MIP_LEVELS; ++l) {
302 free(bo->cpu[l]);
303 }
304 }
305
306 if (bo->has_afbc) {
307 /* TODO */
308 printf("--leaking afbc (%d bytes)--\n", bo->afbc_metadata_size);
309 }
310
311 if (bo->has_checksum) {
312 /* TODO */
313 printf("--leaking checksum (%zd bytes)--\n", bo->checksum_slab.size);
314 }
315
316 if (bo->imported) {
317 screen->driver->free_imported_bo(screen, bo);
318 }
319 }
320
321 static void
322 panfrost_resource_destroy(struct pipe_screen *screen,
323 struct pipe_resource *pt)
324 {
325 struct panfrost_screen *pscreen = panfrost_screen(screen);
326 struct panfrost_resource *rsrc = (struct panfrost_resource *) pt;
327
328 if (rsrc->scanout)
329 renderonly_scanout_destroy(rsrc->scanout, pscreen->ro);
330
331 if (rsrc->bo)
332 panfrost_destroy_bo(pscreen, rsrc->bo);
333
334 FREE(rsrc);
335 }
336
337 static uint8_t *
338 panfrost_map_bo(struct panfrost_context *ctx, struct pipe_transfer *transfer)
339 {
340 struct panfrost_bo *bo = (struct panfrost_bo *)pan_resource(transfer->resource)->bo;
341
342 /* If non-zero level, it's a mipmapped resource and needs to be treated as such */
343 bo->is_mipmap |= transfer->level;
344
345 if (transfer->usage & PIPE_TRANSFER_MAP_DIRECTLY && bo->tiled) {
346 /* We cannot directly map tiled textures */
347 return NULL;
348 }
349
350 if (transfer->resource->bind & PIPE_BIND_DEPTH_STENCIL) {
351 /* Mipmapped readpixels?! */
352 assert(transfer->level == 0);
353
354 /* Set the CPU mapping to that of the depth/stencil buffer in memory, untiled */
355 bo->cpu[transfer->level] = ctx->depth_stencil_buffer.cpu;
356 }
357
358 return bo->cpu[transfer->level];
359 }
360
361 static void *
362 panfrost_transfer_map(struct pipe_context *pctx,
363 struct pipe_resource *resource,
364 unsigned level,
365 unsigned usage, /* a combination of PIPE_TRANSFER_x */
366 const struct pipe_box *box,
367 struct pipe_transfer **out_transfer)
368 {
369 struct panfrost_context *ctx = pan_context(pctx);
370 int bytes_per_pixel = util_format_get_blocksize(resource->format);
371 int stride = bytes_per_pixel * resource->width0; /* TODO: Alignment? */
372 uint8_t *cpu;
373
374 struct pipe_transfer *transfer = CALLOC_STRUCT(pipe_transfer);
375 transfer->level = level;
376 transfer->usage = usage;
377 transfer->box = *box;
378 transfer->stride = stride;
379 assert(!transfer->box.z);
380
381 pipe_resource_reference(&transfer->resource, resource);
382
383 *out_transfer = transfer;
384
385 if (resource->bind & PIPE_BIND_DISPLAY_TARGET ||
386 resource->bind & PIPE_BIND_SCANOUT ||
387 resource->bind & PIPE_BIND_SHARED) {
388 /* Mipmapped readpixels?! */
389 assert(level == 0);
390
391 /* Force a flush -- kill the pipeline */
392 panfrost_flush(pctx, NULL, PIPE_FLUSH_END_OF_FRAME);
393 }
394
395 cpu = panfrost_map_bo(ctx, transfer);
396 if (cpu == NULL)
397 return NULL;
398
399 return cpu + transfer->box.x * bytes_per_pixel + transfer->box.y * stride;
400 }
401
402 static void
403 panfrost_tile_texture(struct panfrost_screen *screen, struct panfrost_resource *rsrc, int level)
404 {
405 struct panfrost_bo *bo = (struct panfrost_bo *)rsrc->bo;
406 int bytes_per_pixel = util_format_get_blocksize(rsrc->base.format);
407 int stride = bytes_per_pixel * rsrc->base.width0; /* TODO: Alignment? */
408
409 int width = rsrc->base.width0 >> level;
410 int height = rsrc->base.height0 >> level;
411
412 /* Estimate swizzled bitmap size. Slight overestimates are fine.
413 * Underestimates will result in memory corruption or worse. */
414
415 int swizzled_sz = panfrost_swizzled_size(width, height, bytes_per_pixel);
416
417 /* Save the entry. But if there was already an entry here (from a
418 * previous upload of the resource), free that one so we don't leak */
419
420 if (bo->entry[level] != NULL) {
421 bo->entry[level]->freed = true;
422 pb_slab_free(&screen->slabs, &bo->entry[level]->base);
423 }
424
425 /* Allocate the transfer given that known size but do not copy */
426 struct pb_slab_entry *entry = pb_slab_alloc(&screen->slabs, swizzled_sz, HEAP_TEXTURE);
427 struct panfrost_memory_entry *p_entry = (struct panfrost_memory_entry *) entry;
428 struct panfrost_memory *backing = (struct panfrost_memory *) entry->slab;
429 uint8_t *swizzled = backing->cpu + p_entry->offset;
430
431 bo->entry[level] = p_entry;
432 bo->gpu[level] = backing->gpu + p_entry->offset;
433
434 /* Run actual texture swizzle, writing directly to the mapped
435 * GPU chunk we allocated */
436
437 panfrost_texture_swizzle(width, height, bytes_per_pixel, stride, bo->cpu[level], swizzled);
438 }
439
440 static void
441 panfrost_unmap_bo(struct panfrost_context *ctx,
442 struct pipe_transfer *transfer)
443 {
444 struct panfrost_bo *bo = (struct panfrost_bo *)pan_resource(transfer->resource)->bo;
445
446 if (transfer->usage & PIPE_TRANSFER_WRITE) {
447 if (transfer->resource->target == PIPE_TEXTURE_2D) {
448 struct panfrost_resource *prsrc = (struct panfrost_resource *) transfer->resource;
449
450 /* Gallium thinks writeback happens here; instead, this is our cue to tile */
451 if (bo->has_afbc) {
452 printf("Warning: writes to afbc surface can't possibly work out well for you...\n");
453 } else if (bo->tiled) {
454 struct pipe_context *gallium = (struct pipe_context *) ctx;
455 struct panfrost_screen *screen = pan_screen(gallium->screen);
456 panfrost_tile_texture(screen, prsrc, transfer->level);
457 }
458 }
459 }
460 }
461
462 static void
463 panfrost_transfer_unmap(struct pipe_context *pctx,
464 struct pipe_transfer *transfer)
465 {
466 struct panfrost_context *ctx = pan_context(pctx);
467
468 panfrost_unmap_bo(ctx, transfer);
469
470 /* Derefence the resource */
471 pipe_resource_reference(&transfer->resource, NULL);
472
473 /* Transfer itself is CALLOCed at the moment */
474 free(transfer);
475 }
476
477 static struct pb_slab *
478 panfrost_slab_alloc(void *priv, unsigned heap, unsigned entry_size, unsigned group_index)
479 {
480 struct panfrost_screen *screen = (struct panfrost_screen *) priv;
481 struct panfrost_memory *mem = CALLOC_STRUCT(panfrost_memory);
482
483 size_t slab_size = (1 << (MAX_SLAB_ENTRY_SIZE + 1));
484
485 mem->slab.num_entries = slab_size / entry_size;
486 mem->slab.num_free = mem->slab.num_entries;
487
488 LIST_INITHEAD(&mem->slab.free);
489 for (unsigned i = 0; i < mem->slab.num_entries; ++i) {
490 /* Create a slab entry */
491 struct panfrost_memory_entry *entry = CALLOC_STRUCT(panfrost_memory_entry);
492 entry->offset = entry_size * i;
493
494 entry->base.slab = &mem->slab;
495 entry->base.group_index = group_index;
496
497 LIST_ADDTAIL(&entry->base.head, &mem->slab.free);
498 }
499
500 /* Actually allocate the memory from kernel-space. Mapped, same_va, no
501 * special flags */
502
503 screen->driver->allocate_slab(screen, mem, slab_size / 4096, true, 0, 0, 0);
504
505 return &mem->slab;
506 }
507
508 static bool
509 panfrost_slab_can_reclaim(void *priv, struct pb_slab_entry *entry)
510 {
511 struct panfrost_memory_entry *p_entry = (struct panfrost_memory_entry *) entry;
512 return p_entry->freed;
513 }
514
515 static void
516 panfrost_slab_free(void *priv, struct pb_slab *slab)
517 {
518 struct panfrost_memory *mem = (struct panfrost_memory *) slab;
519 struct panfrost_screen *screen = (struct panfrost_screen *) priv;
520
521 screen->driver->free_slab(screen, mem);
522 }
523
524 static void
525 panfrost_invalidate_resource(struct pipe_context *pctx, struct pipe_resource *prsc)
526 {
527 //fprintf(stderr, "TODO %s\n", __func__);
528 }
529
530 static enum pipe_format
531 panfrost_resource_get_internal_format(struct pipe_resource *prsrc)
532 {
533 return prsrc->format;
534 }
535
536 static void
537 panfrost_resource_set_stencil(struct pipe_resource *prsrc,
538 struct pipe_resource *stencil)
539 {
540 pan_resource(prsrc)->separate_stencil = pan_resource(stencil);
541 }
542
543 static struct pipe_resource *
544 panfrost_resource_get_stencil(struct pipe_resource *prsrc)
545 {
546 return &pan_resource(prsrc)->separate_stencil->base;
547 }
548
549 static const struct u_transfer_vtbl transfer_vtbl = {
550 .resource_create = panfrost_resource_create,
551 .resource_destroy = panfrost_resource_destroy,
552 .transfer_map = panfrost_transfer_map,
553 .transfer_unmap = panfrost_transfer_unmap,
554 .transfer_flush_region = u_default_transfer_flush_region,
555 .get_internal_format = panfrost_resource_get_internal_format,
556 .set_stencil = panfrost_resource_set_stencil,
557 .get_stencil = panfrost_resource_get_stencil,
558 };
559
560 void
561 panfrost_resource_screen_init(struct panfrost_screen *pscreen)
562 {
563 //pscreen->base.resource_create_with_modifiers =
564 // panfrost_resource_create_with_modifiers;
565 pscreen->base.resource_create = u_transfer_helper_resource_create;
566 pscreen->base.resource_destroy = u_transfer_helper_resource_destroy;
567 pscreen->base.resource_from_handle = panfrost_resource_from_handle;
568 pscreen->base.resource_get_handle = panfrost_resource_get_handle;
569 pscreen->base.transfer_helper = u_transfer_helper_create(&transfer_vtbl,
570 true, false,
571 true, true);
572
573 pb_slabs_init(&pscreen->slabs,
574 MIN_SLAB_ENTRY_SIZE,
575 MAX_SLAB_ENTRY_SIZE,
576
577 3, /* Number of heaps */
578
579 pscreen,
580
581 panfrost_slab_can_reclaim,
582 panfrost_slab_alloc,
583 panfrost_slab_free);
584 }
585
586 void
587 panfrost_resource_context_init(struct pipe_context *pctx)
588 {
589 pctx->transfer_map = u_transfer_helper_transfer_map;
590 pctx->transfer_flush_region = u_transfer_helper_transfer_flush_region;
591 pctx->transfer_unmap = u_transfer_helper_transfer_unmap;
592 pctx->buffer_subdata = u_default_buffer_subdata;
593 pctx->create_surface = panfrost_create_surface;
594 pctx->surface_destroy = panfrost_surface_destroy;
595 pctx->resource_copy_region = util_resource_copy_region;
596 pctx->blit = panfrost_blit;
597 //pctx->generate_mipmap = panfrost_generate_mipmap;
598 pctx->flush_resource = panfrost_flush_resource;
599 pctx->invalidate_resource = panfrost_invalidate_resource;
600 pctx->transfer_flush_region = u_transfer_helper_transfer_flush_region;
601 pctx->buffer_subdata = u_default_buffer_subdata;
602 pctx->texture_subdata = u_default_texture_subdata;
603 }