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