2 * Copyright © 2014 Broadcom
3 * Copyright (C) 2012 Rob Clark <robclark@freedesktop.org>
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:
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
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
25 #include "util/u_memory.h"
26 #include "util/u_format.h"
27 #include "util/u_inlines.h"
28 #include "util/u_surface.h"
29 #include "util/u_upload_mgr.h"
31 #include "vc4_screen.h"
32 #include "vc4_context.h"
33 #include "vc4_resource.h"
34 #include "vc4_tiling.h"
36 static bool miptree_debug = false;
39 vc4_resource_bo_alloc(struct vc4_resource *rsc)
41 struct pipe_resource *prsc = &rsc->base.b;
42 struct pipe_screen *pscreen = prsc->screen;
45 fprintf(stderr, "alloc %p: size %d + offset %d -> %d\n",
48 rsc->slices[0].offset,
49 rsc->slices[0].offset +
51 rsc->cube_map_stride * (prsc->array_size - 1));
54 vc4_bo_unreference(&rsc->bo);
55 rsc->bo = vc4_bo_alloc(vc4_screen(pscreen),
56 rsc->slices[0].offset +
58 rsc->cube_map_stride * (prsc->array_size - 1),
63 vc4_resource_transfer_unmap(struct pipe_context *pctx,
64 struct pipe_transfer *ptrans)
66 struct vc4_context *vc4 = vc4_context(pctx);
67 struct vc4_transfer *trans = vc4_transfer(ptrans);
68 struct pipe_resource *prsc = ptrans->resource;
69 struct vc4_resource *rsc = vc4_resource(prsc);
70 struct vc4_resource_slice *slice = &rsc->slices[ptrans->level];
73 if (ptrans->usage & PIPE_TRANSFER_WRITE) {
74 vc4_store_tiled_image(rsc->bo->map + slice->offset +
75 ptrans->box.z * rsc->cube_map_stride,
77 trans->map, ptrans->stride,
78 slice->tiling, rsc->cpp,
84 pipe_resource_reference(&ptrans->resource, NULL);
85 util_slab_free(&vc4->transfer_pool, ptrans);
89 vc4_resource_transfer_map(struct pipe_context *pctx,
90 struct pipe_resource *prsc,
91 unsigned level, unsigned usage,
92 const struct pipe_box *box,
93 struct pipe_transfer **pptrans)
95 struct vc4_context *vc4 = vc4_context(pctx);
96 struct vc4_resource *rsc = vc4_resource(prsc);
97 struct vc4_resource_slice *slice = &rsc->slices[level];
98 struct vc4_transfer *trans;
99 struct pipe_transfer *ptrans;
100 enum pipe_format format = prsc->format;
103 if (usage & PIPE_TRANSFER_DISCARD_WHOLE_RESOURCE) {
104 vc4_resource_bo_alloc(rsc);
106 /* If it might be bound as one of our vertex buffers, make
107 * sure we re-emit vertex buffer state.
109 if (prsc->bind & PIPE_BIND_VERTEX_BUFFER)
110 vc4->dirty |= VC4_DIRTY_VTXBUF;
111 } else if (!(usage & PIPE_TRANSFER_UNSYNCHRONIZED)) {
112 if (vc4_cl_references_bo(pctx, rsc->bo)) {
113 if ((usage & PIPE_TRANSFER_DISCARD_RANGE) &&
114 prsc->last_level == 0 &&
115 prsc->width0 == box->width &&
116 prsc->height0 == box->height &&
117 prsc->depth0 == box->depth) {
118 vc4_resource_bo_alloc(rsc);
119 if (prsc->bind & PIPE_BIND_VERTEX_BUFFER)
120 vc4->dirty |= VC4_DIRTY_VTXBUF;
127 if (usage & PIPE_TRANSFER_WRITE)
130 trans = util_slab_alloc(&vc4->transfer_pool);
134 /* XXX: Handle DONTBLOCK, DISCARD_RANGE, PERSISTENT, COHERENT. */
136 /* util_slab_alloc() doesn't zero: */
137 memset(trans, 0, sizeof(*trans));
138 ptrans = &trans->base;
140 pipe_resource_reference(&ptrans->resource, prsc);
141 ptrans->level = level;
142 ptrans->usage = usage;
145 /* Note that the current kernel implementation is synchronous, so no
146 * need to do syncing stuff here yet.
149 if (usage & PIPE_TRANSFER_UNSYNCHRONIZED)
150 buf = vc4_bo_map_unsynchronized(rsc->bo);
152 buf = vc4_bo_map(rsc->bo);
154 fprintf(stderr, "Failed to map bo\n");
161 uint32_t utile_w = vc4_utile_width(rsc->cpp);
162 uint32_t utile_h = vc4_utile_height(rsc->cpp);
164 /* No direct mappings of tiled, since we need to manually
167 if (usage & PIPE_TRANSFER_MAP_DIRECTLY)
170 /* We need to align the box to utile boundaries, since that's
171 * what load/store operate on.
173 uint32_t orig_width = ptrans->box.width;
174 uint32_t orig_height = ptrans->box.height;
175 uint32_t box_start_x = ptrans->box.x & (utile_w - 1);
176 uint32_t box_start_y = ptrans->box.y & (utile_h - 1);
177 ptrans->box.width += box_start_x;
178 ptrans->box.x -= box_start_x;
179 ptrans->box.height += box_start_y;
180 ptrans->box.y -= box_start_y;
181 ptrans->box.width = align(ptrans->box.width, utile_w);
182 ptrans->box.height = align(ptrans->box.height, utile_h);
184 ptrans->stride = ptrans->box.width * rsc->cpp;
185 ptrans->layer_stride = ptrans->stride;
187 trans->map = malloc(ptrans->stride * ptrans->box.height);
188 if (usage & PIPE_TRANSFER_READ ||
189 ptrans->box.width != orig_width ||
190 ptrans->box.height != orig_height) {
191 vc4_load_tiled_image(trans->map, ptrans->stride,
192 buf + slice->offset +
193 box->z * rsc->cube_map_stride,
195 slice->tiling, rsc->cpp,
199 box_start_x * rsc->cpp +
200 box_start_y * ptrans->stride);
202 ptrans->stride = slice->stride;
203 ptrans->layer_stride = ptrans->stride;
205 return buf + slice->offset +
206 box->y / util_format_get_blockheight(format) * ptrans->stride +
207 box->x / util_format_get_blockwidth(format) * rsc->cpp +
208 box->z * rsc->cube_map_stride;
213 vc4_resource_transfer_unmap(pctx, ptrans);
218 vc4_resource_destroy(struct pipe_screen *pscreen,
219 struct pipe_resource *prsc)
221 struct vc4_resource *rsc = vc4_resource(prsc);
222 pipe_resource_reference(&rsc->shadow_parent, NULL);
223 vc4_bo_unreference(&rsc->bo);
228 vc4_resource_get_handle(struct pipe_screen *pscreen,
229 struct pipe_resource *prsc,
230 struct winsys_handle *handle)
232 struct vc4_resource *rsc = vc4_resource(prsc);
234 return vc4_screen_bo_get_handle(pscreen, rsc->bo, rsc->slices[0].stride,
238 static const struct u_resource_vtbl vc4_resource_vtbl = {
239 .resource_get_handle = vc4_resource_get_handle,
240 .resource_destroy = vc4_resource_destroy,
241 .transfer_map = vc4_resource_transfer_map,
242 .transfer_flush_region = u_default_transfer_flush_region,
243 .transfer_unmap = vc4_resource_transfer_unmap,
244 .transfer_inline_write = u_default_transfer_inline_write,
248 vc4_setup_slices(struct vc4_resource *rsc)
250 struct pipe_resource *prsc = &rsc->base.b;
251 uint32_t width = prsc->width0;
252 uint32_t height = prsc->height0;
253 uint32_t pot_width = util_next_power_of_two(width);
254 uint32_t pot_height = util_next_power_of_two(height);
256 uint32_t utile_w = vc4_utile_width(rsc->cpp);
257 uint32_t utile_h = vc4_utile_height(rsc->cpp);
259 for (int i = prsc->last_level; i >= 0; i--) {
260 struct vc4_resource_slice *slice = &rsc->slices[i];
262 uint32_t level_width, level_height;
265 level_height = height;
267 level_width = u_minify(pot_width, i);
268 level_height = u_minify(pot_height, i);
272 slice->tiling = VC4_TILING_FORMAT_LINEAR;
273 level_width = align(level_width, utile_w);
275 if (vc4_size_is_lt(level_width, level_height,
277 slice->tiling = VC4_TILING_FORMAT_LT;
278 level_width = align(level_width, utile_w);
279 level_height = align(level_height, utile_h);
281 slice->tiling = VC4_TILING_FORMAT_T;
282 level_width = align(level_width,
284 level_height = align(level_height,
289 slice->offset = offset;
290 slice->stride = level_width * rsc->cpp;
291 slice->size = level_height * slice->stride;
293 offset += slice->size;
296 static const char tiling_chars[] = {
297 [VC4_TILING_FORMAT_LINEAR] = 'R',
298 [VC4_TILING_FORMAT_LT] = 'L',
299 [VC4_TILING_FORMAT_T] = 'T'
302 "rsc setup %p (format %d), %dx%d: "
303 "level %d (%c) -> %dx%d, stride %d@0x%08x\n",
304 rsc, rsc->vc4_format,
305 prsc->width0, prsc->height0,
306 i, tiling_chars[slice->tiling],
307 level_width, level_height,
308 slice->stride, slice->offset);
312 /* The texture base pointer that has to point to level 0 doesn't have
313 * intra-page bits, so we have to align it, and thus shift up all the
316 uint32_t page_align_offset = (align(rsc->slices[0].offset, 4096) -
317 rsc->slices[0].offset);
318 if (page_align_offset) {
319 for (int i = 0; i <= prsc->last_level; i++)
320 rsc->slices[i].offset += page_align_offset;
323 /* Cube map faces appear as whole miptrees at a page-aligned offset
324 * from the first face's miptree.
326 if (prsc->target == PIPE_TEXTURE_CUBE) {
327 rsc->cube_map_stride = align(rsc->slices[0].offset +
328 rsc->slices[0].size, 4096);
332 static struct vc4_resource *
333 vc4_resource_setup(struct pipe_screen *pscreen,
334 const struct pipe_resource *tmpl)
336 struct vc4_resource *rsc = CALLOC_STRUCT(vc4_resource);
339 struct pipe_resource *prsc = &rsc->base.b;
343 pipe_reference_init(&prsc->reference, 1);
344 prsc->screen = pscreen;
346 rsc->base.vtbl = &vc4_resource_vtbl;
347 rsc->cpp = util_format_get_blocksize(tmpl->format);
354 static enum vc4_texture_data_type
355 get_resource_texture_format(struct pipe_resource *prsc)
357 struct vc4_resource *rsc = vc4_resource(prsc);
358 uint8_t format = vc4_get_tex_format(prsc->format);
361 assert(format == VC4_TEXTURE_TYPE_RGBA8888);
362 return VC4_TEXTURE_TYPE_RGBA32R;
368 struct pipe_resource *
369 vc4_resource_create(struct pipe_screen *pscreen,
370 const struct pipe_resource *tmpl)
372 struct vc4_resource *rsc = vc4_resource_setup(pscreen, tmpl);
373 struct pipe_resource *prsc = &rsc->base.b;
375 /* We have to make shared be untiled, since we don't have any way to
376 * communicate metadata about tiling currently.
378 if (tmpl->target == PIPE_BUFFER ||
379 (tmpl->bind & (PIPE_BIND_SCANOUT |
382 PIPE_BIND_CURSOR))) {
388 if (tmpl->target != PIPE_BUFFER)
389 rsc->vc4_format = get_resource_texture_format(prsc);
391 vc4_setup_slices(rsc);
392 vc4_resource_bo_alloc(rsc);
398 vc4_resource_destroy(pscreen, prsc);
402 static struct pipe_resource *
403 vc4_resource_from_handle(struct pipe_screen *pscreen,
404 const struct pipe_resource *tmpl,
405 struct winsys_handle *handle)
407 struct vc4_resource *rsc = vc4_resource_setup(pscreen, tmpl);
408 struct pipe_resource *prsc = &rsc->base.b;
409 struct vc4_resource_slice *slice = &rsc->slices[0];
415 rsc->bo = vc4_screen_bo_from_handle(pscreen, handle);
419 if (!using_vc4_simulator)
420 slice->stride = handle->stride;
422 slice->stride = align(prsc->width0 * rsc->cpp, 16);
424 slice->tiling = VC4_TILING_FORMAT_LINEAR;
426 rsc->vc4_format = get_resource_texture_format(prsc);
430 "rsc import %p (format %d), %dx%d: "
431 "level 0 (R) -> stride %d@0x%08x\n",
432 rsc, rsc->vc4_format,
433 prsc->width0, prsc->height0,
434 slice->stride, slice->offset);
440 vc4_resource_destroy(pscreen, prsc);
444 static struct pipe_surface *
445 vc4_create_surface(struct pipe_context *pctx,
446 struct pipe_resource *ptex,
447 const struct pipe_surface *surf_tmpl)
449 struct vc4_surface *surface = CALLOC_STRUCT(vc4_surface);
450 struct vc4_resource *rsc = vc4_resource(ptex);
455 assert(surf_tmpl->u.tex.first_layer == surf_tmpl->u.tex.last_layer);
457 struct pipe_surface *psurf = &surface->base;
458 unsigned level = surf_tmpl->u.tex.level;
460 pipe_reference_init(&psurf->reference, 1);
461 pipe_resource_reference(&psurf->texture, ptex);
463 psurf->context = pctx;
464 psurf->format = surf_tmpl->format;
465 psurf->width = u_minify(ptex->width0, level);
466 psurf->height = u_minify(ptex->height0, level);
467 psurf->u.tex.level = level;
468 psurf->u.tex.first_layer = surf_tmpl->u.tex.first_layer;
469 psurf->u.tex.last_layer = surf_tmpl->u.tex.last_layer;
470 surface->offset = rsc->slices[level].offset;
471 surface->tiling = rsc->slices[level].tiling;
473 return &surface->base;
477 vc4_surface_destroy(struct pipe_context *pctx, struct pipe_surface *psurf)
479 pipe_resource_reference(&psurf->texture, NULL);
483 /** Debug routine to dump the contents of an 8888 surface to the console */
485 vc4_dump_surface(struct pipe_surface *psurf)
490 struct pipe_resource *prsc = psurf->texture;
491 struct vc4_resource *rsc = vc4_resource(prsc);
492 uint32_t *map = vc4_bo_map(rsc->bo);
493 uint32_t stride = rsc->slices[0].stride / 4;
494 uint32_t width = psurf->width;
495 uint32_t height = psurf->height;
496 uint32_t chunk_w = width / 79;
497 uint32_t chunk_h = height / 40;
498 uint32_t found_colors[10];
499 uint32_t num_found_colors = 0;
501 if (rsc->vc4_format != VC4_TEXTURE_TYPE_RGBA32R) {
502 fprintf(stderr, "%s: Unsupported format %s\n",
503 __func__, util_format_short_name(psurf->format));
507 for (int by = 0; by < height; by += chunk_h) {
508 for (int bx = 0; bx < width; bx += chunk_w) {
509 int all_found_color = -1; /* nothing found */
511 for (int y = by; y < MIN2(height, by + chunk_h); y++) {
512 for (int x = bx; x < MIN2(width, bx + chunk_w); x++) {
513 uint32_t pix = map[y * stride + x];
516 for (i = 0; i < num_found_colors; i++) {
517 if (pix == found_colors[i])
520 if (i == num_found_colors &&
522 ARRAY_SIZE(found_colors)) {
523 found_colors[num_found_colors++] = pix;
526 if (i < num_found_colors) {
527 if (all_found_color == -1)
529 else if (i != all_found_color)
530 all_found_color = ARRAY_SIZE(found_colors);
534 /* If all pixels for this chunk have a consistent
535 * value, then print a character for it. Either a
536 * fixed name (particularly common for piglit tests),
537 * or a runtime-generated number.
539 if (all_found_color >= 0 &&
540 all_found_color < ARRAY_SIZE(found_colors)) {
541 static const struct {
553 for (i = 0; i < ARRAY_SIZE(named_colors); i++) {
554 if (named_colors[i].val ==
555 found_colors[all_found_color]) {
556 fprintf(stderr, "%s",
561 /* For unnamed colors, print a number and the
562 * numbers will have values printed at the
565 if (i == ARRAY_SIZE(named_colors)) {
566 fprintf(stderr, "%c",
567 '0' + all_found_color);
570 /* If there's no consistent color, print this.
572 fprintf(stderr, ".");
575 fprintf(stderr, "\n");
578 for (int i = 0; i < num_found_colors; i++) {
579 fprintf(stderr, "color %d: 0x%08x\n", i, found_colors[i]);
584 vc4_flush_resource(struct pipe_context *pctx, struct pipe_resource *resource)
586 /* All calls to flush_resource are followed by a flush of the context,
587 * so there's nothing to do.
592 vc4_update_shadow_baselevel_texture(struct pipe_context *pctx,
593 struct pipe_sampler_view *view)
595 struct vc4_resource *shadow = vc4_resource(view->texture);
596 struct vc4_resource *orig = vc4_resource(shadow->shadow_parent);
599 if (shadow->writes == orig->writes && orig->bo->private)
602 perf_debug("Updating shadow texture due to %s\n",
603 view->u.tex.first_level ? "base level" : "raster layout");
605 for (int i = 0; i <= shadow->base.b.last_level; i++) {
606 unsigned width = u_minify(shadow->base.b.width0, i);
607 unsigned height = u_minify(shadow->base.b.height0, i);
608 struct pipe_blit_info info = {
610 .resource = &shadow->base.b,
620 .format = shadow->base.b.format,
623 .resource = &orig->base.b,
624 .level = view->u.tex.first_level + i,
633 .format = orig->base.b.format,
637 pctx->blit(pctx, &info);
640 shadow->writes = orig->writes;
644 * Converts a 4-byte index buffer to 2 bytes.
646 * Since GLES2 only has support for 1 and 2-byte indices, the hardware doesn't
647 * include 4-byte index support, and we have to shrink it down.
649 * There's no fallback support for when indices end up being larger than 2^16,
650 * though it will at least assertion fail. Also, if the original index data
651 * was in user memory, it would be nice to not have uploaded it to a VBO
652 * before translating.
654 struct pipe_resource *
655 vc4_get_shadow_index_buffer(struct pipe_context *pctx,
656 const struct pipe_index_buffer *ib,
658 uint32_t *shadow_offset)
660 struct vc4_context *vc4 = vc4_context(pctx);
661 struct vc4_resource *orig = vc4_resource(ib->buffer);
662 perf_debug("Fallback conversion for %d uint indices\n", count);
665 struct pipe_resource *shadow_rsc = NULL;
666 u_upload_alloc(vc4->uploader, 0, count * 2,
667 shadow_offset, &shadow_rsc, &data);
668 uint16_t *dst = data;
670 struct pipe_transfer *src_transfer = NULL;
672 if (ib->user_buffer) {
673 src = ib->user_buffer;
675 src = pipe_buffer_map_range(pctx, &orig->base.b,
678 PIPE_TRANSFER_READ, &src_transfer);
681 for (int i = 0; i < count; i++) {
682 uint32_t src_index = src[i];
683 assert(src_index <= 0xffff);
688 pctx->transfer_unmap(pctx, src_transfer);
694 vc4_resource_screen_init(struct pipe_screen *pscreen)
696 pscreen->resource_create = vc4_resource_create;
697 pscreen->resource_from_handle = vc4_resource_from_handle;
698 pscreen->resource_get_handle = u_resource_get_handle_vtbl;
699 pscreen->resource_destroy = u_resource_destroy_vtbl;
703 vc4_resource_context_init(struct pipe_context *pctx)
705 pctx->transfer_map = u_transfer_map_vtbl;
706 pctx->transfer_flush_region = u_transfer_flush_region_vtbl;
707 pctx->transfer_unmap = u_transfer_unmap_vtbl;
708 pctx->transfer_inline_write = u_transfer_inline_write_vtbl;
709 pctx->create_surface = vc4_create_surface;
710 pctx->surface_destroy = vc4_surface_destroy;
711 pctx->resource_copy_region = util_resource_copy_region;
712 pctx->blit = vc4_blit;
713 pctx->flush_resource = vc4_flush_resource;