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