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