panfrost/midgard: Implement f2u16 and friends
[mesa.git] / src / gallium / drivers / panfrost / pan_resource.c
1 /*
2 * Copyright (C) 2008 VMware, Inc.
3 * Copyright (C) 2014 Broadcom
4 * Copyright (C) 2018-2019 Alyssa Rosenzweig
5 * Copyright (C) 2019 Collabora, Ltd.
6 *
7 * Permission is hereby granted, free of charge, to any person obtaining a
8 * copy of this software and associated documentation files (the "Software"),
9 * to deal in the Software without restriction, including without limitation
10 * the rights to use, copy, modify, merge, publish, distribute, sublicense,
11 * and/or sell copies of the Software, and to permit persons to whom the
12 * Software is furnished to do so, subject to the following conditions:
13 *
14 * The above copyright notice and this permission notice (including the next
15 * paragraph) shall be included in all copies or substantial portions of the
16 * Software.
17 *
18 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
19 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
20 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
21 * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
22 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
23 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
24 * SOFTWARE.
25 *
26 * Authors (Collabora):
27 * Tomeu Vizoso <tomeu.vizoso@collabora.com>
28 * Alyssa Rosenzweig <alyssa.rosenzweig@collabora.com>
29 *
30 */
31
32 #include <xf86drm.h>
33 #include <fcntl.h>
34 #include "drm-uapi/drm_fourcc.h"
35
36 #include "state_tracker/winsys_handle.h"
37 #include "util/u_format.h"
38 #include "util/u_memory.h"
39 #include "util/u_surface.h"
40 #include "util/u_transfer.h"
41 #include "util/u_transfer_helper.h"
42 #include "util/u_gen_mipmap.h"
43
44 #include "pan_context.h"
45 #include "pan_screen.h"
46 #include "pan_resource.h"
47 #include "pan_util.h"
48 #include "pan_tiling.h"
49
50 static struct pipe_resource *
51 panfrost_resource_from_handle(struct pipe_screen *pscreen,
52 const struct pipe_resource *templat,
53 struct winsys_handle *whandle,
54 unsigned usage)
55 {
56 struct panfrost_screen *screen = pan_screen(pscreen);
57 struct panfrost_resource *rsc;
58 struct pipe_resource *prsc;
59
60 assert(whandle->type == WINSYS_HANDLE_TYPE_FD);
61
62 rsc = rzalloc(pscreen, struct panfrost_resource);
63 if (!rsc)
64 return NULL;
65
66 prsc = &rsc->base;
67
68 *prsc = *templat;
69
70 pipe_reference_init(&prsc->reference, 1);
71 prsc->screen = pscreen;
72
73 rsc->bo = panfrost_drm_import_bo(screen, whandle->handle);
74 rsc->slices[0].stride = whandle->stride;
75 rsc->slices[0].initialized = true;
76
77 if (screen->ro) {
78 rsc->scanout =
79 renderonly_create_gpu_import_for_resource(prsc, screen->ro, NULL);
80 /* failure is expected in some cases.. */
81 }
82
83 return prsc;
84 }
85
86 static boolean
87 panfrost_resource_get_handle(struct pipe_screen *pscreen,
88 struct pipe_context *ctx,
89 struct pipe_resource *pt,
90 struct winsys_handle *handle,
91 unsigned usage)
92 {
93 struct panfrost_screen *screen = pan_screen(pscreen);
94 struct panfrost_resource *rsrc = (struct panfrost_resource *) pt;
95 struct renderonly_scanout *scanout = rsrc->scanout;
96
97 handle->modifier = DRM_FORMAT_MOD_INVALID;
98
99 if (handle->type == WINSYS_HANDLE_TYPE_SHARED) {
100 return FALSE;
101 } else if (handle->type == WINSYS_HANDLE_TYPE_KMS) {
102 if (renderonly_get_handle(scanout, handle))
103 return TRUE;
104
105 handle->handle = rsrc->bo->gem_handle;
106 handle->stride = rsrc->slices[0].stride;
107 return TRUE;
108 } else if (handle->type == WINSYS_HANDLE_TYPE_FD) {
109 if (scanout) {
110 struct drm_prime_handle args = {
111 .handle = scanout->handle,
112 .flags = DRM_CLOEXEC,
113 };
114
115 int ret = drmIoctl(screen->ro->kms_fd, DRM_IOCTL_PRIME_HANDLE_TO_FD, &args);
116 if (ret == -1)
117 return FALSE;
118
119 handle->stride = scanout->stride;
120 handle->handle = args.fd;
121
122 return TRUE;
123 } else {
124 int fd = panfrost_drm_export_bo(screen, rsrc->bo);
125
126 if (fd < 0)
127 return FALSE;
128
129 handle->handle = fd;
130 handle->stride = rsrc->slices[0].stride;
131 return TRUE;
132 }
133 }
134
135 return FALSE;
136 }
137
138 static void
139 panfrost_flush_resource(struct pipe_context *pctx, struct pipe_resource *prsc)
140 {
141 //DBG("TODO %s\n", __func__);
142 }
143
144 static struct pipe_surface *
145 panfrost_create_surface(struct pipe_context *pipe,
146 struct pipe_resource *pt,
147 const struct pipe_surface *surf_tmpl)
148 {
149 struct pipe_surface *ps = NULL;
150
151 ps = rzalloc(pipe, struct pipe_surface);
152
153 if (ps) {
154 pipe_reference_init(&ps->reference, 1);
155 pipe_resource_reference(&ps->texture, pt);
156 ps->context = pipe;
157 ps->format = surf_tmpl->format;
158
159 if (pt->target != PIPE_BUFFER) {
160 assert(surf_tmpl->u.tex.level <= pt->last_level);
161 ps->width = u_minify(pt->width0, surf_tmpl->u.tex.level);
162 ps->height = u_minify(pt->height0, surf_tmpl->u.tex.level);
163 ps->u.tex.level = surf_tmpl->u.tex.level;
164 ps->u.tex.first_layer = surf_tmpl->u.tex.first_layer;
165 ps->u.tex.last_layer = surf_tmpl->u.tex.last_layer;
166 } else {
167 /* setting width as number of elements should get us correct renderbuffer width */
168 ps->width = surf_tmpl->u.buf.last_element - surf_tmpl->u.buf.first_element + 1;
169 ps->height = pt->height0;
170 ps->u.buf.first_element = surf_tmpl->u.buf.first_element;
171 ps->u.buf.last_element = surf_tmpl->u.buf.last_element;
172 assert(ps->u.buf.first_element <= ps->u.buf.last_element);
173 assert(ps->u.buf.last_element < ps->width);
174 }
175 }
176
177 return ps;
178 }
179
180 static void
181 panfrost_surface_destroy(struct pipe_context *pipe,
182 struct pipe_surface *surf)
183 {
184 assert(surf->texture);
185 pipe_resource_reference(&surf->texture, NULL);
186 ralloc_free(surf);
187 }
188
189 static struct pipe_resource *
190 panfrost_create_scanout_res(struct pipe_screen *screen,
191 const struct pipe_resource *template)
192 {
193 struct panfrost_screen *pscreen = pan_screen(screen);
194 struct pipe_resource scanout_templat = *template;
195 struct renderonly_scanout *scanout;
196 struct winsys_handle handle;
197 struct pipe_resource *res;
198
199 scanout = renderonly_scanout_for_resource(&scanout_templat,
200 pscreen->ro, &handle);
201 if (!scanout)
202 return NULL;
203
204 assert(handle.type == WINSYS_HANDLE_TYPE_FD);
205 /* TODO: handle modifiers? */
206 res = screen->resource_from_handle(screen, template, &handle,
207 PIPE_HANDLE_USAGE_FRAMEBUFFER_WRITE);
208 close(handle.handle);
209 if (!res)
210 return NULL;
211
212 struct panfrost_resource *pres = pan_resource(res);
213
214 pres->scanout = scanout;
215 pscreen->display_target = pres;
216
217 return res;
218 }
219
220 /* Computes sizes for checksumming, which is 8 bytes per 16x16 tile */
221
222 #define CHECKSUM_TILE_WIDTH 16
223 #define CHECKSUM_TILE_HEIGHT 16
224 #define CHECKSUM_BYTES_PER_TILE 8
225
226 static unsigned
227 panfrost_compute_checksum_sizes(
228 struct panfrost_slice *slice,
229 unsigned width,
230 unsigned height)
231 {
232 unsigned aligned_width = ALIGN_POT(width, CHECKSUM_TILE_WIDTH);
233 unsigned aligned_height = ALIGN_POT(height, CHECKSUM_TILE_HEIGHT);
234
235 unsigned tile_count_x = aligned_width / CHECKSUM_TILE_WIDTH;
236 unsigned tile_count_y = aligned_height / CHECKSUM_TILE_HEIGHT;
237
238 slice->checksum_stride = tile_count_x * CHECKSUM_BYTES_PER_TILE;
239
240 return slice->checksum_stride * tile_count_y;
241 }
242
243 /* Setup the mip tree given a particular layout, possibly with checksumming */
244
245 static void
246 panfrost_setup_slices(struct panfrost_resource *pres, size_t *bo_size)
247 {
248 struct pipe_resource *res = &pres->base;
249 unsigned width = res->width0;
250 unsigned height = res->height0;
251 unsigned depth = res->depth0;
252 unsigned bytes_per_pixel = util_format_get_blocksize(res->format);
253
254 assert(depth > 0);
255
256 /* Tiled operates blockwise; linear is packed. Also, anything
257 * we render to has to be tile-aligned. Maybe not strictly
258 * necessary, but we're not *that* pressed for memory and it
259 * makes code a lot simpler */
260
261 bool renderable = res->bind &
262 (PIPE_BIND_RENDER_TARGET | PIPE_BIND_DEPTH_STENCIL);
263 bool afbc = pres->layout == PAN_AFBC;
264 bool tiled = pres->layout == PAN_TILED;
265 bool should_align = renderable || tiled;
266
267 /* We don't know how to specify a 2D stride for 3D textures */
268
269 bool can_align_stride =
270 res->target != PIPE_TEXTURE_3D;
271
272 should_align &= can_align_stride;
273
274 unsigned offset = 0;
275 unsigned size_2d = 0;
276
277 for (unsigned l = 0; l <= res->last_level; ++l) {
278 struct panfrost_slice *slice = &pres->slices[l];
279
280 unsigned effective_width = width;
281 unsigned effective_height = height;
282 unsigned effective_depth = depth;
283
284 if (should_align) {
285 effective_width = ALIGN_POT(effective_width, 16);
286 effective_height = ALIGN_POT(effective_height, 16);
287
288 /* We don't need to align depth */
289 }
290
291 slice->offset = offset;
292
293 /* Compute the would-be stride */
294 unsigned stride = bytes_per_pixel * effective_width;
295
296 /* ..but cache-line align it for performance */
297 if (can_align_stride && pres->layout == PAN_LINEAR)
298 stride = ALIGN_POT(stride, 64);
299
300 slice->stride = stride;
301
302 unsigned slice_one_size = slice->stride * effective_height;
303 unsigned slice_full_size = slice_one_size * effective_depth;
304
305 /* Report 2D size for 3D texturing */
306
307 if (l == 0)
308 size_2d = slice_one_size;
309
310 /* Compute AFBC sizes if necessary */
311 if (afbc) {
312 slice->header_size =
313 panfrost_afbc_header_size(width, height);
314
315 offset += slice->header_size;
316 }
317
318 offset += slice_full_size;
319
320 /* Add a checksum region if necessary */
321 if (pres->checksummed) {
322 slice->checksum_offset = offset;
323
324 unsigned size = panfrost_compute_checksum_sizes(
325 slice, width, height);
326
327 offset += size;
328 }
329
330 width = u_minify(width, 1);
331 height = u_minify(height, 1);
332 depth = u_minify(depth, 1);
333 }
334
335 assert(res->array_size);
336
337 if (res->target != PIPE_TEXTURE_3D) {
338 /* Arrays and cubemaps have the entire miptree duplicated */
339
340 pres->cubemap_stride = ALIGN_POT(offset, 64);
341 *bo_size = ALIGN_POT(pres->cubemap_stride * res->array_size, 4096);
342 } else {
343 /* 3D strides across the 2D layers */
344 assert(res->array_size == 1);
345
346 pres->cubemap_stride = size_2d;
347 *bo_size = ALIGN_POT(offset, 4096);
348 }
349 }
350
351 static void
352 panfrost_resource_create_bo(struct panfrost_screen *screen, struct panfrost_resource *pres)
353 {
354 struct pipe_resource *res = &pres->base;
355
356 /* Based on the usage, figure out what storing will be used. There are
357 * various tradeoffs:
358 *
359 * Linear: the basic format, bad for memory bandwidth, bad for cache
360 * use. Zero-copy, though. Renderable.
361 *
362 * Tiled: Not compressed, but cache-optimized. Expensive to write into
363 * (due to software tiling), but cheap to sample from. Ideal for most
364 * textures.
365 *
366 * AFBC: Compressed and renderable (so always desirable for non-scanout
367 * rendertargets). Cheap to sample from. The format is black box, so we
368 * can't read/write from software.
369 */
370
371 /* Tiling textures is almost always faster, unless we only use it once */
372
373 bool is_texture = (res->bind & PIPE_BIND_SAMPLER_VIEW);
374 bool is_2d = res->depth0 == 1 && res->array_size == 1;
375 bool is_streaming = (res->usage != PIPE_USAGE_STREAM);
376
377 bool should_tile = is_streaming && is_texture && is_2d;
378
379 /* Depth/stencil can't be tiled, only linear or AFBC */
380 should_tile &= !(res->bind & PIPE_BIND_DEPTH_STENCIL);
381
382 /* FBOs we would like to checksum, if at all possible */
383 bool can_checksum = !(res->bind & (PIPE_BIND_SCANOUT | PIPE_BIND_SHARED));
384 bool should_checksum = res->bind & PIPE_BIND_RENDER_TARGET;
385
386 pres->checksummed = can_checksum && should_checksum;
387
388 /* Set the layout appropriately */
389 pres->layout = should_tile ? PAN_TILED : PAN_LINEAR;
390
391 size_t bo_size;
392
393 panfrost_setup_slices(pres, &bo_size);
394 pres->bo = panfrost_drm_create_bo(screen, bo_size, 0);
395 }
396
397 static struct pipe_resource *
398 panfrost_resource_create(struct pipe_screen *screen,
399 const struct pipe_resource *template)
400 {
401 /* Make sure we're familiar */
402 switch (template->target) {
403 case PIPE_BUFFER:
404 case PIPE_TEXTURE_1D:
405 case PIPE_TEXTURE_2D:
406 case PIPE_TEXTURE_3D:
407 case PIPE_TEXTURE_CUBE:
408 case PIPE_TEXTURE_RECT:
409 case PIPE_TEXTURE_2D_ARRAY:
410 break;
411 default:
412 DBG("Unknown texture target %d\n", template->target);
413 assert(0);
414 }
415
416 if (template->bind &
417 (PIPE_BIND_DISPLAY_TARGET | PIPE_BIND_SCANOUT | PIPE_BIND_SHARED))
418 return panfrost_create_scanout_res(screen, template);
419
420 struct panfrost_resource *so = rzalloc(screen, struct panfrost_resource);
421 struct panfrost_screen *pscreen = (struct panfrost_screen *) screen;
422
423 so->base = *template;
424 so->base.screen = screen;
425
426 pipe_reference_init(&so->base.reference, 1);
427
428 util_range_init(&so->valid_buffer_range);
429
430 panfrost_resource_create_bo(pscreen, so);
431 return (struct pipe_resource *)so;
432 }
433
434 void
435 panfrost_bo_reference(struct panfrost_bo *bo)
436 {
437 pipe_reference(NULL, &bo->reference);
438 }
439
440 void
441 panfrost_bo_unreference(struct pipe_screen *screen, struct panfrost_bo *bo)
442 {
443 /* When the reference count goes to zero, we need to cleanup */
444
445 if (pipe_reference(&bo->reference, NULL))
446 panfrost_drm_release_bo(pan_screen(screen), bo);
447 }
448
449 static void
450 panfrost_resource_destroy(struct pipe_screen *screen,
451 struct pipe_resource *pt)
452 {
453 struct panfrost_screen *pscreen = pan_screen(screen);
454 struct panfrost_resource *rsrc = (struct panfrost_resource *) pt;
455
456 if (rsrc->scanout)
457 renderonly_scanout_destroy(rsrc->scanout, pscreen->ro);
458
459 if (rsrc->bo)
460 panfrost_bo_unreference(screen, rsrc->bo);
461
462 util_range_destroy(&rsrc->valid_buffer_range);
463 ralloc_free(rsrc);
464 }
465
466 static void *
467 panfrost_transfer_map(struct pipe_context *pctx,
468 struct pipe_resource *resource,
469 unsigned level,
470 unsigned usage, /* a combination of PIPE_TRANSFER_x */
471 const struct pipe_box *box,
472 struct pipe_transfer **out_transfer)
473 {
474 int bytes_per_pixel = util_format_get_blocksize(resource->format);
475 struct panfrost_resource *rsrc = pan_resource(resource);
476 struct panfrost_bo *bo = rsrc->bo;
477
478 struct panfrost_gtransfer *transfer = rzalloc(pctx, struct panfrost_gtransfer);
479 transfer->base.level = level;
480 transfer->base.usage = usage;
481 transfer->base.box = *box;
482
483 pipe_resource_reference(&transfer->base.resource, resource);
484
485 *out_transfer = &transfer->base;
486
487 /* Check if we're bound for rendering and this is a read pixels. If so,
488 * we need to flush */
489
490 struct panfrost_context *ctx = pan_context(pctx);
491 struct pipe_framebuffer_state *fb = &ctx->pipe_framebuffer;
492
493 bool is_bound = false;
494
495 for (unsigned c = 0; c < fb->nr_cbufs; ++c) {
496 is_bound |= fb->cbufs[c]->texture == resource;
497 }
498
499 if (is_bound && (usage & PIPE_TRANSFER_READ)) {
500 assert(level == 0);
501 panfrost_flush(pctx, NULL, PIPE_FLUSH_END_OF_FRAME);
502 }
503
504 /* TODO: Respect usage flags */
505
506 if (usage & PIPE_TRANSFER_DISCARD_WHOLE_RESOURCE) {
507 /* TODO: reallocate */
508 //printf("debug: Missed reallocate\n");
509 } else if ((usage & PIPE_TRANSFER_WRITE)
510 && resource->target == PIPE_BUFFER
511 && !util_ranges_intersect(&rsrc->valid_buffer_range, box->x, box->x + box->width)) {
512 /* No flush for writes to uninitialized */
513 } else if (!(usage & PIPE_TRANSFER_UNSYNCHRONIZED)) {
514 if (usage & PIPE_TRANSFER_WRITE) {
515 /* STUB: flush reading */
516 //printf("debug: missed reading flush %d\n", resource->target);
517 } else if (usage & PIPE_TRANSFER_READ) {
518 /* STUB: flush writing */
519 //printf("debug: missed writing flush %d (%d-%d)\n", resource->target, box->x, box->x + box->width);
520 } else {
521 /* Why are you even mapping?! */
522 }
523 }
524
525 if (rsrc->layout != PAN_LINEAR) {
526 /* Non-linear resources need to be indirectly mapped */
527
528 if (usage & PIPE_TRANSFER_MAP_DIRECTLY)
529 return NULL;
530
531 transfer->base.stride = box->width * bytes_per_pixel;
532 transfer->base.layer_stride = transfer->base.stride * box->height;
533 transfer->map = rzalloc_size(transfer, transfer->base.layer_stride * box->depth);
534 assert(box->depth == 1);
535
536 if ((usage & PIPE_TRANSFER_READ) && rsrc->slices[level].initialized) {
537 if (rsrc->layout == PAN_AFBC) {
538 DBG("Unimplemented: reads from AFBC");
539 } else if (rsrc->layout == PAN_TILED) {
540 panfrost_load_tiled_image(
541 transfer->map,
542 bo->cpu + rsrc->slices[level].offset,
543 box,
544 transfer->base.stride,
545 rsrc->slices[level].stride,
546 util_format_get_blocksize(resource->format));
547 }
548 }
549
550 return transfer->map;
551 } else {
552 transfer->base.stride = rsrc->slices[level].stride;
553 transfer->base.layer_stride = rsrc->cubemap_stride;
554
555 /* By mapping direct-write, we're implicitly already
556 * initialized (maybe), so be conservative */
557
558 if ((usage & PIPE_TRANSFER_WRITE) && (usage & PIPE_TRANSFER_MAP_DIRECTLY))
559 rsrc->slices[level].initialized = true;
560
561 return bo->cpu
562 + rsrc->slices[level].offset
563 + transfer->base.box.z * rsrc->cubemap_stride
564 + transfer->base.box.y * rsrc->slices[level].stride
565 + transfer->base.box.x * bytes_per_pixel;
566 }
567 }
568
569 static void
570 panfrost_transfer_unmap(struct pipe_context *pctx,
571 struct pipe_transfer *transfer)
572 {
573 /* Gallium expects writeback here, so we tile */
574
575 struct panfrost_gtransfer *trans = pan_transfer(transfer);
576 struct panfrost_resource *prsrc = (struct panfrost_resource *) transfer->resource;
577
578 if (trans->map) {
579 struct panfrost_bo *bo = prsrc->bo;
580
581 if (transfer->usage & PIPE_TRANSFER_WRITE) {
582 unsigned level = transfer->level;
583 prsrc->slices[level].initialized = true;
584
585 if (prsrc->layout == PAN_AFBC) {
586 DBG("Unimplemented: writes to AFBC\n");
587 } else if (prsrc->layout == PAN_TILED) {
588 assert(transfer->box.depth == 1);
589
590 panfrost_store_tiled_image(
591 bo->cpu + prsrc->slices[level].offset,
592 trans->map,
593 &transfer->box,
594 prsrc->slices[level].stride,
595 transfer->stride,
596 util_format_get_blocksize(prsrc->base.format));
597 }
598 }
599 }
600
601
602 util_range_add(&prsrc->valid_buffer_range,
603 transfer->box.x,
604 transfer->box.x + transfer->box.width);
605
606 /* Derefence the resource */
607 pipe_resource_reference(&transfer->resource, NULL);
608
609 /* Transfer itself is RALLOCed at the moment */
610 ralloc_free(transfer);
611 }
612
613 static void
614 panfrost_transfer_flush_region(struct pipe_context *pctx,
615 struct pipe_transfer *transfer,
616 const struct pipe_box *box)
617 {
618 struct panfrost_resource *rsc = pan_resource(transfer->resource);
619
620 if (transfer->resource->target == PIPE_BUFFER) {
621 util_range_add(&rsc->valid_buffer_range,
622 transfer->box.x + box->x,
623 transfer->box.x + box->x + box->width);
624 }
625 }
626
627 static struct pb_slab *
628 panfrost_slab_alloc(void *priv, unsigned heap, unsigned entry_size, unsigned group_index)
629 {
630 struct panfrost_screen *screen = (struct panfrost_screen *) priv;
631 struct panfrost_memory *mem = rzalloc(screen, struct panfrost_memory);
632
633 size_t slab_size = (1 << (MAX_SLAB_ENTRY_SIZE + 1));
634
635 mem->slab.num_entries = slab_size / entry_size;
636 mem->slab.num_free = mem->slab.num_entries;
637
638 LIST_INITHEAD(&mem->slab.free);
639 for (unsigned i = 0; i < mem->slab.num_entries; ++i) {
640 /* Create a slab entry */
641 struct panfrost_memory_entry *entry = rzalloc(mem, struct panfrost_memory_entry);
642 entry->offset = entry_size * i;
643
644 entry->base.slab = &mem->slab;
645 entry->base.group_index = group_index;
646
647 LIST_ADDTAIL(&entry->base.head, &mem->slab.free);
648 }
649
650 /* Actually allocate the memory from kernel-space. Mapped, same_va, no
651 * special flags */
652
653 panfrost_drm_allocate_slab(screen, mem, slab_size / 4096, true, 0, 0, 0);
654
655 return &mem->slab;
656 }
657
658 static bool
659 panfrost_slab_can_reclaim(void *priv, struct pb_slab_entry *entry)
660 {
661 struct panfrost_memory_entry *p_entry = (struct panfrost_memory_entry *) entry;
662 return p_entry->freed;
663 }
664
665 static void
666 panfrost_slab_free(void *priv, struct pb_slab *slab)
667 {
668 struct panfrost_memory *mem = (struct panfrost_memory *) slab;
669 struct panfrost_screen *screen = (struct panfrost_screen *) priv;
670
671 panfrost_drm_free_slab(screen, mem);
672 ralloc_free(mem);
673 }
674
675 static void
676 panfrost_invalidate_resource(struct pipe_context *pctx, struct pipe_resource *prsc)
677 {
678 //DBG("TODO %s\n", __func__);
679 }
680
681 static enum pipe_format
682 panfrost_resource_get_internal_format(struct pipe_resource *prsrc)
683 {
684 return prsrc->format;
685 }
686
687 static boolean
688 panfrost_generate_mipmap(
689 struct pipe_context *pctx,
690 struct pipe_resource *prsrc,
691 enum pipe_format format,
692 unsigned base_level,
693 unsigned last_level,
694 unsigned first_layer,
695 unsigned last_layer)
696 {
697 struct panfrost_context *ctx = pan_context(pctx);
698 struct panfrost_resource *rsrc = pan_resource(prsrc);
699
700 /* Generating a mipmap invalidates the written levels, so make that
701 * explicit so we don't try to wallpaper them back and end up with
702 * u_blitter recursion */
703
704 assert(rsrc->bo);
705 for (unsigned l = base_level + 1; l <= last_level; ++l)
706 rsrc->slices[l].initialized = false;
707
708 /* Beyond that, we just delegate the hard stuff. We're careful to
709 * include flushes on both ends to make sure the data is really valid.
710 * We could be doing a lot better perf-wise, especially once we have
711 * reorder-type optimizations in place. But for now prioritize
712 * correctness. */
713
714 struct panfrost_job *job = panfrost_get_job_for_fbo(ctx);
715 bool has_draws = job->last_job.gpu;
716
717 if (has_draws)
718 panfrost_flush(pctx, NULL, PIPE_FLUSH_END_OF_FRAME);
719
720 /* We've flushed the original buffer if needed, now trigger a blit */
721
722 bool blit_res = util_gen_mipmap(
723 pctx, prsrc, format,
724 base_level, last_level,
725 first_layer, last_layer,
726 PIPE_TEX_FILTER_LINEAR);
727
728 /* If the blit was successful, flush once more. If it wasn't, well, let
729 * the state tracker deal with it. */
730
731 if (blit_res)
732 panfrost_flush(pctx, NULL, PIPE_FLUSH_END_OF_FRAME);
733
734 return blit_res;
735 }
736
737 /* Computes the address to a texture at a particular slice */
738
739 mali_ptr
740 panfrost_get_texture_address(
741 struct panfrost_resource *rsrc,
742 unsigned level, unsigned face)
743 {
744 unsigned level_offset = rsrc->slices[level].offset;
745 unsigned face_offset = face * rsrc->cubemap_stride;
746
747 return rsrc->bo->gpu + level_offset + face_offset;
748 }
749
750 static void
751 panfrost_resource_set_stencil(struct pipe_resource *prsrc,
752 struct pipe_resource *stencil)
753 {
754 pan_resource(prsrc)->separate_stencil = pan_resource(stencil);
755 }
756
757 static struct pipe_resource *
758 panfrost_resource_get_stencil(struct pipe_resource *prsrc)
759 {
760 return &pan_resource(prsrc)->separate_stencil->base;
761 }
762
763 static const struct u_transfer_vtbl transfer_vtbl = {
764 .resource_create = panfrost_resource_create,
765 .resource_destroy = panfrost_resource_destroy,
766 .transfer_map = panfrost_transfer_map,
767 .transfer_unmap = panfrost_transfer_unmap,
768 .transfer_flush_region = panfrost_transfer_flush_region,
769 .get_internal_format = panfrost_resource_get_internal_format,
770 .set_stencil = panfrost_resource_set_stencil,
771 .get_stencil = panfrost_resource_get_stencil,
772 };
773
774 void
775 panfrost_resource_screen_init(struct panfrost_screen *pscreen)
776 {
777 //pscreen->base.resource_create_with_modifiers =
778 // panfrost_resource_create_with_modifiers;
779 pscreen->base.resource_create = u_transfer_helper_resource_create;
780 pscreen->base.resource_destroy = u_transfer_helper_resource_destroy;
781 pscreen->base.resource_from_handle = panfrost_resource_from_handle;
782 pscreen->base.resource_get_handle = panfrost_resource_get_handle;
783 pscreen->base.transfer_helper = u_transfer_helper_create(&transfer_vtbl,
784 true, false,
785 true, true);
786
787 pb_slabs_init(&pscreen->slabs,
788 MIN_SLAB_ENTRY_SIZE,
789 MAX_SLAB_ENTRY_SIZE,
790
791 3, /* Number of heaps */
792
793 pscreen,
794
795 panfrost_slab_can_reclaim,
796 panfrost_slab_alloc,
797 panfrost_slab_free);
798 }
799
800 void
801 panfrost_resource_screen_deinit(struct panfrost_screen *pscreen)
802 {
803 pb_slabs_deinit(&pscreen->slabs);
804 }
805
806 void
807 panfrost_resource_context_init(struct pipe_context *pctx)
808 {
809 pctx->transfer_map = u_transfer_helper_transfer_map;
810 pctx->transfer_flush_region = u_transfer_helper_transfer_flush_region;
811 pctx->transfer_unmap = u_transfer_helper_transfer_unmap;
812 pctx->buffer_subdata = u_default_buffer_subdata;
813 pctx->create_surface = panfrost_create_surface;
814 pctx->surface_destroy = panfrost_surface_destroy;
815 pctx->resource_copy_region = util_resource_copy_region;
816 pctx->blit = panfrost_blit;
817 pctx->generate_mipmap = panfrost_generate_mipmap;
818 pctx->flush_resource = panfrost_flush_resource;
819 pctx->invalidate_resource = panfrost_invalidate_resource;
820 pctx->transfer_flush_region = u_transfer_helper_transfer_flush_region;
821 pctx->buffer_subdata = u_default_buffer_subdata;
822 pctx->texture_subdata = u_default_texture_subdata;
823 }