• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright © 2014 Broadcom
3  * Copyright (C) 2012 Rob Clark <robclark@freedesktop.org>
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, sublicense,
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 next
13  * paragraph) shall be included in all copies or substantial portions of the
14  * 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 NONINFRINGEMENT.  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 DEALINGS
22  * IN THE SOFTWARE.
23  */
24 
25 #include "pipe/p_defines.h"
26 #include "util/u_memory.h"
27 #include "util/format/u_format.h"
28 #include "util/u_inlines.h"
29 #include "util/u_surface.h"
30 #include "util/u_transfer_helper.h"
31 #include "util/u_upload_mgr.h"
32 #include "util/u_drm.h"
33 
34 #include "drm-uapi/drm_fourcc.h"
35 #include "drm-uapi/vc4_drm.h"
36 #include "vc4_screen.h"
37 #include "vc4_context.h"
38 #include "vc4_resource.h"
39 #include "vc4_tiling.h"
40 
41 static bool
vc4_resource_bo_alloc(struct vc4_resource * rsc)42 vc4_resource_bo_alloc(struct vc4_resource *rsc)
43 {
44         struct pipe_resource *prsc = &rsc->base;
45         struct pipe_screen *pscreen = prsc->screen;
46         struct vc4_bo *bo;
47 
48         if (vc4_debug & VC4_DEBUG_SURFACE) {
49                 fprintf(stderr, "alloc %p: size %d + offset %d -> %d\n",
50                         rsc,
51                         rsc->slices[0].size,
52                         rsc->slices[0].offset,
53                         rsc->slices[0].offset +
54                         rsc->slices[0].size +
55                         rsc->cube_map_stride * (prsc->array_size - 1));
56         }
57 
58         bo = vc4_bo_alloc(vc4_screen(pscreen),
59                           rsc->slices[0].offset +
60                           rsc->slices[0].size +
61                           rsc->cube_map_stride * (prsc->array_size - 1),
62                           "resource");
63         if (bo) {
64                 vc4_bo_unreference(&rsc->bo);
65                 rsc->bo = bo;
66                 return true;
67         } else {
68                 return false;
69         }
70 }
71 
72 static void
vc4_resource_transfer_unmap(struct pipe_context * pctx,struct pipe_transfer * ptrans)73 vc4_resource_transfer_unmap(struct pipe_context *pctx,
74                             struct pipe_transfer *ptrans)
75 {
76         struct vc4_context *vc4 = vc4_context(pctx);
77         struct vc4_transfer *trans = vc4_transfer(ptrans);
78 
79         if (trans->map) {
80                 struct vc4_resource *rsc = vc4_resource(ptrans->resource);
81                 struct vc4_resource_slice *slice = &rsc->slices[ptrans->level];
82 
83                 if (ptrans->usage & PIPE_MAP_WRITE) {
84                         vc4_store_tiled_image(rsc->bo->map + slice->offset +
85                                               ptrans->box.z * rsc->cube_map_stride,
86                                               slice->stride,
87                                               trans->map, ptrans->stride,
88                                               slice->tiling, rsc->cpp,
89                                               &ptrans->box);
90                 }
91                 free(trans->map);
92         }
93 
94         pipe_resource_reference(&ptrans->resource, NULL);
95         slab_free(&vc4->transfer_pool, ptrans);
96 }
97 
98 static void *
vc4_resource_transfer_map(struct pipe_context * pctx,struct pipe_resource * prsc,unsigned level,unsigned usage,const struct pipe_box * box,struct pipe_transfer ** pptrans)99 vc4_resource_transfer_map(struct pipe_context *pctx,
100                           struct pipe_resource *prsc,
101                           unsigned level, unsigned usage,
102                           const struct pipe_box *box,
103                           struct pipe_transfer **pptrans)
104 {
105         struct vc4_context *vc4 = vc4_context(pctx);
106         struct vc4_resource *rsc = vc4_resource(prsc);
107         struct vc4_transfer *trans;
108         struct pipe_transfer *ptrans;
109         enum pipe_format format = prsc->format;
110         char *buf;
111 
112         /* Upgrade DISCARD_RANGE to WHOLE_RESOURCE if the whole resource is
113          * being mapped.
114          */
115         if ((usage & PIPE_MAP_DISCARD_RANGE) &&
116             !(usage & PIPE_MAP_UNSYNCHRONIZED) &&
117             !(prsc->flags & PIPE_RESOURCE_FLAG_MAP_PERSISTENT) &&
118             prsc->last_level == 0 &&
119             prsc->width0 == box->width &&
120             prsc->height0 == box->height &&
121             prsc->depth0 == box->depth &&
122             prsc->array_size == 1 &&
123             rsc->bo->private) {
124                 usage |= PIPE_MAP_DISCARD_WHOLE_RESOURCE;
125         }
126 
127         if (usage & PIPE_MAP_DISCARD_WHOLE_RESOURCE) {
128                 if (vc4_resource_bo_alloc(rsc)) {
129                         /* If it might be bound as one of our vertex buffers,
130                          * make sure we re-emit vertex buffer state.
131                          */
132                         if (prsc->bind & PIPE_BIND_VERTEX_BUFFER)
133                                 vc4->dirty |= VC4_DIRTY_VTXBUF;
134                 } else {
135                         /* If we failed to reallocate, flush users so that we
136                          * don't violate any syncing requirements.
137                          */
138                         vc4_flush_jobs_reading_resource(vc4, prsc);
139                 }
140         } else if (!(usage & PIPE_MAP_UNSYNCHRONIZED)) {
141                 /* If we're writing and the buffer is being used by the CL, we
142                  * have to flush the CL first.  If we're only reading, we need
143                  * to flush if the CL has written our buffer.
144                  */
145                 if (usage & PIPE_MAP_WRITE)
146                         vc4_flush_jobs_reading_resource(vc4, prsc);
147                 else
148                         vc4_flush_jobs_writing_resource(vc4, prsc);
149         }
150 
151         if (usage & PIPE_MAP_WRITE) {
152                 rsc->writes++;
153                 rsc->initialized_buffers = ~0;
154         }
155 
156         trans = slab_zalloc(&vc4->transfer_pool);
157         if (!trans)
158                 return NULL;
159 
160         /* XXX: Handle DONTBLOCK, DISCARD_RANGE, PERSISTENT, COHERENT. */
161 
162         ptrans = &trans->base;
163 
164         pipe_resource_reference(&ptrans->resource, prsc);
165         ptrans->level = level;
166         ptrans->usage = usage;
167         ptrans->box = *box;
168 
169         if (usage & PIPE_MAP_UNSYNCHRONIZED)
170                 buf = vc4_bo_map_unsynchronized(rsc->bo);
171         else
172                 buf = vc4_bo_map(rsc->bo);
173         if (!buf) {
174                 fprintf(stderr, "Failed to map bo\n");
175                 goto fail;
176         }
177 
178         *pptrans = ptrans;
179 
180         struct vc4_resource_slice *slice = &rsc->slices[level];
181         if (rsc->tiled) {
182                 /* No direct mappings of tiled, since we need to manually
183                  * tile/untile.
184                  */
185                 if (usage & PIPE_MAP_DIRECTLY)
186                         return NULL;
187 
188                 /* Our load/store routines work on entire compressed blocks. */
189                 u_box_pixels_to_blocks(&ptrans->box, &ptrans->box, format);
190 
191                 ptrans->stride = ptrans->box.width * rsc->cpp;
192                 ptrans->layer_stride = ptrans->stride * ptrans->box.height;
193 
194                 trans->map = malloc(ptrans->layer_stride * ptrans->box.depth);
195 
196                 if (usage & PIPE_MAP_READ) {
197                         vc4_load_tiled_image(trans->map, ptrans->stride,
198                                              buf + slice->offset +
199                                              ptrans->box.z * rsc->cube_map_stride,
200                                              slice->stride,
201                                              slice->tiling, rsc->cpp,
202                                              &ptrans->box);
203                 }
204                 return trans->map;
205         } else {
206                 ptrans->stride = slice->stride;
207                 ptrans->layer_stride = ptrans->stride;
208 
209                 return buf + slice->offset +
210                         ptrans->box.y / util_format_get_blockheight(format) * ptrans->stride +
211                         ptrans->box.x / util_format_get_blockwidth(format) * rsc->cpp +
212                         ptrans->box.z * rsc->cube_map_stride;
213         }
214 
215 
216 fail:
217         vc4_resource_transfer_unmap(pctx, ptrans);
218         return NULL;
219 }
220 
221 static void
vc4_texture_subdata(struct pipe_context * pctx,struct pipe_resource * prsc,unsigned level,unsigned usage,const struct pipe_box * box,const void * data,unsigned stride,unsigned layer_stride)222 vc4_texture_subdata(struct pipe_context *pctx,
223                     struct pipe_resource *prsc,
224                     unsigned level,
225                     unsigned usage,
226                     const struct pipe_box *box,
227                     const void *data,
228                     unsigned stride,
229                     unsigned layer_stride)
230 {
231         struct vc4_resource *rsc = vc4_resource(prsc);
232         struct vc4_resource_slice *slice = &rsc->slices[level];
233 
234         /* For a direct mapping, we can just take the u_transfer path. */
235         if (!rsc->tiled ||
236             box->depth != 1 ||
237             (usage & PIPE_MAP_DISCARD_WHOLE_RESOURCE)) {
238                 return u_default_texture_subdata(pctx, prsc, level, usage, box,
239                                                  data, stride, layer_stride);
240         }
241 
242         /* Otherwise, map and store the texture data directly into the tiled
243          * texture.
244          */
245         void *buf;
246         if (usage & PIPE_MAP_UNSYNCHRONIZED)
247                 buf = vc4_bo_map_unsynchronized(rsc->bo);
248         else
249                 buf = vc4_bo_map(rsc->bo);
250 
251         vc4_store_tiled_image(buf + slice->offset +
252                               box->z * rsc->cube_map_stride,
253                               slice->stride,
254                               (void *)data, stride,
255                               slice->tiling, rsc->cpp,
256                               box);
257 }
258 
259 static void
vc4_resource_destroy(struct pipe_screen * pscreen,struct pipe_resource * prsc)260 vc4_resource_destroy(struct pipe_screen *pscreen,
261                      struct pipe_resource *prsc)
262 {
263         struct vc4_screen *screen = vc4_screen(pscreen);
264         struct vc4_resource *rsc = vc4_resource(prsc);
265         vc4_bo_unreference(&rsc->bo);
266 
267         if (rsc->scanout)
268                 renderonly_scanout_destroy(rsc->scanout, screen->ro);
269 
270         free(rsc);
271 }
272 
273 static uint64_t
vc4_resource_modifier(struct vc4_resource * rsc)274 vc4_resource_modifier(struct vc4_resource *rsc)
275 {
276         if (rsc->tiled)
277                 return DRM_FORMAT_MOD_BROADCOM_VC4_T_TILED;
278         else
279                 return DRM_FORMAT_MOD_LINEAR;
280 }
281 
282 static bool
vc4_resource_get_handle(struct pipe_screen * pscreen,struct pipe_context * pctx,struct pipe_resource * prsc,struct winsys_handle * whandle,unsigned usage)283 vc4_resource_get_handle(struct pipe_screen *pscreen,
284                         struct pipe_context *pctx,
285                         struct pipe_resource *prsc,
286                         struct winsys_handle *whandle,
287                         unsigned usage)
288 {
289         struct vc4_screen *screen = vc4_screen(pscreen);
290         struct vc4_resource *rsc = vc4_resource(prsc);
291 
292         whandle->stride = rsc->slices[0].stride;
293         whandle->offset = 0;
294         whandle->modifier = vc4_resource_modifier(rsc);
295 
296         /* If we're passing some reference to our BO out to some other part of
297          * the system, then we can't do any optimizations about only us being
298          * the ones seeing it (like BO caching or shadow update avoidance).
299          */
300         rsc->bo->private = false;
301 
302         switch (whandle->type) {
303         case WINSYS_HANDLE_TYPE_SHARED:
304                 if (screen->ro) {
305                         /* This could probably be supported, assuming that a
306                          * control node was used for pl111.
307                          */
308                         fprintf(stderr, "flink unsupported with pl111\n");
309                         return false;
310                 }
311 
312                 return vc4_bo_flink(rsc->bo, &whandle->handle);
313         case WINSYS_HANDLE_TYPE_KMS:
314                 if (screen->ro) {
315                         return renderonly_get_handle(rsc->scanout, whandle);
316                 }
317                 whandle->handle = rsc->bo->handle;
318                 return true;
319         case WINSYS_HANDLE_TYPE_FD:
320                 /* FDs are cross-device, so we can export directly from vc4.
321                  */
322                 whandle->handle = vc4_bo_get_dmabuf(rsc->bo);
323                 return whandle->handle != -1;
324         }
325 
326         return false;
327 }
328 
329 static bool
vc4_resource_get_param(struct pipe_screen * pscreen,struct pipe_context * pctx,struct pipe_resource * prsc,unsigned plane,unsigned layer,unsigned level,enum pipe_resource_param param,unsigned usage,uint64_t * value)330 vc4_resource_get_param(struct pipe_screen *pscreen,
331                        struct pipe_context *pctx, struct pipe_resource *prsc,
332                        unsigned plane, unsigned layer, unsigned level,
333                        enum pipe_resource_param param,
334                        unsigned usage, uint64_t *value)
335 {
336         struct vc4_resource *rsc = vc4_resource(prsc);
337 
338         switch (param) {
339         case PIPE_RESOURCE_PARAM_STRIDE:
340                 *value = rsc->slices[level].stride;
341                 return true;
342         case PIPE_RESOURCE_PARAM_OFFSET:
343                 *value = 0;
344                 return true;
345         case PIPE_RESOURCE_PARAM_MODIFIER:
346                 *value = vc4_resource_modifier(rsc);
347                 return true;
348         default:
349                 return false;
350         }
351 }
352 
353 static void
vc4_setup_slices(struct vc4_resource * rsc,const char * caller)354 vc4_setup_slices(struct vc4_resource *rsc, const char *caller)
355 {
356         struct pipe_resource *prsc = &rsc->base;
357         uint32_t width = prsc->width0;
358         uint32_t height = prsc->height0;
359         if (prsc->format == PIPE_FORMAT_ETC1_RGB8) {
360                 width = (width + 3) >> 2;
361                 height = (height + 3) >> 2;
362         }
363 
364         uint32_t pot_width = util_next_power_of_two(width);
365         uint32_t pot_height = util_next_power_of_two(height);
366         uint32_t offset = 0;
367         uint32_t utile_w = vc4_utile_width(rsc->cpp);
368         uint32_t utile_h = vc4_utile_height(rsc->cpp);
369 
370         for (int i = prsc->last_level; i >= 0; i--) {
371                 struct vc4_resource_slice *slice = &rsc->slices[i];
372 
373                 uint32_t level_width, level_height;
374                 if (i == 0) {
375                         level_width = width;
376                         level_height = height;
377                 } else {
378                         level_width = u_minify(pot_width, i);
379                         level_height = u_minify(pot_height, i);
380                 }
381 
382                 if (!rsc->tiled) {
383                         slice->tiling = VC4_TILING_FORMAT_LINEAR;
384                         if (prsc->nr_samples > 1) {
385                                 /* MSAA (4x) surfaces are stored as raw tile buffer contents. */
386                                 level_width = align(level_width, 32);
387                                 level_height = align(level_height, 32);
388                         } else {
389                                 level_width = align(level_width, utile_w);
390                         }
391                 } else {
392                         if (vc4_size_is_lt(level_width, level_height,
393                                            rsc->cpp)) {
394                                 slice->tiling = VC4_TILING_FORMAT_LT;
395                                 level_width = align(level_width, utile_w);
396                                 level_height = align(level_height, utile_h);
397                         } else {
398                                 slice->tiling = VC4_TILING_FORMAT_T;
399                                 level_width = align(level_width,
400                                                     4 * 2 * utile_w);
401                                 level_height = align(level_height,
402                                                      4 * 2 * utile_h);
403                         }
404                 }
405 
406                 slice->offset = offset;
407                 slice->stride = (level_width * rsc->cpp *
408                                  MAX2(prsc->nr_samples, 1));
409                 slice->size = level_height * slice->stride;
410 
411                 offset += slice->size;
412 
413                 if (vc4_debug & VC4_DEBUG_SURFACE) {
414                         static const char tiling_chars[] = {
415                                 [VC4_TILING_FORMAT_LINEAR] = 'R',
416                                 [VC4_TILING_FORMAT_LT] = 'L',
417                                 [VC4_TILING_FORMAT_T] = 'T'
418                         };
419                         fprintf(stderr,
420                                 "rsc %s %p (format %s: vc4 %d), %dx%d: "
421                                 "level %d (%c) -> %dx%d, stride %d@0x%08x\n",
422                                 caller, rsc,
423                                 util_format_short_name(prsc->format),
424                                 rsc->vc4_format,
425                                 prsc->width0, prsc->height0,
426                                 i, tiling_chars[slice->tiling],
427                                 level_width, level_height,
428                                 slice->stride, slice->offset);
429                 }
430         }
431 
432         /* The texture base pointer that has to point to level 0 doesn't have
433          * intra-page bits, so we have to align it, and thus shift up all the
434          * smaller slices.
435          */
436         uint32_t page_align_offset = (align(rsc->slices[0].offset, 4096) -
437                                       rsc->slices[0].offset);
438         if (page_align_offset) {
439                 for (int i = 0; i <= prsc->last_level; i++)
440                         rsc->slices[i].offset += page_align_offset;
441         }
442 
443         /* Cube map faces appear as whole miptrees at a page-aligned offset
444          * from the first face's miptree.
445          */
446         if (prsc->target == PIPE_TEXTURE_CUBE) {
447                 rsc->cube_map_stride = align(rsc->slices[0].offset +
448                                              rsc->slices[0].size, 4096);
449         }
450 }
451 
452 static struct vc4_resource *
vc4_resource_setup(struct pipe_screen * pscreen,const struct pipe_resource * tmpl)453 vc4_resource_setup(struct pipe_screen *pscreen,
454                    const struct pipe_resource *tmpl)
455 {
456         struct vc4_resource *rsc = CALLOC_STRUCT(vc4_resource);
457         if (!rsc)
458                 return NULL;
459         struct pipe_resource *prsc = &rsc->base;
460 
461         *prsc = *tmpl;
462 
463         pipe_reference_init(&prsc->reference, 1);
464         prsc->screen = pscreen;
465 
466         if (prsc->nr_samples <= 1)
467                 rsc->cpp = util_format_get_blocksize(tmpl->format);
468         else
469                 rsc->cpp = sizeof(uint32_t);
470 
471         assert(rsc->cpp);
472 
473         return rsc;
474 }
475 
476 static enum vc4_texture_data_type
get_resource_texture_format(struct pipe_resource * prsc)477 get_resource_texture_format(struct pipe_resource *prsc)
478 {
479         struct vc4_resource *rsc = vc4_resource(prsc);
480         uint8_t format = vc4_get_tex_format(prsc->format);
481 
482         if (!rsc->tiled) {
483                 if (prsc->nr_samples > 1) {
484                         return ~0;
485                 } else {
486                         if (format == VC4_TEXTURE_TYPE_RGBA8888)
487                                 return VC4_TEXTURE_TYPE_RGBA32R;
488                         else
489                                 return ~0;
490                 }
491         }
492 
493         return format;
494 }
495 
496 static struct pipe_resource *
vc4_resource_create_with_modifiers(struct pipe_screen * pscreen,const struct pipe_resource * tmpl,const uint64_t * modifiers,int count)497 vc4_resource_create_with_modifiers(struct pipe_screen *pscreen,
498                                    const struct pipe_resource *tmpl,
499                                    const uint64_t *modifiers,
500                                    int count)
501 {
502         struct vc4_screen *screen = vc4_screen(pscreen);
503         struct vc4_resource *rsc = vc4_resource_setup(pscreen, tmpl);
504         struct pipe_resource *prsc = &rsc->base;
505         bool linear_ok = drm_find_modifier(DRM_FORMAT_MOD_LINEAR, modifiers, count);
506         /* Use a tiled layout if we can, for better 3D performance. */
507         bool should_tile = true;
508 
509         /* VBOs/PBOs are untiled (and 1 height). */
510         if (tmpl->target == PIPE_BUFFER)
511                 should_tile = false;
512 
513         /* MSAA buffers are linear. */
514         if (tmpl->nr_samples > 1)
515                 should_tile = false;
516 
517         /* No tiling when we're sharing with another device (pl111). */
518         if (screen->ro && (tmpl->bind & PIPE_BIND_SCANOUT))
519                 should_tile = false;
520 
521         /* Cursors are always linear, and the user can request linear as well.
522          */
523         if (tmpl->bind & (PIPE_BIND_LINEAR | PIPE_BIND_CURSOR))
524                 should_tile = false;
525 
526         /* No shared objects with LT format -- the kernel only has T-format
527          * metadata.  LT objects are small enough it's not worth the trouble to
528          * give them metadata to tile.
529          */
530         if ((tmpl->bind & (PIPE_BIND_SHARED | PIPE_BIND_SCANOUT)) &&
531             vc4_size_is_lt(prsc->width0, prsc->height0, rsc->cpp))
532                 should_tile = false;
533 
534         /* If we're sharing or scanning out, we need the ioctl present to
535          * inform the kernel or the other side.
536          */
537         if ((tmpl->bind & (PIPE_BIND_SHARED |
538                            PIPE_BIND_SCANOUT)) && !screen->has_tiling_ioctl)
539                 should_tile = false;
540 
541         /* No user-specified modifier; determine our own. */
542         if (count == 1 && modifiers[0] == DRM_FORMAT_MOD_INVALID) {
543                 linear_ok = true;
544                 rsc->tiled = should_tile;
545         } else if (should_tile &&
546                    drm_find_modifier(DRM_FORMAT_MOD_BROADCOM_VC4_T_TILED,
547                                  modifiers, count)) {
548                 rsc->tiled = true;
549         } else if (linear_ok) {
550                 rsc->tiled = false;
551         } else {
552                 fprintf(stderr, "Unsupported modifier requested\n");
553                 return NULL;
554         }
555 
556         if (tmpl->target != PIPE_BUFFER)
557                 rsc->vc4_format = get_resource_texture_format(prsc);
558 
559         vc4_setup_slices(rsc, "create");
560         if (!vc4_resource_bo_alloc(rsc))
561                 goto fail;
562 
563         if (screen->has_tiling_ioctl) {
564                 uint64_t modifier;
565                 if (rsc->tiled)
566                         modifier = DRM_FORMAT_MOD_BROADCOM_VC4_T_TILED;
567                 else
568                         modifier = DRM_FORMAT_MOD_LINEAR;
569                 struct drm_vc4_set_tiling set_tiling = {
570                         .handle = rsc->bo->handle,
571                         .modifier = modifier,
572                 };
573                 int ret = vc4_ioctl(screen->fd, DRM_IOCTL_VC4_SET_TILING,
574                                     &set_tiling);
575                 if (ret != 0)
576                         goto fail;
577         }
578 
579         /* Set up the "scanout resource" (the dmabuf export of our buffer to
580          * the KMS handle) if the buffer might ever have
581          * resource_get_handle(WINSYS_HANDLE_TYPE_KMS) called on it.
582          * create_with_modifiers() doesn't give us usage flags, so we have to
583          * assume that all calls with modifiers are scanout-possible.
584          */
585         if (screen->ro &&
586             ((tmpl->bind & PIPE_BIND_SCANOUT) ||
587              !(count == 1 && modifiers[0] == DRM_FORMAT_MOD_INVALID))) {
588                 rsc->scanout =
589                         renderonly_scanout_for_resource(prsc, screen->ro, NULL);
590                 if (!rsc->scanout)
591                         goto fail;
592         }
593 
594         vc4_bo_label(screen, rsc->bo, "%sresource %dx%d@%d/%d",
595                      (tmpl->bind & PIPE_BIND_SCANOUT) ? "scanout " : "",
596                      tmpl->width0, tmpl->height0,
597                      rsc->cpp * 8, prsc->last_level);
598 
599         return prsc;
600 fail:
601         vc4_resource_destroy(pscreen, prsc);
602         return NULL;
603 }
604 
605 struct pipe_resource *
vc4_resource_create(struct pipe_screen * pscreen,const struct pipe_resource * tmpl)606 vc4_resource_create(struct pipe_screen *pscreen,
607                     const struct pipe_resource *tmpl)
608 {
609         const uint64_t mod = DRM_FORMAT_MOD_INVALID;
610         return vc4_resource_create_with_modifiers(pscreen, tmpl, &mod, 1);
611 }
612 
613 static struct pipe_resource *
vc4_resource_from_handle(struct pipe_screen * pscreen,const struct pipe_resource * tmpl,struct winsys_handle * whandle,unsigned usage)614 vc4_resource_from_handle(struct pipe_screen *pscreen,
615                          const struct pipe_resource *tmpl,
616                          struct winsys_handle *whandle,
617                          unsigned usage)
618 {
619         struct vc4_screen *screen = vc4_screen(pscreen);
620         struct vc4_resource *rsc = vc4_resource_setup(pscreen, tmpl);
621         struct pipe_resource *prsc = &rsc->base;
622         struct vc4_resource_slice *slice = &rsc->slices[0];
623 
624         if (!rsc)
625                 return NULL;
626 
627         switch (whandle->type) {
628         case WINSYS_HANDLE_TYPE_SHARED:
629                 rsc->bo = vc4_bo_open_name(screen, whandle->handle);
630                 break;
631         case WINSYS_HANDLE_TYPE_FD:
632                 rsc->bo = vc4_bo_open_dmabuf(screen, whandle->handle);
633                 break;
634         default:
635                 fprintf(stderr,
636                         "Attempt to import unsupported handle type %d\n",
637                         whandle->type);
638         }
639 
640         if (!rsc->bo)
641                 goto fail;
642 
643         struct drm_vc4_get_tiling get_tiling = {
644                 .handle = rsc->bo->handle,
645         };
646         int ret = vc4_ioctl(screen->fd, DRM_IOCTL_VC4_GET_TILING, &get_tiling);
647 
648         if (ret != 0) {
649                 whandle->modifier = DRM_FORMAT_MOD_LINEAR;
650         } else if (whandle->modifier == DRM_FORMAT_MOD_INVALID) {
651                 whandle->modifier = get_tiling.modifier;
652         } else if (whandle->modifier != get_tiling.modifier) {
653                 fprintf(stderr,
654                         "Modifier 0x%llx vs. tiling (0x%llx) mismatch\n",
655                         (long long)whandle->modifier, get_tiling.modifier);
656                 goto fail;
657         }
658 
659         switch (whandle->modifier) {
660         case DRM_FORMAT_MOD_LINEAR:
661                 rsc->tiled = false;
662                 break;
663         case DRM_FORMAT_MOD_BROADCOM_VC4_T_TILED:
664                 rsc->tiled = true;
665                 break;
666         default:
667                 fprintf(stderr,
668                         "Attempt to import unsupported modifier 0x%llx\n",
669                         (long long)whandle->modifier);
670                 goto fail;
671         }
672 
673         rsc->vc4_format = get_resource_texture_format(prsc);
674         vc4_setup_slices(rsc, "import");
675 
676         if (whandle->offset != 0) {
677                 if (rsc->tiled) {
678                         fprintf(stderr,
679                                 "Attempt to import unsupported "
680                                 "winsys offset %u\n",
681                                 whandle->offset);
682                         goto fail;
683                 }
684 
685                 rsc->slices[0].offset += whandle->offset;
686 
687                 if (rsc->slices[0].offset + rsc->slices[0].size >
688                     rsc->bo->size) {
689                         fprintf(stderr, "Attempt to import "
690                                 "with overflowing offset (%d + %d > %d)\n",
691                                 whandle->offset,
692                                 rsc->slices[0].size,
693                                 rsc->bo->size);
694                         goto fail;
695                 }
696         }
697 
698         if (screen->ro) {
699                 /* Make sure that renderonly has a handle to our buffer in the
700                  * display's fd, so that a later renderonly_get_handle()
701                  * returns correct handles or GEM names.
702                  */
703                 rsc->scanout =
704                         renderonly_create_gpu_import_for_resource(prsc,
705                                                                   screen->ro,
706                                                                   NULL);
707         }
708 
709         if (rsc->tiled && whandle->stride != slice->stride) {
710                 static bool warned = false;
711                 if (!warned) {
712                         warned = true;
713                         fprintf(stderr,
714                                 "Attempting to import %dx%d %s with "
715                                 "unsupported stride %d instead of %d\n",
716                                 prsc->width0, prsc->height0,
717                                 util_format_short_name(prsc->format),
718                                 whandle->stride,
719                                 slice->stride);
720                 }
721                 goto fail;
722         } else if (!rsc->tiled) {
723                 slice->stride = whandle->stride;
724         }
725 
726         return prsc;
727 
728 fail:
729         vc4_resource_destroy(pscreen, prsc);
730         return NULL;
731 }
732 
733 static struct pipe_surface *
vc4_create_surface(struct pipe_context * pctx,struct pipe_resource * ptex,const struct pipe_surface * surf_tmpl)734 vc4_create_surface(struct pipe_context *pctx,
735                    struct pipe_resource *ptex,
736                    const struct pipe_surface *surf_tmpl)
737 {
738         struct vc4_surface *surface = CALLOC_STRUCT(vc4_surface);
739         struct vc4_resource *rsc = vc4_resource(ptex);
740 
741         if (!surface)
742                 return NULL;
743 
744         assert(surf_tmpl->u.tex.first_layer == surf_tmpl->u.tex.last_layer);
745 
746         struct pipe_surface *psurf = &surface->base;
747         unsigned level = surf_tmpl->u.tex.level;
748 
749         pipe_reference_init(&psurf->reference, 1);
750         pipe_resource_reference(&psurf->texture, ptex);
751 
752         psurf->context = pctx;
753         psurf->format = surf_tmpl->format;
754         psurf->width = u_minify(ptex->width0, level);
755         psurf->height = u_minify(ptex->height0, level);
756         psurf->u.tex.level = level;
757         psurf->u.tex.first_layer = surf_tmpl->u.tex.first_layer;
758         psurf->u.tex.last_layer = surf_tmpl->u.tex.last_layer;
759         surface->offset = (rsc->slices[level].offset +
760                            psurf->u.tex.first_layer * rsc->cube_map_stride);
761         surface->tiling = rsc->slices[level].tiling;
762 
763         return &surface->base;
764 }
765 
766 static void
vc4_surface_destroy(struct pipe_context * pctx,struct pipe_surface * psurf)767 vc4_surface_destroy(struct pipe_context *pctx, struct pipe_surface *psurf)
768 {
769         pipe_resource_reference(&psurf->texture, NULL);
770         FREE(psurf);
771 }
772 
773 static void
vc4_dump_surface_non_msaa(struct pipe_surface * psurf)774 vc4_dump_surface_non_msaa(struct pipe_surface *psurf)
775 {
776         struct pipe_resource *prsc = psurf->texture;
777         struct vc4_resource *rsc = vc4_resource(prsc);
778         uint32_t *map = vc4_bo_map(rsc->bo);
779         uint32_t stride = rsc->slices[0].stride / 4;
780         uint32_t width = psurf->width;
781         uint32_t height = psurf->height;
782         uint32_t chunk_w = width / 79;
783         uint32_t chunk_h = height / 40;
784         uint32_t found_colors[10] = { 0 };
785         uint32_t num_found_colors = 0;
786 
787         if (rsc->vc4_format != VC4_TEXTURE_TYPE_RGBA32R) {
788                 fprintf(stderr, "%s: Unsupported format %s\n",
789                         __func__, util_format_short_name(psurf->format));
790                 return;
791         }
792 
793         for (int by = 0; by < height; by += chunk_h) {
794                 for (int bx = 0; bx < width; bx += chunk_w) {
795                         int all_found_color = -1; /* nothing found */
796 
797                         for (int y = by; y < MIN2(height, by + chunk_h); y++) {
798                                 for (int x = bx; x < MIN2(width, bx + chunk_w); x++) {
799                                         uint32_t pix = map[y * stride + x];
800 
801                                         int i;
802                                         for (i = 0; i < num_found_colors; i++) {
803                                                 if (pix == found_colors[i])
804                                                         break;
805                                         }
806                                         if (i == num_found_colors &&
807                                             num_found_colors <
808                                             ARRAY_SIZE(found_colors)) {
809                                                 found_colors[num_found_colors++] = pix;
810                                         }
811 
812                                         if (i < num_found_colors) {
813                                                 if (all_found_color == -1)
814                                                         all_found_color = i;
815                                                 else if (i != all_found_color)
816                                                         all_found_color = ARRAY_SIZE(found_colors);
817                                         }
818                                 }
819                         }
820                         /* If all pixels for this chunk have a consistent
821                          * value, then print a character for it.  Either a
822                          * fixed name (particularly common for piglit tests),
823                          * or a runtime-generated number.
824                          */
825                         if (all_found_color >= 0 &&
826                             all_found_color < ARRAY_SIZE(found_colors)) {
827                                 static const struct {
828                                         uint32_t val;
829                                         const char *c;
830                                 } named_colors[] = {
831                                         { 0xff000000, "█" },
832                                         { 0x00000000, "█" },
833                                         { 0xffff0000, "r" },
834                                         { 0xff00ff00, "g" },
835                                         { 0xff0000ff, "b" },
836                                         { 0xffffffff, "w" },
837                                 };
838                                 int i;
839                                 for (i = 0; i < ARRAY_SIZE(named_colors); i++) {
840                                         if (named_colors[i].val ==
841                                             found_colors[all_found_color]) {
842                                                 fprintf(stderr, "%s",
843                                                         named_colors[i].c);
844                                                 break;
845                                         }
846                                 }
847                                 /* For unnamed colors, print a number and the
848                                  * numbers will have values printed at the
849                                  * end.
850                                  */
851                                 if (i == ARRAY_SIZE(named_colors)) {
852                                         fprintf(stderr, "%c",
853                                                 '0' + all_found_color);
854                                 }
855                         } else {
856                                 /* If there's no consistent color, print this.
857                                  */
858                                 fprintf(stderr, ".");
859                         }
860                 }
861                 fprintf(stderr, "\n");
862         }
863 
864         for (int i = 0; i < num_found_colors; i++) {
865                 fprintf(stderr, "color %d: 0x%08x\n", i, found_colors[i]);
866         }
867 }
868 
869 static uint32_t
vc4_surface_msaa_get_sample(struct pipe_surface * psurf,uint32_t x,uint32_t y,uint32_t sample)870 vc4_surface_msaa_get_sample(struct pipe_surface *psurf,
871                             uint32_t x, uint32_t y, uint32_t sample)
872 {
873         struct pipe_resource *prsc = psurf->texture;
874         struct vc4_resource *rsc = vc4_resource(prsc);
875         uint32_t tile_w = 32, tile_h = 32;
876         uint32_t tiles_w = DIV_ROUND_UP(psurf->width, 32);
877 
878         uint32_t tile_x = x / tile_w;
879         uint32_t tile_y = y / tile_h;
880         uint32_t *tile = (vc4_bo_map(rsc->bo) +
881                           VC4_TILE_BUFFER_SIZE * (tile_y * tiles_w + tile_x));
882         uint32_t subtile_x = x % tile_w;
883         uint32_t subtile_y = y % tile_h;
884 
885         uint32_t quad_samples = VC4_MAX_SAMPLES * 4;
886         uint32_t tile_stride = quad_samples * tile_w / 2;
887 
888         return *((uint32_t *)tile +
889                  (subtile_y >> 1) * tile_stride +
890                  (subtile_x >> 1) * quad_samples +
891                  ((subtile_y & 1) << 1) +
892                  (subtile_x & 1) +
893                  sample);
894 }
895 
896 static void
vc4_dump_surface_msaa_char(struct pipe_surface * psurf,uint32_t start_x,uint32_t start_y,uint32_t w,uint32_t h)897 vc4_dump_surface_msaa_char(struct pipe_surface *psurf,
898                            uint32_t start_x, uint32_t start_y,
899                            uint32_t w, uint32_t h)
900 {
901         bool all_same_color = true;
902         uint32_t all_pix = 0;
903 
904         for (int y = start_y; y < start_y + h; y++) {
905                 for (int x = start_x; x < start_x + w; x++) {
906                         for (int s = 0; s < VC4_MAX_SAMPLES; s++) {
907                                 uint32_t pix = vc4_surface_msaa_get_sample(psurf,
908                                                                            x, y,
909                                                                            s);
910                                 if (x == start_x && y == start_y)
911                                         all_pix = pix;
912                                 else if (all_pix != pix)
913                                         all_same_color = false;
914                         }
915                 }
916         }
917         if (all_same_color) {
918                 static const struct {
919                         uint32_t val;
920                         const char *c;
921                 } named_colors[] = {
922                         { 0xff000000, "█" },
923                         { 0x00000000, "█" },
924                         { 0xffff0000, "r" },
925                         { 0xff00ff00, "g" },
926                         { 0xff0000ff, "b" },
927                         { 0xffffffff, "w" },
928                 };
929                 int i;
930                 for (i = 0; i < ARRAY_SIZE(named_colors); i++) {
931                         if (named_colors[i].val == all_pix) {
932                                 fprintf(stderr, "%s",
933                                         named_colors[i].c);
934                                 return;
935                         }
936                 }
937                 fprintf(stderr, "x");
938         } else {
939                 fprintf(stderr, ".");
940         }
941 }
942 
943 static void
vc4_dump_surface_msaa(struct pipe_surface * psurf)944 vc4_dump_surface_msaa(struct pipe_surface *psurf)
945 {
946         uint32_t tile_w = 32, tile_h = 32;
947         uint32_t tiles_w = DIV_ROUND_UP(psurf->width, tile_w);
948         uint32_t tiles_h = DIV_ROUND_UP(psurf->height, tile_h);
949         uint32_t char_w = 140, char_h = 60;
950         uint32_t char_w_per_tile = char_w / tiles_w - 1;
951         uint32_t char_h_per_tile = char_h / tiles_h - 1;
952 
953         fprintf(stderr, "Surface: %dx%d (%dx MSAA)\n",
954                 psurf->width, psurf->height, psurf->texture->nr_samples);
955 
956         for (int x = 0; x < (char_w_per_tile + 1) * tiles_w; x++)
957                 fprintf(stderr, "-");
958         fprintf(stderr, "\n");
959 
960         for (int ty = 0; ty < psurf->height; ty += tile_h) {
961                 for (int y = 0; y < char_h_per_tile; y++) {
962 
963                         for (int tx = 0; tx < psurf->width; tx += tile_w) {
964                                 for (int x = 0; x < char_w_per_tile; x++) {
965                                         uint32_t bx1 = (x * tile_w /
966                                                         char_w_per_tile);
967                                         uint32_t bx2 = ((x + 1) * tile_w /
968                                                         char_w_per_tile);
969                                         uint32_t by1 = (y * tile_h /
970                                                         char_h_per_tile);
971                                         uint32_t by2 = ((y + 1) * tile_h /
972                                                         char_h_per_tile);
973 
974                                         vc4_dump_surface_msaa_char(psurf,
975                                                                    tx + bx1,
976                                                                    ty + by1,
977                                                                    bx2 - bx1,
978                                                                    by2 - by1);
979                                 }
980                                 fprintf(stderr, "|");
981                         }
982                         fprintf(stderr, "\n");
983                 }
984 
985                 for (int x = 0; x < (char_w_per_tile + 1) * tiles_w; x++)
986                         fprintf(stderr, "-");
987                 fprintf(stderr, "\n");
988         }
989 }
990 
991 /** Debug routine to dump the contents of an 8888 surface to the console */
992 void
vc4_dump_surface(struct pipe_surface * psurf)993 vc4_dump_surface(struct pipe_surface *psurf)
994 {
995         if (!psurf)
996                 return;
997 
998         if (psurf->texture->nr_samples > 1)
999                 vc4_dump_surface_msaa(psurf);
1000         else
1001                 vc4_dump_surface_non_msaa(psurf);
1002 }
1003 
1004 static void
vc4_flush_resource(struct pipe_context * pctx,struct pipe_resource * resource)1005 vc4_flush_resource(struct pipe_context *pctx, struct pipe_resource *resource)
1006 {
1007         /* All calls to flush_resource are followed by a flush of the context,
1008          * so there's nothing to do.
1009          */
1010 }
1011 
1012 void
vc4_update_shadow_baselevel_texture(struct pipe_context * pctx,struct pipe_sampler_view * pview)1013 vc4_update_shadow_baselevel_texture(struct pipe_context *pctx,
1014                                     struct pipe_sampler_view *pview)
1015 {
1016         struct vc4_context *vc4 = vc4_context(pctx);
1017         struct vc4_sampler_view *view = vc4_sampler_view(pview);
1018         struct vc4_resource *shadow = vc4_resource(view->texture);
1019         struct vc4_resource *orig = vc4_resource(pview->texture);
1020 
1021         assert(view->texture != pview->texture);
1022 
1023         if (shadow->writes == orig->writes && orig->bo->private)
1024                 return;
1025 
1026         perf_debug("Updating %dx%d@%d shadow texture due to %s\n",
1027                    orig->base.width0, orig->base.height0,
1028                    pview->u.tex.first_level,
1029                    pview->u.tex.first_level ? "base level" : "raster layout");
1030 
1031         for (int i = 0; i <= shadow->base.last_level; i++) {
1032                 unsigned width = u_minify(shadow->base.width0, i);
1033                 unsigned height = u_minify(shadow->base.height0, i);
1034                 struct pipe_blit_info info = {
1035                         .dst = {
1036                                 .resource = &shadow->base,
1037                                 .level = i,
1038                                 .box = {
1039                                         .x = 0,
1040                                         .y = 0,
1041                                         .z = 0,
1042                                         .width = width,
1043                                         .height = height,
1044                                         .depth = 1,
1045                                 },
1046                                 .format = shadow->base.format,
1047                         },
1048                         .src = {
1049                                 .resource = &orig->base,
1050                                 .level = pview->u.tex.first_level + i,
1051                                 .box = {
1052                                         .x = 0,
1053                                         .y = 0,
1054                                         .z = 0,
1055                                         .width = width,
1056                                         .height = height,
1057                                         .depth = 1,
1058                                 },
1059                                 .format = orig->base.format,
1060                         },
1061                         .mask = ~0,
1062                 };
1063                 pctx->blit(pctx, &info);
1064         }
1065 
1066         shadow->writes = orig->writes;
1067 }
1068 
1069 /**
1070  * Converts a 4-byte index buffer to 2 bytes.
1071  *
1072  * Since GLES2 only has support for 1 and 2-byte indices, the hardware doesn't
1073  * include 4-byte index support, and we have to shrink it down.
1074  *
1075  * There's no fallback support for when indices end up being larger than 2^16,
1076  * though it will at least assertion fail.  Also, if the original index data
1077  * was in user memory, it would be nice to not have uploaded it to a VBO
1078  * before translating.
1079  */
1080 struct pipe_resource *
vc4_get_shadow_index_buffer(struct pipe_context * pctx,const struct pipe_draw_info * info,uint32_t offset,uint32_t count,uint32_t * shadow_offset)1081 vc4_get_shadow_index_buffer(struct pipe_context *pctx,
1082                             const struct pipe_draw_info *info,
1083                             uint32_t offset,
1084                             uint32_t count,
1085                             uint32_t *shadow_offset)
1086 {
1087         struct vc4_context *vc4 = vc4_context(pctx);
1088         struct vc4_resource *orig = vc4_resource(info->index.resource);
1089         perf_debug("Fallback conversion for %d uint indices\n", count);
1090 
1091         void *data;
1092         struct pipe_resource *shadow_rsc = NULL;
1093         u_upload_alloc(vc4->uploader, 0, count * 2, 4,
1094                        shadow_offset, &shadow_rsc, &data);
1095         uint16_t *dst = data;
1096 
1097         struct pipe_transfer *src_transfer = NULL;
1098         const uint32_t *src;
1099         if (info->has_user_indices) {
1100                 src = (uint32_t*)((char*)info->index.user + offset);
1101         } else {
1102                 src = pipe_buffer_map_range(pctx, &orig->base,
1103                                             offset,
1104                                             count * 4,
1105                                             PIPE_MAP_READ, &src_transfer);
1106         }
1107 
1108         for (int i = 0; i < count; i++) {
1109                 uint32_t src_index = src[i];
1110                 assert(src_index <= 0xffff);
1111                 dst[i] = src_index;
1112         }
1113 
1114         if (src_transfer)
1115                 pctx->buffer_unmap(pctx, src_transfer);
1116 
1117         return shadow_rsc;
1118 }
1119 
1120 static const struct u_transfer_vtbl transfer_vtbl = {
1121         .resource_create          = vc4_resource_create,
1122         .resource_destroy         = vc4_resource_destroy,
1123         .transfer_map             = vc4_resource_transfer_map,
1124         .transfer_unmap           = vc4_resource_transfer_unmap,
1125         .transfer_flush_region    = u_default_transfer_flush_region,
1126 };
1127 
1128 void
vc4_resource_screen_init(struct pipe_screen * pscreen)1129 vc4_resource_screen_init(struct pipe_screen *pscreen)
1130 {
1131         struct vc4_screen *screen = vc4_screen(pscreen);
1132 
1133         pscreen->resource_create = vc4_resource_create;
1134         pscreen->resource_create_with_modifiers =
1135                 vc4_resource_create_with_modifiers;
1136         pscreen->resource_from_handle = vc4_resource_from_handle;
1137         pscreen->resource_get_handle = vc4_resource_get_handle;
1138         pscreen->resource_get_param = vc4_resource_get_param;
1139         pscreen->resource_destroy = vc4_resource_destroy;
1140         pscreen->transfer_helper = u_transfer_helper_create(&transfer_vtbl,
1141                                                             false, false,
1142                                                             false, true,
1143                                                             false);
1144 
1145         /* Test if the kernel has GET_TILING; it will return -EINVAL if the
1146          * ioctl does not exist, but -ENOENT if we pass an impossible handle.
1147          * 0 cannot be a valid GEM object, so use that.
1148          */
1149         struct drm_vc4_get_tiling get_tiling = {
1150                 .handle = 0x0,
1151         };
1152         int ret = vc4_ioctl(screen->fd, DRM_IOCTL_VC4_GET_TILING, &get_tiling);
1153         if (ret == -1 && errno == ENOENT)
1154                 screen->has_tiling_ioctl = true;
1155 }
1156 
1157 void
vc4_resource_context_init(struct pipe_context * pctx)1158 vc4_resource_context_init(struct pipe_context *pctx)
1159 {
1160         pctx->buffer_map = u_transfer_helper_transfer_map;
1161         pctx->texture_map = u_transfer_helper_transfer_map;
1162         pctx->transfer_flush_region = u_transfer_helper_transfer_flush_region;
1163         pctx->buffer_unmap = u_transfer_helper_transfer_unmap;
1164         pctx->texture_unmap = u_transfer_helper_transfer_unmap;
1165         pctx->buffer_subdata = u_default_buffer_subdata;
1166         pctx->texture_subdata = vc4_texture_subdata;
1167         pctx->create_surface = vc4_create_surface;
1168         pctx->surface_destroy = vc4_surface_destroy;
1169         pctx->resource_copy_region = util_resource_copy_region;
1170         pctx->blit = vc4_blit;
1171         pctx->flush_resource = vc4_flush_resource;
1172 }
1173