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