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