OSDN Git Service

vc4: Sanity check strides for imported BOs.
[android-x86/external-mesa.git] / src / gallium / drivers / vc4 / vc4_resource.c
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 "util/u_blit.h"
26 #include "util/u_memory.h"
27 #include "util/u_format.h"
28 #include "util/u_inlines.h"
29 #include "util/u_surface.h"
30 #include "util/u_upload_mgr.h"
31
32 #include "vc4_screen.h"
33 #include "vc4_context.h"
34 #include "vc4_resource.h"
35 #include "vc4_tiling.h"
36
37 static bool miptree_debug = false;
38
39 static bool
40 vc4_resource_bo_alloc(struct vc4_resource *rsc)
41 {
42         struct pipe_resource *prsc = &rsc->base.b;
43         struct pipe_screen *pscreen = prsc->screen;
44         struct vc4_bo *bo;
45
46         if (miptree_debug) {
47                 fprintf(stderr, "alloc %p: size %d + offset %d -> %d\n",
48                         rsc,
49                         rsc->slices[0].size,
50                         rsc->slices[0].offset,
51                         rsc->slices[0].offset +
52                         rsc->slices[0].size +
53                         rsc->cube_map_stride * (prsc->array_size - 1));
54         }
55
56         bo = vc4_bo_alloc(vc4_screen(pscreen),
57                           rsc->slices[0].offset +
58                           rsc->slices[0].size +
59                           rsc->cube_map_stride * (prsc->array_size - 1),
60                           "resource");
61         if (bo) {
62                 vc4_bo_unreference(&rsc->bo);
63                 rsc->bo = bo;
64                 return true;
65         } else {
66                 return false;
67         }
68 }
69
70 static void
71 vc4_resource_transfer_unmap(struct pipe_context *pctx,
72                             struct pipe_transfer *ptrans)
73 {
74         struct vc4_context *vc4 = vc4_context(pctx);
75         struct vc4_transfer *trans = vc4_transfer(ptrans);
76
77         if (trans->map) {
78                 struct vc4_resource *rsc;
79                 struct vc4_resource_slice *slice;
80                 if (trans->ss_resource) {
81                         rsc = vc4_resource(trans->ss_resource);
82                         slice = &rsc->slices[0];
83                 } else {
84                         rsc = vc4_resource(ptrans->resource);
85                         slice = &rsc->slices[ptrans->level];
86                 }
87
88                 if (ptrans->usage & PIPE_TRANSFER_WRITE) {
89                         vc4_store_tiled_image(rsc->bo->map + slice->offset +
90                                               ptrans->box.z * rsc->cube_map_stride,
91                                               slice->stride,
92                                               trans->map, ptrans->stride,
93                                               slice->tiling, rsc->cpp,
94                                               &ptrans->box);
95                 }
96                 free(trans->map);
97         }
98
99         if (trans->ss_resource && (ptrans->usage & PIPE_TRANSFER_WRITE)) {
100                 struct pipe_blit_info blit;
101                 memset(&blit, 0, sizeof(blit));
102
103                 blit.src.resource = trans->ss_resource;
104                 blit.src.format = trans->ss_resource->format;
105                 blit.src.box.width = trans->ss_box.width;
106                 blit.src.box.height = trans->ss_box.height;
107                 blit.src.box.depth = 1;
108
109                 blit.dst.resource = ptrans->resource;
110                 blit.dst.format = ptrans->resource->format;
111                 blit.dst.level = ptrans->level;
112                 blit.dst.box = trans->ss_box;
113
114                 blit.mask = util_format_get_mask(ptrans->resource->format);
115                 blit.filter = PIPE_TEX_FILTER_NEAREST;
116
117                 pctx->blit(pctx, &blit);
118                 vc4_flush(pctx);
119
120                 pipe_resource_reference(&trans->ss_resource, NULL);
121         }
122
123         pipe_resource_reference(&ptrans->resource, NULL);
124         util_slab_free(&vc4->transfer_pool, ptrans);
125 }
126
127 static struct pipe_resource *
128 vc4_get_temp_resource(struct pipe_context *pctx,
129                       struct pipe_resource *prsc,
130                       const struct pipe_box *box)
131 {
132         struct pipe_resource temp_setup;
133
134         memset(&temp_setup, 0, sizeof(temp_setup));
135         temp_setup.target = prsc->target;
136         temp_setup.format = prsc->format;
137         temp_setup.width0 = box->width;
138         temp_setup.height0 = box->height;
139         temp_setup.depth0 = 1;
140         temp_setup.array_size = 1;
141
142         return pctx->screen->resource_create(pctx->screen, &temp_setup);
143 }
144
145 static void *
146 vc4_resource_transfer_map(struct pipe_context *pctx,
147                           struct pipe_resource *prsc,
148                           unsigned level, unsigned usage,
149                           const struct pipe_box *box,
150                           struct pipe_transfer **pptrans)
151 {
152         struct vc4_context *vc4 = vc4_context(pctx);
153         struct vc4_resource *rsc = vc4_resource(prsc);
154         struct vc4_transfer *trans;
155         struct pipe_transfer *ptrans;
156         enum pipe_format format = prsc->format;
157         char *buf;
158
159         if (usage & PIPE_TRANSFER_DISCARD_WHOLE_RESOURCE) {
160                 if (vc4_resource_bo_alloc(rsc)) {
161
162                         /* If it might be bound as one of our vertex buffers,
163                          * make sure we re-emit vertex buffer state.
164                          */
165                         if (prsc->bind & PIPE_BIND_VERTEX_BUFFER)
166                                 vc4->dirty |= VC4_DIRTY_VTXBUF;
167                 } else {
168                         /* If we failed to reallocate, flush everything so
169                          * that we don't violate any syncing requirements.
170                          */
171                         vc4_flush(pctx);
172                 }
173         } else if (!(usage & PIPE_TRANSFER_UNSYNCHRONIZED)) {
174                 if (vc4_cl_references_bo(pctx, rsc->bo)) {
175                         if ((usage & PIPE_TRANSFER_DISCARD_RANGE) &&
176                             prsc->last_level == 0 &&
177                             prsc->width0 == box->width &&
178                             prsc->height0 == box->height &&
179                             prsc->depth0 == box->depth &&
180                             vc4_resource_bo_alloc(rsc)) {
181                                 if (prsc->bind & PIPE_BIND_VERTEX_BUFFER)
182                                         vc4->dirty |= VC4_DIRTY_VTXBUF;
183                         } else {
184                                 vc4_flush(pctx);
185                         }
186                 }
187         }
188
189         if (usage & PIPE_TRANSFER_WRITE)
190                 rsc->writes++;
191
192         trans = util_slab_alloc(&vc4->transfer_pool);
193         if (!trans)
194                 return NULL;
195
196         /* XXX: Handle DONTBLOCK, DISCARD_RANGE, PERSISTENT, COHERENT. */
197
198         /* util_slab_alloc() doesn't zero: */
199         memset(trans, 0, sizeof(*trans));
200         ptrans = &trans->base;
201
202         pipe_resource_reference(&ptrans->resource, prsc);
203         ptrans->level = level;
204         ptrans->usage = usage;
205         ptrans->box = *box;
206
207         /* If the resource is multisampled, we need to resolve to single
208          * sample.  This seems like it should be handled at a higher layer.
209          */
210         if (prsc->nr_samples > 1) {
211                 trans->ss_resource = vc4_get_temp_resource(pctx, prsc, box);
212                 if (!trans->ss_resource)
213                         goto fail;
214                 assert(!trans->ss_resource->nr_samples);
215
216                 /* The ptrans->box gets modified for tile alignment, so save
217                  * the original box for unmap time.
218                  */
219                 trans->ss_box = *box;
220
221                 if (usage & PIPE_TRANSFER_READ) {
222                         struct pipe_blit_info blit;
223                         memset(&blit, 0, sizeof(blit));
224
225                         blit.src.resource = ptrans->resource;
226                         blit.src.format = ptrans->resource->format;
227                         blit.src.level = ptrans->level;
228                         blit.src.box = trans->ss_box;
229
230                         blit.dst.resource = trans->ss_resource;
231                         blit.dst.format = trans->ss_resource->format;
232                         blit.dst.box.width = trans->ss_box.width;
233                         blit.dst.box.height = trans->ss_box.height;
234                         blit.dst.box.depth = 1;
235
236                         blit.mask = util_format_get_mask(prsc->format);
237                         blit.filter = PIPE_TEX_FILTER_NEAREST;
238
239                         pctx->blit(pctx, &blit);
240                         vc4_flush(pctx);
241                 }
242
243                 /* The rest of the mapping process should use our temporary. */
244                 prsc = trans->ss_resource;
245                 rsc = vc4_resource(prsc);
246                 ptrans->box.x = 0;
247                 ptrans->box.y = 0;
248                 ptrans->box.z = 0;
249         }
250
251         /* Note that the current kernel implementation is synchronous, so no
252          * need to do syncing stuff here yet.
253          */
254
255         if (usage & PIPE_TRANSFER_UNSYNCHRONIZED)
256                 buf = vc4_bo_map_unsynchronized(rsc->bo);
257         else
258                 buf = vc4_bo_map(rsc->bo);
259         if (!buf) {
260                 fprintf(stderr, "Failed to map bo\n");
261                 goto fail;
262         }
263
264         *pptrans = ptrans;
265
266         struct vc4_resource_slice *slice = &rsc->slices[level];
267         if (rsc->tiled) {
268                 uint32_t utile_w = vc4_utile_width(rsc->cpp);
269                 uint32_t utile_h = vc4_utile_height(rsc->cpp);
270
271                 /* No direct mappings of tiled, since we need to manually
272                  * tile/untile.
273                  */
274                 if (usage & PIPE_TRANSFER_MAP_DIRECTLY)
275                         return NULL;
276
277                 /* We need to align the box to utile boundaries, since that's
278                  * what load/store operate on.
279                  */
280                 uint32_t orig_width = ptrans->box.width;
281                 uint32_t orig_height = ptrans->box.height;
282                 uint32_t box_start_x = ptrans->box.x & (utile_w - 1);
283                 uint32_t box_start_y = ptrans->box.y & (utile_h - 1);
284                 ptrans->box.width += box_start_x;
285                 ptrans->box.x -= box_start_x;
286                 ptrans->box.height += box_start_y;
287                 ptrans->box.y -= box_start_y;
288                 ptrans->box.width = align(ptrans->box.width, utile_w);
289                 ptrans->box.height = align(ptrans->box.height, utile_h);
290
291                 ptrans->stride = ptrans->box.width * rsc->cpp;
292                 ptrans->layer_stride = ptrans->stride;
293
294                 trans->map = malloc(ptrans->stride * ptrans->box.height);
295                 if (usage & PIPE_TRANSFER_READ ||
296                     ptrans->box.width != orig_width ||
297                     ptrans->box.height != orig_height) {
298                         vc4_load_tiled_image(trans->map, ptrans->stride,
299                                              buf + slice->offset +
300                                              ptrans->box.z * rsc->cube_map_stride,
301                                              slice->stride,
302                                              slice->tiling, rsc->cpp,
303                                              &ptrans->box);
304                 }
305                 return (trans->map +
306                         box_start_x * rsc->cpp +
307                         box_start_y * ptrans->stride);
308         } else {
309                 ptrans->stride = slice->stride;
310                 ptrans->layer_stride = ptrans->stride;
311
312                 return buf + slice->offset +
313                         ptrans->box.y / util_format_get_blockheight(format) * ptrans->stride +
314                         ptrans->box.x / util_format_get_blockwidth(format) * rsc->cpp +
315                         ptrans->box.z * rsc->cube_map_stride;
316         }
317
318
319 fail:
320         vc4_resource_transfer_unmap(pctx, ptrans);
321         return NULL;
322 }
323
324 static void
325 vc4_resource_destroy(struct pipe_screen *pscreen,
326                      struct pipe_resource *prsc)
327 {
328         struct vc4_resource *rsc = vc4_resource(prsc);
329         pipe_resource_reference(&rsc->shadow_parent, NULL);
330         vc4_bo_unreference(&rsc->bo);
331         free(rsc);
332 }
333
334 static boolean
335 vc4_resource_get_handle(struct pipe_screen *pscreen,
336                         struct pipe_resource *prsc,
337                         struct winsys_handle *handle)
338 {
339         struct vc4_resource *rsc = vc4_resource(prsc);
340
341         return vc4_screen_bo_get_handle(pscreen, rsc->bo, rsc->slices[0].stride,
342                                         handle);
343 }
344
345 static const struct u_resource_vtbl vc4_resource_vtbl = {
346         .resource_get_handle      = vc4_resource_get_handle,
347         .resource_destroy         = vc4_resource_destroy,
348         .transfer_map             = vc4_resource_transfer_map,
349         .transfer_flush_region    = u_default_transfer_flush_region,
350         .transfer_unmap           = vc4_resource_transfer_unmap,
351         .transfer_inline_write    = u_default_transfer_inline_write,
352 };
353
354 static void
355 vc4_setup_slices(struct vc4_resource *rsc)
356 {
357         struct pipe_resource *prsc = &rsc->base.b;
358         uint32_t width = prsc->width0;
359         uint32_t height = prsc->height0;
360         uint32_t pot_width = util_next_power_of_two(width);
361         uint32_t pot_height = util_next_power_of_two(height);
362         uint32_t offset = 0;
363         uint32_t utile_w = vc4_utile_width(rsc->cpp);
364         uint32_t utile_h = vc4_utile_height(rsc->cpp);
365
366         for (int i = prsc->last_level; i >= 0; i--) {
367                 struct vc4_resource_slice *slice = &rsc->slices[i];
368
369                 uint32_t level_width, level_height;
370                 if (i == 0) {
371                         level_width = width;
372                         level_height = height;
373                 } else {
374                         level_width = u_minify(pot_width, i);
375                         level_height = u_minify(pot_height, i);
376                 }
377
378                 if (!rsc->tiled) {
379                         slice->tiling = VC4_TILING_FORMAT_LINEAR;
380                         if (prsc->nr_samples > 1) {
381                                 /* MSAA (4x) surfaces are stored as raw tile buffer contents. */
382                                 level_width = align(level_width, 32);
383                                 level_height = align(level_height, 32);
384                         } else {
385                                 level_width = align(level_width, utile_w);
386                         }
387                 } else {
388                         if (vc4_size_is_lt(level_width, level_height,
389                                            rsc->cpp)) {
390                                 slice->tiling = VC4_TILING_FORMAT_LT;
391                                 level_width = align(level_width, utile_w);
392                                 level_height = align(level_height, utile_h);
393                         } else {
394                                 slice->tiling = VC4_TILING_FORMAT_T;
395                                 level_width = align(level_width,
396                                                     4 * 2 * utile_w);
397                                 level_height = align(level_height,
398                                                      4 * 2 * utile_h);
399                         }
400                 }
401
402                 slice->offset = offset;
403                 slice->stride = (level_width * rsc->cpp *
404                                  MAX2(prsc->nr_samples, 1));
405                 slice->size = level_height * slice->stride;
406
407                 offset += slice->size;
408
409                 if (miptree_debug) {
410                         static const char tiling_chars[] = {
411                                 [VC4_TILING_FORMAT_LINEAR] = 'R',
412                                 [VC4_TILING_FORMAT_LT] = 'L',
413                                 [VC4_TILING_FORMAT_T] = 'T'
414                         };
415                         fprintf(stderr,
416                                 "rsc setup %p (format %d), %dx%d: "
417                                 "level %d (%c) -> %dx%d, stride %d@0x%08x\n",
418                                 rsc, rsc->vc4_format,
419                                 prsc->width0, prsc->height0,
420                                 i, tiling_chars[slice->tiling],
421                                 level_width, level_height,
422                                 slice->stride, slice->offset);
423                 }
424         }
425
426         /* The texture base pointer that has to point to level 0 doesn't have
427          * intra-page bits, so we have to align it, and thus shift up all the
428          * smaller slices.
429          */
430         uint32_t page_align_offset = (align(rsc->slices[0].offset, 4096) -
431                                       rsc->slices[0].offset);
432         if (page_align_offset) {
433                 for (int i = 0; i <= prsc->last_level; i++)
434                         rsc->slices[i].offset += page_align_offset;
435         }
436
437         /* Cube map faces appear as whole miptrees at a page-aligned offset
438          * from the first face's miptree.
439          */
440         if (prsc->target == PIPE_TEXTURE_CUBE) {
441                 rsc->cube_map_stride = align(rsc->slices[0].offset +
442                                              rsc->slices[0].size, 4096);
443         }
444 }
445
446 static struct vc4_resource *
447 vc4_resource_setup(struct pipe_screen *pscreen,
448                    const struct pipe_resource *tmpl)
449 {
450         struct vc4_resource *rsc = CALLOC_STRUCT(vc4_resource);
451         if (!rsc)
452                 return NULL;
453         struct pipe_resource *prsc = &rsc->base.b;
454
455         *prsc = *tmpl;
456
457         pipe_reference_init(&prsc->reference, 1);
458         prsc->screen = pscreen;
459
460         rsc->base.vtbl = &vc4_resource_vtbl;
461         if (prsc->nr_samples <= 1)
462                 rsc->cpp = util_format_get_blocksize(tmpl->format);
463         else
464                 rsc->cpp = sizeof(uint32_t);
465
466         assert(rsc->cpp);
467
468         return rsc;
469 }
470
471 static enum vc4_texture_data_type
472 get_resource_texture_format(struct pipe_resource *prsc)
473 {
474         struct vc4_resource *rsc = vc4_resource(prsc);
475         uint8_t format = vc4_get_tex_format(prsc->format);
476
477         if (!rsc->tiled) {
478                 if (prsc->nr_samples > 1) {
479                         return ~0;
480                 } else {
481                         assert(format == VC4_TEXTURE_TYPE_RGBA8888);
482                         return VC4_TEXTURE_TYPE_RGBA32R;
483                 }
484         }
485
486         return format;
487 }
488
489 struct pipe_resource *
490 vc4_resource_create(struct pipe_screen *pscreen,
491                     const struct pipe_resource *tmpl)
492 {
493         struct vc4_resource *rsc = vc4_resource_setup(pscreen, tmpl);
494         struct pipe_resource *prsc = &rsc->base.b;
495
496         /* We have to make shared be untiled, since we don't have any way to
497          * communicate metadata about tiling currently.
498          */
499         if (tmpl->target == PIPE_BUFFER ||
500             tmpl->nr_samples > 1 ||
501             (tmpl->bind & (PIPE_BIND_SCANOUT |
502                            PIPE_BIND_LINEAR |
503                            PIPE_BIND_SHARED |
504                            PIPE_BIND_CURSOR))) {
505                 rsc->tiled = false;
506         } else {
507                 rsc->tiled = true;
508         }
509
510         if (tmpl->target != PIPE_BUFFER)
511                 rsc->vc4_format = get_resource_texture_format(prsc);
512
513         vc4_setup_slices(rsc);
514         if (!vc4_resource_bo_alloc(rsc))
515                 goto fail;
516
517         return prsc;
518 fail:
519         vc4_resource_destroy(pscreen, prsc);
520         return NULL;
521 }
522
523 static struct pipe_resource *
524 vc4_resource_from_handle(struct pipe_screen *pscreen,
525                          const struct pipe_resource *tmpl,
526                          struct winsys_handle *handle,
527                          unsigned usage)
528 {
529         struct vc4_resource *rsc = vc4_resource_setup(pscreen, tmpl);
530         struct pipe_resource *prsc = &rsc->base.b;
531         struct vc4_resource_slice *slice = &rsc->slices[0];
532         uint32_t expected_stride = align(prsc->width0 / rsc->cpp,
533                                          vc4_utile_width(rsc->cpp));
534
535         if (!rsc)
536                 return NULL;
537
538         if (handle->stride != expected_stride) {
539                 static bool warned = false;
540                 if (!warned) {
541                         warned = true;
542                         fprintf(stderr,
543                                 "Attempting to import %dx%d %s with "
544                                 "unsupported stride %d instead of %d\n",
545                                 prsc->width0, prsc->height0,
546                                 util_format_short_name(prsc->format),
547                                 handle->stride,
548                                 expected_stride);
549                 }
550                 return NULL;
551         }
552
553         rsc->tiled = false;
554         rsc->bo = vc4_screen_bo_from_handle(pscreen, handle);
555         if (!rsc->bo)
556                 goto fail;
557
558         slice->stride = handle->stride;
559         slice->tiling = VC4_TILING_FORMAT_LINEAR;
560
561         rsc->vc4_format = get_resource_texture_format(prsc);
562
563         if (miptree_debug) {
564                 fprintf(stderr,
565                         "rsc import %p (format %d), %dx%d: "
566                         "level 0 (R) -> stride %d@0x%08x\n",
567                         rsc, rsc->vc4_format,
568                         prsc->width0, prsc->height0,
569                         slice->stride, slice->offset);
570         }
571
572         return prsc;
573
574 fail:
575         vc4_resource_destroy(pscreen, prsc);
576         return NULL;
577 }
578
579 static struct pipe_surface *
580 vc4_create_surface(struct pipe_context *pctx,
581                    struct pipe_resource *ptex,
582                    const struct pipe_surface *surf_tmpl)
583 {
584         struct vc4_surface *surface = CALLOC_STRUCT(vc4_surface);
585         struct vc4_resource *rsc = vc4_resource(ptex);
586
587         if (!surface)
588                 return NULL;
589
590         assert(surf_tmpl->u.tex.first_layer == surf_tmpl->u.tex.last_layer);
591
592         struct pipe_surface *psurf = &surface->base;
593         unsigned level = surf_tmpl->u.tex.level;
594
595         pipe_reference_init(&psurf->reference, 1);
596         pipe_resource_reference(&psurf->texture, ptex);
597
598         psurf->context = pctx;
599         psurf->format = surf_tmpl->format;
600         psurf->width = u_minify(ptex->width0, level);
601         psurf->height = u_minify(ptex->height0, level);
602         psurf->u.tex.level = level;
603         psurf->u.tex.first_layer = surf_tmpl->u.tex.first_layer;
604         psurf->u.tex.last_layer = surf_tmpl->u.tex.last_layer;
605         surface->offset = rsc->slices[level].offset;
606         surface->tiling = rsc->slices[level].tiling;
607
608         return &surface->base;
609 }
610
611 static void
612 vc4_surface_destroy(struct pipe_context *pctx, struct pipe_surface *psurf)
613 {
614         pipe_resource_reference(&psurf->texture, NULL);
615         FREE(psurf);
616 }
617
618 static void
619 vc4_dump_surface_non_msaa(struct pipe_surface *psurf)
620 {
621         struct pipe_resource *prsc = psurf->texture;
622         struct vc4_resource *rsc = vc4_resource(prsc);
623         uint32_t *map = vc4_bo_map(rsc->bo);
624         uint32_t stride = rsc->slices[0].stride / 4;
625         uint32_t width = psurf->width;
626         uint32_t height = psurf->height;
627         uint32_t chunk_w = width / 79;
628         uint32_t chunk_h = height / 40;
629         uint32_t found_colors[10];
630         uint32_t num_found_colors = 0;
631
632         if (rsc->vc4_format != VC4_TEXTURE_TYPE_RGBA32R) {
633                 fprintf(stderr, "%s: Unsupported format %s\n",
634                         __func__, util_format_short_name(psurf->format));
635                 return;
636         }
637
638         for (int by = 0; by < height; by += chunk_h) {
639                 for (int bx = 0; bx < width; bx += chunk_w) {
640                         int all_found_color = -1; /* nothing found */
641
642                         for (int y = by; y < MIN2(height, by + chunk_h); y++) {
643                                 for (int x = bx; x < MIN2(width, bx + chunk_w); x++) {
644                                         uint32_t pix = map[y * stride + x];
645
646                                         int i;
647                                         for (i = 0; i < num_found_colors; i++) {
648                                                 if (pix == found_colors[i])
649                                                         break;
650                                         }
651                                         if (i == num_found_colors &&
652                                             num_found_colors <
653                                             ARRAY_SIZE(found_colors)) {
654                                                 found_colors[num_found_colors++] = pix;
655                                         }
656
657                                         if (i < num_found_colors) {
658                                                 if (all_found_color == -1)
659                                                         all_found_color = i;
660                                                 else if (i != all_found_color)
661                                                         all_found_color = ARRAY_SIZE(found_colors);
662                                         }
663                                 }
664                         }
665                         /* If all pixels for this chunk have a consistent
666                          * value, then print a character for it.  Either a
667                          * fixed name (particularly common for piglit tests),
668                          * or a runtime-generated number.
669                          */
670                         if (all_found_color >= 0 &&
671                             all_found_color < ARRAY_SIZE(found_colors)) {
672                                 static const struct {
673                                         uint32_t val;
674                                         const char *c;
675                                 } named_colors[] = {
676                                         { 0xff000000, "█" },
677                                         { 0x00000000, "█" },
678                                         { 0xffff0000, "r" },
679                                         { 0xff00ff00, "g" },
680                                         { 0xff0000ff, "b" },
681                                         { 0xffffffff, "w" },
682                                 };
683                                 int i;
684                                 for (i = 0; i < ARRAY_SIZE(named_colors); i++) {
685                                         if (named_colors[i].val ==
686                                             found_colors[all_found_color]) {
687                                                 fprintf(stderr, "%s",
688                                                         named_colors[i].c);
689                                                 break;
690                                         }
691                                 }
692                                 /* For unnamed colors, print a number and the
693                                  * numbers will have values printed at the
694                                  * end.
695                                  */
696                                 if (i == ARRAY_SIZE(named_colors)) {
697                                         fprintf(stderr, "%c",
698                                                 '0' + all_found_color);
699                                 }
700                         } else {
701                                 /* If there's no consistent color, print this.
702                                  */
703                                 fprintf(stderr, ".");
704                         }
705                 }
706                 fprintf(stderr, "\n");
707         }
708
709         for (int i = 0; i < num_found_colors; i++) {
710                 fprintf(stderr, "color %d: 0x%08x\n", i, found_colors[i]);
711         }
712 }
713
714 static uint32_t
715 vc4_surface_msaa_get_sample(struct pipe_surface *psurf,
716                             uint32_t x, uint32_t y, uint32_t sample)
717 {
718         struct pipe_resource *prsc = psurf->texture;
719         struct vc4_resource *rsc = vc4_resource(prsc);
720         uint32_t tile_w = 32, tile_h = 32;
721         uint32_t tiles_w = DIV_ROUND_UP(psurf->width, 32);
722
723         uint32_t tile_x = x / tile_w;
724         uint32_t tile_y = y / tile_h;
725         uint32_t *tile = (vc4_bo_map(rsc->bo) +
726                           VC4_TILE_BUFFER_SIZE * (tile_y * tiles_w + tile_x));
727         uint32_t subtile_x = x % tile_w;
728         uint32_t subtile_y = y % tile_h;
729
730         uint32_t quad_samples = VC4_MAX_SAMPLES * 4;
731         uint32_t tile_stride = quad_samples * tile_w / 2;
732
733         return *((uint32_t *)tile +
734                  (subtile_y >> 1) * tile_stride +
735                  (subtile_x >> 1) * quad_samples +
736                  ((subtile_y & 1) << 1) +
737                  (subtile_x & 1) +
738                  sample);
739 }
740
741 static void
742 vc4_dump_surface_msaa_char(struct pipe_surface *psurf,
743                            uint32_t start_x, uint32_t start_y,
744                            uint32_t w, uint32_t h)
745 {
746         bool all_same_color = true;
747         uint32_t all_pix = 0;
748
749         for (int y = start_y; y < start_y + h; y++) {
750                 for (int x = start_x; x < start_x + w; x++) {
751                         for (int s = 0; s < VC4_MAX_SAMPLES; s++) {
752                                 uint32_t pix = vc4_surface_msaa_get_sample(psurf,
753                                                                            x, y,
754                                                                            s);
755                                 if (x == start_x && y == start_y)
756                                         all_pix = pix;
757                                 else if (all_pix != pix)
758                                         all_same_color = false;
759                         }
760                 }
761         }
762         if (all_same_color) {
763                 static const struct {
764                         uint32_t val;
765                         const char *c;
766                 } named_colors[] = {
767                         { 0xff000000, "█" },
768                         { 0x00000000, "█" },
769                         { 0xffff0000, "r" },
770                         { 0xff00ff00, "g" },
771                         { 0xff0000ff, "b" },
772                         { 0xffffffff, "w" },
773                 };
774                 int i;
775                 for (i = 0; i < ARRAY_SIZE(named_colors); i++) {
776                         if (named_colors[i].val == all_pix) {
777                                 fprintf(stderr, "%s",
778                                         named_colors[i].c);
779                                 return;
780                         }
781                 }
782                 fprintf(stderr, "x");
783         } else {
784                 fprintf(stderr, ".");
785         }
786 }
787
788 static void
789 vc4_dump_surface_msaa(struct pipe_surface *psurf)
790 {
791         uint32_t tile_w = 32, tile_h = 32;
792         uint32_t tiles_w = DIV_ROUND_UP(psurf->width, tile_w);
793         uint32_t tiles_h = DIV_ROUND_UP(psurf->height, tile_h);
794         uint32_t char_w = 140, char_h = 60;
795         uint32_t char_w_per_tile = char_w / tiles_w - 1;
796         uint32_t char_h_per_tile = char_h / tiles_h - 1;
797         uint32_t found_colors[10];
798         uint32_t num_found_colors = 0;
799
800         fprintf(stderr, "Surface: %dx%d (%dx MSAA)\n",
801                 psurf->width, psurf->height, psurf->texture->nr_samples);
802
803         for (int x = 0; x < (char_w_per_tile + 1) * tiles_w; x++)
804                 fprintf(stderr, "-");
805         fprintf(stderr, "\n");
806
807         for (int ty = 0; ty < psurf->height; ty += tile_h) {
808                 for (int y = 0; y < char_h_per_tile; y++) {
809
810                         for (int tx = 0; tx < psurf->width; tx += tile_w) {
811                                 for (int x = 0; x < char_w_per_tile; x++) {
812                                         uint32_t bx1 = (x * tile_w /
813                                                         char_w_per_tile);
814                                         uint32_t bx2 = ((x + 1) * tile_w /
815                                                         char_w_per_tile);
816                                         uint32_t by1 = (y * tile_h /
817                                                         char_h_per_tile);
818                                         uint32_t by2 = ((y + 1) * tile_h /
819                                                         char_h_per_tile);
820
821                                         vc4_dump_surface_msaa_char(psurf,
822                                                                    tx + bx1,
823                                                                    ty + by1,
824                                                                    bx2 - bx1,
825                                                                    by2 - by1);
826                                 }
827                                 fprintf(stderr, "|");
828                         }
829                         fprintf(stderr, "\n");
830                 }
831
832                 for (int x = 0; x < (char_w_per_tile + 1) * tiles_w; x++)
833                         fprintf(stderr, "-");
834                 fprintf(stderr, "\n");
835         }
836
837         for (int i = 0; i < num_found_colors; i++) {
838                 fprintf(stderr, "color %d: 0x%08x\n", i, found_colors[i]);
839         }
840 }
841
842 /** Debug routine to dump the contents of an 8888 surface to the console */
843 void
844 vc4_dump_surface(struct pipe_surface *psurf)
845 {
846         if (!psurf)
847                 return;
848
849         if (psurf->texture->nr_samples > 1)
850                 vc4_dump_surface_msaa(psurf);
851         else
852                 vc4_dump_surface_non_msaa(psurf);
853 }
854
855 static void
856 vc4_flush_resource(struct pipe_context *pctx, struct pipe_resource *resource)
857 {
858         /* All calls to flush_resource are followed by a flush of the context,
859          * so there's nothing to do.
860          */
861 }
862
863 void
864 vc4_update_shadow_baselevel_texture(struct pipe_context *pctx,
865                                     struct pipe_sampler_view *view)
866 {
867         struct vc4_resource *shadow = vc4_resource(view->texture);
868         struct vc4_resource *orig = vc4_resource(shadow->shadow_parent);
869         assert(orig);
870
871         if (shadow->writes == orig->writes && orig->bo->private)
872                 return;
873
874         perf_debug("Updating shadow texture due to %s\n",
875                    view->u.tex.first_level ? "base level" : "raster layout");
876
877         for (int i = 0; i <= shadow->base.b.last_level; i++) {
878                 unsigned width = u_minify(shadow->base.b.width0, i);
879                 unsigned height = u_minify(shadow->base.b.height0, i);
880                 struct pipe_blit_info info = {
881                         .dst = {
882                                 .resource = &shadow->base.b,
883                                 .level = i,
884                                 .box = {
885                                         .x = 0,
886                                         .y = 0,
887                                         .z = 0,
888                                         .width = width,
889                                         .height = height,
890                                         .depth = 1,
891                                 },
892                                 .format = shadow->base.b.format,
893                         },
894                         .src = {
895                                 .resource = &orig->base.b,
896                                 .level = view->u.tex.first_level + i,
897                                 .box = {
898                                         .x = 0,
899                                         .y = 0,
900                                         .z = 0,
901                                         .width = width,
902                                         .height = height,
903                                         .depth = 1,
904                                 },
905                                 .format = orig->base.b.format,
906                         },
907                         .mask = ~0,
908                 };
909                 pctx->blit(pctx, &info);
910         }
911
912         shadow->writes = orig->writes;
913 }
914
915 /**
916  * Converts a 4-byte index buffer to 2 bytes.
917  *
918  * Since GLES2 only has support for 1 and 2-byte indices, the hardware doesn't
919  * include 4-byte index support, and we have to shrink it down.
920  *
921  * There's no fallback support for when indices end up being larger than 2^16,
922  * though it will at least assertion fail.  Also, if the original index data
923  * was in user memory, it would be nice to not have uploaded it to a VBO
924  * before translating.
925  */
926 struct pipe_resource *
927 vc4_get_shadow_index_buffer(struct pipe_context *pctx,
928                             const struct pipe_index_buffer *ib,
929                             uint32_t count,
930                             uint32_t *shadow_offset)
931 {
932         struct vc4_context *vc4 = vc4_context(pctx);
933         struct vc4_resource *orig = vc4_resource(ib->buffer);
934         perf_debug("Fallback conversion for %d uint indices\n", count);
935
936         void *data;
937         struct pipe_resource *shadow_rsc = NULL;
938         u_upload_alloc(vc4->uploader, 0, count * 2, 4,
939                        shadow_offset, &shadow_rsc, &data);
940         uint16_t *dst = data;
941
942         struct pipe_transfer *src_transfer = NULL;
943         const uint32_t *src;
944         if (ib->user_buffer) {
945                 src = ib->user_buffer;
946         } else {
947                 src = pipe_buffer_map_range(pctx, &orig->base.b,
948                                             ib->offset,
949                                             count * 4,
950                                             PIPE_TRANSFER_READ, &src_transfer);
951         }
952
953         for (int i = 0; i < count; i++) {
954                 uint32_t src_index = src[i];
955                 assert(src_index <= 0xffff);
956                 dst[i] = src_index;
957         }
958
959         if (src_transfer)
960                 pctx->transfer_unmap(pctx, src_transfer);
961
962         return shadow_rsc;
963 }
964
965 void
966 vc4_resource_screen_init(struct pipe_screen *pscreen)
967 {
968         pscreen->resource_create = vc4_resource_create;
969         pscreen->resource_from_handle = vc4_resource_from_handle;
970         pscreen->resource_get_handle = u_resource_get_handle_vtbl;
971         pscreen->resource_destroy = u_resource_destroy_vtbl;
972 }
973
974 void
975 vc4_resource_context_init(struct pipe_context *pctx)
976 {
977         pctx->transfer_map = u_transfer_map_vtbl;
978         pctx->transfer_flush_region = u_transfer_flush_region_vtbl;
979         pctx->transfer_unmap = u_transfer_unmap_vtbl;
980         pctx->transfer_inline_write = u_transfer_inline_write_vtbl;
981         pctx->create_surface = vc4_create_surface;
982         pctx->surface_destroy = vc4_surface_destroy;
983         pctx->resource_copy_region = util_resource_copy_region;
984         pctx->blit = vc4_blit;
985         pctx->flush_resource = vc4_flush_resource;
986 }